blob: 3a585ffa9c290cda82fd3f147fbcddbc91745c8d [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
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000040 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 *
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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
106#ifdef FEAT_FOLDING
107static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108#endif
109
110/*
111 * Buffer for one screen line (characters and attributes).
112 */
113static schar_T *current_ScreenLine;
114
115static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000116static 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 +0000117#ifdef FEAT_FOLDING
118static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000122static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124#ifdef FEAT_RIGHTLEFT
125static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127#else
128static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#ifdef FEAT_VERTSPLIT
132static void draw_vsep_win __ARGS((win_T *wp, int row));
133#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
135static void redraw_custum_statusline __ARGS((win_T *wp));
136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000138#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void start_search_hl __ARGS((void));
140static void end_search_hl __ARGS((void));
141static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143#endif
144static void screen_start_highlight __ARGS((int attr));
145static void screen_char __ARGS((unsigned off, int row, int col));
146#ifdef FEAT_MBYTE
147static void screen_char_2 __ARGS((unsigned off, int row, int col));
148#endif
149static void screenclear2 __ARGS((void));
150static void lineclear __ARGS((unsigned off, int width));
151static void lineinvalid __ARGS((unsigned off, int width));
152#ifdef FEAT_VERTSPLIT
153static void linecopy __ARGS((int to, int from, win_T *wp));
154static void redraw_block __ARGS((int row, int end, win_T *wp));
155#endif
156static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000160static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163static int fillchar_status __ARGS((int *attr, int is_curwin));
164#endif
165#ifdef FEAT_VERTSPLIT
166static int fillchar_vsep __ARGS((int *attr));
167#endif
168#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000169static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#endif
171#ifdef FEAT_CMDL_INFO
172static void win_redr_ruler __ARGS((win_T *wp, int always));
173#endif
174
175#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176/* Ugly global: overrule attribute used by screen_char() */
177static int screen_char_attr = 0;
178#endif
179
180/*
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
184 */
185 void
186redraw_later(type)
187 int type;
188{
189 redraw_win_later(curwin, type);
190}
191
192 void
193redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
196{
197 if (wp->w_redr_type < type)
198 {
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
204 }
205}
206
207/*
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
210 */
211 void
212redraw_later_clear()
213{
214 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000215#ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221#endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224}
225
226/*
227 * Mark all windows to be redrawn later.
228 */
229 void
230redraw_all_later(type)
231 int type;
232{
233 win_T *wp;
234
235 FOR_ALL_WINDOWS(wp)
236 {
237 redraw_win_later(wp, type);
238 }
239}
240
241/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000242 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 */
244 void
245redraw_curbuf_later(type)
246 int type;
247{
248 redraw_buf_later(curbuf, type);
249}
250
251 void
252redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
255{
256 win_T *wp;
257
258 FOR_ALL_WINDOWS(wp)
259 {
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
262 }
263}
264
265/*
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
272 */
273/*ARGSUSED*/
274 void
275redrawWinline(lnum, invalid)
276 linenr_T lnum;
277 int invalid; /* window line height is invalid now */
278{
279#ifdef FEAT_FOLDING
280 int i;
281#endif
282
283 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
284 curwin->w_redraw_top = lnum;
285 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
286 curwin->w_redraw_bot = lnum;
287 redraw_later(VALID);
288
289#ifdef FEAT_FOLDING
290 if (invalid)
291 {
292 /* A w_lines[] entry for this lnum has become invalid. */
293 i = find_wl_entry(curwin, lnum);
294 if (i >= 0)
295 curwin->w_lines[i].wl_valid = FALSE;
296 }
297#endif
298}
299
300/*
301 * update all windows that are editing the current buffer
302 */
303 void
304update_curbuf(type)
305 int type;
306{
307 redraw_curbuf_later(type);
308 update_screen(type);
309}
310
311/*
312 * update_screen()
313 *
314 * Based on the current value of curwin->w_topline, transfer a screenfull
315 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
316 */
317 void
318update_screen(type)
319 int type;
320{
321 win_T *wp;
322 static int did_intro = FALSE;
323#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
324 int did_one;
325#endif
326
327 if (!screen_valid(TRUE))
328 return;
329
330 if (must_redraw)
331 {
332 if (type < must_redraw) /* use maximal type */
333 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000334
335 /* must_redraw is reset here, so that when we run into some weird
336 * reason to redraw while busy redrawing (e.g., asynchronous
337 * scrolling), or update_topline() in win_update() will cause a
338 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 must_redraw = 0;
340 }
341
342 /* Need to update w_lines[]. */
343 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
344 type = NOT_VALID;
345
346 if (!redrawing())
347 {
348 redraw_later(type); /* remember type for next time */
349 must_redraw = type;
350 if (type > INVERTED_ALL)
351 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
352 return;
353 }
354
355 updating_screen = TRUE;
356#ifdef FEAT_SYN_HL
357 ++display_tick; /* let syntax code know we're in a next round of
358 * display updating */
359#endif
360
361 /*
362 * if the screen was scrolled up when displaying a message, scroll it down
363 */
364 if (msg_scrolled)
365 {
366 clear_cmdline = TRUE;
367 if (msg_scrolled > Rows - 5) /* clearing is faster */
368 type = CLEAR;
369 else if (type != CLEAR)
370 {
371 check_for_delay(FALSE);
372 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
373 type = CLEAR;
374 FOR_ALL_WINDOWS(wp)
375 {
376 if (W_WINROW(wp) < msg_scrolled)
377 {
378 if (W_WINROW(wp) + wp->w_height > msg_scrolled
379 && wp->w_redr_type < REDRAW_TOP
380 && wp->w_lines_valid > 0
381 && wp->w_topline == wp->w_lines[0].wl_lnum)
382 {
383 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
384 wp->w_redr_type = REDRAW_TOP;
385 }
386 else
387 {
388 wp->w_redr_type = NOT_VALID;
389#ifdef FEAT_WINDOWS
390 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
391 <= msg_scrolled)
392 wp->w_redr_status = TRUE;
393#endif
394 }
395 }
396 }
397 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000398#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000399 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 }
402 msg_scrolled = 0;
403 need_wait_return = FALSE;
404 }
405
406 /* reset cmdline_row now (may have been changed temporarily) */
407 compute_cmdrow();
408
409 /* Check for changed highlighting */
410 if (need_highlight_changed)
411 highlight_changed();
412
413 if (type == CLEAR) /* first clear screen */
414 {
415 screenclear(); /* will reset clear_cmdline */
416 type = NOT_VALID;
417 }
418
419 if (clear_cmdline) /* going to clear cmdline (done below) */
420 check_for_delay(FALSE);
421
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000422#ifdef FEAT_LINEBREAK
423 /* Force redraw when width of 'number' column changes. */
424 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000425 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000426 curwin->w_redr_type = NOT_VALID;
427#endif
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 /*
430 * Only start redrawing if there is really something to do.
431 */
432 if (type == INVERTED)
433 update_curswant();
434 if (curwin->w_redr_type < type
435 && !((type == VALID
436 && curwin->w_lines[0].wl_valid
437#ifdef FEAT_DIFF
438 && curwin->w_topfill == curwin->w_old_topfill
439 && curwin->w_botfill == curwin->w_old_botfill
440#endif
441 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
442#ifdef FEAT_VISUAL
443 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000444 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
446 && curwin->w_old_visual_mode == VIsual_mode
447 && (curwin->w_valid & VALID_VIRTCOL)
448 && curwin->w_old_curswant == curwin->w_curswant)
449#endif
450 ))
451 curwin->w_redr_type = type;
452
Bram Moolenaar5a305422006-04-28 22:38:25 +0000453#ifdef FEAT_WINDOWS
454 /* Redraw the tab pages line if needed. */
455 if (redraw_tabline || type >= NOT_VALID)
456 draw_tabline();
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#ifdef FEAT_SYN_HL
460 /*
461 * Correct stored syntax highlighting info for changes in each displayed
462 * buffer. Each buffer must only be done once.
463 */
464 FOR_ALL_WINDOWS(wp)
465 {
466 if (wp->w_buffer->b_mod_set)
467 {
468# ifdef FEAT_WINDOWS
469 win_T *wwp;
470
471 /* Check if we already did this buffer. */
472 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
473 if (wwp->w_buffer == wp->w_buffer)
474 break;
475# endif
476 if (
477# ifdef FEAT_WINDOWS
478 wwp == wp &&
479# endif
480 syntax_present(wp->w_buffer))
481 syn_stack_apply_changes(wp->w_buffer);
482 }
483 }
484#endif
485
486 /*
487 * Go from top to bottom through the windows, redrawing the ones that need
488 * it.
489 */
490#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
491 did_one = FALSE;
492#endif
493#ifdef FEAT_SEARCH_EXTRA
494 search_hl.rm.regprog = NULL;
495#endif
496 FOR_ALL_WINDOWS(wp)
497 {
498 if (wp->w_redr_type != 0)
499 {
500 cursor_off();
501#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
502 if (!did_one)
503 {
504 did_one = TRUE;
505# ifdef FEAT_SEARCH_EXTRA
506 start_search_hl();
507# endif
508# ifdef FEAT_CLIPBOARD
509 /* When Visual area changed, may have to update selection. */
510 if (clip_star.available && clip_isautosel())
511 clip_update_selection();
512# endif
513#ifdef FEAT_GUI
514 /* Remove the cursor before starting to do anything, because
515 * scrolling may make it difficult to redraw the text under
516 * it. */
517 if (gui.in_use)
518 gui_undraw_cursor();
519#endif
520 }
521#endif
522 win_update(wp);
523 }
524
525#ifdef FEAT_WINDOWS
526 /* redraw status line after the window to minimize cursor movement */
527 if (wp->w_redr_status)
528 {
529 cursor_off();
530 win_redr_status(wp);
531 }
532#endif
533 }
534#if defined(FEAT_SEARCH_EXTRA)
535 end_search_hl();
536#endif
537
538#ifdef FEAT_WINDOWS
539 /* Reset b_mod_set flags. Going through all windows is probably faster
540 * than going through all buffers (there could be many buffers). */
541 for (wp = firstwin; wp != NULL; wp = wp->w_next)
542 wp->w_buffer->b_mod_set = FALSE;
543#else
544 curbuf->b_mod_set = FALSE;
545#endif
546
547 updating_screen = FALSE;
548#ifdef FEAT_GUI
549 gui_may_resize_shell();
550#endif
551
552 /* Clear or redraw the command line. Done last, because scrolling may
553 * mess up the command line. */
554 if (clear_cmdline || redraw_cmdline)
555 showmode();
556
557 /* May put up an introductory message when not editing a file */
558 if (!did_intro && bufempty()
559 && curbuf->b_fname == NULL
560#ifdef FEAT_WINDOWS
561 && firstwin->w_next == NULL
562#endif
563 && vim_strchr(p_shm, SHM_INTRO) == NULL)
564 intro_message(FALSE);
565 did_intro = TRUE;
566
567#ifdef FEAT_GUI
568 /* Redraw the cursor and update the scrollbars when all screen updating is
569 * done. */
570 if (gui.in_use)
571 {
572 out_flush(); /* required before updating the cursor */
573 if (did_one)
574 gui_update_cursor(FALSE, FALSE);
575 gui_update_scrollbars(FALSE);
576 }
577#endif
578}
579
580#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
581static void update_prepare __ARGS((void));
582static void update_finish __ARGS((void));
583
584/*
585 * Prepare for updating one or more windows.
586 */
587 static void
588update_prepare()
589{
590 cursor_off();
591 updating_screen = TRUE;
592#ifdef FEAT_GUI
593 /* Remove the cursor before starting to do anything, because scrolling may
594 * make it difficult to redraw the text under it. */
595 if (gui.in_use)
596 gui_undraw_cursor();
597#endif
598#ifdef FEAT_SEARCH_EXTRA
599 start_search_hl();
600#endif
601}
602
603/*
604 * Finish updating one or more windows.
605 */
606 static void
607update_finish()
608{
609 if (redraw_cmdline)
610 showmode();
611
612# ifdef FEAT_SEARCH_EXTRA
613 end_search_hl();
614# endif
615
616 updating_screen = FALSE;
617
618# ifdef FEAT_GUI
619 gui_may_resize_shell();
620
621 /* Redraw the cursor and update the scrollbars when all screen updating is
622 * done. */
623 if (gui.in_use)
624 {
625 out_flush(); /* required before updating the cursor */
626 gui_update_cursor(FALSE, FALSE);
627 gui_update_scrollbars(FALSE);
628 }
629# endif
630}
631#endif
632
633#if defined(FEAT_SIGNS) || defined(PROTO)
634 void
635update_debug_sign(buf, lnum)
636 buf_T *buf;
637 linenr_T lnum;
638{
639 win_T *wp;
640 int doit = FALSE;
641
642# ifdef FEAT_FOLDING
643 win_foldinfo.fi_level = 0;
644# endif
645
646 /* update/delete a specific mark */
647 FOR_ALL_WINDOWS(wp)
648 {
649 if (buf != NULL && lnum > 0)
650 {
651 if (wp->w_buffer == buf && lnum >= wp->w_topline
652 && lnum < wp->w_botline)
653 {
654 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
655 wp->w_redraw_top = lnum;
656 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
657 wp->w_redraw_bot = lnum;
658 redraw_win_later(wp, VALID);
659 }
660 }
661 else
662 redraw_win_later(wp, VALID);
663 if (wp->w_redr_type != 0)
664 doit = TRUE;
665 }
666
667 if (!doit)
668 return;
669
670 /* update all windows that need updating */
671 update_prepare();
672
673# ifdef FEAT_WINDOWS
674 for (wp = firstwin; wp; wp = wp->w_next)
675 {
676 if (wp->w_redr_type != 0)
677 win_update(wp);
678 if (wp->w_redr_status)
679 win_redr_status(wp);
680 }
681# else
682 if (curwin->w_redr_type != 0)
683 win_update(curwin);
684# endif
685
686 update_finish();
687}
688#endif
689
690
691#if defined(FEAT_GUI) || defined(PROTO)
692/*
693 * Update a single window, its status line and maybe the command line msg.
694 * Used for the GUI scrollbar.
695 */
696 void
697updateWindow(wp)
698 win_T *wp;
699{
700 update_prepare();
701
702#ifdef FEAT_CLIPBOARD
703 /* When Visual area changed, may have to update selection. */
704 if (clip_star.available && clip_isautosel())
705 clip_update_selection();
706#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000711 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000712 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000713 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (wp->w_redr_status
716# ifdef FEAT_CMDL_INFO
717 || p_ru
718# endif
719# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000720 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721# endif
722 )
723 win_redr_status(wp);
724#endif
725
726 update_finish();
727}
728#endif
729
730/*
731 * Update a single window.
732 *
733 * This may cause the windows below it also to be redrawn (when clearing the
734 * screen or scrolling lines).
735 *
736 * How the window is redrawn depends on wp->w_redr_type. Each type also
737 * implies the one below it.
738 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000739 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
741 * INVERTED redraw the changed part of the Visual area
742 * INVERTED_ALL redraw the whole Visual area
743 * VALID 1. scroll up/down to adjust for a changed w_topline
744 * 2. update lines at the top when scrolled down
745 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000746 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * b_mod_top and b_mod_bot.
748 * - if wp->w_redraw_top non-zero, redraw lines between
749 * wp->w_redraw_top and wp->w_redr_bot.
750 * - continue redrawing when syntax status is invalid.
751 * 4. if scrolled up, update lines at the bottom.
752 * This results in three areas that may need updating:
753 * top: from first row to top_end (when scrolled down)
754 * mid: from mid_start to mid_end (update inversion or changed text)
755 * bot: from bot_start to last row (when scrolled up)
756 */
757 static void
758win_update(wp)
759 win_T *wp;
760{
761 buf_T *buf = wp->w_buffer;
762 int type;
763 int top_end = 0; /* Below last row of the top area that needs
764 updating. 0 when no top area updating. */
765 int mid_start = 999;/* first row of the mid area that needs
766 updating. 999 when no mid area updating. */
767 int mid_end = 0; /* Below last row of the mid area that needs
768 updating. 0 when no mid area updating. */
769 int bot_start = 999;/* first row of the bot area that needs
770 updating. 999 when no bot area updating */
771#ifdef FEAT_VISUAL
772 int scrolled_down = FALSE; /* TRUE when scrolled down when
773 w_topline got smaller a bit */
774#endif
775#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000776 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 int top_to_mod = FALSE; /* redraw above mod_top */
778#endif
779
780 int row; /* current window row to display */
781 linenr_T lnum; /* current buffer lnum to display */
782 int idx; /* current index in w_lines[] */
783 int srow; /* starting row of the current line */
784
785 int eof = FALSE; /* if TRUE, we hit the end of the file */
786 int didline = FALSE; /* if TRUE, we finished the last line */
787 int i;
788 long j;
789 static int recursive = FALSE; /* being called recursively */
790 int old_botline = wp->w_botline;
791#ifdef FEAT_FOLDING
792 long fold_count;
793#endif
794#ifdef FEAT_SYN_HL
795 /* remember what happened to the previous line, to know if
796 * check_visual_highlight() can be used */
797#define DID_NONE 1 /* didn't update a line */
798#define DID_LINE 2 /* updated a normal line */
799#define DID_FOLD 3 /* updated a folded line */
800 int did_update = DID_NONE;
801 linenr_T syntax_last_parsed = 0; /* last parsed text line */
802#endif
803 linenr_T mod_top = 0;
804 linenr_T mod_bot = 0;
805#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
806 int save_got_int;
807#endif
808
809 type = wp->w_redr_type;
810
811 if (type == NOT_VALID)
812 {
813#ifdef FEAT_WINDOWS
814 wp->w_redr_status = TRUE;
815#endif
816 wp->w_lines_valid = 0;
817 }
818
819 /* Window is zero-height: nothing to draw. */
820 if (wp->w_height == 0)
821 {
822 wp->w_redr_type = 0;
823 return;
824 }
825
826#ifdef FEAT_VERTSPLIT
827 /* Window is zero-width: Only need to draw the separator. */
828 if (wp->w_width == 0)
829 {
830 /* draw the vertical separator right of this window */
831 draw_vsep_win(wp, 0);
832 wp->w_redr_type = 0;
833 return;
834 }
835#endif
836
837#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000838 /* Setup for match and 'hlsearch' highlighting. Disable any previous
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000839 * match */
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000840 cur = wp->w_match_head;
841 while (cur != NULL)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000842 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000843 cur->hl.rm = cur->match;
844 if (cur->hlg_id == 0)
845 cur->hl.attr = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000846 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000847 cur->hl.attr = syn_id2attr(cur->hlg_id);
848 cur->hl.buf = buf;
849 cur->hl.lnum = 0;
850 cur->hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000851# ifdef FEAT_RELTIME
852 /* Set the time limit to 'redrawtime'. */
853 profile_setlimit(p_rdt, &(cur->hl.tm));
854# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000855 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 search_hl.buf = buf;
858 search_hl.lnum = 0;
859 search_hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000860 /* time limit is set at the toplevel, for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#endif
862
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000863#ifdef FEAT_LINEBREAK
864 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000865 i = wp->w_p_nu ? number_width(wp) : 0;
866 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000867 {
868 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000869 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000870 }
871 else
872#endif
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
875 {
876 /*
877 * When there are both inserted/deleted lines and specific lines to be
878 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
879 * everything (only happens when redrawing is off for while).
880 */
881 type = NOT_VALID;
882 }
883 else
884 {
885 /*
886 * Set mod_top to the first line that needs displaying because of
887 * changes. Set mod_bot to the first line after the changes.
888 */
889 mod_top = wp->w_redraw_top;
890 if (wp->w_redraw_bot != 0)
891 mod_bot = wp->w_redraw_bot + 1;
892 else
893 mod_bot = 0;
894 wp->w_redraw_top = 0; /* reset for next time */
895 wp->w_redraw_bot = 0;
896 if (buf->b_mod_set)
897 {
898 if (mod_top == 0 || mod_top > buf->b_mod_top)
899 {
900 mod_top = buf->b_mod_top;
901#ifdef FEAT_SYN_HL
902 /* Need to redraw lines above the change that may be included
903 * in a pattern match. */
904 if (syntax_present(buf))
905 {
906 mod_top -= buf->b_syn_sync_linebreaks;
907 if (mod_top < 1)
908 mod_top = 1;
909 }
910#endif
911 }
912 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
913 mod_bot = buf->b_mod_bot;
914
915#ifdef FEAT_SEARCH_EXTRA
916 /* When 'hlsearch' is on and using a multi-line search pattern, a
917 * change in one line may make the Search highlighting in a
918 * previous line invalid. Simple solution: redraw all visible
919 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000920 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000922 if (search_hl.rm.regprog != NULL
923 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000925 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000926 {
927 cur = wp->w_match_head;
928 while (cur != NULL)
929 {
930 if (cur->match.regprog != NULL
931 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000932 {
933 top_to_mod = TRUE;
934 break;
935 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000936 cur = cur->next;
937 }
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#endif
940 }
941#ifdef FEAT_FOLDING
942 if (mod_top != 0 && hasAnyFolding(wp))
943 {
944 linenr_T lnumt, lnumb;
945
946 /*
947 * A change in a line can cause lines above it to become folded or
948 * unfolded. Find the top most buffer line that may be affected.
949 * If the line was previously folded and displayed, get the first
950 * line of that fold. If the line is folded now, get the first
951 * folded line. Use the minimum of these two.
952 */
953
954 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
955 * the line below it. If there is no valid entry, use w_topline.
956 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
957 * to this line. If there is no valid entry, use MAXLNUM. */
958 lnumt = wp->w_topline;
959 lnumb = MAXLNUM;
960 for (i = 0; i < wp->w_lines_valid; ++i)
961 if (wp->w_lines[i].wl_valid)
962 {
963 if (wp->w_lines[i].wl_lastlnum < mod_top)
964 lnumt = wp->w_lines[i].wl_lastlnum + 1;
965 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
966 {
967 lnumb = wp->w_lines[i].wl_lnum;
968 /* When there is a fold column it might need updating
969 * in the next line ("J" just above an open fold). */
970 if (wp->w_p_fdc > 0)
971 ++lnumb;
972 }
973 }
974
975 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
976 if (mod_top > lnumt)
977 mod_top = lnumt;
978
979 /* Now do the same for the bottom line (one above mod_bot). */
980 --mod_bot;
981 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
982 ++mod_bot;
983 if (mod_bot < lnumb)
984 mod_bot = lnumb;
985 }
986#endif
987
988 /* When a change starts above w_topline and the end is below
989 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000990 * If the end of the change is above w_topline: do like no change was
991 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (mod_top != 0 && mod_top < wp->w_topline)
993 {
994 if (mod_bot > wp->w_topline)
995 mod_top = wp->w_topline;
996#ifdef FEAT_SYN_HL
997 else if (syntax_present(buf))
998 top_end = 1;
999#endif
1000 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001001
1002 /* When line numbers are displayed need to redraw all lines below
1003 * inserted/deleted lines. */
1004 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1005 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007
1008 /*
1009 * When only displaying the lines at the top, set top_end. Used when
1010 * window has scrolled down for msg_scrolled.
1011 */
1012 if (type == REDRAW_TOP)
1013 {
1014 j = 0;
1015 for (i = 0; i < wp->w_lines_valid; ++i)
1016 {
1017 j += wp->w_lines[i].wl_size;
1018 if (j >= wp->w_upd_rows)
1019 {
1020 top_end = j;
1021 break;
1022 }
1023 }
1024 if (top_end == 0)
1025 /* not found (cannot happen?): redraw everything */
1026 type = NOT_VALID;
1027 else
1028 /* top area defined, the rest is VALID */
1029 type = VALID;
1030 }
1031
Bram Moolenaar367329b2007-08-30 11:53:22 +00001032 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001033 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1034 * non-zero and thus not FALSE) will indicate that screenclear() was not
1035 * called. */
1036 if (screen_cleared)
1037 screen_cleared = MAYBE;
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 /*
1040 * If there are no changes on the screen that require a complete redraw,
1041 * handle three cases:
1042 * 1: we are off the top of the screen by a few lines: scroll down
1043 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1044 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1045 * w_lines[] that needs updating.
1046 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001047 if ((type == VALID || type == SOME_VALID
1048 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#ifdef FEAT_DIFF
1050 && !wp->w_botfill && !wp->w_old_botfill
1051#endif
1052 )
1053 {
1054 if (mod_top != 0 && wp->w_topline == mod_top)
1055 {
1056 /*
1057 * w_topline is the first changed line, the scrolling will be done
1058 * further down.
1059 */
1060 }
1061 else if (wp->w_lines[0].wl_valid
1062 && (wp->w_topline < wp->w_lines[0].wl_lnum
1063#ifdef FEAT_DIFF
1064 || (wp->w_topline == wp->w_lines[0].wl_lnum
1065 && wp->w_topfill > wp->w_old_topfill)
1066#endif
1067 ))
1068 {
1069 /*
1070 * New topline is above old topline: May scroll down.
1071 */
1072#ifdef FEAT_FOLDING
1073 if (hasAnyFolding(wp))
1074 {
1075 linenr_T ln;
1076
1077 /* count the number of lines we are off, counting a sequence
1078 * of folded lines as one */
1079 j = 0;
1080 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1081 {
1082 ++j;
1083 if (j >= wp->w_height - 2)
1084 break;
1085 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1086 }
1087 }
1088 else
1089#endif
1090 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1091 if (j < wp->w_height - 2) /* not too far off */
1092 {
1093 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1094#ifdef FEAT_DIFF
1095 /* insert extra lines for previously invisible filler lines */
1096 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1097 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1098 - wp->w_old_topfill;
1099#endif
1100 if (i < wp->w_height - 2) /* less than a screen off */
1101 {
1102 /*
1103 * Try to insert the correct number of lines.
1104 * If not the last window, delete the lines at the bottom.
1105 * win_ins_lines may fail when the terminal can't do it.
1106 */
1107 if (i > 0)
1108 check_for_delay(FALSE);
1109 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1110 {
1111 if (wp->w_lines_valid != 0)
1112 {
1113 /* Need to update rows that are new, stop at the
1114 * first one that scrolled down. */
1115 top_end = i;
1116#ifdef FEAT_VISUAL
1117 scrolled_down = TRUE;
1118#endif
1119
1120 /* Move the entries that were scrolled, disable
1121 * the entries for the lines to be redrawn. */
1122 if ((wp->w_lines_valid += j) > wp->w_height)
1123 wp->w_lines_valid = wp->w_height;
1124 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1125 wp->w_lines[idx] = wp->w_lines[idx - j];
1126 while (idx >= 0)
1127 wp->w_lines[idx--].wl_valid = FALSE;
1128 }
1129 }
1130 else
1131 mid_start = 0; /* redraw all lines */
1132 }
1133 else
1134 mid_start = 0; /* redraw all lines */
1135 }
1136 else
1137 mid_start = 0; /* redraw all lines */
1138 }
1139 else
1140 {
1141 /*
1142 * New topline is at or below old topline: May scroll up.
1143 * When topline didn't change, find first entry in w_lines[] that
1144 * needs updating.
1145 */
1146
1147 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1148 j = -1;
1149 row = 0;
1150 for (i = 0; i < wp->w_lines_valid; i++)
1151 {
1152 if (wp->w_lines[i].wl_valid
1153 && wp->w_lines[i].wl_lnum == wp->w_topline)
1154 {
1155 j = i;
1156 break;
1157 }
1158 row += wp->w_lines[i].wl_size;
1159 }
1160 if (j == -1)
1161 {
1162 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1163 * lines */
1164 mid_start = 0;
1165 }
1166 else
1167 {
1168 /*
1169 * Try to delete the correct number of lines.
1170 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1171 */
1172#ifdef FEAT_DIFF
1173 /* If the topline didn't change, delete old filler lines,
1174 * otherwise delete filler lines of the new topline... */
1175 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1176 row += wp->w_old_topfill;
1177 else
1178 row += diff_check_fill(wp, wp->w_topline);
1179 /* ... but don't delete new filler lines. */
1180 row -= wp->w_topfill;
1181#endif
1182 if (row > 0)
1183 {
1184 check_for_delay(FALSE);
1185 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1186 bot_start = wp->w_height - row;
1187 else
1188 mid_start = 0; /* redraw all lines */
1189 }
1190 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1191 {
1192 /*
1193 * Skip the lines (below the deleted lines) that are still
1194 * valid and don't need redrawing. Copy their info
1195 * upwards, to compensate for the deleted lines. Set
1196 * bot_start to the first row that needs redrawing.
1197 */
1198 bot_start = 0;
1199 idx = 0;
1200 for (;;)
1201 {
1202 wp->w_lines[idx] = wp->w_lines[j];
1203 /* stop at line that didn't fit, unless it is still
1204 * valid (no lines deleted) */
1205 if (row > 0 && bot_start + row
1206 + (int)wp->w_lines[j].wl_size > wp->w_height)
1207 {
1208 wp->w_lines_valid = idx + 1;
1209 break;
1210 }
1211 bot_start += wp->w_lines[idx++].wl_size;
1212
1213 /* stop at the last valid entry in w_lines[].wl_size */
1214 if (++j >= wp->w_lines_valid)
1215 {
1216 wp->w_lines_valid = idx;
1217 break;
1218 }
1219 }
1220#ifdef FEAT_DIFF
1221 /* Correct the first entry for filler lines at the top
1222 * when it won't get updated below. */
1223 if (wp->w_p_diff && bot_start > 0)
1224 wp->w_lines[0].wl_size =
1225 plines_win_nofill(wp, wp->w_topline, TRUE)
1226 + wp->w_topfill;
1227#endif
1228 }
1229 }
1230 }
1231
1232 /* When starting redraw in the first line, redraw all lines. When
1233 * there is only one window it's probably faster to clear the screen
1234 * first. */
1235 if (mid_start == 0)
1236 {
1237 mid_end = wp->w_height;
1238 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001239 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001240 /* Clear the screen when it was not done by win_del_lines() or
1241 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1242 * then. */
1243 if (screen_cleared != TRUE)
1244 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001245#ifdef FEAT_WINDOWS
1246 /* The screen was cleared, redraw the tab pages line. */
1247 if (redraw_tabline)
1248 draw_tabline();
1249#endif
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001252
1253 /* When win_del_lines() or win_ins_lines() caused the screen to be
1254 * cleared (only happens for the first window) or when screenclear()
1255 * was called directly above, "must_redraw" will have been set to
1256 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1257 if (screen_cleared == TRUE)
1258 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260 else
1261 {
1262 /* Not VALID or INVERTED: redraw all lines. */
1263 mid_start = 0;
1264 mid_end = wp->w_height;
1265 }
1266
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001267 if (type == SOME_VALID)
1268 {
1269 /* SOME_VALID: redraw all lines. */
1270 mid_start = 0;
1271 mid_end = wp->w_height;
1272 type = NOT_VALID;
1273 }
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#ifdef FEAT_VISUAL
1276 /* check if we are updating or removing the inverted part */
1277 if ((VIsual_active && buf == curwin->w_buffer)
1278 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1279 {
1280 linenr_T from, to;
1281
1282 if (VIsual_active)
1283 {
1284 if (VIsual_active
1285 && (VIsual_mode != wp->w_old_visual_mode
1286 || type == INVERTED_ALL))
1287 {
1288 /*
1289 * If the type of Visual selection changed, redraw the whole
1290 * selection. Also when the ownership of the X selection is
1291 * gained or lost.
1292 */
1293 if (curwin->w_cursor.lnum < VIsual.lnum)
1294 {
1295 from = curwin->w_cursor.lnum;
1296 to = VIsual.lnum;
1297 }
1298 else
1299 {
1300 from = VIsual.lnum;
1301 to = curwin->w_cursor.lnum;
1302 }
1303 /* redraw more when the cursor moved as well */
1304 if (wp->w_old_cursor_lnum < from)
1305 from = wp->w_old_cursor_lnum;
1306 if (wp->w_old_cursor_lnum > to)
1307 to = wp->w_old_cursor_lnum;
1308 if (wp->w_old_visual_lnum < from)
1309 from = wp->w_old_visual_lnum;
1310 if (wp->w_old_visual_lnum > to)
1311 to = wp->w_old_visual_lnum;
1312 }
1313 else
1314 {
1315 /*
1316 * Find the line numbers that need to be updated: The lines
1317 * between the old cursor position and the current cursor
1318 * position. Also check if the Visual position changed.
1319 */
1320 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1321 {
1322 from = curwin->w_cursor.lnum;
1323 to = wp->w_old_cursor_lnum;
1324 }
1325 else
1326 {
1327 from = wp->w_old_cursor_lnum;
1328 to = curwin->w_cursor.lnum;
1329 if (from == 0) /* Visual mode just started */
1330 from = to;
1331 }
1332
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001333 if (VIsual.lnum != wp->w_old_visual_lnum
1334 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 if (wp->w_old_visual_lnum < from
1337 && wp->w_old_visual_lnum != 0)
1338 from = wp->w_old_visual_lnum;
1339 if (wp->w_old_visual_lnum > to)
1340 to = wp->w_old_visual_lnum;
1341 if (VIsual.lnum < from)
1342 from = VIsual.lnum;
1343 if (VIsual.lnum > to)
1344 to = VIsual.lnum;
1345 }
1346 }
1347
1348 /*
1349 * If in block mode and changed column or curwin->w_curswant:
1350 * update all lines.
1351 * First compute the actual start and end column.
1352 */
1353 if (VIsual_mode == Ctrl_V)
1354 {
1355 colnr_T fromc, toc;
1356
1357 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1358 ++toc;
1359 if (curwin->w_curswant == MAXCOL)
1360 toc = MAXCOL;
1361
1362 if (fromc != wp->w_old_cursor_fcol
1363 || toc != wp->w_old_cursor_lcol)
1364 {
1365 if (from > VIsual.lnum)
1366 from = VIsual.lnum;
1367 if (to < VIsual.lnum)
1368 to = VIsual.lnum;
1369 }
1370 wp->w_old_cursor_fcol = fromc;
1371 wp->w_old_cursor_lcol = toc;
1372 }
1373 }
1374 else
1375 {
1376 /* Use the line numbers of the old Visual area. */
1377 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1378 {
1379 from = wp->w_old_cursor_lnum;
1380 to = wp->w_old_visual_lnum;
1381 }
1382 else
1383 {
1384 from = wp->w_old_visual_lnum;
1385 to = wp->w_old_cursor_lnum;
1386 }
1387 }
1388
1389 /*
1390 * There is no need to update lines above the top of the window.
1391 */
1392 if (from < wp->w_topline)
1393 from = wp->w_topline;
1394
1395 /*
1396 * If we know the value of w_botline, use it to restrict the update to
1397 * the lines that are visible in the window.
1398 */
1399 if (wp->w_valid & VALID_BOTLINE)
1400 {
1401 if (from >= wp->w_botline)
1402 from = wp->w_botline - 1;
1403 if (to >= wp->w_botline)
1404 to = wp->w_botline - 1;
1405 }
1406
1407 /*
1408 * Find the minimal part to be updated.
1409 * Watch out for scrolling that made entries in w_lines[] invalid.
1410 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1411 * top_end; need to redraw from top_end to the "to" line.
1412 * A middle mouse click with a Visual selection may change the text
1413 * above the Visual area and reset wl_valid, do count these for
1414 * mid_end (in srow).
1415 */
1416 if (mid_start > 0)
1417 {
1418 lnum = wp->w_topline;
1419 idx = 0;
1420 srow = 0;
1421 if (scrolled_down)
1422 mid_start = top_end;
1423 else
1424 mid_start = 0;
1425 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1426 {
1427 if (wp->w_lines[idx].wl_valid)
1428 mid_start += wp->w_lines[idx].wl_size;
1429 else if (!scrolled_down)
1430 srow += wp->w_lines[idx].wl_size;
1431 ++idx;
1432# ifdef FEAT_FOLDING
1433 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1434 lnum = wp->w_lines[idx].wl_lnum;
1435 else
1436# endif
1437 ++lnum;
1438 }
1439 srow += mid_start;
1440 mid_end = wp->w_height;
1441 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1442 {
1443 if (wp->w_lines[idx].wl_valid
1444 && wp->w_lines[idx].wl_lnum >= to + 1)
1445 {
1446 /* Only update until first row of this line */
1447 mid_end = srow;
1448 break;
1449 }
1450 srow += wp->w_lines[idx].wl_size;
1451 }
1452 }
1453 }
1454
1455 if (VIsual_active && buf == curwin->w_buffer)
1456 {
1457 wp->w_old_visual_mode = VIsual_mode;
1458 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1459 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001460 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 wp->w_old_curswant = curwin->w_curswant;
1462 }
1463 else
1464 {
1465 wp->w_old_visual_mode = 0;
1466 wp->w_old_cursor_lnum = 0;
1467 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001468 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470#endif /* FEAT_VISUAL */
1471
1472#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1473 /* reset got_int, otherwise regexp won't work */
1474 save_got_int = got_int;
1475 got_int = 0;
1476#endif
1477#ifdef FEAT_FOLDING
1478 win_foldinfo.fi_level = 0;
1479#endif
1480
1481 /*
1482 * Update all the window rows.
1483 */
1484 idx = 0; /* first entry in w_lines[].wl_size */
1485 row = 0;
1486 srow = 0;
1487 lnum = wp->w_topline; /* first line shown in window */
1488 for (;;)
1489 {
1490 /* stop updating when reached the end of the window (check for _past_
1491 * the end of the window is at the end of the loop) */
1492 if (row == wp->w_height)
1493 {
1494 didline = TRUE;
1495 break;
1496 }
1497
1498 /* stop updating when hit the end of the file */
1499 if (lnum > buf->b_ml.ml_line_count)
1500 {
1501 eof = TRUE;
1502 break;
1503 }
1504
1505 /* Remember the starting row of the line that is going to be dealt
1506 * with. It is used further down when the line doesn't fit. */
1507 srow = row;
1508
1509 /*
1510 * Update a line when it is in an area that needs updating, when it
1511 * has changes or w_lines[idx] is invalid.
1512 * bot_start may be halfway a wrapped line after using
1513 * win_del_lines(), check if the current line includes it.
1514 * When syntax folding is being used, the saved syntax states will
1515 * already have been updated, we can't see where the syntax state is
1516 * the same again, just update until the end of the window.
1517 */
1518 if (row < top_end
1519 || (row >= mid_start && row < mid_end)
1520#ifdef FEAT_SEARCH_EXTRA
1521 || top_to_mod
1522#endif
1523 || idx >= wp->w_lines_valid
1524 || (row + wp->w_lines[idx].wl_size > bot_start)
1525 || (mod_top != 0
1526 && (lnum == mod_top
1527 || (lnum >= mod_top
1528 && (lnum < mod_bot
1529#ifdef FEAT_SYN_HL
1530 || did_update == DID_FOLD
1531 || (did_update == DID_LINE
1532 && syntax_present(buf)
1533 && (
1534# ifdef FEAT_FOLDING
1535 (foldmethodIsSyntax(wp)
1536 && hasAnyFolding(wp)) ||
1537# endif
1538 syntax_check_changed(lnum)))
1539#endif
1540 )))))
1541 {
1542#ifdef FEAT_SEARCH_EXTRA
1543 if (lnum == mod_top)
1544 top_to_mod = FALSE;
1545#endif
1546
1547 /*
1548 * When at start of changed lines: May scroll following lines
1549 * up or down to minimize redrawing.
1550 * Don't do this when the change continues until the end.
1551 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1552 */
1553 if (lnum == mod_top
1554 && mod_bot != MAXLNUM
1555 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1556 {
1557 int old_rows = 0;
1558 int new_rows = 0;
1559 int xtra_rows;
1560 linenr_T l;
1561
1562 /* Count the old number of window rows, using w_lines[], which
1563 * should still contain the sizes for the lines as they are
1564 * currently displayed. */
1565 for (i = idx; i < wp->w_lines_valid; ++i)
1566 {
1567 /* Only valid lines have a meaningful wl_lnum. Invalid
1568 * lines are part of the changed area. */
1569 if (wp->w_lines[i].wl_valid
1570 && wp->w_lines[i].wl_lnum == mod_bot)
1571 break;
1572 old_rows += wp->w_lines[i].wl_size;
1573#ifdef FEAT_FOLDING
1574 if (wp->w_lines[i].wl_valid
1575 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1576 {
1577 /* Must have found the last valid entry above mod_bot.
1578 * Add following invalid entries. */
1579 ++i;
1580 while (i < wp->w_lines_valid
1581 && !wp->w_lines[i].wl_valid)
1582 old_rows += wp->w_lines[i++].wl_size;
1583 break;
1584 }
1585#endif
1586 }
1587
1588 if (i >= wp->w_lines_valid)
1589 {
1590 /* We can't find a valid line below the changed lines,
1591 * need to redraw until the end of the window.
1592 * Inserting/deleting lines has no use. */
1593 bot_start = 0;
1594 }
1595 else
1596 {
1597 /* Able to count old number of rows: Count new window
1598 * rows, and may insert/delete lines */
1599 j = idx;
1600 for (l = lnum; l < mod_bot; ++l)
1601 {
1602#ifdef FEAT_FOLDING
1603 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1604 ++new_rows;
1605 else
1606#endif
1607#ifdef FEAT_DIFF
1608 if (l == wp->w_topline)
1609 new_rows += plines_win_nofill(wp, l, TRUE)
1610 + wp->w_topfill;
1611 else
1612#endif
1613 new_rows += plines_win(wp, l, TRUE);
1614 ++j;
1615 if (new_rows > wp->w_height - row - 2)
1616 {
1617 /* it's getting too much, must redraw the rest */
1618 new_rows = 9999;
1619 break;
1620 }
1621 }
1622 xtra_rows = new_rows - old_rows;
1623 if (xtra_rows < 0)
1624 {
1625 /* May scroll text up. If there is not enough
1626 * remaining text or scrolling fails, must redraw the
1627 * rest. If scrolling works, must redraw the text
1628 * below the scrolled text. */
1629 if (row - xtra_rows >= wp->w_height - 2)
1630 mod_bot = MAXLNUM;
1631 else
1632 {
1633 check_for_delay(FALSE);
1634 if (win_del_lines(wp, row,
1635 -xtra_rows, FALSE, FALSE) == FAIL)
1636 mod_bot = MAXLNUM;
1637 else
1638 bot_start = wp->w_height + xtra_rows;
1639 }
1640 }
1641 else if (xtra_rows > 0)
1642 {
1643 /* May scroll text down. If there is not enough
1644 * remaining text of scrolling fails, must redraw the
1645 * rest. */
1646 if (row + xtra_rows >= wp->w_height - 2)
1647 mod_bot = MAXLNUM;
1648 else
1649 {
1650 check_for_delay(FALSE);
1651 if (win_ins_lines(wp, row + old_rows,
1652 xtra_rows, FALSE, FALSE) == FAIL)
1653 mod_bot = MAXLNUM;
1654 else if (top_end > row + old_rows)
1655 /* Scrolled the part at the top that requires
1656 * updating down. */
1657 top_end += xtra_rows;
1658 }
1659 }
1660
1661 /* When not updating the rest, may need to move w_lines[]
1662 * entries. */
1663 if (mod_bot != MAXLNUM && i != j)
1664 {
1665 if (j < i)
1666 {
1667 int x = row + new_rows;
1668
1669 /* move entries in w_lines[] upwards */
1670 for (;;)
1671 {
1672 /* stop at last valid entry in w_lines[] */
1673 if (i >= wp->w_lines_valid)
1674 {
1675 wp->w_lines_valid = j;
1676 break;
1677 }
1678 wp->w_lines[j] = wp->w_lines[i];
1679 /* stop at a line that won't fit */
1680 if (x + (int)wp->w_lines[j].wl_size
1681 > wp->w_height)
1682 {
1683 wp->w_lines_valid = j + 1;
1684 break;
1685 }
1686 x += wp->w_lines[j++].wl_size;
1687 ++i;
1688 }
1689 if (bot_start > x)
1690 bot_start = x;
1691 }
1692 else /* j > i */
1693 {
1694 /* move entries in w_lines[] downwards */
1695 j -= i;
1696 wp->w_lines_valid += j;
1697 if (wp->w_lines_valid > wp->w_height)
1698 wp->w_lines_valid = wp->w_height;
1699 for (i = wp->w_lines_valid; i - j >= idx; --i)
1700 wp->w_lines[i] = wp->w_lines[i - j];
1701
1702 /* The w_lines[] entries for inserted lines are
1703 * now invalid, but wl_size may be used above.
1704 * Reset to zero. */
1705 while (i >= idx)
1706 {
1707 wp->w_lines[i].wl_size = 0;
1708 wp->w_lines[i--].wl_valid = FALSE;
1709 }
1710 }
1711 }
1712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714
1715#ifdef FEAT_FOLDING
1716 /*
1717 * When lines are folded, display one line for all of them.
1718 * Otherwise, display normally (can be several display lines when
1719 * 'wrap' is on).
1720 */
1721 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1722 if (fold_count != 0)
1723 {
1724 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1725 ++row;
1726 --fold_count;
1727 wp->w_lines[idx].wl_folded = TRUE;
1728 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1729# ifdef FEAT_SYN_HL
1730 did_update = DID_FOLD;
1731# endif
1732 }
1733 else
1734#endif
1735 if (idx < wp->w_lines_valid
1736 && wp->w_lines[idx].wl_valid
1737 && wp->w_lines[idx].wl_lnum == lnum
1738 && lnum > wp->w_topline
1739 && !(dy_flags & DY_LASTLINE)
1740 && srow + wp->w_lines[idx].wl_size > wp->w_height
1741#ifdef FEAT_DIFF
1742 && diff_check_fill(wp, lnum) == 0
1743#endif
1744 )
1745 {
1746 /* This line is not going to fit. Don't draw anything here,
1747 * will draw "@ " lines below. */
1748 row = wp->w_height + 1;
1749 }
1750 else
1751 {
1752#ifdef FEAT_SEARCH_EXTRA
1753 prepare_search_hl(wp, lnum);
1754#endif
1755#ifdef FEAT_SYN_HL
1756 /* Let the syntax stuff know we skipped a few lines. */
1757 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1758 && syntax_present(buf))
1759 syntax_end_parsing(syntax_last_parsed + 1);
1760#endif
1761
1762 /*
1763 * Display one line.
1764 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001765 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
1767#ifdef FEAT_FOLDING
1768 wp->w_lines[idx].wl_folded = FALSE;
1769 wp->w_lines[idx].wl_lastlnum = lnum;
1770#endif
1771#ifdef FEAT_SYN_HL
1772 did_update = DID_LINE;
1773 syntax_last_parsed = lnum;
1774#endif
1775 }
1776
1777 wp->w_lines[idx].wl_lnum = lnum;
1778 wp->w_lines[idx].wl_valid = TRUE;
1779 if (row > wp->w_height) /* past end of screen */
1780 {
1781 /* we may need the size of that too long line later on */
1782 if (dollar_vcol == 0)
1783 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1784 ++idx;
1785 break;
1786 }
1787 if (dollar_vcol == 0)
1788 wp->w_lines[idx].wl_size = row - srow;
1789 ++idx;
1790#ifdef FEAT_FOLDING
1791 lnum += fold_count + 1;
1792#else
1793 ++lnum;
1794#endif
1795 }
1796 else
1797 {
1798 /* This line does not need updating, advance to the next one */
1799 row += wp->w_lines[idx++].wl_size;
1800 if (row > wp->w_height) /* past end of screen */
1801 break;
1802#ifdef FEAT_FOLDING
1803 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1804#else
1805 ++lnum;
1806#endif
1807#ifdef FEAT_SYN_HL
1808 did_update = DID_NONE;
1809#endif
1810 }
1811
1812 if (lnum > buf->b_ml.ml_line_count)
1813 {
1814 eof = TRUE;
1815 break;
1816 }
1817 }
1818 /*
1819 * End of loop over all window lines.
1820 */
1821
1822
1823 if (idx > wp->w_lines_valid)
1824 wp->w_lines_valid = idx;
1825
1826#ifdef FEAT_SYN_HL
1827 /*
1828 * Let the syntax stuff know we stop parsing here.
1829 */
1830 if (syntax_last_parsed != 0 && syntax_present(buf))
1831 syntax_end_parsing(syntax_last_parsed + 1);
1832#endif
1833
1834 /*
1835 * If we didn't hit the end of the file, and we didn't finish the last
1836 * line we were working on, then the line didn't fit.
1837 */
1838 wp->w_empty_rows = 0;
1839#ifdef FEAT_DIFF
1840 wp->w_filler_rows = 0;
1841#endif
1842 if (!eof && !didline)
1843 {
1844 if (lnum == wp->w_topline)
1845 {
1846 /*
1847 * Single line that does not fit!
1848 * Don't overwrite it, it can be edited.
1849 */
1850 wp->w_botline = lnum + 1;
1851 }
1852#ifdef FEAT_DIFF
1853 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1854 {
1855 /* Window ends in filler lines. */
1856 wp->w_botline = lnum;
1857 wp->w_filler_rows = wp->w_height - srow;
1858 }
1859#endif
1860 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1861 {
1862 /*
1863 * Last line isn't finished: Display "@@@" at the end.
1864 */
1865 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1866 W_WINROW(wp) + wp->w_height,
1867 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1868 '@', '@', hl_attr(HLF_AT));
1869 set_empty_rows(wp, srow);
1870 wp->w_botline = lnum;
1871 }
1872 else
1873 {
1874 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1875 wp->w_botline = lnum;
1876 }
1877 }
1878 else
1879 {
1880#ifdef FEAT_VERTSPLIT
1881 draw_vsep_win(wp, row);
1882#endif
1883 if (eof) /* we hit the end of the file */
1884 {
1885 wp->w_botline = buf->b_ml.ml_line_count + 1;
1886#ifdef FEAT_DIFF
1887 j = diff_check_fill(wp, wp->w_botline);
1888 if (j > 0 && !wp->w_botfill)
1889 {
1890 /*
1891 * Display filler lines at the end of the file
1892 */
1893 if (char2cells(fill_diff) > 1)
1894 i = '-';
1895 else
1896 i = fill_diff;
1897 if (row + j > wp->w_height)
1898 j = wp->w_height - row;
1899 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1900 row += j;
1901 }
1902#endif
1903 }
1904 else if (dollar_vcol == 0)
1905 wp->w_botline = lnum;
1906
1907 /* make sure the rest of the screen is blank */
1908 /* put '~'s on rows that aren't part of the file. */
1909 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1910 }
1911
1912 /* Reset the type of redrawing required, the window has been updated. */
1913 wp->w_redr_type = 0;
1914#ifdef FEAT_DIFF
1915 wp->w_old_topfill = wp->w_topfill;
1916 wp->w_old_botfill = wp->w_botfill;
1917#endif
1918
1919 if (dollar_vcol == 0)
1920 {
1921 /*
1922 * There is a trick with w_botline. If we invalidate it on each
1923 * change that might modify it, this will cause a lot of expensive
1924 * calls to plines() in update_topline() each time. Therefore the
1925 * value of w_botline is often approximated, and this value is used to
1926 * compute the value of w_topline. If the value of w_botline was
1927 * wrong, check that the value of w_topline is correct (cursor is on
1928 * the visible part of the text). If it's not, we need to redraw
1929 * again. Mostly this just means scrolling up a few lines, so it
1930 * doesn't look too bad. Only do this for the current window (where
1931 * changes are relevant).
1932 */
1933 wp->w_valid |= VALID_BOTLINE;
1934 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1935 {
1936 recursive = TRUE;
1937 curwin->w_valid &= ~VALID_TOPLINE;
1938 update_topline(); /* may invalidate w_botline again */
1939 if (must_redraw != 0)
1940 {
1941 /* Don't update for changes in buffer again. */
1942 i = curbuf->b_mod_set;
1943 curbuf->b_mod_set = FALSE;
1944 win_update(curwin);
1945 must_redraw = 0;
1946 curbuf->b_mod_set = i;
1947 }
1948 recursive = FALSE;
1949 }
1950 }
1951
1952#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1953 /* restore got_int, unless CTRL-C was hit while redrawing */
1954 if (!got_int)
1955 got_int = save_got_int;
1956#endif
1957}
1958
1959#ifdef FEAT_SIGNS
1960static int draw_signcolumn __ARGS((win_T *wp));
1961
1962/*
1963 * Return TRUE when window "wp" has a column to draw signs in.
1964 */
1965 static int
1966draw_signcolumn(wp)
1967 win_T *wp;
1968{
1969 return (wp->w_buffer->b_signlist != NULL
1970# ifdef FEAT_NETBEANS_INTG
1971 || usingNetbeans
1972# endif
1973 );
1974}
1975#endif
1976
1977/*
1978 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1979 * as the filler character.
1980 */
1981 static void
1982win_draw_end(wp, c1, c2, row, endrow, hl)
1983 win_T *wp;
1984 int c1;
1985 int c2;
1986 int row;
1987 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001988 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1991 int n = 0;
1992# define FDC_OFF n
1993#else
1994# define FDC_OFF 0
1995#endif
1996
1997#ifdef FEAT_RIGHTLEFT
1998 if (wp->w_p_rl)
1999 {
2000 /* No check for cmdline window: should never be right-left. */
2001# ifdef FEAT_FOLDING
2002 n = wp->w_p_fdc;
2003
2004 if (n > 0)
2005 {
2006 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002007 if (n > W_WIDTH(wp))
2008 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2010 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2011 ' ', ' ', hl_attr(HLF_FC));
2012 }
2013# endif
2014# ifdef FEAT_SIGNS
2015 if (draw_signcolumn(wp))
2016 {
2017 int nn = n + 2;
2018
2019 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002020 if (nn > W_WIDTH(wp))
2021 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2023 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2024 ' ', ' ', hl_attr(HLF_SC));
2025 n = nn;
2026 }
2027# endif
2028 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2029 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2030 c2, c2, hl_attr(hl));
2031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2033 c1, c2, hl_attr(hl));
2034 }
2035 else
2036#endif
2037 {
2038#ifdef FEAT_CMDWIN
2039 if (cmdwin_type != 0 && wp == curwin)
2040 {
2041 /* draw the cmdline character in the leftmost column */
2042 n = 1;
2043 if (n > wp->w_width)
2044 n = wp->w_width;
2045 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2046 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2047 cmdwin_type, ' ', hl_attr(HLF_AT));
2048 }
2049#endif
2050#ifdef FEAT_FOLDING
2051 if (wp->w_p_fdc > 0)
2052 {
2053 int nn = n + wp->w_p_fdc;
2054
2055 /* draw the fold column at the left */
2056 if (nn > W_WIDTH(wp))
2057 nn = W_WIDTH(wp);
2058 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2059 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2060 ' ', ' ', hl_attr(HLF_FC));
2061 n = nn;
2062 }
2063#endif
2064#ifdef FEAT_SIGNS
2065 if (draw_signcolumn(wp))
2066 {
2067 int nn = n + 2;
2068
2069 /* draw the sign column after the fold column */
2070 if (nn > W_WIDTH(wp))
2071 nn = W_WIDTH(wp);
2072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2073 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2074 ' ', ' ', hl_attr(HLF_SC));
2075 n = nn;
2076 }
2077#endif
2078 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2079 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2080 c1, c2, hl_attr(hl));
2081 }
2082 set_empty_rows(wp, row);
2083}
2084
2085#ifdef FEAT_FOLDING
2086/*
2087 * Display one folded line.
2088 */
2089 static void
2090fold_line(wp, fold_count, foldinfo, lnum, row)
2091 win_T *wp;
2092 long fold_count;
2093 foldinfo_T *foldinfo;
2094 linenr_T lnum;
2095 int row;
2096{
2097 char_u buf[51];
2098 pos_T *top, *bot;
2099 linenr_T lnume = lnum + fold_count - 1;
2100 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002101 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 int col;
2104 int txtcol;
2105 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002106 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108 /* Build the fold line:
2109 * 1. Add the cmdwin_type for the command-line window
2110 * 2. Add the 'foldcolumn'
2111 * 3. Add the 'number' column
2112 * 4. Compose the text
2113 * 5. Add the text
2114 * 6. set highlighting for the Visual area an other text
2115 */
2116 col = 0;
2117
2118 /*
2119 * 1. Add the cmdwin_type for the command-line window
2120 * Ignores 'rightleft', this window is never right-left.
2121 */
2122#ifdef FEAT_CMDWIN
2123 if (cmdwin_type != 0 && wp == curwin)
2124 {
2125 ScreenLines[off] = cmdwin_type;
2126 ScreenAttrs[off] = hl_attr(HLF_AT);
2127#ifdef FEAT_MBYTE
2128 if (enc_utf8)
2129 ScreenLinesUC[off] = 0;
2130#endif
2131 ++col;
2132 }
2133#endif
2134
2135 /*
2136 * 2. Add the 'foldcolumn'
2137 */
2138 fdc = wp->w_p_fdc;
2139 if (fdc > W_WIDTH(wp) - col)
2140 fdc = W_WIDTH(wp) - col;
2141 if (fdc > 0)
2142 {
2143 fill_foldcolumn(buf, wp, TRUE, lnum);
2144#ifdef FEAT_RIGHTLEFT
2145 if (wp->w_p_rl)
2146 {
2147 int i;
2148
2149 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2150 hl_attr(HLF_FC));
2151 /* reverse the fold column */
2152 for (i = 0; i < fdc; ++i)
2153 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2154 }
2155 else
2156#endif
2157 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2158 col += fdc;
2159 }
2160
2161#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002162# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2163 for (ri = 0; ri < l; ++ri) \
2164 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2165 else \
2166 for (ri = 0; ri < l; ++ri) \
2167 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002169# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2170 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172
2173 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002174 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176#ifdef FEAT_SIGNS
2177 /* If signs are being displayed, add two spaces. */
2178 if (draw_signcolumn(wp))
2179 {
2180 len = W_WIDTH(wp) - col;
2181 if (len > 0)
2182 {
2183 if (len > 2)
2184 len = 2;
2185# ifdef FEAT_RIGHTLEFT
2186 if (wp->w_p_rl)
2187 /* the line number isn't reversed */
2188 copy_text_attr(off + W_WIDTH(wp) - len - col,
2189 (char_u *)" ", len, hl_attr(HLF_FL));
2190 else
2191# endif
2192 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2193 col += len;
2194 }
2195 }
2196#endif
2197
2198 /*
2199 * 3. Add the 'number' column
2200 */
2201 if (wp->w_p_nu)
2202 {
2203 len = W_WIDTH(wp) - col;
2204 if (len > 0)
2205 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002206 int w = number_width(wp);
2207
2208 if (len > w + 1)
2209 len = w + 1;
2210 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_RIGHTLEFT
2212 if (wp->w_p_rl)
2213 /* the line number isn't reversed */
2214 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2215 hl_attr(HLF_FL));
2216 else
2217#endif
2218 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2219 col += len;
2220 }
2221 }
2222
2223 /*
2224 * 4. Compose the folded-line string with 'foldtext', if set.
2225 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002226 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228 txtcol = col; /* remember where text starts */
2229
2230 /*
2231 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2232 * Right-left text is put in columns 0 - number-col, normal text is put
2233 * in columns number-col - window-width.
2234 */
2235#ifdef FEAT_MBYTE
2236 if (has_mbyte)
2237 {
2238 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002239 int u8c, u8cc[MAX_MCO];
2240 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 int idx;
2242 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002243 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244# ifdef FEAT_ARABIC
2245 int prev_c = 0; /* previous Arabic character */
2246 int prev_c1 = 0; /* first composing char for prev_c */
2247# endif
2248
2249# ifdef FEAT_RIGHTLEFT
2250 if (wp->w_p_rl)
2251 idx = off;
2252 else
2253# endif
2254 idx = off + col;
2255
2256 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2257 for (p = text; *p != NUL; )
2258 {
2259 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002260 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (col + cells > W_WIDTH(wp)
2262# ifdef FEAT_RIGHTLEFT
2263 - (wp->w_p_rl ? col : 0)
2264# endif
2265 )
2266 break;
2267 ScreenLines[idx] = *p;
2268 if (enc_utf8)
2269 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002270 u8c = utfc_ptr2char(p, u8cc);
2271 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 ScreenLinesUC[idx] = 0;
2274#ifdef FEAT_ARABIC
2275 prev_c = u8c;
2276#endif
2277 }
2278 else
2279 {
2280#ifdef FEAT_ARABIC
2281 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2282 {
2283 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002284 int pc, pc1, nc;
2285 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 int firstbyte = *p;
2287
2288 /* The idea of what is the previous and next
2289 * character depends on 'rightleft'. */
2290 if (wp->w_p_rl)
2291 {
2292 pc = prev_c;
2293 pc1 = prev_c1;
2294 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002295 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 else
2298 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002299 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002301 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 prev_c = u8c;
2304
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002305 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 pc, pc1, nc);
2307 ScreenLines[idx] = firstbyte;
2308 }
2309 else
2310 prev_c = u8c;
2311#endif
2312 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002313#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (u8c >= 0x10000)
2315 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2316 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002319 for (i = 0; i < Screen_mco; ++i)
2320 {
2321 ScreenLinesC[i][idx] = u8cc[i];
2322 if (u8cc[i] == 0)
2323 break;
2324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326 if (cells > 1)
2327 ScreenLines[idx + 1] = 0;
2328 }
2329 else if (cells > 1) /* double-byte character */
2330 {
2331 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2332 ScreenLines2[idx] = p[1];
2333 else
2334 ScreenLines[idx + 1] = p[1];
2335 }
2336 col += cells;
2337 idx += cells;
2338 p += c_len;
2339 }
2340 }
2341 else
2342#endif
2343 {
2344 len = (int)STRLEN(text);
2345 if (len > W_WIDTH(wp) - col)
2346 len = W_WIDTH(wp) - col;
2347 if (len > 0)
2348 {
2349#ifdef FEAT_RIGHTLEFT
2350 if (wp->w_p_rl)
2351 STRNCPY(current_ScreenLine, text, len);
2352 else
2353#endif
2354 STRNCPY(current_ScreenLine + col, text, len);
2355 col += len;
2356 }
2357 }
2358
2359 /* Fill the rest of the line with the fold filler */
2360#ifdef FEAT_RIGHTLEFT
2361 if (wp->w_p_rl)
2362 col -= txtcol;
2363#endif
2364 while (col < W_WIDTH(wp)
2365#ifdef FEAT_RIGHTLEFT
2366 - (wp->w_p_rl ? txtcol : 0)
2367#endif
2368 )
2369 {
2370#ifdef FEAT_MBYTE
2371 if (enc_utf8)
2372 {
2373 if (fill_fold >= 0x80)
2374 {
2375 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002376 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378 else
2379 ScreenLinesUC[off + col] = 0;
2380 }
2381#endif
2382 ScreenLines[off + col++] = fill_fold;
2383 }
2384
2385 if (text != buf)
2386 vim_free(text);
2387
2388 /*
2389 * 6. set highlighting for the Visual area an other text.
2390 * If all folded lines are in the Visual area, highlight the line.
2391 */
2392#ifdef FEAT_VISUAL
2393 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2394 {
2395 if (ltoreq(curwin->w_cursor, VIsual))
2396 {
2397 /* Visual is after curwin->w_cursor */
2398 top = &curwin->w_cursor;
2399 bot = &VIsual;
2400 }
2401 else
2402 {
2403 /* Visual is before curwin->w_cursor */
2404 top = &VIsual;
2405 bot = &curwin->w_cursor;
2406 }
2407 if (lnum >= top->lnum
2408 && lnume <= bot->lnum
2409 && (VIsual_mode != 'v'
2410 || ((lnum > top->lnum
2411 || (lnum == top->lnum
2412 && top->col == 0))
2413 && (lnume < bot->lnum
2414 || (lnume == bot->lnum
2415 && (bot->col - (*p_sel == 'e'))
2416 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2417 {
2418 if (VIsual_mode == Ctrl_V)
2419 {
2420 /* Visual block mode: highlight the chars part of the block */
2421 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2422 {
2423 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2424 len = wp->w_old_cursor_lcol;
2425 else
2426 len = W_WIDTH(wp) - txtcol;
2427 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002428 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430 }
2431 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002434 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 }
2438#endif
2439
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002440#ifdef FEAT_SYN_HL
2441 /* Show 'cursorcolumn' in the fold line. */
2442 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2443 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2444 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
2447 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2448 (int)W_WIDTH(wp), FALSE);
2449
2450 /*
2451 * Update w_cline_height and w_cline_folded if the cursor line was
2452 * updated (saves a call to plines() later).
2453 */
2454 if (wp == curwin
2455 && lnum <= curwin->w_cursor.lnum
2456 && lnume >= curwin->w_cursor.lnum)
2457 {
2458 curwin->w_cline_row = row;
2459 curwin->w_cline_height = 1;
2460 curwin->w_cline_folded = TRUE;
2461 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2462 }
2463}
2464
2465/*
2466 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2467 */
2468 static void
2469copy_text_attr(off, buf, len, attr)
2470 int off;
2471 char_u *buf;
2472 int len;
2473 int attr;
2474{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002475 int i;
2476
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 mch_memmove(ScreenLines + off, buf, (size_t)len);
2478# ifdef FEAT_MBYTE
2479 if (enc_utf8)
2480 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2481# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002482 for (i = 0; i < len; ++i)
2483 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484}
2485
2486/*
2487 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002488 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
2490 static void
2491fill_foldcolumn(p, wp, closed, lnum)
2492 char_u *p;
2493 win_T *wp;
2494 int closed; /* TRUE of FALSE */
2495 linenr_T lnum; /* current line number */
2496{
2497 int i = 0;
2498 int level;
2499 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002500 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 /* Init to all spaces. */
2503 copy_spaces(p, (size_t)wp->w_p_fdc);
2504
2505 level = win_foldinfo.fi_level;
2506 if (level > 0)
2507 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002508 /* If there is only one column put more info in it. */
2509 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 /* If the column is too narrow, we start at the lowest level that
2512 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002513 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (first_level < 1)
2515 first_level = 1;
2516
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002517 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 if (win_foldinfo.fi_lnum == lnum
2520 && first_level + i >= win_foldinfo.fi_low_level)
2521 p[i] = '-';
2522 else if (first_level == 1)
2523 p[i] = '|';
2524 else if (first_level + i <= 9)
2525 p[i] = '0' + first_level + i;
2526 else
2527 p[i] = '>';
2528 if (first_level + i == level)
2529 break;
2530 }
2531 }
2532 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002533 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534}
2535#endif /* FEAT_FOLDING */
2536
2537/*
2538 * Display line "lnum" of window 'wp' on the screen.
2539 * Start at row "startrow", stop when "endrow" is reached.
2540 * wp->w_virtcol needs to be valid.
2541 *
2542 * Return the number of last row the line occupies.
2543 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002544/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002546win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 win_T *wp;
2548 linenr_T lnum;
2549 int startrow;
2550 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002551 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 int col; /* visual column on screen */
2554 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2555 int c = 0; /* init for GCC */
2556 long vcol = 0; /* virtual column (for tabs) */
2557 long vcol_prev = -1; /* "vcol" of previous character */
2558 char_u *line; /* current line */
2559 char_u *ptr; /* current position in "line" */
2560 int row; /* row in the window, excl w_winrow */
2561 int screen_row; /* row on the screen, incl w_winrow */
2562
2563 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2564 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002565 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 int c_extra = NUL; /* extra chars, all the same */
2567 int extra_attr = 0; /* attributes when n_extra != 0 */
2568 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2569 displaying lcs_eol at end-of-line */
2570 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2571 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2572
2573 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2574 int saved_n_extra = 0;
2575 char_u *saved_p_extra = NULL;
2576 int saved_c_extra = 0;
2577 int saved_char_attr = 0;
2578
2579 int n_attr = 0; /* chars with special attr */
2580 int saved_attr2 = 0; /* char_attr saved for n_attr */
2581 int n_attr3 = 0; /* chars with overruling special attr */
2582 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2583
2584 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2585
2586 int fromcol, tocol; /* start/end of inverting */
2587 int fromcol_prev = -2; /* start of inverting after cursor */
2588 int noinvcur = FALSE; /* don't invert the cursor */
2589#ifdef FEAT_VISUAL
2590 pos_T *top, *bot;
2591#endif
2592 pos_T pos;
2593 long v;
2594
2595 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002596 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2598 in this line */
2599 int attr = 0; /* attributes for area highlighting */
2600 int area_attr = 0; /* attributes desired by highlighting */
2601 int search_attr = 0; /* attributes desired by 'hlsearch' */
2602#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002603 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 int syntax_attr = 0; /* attributes desired by syntax */
2605 int has_syntax = FALSE; /* this buffer has syntax highl. */
2606 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002607 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002608#endif
2609#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002610 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002611# define SPWORDLEN 150
2612 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002613 int nextlinecol = 0; /* column where nextline[] starts */
2614 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002615 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002616 int spell_attr = 0; /* attributes desired by spelling */
2617 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002618 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2619 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002620 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002621 static int cap_col = -1; /* column to check for Cap word */
2622 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002623 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624#endif
2625 int extra_check; /* has syntax or linebreak */
2626#ifdef FEAT_MBYTE
2627 int multi_attr = 0; /* attributes desired by multibyte */
2628 int mb_l = 1; /* multi-byte byte length */
2629 int mb_c = 0; /* decoded multi-byte character */
2630 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002631 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632#endif
2633#ifdef FEAT_DIFF
2634 int filler_lines; /* nr of filler lines to be drawn */
2635 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002636 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 int change_start = MAXCOL; /* first col of changed area */
2638 int change_end = -1; /* last col of changed area */
2639#endif
2640 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2641#ifdef FEAT_LINEBREAK
2642 int need_showbreak = FALSE;
2643#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002644#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2645 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002647 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648#endif
2649#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002650 matchitem_T *cur; /* points to the match list */
2651 match_T *shl; /* points to search_hl or a match */
2652 int shl_flag; /* flag to indicate whether search_hl
2653 has been processed or not */
2654 int prevcol_hl_flag; /* flag to indicate whether prevcol
2655 equals startcol of search_hl or one
2656 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658#ifdef FEAT_ARABIC
2659 int prev_c = 0; /* previous Arabic character */
2660 int prev_c1 = 0; /* first composing char for prev_c */
2661#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002662#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002663 int did_line_attr = 0;
2664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 /* draw_state: items that are drawn in sequence: */
2667#define WL_START 0 /* nothing done yet */
2668#ifdef FEAT_CMDWIN
2669# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2670#else
2671# define WL_CMDLINE WL_START
2672#endif
2673#ifdef FEAT_FOLDING
2674# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2675#else
2676# define WL_FOLD WL_CMDLINE
2677#endif
2678#ifdef FEAT_SIGNS
2679# define WL_SIGN WL_FOLD + 1 /* column for signs */
2680#else
2681# define WL_SIGN WL_FOLD /* column for signs */
2682#endif
2683#define WL_NR WL_SIGN + 1 /* line number */
2684#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2685# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2686#else
2687# define WL_SBR WL_NR
2688#endif
2689#define WL_LINE WL_SBR + 1 /* text in the line */
2690 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002691#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 int feedback_col = 0;
2693 int feedback_old_attr = -1;
2694#endif
2695
2696
2697 if (startrow > endrow) /* past the end already! */
2698 return startrow;
2699
2700 row = startrow;
2701 screen_row = row + W_WINROW(wp);
2702
2703 /*
2704 * To speed up the loop below, set extra_check when there is linebreak,
2705 * trailing white space and/or syntax processing to be done.
2706 */
2707#ifdef FEAT_LINEBREAK
2708 extra_check = wp->w_p_lbr;
2709#else
2710 extra_check = 0;
2711#endif
2712#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002713 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
2715 /* Prepare for syntax highlighting in this line. When there is an
2716 * error, stop syntax highlighting. */
2717 save_did_emsg = did_emsg;
2718 did_emsg = FALSE;
2719 syntax_start(wp, lnum);
2720 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002721 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 else
2723 {
2724 did_emsg = save_did_emsg;
2725 has_syntax = TRUE;
2726 extra_check = TRUE;
2727 }
2728 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002729#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002730
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002731#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002732 if (wp->w_p_spell
2733 && *wp->w_buffer->b_p_spl != NUL
2734 && wp->w_buffer->b_langp.ga_len > 0
2735 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002736 {
2737 /* Prepare for spell checking. */
2738 has_spell = TRUE;
2739 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002740
2741 /* Get the start of the next line, so that words that wrap to the next
2742 * line are found too: "et<line-break>al.".
2743 * Trick: skip a few chars for C/shell/Vim comments */
2744 nextline[SPWORDLEN] = NUL;
2745 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2746 {
2747 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2748 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2749 }
2750
2751 /* When a word wrapped from the previous line the start of the current
2752 * line is valid. */
2753 if (lnum == checked_lnum)
2754 cur_checked_col = checked_col;
2755 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002756
2757 /* When there was a sentence end in the previous line may require a
2758 * word starting with capital in this line. In line 1 always check
2759 * the first word. */
2760 if (lnum != capcol_lnum)
2761 cap_col = -1;
2762 if (lnum == 1)
2763 cap_col = 0;
2764 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766#endif
2767
2768 /*
2769 * handle visual active in this window
2770 */
2771 fromcol = -10;
2772 tocol = MAXCOL;
2773#ifdef FEAT_VISUAL
2774 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2775 {
2776 /* Visual is after curwin->w_cursor */
2777 if (ltoreq(curwin->w_cursor, VIsual))
2778 {
2779 top = &curwin->w_cursor;
2780 bot = &VIsual;
2781 }
2782 else /* Visual is before curwin->w_cursor */
2783 {
2784 top = &VIsual;
2785 bot = &curwin->w_cursor;
2786 }
2787 if (VIsual_mode == Ctrl_V) /* block mode */
2788 {
2789 if (lnum >= top->lnum && lnum <= bot->lnum)
2790 {
2791 fromcol = wp->w_old_cursor_fcol;
2792 tocol = wp->w_old_cursor_lcol;
2793 }
2794 }
2795 else /* non-block mode */
2796 {
2797 if (lnum > top->lnum && lnum <= bot->lnum)
2798 fromcol = 0;
2799 else if (lnum == top->lnum)
2800 {
2801 if (VIsual_mode == 'V') /* linewise */
2802 fromcol = 0;
2803 else
2804 {
2805 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2806 if (gchar_pos(top) == NUL)
2807 tocol = fromcol + 1;
2808 }
2809 }
2810 if (VIsual_mode != 'V' && lnum == bot->lnum)
2811 {
2812 if (*p_sel == 'e' && bot->col == 0
2813#ifdef FEAT_VIRTUALEDIT
2814 && bot->coladd == 0
2815#endif
2816 )
2817 {
2818 fromcol = -10;
2819 tocol = MAXCOL;
2820 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002821 else if (bot->col == MAXCOL)
2822 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 else
2824 {
2825 pos = *bot;
2826 if (*p_sel == 'e')
2827 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2828 else
2829 {
2830 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2831 ++tocol;
2832 }
2833 }
2834 }
2835 }
2836
2837#ifndef MSDOS
2838 /* Check if the character under the cursor should not be inverted */
2839 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2840# ifdef FEAT_GUI
2841 && !gui.in_use
2842# endif
2843 )
2844 noinvcur = TRUE;
2845#endif
2846
2847 /* if inverting in this line set area_highlighting */
2848 if (fromcol >= 0)
2849 {
2850 area_highlighting = TRUE;
2851 attr = hl_attr(HLF_V);
2852#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2853 if (clip_star.available && !clip_star.owned && clip_isautosel())
2854 attr = hl_attr(HLF_VNC);
2855#endif
2856 }
2857 }
2858
2859 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002860 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
2862 else
2863#endif /* FEAT_VISUAL */
2864 if (highlight_match
2865 && wp == curwin
2866 && lnum >= curwin->w_cursor.lnum
2867 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2868 {
2869 if (lnum == curwin->w_cursor.lnum)
2870 getvcol(curwin, &(curwin->w_cursor),
2871 (colnr_T *)&fromcol, NULL, NULL);
2872 else
2873 fromcol = 0;
2874 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2875 {
2876 pos.lnum = lnum;
2877 pos.col = search_match_endcol;
2878 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2879 }
2880 else
2881 tocol = MAXCOL;
2882 if (fromcol == tocol) /* do at least one character */
2883 tocol = fromcol + 1; /* happens when past end of line */
2884 area_highlighting = TRUE;
2885 attr = hl_attr(HLF_I);
2886 }
2887
2888#ifdef FEAT_DIFF
2889 filler_lines = diff_check(wp, lnum);
2890 if (filler_lines < 0)
2891 {
2892 if (filler_lines == -1)
2893 {
2894 if (diff_find_change(wp, lnum, &change_start, &change_end))
2895 diff_hlf = HLF_ADD; /* added line */
2896 else if (change_start == 0)
2897 diff_hlf = HLF_TXD; /* changed text */
2898 else
2899 diff_hlf = HLF_CHD; /* changed line */
2900 }
2901 else
2902 diff_hlf = HLF_ADD; /* added line */
2903 filler_lines = 0;
2904 area_highlighting = TRUE;
2905 }
2906 if (lnum == wp->w_topline)
2907 filler_lines = wp->w_topfill;
2908 filler_todo = filler_lines;
2909#endif
2910
2911#ifdef LINE_ATTR
2912# ifdef FEAT_SIGNS
2913 /* If this line has a sign with line highlighting set line_attr. */
2914 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2915 if (v != 0)
2916 line_attr = sign_get_attr((int)v, TRUE);
2917# endif
2918# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2919 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 line_attr = hl_attr(HLF_L);
2922# endif
2923 if (line_attr != 0)
2924 area_highlighting = TRUE;
2925#endif
2926
2927 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2928 ptr = line;
2929
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002930#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002931 if (has_spell)
2932 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002933 /* For checking first word with a capital skip white space. */
2934 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002935 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002936
Bram Moolenaar30abd282005-06-22 22:35:10 +00002937 /* To be able to spell-check over line boundaries copy the end of the
2938 * current line into nextline[]. Above the start of the next line was
2939 * copied to nextline[SPWORDLEN]. */
2940 if (nextline[SPWORDLEN] == NUL)
2941 {
2942 /* No next line or it is empty. */
2943 nextlinecol = MAXCOL;
2944 nextline_idx = 0;
2945 }
2946 else
2947 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002948 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002949 if (v < SPWORDLEN)
2950 {
2951 /* Short line, use it completely and append the start of the
2952 * next line. */
2953 nextlinecol = 0;
2954 mch_memmove(nextline, line, (size_t)v);
2955 mch_memmove(nextline + v, nextline + SPWORDLEN,
2956 STRLEN(nextline + SPWORDLEN) + 1);
2957 nextline_idx = v + 1;
2958 }
2959 else
2960 {
2961 /* Long line, use only the last SPWORDLEN bytes. */
2962 nextlinecol = v - SPWORDLEN;
2963 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2964 nextline_idx = SPWORDLEN + 1;
2965 }
2966 }
2967 }
2968#endif
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 /* find start of trailing whitespace */
2971 if (wp->w_p_list && lcs_trail)
2972 {
2973 trailcol = (colnr_T)STRLEN(ptr);
2974 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2975 --trailcol;
2976 trailcol += (colnr_T) (ptr - line);
2977 extra_check = TRUE;
2978 }
2979
2980 /*
2981 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2982 * first character to be displayed.
2983 */
2984 if (wp->w_p_wrap)
2985 v = wp->w_skipcol;
2986 else
2987 v = wp->w_leftcol;
2988 if (v > 0)
2989 {
2990#ifdef FEAT_MBYTE
2991 char_u *prev_ptr = ptr;
2992#endif
2993 while (vcol < v && *ptr != NUL)
2994 {
2995 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2996 vcol += c;
2997#ifdef FEAT_MBYTE
2998 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003000 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002
3003#ifdef FEAT_VIRTUALEDIT
3004 /* When 'virtualedit' is set the end of the line may be before the
3005 * start of the displayed part. */
3006 if (vcol < v && *ptr == NUL && virtual_active())
3007 vcol = v;
3008#endif
3009
3010 /* Handle a character that's not completely on the screen: Put ptr at
3011 * that character but skip the first few screen characters. */
3012 if (vcol > v)
3013 {
3014 vcol -= c;
3015#ifdef FEAT_MBYTE
3016 ptr = prev_ptr;
3017#else
3018 --ptr;
3019#endif
3020 n_skip = v - vcol;
3021 }
3022
3023 /*
3024 * Adjust for when the inverted text is before the screen,
3025 * and when the start of the inverted text is before the screen.
3026 */
3027 if (tocol <= vcol)
3028 fromcol = 0;
3029 else if (fromcol >= 0 && fromcol < vcol)
3030 fromcol = vcol;
3031
3032#ifdef FEAT_LINEBREAK
3033 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3034 if (wp->w_p_wrap)
3035 need_showbreak = TRUE;
3036#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003037#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003038 /* When spell checking a word we need to figure out the start of the
3039 * word and if it's badly spelled or not. */
3040 if (has_spell)
3041 {
3042 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003043 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003044 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003045
3046 pos = wp->w_cursor;
3047 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003048 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003049 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003050
3051 /* spell_move_to() may call ml_get() and make "line" invalid */
3052 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3053 ptr = line + linecol;
3054
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003055 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003056 {
3057 /* no bad word found at line start, don't check until end of a
3058 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003059 spell_hlf = HLF_COUNT;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003060 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3061 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003062 }
3063 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003064 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003065 /* bad word found, use attributes until end of word */
3066 word_end = wp->w_cursor.col + len + 1;
3067
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003068 /* Turn index into actual attributes. */
3069 if (spell_hlf != HLF_COUNT)
3070 spell_attr = highlight_attr[spell_hlf];
3071 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003072 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003073
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003074# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003075 /* Need to restart syntax highlighting for this line. */
3076 if (has_syntax)
3077 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003078# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003079 }
3080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
3082
3083 /*
3084 * Correct highlighting for cursor that can't be disabled.
3085 * Avoids having to check this for each character.
3086 */
3087 if (fromcol >= 0)
3088 {
3089 if (noinvcur)
3090 {
3091 if ((colnr_T)fromcol == wp->w_virtcol)
3092 {
3093 /* highlighting starts at cursor, let it start just after the
3094 * cursor */
3095 fromcol_prev = fromcol;
3096 fromcol = -1;
3097 }
3098 else if ((colnr_T)fromcol < wp->w_virtcol)
3099 /* restart highlighting after the cursor */
3100 fromcol_prev = wp->w_virtcol;
3101 }
3102 if (fromcol >= tocol)
3103 fromcol = -1;
3104 }
3105
3106#ifdef FEAT_SEARCH_EXTRA
3107 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003108 * Handle highlighting the last used search pattern and matches.
3109 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003111 cur = wp->w_match_head;
3112 shl_flag = FALSE;
3113 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003115 if (shl_flag == FALSE)
3116 {
3117 shl = &search_hl;
3118 shl_flag = TRUE;
3119 }
3120 else
3121 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003122 shl->startcol = MAXCOL;
3123 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 shl->attr_cur = 0;
3125 if (shl->rm.regprog != NULL)
3126 {
3127 v = (long)(ptr - line);
3128 next_search_hl(wp, shl, lnum, (colnr_T)v);
3129
3130 /* Need to get the line again, a multi-line regexp may have made it
3131 * invalid. */
3132 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3133 ptr = line + v;
3134
3135 if (shl->lnum != 0 && shl->lnum <= lnum)
3136 {
3137 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003138 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003140 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3142 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003143 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003145 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003147 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
3149#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003150 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003151 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 else
3153#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003154 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003156 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 {
3158 shl->attr_cur = shl->attr;
3159 search_attr = shl->attr;
3160 }
3161 area_highlighting = TRUE;
3162 }
3163 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003164 if (shl != &search_hl && cur != NULL)
3165 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
3167#endif
3168
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003169#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003170 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3171 * active, because it's not clear what is selected then. */
3172 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003173 {
3174 line_attr = hl_attr(HLF_CUL);
3175 area_highlighting = TRUE;
3176 }
3177#endif
3178
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003179 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 col = 0;
3181#ifdef FEAT_RIGHTLEFT
3182 if (wp->w_p_rl)
3183 {
3184 /* Rightleft window: process the text in the normal direction, but put
3185 * it in current_ScreenLine[] from right to left. Start at the
3186 * rightmost column of the window. */
3187 col = W_WIDTH(wp) - 1;
3188 off += col;
3189 }
3190#endif
3191
3192 /*
3193 * Repeat for the whole displayed line.
3194 */
3195 for (;;)
3196 {
3197 /* Skip this quickly when working on the text. */
3198 if (draw_state != WL_LINE)
3199 {
3200#ifdef FEAT_CMDWIN
3201 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3202 {
3203 draw_state = WL_CMDLINE;
3204 if (cmdwin_type != 0 && wp == curwin)
3205 {
3206 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003208 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 char_attr = hl_attr(HLF_AT);
3210 }
3211 }
3212#endif
3213
3214#ifdef FEAT_FOLDING
3215 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3216 {
3217 draw_state = WL_FOLD;
3218 if (wp->w_p_fdc > 0)
3219 {
3220 /* Draw the 'foldcolumn'. */
3221 fill_foldcolumn(extra, wp, FALSE, lnum);
3222 n_extra = wp->w_p_fdc;
3223 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003224 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 c_extra = NUL;
3226 char_attr = hl_attr(HLF_FC);
3227 }
3228 }
3229#endif
3230
3231#ifdef FEAT_SIGNS
3232 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3233 {
3234 draw_state = WL_SIGN;
3235 /* Show the sign column when there are any signs in this
3236 * buffer or when using Netbeans. */
3237 if (draw_signcolumn(wp)
3238# ifdef FEAT_DIFF
3239 && filler_todo <= 0
3240# endif
3241 )
3242 {
3243 int_u text_sign;
3244# ifdef FEAT_SIGN_ICONS
3245 int_u icon_sign;
3246# endif
3247
3248 /* Draw two cells with the sign value or blank. */
3249 c_extra = ' ';
3250 char_attr = hl_attr(HLF_SC);
3251 n_extra = 2;
3252
3253 if (row == startrow)
3254 {
3255 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3256 SIGN_TEXT);
3257# ifdef FEAT_SIGN_ICONS
3258 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3259 SIGN_ICON);
3260 if (gui.in_use && icon_sign != 0)
3261 {
3262 /* Use the image in this position. */
3263 c_extra = SIGN_BYTE;
3264# ifdef FEAT_NETBEANS_INTG
3265 if (buf_signcount(wp->w_buffer, lnum) > 1)
3266 c_extra = MULTISIGN_BYTE;
3267# endif
3268 char_attr = icon_sign;
3269 }
3270 else
3271# endif
3272 if (text_sign != 0)
3273 {
3274 p_extra = sign_get_text(text_sign);
3275 if (p_extra != NULL)
3276 {
3277 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003278 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 char_attr = sign_get_attr(text_sign, FALSE);
3281 }
3282 }
3283 }
3284 }
3285#endif
3286
3287 if (draw_state == WL_NR - 1 && n_extra == 0)
3288 {
3289 draw_state = WL_NR;
3290 /* Display the line number. After the first fill with blanks
3291 * when the 'n' flag isn't in 'cpo' */
3292 if (wp->w_p_nu
3293 && (row == startrow
3294#ifdef FEAT_DIFF
3295 + filler_lines
3296#endif
3297 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3298 {
3299 /* Draw the line number (empty space after wrapping). */
3300 if (row == startrow
3301#ifdef FEAT_DIFF
3302 + filler_lines
3303#endif
3304 )
3305 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003306 sprintf((char *)extra, "%*ld ",
3307 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (wp->w_skipcol > 0)
3309 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3310 *p_extra = '-';
3311#ifdef FEAT_RIGHTLEFT
3312 if (wp->w_p_rl) /* reverse line numbers */
3313 rl_mirror(extra);
3314#endif
3315 p_extra = extra;
3316 c_extra = NUL;
3317 }
3318 else
3319 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003320 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003322#ifdef FEAT_SYN_HL
3323 /* When 'cursorline' is set highlight the line number of
3324 * the current line differently. */
3325 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3326 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
3329 }
3330
3331#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3332 if (draw_state == WL_SBR - 1 && n_extra == 0)
3333 {
3334 draw_state = WL_SBR;
3335# ifdef FEAT_DIFF
3336 if (filler_todo > 0)
3337 {
3338 /* Draw "deleted" diff line(s). */
3339 if (char2cells(fill_diff) > 1)
3340 c_extra = '-';
3341 else
3342 c_extra = fill_diff;
3343# ifdef FEAT_RIGHTLEFT
3344 if (wp->w_p_rl)
3345 n_extra = col + 1;
3346 else
3347# endif
3348 n_extra = W_WIDTH(wp) - col;
3349 char_attr = hl_attr(HLF_DED);
3350 }
3351# endif
3352# ifdef FEAT_LINEBREAK
3353 if (*p_sbr != NUL && need_showbreak)
3354 {
3355 /* Draw 'showbreak' at the start of each broken line. */
3356 p_extra = p_sbr;
3357 c_extra = NUL;
3358 n_extra = (int)STRLEN(p_sbr);
3359 char_attr = hl_attr(HLF_AT);
3360 need_showbreak = FALSE;
3361 /* Correct end of highlighted area for 'showbreak',
3362 * required when 'linebreak' is also set. */
3363 if (tocol == vcol)
3364 tocol += n_extra;
3365 }
3366# endif
3367 }
3368#endif
3369
3370 if (draw_state == WL_LINE - 1 && n_extra == 0)
3371 {
3372 draw_state = WL_LINE;
3373 if (saved_n_extra)
3374 {
3375 /* Continue item from end of wrapped line. */
3376 n_extra = saved_n_extra;
3377 c_extra = saved_c_extra;
3378 p_extra = saved_p_extra;
3379 char_attr = saved_char_attr;
3380 }
3381 else
3382 char_attr = 0;
3383 }
3384 }
3385
3386 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003387 if (dollar_vcol != 0 && wp == curwin
3388 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#ifdef FEAT_DIFF
3390 && filler_todo <= 0
3391#endif
3392 )
3393 {
3394 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3395 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003396 /* Pretend we have finished updating the window. Except when
3397 * 'cursorcolumn' is set. */
3398#ifdef FEAT_SYN_HL
3399 if (wp->w_p_cuc)
3400 row = wp->w_cline_row + wp->w_cline_height;
3401 else
3402#endif
3403 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 break;
3405 }
3406
3407 if (draw_state == WL_LINE && area_highlighting)
3408 {
3409 /* handle Visual or match highlighting in this line */
3410 if (vcol == fromcol
3411#ifdef FEAT_MBYTE
3412 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3413 && (*mb_ptr2cells)(ptr) > 1)
3414#endif
3415 || ((int)vcol_prev == fromcol_prev
3416 && vcol < tocol))
3417 area_attr = attr; /* start highlighting */
3418 else if (area_attr != 0
3419 && (vcol == tocol
3420 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423#ifdef FEAT_SEARCH_EXTRA
3424 if (!n_extra)
3425 {
3426 /*
3427 * Check for start/end of search pattern match.
3428 * After end, check for start/end of next match.
3429 * When another match, have to check for start again.
3430 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003431 * Do this for 'search_hl' and the match list (ordered by
3432 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003434 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003435 cur = wp->w_match_head;
3436 shl_flag = FALSE;
3437 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003439 if (shl_flag == FALSE
3440 && ((cur != NULL
3441 && cur->priority > SEARCH_HL_PRIORITY)
3442 || cur == NULL))
3443 {
3444 shl = &search_hl;
3445 shl_flag = TRUE;
3446 }
3447 else
3448 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 while (shl->rm.regprog != NULL)
3450 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003451 if (shl->startcol != MAXCOL
3452 && v >= (long)shl->startcol
3453 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
3455 shl->attr_cur = shl->attr;
3456 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003457 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 shl->attr_cur = 0;
3460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 next_search_hl(wp, shl, lnum, (colnr_T)v);
3462
3463 /* Need to get the line again, a multi-line regexp
3464 * may have made it invalid. */
3465 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3466 ptr = line + v;
3467
3468 if (shl->lnum == lnum)
3469 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003470 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003472 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003474 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003476 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
3478 /* highlight empty match, try again after
3479 * it */
3480#ifdef FEAT_MBYTE
3481 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003482 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003483 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else
3485#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003486 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488
3489 /* Loop to check if the match starts at the
3490 * current position */
3491 continue;
3492 }
3493 }
3494 break;
3495 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003496 if (shl != &search_hl && cur != NULL)
3497 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003499
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003500 /* Use attributes from match with highest priority among
3501 * 'search_hl' and the match list. */
3502 search_attr = search_hl.attr_cur;
3503 cur = wp->w_match_head;
3504 shl_flag = FALSE;
3505 while (cur != NULL || shl_flag == FALSE)
3506 {
3507 if (shl_flag == FALSE
3508 && ((cur != NULL
3509 && cur->priority > SEARCH_HL_PRIORITY)
3510 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003511 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003512 shl = &search_hl;
3513 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003514 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003515 else
3516 shl = &cur->hl;
3517 if (shl->attr_cur != 0)
3518 search_attr = shl->attr_cur;
3519 if (shl != &search_hl && cur != NULL)
3520 cur = cur->next;
3521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523#endif
3524
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003526 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003528 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3529 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003531 if (diff_hlf == HLF_TXD && ptr - line > change_end
3532 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003534 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003537
3538 /* Decide which of the highlight attributes to use. */
3539 attr_pri = TRUE;
3540 if (area_attr != 0)
3541 char_attr = area_attr;
3542 else if (search_attr != 0)
3543 char_attr = search_attr;
3544#ifdef LINE_ATTR
3545 /* Use line_attr when not in the Visual or 'incsearch' area
3546 * (area_attr may be 0 when "noinvcur" is set). */
3547 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3548 || (vcol < fromcol || vcol >= tocol)))
3549 char_attr = line_attr;
3550#endif
3551 else
3552 {
3553 attr_pri = FALSE;
3554#ifdef FEAT_SYN_HL
3555 if (has_syntax)
3556 char_attr = syntax_attr;
3557 else
3558#endif
3559 char_attr = 0;
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562
3563 /*
3564 * Get the next character to put on the screen.
3565 */
3566 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003567 * The "p_extra" points to the extra stuff that is inserted to
3568 * represent special characters (non-printable stuff) and other
3569 * things. When all characters are the same, c_extra is used.
3570 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3571 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3573 */
3574 if (n_extra > 0)
3575 {
3576 if (c_extra != NUL)
3577 {
3578 c = c_extra;
3579#ifdef FEAT_MBYTE
3580 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3581 if (enc_utf8 && (*mb_char2len)(c) > 1)
3582 {
3583 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003584 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003585 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587 else
3588 mb_utf8 = FALSE;
3589#endif
3590 }
3591 else
3592 {
3593 c = *p_extra;
3594#ifdef FEAT_MBYTE
3595 if (has_mbyte)
3596 {
3597 mb_c = c;
3598 if (enc_utf8)
3599 {
3600 /* If the UTF-8 character is more than one byte:
3601 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003602 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 mb_utf8 = FALSE;
3604 if (mb_l > n_extra)
3605 mb_l = 1;
3606 else if (mb_l > 1)
3607 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003608 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003610 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 }
3613 else
3614 {
3615 /* if this is a DBCS character, put it in "mb_c" */
3616 mb_l = MB_BYTE2LEN(c);
3617 if (mb_l >= n_extra)
3618 mb_l = 1;
3619 else if (mb_l > 1)
3620 mb_c = (c << 8) + p_extra[1];
3621 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003622 if (mb_l == 0) /* at the NUL at end-of-line */
3623 mb_l = 1;
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 /* If a double-width char doesn't fit display a '>' in the
3626 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003627 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628# ifdef FEAT_RIGHTLEFT
3629 wp->w_p_rl ? (col <= 0) :
3630# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003631 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 && (*mb_char2cells)(mb_c) == 2)
3633 {
3634 c = '>';
3635 mb_c = c;
3636 mb_l = 1;
3637 mb_utf8 = FALSE;
3638 multi_attr = hl_attr(HLF_AT);
3639 /* put the pointer back to output the double-width
3640 * character at the start of the next line. */
3641 ++n_extra;
3642 --p_extra;
3643 }
3644 else
3645 {
3646 n_extra -= mb_l - 1;
3647 p_extra += mb_l - 1;
3648 }
3649 }
3650#endif
3651 ++p_extra;
3652 }
3653 --n_extra;
3654 }
3655 else
3656 {
3657 /*
3658 * Get a character from the line itself.
3659 */
3660 c = *ptr;
3661#ifdef FEAT_MBYTE
3662 if (has_mbyte)
3663 {
3664 mb_c = c;
3665 if (enc_utf8)
3666 {
3667 /* If the UTF-8 character is more than one byte: Decode it
3668 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003669 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 mb_utf8 = FALSE;
3671 if (mb_l > 1)
3672 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003673 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 /* Overlong encoded ASCII or ASCII with composing char
3675 * is displayed normally, except a NUL. */
3676 if (mb_c < 0x80)
3677 c = mb_c;
3678 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003679
3680 /* At start of the line we can have a composing char.
3681 * Draw it as a space with a composing char. */
3682 if (utf_iscomposing(mb_c))
3683 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003684 int i;
3685
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003686 for (i = Screen_mco - 1; i > 0; --i)
3687 u8cc[i] = u8cc[i - 1];
3688 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003689 mb_c = ' ';
3690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692
3693 if ((mb_l == 1 && c >= 0x80)
3694 || (mb_l >= 1 && mb_c == 0)
3695 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003696# ifdef UNICODE16
3697 || mb_c >= 0x10000
3698# endif
3699 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
3701 /*
3702 * Illegal UTF-8 byte: display as <xx>.
3703 * Non-BMP character : display as ? or fullwidth ?.
3704 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003705# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
3709 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003710# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 if (wp->w_p_rl) /* reverse */
3712 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003715# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 else if (utf_char2cells(mb_c) != 2)
3717 STRCPY(extra, "?");
3718 else
3719 /* 0xff1f in UTF-8: full-width '?' */
3720 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 p_extra = extra;
3724 c = *p_extra;
3725 mb_c = mb_ptr2char_adv(&p_extra);
3726 mb_utf8 = (c >= 0x80);
3727 n_extra = (int)STRLEN(p_extra);
3728 c_extra = NUL;
3729 if (area_attr == 0 && search_attr == 0)
3730 {
3731 n_attr = n_extra + 1;
3732 extra_attr = hl_attr(HLF_8);
3733 saved_attr2 = char_attr; /* save current attr */
3734 }
3735 }
3736 else if (mb_l == 0) /* at the NUL at end-of-line */
3737 mb_l = 1;
3738#ifdef FEAT_ARABIC
3739 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3740 {
3741 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003742 int pc, pc1, nc;
3743 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 /* The idea of what is the previous and next
3746 * character depends on 'rightleft'. */
3747 if (wp->w_p_rl)
3748 {
3749 pc = prev_c;
3750 pc1 = prev_c1;
3751 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003752 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 else
3755 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003756 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003758 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 prev_c = mb_c;
3761
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003762 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
3764 else
3765 prev_c = mb_c;
3766#endif
3767 }
3768 else /* enc_dbcs */
3769 {
3770 mb_l = MB_BYTE2LEN(c);
3771 if (mb_l == 0) /* at the NUL at end-of-line */
3772 mb_l = 1;
3773 else if (mb_l > 1)
3774 {
3775 /* We assume a second byte below 32 is illegal.
3776 * Hopefully this is OK for all double-byte encodings!
3777 */
3778 if (ptr[1] >= 32)
3779 mb_c = (c << 8) + ptr[1];
3780 else
3781 {
3782 if (ptr[1] == NUL)
3783 {
3784 /* head byte at end of line */
3785 mb_l = 1;
3786 transchar_nonprint(extra, c);
3787 }
3788 else
3789 {
3790 /* illegal tail byte */
3791 mb_l = 2;
3792 STRCPY(extra, "XX");
3793 }
3794 p_extra = extra;
3795 n_extra = (int)STRLEN(extra) - 1;
3796 c_extra = NUL;
3797 c = *p_extra++;
3798 if (area_attr == 0 && search_attr == 0)
3799 {
3800 n_attr = n_extra + 1;
3801 extra_attr = hl_attr(HLF_8);
3802 saved_attr2 = char_attr; /* save current attr */
3803 }
3804 mb_c = c;
3805 }
3806 }
3807 }
3808 /* If a double-width char doesn't fit display a '>' in the
3809 * last column; the character is displayed at the start of the
3810 * next line. */
3811 if ((
3812# ifdef FEAT_RIGHTLEFT
3813 wp->w_p_rl ? (col <= 0) :
3814# endif
3815 (col >= W_WIDTH(wp) - 1))
3816 && (*mb_char2cells)(mb_c) == 2)
3817 {
3818 c = '>';
3819 mb_c = c;
3820 mb_utf8 = FALSE;
3821 mb_l = 1;
3822 multi_attr = hl_attr(HLF_AT);
3823 /* Put pointer back so that the character will be
3824 * displayed at the start of the next line. */
3825 --ptr;
3826 }
3827 else if (*ptr != NUL)
3828 ptr += mb_l - 1;
3829
3830 /* If a double-width char doesn't fit at the left side display
3831 * a '<' in the first column. */
3832 if (n_skip > 0 && mb_l > 1)
3833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003835 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 c = ' ';
3837 if (area_attr == 0 && search_attr == 0)
3838 {
3839 n_attr = n_extra + 1;
3840 extra_attr = hl_attr(HLF_AT);
3841 saved_attr2 = char_attr; /* save current attr */
3842 }
3843 mb_c = c;
3844 mb_utf8 = FALSE;
3845 mb_l = 1;
3846 }
3847
3848 }
3849#endif
3850 ++ptr;
3851
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003852 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003853 if (wp->w_p_list && (c == 160
3854#ifdef FEAT_MBYTE
3855 || (mb_utf8 && mb_c == 160)
3856#endif
3857 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003858 {
3859 c = lcs_nbsp;
3860 if (area_attr == 0 && search_attr == 0)
3861 {
3862 n_attr = 1;
3863 extra_attr = hl_attr(HLF_8);
3864 saved_attr2 = char_attr; /* save current attr */
3865 }
3866#ifdef FEAT_MBYTE
3867 mb_c = c;
3868 if (enc_utf8 && (*mb_char2len)(c) > 1)
3869 {
3870 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003871 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003872 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003873 }
3874 else
3875 mb_utf8 = FALSE;
3876#endif
3877 }
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (extra_check)
3880 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003881#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003882 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003883#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003884
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003885#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 /* Get syntax attribute, unless still at the start of the line
3887 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003888 v = (long)(ptr - line);
3889 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891 /* Get the syntax attribute for the character. If there
3892 * is an error, disable syntax highlighting. */
3893 save_did_emsg = did_emsg;
3894 did_emsg = FALSE;
3895
Bram Moolenaar217ad922005-03-20 22:37:15 +00003896 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003897# ifdef FEAT_SPELL
3898 has_spell ? &can_spell :
3899# endif
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00003900 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
3902 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003903 {
3904 wp->w_buffer->b_syn_error = TRUE;
3905 has_syntax = FALSE;
3906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 else
3908 did_emsg = save_did_emsg;
3909
3910 /* Need to get the line again, a multi-line regexp may
3911 * have made it invalid. */
3912 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3913 ptr = line + v;
3914
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003915 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003917 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003918 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003920#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003921
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003922#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003923 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003924 * Only do this when there is no syntax highlighting, the
3925 * @Spell cluster is not used or the current syntax item
3926 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003927 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003928 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003929 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003930# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003931 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003932 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003933# endif
3934 if (c != 0 && (
3935# ifdef FEAT_SYN_HL
3936 !has_syntax ||
3937# endif
3938 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003939 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003940 char_u *prev_ptr, *p;
3941 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003942 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003943# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003944 if (has_mbyte)
3945 {
3946 prev_ptr = ptr - mb_l;
3947 v -= mb_l - 1;
3948 }
3949 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003950# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003951 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003952
3953 /* Use nextline[] if possible, it has the start of the
3954 * next line concatenated. */
3955 if ((prev_ptr - line) - nextlinecol >= 0)
3956 p = nextline + (prev_ptr - line) - nextlinecol;
3957 else
3958 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003959 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003960 len = spell_check(wp, p, &spell_hlf, &cap_col,
3961 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003962 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003963
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003964 /* In Insert mode only highlight a word that
3965 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003966 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003967 && (State & INSERT) != 0
3968 && wp->w_cursor.lnum == lnum
3969 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003970 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003971 && wp->w_cursor.col < (colnr_T)word_end)
3972 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003973 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003974 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003975 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003976
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003977 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003978 && (p - nextline) + len > nextline_idx)
3979 {
3980 /* Remember that the good word continues at the
3981 * start of the next line. */
3982 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003983 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003984 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003985
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003986 /* Turn index into actual attributes. */
3987 if (spell_hlf != HLF_COUNT)
3988 spell_attr = highlight_attr[spell_hlf];
3989
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003990 if (cap_col > 0)
3991 {
3992 if (p != prev_ptr
3993 && (p - nextline) + cap_col >= nextline_idx)
3994 {
3995 /* Remember that the word in the next line
3996 * must start with a capital. */
3997 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003998 cap_col = (int)((p - nextline) + cap_col
3999 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004000 }
4001 else
4002 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004003 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004004 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004005 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004006 }
4007 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004008 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004009 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004010 char_attr = hl_combine_attr(char_attr, spell_attr);
4011 else
4012 char_attr = hl_combine_attr(spell_attr, char_attr);
4013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#endif
4015#ifdef FEAT_LINEBREAK
4016 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004017 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 */
4019 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
4022 n_extra = win_lbr_chartabsize(wp, ptr - (
4023# ifdef FEAT_MBYTE
4024 has_mbyte ? mb_l :
4025# endif
4026 1), (colnr_T)vcol, NULL) - 1;
4027 c_extra = ' ';
4028 if (vim_iswhite(c))
4029 c = ' ';
4030 }
4031#endif
4032
4033 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4034 {
4035 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004036 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038 n_attr = 1;
4039 extra_attr = hl_attr(HLF_8);
4040 saved_attr2 = char_attr; /* save current attr */
4041 }
4042#ifdef FEAT_MBYTE
4043 mb_c = c;
4044 if (enc_utf8 && (*mb_char2len)(c) > 1)
4045 {
4046 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004047 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004048 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 else
4051 mb_utf8 = FALSE;
4052#endif
4053 }
4054 }
4055
4056 /*
4057 * Handling of non-printable characters.
4058 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
4061 /*
4062 * when getting a character from the file, we may have to
4063 * turn it into something else on the way to putting it
4064 * into "ScreenLines".
4065 */
4066 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4067 {
4068 /* tab amount depends on current column */
4069 n_extra = (int)wp->w_buffer->b_p_ts
4070 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4071#ifdef FEAT_MBYTE
4072 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4073#endif
4074 if (wp->w_p_list)
4075 {
4076 c = lcs_tab1;
4077 c_extra = lcs_tab2;
4078 n_attr = n_extra + 1;
4079 extra_attr = hl_attr(HLF_8);
4080 saved_attr2 = char_attr; /* save current attr */
4081#ifdef FEAT_MBYTE
4082 mb_c = c;
4083 if (enc_utf8 && (*mb_char2len)(c) > 1)
4084 {
4085 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004086 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004087 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089#endif
4090 }
4091 else
4092 {
4093 c_extra = ' ';
4094 c = ' ';
4095 }
4096 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004097 else if (c == NUL
4098 && ((wp->w_p_list && lcs_eol > 0)
4099 || ((fromcol >= 0 || fromcol_prev >= 0)
4100 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004101#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004102 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004103#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004104 && (
4105# ifdef FEAT_RIGHTLEFT
4106 wp->w_p_rl ? (col >= 0) :
4107# endif
4108 (col < W_WIDTH(wp)))
4109 && !(noinvcur
4110 && (colnr_T)vcol == wp->w_virtcol)))
4111 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004113 /* Display a '$' after the line or highlight an extra
4114 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4116 /* For a diff line the highlighting continues after the
4117 * "$". */
4118 if (
4119# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004120 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121# ifdef LINE_ATTR
4122 &&
4123# endif
4124# endif
4125# ifdef LINE_ATTR
4126 line_attr == 0
4127# endif
4128 )
4129#endif
4130 {
4131#ifdef FEAT_VIRTUALEDIT
4132 /* In virtualedit, visual selections may extend
4133 * beyond end of line. */
4134 if (area_highlighting && virtual_active()
4135 && tocol != MAXCOL && vcol < tocol)
4136 n_extra = 0;
4137 else
4138#endif
4139 {
4140 p_extra = at_end_str;
4141 n_extra = 1;
4142 c_extra = NUL;
4143 }
4144 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004145 if (wp->w_p_list)
4146 c = lcs_eol;
4147 else
4148 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 lcs_eol_one = -1;
4150 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004151 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
4153 extra_attr = hl_attr(HLF_AT);
4154 n_attr = 1;
4155 }
4156#ifdef FEAT_MBYTE
4157 mb_c = c;
4158 if (enc_utf8 && (*mb_char2len)(c) > 1)
4159 {
4160 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004161 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004162 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164 else
4165 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4166#endif
4167 }
4168 else if (c != NUL)
4169 {
4170 p_extra = transchar(c);
4171#ifdef FEAT_RIGHTLEFT
4172 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4173 rl_mirror(p_extra); /* reverse "<12>" */
4174#endif
4175 n_extra = byte2cells(c) - 1;
4176 c_extra = NUL;
4177 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004178 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 n_attr = n_extra + 1;
4181 extra_attr = hl_attr(HLF_8);
4182 saved_attr2 = char_attr; /* save current attr */
4183 }
4184#ifdef FEAT_MBYTE
4185 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4186#endif
4187 }
4188#ifdef FEAT_VIRTUALEDIT
4189 else if (VIsual_active
4190 && (VIsual_mode == Ctrl_V
4191 || VIsual_mode == 'v')
4192 && virtual_active()
4193 && tocol != MAXCOL
4194 && vcol < tocol
4195 && (
4196# ifdef FEAT_RIGHTLEFT
4197 wp->w_p_rl ? (col >= 0) :
4198# endif
4199 (col < W_WIDTH(wp))))
4200 {
4201 c = ' ';
4202 --ptr; /* put it back at the NUL */
4203 }
4204#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004205#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 else if ((
4207# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004208 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 ) && (
4212# ifdef FEAT_RIGHTLEFT
4213 wp->w_p_rl ? (col >= 0) :
4214# endif
4215 (col < W_WIDTH(wp))))
4216 {
4217 /* Highlight until the right side of the window */
4218 c = ' ';
4219 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004220
4221 /* Remember we do the char for line highlighting. */
4222 ++did_line_attr;
4223
4224 /* don't do search HL for the rest of the line */
4225 if (line_attr != 0 && char_attr == search_attr && col > 0)
4226 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227# ifdef FEAT_DIFF
4228 if (diff_hlf == HLF_TXD)
4229 {
4230 diff_hlf = HLF_CHD;
4231 if (attr == 0 || char_attr != attr)
4232 char_attr = hl_attr(diff_hlf);
4233 }
4234# endif
4235 }
4236#endif
4237 }
4238 }
4239
4240 /* Don't override visual selection highlighting. */
4241 if (n_attr > 0
4242 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004243 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 char_attr = extra_attr;
4245
Bram Moolenaar81695252004-12-29 20:58:21 +00004246#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 /* XIM don't send preedit_start and preedit_end, but they send
4248 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4249 * im_is_preediting() here. */
4250 if (xic != NULL
4251 && lnum == curwin->w_cursor.lnum
4252 && (State & INSERT)
4253 && !p_imdisable
4254 && im_is_preediting()
4255 && draw_state == WL_LINE)
4256 {
4257 colnr_T tcol;
4258
4259 if (preedit_end_col == MAXCOL)
4260 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4261 else
4262 tcol = preedit_end_col;
4263 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4264 {
4265 if (feedback_old_attr < 0)
4266 {
4267 feedback_col = 0;
4268 feedback_old_attr = char_attr;
4269 }
4270 char_attr = im_get_feedback_attr(feedback_col);
4271 if (char_attr < 0)
4272 char_attr = feedback_old_attr;
4273 feedback_col++;
4274 }
4275 else if (feedback_old_attr >= 0)
4276 {
4277 char_attr = feedback_old_attr;
4278 feedback_old_attr = -1;
4279 feedback_col = 0;
4280 }
4281 }
4282#endif
4283 /*
4284 * Handle the case where we are in column 0 but not on the first
4285 * character of the line and the user wants us to show us a
4286 * special character (via 'listchars' option "precedes:<char>".
4287 */
4288 if (lcs_prec_todo != NUL
4289 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4290#ifdef FEAT_DIFF
4291 && filler_todo <= 0
4292#endif
4293 && draw_state > WL_NR
4294 && c != NUL)
4295 {
4296 c = lcs_prec;
4297 lcs_prec_todo = NUL;
4298#ifdef FEAT_MBYTE
4299 mb_c = c;
4300 if (enc_utf8 && (*mb_char2len)(c) > 1)
4301 {
4302 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004303 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004304 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else
4307 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4308#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004309 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 {
4311 saved_attr3 = char_attr; /* save current attr */
4312 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4313 n_attr3 = 1;
4314 }
4315 }
4316
4317 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004318 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004320 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004321#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004322 || did_line_attr == 1
4323#endif
4324 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004326#ifdef FEAT_SEARCH_EXTRA
4327 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004328
4329 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004330 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004331 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004332#endif
4333
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /* invert at least one char, used for Visual and empty line or
4335 * highlight match at end of line. If it's beyond the last
4336 * char on the screen, just overwrite that one (tricky!) Not
4337 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004338#ifdef FEAT_SEARCH_EXTRA
4339 prevcol_hl_flag = FALSE;
4340 if (prevcol == (long)search_hl.startcol)
4341 prevcol_hl_flag = TRUE;
4342 else
4343 {
4344 cur = wp->w_match_head;
4345 while (cur != NULL)
4346 {
4347 if (prevcol == (long)cur->hl.startcol)
4348 {
4349 prevcol_hl_flag = TRUE;
4350 break;
4351 }
4352 cur = cur->next;
4353 }
4354 }
4355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004357 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#ifdef FEAT_SEARCH_EXTRA
4359 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004360 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004361# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004362 && did_line_attr <= 1
4363# endif
4364 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365#endif
4366 ))
4367 {
4368 int n = 0;
4369
4370#ifdef FEAT_RIGHTLEFT
4371 if (wp->w_p_rl)
4372 {
4373 if (col < 0)
4374 n = 1;
4375 }
4376 else
4377#endif
4378 {
4379 if (col >= W_WIDTH(wp))
4380 n = -1;
4381 }
4382 if (n != 0)
4383 {
4384 /* At the window boundary, highlight the last character
4385 * instead (better than nothing). */
4386 off += n;
4387 col += n;
4388 }
4389 else
4390 {
4391 /* Add a blank character to highlight. */
4392 ScreenLines[off] = ' ';
4393#ifdef FEAT_MBYTE
4394 if (enc_utf8)
4395 ScreenLinesUC[off] = 0;
4396#endif
4397 }
4398#ifdef FEAT_SEARCH_EXTRA
4399 if (area_attr == 0)
4400 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004401 /* Use attributes from match with highest priority among
4402 * 'search_hl' and the match list. */
4403 char_attr = search_hl.attr;
4404 cur = wp->w_match_head;
4405 shl_flag = FALSE;
4406 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004407 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004408 if (shl_flag == FALSE
4409 && ((cur != NULL
4410 && cur->priority > SEARCH_HL_PRIORITY)
4411 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004412 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004413 shl = &search_hl;
4414 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004415 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004416 else
4417 shl = &cur->hl;
4418 if ((ptr - line) - 1 == (long)shl->startcol)
4419 char_attr = shl->attr;
4420 if (shl != &search_hl && cur != NULL)
4421 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424#endif
4425 ScreenAttrs[off] = char_attr;
4426#ifdef FEAT_RIGHTLEFT
4427 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004430 --off;
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 else
4433#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004436 ++off;
4437 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004438 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004439#ifdef FEAT_SYN_HL
4440 eol_hl_off = 1;
4441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaar91170f82006-05-05 21:15:17 +00004445 /*
4446 * At end of the text line.
4447 */
4448 if (c == NUL)
4449 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004450#ifdef FEAT_SYN_HL
Bram Moolenaara443af82007-11-08 13:51:42 +00004451 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol)
4452 {
4453 /* highlight last char after line */
4454 --col;
4455 --off;
4456 --vcol;
4457 }
4458
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004459 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004460 if (wp->w_p_wrap)
4461 v = wp->w_skipcol;
4462 else
4463 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004464 /* check if line ends before left margin */
4465 if (vcol < v + col - win_col_off(wp))
4466
4467 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004468 if (wp->w_p_cuc
Bram Moolenaara443af82007-11-08 13:51:42 +00004469 && (int)wp->w_virtcol >= vcol - eol_hl_off
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004470 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4471 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004472 && lnum != wp->w_cursor.lnum
4473# ifdef FEAT_RIGHTLEFT
4474 && !wp->w_p_rl
4475# endif
4476 )
4477 {
4478 while (col < W_WIDTH(wp))
4479 {
4480 ScreenLines[off] = ' ';
4481#ifdef FEAT_MBYTE
4482 if (enc_utf8)
4483 ScreenLinesUC[off] = 0;
4484#endif
4485 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004486 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004487 {
4488 ScreenAttrs[off] = hl_attr(HLF_CUC);
4489 break;
4490 }
4491 ScreenAttrs[off++] = 0;
4492 ++vcol;
4493 }
4494 }
4495#endif
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4498 wp->w_p_rl);
4499 row++;
4500
4501 /*
4502 * Update w_cline_height and w_cline_folded if the cursor line was
4503 * updated (saves a call to plines() later).
4504 */
4505 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4506 {
4507 curwin->w_cline_row = startrow;
4508 curwin->w_cline_height = row - startrow;
4509#ifdef FEAT_FOLDING
4510 curwin->w_cline_folded = FALSE;
4511#endif
4512 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4513 }
4514
4515 break;
4516 }
4517
4518 /* line continues beyond line end */
4519 if (lcs_ext
4520 && !wp->w_p_wrap
4521#ifdef FEAT_DIFF
4522 && filler_todo <= 0
4523#endif
4524 && (
4525#ifdef FEAT_RIGHTLEFT
4526 wp->w_p_rl ? col == 0 :
4527#endif
4528 col == W_WIDTH(wp) - 1)
4529 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004530 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4532 {
4533 c = lcs_ext;
4534 char_attr = hl_attr(HLF_AT);
4535#ifdef FEAT_MBYTE
4536 mb_c = c;
4537 if (enc_utf8 && (*mb_char2len)(c) > 1)
4538 {
4539 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004540 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004541 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543 else
4544 mb_utf8 = FALSE;
4545#endif
4546 }
4547
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004548#ifdef FEAT_SYN_HL
4549 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4550 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004551 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004552 && lnum != wp->w_cursor.lnum
4553 && draw_state == WL_LINE)
4554 {
4555 vcol_save_attr = char_attr;
4556 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4557 }
4558 else
4559 vcol_save_attr = -1;
4560#endif
4561
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 /*
4563 * Store character to be displayed.
4564 * Skip characters that are left of the screen for 'nowrap'.
4565 */
4566 vcol_prev = vcol;
4567 if (draw_state < WL_LINE || n_skip <= 0)
4568 {
4569 /*
4570 * Store the character.
4571 */
4572#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4573 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4574 {
4575 /* A double-wide character is: put first halve in left cell. */
4576 --off;
4577 --col;
4578 }
4579#endif
4580 ScreenLines[off] = c;
4581#ifdef FEAT_MBYTE
4582 if (enc_dbcs == DBCS_JPNU)
4583 ScreenLines2[off] = mb_c & 0xff;
4584 else if (enc_utf8)
4585 {
4586 if (mb_utf8)
4587 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004588 int i;
4589
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004591 if ((c & 0xff) == 0)
4592 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004593 for (i = 0; i < Screen_mco; ++i)
4594 {
4595 ScreenLinesC[i][off] = u8cc[i];
4596 if (u8cc[i] == 0)
4597 break;
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600 else
4601 ScreenLinesUC[off] = 0;
4602 }
4603 if (multi_attr)
4604 {
4605 ScreenAttrs[off] = multi_attr;
4606 multi_attr = 0;
4607 }
4608 else
4609#endif
4610 ScreenAttrs[off] = char_attr;
4611
4612#ifdef FEAT_MBYTE
4613 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4614 {
4615 /* Need to fill two screen columns. */
4616 ++off;
4617 ++col;
4618 if (enc_utf8)
4619 /* UTF-8: Put a 0 in the second screen char. */
4620 ScreenLines[off] = 0;
4621 else
4622 /* DBCS: Put second byte in the second screen char. */
4623 ScreenLines[off] = mb_c & 0xff;
4624 ++vcol;
4625 /* When "tocol" is halfway a character, set it to the end of
4626 * the character, otherwise highlighting won't stop. */
4627 if (tocol == vcol)
4628 ++tocol;
4629#ifdef FEAT_RIGHTLEFT
4630 if (wp->w_p_rl)
4631 {
4632 /* now it's time to backup one cell */
4633 --off;
4634 --col;
4635 }
4636#endif
4637 }
4638#endif
4639#ifdef FEAT_RIGHTLEFT
4640 if (wp->w_p_rl)
4641 {
4642 --off;
4643 --col;
4644 }
4645 else
4646#endif
4647 {
4648 ++off;
4649 ++col;
4650 }
4651 }
4652 else
4653 --n_skip;
4654
4655 /* Only advance the "vcol" when after the 'number' column. */
4656 if (draw_state >= WL_SBR
4657#ifdef FEAT_DIFF
4658 && filler_todo <= 0
4659#endif
4660 )
4661 ++vcol;
4662
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004663#ifdef FEAT_SYN_HL
4664 if (vcol_save_attr >= 0)
4665 char_attr = vcol_save_attr;
4666#endif
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /* restore attributes after "predeces" in 'listchars' */
4669 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4670 char_attr = saved_attr3;
4671
4672 /* restore attributes after last 'listchars' or 'number' char */
4673 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4674 char_attr = saved_attr2;
4675
4676 /*
4677 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004678 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 */
4680 if ((
4681#ifdef FEAT_RIGHTLEFT
4682 wp->w_p_rl ? (col < 0) :
4683#endif
4684 (col >= W_WIDTH(wp)))
4685 && (*ptr != NUL
4686#ifdef FEAT_DIFF
4687 || filler_todo > 0
4688#endif
4689 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4690 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4691 )
4692 {
4693 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4694 wp->w_p_rl);
4695 ++row;
4696 ++screen_row;
4697
4698 /* When not wrapping and finished diff lines, or when displayed
4699 * '$' and highlighting until last column, break here. */
4700 if ((!wp->w_p_wrap
4701#ifdef FEAT_DIFF
4702 && filler_todo <= 0
4703#endif
4704 ) || lcs_eol_one == -1)
4705 break;
4706
4707 /* When the window is too narrow draw all "@" lines. */
4708 if (draw_state != WL_LINE
4709#ifdef FEAT_DIFF
4710 && filler_todo <= 0
4711#endif
4712 )
4713 {
4714 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4715#ifdef FEAT_VERTSPLIT
4716 draw_vsep_win(wp, row);
4717#endif
4718 row = endrow;
4719 }
4720
4721 /* When line got too long for screen break here. */
4722 if (row == endrow)
4723 {
4724 ++row;
4725 break;
4726 }
4727
4728 if (screen_cur_row == screen_row - 1
4729#ifdef FEAT_DIFF
4730 && filler_todo <= 0
4731#endif
4732 && W_WIDTH(wp) == Columns)
4733 {
4734 /* Remember that the line wraps, used for modeless copy. */
4735 LineWraps[screen_row - 1] = TRUE;
4736
4737 /*
4738 * Special trick to make copy/paste of wrapped lines work with
4739 * xterm/screen: write an extra character beyond the end of
4740 * the line. This will work with all terminal types
4741 * (regardless of the xn,am settings).
4742 * Only do this on a fast tty.
4743 * Only do this if the cursor is on the current line
4744 * (something has been written in it).
4745 * Don't do this for the GUI.
4746 * Don't do this for double-width characters.
4747 * Don't do this for a window not at the right screen border.
4748 */
4749 if (p_tf
4750#ifdef FEAT_GUI
4751 && !gui.in_use
4752#endif
4753#ifdef FEAT_MBYTE
4754 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004755 && ((*mb_off2cells)(LineOffset[screen_row],
4756 LineOffset[screen_row] + screen_Columns)
4757 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004759 + (int)Columns - 2,
4760 LineOffset[screen_row] + screen_Columns)
4761 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762#endif
4763 )
4764 {
4765 /* First make sure we are at the end of the screen line,
4766 * then output the same character again to let the
4767 * terminal know about the wrap. If the terminal doesn't
4768 * auto-wrap, we overwrite the character. */
4769 if (screen_cur_col != W_WIDTH(wp))
4770 screen_char(LineOffset[screen_row - 1]
4771 + (unsigned)Columns - 1,
4772 screen_row - 1, (int)(Columns - 1));
4773
4774#ifdef FEAT_MBYTE
4775 /* When there is a multi-byte character, just output a
4776 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004777 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4778 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 out_char(' ');
4780 else
4781#endif
4782 out_char(ScreenLines[LineOffset[screen_row - 1]
4783 + (Columns - 1)]);
4784 /* force a redraw of the first char on the next line */
4785 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4786 screen_start(); /* don't know where cursor is now */
4787 }
4788 }
4789
4790 col = 0;
4791 off = (unsigned)(current_ScreenLine - ScreenLines);
4792#ifdef FEAT_RIGHTLEFT
4793 if (wp->w_p_rl)
4794 {
4795 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4796 off += col;
4797 }
4798#endif
4799
4800 /* reset the drawing state for the start of a wrapped line */
4801 draw_state = WL_START;
4802 saved_n_extra = n_extra;
4803 saved_p_extra = p_extra;
4804 saved_c_extra = c_extra;
4805 saved_char_attr = char_attr;
4806 n_extra = 0;
4807 lcs_prec_todo = lcs_prec;
4808#ifdef FEAT_LINEBREAK
4809# ifdef FEAT_DIFF
4810 if (filler_todo <= 0)
4811# endif
4812 need_showbreak = TRUE;
4813#endif
4814#ifdef FEAT_DIFF
4815 --filler_todo;
4816 /* When the filler lines are actually below the last line of the
4817 * file, don't draw the line itself, break here. */
4818 if (filler_todo == 0 && wp->w_botfill)
4819 break;
4820#endif
4821 }
4822
4823 } /* for every character in the line */
4824
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004825#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004826 /* After an empty line check first word for capital. */
4827 if (*skipwhite(line) == NUL)
4828 {
4829 capcol_lnum = lnum + 1;
4830 cap_col = 0;
4831 }
4832#endif
4833
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return row;
4835}
4836
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004837#ifdef FEAT_MBYTE
4838static int comp_char_differs __ARGS((int, int));
4839
4840/*
4841 * Return if the composing characters at "off_from" and "off_to" differ.
4842 */
4843 static int
4844comp_char_differs(off_from, off_to)
4845 int off_from;
4846 int off_to;
4847{
4848 int i;
4849
4850 for (i = 0; i < Screen_mco; ++i)
4851 {
4852 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4853 return TRUE;
4854 if (ScreenLinesC[i][off_from] == 0)
4855 break;
4856 }
4857 return FALSE;
4858}
4859#endif
4860
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861/*
4862 * Check whether the given character needs redrawing:
4863 * - the (first byte of the) character is different
4864 * - the attributes are different
4865 * - the character is multi-byte and the next byte is different
4866 */
4867 static int
4868char_needs_redraw(off_from, off_to, cols)
4869 int off_from;
4870 int off_to;
4871 int cols;
4872{
4873 if (cols > 0
4874 && ((ScreenLines[off_from] != ScreenLines[off_to]
4875 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4876
4877#ifdef FEAT_MBYTE
4878 || (enc_dbcs != 0
4879 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4880 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4881 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4882 : (cols > 1 && ScreenLines[off_from + 1]
4883 != ScreenLines[off_to + 1])))
4884 || (enc_utf8
4885 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4886 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004887 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#endif
4889 ))
4890 return TRUE;
4891 return FALSE;
4892}
4893
4894/*
4895 * Move one "cooked" screen line to the screen, but only the characters that
4896 * have actually changed. Handle insert/delete character.
4897 * "coloff" gives the first column on the screen for this line.
4898 * "endcol" gives the columns where valid characters are.
4899 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4900 * needs to be cleared, negative otherwise.
4901 * "rlflag" is TRUE in a rightleft window:
4902 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4903 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4904 */
4905 static void
4906screen_line(row, coloff, endcol, clear_width
4907#ifdef FEAT_RIGHTLEFT
4908 , rlflag
4909#endif
4910 )
4911 int row;
4912 int coloff;
4913 int endcol;
4914 int clear_width;
4915#ifdef FEAT_RIGHTLEFT
4916 int rlflag;
4917#endif
4918{
4919 unsigned off_from;
4920 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004921#ifdef FEAT_MBYTE
4922 unsigned max_off_from;
4923 unsigned max_off_to;
4924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 int col = 0;
4926#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4927 int hl;
4928#endif
4929 int force = FALSE; /* force update rest of the line */
4930 int redraw_this /* bool: does character need redraw? */
4931#ifdef FEAT_GUI
4932 = TRUE /* For GUI when while-loop empty */
4933#endif
4934 ;
4935 int redraw_next; /* redraw_this for next character */
4936#ifdef FEAT_MBYTE
4937 int clear_next = FALSE;
4938 int char_cells; /* 1: normal char */
4939 /* 2: occupies two display cells */
4940# define CHAR_CELLS char_cells
4941#else
4942# define CHAR_CELLS 1
4943#endif
4944
4945# ifdef FEAT_CLIPBOARD
4946 clip_may_clear_selection(row, row);
4947# endif
4948
4949 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4950 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004951#ifdef FEAT_MBYTE
4952 max_off_from = off_from + screen_Columns;
4953 max_off_to = LineOffset[row] + screen_Columns;
4954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
4956#ifdef FEAT_RIGHTLEFT
4957 if (rlflag)
4958 {
4959 /* Clear rest first, because it's left of the text. */
4960 if (clear_width > 0)
4961 {
4962 while (col <= endcol && ScreenLines[off_to] == ' '
4963 && ScreenAttrs[off_to] == 0
4964# ifdef FEAT_MBYTE
4965 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4966# endif
4967 )
4968 {
4969 ++off_to;
4970 ++col;
4971 }
4972 if (col <= endcol)
4973 screen_fill(row, row + 1, col + coloff,
4974 endcol + coloff + 1, ' ', ' ', 0);
4975 }
4976 col = endcol + 1;
4977 off_to = LineOffset[row] + col + coloff;
4978 off_from += col;
4979 endcol = (clear_width > 0 ? clear_width : -clear_width);
4980 }
4981#endif /* FEAT_RIGHTLEFT */
4982
4983 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4984
4985 while (col < endcol)
4986 {
4987#ifdef FEAT_MBYTE
4988 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00004989 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 else
4991 char_cells = 1;
4992#endif
4993
4994 redraw_this = redraw_next;
4995 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4996 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4997
4998#ifdef FEAT_GUI
4999 /* If the next character was bold, then redraw the current character to
5000 * remove any pixels that might have spilt over into us. This only
5001 * happens in the GUI.
5002 */
5003 if (redraw_next && gui.in_use)
5004 {
5005 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005006 if (hl > HL_ALL)
5007 hl = syn_attr2attr(hl);
5008 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 redraw_this = TRUE;
5010 }
5011#endif
5012
5013 if (redraw_this)
5014 {
5015 /*
5016 * Special handling when 'xs' termcap flag set (hpterm):
5017 * Attributes for characters are stored at the position where the
5018 * cursor is when writing the highlighting code. The
5019 * start-highlighting code must be written with the cursor on the
5020 * first highlighted character. The stop-highlighting code must
5021 * be written with the cursor just after the last highlighted
5022 * character.
5023 * Overwriting a character doesn't remove it's highlighting. Need
5024 * to clear the rest of the line, and force redrawing it
5025 * completely.
5026 */
5027 if ( p_wiv
5028 && !force
5029#ifdef FEAT_GUI
5030 && !gui.in_use
5031#endif
5032 && ScreenAttrs[off_to] != 0
5033 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5034 {
5035 /*
5036 * Need to remove highlighting attributes here.
5037 */
5038 windgoto(row, col + coloff);
5039 out_str(T_CE); /* clear rest of this screen line */
5040 screen_start(); /* don't know where cursor is now */
5041 force = TRUE; /* force redraw of rest of the line */
5042 redraw_next = TRUE; /* or else next char would miss out */
5043
5044 /*
5045 * If the previous character was highlighted, need to stop
5046 * highlighting at this character.
5047 */
5048 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5049 {
5050 screen_attr = ScreenAttrs[off_to - 1];
5051 term_windgoto(row, col + coloff);
5052 screen_stop_highlight();
5053 }
5054 else
5055 screen_attr = 0; /* highlighting has stopped */
5056 }
5057#ifdef FEAT_MBYTE
5058 if (enc_dbcs != 0)
5059 {
5060 /* Check if overwriting a double-byte with a single-byte or
5061 * the other way around requires another character to be
5062 * redrawn. For UTF-8 this isn't needed, because comparing
5063 * ScreenLinesUC[] is sufficient. */
5064 if (char_cells == 1
5065 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005066 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
5068 /* Writing a single-cell character over a double-cell
5069 * character: need to redraw the next cell. */
5070 ScreenLines[off_to + 1] = 0;
5071 redraw_next = TRUE;
5072 }
5073 else if (char_cells == 2
5074 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005075 && (*mb_off2cells)(off_to, max_off_to) == 1
5076 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 /* Writing the second half of a double-cell character over
5079 * a double-cell character: need to redraw the second
5080 * cell. */
5081 ScreenLines[off_to + 2] = 0;
5082 redraw_next = TRUE;
5083 }
5084
5085 if (enc_dbcs == DBCS_JPNU)
5086 ScreenLines2[off_to] = ScreenLines2[off_from];
5087 }
5088 /* When writing a single-width character over a double-width
5089 * character and at the end of the redrawn text, need to clear out
5090 * the right halve of the old character.
5091 * Also required when writing the right halve of a double-width
5092 * char over the left halve of an existing one. */
5093 if (has_mbyte && col + char_cells == endcol
5094 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005095 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005097 && (*mb_off2cells)(off_to, max_off_to) == 1
5098 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 clear_next = TRUE;
5100#endif
5101
5102 ScreenLines[off_to] = ScreenLines[off_from];
5103#ifdef FEAT_MBYTE
5104 if (enc_utf8)
5105 {
5106 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5107 if (ScreenLinesUC[off_from] != 0)
5108 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005109 int i;
5110
5111 for (i = 0; i < Screen_mco; ++i)
5112 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
5114 }
5115 if (char_cells == 2)
5116 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5117#endif
5118
5119#if defined(FEAT_GUI) || defined(UNIX)
5120 /* The bold trick makes a single row of pixels appear in the next
5121 * character. When a bold character is removed, the next
5122 * character should be redrawn too. This happens for our own GUI
5123 * and for some xterms. */
5124 if (
5125# ifdef FEAT_GUI
5126 gui.in_use
5127# endif
5128# if defined(FEAT_GUI) && defined(UNIX)
5129 ||
5130# endif
5131# ifdef UNIX
5132 term_is_xterm
5133# endif
5134 )
5135 {
5136 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005137 if (hl > HL_ALL)
5138 hl = syn_attr2attr(hl);
5139 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 redraw_next = TRUE;
5141 }
5142#endif
5143 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5144#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005145 /* For simplicity set the attributes of second half of a
5146 * double-wide character equal to the first half. */
5147 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005149
5150 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 else
5153#endif
5154 screen_char(off_to, row, col + coloff);
5155 }
5156 else if ( p_wiv
5157#ifdef FEAT_GUI
5158 && !gui.in_use
5159#endif
5160 && col + coloff > 0)
5161 {
5162 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5163 {
5164 /*
5165 * Don't output stop-highlight when moving the cursor, it will
5166 * stop the highlighting when it should continue.
5167 */
5168 screen_attr = 0;
5169 }
5170 else if (screen_attr != 0)
5171 screen_stop_highlight();
5172 }
5173
5174 off_to += CHAR_CELLS;
5175 off_from += CHAR_CELLS;
5176 col += CHAR_CELLS;
5177 }
5178
5179#ifdef FEAT_MBYTE
5180 if (clear_next)
5181 {
5182 /* Clear the second half of a double-wide character of which the left
5183 * half was overwritten with a single-wide character. */
5184 ScreenLines[off_to] = ' ';
5185 if (enc_utf8)
5186 ScreenLinesUC[off_to] = 0;
5187 screen_char(off_to, row, col + coloff);
5188 }
5189#endif
5190
5191 if (clear_width > 0
5192#ifdef FEAT_RIGHTLEFT
5193 && !rlflag
5194#endif
5195 )
5196 {
5197#ifdef FEAT_GUI
5198 int startCol = col;
5199#endif
5200
5201 /* blank out the rest of the line */
5202 while (col < clear_width && ScreenLines[off_to] == ' '
5203 && ScreenAttrs[off_to] == 0
5204#ifdef FEAT_MBYTE
5205 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5206#endif
5207 )
5208 {
5209 ++off_to;
5210 ++col;
5211 }
5212 if (col < clear_width)
5213 {
5214#ifdef FEAT_GUI
5215 /*
5216 * In the GUI, clearing the rest of the line may leave pixels
5217 * behind if the first character cleared was bold. Some bold
5218 * fonts spill over the left. In this case we redraw the previous
5219 * character too. If we didn't skip any blanks above, then we
5220 * only redraw if the character wasn't already redrawn anyway.
5221 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005222 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
5224 hl = ScreenAttrs[off_to];
5225 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005226 {
5227 int prev_cells = 1;
5228# ifdef FEAT_MBYTE
5229 if (enc_utf8)
5230 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5231 * that its width is 2. */
5232 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5233 else if (enc_dbcs != 0)
5234 {
5235 /* find previous character by counting from first
5236 * column and get its width. */
5237 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005238 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005239
5240 while (off < off_to)
5241 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005242 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005243 off += prev_cells;
5244 }
5245 }
5246
5247 if (enc_dbcs != 0 && prev_cells > 1)
5248 screen_char_2(off_to - prev_cells, row,
5249 col + coloff - prev_cells);
5250 else
5251# endif
5252 screen_char(off_to - prev_cells, row,
5253 col + coloff - prev_cells);
5254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
5256#endif
5257 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5258 ' ', ' ', 0);
5259#ifdef FEAT_VERTSPLIT
5260 off_to += clear_width - col;
5261 col = clear_width;
5262#endif
5263 }
5264 }
5265
5266 if (clear_width > 0)
5267 {
5268#ifdef FEAT_VERTSPLIT
5269 /* For a window that's left of another, draw the separator char. */
5270 if (col + coloff < Columns)
5271 {
5272 int c;
5273
5274 c = fillchar_vsep(&hl);
5275 if (ScreenLines[off_to] != c
5276# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005277 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5278 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279# endif
5280 || ScreenAttrs[off_to] != hl)
5281 {
5282 ScreenLines[off_to] = c;
5283 ScreenAttrs[off_to] = hl;
5284# ifdef FEAT_MBYTE
5285 if (enc_utf8)
5286 {
5287 if (c >= 0x80)
5288 {
5289 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005290 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 else
5293 ScreenLinesUC[off_to] = 0;
5294 }
5295# endif
5296 screen_char(off_to, row, col + coloff);
5297 }
5298 }
5299 else
5300#endif
5301 LineWraps[row] = FALSE;
5302 }
5303}
5304
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005305#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005307 * Mirror text "str" for right-left displaying.
5308 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005310 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311rl_mirror(str)
5312 char_u *str;
5313{
5314 char_u *p1, *p2;
5315 int t;
5316
5317 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5318 {
5319 t = *p1;
5320 *p1 = *p2;
5321 *p2 = t;
5322 }
5323}
5324#endif
5325
5326#if defined(FEAT_WINDOWS) || defined(PROTO)
5327/*
5328 * mark all status lines for redraw; used after first :cd
5329 */
5330 void
5331status_redraw_all()
5332{
5333 win_T *wp;
5334
5335 for (wp = firstwin; wp; wp = wp->w_next)
5336 if (wp->w_status_height)
5337 {
5338 wp->w_redr_status = TRUE;
5339 redraw_later(VALID);
5340 }
5341}
5342
5343/*
5344 * mark all status lines of the current buffer for redraw
5345 */
5346 void
5347status_redraw_curbuf()
5348{
5349 win_T *wp;
5350
5351 for (wp = firstwin; wp; wp = wp->w_next)
5352 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5353 {
5354 wp->w_redr_status = TRUE;
5355 redraw_later(VALID);
5356 }
5357}
5358
5359/*
5360 * Redraw all status lines that need to be redrawn.
5361 */
5362 void
5363redraw_statuslines()
5364{
5365 win_T *wp;
5366
5367 for (wp = firstwin; wp; wp = wp->w_next)
5368 if (wp->w_redr_status)
5369 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005370 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005371 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372}
5373#endif
5374
5375#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5376/*
5377 * Redraw all status lines at the bottom of frame "frp".
5378 */
5379 void
5380win_redraw_last_status(frp)
5381 frame_T *frp;
5382{
5383 if (frp->fr_layout == FR_LEAF)
5384 frp->fr_win->w_redr_status = TRUE;
5385 else if (frp->fr_layout == FR_ROW)
5386 {
5387 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5388 win_redraw_last_status(frp);
5389 }
5390 else /* frp->fr_layout == FR_COL */
5391 {
5392 frp = frp->fr_child;
5393 while (frp->fr_next != NULL)
5394 frp = frp->fr_next;
5395 win_redraw_last_status(frp);
5396 }
5397}
5398#endif
5399
5400#ifdef FEAT_VERTSPLIT
5401/*
5402 * Draw the verticap separator right of window "wp" starting with line "row".
5403 */
5404 static void
5405draw_vsep_win(wp, row)
5406 win_T *wp;
5407 int row;
5408{
5409 int hl;
5410 int c;
5411
5412 if (wp->w_vsep_width)
5413 {
5414 /* draw the vertical separator right of this window */
5415 c = fillchar_vsep(&hl);
5416 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5417 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5418 c, ' ', hl);
5419 }
5420}
5421#endif
5422
5423#ifdef FEAT_WILDMENU
5424static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005425static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005428 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 */
5430 static int
5431status_match_len(xp, s)
5432 expand_T *xp;
5433 char_u *s;
5434{
5435 int len = 0;
5436
5437#ifdef FEAT_MENU
5438 int emenu = (xp->xp_context == EXPAND_MENUS
5439 || xp->xp_context == EXPAND_MENUNAMES);
5440
5441 /* Check for menu separators - replace with '|'. */
5442 if (emenu && menu_is_separator(s))
5443 return 1;
5444#endif
5445
5446 while (*s != NUL)
5447 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005448 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005450 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005451 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453
5454 return len;
5455}
5456
5457/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005458 * Return TRUE for characters that are not displayed in a status match.
5459 * These are backslashes used for escaping. Do show backslashes in help tags.
5460 */
5461 static int
5462skip_status_match_char(xp, s)
5463 expand_T *xp;
5464 char_u *s;
5465{
5466 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5467#ifdef FEAT_MENU
5468 || ((xp->xp_context == EXPAND_MENUS
5469 || xp->xp_context == EXPAND_MENUNAMES)
5470 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5471#endif
5472 );
5473}
5474
5475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 * Show wildchar matches in the status line.
5477 * Show at least the "match" item.
5478 * We start at item 'first_match' in the list and show all matches that fit.
5479 *
5480 * If inversion is possible we use it. Else '=' characters are used.
5481 */
5482 void
5483win_redr_status_matches(xp, num_matches, matches, match, showtail)
5484 expand_T *xp;
5485 int num_matches;
5486 char_u **matches; /* list of matches */
5487 int match;
5488 int showtail;
5489{
5490#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5491 int row;
5492 char_u *buf;
5493 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005494 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 int fillchar;
5496 int attr;
5497 int i;
5498 int highlight = TRUE;
5499 char_u *selstart = NULL;
5500 int selstart_col = 0;
5501 char_u *selend = NULL;
5502 static int first_match = 0;
5503 int add_left = FALSE;
5504 char_u *s;
5505#ifdef FEAT_MENU
5506 int emenu;
5507#endif
5508#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5509 int l;
5510#endif
5511
5512 if (matches == NULL) /* interrupted completion? */
5513 return;
5514
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005515#ifdef FEAT_MBYTE
5516 if (has_mbyte)
5517 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5518 else
5519#endif
5520 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 if (buf == NULL)
5522 return;
5523
5524 if (match == -1) /* don't show match but original text */
5525 {
5526 match = 0;
5527 highlight = FALSE;
5528 }
5529 /* count 1 for the ending ">" */
5530 clen = status_match_len(xp, L_MATCH(match)) + 3;
5531 if (match == 0)
5532 first_match = 0;
5533 else if (match < first_match)
5534 {
5535 /* jumping left, as far as we can go */
5536 first_match = match;
5537 add_left = TRUE;
5538 }
5539 else
5540 {
5541 /* check if match fits on the screen */
5542 for (i = first_match; i < match; ++i)
5543 clen += status_match_len(xp, L_MATCH(i)) + 2;
5544 if (first_match > 0)
5545 clen += 2;
5546 /* jumping right, put match at the left */
5547 if ((long)clen > Columns)
5548 {
5549 first_match = match;
5550 /* if showing the last match, we can add some on the left */
5551 clen = 2;
5552 for (i = match; i < num_matches; ++i)
5553 {
5554 clen += status_match_len(xp, L_MATCH(i)) + 2;
5555 if ((long)clen >= Columns)
5556 break;
5557 }
5558 if (i == num_matches)
5559 add_left = TRUE;
5560 }
5561 }
5562 if (add_left)
5563 while (first_match > 0)
5564 {
5565 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5566 if ((long)clen >= Columns)
5567 break;
5568 --first_match;
5569 }
5570
5571 fillchar = fillchar_status(&attr, TRUE);
5572
5573 if (first_match == 0)
5574 {
5575 *buf = NUL;
5576 len = 0;
5577 }
5578 else
5579 {
5580 STRCPY(buf, "< ");
5581 len = 2;
5582 }
5583 clen = len;
5584
5585 i = first_match;
5586 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5587 {
5588 if (i == match)
5589 {
5590 selstart = buf + len;
5591 selstart_col = clen;
5592 }
5593
5594 s = L_MATCH(i);
5595 /* Check for menu separators - replace with '|' */
5596#ifdef FEAT_MENU
5597 emenu = (xp->xp_context == EXPAND_MENUS
5598 || xp->xp_context == EXPAND_MENUNAMES);
5599 if (emenu && menu_is_separator(s))
5600 {
5601 STRCPY(buf + len, transchar('|'));
5602 l = (int)STRLEN(buf + len);
5603 len += l;
5604 clen += l;
5605 }
5606 else
5607#endif
5608 for ( ; *s != NUL; ++s)
5609 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005610 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 ++s;
5612 clen += ptr2cells(s);
5613#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005614 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {
5616 STRNCPY(buf + len, s, l);
5617 s += l - 1;
5618 len += l;
5619 }
5620 else
5621#endif
5622 {
5623 STRCPY(buf + len, transchar_byte(*s));
5624 len += (int)STRLEN(buf + len);
5625 }
5626 }
5627 if (i == match)
5628 selend = buf + len;
5629
5630 *(buf + len++) = ' ';
5631 *(buf + len++) = ' ';
5632 clen += 2;
5633 if (++i == num_matches)
5634 break;
5635 }
5636
5637 if (i != num_matches)
5638 {
5639 *(buf + len++) = '>';
5640 ++clen;
5641 }
5642
5643 buf[len] = NUL;
5644
5645 row = cmdline_row - 1;
5646 if (row >= 0)
5647 {
5648 if (wild_menu_showing == 0)
5649 {
5650 if (msg_scrolled > 0)
5651 {
5652 /* Put the wildmenu just above the command line. If there is
5653 * no room, scroll the screen one line up. */
5654 if (cmdline_row == Rows - 1)
5655 {
5656 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5657 ++msg_scrolled;
5658 }
5659 else
5660 {
5661 ++cmdline_row;
5662 ++row;
5663 }
5664 wild_menu_showing = WM_SCROLLED;
5665 }
5666 else
5667 {
5668 /* Create status line if needed by setting 'laststatus' to 2.
5669 * Set 'winminheight' to zero to avoid that the window is
5670 * resized. */
5671 if (lastwin->w_status_height == 0)
5672 {
5673 save_p_ls = p_ls;
5674 save_p_wmh = p_wmh;
5675 p_ls = 2;
5676 p_wmh = 0;
5677 last_status(FALSE);
5678 }
5679 wild_menu_showing = WM_SHOWN;
5680 }
5681 }
5682
5683 screen_puts(buf, row, 0, attr);
5684 if (selstart != NULL && highlight)
5685 {
5686 *selend = NUL;
5687 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5688 }
5689
5690 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5691 }
5692
5693#ifdef FEAT_VERTSPLIT
5694 win_redraw_last_status(topframe);
5695#else
5696 lastwin->w_redr_status = TRUE;
5697#endif
5698 vim_free(buf);
5699}
5700#endif
5701
5702#if defined(FEAT_WINDOWS) || defined(PROTO)
5703/*
5704 * Redraw the status line of window wp.
5705 *
5706 * If inversion is possible we use it. Else '=' characters are used.
5707 */
5708 void
5709win_redr_status(wp)
5710 win_T *wp;
5711{
5712 int row;
5713 char_u *p;
5714 int len;
5715 int fillchar;
5716 int attr;
5717 int this_ru_col;
5718
5719 wp->w_redr_status = FALSE;
5720 if (wp->w_status_height == 0)
5721 {
5722 /* no status line, can only be last window */
5723 redraw_cmdline = TRUE;
5724 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005725 else if (!redrawing()
5726#ifdef FEAT_INS_EXPAND
5727 /* don't update status line when popup menu is visible and may be
5728 * drawn over it */
5729 || pum_visible()
5730#endif
5731 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
5733 /* Don't redraw right now, do it later. */
5734 wp->w_redr_status = TRUE;
5735 }
5736#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005737 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
5739 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005740 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742#endif
5743 else
5744 {
5745 fillchar = fillchar_status(&attr, wp == curwin);
5746
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005747 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 p = NameBuff;
5749 len = (int)STRLEN(p);
5750
5751 if (wp->w_buffer->b_help
5752#ifdef FEAT_QUICKFIX
5753 || wp->w_p_pvw
5754#endif
5755 || bufIsChanged(wp->w_buffer)
5756 || wp->w_buffer->b_p_ro)
5757 *(p + len++) = ' ';
5758 if (wp->w_buffer->b_help)
5759 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005760 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 len += (int)STRLEN(p + len);
5762 }
5763#ifdef FEAT_QUICKFIX
5764 if (wp->w_p_pvw)
5765 {
5766 STRCPY(p + len, _("[Preview]"));
5767 len += (int)STRLEN(p + len);
5768 }
5769#endif
5770 if (bufIsChanged(wp->w_buffer))
5771 {
5772 STRCPY(p + len, "[+]");
5773 len += 3;
5774 }
5775 if (wp->w_buffer->b_p_ro)
5776 {
5777 STRCPY(p + len, "[RO]");
5778 len += 4;
5779 }
5780
5781#ifndef FEAT_VERTSPLIT
5782 this_ru_col = ru_col;
5783 if (this_ru_col < (Columns + 1) / 2)
5784 this_ru_col = (Columns + 1) / 2;
5785#else
5786 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5787 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5788 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5789 if (this_ru_col <= 1)
5790 {
5791 p = (char_u *)"<"; /* No room for file name! */
5792 len = 1;
5793 }
5794 else
5795#endif
5796#ifdef FEAT_MBYTE
5797 if (has_mbyte)
5798 {
5799 int clen = 0, i;
5800
5801 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005802 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 clen += (*mb_ptr2cells)(p + i);
5804 /* Find first character that will fit.
5805 * Going from start to end is much faster for DBCS. */
5806 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005807 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 clen -= (*mb_ptr2cells)(p + i);
5809 len = clen;
5810 if (i > 0)
5811 {
5812 p = p + i - 1;
5813 *p = '<';
5814 ++len;
5815 }
5816
5817 }
5818 else
5819#endif
5820 if (len > this_ru_col - 1)
5821 {
5822 p += len - (this_ru_col - 1);
5823 *p = '<';
5824 len = this_ru_col - 1;
5825 }
5826
5827 row = W_WINROW(wp) + wp->w_height;
5828 screen_puts(p, row, W_WINCOL(wp), attr);
5829 screen_fill(row, row + 1, len + W_WINCOL(wp),
5830 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5831
5832 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5833 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5834 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5835 - 1 + W_WINCOL(wp)), attr);
5836
5837#ifdef FEAT_CMDL_INFO
5838 win_redr_ruler(wp, TRUE);
5839#endif
5840 }
5841
5842#ifdef FEAT_VERTSPLIT
5843 /*
5844 * May need to draw the character below the vertical separator.
5845 */
5846 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5847 {
5848 if (stl_connected(wp))
5849 fillchar = fillchar_status(&attr, wp == curwin);
5850 else
5851 fillchar = fillchar_vsep(&attr);
5852 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5853 attr);
5854 }
5855#endif
5856}
5857
Bram Moolenaar238a5642006-02-21 22:12:05 +00005858#ifdef FEAT_STL_OPT
5859/*
5860 * Redraw the status line according to 'statusline' and take care of any
5861 * errors encountered.
5862 */
5863 static void
5864redraw_custum_statusline(wp)
5865 win_T *wp;
5866{
5867 int save_called_emsg = called_emsg;
5868
5869 called_emsg = FALSE;
5870 win_redr_custom(wp, FALSE);
5871 if (called_emsg)
5872 set_string_option_direct((char_u *)"statusline", -1,
5873 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005874 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005875 called_emsg |= save_called_emsg;
5876}
5877#endif
5878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879# ifdef FEAT_VERTSPLIT
5880/*
5881 * Return TRUE if the status line of window "wp" is connected to the status
5882 * line of the window right of it. If not, then it's a vertical separator.
5883 * Only call if (wp->w_vsep_width != 0).
5884 */
5885 int
5886stl_connected(wp)
5887 win_T *wp;
5888{
5889 frame_T *fr;
5890
5891 fr = wp->w_frame;
5892 while (fr->fr_parent != NULL)
5893 {
5894 if (fr->fr_parent->fr_layout == FR_COL)
5895 {
5896 if (fr->fr_next != NULL)
5897 break;
5898 }
5899 else
5900 {
5901 if (fr->fr_next != NULL)
5902 return TRUE;
5903 }
5904 fr = fr->fr_parent;
5905 }
5906 return FALSE;
5907}
5908# endif
5909
5910#endif /* FEAT_WINDOWS */
5911
5912#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5913/*
5914 * Get the value to show for the language mappings, active 'keymap'.
5915 */
5916 int
5917get_keymap_str(wp, buf, len)
5918 win_T *wp;
5919 char_u *buf; /* buffer for the result */
5920 int len; /* length of buffer */
5921{
5922 char_u *p;
5923
5924 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5925 return FALSE;
5926
5927 {
5928#ifdef FEAT_EVAL
5929 buf_T *old_curbuf = curbuf;
5930 win_T *old_curwin = curwin;
5931 char_u *s;
5932
5933 curbuf = wp->w_buffer;
5934 curwin = wp;
5935 STRCPY(buf, "b:keymap_name"); /* must be writable */
5936 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005937 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 --emsg_skip;
5939 curbuf = old_curbuf;
5940 curwin = old_curwin;
5941 if (p == NULL || *p == NUL)
5942#endif
5943 {
5944#ifdef FEAT_KEYMAP
5945 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5946 p = wp->w_buffer->b_p_keymap;
5947 else
5948#endif
5949 p = (char_u *)"lang";
5950 }
5951 if ((int)(STRLEN(p) + 3) < len)
5952 sprintf((char *)buf, "<%s>", p);
5953 else
5954 buf[0] = NUL;
5955#ifdef FEAT_EVAL
5956 vim_free(s);
5957#endif
5958 }
5959 return buf[0] != NUL;
5960}
5961#endif
5962
5963#if defined(FEAT_STL_OPT) || defined(PROTO)
5964/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005965 * Redraw the status line or ruler of window "wp".
5966 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 */
5968 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005969win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005971 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972{
5973 int attr;
5974 int curattr;
5975 int row;
5976 int col = 0;
5977 int maxwidth;
5978 int width;
5979 int n;
5980 int len;
5981 int fillchar;
5982 char_u buf[MAXPATHL];
5983 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005984 struct stl_hlrec hltab[STL_MAX_ITEM];
5985 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005986 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
5988 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005989 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005991 /* Use 'tabline'. Always at the first line of the screen. */
5992 p = p_tal;
5993 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005994 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005995 attr = hl_attr(HLF_TPF);
5996 maxwidth = Columns;
5997# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005998 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006001 else
6002 {
6003 row = W_WINROW(wp) + wp->w_height;
6004 fillchar = fillchar_status(&attr, wp == curwin);
6005 maxwidth = W_WIDTH(wp);
6006
6007 if (draw_ruler)
6008 {
6009 p = p_ruf;
6010 /* advance past any leading group spec - implicit in ru_col */
6011 if (*p == '%')
6012 {
6013 if (*++p == '-')
6014 p++;
6015 if (atoi((char *) p))
6016 while (VIM_ISDIGIT(*p))
6017 p++;
6018 if (*p++ != '(')
6019 p = p_ruf;
6020 }
6021#ifdef FEAT_VERTSPLIT
6022 col = ru_col - (Columns - W_WIDTH(wp));
6023 if (col < (W_WIDTH(wp) + 1) / 2)
6024 col = (W_WIDTH(wp) + 1) / 2;
6025#else
6026 col = ru_col;
6027 if (col > (Columns + 1) / 2)
6028 col = (Columns + 1) / 2;
6029#endif
6030 maxwidth = W_WIDTH(wp) - col;
6031#ifdef FEAT_WINDOWS
6032 if (!wp->w_status_height)
6033#endif
6034 {
6035 row = Rows - 1;
6036 --maxwidth; /* writing in last column may cause scrolling */
6037 fillchar = ' ';
6038 attr = 0;
6039 }
6040
6041# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006042 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006043# endif
6044 }
6045 else
6046 {
6047 if (*wp->w_p_stl != NUL)
6048 p = wp->w_p_stl;
6049 else
6050 p = p_stl;
6051# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006052 use_sandbox = was_set_insecurely((char_u *)"statusline",
6053 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006054# endif
6055 }
6056
6057#ifdef FEAT_VERTSPLIT
6058 col += W_WINCOL(wp);
6059#endif
6060 }
6061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 if (maxwidth <= 0)
6063 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006065 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6066 buf, sizeof(buf),
6067 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006068 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006069 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071 while (width < maxwidth && len < sizeof(buf) - 1)
6072 {
6073#ifdef FEAT_MBYTE
6074 len += (*mb_char2bytes)(fillchar, buf + len);
6075#else
6076 buf[len++] = fillchar;
6077#endif
6078 ++width;
6079 }
6080 buf[len] = NUL;
6081
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006082 /*
6083 * Draw each snippet with the specified highlighting.
6084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 curattr = attr;
6086 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006087 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006089 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 screen_puts_len(p, len, row, col, curattr);
6091 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006092 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006094 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006096 else if (hltab[n].userhl < 0)
6097 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006099 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006100 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#endif
6102 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006103 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 }
6105 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006106
6107 if (wp == NULL)
6108 {
6109 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6110 col = 0;
6111 len = 0;
6112 p = buf;
6113 fillchar = 0;
6114 for (n = 0; tabtab[n].start != NULL; n++)
6115 {
6116 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6117 while (col < len)
6118 TabPageIdxs[col++] = fillchar;
6119 p = tabtab[n].start;
6120 fillchar = tabtab[n].userhl;
6121 }
6122 while (col < Columns)
6123 TabPageIdxs[col++] = fillchar;
6124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125}
6126
6127#endif /* FEAT_STL_OPT */
6128
6129/*
6130 * Output a single character directly to the screen and update ScreenLines.
6131 */
6132 void
6133screen_putchar(c, row, col, attr)
6134 int c;
6135 int row, col;
6136 int attr;
6137{
6138#ifdef FEAT_MBYTE
6139 char_u buf[MB_MAXBYTES + 1];
6140
6141 buf[(*mb_char2bytes)(c, buf)] = NUL;
6142#else
6143 char_u buf[2];
6144
6145 buf[0] = c;
6146 buf[1] = NUL;
6147#endif
6148 screen_puts(buf, row, col, attr);
6149}
6150
6151/*
6152 * Get a single character directly from ScreenLines into "bytes[]".
6153 * Also return its attribute in *attrp;
6154 */
6155 void
6156screen_getbytes(row, col, bytes, attrp)
6157 int row, col;
6158 char_u *bytes;
6159 int *attrp;
6160{
6161 unsigned off;
6162
6163 /* safety check */
6164 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6165 {
6166 off = LineOffset[row] + col;
6167 *attrp = ScreenAttrs[off];
6168 bytes[0] = ScreenLines[off];
6169 bytes[1] = NUL;
6170
6171#ifdef FEAT_MBYTE
6172 if (enc_utf8 && ScreenLinesUC[off] != 0)
6173 bytes[utfc_char2bytes(off, bytes)] = NUL;
6174 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6175 {
6176 bytes[0] = ScreenLines[off];
6177 bytes[1] = ScreenLines2[off];
6178 bytes[2] = NUL;
6179 }
6180 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6181 {
6182 bytes[1] = ScreenLines[off + 1];
6183 bytes[2] = NUL;
6184 }
6185#endif
6186 }
6187}
6188
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006189#ifdef FEAT_MBYTE
6190static int screen_comp_differs __ARGS((int, int*));
6191
6192/*
6193 * Return TRUE if composing characters for screen posn "off" differs from
6194 * composing characters in "u8cc".
6195 */
6196 static int
6197screen_comp_differs(off, u8cc)
6198 int off;
6199 int *u8cc;
6200{
6201 int i;
6202
6203 for (i = 0; i < Screen_mco; ++i)
6204 {
6205 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6206 return TRUE;
6207 if (u8cc[i] == 0)
6208 break;
6209 }
6210 return FALSE;
6211}
6212#endif
6213
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214/*
6215 * Put string '*text' on the screen at position 'row' and 'col', with
6216 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6217 * Note: only outputs within one row, message is truncated at screen boundary!
6218 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6219 */
6220 void
6221screen_puts(text, row, col, attr)
6222 char_u *text;
6223 int row;
6224 int col;
6225 int attr;
6226{
6227 screen_puts_len(text, -1, row, col, attr);
6228}
6229
6230/*
6231 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6232 * a NUL.
6233 */
6234 void
6235screen_puts_len(text, len, row, col, attr)
6236 char_u *text;
6237 int len;
6238 int row;
6239 int col;
6240 int attr;
6241{
6242 unsigned off;
6243 char_u *ptr = text;
6244 int c;
6245#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006246 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 int mbyte_blen = 1;
6248 int mbyte_cells = 1;
6249 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006250 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 int clear_next_cell = FALSE;
6252# ifdef FEAT_ARABIC
6253 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006254 int pc, nc, nc1;
6255 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256# endif
6257#endif
6258
6259 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6260 return;
6261
6262 off = LineOffset[row] + col;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006263#ifdef FEAT_MBYTE
6264 max_off = LineOffset[row] + screen_Columns;
6265#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006266 while (col < screen_Columns
6267 && (len < 0 || (int)(ptr - text) < len)
6268 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
6270 c = *ptr;
6271#ifdef FEAT_MBYTE
6272 /* check if this is the first byte of a multibyte */
6273 if (has_mbyte)
6274 {
6275 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006276 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006278 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6280 mbyte_cells = 1;
6281 else if (enc_dbcs != 0)
6282 mbyte_cells = mbyte_blen;
6283 else /* enc_utf8 */
6284 {
6285 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006286 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 (int)((text + len) - ptr));
6288 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006289 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006291# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 /* Non-BMP character: display as ? or fullwidth ?. */
6293 if (u8c >= 0x10000)
6294 {
6295 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6296 if (attr == 0)
6297 attr = hl_attr(HLF_8);
6298 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300# ifdef FEAT_ARABIC
6301 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6302 {
6303 /* Do Arabic shaping. */
6304 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6305 {
6306 /* Past end of string to be displayed. */
6307 nc = NUL;
6308 nc1 = NUL;
6309 }
6310 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006311 {
6312 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6313 nc1 = pcc[0];
6314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 pc = prev_c;
6316 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006317 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 }
6319 else
6320 prev_c = u8c;
6321# endif
6322 }
6323 }
6324#endif
6325
6326 if (ScreenLines[off] != c
6327#ifdef FEAT_MBYTE
6328 || (mbyte_cells == 2
6329 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6330 || (enc_dbcs == DBCS_JPNU
6331 && c == 0x8e
6332 && ScreenLines2[off] != ptr[1])
6333 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006334 && (ScreenLinesUC[off] != (u8char_T)u8c
6335 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336#endif
6337 || ScreenAttrs[off] != attr
6338 || exmode_active
6339 )
6340 {
6341#if defined(FEAT_GUI) || defined(UNIX)
6342 /* The bold trick makes a single row of pixels appear in the next
6343 * character. When a bold character is removed, the next
6344 * character should be redrawn too. This happens for our own GUI
6345 * and for some xterms.
6346 * Force the redraw by setting the attribute to a different value
6347 * than "attr", the contents of ScreenLines[] may be needed by
6348 * mb_off2cells() further on.
6349 * Don't do this for the last drawn character, because the next
6350 * character may not be redrawn. */
6351 if (
6352# ifdef FEAT_GUI
6353 gui.in_use
6354# endif
6355# if defined(FEAT_GUI) && defined(UNIX)
6356 ||
6357# endif
6358# ifdef UNIX
6359 term_is_xterm
6360# endif
6361 )
6362 {
6363 int n;
6364
6365 n = ScreenAttrs[off];
6366# ifdef FEAT_MBYTE
6367 if (col + mbyte_cells < screen_Columns
6368 && (n > HL_ALL || (n & HL_BOLD))
6369 && (len < 0 ? ptr[mbyte_blen] != NUL
6370 : ptr + mbyte_blen < text + len))
6371 ScreenAttrs[off + mbyte_cells] = attr + 1;
6372# else
6373 if (col + 1 < screen_Columns
6374 && (n > HL_ALL || (n & HL_BOLD))
6375 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6376 ScreenLines[off + 1] = 0;
6377# endif
6378 }
6379#endif
6380#ifdef FEAT_MBYTE
6381 /* When at the end of the text and overwriting a two-cell
6382 * character with a one-cell character, need to clear the next
6383 * cell. Also when overwriting the left halve of a two-cell char
6384 * with the right halve of a two-cell char. Do this only once
6385 * (mb_off2cells() may return 2 on the right halve). */
6386 if (clear_next_cell)
6387 clear_next_cell = FALSE;
6388 else if (has_mbyte
6389 && (len < 0 ? ptr[mbyte_blen] == NUL
6390 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006391 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006393 && (*mb_off2cells)(off, max_off) == 1
6394 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 clear_next_cell = TRUE;
6396
6397 /* Make sure we never leave a second byte of a double-byte behind,
6398 * it confuses mb_off2cells(). */
6399 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006400 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006402 && (*mb_off2cells)(off, max_off) == 1
6403 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 ScreenLines[off + mbyte_blen] = 0;
6405#endif
6406 ScreenLines[off] = c;
6407 ScreenAttrs[off] = attr;
6408#ifdef FEAT_MBYTE
6409 if (enc_utf8)
6410 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006411 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 ScreenLinesUC[off] = 0;
6413 else
6414 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006415 int i;
6416
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006418 for (i = 0; i < Screen_mco; ++i)
6419 {
6420 ScreenLinesC[i][off] = u8cc[i];
6421 if (u8cc[i] == 0)
6422 break;
6423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425 if (mbyte_cells == 2)
6426 {
6427 ScreenLines[off + 1] = 0;
6428 ScreenAttrs[off + 1] = attr;
6429 }
6430 screen_char(off, row, col);
6431 }
6432 else if (mbyte_cells == 2)
6433 {
6434 ScreenLines[off + 1] = ptr[1];
6435 ScreenAttrs[off + 1] = attr;
6436 screen_char_2(off, row, col);
6437 }
6438 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6439 {
6440 ScreenLines2[off] = ptr[1];
6441 screen_char(off, row, col);
6442 }
6443 else
6444#endif
6445 screen_char(off, row, col);
6446 }
6447#ifdef FEAT_MBYTE
6448 if (has_mbyte)
6449 {
6450 off += mbyte_cells;
6451 col += mbyte_cells;
6452 ptr += mbyte_blen;
6453 if (clear_next_cell)
6454 ptr = (char_u *)" ";
6455 }
6456 else
6457#endif
6458 {
6459 ++off;
6460 ++col;
6461 ++ptr;
6462 }
6463 }
6464}
6465
6466#ifdef FEAT_SEARCH_EXTRA
6467/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006468 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 */
6470 static void
6471start_search_hl()
6472{
6473 if (p_hls && !no_hlsearch)
6474 {
6475 last_pat_prog(&search_hl.rm);
6476 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006477# ifdef FEAT_RELTIME
6478 /* Set the time limit to 'redrawtime'. */
6479 profile_setlimit(p_rdt, &search_hl.tm);
6480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 }
6482}
6483
6484/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006485 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 */
6487 static void
6488end_search_hl()
6489{
6490 if (search_hl.rm.regprog != NULL)
6491 {
6492 vim_free(search_hl.rm.regprog);
6493 search_hl.rm.regprog = NULL;
6494 }
6495}
6496
6497/*
6498 * Advance to the match in window "wp" line "lnum" or past it.
6499 */
6500 static void
6501prepare_search_hl(wp, lnum)
6502 win_T *wp;
6503 linenr_T lnum;
6504{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006505 matchitem_T *cur; /* points to the match list */
6506 match_T *shl; /* points to search_hl or a match */
6507 int shl_flag; /* flag to indicate whether search_hl
6508 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 int n;
6510
6511 /*
6512 * When using a multi-line pattern, start searching at the top
6513 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006514 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006516 cur = wp->w_match_head;
6517 shl_flag = FALSE;
6518 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006520 if (shl_flag == FALSE)
6521 {
6522 shl = &search_hl;
6523 shl_flag = TRUE;
6524 }
6525 else
6526 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (shl->rm.regprog != NULL
6528 && shl->lnum == 0
6529 && re_multiline(shl->rm.regprog))
6530 {
6531 if (shl->first_lnum == 0)
6532 {
6533# ifdef FEAT_FOLDING
6534 for (shl->first_lnum = lnum;
6535 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6536 if (hasFoldingWin(wp, shl->first_lnum - 1,
6537 NULL, NULL, TRUE, NULL))
6538 break;
6539# else
6540 shl->first_lnum = wp->w_topline;
6541# endif
6542 }
6543 n = 0;
6544 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6545 {
6546 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6547 if (shl->lnum != 0)
6548 {
6549 shl->first_lnum = shl->lnum
6550 + shl->rm.endpos[0].lnum
6551 - shl->rm.startpos[0].lnum;
6552 n = shl->rm.endpos[0].col;
6553 }
6554 else
6555 {
6556 ++shl->first_lnum;
6557 n = 0;
6558 }
6559 }
6560 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006561 if (shl != &search_hl && cur != NULL)
6562 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 }
6564}
6565
6566/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006567 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 * Uses shl->buf.
6569 * Sets shl->lnum and shl->rm contents.
6570 * Note: Assumes a previous match is always before "lnum", unless
6571 * shl->lnum is zero.
6572 * Careful: Any pointers for buffer lines will become invalid.
6573 */
6574 static void
6575next_search_hl(win, shl, lnum, mincol)
6576 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006577 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 linenr_T lnum;
6579 colnr_T mincol; /* minimal column for a match */
6580{
6581 linenr_T l;
6582 colnr_T matchcol;
6583 long nmatched;
6584
6585 if (shl->lnum != 0)
6586 {
6587 /* Check for three situations:
6588 * 1. If the "lnum" is below a previous match, start a new search.
6589 * 2. If the previous match includes "mincol", use it.
6590 * 3. Continue after the previous match.
6591 */
6592 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6593 if (lnum > l)
6594 shl->lnum = 0;
6595 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6596 return;
6597 }
6598
6599 /*
6600 * Repeat searching for a match until one is found that includes "mincol"
6601 * or none is found in this line.
6602 */
6603 called_emsg = FALSE;
6604 for (;;)
6605 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006606#ifdef FEAT_RELTIME
6607 /* Stop searching after passing the time limit. */
6608 if (profile_passed_limit(&(shl->tm)))
6609 {
6610 shl->lnum = 0; /* no match found in time */
6611 break;
6612 }
6613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 /* Three situations:
6615 * 1. No useful previous match: search from start of line.
6616 * 2. Not Vi compatible or empty match: continue at next character.
6617 * Break the loop if this is beyond the end of the line.
6618 * 3. Vi compatible searching: continue at end of previous match.
6619 */
6620 if (shl->lnum == 0)
6621 matchcol = 0;
6622 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6623 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006624 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006626 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006627
6628 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006629 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006630 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006632 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 shl->lnum = 0;
6634 break;
6635 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006636#ifdef FEAT_MBYTE
6637 if (has_mbyte)
6638 matchcol += mb_ptr2len(ml);
6639 else
6640#endif
6641 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643 else
6644 matchcol = shl->rm.endpos[0].col;
6645
6646 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006647 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6648#ifdef FEAT_RELTIME
6649 &(shl->tm)
6650#else
6651 NULL
6652#endif
6653 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (called_emsg)
6655 {
6656 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006657 if (shl == &search_hl)
6658 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006659 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006660 vim_free(shl->rm.regprog);
6661 no_hlsearch = TRUE;
6662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006664 shl->lnum = 0;
6665 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 break;
6667 }
6668 if (nmatched == 0)
6669 {
6670 shl->lnum = 0; /* no match found */
6671 break;
6672 }
6673 if (shl->rm.startpos[0].lnum > 0
6674 || shl->rm.startpos[0].col >= mincol
6675 || nmatched > 1
6676 || shl->rm.endpos[0].col > mincol)
6677 {
6678 shl->lnum += shl->rm.startpos[0].lnum;
6679 break; /* useful match found */
6680 }
6681 }
6682}
6683#endif
6684
6685 static void
6686screen_start_highlight(attr)
6687 int attr;
6688{
6689 attrentry_T *aep = NULL;
6690
6691 screen_attr = attr;
6692 if (full_screen
6693#ifdef WIN3264
6694 && termcap_active
6695#endif
6696 )
6697 {
6698#ifdef FEAT_GUI
6699 if (gui.in_use)
6700 {
6701 char buf[20];
6702
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006703 /* The GUI handles this internally. */
6704 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 OUT_STR(buf);
6706 }
6707 else
6708#endif
6709 {
6710 if (attr > HL_ALL) /* special HL attr. */
6711 {
6712 if (t_colors > 1)
6713 aep = syn_cterm_attr2entry(attr);
6714 else
6715 aep = syn_term_attr2entry(attr);
6716 if (aep == NULL) /* did ":syntax clear" */
6717 attr = 0;
6718 else
6719 attr = aep->ae_attr;
6720 }
6721 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6722 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006723 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6724 && cterm_normal_fg_bold)
6725 /* If the Normal FG color has BOLD attribute and the new HL
6726 * has a FG color defined, clear BOLD. */
6727 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6729 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006730 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6731 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 out_str(T_US);
6733 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6734 out_str(T_CZH);
6735 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6736 out_str(T_MR);
6737
6738 /*
6739 * Output the color or start string after bold etc., in case the
6740 * bold etc. override the color setting.
6741 */
6742 if (aep != NULL)
6743 {
6744 if (t_colors > 1)
6745 {
6746 if (aep->ae_u.cterm.fg_color)
6747 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6748 if (aep->ae_u.cterm.bg_color)
6749 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6750 }
6751 else
6752 {
6753 if (aep->ae_u.term.start != NULL)
6754 out_str(aep->ae_u.term.start);
6755 }
6756 }
6757 }
6758 }
6759}
6760
6761 void
6762screen_stop_highlight()
6763{
6764 int do_ME = FALSE; /* output T_ME code */
6765
6766 if (screen_attr != 0
6767#ifdef WIN3264
6768 && termcap_active
6769#endif
6770 )
6771 {
6772#ifdef FEAT_GUI
6773 if (gui.in_use)
6774 {
6775 char buf[20];
6776
6777 /* use internal GUI code */
6778 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6779 OUT_STR(buf);
6780 }
6781 else
6782#endif
6783 {
6784 if (screen_attr > HL_ALL) /* special HL attr. */
6785 {
6786 attrentry_T *aep;
6787
6788 if (t_colors > 1)
6789 {
6790 /*
6791 * Assume that t_me restores the original colors!
6792 */
6793 aep = syn_cterm_attr2entry(screen_attr);
6794 if (aep != NULL && (aep->ae_u.cterm.fg_color
6795 || aep->ae_u.cterm.bg_color))
6796 do_ME = TRUE;
6797 }
6798 else
6799 {
6800 aep = syn_term_attr2entry(screen_attr);
6801 if (aep != NULL && aep->ae_u.term.stop != NULL)
6802 {
6803 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6804 do_ME = TRUE;
6805 else
6806 out_str(aep->ae_u.term.stop);
6807 }
6808 }
6809 if (aep == NULL) /* did ":syntax clear" */
6810 screen_attr = 0;
6811 else
6812 screen_attr = aep->ae_attr;
6813 }
6814
6815 /*
6816 * Often all ending-codes are equal to T_ME. Avoid outputting the
6817 * same sequence several times.
6818 */
6819 if (screen_attr & HL_STANDOUT)
6820 {
6821 if (STRCMP(T_SE, T_ME) == 0)
6822 do_ME = TRUE;
6823 else
6824 out_str(T_SE);
6825 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006826 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {
6828 if (STRCMP(T_UE, T_ME) == 0)
6829 do_ME = TRUE;
6830 else
6831 out_str(T_UE);
6832 }
6833 if (screen_attr & HL_ITALIC)
6834 {
6835 if (STRCMP(T_CZR, T_ME) == 0)
6836 do_ME = TRUE;
6837 else
6838 out_str(T_CZR);
6839 }
6840 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6841 out_str(T_ME);
6842
6843 if (t_colors > 1)
6844 {
6845 /* set Normal cterm colors */
6846 if (cterm_normal_fg_color != 0)
6847 term_fg_color(cterm_normal_fg_color - 1);
6848 if (cterm_normal_bg_color != 0)
6849 term_bg_color(cterm_normal_bg_color - 1);
6850 if (cterm_normal_fg_bold)
6851 out_str(T_MD);
6852 }
6853 }
6854 }
6855 screen_attr = 0;
6856}
6857
6858/*
6859 * Reset the colors for a cterm. Used when leaving Vim.
6860 * The machine specific code may override this again.
6861 */
6862 void
6863reset_cterm_colors()
6864{
6865 if (t_colors > 1)
6866 {
6867 /* set Normal cterm colors */
6868 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6869 {
6870 out_str(T_OP);
6871 screen_attr = -1;
6872 }
6873 if (cterm_normal_fg_bold)
6874 {
6875 out_str(T_ME);
6876 screen_attr = -1;
6877 }
6878 }
6879}
6880
6881/*
6882 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6883 * using the attributes from ScreenAttrs["off"].
6884 */
6885 static void
6886screen_char(off, row, col)
6887 unsigned off;
6888 int row;
6889 int col;
6890{
6891 int attr;
6892
6893 /* Check for illegal values, just in case (could happen just after
6894 * resizing). */
6895 if (row >= screen_Rows || col >= screen_Columns)
6896 return;
6897
6898 /* Outputting the last character on the screen may scrollup the screen.
6899 * Don't to it! Mark the character invalid (update it when scrolled up) */
6900 if (row == screen_Rows - 1 && col == screen_Columns - 1
6901#ifdef FEAT_RIGHTLEFT
6902 /* account for first command-line character in rightleft mode */
6903 && !cmdmsg_rl
6904#endif
6905 )
6906 {
6907 ScreenAttrs[off] = (sattr_T)-1;
6908 return;
6909 }
6910
6911 /*
6912 * Stop highlighting first, so it's easier to move the cursor.
6913 */
6914#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6915 if (screen_char_attr != 0)
6916 attr = screen_char_attr;
6917 else
6918#endif
6919 attr = ScreenAttrs[off];
6920 if (screen_attr != attr)
6921 screen_stop_highlight();
6922
6923 windgoto(row, col);
6924
6925 if (screen_attr != attr)
6926 screen_start_highlight(attr);
6927
6928#ifdef FEAT_MBYTE
6929 if (enc_utf8 && ScreenLinesUC[off] != 0)
6930 {
6931 char_u buf[MB_MAXBYTES + 1];
6932
6933 /* Convert UTF-8 character to bytes and write it. */
6934
6935 buf[utfc_char2bytes(off, buf)] = NUL;
6936
6937 out_str(buf);
6938 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6939 ++screen_cur_col;
6940 }
6941 else
6942#endif
6943 {
6944#ifdef FEAT_MBYTE
6945 out_flush_check();
6946#endif
6947 out_char(ScreenLines[off]);
6948#ifdef FEAT_MBYTE
6949 /* double-byte character in single-width cell */
6950 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6951 out_char(ScreenLines2[off]);
6952#endif
6953 }
6954
6955 screen_cur_col++;
6956}
6957
6958#ifdef FEAT_MBYTE
6959
6960/*
6961 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6962 * on the screen at position 'row' and 'col'.
6963 * The attributes of the first byte is used for all. This is required to
6964 * output the two bytes of a double-byte character with nothing in between.
6965 */
6966 static void
6967screen_char_2(off, row, col)
6968 unsigned off;
6969 int row;
6970 int col;
6971{
6972 /* Check for illegal values (could be wrong when screen was resized). */
6973 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6974 return;
6975
6976 /* Outputting the last character on the screen may scrollup the screen.
6977 * Don't to it! Mark the character invalid (update it when scrolled up) */
6978 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6979 {
6980 ScreenAttrs[off] = (sattr_T)-1;
6981 return;
6982 }
6983
6984 /* Output the first byte normally (positions the cursor), then write the
6985 * second byte directly. */
6986 screen_char(off, row, col);
6987 out_char(ScreenLines[off + 1]);
6988 ++screen_cur_col;
6989}
6990#endif
6991
6992#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6993/*
6994 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6995 * This uses the contents of ScreenLines[] and doesn't change it.
6996 */
6997 void
6998screen_draw_rectangle(row, col, height, width, invert)
6999 int row;
7000 int col;
7001 int height;
7002 int width;
7003 int invert;
7004{
7005 int r, c;
7006 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007007#ifdef FEAT_MBYTE
7008 int max_off;
7009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007011 /* Can't use ScreenLines unless initialized */
7012 if (ScreenLines == NULL)
7013 return;
7014
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (invert)
7016 screen_char_attr = HL_INVERSE;
7017 for (r = row; r < row + height; ++r)
7018 {
7019 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007020#ifdef FEAT_MBYTE
7021 max_off = off + screen_Columns;
7022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 for (c = col; c < col + width; ++c)
7024 {
7025#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007026 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
7028 screen_char_2(off + c, r, c);
7029 ++c;
7030 }
7031 else
7032#endif
7033 {
7034 screen_char(off + c, r, c);
7035#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007036 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 ++c;
7038#endif
7039 }
7040 }
7041 }
7042 screen_char_attr = 0;
7043}
7044#endif
7045
7046#ifdef FEAT_VERTSPLIT
7047/*
7048 * Redraw the characters for a vertically split window.
7049 */
7050 static void
7051redraw_block(row, end, wp)
7052 int row;
7053 int end;
7054 win_T *wp;
7055{
7056 int col;
7057 int width;
7058
7059# ifdef FEAT_CLIPBOARD
7060 clip_may_clear_selection(row, end - 1);
7061# endif
7062
7063 if (wp == NULL)
7064 {
7065 col = 0;
7066 width = Columns;
7067 }
7068 else
7069 {
7070 col = wp->w_wincol;
7071 width = wp->w_width;
7072 }
7073 screen_draw_rectangle(row, col, end - row, width, FALSE);
7074}
7075#endif
7076
7077/*
7078 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7079 * with character 'c1' in first column followed by 'c2' in the other columns.
7080 * Use attributes 'attr'.
7081 */
7082 void
7083screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7084 int start_row, end_row;
7085 int start_col, end_col;
7086 int c1, c2;
7087 int attr;
7088{
7089 int row;
7090 int col;
7091 int off;
7092 int end_off;
7093 int did_delete;
7094 int c;
7095 int norm_term;
7096#if defined(FEAT_GUI) || defined(UNIX)
7097 int force_next = FALSE;
7098#endif
7099
7100 if (end_row > screen_Rows) /* safety check */
7101 end_row = screen_Rows;
7102 if (end_col > screen_Columns) /* safety check */
7103 end_col = screen_Columns;
7104 if (ScreenLines == NULL
7105 || start_row >= end_row
7106 || start_col >= end_col) /* nothing to do */
7107 return;
7108
7109 /* it's a "normal" terminal when not in a GUI or cterm */
7110 norm_term = (
7111#ifdef FEAT_GUI
7112 !gui.in_use &&
7113#endif
7114 t_colors <= 1);
7115 for (row = start_row; row < end_row; ++row)
7116 {
7117 /*
7118 * Try to use delete-line termcap code, when no attributes or in a
7119 * "normal" terminal, where a bold/italic space is just a
7120 * space.
7121 */
7122 did_delete = FALSE;
7123 if (c2 == ' '
7124 && end_col == Columns
7125 && can_clear(T_CE)
7126 && (attr == 0
7127 || (norm_term
7128 && attr <= HL_ALL
7129 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7130 {
7131 /*
7132 * check if we really need to clear something
7133 */
7134 col = start_col;
7135 if (c1 != ' ') /* don't clear first char */
7136 ++col;
7137
7138 off = LineOffset[row] + col;
7139 end_off = LineOffset[row] + end_col;
7140
7141 /* skip blanks (used often, keep it fast!) */
7142#ifdef FEAT_MBYTE
7143 if (enc_utf8)
7144 while (off < end_off && ScreenLines[off] == ' '
7145 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7146 ++off;
7147 else
7148#endif
7149 while (off < end_off && ScreenLines[off] == ' '
7150 && ScreenAttrs[off] == 0)
7151 ++off;
7152 if (off < end_off) /* something to be cleared */
7153 {
7154 col = off - LineOffset[row];
7155 screen_stop_highlight();
7156 term_windgoto(row, col);/* clear rest of this screen line */
7157 out_str(T_CE);
7158 screen_start(); /* don't know where cursor is now */
7159 col = end_col - col;
7160 while (col--) /* clear chars in ScreenLines */
7161 {
7162 ScreenLines[off] = ' ';
7163#ifdef FEAT_MBYTE
7164 if (enc_utf8)
7165 ScreenLinesUC[off] = 0;
7166#endif
7167 ScreenAttrs[off] = 0;
7168 ++off;
7169 }
7170 }
7171 did_delete = TRUE; /* the chars are cleared now */
7172 }
7173
7174 off = LineOffset[row] + start_col;
7175 c = c1;
7176 for (col = start_col; col < end_col; ++col)
7177 {
7178 if (ScreenLines[off] != c
7179#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007180 || (enc_utf8 && (int)ScreenLinesUC[off]
7181 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182#endif
7183 || ScreenAttrs[off] != attr
7184#if defined(FEAT_GUI) || defined(UNIX)
7185 || force_next
7186#endif
7187 )
7188 {
7189#if defined(FEAT_GUI) || defined(UNIX)
7190 /* The bold trick may make a single row of pixels appear in
7191 * the next character. When a bold character is removed, the
7192 * next character should be redrawn too. This happens for our
7193 * own GUI and for some xterms. */
7194 if (
7195# ifdef FEAT_GUI
7196 gui.in_use
7197# endif
7198# if defined(FEAT_GUI) && defined(UNIX)
7199 ||
7200# endif
7201# ifdef UNIX
7202 term_is_xterm
7203# endif
7204 )
7205 {
7206 if (ScreenLines[off] != ' '
7207 && (ScreenAttrs[off] > HL_ALL
7208 || ScreenAttrs[off] & HL_BOLD))
7209 force_next = TRUE;
7210 else
7211 force_next = FALSE;
7212 }
7213#endif
7214 ScreenLines[off] = c;
7215#ifdef FEAT_MBYTE
7216 if (enc_utf8)
7217 {
7218 if (c >= 0x80)
7219 {
7220 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007221 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223 else
7224 ScreenLinesUC[off] = 0;
7225 }
7226#endif
7227 ScreenAttrs[off] = attr;
7228 if (!did_delete || c != ' ')
7229 screen_char(off, row, col);
7230 }
7231 ++off;
7232 if (col == start_col)
7233 {
7234 if (did_delete)
7235 break;
7236 c = c2;
7237 }
7238 }
7239 if (end_col == Columns)
7240 LineWraps[row] = FALSE;
7241 if (row == Rows - 1) /* overwritten the command line */
7242 {
7243 redraw_cmdline = TRUE;
7244 if (c1 == ' ' && c2 == ' ')
7245 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007246 if (start_col == 0)
7247 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
7249 }
7250}
7251
7252/*
7253 * Check if there should be a delay. Used before clearing or redrawing the
7254 * screen or the command line.
7255 */
7256 void
7257check_for_delay(check_msg_scroll)
7258 int check_msg_scroll;
7259{
7260 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7261 && !did_wait_return
7262 && emsg_silent == 0)
7263 {
7264 out_flush();
7265 ui_delay(1000L, TRUE);
7266 emsg_on_display = FALSE;
7267 if (check_msg_scroll)
7268 msg_scroll = FALSE;
7269 }
7270}
7271
7272/*
7273 * screen_valid - allocate screen buffers if size changed
7274 * If "clear" is TRUE: clear screen if it has been resized.
7275 * Returns TRUE if there is a valid screen to write to.
7276 * Returns FALSE when starting up and screen not initialized yet.
7277 */
7278 int
7279screen_valid(clear)
7280 int clear;
7281{
7282 screenalloc(clear); /* allocate screen buffers if size changed */
7283 return (ScreenLines != NULL);
7284}
7285
7286/*
7287 * Resize the shell to Rows and Columns.
7288 * Allocate ScreenLines[] and associated items.
7289 *
7290 * There may be some time between setting Rows and Columns and (re)allocating
7291 * ScreenLines[]. This happens when starting up and when (manually) changing
7292 * the shell size. Always use screen_Rows and screen_Columns to access items
7293 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7294 * final size of the shell is needed.
7295 */
7296 void
7297screenalloc(clear)
7298 int clear;
7299{
7300 int new_row, old_row;
7301#ifdef FEAT_GUI
7302 int old_Rows;
7303#endif
7304 win_T *wp;
7305 int outofmem = FALSE;
7306 int len;
7307 schar_T *new_ScreenLines;
7308#ifdef FEAT_MBYTE
7309 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007310 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007312 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313#endif
7314 sattr_T *new_ScreenAttrs;
7315 unsigned *new_LineOffset;
7316 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007317#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007318 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007319 tabpage_T *tp;
7320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007322 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
7324 /*
7325 * Allocation of the screen buffers is done only when the size changes and
7326 * when Rows and Columns have been set and we have started doing full
7327 * screen stuff.
7328 */
7329 if ((ScreenLines != NULL
7330 && Rows == screen_Rows
7331 && Columns == screen_Columns
7332#ifdef FEAT_MBYTE
7333 && enc_utf8 == (ScreenLinesUC != NULL)
7334 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007335 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#endif
7337 )
7338 || Rows == 0
7339 || Columns == 0
7340 || (!full_screen && ScreenLines == NULL))
7341 return;
7342
7343 /*
7344 * It's possible that we produce an out-of-memory message below, which
7345 * will cause this function to be called again. To break the loop, just
7346 * return here.
7347 */
7348 if (entered)
7349 return;
7350 entered = TRUE;
7351
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007352 /*
7353 * Note that the window sizes are updated before reallocating the arrays,
7354 * thus we must not redraw here!
7355 */
7356 ++RedrawingDisabled;
7357
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 win_new_shellsize(); /* fit the windows in the new sized shell */
7359
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 comp_col(); /* recompute columns for shown command and ruler */
7361
7362 /*
7363 * We're changing the size of the screen.
7364 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7365 * - Move lines from the old arrays into the new arrays, clear extra
7366 * lines (unless the screen is going to be cleared).
7367 * - Free the old arrays.
7368 *
7369 * If anything fails, make ScreenLines NULL, so we don't do anything!
7370 * Continuing with the old ScreenLines may result in a crash, because the
7371 * size is wrong.
7372 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007373 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 new_ScreenLines = (schar_T *)lalloc((long_u)(
7377 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7378#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007379 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 if (enc_utf8)
7381 {
7382 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7383 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007384 for (i = 0; i < p_mco; ++i)
7385 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7387 }
7388 if (enc_dbcs == DBCS_JPNU)
7389 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7390 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7391#endif
7392 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7393 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7394 new_LineOffset = (unsigned *)lalloc((long_u)(
7395 Rows * sizeof(unsigned)), FALSE);
7396 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007397#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007398 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007401 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
7403 if (win_alloc_lines(wp) == FAIL)
7404 {
7405 outofmem = TRUE;
7406#ifdef FEAT_WINDOWS
7407 break;
7408#endif
7409 }
7410 }
7411
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007412#ifdef FEAT_MBYTE
7413 for (i = 0; i < p_mco; ++i)
7414 if (new_ScreenLinesC[i] == NULL)
7415 break;
7416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (new_ScreenLines == NULL
7418#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007419 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7421#endif
7422 || new_ScreenAttrs == NULL
7423 || new_LineOffset == NULL
7424 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007425#ifdef FEAT_WINDOWS
7426 || new_TabPageIdxs == NULL
7427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 || outofmem)
7429 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007430 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007431 {
7432 /* guess the size */
7433 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7434
7435 /* Remember we did this to avoid getting outofmem messages over
7436 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007437 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 vim_free(new_ScreenLines);
7440 new_ScreenLines = NULL;
7441#ifdef FEAT_MBYTE
7442 vim_free(new_ScreenLinesUC);
7443 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007444 for (i = 0; i < p_mco; ++i)
7445 {
7446 vim_free(new_ScreenLinesC[i]);
7447 new_ScreenLinesC[i] = NULL;
7448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 vim_free(new_ScreenLines2);
7450 new_ScreenLines2 = NULL;
7451#endif
7452 vim_free(new_ScreenAttrs);
7453 new_ScreenAttrs = NULL;
7454 vim_free(new_LineOffset);
7455 new_LineOffset = NULL;
7456 vim_free(new_LineWraps);
7457 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007458#ifdef FEAT_WINDOWS
7459 vim_free(new_TabPageIdxs);
7460 new_TabPageIdxs = NULL;
7461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
7463 else
7464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007465 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007466
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 for (new_row = 0; new_row < Rows; ++new_row)
7468 {
7469 new_LineOffset[new_row] = new_row * Columns;
7470 new_LineWraps[new_row] = FALSE;
7471
7472 /*
7473 * If the screen is not going to be cleared, copy as much as
7474 * possible from the old screen to the new one and clear the rest
7475 * (used when resizing the window at the "--more--" prompt or when
7476 * executing an external command, for the GUI).
7477 */
7478 if (!clear)
7479 {
7480 (void)vim_memset(new_ScreenLines + new_row * Columns,
7481 ' ', (size_t)Columns * sizeof(schar_T));
7482#ifdef FEAT_MBYTE
7483 if (enc_utf8)
7484 {
7485 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7486 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007487 for (i = 0; i < p_mco; ++i)
7488 (void)vim_memset(new_ScreenLinesC[i]
7489 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 0, (size_t)Columns * sizeof(u8char_T));
7491 }
7492 if (enc_dbcs == DBCS_JPNU)
7493 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7494 0, (size_t)Columns * sizeof(schar_T));
7495#endif
7496 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7497 0, (size_t)Columns * sizeof(sattr_T));
7498 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007499 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 {
7501 if (screen_Columns < Columns)
7502 len = screen_Columns;
7503 else
7504 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007505#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007506 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007507 * may be invalid now. Also when p_mco changes. */
7508 if (!(enc_utf8 && ScreenLinesUC == NULL)
7509 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007510#endif
7511 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7512 ScreenLines + LineOffset[old_row],
7513 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007515 if (enc_utf8 && ScreenLinesUC != NULL
7516 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 {
7518 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7519 ScreenLinesUC + LineOffset[old_row],
7520 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007521 for (i = 0; i < p_mco; ++i)
7522 mch_memmove(new_ScreenLinesC[i]
7523 + new_LineOffset[new_row],
7524 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 (size_t)len * sizeof(u8char_T));
7526 }
7527 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7528 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7529 ScreenLines2 + LineOffset[old_row],
7530 (size_t)len * sizeof(schar_T));
7531#endif
7532 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7533 ScreenAttrs + LineOffset[old_row],
7534 (size_t)len * sizeof(sattr_T));
7535 }
7536 }
7537 }
7538 /* Use the last line of the screen for the current line. */
7539 current_ScreenLine = new_ScreenLines + Rows * Columns;
7540 }
7541
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007542 free_screenlines();
7543
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 ScreenLines = new_ScreenLines;
7545#ifdef FEAT_MBYTE
7546 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007547 for (i = 0; i < p_mco; ++i)
7548 ScreenLinesC[i] = new_ScreenLinesC[i];
7549 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 ScreenLines2 = new_ScreenLines2;
7551#endif
7552 ScreenAttrs = new_ScreenAttrs;
7553 LineOffset = new_LineOffset;
7554 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007555#ifdef FEAT_WINDOWS
7556 TabPageIdxs = new_TabPageIdxs;
7557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
7559 /* It's important that screen_Rows and screen_Columns reflect the actual
7560 * size of ScreenLines[]. Set them before calling anything. */
7561#ifdef FEAT_GUI
7562 old_Rows = screen_Rows;
7563#endif
7564 screen_Rows = Rows;
7565 screen_Columns = Columns;
7566
7567 must_redraw = CLEAR; /* need to clear the screen later */
7568 if (clear)
7569 screenclear2();
7570
7571#ifdef FEAT_GUI
7572 else if (gui.in_use
7573 && !gui.starting
7574 && ScreenLines != NULL
7575 && old_Rows != Rows)
7576 {
7577 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7578 /*
7579 * Adjust the position of the cursor, for when executing an external
7580 * command.
7581 */
7582 if (msg_row >= Rows) /* Rows got smaller */
7583 msg_row = Rows - 1; /* put cursor at last row */
7584 else if (Rows > old_Rows) /* Rows got bigger */
7585 msg_row += Rows - old_Rows; /* put cursor in same place */
7586 if (msg_col >= Columns) /* Columns got smaller */
7587 msg_col = Columns - 1; /* put cursor at last column */
7588 }
7589#endif
7590
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007592 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007593
7594#ifdef FEAT_AUTOCMD
7595 if (starting == 0)
7596 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598}
7599
7600 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007601free_screenlines()
7602{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007603#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007604 int i;
7605
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007606 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007607 for (i = 0; i < Screen_mco; ++i)
7608 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007609 vim_free(ScreenLines2);
7610#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007611 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007612 vim_free(ScreenAttrs);
7613 vim_free(LineOffset);
7614 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007615#ifdef FEAT_WINDOWS
7616 vim_free(TabPageIdxs);
7617#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007618}
7619
7620 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621screenclear()
7622{
7623 check_for_delay(FALSE);
7624 screenalloc(FALSE); /* allocate screen buffers if size changed */
7625 screenclear2(); /* clear the screen */
7626}
7627
7628 static void
7629screenclear2()
7630{
7631 int i;
7632
7633 if (starting == NO_SCREEN || ScreenLines == NULL
7634#ifdef FEAT_GUI
7635 || (gui.in_use && gui.starting)
7636#endif
7637 )
7638 return;
7639
7640#ifdef FEAT_GUI
7641 if (!gui.in_use)
7642#endif
7643 screen_attr = -1; /* force setting the Normal colors */
7644 screen_stop_highlight(); /* don't want highlighting here */
7645
7646#ifdef FEAT_CLIPBOARD
7647 /* disable selection without redrawing it */
7648 clip_scroll_selection(9999);
7649#endif
7650
7651 /* blank out ScreenLines */
7652 for (i = 0; i < Rows; ++i)
7653 {
7654 lineclear(LineOffset[i], (int)Columns);
7655 LineWraps[i] = FALSE;
7656 }
7657
7658 if (can_clear(T_CL))
7659 {
7660 out_str(T_CL); /* clear the display */
7661 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007662 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664 else
7665 {
7666 /* can't clear the screen, mark all chars with invalid attributes */
7667 for (i = 0; i < Rows; ++i)
7668 lineinvalid(LineOffset[i], (int)Columns);
7669 clear_cmdline = TRUE;
7670 }
7671
7672 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7673
7674 win_rest_invalid(firstwin);
7675 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007676#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007677 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 if (must_redraw == CLEAR) /* no need to clear again */
7680 must_redraw = NOT_VALID;
7681 compute_cmdrow();
7682 msg_row = cmdline_row; /* put cursor on last line for messages */
7683 msg_col = 0;
7684 screen_start(); /* don't know where cursor is now */
7685 msg_scrolled = 0; /* can't scroll back */
7686 msg_didany = FALSE;
7687 msg_didout = FALSE;
7688}
7689
7690/*
7691 * Clear one line in ScreenLines.
7692 */
7693 static void
7694lineclear(off, width)
7695 unsigned off;
7696 int width;
7697{
7698 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7699#ifdef FEAT_MBYTE
7700 if (enc_utf8)
7701 (void)vim_memset(ScreenLinesUC + off, 0,
7702 (size_t)width * sizeof(u8char_T));
7703#endif
7704 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7705}
7706
7707/*
7708 * Mark one line in ScreenLines invalid by setting the attributes to an
7709 * invalid value.
7710 */
7711 static void
7712lineinvalid(off, width)
7713 unsigned off;
7714 int width;
7715{
7716 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7717}
7718
7719#ifdef FEAT_VERTSPLIT
7720/*
7721 * Copy part of a Screenline for vertically split window "wp".
7722 */
7723 static void
7724linecopy(to, from, wp)
7725 int to;
7726 int from;
7727 win_T *wp;
7728{
7729 unsigned off_to = LineOffset[to] + wp->w_wincol;
7730 unsigned off_from = LineOffset[from] + wp->w_wincol;
7731
7732 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7733 wp->w_width * sizeof(schar_T));
7734# ifdef FEAT_MBYTE
7735 if (enc_utf8)
7736 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007737 int i;
7738
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7740 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007741 for (i = 0; i < p_mco; ++i)
7742 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7743 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 }
7745 if (enc_dbcs == DBCS_JPNU)
7746 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7747 wp->w_width * sizeof(schar_T));
7748# endif
7749 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7750 wp->w_width * sizeof(sattr_T));
7751}
7752#endif
7753
7754/*
7755 * Return TRUE if clearing with term string "p" would work.
7756 * It can't work when the string is empty or it won't set the right background.
7757 */
7758 int
7759can_clear(p)
7760 char_u *p;
7761{
7762 return (*p != NUL && (t_colors <= 1
7763#ifdef FEAT_GUI
7764 || gui.in_use
7765#endif
7766 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7767}
7768
7769/*
7770 * Reset cursor position. Use whenever cursor was moved because of outputting
7771 * something directly to the screen (shell commands) or a terminal control
7772 * code.
7773 */
7774 void
7775screen_start()
7776{
7777 screen_cur_row = screen_cur_col = 9999;
7778}
7779
7780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 * Move the cursor to position "row","col" in the screen.
7782 * This tries to find the most efficient way to move, minimizing the number of
7783 * characters sent to the terminal.
7784 */
7785 void
7786windgoto(row, col)
7787 int row;
7788 int col;
7789{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007790 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 int i;
7792 int plan;
7793 int cost;
7794 int wouldbe_col;
7795 int noinvcurs;
7796 char_u *bs;
7797 int goto_cost;
7798 int attr;
7799
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007800#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7802
7803#define PLAN_LE 1
7804#define PLAN_CR 2
7805#define PLAN_NL 3
7806#define PLAN_WRITE 4
7807 /* Can't use ScreenLines unless initialized */
7808 if (ScreenLines == NULL)
7809 return;
7810
7811 if (col != screen_cur_col || row != screen_cur_row)
7812 {
7813 /* Check for valid position. */
7814 if (row < 0) /* window without text lines? */
7815 row = 0;
7816 if (row >= screen_Rows)
7817 row = screen_Rows - 1;
7818 if (col >= screen_Columns)
7819 col = screen_Columns - 1;
7820
7821 /* check if no cursor movement is allowed in highlight mode */
7822 if (screen_attr && *T_MS == NUL)
7823 noinvcurs = HIGHL_COST;
7824 else
7825 noinvcurs = 0;
7826 goto_cost = GOTO_COST + noinvcurs;
7827
7828 /*
7829 * Plan how to do the positioning:
7830 * 1. Use CR to move it to column 0, same row.
7831 * 2. Use T_LE to move it a few columns to the left.
7832 * 3. Use NL to move a few lines down, column 0.
7833 * 4. Move a few columns to the right with T_ND or by writing chars.
7834 *
7835 * Don't do this if the cursor went beyond the last column, the cursor
7836 * position is unknown then (some terminals wrap, some don't )
7837 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007838 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 * characters to move the cursor to the right.
7840 */
7841 if (row >= screen_cur_row && screen_cur_col < Columns)
7842 {
7843 /*
7844 * If the cursor is in the same row, bigger col, we can use CR
7845 * or T_LE.
7846 */
7847 bs = NULL; /* init for GCC */
7848 attr = screen_attr;
7849 if (row == screen_cur_row && col < screen_cur_col)
7850 {
7851 /* "le" is preferred over "bc", because "bc" is obsolete */
7852 if (*T_LE)
7853 bs = T_LE; /* "cursor left" */
7854 else
7855 bs = T_BC; /* "backspace character (old) */
7856 if (*bs)
7857 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7858 else
7859 cost = 999;
7860 if (col + 1 < cost) /* using CR is less characters */
7861 {
7862 plan = PLAN_CR;
7863 wouldbe_col = 0;
7864 cost = 1; /* CR is just one character */
7865 }
7866 else
7867 {
7868 plan = PLAN_LE;
7869 wouldbe_col = col;
7870 }
7871 if (noinvcurs) /* will stop highlighting */
7872 {
7873 cost += noinvcurs;
7874 attr = 0;
7875 }
7876 }
7877
7878 /*
7879 * If the cursor is above where we want to be, we can use CR LF.
7880 */
7881 else if (row > screen_cur_row)
7882 {
7883 plan = PLAN_NL;
7884 wouldbe_col = 0;
7885 cost = (row - screen_cur_row) * 2; /* CR LF */
7886 if (noinvcurs) /* will stop highlighting */
7887 {
7888 cost += noinvcurs;
7889 attr = 0;
7890 }
7891 }
7892
7893 /*
7894 * If the cursor is in the same row, smaller col, just use write.
7895 */
7896 else
7897 {
7898 plan = PLAN_WRITE;
7899 wouldbe_col = screen_cur_col;
7900 cost = 0;
7901 }
7902
7903 /*
7904 * Check if any characters that need to be written have the
7905 * correct attributes. Also avoid UTF-8 characters.
7906 */
7907 i = col - wouldbe_col;
7908 if (i > 0)
7909 cost += i;
7910 if (cost < goto_cost && i > 0)
7911 {
7912 /*
7913 * Check if the attributes are correct without additionally
7914 * stopping highlighting.
7915 */
7916 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7917 while (i && *p++ == attr)
7918 --i;
7919 if (i != 0)
7920 {
7921 /*
7922 * Try if it works when highlighting is stopped here.
7923 */
7924 if (*--p == 0)
7925 {
7926 cost += noinvcurs;
7927 while (i && *p++ == 0)
7928 --i;
7929 }
7930 if (i != 0)
7931 cost = 999; /* different attributes, don't do it */
7932 }
7933#ifdef FEAT_MBYTE
7934 if (enc_utf8)
7935 {
7936 /* Don't use an UTF-8 char for positioning, it's slow. */
7937 for (i = wouldbe_col; i < col; ++i)
7938 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7939 {
7940 cost = 999;
7941 break;
7942 }
7943 }
7944#endif
7945 }
7946
7947 /*
7948 * We can do it without term_windgoto()!
7949 */
7950 if (cost < goto_cost)
7951 {
7952 if (plan == PLAN_LE)
7953 {
7954 if (noinvcurs)
7955 screen_stop_highlight();
7956 while (screen_cur_col > col)
7957 {
7958 out_str(bs);
7959 --screen_cur_col;
7960 }
7961 }
7962 else if (plan == PLAN_CR)
7963 {
7964 if (noinvcurs)
7965 screen_stop_highlight();
7966 out_char('\r');
7967 screen_cur_col = 0;
7968 }
7969 else if (plan == PLAN_NL)
7970 {
7971 if (noinvcurs)
7972 screen_stop_highlight();
7973 while (screen_cur_row < row)
7974 {
7975 out_char('\n');
7976 ++screen_cur_row;
7977 }
7978 screen_cur_col = 0;
7979 }
7980
7981 i = col - screen_cur_col;
7982 if (i > 0)
7983 {
7984 /*
7985 * Use cursor-right if it's one character only. Avoids
7986 * removing a line of pixels from the last bold char, when
7987 * using the bold trick in the GUI.
7988 */
7989 if (T_ND[0] != NUL && T_ND[1] == NUL)
7990 {
7991 while (i-- > 0)
7992 out_char(*T_ND);
7993 }
7994 else
7995 {
7996 int off;
7997
7998 off = LineOffset[row] + screen_cur_col;
7999 while (i-- > 0)
8000 {
8001 if (ScreenAttrs[off] != screen_attr)
8002 screen_stop_highlight();
8003#ifdef FEAT_MBYTE
8004 out_flush_check();
8005#endif
8006 out_char(ScreenLines[off]);
8007#ifdef FEAT_MBYTE
8008 if (enc_dbcs == DBCS_JPNU
8009 && ScreenLines[off] == 0x8e)
8010 out_char(ScreenLines2[off]);
8011#endif
8012 ++off;
8013 }
8014 }
8015 }
8016 }
8017 }
8018 else
8019 cost = 999;
8020
8021 if (cost >= goto_cost)
8022 {
8023 if (noinvcurs)
8024 screen_stop_highlight();
8025 if (row == screen_cur_row && (col > screen_cur_col) &&
8026 *T_CRI != NUL)
8027 term_cursor_right(col - screen_cur_col);
8028 else
8029 term_windgoto(row, col);
8030 }
8031 screen_cur_row = row;
8032 screen_cur_col = col;
8033 }
8034}
8035
8036/*
8037 * Set cursor to its position in the current window.
8038 */
8039 void
8040setcursor()
8041{
8042 if (redrawing())
8043 {
8044 validate_cursor();
8045 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8046 W_WINCOL(curwin) + (
8047#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008048 /* With 'rightleft' set and the cursor on a double-wide
8049 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8051# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008052 (has_mbyte
8053 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8054 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055# endif
8056 1)) :
8057#endif
8058 curwin->w_wcol));
8059 }
8060}
8061
8062
8063/*
8064 * insert 'line_count' lines at 'row' in window 'wp'
8065 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8066 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8067 * scrolling.
8068 * Returns FAIL if the lines are not inserted, OK for success.
8069 */
8070 int
8071win_ins_lines(wp, row, line_count, invalid, mayclear)
8072 win_T *wp;
8073 int row;
8074 int line_count;
8075 int invalid;
8076 int mayclear;
8077{
8078 int did_delete;
8079 int nextrow;
8080 int lastrow;
8081 int retval;
8082
8083 if (invalid)
8084 wp->w_lines_valid = 0;
8085
8086 if (wp->w_height < 5)
8087 return FAIL;
8088
8089 if (line_count > wp->w_height - row)
8090 line_count = wp->w_height - row;
8091
8092 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8093 if (retval != MAYBE)
8094 return retval;
8095
8096 /*
8097 * If there is a next window or a status line, we first try to delete the
8098 * lines at the bottom to avoid messing what is after the window.
8099 * If this fails and there are following windows, don't do anything to avoid
8100 * messing up those windows, better just redraw.
8101 */
8102 did_delete = FALSE;
8103#ifdef FEAT_WINDOWS
8104 if (wp->w_next != NULL || wp->w_status_height)
8105 {
8106 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8107 line_count, (int)Rows, FALSE, NULL) == OK)
8108 did_delete = TRUE;
8109 else if (wp->w_next)
8110 return FAIL;
8111 }
8112#endif
8113 /*
8114 * if no lines deleted, blank the lines that will end up below the window
8115 */
8116 if (!did_delete)
8117 {
8118#ifdef FEAT_WINDOWS
8119 wp->w_redr_status = TRUE;
8120#endif
8121 redraw_cmdline = TRUE;
8122 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8123 lastrow = nextrow + line_count;
8124 if (lastrow > Rows)
8125 lastrow = Rows;
8126 screen_fill(nextrow - line_count, lastrow - line_count,
8127 W_WINCOL(wp), (int)W_ENDCOL(wp),
8128 ' ', ' ', 0);
8129 }
8130
8131 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8132 == FAIL)
8133 {
8134 /* deletion will have messed up other windows */
8135 if (did_delete)
8136 {
8137#ifdef FEAT_WINDOWS
8138 wp->w_redr_status = TRUE;
8139#endif
8140 win_rest_invalid(W_NEXT(wp));
8141 }
8142 return FAIL;
8143 }
8144
8145 return OK;
8146}
8147
8148/*
8149 * delete "line_count" window lines at "row" in window "wp"
8150 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8151 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8152 * scrolling
8153 * Return OK for success, FAIL if the lines are not deleted.
8154 */
8155 int
8156win_del_lines(wp, row, line_count, invalid, mayclear)
8157 win_T *wp;
8158 int row;
8159 int line_count;
8160 int invalid;
8161 int mayclear;
8162{
8163 int retval;
8164
8165 if (invalid)
8166 wp->w_lines_valid = 0;
8167
8168 if (line_count > wp->w_height - row)
8169 line_count = wp->w_height - row;
8170
8171 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8172 if (retval != MAYBE)
8173 return retval;
8174
8175 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8176 (int)Rows, FALSE, NULL) == FAIL)
8177 return FAIL;
8178
8179#ifdef FEAT_WINDOWS
8180 /*
8181 * If there are windows or status lines below, try to put them at the
8182 * correct place. If we can't do that, they have to be redrawn.
8183 */
8184 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8185 {
8186 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8187 line_count, (int)Rows, NULL) == FAIL)
8188 {
8189 wp->w_redr_status = TRUE;
8190 win_rest_invalid(wp->w_next);
8191 }
8192 }
8193 /*
8194 * If this is the last window and there is no status line, redraw the
8195 * command line later.
8196 */
8197 else
8198#endif
8199 redraw_cmdline = TRUE;
8200 return OK;
8201}
8202
8203/*
8204 * Common code for win_ins_lines() and win_del_lines().
8205 * Returns OK or FAIL when the work has been done.
8206 * Returns MAYBE when not finished yet.
8207 */
8208 static int
8209win_do_lines(wp, row, line_count, mayclear, del)
8210 win_T *wp;
8211 int row;
8212 int line_count;
8213 int mayclear;
8214 int del;
8215{
8216 int retval;
8217
8218 if (!redrawing() || line_count <= 0)
8219 return FAIL;
8220
8221 /* only a few lines left: redraw is faster */
8222 if (mayclear && Rows - line_count < 5
8223#ifdef FEAT_VERTSPLIT
8224 && wp->w_width == Columns
8225#endif
8226 )
8227 {
8228 screenclear(); /* will set wp->w_lines_valid to 0 */
8229 return FAIL;
8230 }
8231
8232 /*
8233 * Delete all remaining lines
8234 */
8235 if (row + line_count >= wp->w_height)
8236 {
8237 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8238 W_WINCOL(wp), (int)W_ENDCOL(wp),
8239 ' ', ' ', 0);
8240 return OK;
8241 }
8242
8243 /*
8244 * when scrolling, the message on the command line should be cleared,
8245 * otherwise it will stay there forever.
8246 */
8247 clear_cmdline = TRUE;
8248
8249 /*
8250 * If the terminal can set a scroll region, use that.
8251 * Always do this in a vertically split window. This will redraw from
8252 * ScreenLines[] when t_CV isn't defined. That's faster than using
8253 * win_line().
8254 * Don't use a scroll region when we are going to redraw the text, writing
8255 * a character in the lower right corner of the scroll region causes a
8256 * scroll-up in the DJGPP version.
8257 */
8258 if (scroll_region
8259#ifdef FEAT_VERTSPLIT
8260 || W_WIDTH(wp) != Columns
8261#endif
8262 )
8263 {
8264#ifdef FEAT_VERTSPLIT
8265 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8266#endif
8267 scroll_region_set(wp, row);
8268 if (del)
8269 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8270 wp->w_height - row, FALSE, wp);
8271 else
8272 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8273 wp->w_height - row, wp);
8274#ifdef FEAT_VERTSPLIT
8275 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8276#endif
8277 scroll_region_reset();
8278 return retval;
8279 }
8280
8281#ifdef FEAT_WINDOWS
8282 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8283 return FAIL;
8284#endif
8285
8286 return MAYBE;
8287}
8288
8289/*
8290 * window 'wp' and everything after it is messed up, mark it for redraw
8291 */
8292 static void
8293win_rest_invalid(wp)
8294 win_T *wp;
8295{
8296#ifdef FEAT_WINDOWS
8297 while (wp != NULL)
8298#else
8299 if (wp != NULL)
8300#endif
8301 {
8302 redraw_win_later(wp, NOT_VALID);
8303#ifdef FEAT_WINDOWS
8304 wp->w_redr_status = TRUE;
8305 wp = wp->w_next;
8306#endif
8307 }
8308 redraw_cmdline = TRUE;
8309}
8310
8311/*
8312 * The rest of the routines in this file perform screen manipulations. The
8313 * given operation is performed physically on the screen. The corresponding
8314 * change is also made to the internal screen image. In this way, the editor
8315 * anticipates the effect of editing changes on the appearance of the screen.
8316 * That way, when we call screenupdate a complete redraw isn't usually
8317 * necessary. Another advantage is that we can keep adding code to anticipate
8318 * screen changes, and in the meantime, everything still works.
8319 */
8320
8321/*
8322 * types for inserting or deleting lines
8323 */
8324#define USE_T_CAL 1
8325#define USE_T_CDL 2
8326#define USE_T_AL 3
8327#define USE_T_CE 4
8328#define USE_T_DL 5
8329#define USE_T_SR 6
8330#define USE_NL 7
8331#define USE_T_CD 8
8332#define USE_REDRAW 9
8333
8334/*
8335 * insert lines on the screen and update ScreenLines[]
8336 * 'end' is the line after the scrolled part. Normally it is Rows.
8337 * When scrolling region used 'off' is the offset from the top for the region.
8338 * 'row' and 'end' are relative to the start of the region.
8339 *
8340 * return FAIL for failure, OK for success.
8341 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008342 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343screen_ins_lines(off, row, line_count, end, wp)
8344 int off;
8345 int row;
8346 int line_count;
8347 int end;
8348 win_T *wp; /* NULL or window to use width from */
8349{
8350 int i;
8351 int j;
8352 unsigned temp;
8353 int cursor_row;
8354 int type;
8355 int result_empty;
8356 int can_ce = can_clear(T_CE);
8357
8358 /*
8359 * FAIL if
8360 * - there is no valid screen
8361 * - the screen has to be redrawn completely
8362 * - the line count is less than one
8363 * - the line count is more than 'ttyscroll'
8364 */
8365 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8366 return FAIL;
8367
8368 /*
8369 * There are seven ways to insert lines:
8370 * 0. When in a vertically split window and t_CV isn't set, redraw the
8371 * characters from ScreenLines[].
8372 * 1. Use T_CD (clear to end of display) if it exists and the result of
8373 * the insert is just empty lines
8374 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8375 * present or line_count > 1. It looks better if we do all the inserts
8376 * at once.
8377 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8378 * insert is just empty lines and T_CE is not present or line_count >
8379 * 1.
8380 * 4. Use T_AL (insert line) if it exists.
8381 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8382 * just empty lines.
8383 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8384 * just empty lines.
8385 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8386 * the 'da' flag is not set or we have clear line capability.
8387 * 8. redraw the characters from ScreenLines[].
8388 *
8389 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8390 * the scrollbar for the window. It does have insert line, use that if it
8391 * exists.
8392 */
8393 result_empty = (row + line_count >= end);
8394#ifdef FEAT_VERTSPLIT
8395 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8396 type = USE_REDRAW;
8397 else
8398#endif
8399 if (can_clear(T_CD) && result_empty)
8400 type = USE_T_CD;
8401 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8402 type = USE_T_CAL;
8403 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8404 type = USE_T_CDL;
8405 else if (*T_AL != NUL)
8406 type = USE_T_AL;
8407 else if (can_ce && result_empty)
8408 type = USE_T_CE;
8409 else if (*T_DL != NUL && result_empty)
8410 type = USE_T_DL;
8411 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8412 type = USE_T_SR;
8413 else
8414 return FAIL;
8415
8416 /*
8417 * For clearing the lines screen_del_lines() is used. This will also take
8418 * care of t_db if necessary.
8419 */
8420 if (type == USE_T_CD || type == USE_T_CDL ||
8421 type == USE_T_CE || type == USE_T_DL)
8422 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8423
8424 /*
8425 * If text is retained below the screen, first clear or delete as many
8426 * lines at the bottom of the window as are about to be inserted so that
8427 * the deleted lines won't later surface during a screen_del_lines.
8428 */
8429 if (*T_DB)
8430 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8431
8432#ifdef FEAT_CLIPBOARD
8433 /* Remove a modeless selection when inserting lines halfway the screen
8434 * or not the full width of the screen. */
8435 if (off + row > 0
8436# ifdef FEAT_VERTSPLIT
8437 || (wp != NULL && wp->w_width != Columns)
8438# endif
8439 )
8440 clip_clear_selection();
8441 else
8442 clip_scroll_selection(-line_count);
8443#endif
8444
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445#ifdef FEAT_GUI
8446 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8447 * scrolling is actually carried out. */
8448 gui_dont_update_cursor();
8449#endif
8450
8451 if (*T_CCS != NUL) /* cursor relative to region */
8452 cursor_row = row;
8453 else
8454 cursor_row = row + off;
8455
8456 /*
8457 * Shift LineOffset[] line_count down to reflect the inserted lines.
8458 * Clear the inserted lines in ScreenLines[].
8459 */
8460 row += off;
8461 end += off;
8462 for (i = 0; i < line_count; ++i)
8463 {
8464#ifdef FEAT_VERTSPLIT
8465 if (wp != NULL && wp->w_width != Columns)
8466 {
8467 /* need to copy part of a line */
8468 j = end - 1 - i;
8469 while ((j -= line_count) >= row)
8470 linecopy(j + line_count, j, wp);
8471 j += line_count;
8472 if (can_clear((char_u *)" "))
8473 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8474 else
8475 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8476 LineWraps[j] = FALSE;
8477 }
8478 else
8479#endif
8480 {
8481 j = end - 1 - i;
8482 temp = LineOffset[j];
8483 while ((j -= line_count) >= row)
8484 {
8485 LineOffset[j + line_count] = LineOffset[j];
8486 LineWraps[j + line_count] = LineWraps[j];
8487 }
8488 LineOffset[j + line_count] = temp;
8489 LineWraps[j + line_count] = FALSE;
8490 if (can_clear((char_u *)" "))
8491 lineclear(temp, (int)Columns);
8492 else
8493 lineinvalid(temp, (int)Columns);
8494 }
8495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496
8497 screen_stop_highlight();
8498 windgoto(cursor_row, 0);
8499
8500#ifdef FEAT_VERTSPLIT
8501 /* redraw the characters */
8502 if (type == USE_REDRAW)
8503 redraw_block(row, end, wp);
8504 else
8505#endif
8506 if (type == USE_T_CAL)
8507 {
8508 term_append_lines(line_count);
8509 screen_start(); /* don't know where cursor is now */
8510 }
8511 else
8512 {
8513 for (i = 0; i < line_count; i++)
8514 {
8515 if (type == USE_T_AL)
8516 {
8517 if (i && cursor_row != 0)
8518 windgoto(cursor_row, 0);
8519 out_str(T_AL);
8520 }
8521 else /* type == USE_T_SR */
8522 out_str(T_SR);
8523 screen_start(); /* don't know where cursor is now */
8524 }
8525 }
8526
8527 /*
8528 * With scroll-reverse and 'da' flag set we need to clear the lines that
8529 * have been scrolled down into the region.
8530 */
8531 if (type == USE_T_SR && *T_DA)
8532 {
8533 for (i = 0; i < line_count; ++i)
8534 {
8535 windgoto(off + i, 0);
8536 out_str(T_CE);
8537 screen_start(); /* don't know where cursor is now */
8538 }
8539 }
8540
8541#ifdef FEAT_GUI
8542 gui_can_update_cursor();
8543 if (gui.in_use)
8544 out_flush(); /* always flush after a scroll */
8545#endif
8546 return OK;
8547}
8548
8549/*
8550 * delete lines on the screen and update ScreenLines[]
8551 * 'end' is the line after the scrolled part. Normally it is Rows.
8552 * When scrolling region used 'off' is the offset from the top for the region.
8553 * 'row' and 'end' are relative to the start of the region.
8554 *
8555 * Return OK for success, FAIL if the lines are not deleted.
8556 */
8557/*ARGSUSED*/
8558 int
8559screen_del_lines(off, row, line_count, end, force, wp)
8560 int off;
8561 int row;
8562 int line_count;
8563 int end;
8564 int force; /* even when line_count > p_ttyscroll */
8565 win_T *wp; /* NULL or window to use width from */
8566{
8567 int j;
8568 int i;
8569 unsigned temp;
8570 int cursor_row;
8571 int cursor_end;
8572 int result_empty; /* result is empty until end of region */
8573 int can_delete; /* deleting line codes can be used */
8574 int type;
8575
8576 /*
8577 * FAIL if
8578 * - there is no valid screen
8579 * - the screen has to be redrawn completely
8580 * - the line count is less than one
8581 * - the line count is more than 'ttyscroll'
8582 */
8583 if (!screen_valid(TRUE) || line_count <= 0 ||
8584 (!force && line_count > p_ttyscroll))
8585 return FAIL;
8586
8587 /*
8588 * Check if the rest of the current region will become empty.
8589 */
8590 result_empty = row + line_count >= end;
8591
8592 /*
8593 * We can delete lines only when 'db' flag not set or when 'ce' option
8594 * available.
8595 */
8596 can_delete = (*T_DB == NUL || can_clear(T_CE));
8597
8598 /*
8599 * There are six ways to delete lines:
8600 * 0. When in a vertically split window and t_CV isn't set, redraw the
8601 * characters from ScreenLines[].
8602 * 1. Use T_CD if it exists and the result is empty.
8603 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8604 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8605 * none of the other ways work.
8606 * 4. Use T_CE (erase line) if the result is empty.
8607 * 5. Use T_DL (delete line) if it exists.
8608 * 6. redraw the characters from ScreenLines[].
8609 */
8610#ifdef FEAT_VERTSPLIT
8611 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8612 type = USE_REDRAW;
8613 else
8614#endif
8615 if (can_clear(T_CD) && result_empty)
8616 type = USE_T_CD;
8617#if defined(__BEOS__) && defined(BEOS_DR8)
8618 /*
8619 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8620 * its internal termcap... this works okay for tests which test *T_DB !=
8621 * NUL. It has the disadvantage that the user cannot use any :set t_*
8622 * command to get T_DB (back) to empty_option, only :set term=... will do
8623 * the trick...
8624 * Anyway, this hack will hopefully go away with the next OS release.
8625 * (Olaf Seibert)
8626 */
8627 else if (row == 0 && T_DB == empty_option
8628 && (line_count == 1 || *T_CDL == NUL))
8629#else
8630 else if (row == 0 && (
8631#ifndef AMIGA
8632 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8633 * up, so use delete-line command */
8634 line_count == 1 ||
8635#endif
8636 *T_CDL == NUL))
8637#endif
8638 type = USE_NL;
8639 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8640 type = USE_T_CDL;
8641 else if (can_clear(T_CE) && result_empty
8642#ifdef FEAT_VERTSPLIT
8643 && (wp == NULL || wp->w_width == Columns)
8644#endif
8645 )
8646 type = USE_T_CE;
8647 else if (*T_DL != NUL && can_delete)
8648 type = USE_T_DL;
8649 else if (*T_CDL != NUL && can_delete)
8650 type = USE_T_CDL;
8651 else
8652 return FAIL;
8653
8654#ifdef FEAT_CLIPBOARD
8655 /* Remove a modeless selection when deleting lines halfway the screen or
8656 * not the full width of the screen. */
8657 if (off + row > 0
8658# ifdef FEAT_VERTSPLIT
8659 || (wp != NULL && wp->w_width != Columns)
8660# endif
8661 )
8662 clip_clear_selection();
8663 else
8664 clip_scroll_selection(line_count);
8665#endif
8666
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667#ifdef FEAT_GUI
8668 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8669 * scrolling is actually carried out. */
8670 gui_dont_update_cursor();
8671#endif
8672
8673 if (*T_CCS != NUL) /* cursor relative to region */
8674 {
8675 cursor_row = row;
8676 cursor_end = end;
8677 }
8678 else
8679 {
8680 cursor_row = row + off;
8681 cursor_end = end + off;
8682 }
8683
8684 /*
8685 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8686 * Clear the inserted lines in ScreenLines[].
8687 */
8688 row += off;
8689 end += off;
8690 for (i = 0; i < line_count; ++i)
8691 {
8692#ifdef FEAT_VERTSPLIT
8693 if (wp != NULL && wp->w_width != Columns)
8694 {
8695 /* need to copy part of a line */
8696 j = row + i;
8697 while ((j += line_count) <= end - 1)
8698 linecopy(j - line_count, j, wp);
8699 j -= line_count;
8700 if (can_clear((char_u *)" "))
8701 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8702 else
8703 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8704 LineWraps[j] = FALSE;
8705 }
8706 else
8707#endif
8708 {
8709 /* whole width, moving the line pointers is faster */
8710 j = row + i;
8711 temp = LineOffset[j];
8712 while ((j += line_count) <= end - 1)
8713 {
8714 LineOffset[j - line_count] = LineOffset[j];
8715 LineWraps[j - line_count] = LineWraps[j];
8716 }
8717 LineOffset[j - line_count] = temp;
8718 LineWraps[j - line_count] = FALSE;
8719 if (can_clear((char_u *)" "))
8720 lineclear(temp, (int)Columns);
8721 else
8722 lineinvalid(temp, (int)Columns);
8723 }
8724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725
8726 screen_stop_highlight();
8727
8728#ifdef FEAT_VERTSPLIT
8729 /* redraw the characters */
8730 if (type == USE_REDRAW)
8731 redraw_block(row, end, wp);
8732 else
8733#endif
8734 if (type == USE_T_CD) /* delete the lines */
8735 {
8736 windgoto(cursor_row, 0);
8737 out_str(T_CD);
8738 screen_start(); /* don't know where cursor is now */
8739 }
8740 else if (type == USE_T_CDL)
8741 {
8742 windgoto(cursor_row, 0);
8743 term_delete_lines(line_count);
8744 screen_start(); /* don't know where cursor is now */
8745 }
8746 /*
8747 * Deleting lines at top of the screen or scroll region: Just scroll
8748 * the whole screen (scroll region) up by outputting newlines on the
8749 * last line.
8750 */
8751 else if (type == USE_NL)
8752 {
8753 windgoto(cursor_end - 1, 0);
8754 for (i = line_count; --i >= 0; )
8755 out_char('\n'); /* cursor will remain on same line */
8756 }
8757 else
8758 {
8759 for (i = line_count; --i >= 0; )
8760 {
8761 if (type == USE_T_DL)
8762 {
8763 windgoto(cursor_row, 0);
8764 out_str(T_DL); /* delete a line */
8765 }
8766 else /* type == USE_T_CE */
8767 {
8768 windgoto(cursor_row + i, 0);
8769 out_str(T_CE); /* erase a line */
8770 }
8771 screen_start(); /* don't know where cursor is now */
8772 }
8773 }
8774
8775 /*
8776 * If the 'db' flag is set, we need to clear the lines that have been
8777 * scrolled up at the bottom of the region.
8778 */
8779 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8780 {
8781 for (i = line_count; i > 0; --i)
8782 {
8783 windgoto(cursor_end - i, 0);
8784 out_str(T_CE); /* erase a line */
8785 screen_start(); /* don't know where cursor is now */
8786 }
8787 }
8788
8789#ifdef FEAT_GUI
8790 gui_can_update_cursor();
8791 if (gui.in_use)
8792 out_flush(); /* always flush after a scroll */
8793#endif
8794
8795 return OK;
8796}
8797
8798/*
8799 * show the current mode and ruler
8800 *
8801 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8802 * If clear_cmdline is FALSE there may be a message there that needs to be
8803 * cleared only if a mode is shown.
8804 * Return the length of the message (0 if no message).
8805 */
8806 int
8807showmode()
8808{
8809 int need_clear;
8810 int length = 0;
8811 int do_mode;
8812 int attr;
8813 int nwr_save;
8814#ifdef FEAT_INS_EXPAND
8815 int sub_attr;
8816#endif
8817
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008818 do_mode = ((p_smd && msg_silent == 0)
8819 && ((State & INSERT)
8820 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821#ifdef FEAT_VISUAL
8822 || VIsual_active
8823#endif
8824 ));
8825 if (do_mode || Recording)
8826 {
8827 /*
8828 * Don't show mode right now, when not redrawing or inside a mapping.
8829 * Call char_avail() only when we are going to show something, because
8830 * it takes a bit of time.
8831 */
8832 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8833 {
8834 redraw_cmdline = TRUE; /* show mode later */
8835 return 0;
8836 }
8837
8838 nwr_save = need_wait_return;
8839
8840 /* wait a bit before overwriting an important message */
8841 check_for_delay(FALSE);
8842
8843 /* if the cmdline is more than one line high, erase top lines */
8844 need_clear = clear_cmdline;
8845 if (clear_cmdline && cmdline_row < Rows - 1)
8846 msg_clr_cmdline(); /* will reset clear_cmdline */
8847
8848 /* Position on the last line in the window, column 0 */
8849 msg_pos_mode();
8850 cursor_off();
8851 attr = hl_attr(HLF_CM); /* Highlight mode */
8852 if (do_mode)
8853 {
8854 MSG_PUTS_ATTR("--", attr);
8855#if defined(FEAT_XIM)
8856 if (xic != NULL && im_get_status() && !p_imdisable
8857 && curbuf->b_p_iminsert == B_IMODE_IM)
8858# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8859 MSG_PUTS_ATTR(" IM", attr);
8860# else
8861 MSG_PUTS_ATTR(" XIM", attr);
8862# endif
8863#endif
8864#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8865 if (gui.in_use)
8866 {
8867 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008868 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 }
8870#endif
8871#ifdef FEAT_INS_EXPAND
8872 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8873 {
8874 /* These messages can get long, avoid a wrap in a narrow
8875 * window. Prefer showing edit_submode_extra. */
8876 length = (Rows - msg_row) * Columns - 3;
8877 if (edit_submode_extra != NULL)
8878 length -= vim_strsize(edit_submode_extra);
8879 if (length > 0)
8880 {
8881 if (edit_submode_pre != NULL)
8882 length -= vim_strsize(edit_submode_pre);
8883 if (length - vim_strsize(edit_submode) > 0)
8884 {
8885 if (edit_submode_pre != NULL)
8886 msg_puts_attr(edit_submode_pre, attr);
8887 msg_puts_attr(edit_submode, attr);
8888 }
8889 if (edit_submode_extra != NULL)
8890 {
8891 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8892 if ((int)edit_submode_highl < (int)HLF_COUNT)
8893 sub_attr = hl_attr(edit_submode_highl);
8894 else
8895 sub_attr = attr;
8896 msg_puts_attr(edit_submode_extra, sub_attr);
8897 }
8898 }
8899 length = 0;
8900 }
8901 else
8902#endif
8903 {
8904#ifdef FEAT_VREPLACE
8905 if (State & VREPLACE_FLAG)
8906 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8907 else
8908#endif
8909 if (State & REPLACE_FLAG)
8910 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8911 else if (State & INSERT)
8912 {
8913#ifdef FEAT_RIGHTLEFT
8914 if (p_ri)
8915 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8916#endif
8917 MSG_PUTS_ATTR(_(" INSERT"), attr);
8918 }
8919 else if (restart_edit == 'I')
8920 MSG_PUTS_ATTR(_(" (insert)"), attr);
8921 else if (restart_edit == 'R')
8922 MSG_PUTS_ATTR(_(" (replace)"), attr);
8923 else if (restart_edit == 'V')
8924 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8925#ifdef FEAT_RIGHTLEFT
8926 if (p_hkmap)
8927 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8928# ifdef FEAT_FKMAP
8929 if (p_fkmap)
8930 MSG_PUTS_ATTR(farsi_text_5, attr);
8931# endif
8932#endif
8933#ifdef FEAT_KEYMAP
8934 if (State & LANGMAP)
8935 {
8936# ifdef FEAT_ARABIC
8937 if (curwin->w_p_arab)
8938 MSG_PUTS_ATTR(_(" Arabic"), attr);
8939 else
8940# endif
8941 MSG_PUTS_ATTR(_(" (lang)"), attr);
8942 }
8943#endif
8944 if ((State & INSERT) && p_paste)
8945 MSG_PUTS_ATTR(_(" (paste)"), attr);
8946
8947#ifdef FEAT_VISUAL
8948 if (VIsual_active)
8949 {
8950 char *p;
8951
8952 /* Don't concatenate separate words to avoid translation
8953 * problems. */
8954 switch ((VIsual_select ? 4 : 0)
8955 + (VIsual_mode == Ctrl_V) * 2
8956 + (VIsual_mode == 'V'))
8957 {
8958 case 0: p = N_(" VISUAL"); break;
8959 case 1: p = N_(" VISUAL LINE"); break;
8960 case 2: p = N_(" VISUAL BLOCK"); break;
8961 case 4: p = N_(" SELECT"); break;
8962 case 5: p = N_(" SELECT LINE"); break;
8963 default: p = N_(" SELECT BLOCK"); break;
8964 }
8965 MSG_PUTS_ATTR(_(p), attr);
8966 }
8967#endif
8968 MSG_PUTS_ATTR(" --", attr);
8969 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 need_clear = TRUE;
8972 }
8973 if (Recording
8974#ifdef FEAT_INS_EXPAND
8975 && edit_submode == NULL /* otherwise it gets too long */
8976#endif
8977 )
8978 {
8979 MSG_PUTS_ATTR(_("recording"), attr);
8980 need_clear = TRUE;
8981 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008982
8983 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 if (need_clear || clear_cmdline)
8985 msg_clr_eos();
8986 msg_didout = FALSE; /* overwrite this message */
8987 length = msg_col;
8988 msg_col = 0;
8989 need_wait_return = nwr_save; /* never ask for hit-return for this */
8990 }
8991 else if (clear_cmdline && msg_silent == 0)
8992 /* Clear the whole command line. Will reset "clear_cmdline". */
8993 msg_clr_cmdline();
8994
8995#ifdef FEAT_CMDL_INFO
8996# ifdef FEAT_VISUAL
8997 /* In Visual mode the size of the selected area must be redrawn. */
8998 if (VIsual_active)
8999 clear_showcmd();
9000# endif
9001
9002 /* If the last window has no status line, the ruler is after the mode
9003 * message and must be redrawn */
9004 if (redrawing()
9005# ifdef FEAT_WINDOWS
9006 && lastwin->w_status_height == 0
9007# endif
9008 )
9009 win_redr_ruler(lastwin, TRUE);
9010#endif
9011 redraw_cmdline = FALSE;
9012 clear_cmdline = FALSE;
9013
9014 return length;
9015}
9016
9017/*
9018 * Position for a mode message.
9019 */
9020 static void
9021msg_pos_mode()
9022{
9023 msg_col = 0;
9024 msg_row = Rows - 1;
9025}
9026
9027/*
9028 * Delete mode message. Used when ESC is typed which is expected to end
9029 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009030 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 */
9032 void
9033unshowmode(force)
9034 int force;
9035{
9036 /*
9037 * Don't delete it right now, when not redrawing or insided a mapping.
9038 */
9039 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9040 redraw_cmdline = TRUE; /* delete mode later */
9041 else
9042 {
9043 msg_pos_mode();
9044 if (Recording)
9045 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9046 msg_clr_eos();
9047 }
9048}
9049
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009050#if defined(FEAT_WINDOWS)
9051/*
9052 * Draw the tab pages line at the top of the Vim window.
9053 */
9054 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009055draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009056{
9057 int tabcount = 0;
9058 tabpage_T *tp;
9059 int tabwidth;
9060 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009061 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009062 int attr;
9063 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009064 win_T *cwp;
9065 int wincount;
9066 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009067 int c;
9068 int len;
9069 int attr_sel = hl_attr(HLF_TPS);
9070 int attr_nosel = hl_attr(HLF_TP);
9071 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009072 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009073 int room;
9074 int use_sep_chars = (t_colors < 8
9075#ifdef FEAT_GUI
9076 && !gui.in_use
9077#endif
9078 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009079
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009080 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009081
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009082#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009083 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009084 if (gui_use_tabline())
9085 {
9086 gui_update_tabline();
9087 return;
9088 }
9089#endif
9090
9091 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009092 return;
9093
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009094#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009095
9096 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9097 for (scol = 0; scol < Columns; ++scol)
9098 TabPageIdxs[scol] = 0;
9099
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009100 /* Use the 'tabline' option if it's set. */
9101 if (*p_tal != NUL)
9102 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009103 int save_called_emsg = called_emsg;
9104
9105 /* Check for an error. If there is one we would loop in redrawing the
9106 * screen. Avoid that by making 'tabline' empty. */
9107 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009108 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009109 if (called_emsg)
9110 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009111 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009112 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009113 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009114 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009115#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009116 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009117 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9118 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009119
Bram Moolenaar238a5642006-02-21 22:12:05 +00009120 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9121 if (tabwidth < 6)
9122 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009123
Bram Moolenaar238a5642006-02-21 22:12:05 +00009124 attr = attr_nosel;
9125 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009126 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009127 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9128 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009129 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009130 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009131
Bram Moolenaar238a5642006-02-21 22:12:05 +00009132 if (tp->tp_topframe == topframe)
9133 attr = attr_sel;
9134 if (use_sep_chars && col > 0)
9135 screen_putchar('|', 0, col++, attr);
9136
9137 if (tp->tp_topframe != topframe)
9138 attr = attr_nosel;
9139
9140 screen_putchar(' ', 0, col++, attr);
9141
9142 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009143 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009144 cwp = curwin;
9145 wp = firstwin;
9146 }
9147 else
9148 {
9149 cwp = tp->tp_curwin;
9150 wp = tp->tp_firstwin;
9151 }
9152
9153 modified = FALSE;
9154 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9155 if (bufIsChanged(wp->w_buffer))
9156 modified = TRUE;
9157 if (modified || wincount > 1)
9158 {
9159 if (wincount > 1)
9160 {
9161 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009162 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009163 if (col + len >= Columns - 3)
9164 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009165 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009166#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009167 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009168#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009169 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009170#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009171 );
9172 col += len;
9173 }
9174 if (modified)
9175 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9176 screen_putchar(' ', 0, col++, attr);
9177 }
9178
9179 room = scol - col + tabwidth - 1;
9180 if (room > 0)
9181 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009182 /* Get buffer name in NameBuff[] */
9183 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009184 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009185 len = vim_strsize(NameBuff);
9186 p = NameBuff;
9187#ifdef FEAT_MBYTE
9188 if (has_mbyte)
9189 while (len > room)
9190 {
9191 len -= ptr2cells(p);
9192 mb_ptr_adv(p);
9193 }
9194 else
9195#endif
9196 if (len > room)
9197 {
9198 p += len - room;
9199 len = room;
9200 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009201 if (len > Columns - col - 1)
9202 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009203
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009204 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009205 col += len;
9206 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009207 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009208
9209 /* Store the tab page number in TabPageIdxs[], so that
9210 * jump_to_mouse() knows where each one is. */
9211 ++tabcount;
9212 while (scol < col)
9213 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009214 }
9215
Bram Moolenaar238a5642006-02-21 22:12:05 +00009216 if (use_sep_chars)
9217 c = '_';
9218 else
9219 c = ' ';
9220 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009221
9222 /* Put an "X" for closing the current tab if there are several. */
9223 if (first_tabpage->tp_next != NULL)
9224 {
9225 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9226 TabPageIdxs[Columns - 1] = -999;
9227 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009228 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009229
9230 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9231 * set. */
9232 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009233}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009234
9235/*
9236 * Get buffer name for "buf" into NameBuff[].
9237 * Takes care of special buffer names and translates special characters.
9238 */
9239 void
9240get_trans_bufname(buf)
9241 buf_T *buf;
9242{
9243 if (buf_spname(buf) != NULL)
9244 STRCPY(NameBuff, buf_spname(buf));
9245 else
9246 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9247 trans_characters(NameBuff, MAXPATHL);
9248}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009249#endif
9250
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9252/*
9253 * Get the character to use in a status line. Get its attributes in "*attr".
9254 */
9255 static int
9256fillchar_status(attr, is_curwin)
9257 int *attr;
9258 int is_curwin;
9259{
9260 int fill;
9261 if (is_curwin)
9262 {
9263 *attr = hl_attr(HLF_S);
9264 fill = fill_stl;
9265 }
9266 else
9267 {
9268 *attr = hl_attr(HLF_SNC);
9269 fill = fill_stlnc;
9270 }
9271 /* Use fill when there is highlighting, and highlighting of current
9272 * window differs, or the fillchars differ, or this is not the
9273 * current window */
9274 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9275 || !is_curwin || firstwin == lastwin)
9276 || (fill_stl != fill_stlnc)))
9277 return fill;
9278 if (is_curwin)
9279 return '^';
9280 return '=';
9281}
9282#endif
9283
9284#ifdef FEAT_VERTSPLIT
9285/*
9286 * Get the character to use in a separator between vertically split windows.
9287 * Get its attributes in "*attr".
9288 */
9289 static int
9290fillchar_vsep(attr)
9291 int *attr;
9292{
9293 *attr = hl_attr(HLF_C);
9294 if (*attr == 0 && fill_vert == ' ')
9295 return '|';
9296 else
9297 return fill_vert;
9298}
9299#endif
9300
9301/*
9302 * Return TRUE if redrawing should currently be done.
9303 */
9304 int
9305redrawing()
9306{
9307 return (!RedrawingDisabled
9308 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9309}
9310
9311/*
9312 * Return TRUE if printing messages should currently be done.
9313 */
9314 int
9315messaging()
9316{
9317 return (!(p_lz && char_avail() && !KeyTyped));
9318}
9319
9320/*
9321 * Show current status info in ruler and various other places
9322 * If always is FALSE, only show ruler if position has changed.
9323 */
9324 void
9325showruler(always)
9326 int always;
9327{
9328 if (!always && !redrawing())
9329 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009330#ifdef FEAT_INS_EXPAND
9331 if (pum_visible())
9332 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009333# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009334 /* Don't redraw right now, do it later. */
9335 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009336# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009337 return;
9338 }
9339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009341 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009342 {
9343 redraw_custum_statusline(curwin);
9344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 else
9346#endif
9347#ifdef FEAT_CMDL_INFO
9348 win_redr_ruler(curwin, always);
9349#endif
9350
9351#ifdef FEAT_TITLE
9352 if (need_maketitle
9353# ifdef FEAT_STL_OPT
9354 || (p_icon && (stl_syntax & STL_IN_ICON))
9355 || (p_title && (stl_syntax & STL_IN_TITLE))
9356# endif
9357 )
9358 maketitle();
9359#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009360#ifdef FEAT_WINDOWS
9361 /* Redraw the tab pages line if needed. */
9362 if (redraw_tabline)
9363 draw_tabline();
9364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365}
9366
9367#ifdef FEAT_CMDL_INFO
9368 static void
9369win_redr_ruler(wp, always)
9370 win_T *wp;
9371 int always;
9372{
9373 char_u buffer[70];
9374 int row;
9375 int fillchar;
9376 int attr;
9377 int empty_line = FALSE;
9378 colnr_T virtcol;
9379 int i;
9380 int o;
9381#ifdef FEAT_VERTSPLIT
9382 int this_ru_col;
9383 int off = 0;
9384 int width = Columns;
9385# define WITH_OFF(x) x
9386# define WITH_WIDTH(x) x
9387#else
9388# define WITH_OFF(x) 0
9389# define WITH_WIDTH(x) Columns
9390# define this_ru_col ru_col
9391#endif
9392
9393 /* If 'ruler' off or redrawing disabled, don't do anything */
9394 if (!p_ru)
9395 return;
9396
9397 /*
9398 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9399 * after deleting lines, before cursor.lnum is corrected.
9400 */
9401 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9402 return;
9403
9404#ifdef FEAT_INS_EXPAND
9405 /* Don't draw the ruler while doing insert-completion, it might overwrite
9406 * the (long) mode message. */
9407# ifdef FEAT_WINDOWS
9408 if (wp == lastwin && lastwin->w_status_height == 0)
9409# endif
9410 if (edit_submode != NULL)
9411 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009412 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9413 if (pum_visible())
9414 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415#endif
9416
9417#ifdef FEAT_STL_OPT
9418 if (*p_ruf)
9419 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009420 int save_called_emsg = called_emsg;
9421
9422 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009424 if (called_emsg)
9425 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009426 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009427 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 return;
9429 }
9430#endif
9431
9432 /*
9433 * Check if not in Insert mode and the line is empty (will show "0-1").
9434 */
9435 if (!(State & INSERT)
9436 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9437 empty_line = TRUE;
9438
9439 /*
9440 * Only draw the ruler when something changed.
9441 */
9442 validate_virtcol_win(wp);
9443 if ( redraw_cmdline
9444 || always
9445 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9446 || wp->w_cursor.col != wp->w_ru_cursor.col
9447 || wp->w_virtcol != wp->w_ru_virtcol
9448#ifdef FEAT_VIRTUALEDIT
9449 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9450#endif
9451 || wp->w_topline != wp->w_ru_topline
9452 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9453#ifdef FEAT_DIFF
9454 || wp->w_topfill != wp->w_ru_topfill
9455#endif
9456 || empty_line != wp->w_ru_empty)
9457 {
9458 cursor_off();
9459#ifdef FEAT_WINDOWS
9460 if (wp->w_status_height)
9461 {
9462 row = W_WINROW(wp) + wp->w_height;
9463 fillchar = fillchar_status(&attr, wp == curwin);
9464# ifdef FEAT_VERTSPLIT
9465 off = W_WINCOL(wp);
9466 width = W_WIDTH(wp);
9467# endif
9468 }
9469 else
9470#endif
9471 {
9472 row = Rows - 1;
9473 fillchar = ' ';
9474 attr = 0;
9475#ifdef FEAT_VERTSPLIT
9476 width = Columns;
9477 off = 0;
9478#endif
9479 }
9480
9481 /* In list mode virtcol needs to be recomputed */
9482 virtcol = wp->w_virtcol;
9483 if (wp->w_p_list && lcs_tab1 == NUL)
9484 {
9485 wp->w_p_list = FALSE;
9486 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9487 wp->w_p_list = TRUE;
9488 }
9489
9490 /*
9491 * Some sprintfs return the length, some return a pointer.
9492 * To avoid portability problems we use strlen() here.
9493 */
9494 sprintf((char *)buffer, "%ld,",
9495 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9496 ? 0L
9497 : (long)(wp->w_cursor.lnum));
9498 col_print(buffer + STRLEN(buffer),
9499 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9500 (int)virtcol + 1);
9501
9502 /*
9503 * Add a "50%" if there is room for it.
9504 * On the last line, don't print in the last column (scrolls the
9505 * screen up on some terminals).
9506 */
9507 i = (int)STRLEN(buffer);
9508 get_rel_pos(wp, buffer + i + 1);
9509 o = i + vim_strsize(buffer + i + 1);
9510#ifdef FEAT_WINDOWS
9511 if (wp->w_status_height == 0) /* can't use last char of screen */
9512#endif
9513 ++o;
9514#ifdef FEAT_VERTSPLIT
9515 this_ru_col = ru_col - (Columns - width);
9516 if (this_ru_col < 0)
9517 this_ru_col = 0;
9518#endif
9519 /* Never use more than half the window/screen width, leave the other
9520 * half for the filename. */
9521 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9522 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9523 if (this_ru_col + o < WITH_WIDTH(width))
9524 {
9525 while (this_ru_col + o < WITH_WIDTH(width))
9526 {
9527#ifdef FEAT_MBYTE
9528 if (has_mbyte)
9529 i += (*mb_char2bytes)(fillchar, buffer + i);
9530 else
9531#endif
9532 buffer[i++] = fillchar;
9533 ++o;
9534 }
9535 get_rel_pos(wp, buffer + i);
9536 }
9537 /* Truncate at window boundary. */
9538#ifdef FEAT_MBYTE
9539 if (has_mbyte)
9540 {
9541 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009542 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 {
9544 o += (*mb_ptr2cells)(buffer + i);
9545 if (this_ru_col + o > WITH_WIDTH(width))
9546 {
9547 buffer[i] = NUL;
9548 break;
9549 }
9550 }
9551 }
9552 else
9553#endif
9554 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9555 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9556
9557 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9558 i = redraw_cmdline;
9559 screen_fill(row, row + 1,
9560 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9561 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9562 fillchar, fillchar, attr);
9563 /* don't redraw the cmdline because of showing the ruler */
9564 redraw_cmdline = i;
9565 wp->w_ru_cursor = wp->w_cursor;
9566 wp->w_ru_virtcol = wp->w_virtcol;
9567 wp->w_ru_empty = empty_line;
9568 wp->w_ru_topline = wp->w_topline;
9569 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9570#ifdef FEAT_DIFF
9571 wp->w_ru_topfill = wp->w_topfill;
9572#endif
9573 }
9574}
9575#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009576
9577#if defined(FEAT_LINEBREAK) || defined(PROTO)
9578/*
9579 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009580 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009581 * Otherwise it depends on 'numberwidth' and the line count.
9582 */
9583 int
9584number_width(wp)
9585 win_T *wp;
9586{
9587 int n;
9588 linenr_T lnum;
9589
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009590 lnum = wp->w_buffer->b_ml.ml_line_count;
9591 if (lnum == wp->w_nrwidth_line_count)
9592 return wp->w_nrwidth_width;
9593 wp->w_nrwidth_line_count = lnum;
9594
9595 n = 0;
9596 do
9597 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009598 lnum /= 10;
9599 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009600 } while (lnum > 0);
9601
9602 /* 'numberwidth' gives the minimal width plus one */
9603 if (n < wp->w_p_nuw - 1)
9604 n = wp->w_p_nuw - 1;
9605
9606 wp->w_nrwidth_width = n;
9607 return n;
9608}
9609#endif