blob: 770187bb90e00ad5cb2fed587e164772da23c314 [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;
851 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 search_hl.buf = buf;
854 search_hl.lnum = 0;
855 search_hl.first_lnum = 0;
856#endif
857
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000858#ifdef FEAT_LINEBREAK
859 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000860 i = wp->w_p_nu ? number_width(wp) : 0;
861 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000862 {
863 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000864 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000865 }
866 else
867#endif
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
870 {
871 /*
872 * When there are both inserted/deleted lines and specific lines to be
873 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
874 * everything (only happens when redrawing is off for while).
875 */
876 type = NOT_VALID;
877 }
878 else
879 {
880 /*
881 * Set mod_top to the first line that needs displaying because of
882 * changes. Set mod_bot to the first line after the changes.
883 */
884 mod_top = wp->w_redraw_top;
885 if (wp->w_redraw_bot != 0)
886 mod_bot = wp->w_redraw_bot + 1;
887 else
888 mod_bot = 0;
889 wp->w_redraw_top = 0; /* reset for next time */
890 wp->w_redraw_bot = 0;
891 if (buf->b_mod_set)
892 {
893 if (mod_top == 0 || mod_top > buf->b_mod_top)
894 {
895 mod_top = buf->b_mod_top;
896#ifdef FEAT_SYN_HL
897 /* Need to redraw lines above the change that may be included
898 * in a pattern match. */
899 if (syntax_present(buf))
900 {
901 mod_top -= buf->b_syn_sync_linebreaks;
902 if (mod_top < 1)
903 mod_top = 1;
904 }
905#endif
906 }
907 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
908 mod_bot = buf->b_mod_bot;
909
910#ifdef FEAT_SEARCH_EXTRA
911 /* When 'hlsearch' is on and using a multi-line search pattern, a
912 * change in one line may make the Search highlighting in a
913 * previous line invalid. Simple solution: redraw all visible
914 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000915 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000917 if (search_hl.rm.regprog != NULL
918 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000920 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000921 {
922 cur = wp->w_match_head;
923 while (cur != NULL)
924 {
925 if (cur->match.regprog != NULL
926 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000927 {
928 top_to_mod = TRUE;
929 break;
930 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000931 cur = cur->next;
932 }
933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#endif
935 }
936#ifdef FEAT_FOLDING
937 if (mod_top != 0 && hasAnyFolding(wp))
938 {
939 linenr_T lnumt, lnumb;
940
941 /*
942 * A change in a line can cause lines above it to become folded or
943 * unfolded. Find the top most buffer line that may be affected.
944 * If the line was previously folded and displayed, get the first
945 * line of that fold. If the line is folded now, get the first
946 * folded line. Use the minimum of these two.
947 */
948
949 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
950 * the line below it. If there is no valid entry, use w_topline.
951 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
952 * to this line. If there is no valid entry, use MAXLNUM. */
953 lnumt = wp->w_topline;
954 lnumb = MAXLNUM;
955 for (i = 0; i < wp->w_lines_valid; ++i)
956 if (wp->w_lines[i].wl_valid)
957 {
958 if (wp->w_lines[i].wl_lastlnum < mod_top)
959 lnumt = wp->w_lines[i].wl_lastlnum + 1;
960 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
961 {
962 lnumb = wp->w_lines[i].wl_lnum;
963 /* When there is a fold column it might need updating
964 * in the next line ("J" just above an open fold). */
965 if (wp->w_p_fdc > 0)
966 ++lnumb;
967 }
968 }
969
970 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
971 if (mod_top > lnumt)
972 mod_top = lnumt;
973
974 /* Now do the same for the bottom line (one above mod_bot). */
975 --mod_bot;
976 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
977 ++mod_bot;
978 if (mod_bot < lnumb)
979 mod_bot = lnumb;
980 }
981#endif
982
983 /* When a change starts above w_topline and the end is below
984 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000985 * If the end of the change is above w_topline: do like no change was
986 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (mod_top != 0 && mod_top < wp->w_topline)
988 {
989 if (mod_bot > wp->w_topline)
990 mod_top = wp->w_topline;
991#ifdef FEAT_SYN_HL
992 else if (syntax_present(buf))
993 top_end = 1;
994#endif
995 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000996
997 /* When line numbers are displayed need to redraw all lines below
998 * inserted/deleted lines. */
999 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1000 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002
1003 /*
1004 * When only displaying the lines at the top, set top_end. Used when
1005 * window has scrolled down for msg_scrolled.
1006 */
1007 if (type == REDRAW_TOP)
1008 {
1009 j = 0;
1010 for (i = 0; i < wp->w_lines_valid; ++i)
1011 {
1012 j += wp->w_lines[i].wl_size;
1013 if (j >= wp->w_upd_rows)
1014 {
1015 top_end = j;
1016 break;
1017 }
1018 }
1019 if (top_end == 0)
1020 /* not found (cannot happen?): redraw everything */
1021 type = NOT_VALID;
1022 else
1023 /* top area defined, the rest is VALID */
1024 type = VALID;
1025 }
1026
Bram Moolenaar367329b2007-08-30 11:53:22 +00001027 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001028 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1029 * non-zero and thus not FALSE) will indicate that screenclear() was not
1030 * called. */
1031 if (screen_cleared)
1032 screen_cleared = MAYBE;
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 /*
1035 * If there are no changes on the screen that require a complete redraw,
1036 * handle three cases:
1037 * 1: we are off the top of the screen by a few lines: scroll down
1038 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1039 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1040 * w_lines[] that needs updating.
1041 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001042 if ((type == VALID || type == SOME_VALID
1043 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044#ifdef FEAT_DIFF
1045 && !wp->w_botfill && !wp->w_old_botfill
1046#endif
1047 )
1048 {
1049 if (mod_top != 0 && wp->w_topline == mod_top)
1050 {
1051 /*
1052 * w_topline is the first changed line, the scrolling will be done
1053 * further down.
1054 */
1055 }
1056 else if (wp->w_lines[0].wl_valid
1057 && (wp->w_topline < wp->w_lines[0].wl_lnum
1058#ifdef FEAT_DIFF
1059 || (wp->w_topline == wp->w_lines[0].wl_lnum
1060 && wp->w_topfill > wp->w_old_topfill)
1061#endif
1062 ))
1063 {
1064 /*
1065 * New topline is above old topline: May scroll down.
1066 */
1067#ifdef FEAT_FOLDING
1068 if (hasAnyFolding(wp))
1069 {
1070 linenr_T ln;
1071
1072 /* count the number of lines we are off, counting a sequence
1073 * of folded lines as one */
1074 j = 0;
1075 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1076 {
1077 ++j;
1078 if (j >= wp->w_height - 2)
1079 break;
1080 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1081 }
1082 }
1083 else
1084#endif
1085 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1086 if (j < wp->w_height - 2) /* not too far off */
1087 {
1088 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1089#ifdef FEAT_DIFF
1090 /* insert extra lines for previously invisible filler lines */
1091 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1092 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1093 - wp->w_old_topfill;
1094#endif
1095 if (i < wp->w_height - 2) /* less than a screen off */
1096 {
1097 /*
1098 * Try to insert the correct number of lines.
1099 * If not the last window, delete the lines at the bottom.
1100 * win_ins_lines may fail when the terminal can't do it.
1101 */
1102 if (i > 0)
1103 check_for_delay(FALSE);
1104 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1105 {
1106 if (wp->w_lines_valid != 0)
1107 {
1108 /* Need to update rows that are new, stop at the
1109 * first one that scrolled down. */
1110 top_end = i;
1111#ifdef FEAT_VISUAL
1112 scrolled_down = TRUE;
1113#endif
1114
1115 /* Move the entries that were scrolled, disable
1116 * the entries for the lines to be redrawn. */
1117 if ((wp->w_lines_valid += j) > wp->w_height)
1118 wp->w_lines_valid = wp->w_height;
1119 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1120 wp->w_lines[idx] = wp->w_lines[idx - j];
1121 while (idx >= 0)
1122 wp->w_lines[idx--].wl_valid = FALSE;
1123 }
1124 }
1125 else
1126 mid_start = 0; /* redraw all lines */
1127 }
1128 else
1129 mid_start = 0; /* redraw all lines */
1130 }
1131 else
1132 mid_start = 0; /* redraw all lines */
1133 }
1134 else
1135 {
1136 /*
1137 * New topline is at or below old topline: May scroll up.
1138 * When topline didn't change, find first entry in w_lines[] that
1139 * needs updating.
1140 */
1141
1142 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1143 j = -1;
1144 row = 0;
1145 for (i = 0; i < wp->w_lines_valid; i++)
1146 {
1147 if (wp->w_lines[i].wl_valid
1148 && wp->w_lines[i].wl_lnum == wp->w_topline)
1149 {
1150 j = i;
1151 break;
1152 }
1153 row += wp->w_lines[i].wl_size;
1154 }
1155 if (j == -1)
1156 {
1157 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1158 * lines */
1159 mid_start = 0;
1160 }
1161 else
1162 {
1163 /*
1164 * Try to delete the correct number of lines.
1165 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1166 */
1167#ifdef FEAT_DIFF
1168 /* If the topline didn't change, delete old filler lines,
1169 * otherwise delete filler lines of the new topline... */
1170 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1171 row += wp->w_old_topfill;
1172 else
1173 row += diff_check_fill(wp, wp->w_topline);
1174 /* ... but don't delete new filler lines. */
1175 row -= wp->w_topfill;
1176#endif
1177 if (row > 0)
1178 {
1179 check_for_delay(FALSE);
1180 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1181 bot_start = wp->w_height - row;
1182 else
1183 mid_start = 0; /* redraw all lines */
1184 }
1185 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1186 {
1187 /*
1188 * Skip the lines (below the deleted lines) that are still
1189 * valid and don't need redrawing. Copy their info
1190 * upwards, to compensate for the deleted lines. Set
1191 * bot_start to the first row that needs redrawing.
1192 */
1193 bot_start = 0;
1194 idx = 0;
1195 for (;;)
1196 {
1197 wp->w_lines[idx] = wp->w_lines[j];
1198 /* stop at line that didn't fit, unless it is still
1199 * valid (no lines deleted) */
1200 if (row > 0 && bot_start + row
1201 + (int)wp->w_lines[j].wl_size > wp->w_height)
1202 {
1203 wp->w_lines_valid = idx + 1;
1204 break;
1205 }
1206 bot_start += wp->w_lines[idx++].wl_size;
1207
1208 /* stop at the last valid entry in w_lines[].wl_size */
1209 if (++j >= wp->w_lines_valid)
1210 {
1211 wp->w_lines_valid = idx;
1212 break;
1213 }
1214 }
1215#ifdef FEAT_DIFF
1216 /* Correct the first entry for filler lines at the top
1217 * when it won't get updated below. */
1218 if (wp->w_p_diff && bot_start > 0)
1219 wp->w_lines[0].wl_size =
1220 plines_win_nofill(wp, wp->w_topline, TRUE)
1221 + wp->w_topfill;
1222#endif
1223 }
1224 }
1225 }
1226
1227 /* When starting redraw in the first line, redraw all lines. When
1228 * there is only one window it's probably faster to clear the screen
1229 * first. */
1230 if (mid_start == 0)
1231 {
1232 mid_end = wp->w_height;
1233 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001234 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001235 /* Clear the screen when it was not done by win_del_lines() or
1236 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1237 * then. */
1238 if (screen_cleared != TRUE)
1239 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001240#ifdef FEAT_WINDOWS
1241 /* The screen was cleared, redraw the tab pages line. */
1242 if (redraw_tabline)
1243 draw_tabline();
1244#endif
1245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001247
1248 /* When win_del_lines() or win_ins_lines() caused the screen to be
1249 * cleared (only happens for the first window) or when screenclear()
1250 * was called directly above, "must_redraw" will have been set to
1251 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1252 if (screen_cleared == TRUE)
1253 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 }
1255 else
1256 {
1257 /* Not VALID or INVERTED: redraw all lines. */
1258 mid_start = 0;
1259 mid_end = wp->w_height;
1260 }
1261
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001262 if (type == SOME_VALID)
1263 {
1264 /* SOME_VALID: redraw all lines. */
1265 mid_start = 0;
1266 mid_end = wp->w_height;
1267 type = NOT_VALID;
1268 }
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270#ifdef FEAT_VISUAL
1271 /* check if we are updating or removing the inverted part */
1272 if ((VIsual_active && buf == curwin->w_buffer)
1273 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1274 {
1275 linenr_T from, to;
1276
1277 if (VIsual_active)
1278 {
1279 if (VIsual_active
1280 && (VIsual_mode != wp->w_old_visual_mode
1281 || type == INVERTED_ALL))
1282 {
1283 /*
1284 * If the type of Visual selection changed, redraw the whole
1285 * selection. Also when the ownership of the X selection is
1286 * gained or lost.
1287 */
1288 if (curwin->w_cursor.lnum < VIsual.lnum)
1289 {
1290 from = curwin->w_cursor.lnum;
1291 to = VIsual.lnum;
1292 }
1293 else
1294 {
1295 from = VIsual.lnum;
1296 to = curwin->w_cursor.lnum;
1297 }
1298 /* redraw more when the cursor moved as well */
1299 if (wp->w_old_cursor_lnum < from)
1300 from = wp->w_old_cursor_lnum;
1301 if (wp->w_old_cursor_lnum > to)
1302 to = wp->w_old_cursor_lnum;
1303 if (wp->w_old_visual_lnum < from)
1304 from = wp->w_old_visual_lnum;
1305 if (wp->w_old_visual_lnum > to)
1306 to = wp->w_old_visual_lnum;
1307 }
1308 else
1309 {
1310 /*
1311 * Find the line numbers that need to be updated: The lines
1312 * between the old cursor position and the current cursor
1313 * position. Also check if the Visual position changed.
1314 */
1315 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1316 {
1317 from = curwin->w_cursor.lnum;
1318 to = wp->w_old_cursor_lnum;
1319 }
1320 else
1321 {
1322 from = wp->w_old_cursor_lnum;
1323 to = curwin->w_cursor.lnum;
1324 if (from == 0) /* Visual mode just started */
1325 from = to;
1326 }
1327
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001328 if (VIsual.lnum != wp->w_old_visual_lnum
1329 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 {
1331 if (wp->w_old_visual_lnum < from
1332 && wp->w_old_visual_lnum != 0)
1333 from = wp->w_old_visual_lnum;
1334 if (wp->w_old_visual_lnum > to)
1335 to = wp->w_old_visual_lnum;
1336 if (VIsual.lnum < from)
1337 from = VIsual.lnum;
1338 if (VIsual.lnum > to)
1339 to = VIsual.lnum;
1340 }
1341 }
1342
1343 /*
1344 * If in block mode and changed column or curwin->w_curswant:
1345 * update all lines.
1346 * First compute the actual start and end column.
1347 */
1348 if (VIsual_mode == Ctrl_V)
1349 {
1350 colnr_T fromc, toc;
1351
1352 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1353 ++toc;
1354 if (curwin->w_curswant == MAXCOL)
1355 toc = MAXCOL;
1356
1357 if (fromc != wp->w_old_cursor_fcol
1358 || toc != wp->w_old_cursor_lcol)
1359 {
1360 if (from > VIsual.lnum)
1361 from = VIsual.lnum;
1362 if (to < VIsual.lnum)
1363 to = VIsual.lnum;
1364 }
1365 wp->w_old_cursor_fcol = fromc;
1366 wp->w_old_cursor_lcol = toc;
1367 }
1368 }
1369 else
1370 {
1371 /* Use the line numbers of the old Visual area. */
1372 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1373 {
1374 from = wp->w_old_cursor_lnum;
1375 to = wp->w_old_visual_lnum;
1376 }
1377 else
1378 {
1379 from = wp->w_old_visual_lnum;
1380 to = wp->w_old_cursor_lnum;
1381 }
1382 }
1383
1384 /*
1385 * There is no need to update lines above the top of the window.
1386 */
1387 if (from < wp->w_topline)
1388 from = wp->w_topline;
1389
1390 /*
1391 * If we know the value of w_botline, use it to restrict the update to
1392 * the lines that are visible in the window.
1393 */
1394 if (wp->w_valid & VALID_BOTLINE)
1395 {
1396 if (from >= wp->w_botline)
1397 from = wp->w_botline - 1;
1398 if (to >= wp->w_botline)
1399 to = wp->w_botline - 1;
1400 }
1401
1402 /*
1403 * Find the minimal part to be updated.
1404 * Watch out for scrolling that made entries in w_lines[] invalid.
1405 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1406 * top_end; need to redraw from top_end to the "to" line.
1407 * A middle mouse click with a Visual selection may change the text
1408 * above the Visual area and reset wl_valid, do count these for
1409 * mid_end (in srow).
1410 */
1411 if (mid_start > 0)
1412 {
1413 lnum = wp->w_topline;
1414 idx = 0;
1415 srow = 0;
1416 if (scrolled_down)
1417 mid_start = top_end;
1418 else
1419 mid_start = 0;
1420 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1421 {
1422 if (wp->w_lines[idx].wl_valid)
1423 mid_start += wp->w_lines[idx].wl_size;
1424 else if (!scrolled_down)
1425 srow += wp->w_lines[idx].wl_size;
1426 ++idx;
1427# ifdef FEAT_FOLDING
1428 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1429 lnum = wp->w_lines[idx].wl_lnum;
1430 else
1431# endif
1432 ++lnum;
1433 }
1434 srow += mid_start;
1435 mid_end = wp->w_height;
1436 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1437 {
1438 if (wp->w_lines[idx].wl_valid
1439 && wp->w_lines[idx].wl_lnum >= to + 1)
1440 {
1441 /* Only update until first row of this line */
1442 mid_end = srow;
1443 break;
1444 }
1445 srow += wp->w_lines[idx].wl_size;
1446 }
1447 }
1448 }
1449
1450 if (VIsual_active && buf == curwin->w_buffer)
1451 {
1452 wp->w_old_visual_mode = VIsual_mode;
1453 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1454 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001455 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 wp->w_old_curswant = curwin->w_curswant;
1457 }
1458 else
1459 {
1460 wp->w_old_visual_mode = 0;
1461 wp->w_old_cursor_lnum = 0;
1462 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001463 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465#endif /* FEAT_VISUAL */
1466
1467#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1468 /* reset got_int, otherwise regexp won't work */
1469 save_got_int = got_int;
1470 got_int = 0;
1471#endif
1472#ifdef FEAT_FOLDING
1473 win_foldinfo.fi_level = 0;
1474#endif
1475
1476 /*
1477 * Update all the window rows.
1478 */
1479 idx = 0; /* first entry in w_lines[].wl_size */
1480 row = 0;
1481 srow = 0;
1482 lnum = wp->w_topline; /* first line shown in window */
1483 for (;;)
1484 {
1485 /* stop updating when reached the end of the window (check for _past_
1486 * the end of the window is at the end of the loop) */
1487 if (row == wp->w_height)
1488 {
1489 didline = TRUE;
1490 break;
1491 }
1492
1493 /* stop updating when hit the end of the file */
1494 if (lnum > buf->b_ml.ml_line_count)
1495 {
1496 eof = TRUE;
1497 break;
1498 }
1499
1500 /* Remember the starting row of the line that is going to be dealt
1501 * with. It is used further down when the line doesn't fit. */
1502 srow = row;
1503
1504 /*
1505 * Update a line when it is in an area that needs updating, when it
1506 * has changes or w_lines[idx] is invalid.
1507 * bot_start may be halfway a wrapped line after using
1508 * win_del_lines(), check if the current line includes it.
1509 * When syntax folding is being used, the saved syntax states will
1510 * already have been updated, we can't see where the syntax state is
1511 * the same again, just update until the end of the window.
1512 */
1513 if (row < top_end
1514 || (row >= mid_start && row < mid_end)
1515#ifdef FEAT_SEARCH_EXTRA
1516 || top_to_mod
1517#endif
1518 || idx >= wp->w_lines_valid
1519 || (row + wp->w_lines[idx].wl_size > bot_start)
1520 || (mod_top != 0
1521 && (lnum == mod_top
1522 || (lnum >= mod_top
1523 && (lnum < mod_bot
1524#ifdef FEAT_SYN_HL
1525 || did_update == DID_FOLD
1526 || (did_update == DID_LINE
1527 && syntax_present(buf)
1528 && (
1529# ifdef FEAT_FOLDING
1530 (foldmethodIsSyntax(wp)
1531 && hasAnyFolding(wp)) ||
1532# endif
1533 syntax_check_changed(lnum)))
1534#endif
1535 )))))
1536 {
1537#ifdef FEAT_SEARCH_EXTRA
1538 if (lnum == mod_top)
1539 top_to_mod = FALSE;
1540#endif
1541
1542 /*
1543 * When at start of changed lines: May scroll following lines
1544 * up or down to minimize redrawing.
1545 * Don't do this when the change continues until the end.
1546 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1547 */
1548 if (lnum == mod_top
1549 && mod_bot != MAXLNUM
1550 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1551 {
1552 int old_rows = 0;
1553 int new_rows = 0;
1554 int xtra_rows;
1555 linenr_T l;
1556
1557 /* Count the old number of window rows, using w_lines[], which
1558 * should still contain the sizes for the lines as they are
1559 * currently displayed. */
1560 for (i = idx; i < wp->w_lines_valid; ++i)
1561 {
1562 /* Only valid lines have a meaningful wl_lnum. Invalid
1563 * lines are part of the changed area. */
1564 if (wp->w_lines[i].wl_valid
1565 && wp->w_lines[i].wl_lnum == mod_bot)
1566 break;
1567 old_rows += wp->w_lines[i].wl_size;
1568#ifdef FEAT_FOLDING
1569 if (wp->w_lines[i].wl_valid
1570 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1571 {
1572 /* Must have found the last valid entry above mod_bot.
1573 * Add following invalid entries. */
1574 ++i;
1575 while (i < wp->w_lines_valid
1576 && !wp->w_lines[i].wl_valid)
1577 old_rows += wp->w_lines[i++].wl_size;
1578 break;
1579 }
1580#endif
1581 }
1582
1583 if (i >= wp->w_lines_valid)
1584 {
1585 /* We can't find a valid line below the changed lines,
1586 * need to redraw until the end of the window.
1587 * Inserting/deleting lines has no use. */
1588 bot_start = 0;
1589 }
1590 else
1591 {
1592 /* Able to count old number of rows: Count new window
1593 * rows, and may insert/delete lines */
1594 j = idx;
1595 for (l = lnum; l < mod_bot; ++l)
1596 {
1597#ifdef FEAT_FOLDING
1598 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1599 ++new_rows;
1600 else
1601#endif
1602#ifdef FEAT_DIFF
1603 if (l == wp->w_topline)
1604 new_rows += plines_win_nofill(wp, l, TRUE)
1605 + wp->w_topfill;
1606 else
1607#endif
1608 new_rows += plines_win(wp, l, TRUE);
1609 ++j;
1610 if (new_rows > wp->w_height - row - 2)
1611 {
1612 /* it's getting too much, must redraw the rest */
1613 new_rows = 9999;
1614 break;
1615 }
1616 }
1617 xtra_rows = new_rows - old_rows;
1618 if (xtra_rows < 0)
1619 {
1620 /* May scroll text up. If there is not enough
1621 * remaining text or scrolling fails, must redraw the
1622 * rest. If scrolling works, must redraw the text
1623 * below the scrolled text. */
1624 if (row - xtra_rows >= wp->w_height - 2)
1625 mod_bot = MAXLNUM;
1626 else
1627 {
1628 check_for_delay(FALSE);
1629 if (win_del_lines(wp, row,
1630 -xtra_rows, FALSE, FALSE) == FAIL)
1631 mod_bot = MAXLNUM;
1632 else
1633 bot_start = wp->w_height + xtra_rows;
1634 }
1635 }
1636 else if (xtra_rows > 0)
1637 {
1638 /* May scroll text down. If there is not enough
1639 * remaining text of scrolling fails, must redraw the
1640 * rest. */
1641 if (row + xtra_rows >= wp->w_height - 2)
1642 mod_bot = MAXLNUM;
1643 else
1644 {
1645 check_for_delay(FALSE);
1646 if (win_ins_lines(wp, row + old_rows,
1647 xtra_rows, FALSE, FALSE) == FAIL)
1648 mod_bot = MAXLNUM;
1649 else if (top_end > row + old_rows)
1650 /* Scrolled the part at the top that requires
1651 * updating down. */
1652 top_end += xtra_rows;
1653 }
1654 }
1655
1656 /* When not updating the rest, may need to move w_lines[]
1657 * entries. */
1658 if (mod_bot != MAXLNUM && i != j)
1659 {
1660 if (j < i)
1661 {
1662 int x = row + new_rows;
1663
1664 /* move entries in w_lines[] upwards */
1665 for (;;)
1666 {
1667 /* stop at last valid entry in w_lines[] */
1668 if (i >= wp->w_lines_valid)
1669 {
1670 wp->w_lines_valid = j;
1671 break;
1672 }
1673 wp->w_lines[j] = wp->w_lines[i];
1674 /* stop at a line that won't fit */
1675 if (x + (int)wp->w_lines[j].wl_size
1676 > wp->w_height)
1677 {
1678 wp->w_lines_valid = j + 1;
1679 break;
1680 }
1681 x += wp->w_lines[j++].wl_size;
1682 ++i;
1683 }
1684 if (bot_start > x)
1685 bot_start = x;
1686 }
1687 else /* j > i */
1688 {
1689 /* move entries in w_lines[] downwards */
1690 j -= i;
1691 wp->w_lines_valid += j;
1692 if (wp->w_lines_valid > wp->w_height)
1693 wp->w_lines_valid = wp->w_height;
1694 for (i = wp->w_lines_valid; i - j >= idx; --i)
1695 wp->w_lines[i] = wp->w_lines[i - j];
1696
1697 /* The w_lines[] entries for inserted lines are
1698 * now invalid, but wl_size may be used above.
1699 * Reset to zero. */
1700 while (i >= idx)
1701 {
1702 wp->w_lines[i].wl_size = 0;
1703 wp->w_lines[i--].wl_valid = FALSE;
1704 }
1705 }
1706 }
1707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
1709
1710#ifdef FEAT_FOLDING
1711 /*
1712 * When lines are folded, display one line for all of them.
1713 * Otherwise, display normally (can be several display lines when
1714 * 'wrap' is on).
1715 */
1716 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1717 if (fold_count != 0)
1718 {
1719 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1720 ++row;
1721 --fold_count;
1722 wp->w_lines[idx].wl_folded = TRUE;
1723 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1724# ifdef FEAT_SYN_HL
1725 did_update = DID_FOLD;
1726# endif
1727 }
1728 else
1729#endif
1730 if (idx < wp->w_lines_valid
1731 && wp->w_lines[idx].wl_valid
1732 && wp->w_lines[idx].wl_lnum == lnum
1733 && lnum > wp->w_topline
1734 && !(dy_flags & DY_LASTLINE)
1735 && srow + wp->w_lines[idx].wl_size > wp->w_height
1736#ifdef FEAT_DIFF
1737 && diff_check_fill(wp, lnum) == 0
1738#endif
1739 )
1740 {
1741 /* This line is not going to fit. Don't draw anything here,
1742 * will draw "@ " lines below. */
1743 row = wp->w_height + 1;
1744 }
1745 else
1746 {
1747#ifdef FEAT_SEARCH_EXTRA
1748 prepare_search_hl(wp, lnum);
1749#endif
1750#ifdef FEAT_SYN_HL
1751 /* Let the syntax stuff know we skipped a few lines. */
1752 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1753 && syntax_present(buf))
1754 syntax_end_parsing(syntax_last_parsed + 1);
1755#endif
1756
1757 /*
1758 * Display one line.
1759 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001760 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762#ifdef FEAT_FOLDING
1763 wp->w_lines[idx].wl_folded = FALSE;
1764 wp->w_lines[idx].wl_lastlnum = lnum;
1765#endif
1766#ifdef FEAT_SYN_HL
1767 did_update = DID_LINE;
1768 syntax_last_parsed = lnum;
1769#endif
1770 }
1771
1772 wp->w_lines[idx].wl_lnum = lnum;
1773 wp->w_lines[idx].wl_valid = TRUE;
1774 if (row > wp->w_height) /* past end of screen */
1775 {
1776 /* we may need the size of that too long line later on */
1777 if (dollar_vcol == 0)
1778 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1779 ++idx;
1780 break;
1781 }
1782 if (dollar_vcol == 0)
1783 wp->w_lines[idx].wl_size = row - srow;
1784 ++idx;
1785#ifdef FEAT_FOLDING
1786 lnum += fold_count + 1;
1787#else
1788 ++lnum;
1789#endif
1790 }
1791 else
1792 {
1793 /* This line does not need updating, advance to the next one */
1794 row += wp->w_lines[idx++].wl_size;
1795 if (row > wp->w_height) /* past end of screen */
1796 break;
1797#ifdef FEAT_FOLDING
1798 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1799#else
1800 ++lnum;
1801#endif
1802#ifdef FEAT_SYN_HL
1803 did_update = DID_NONE;
1804#endif
1805 }
1806
1807 if (lnum > buf->b_ml.ml_line_count)
1808 {
1809 eof = TRUE;
1810 break;
1811 }
1812 }
1813 /*
1814 * End of loop over all window lines.
1815 */
1816
1817
1818 if (idx > wp->w_lines_valid)
1819 wp->w_lines_valid = idx;
1820
1821#ifdef FEAT_SYN_HL
1822 /*
1823 * Let the syntax stuff know we stop parsing here.
1824 */
1825 if (syntax_last_parsed != 0 && syntax_present(buf))
1826 syntax_end_parsing(syntax_last_parsed + 1);
1827#endif
1828
1829 /*
1830 * If we didn't hit the end of the file, and we didn't finish the last
1831 * line we were working on, then the line didn't fit.
1832 */
1833 wp->w_empty_rows = 0;
1834#ifdef FEAT_DIFF
1835 wp->w_filler_rows = 0;
1836#endif
1837 if (!eof && !didline)
1838 {
1839 if (lnum == wp->w_topline)
1840 {
1841 /*
1842 * Single line that does not fit!
1843 * Don't overwrite it, it can be edited.
1844 */
1845 wp->w_botline = lnum + 1;
1846 }
1847#ifdef FEAT_DIFF
1848 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1849 {
1850 /* Window ends in filler lines. */
1851 wp->w_botline = lnum;
1852 wp->w_filler_rows = wp->w_height - srow;
1853 }
1854#endif
1855 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1856 {
1857 /*
1858 * Last line isn't finished: Display "@@@" at the end.
1859 */
1860 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1861 W_WINROW(wp) + wp->w_height,
1862 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1863 '@', '@', hl_attr(HLF_AT));
1864 set_empty_rows(wp, srow);
1865 wp->w_botline = lnum;
1866 }
1867 else
1868 {
1869 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1870 wp->w_botline = lnum;
1871 }
1872 }
1873 else
1874 {
1875#ifdef FEAT_VERTSPLIT
1876 draw_vsep_win(wp, row);
1877#endif
1878 if (eof) /* we hit the end of the file */
1879 {
1880 wp->w_botline = buf->b_ml.ml_line_count + 1;
1881#ifdef FEAT_DIFF
1882 j = diff_check_fill(wp, wp->w_botline);
1883 if (j > 0 && !wp->w_botfill)
1884 {
1885 /*
1886 * Display filler lines at the end of the file
1887 */
1888 if (char2cells(fill_diff) > 1)
1889 i = '-';
1890 else
1891 i = fill_diff;
1892 if (row + j > wp->w_height)
1893 j = wp->w_height - row;
1894 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1895 row += j;
1896 }
1897#endif
1898 }
1899 else if (dollar_vcol == 0)
1900 wp->w_botline = lnum;
1901
1902 /* make sure the rest of the screen is blank */
1903 /* put '~'s on rows that aren't part of the file. */
1904 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1905 }
1906
1907 /* Reset the type of redrawing required, the window has been updated. */
1908 wp->w_redr_type = 0;
1909#ifdef FEAT_DIFF
1910 wp->w_old_topfill = wp->w_topfill;
1911 wp->w_old_botfill = wp->w_botfill;
1912#endif
1913
1914 if (dollar_vcol == 0)
1915 {
1916 /*
1917 * There is a trick with w_botline. If we invalidate it on each
1918 * change that might modify it, this will cause a lot of expensive
1919 * calls to plines() in update_topline() each time. Therefore the
1920 * value of w_botline is often approximated, and this value is used to
1921 * compute the value of w_topline. If the value of w_botline was
1922 * wrong, check that the value of w_topline is correct (cursor is on
1923 * the visible part of the text). If it's not, we need to redraw
1924 * again. Mostly this just means scrolling up a few lines, so it
1925 * doesn't look too bad. Only do this for the current window (where
1926 * changes are relevant).
1927 */
1928 wp->w_valid |= VALID_BOTLINE;
1929 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1930 {
1931 recursive = TRUE;
1932 curwin->w_valid &= ~VALID_TOPLINE;
1933 update_topline(); /* may invalidate w_botline again */
1934 if (must_redraw != 0)
1935 {
1936 /* Don't update for changes in buffer again. */
1937 i = curbuf->b_mod_set;
1938 curbuf->b_mod_set = FALSE;
1939 win_update(curwin);
1940 must_redraw = 0;
1941 curbuf->b_mod_set = i;
1942 }
1943 recursive = FALSE;
1944 }
1945 }
1946
1947#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1948 /* restore got_int, unless CTRL-C was hit while redrawing */
1949 if (!got_int)
1950 got_int = save_got_int;
1951#endif
1952}
1953
1954#ifdef FEAT_SIGNS
1955static int draw_signcolumn __ARGS((win_T *wp));
1956
1957/*
1958 * Return TRUE when window "wp" has a column to draw signs in.
1959 */
1960 static int
1961draw_signcolumn(wp)
1962 win_T *wp;
1963{
1964 return (wp->w_buffer->b_signlist != NULL
1965# ifdef FEAT_NETBEANS_INTG
1966 || usingNetbeans
1967# endif
1968 );
1969}
1970#endif
1971
1972/*
1973 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1974 * as the filler character.
1975 */
1976 static void
1977win_draw_end(wp, c1, c2, row, endrow, hl)
1978 win_T *wp;
1979 int c1;
1980 int c2;
1981 int row;
1982 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001983 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
1985#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1986 int n = 0;
1987# define FDC_OFF n
1988#else
1989# define FDC_OFF 0
1990#endif
1991
1992#ifdef FEAT_RIGHTLEFT
1993 if (wp->w_p_rl)
1994 {
1995 /* No check for cmdline window: should never be right-left. */
1996# ifdef FEAT_FOLDING
1997 n = wp->w_p_fdc;
1998
1999 if (n > 0)
2000 {
2001 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002002 if (n > W_WIDTH(wp))
2003 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2005 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2006 ' ', ' ', hl_attr(HLF_FC));
2007 }
2008# endif
2009# ifdef FEAT_SIGNS
2010 if (draw_signcolumn(wp))
2011 {
2012 int nn = n + 2;
2013
2014 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002015 if (nn > W_WIDTH(wp))
2016 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2018 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2019 ' ', ' ', hl_attr(HLF_SC));
2020 n = nn;
2021 }
2022# endif
2023 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2024 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2025 c2, c2, hl_attr(hl));
2026 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2027 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2028 c1, c2, hl_attr(hl));
2029 }
2030 else
2031#endif
2032 {
2033#ifdef FEAT_CMDWIN
2034 if (cmdwin_type != 0 && wp == curwin)
2035 {
2036 /* draw the cmdline character in the leftmost column */
2037 n = 1;
2038 if (n > wp->w_width)
2039 n = wp->w_width;
2040 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2041 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2042 cmdwin_type, ' ', hl_attr(HLF_AT));
2043 }
2044#endif
2045#ifdef FEAT_FOLDING
2046 if (wp->w_p_fdc > 0)
2047 {
2048 int nn = n + wp->w_p_fdc;
2049
2050 /* draw the fold column at the left */
2051 if (nn > W_WIDTH(wp))
2052 nn = W_WIDTH(wp);
2053 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2054 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2055 ' ', ' ', hl_attr(HLF_FC));
2056 n = nn;
2057 }
2058#endif
2059#ifdef FEAT_SIGNS
2060 if (draw_signcolumn(wp))
2061 {
2062 int nn = n + 2;
2063
2064 /* draw the sign column after the fold column */
2065 if (nn > W_WIDTH(wp))
2066 nn = W_WIDTH(wp);
2067 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2068 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2069 ' ', ' ', hl_attr(HLF_SC));
2070 n = nn;
2071 }
2072#endif
2073 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2074 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2075 c1, c2, hl_attr(hl));
2076 }
2077 set_empty_rows(wp, row);
2078}
2079
2080#ifdef FEAT_FOLDING
2081/*
2082 * Display one folded line.
2083 */
2084 static void
2085fold_line(wp, fold_count, foldinfo, lnum, row)
2086 win_T *wp;
2087 long fold_count;
2088 foldinfo_T *foldinfo;
2089 linenr_T lnum;
2090 int row;
2091{
2092 char_u buf[51];
2093 pos_T *top, *bot;
2094 linenr_T lnume = lnum + fold_count - 1;
2095 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002096 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 int col;
2099 int txtcol;
2100 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002101 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
2103 /* Build the fold line:
2104 * 1. Add the cmdwin_type for the command-line window
2105 * 2. Add the 'foldcolumn'
2106 * 3. Add the 'number' column
2107 * 4. Compose the text
2108 * 5. Add the text
2109 * 6. set highlighting for the Visual area an other text
2110 */
2111 col = 0;
2112
2113 /*
2114 * 1. Add the cmdwin_type for the command-line window
2115 * Ignores 'rightleft', this window is never right-left.
2116 */
2117#ifdef FEAT_CMDWIN
2118 if (cmdwin_type != 0 && wp == curwin)
2119 {
2120 ScreenLines[off] = cmdwin_type;
2121 ScreenAttrs[off] = hl_attr(HLF_AT);
2122#ifdef FEAT_MBYTE
2123 if (enc_utf8)
2124 ScreenLinesUC[off] = 0;
2125#endif
2126 ++col;
2127 }
2128#endif
2129
2130 /*
2131 * 2. Add the 'foldcolumn'
2132 */
2133 fdc = wp->w_p_fdc;
2134 if (fdc > W_WIDTH(wp) - col)
2135 fdc = W_WIDTH(wp) - col;
2136 if (fdc > 0)
2137 {
2138 fill_foldcolumn(buf, wp, TRUE, lnum);
2139#ifdef FEAT_RIGHTLEFT
2140 if (wp->w_p_rl)
2141 {
2142 int i;
2143
2144 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2145 hl_attr(HLF_FC));
2146 /* reverse the fold column */
2147 for (i = 0; i < fdc; ++i)
2148 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2149 }
2150 else
2151#endif
2152 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2153 col += fdc;
2154 }
2155
2156#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002157# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2158 for (ri = 0; ri < l; ++ri) \
2159 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2160 else \
2161 for (ri = 0; ri < l; ++ri) \
2162 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002164# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2165 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166#endif
2167
2168 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002169 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171#ifdef FEAT_SIGNS
2172 /* If signs are being displayed, add two spaces. */
2173 if (draw_signcolumn(wp))
2174 {
2175 len = W_WIDTH(wp) - col;
2176 if (len > 0)
2177 {
2178 if (len > 2)
2179 len = 2;
2180# ifdef FEAT_RIGHTLEFT
2181 if (wp->w_p_rl)
2182 /* the line number isn't reversed */
2183 copy_text_attr(off + W_WIDTH(wp) - len - col,
2184 (char_u *)" ", len, hl_attr(HLF_FL));
2185 else
2186# endif
2187 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2188 col += len;
2189 }
2190 }
2191#endif
2192
2193 /*
2194 * 3. Add the 'number' column
2195 */
2196 if (wp->w_p_nu)
2197 {
2198 len = W_WIDTH(wp) - col;
2199 if (len > 0)
2200 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002201 int w = number_width(wp);
2202
2203 if (len > w + 1)
2204 len = w + 1;
2205 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#ifdef FEAT_RIGHTLEFT
2207 if (wp->w_p_rl)
2208 /* the line number isn't reversed */
2209 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2210 hl_attr(HLF_FL));
2211 else
2212#endif
2213 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2214 col += len;
2215 }
2216 }
2217
2218 /*
2219 * 4. Compose the folded-line string with 'foldtext', if set.
2220 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002221 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 txtcol = col; /* remember where text starts */
2224
2225 /*
2226 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2227 * Right-left text is put in columns 0 - number-col, normal text is put
2228 * in columns number-col - window-width.
2229 */
2230#ifdef FEAT_MBYTE
2231 if (has_mbyte)
2232 {
2233 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002234 int u8c, u8cc[MAX_MCO];
2235 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 int idx;
2237 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002238 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239# ifdef FEAT_ARABIC
2240 int prev_c = 0; /* previous Arabic character */
2241 int prev_c1 = 0; /* first composing char for prev_c */
2242# endif
2243
2244# ifdef FEAT_RIGHTLEFT
2245 if (wp->w_p_rl)
2246 idx = off;
2247 else
2248# endif
2249 idx = off + col;
2250
2251 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2252 for (p = text; *p != NUL; )
2253 {
2254 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002255 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (col + cells > W_WIDTH(wp)
2257# ifdef FEAT_RIGHTLEFT
2258 - (wp->w_p_rl ? col : 0)
2259# endif
2260 )
2261 break;
2262 ScreenLines[idx] = *p;
2263 if (enc_utf8)
2264 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002265 u8c = utfc_ptr2char(p, u8cc);
2266 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
2268 ScreenLinesUC[idx] = 0;
2269#ifdef FEAT_ARABIC
2270 prev_c = u8c;
2271#endif
2272 }
2273 else
2274 {
2275#ifdef FEAT_ARABIC
2276 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2277 {
2278 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002279 int pc, pc1, nc;
2280 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int firstbyte = *p;
2282
2283 /* The idea of what is the previous and next
2284 * character depends on 'rightleft'. */
2285 if (wp->w_p_rl)
2286 {
2287 pc = prev_c;
2288 pc1 = prev_c1;
2289 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002290 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 else
2293 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002294 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002296 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298 prev_c = u8c;
2299
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002300 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 pc, pc1, nc);
2302 ScreenLines[idx] = firstbyte;
2303 }
2304 else
2305 prev_c = u8c;
2306#endif
2307 /* Non-BMP character: display as ? or fullwidth ?. */
2308 if (u8c >= 0x10000)
2309 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2310 else
2311 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002312 for (i = 0; i < Screen_mco; ++i)
2313 {
2314 ScreenLinesC[i][idx] = u8cc[i];
2315 if (u8cc[i] == 0)
2316 break;
2317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319 if (cells > 1)
2320 ScreenLines[idx + 1] = 0;
2321 }
2322 else if (cells > 1) /* double-byte character */
2323 {
2324 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2325 ScreenLines2[idx] = p[1];
2326 else
2327 ScreenLines[idx + 1] = p[1];
2328 }
2329 col += cells;
2330 idx += cells;
2331 p += c_len;
2332 }
2333 }
2334 else
2335#endif
2336 {
2337 len = (int)STRLEN(text);
2338 if (len > W_WIDTH(wp) - col)
2339 len = W_WIDTH(wp) - col;
2340 if (len > 0)
2341 {
2342#ifdef FEAT_RIGHTLEFT
2343 if (wp->w_p_rl)
2344 STRNCPY(current_ScreenLine, text, len);
2345 else
2346#endif
2347 STRNCPY(current_ScreenLine + col, text, len);
2348 col += len;
2349 }
2350 }
2351
2352 /* Fill the rest of the line with the fold filler */
2353#ifdef FEAT_RIGHTLEFT
2354 if (wp->w_p_rl)
2355 col -= txtcol;
2356#endif
2357 while (col < W_WIDTH(wp)
2358#ifdef FEAT_RIGHTLEFT
2359 - (wp->w_p_rl ? txtcol : 0)
2360#endif
2361 )
2362 {
2363#ifdef FEAT_MBYTE
2364 if (enc_utf8)
2365 {
2366 if (fill_fold >= 0x80)
2367 {
2368 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002369 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371 else
2372 ScreenLinesUC[off + col] = 0;
2373 }
2374#endif
2375 ScreenLines[off + col++] = fill_fold;
2376 }
2377
2378 if (text != buf)
2379 vim_free(text);
2380
2381 /*
2382 * 6. set highlighting for the Visual area an other text.
2383 * If all folded lines are in the Visual area, highlight the line.
2384 */
2385#ifdef FEAT_VISUAL
2386 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2387 {
2388 if (ltoreq(curwin->w_cursor, VIsual))
2389 {
2390 /* Visual is after curwin->w_cursor */
2391 top = &curwin->w_cursor;
2392 bot = &VIsual;
2393 }
2394 else
2395 {
2396 /* Visual is before curwin->w_cursor */
2397 top = &VIsual;
2398 bot = &curwin->w_cursor;
2399 }
2400 if (lnum >= top->lnum
2401 && lnume <= bot->lnum
2402 && (VIsual_mode != 'v'
2403 || ((lnum > top->lnum
2404 || (lnum == top->lnum
2405 && top->col == 0))
2406 && (lnume < bot->lnum
2407 || (lnume == bot->lnum
2408 && (bot->col - (*p_sel == 'e'))
2409 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2410 {
2411 if (VIsual_mode == Ctrl_V)
2412 {
2413 /* Visual block mode: highlight the chars part of the block */
2414 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2415 {
2416 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2417 len = wp->w_old_cursor_lcol;
2418 else
2419 len = W_WIDTH(wp) - txtcol;
2420 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002421 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423 }
2424 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002427 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430 }
2431#endif
2432
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002433#ifdef FEAT_SYN_HL
2434 /* Show 'cursorcolumn' in the fold line. */
2435 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2436 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2437 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2441 (int)W_WIDTH(wp), FALSE);
2442
2443 /*
2444 * Update w_cline_height and w_cline_folded if the cursor line was
2445 * updated (saves a call to plines() later).
2446 */
2447 if (wp == curwin
2448 && lnum <= curwin->w_cursor.lnum
2449 && lnume >= curwin->w_cursor.lnum)
2450 {
2451 curwin->w_cline_row = row;
2452 curwin->w_cline_height = 1;
2453 curwin->w_cline_folded = TRUE;
2454 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2455 }
2456}
2457
2458/*
2459 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2460 */
2461 static void
2462copy_text_attr(off, buf, len, attr)
2463 int off;
2464 char_u *buf;
2465 int len;
2466 int attr;
2467{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002468 int i;
2469
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 mch_memmove(ScreenLines + off, buf, (size_t)len);
2471# ifdef FEAT_MBYTE
2472 if (enc_utf8)
2473 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2474# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002475 for (i = 0; i < len; ++i)
2476 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477}
2478
2479/*
2480 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002481 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 */
2483 static void
2484fill_foldcolumn(p, wp, closed, lnum)
2485 char_u *p;
2486 win_T *wp;
2487 int closed; /* TRUE of FALSE */
2488 linenr_T lnum; /* current line number */
2489{
2490 int i = 0;
2491 int level;
2492 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002493 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 /* Init to all spaces. */
2496 copy_spaces(p, (size_t)wp->w_p_fdc);
2497
2498 level = win_foldinfo.fi_level;
2499 if (level > 0)
2500 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002501 /* If there is only one column put more info in it. */
2502 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 /* If the column is too narrow, we start at the lowest level that
2505 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002506 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (first_level < 1)
2508 first_level = 1;
2509
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002510 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 if (win_foldinfo.fi_lnum == lnum
2513 && first_level + i >= win_foldinfo.fi_low_level)
2514 p[i] = '-';
2515 else if (first_level == 1)
2516 p[i] = '|';
2517 else if (first_level + i <= 9)
2518 p[i] = '0' + first_level + i;
2519 else
2520 p[i] = '>';
2521 if (first_level + i == level)
2522 break;
2523 }
2524 }
2525 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002526 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527}
2528#endif /* FEAT_FOLDING */
2529
2530/*
2531 * Display line "lnum" of window 'wp' on the screen.
2532 * Start at row "startrow", stop when "endrow" is reached.
2533 * wp->w_virtcol needs to be valid.
2534 *
2535 * Return the number of last row the line occupies.
2536 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002537/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002539win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 win_T *wp;
2541 linenr_T lnum;
2542 int startrow;
2543 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002544 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 int col; /* visual column on screen */
2547 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2548 int c = 0; /* init for GCC */
2549 long vcol = 0; /* virtual column (for tabs) */
2550 long vcol_prev = -1; /* "vcol" of previous character */
2551 char_u *line; /* current line */
2552 char_u *ptr; /* current position in "line" */
2553 int row; /* row in the window, excl w_winrow */
2554 int screen_row; /* row on the screen, incl w_winrow */
2555
2556 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2557 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002558 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 int c_extra = NUL; /* extra chars, all the same */
2560 int extra_attr = 0; /* attributes when n_extra != 0 */
2561 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2562 displaying lcs_eol at end-of-line */
2563 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2564 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2565
2566 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2567 int saved_n_extra = 0;
2568 char_u *saved_p_extra = NULL;
2569 int saved_c_extra = 0;
2570 int saved_char_attr = 0;
2571
2572 int n_attr = 0; /* chars with special attr */
2573 int saved_attr2 = 0; /* char_attr saved for n_attr */
2574 int n_attr3 = 0; /* chars with overruling special attr */
2575 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2576
2577 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2578
2579 int fromcol, tocol; /* start/end of inverting */
2580 int fromcol_prev = -2; /* start of inverting after cursor */
2581 int noinvcur = FALSE; /* don't invert the cursor */
2582#ifdef FEAT_VISUAL
2583 pos_T *top, *bot;
2584#endif
2585 pos_T pos;
2586 long v;
2587
2588 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002589 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2591 in this line */
2592 int attr = 0; /* attributes for area highlighting */
2593 int area_attr = 0; /* attributes desired by highlighting */
2594 int search_attr = 0; /* attributes desired by 'hlsearch' */
2595#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002596 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 int syntax_attr = 0; /* attributes desired by syntax */
2598 int has_syntax = FALSE; /* this buffer has syntax highl. */
2599 int save_did_emsg;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002600#endif
2601#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002602 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002603# define SPWORDLEN 150
2604 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002605 int nextlinecol = 0; /* column where nextline[] starts */
2606 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002607 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002608 int spell_attr = 0; /* attributes desired by spelling */
2609 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002610 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2611 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002612 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002613 static int cap_col = -1; /* column to check for Cap word */
2614 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002615 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#endif
2617 int extra_check; /* has syntax or linebreak */
2618#ifdef FEAT_MBYTE
2619 int multi_attr = 0; /* attributes desired by multibyte */
2620 int mb_l = 1; /* multi-byte byte length */
2621 int mb_c = 0; /* decoded multi-byte character */
2622 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002623 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624#endif
2625#ifdef FEAT_DIFF
2626 int filler_lines; /* nr of filler lines to be drawn */
2627 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002628 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 int change_start = MAXCOL; /* first col of changed area */
2630 int change_end = -1; /* last col of changed area */
2631#endif
2632 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2633#ifdef FEAT_LINEBREAK
2634 int need_showbreak = FALSE;
2635#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002636#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2637 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638# define LINE_ATTR
2639 int line_attr = 0; /* atrribute for the whole line */
2640#endif
2641#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002642 matchitem_T *cur; /* points to the match list */
2643 match_T *shl; /* points to search_hl or a match */
2644 int shl_flag; /* flag to indicate whether search_hl
2645 has been processed or not */
2646 int prevcol_hl_flag; /* flag to indicate whether prevcol
2647 equals startcol of search_hl or one
2648 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#endif
2650#ifdef FEAT_ARABIC
2651 int prev_c = 0; /* previous Arabic character */
2652 int prev_c1 = 0; /* first composing char for prev_c */
2653#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002654#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002655 int did_line_attr = 0;
2656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 /* draw_state: items that are drawn in sequence: */
2659#define WL_START 0 /* nothing done yet */
2660#ifdef FEAT_CMDWIN
2661# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2662#else
2663# define WL_CMDLINE WL_START
2664#endif
2665#ifdef FEAT_FOLDING
2666# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2667#else
2668# define WL_FOLD WL_CMDLINE
2669#endif
2670#ifdef FEAT_SIGNS
2671# define WL_SIGN WL_FOLD + 1 /* column for signs */
2672#else
2673# define WL_SIGN WL_FOLD /* column for signs */
2674#endif
2675#define WL_NR WL_SIGN + 1 /* line number */
2676#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2677# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2678#else
2679# define WL_SBR WL_NR
2680#endif
2681#define WL_LINE WL_SBR + 1 /* text in the line */
2682 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002683#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int feedback_col = 0;
2685 int feedback_old_attr = -1;
2686#endif
2687
2688
2689 if (startrow > endrow) /* past the end already! */
2690 return startrow;
2691
2692 row = startrow;
2693 screen_row = row + W_WINROW(wp);
2694
2695 /*
2696 * To speed up the loop below, set extra_check when there is linebreak,
2697 * trailing white space and/or syntax processing to be done.
2698 */
2699#ifdef FEAT_LINEBREAK
2700 extra_check = wp->w_p_lbr;
2701#else
2702 extra_check = 0;
2703#endif
2704#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002705 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
2707 /* Prepare for syntax highlighting in this line. When there is an
2708 * error, stop syntax highlighting. */
2709 save_did_emsg = did_emsg;
2710 did_emsg = FALSE;
2711 syntax_start(wp, lnum);
2712 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002713 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 else
2715 {
2716 did_emsg = save_did_emsg;
2717 has_syntax = TRUE;
2718 extra_check = TRUE;
2719 }
2720 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002721#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002722
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002723#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002724 if (wp->w_p_spell
2725 && *wp->w_buffer->b_p_spl != NUL
2726 && wp->w_buffer->b_langp.ga_len > 0
2727 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002728 {
2729 /* Prepare for spell checking. */
2730 has_spell = TRUE;
2731 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002732
2733 /* Get the start of the next line, so that words that wrap to the next
2734 * line are found too: "et<line-break>al.".
2735 * Trick: skip a few chars for C/shell/Vim comments */
2736 nextline[SPWORDLEN] = NUL;
2737 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2738 {
2739 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2740 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2741 }
2742
2743 /* When a word wrapped from the previous line the start of the current
2744 * line is valid. */
2745 if (lnum == checked_lnum)
2746 cur_checked_col = checked_col;
2747 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002748
2749 /* When there was a sentence end in the previous line may require a
2750 * word starting with capital in this line. In line 1 always check
2751 * the first word. */
2752 if (lnum != capcol_lnum)
2753 cap_col = -1;
2754 if (lnum == 1)
2755 cap_col = 0;
2756 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
2759
2760 /*
2761 * handle visual active in this window
2762 */
2763 fromcol = -10;
2764 tocol = MAXCOL;
2765#ifdef FEAT_VISUAL
2766 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2767 {
2768 /* Visual is after curwin->w_cursor */
2769 if (ltoreq(curwin->w_cursor, VIsual))
2770 {
2771 top = &curwin->w_cursor;
2772 bot = &VIsual;
2773 }
2774 else /* Visual is before curwin->w_cursor */
2775 {
2776 top = &VIsual;
2777 bot = &curwin->w_cursor;
2778 }
2779 if (VIsual_mode == Ctrl_V) /* block mode */
2780 {
2781 if (lnum >= top->lnum && lnum <= bot->lnum)
2782 {
2783 fromcol = wp->w_old_cursor_fcol;
2784 tocol = wp->w_old_cursor_lcol;
2785 }
2786 }
2787 else /* non-block mode */
2788 {
2789 if (lnum > top->lnum && lnum <= bot->lnum)
2790 fromcol = 0;
2791 else if (lnum == top->lnum)
2792 {
2793 if (VIsual_mode == 'V') /* linewise */
2794 fromcol = 0;
2795 else
2796 {
2797 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2798 if (gchar_pos(top) == NUL)
2799 tocol = fromcol + 1;
2800 }
2801 }
2802 if (VIsual_mode != 'V' && lnum == bot->lnum)
2803 {
2804 if (*p_sel == 'e' && bot->col == 0
2805#ifdef FEAT_VIRTUALEDIT
2806 && bot->coladd == 0
2807#endif
2808 )
2809 {
2810 fromcol = -10;
2811 tocol = MAXCOL;
2812 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002813 else if (bot->col == MAXCOL)
2814 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 else
2816 {
2817 pos = *bot;
2818 if (*p_sel == 'e')
2819 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2820 else
2821 {
2822 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2823 ++tocol;
2824 }
2825 }
2826 }
2827 }
2828
2829#ifndef MSDOS
2830 /* Check if the character under the cursor should not be inverted */
2831 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2832# ifdef FEAT_GUI
2833 && !gui.in_use
2834# endif
2835 )
2836 noinvcur = TRUE;
2837#endif
2838
2839 /* if inverting in this line set area_highlighting */
2840 if (fromcol >= 0)
2841 {
2842 area_highlighting = TRUE;
2843 attr = hl_attr(HLF_V);
2844#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2845 if (clip_star.available && !clip_star.owned && clip_isautosel())
2846 attr = hl_attr(HLF_VNC);
2847#endif
2848 }
2849 }
2850
2851 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002852 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 */
2854 else
2855#endif /* FEAT_VISUAL */
2856 if (highlight_match
2857 && wp == curwin
2858 && lnum >= curwin->w_cursor.lnum
2859 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2860 {
2861 if (lnum == curwin->w_cursor.lnum)
2862 getvcol(curwin, &(curwin->w_cursor),
2863 (colnr_T *)&fromcol, NULL, NULL);
2864 else
2865 fromcol = 0;
2866 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2867 {
2868 pos.lnum = lnum;
2869 pos.col = search_match_endcol;
2870 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2871 }
2872 else
2873 tocol = MAXCOL;
2874 if (fromcol == tocol) /* do at least one character */
2875 tocol = fromcol + 1; /* happens when past end of line */
2876 area_highlighting = TRUE;
2877 attr = hl_attr(HLF_I);
2878 }
2879
2880#ifdef FEAT_DIFF
2881 filler_lines = diff_check(wp, lnum);
2882 if (filler_lines < 0)
2883 {
2884 if (filler_lines == -1)
2885 {
2886 if (diff_find_change(wp, lnum, &change_start, &change_end))
2887 diff_hlf = HLF_ADD; /* added line */
2888 else if (change_start == 0)
2889 diff_hlf = HLF_TXD; /* changed text */
2890 else
2891 diff_hlf = HLF_CHD; /* changed line */
2892 }
2893 else
2894 diff_hlf = HLF_ADD; /* added line */
2895 filler_lines = 0;
2896 area_highlighting = TRUE;
2897 }
2898 if (lnum == wp->w_topline)
2899 filler_lines = wp->w_topfill;
2900 filler_todo = filler_lines;
2901#endif
2902
2903#ifdef LINE_ATTR
2904# ifdef FEAT_SIGNS
2905 /* If this line has a sign with line highlighting set line_attr. */
2906 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2907 if (v != 0)
2908 line_attr = sign_get_attr((int)v, TRUE);
2909# endif
2910# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2911 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002912 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 line_attr = hl_attr(HLF_L);
2914# endif
2915 if (line_attr != 0)
2916 area_highlighting = TRUE;
2917#endif
2918
2919 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2920 ptr = line;
2921
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002922#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002923 if (has_spell)
2924 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002925 /* For checking first word with a capital skip white space. */
2926 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002927 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002928
Bram Moolenaar30abd282005-06-22 22:35:10 +00002929 /* To be able to spell-check over line boundaries copy the end of the
2930 * current line into nextline[]. Above the start of the next line was
2931 * copied to nextline[SPWORDLEN]. */
2932 if (nextline[SPWORDLEN] == NUL)
2933 {
2934 /* No next line or it is empty. */
2935 nextlinecol = MAXCOL;
2936 nextline_idx = 0;
2937 }
2938 else
2939 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002940 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002941 if (v < SPWORDLEN)
2942 {
2943 /* Short line, use it completely and append the start of the
2944 * next line. */
2945 nextlinecol = 0;
2946 mch_memmove(nextline, line, (size_t)v);
2947 mch_memmove(nextline + v, nextline + SPWORDLEN,
2948 STRLEN(nextline + SPWORDLEN) + 1);
2949 nextline_idx = v + 1;
2950 }
2951 else
2952 {
2953 /* Long line, use only the last SPWORDLEN bytes. */
2954 nextlinecol = v - SPWORDLEN;
2955 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2956 nextline_idx = SPWORDLEN + 1;
2957 }
2958 }
2959 }
2960#endif
2961
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 /* find start of trailing whitespace */
2963 if (wp->w_p_list && lcs_trail)
2964 {
2965 trailcol = (colnr_T)STRLEN(ptr);
2966 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2967 --trailcol;
2968 trailcol += (colnr_T) (ptr - line);
2969 extra_check = TRUE;
2970 }
2971
2972 /*
2973 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2974 * first character to be displayed.
2975 */
2976 if (wp->w_p_wrap)
2977 v = wp->w_skipcol;
2978 else
2979 v = wp->w_leftcol;
2980 if (v > 0)
2981 {
2982#ifdef FEAT_MBYTE
2983 char_u *prev_ptr = ptr;
2984#endif
2985 while (vcol < v && *ptr != NUL)
2986 {
2987 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2988 vcol += c;
2989#ifdef FEAT_MBYTE
2990 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002992 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994
2995#ifdef FEAT_VIRTUALEDIT
2996 /* When 'virtualedit' is set the end of the line may be before the
2997 * start of the displayed part. */
2998 if (vcol < v && *ptr == NUL && virtual_active())
2999 vcol = v;
3000#endif
3001
3002 /* Handle a character that's not completely on the screen: Put ptr at
3003 * that character but skip the first few screen characters. */
3004 if (vcol > v)
3005 {
3006 vcol -= c;
3007#ifdef FEAT_MBYTE
3008 ptr = prev_ptr;
3009#else
3010 --ptr;
3011#endif
3012 n_skip = v - vcol;
3013 }
3014
3015 /*
3016 * Adjust for when the inverted text is before the screen,
3017 * and when the start of the inverted text is before the screen.
3018 */
3019 if (tocol <= vcol)
3020 fromcol = 0;
3021 else if (fromcol >= 0 && fromcol < vcol)
3022 fromcol = vcol;
3023
3024#ifdef FEAT_LINEBREAK
3025 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3026 if (wp->w_p_wrap)
3027 need_showbreak = TRUE;
3028#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003029#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003030 /* When spell checking a word we need to figure out the start of the
3031 * word and if it's badly spelled or not. */
3032 if (has_spell)
3033 {
3034 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003035 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003036
3037 pos = wp->w_cursor;
3038 wp->w_cursor.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003039 wp->w_cursor.col = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003040 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003041 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003042 {
3043 /* no bad word found at line start, don't check until end of a
3044 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003045 spell_hlf = HLF_COUNT;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003046 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003047 }
3048 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003049 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003050 /* bad word found, use attributes until end of word */
3051 word_end = wp->w_cursor.col + len + 1;
3052
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003053 /* Turn index into actual attributes. */
3054 if (spell_hlf != HLF_COUNT)
3055 spell_attr = highlight_attr[spell_hlf];
3056 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003057 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003058
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003059# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003060 /* Need to restart syntax highlighting for this line. */
3061 if (has_syntax)
3062 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003063# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003064 }
3065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 }
3067
3068 /*
3069 * Correct highlighting for cursor that can't be disabled.
3070 * Avoids having to check this for each character.
3071 */
3072 if (fromcol >= 0)
3073 {
3074 if (noinvcur)
3075 {
3076 if ((colnr_T)fromcol == wp->w_virtcol)
3077 {
3078 /* highlighting starts at cursor, let it start just after the
3079 * cursor */
3080 fromcol_prev = fromcol;
3081 fromcol = -1;
3082 }
3083 else if ((colnr_T)fromcol < wp->w_virtcol)
3084 /* restart highlighting after the cursor */
3085 fromcol_prev = wp->w_virtcol;
3086 }
3087 if (fromcol >= tocol)
3088 fromcol = -1;
3089 }
3090
3091#ifdef FEAT_SEARCH_EXTRA
3092 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003093 * Handle highlighting the last used search pattern and matches.
3094 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003096 cur = wp->w_match_head;
3097 shl_flag = FALSE;
3098 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003100 if (shl_flag == FALSE)
3101 {
3102 shl = &search_hl;
3103 shl_flag = TRUE;
3104 }
3105 else
3106 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003107 shl->startcol = MAXCOL;
3108 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 shl->attr_cur = 0;
3110 if (shl->rm.regprog != NULL)
3111 {
3112 v = (long)(ptr - line);
3113 next_search_hl(wp, shl, lnum, (colnr_T)v);
3114
3115 /* Need to get the line again, a multi-line regexp may have made it
3116 * invalid. */
3117 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3118 ptr = line + v;
3119
3120 if (shl->lnum != 0 && shl->lnum <= lnum)
3121 {
3122 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003123 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003125 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3127 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003128 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003130 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003132 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
3134#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003135 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003136 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 else
3138#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003139 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003141 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 shl->attr_cur = shl->attr;
3144 search_attr = shl->attr;
3145 }
3146 area_highlighting = TRUE;
3147 }
3148 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003149 if (shl != &search_hl && cur != NULL)
3150 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 }
3152#endif
3153
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003154#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003155 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3156 * active, because it's not clear what is selected then. */
3157 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003158 {
3159 line_attr = hl_attr(HLF_CUL);
3160 area_highlighting = TRUE;
3161 }
3162#endif
3163
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003164 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 col = 0;
3166#ifdef FEAT_RIGHTLEFT
3167 if (wp->w_p_rl)
3168 {
3169 /* Rightleft window: process the text in the normal direction, but put
3170 * it in current_ScreenLine[] from right to left. Start at the
3171 * rightmost column of the window. */
3172 col = W_WIDTH(wp) - 1;
3173 off += col;
3174 }
3175#endif
3176
3177 /*
3178 * Repeat for the whole displayed line.
3179 */
3180 for (;;)
3181 {
3182 /* Skip this quickly when working on the text. */
3183 if (draw_state != WL_LINE)
3184 {
3185#ifdef FEAT_CMDWIN
3186 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3187 {
3188 draw_state = WL_CMDLINE;
3189 if (cmdwin_type != 0 && wp == curwin)
3190 {
3191 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003193 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 char_attr = hl_attr(HLF_AT);
3195 }
3196 }
3197#endif
3198
3199#ifdef FEAT_FOLDING
3200 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3201 {
3202 draw_state = WL_FOLD;
3203 if (wp->w_p_fdc > 0)
3204 {
3205 /* Draw the 'foldcolumn'. */
3206 fill_foldcolumn(extra, wp, FALSE, lnum);
3207 n_extra = wp->w_p_fdc;
3208 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003209 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 c_extra = NUL;
3211 char_attr = hl_attr(HLF_FC);
3212 }
3213 }
3214#endif
3215
3216#ifdef FEAT_SIGNS
3217 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3218 {
3219 draw_state = WL_SIGN;
3220 /* Show the sign column when there are any signs in this
3221 * buffer or when using Netbeans. */
3222 if (draw_signcolumn(wp)
3223# ifdef FEAT_DIFF
3224 && filler_todo <= 0
3225# endif
3226 )
3227 {
3228 int_u text_sign;
3229# ifdef FEAT_SIGN_ICONS
3230 int_u icon_sign;
3231# endif
3232
3233 /* Draw two cells with the sign value or blank. */
3234 c_extra = ' ';
3235 char_attr = hl_attr(HLF_SC);
3236 n_extra = 2;
3237
3238 if (row == startrow)
3239 {
3240 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3241 SIGN_TEXT);
3242# ifdef FEAT_SIGN_ICONS
3243 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3244 SIGN_ICON);
3245 if (gui.in_use && icon_sign != 0)
3246 {
3247 /* Use the image in this position. */
3248 c_extra = SIGN_BYTE;
3249# ifdef FEAT_NETBEANS_INTG
3250 if (buf_signcount(wp->w_buffer, lnum) > 1)
3251 c_extra = MULTISIGN_BYTE;
3252# endif
3253 char_attr = icon_sign;
3254 }
3255 else
3256# endif
3257 if (text_sign != 0)
3258 {
3259 p_extra = sign_get_text(text_sign);
3260 if (p_extra != NULL)
3261 {
3262 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003263 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
3265 char_attr = sign_get_attr(text_sign, FALSE);
3266 }
3267 }
3268 }
3269 }
3270#endif
3271
3272 if (draw_state == WL_NR - 1 && n_extra == 0)
3273 {
3274 draw_state = WL_NR;
3275 /* Display the line number. After the first fill with blanks
3276 * when the 'n' flag isn't in 'cpo' */
3277 if (wp->w_p_nu
3278 && (row == startrow
3279#ifdef FEAT_DIFF
3280 + filler_lines
3281#endif
3282 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3283 {
3284 /* Draw the line number (empty space after wrapping). */
3285 if (row == startrow
3286#ifdef FEAT_DIFF
3287 + filler_lines
3288#endif
3289 )
3290 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003291 sprintf((char *)extra, "%*ld ",
3292 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (wp->w_skipcol > 0)
3294 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3295 *p_extra = '-';
3296#ifdef FEAT_RIGHTLEFT
3297 if (wp->w_p_rl) /* reverse line numbers */
3298 rl_mirror(extra);
3299#endif
3300 p_extra = extra;
3301 c_extra = NUL;
3302 }
3303 else
3304 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003305 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003307#ifdef FEAT_SYN_HL
3308 /* When 'cursorline' is set highlight the line number of
3309 * the current line differently. */
3310 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3311 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314 }
3315
3316#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3317 if (draw_state == WL_SBR - 1 && n_extra == 0)
3318 {
3319 draw_state = WL_SBR;
3320# ifdef FEAT_DIFF
3321 if (filler_todo > 0)
3322 {
3323 /* Draw "deleted" diff line(s). */
3324 if (char2cells(fill_diff) > 1)
3325 c_extra = '-';
3326 else
3327 c_extra = fill_diff;
3328# ifdef FEAT_RIGHTLEFT
3329 if (wp->w_p_rl)
3330 n_extra = col + 1;
3331 else
3332# endif
3333 n_extra = W_WIDTH(wp) - col;
3334 char_attr = hl_attr(HLF_DED);
3335 }
3336# endif
3337# ifdef FEAT_LINEBREAK
3338 if (*p_sbr != NUL && need_showbreak)
3339 {
3340 /* Draw 'showbreak' at the start of each broken line. */
3341 p_extra = p_sbr;
3342 c_extra = NUL;
3343 n_extra = (int)STRLEN(p_sbr);
3344 char_attr = hl_attr(HLF_AT);
3345 need_showbreak = FALSE;
3346 /* Correct end of highlighted area for 'showbreak',
3347 * required when 'linebreak' is also set. */
3348 if (tocol == vcol)
3349 tocol += n_extra;
3350 }
3351# endif
3352 }
3353#endif
3354
3355 if (draw_state == WL_LINE - 1 && n_extra == 0)
3356 {
3357 draw_state = WL_LINE;
3358 if (saved_n_extra)
3359 {
3360 /* Continue item from end of wrapped line. */
3361 n_extra = saved_n_extra;
3362 c_extra = saved_c_extra;
3363 p_extra = saved_p_extra;
3364 char_attr = saved_char_attr;
3365 }
3366 else
3367 char_attr = 0;
3368 }
3369 }
3370
3371 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003372 if (dollar_vcol != 0 && wp == curwin
3373 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#ifdef FEAT_DIFF
3375 && filler_todo <= 0
3376#endif
3377 )
3378 {
3379 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3380 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003381 /* Pretend we have finished updating the window. Except when
3382 * 'cursorcolumn' is set. */
3383#ifdef FEAT_SYN_HL
3384 if (wp->w_p_cuc)
3385 row = wp->w_cline_row + wp->w_cline_height;
3386 else
3387#endif
3388 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 break;
3390 }
3391
3392 if (draw_state == WL_LINE && area_highlighting)
3393 {
3394 /* handle Visual or match highlighting in this line */
3395 if (vcol == fromcol
3396#ifdef FEAT_MBYTE
3397 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3398 && (*mb_ptr2cells)(ptr) > 1)
3399#endif
3400 || ((int)vcol_prev == fromcol_prev
3401 && vcol < tocol))
3402 area_attr = attr; /* start highlighting */
3403 else if (area_attr != 0
3404 && (vcol == tocol
3405 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408#ifdef FEAT_SEARCH_EXTRA
3409 if (!n_extra)
3410 {
3411 /*
3412 * Check for start/end of search pattern match.
3413 * After end, check for start/end of next match.
3414 * When another match, have to check for start again.
3415 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003416 * Do this for 'search_hl' and the match list (ordered by
3417 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003419 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003420 cur = wp->w_match_head;
3421 shl_flag = FALSE;
3422 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003424 if (shl_flag == FALSE
3425 && ((cur != NULL
3426 && cur->priority > SEARCH_HL_PRIORITY)
3427 || cur == NULL))
3428 {
3429 shl = &search_hl;
3430 shl_flag = TRUE;
3431 }
3432 else
3433 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 while (shl->rm.regprog != NULL)
3435 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003436 if (shl->startcol != MAXCOL
3437 && v >= (long)shl->startcol
3438 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 shl->attr_cur = shl->attr;
3441 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003442 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
3444 shl->attr_cur = 0;
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 next_search_hl(wp, shl, lnum, (colnr_T)v);
3447
3448 /* Need to get the line again, a multi-line regexp
3449 * may have made it invalid. */
3450 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3451 ptr = line + v;
3452
3453 if (shl->lnum == lnum)
3454 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003455 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003457 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003459 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003461 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 /* highlight empty match, try again after
3464 * it */
3465#ifdef FEAT_MBYTE
3466 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003467 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003468 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 else
3470#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003471 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473
3474 /* Loop to check if the match starts at the
3475 * current position */
3476 continue;
3477 }
3478 }
3479 break;
3480 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003481 if (shl != &search_hl && cur != NULL)
3482 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003484
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003485 /* Use attributes from match with highest priority among
3486 * 'search_hl' and the match list. */
3487 search_attr = search_hl.attr_cur;
3488 cur = wp->w_match_head;
3489 shl_flag = FALSE;
3490 while (cur != NULL || shl_flag == FALSE)
3491 {
3492 if (shl_flag == FALSE
3493 && ((cur != NULL
3494 && cur->priority > SEARCH_HL_PRIORITY)
3495 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003496 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003497 shl = &search_hl;
3498 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003499 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003500 else
3501 shl = &cur->hl;
3502 if (shl->attr_cur != 0)
3503 search_attr = shl->attr_cur;
3504 if (shl != &search_hl && cur != NULL)
3505 cur = cur->next;
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508#endif
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003511 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003513 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3514 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003516 if (diff_hlf == HLF_TXD && ptr - line > change_end
3517 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003519 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003522
3523 /* Decide which of the highlight attributes to use. */
3524 attr_pri = TRUE;
3525 if (area_attr != 0)
3526 char_attr = area_attr;
3527 else if (search_attr != 0)
3528 char_attr = search_attr;
3529#ifdef LINE_ATTR
3530 /* Use line_attr when not in the Visual or 'incsearch' area
3531 * (area_attr may be 0 when "noinvcur" is set). */
3532 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3533 || (vcol < fromcol || vcol >= tocol)))
3534 char_attr = line_attr;
3535#endif
3536 else
3537 {
3538 attr_pri = FALSE;
3539#ifdef FEAT_SYN_HL
3540 if (has_syntax)
3541 char_attr = syntax_attr;
3542 else
3543#endif
3544 char_attr = 0;
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
3548 /*
3549 * Get the next character to put on the screen.
3550 */
3551 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003552 * The "p_extra" points to the extra stuff that is inserted to
3553 * represent special characters (non-printable stuff) and other
3554 * things. When all characters are the same, c_extra is used.
3555 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3556 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3558 */
3559 if (n_extra > 0)
3560 {
3561 if (c_extra != NUL)
3562 {
3563 c = c_extra;
3564#ifdef FEAT_MBYTE
3565 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3566 if (enc_utf8 && (*mb_char2len)(c) > 1)
3567 {
3568 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003569 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003570 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 else
3573 mb_utf8 = FALSE;
3574#endif
3575 }
3576 else
3577 {
3578 c = *p_extra;
3579#ifdef FEAT_MBYTE
3580 if (has_mbyte)
3581 {
3582 mb_c = c;
3583 if (enc_utf8)
3584 {
3585 /* If the UTF-8 character is more than one byte:
3586 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003587 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 mb_utf8 = FALSE;
3589 if (mb_l > n_extra)
3590 mb_l = 1;
3591 else if (mb_l > 1)
3592 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003593 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003595 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597 }
3598 else
3599 {
3600 /* if this is a DBCS character, put it in "mb_c" */
3601 mb_l = MB_BYTE2LEN(c);
3602 if (mb_l >= n_extra)
3603 mb_l = 1;
3604 else if (mb_l > 1)
3605 mb_c = (c << 8) + p_extra[1];
3606 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003607 if (mb_l == 0) /* at the NUL at end-of-line */
3608 mb_l = 1;
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 /* If a double-width char doesn't fit display a '>' in the
3611 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003612 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613# ifdef FEAT_RIGHTLEFT
3614 wp->w_p_rl ? (col <= 0) :
3615# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003616 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 && (*mb_char2cells)(mb_c) == 2)
3618 {
3619 c = '>';
3620 mb_c = c;
3621 mb_l = 1;
3622 mb_utf8 = FALSE;
3623 multi_attr = hl_attr(HLF_AT);
3624 /* put the pointer back to output the double-width
3625 * character at the start of the next line. */
3626 ++n_extra;
3627 --p_extra;
3628 }
3629 else
3630 {
3631 n_extra -= mb_l - 1;
3632 p_extra += mb_l - 1;
3633 }
3634 }
3635#endif
3636 ++p_extra;
3637 }
3638 --n_extra;
3639 }
3640 else
3641 {
3642 /*
3643 * Get a character from the line itself.
3644 */
3645 c = *ptr;
3646#ifdef FEAT_MBYTE
3647 if (has_mbyte)
3648 {
3649 mb_c = c;
3650 if (enc_utf8)
3651 {
3652 /* If the UTF-8 character is more than one byte: Decode it
3653 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003654 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 mb_utf8 = FALSE;
3656 if (mb_l > 1)
3657 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003658 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 /* Overlong encoded ASCII or ASCII with composing char
3660 * is displayed normally, except a NUL. */
3661 if (mb_c < 0x80)
3662 c = mb_c;
3663 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003664
3665 /* At start of the line we can have a composing char.
3666 * Draw it as a space with a composing char. */
3667 if (utf_iscomposing(mb_c))
3668 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003669 int i;
3670
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003671 for (i = Screen_mco - 1; i > 0; --i)
3672 u8cc[i] = u8cc[i - 1];
3673 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003674 mb_c = ' ';
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677
3678 if ((mb_l == 1 && c >= 0x80)
3679 || (mb_l >= 1 && mb_c == 0)
3680 || (mb_l > 1 && (!vim_isprintc(mb_c)
3681 || mb_c >= 0x10000)))
3682 {
3683 /*
3684 * Illegal UTF-8 byte: display as <xx>.
3685 * Non-BMP character : display as ? or fullwidth ?.
3686 */
3687 if (mb_c < 0x10000)
3688 {
3689 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003690# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 if (wp->w_p_rl) /* reverse */
3692 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695 else if (utf_char2cells(mb_c) != 2)
3696 STRCPY(extra, "?");
3697 else
3698 /* 0xff1f in UTF-8: full-width '?' */
3699 STRCPY(extra, "\357\274\237");
3700
3701 p_extra = extra;
3702 c = *p_extra;
3703 mb_c = mb_ptr2char_adv(&p_extra);
3704 mb_utf8 = (c >= 0x80);
3705 n_extra = (int)STRLEN(p_extra);
3706 c_extra = NUL;
3707 if (area_attr == 0 && search_attr == 0)
3708 {
3709 n_attr = n_extra + 1;
3710 extra_attr = hl_attr(HLF_8);
3711 saved_attr2 = char_attr; /* save current attr */
3712 }
3713 }
3714 else if (mb_l == 0) /* at the NUL at end-of-line */
3715 mb_l = 1;
3716#ifdef FEAT_ARABIC
3717 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3718 {
3719 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003720 int pc, pc1, nc;
3721 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 /* The idea of what is the previous and next
3724 * character depends on 'rightleft'. */
3725 if (wp->w_p_rl)
3726 {
3727 pc = prev_c;
3728 pc1 = prev_c1;
3729 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003730 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 else
3733 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003734 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003736 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738 prev_c = mb_c;
3739
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003740 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 else
3743 prev_c = mb_c;
3744#endif
3745 }
3746 else /* enc_dbcs */
3747 {
3748 mb_l = MB_BYTE2LEN(c);
3749 if (mb_l == 0) /* at the NUL at end-of-line */
3750 mb_l = 1;
3751 else if (mb_l > 1)
3752 {
3753 /* We assume a second byte below 32 is illegal.
3754 * Hopefully this is OK for all double-byte encodings!
3755 */
3756 if (ptr[1] >= 32)
3757 mb_c = (c << 8) + ptr[1];
3758 else
3759 {
3760 if (ptr[1] == NUL)
3761 {
3762 /* head byte at end of line */
3763 mb_l = 1;
3764 transchar_nonprint(extra, c);
3765 }
3766 else
3767 {
3768 /* illegal tail byte */
3769 mb_l = 2;
3770 STRCPY(extra, "XX");
3771 }
3772 p_extra = extra;
3773 n_extra = (int)STRLEN(extra) - 1;
3774 c_extra = NUL;
3775 c = *p_extra++;
3776 if (area_attr == 0 && search_attr == 0)
3777 {
3778 n_attr = n_extra + 1;
3779 extra_attr = hl_attr(HLF_8);
3780 saved_attr2 = char_attr; /* save current attr */
3781 }
3782 mb_c = c;
3783 }
3784 }
3785 }
3786 /* If a double-width char doesn't fit display a '>' in the
3787 * last column; the character is displayed at the start of the
3788 * next line. */
3789 if ((
3790# ifdef FEAT_RIGHTLEFT
3791 wp->w_p_rl ? (col <= 0) :
3792# endif
3793 (col >= W_WIDTH(wp) - 1))
3794 && (*mb_char2cells)(mb_c) == 2)
3795 {
3796 c = '>';
3797 mb_c = c;
3798 mb_utf8 = FALSE;
3799 mb_l = 1;
3800 multi_attr = hl_attr(HLF_AT);
3801 /* Put pointer back so that the character will be
3802 * displayed at the start of the next line. */
3803 --ptr;
3804 }
3805 else if (*ptr != NUL)
3806 ptr += mb_l - 1;
3807
3808 /* If a double-width char doesn't fit at the left side display
3809 * a '<' in the first column. */
3810 if (n_skip > 0 && mb_l > 1)
3811 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003813 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 c = ' ';
3815 if (area_attr == 0 && search_attr == 0)
3816 {
3817 n_attr = n_extra + 1;
3818 extra_attr = hl_attr(HLF_AT);
3819 saved_attr2 = char_attr; /* save current attr */
3820 }
3821 mb_c = c;
3822 mb_utf8 = FALSE;
3823 mb_l = 1;
3824 }
3825
3826 }
3827#endif
3828 ++ptr;
3829
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003830 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003831 if (wp->w_p_list && (c == 160
3832#ifdef FEAT_MBYTE
3833 || (mb_utf8 && mb_c == 160)
3834#endif
3835 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003836 {
3837 c = lcs_nbsp;
3838 if (area_attr == 0 && search_attr == 0)
3839 {
3840 n_attr = 1;
3841 extra_attr = hl_attr(HLF_8);
3842 saved_attr2 = char_attr; /* save current attr */
3843 }
3844#ifdef FEAT_MBYTE
3845 mb_c = c;
3846 if (enc_utf8 && (*mb_char2len)(c) > 1)
3847 {
3848 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003849 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003850 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003851 }
3852 else
3853 mb_utf8 = FALSE;
3854#endif
3855 }
3856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (extra_check)
3858 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003859#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003860 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003861#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003862
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003863#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 /* Get syntax attribute, unless still at the start of the line
3865 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003866 v = (long)(ptr - line);
3867 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 /* Get the syntax attribute for the character. If there
3870 * is an error, disable syntax highlighting. */
3871 save_did_emsg = did_emsg;
3872 did_emsg = FALSE;
3873
Bram Moolenaar217ad922005-03-20 22:37:15 +00003874 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003875# ifdef FEAT_SPELL
3876 has_spell ? &can_spell :
3877# endif
3878 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003881 {
3882 wp->w_buffer->b_syn_error = TRUE;
3883 has_syntax = FALSE;
3884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 else
3886 did_emsg = save_did_emsg;
3887
3888 /* Need to get the line again, a multi-line regexp may
3889 * have made it invalid. */
3890 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3891 ptr = line + v;
3892
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003893 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003895 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003896 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003898#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003899
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003900#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003901 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003902 * Only do this when there is no syntax highlighting, the
3903 * @Spell cluster is not used or the current syntax item
3904 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003905 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003906 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003907 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003908# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003909 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003910 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003911# endif
3912 if (c != 0 && (
3913# ifdef FEAT_SYN_HL
3914 !has_syntax ||
3915# endif
3916 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003917 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003918 char_u *prev_ptr, *p;
3919 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003920 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003921# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003922 if (has_mbyte)
3923 {
3924 prev_ptr = ptr - mb_l;
3925 v -= mb_l - 1;
3926 }
3927 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003928# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003929 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003930
3931 /* Use nextline[] if possible, it has the start of the
3932 * next line concatenated. */
3933 if ((prev_ptr - line) - nextlinecol >= 0)
3934 p = nextline + (prev_ptr - line) - nextlinecol;
3935 else
3936 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003937 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003938 len = spell_check(wp, p, &spell_hlf, &cap_col,
3939 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003940 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003941
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003942 /* In Insert mode only highlight a word that
3943 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003944 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003945 && (State & INSERT) != 0
3946 && wp->w_cursor.lnum == lnum
3947 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003948 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003949 && wp->w_cursor.col < (colnr_T)word_end)
3950 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003951 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003952 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003953 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003954
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003955 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003956 && (p - nextline) + len > nextline_idx)
3957 {
3958 /* Remember that the good word continues at the
3959 * start of the next line. */
3960 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003961 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003962 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003963
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003964 /* Turn index into actual attributes. */
3965 if (spell_hlf != HLF_COUNT)
3966 spell_attr = highlight_attr[spell_hlf];
3967
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003968 if (cap_col > 0)
3969 {
3970 if (p != prev_ptr
3971 && (p - nextline) + cap_col >= nextline_idx)
3972 {
3973 /* Remember that the word in the next line
3974 * must start with a capital. */
3975 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003976 cap_col = (int)((p - nextline) + cap_col
3977 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003978 }
3979 else
3980 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003981 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003982 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003983 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003984 }
3985 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003986 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003987 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003988 char_attr = hl_combine_attr(char_attr, spell_attr);
3989 else
3990 char_attr = hl_combine_attr(spell_attr, char_attr);
3991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#endif
3993#ifdef FEAT_LINEBREAK
3994 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003995 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 */
3997 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003998 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
4000 n_extra = win_lbr_chartabsize(wp, ptr - (
4001# ifdef FEAT_MBYTE
4002 has_mbyte ? mb_l :
4003# endif
4004 1), (colnr_T)vcol, NULL) - 1;
4005 c_extra = ' ';
4006 if (vim_iswhite(c))
4007 c = ' ';
4008 }
4009#endif
4010
4011 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4012 {
4013 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004014 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 n_attr = 1;
4017 extra_attr = hl_attr(HLF_8);
4018 saved_attr2 = char_attr; /* save current attr */
4019 }
4020#ifdef FEAT_MBYTE
4021 mb_c = c;
4022 if (enc_utf8 && (*mb_char2len)(c) > 1)
4023 {
4024 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004025 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028 else
4029 mb_utf8 = FALSE;
4030#endif
4031 }
4032 }
4033
4034 /*
4035 * Handling of non-printable characters.
4036 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004037 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
4039 /*
4040 * when getting a character from the file, we may have to
4041 * turn it into something else on the way to putting it
4042 * into "ScreenLines".
4043 */
4044 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4045 {
4046 /* tab amount depends on current column */
4047 n_extra = (int)wp->w_buffer->b_p_ts
4048 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4049#ifdef FEAT_MBYTE
4050 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4051#endif
4052 if (wp->w_p_list)
4053 {
4054 c = lcs_tab1;
4055 c_extra = lcs_tab2;
4056 n_attr = n_extra + 1;
4057 extra_attr = hl_attr(HLF_8);
4058 saved_attr2 = char_attr; /* save current attr */
4059#ifdef FEAT_MBYTE
4060 mb_c = c;
4061 if (enc_utf8 && (*mb_char2len)(c) > 1)
4062 {
4063 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004064 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004065 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067#endif
4068 }
4069 else
4070 {
4071 c_extra = ' ';
4072 c = ' ';
4073 }
4074 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004075 else if (c == NUL
4076 && ((wp->w_p_list && lcs_eol > 0)
4077 || ((fromcol >= 0 || fromcol_prev >= 0)
4078 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004079#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004080 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004081#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004082 && (
4083# ifdef FEAT_RIGHTLEFT
4084 wp->w_p_rl ? (col >= 0) :
4085# endif
4086 (col < W_WIDTH(wp)))
4087 && !(noinvcur
4088 && (colnr_T)vcol == wp->w_virtcol)))
4089 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004091 /* Display a '$' after the line or highlight an extra
4092 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4094 /* For a diff line the highlighting continues after the
4095 * "$". */
4096 if (
4097# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004098 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099# ifdef LINE_ATTR
4100 &&
4101# endif
4102# endif
4103# ifdef LINE_ATTR
4104 line_attr == 0
4105# endif
4106 )
4107#endif
4108 {
4109#ifdef FEAT_VIRTUALEDIT
4110 /* In virtualedit, visual selections may extend
4111 * beyond end of line. */
4112 if (area_highlighting && virtual_active()
4113 && tocol != MAXCOL && vcol < tocol)
4114 n_extra = 0;
4115 else
4116#endif
4117 {
4118 p_extra = at_end_str;
4119 n_extra = 1;
4120 c_extra = NUL;
4121 }
4122 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004123 if (wp->w_p_list)
4124 c = lcs_eol;
4125 else
4126 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 lcs_eol_one = -1;
4128 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004129 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
4131 extra_attr = hl_attr(HLF_AT);
4132 n_attr = 1;
4133 }
4134#ifdef FEAT_MBYTE
4135 mb_c = c;
4136 if (enc_utf8 && (*mb_char2len)(c) > 1)
4137 {
4138 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004139 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004140 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 else
4143 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4144#endif
4145 }
4146 else if (c != NUL)
4147 {
4148 p_extra = transchar(c);
4149#ifdef FEAT_RIGHTLEFT
4150 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4151 rl_mirror(p_extra); /* reverse "<12>" */
4152#endif
4153 n_extra = byte2cells(c) - 1;
4154 c_extra = NUL;
4155 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004156 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 {
4158 n_attr = n_extra + 1;
4159 extra_attr = hl_attr(HLF_8);
4160 saved_attr2 = char_attr; /* save current attr */
4161 }
4162#ifdef FEAT_MBYTE
4163 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4164#endif
4165 }
4166#ifdef FEAT_VIRTUALEDIT
4167 else if (VIsual_active
4168 && (VIsual_mode == Ctrl_V
4169 || VIsual_mode == 'v')
4170 && virtual_active()
4171 && tocol != MAXCOL
4172 && vcol < tocol
4173 && (
4174# ifdef FEAT_RIGHTLEFT
4175 wp->w_p_rl ? (col >= 0) :
4176# endif
4177 (col < W_WIDTH(wp))))
4178 {
4179 c = ' ';
4180 --ptr; /* put it back at the NUL */
4181 }
4182#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004183#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 else if ((
4185# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004186 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 ) && (
4190# ifdef FEAT_RIGHTLEFT
4191 wp->w_p_rl ? (col >= 0) :
4192# endif
4193 (col < W_WIDTH(wp))))
4194 {
4195 /* Highlight until the right side of the window */
4196 c = ' ';
4197 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004198
4199 /* Remember we do the char for line highlighting. */
4200 ++did_line_attr;
4201
4202 /* don't do search HL for the rest of the line */
4203 if (line_attr != 0 && char_attr == search_attr && col > 0)
4204 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205# ifdef FEAT_DIFF
4206 if (diff_hlf == HLF_TXD)
4207 {
4208 diff_hlf = HLF_CHD;
4209 if (attr == 0 || char_attr != attr)
4210 char_attr = hl_attr(diff_hlf);
4211 }
4212# endif
4213 }
4214#endif
4215 }
4216 }
4217
4218 /* Don't override visual selection highlighting. */
4219 if (n_attr > 0
4220 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004221 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 char_attr = extra_attr;
4223
Bram Moolenaar81695252004-12-29 20:58:21 +00004224#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 /* XIM don't send preedit_start and preedit_end, but they send
4226 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4227 * im_is_preediting() here. */
4228 if (xic != NULL
4229 && lnum == curwin->w_cursor.lnum
4230 && (State & INSERT)
4231 && !p_imdisable
4232 && im_is_preediting()
4233 && draw_state == WL_LINE)
4234 {
4235 colnr_T tcol;
4236
4237 if (preedit_end_col == MAXCOL)
4238 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4239 else
4240 tcol = preedit_end_col;
4241 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4242 {
4243 if (feedback_old_attr < 0)
4244 {
4245 feedback_col = 0;
4246 feedback_old_attr = char_attr;
4247 }
4248 char_attr = im_get_feedback_attr(feedback_col);
4249 if (char_attr < 0)
4250 char_attr = feedback_old_attr;
4251 feedback_col++;
4252 }
4253 else if (feedback_old_attr >= 0)
4254 {
4255 char_attr = feedback_old_attr;
4256 feedback_old_attr = -1;
4257 feedback_col = 0;
4258 }
4259 }
4260#endif
4261 /*
4262 * Handle the case where we are in column 0 but not on the first
4263 * character of the line and the user wants us to show us a
4264 * special character (via 'listchars' option "precedes:<char>".
4265 */
4266 if (lcs_prec_todo != NUL
4267 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4268#ifdef FEAT_DIFF
4269 && filler_todo <= 0
4270#endif
4271 && draw_state > WL_NR
4272 && c != NUL)
4273 {
4274 c = lcs_prec;
4275 lcs_prec_todo = NUL;
4276#ifdef FEAT_MBYTE
4277 mb_c = c;
4278 if (enc_utf8 && (*mb_char2len)(c) > 1)
4279 {
4280 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004281 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004282 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 else
4285 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4286#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004287 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 saved_attr3 = char_attr; /* save current attr */
4290 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4291 n_attr3 = 1;
4292 }
4293 }
4294
4295 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004296 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004298 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004299#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004300 || did_line_attr == 1
4301#endif
4302 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004304#ifdef FEAT_SEARCH_EXTRA
4305 long prevcol = (long)(ptr - line) - (c == NUL);
4306#endif
4307
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 /* invert at least one char, used for Visual and empty line or
4309 * highlight match at end of line. If it's beyond the last
4310 * char on the screen, just overwrite that one (tricky!) Not
4311 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004312#ifdef FEAT_SEARCH_EXTRA
4313 prevcol_hl_flag = FALSE;
4314 if (prevcol == (long)search_hl.startcol)
4315 prevcol_hl_flag = TRUE;
4316 else
4317 {
4318 cur = wp->w_match_head;
4319 while (cur != NULL)
4320 {
4321 if (prevcol == (long)cur->hl.startcol)
4322 {
4323 prevcol_hl_flag = TRUE;
4324 break;
4325 }
4326 cur = cur->next;
4327 }
4328 }
4329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004331 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332#ifdef FEAT_SEARCH_EXTRA
4333 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004334 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004335# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004336 && did_line_attr <= 1
4337# endif
4338 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339#endif
4340 ))
4341 {
4342 int n = 0;
4343
4344#ifdef FEAT_RIGHTLEFT
4345 if (wp->w_p_rl)
4346 {
4347 if (col < 0)
4348 n = 1;
4349 }
4350 else
4351#endif
4352 {
4353 if (col >= W_WIDTH(wp))
4354 n = -1;
4355 }
4356 if (n != 0)
4357 {
4358 /* At the window boundary, highlight the last character
4359 * instead (better than nothing). */
4360 off += n;
4361 col += n;
4362 }
4363 else
4364 {
4365 /* Add a blank character to highlight. */
4366 ScreenLines[off] = ' ';
4367#ifdef FEAT_MBYTE
4368 if (enc_utf8)
4369 ScreenLinesUC[off] = 0;
4370#endif
4371 }
4372#ifdef FEAT_SEARCH_EXTRA
4373 if (area_attr == 0)
4374 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004375 /* Use attributes from match with highest priority among
4376 * 'search_hl' and the match list. */
4377 char_attr = search_hl.attr;
4378 cur = wp->w_match_head;
4379 shl_flag = FALSE;
4380 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004381 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004382 if (shl_flag == FALSE
4383 && ((cur != NULL
4384 && cur->priority > SEARCH_HL_PRIORITY)
4385 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004386 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004387 shl = &search_hl;
4388 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004389 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004390 else
4391 shl = &cur->hl;
4392 if ((ptr - line) - 1 == (long)shl->startcol)
4393 char_attr = shl->attr;
4394 if (shl != &search_hl && cur != NULL)
4395 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398#endif
4399 ScreenAttrs[off] = char_attr;
4400#ifdef FEAT_RIGHTLEFT
4401 if (wp->w_p_rl)
4402 --col;
4403 else
4404#endif
4405 ++col;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004406 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
Bram Moolenaar91170f82006-05-05 21:15:17 +00004410 /*
4411 * At end of the text line.
4412 */
4413 if (c == NUL)
4414 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004415#ifdef FEAT_SYN_HL
4416 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004417 if (wp->w_p_wrap)
4418 v = wp->w_skipcol;
4419 else
4420 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004421 /* check if line ends before left margin */
4422 if (vcol < v + col - win_col_off(wp))
4423
4424 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004425 if (wp->w_p_cuc
4426 && (int)wp->w_virtcol >= vcol
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004427 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4428 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004429 && lnum != wp->w_cursor.lnum
4430# ifdef FEAT_RIGHTLEFT
4431 && !wp->w_p_rl
4432# endif
4433 )
4434 {
4435 while (col < W_WIDTH(wp))
4436 {
4437 ScreenLines[off] = ' ';
4438#ifdef FEAT_MBYTE
4439 if (enc_utf8)
4440 ScreenLinesUC[off] = 0;
4441#endif
4442 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004443 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004444 {
4445 ScreenAttrs[off] = hl_attr(HLF_CUC);
4446 break;
4447 }
4448 ScreenAttrs[off++] = 0;
4449 ++vcol;
4450 }
4451 }
4452#endif
4453
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4455 wp->w_p_rl);
4456 row++;
4457
4458 /*
4459 * Update w_cline_height and w_cline_folded if the cursor line was
4460 * updated (saves a call to plines() later).
4461 */
4462 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4463 {
4464 curwin->w_cline_row = startrow;
4465 curwin->w_cline_height = row - startrow;
4466#ifdef FEAT_FOLDING
4467 curwin->w_cline_folded = FALSE;
4468#endif
4469 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4470 }
4471
4472 break;
4473 }
4474
4475 /* line continues beyond line end */
4476 if (lcs_ext
4477 && !wp->w_p_wrap
4478#ifdef FEAT_DIFF
4479 && filler_todo <= 0
4480#endif
4481 && (
4482#ifdef FEAT_RIGHTLEFT
4483 wp->w_p_rl ? col == 0 :
4484#endif
4485 col == W_WIDTH(wp) - 1)
4486 && (*ptr != NUL
4487 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4488 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4489 {
4490 c = lcs_ext;
4491 char_attr = hl_attr(HLF_AT);
4492#ifdef FEAT_MBYTE
4493 mb_c = c;
4494 if (enc_utf8 && (*mb_char2len)(c) > 1)
4495 {
4496 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004498 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 else
4501 mb_utf8 = FALSE;
4502#endif
4503 }
4504
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004505#ifdef FEAT_SYN_HL
4506 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4507 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004508 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004509 && lnum != wp->w_cursor.lnum
4510 && draw_state == WL_LINE)
4511 {
4512 vcol_save_attr = char_attr;
4513 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4514 }
4515 else
4516 vcol_save_attr = -1;
4517#endif
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /*
4520 * Store character to be displayed.
4521 * Skip characters that are left of the screen for 'nowrap'.
4522 */
4523 vcol_prev = vcol;
4524 if (draw_state < WL_LINE || n_skip <= 0)
4525 {
4526 /*
4527 * Store the character.
4528 */
4529#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4530 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4531 {
4532 /* A double-wide character is: put first halve in left cell. */
4533 --off;
4534 --col;
4535 }
4536#endif
4537 ScreenLines[off] = c;
4538#ifdef FEAT_MBYTE
4539 if (enc_dbcs == DBCS_JPNU)
4540 ScreenLines2[off] = mb_c & 0xff;
4541 else if (enc_utf8)
4542 {
4543 if (mb_utf8)
4544 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004545 int i;
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004548 if ((c & 0xff) == 0)
4549 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004550 for (i = 0; i < Screen_mco; ++i)
4551 {
4552 ScreenLinesC[i][off] = u8cc[i];
4553 if (u8cc[i] == 0)
4554 break;
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 else
4558 ScreenLinesUC[off] = 0;
4559 }
4560 if (multi_attr)
4561 {
4562 ScreenAttrs[off] = multi_attr;
4563 multi_attr = 0;
4564 }
4565 else
4566#endif
4567 ScreenAttrs[off] = char_attr;
4568
4569#ifdef FEAT_MBYTE
4570 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4571 {
4572 /* Need to fill two screen columns. */
4573 ++off;
4574 ++col;
4575 if (enc_utf8)
4576 /* UTF-8: Put a 0 in the second screen char. */
4577 ScreenLines[off] = 0;
4578 else
4579 /* DBCS: Put second byte in the second screen char. */
4580 ScreenLines[off] = mb_c & 0xff;
4581 ++vcol;
4582 /* When "tocol" is halfway a character, set it to the end of
4583 * the character, otherwise highlighting won't stop. */
4584 if (tocol == vcol)
4585 ++tocol;
4586#ifdef FEAT_RIGHTLEFT
4587 if (wp->w_p_rl)
4588 {
4589 /* now it's time to backup one cell */
4590 --off;
4591 --col;
4592 }
4593#endif
4594 }
4595#endif
4596#ifdef FEAT_RIGHTLEFT
4597 if (wp->w_p_rl)
4598 {
4599 --off;
4600 --col;
4601 }
4602 else
4603#endif
4604 {
4605 ++off;
4606 ++col;
4607 }
4608 }
4609 else
4610 --n_skip;
4611
4612 /* Only advance the "vcol" when after the 'number' column. */
4613 if (draw_state >= WL_SBR
4614#ifdef FEAT_DIFF
4615 && filler_todo <= 0
4616#endif
4617 )
4618 ++vcol;
4619
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004620#ifdef FEAT_SYN_HL
4621 if (vcol_save_attr >= 0)
4622 char_attr = vcol_save_attr;
4623#endif
4624
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 /* restore attributes after "predeces" in 'listchars' */
4626 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4627 char_attr = saved_attr3;
4628
4629 /* restore attributes after last 'listchars' or 'number' char */
4630 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4631 char_attr = saved_attr2;
4632
4633 /*
4634 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004635 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 */
4637 if ((
4638#ifdef FEAT_RIGHTLEFT
4639 wp->w_p_rl ? (col < 0) :
4640#endif
4641 (col >= W_WIDTH(wp)))
4642 && (*ptr != NUL
4643#ifdef FEAT_DIFF
4644 || filler_todo > 0
4645#endif
4646 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4647 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4648 )
4649 {
4650 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4651 wp->w_p_rl);
4652 ++row;
4653 ++screen_row;
4654
4655 /* When not wrapping and finished diff lines, or when displayed
4656 * '$' and highlighting until last column, break here. */
4657 if ((!wp->w_p_wrap
4658#ifdef FEAT_DIFF
4659 && filler_todo <= 0
4660#endif
4661 ) || lcs_eol_one == -1)
4662 break;
4663
4664 /* When the window is too narrow draw all "@" lines. */
4665 if (draw_state != WL_LINE
4666#ifdef FEAT_DIFF
4667 && filler_todo <= 0
4668#endif
4669 )
4670 {
4671 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4672#ifdef FEAT_VERTSPLIT
4673 draw_vsep_win(wp, row);
4674#endif
4675 row = endrow;
4676 }
4677
4678 /* When line got too long for screen break here. */
4679 if (row == endrow)
4680 {
4681 ++row;
4682 break;
4683 }
4684
4685 if (screen_cur_row == screen_row - 1
4686#ifdef FEAT_DIFF
4687 && filler_todo <= 0
4688#endif
4689 && W_WIDTH(wp) == Columns)
4690 {
4691 /* Remember that the line wraps, used for modeless copy. */
4692 LineWraps[screen_row - 1] = TRUE;
4693
4694 /*
4695 * Special trick to make copy/paste of wrapped lines work with
4696 * xterm/screen: write an extra character beyond the end of
4697 * the line. This will work with all terminal types
4698 * (regardless of the xn,am settings).
4699 * Only do this on a fast tty.
4700 * Only do this if the cursor is on the current line
4701 * (something has been written in it).
4702 * Don't do this for the GUI.
4703 * Don't do this for double-width characters.
4704 * Don't do this for a window not at the right screen border.
4705 */
4706 if (p_tf
4707#ifdef FEAT_GUI
4708 && !gui.in_use
4709#endif
4710#ifdef FEAT_MBYTE
4711 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004712 && ((*mb_off2cells)(LineOffset[screen_row],
4713 LineOffset[screen_row] + screen_Columns)
4714 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004716 + (int)Columns - 2,
4717 LineOffset[screen_row] + screen_Columns)
4718 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719#endif
4720 )
4721 {
4722 /* First make sure we are at the end of the screen line,
4723 * then output the same character again to let the
4724 * terminal know about the wrap. If the terminal doesn't
4725 * auto-wrap, we overwrite the character. */
4726 if (screen_cur_col != W_WIDTH(wp))
4727 screen_char(LineOffset[screen_row - 1]
4728 + (unsigned)Columns - 1,
4729 screen_row - 1, (int)(Columns - 1));
4730
4731#ifdef FEAT_MBYTE
4732 /* When there is a multi-byte character, just output a
4733 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004734 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4735 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 out_char(' ');
4737 else
4738#endif
4739 out_char(ScreenLines[LineOffset[screen_row - 1]
4740 + (Columns - 1)]);
4741 /* force a redraw of the first char on the next line */
4742 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4743 screen_start(); /* don't know where cursor is now */
4744 }
4745 }
4746
4747 col = 0;
4748 off = (unsigned)(current_ScreenLine - ScreenLines);
4749#ifdef FEAT_RIGHTLEFT
4750 if (wp->w_p_rl)
4751 {
4752 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4753 off += col;
4754 }
4755#endif
4756
4757 /* reset the drawing state for the start of a wrapped line */
4758 draw_state = WL_START;
4759 saved_n_extra = n_extra;
4760 saved_p_extra = p_extra;
4761 saved_c_extra = c_extra;
4762 saved_char_attr = char_attr;
4763 n_extra = 0;
4764 lcs_prec_todo = lcs_prec;
4765#ifdef FEAT_LINEBREAK
4766# ifdef FEAT_DIFF
4767 if (filler_todo <= 0)
4768# endif
4769 need_showbreak = TRUE;
4770#endif
4771#ifdef FEAT_DIFF
4772 --filler_todo;
4773 /* When the filler lines are actually below the last line of the
4774 * file, don't draw the line itself, break here. */
4775 if (filler_todo == 0 && wp->w_botfill)
4776 break;
4777#endif
4778 }
4779
4780 } /* for every character in the line */
4781
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004782#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004783 /* After an empty line check first word for capital. */
4784 if (*skipwhite(line) == NUL)
4785 {
4786 capcol_lnum = lnum + 1;
4787 cap_col = 0;
4788 }
4789#endif
4790
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 return row;
4792}
4793
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004794#ifdef FEAT_MBYTE
4795static int comp_char_differs __ARGS((int, int));
4796
4797/*
4798 * Return if the composing characters at "off_from" and "off_to" differ.
4799 */
4800 static int
4801comp_char_differs(off_from, off_to)
4802 int off_from;
4803 int off_to;
4804{
4805 int i;
4806
4807 for (i = 0; i < Screen_mco; ++i)
4808 {
4809 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4810 return TRUE;
4811 if (ScreenLinesC[i][off_from] == 0)
4812 break;
4813 }
4814 return FALSE;
4815}
4816#endif
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818/*
4819 * Check whether the given character needs redrawing:
4820 * - the (first byte of the) character is different
4821 * - the attributes are different
4822 * - the character is multi-byte and the next byte is different
4823 */
4824 static int
4825char_needs_redraw(off_from, off_to, cols)
4826 int off_from;
4827 int off_to;
4828 int cols;
4829{
4830 if (cols > 0
4831 && ((ScreenLines[off_from] != ScreenLines[off_to]
4832 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4833
4834#ifdef FEAT_MBYTE
4835 || (enc_dbcs != 0
4836 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4837 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4838 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4839 : (cols > 1 && ScreenLines[off_from + 1]
4840 != ScreenLines[off_to + 1])))
4841 || (enc_utf8
4842 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4843 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004844 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#endif
4846 ))
4847 return TRUE;
4848 return FALSE;
4849}
4850
4851/*
4852 * Move one "cooked" screen line to the screen, but only the characters that
4853 * have actually changed. Handle insert/delete character.
4854 * "coloff" gives the first column on the screen for this line.
4855 * "endcol" gives the columns where valid characters are.
4856 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4857 * needs to be cleared, negative otherwise.
4858 * "rlflag" is TRUE in a rightleft window:
4859 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4860 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4861 */
4862 static void
4863screen_line(row, coloff, endcol, clear_width
4864#ifdef FEAT_RIGHTLEFT
4865 , rlflag
4866#endif
4867 )
4868 int row;
4869 int coloff;
4870 int endcol;
4871 int clear_width;
4872#ifdef FEAT_RIGHTLEFT
4873 int rlflag;
4874#endif
4875{
4876 unsigned off_from;
4877 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004878#ifdef FEAT_MBYTE
4879 unsigned max_off_from;
4880 unsigned max_off_to;
4881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 int col = 0;
4883#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4884 int hl;
4885#endif
4886 int force = FALSE; /* force update rest of the line */
4887 int redraw_this /* bool: does character need redraw? */
4888#ifdef FEAT_GUI
4889 = TRUE /* For GUI when while-loop empty */
4890#endif
4891 ;
4892 int redraw_next; /* redraw_this for next character */
4893#ifdef FEAT_MBYTE
4894 int clear_next = FALSE;
4895 int char_cells; /* 1: normal char */
4896 /* 2: occupies two display cells */
4897# define CHAR_CELLS char_cells
4898#else
4899# define CHAR_CELLS 1
4900#endif
4901
4902# ifdef FEAT_CLIPBOARD
4903 clip_may_clear_selection(row, row);
4904# endif
4905
4906 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4907 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004908#ifdef FEAT_MBYTE
4909 max_off_from = off_from + screen_Columns;
4910 max_off_to = LineOffset[row] + screen_Columns;
4911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
4913#ifdef FEAT_RIGHTLEFT
4914 if (rlflag)
4915 {
4916 /* Clear rest first, because it's left of the text. */
4917 if (clear_width > 0)
4918 {
4919 while (col <= endcol && ScreenLines[off_to] == ' '
4920 && ScreenAttrs[off_to] == 0
4921# ifdef FEAT_MBYTE
4922 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4923# endif
4924 )
4925 {
4926 ++off_to;
4927 ++col;
4928 }
4929 if (col <= endcol)
4930 screen_fill(row, row + 1, col + coloff,
4931 endcol + coloff + 1, ' ', ' ', 0);
4932 }
4933 col = endcol + 1;
4934 off_to = LineOffset[row] + col + coloff;
4935 off_from += col;
4936 endcol = (clear_width > 0 ? clear_width : -clear_width);
4937 }
4938#endif /* FEAT_RIGHTLEFT */
4939
4940 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4941
4942 while (col < endcol)
4943 {
4944#ifdef FEAT_MBYTE
4945 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00004946 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
4948 char_cells = 1;
4949#endif
4950
4951 redraw_this = redraw_next;
4952 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4953 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4954
4955#ifdef FEAT_GUI
4956 /* If the next character was bold, then redraw the current character to
4957 * remove any pixels that might have spilt over into us. This only
4958 * happens in the GUI.
4959 */
4960 if (redraw_next && gui.in_use)
4961 {
4962 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004963 if (hl > HL_ALL)
4964 hl = syn_attr2attr(hl);
4965 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 redraw_this = TRUE;
4967 }
4968#endif
4969
4970 if (redraw_this)
4971 {
4972 /*
4973 * Special handling when 'xs' termcap flag set (hpterm):
4974 * Attributes for characters are stored at the position where the
4975 * cursor is when writing the highlighting code. The
4976 * start-highlighting code must be written with the cursor on the
4977 * first highlighted character. The stop-highlighting code must
4978 * be written with the cursor just after the last highlighted
4979 * character.
4980 * Overwriting a character doesn't remove it's highlighting. Need
4981 * to clear the rest of the line, and force redrawing it
4982 * completely.
4983 */
4984 if ( p_wiv
4985 && !force
4986#ifdef FEAT_GUI
4987 && !gui.in_use
4988#endif
4989 && ScreenAttrs[off_to] != 0
4990 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4991 {
4992 /*
4993 * Need to remove highlighting attributes here.
4994 */
4995 windgoto(row, col + coloff);
4996 out_str(T_CE); /* clear rest of this screen line */
4997 screen_start(); /* don't know where cursor is now */
4998 force = TRUE; /* force redraw of rest of the line */
4999 redraw_next = TRUE; /* or else next char would miss out */
5000
5001 /*
5002 * If the previous character was highlighted, need to stop
5003 * highlighting at this character.
5004 */
5005 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5006 {
5007 screen_attr = ScreenAttrs[off_to - 1];
5008 term_windgoto(row, col + coloff);
5009 screen_stop_highlight();
5010 }
5011 else
5012 screen_attr = 0; /* highlighting has stopped */
5013 }
5014#ifdef FEAT_MBYTE
5015 if (enc_dbcs != 0)
5016 {
5017 /* Check if overwriting a double-byte with a single-byte or
5018 * the other way around requires another character to be
5019 * redrawn. For UTF-8 this isn't needed, because comparing
5020 * ScreenLinesUC[] is sufficient. */
5021 if (char_cells == 1
5022 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005023 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
5025 /* Writing a single-cell character over a double-cell
5026 * character: need to redraw the next cell. */
5027 ScreenLines[off_to + 1] = 0;
5028 redraw_next = TRUE;
5029 }
5030 else if (char_cells == 2
5031 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005032 && (*mb_off2cells)(off_to, max_off_to) == 1
5033 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 /* Writing the second half of a double-cell character over
5036 * a double-cell character: need to redraw the second
5037 * cell. */
5038 ScreenLines[off_to + 2] = 0;
5039 redraw_next = TRUE;
5040 }
5041
5042 if (enc_dbcs == DBCS_JPNU)
5043 ScreenLines2[off_to] = ScreenLines2[off_from];
5044 }
5045 /* When writing a single-width character over a double-width
5046 * character and at the end of the redrawn text, need to clear out
5047 * the right halve of the old character.
5048 * Also required when writing the right halve of a double-width
5049 * char over the left halve of an existing one. */
5050 if (has_mbyte && col + char_cells == endcol
5051 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005052 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005054 && (*mb_off2cells)(off_to, max_off_to) == 1
5055 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 clear_next = TRUE;
5057#endif
5058
5059 ScreenLines[off_to] = ScreenLines[off_from];
5060#ifdef FEAT_MBYTE
5061 if (enc_utf8)
5062 {
5063 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5064 if (ScreenLinesUC[off_from] != 0)
5065 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005066 int i;
5067
5068 for (i = 0; i < Screen_mco; ++i)
5069 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 }
5072 if (char_cells == 2)
5073 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5074#endif
5075
5076#if defined(FEAT_GUI) || defined(UNIX)
5077 /* The bold trick makes a single row of pixels appear in the next
5078 * character. When a bold character is removed, the next
5079 * character should be redrawn too. This happens for our own GUI
5080 * and for some xterms. */
5081 if (
5082# ifdef FEAT_GUI
5083 gui.in_use
5084# endif
5085# if defined(FEAT_GUI) && defined(UNIX)
5086 ||
5087# endif
5088# ifdef UNIX
5089 term_is_xterm
5090# endif
5091 )
5092 {
5093 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005094 if (hl > HL_ALL)
5095 hl = syn_attr2attr(hl);
5096 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 redraw_next = TRUE;
5098 }
5099#endif
5100 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5101#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005102 /* For simplicity set the attributes of second half of a
5103 * double-wide character equal to the first half. */
5104 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005106
5107 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 else
5110#endif
5111 screen_char(off_to, row, col + coloff);
5112 }
5113 else if ( p_wiv
5114#ifdef FEAT_GUI
5115 && !gui.in_use
5116#endif
5117 && col + coloff > 0)
5118 {
5119 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5120 {
5121 /*
5122 * Don't output stop-highlight when moving the cursor, it will
5123 * stop the highlighting when it should continue.
5124 */
5125 screen_attr = 0;
5126 }
5127 else if (screen_attr != 0)
5128 screen_stop_highlight();
5129 }
5130
5131 off_to += CHAR_CELLS;
5132 off_from += CHAR_CELLS;
5133 col += CHAR_CELLS;
5134 }
5135
5136#ifdef FEAT_MBYTE
5137 if (clear_next)
5138 {
5139 /* Clear the second half of a double-wide character of which the left
5140 * half was overwritten with a single-wide character. */
5141 ScreenLines[off_to] = ' ';
5142 if (enc_utf8)
5143 ScreenLinesUC[off_to] = 0;
5144 screen_char(off_to, row, col + coloff);
5145 }
5146#endif
5147
5148 if (clear_width > 0
5149#ifdef FEAT_RIGHTLEFT
5150 && !rlflag
5151#endif
5152 )
5153 {
5154#ifdef FEAT_GUI
5155 int startCol = col;
5156#endif
5157
5158 /* blank out the rest of the line */
5159 while (col < clear_width && ScreenLines[off_to] == ' '
5160 && ScreenAttrs[off_to] == 0
5161#ifdef FEAT_MBYTE
5162 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5163#endif
5164 )
5165 {
5166 ++off_to;
5167 ++col;
5168 }
5169 if (col < clear_width)
5170 {
5171#ifdef FEAT_GUI
5172 /*
5173 * In the GUI, clearing the rest of the line may leave pixels
5174 * behind if the first character cleared was bold. Some bold
5175 * fonts spill over the left. In this case we redraw the previous
5176 * character too. If we didn't skip any blanks above, then we
5177 * only redraw if the character wasn't already redrawn anyway.
5178 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005179 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
5181 hl = ScreenAttrs[off_to];
5182 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005183 {
5184 int prev_cells = 1;
5185# ifdef FEAT_MBYTE
5186 if (enc_utf8)
5187 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5188 * that its width is 2. */
5189 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5190 else if (enc_dbcs != 0)
5191 {
5192 /* find previous character by counting from first
5193 * column and get its width. */
5194 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005195 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005196
5197 while (off < off_to)
5198 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005199 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005200 off += prev_cells;
5201 }
5202 }
5203
5204 if (enc_dbcs != 0 && prev_cells > 1)
5205 screen_char_2(off_to - prev_cells, row,
5206 col + coloff - prev_cells);
5207 else
5208# endif
5209 screen_char(off_to - prev_cells, row,
5210 col + coloff - prev_cells);
5211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213#endif
5214 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5215 ' ', ' ', 0);
5216#ifdef FEAT_VERTSPLIT
5217 off_to += clear_width - col;
5218 col = clear_width;
5219#endif
5220 }
5221 }
5222
5223 if (clear_width > 0)
5224 {
5225#ifdef FEAT_VERTSPLIT
5226 /* For a window that's left of another, draw the separator char. */
5227 if (col + coloff < Columns)
5228 {
5229 int c;
5230
5231 c = fillchar_vsep(&hl);
5232 if (ScreenLines[off_to] != c
5233# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005234 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5235 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236# endif
5237 || ScreenAttrs[off_to] != hl)
5238 {
5239 ScreenLines[off_to] = c;
5240 ScreenAttrs[off_to] = hl;
5241# ifdef FEAT_MBYTE
5242 if (enc_utf8)
5243 {
5244 if (c >= 0x80)
5245 {
5246 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005247 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249 else
5250 ScreenLinesUC[off_to] = 0;
5251 }
5252# endif
5253 screen_char(off_to, row, col + coloff);
5254 }
5255 }
5256 else
5257#endif
5258 LineWraps[row] = FALSE;
5259 }
5260}
5261
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005262#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005264 * Mirror text "str" for right-left displaying.
5265 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005267 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268rl_mirror(str)
5269 char_u *str;
5270{
5271 char_u *p1, *p2;
5272 int t;
5273
5274 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5275 {
5276 t = *p1;
5277 *p1 = *p2;
5278 *p2 = t;
5279 }
5280}
5281#endif
5282
5283#if defined(FEAT_WINDOWS) || defined(PROTO)
5284/*
5285 * mark all status lines for redraw; used after first :cd
5286 */
5287 void
5288status_redraw_all()
5289{
5290 win_T *wp;
5291
5292 for (wp = firstwin; wp; wp = wp->w_next)
5293 if (wp->w_status_height)
5294 {
5295 wp->w_redr_status = TRUE;
5296 redraw_later(VALID);
5297 }
5298}
5299
5300/*
5301 * mark all status lines of the current buffer for redraw
5302 */
5303 void
5304status_redraw_curbuf()
5305{
5306 win_T *wp;
5307
5308 for (wp = firstwin; wp; wp = wp->w_next)
5309 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5310 {
5311 wp->w_redr_status = TRUE;
5312 redraw_later(VALID);
5313 }
5314}
5315
5316/*
5317 * Redraw all status lines that need to be redrawn.
5318 */
5319 void
5320redraw_statuslines()
5321{
5322 win_T *wp;
5323
5324 for (wp = firstwin; wp; wp = wp->w_next)
5325 if (wp->w_redr_status)
5326 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005327 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005328 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329}
5330#endif
5331
5332#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5333/*
5334 * Redraw all status lines at the bottom of frame "frp".
5335 */
5336 void
5337win_redraw_last_status(frp)
5338 frame_T *frp;
5339{
5340 if (frp->fr_layout == FR_LEAF)
5341 frp->fr_win->w_redr_status = TRUE;
5342 else if (frp->fr_layout == FR_ROW)
5343 {
5344 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5345 win_redraw_last_status(frp);
5346 }
5347 else /* frp->fr_layout == FR_COL */
5348 {
5349 frp = frp->fr_child;
5350 while (frp->fr_next != NULL)
5351 frp = frp->fr_next;
5352 win_redraw_last_status(frp);
5353 }
5354}
5355#endif
5356
5357#ifdef FEAT_VERTSPLIT
5358/*
5359 * Draw the verticap separator right of window "wp" starting with line "row".
5360 */
5361 static void
5362draw_vsep_win(wp, row)
5363 win_T *wp;
5364 int row;
5365{
5366 int hl;
5367 int c;
5368
5369 if (wp->w_vsep_width)
5370 {
5371 /* draw the vertical separator right of this window */
5372 c = fillchar_vsep(&hl);
5373 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5374 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5375 c, ' ', hl);
5376 }
5377}
5378#endif
5379
5380#ifdef FEAT_WILDMENU
5381static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005382static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
5384/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005385 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 */
5387 static int
5388status_match_len(xp, s)
5389 expand_T *xp;
5390 char_u *s;
5391{
5392 int len = 0;
5393
5394#ifdef FEAT_MENU
5395 int emenu = (xp->xp_context == EXPAND_MENUS
5396 || xp->xp_context == EXPAND_MENUNAMES);
5397
5398 /* Check for menu separators - replace with '|'. */
5399 if (emenu && menu_is_separator(s))
5400 return 1;
5401#endif
5402
5403 while (*s != NUL)
5404 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005405 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005407 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005408 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410
5411 return len;
5412}
5413
5414/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005415 * Return TRUE for characters that are not displayed in a status match.
5416 * These are backslashes used for escaping. Do show backslashes in help tags.
5417 */
5418 static int
5419skip_status_match_char(xp, s)
5420 expand_T *xp;
5421 char_u *s;
5422{
5423 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5424#ifdef FEAT_MENU
5425 || ((xp->xp_context == EXPAND_MENUS
5426 || xp->xp_context == EXPAND_MENUNAMES)
5427 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5428#endif
5429 );
5430}
5431
5432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 * Show wildchar matches in the status line.
5434 * Show at least the "match" item.
5435 * We start at item 'first_match' in the list and show all matches that fit.
5436 *
5437 * If inversion is possible we use it. Else '=' characters are used.
5438 */
5439 void
5440win_redr_status_matches(xp, num_matches, matches, match, showtail)
5441 expand_T *xp;
5442 int num_matches;
5443 char_u **matches; /* list of matches */
5444 int match;
5445 int showtail;
5446{
5447#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5448 int row;
5449 char_u *buf;
5450 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005451 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 int fillchar;
5453 int attr;
5454 int i;
5455 int highlight = TRUE;
5456 char_u *selstart = NULL;
5457 int selstart_col = 0;
5458 char_u *selend = NULL;
5459 static int first_match = 0;
5460 int add_left = FALSE;
5461 char_u *s;
5462#ifdef FEAT_MENU
5463 int emenu;
5464#endif
5465#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5466 int l;
5467#endif
5468
5469 if (matches == NULL) /* interrupted completion? */
5470 return;
5471
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005472#ifdef FEAT_MBYTE
5473 if (has_mbyte)
5474 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5475 else
5476#endif
5477 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 if (buf == NULL)
5479 return;
5480
5481 if (match == -1) /* don't show match but original text */
5482 {
5483 match = 0;
5484 highlight = FALSE;
5485 }
5486 /* count 1 for the ending ">" */
5487 clen = status_match_len(xp, L_MATCH(match)) + 3;
5488 if (match == 0)
5489 first_match = 0;
5490 else if (match < first_match)
5491 {
5492 /* jumping left, as far as we can go */
5493 first_match = match;
5494 add_left = TRUE;
5495 }
5496 else
5497 {
5498 /* check if match fits on the screen */
5499 for (i = first_match; i < match; ++i)
5500 clen += status_match_len(xp, L_MATCH(i)) + 2;
5501 if (first_match > 0)
5502 clen += 2;
5503 /* jumping right, put match at the left */
5504 if ((long)clen > Columns)
5505 {
5506 first_match = match;
5507 /* if showing the last match, we can add some on the left */
5508 clen = 2;
5509 for (i = match; i < num_matches; ++i)
5510 {
5511 clen += status_match_len(xp, L_MATCH(i)) + 2;
5512 if ((long)clen >= Columns)
5513 break;
5514 }
5515 if (i == num_matches)
5516 add_left = TRUE;
5517 }
5518 }
5519 if (add_left)
5520 while (first_match > 0)
5521 {
5522 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5523 if ((long)clen >= Columns)
5524 break;
5525 --first_match;
5526 }
5527
5528 fillchar = fillchar_status(&attr, TRUE);
5529
5530 if (first_match == 0)
5531 {
5532 *buf = NUL;
5533 len = 0;
5534 }
5535 else
5536 {
5537 STRCPY(buf, "< ");
5538 len = 2;
5539 }
5540 clen = len;
5541
5542 i = first_match;
5543 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5544 {
5545 if (i == match)
5546 {
5547 selstart = buf + len;
5548 selstart_col = clen;
5549 }
5550
5551 s = L_MATCH(i);
5552 /* Check for menu separators - replace with '|' */
5553#ifdef FEAT_MENU
5554 emenu = (xp->xp_context == EXPAND_MENUS
5555 || xp->xp_context == EXPAND_MENUNAMES);
5556 if (emenu && menu_is_separator(s))
5557 {
5558 STRCPY(buf + len, transchar('|'));
5559 l = (int)STRLEN(buf + len);
5560 len += l;
5561 clen += l;
5562 }
5563 else
5564#endif
5565 for ( ; *s != NUL; ++s)
5566 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005567 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 ++s;
5569 clen += ptr2cells(s);
5570#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005571 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
5573 STRNCPY(buf + len, s, l);
5574 s += l - 1;
5575 len += l;
5576 }
5577 else
5578#endif
5579 {
5580 STRCPY(buf + len, transchar_byte(*s));
5581 len += (int)STRLEN(buf + len);
5582 }
5583 }
5584 if (i == match)
5585 selend = buf + len;
5586
5587 *(buf + len++) = ' ';
5588 *(buf + len++) = ' ';
5589 clen += 2;
5590 if (++i == num_matches)
5591 break;
5592 }
5593
5594 if (i != num_matches)
5595 {
5596 *(buf + len++) = '>';
5597 ++clen;
5598 }
5599
5600 buf[len] = NUL;
5601
5602 row = cmdline_row - 1;
5603 if (row >= 0)
5604 {
5605 if (wild_menu_showing == 0)
5606 {
5607 if (msg_scrolled > 0)
5608 {
5609 /* Put the wildmenu just above the command line. If there is
5610 * no room, scroll the screen one line up. */
5611 if (cmdline_row == Rows - 1)
5612 {
5613 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5614 ++msg_scrolled;
5615 }
5616 else
5617 {
5618 ++cmdline_row;
5619 ++row;
5620 }
5621 wild_menu_showing = WM_SCROLLED;
5622 }
5623 else
5624 {
5625 /* Create status line if needed by setting 'laststatus' to 2.
5626 * Set 'winminheight' to zero to avoid that the window is
5627 * resized. */
5628 if (lastwin->w_status_height == 0)
5629 {
5630 save_p_ls = p_ls;
5631 save_p_wmh = p_wmh;
5632 p_ls = 2;
5633 p_wmh = 0;
5634 last_status(FALSE);
5635 }
5636 wild_menu_showing = WM_SHOWN;
5637 }
5638 }
5639
5640 screen_puts(buf, row, 0, attr);
5641 if (selstart != NULL && highlight)
5642 {
5643 *selend = NUL;
5644 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5645 }
5646
5647 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5648 }
5649
5650#ifdef FEAT_VERTSPLIT
5651 win_redraw_last_status(topframe);
5652#else
5653 lastwin->w_redr_status = TRUE;
5654#endif
5655 vim_free(buf);
5656}
5657#endif
5658
5659#if defined(FEAT_WINDOWS) || defined(PROTO)
5660/*
5661 * Redraw the status line of window wp.
5662 *
5663 * If inversion is possible we use it. Else '=' characters are used.
5664 */
5665 void
5666win_redr_status(wp)
5667 win_T *wp;
5668{
5669 int row;
5670 char_u *p;
5671 int len;
5672 int fillchar;
5673 int attr;
5674 int this_ru_col;
5675
5676 wp->w_redr_status = FALSE;
5677 if (wp->w_status_height == 0)
5678 {
5679 /* no status line, can only be last window */
5680 redraw_cmdline = TRUE;
5681 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005682 else if (!redrawing()
5683#ifdef FEAT_INS_EXPAND
5684 /* don't update status line when popup menu is visible and may be
5685 * drawn over it */
5686 || pum_visible()
5687#endif
5688 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
5690 /* Don't redraw right now, do it later. */
5691 wp->w_redr_status = TRUE;
5692 }
5693#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005694 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {
5696 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005697 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699#endif
5700 else
5701 {
5702 fillchar = fillchar_status(&attr, wp == curwin);
5703
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005704 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 p = NameBuff;
5706 len = (int)STRLEN(p);
5707
5708 if (wp->w_buffer->b_help
5709#ifdef FEAT_QUICKFIX
5710 || wp->w_p_pvw
5711#endif
5712 || bufIsChanged(wp->w_buffer)
5713 || wp->w_buffer->b_p_ro)
5714 *(p + len++) = ' ';
5715 if (wp->w_buffer->b_help)
5716 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005717 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 len += (int)STRLEN(p + len);
5719 }
5720#ifdef FEAT_QUICKFIX
5721 if (wp->w_p_pvw)
5722 {
5723 STRCPY(p + len, _("[Preview]"));
5724 len += (int)STRLEN(p + len);
5725 }
5726#endif
5727 if (bufIsChanged(wp->w_buffer))
5728 {
5729 STRCPY(p + len, "[+]");
5730 len += 3;
5731 }
5732 if (wp->w_buffer->b_p_ro)
5733 {
5734 STRCPY(p + len, "[RO]");
5735 len += 4;
5736 }
5737
5738#ifndef FEAT_VERTSPLIT
5739 this_ru_col = ru_col;
5740 if (this_ru_col < (Columns + 1) / 2)
5741 this_ru_col = (Columns + 1) / 2;
5742#else
5743 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5744 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5745 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5746 if (this_ru_col <= 1)
5747 {
5748 p = (char_u *)"<"; /* No room for file name! */
5749 len = 1;
5750 }
5751 else
5752#endif
5753#ifdef FEAT_MBYTE
5754 if (has_mbyte)
5755 {
5756 int clen = 0, i;
5757
5758 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005759 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 clen += (*mb_ptr2cells)(p + i);
5761 /* Find first character that will fit.
5762 * Going from start to end is much faster for DBCS. */
5763 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005764 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 clen -= (*mb_ptr2cells)(p + i);
5766 len = clen;
5767 if (i > 0)
5768 {
5769 p = p + i - 1;
5770 *p = '<';
5771 ++len;
5772 }
5773
5774 }
5775 else
5776#endif
5777 if (len > this_ru_col - 1)
5778 {
5779 p += len - (this_ru_col - 1);
5780 *p = '<';
5781 len = this_ru_col - 1;
5782 }
5783
5784 row = W_WINROW(wp) + wp->w_height;
5785 screen_puts(p, row, W_WINCOL(wp), attr);
5786 screen_fill(row, row + 1, len + W_WINCOL(wp),
5787 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5788
5789 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5790 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5791 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5792 - 1 + W_WINCOL(wp)), attr);
5793
5794#ifdef FEAT_CMDL_INFO
5795 win_redr_ruler(wp, TRUE);
5796#endif
5797 }
5798
5799#ifdef FEAT_VERTSPLIT
5800 /*
5801 * May need to draw the character below the vertical separator.
5802 */
5803 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5804 {
5805 if (stl_connected(wp))
5806 fillchar = fillchar_status(&attr, wp == curwin);
5807 else
5808 fillchar = fillchar_vsep(&attr);
5809 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5810 attr);
5811 }
5812#endif
5813}
5814
Bram Moolenaar238a5642006-02-21 22:12:05 +00005815#ifdef FEAT_STL_OPT
5816/*
5817 * Redraw the status line according to 'statusline' and take care of any
5818 * errors encountered.
5819 */
5820 static void
5821redraw_custum_statusline(wp)
5822 win_T *wp;
5823{
5824 int save_called_emsg = called_emsg;
5825
5826 called_emsg = FALSE;
5827 win_redr_custom(wp, FALSE);
5828 if (called_emsg)
5829 set_string_option_direct((char_u *)"statusline", -1,
5830 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005831 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005832 called_emsg |= save_called_emsg;
5833}
5834#endif
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836# ifdef FEAT_VERTSPLIT
5837/*
5838 * Return TRUE if the status line of window "wp" is connected to the status
5839 * line of the window right of it. If not, then it's a vertical separator.
5840 * Only call if (wp->w_vsep_width != 0).
5841 */
5842 int
5843stl_connected(wp)
5844 win_T *wp;
5845{
5846 frame_T *fr;
5847
5848 fr = wp->w_frame;
5849 while (fr->fr_parent != NULL)
5850 {
5851 if (fr->fr_parent->fr_layout == FR_COL)
5852 {
5853 if (fr->fr_next != NULL)
5854 break;
5855 }
5856 else
5857 {
5858 if (fr->fr_next != NULL)
5859 return TRUE;
5860 }
5861 fr = fr->fr_parent;
5862 }
5863 return FALSE;
5864}
5865# endif
5866
5867#endif /* FEAT_WINDOWS */
5868
5869#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5870/*
5871 * Get the value to show for the language mappings, active 'keymap'.
5872 */
5873 int
5874get_keymap_str(wp, buf, len)
5875 win_T *wp;
5876 char_u *buf; /* buffer for the result */
5877 int len; /* length of buffer */
5878{
5879 char_u *p;
5880
5881 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5882 return FALSE;
5883
5884 {
5885#ifdef FEAT_EVAL
5886 buf_T *old_curbuf = curbuf;
5887 win_T *old_curwin = curwin;
5888 char_u *s;
5889
5890 curbuf = wp->w_buffer;
5891 curwin = wp;
5892 STRCPY(buf, "b:keymap_name"); /* must be writable */
5893 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005894 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 --emsg_skip;
5896 curbuf = old_curbuf;
5897 curwin = old_curwin;
5898 if (p == NULL || *p == NUL)
5899#endif
5900 {
5901#ifdef FEAT_KEYMAP
5902 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5903 p = wp->w_buffer->b_p_keymap;
5904 else
5905#endif
5906 p = (char_u *)"lang";
5907 }
5908 if ((int)(STRLEN(p) + 3) < len)
5909 sprintf((char *)buf, "<%s>", p);
5910 else
5911 buf[0] = NUL;
5912#ifdef FEAT_EVAL
5913 vim_free(s);
5914#endif
5915 }
5916 return buf[0] != NUL;
5917}
5918#endif
5919
5920#if defined(FEAT_STL_OPT) || defined(PROTO)
5921/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005922 * Redraw the status line or ruler of window "wp".
5923 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 */
5925 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005926win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005928 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 int attr;
5931 int curattr;
5932 int row;
5933 int col = 0;
5934 int maxwidth;
5935 int width;
5936 int n;
5937 int len;
5938 int fillchar;
5939 char_u buf[MAXPATHL];
5940 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005941 struct stl_hlrec hltab[STL_MAX_ITEM];
5942 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005943 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005946 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005948 /* Use 'tabline'. Always at the first line of the screen. */
5949 p = p_tal;
5950 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005951 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005952 attr = hl_attr(HLF_TPF);
5953 maxwidth = Columns;
5954# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005955 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005958 else
5959 {
5960 row = W_WINROW(wp) + wp->w_height;
5961 fillchar = fillchar_status(&attr, wp == curwin);
5962 maxwidth = W_WIDTH(wp);
5963
5964 if (draw_ruler)
5965 {
5966 p = p_ruf;
5967 /* advance past any leading group spec - implicit in ru_col */
5968 if (*p == '%')
5969 {
5970 if (*++p == '-')
5971 p++;
5972 if (atoi((char *) p))
5973 while (VIM_ISDIGIT(*p))
5974 p++;
5975 if (*p++ != '(')
5976 p = p_ruf;
5977 }
5978#ifdef FEAT_VERTSPLIT
5979 col = ru_col - (Columns - W_WIDTH(wp));
5980 if (col < (W_WIDTH(wp) + 1) / 2)
5981 col = (W_WIDTH(wp) + 1) / 2;
5982#else
5983 col = ru_col;
5984 if (col > (Columns + 1) / 2)
5985 col = (Columns + 1) / 2;
5986#endif
5987 maxwidth = W_WIDTH(wp) - col;
5988#ifdef FEAT_WINDOWS
5989 if (!wp->w_status_height)
5990#endif
5991 {
5992 row = Rows - 1;
5993 --maxwidth; /* writing in last column may cause scrolling */
5994 fillchar = ' ';
5995 attr = 0;
5996 }
5997
5998# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005999 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006000# endif
6001 }
6002 else
6003 {
6004 if (*wp->w_p_stl != NUL)
6005 p = wp->w_p_stl;
6006 else
6007 p = p_stl;
6008# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006009 use_sandbox = was_set_insecurely((char_u *)"statusline",
6010 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006011# endif
6012 }
6013
6014#ifdef FEAT_VERTSPLIT
6015 col += W_WINCOL(wp);
6016#endif
6017 }
6018
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (maxwidth <= 0)
6020 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006022 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6023 buf, sizeof(buf),
6024 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006025 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006026 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027
6028 while (width < maxwidth && len < sizeof(buf) - 1)
6029 {
6030#ifdef FEAT_MBYTE
6031 len += (*mb_char2bytes)(fillchar, buf + len);
6032#else
6033 buf[len++] = fillchar;
6034#endif
6035 ++width;
6036 }
6037 buf[len] = NUL;
6038
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006039 /*
6040 * Draw each snippet with the specified highlighting.
6041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 curattr = attr;
6043 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006044 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006046 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 screen_puts_len(p, len, row, col, curattr);
6048 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006049 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006051 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006053 else if (hltab[n].userhl < 0)
6054 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006056 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006057 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058#endif
6059 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006060 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 }
6062 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006063
6064 if (wp == NULL)
6065 {
6066 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6067 col = 0;
6068 len = 0;
6069 p = buf;
6070 fillchar = 0;
6071 for (n = 0; tabtab[n].start != NULL; n++)
6072 {
6073 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6074 while (col < len)
6075 TabPageIdxs[col++] = fillchar;
6076 p = tabtab[n].start;
6077 fillchar = tabtab[n].userhl;
6078 }
6079 while (col < Columns)
6080 TabPageIdxs[col++] = fillchar;
6081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082}
6083
6084#endif /* FEAT_STL_OPT */
6085
6086/*
6087 * Output a single character directly to the screen and update ScreenLines.
6088 */
6089 void
6090screen_putchar(c, row, col, attr)
6091 int c;
6092 int row, col;
6093 int attr;
6094{
6095#ifdef FEAT_MBYTE
6096 char_u buf[MB_MAXBYTES + 1];
6097
6098 buf[(*mb_char2bytes)(c, buf)] = NUL;
6099#else
6100 char_u buf[2];
6101
6102 buf[0] = c;
6103 buf[1] = NUL;
6104#endif
6105 screen_puts(buf, row, col, attr);
6106}
6107
6108/*
6109 * Get a single character directly from ScreenLines into "bytes[]".
6110 * Also return its attribute in *attrp;
6111 */
6112 void
6113screen_getbytes(row, col, bytes, attrp)
6114 int row, col;
6115 char_u *bytes;
6116 int *attrp;
6117{
6118 unsigned off;
6119
6120 /* safety check */
6121 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6122 {
6123 off = LineOffset[row] + col;
6124 *attrp = ScreenAttrs[off];
6125 bytes[0] = ScreenLines[off];
6126 bytes[1] = NUL;
6127
6128#ifdef FEAT_MBYTE
6129 if (enc_utf8 && ScreenLinesUC[off] != 0)
6130 bytes[utfc_char2bytes(off, bytes)] = NUL;
6131 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6132 {
6133 bytes[0] = ScreenLines[off];
6134 bytes[1] = ScreenLines2[off];
6135 bytes[2] = NUL;
6136 }
6137 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6138 {
6139 bytes[1] = ScreenLines[off + 1];
6140 bytes[2] = NUL;
6141 }
6142#endif
6143 }
6144}
6145
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006146#ifdef FEAT_MBYTE
6147static int screen_comp_differs __ARGS((int, int*));
6148
6149/*
6150 * Return TRUE if composing characters for screen posn "off" differs from
6151 * composing characters in "u8cc".
6152 */
6153 static int
6154screen_comp_differs(off, u8cc)
6155 int off;
6156 int *u8cc;
6157{
6158 int i;
6159
6160 for (i = 0; i < Screen_mco; ++i)
6161 {
6162 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6163 return TRUE;
6164 if (u8cc[i] == 0)
6165 break;
6166 }
6167 return FALSE;
6168}
6169#endif
6170
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171/*
6172 * Put string '*text' on the screen at position 'row' and 'col', with
6173 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6174 * Note: only outputs within one row, message is truncated at screen boundary!
6175 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6176 */
6177 void
6178screen_puts(text, row, col, attr)
6179 char_u *text;
6180 int row;
6181 int col;
6182 int attr;
6183{
6184 screen_puts_len(text, -1, row, col, attr);
6185}
6186
6187/*
6188 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6189 * a NUL.
6190 */
6191 void
6192screen_puts_len(text, len, row, col, attr)
6193 char_u *text;
6194 int len;
6195 int row;
6196 int col;
6197 int attr;
6198{
6199 unsigned off;
6200 char_u *ptr = text;
6201 int c;
6202#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006203 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int mbyte_blen = 1;
6205 int mbyte_cells = 1;
6206 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006207 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 int clear_next_cell = FALSE;
6209# ifdef FEAT_ARABIC
6210 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006211 int pc, nc, nc1;
6212 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213# endif
6214#endif
6215
6216 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6217 return;
6218
6219 off = LineOffset[row] + col;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006220#ifdef FEAT_MBYTE
6221 max_off = LineOffset[row] + screen_Columns;
6222#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006223 while (col < screen_Columns
6224 && (len < 0 || (int)(ptr - text) < len)
6225 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
6227 c = *ptr;
6228#ifdef FEAT_MBYTE
6229 /* check if this is the first byte of a multibyte */
6230 if (has_mbyte)
6231 {
6232 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006233 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006235 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6237 mbyte_cells = 1;
6238 else if (enc_dbcs != 0)
6239 mbyte_cells = mbyte_blen;
6240 else /* enc_utf8 */
6241 {
6242 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006243 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 (int)((text + len) - ptr));
6245 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006246 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 mbyte_cells = utf_char2cells(u8c);
6248 /* Non-BMP character: display as ? or fullwidth ?. */
6249 if (u8c >= 0x10000)
6250 {
6251 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6252 if (attr == 0)
6253 attr = hl_attr(HLF_8);
6254 }
6255# ifdef FEAT_ARABIC
6256 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6257 {
6258 /* Do Arabic shaping. */
6259 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6260 {
6261 /* Past end of string to be displayed. */
6262 nc = NUL;
6263 nc1 = NUL;
6264 }
6265 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006266 {
6267 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6268 nc1 = pcc[0];
6269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 pc = prev_c;
6271 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006272 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
6274 else
6275 prev_c = u8c;
6276# endif
6277 }
6278 }
6279#endif
6280
6281 if (ScreenLines[off] != c
6282#ifdef FEAT_MBYTE
6283 || (mbyte_cells == 2
6284 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6285 || (enc_dbcs == DBCS_JPNU
6286 && c == 0x8e
6287 && ScreenLines2[off] != ptr[1])
6288 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006289 && (ScreenLinesUC[off] != (u8char_T)u8c
6290 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291#endif
6292 || ScreenAttrs[off] != attr
6293 || exmode_active
6294 )
6295 {
6296#if defined(FEAT_GUI) || defined(UNIX)
6297 /* The bold trick makes a single row of pixels appear in the next
6298 * character. When a bold character is removed, the next
6299 * character should be redrawn too. This happens for our own GUI
6300 * and for some xterms.
6301 * Force the redraw by setting the attribute to a different value
6302 * than "attr", the contents of ScreenLines[] may be needed by
6303 * mb_off2cells() further on.
6304 * Don't do this for the last drawn character, because the next
6305 * character may not be redrawn. */
6306 if (
6307# ifdef FEAT_GUI
6308 gui.in_use
6309# endif
6310# if defined(FEAT_GUI) && defined(UNIX)
6311 ||
6312# endif
6313# ifdef UNIX
6314 term_is_xterm
6315# endif
6316 )
6317 {
6318 int n;
6319
6320 n = ScreenAttrs[off];
6321# ifdef FEAT_MBYTE
6322 if (col + mbyte_cells < screen_Columns
6323 && (n > HL_ALL || (n & HL_BOLD))
6324 && (len < 0 ? ptr[mbyte_blen] != NUL
6325 : ptr + mbyte_blen < text + len))
6326 ScreenAttrs[off + mbyte_cells] = attr + 1;
6327# else
6328 if (col + 1 < screen_Columns
6329 && (n > HL_ALL || (n & HL_BOLD))
6330 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6331 ScreenLines[off + 1] = 0;
6332# endif
6333 }
6334#endif
6335#ifdef FEAT_MBYTE
6336 /* When at the end of the text and overwriting a two-cell
6337 * character with a one-cell character, need to clear the next
6338 * cell. Also when overwriting the left halve of a two-cell char
6339 * with the right halve of a two-cell char. Do this only once
6340 * (mb_off2cells() may return 2 on the right halve). */
6341 if (clear_next_cell)
6342 clear_next_cell = FALSE;
6343 else if (has_mbyte
6344 && (len < 0 ? ptr[mbyte_blen] == NUL
6345 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006346 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006348 && (*mb_off2cells)(off, max_off) == 1
6349 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 clear_next_cell = TRUE;
6351
6352 /* Make sure we never leave a second byte of a double-byte behind,
6353 * it confuses mb_off2cells(). */
6354 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006355 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006357 && (*mb_off2cells)(off, max_off) == 1
6358 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 ScreenLines[off + mbyte_blen] = 0;
6360#endif
6361 ScreenLines[off] = c;
6362 ScreenAttrs[off] = attr;
6363#ifdef FEAT_MBYTE
6364 if (enc_utf8)
6365 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006366 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 ScreenLinesUC[off] = 0;
6368 else
6369 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006370 int i;
6371
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006373 for (i = 0; i < Screen_mco; ++i)
6374 {
6375 ScreenLinesC[i][off] = u8cc[i];
6376 if (u8cc[i] == 0)
6377 break;
6378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
6380 if (mbyte_cells == 2)
6381 {
6382 ScreenLines[off + 1] = 0;
6383 ScreenAttrs[off + 1] = attr;
6384 }
6385 screen_char(off, row, col);
6386 }
6387 else if (mbyte_cells == 2)
6388 {
6389 ScreenLines[off + 1] = ptr[1];
6390 ScreenAttrs[off + 1] = attr;
6391 screen_char_2(off, row, col);
6392 }
6393 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6394 {
6395 ScreenLines2[off] = ptr[1];
6396 screen_char(off, row, col);
6397 }
6398 else
6399#endif
6400 screen_char(off, row, col);
6401 }
6402#ifdef FEAT_MBYTE
6403 if (has_mbyte)
6404 {
6405 off += mbyte_cells;
6406 col += mbyte_cells;
6407 ptr += mbyte_blen;
6408 if (clear_next_cell)
6409 ptr = (char_u *)" ";
6410 }
6411 else
6412#endif
6413 {
6414 ++off;
6415 ++col;
6416 ++ptr;
6417 }
6418 }
6419}
6420
6421#ifdef FEAT_SEARCH_EXTRA
6422/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006423 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 */
6425 static void
6426start_search_hl()
6427{
6428 if (p_hls && !no_hlsearch)
6429 {
6430 last_pat_prog(&search_hl.rm);
6431 search_hl.attr = hl_attr(HLF_L);
6432 }
6433}
6434
6435/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006436 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 */
6438 static void
6439end_search_hl()
6440{
6441 if (search_hl.rm.regprog != NULL)
6442 {
6443 vim_free(search_hl.rm.regprog);
6444 search_hl.rm.regprog = NULL;
6445 }
6446}
6447
6448/*
6449 * Advance to the match in window "wp" line "lnum" or past it.
6450 */
6451 static void
6452prepare_search_hl(wp, lnum)
6453 win_T *wp;
6454 linenr_T lnum;
6455{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006456 matchitem_T *cur; /* points to the match list */
6457 match_T *shl; /* points to search_hl or a match */
6458 int shl_flag; /* flag to indicate whether search_hl
6459 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 int n;
6461
6462 /*
6463 * When using a multi-line pattern, start searching at the top
6464 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006465 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006467 cur = wp->w_match_head;
6468 shl_flag = FALSE;
6469 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006471 if (shl_flag == FALSE)
6472 {
6473 shl = &search_hl;
6474 shl_flag = TRUE;
6475 }
6476 else
6477 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (shl->rm.regprog != NULL
6479 && shl->lnum == 0
6480 && re_multiline(shl->rm.regprog))
6481 {
6482 if (shl->first_lnum == 0)
6483 {
6484# ifdef FEAT_FOLDING
6485 for (shl->first_lnum = lnum;
6486 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6487 if (hasFoldingWin(wp, shl->first_lnum - 1,
6488 NULL, NULL, TRUE, NULL))
6489 break;
6490# else
6491 shl->first_lnum = wp->w_topline;
6492# endif
6493 }
6494 n = 0;
6495 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6496 {
6497 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6498 if (shl->lnum != 0)
6499 {
6500 shl->first_lnum = shl->lnum
6501 + shl->rm.endpos[0].lnum
6502 - shl->rm.startpos[0].lnum;
6503 n = shl->rm.endpos[0].col;
6504 }
6505 else
6506 {
6507 ++shl->first_lnum;
6508 n = 0;
6509 }
6510 }
6511 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006512 if (shl != &search_hl && cur != NULL)
6513 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515}
6516
6517/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006518 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 * Uses shl->buf.
6520 * Sets shl->lnum and shl->rm contents.
6521 * Note: Assumes a previous match is always before "lnum", unless
6522 * shl->lnum is zero.
6523 * Careful: Any pointers for buffer lines will become invalid.
6524 */
6525 static void
6526next_search_hl(win, shl, lnum, mincol)
6527 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006528 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 linenr_T lnum;
6530 colnr_T mincol; /* minimal column for a match */
6531{
6532 linenr_T l;
6533 colnr_T matchcol;
6534 long nmatched;
6535
6536 if (shl->lnum != 0)
6537 {
6538 /* Check for three situations:
6539 * 1. If the "lnum" is below a previous match, start a new search.
6540 * 2. If the previous match includes "mincol", use it.
6541 * 3. Continue after the previous match.
6542 */
6543 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6544 if (lnum > l)
6545 shl->lnum = 0;
6546 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6547 return;
6548 }
6549
6550 /*
6551 * Repeat searching for a match until one is found that includes "mincol"
6552 * or none is found in this line.
6553 */
6554 called_emsg = FALSE;
6555 for (;;)
6556 {
6557 /* Three situations:
6558 * 1. No useful previous match: search from start of line.
6559 * 2. Not Vi compatible or empty match: continue at next character.
6560 * Break the loop if this is beyond the end of the line.
6561 * 3. Vi compatible searching: continue at end of previous match.
6562 */
6563 if (shl->lnum == 0)
6564 matchcol = 0;
6565 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6566 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006567 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006569 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006570
6571 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006572 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006573 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006575 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 shl->lnum = 0;
6577 break;
6578 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006579#ifdef FEAT_MBYTE
6580 if (has_mbyte)
6581 matchcol += mb_ptr2len(ml);
6582 else
6583#endif
6584 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 }
6586 else
6587 matchcol = shl->rm.endpos[0].col;
6588
6589 shl->lnum = lnum;
6590 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6591 if (called_emsg)
6592 {
6593 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006594 if (shl == &search_hl)
6595 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006596 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006597 vim_free(shl->rm.regprog);
6598 no_hlsearch = TRUE;
6599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006601 shl->lnum = 0;
6602 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 break;
6604 }
6605 if (nmatched == 0)
6606 {
6607 shl->lnum = 0; /* no match found */
6608 break;
6609 }
6610 if (shl->rm.startpos[0].lnum > 0
6611 || shl->rm.startpos[0].col >= mincol
6612 || nmatched > 1
6613 || shl->rm.endpos[0].col > mincol)
6614 {
6615 shl->lnum += shl->rm.startpos[0].lnum;
6616 break; /* useful match found */
6617 }
6618 }
6619}
6620#endif
6621
6622 static void
6623screen_start_highlight(attr)
6624 int attr;
6625{
6626 attrentry_T *aep = NULL;
6627
6628 screen_attr = attr;
6629 if (full_screen
6630#ifdef WIN3264
6631 && termcap_active
6632#endif
6633 )
6634 {
6635#ifdef FEAT_GUI
6636 if (gui.in_use)
6637 {
6638 char buf[20];
6639
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006640 /* The GUI handles this internally. */
6641 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 OUT_STR(buf);
6643 }
6644 else
6645#endif
6646 {
6647 if (attr > HL_ALL) /* special HL attr. */
6648 {
6649 if (t_colors > 1)
6650 aep = syn_cterm_attr2entry(attr);
6651 else
6652 aep = syn_term_attr2entry(attr);
6653 if (aep == NULL) /* did ":syntax clear" */
6654 attr = 0;
6655 else
6656 attr = aep->ae_attr;
6657 }
6658 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6659 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006660 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6661 && cterm_normal_fg_bold)
6662 /* If the Normal FG color has BOLD attribute and the new HL
6663 * has a FG color defined, clear BOLD. */
6664 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6666 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006667 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6668 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 out_str(T_US);
6670 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6671 out_str(T_CZH);
6672 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6673 out_str(T_MR);
6674
6675 /*
6676 * Output the color or start string after bold etc., in case the
6677 * bold etc. override the color setting.
6678 */
6679 if (aep != NULL)
6680 {
6681 if (t_colors > 1)
6682 {
6683 if (aep->ae_u.cterm.fg_color)
6684 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6685 if (aep->ae_u.cterm.bg_color)
6686 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6687 }
6688 else
6689 {
6690 if (aep->ae_u.term.start != NULL)
6691 out_str(aep->ae_u.term.start);
6692 }
6693 }
6694 }
6695 }
6696}
6697
6698 void
6699screen_stop_highlight()
6700{
6701 int do_ME = FALSE; /* output T_ME code */
6702
6703 if (screen_attr != 0
6704#ifdef WIN3264
6705 && termcap_active
6706#endif
6707 )
6708 {
6709#ifdef FEAT_GUI
6710 if (gui.in_use)
6711 {
6712 char buf[20];
6713
6714 /* use internal GUI code */
6715 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6716 OUT_STR(buf);
6717 }
6718 else
6719#endif
6720 {
6721 if (screen_attr > HL_ALL) /* special HL attr. */
6722 {
6723 attrentry_T *aep;
6724
6725 if (t_colors > 1)
6726 {
6727 /*
6728 * Assume that t_me restores the original colors!
6729 */
6730 aep = syn_cterm_attr2entry(screen_attr);
6731 if (aep != NULL && (aep->ae_u.cterm.fg_color
6732 || aep->ae_u.cterm.bg_color))
6733 do_ME = TRUE;
6734 }
6735 else
6736 {
6737 aep = syn_term_attr2entry(screen_attr);
6738 if (aep != NULL && aep->ae_u.term.stop != NULL)
6739 {
6740 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6741 do_ME = TRUE;
6742 else
6743 out_str(aep->ae_u.term.stop);
6744 }
6745 }
6746 if (aep == NULL) /* did ":syntax clear" */
6747 screen_attr = 0;
6748 else
6749 screen_attr = aep->ae_attr;
6750 }
6751
6752 /*
6753 * Often all ending-codes are equal to T_ME. Avoid outputting the
6754 * same sequence several times.
6755 */
6756 if (screen_attr & HL_STANDOUT)
6757 {
6758 if (STRCMP(T_SE, T_ME) == 0)
6759 do_ME = TRUE;
6760 else
6761 out_str(T_SE);
6762 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006763 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
6765 if (STRCMP(T_UE, T_ME) == 0)
6766 do_ME = TRUE;
6767 else
6768 out_str(T_UE);
6769 }
6770 if (screen_attr & HL_ITALIC)
6771 {
6772 if (STRCMP(T_CZR, T_ME) == 0)
6773 do_ME = TRUE;
6774 else
6775 out_str(T_CZR);
6776 }
6777 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6778 out_str(T_ME);
6779
6780 if (t_colors > 1)
6781 {
6782 /* set Normal cterm colors */
6783 if (cterm_normal_fg_color != 0)
6784 term_fg_color(cterm_normal_fg_color - 1);
6785 if (cterm_normal_bg_color != 0)
6786 term_bg_color(cterm_normal_bg_color - 1);
6787 if (cterm_normal_fg_bold)
6788 out_str(T_MD);
6789 }
6790 }
6791 }
6792 screen_attr = 0;
6793}
6794
6795/*
6796 * Reset the colors for a cterm. Used when leaving Vim.
6797 * The machine specific code may override this again.
6798 */
6799 void
6800reset_cterm_colors()
6801{
6802 if (t_colors > 1)
6803 {
6804 /* set Normal cterm colors */
6805 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6806 {
6807 out_str(T_OP);
6808 screen_attr = -1;
6809 }
6810 if (cterm_normal_fg_bold)
6811 {
6812 out_str(T_ME);
6813 screen_attr = -1;
6814 }
6815 }
6816}
6817
6818/*
6819 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6820 * using the attributes from ScreenAttrs["off"].
6821 */
6822 static void
6823screen_char(off, row, col)
6824 unsigned off;
6825 int row;
6826 int col;
6827{
6828 int attr;
6829
6830 /* Check for illegal values, just in case (could happen just after
6831 * resizing). */
6832 if (row >= screen_Rows || col >= screen_Columns)
6833 return;
6834
6835 /* Outputting the last character on the screen may scrollup the screen.
6836 * Don't to it! Mark the character invalid (update it when scrolled up) */
6837 if (row == screen_Rows - 1 && col == screen_Columns - 1
6838#ifdef FEAT_RIGHTLEFT
6839 /* account for first command-line character in rightleft mode */
6840 && !cmdmsg_rl
6841#endif
6842 )
6843 {
6844 ScreenAttrs[off] = (sattr_T)-1;
6845 return;
6846 }
6847
6848 /*
6849 * Stop highlighting first, so it's easier to move the cursor.
6850 */
6851#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6852 if (screen_char_attr != 0)
6853 attr = screen_char_attr;
6854 else
6855#endif
6856 attr = ScreenAttrs[off];
6857 if (screen_attr != attr)
6858 screen_stop_highlight();
6859
6860 windgoto(row, col);
6861
6862 if (screen_attr != attr)
6863 screen_start_highlight(attr);
6864
6865#ifdef FEAT_MBYTE
6866 if (enc_utf8 && ScreenLinesUC[off] != 0)
6867 {
6868 char_u buf[MB_MAXBYTES + 1];
6869
6870 /* Convert UTF-8 character to bytes and write it. */
6871
6872 buf[utfc_char2bytes(off, buf)] = NUL;
6873
6874 out_str(buf);
6875 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6876 ++screen_cur_col;
6877 }
6878 else
6879#endif
6880 {
6881#ifdef FEAT_MBYTE
6882 out_flush_check();
6883#endif
6884 out_char(ScreenLines[off]);
6885#ifdef FEAT_MBYTE
6886 /* double-byte character in single-width cell */
6887 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6888 out_char(ScreenLines2[off]);
6889#endif
6890 }
6891
6892 screen_cur_col++;
6893}
6894
6895#ifdef FEAT_MBYTE
6896
6897/*
6898 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6899 * on the screen at position 'row' and 'col'.
6900 * The attributes of the first byte is used for all. This is required to
6901 * output the two bytes of a double-byte character with nothing in between.
6902 */
6903 static void
6904screen_char_2(off, row, col)
6905 unsigned off;
6906 int row;
6907 int col;
6908{
6909 /* Check for illegal values (could be wrong when screen was resized). */
6910 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6911 return;
6912
6913 /* Outputting the last character on the screen may scrollup the screen.
6914 * Don't to it! Mark the character invalid (update it when scrolled up) */
6915 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6916 {
6917 ScreenAttrs[off] = (sattr_T)-1;
6918 return;
6919 }
6920
6921 /* Output the first byte normally (positions the cursor), then write the
6922 * second byte directly. */
6923 screen_char(off, row, col);
6924 out_char(ScreenLines[off + 1]);
6925 ++screen_cur_col;
6926}
6927#endif
6928
6929#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6930/*
6931 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6932 * This uses the contents of ScreenLines[] and doesn't change it.
6933 */
6934 void
6935screen_draw_rectangle(row, col, height, width, invert)
6936 int row;
6937 int col;
6938 int height;
6939 int width;
6940 int invert;
6941{
6942 int r, c;
6943 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006944#ifdef FEAT_MBYTE
6945 int max_off;
6946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006948 /* Can't use ScreenLines unless initialized */
6949 if (ScreenLines == NULL)
6950 return;
6951
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (invert)
6953 screen_char_attr = HL_INVERSE;
6954 for (r = row; r < row + height; ++r)
6955 {
6956 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006957#ifdef FEAT_MBYTE
6958 max_off = off + screen_Columns;
6959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 for (c = col; c < col + width; ++c)
6961 {
6962#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006963 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 screen_char_2(off + c, r, c);
6966 ++c;
6967 }
6968 else
6969#endif
6970 {
6971 screen_char(off + c, r, c);
6972#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006973 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 ++c;
6975#endif
6976 }
6977 }
6978 }
6979 screen_char_attr = 0;
6980}
6981#endif
6982
6983#ifdef FEAT_VERTSPLIT
6984/*
6985 * Redraw the characters for a vertically split window.
6986 */
6987 static void
6988redraw_block(row, end, wp)
6989 int row;
6990 int end;
6991 win_T *wp;
6992{
6993 int col;
6994 int width;
6995
6996# ifdef FEAT_CLIPBOARD
6997 clip_may_clear_selection(row, end - 1);
6998# endif
6999
7000 if (wp == NULL)
7001 {
7002 col = 0;
7003 width = Columns;
7004 }
7005 else
7006 {
7007 col = wp->w_wincol;
7008 width = wp->w_width;
7009 }
7010 screen_draw_rectangle(row, col, end - row, width, FALSE);
7011}
7012#endif
7013
7014/*
7015 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7016 * with character 'c1' in first column followed by 'c2' in the other columns.
7017 * Use attributes 'attr'.
7018 */
7019 void
7020screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7021 int start_row, end_row;
7022 int start_col, end_col;
7023 int c1, c2;
7024 int attr;
7025{
7026 int row;
7027 int col;
7028 int off;
7029 int end_off;
7030 int did_delete;
7031 int c;
7032 int norm_term;
7033#if defined(FEAT_GUI) || defined(UNIX)
7034 int force_next = FALSE;
7035#endif
7036
7037 if (end_row > screen_Rows) /* safety check */
7038 end_row = screen_Rows;
7039 if (end_col > screen_Columns) /* safety check */
7040 end_col = screen_Columns;
7041 if (ScreenLines == NULL
7042 || start_row >= end_row
7043 || start_col >= end_col) /* nothing to do */
7044 return;
7045
7046 /* it's a "normal" terminal when not in a GUI or cterm */
7047 norm_term = (
7048#ifdef FEAT_GUI
7049 !gui.in_use &&
7050#endif
7051 t_colors <= 1);
7052 for (row = start_row; row < end_row; ++row)
7053 {
7054 /*
7055 * Try to use delete-line termcap code, when no attributes or in a
7056 * "normal" terminal, where a bold/italic space is just a
7057 * space.
7058 */
7059 did_delete = FALSE;
7060 if (c2 == ' '
7061 && end_col == Columns
7062 && can_clear(T_CE)
7063 && (attr == 0
7064 || (norm_term
7065 && attr <= HL_ALL
7066 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7067 {
7068 /*
7069 * check if we really need to clear something
7070 */
7071 col = start_col;
7072 if (c1 != ' ') /* don't clear first char */
7073 ++col;
7074
7075 off = LineOffset[row] + col;
7076 end_off = LineOffset[row] + end_col;
7077
7078 /* skip blanks (used often, keep it fast!) */
7079#ifdef FEAT_MBYTE
7080 if (enc_utf8)
7081 while (off < end_off && ScreenLines[off] == ' '
7082 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7083 ++off;
7084 else
7085#endif
7086 while (off < end_off && ScreenLines[off] == ' '
7087 && ScreenAttrs[off] == 0)
7088 ++off;
7089 if (off < end_off) /* something to be cleared */
7090 {
7091 col = off - LineOffset[row];
7092 screen_stop_highlight();
7093 term_windgoto(row, col);/* clear rest of this screen line */
7094 out_str(T_CE);
7095 screen_start(); /* don't know where cursor is now */
7096 col = end_col - col;
7097 while (col--) /* clear chars in ScreenLines */
7098 {
7099 ScreenLines[off] = ' ';
7100#ifdef FEAT_MBYTE
7101 if (enc_utf8)
7102 ScreenLinesUC[off] = 0;
7103#endif
7104 ScreenAttrs[off] = 0;
7105 ++off;
7106 }
7107 }
7108 did_delete = TRUE; /* the chars are cleared now */
7109 }
7110
7111 off = LineOffset[row] + start_col;
7112 c = c1;
7113 for (col = start_col; col < end_col; ++col)
7114 {
7115 if (ScreenLines[off] != c
7116#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007117 || (enc_utf8 && (int)ScreenLinesUC[off]
7118 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119#endif
7120 || ScreenAttrs[off] != attr
7121#if defined(FEAT_GUI) || defined(UNIX)
7122 || force_next
7123#endif
7124 )
7125 {
7126#if defined(FEAT_GUI) || defined(UNIX)
7127 /* The bold trick may make a single row of pixels appear in
7128 * the next character. When a bold character is removed, the
7129 * next character should be redrawn too. This happens for our
7130 * own GUI and for some xterms. */
7131 if (
7132# ifdef FEAT_GUI
7133 gui.in_use
7134# endif
7135# if defined(FEAT_GUI) && defined(UNIX)
7136 ||
7137# endif
7138# ifdef UNIX
7139 term_is_xterm
7140# endif
7141 )
7142 {
7143 if (ScreenLines[off] != ' '
7144 && (ScreenAttrs[off] > HL_ALL
7145 || ScreenAttrs[off] & HL_BOLD))
7146 force_next = TRUE;
7147 else
7148 force_next = FALSE;
7149 }
7150#endif
7151 ScreenLines[off] = c;
7152#ifdef FEAT_MBYTE
7153 if (enc_utf8)
7154 {
7155 if (c >= 0x80)
7156 {
7157 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007158 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 }
7160 else
7161 ScreenLinesUC[off] = 0;
7162 }
7163#endif
7164 ScreenAttrs[off] = attr;
7165 if (!did_delete || c != ' ')
7166 screen_char(off, row, col);
7167 }
7168 ++off;
7169 if (col == start_col)
7170 {
7171 if (did_delete)
7172 break;
7173 c = c2;
7174 }
7175 }
7176 if (end_col == Columns)
7177 LineWraps[row] = FALSE;
7178 if (row == Rows - 1) /* overwritten the command line */
7179 {
7180 redraw_cmdline = TRUE;
7181 if (c1 == ' ' && c2 == ' ')
7182 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007183 if (start_col == 0)
7184 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 }
7186 }
7187}
7188
7189/*
7190 * Check if there should be a delay. Used before clearing or redrawing the
7191 * screen or the command line.
7192 */
7193 void
7194check_for_delay(check_msg_scroll)
7195 int check_msg_scroll;
7196{
7197 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7198 && !did_wait_return
7199 && emsg_silent == 0)
7200 {
7201 out_flush();
7202 ui_delay(1000L, TRUE);
7203 emsg_on_display = FALSE;
7204 if (check_msg_scroll)
7205 msg_scroll = FALSE;
7206 }
7207}
7208
7209/*
7210 * screen_valid - allocate screen buffers if size changed
7211 * If "clear" is TRUE: clear screen if it has been resized.
7212 * Returns TRUE if there is a valid screen to write to.
7213 * Returns FALSE when starting up and screen not initialized yet.
7214 */
7215 int
7216screen_valid(clear)
7217 int clear;
7218{
7219 screenalloc(clear); /* allocate screen buffers if size changed */
7220 return (ScreenLines != NULL);
7221}
7222
7223/*
7224 * Resize the shell to Rows and Columns.
7225 * Allocate ScreenLines[] and associated items.
7226 *
7227 * There may be some time between setting Rows and Columns and (re)allocating
7228 * ScreenLines[]. This happens when starting up and when (manually) changing
7229 * the shell size. Always use screen_Rows and screen_Columns to access items
7230 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7231 * final size of the shell is needed.
7232 */
7233 void
7234screenalloc(clear)
7235 int clear;
7236{
7237 int new_row, old_row;
7238#ifdef FEAT_GUI
7239 int old_Rows;
7240#endif
7241 win_T *wp;
7242 int outofmem = FALSE;
7243 int len;
7244 schar_T *new_ScreenLines;
7245#ifdef FEAT_MBYTE
7246 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007247 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007249 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250#endif
7251 sattr_T *new_ScreenAttrs;
7252 unsigned *new_LineOffset;
7253 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007254#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007255 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007256 tabpage_T *tp;
7257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007259 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 /*
7262 * Allocation of the screen buffers is done only when the size changes and
7263 * when Rows and Columns have been set and we have started doing full
7264 * screen stuff.
7265 */
7266 if ((ScreenLines != NULL
7267 && Rows == screen_Rows
7268 && Columns == screen_Columns
7269#ifdef FEAT_MBYTE
7270 && enc_utf8 == (ScreenLinesUC != NULL)
7271 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007272 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273#endif
7274 )
7275 || Rows == 0
7276 || Columns == 0
7277 || (!full_screen && ScreenLines == NULL))
7278 return;
7279
7280 /*
7281 * It's possible that we produce an out-of-memory message below, which
7282 * will cause this function to be called again. To break the loop, just
7283 * return here.
7284 */
7285 if (entered)
7286 return;
7287 entered = TRUE;
7288
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007289 /*
7290 * Note that the window sizes are updated before reallocating the arrays,
7291 * thus we must not redraw here!
7292 */
7293 ++RedrawingDisabled;
7294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 win_new_shellsize(); /* fit the windows in the new sized shell */
7296
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 comp_col(); /* recompute columns for shown command and ruler */
7298
7299 /*
7300 * We're changing the size of the screen.
7301 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7302 * - Move lines from the old arrays into the new arrays, clear extra
7303 * lines (unless the screen is going to be cleared).
7304 * - Free the old arrays.
7305 *
7306 * If anything fails, make ScreenLines NULL, so we don't do anything!
7307 * Continuing with the old ScreenLines may result in a crash, because the
7308 * size is wrong.
7309 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007310 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
7313 new_ScreenLines = (schar_T *)lalloc((long_u)(
7314 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7315#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007316 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 if (enc_utf8)
7318 {
7319 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7320 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007321 for (i = 0; i < p_mco; ++i)
7322 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7324 }
7325 if (enc_dbcs == DBCS_JPNU)
7326 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7327 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7328#endif
7329 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7330 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7331 new_LineOffset = (unsigned *)lalloc((long_u)(
7332 Rows * sizeof(unsigned)), FALSE);
7333 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007334#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007335 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007338 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
7340 if (win_alloc_lines(wp) == FAIL)
7341 {
7342 outofmem = TRUE;
7343#ifdef FEAT_WINDOWS
7344 break;
7345#endif
7346 }
7347 }
7348
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007349#ifdef FEAT_MBYTE
7350 for (i = 0; i < p_mco; ++i)
7351 if (new_ScreenLinesC[i] == NULL)
7352 break;
7353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 if (new_ScreenLines == NULL
7355#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007356 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7358#endif
7359 || new_ScreenAttrs == NULL
7360 || new_LineOffset == NULL
7361 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007362#ifdef FEAT_WINDOWS
7363 || new_TabPageIdxs == NULL
7364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 || outofmem)
7366 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007367 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007368 {
7369 /* guess the size */
7370 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7371
7372 /* Remember we did this to avoid getting outofmem messages over
7373 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007374 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 vim_free(new_ScreenLines);
7377 new_ScreenLines = NULL;
7378#ifdef FEAT_MBYTE
7379 vim_free(new_ScreenLinesUC);
7380 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007381 for (i = 0; i < p_mco; ++i)
7382 {
7383 vim_free(new_ScreenLinesC[i]);
7384 new_ScreenLinesC[i] = NULL;
7385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 vim_free(new_ScreenLines2);
7387 new_ScreenLines2 = NULL;
7388#endif
7389 vim_free(new_ScreenAttrs);
7390 new_ScreenAttrs = NULL;
7391 vim_free(new_LineOffset);
7392 new_LineOffset = NULL;
7393 vim_free(new_LineWraps);
7394 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007395#ifdef FEAT_WINDOWS
7396 vim_free(new_TabPageIdxs);
7397 new_TabPageIdxs = NULL;
7398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
7400 else
7401 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007402 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007403
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 for (new_row = 0; new_row < Rows; ++new_row)
7405 {
7406 new_LineOffset[new_row] = new_row * Columns;
7407 new_LineWraps[new_row] = FALSE;
7408
7409 /*
7410 * If the screen is not going to be cleared, copy as much as
7411 * possible from the old screen to the new one and clear the rest
7412 * (used when resizing the window at the "--more--" prompt or when
7413 * executing an external command, for the GUI).
7414 */
7415 if (!clear)
7416 {
7417 (void)vim_memset(new_ScreenLines + new_row * Columns,
7418 ' ', (size_t)Columns * sizeof(schar_T));
7419#ifdef FEAT_MBYTE
7420 if (enc_utf8)
7421 {
7422 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7423 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007424 for (i = 0; i < p_mco; ++i)
7425 (void)vim_memset(new_ScreenLinesC[i]
7426 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 0, (size_t)Columns * sizeof(u8char_T));
7428 }
7429 if (enc_dbcs == DBCS_JPNU)
7430 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7431 0, (size_t)Columns * sizeof(schar_T));
7432#endif
7433 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7434 0, (size_t)Columns * sizeof(sattr_T));
7435 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007436 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
7438 if (screen_Columns < Columns)
7439 len = screen_Columns;
7440 else
7441 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007442#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007443 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007444 * may be invalid now. Also when p_mco changes. */
7445 if (!(enc_utf8 && ScreenLinesUC == NULL)
7446 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007447#endif
7448 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7449 ScreenLines + LineOffset[old_row],
7450 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007452 if (enc_utf8 && ScreenLinesUC != NULL
7453 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {
7455 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7456 ScreenLinesUC + LineOffset[old_row],
7457 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007458 for (i = 0; i < p_mco; ++i)
7459 mch_memmove(new_ScreenLinesC[i]
7460 + new_LineOffset[new_row],
7461 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 (size_t)len * sizeof(u8char_T));
7463 }
7464 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7465 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7466 ScreenLines2 + LineOffset[old_row],
7467 (size_t)len * sizeof(schar_T));
7468#endif
7469 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7470 ScreenAttrs + LineOffset[old_row],
7471 (size_t)len * sizeof(sattr_T));
7472 }
7473 }
7474 }
7475 /* Use the last line of the screen for the current line. */
7476 current_ScreenLine = new_ScreenLines + Rows * Columns;
7477 }
7478
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007479 free_screenlines();
7480
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 ScreenLines = new_ScreenLines;
7482#ifdef FEAT_MBYTE
7483 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007484 for (i = 0; i < p_mco; ++i)
7485 ScreenLinesC[i] = new_ScreenLinesC[i];
7486 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 ScreenLines2 = new_ScreenLines2;
7488#endif
7489 ScreenAttrs = new_ScreenAttrs;
7490 LineOffset = new_LineOffset;
7491 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007492#ifdef FEAT_WINDOWS
7493 TabPageIdxs = new_TabPageIdxs;
7494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
7496 /* It's important that screen_Rows and screen_Columns reflect the actual
7497 * size of ScreenLines[]. Set them before calling anything. */
7498#ifdef FEAT_GUI
7499 old_Rows = screen_Rows;
7500#endif
7501 screen_Rows = Rows;
7502 screen_Columns = Columns;
7503
7504 must_redraw = CLEAR; /* need to clear the screen later */
7505 if (clear)
7506 screenclear2();
7507
7508#ifdef FEAT_GUI
7509 else if (gui.in_use
7510 && !gui.starting
7511 && ScreenLines != NULL
7512 && old_Rows != Rows)
7513 {
7514 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7515 /*
7516 * Adjust the position of the cursor, for when executing an external
7517 * command.
7518 */
7519 if (msg_row >= Rows) /* Rows got smaller */
7520 msg_row = Rows - 1; /* put cursor at last row */
7521 else if (Rows > old_Rows) /* Rows got bigger */
7522 msg_row += Rows - old_Rows; /* put cursor in same place */
7523 if (msg_col >= Columns) /* Columns got smaller */
7524 msg_col = Columns - 1; /* put cursor at last column */
7525 }
7526#endif
7527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007529 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007530
7531#ifdef FEAT_AUTOCMD
7532 if (starting == 0)
7533 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535}
7536
7537 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007538free_screenlines()
7539{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007540#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007541 int i;
7542
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007543 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007544 for (i = 0; i < Screen_mco; ++i)
7545 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007546 vim_free(ScreenLines2);
7547#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007548 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007549 vim_free(ScreenAttrs);
7550 vim_free(LineOffset);
7551 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007552#ifdef FEAT_WINDOWS
7553 vim_free(TabPageIdxs);
7554#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007555}
7556
7557 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558screenclear()
7559{
7560 check_for_delay(FALSE);
7561 screenalloc(FALSE); /* allocate screen buffers if size changed */
7562 screenclear2(); /* clear the screen */
7563}
7564
7565 static void
7566screenclear2()
7567{
7568 int i;
7569
7570 if (starting == NO_SCREEN || ScreenLines == NULL
7571#ifdef FEAT_GUI
7572 || (gui.in_use && gui.starting)
7573#endif
7574 )
7575 return;
7576
7577#ifdef FEAT_GUI
7578 if (!gui.in_use)
7579#endif
7580 screen_attr = -1; /* force setting the Normal colors */
7581 screen_stop_highlight(); /* don't want highlighting here */
7582
7583#ifdef FEAT_CLIPBOARD
7584 /* disable selection without redrawing it */
7585 clip_scroll_selection(9999);
7586#endif
7587
7588 /* blank out ScreenLines */
7589 for (i = 0; i < Rows; ++i)
7590 {
7591 lineclear(LineOffset[i], (int)Columns);
7592 LineWraps[i] = FALSE;
7593 }
7594
7595 if (can_clear(T_CL))
7596 {
7597 out_str(T_CL); /* clear the display */
7598 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007599 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
7601 else
7602 {
7603 /* can't clear the screen, mark all chars with invalid attributes */
7604 for (i = 0; i < Rows; ++i)
7605 lineinvalid(LineOffset[i], (int)Columns);
7606 clear_cmdline = TRUE;
7607 }
7608
7609 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7610
7611 win_rest_invalid(firstwin);
7612 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007613#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007614 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 if (must_redraw == CLEAR) /* no need to clear again */
7617 must_redraw = NOT_VALID;
7618 compute_cmdrow();
7619 msg_row = cmdline_row; /* put cursor on last line for messages */
7620 msg_col = 0;
7621 screen_start(); /* don't know where cursor is now */
7622 msg_scrolled = 0; /* can't scroll back */
7623 msg_didany = FALSE;
7624 msg_didout = FALSE;
7625}
7626
7627/*
7628 * Clear one line in ScreenLines.
7629 */
7630 static void
7631lineclear(off, width)
7632 unsigned off;
7633 int width;
7634{
7635 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7636#ifdef FEAT_MBYTE
7637 if (enc_utf8)
7638 (void)vim_memset(ScreenLinesUC + off, 0,
7639 (size_t)width * sizeof(u8char_T));
7640#endif
7641 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7642}
7643
7644/*
7645 * Mark one line in ScreenLines invalid by setting the attributes to an
7646 * invalid value.
7647 */
7648 static void
7649lineinvalid(off, width)
7650 unsigned off;
7651 int width;
7652{
7653 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7654}
7655
7656#ifdef FEAT_VERTSPLIT
7657/*
7658 * Copy part of a Screenline for vertically split window "wp".
7659 */
7660 static void
7661linecopy(to, from, wp)
7662 int to;
7663 int from;
7664 win_T *wp;
7665{
7666 unsigned off_to = LineOffset[to] + wp->w_wincol;
7667 unsigned off_from = LineOffset[from] + wp->w_wincol;
7668
7669 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7670 wp->w_width * sizeof(schar_T));
7671# ifdef FEAT_MBYTE
7672 if (enc_utf8)
7673 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007674 int i;
7675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7677 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007678 for (i = 0; i < p_mco; ++i)
7679 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7680 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
7682 if (enc_dbcs == DBCS_JPNU)
7683 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7684 wp->w_width * sizeof(schar_T));
7685# endif
7686 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7687 wp->w_width * sizeof(sattr_T));
7688}
7689#endif
7690
7691/*
7692 * Return TRUE if clearing with term string "p" would work.
7693 * It can't work when the string is empty or it won't set the right background.
7694 */
7695 int
7696can_clear(p)
7697 char_u *p;
7698{
7699 return (*p != NUL && (t_colors <= 1
7700#ifdef FEAT_GUI
7701 || gui.in_use
7702#endif
7703 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7704}
7705
7706/*
7707 * Reset cursor position. Use whenever cursor was moved because of outputting
7708 * something directly to the screen (shell commands) or a terminal control
7709 * code.
7710 */
7711 void
7712screen_start()
7713{
7714 screen_cur_row = screen_cur_col = 9999;
7715}
7716
7717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 * Move the cursor to position "row","col" in the screen.
7719 * This tries to find the most efficient way to move, minimizing the number of
7720 * characters sent to the terminal.
7721 */
7722 void
7723windgoto(row, col)
7724 int row;
7725 int col;
7726{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007727 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 int i;
7729 int plan;
7730 int cost;
7731 int wouldbe_col;
7732 int noinvcurs;
7733 char_u *bs;
7734 int goto_cost;
7735 int attr;
7736
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007737#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7739
7740#define PLAN_LE 1
7741#define PLAN_CR 2
7742#define PLAN_NL 3
7743#define PLAN_WRITE 4
7744 /* Can't use ScreenLines unless initialized */
7745 if (ScreenLines == NULL)
7746 return;
7747
7748 if (col != screen_cur_col || row != screen_cur_row)
7749 {
7750 /* Check for valid position. */
7751 if (row < 0) /* window without text lines? */
7752 row = 0;
7753 if (row >= screen_Rows)
7754 row = screen_Rows - 1;
7755 if (col >= screen_Columns)
7756 col = screen_Columns - 1;
7757
7758 /* check if no cursor movement is allowed in highlight mode */
7759 if (screen_attr && *T_MS == NUL)
7760 noinvcurs = HIGHL_COST;
7761 else
7762 noinvcurs = 0;
7763 goto_cost = GOTO_COST + noinvcurs;
7764
7765 /*
7766 * Plan how to do the positioning:
7767 * 1. Use CR to move it to column 0, same row.
7768 * 2. Use T_LE to move it a few columns to the left.
7769 * 3. Use NL to move a few lines down, column 0.
7770 * 4. Move a few columns to the right with T_ND or by writing chars.
7771 *
7772 * Don't do this if the cursor went beyond the last column, the cursor
7773 * position is unknown then (some terminals wrap, some don't )
7774 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007775 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 * characters to move the cursor to the right.
7777 */
7778 if (row >= screen_cur_row && screen_cur_col < Columns)
7779 {
7780 /*
7781 * If the cursor is in the same row, bigger col, we can use CR
7782 * or T_LE.
7783 */
7784 bs = NULL; /* init for GCC */
7785 attr = screen_attr;
7786 if (row == screen_cur_row && col < screen_cur_col)
7787 {
7788 /* "le" is preferred over "bc", because "bc" is obsolete */
7789 if (*T_LE)
7790 bs = T_LE; /* "cursor left" */
7791 else
7792 bs = T_BC; /* "backspace character (old) */
7793 if (*bs)
7794 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7795 else
7796 cost = 999;
7797 if (col + 1 < cost) /* using CR is less characters */
7798 {
7799 plan = PLAN_CR;
7800 wouldbe_col = 0;
7801 cost = 1; /* CR is just one character */
7802 }
7803 else
7804 {
7805 plan = PLAN_LE;
7806 wouldbe_col = col;
7807 }
7808 if (noinvcurs) /* will stop highlighting */
7809 {
7810 cost += noinvcurs;
7811 attr = 0;
7812 }
7813 }
7814
7815 /*
7816 * If the cursor is above where we want to be, we can use CR LF.
7817 */
7818 else if (row > screen_cur_row)
7819 {
7820 plan = PLAN_NL;
7821 wouldbe_col = 0;
7822 cost = (row - screen_cur_row) * 2; /* CR LF */
7823 if (noinvcurs) /* will stop highlighting */
7824 {
7825 cost += noinvcurs;
7826 attr = 0;
7827 }
7828 }
7829
7830 /*
7831 * If the cursor is in the same row, smaller col, just use write.
7832 */
7833 else
7834 {
7835 plan = PLAN_WRITE;
7836 wouldbe_col = screen_cur_col;
7837 cost = 0;
7838 }
7839
7840 /*
7841 * Check if any characters that need to be written have the
7842 * correct attributes. Also avoid UTF-8 characters.
7843 */
7844 i = col - wouldbe_col;
7845 if (i > 0)
7846 cost += i;
7847 if (cost < goto_cost && i > 0)
7848 {
7849 /*
7850 * Check if the attributes are correct without additionally
7851 * stopping highlighting.
7852 */
7853 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7854 while (i && *p++ == attr)
7855 --i;
7856 if (i != 0)
7857 {
7858 /*
7859 * Try if it works when highlighting is stopped here.
7860 */
7861 if (*--p == 0)
7862 {
7863 cost += noinvcurs;
7864 while (i && *p++ == 0)
7865 --i;
7866 }
7867 if (i != 0)
7868 cost = 999; /* different attributes, don't do it */
7869 }
7870#ifdef FEAT_MBYTE
7871 if (enc_utf8)
7872 {
7873 /* Don't use an UTF-8 char for positioning, it's slow. */
7874 for (i = wouldbe_col; i < col; ++i)
7875 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7876 {
7877 cost = 999;
7878 break;
7879 }
7880 }
7881#endif
7882 }
7883
7884 /*
7885 * We can do it without term_windgoto()!
7886 */
7887 if (cost < goto_cost)
7888 {
7889 if (plan == PLAN_LE)
7890 {
7891 if (noinvcurs)
7892 screen_stop_highlight();
7893 while (screen_cur_col > col)
7894 {
7895 out_str(bs);
7896 --screen_cur_col;
7897 }
7898 }
7899 else if (plan == PLAN_CR)
7900 {
7901 if (noinvcurs)
7902 screen_stop_highlight();
7903 out_char('\r');
7904 screen_cur_col = 0;
7905 }
7906 else if (plan == PLAN_NL)
7907 {
7908 if (noinvcurs)
7909 screen_stop_highlight();
7910 while (screen_cur_row < row)
7911 {
7912 out_char('\n');
7913 ++screen_cur_row;
7914 }
7915 screen_cur_col = 0;
7916 }
7917
7918 i = col - screen_cur_col;
7919 if (i > 0)
7920 {
7921 /*
7922 * Use cursor-right if it's one character only. Avoids
7923 * removing a line of pixels from the last bold char, when
7924 * using the bold trick in the GUI.
7925 */
7926 if (T_ND[0] != NUL && T_ND[1] == NUL)
7927 {
7928 while (i-- > 0)
7929 out_char(*T_ND);
7930 }
7931 else
7932 {
7933 int off;
7934
7935 off = LineOffset[row] + screen_cur_col;
7936 while (i-- > 0)
7937 {
7938 if (ScreenAttrs[off] != screen_attr)
7939 screen_stop_highlight();
7940#ifdef FEAT_MBYTE
7941 out_flush_check();
7942#endif
7943 out_char(ScreenLines[off]);
7944#ifdef FEAT_MBYTE
7945 if (enc_dbcs == DBCS_JPNU
7946 && ScreenLines[off] == 0x8e)
7947 out_char(ScreenLines2[off]);
7948#endif
7949 ++off;
7950 }
7951 }
7952 }
7953 }
7954 }
7955 else
7956 cost = 999;
7957
7958 if (cost >= goto_cost)
7959 {
7960 if (noinvcurs)
7961 screen_stop_highlight();
7962 if (row == screen_cur_row && (col > screen_cur_col) &&
7963 *T_CRI != NUL)
7964 term_cursor_right(col - screen_cur_col);
7965 else
7966 term_windgoto(row, col);
7967 }
7968 screen_cur_row = row;
7969 screen_cur_col = col;
7970 }
7971}
7972
7973/*
7974 * Set cursor to its position in the current window.
7975 */
7976 void
7977setcursor()
7978{
7979 if (redrawing())
7980 {
7981 validate_cursor();
7982 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7983 W_WINCOL(curwin) + (
7984#ifdef FEAT_RIGHTLEFT
7985 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7986# ifdef FEAT_MBYTE
7987 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7988# endif
7989 1)) :
7990#endif
7991 curwin->w_wcol));
7992 }
7993}
7994
7995
7996/*
7997 * insert 'line_count' lines at 'row' in window 'wp'
7998 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7999 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8000 * scrolling.
8001 * Returns FAIL if the lines are not inserted, OK for success.
8002 */
8003 int
8004win_ins_lines(wp, row, line_count, invalid, mayclear)
8005 win_T *wp;
8006 int row;
8007 int line_count;
8008 int invalid;
8009 int mayclear;
8010{
8011 int did_delete;
8012 int nextrow;
8013 int lastrow;
8014 int retval;
8015
8016 if (invalid)
8017 wp->w_lines_valid = 0;
8018
8019 if (wp->w_height < 5)
8020 return FAIL;
8021
8022 if (line_count > wp->w_height - row)
8023 line_count = wp->w_height - row;
8024
8025 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8026 if (retval != MAYBE)
8027 return retval;
8028
8029 /*
8030 * If there is a next window or a status line, we first try to delete the
8031 * lines at the bottom to avoid messing what is after the window.
8032 * If this fails and there are following windows, don't do anything to avoid
8033 * messing up those windows, better just redraw.
8034 */
8035 did_delete = FALSE;
8036#ifdef FEAT_WINDOWS
8037 if (wp->w_next != NULL || wp->w_status_height)
8038 {
8039 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8040 line_count, (int)Rows, FALSE, NULL) == OK)
8041 did_delete = TRUE;
8042 else if (wp->w_next)
8043 return FAIL;
8044 }
8045#endif
8046 /*
8047 * if no lines deleted, blank the lines that will end up below the window
8048 */
8049 if (!did_delete)
8050 {
8051#ifdef FEAT_WINDOWS
8052 wp->w_redr_status = TRUE;
8053#endif
8054 redraw_cmdline = TRUE;
8055 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8056 lastrow = nextrow + line_count;
8057 if (lastrow > Rows)
8058 lastrow = Rows;
8059 screen_fill(nextrow - line_count, lastrow - line_count,
8060 W_WINCOL(wp), (int)W_ENDCOL(wp),
8061 ' ', ' ', 0);
8062 }
8063
8064 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8065 == FAIL)
8066 {
8067 /* deletion will have messed up other windows */
8068 if (did_delete)
8069 {
8070#ifdef FEAT_WINDOWS
8071 wp->w_redr_status = TRUE;
8072#endif
8073 win_rest_invalid(W_NEXT(wp));
8074 }
8075 return FAIL;
8076 }
8077
8078 return OK;
8079}
8080
8081/*
8082 * delete "line_count" window lines at "row" in window "wp"
8083 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8084 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8085 * scrolling
8086 * Return OK for success, FAIL if the lines are not deleted.
8087 */
8088 int
8089win_del_lines(wp, row, line_count, invalid, mayclear)
8090 win_T *wp;
8091 int row;
8092 int line_count;
8093 int invalid;
8094 int mayclear;
8095{
8096 int retval;
8097
8098 if (invalid)
8099 wp->w_lines_valid = 0;
8100
8101 if (line_count > wp->w_height - row)
8102 line_count = wp->w_height - row;
8103
8104 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8105 if (retval != MAYBE)
8106 return retval;
8107
8108 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8109 (int)Rows, FALSE, NULL) == FAIL)
8110 return FAIL;
8111
8112#ifdef FEAT_WINDOWS
8113 /*
8114 * If there are windows or status lines below, try to put them at the
8115 * correct place. If we can't do that, they have to be redrawn.
8116 */
8117 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8118 {
8119 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8120 line_count, (int)Rows, NULL) == FAIL)
8121 {
8122 wp->w_redr_status = TRUE;
8123 win_rest_invalid(wp->w_next);
8124 }
8125 }
8126 /*
8127 * If this is the last window and there is no status line, redraw the
8128 * command line later.
8129 */
8130 else
8131#endif
8132 redraw_cmdline = TRUE;
8133 return OK;
8134}
8135
8136/*
8137 * Common code for win_ins_lines() and win_del_lines().
8138 * Returns OK or FAIL when the work has been done.
8139 * Returns MAYBE when not finished yet.
8140 */
8141 static int
8142win_do_lines(wp, row, line_count, mayclear, del)
8143 win_T *wp;
8144 int row;
8145 int line_count;
8146 int mayclear;
8147 int del;
8148{
8149 int retval;
8150
8151 if (!redrawing() || line_count <= 0)
8152 return FAIL;
8153
8154 /* only a few lines left: redraw is faster */
8155 if (mayclear && Rows - line_count < 5
8156#ifdef FEAT_VERTSPLIT
8157 && wp->w_width == Columns
8158#endif
8159 )
8160 {
8161 screenclear(); /* will set wp->w_lines_valid to 0 */
8162 return FAIL;
8163 }
8164
8165 /*
8166 * Delete all remaining lines
8167 */
8168 if (row + line_count >= wp->w_height)
8169 {
8170 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8171 W_WINCOL(wp), (int)W_ENDCOL(wp),
8172 ' ', ' ', 0);
8173 return OK;
8174 }
8175
8176 /*
8177 * when scrolling, the message on the command line should be cleared,
8178 * otherwise it will stay there forever.
8179 */
8180 clear_cmdline = TRUE;
8181
8182 /*
8183 * If the terminal can set a scroll region, use that.
8184 * Always do this in a vertically split window. This will redraw from
8185 * ScreenLines[] when t_CV isn't defined. That's faster than using
8186 * win_line().
8187 * Don't use a scroll region when we are going to redraw the text, writing
8188 * a character in the lower right corner of the scroll region causes a
8189 * scroll-up in the DJGPP version.
8190 */
8191 if (scroll_region
8192#ifdef FEAT_VERTSPLIT
8193 || W_WIDTH(wp) != Columns
8194#endif
8195 )
8196 {
8197#ifdef FEAT_VERTSPLIT
8198 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8199#endif
8200 scroll_region_set(wp, row);
8201 if (del)
8202 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8203 wp->w_height - row, FALSE, wp);
8204 else
8205 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8206 wp->w_height - row, wp);
8207#ifdef FEAT_VERTSPLIT
8208 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8209#endif
8210 scroll_region_reset();
8211 return retval;
8212 }
8213
8214#ifdef FEAT_WINDOWS
8215 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8216 return FAIL;
8217#endif
8218
8219 return MAYBE;
8220}
8221
8222/*
8223 * window 'wp' and everything after it is messed up, mark it for redraw
8224 */
8225 static void
8226win_rest_invalid(wp)
8227 win_T *wp;
8228{
8229#ifdef FEAT_WINDOWS
8230 while (wp != NULL)
8231#else
8232 if (wp != NULL)
8233#endif
8234 {
8235 redraw_win_later(wp, NOT_VALID);
8236#ifdef FEAT_WINDOWS
8237 wp->w_redr_status = TRUE;
8238 wp = wp->w_next;
8239#endif
8240 }
8241 redraw_cmdline = TRUE;
8242}
8243
8244/*
8245 * The rest of the routines in this file perform screen manipulations. The
8246 * given operation is performed physically on the screen. The corresponding
8247 * change is also made to the internal screen image. In this way, the editor
8248 * anticipates the effect of editing changes on the appearance of the screen.
8249 * That way, when we call screenupdate a complete redraw isn't usually
8250 * necessary. Another advantage is that we can keep adding code to anticipate
8251 * screen changes, and in the meantime, everything still works.
8252 */
8253
8254/*
8255 * types for inserting or deleting lines
8256 */
8257#define USE_T_CAL 1
8258#define USE_T_CDL 2
8259#define USE_T_AL 3
8260#define USE_T_CE 4
8261#define USE_T_DL 5
8262#define USE_T_SR 6
8263#define USE_NL 7
8264#define USE_T_CD 8
8265#define USE_REDRAW 9
8266
8267/*
8268 * insert lines on the screen and update ScreenLines[]
8269 * 'end' is the line after the scrolled part. Normally it is Rows.
8270 * When scrolling region used 'off' is the offset from the top for the region.
8271 * 'row' and 'end' are relative to the start of the region.
8272 *
8273 * return FAIL for failure, OK for success.
8274 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008275 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276screen_ins_lines(off, row, line_count, end, wp)
8277 int off;
8278 int row;
8279 int line_count;
8280 int end;
8281 win_T *wp; /* NULL or window to use width from */
8282{
8283 int i;
8284 int j;
8285 unsigned temp;
8286 int cursor_row;
8287 int type;
8288 int result_empty;
8289 int can_ce = can_clear(T_CE);
8290
8291 /*
8292 * FAIL if
8293 * - there is no valid screen
8294 * - the screen has to be redrawn completely
8295 * - the line count is less than one
8296 * - the line count is more than 'ttyscroll'
8297 */
8298 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8299 return FAIL;
8300
8301 /*
8302 * There are seven ways to insert lines:
8303 * 0. When in a vertically split window and t_CV isn't set, redraw the
8304 * characters from ScreenLines[].
8305 * 1. Use T_CD (clear to end of display) if it exists and the result of
8306 * the insert is just empty lines
8307 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8308 * present or line_count > 1. It looks better if we do all the inserts
8309 * at once.
8310 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8311 * insert is just empty lines and T_CE is not present or line_count >
8312 * 1.
8313 * 4. Use T_AL (insert line) if it exists.
8314 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8315 * just empty lines.
8316 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8317 * just empty lines.
8318 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8319 * the 'da' flag is not set or we have clear line capability.
8320 * 8. redraw the characters from ScreenLines[].
8321 *
8322 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8323 * the scrollbar for the window. It does have insert line, use that if it
8324 * exists.
8325 */
8326 result_empty = (row + line_count >= end);
8327#ifdef FEAT_VERTSPLIT
8328 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8329 type = USE_REDRAW;
8330 else
8331#endif
8332 if (can_clear(T_CD) && result_empty)
8333 type = USE_T_CD;
8334 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8335 type = USE_T_CAL;
8336 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8337 type = USE_T_CDL;
8338 else if (*T_AL != NUL)
8339 type = USE_T_AL;
8340 else if (can_ce && result_empty)
8341 type = USE_T_CE;
8342 else if (*T_DL != NUL && result_empty)
8343 type = USE_T_DL;
8344 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8345 type = USE_T_SR;
8346 else
8347 return FAIL;
8348
8349 /*
8350 * For clearing the lines screen_del_lines() is used. This will also take
8351 * care of t_db if necessary.
8352 */
8353 if (type == USE_T_CD || type == USE_T_CDL ||
8354 type == USE_T_CE || type == USE_T_DL)
8355 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8356
8357 /*
8358 * If text is retained below the screen, first clear or delete as many
8359 * lines at the bottom of the window as are about to be inserted so that
8360 * the deleted lines won't later surface during a screen_del_lines.
8361 */
8362 if (*T_DB)
8363 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8364
8365#ifdef FEAT_CLIPBOARD
8366 /* Remove a modeless selection when inserting lines halfway the screen
8367 * or not the full width of the screen. */
8368 if (off + row > 0
8369# ifdef FEAT_VERTSPLIT
8370 || (wp != NULL && wp->w_width != Columns)
8371# endif
8372 )
8373 clip_clear_selection();
8374 else
8375 clip_scroll_selection(-line_count);
8376#endif
8377
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378#ifdef FEAT_GUI
8379 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8380 * scrolling is actually carried out. */
8381 gui_dont_update_cursor();
8382#endif
8383
8384 if (*T_CCS != NUL) /* cursor relative to region */
8385 cursor_row = row;
8386 else
8387 cursor_row = row + off;
8388
8389 /*
8390 * Shift LineOffset[] line_count down to reflect the inserted lines.
8391 * Clear the inserted lines in ScreenLines[].
8392 */
8393 row += off;
8394 end += off;
8395 for (i = 0; i < line_count; ++i)
8396 {
8397#ifdef FEAT_VERTSPLIT
8398 if (wp != NULL && wp->w_width != Columns)
8399 {
8400 /* need to copy part of a line */
8401 j = end - 1 - i;
8402 while ((j -= line_count) >= row)
8403 linecopy(j + line_count, j, wp);
8404 j += line_count;
8405 if (can_clear((char_u *)" "))
8406 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8407 else
8408 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8409 LineWraps[j] = FALSE;
8410 }
8411 else
8412#endif
8413 {
8414 j = end - 1 - i;
8415 temp = LineOffset[j];
8416 while ((j -= line_count) >= row)
8417 {
8418 LineOffset[j + line_count] = LineOffset[j];
8419 LineWraps[j + line_count] = LineWraps[j];
8420 }
8421 LineOffset[j + line_count] = temp;
8422 LineWraps[j + line_count] = FALSE;
8423 if (can_clear((char_u *)" "))
8424 lineclear(temp, (int)Columns);
8425 else
8426 lineinvalid(temp, (int)Columns);
8427 }
8428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429
8430 screen_stop_highlight();
8431 windgoto(cursor_row, 0);
8432
8433#ifdef FEAT_VERTSPLIT
8434 /* redraw the characters */
8435 if (type == USE_REDRAW)
8436 redraw_block(row, end, wp);
8437 else
8438#endif
8439 if (type == USE_T_CAL)
8440 {
8441 term_append_lines(line_count);
8442 screen_start(); /* don't know where cursor is now */
8443 }
8444 else
8445 {
8446 for (i = 0; i < line_count; i++)
8447 {
8448 if (type == USE_T_AL)
8449 {
8450 if (i && cursor_row != 0)
8451 windgoto(cursor_row, 0);
8452 out_str(T_AL);
8453 }
8454 else /* type == USE_T_SR */
8455 out_str(T_SR);
8456 screen_start(); /* don't know where cursor is now */
8457 }
8458 }
8459
8460 /*
8461 * With scroll-reverse and 'da' flag set we need to clear the lines that
8462 * have been scrolled down into the region.
8463 */
8464 if (type == USE_T_SR && *T_DA)
8465 {
8466 for (i = 0; i < line_count; ++i)
8467 {
8468 windgoto(off + i, 0);
8469 out_str(T_CE);
8470 screen_start(); /* don't know where cursor is now */
8471 }
8472 }
8473
8474#ifdef FEAT_GUI
8475 gui_can_update_cursor();
8476 if (gui.in_use)
8477 out_flush(); /* always flush after a scroll */
8478#endif
8479 return OK;
8480}
8481
8482/*
8483 * delete lines on the screen and update ScreenLines[]
8484 * 'end' is the line after the scrolled part. Normally it is Rows.
8485 * When scrolling region used 'off' is the offset from the top for the region.
8486 * 'row' and 'end' are relative to the start of the region.
8487 *
8488 * Return OK for success, FAIL if the lines are not deleted.
8489 */
8490/*ARGSUSED*/
8491 int
8492screen_del_lines(off, row, line_count, end, force, wp)
8493 int off;
8494 int row;
8495 int line_count;
8496 int end;
8497 int force; /* even when line_count > p_ttyscroll */
8498 win_T *wp; /* NULL or window to use width from */
8499{
8500 int j;
8501 int i;
8502 unsigned temp;
8503 int cursor_row;
8504 int cursor_end;
8505 int result_empty; /* result is empty until end of region */
8506 int can_delete; /* deleting line codes can be used */
8507 int type;
8508
8509 /*
8510 * FAIL if
8511 * - there is no valid screen
8512 * - the screen has to be redrawn completely
8513 * - the line count is less than one
8514 * - the line count is more than 'ttyscroll'
8515 */
8516 if (!screen_valid(TRUE) || line_count <= 0 ||
8517 (!force && line_count > p_ttyscroll))
8518 return FAIL;
8519
8520 /*
8521 * Check if the rest of the current region will become empty.
8522 */
8523 result_empty = row + line_count >= end;
8524
8525 /*
8526 * We can delete lines only when 'db' flag not set or when 'ce' option
8527 * available.
8528 */
8529 can_delete = (*T_DB == NUL || can_clear(T_CE));
8530
8531 /*
8532 * There are six ways to delete lines:
8533 * 0. When in a vertically split window and t_CV isn't set, redraw the
8534 * characters from ScreenLines[].
8535 * 1. Use T_CD if it exists and the result is empty.
8536 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8537 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8538 * none of the other ways work.
8539 * 4. Use T_CE (erase line) if the result is empty.
8540 * 5. Use T_DL (delete line) if it exists.
8541 * 6. redraw the characters from ScreenLines[].
8542 */
8543#ifdef FEAT_VERTSPLIT
8544 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8545 type = USE_REDRAW;
8546 else
8547#endif
8548 if (can_clear(T_CD) && result_empty)
8549 type = USE_T_CD;
8550#if defined(__BEOS__) && defined(BEOS_DR8)
8551 /*
8552 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8553 * its internal termcap... this works okay for tests which test *T_DB !=
8554 * NUL. It has the disadvantage that the user cannot use any :set t_*
8555 * command to get T_DB (back) to empty_option, only :set term=... will do
8556 * the trick...
8557 * Anyway, this hack will hopefully go away with the next OS release.
8558 * (Olaf Seibert)
8559 */
8560 else if (row == 0 && T_DB == empty_option
8561 && (line_count == 1 || *T_CDL == NUL))
8562#else
8563 else if (row == 0 && (
8564#ifndef AMIGA
8565 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8566 * up, so use delete-line command */
8567 line_count == 1 ||
8568#endif
8569 *T_CDL == NUL))
8570#endif
8571 type = USE_NL;
8572 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8573 type = USE_T_CDL;
8574 else if (can_clear(T_CE) && result_empty
8575#ifdef FEAT_VERTSPLIT
8576 && (wp == NULL || wp->w_width == Columns)
8577#endif
8578 )
8579 type = USE_T_CE;
8580 else if (*T_DL != NUL && can_delete)
8581 type = USE_T_DL;
8582 else if (*T_CDL != NUL && can_delete)
8583 type = USE_T_CDL;
8584 else
8585 return FAIL;
8586
8587#ifdef FEAT_CLIPBOARD
8588 /* Remove a modeless selection when deleting lines halfway the screen or
8589 * not the full width of the screen. */
8590 if (off + row > 0
8591# ifdef FEAT_VERTSPLIT
8592 || (wp != NULL && wp->w_width != Columns)
8593# endif
8594 )
8595 clip_clear_selection();
8596 else
8597 clip_scroll_selection(line_count);
8598#endif
8599
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600#ifdef FEAT_GUI
8601 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8602 * scrolling is actually carried out. */
8603 gui_dont_update_cursor();
8604#endif
8605
8606 if (*T_CCS != NUL) /* cursor relative to region */
8607 {
8608 cursor_row = row;
8609 cursor_end = end;
8610 }
8611 else
8612 {
8613 cursor_row = row + off;
8614 cursor_end = end + off;
8615 }
8616
8617 /*
8618 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8619 * Clear the inserted lines in ScreenLines[].
8620 */
8621 row += off;
8622 end += off;
8623 for (i = 0; i < line_count; ++i)
8624 {
8625#ifdef FEAT_VERTSPLIT
8626 if (wp != NULL && wp->w_width != Columns)
8627 {
8628 /* need to copy part of a line */
8629 j = row + i;
8630 while ((j += line_count) <= end - 1)
8631 linecopy(j - line_count, j, wp);
8632 j -= line_count;
8633 if (can_clear((char_u *)" "))
8634 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8635 else
8636 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8637 LineWraps[j] = FALSE;
8638 }
8639 else
8640#endif
8641 {
8642 /* whole width, moving the line pointers is faster */
8643 j = row + i;
8644 temp = LineOffset[j];
8645 while ((j += line_count) <= end - 1)
8646 {
8647 LineOffset[j - line_count] = LineOffset[j];
8648 LineWraps[j - line_count] = LineWraps[j];
8649 }
8650 LineOffset[j - line_count] = temp;
8651 LineWraps[j - line_count] = FALSE;
8652 if (can_clear((char_u *)" "))
8653 lineclear(temp, (int)Columns);
8654 else
8655 lineinvalid(temp, (int)Columns);
8656 }
8657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
8659 screen_stop_highlight();
8660
8661#ifdef FEAT_VERTSPLIT
8662 /* redraw the characters */
8663 if (type == USE_REDRAW)
8664 redraw_block(row, end, wp);
8665 else
8666#endif
8667 if (type == USE_T_CD) /* delete the lines */
8668 {
8669 windgoto(cursor_row, 0);
8670 out_str(T_CD);
8671 screen_start(); /* don't know where cursor is now */
8672 }
8673 else if (type == USE_T_CDL)
8674 {
8675 windgoto(cursor_row, 0);
8676 term_delete_lines(line_count);
8677 screen_start(); /* don't know where cursor is now */
8678 }
8679 /*
8680 * Deleting lines at top of the screen or scroll region: Just scroll
8681 * the whole screen (scroll region) up by outputting newlines on the
8682 * last line.
8683 */
8684 else if (type == USE_NL)
8685 {
8686 windgoto(cursor_end - 1, 0);
8687 for (i = line_count; --i >= 0; )
8688 out_char('\n'); /* cursor will remain on same line */
8689 }
8690 else
8691 {
8692 for (i = line_count; --i >= 0; )
8693 {
8694 if (type == USE_T_DL)
8695 {
8696 windgoto(cursor_row, 0);
8697 out_str(T_DL); /* delete a line */
8698 }
8699 else /* type == USE_T_CE */
8700 {
8701 windgoto(cursor_row + i, 0);
8702 out_str(T_CE); /* erase a line */
8703 }
8704 screen_start(); /* don't know where cursor is now */
8705 }
8706 }
8707
8708 /*
8709 * If the 'db' flag is set, we need to clear the lines that have been
8710 * scrolled up at the bottom of the region.
8711 */
8712 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8713 {
8714 for (i = line_count; i > 0; --i)
8715 {
8716 windgoto(cursor_end - i, 0);
8717 out_str(T_CE); /* erase a line */
8718 screen_start(); /* don't know where cursor is now */
8719 }
8720 }
8721
8722#ifdef FEAT_GUI
8723 gui_can_update_cursor();
8724 if (gui.in_use)
8725 out_flush(); /* always flush after a scroll */
8726#endif
8727
8728 return OK;
8729}
8730
8731/*
8732 * show the current mode and ruler
8733 *
8734 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8735 * If clear_cmdline is FALSE there may be a message there that needs to be
8736 * cleared only if a mode is shown.
8737 * Return the length of the message (0 if no message).
8738 */
8739 int
8740showmode()
8741{
8742 int need_clear;
8743 int length = 0;
8744 int do_mode;
8745 int attr;
8746 int nwr_save;
8747#ifdef FEAT_INS_EXPAND
8748 int sub_attr;
8749#endif
8750
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008751 do_mode = ((p_smd && msg_silent == 0)
8752 && ((State & INSERT)
8753 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754#ifdef FEAT_VISUAL
8755 || VIsual_active
8756#endif
8757 ));
8758 if (do_mode || Recording)
8759 {
8760 /*
8761 * Don't show mode right now, when not redrawing or inside a mapping.
8762 * Call char_avail() only when we are going to show something, because
8763 * it takes a bit of time.
8764 */
8765 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8766 {
8767 redraw_cmdline = TRUE; /* show mode later */
8768 return 0;
8769 }
8770
8771 nwr_save = need_wait_return;
8772
8773 /* wait a bit before overwriting an important message */
8774 check_for_delay(FALSE);
8775
8776 /* if the cmdline is more than one line high, erase top lines */
8777 need_clear = clear_cmdline;
8778 if (clear_cmdline && cmdline_row < Rows - 1)
8779 msg_clr_cmdline(); /* will reset clear_cmdline */
8780
8781 /* Position on the last line in the window, column 0 */
8782 msg_pos_mode();
8783 cursor_off();
8784 attr = hl_attr(HLF_CM); /* Highlight mode */
8785 if (do_mode)
8786 {
8787 MSG_PUTS_ATTR("--", attr);
8788#if defined(FEAT_XIM)
8789 if (xic != NULL && im_get_status() && !p_imdisable
8790 && curbuf->b_p_iminsert == B_IMODE_IM)
8791# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8792 MSG_PUTS_ATTR(" IM", attr);
8793# else
8794 MSG_PUTS_ATTR(" XIM", attr);
8795# endif
8796#endif
8797#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8798 if (gui.in_use)
8799 {
8800 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008801 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 }
8803#endif
8804#ifdef FEAT_INS_EXPAND
8805 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8806 {
8807 /* These messages can get long, avoid a wrap in a narrow
8808 * window. Prefer showing edit_submode_extra. */
8809 length = (Rows - msg_row) * Columns - 3;
8810 if (edit_submode_extra != NULL)
8811 length -= vim_strsize(edit_submode_extra);
8812 if (length > 0)
8813 {
8814 if (edit_submode_pre != NULL)
8815 length -= vim_strsize(edit_submode_pre);
8816 if (length - vim_strsize(edit_submode) > 0)
8817 {
8818 if (edit_submode_pre != NULL)
8819 msg_puts_attr(edit_submode_pre, attr);
8820 msg_puts_attr(edit_submode, attr);
8821 }
8822 if (edit_submode_extra != NULL)
8823 {
8824 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8825 if ((int)edit_submode_highl < (int)HLF_COUNT)
8826 sub_attr = hl_attr(edit_submode_highl);
8827 else
8828 sub_attr = attr;
8829 msg_puts_attr(edit_submode_extra, sub_attr);
8830 }
8831 }
8832 length = 0;
8833 }
8834 else
8835#endif
8836 {
8837#ifdef FEAT_VREPLACE
8838 if (State & VREPLACE_FLAG)
8839 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8840 else
8841#endif
8842 if (State & REPLACE_FLAG)
8843 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8844 else if (State & INSERT)
8845 {
8846#ifdef FEAT_RIGHTLEFT
8847 if (p_ri)
8848 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8849#endif
8850 MSG_PUTS_ATTR(_(" INSERT"), attr);
8851 }
8852 else if (restart_edit == 'I')
8853 MSG_PUTS_ATTR(_(" (insert)"), attr);
8854 else if (restart_edit == 'R')
8855 MSG_PUTS_ATTR(_(" (replace)"), attr);
8856 else if (restart_edit == 'V')
8857 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8858#ifdef FEAT_RIGHTLEFT
8859 if (p_hkmap)
8860 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8861# ifdef FEAT_FKMAP
8862 if (p_fkmap)
8863 MSG_PUTS_ATTR(farsi_text_5, attr);
8864# endif
8865#endif
8866#ifdef FEAT_KEYMAP
8867 if (State & LANGMAP)
8868 {
8869# ifdef FEAT_ARABIC
8870 if (curwin->w_p_arab)
8871 MSG_PUTS_ATTR(_(" Arabic"), attr);
8872 else
8873# endif
8874 MSG_PUTS_ATTR(_(" (lang)"), attr);
8875 }
8876#endif
8877 if ((State & INSERT) && p_paste)
8878 MSG_PUTS_ATTR(_(" (paste)"), attr);
8879
8880#ifdef FEAT_VISUAL
8881 if (VIsual_active)
8882 {
8883 char *p;
8884
8885 /* Don't concatenate separate words to avoid translation
8886 * problems. */
8887 switch ((VIsual_select ? 4 : 0)
8888 + (VIsual_mode == Ctrl_V) * 2
8889 + (VIsual_mode == 'V'))
8890 {
8891 case 0: p = N_(" VISUAL"); break;
8892 case 1: p = N_(" VISUAL LINE"); break;
8893 case 2: p = N_(" VISUAL BLOCK"); break;
8894 case 4: p = N_(" SELECT"); break;
8895 case 5: p = N_(" SELECT LINE"); break;
8896 default: p = N_(" SELECT BLOCK"); break;
8897 }
8898 MSG_PUTS_ATTR(_(p), attr);
8899 }
8900#endif
8901 MSG_PUTS_ATTR(" --", attr);
8902 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008903
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 need_clear = TRUE;
8905 }
8906 if (Recording
8907#ifdef FEAT_INS_EXPAND
8908 && edit_submode == NULL /* otherwise it gets too long */
8909#endif
8910 )
8911 {
8912 MSG_PUTS_ATTR(_("recording"), attr);
8913 need_clear = TRUE;
8914 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008915
8916 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 if (need_clear || clear_cmdline)
8918 msg_clr_eos();
8919 msg_didout = FALSE; /* overwrite this message */
8920 length = msg_col;
8921 msg_col = 0;
8922 need_wait_return = nwr_save; /* never ask for hit-return for this */
8923 }
8924 else if (clear_cmdline && msg_silent == 0)
8925 /* Clear the whole command line. Will reset "clear_cmdline". */
8926 msg_clr_cmdline();
8927
8928#ifdef FEAT_CMDL_INFO
8929# ifdef FEAT_VISUAL
8930 /* In Visual mode the size of the selected area must be redrawn. */
8931 if (VIsual_active)
8932 clear_showcmd();
8933# endif
8934
8935 /* If the last window has no status line, the ruler is after the mode
8936 * message and must be redrawn */
8937 if (redrawing()
8938# ifdef FEAT_WINDOWS
8939 && lastwin->w_status_height == 0
8940# endif
8941 )
8942 win_redr_ruler(lastwin, TRUE);
8943#endif
8944 redraw_cmdline = FALSE;
8945 clear_cmdline = FALSE;
8946
8947 return length;
8948}
8949
8950/*
8951 * Position for a mode message.
8952 */
8953 static void
8954msg_pos_mode()
8955{
8956 msg_col = 0;
8957 msg_row = Rows - 1;
8958}
8959
8960/*
8961 * Delete mode message. Used when ESC is typed which is expected to end
8962 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008963 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 */
8965 void
8966unshowmode(force)
8967 int force;
8968{
8969 /*
8970 * Don't delete it right now, when not redrawing or insided a mapping.
8971 */
8972 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8973 redraw_cmdline = TRUE; /* delete mode later */
8974 else
8975 {
8976 msg_pos_mode();
8977 if (Recording)
8978 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8979 msg_clr_eos();
8980 }
8981}
8982
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008983#if defined(FEAT_WINDOWS)
8984/*
8985 * Draw the tab pages line at the top of the Vim window.
8986 */
8987 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008988draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008989{
8990 int tabcount = 0;
8991 tabpage_T *tp;
8992 int tabwidth;
8993 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008994 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008995 int attr;
8996 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008997 win_T *cwp;
8998 int wincount;
8999 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009000 int c;
9001 int len;
9002 int attr_sel = hl_attr(HLF_TPS);
9003 int attr_nosel = hl_attr(HLF_TP);
9004 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009005 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009006 int room;
9007 int use_sep_chars = (t_colors < 8
9008#ifdef FEAT_GUI
9009 && !gui.in_use
9010#endif
9011 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009012
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009013 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009014
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009015#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009016 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009017 if (gui_use_tabline())
9018 {
9019 gui_update_tabline();
9020 return;
9021 }
9022#endif
9023
9024 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009025 return;
9026
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009027#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009028
9029 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9030 for (scol = 0; scol < Columns; ++scol)
9031 TabPageIdxs[scol] = 0;
9032
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009033 /* Use the 'tabline' option if it's set. */
9034 if (*p_tal != NUL)
9035 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009036 int save_called_emsg = called_emsg;
9037
9038 /* Check for an error. If there is one we would loop in redrawing the
9039 * screen. Avoid that by making 'tabline' empty. */
9040 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009041 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009042 if (called_emsg)
9043 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009044 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009045 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009046 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009047 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009048#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009049 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009050 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9051 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009052
Bram Moolenaar238a5642006-02-21 22:12:05 +00009053 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9054 if (tabwidth < 6)
9055 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009056
Bram Moolenaar238a5642006-02-21 22:12:05 +00009057 attr = attr_nosel;
9058 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009059 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009060 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9061 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009062 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009063 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009064
Bram Moolenaar238a5642006-02-21 22:12:05 +00009065 if (tp->tp_topframe == topframe)
9066 attr = attr_sel;
9067 if (use_sep_chars && col > 0)
9068 screen_putchar('|', 0, col++, attr);
9069
9070 if (tp->tp_topframe != topframe)
9071 attr = attr_nosel;
9072
9073 screen_putchar(' ', 0, col++, attr);
9074
9075 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009076 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009077 cwp = curwin;
9078 wp = firstwin;
9079 }
9080 else
9081 {
9082 cwp = tp->tp_curwin;
9083 wp = tp->tp_firstwin;
9084 }
9085
9086 modified = FALSE;
9087 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9088 if (bufIsChanged(wp->w_buffer))
9089 modified = TRUE;
9090 if (modified || wincount > 1)
9091 {
9092 if (wincount > 1)
9093 {
9094 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009095 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009096 if (col + len >= Columns - 3)
9097 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009098 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009099#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009100 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009101#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009102 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009103#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009104 );
9105 col += len;
9106 }
9107 if (modified)
9108 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9109 screen_putchar(' ', 0, col++, attr);
9110 }
9111
9112 room = scol - col + tabwidth - 1;
9113 if (room > 0)
9114 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009115 /* Get buffer name in NameBuff[] */
9116 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009117 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009118 len = vim_strsize(NameBuff);
9119 p = NameBuff;
9120#ifdef FEAT_MBYTE
9121 if (has_mbyte)
9122 while (len > room)
9123 {
9124 len -= ptr2cells(p);
9125 mb_ptr_adv(p);
9126 }
9127 else
9128#endif
9129 if (len > room)
9130 {
9131 p += len - room;
9132 len = room;
9133 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009134 if (len > Columns - col - 1)
9135 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009136
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009137 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009138 col += len;
9139 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009140 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009141
9142 /* Store the tab page number in TabPageIdxs[], so that
9143 * jump_to_mouse() knows where each one is. */
9144 ++tabcount;
9145 while (scol < col)
9146 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009147 }
9148
Bram Moolenaar238a5642006-02-21 22:12:05 +00009149 if (use_sep_chars)
9150 c = '_';
9151 else
9152 c = ' ';
9153 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009154
9155 /* Put an "X" for closing the current tab if there are several. */
9156 if (first_tabpage->tp_next != NULL)
9157 {
9158 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9159 TabPageIdxs[Columns - 1] = -999;
9160 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009161 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009162
9163 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9164 * set. */
9165 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009166}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009167
9168/*
9169 * Get buffer name for "buf" into NameBuff[].
9170 * Takes care of special buffer names and translates special characters.
9171 */
9172 void
9173get_trans_bufname(buf)
9174 buf_T *buf;
9175{
9176 if (buf_spname(buf) != NULL)
9177 STRCPY(NameBuff, buf_spname(buf));
9178 else
9179 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9180 trans_characters(NameBuff, MAXPATHL);
9181}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009182#endif
9183
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9185/*
9186 * Get the character to use in a status line. Get its attributes in "*attr".
9187 */
9188 static int
9189fillchar_status(attr, is_curwin)
9190 int *attr;
9191 int is_curwin;
9192{
9193 int fill;
9194 if (is_curwin)
9195 {
9196 *attr = hl_attr(HLF_S);
9197 fill = fill_stl;
9198 }
9199 else
9200 {
9201 *attr = hl_attr(HLF_SNC);
9202 fill = fill_stlnc;
9203 }
9204 /* Use fill when there is highlighting, and highlighting of current
9205 * window differs, or the fillchars differ, or this is not the
9206 * current window */
9207 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9208 || !is_curwin || firstwin == lastwin)
9209 || (fill_stl != fill_stlnc)))
9210 return fill;
9211 if (is_curwin)
9212 return '^';
9213 return '=';
9214}
9215#endif
9216
9217#ifdef FEAT_VERTSPLIT
9218/*
9219 * Get the character to use in a separator between vertically split windows.
9220 * Get its attributes in "*attr".
9221 */
9222 static int
9223fillchar_vsep(attr)
9224 int *attr;
9225{
9226 *attr = hl_attr(HLF_C);
9227 if (*attr == 0 && fill_vert == ' ')
9228 return '|';
9229 else
9230 return fill_vert;
9231}
9232#endif
9233
9234/*
9235 * Return TRUE if redrawing should currently be done.
9236 */
9237 int
9238redrawing()
9239{
9240 return (!RedrawingDisabled
9241 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9242}
9243
9244/*
9245 * Return TRUE if printing messages should currently be done.
9246 */
9247 int
9248messaging()
9249{
9250 return (!(p_lz && char_avail() && !KeyTyped));
9251}
9252
9253/*
9254 * Show current status info in ruler and various other places
9255 * If always is FALSE, only show ruler if position has changed.
9256 */
9257 void
9258showruler(always)
9259 int always;
9260{
9261 if (!always && !redrawing())
9262 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009263#ifdef FEAT_INS_EXPAND
9264 if (pum_visible())
9265 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009266# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009267 /* Don't redraw right now, do it later. */
9268 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009269# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009270 return;
9271 }
9272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009274 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009275 {
9276 redraw_custum_statusline(curwin);
9277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 else
9279#endif
9280#ifdef FEAT_CMDL_INFO
9281 win_redr_ruler(curwin, always);
9282#endif
9283
9284#ifdef FEAT_TITLE
9285 if (need_maketitle
9286# ifdef FEAT_STL_OPT
9287 || (p_icon && (stl_syntax & STL_IN_ICON))
9288 || (p_title && (stl_syntax & STL_IN_TITLE))
9289# endif
9290 )
9291 maketitle();
9292#endif
9293}
9294
9295#ifdef FEAT_CMDL_INFO
9296 static void
9297win_redr_ruler(wp, always)
9298 win_T *wp;
9299 int always;
9300{
9301 char_u buffer[70];
9302 int row;
9303 int fillchar;
9304 int attr;
9305 int empty_line = FALSE;
9306 colnr_T virtcol;
9307 int i;
9308 int o;
9309#ifdef FEAT_VERTSPLIT
9310 int this_ru_col;
9311 int off = 0;
9312 int width = Columns;
9313# define WITH_OFF(x) x
9314# define WITH_WIDTH(x) x
9315#else
9316# define WITH_OFF(x) 0
9317# define WITH_WIDTH(x) Columns
9318# define this_ru_col ru_col
9319#endif
9320
9321 /* If 'ruler' off or redrawing disabled, don't do anything */
9322 if (!p_ru)
9323 return;
9324
9325 /*
9326 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9327 * after deleting lines, before cursor.lnum is corrected.
9328 */
9329 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9330 return;
9331
9332#ifdef FEAT_INS_EXPAND
9333 /* Don't draw the ruler while doing insert-completion, it might overwrite
9334 * the (long) mode message. */
9335# ifdef FEAT_WINDOWS
9336 if (wp == lastwin && lastwin->w_status_height == 0)
9337# endif
9338 if (edit_submode != NULL)
9339 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009340 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9341 if (pum_visible())
9342 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343#endif
9344
9345#ifdef FEAT_STL_OPT
9346 if (*p_ruf)
9347 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009348 int save_called_emsg = called_emsg;
9349
9350 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009352 if (called_emsg)
9353 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009354 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009355 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 return;
9357 }
9358#endif
9359
9360 /*
9361 * Check if not in Insert mode and the line is empty (will show "0-1").
9362 */
9363 if (!(State & INSERT)
9364 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9365 empty_line = TRUE;
9366
9367 /*
9368 * Only draw the ruler when something changed.
9369 */
9370 validate_virtcol_win(wp);
9371 if ( redraw_cmdline
9372 || always
9373 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9374 || wp->w_cursor.col != wp->w_ru_cursor.col
9375 || wp->w_virtcol != wp->w_ru_virtcol
9376#ifdef FEAT_VIRTUALEDIT
9377 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9378#endif
9379 || wp->w_topline != wp->w_ru_topline
9380 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9381#ifdef FEAT_DIFF
9382 || wp->w_topfill != wp->w_ru_topfill
9383#endif
9384 || empty_line != wp->w_ru_empty)
9385 {
9386 cursor_off();
9387#ifdef FEAT_WINDOWS
9388 if (wp->w_status_height)
9389 {
9390 row = W_WINROW(wp) + wp->w_height;
9391 fillchar = fillchar_status(&attr, wp == curwin);
9392# ifdef FEAT_VERTSPLIT
9393 off = W_WINCOL(wp);
9394 width = W_WIDTH(wp);
9395# endif
9396 }
9397 else
9398#endif
9399 {
9400 row = Rows - 1;
9401 fillchar = ' ';
9402 attr = 0;
9403#ifdef FEAT_VERTSPLIT
9404 width = Columns;
9405 off = 0;
9406#endif
9407 }
9408
9409 /* In list mode virtcol needs to be recomputed */
9410 virtcol = wp->w_virtcol;
9411 if (wp->w_p_list && lcs_tab1 == NUL)
9412 {
9413 wp->w_p_list = FALSE;
9414 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9415 wp->w_p_list = TRUE;
9416 }
9417
9418 /*
9419 * Some sprintfs return the length, some return a pointer.
9420 * To avoid portability problems we use strlen() here.
9421 */
9422 sprintf((char *)buffer, "%ld,",
9423 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9424 ? 0L
9425 : (long)(wp->w_cursor.lnum));
9426 col_print(buffer + STRLEN(buffer),
9427 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9428 (int)virtcol + 1);
9429
9430 /*
9431 * Add a "50%" if there is room for it.
9432 * On the last line, don't print in the last column (scrolls the
9433 * screen up on some terminals).
9434 */
9435 i = (int)STRLEN(buffer);
9436 get_rel_pos(wp, buffer + i + 1);
9437 o = i + vim_strsize(buffer + i + 1);
9438#ifdef FEAT_WINDOWS
9439 if (wp->w_status_height == 0) /* can't use last char of screen */
9440#endif
9441 ++o;
9442#ifdef FEAT_VERTSPLIT
9443 this_ru_col = ru_col - (Columns - width);
9444 if (this_ru_col < 0)
9445 this_ru_col = 0;
9446#endif
9447 /* Never use more than half the window/screen width, leave the other
9448 * half for the filename. */
9449 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9450 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9451 if (this_ru_col + o < WITH_WIDTH(width))
9452 {
9453 while (this_ru_col + o < WITH_WIDTH(width))
9454 {
9455#ifdef FEAT_MBYTE
9456 if (has_mbyte)
9457 i += (*mb_char2bytes)(fillchar, buffer + i);
9458 else
9459#endif
9460 buffer[i++] = fillchar;
9461 ++o;
9462 }
9463 get_rel_pos(wp, buffer + i);
9464 }
9465 /* Truncate at window boundary. */
9466#ifdef FEAT_MBYTE
9467 if (has_mbyte)
9468 {
9469 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009470 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 {
9472 o += (*mb_ptr2cells)(buffer + i);
9473 if (this_ru_col + o > WITH_WIDTH(width))
9474 {
9475 buffer[i] = NUL;
9476 break;
9477 }
9478 }
9479 }
9480 else
9481#endif
9482 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9483 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9484
9485 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9486 i = redraw_cmdline;
9487 screen_fill(row, row + 1,
9488 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9489 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9490 fillchar, fillchar, attr);
9491 /* don't redraw the cmdline because of showing the ruler */
9492 redraw_cmdline = i;
9493 wp->w_ru_cursor = wp->w_cursor;
9494 wp->w_ru_virtcol = wp->w_virtcol;
9495 wp->w_ru_empty = empty_line;
9496 wp->w_ru_topline = wp->w_topline;
9497 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9498#ifdef FEAT_DIFF
9499 wp->w_ru_topfill = wp->w_topfill;
9500#endif
9501 }
9502}
9503#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009504
9505#if defined(FEAT_LINEBREAK) || defined(PROTO)
9506/*
9507 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009508 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009509 * Otherwise it depends on 'numberwidth' and the line count.
9510 */
9511 int
9512number_width(wp)
9513 win_T *wp;
9514{
9515 int n;
9516 linenr_T lnum;
9517
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009518 lnum = wp->w_buffer->b_ml.ml_line_count;
9519 if (lnum == wp->w_nrwidth_line_count)
9520 return wp->w_nrwidth_width;
9521 wp->w_nrwidth_line_count = lnum;
9522
9523 n = 0;
9524 do
9525 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009526 lnum /= 10;
9527 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009528 } while (lnum > 0);
9529
9530 /* 'numberwidth' gives the minimal width plus one */
9531 if (n < wp->w_p_nuw - 1)
9532 n = wp->w_p_nuw - 1;
9533
9534 wp->w_nrwidth_width = n;
9535 return n;
9536}
9537#endif