blob: ed3a1c1d523ca0ffc2d7476182629fbd649ec20d [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 ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002308#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (u8c >= 0x10000)
2310 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2311 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002314 for (i = 0; i < Screen_mco; ++i)
2315 {
2316 ScreenLinesC[i][idx] = u8cc[i];
2317 if (u8cc[i] == 0)
2318 break;
2319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 if (cells > 1)
2322 ScreenLines[idx + 1] = 0;
2323 }
2324 else if (cells > 1) /* double-byte character */
2325 {
2326 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2327 ScreenLines2[idx] = p[1];
2328 else
2329 ScreenLines[idx + 1] = p[1];
2330 }
2331 col += cells;
2332 idx += cells;
2333 p += c_len;
2334 }
2335 }
2336 else
2337#endif
2338 {
2339 len = (int)STRLEN(text);
2340 if (len > W_WIDTH(wp) - col)
2341 len = W_WIDTH(wp) - col;
2342 if (len > 0)
2343 {
2344#ifdef FEAT_RIGHTLEFT
2345 if (wp->w_p_rl)
2346 STRNCPY(current_ScreenLine, text, len);
2347 else
2348#endif
2349 STRNCPY(current_ScreenLine + col, text, len);
2350 col += len;
2351 }
2352 }
2353
2354 /* Fill the rest of the line with the fold filler */
2355#ifdef FEAT_RIGHTLEFT
2356 if (wp->w_p_rl)
2357 col -= txtcol;
2358#endif
2359 while (col < W_WIDTH(wp)
2360#ifdef FEAT_RIGHTLEFT
2361 - (wp->w_p_rl ? txtcol : 0)
2362#endif
2363 )
2364 {
2365#ifdef FEAT_MBYTE
2366 if (enc_utf8)
2367 {
2368 if (fill_fold >= 0x80)
2369 {
2370 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002371 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373 else
2374 ScreenLinesUC[off + col] = 0;
2375 }
2376#endif
2377 ScreenLines[off + col++] = fill_fold;
2378 }
2379
2380 if (text != buf)
2381 vim_free(text);
2382
2383 /*
2384 * 6. set highlighting for the Visual area an other text.
2385 * If all folded lines are in the Visual area, highlight the line.
2386 */
2387#ifdef FEAT_VISUAL
2388 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2389 {
2390 if (ltoreq(curwin->w_cursor, VIsual))
2391 {
2392 /* Visual is after curwin->w_cursor */
2393 top = &curwin->w_cursor;
2394 bot = &VIsual;
2395 }
2396 else
2397 {
2398 /* Visual is before curwin->w_cursor */
2399 top = &VIsual;
2400 bot = &curwin->w_cursor;
2401 }
2402 if (lnum >= top->lnum
2403 && lnume <= bot->lnum
2404 && (VIsual_mode != 'v'
2405 || ((lnum > top->lnum
2406 || (lnum == top->lnum
2407 && top->col == 0))
2408 && (lnume < bot->lnum
2409 || (lnume == bot->lnum
2410 && (bot->col - (*p_sel == 'e'))
2411 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2412 {
2413 if (VIsual_mode == Ctrl_V)
2414 {
2415 /* Visual block mode: highlight the chars part of the block */
2416 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2417 {
2418 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2419 len = wp->w_old_cursor_lcol;
2420 else
2421 len = W_WIDTH(wp) - txtcol;
2422 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002423 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425 }
2426 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002429 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432 }
2433#endif
2434
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002435#ifdef FEAT_SYN_HL
2436 /* Show 'cursorcolumn' in the fold line. */
2437 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2438 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2439 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2443 (int)W_WIDTH(wp), FALSE);
2444
2445 /*
2446 * Update w_cline_height and w_cline_folded if the cursor line was
2447 * updated (saves a call to plines() later).
2448 */
2449 if (wp == curwin
2450 && lnum <= curwin->w_cursor.lnum
2451 && lnume >= curwin->w_cursor.lnum)
2452 {
2453 curwin->w_cline_row = row;
2454 curwin->w_cline_height = 1;
2455 curwin->w_cline_folded = TRUE;
2456 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2457 }
2458}
2459
2460/*
2461 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2462 */
2463 static void
2464copy_text_attr(off, buf, len, attr)
2465 int off;
2466 char_u *buf;
2467 int len;
2468 int attr;
2469{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002470 int i;
2471
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 mch_memmove(ScreenLines + off, buf, (size_t)len);
2473# ifdef FEAT_MBYTE
2474 if (enc_utf8)
2475 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2476# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002477 for (i = 0; i < len; ++i)
2478 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479}
2480
2481/*
2482 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002483 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
2485 static void
2486fill_foldcolumn(p, wp, closed, lnum)
2487 char_u *p;
2488 win_T *wp;
2489 int closed; /* TRUE of FALSE */
2490 linenr_T lnum; /* current line number */
2491{
2492 int i = 0;
2493 int level;
2494 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002495 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 /* Init to all spaces. */
2498 copy_spaces(p, (size_t)wp->w_p_fdc);
2499
2500 level = win_foldinfo.fi_level;
2501 if (level > 0)
2502 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002503 /* If there is only one column put more info in it. */
2504 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 /* If the column is too narrow, we start at the lowest level that
2507 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002508 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (first_level < 1)
2510 first_level = 1;
2511
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002512 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 if (win_foldinfo.fi_lnum == lnum
2515 && first_level + i >= win_foldinfo.fi_low_level)
2516 p[i] = '-';
2517 else if (first_level == 1)
2518 p[i] = '|';
2519 else if (first_level + i <= 9)
2520 p[i] = '0' + first_level + i;
2521 else
2522 p[i] = '>';
2523 if (first_level + i == level)
2524 break;
2525 }
2526 }
2527 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002528 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529}
2530#endif /* FEAT_FOLDING */
2531
2532/*
2533 * Display line "lnum" of window 'wp' on the screen.
2534 * Start at row "startrow", stop when "endrow" is reached.
2535 * wp->w_virtcol needs to be valid.
2536 *
2537 * Return the number of last row the line occupies.
2538 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002539/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002541win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 win_T *wp;
2543 linenr_T lnum;
2544 int startrow;
2545 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002546 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 int col; /* visual column on screen */
2549 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2550 int c = 0; /* init for GCC */
2551 long vcol = 0; /* virtual column (for tabs) */
2552 long vcol_prev = -1; /* "vcol" of previous character */
2553 char_u *line; /* current line */
2554 char_u *ptr; /* current position in "line" */
2555 int row; /* row in the window, excl w_winrow */
2556 int screen_row; /* row on the screen, incl w_winrow */
2557
2558 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2559 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002560 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 int c_extra = NUL; /* extra chars, all the same */
2562 int extra_attr = 0; /* attributes when n_extra != 0 */
2563 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2564 displaying lcs_eol at end-of-line */
2565 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2566 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2567
2568 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2569 int saved_n_extra = 0;
2570 char_u *saved_p_extra = NULL;
2571 int saved_c_extra = 0;
2572 int saved_char_attr = 0;
2573
2574 int n_attr = 0; /* chars with special attr */
2575 int saved_attr2 = 0; /* char_attr saved for n_attr */
2576 int n_attr3 = 0; /* chars with overruling special attr */
2577 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2578
2579 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2580
2581 int fromcol, tocol; /* start/end of inverting */
2582 int fromcol_prev = -2; /* start of inverting after cursor */
2583 int noinvcur = FALSE; /* don't invert the cursor */
2584#ifdef FEAT_VISUAL
2585 pos_T *top, *bot;
2586#endif
2587 pos_T pos;
2588 long v;
2589
2590 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002591 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2593 in this line */
2594 int attr = 0; /* attributes for area highlighting */
2595 int area_attr = 0; /* attributes desired by highlighting */
2596 int search_attr = 0; /* attributes desired by 'hlsearch' */
2597#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002598 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 int syntax_attr = 0; /* attributes desired by syntax */
2600 int has_syntax = FALSE; /* this buffer has syntax highl. */
2601 int save_did_emsg;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002602#endif
2603#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002604 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002605# define SPWORDLEN 150
2606 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002607 int nextlinecol = 0; /* column where nextline[] starts */
2608 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002609 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002610 int spell_attr = 0; /* attributes desired by spelling */
2611 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002612 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2613 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002614 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002615 static int cap_col = -1; /* column to check for Cap word */
2616 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002617 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#endif
2619 int extra_check; /* has syntax or linebreak */
2620#ifdef FEAT_MBYTE
2621 int multi_attr = 0; /* attributes desired by multibyte */
2622 int mb_l = 1; /* multi-byte byte length */
2623 int mb_c = 0; /* decoded multi-byte character */
2624 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002625 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#endif
2627#ifdef FEAT_DIFF
2628 int filler_lines; /* nr of filler lines to be drawn */
2629 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002630 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 int change_start = MAXCOL; /* first col of changed area */
2632 int change_end = -1; /* last col of changed area */
2633#endif
2634 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2635#ifdef FEAT_LINEBREAK
2636 int need_showbreak = FALSE;
2637#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002638#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2639 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640# define LINE_ATTR
2641 int line_attr = 0; /* atrribute for the whole line */
2642#endif
2643#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002644 matchitem_T *cur; /* points to the match list */
2645 match_T *shl; /* points to search_hl or a match */
2646 int shl_flag; /* flag to indicate whether search_hl
2647 has been processed or not */
2648 int prevcol_hl_flag; /* flag to indicate whether prevcol
2649 equals startcol of search_hl or one
2650 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#endif
2652#ifdef FEAT_ARABIC
2653 int prev_c = 0; /* previous Arabic character */
2654 int prev_c1 = 0; /* first composing char for prev_c */
2655#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002656#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002657 int did_line_attr = 0;
2658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 /* draw_state: items that are drawn in sequence: */
2661#define WL_START 0 /* nothing done yet */
2662#ifdef FEAT_CMDWIN
2663# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2664#else
2665# define WL_CMDLINE WL_START
2666#endif
2667#ifdef FEAT_FOLDING
2668# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2669#else
2670# define WL_FOLD WL_CMDLINE
2671#endif
2672#ifdef FEAT_SIGNS
2673# define WL_SIGN WL_FOLD + 1 /* column for signs */
2674#else
2675# define WL_SIGN WL_FOLD /* column for signs */
2676#endif
2677#define WL_NR WL_SIGN + 1 /* line number */
2678#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2679# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2680#else
2681# define WL_SBR WL_NR
2682#endif
2683#define WL_LINE WL_SBR + 1 /* text in the line */
2684 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002685#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 int feedback_col = 0;
2687 int feedback_old_attr = -1;
2688#endif
2689
2690
2691 if (startrow > endrow) /* past the end already! */
2692 return startrow;
2693
2694 row = startrow;
2695 screen_row = row + W_WINROW(wp);
2696
2697 /*
2698 * To speed up the loop below, set extra_check when there is linebreak,
2699 * trailing white space and/or syntax processing to be done.
2700 */
2701#ifdef FEAT_LINEBREAK
2702 extra_check = wp->w_p_lbr;
2703#else
2704 extra_check = 0;
2705#endif
2706#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002707 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 /* Prepare for syntax highlighting in this line. When there is an
2710 * error, stop syntax highlighting. */
2711 save_did_emsg = did_emsg;
2712 did_emsg = FALSE;
2713 syntax_start(wp, lnum);
2714 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002715 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 else
2717 {
2718 did_emsg = save_did_emsg;
2719 has_syntax = TRUE;
2720 extra_check = TRUE;
2721 }
2722 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002723#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002724
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002725#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002726 if (wp->w_p_spell
2727 && *wp->w_buffer->b_p_spl != NUL
2728 && wp->w_buffer->b_langp.ga_len > 0
2729 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002730 {
2731 /* Prepare for spell checking. */
2732 has_spell = TRUE;
2733 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002734
2735 /* Get the start of the next line, so that words that wrap to the next
2736 * line are found too: "et<line-break>al.".
2737 * Trick: skip a few chars for C/shell/Vim comments */
2738 nextline[SPWORDLEN] = NUL;
2739 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2740 {
2741 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2742 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2743 }
2744
2745 /* When a word wrapped from the previous line the start of the current
2746 * line is valid. */
2747 if (lnum == checked_lnum)
2748 cur_checked_col = checked_col;
2749 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002750
2751 /* When there was a sentence end in the previous line may require a
2752 * word starting with capital in this line. In line 1 always check
2753 * the first word. */
2754 if (lnum != capcol_lnum)
2755 cap_col = -1;
2756 if (lnum == 1)
2757 cap_col = 0;
2758 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760#endif
2761
2762 /*
2763 * handle visual active in this window
2764 */
2765 fromcol = -10;
2766 tocol = MAXCOL;
2767#ifdef FEAT_VISUAL
2768 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2769 {
2770 /* Visual is after curwin->w_cursor */
2771 if (ltoreq(curwin->w_cursor, VIsual))
2772 {
2773 top = &curwin->w_cursor;
2774 bot = &VIsual;
2775 }
2776 else /* Visual is before curwin->w_cursor */
2777 {
2778 top = &VIsual;
2779 bot = &curwin->w_cursor;
2780 }
2781 if (VIsual_mode == Ctrl_V) /* block mode */
2782 {
2783 if (lnum >= top->lnum && lnum <= bot->lnum)
2784 {
2785 fromcol = wp->w_old_cursor_fcol;
2786 tocol = wp->w_old_cursor_lcol;
2787 }
2788 }
2789 else /* non-block mode */
2790 {
2791 if (lnum > top->lnum && lnum <= bot->lnum)
2792 fromcol = 0;
2793 else if (lnum == top->lnum)
2794 {
2795 if (VIsual_mode == 'V') /* linewise */
2796 fromcol = 0;
2797 else
2798 {
2799 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2800 if (gchar_pos(top) == NUL)
2801 tocol = fromcol + 1;
2802 }
2803 }
2804 if (VIsual_mode != 'V' && lnum == bot->lnum)
2805 {
2806 if (*p_sel == 'e' && bot->col == 0
2807#ifdef FEAT_VIRTUALEDIT
2808 && bot->coladd == 0
2809#endif
2810 )
2811 {
2812 fromcol = -10;
2813 tocol = MAXCOL;
2814 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002815 else if (bot->col == MAXCOL)
2816 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 else
2818 {
2819 pos = *bot;
2820 if (*p_sel == 'e')
2821 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2822 else
2823 {
2824 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2825 ++tocol;
2826 }
2827 }
2828 }
2829 }
2830
2831#ifndef MSDOS
2832 /* Check if the character under the cursor should not be inverted */
2833 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2834# ifdef FEAT_GUI
2835 && !gui.in_use
2836# endif
2837 )
2838 noinvcur = TRUE;
2839#endif
2840
2841 /* if inverting in this line set area_highlighting */
2842 if (fromcol >= 0)
2843 {
2844 area_highlighting = TRUE;
2845 attr = hl_attr(HLF_V);
2846#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2847 if (clip_star.available && !clip_star.owned && clip_isautosel())
2848 attr = hl_attr(HLF_VNC);
2849#endif
2850 }
2851 }
2852
2853 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002854 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 */
2856 else
2857#endif /* FEAT_VISUAL */
2858 if (highlight_match
2859 && wp == curwin
2860 && lnum >= curwin->w_cursor.lnum
2861 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2862 {
2863 if (lnum == curwin->w_cursor.lnum)
2864 getvcol(curwin, &(curwin->w_cursor),
2865 (colnr_T *)&fromcol, NULL, NULL);
2866 else
2867 fromcol = 0;
2868 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2869 {
2870 pos.lnum = lnum;
2871 pos.col = search_match_endcol;
2872 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2873 }
2874 else
2875 tocol = MAXCOL;
2876 if (fromcol == tocol) /* do at least one character */
2877 tocol = fromcol + 1; /* happens when past end of line */
2878 area_highlighting = TRUE;
2879 attr = hl_attr(HLF_I);
2880 }
2881
2882#ifdef FEAT_DIFF
2883 filler_lines = diff_check(wp, lnum);
2884 if (filler_lines < 0)
2885 {
2886 if (filler_lines == -1)
2887 {
2888 if (diff_find_change(wp, lnum, &change_start, &change_end))
2889 diff_hlf = HLF_ADD; /* added line */
2890 else if (change_start == 0)
2891 diff_hlf = HLF_TXD; /* changed text */
2892 else
2893 diff_hlf = HLF_CHD; /* changed line */
2894 }
2895 else
2896 diff_hlf = HLF_ADD; /* added line */
2897 filler_lines = 0;
2898 area_highlighting = TRUE;
2899 }
2900 if (lnum == wp->w_topline)
2901 filler_lines = wp->w_topfill;
2902 filler_todo = filler_lines;
2903#endif
2904
2905#ifdef LINE_ATTR
2906# ifdef FEAT_SIGNS
2907 /* If this line has a sign with line highlighting set line_attr. */
2908 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2909 if (v != 0)
2910 line_attr = sign_get_attr((int)v, TRUE);
2911# endif
2912# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2913 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002914 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 line_attr = hl_attr(HLF_L);
2916# endif
2917 if (line_attr != 0)
2918 area_highlighting = TRUE;
2919#endif
2920
2921 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2922 ptr = line;
2923
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002924#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002925 if (has_spell)
2926 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002927 /* For checking first word with a capital skip white space. */
2928 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002929 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002930
Bram Moolenaar30abd282005-06-22 22:35:10 +00002931 /* To be able to spell-check over line boundaries copy the end of the
2932 * current line into nextline[]. Above the start of the next line was
2933 * copied to nextline[SPWORDLEN]. */
2934 if (nextline[SPWORDLEN] == NUL)
2935 {
2936 /* No next line or it is empty. */
2937 nextlinecol = MAXCOL;
2938 nextline_idx = 0;
2939 }
2940 else
2941 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002942 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002943 if (v < SPWORDLEN)
2944 {
2945 /* Short line, use it completely and append the start of the
2946 * next line. */
2947 nextlinecol = 0;
2948 mch_memmove(nextline, line, (size_t)v);
2949 mch_memmove(nextline + v, nextline + SPWORDLEN,
2950 STRLEN(nextline + SPWORDLEN) + 1);
2951 nextline_idx = v + 1;
2952 }
2953 else
2954 {
2955 /* Long line, use only the last SPWORDLEN bytes. */
2956 nextlinecol = v - SPWORDLEN;
2957 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2958 nextline_idx = SPWORDLEN + 1;
2959 }
2960 }
2961 }
2962#endif
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 /* find start of trailing whitespace */
2965 if (wp->w_p_list && lcs_trail)
2966 {
2967 trailcol = (colnr_T)STRLEN(ptr);
2968 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2969 --trailcol;
2970 trailcol += (colnr_T) (ptr - line);
2971 extra_check = TRUE;
2972 }
2973
2974 /*
2975 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2976 * first character to be displayed.
2977 */
2978 if (wp->w_p_wrap)
2979 v = wp->w_skipcol;
2980 else
2981 v = wp->w_leftcol;
2982 if (v > 0)
2983 {
2984#ifdef FEAT_MBYTE
2985 char_u *prev_ptr = ptr;
2986#endif
2987 while (vcol < v && *ptr != NUL)
2988 {
2989 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2990 vcol += c;
2991#ifdef FEAT_MBYTE
2992 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002994 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996
2997#ifdef FEAT_VIRTUALEDIT
2998 /* When 'virtualedit' is set the end of the line may be before the
2999 * start of the displayed part. */
3000 if (vcol < v && *ptr == NUL && virtual_active())
3001 vcol = v;
3002#endif
3003
3004 /* Handle a character that's not completely on the screen: Put ptr at
3005 * that character but skip the first few screen characters. */
3006 if (vcol > v)
3007 {
3008 vcol -= c;
3009#ifdef FEAT_MBYTE
3010 ptr = prev_ptr;
3011#else
3012 --ptr;
3013#endif
3014 n_skip = v - vcol;
3015 }
3016
3017 /*
3018 * Adjust for when the inverted text is before the screen,
3019 * and when the start of the inverted text is before the screen.
3020 */
3021 if (tocol <= vcol)
3022 fromcol = 0;
3023 else if (fromcol >= 0 && fromcol < vcol)
3024 fromcol = vcol;
3025
3026#ifdef FEAT_LINEBREAK
3027 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3028 if (wp->w_p_wrap)
3029 need_showbreak = TRUE;
3030#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003031#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003032 /* When spell checking a word we need to figure out the start of the
3033 * word and if it's badly spelled or not. */
3034 if (has_spell)
3035 {
3036 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003037 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003038
3039 pos = wp->w_cursor;
3040 wp->w_cursor.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003041 wp->w_cursor.col = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003042 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003043 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003044 {
3045 /* no bad word found at line start, don't check until end of a
3046 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003047 spell_hlf = HLF_COUNT;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003048 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003049 }
3050 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003051 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003052 /* bad word found, use attributes until end of word */
3053 word_end = wp->w_cursor.col + len + 1;
3054
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003055 /* Turn index into actual attributes. */
3056 if (spell_hlf != HLF_COUNT)
3057 spell_attr = highlight_attr[spell_hlf];
3058 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003059 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003060
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003061# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003062 /* Need to restart syntax highlighting for this line. */
3063 if (has_syntax)
3064 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003065# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003066 }
3067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 }
3069
3070 /*
3071 * Correct highlighting for cursor that can't be disabled.
3072 * Avoids having to check this for each character.
3073 */
3074 if (fromcol >= 0)
3075 {
3076 if (noinvcur)
3077 {
3078 if ((colnr_T)fromcol == wp->w_virtcol)
3079 {
3080 /* highlighting starts at cursor, let it start just after the
3081 * cursor */
3082 fromcol_prev = fromcol;
3083 fromcol = -1;
3084 }
3085 else if ((colnr_T)fromcol < wp->w_virtcol)
3086 /* restart highlighting after the cursor */
3087 fromcol_prev = wp->w_virtcol;
3088 }
3089 if (fromcol >= tocol)
3090 fromcol = -1;
3091 }
3092
3093#ifdef FEAT_SEARCH_EXTRA
3094 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003095 * Handle highlighting the last used search pattern and matches.
3096 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003098 cur = wp->w_match_head;
3099 shl_flag = FALSE;
3100 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003102 if (shl_flag == FALSE)
3103 {
3104 shl = &search_hl;
3105 shl_flag = TRUE;
3106 }
3107 else
3108 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003109 shl->startcol = MAXCOL;
3110 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 shl->attr_cur = 0;
3112 if (shl->rm.regprog != NULL)
3113 {
3114 v = (long)(ptr - line);
3115 next_search_hl(wp, shl, lnum, (colnr_T)v);
3116
3117 /* Need to get the line again, a multi-line regexp may have made it
3118 * invalid. */
3119 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3120 ptr = line + v;
3121
3122 if (shl->lnum != 0 && shl->lnum <= lnum)
3123 {
3124 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003125 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003127 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3129 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003130 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003132 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003134 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003137 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003138 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 else
3140#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003141 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003143 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 shl->attr_cur = shl->attr;
3146 search_attr = shl->attr;
3147 }
3148 area_highlighting = TRUE;
3149 }
3150 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003151 if (shl != &search_hl && cur != NULL)
3152 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
3154#endif
3155
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003156#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003157 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3158 * active, because it's not clear what is selected then. */
3159 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003160 {
3161 line_attr = hl_attr(HLF_CUL);
3162 area_highlighting = TRUE;
3163 }
3164#endif
3165
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003166 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 col = 0;
3168#ifdef FEAT_RIGHTLEFT
3169 if (wp->w_p_rl)
3170 {
3171 /* Rightleft window: process the text in the normal direction, but put
3172 * it in current_ScreenLine[] from right to left. Start at the
3173 * rightmost column of the window. */
3174 col = W_WIDTH(wp) - 1;
3175 off += col;
3176 }
3177#endif
3178
3179 /*
3180 * Repeat for the whole displayed line.
3181 */
3182 for (;;)
3183 {
3184 /* Skip this quickly when working on the text. */
3185 if (draw_state != WL_LINE)
3186 {
3187#ifdef FEAT_CMDWIN
3188 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3189 {
3190 draw_state = WL_CMDLINE;
3191 if (cmdwin_type != 0 && wp == curwin)
3192 {
3193 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003195 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 char_attr = hl_attr(HLF_AT);
3197 }
3198 }
3199#endif
3200
3201#ifdef FEAT_FOLDING
3202 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3203 {
3204 draw_state = WL_FOLD;
3205 if (wp->w_p_fdc > 0)
3206 {
3207 /* Draw the 'foldcolumn'. */
3208 fill_foldcolumn(extra, wp, FALSE, lnum);
3209 n_extra = wp->w_p_fdc;
3210 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003211 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 c_extra = NUL;
3213 char_attr = hl_attr(HLF_FC);
3214 }
3215 }
3216#endif
3217
3218#ifdef FEAT_SIGNS
3219 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3220 {
3221 draw_state = WL_SIGN;
3222 /* Show the sign column when there are any signs in this
3223 * buffer or when using Netbeans. */
3224 if (draw_signcolumn(wp)
3225# ifdef FEAT_DIFF
3226 && filler_todo <= 0
3227# endif
3228 )
3229 {
3230 int_u text_sign;
3231# ifdef FEAT_SIGN_ICONS
3232 int_u icon_sign;
3233# endif
3234
3235 /* Draw two cells with the sign value or blank. */
3236 c_extra = ' ';
3237 char_attr = hl_attr(HLF_SC);
3238 n_extra = 2;
3239
3240 if (row == startrow)
3241 {
3242 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3243 SIGN_TEXT);
3244# ifdef FEAT_SIGN_ICONS
3245 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3246 SIGN_ICON);
3247 if (gui.in_use && icon_sign != 0)
3248 {
3249 /* Use the image in this position. */
3250 c_extra = SIGN_BYTE;
3251# ifdef FEAT_NETBEANS_INTG
3252 if (buf_signcount(wp->w_buffer, lnum) > 1)
3253 c_extra = MULTISIGN_BYTE;
3254# endif
3255 char_attr = icon_sign;
3256 }
3257 else
3258# endif
3259 if (text_sign != 0)
3260 {
3261 p_extra = sign_get_text(text_sign);
3262 if (p_extra != NULL)
3263 {
3264 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003265 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267 char_attr = sign_get_attr(text_sign, FALSE);
3268 }
3269 }
3270 }
3271 }
3272#endif
3273
3274 if (draw_state == WL_NR - 1 && n_extra == 0)
3275 {
3276 draw_state = WL_NR;
3277 /* Display the line number. After the first fill with blanks
3278 * when the 'n' flag isn't in 'cpo' */
3279 if (wp->w_p_nu
3280 && (row == startrow
3281#ifdef FEAT_DIFF
3282 + filler_lines
3283#endif
3284 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3285 {
3286 /* Draw the line number (empty space after wrapping). */
3287 if (row == startrow
3288#ifdef FEAT_DIFF
3289 + filler_lines
3290#endif
3291 )
3292 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003293 sprintf((char *)extra, "%*ld ",
3294 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (wp->w_skipcol > 0)
3296 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3297 *p_extra = '-';
3298#ifdef FEAT_RIGHTLEFT
3299 if (wp->w_p_rl) /* reverse line numbers */
3300 rl_mirror(extra);
3301#endif
3302 p_extra = extra;
3303 c_extra = NUL;
3304 }
3305 else
3306 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003307 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003309#ifdef FEAT_SYN_HL
3310 /* When 'cursorline' is set highlight the line number of
3311 * the current line differently. */
3312 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3313 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316 }
3317
3318#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3319 if (draw_state == WL_SBR - 1 && n_extra == 0)
3320 {
3321 draw_state = WL_SBR;
3322# ifdef FEAT_DIFF
3323 if (filler_todo > 0)
3324 {
3325 /* Draw "deleted" diff line(s). */
3326 if (char2cells(fill_diff) > 1)
3327 c_extra = '-';
3328 else
3329 c_extra = fill_diff;
3330# ifdef FEAT_RIGHTLEFT
3331 if (wp->w_p_rl)
3332 n_extra = col + 1;
3333 else
3334# endif
3335 n_extra = W_WIDTH(wp) - col;
3336 char_attr = hl_attr(HLF_DED);
3337 }
3338# endif
3339# ifdef FEAT_LINEBREAK
3340 if (*p_sbr != NUL && need_showbreak)
3341 {
3342 /* Draw 'showbreak' at the start of each broken line. */
3343 p_extra = p_sbr;
3344 c_extra = NUL;
3345 n_extra = (int)STRLEN(p_sbr);
3346 char_attr = hl_attr(HLF_AT);
3347 need_showbreak = FALSE;
3348 /* Correct end of highlighted area for 'showbreak',
3349 * required when 'linebreak' is also set. */
3350 if (tocol == vcol)
3351 tocol += n_extra;
3352 }
3353# endif
3354 }
3355#endif
3356
3357 if (draw_state == WL_LINE - 1 && n_extra == 0)
3358 {
3359 draw_state = WL_LINE;
3360 if (saved_n_extra)
3361 {
3362 /* Continue item from end of wrapped line. */
3363 n_extra = saved_n_extra;
3364 c_extra = saved_c_extra;
3365 p_extra = saved_p_extra;
3366 char_attr = saved_char_attr;
3367 }
3368 else
3369 char_attr = 0;
3370 }
3371 }
3372
3373 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003374 if (dollar_vcol != 0 && wp == curwin
3375 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#ifdef FEAT_DIFF
3377 && filler_todo <= 0
3378#endif
3379 )
3380 {
3381 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3382 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003383 /* Pretend we have finished updating the window. Except when
3384 * 'cursorcolumn' is set. */
3385#ifdef FEAT_SYN_HL
3386 if (wp->w_p_cuc)
3387 row = wp->w_cline_row + wp->w_cline_height;
3388 else
3389#endif
3390 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 break;
3392 }
3393
3394 if (draw_state == WL_LINE && area_highlighting)
3395 {
3396 /* handle Visual or match highlighting in this line */
3397 if (vcol == fromcol
3398#ifdef FEAT_MBYTE
3399 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3400 && (*mb_ptr2cells)(ptr) > 1)
3401#endif
3402 || ((int)vcol_prev == fromcol_prev
3403 && vcol < tocol))
3404 area_attr = attr; /* start highlighting */
3405 else if (area_attr != 0
3406 && (vcol == tocol
3407 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410#ifdef FEAT_SEARCH_EXTRA
3411 if (!n_extra)
3412 {
3413 /*
3414 * Check for start/end of search pattern match.
3415 * After end, check for start/end of next match.
3416 * When another match, have to check for start again.
3417 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003418 * Do this for 'search_hl' and the match list (ordered by
3419 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003421 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003422 cur = wp->w_match_head;
3423 shl_flag = FALSE;
3424 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003426 if (shl_flag == FALSE
3427 && ((cur != NULL
3428 && cur->priority > SEARCH_HL_PRIORITY)
3429 || cur == NULL))
3430 {
3431 shl = &search_hl;
3432 shl_flag = TRUE;
3433 }
3434 else
3435 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 while (shl->rm.regprog != NULL)
3437 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003438 if (shl->startcol != MAXCOL
3439 && v >= (long)shl->startcol
3440 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 {
3442 shl->attr_cur = shl->attr;
3443 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003444 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 shl->attr_cur = 0;
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 next_search_hl(wp, shl, lnum, (colnr_T)v);
3449
3450 /* Need to get the line again, a multi-line regexp
3451 * may have made it invalid. */
3452 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3453 ptr = line + v;
3454
3455 if (shl->lnum == lnum)
3456 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003457 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003459 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003461 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003463 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 /* highlight empty match, try again after
3466 * it */
3467#ifdef FEAT_MBYTE
3468 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003469 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003470 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else
3472#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003473 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475
3476 /* Loop to check if the match starts at the
3477 * current position */
3478 continue;
3479 }
3480 }
3481 break;
3482 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003483 if (shl != &search_hl && cur != NULL)
3484 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003486
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003487 /* Use attributes from match with highest priority among
3488 * 'search_hl' and the match list. */
3489 search_attr = search_hl.attr_cur;
3490 cur = wp->w_match_head;
3491 shl_flag = FALSE;
3492 while (cur != NULL || shl_flag == FALSE)
3493 {
3494 if (shl_flag == FALSE
3495 && ((cur != NULL
3496 && cur->priority > SEARCH_HL_PRIORITY)
3497 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003498 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003499 shl = &search_hl;
3500 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003501 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003502 else
3503 shl = &cur->hl;
3504 if (shl->attr_cur != 0)
3505 search_attr = shl->attr_cur;
3506 if (shl != &search_hl && cur != NULL)
3507 cur = cur->next;
3508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510#endif
3511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003513 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003515 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3516 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003518 if (diff_hlf == HLF_TXD && ptr - line > change_end
3519 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003521 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003524
3525 /* Decide which of the highlight attributes to use. */
3526 attr_pri = TRUE;
3527 if (area_attr != 0)
3528 char_attr = area_attr;
3529 else if (search_attr != 0)
3530 char_attr = search_attr;
3531#ifdef LINE_ATTR
3532 /* Use line_attr when not in the Visual or 'incsearch' area
3533 * (area_attr may be 0 when "noinvcur" is set). */
3534 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3535 || (vcol < fromcol || vcol >= tocol)))
3536 char_attr = line_attr;
3537#endif
3538 else
3539 {
3540 attr_pri = FALSE;
3541#ifdef FEAT_SYN_HL
3542 if (has_syntax)
3543 char_attr = syntax_attr;
3544 else
3545#endif
3546 char_attr = 0;
3547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549
3550 /*
3551 * Get the next character to put on the screen.
3552 */
3553 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003554 * The "p_extra" points to the extra stuff that is inserted to
3555 * represent special characters (non-printable stuff) and other
3556 * things. When all characters are the same, c_extra is used.
3557 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3558 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3560 */
3561 if (n_extra > 0)
3562 {
3563 if (c_extra != NUL)
3564 {
3565 c = c_extra;
3566#ifdef FEAT_MBYTE
3567 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3568 if (enc_utf8 && (*mb_char2len)(c) > 1)
3569 {
3570 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003571 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003572 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574 else
3575 mb_utf8 = FALSE;
3576#endif
3577 }
3578 else
3579 {
3580 c = *p_extra;
3581#ifdef FEAT_MBYTE
3582 if (has_mbyte)
3583 {
3584 mb_c = c;
3585 if (enc_utf8)
3586 {
3587 /* If the UTF-8 character is more than one byte:
3588 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003589 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 mb_utf8 = FALSE;
3591 if (mb_l > n_extra)
3592 mb_l = 1;
3593 else if (mb_l > 1)
3594 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003595 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003597 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599 }
3600 else
3601 {
3602 /* if this is a DBCS character, put it in "mb_c" */
3603 mb_l = MB_BYTE2LEN(c);
3604 if (mb_l >= n_extra)
3605 mb_l = 1;
3606 else if (mb_l > 1)
3607 mb_c = (c << 8) + p_extra[1];
3608 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003609 if (mb_l == 0) /* at the NUL at end-of-line */
3610 mb_l = 1;
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 /* If a double-width char doesn't fit display a '>' in the
3613 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003614 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615# ifdef FEAT_RIGHTLEFT
3616 wp->w_p_rl ? (col <= 0) :
3617# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003618 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 && (*mb_char2cells)(mb_c) == 2)
3620 {
3621 c = '>';
3622 mb_c = c;
3623 mb_l = 1;
3624 mb_utf8 = FALSE;
3625 multi_attr = hl_attr(HLF_AT);
3626 /* put the pointer back to output the double-width
3627 * character at the start of the next line. */
3628 ++n_extra;
3629 --p_extra;
3630 }
3631 else
3632 {
3633 n_extra -= mb_l - 1;
3634 p_extra += mb_l - 1;
3635 }
3636 }
3637#endif
3638 ++p_extra;
3639 }
3640 --n_extra;
3641 }
3642 else
3643 {
3644 /*
3645 * Get a character from the line itself.
3646 */
3647 c = *ptr;
3648#ifdef FEAT_MBYTE
3649 if (has_mbyte)
3650 {
3651 mb_c = c;
3652 if (enc_utf8)
3653 {
3654 /* If the UTF-8 character is more than one byte: Decode it
3655 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003656 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 mb_utf8 = FALSE;
3658 if (mb_l > 1)
3659 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003660 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 /* Overlong encoded ASCII or ASCII with composing char
3662 * is displayed normally, except a NUL. */
3663 if (mb_c < 0x80)
3664 c = mb_c;
3665 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003666
3667 /* At start of the line we can have a composing char.
3668 * Draw it as a space with a composing char. */
3669 if (utf_iscomposing(mb_c))
3670 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003671 int i;
3672
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003673 for (i = Screen_mco - 1; i > 0; --i)
3674 u8cc[i] = u8cc[i - 1];
3675 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003676 mb_c = ' ';
3677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679
3680 if ((mb_l == 1 && c >= 0x80)
3681 || (mb_l >= 1 && mb_c == 0)
3682 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003683# ifdef UNICODE16
3684 || mb_c >= 0x10000
3685# endif
3686 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
3688 /*
3689 * Illegal UTF-8 byte: display as <xx>.
3690 * Non-BMP character : display as ? or fullwidth ?.
3691 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003692# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
3696 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003697# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 if (wp->w_p_rl) /* reverse */
3699 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003702# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 else if (utf_char2cells(mb_c) != 2)
3704 STRCPY(extra, "?");
3705 else
3706 /* 0xff1f in UTF-8: full-width '?' */
3707 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 p_extra = extra;
3711 c = *p_extra;
3712 mb_c = mb_ptr2char_adv(&p_extra);
3713 mb_utf8 = (c >= 0x80);
3714 n_extra = (int)STRLEN(p_extra);
3715 c_extra = NUL;
3716 if (area_attr == 0 && search_attr == 0)
3717 {
3718 n_attr = n_extra + 1;
3719 extra_attr = hl_attr(HLF_8);
3720 saved_attr2 = char_attr; /* save current attr */
3721 }
3722 }
3723 else if (mb_l == 0) /* at the NUL at end-of-line */
3724 mb_l = 1;
3725#ifdef FEAT_ARABIC
3726 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3727 {
3728 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003729 int pc, pc1, nc;
3730 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 /* The idea of what is the previous and next
3733 * character depends on 'rightleft'. */
3734 if (wp->w_p_rl)
3735 {
3736 pc = prev_c;
3737 pc1 = prev_c1;
3738 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003739 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741 else
3742 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003743 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003745 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747 prev_c = mb_c;
3748
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003749 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751 else
3752 prev_c = mb_c;
3753#endif
3754 }
3755 else /* enc_dbcs */
3756 {
3757 mb_l = MB_BYTE2LEN(c);
3758 if (mb_l == 0) /* at the NUL at end-of-line */
3759 mb_l = 1;
3760 else if (mb_l > 1)
3761 {
3762 /* We assume a second byte below 32 is illegal.
3763 * Hopefully this is OK for all double-byte encodings!
3764 */
3765 if (ptr[1] >= 32)
3766 mb_c = (c << 8) + ptr[1];
3767 else
3768 {
3769 if (ptr[1] == NUL)
3770 {
3771 /* head byte at end of line */
3772 mb_l = 1;
3773 transchar_nonprint(extra, c);
3774 }
3775 else
3776 {
3777 /* illegal tail byte */
3778 mb_l = 2;
3779 STRCPY(extra, "XX");
3780 }
3781 p_extra = extra;
3782 n_extra = (int)STRLEN(extra) - 1;
3783 c_extra = NUL;
3784 c = *p_extra++;
3785 if (area_attr == 0 && search_attr == 0)
3786 {
3787 n_attr = n_extra + 1;
3788 extra_attr = hl_attr(HLF_8);
3789 saved_attr2 = char_attr; /* save current attr */
3790 }
3791 mb_c = c;
3792 }
3793 }
3794 }
3795 /* If a double-width char doesn't fit display a '>' in the
3796 * last column; the character is displayed at the start of the
3797 * next line. */
3798 if ((
3799# ifdef FEAT_RIGHTLEFT
3800 wp->w_p_rl ? (col <= 0) :
3801# endif
3802 (col >= W_WIDTH(wp) - 1))
3803 && (*mb_char2cells)(mb_c) == 2)
3804 {
3805 c = '>';
3806 mb_c = c;
3807 mb_utf8 = FALSE;
3808 mb_l = 1;
3809 multi_attr = hl_attr(HLF_AT);
3810 /* Put pointer back so that the character will be
3811 * displayed at the start of the next line. */
3812 --ptr;
3813 }
3814 else if (*ptr != NUL)
3815 ptr += mb_l - 1;
3816
3817 /* If a double-width char doesn't fit at the left side display
3818 * a '<' in the first column. */
3819 if (n_skip > 0 && mb_l > 1)
3820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003822 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 c = ' ';
3824 if (area_attr == 0 && search_attr == 0)
3825 {
3826 n_attr = n_extra + 1;
3827 extra_attr = hl_attr(HLF_AT);
3828 saved_attr2 = char_attr; /* save current attr */
3829 }
3830 mb_c = c;
3831 mb_utf8 = FALSE;
3832 mb_l = 1;
3833 }
3834
3835 }
3836#endif
3837 ++ptr;
3838
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003839 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003840 if (wp->w_p_list && (c == 160
3841#ifdef FEAT_MBYTE
3842 || (mb_utf8 && mb_c == 160)
3843#endif
3844 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003845 {
3846 c = lcs_nbsp;
3847 if (area_attr == 0 && search_attr == 0)
3848 {
3849 n_attr = 1;
3850 extra_attr = hl_attr(HLF_8);
3851 saved_attr2 = char_attr; /* save current attr */
3852 }
3853#ifdef FEAT_MBYTE
3854 mb_c = c;
3855 if (enc_utf8 && (*mb_char2len)(c) > 1)
3856 {
3857 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003858 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003859 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003860 }
3861 else
3862 mb_utf8 = FALSE;
3863#endif
3864 }
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (extra_check)
3867 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003868#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003869 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003870#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003871
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003872#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 /* Get syntax attribute, unless still at the start of the line
3874 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003875 v = (long)(ptr - line);
3876 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
3878 /* Get the syntax attribute for the character. If there
3879 * is an error, disable syntax highlighting. */
3880 save_did_emsg = did_emsg;
3881 did_emsg = FALSE;
3882
Bram Moolenaar217ad922005-03-20 22:37:15 +00003883 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003884# ifdef FEAT_SPELL
3885 has_spell ? &can_spell :
3886# endif
3887 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
3889 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003890 {
3891 wp->w_buffer->b_syn_error = TRUE;
3892 has_syntax = FALSE;
3893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 else
3895 did_emsg = save_did_emsg;
3896
3897 /* Need to get the line again, a multi-line regexp may
3898 * have made it invalid. */
3899 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3900 ptr = line + v;
3901
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003902 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003904 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003905 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003907#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003908
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003909#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003910 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003911 * Only do this when there is no syntax highlighting, the
3912 * @Spell cluster is not used or the current syntax item
3913 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003914 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003915 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003916 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003917# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003918 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003919 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003920# endif
3921 if (c != 0 && (
3922# ifdef FEAT_SYN_HL
3923 !has_syntax ||
3924# endif
3925 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003926 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003927 char_u *prev_ptr, *p;
3928 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003929 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003930# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003931 if (has_mbyte)
3932 {
3933 prev_ptr = ptr - mb_l;
3934 v -= mb_l - 1;
3935 }
3936 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003937# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003938 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003939
3940 /* Use nextline[] if possible, it has the start of the
3941 * next line concatenated. */
3942 if ((prev_ptr - line) - nextlinecol >= 0)
3943 p = nextline + (prev_ptr - line) - nextlinecol;
3944 else
3945 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003946 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003947 len = spell_check(wp, p, &spell_hlf, &cap_col,
3948 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003949 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003950
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003951 /* In Insert mode only highlight a word that
3952 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003953 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003954 && (State & INSERT) != 0
3955 && wp->w_cursor.lnum == lnum
3956 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003957 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003958 && wp->w_cursor.col < (colnr_T)word_end)
3959 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003960 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003961 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003962 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003963
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003964 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003965 && (p - nextline) + len > nextline_idx)
3966 {
3967 /* Remember that the good word continues at the
3968 * start of the next line. */
3969 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003970 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003971 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003972
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003973 /* Turn index into actual attributes. */
3974 if (spell_hlf != HLF_COUNT)
3975 spell_attr = highlight_attr[spell_hlf];
3976
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003977 if (cap_col > 0)
3978 {
3979 if (p != prev_ptr
3980 && (p - nextline) + cap_col >= nextline_idx)
3981 {
3982 /* Remember that the word in the next line
3983 * must start with a capital. */
3984 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003985 cap_col = (int)((p - nextline) + cap_col
3986 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003987 }
3988 else
3989 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003990 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003991 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003992 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003993 }
3994 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003995 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003996 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003997 char_attr = hl_combine_attr(char_attr, spell_attr);
3998 else
3999 char_attr = hl_combine_attr(spell_attr, char_attr);
4000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001#endif
4002#ifdef FEAT_LINEBREAK
4003 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004004 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 */
4006 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
4009 n_extra = win_lbr_chartabsize(wp, ptr - (
4010# ifdef FEAT_MBYTE
4011 has_mbyte ? mb_l :
4012# endif
4013 1), (colnr_T)vcol, NULL) - 1;
4014 c_extra = ' ';
4015 if (vim_iswhite(c))
4016 c = ' ';
4017 }
4018#endif
4019
4020 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4021 {
4022 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004023 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
4025 n_attr = 1;
4026 extra_attr = hl_attr(HLF_8);
4027 saved_attr2 = char_attr; /* save current attr */
4028 }
4029#ifdef FEAT_MBYTE
4030 mb_c = c;
4031 if (enc_utf8 && (*mb_char2len)(c) > 1)
4032 {
4033 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004034 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004035 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
4037 else
4038 mb_utf8 = FALSE;
4039#endif
4040 }
4041 }
4042
4043 /*
4044 * Handling of non-printable characters.
4045 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004046 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
4048 /*
4049 * when getting a character from the file, we may have to
4050 * turn it into something else on the way to putting it
4051 * into "ScreenLines".
4052 */
4053 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4054 {
4055 /* tab amount depends on current column */
4056 n_extra = (int)wp->w_buffer->b_p_ts
4057 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4058#ifdef FEAT_MBYTE
4059 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4060#endif
4061 if (wp->w_p_list)
4062 {
4063 c = lcs_tab1;
4064 c_extra = lcs_tab2;
4065 n_attr = n_extra + 1;
4066 extra_attr = hl_attr(HLF_8);
4067 saved_attr2 = char_attr; /* save current attr */
4068#ifdef FEAT_MBYTE
4069 mb_c = c;
4070 if (enc_utf8 && (*mb_char2len)(c) > 1)
4071 {
4072 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004073 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076#endif
4077 }
4078 else
4079 {
4080 c_extra = ' ';
4081 c = ' ';
4082 }
4083 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004084 else if (c == NUL
4085 && ((wp->w_p_list && lcs_eol > 0)
4086 || ((fromcol >= 0 || fromcol_prev >= 0)
4087 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004088#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004089 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004090#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004091 && (
4092# ifdef FEAT_RIGHTLEFT
4093 wp->w_p_rl ? (col >= 0) :
4094# endif
4095 (col < W_WIDTH(wp)))
4096 && !(noinvcur
4097 && (colnr_T)vcol == wp->w_virtcol)))
4098 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004100 /* Display a '$' after the line or highlight an extra
4101 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4103 /* For a diff line the highlighting continues after the
4104 * "$". */
4105 if (
4106# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004107 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108# ifdef LINE_ATTR
4109 &&
4110# endif
4111# endif
4112# ifdef LINE_ATTR
4113 line_attr == 0
4114# endif
4115 )
4116#endif
4117 {
4118#ifdef FEAT_VIRTUALEDIT
4119 /* In virtualedit, visual selections may extend
4120 * beyond end of line. */
4121 if (area_highlighting && virtual_active()
4122 && tocol != MAXCOL && vcol < tocol)
4123 n_extra = 0;
4124 else
4125#endif
4126 {
4127 p_extra = at_end_str;
4128 n_extra = 1;
4129 c_extra = NUL;
4130 }
4131 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004132 if (wp->w_p_list)
4133 c = lcs_eol;
4134 else
4135 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 lcs_eol_one = -1;
4137 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004138 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
4140 extra_attr = hl_attr(HLF_AT);
4141 n_attr = 1;
4142 }
4143#ifdef FEAT_MBYTE
4144 mb_c = c;
4145 if (enc_utf8 && (*mb_char2len)(c) > 1)
4146 {
4147 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004148 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004149 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
4151 else
4152 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4153#endif
4154 }
4155 else if (c != NUL)
4156 {
4157 p_extra = transchar(c);
4158#ifdef FEAT_RIGHTLEFT
4159 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4160 rl_mirror(p_extra); /* reverse "<12>" */
4161#endif
4162 n_extra = byte2cells(c) - 1;
4163 c_extra = NUL;
4164 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004165 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
4167 n_attr = n_extra + 1;
4168 extra_attr = hl_attr(HLF_8);
4169 saved_attr2 = char_attr; /* save current attr */
4170 }
4171#ifdef FEAT_MBYTE
4172 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4173#endif
4174 }
4175#ifdef FEAT_VIRTUALEDIT
4176 else if (VIsual_active
4177 && (VIsual_mode == Ctrl_V
4178 || VIsual_mode == 'v')
4179 && virtual_active()
4180 && tocol != MAXCOL
4181 && vcol < tocol
4182 && (
4183# ifdef FEAT_RIGHTLEFT
4184 wp->w_p_rl ? (col >= 0) :
4185# endif
4186 (col < W_WIDTH(wp))))
4187 {
4188 c = ' ';
4189 --ptr; /* put it back at the NUL */
4190 }
4191#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004192#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else if ((
4194# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004195 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 ) && (
4199# ifdef FEAT_RIGHTLEFT
4200 wp->w_p_rl ? (col >= 0) :
4201# endif
4202 (col < W_WIDTH(wp))))
4203 {
4204 /* Highlight until the right side of the window */
4205 c = ' ';
4206 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004207
4208 /* Remember we do the char for line highlighting. */
4209 ++did_line_attr;
4210
4211 /* don't do search HL for the rest of the line */
4212 if (line_attr != 0 && char_attr == search_attr && col > 0)
4213 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214# ifdef FEAT_DIFF
4215 if (diff_hlf == HLF_TXD)
4216 {
4217 diff_hlf = HLF_CHD;
4218 if (attr == 0 || char_attr != attr)
4219 char_attr = hl_attr(diff_hlf);
4220 }
4221# endif
4222 }
4223#endif
4224 }
4225 }
4226
4227 /* Don't override visual selection highlighting. */
4228 if (n_attr > 0
4229 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004230 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 char_attr = extra_attr;
4232
Bram Moolenaar81695252004-12-29 20:58:21 +00004233#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 /* XIM don't send preedit_start and preedit_end, but they send
4235 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4236 * im_is_preediting() here. */
4237 if (xic != NULL
4238 && lnum == curwin->w_cursor.lnum
4239 && (State & INSERT)
4240 && !p_imdisable
4241 && im_is_preediting()
4242 && draw_state == WL_LINE)
4243 {
4244 colnr_T tcol;
4245
4246 if (preedit_end_col == MAXCOL)
4247 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4248 else
4249 tcol = preedit_end_col;
4250 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4251 {
4252 if (feedback_old_attr < 0)
4253 {
4254 feedback_col = 0;
4255 feedback_old_attr = char_attr;
4256 }
4257 char_attr = im_get_feedback_attr(feedback_col);
4258 if (char_attr < 0)
4259 char_attr = feedback_old_attr;
4260 feedback_col++;
4261 }
4262 else if (feedback_old_attr >= 0)
4263 {
4264 char_attr = feedback_old_attr;
4265 feedback_old_attr = -1;
4266 feedback_col = 0;
4267 }
4268 }
4269#endif
4270 /*
4271 * Handle the case where we are in column 0 but not on the first
4272 * character of the line and the user wants us to show us a
4273 * special character (via 'listchars' option "precedes:<char>".
4274 */
4275 if (lcs_prec_todo != NUL
4276 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4277#ifdef FEAT_DIFF
4278 && filler_todo <= 0
4279#endif
4280 && draw_state > WL_NR
4281 && c != NUL)
4282 {
4283 c = lcs_prec;
4284 lcs_prec_todo = NUL;
4285#ifdef FEAT_MBYTE
4286 mb_c = c;
4287 if (enc_utf8 && (*mb_char2len)(c) > 1)
4288 {
4289 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004290 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004291 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 else
4294 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4295#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004296 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
4298 saved_attr3 = char_attr; /* save current attr */
4299 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4300 n_attr3 = 1;
4301 }
4302 }
4303
4304 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004305 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004307 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004308#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004309 || did_line_attr == 1
4310#endif
4311 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004313#ifdef FEAT_SEARCH_EXTRA
4314 long prevcol = (long)(ptr - line) - (c == NUL);
4315#endif
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 /* invert at least one char, used for Visual and empty line or
4318 * highlight match at end of line. If it's beyond the last
4319 * char on the screen, just overwrite that one (tricky!) Not
4320 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004321#ifdef FEAT_SEARCH_EXTRA
4322 prevcol_hl_flag = FALSE;
4323 if (prevcol == (long)search_hl.startcol)
4324 prevcol_hl_flag = TRUE;
4325 else
4326 {
4327 cur = wp->w_match_head;
4328 while (cur != NULL)
4329 {
4330 if (prevcol == (long)cur->hl.startcol)
4331 {
4332 prevcol_hl_flag = TRUE;
4333 break;
4334 }
4335 cur = cur->next;
4336 }
4337 }
4338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004340 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#ifdef FEAT_SEARCH_EXTRA
4342 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004343 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004344# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004345 && did_line_attr <= 1
4346# endif
4347 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348#endif
4349 ))
4350 {
4351 int n = 0;
4352
4353#ifdef FEAT_RIGHTLEFT
4354 if (wp->w_p_rl)
4355 {
4356 if (col < 0)
4357 n = 1;
4358 }
4359 else
4360#endif
4361 {
4362 if (col >= W_WIDTH(wp))
4363 n = -1;
4364 }
4365 if (n != 0)
4366 {
4367 /* At the window boundary, highlight the last character
4368 * instead (better than nothing). */
4369 off += n;
4370 col += n;
4371 }
4372 else
4373 {
4374 /* Add a blank character to highlight. */
4375 ScreenLines[off] = ' ';
4376#ifdef FEAT_MBYTE
4377 if (enc_utf8)
4378 ScreenLinesUC[off] = 0;
4379#endif
4380 }
4381#ifdef FEAT_SEARCH_EXTRA
4382 if (area_attr == 0)
4383 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004384 /* Use attributes from match with highest priority among
4385 * 'search_hl' and the match list. */
4386 char_attr = search_hl.attr;
4387 cur = wp->w_match_head;
4388 shl_flag = FALSE;
4389 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004390 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004391 if (shl_flag == FALSE
4392 && ((cur != NULL
4393 && cur->priority > SEARCH_HL_PRIORITY)
4394 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004395 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004396 shl = &search_hl;
4397 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004398 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004399 else
4400 shl = &cur->hl;
4401 if ((ptr - line) - 1 == (long)shl->startcol)
4402 char_attr = shl->attr;
4403 if (shl != &search_hl && cur != NULL)
4404 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407#endif
4408 ScreenAttrs[off] = char_attr;
4409#ifdef FEAT_RIGHTLEFT
4410 if (wp->w_p_rl)
4411 --col;
4412 else
4413#endif
4414 ++col;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004415 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar91170f82006-05-05 21:15:17 +00004419 /*
4420 * At end of the text line.
4421 */
4422 if (c == NUL)
4423 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004424#ifdef FEAT_SYN_HL
4425 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004426 if (wp->w_p_wrap)
4427 v = wp->w_skipcol;
4428 else
4429 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004430 /* check if line ends before left margin */
4431 if (vcol < v + col - win_col_off(wp))
4432
4433 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004434 if (wp->w_p_cuc
4435 && (int)wp->w_virtcol >= vcol
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004436 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4437 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004438 && lnum != wp->w_cursor.lnum
4439# ifdef FEAT_RIGHTLEFT
4440 && !wp->w_p_rl
4441# endif
4442 )
4443 {
4444 while (col < W_WIDTH(wp))
4445 {
4446 ScreenLines[off] = ' ';
4447#ifdef FEAT_MBYTE
4448 if (enc_utf8)
4449 ScreenLinesUC[off] = 0;
4450#endif
4451 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004452 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004453 {
4454 ScreenAttrs[off] = hl_attr(HLF_CUC);
4455 break;
4456 }
4457 ScreenAttrs[off++] = 0;
4458 ++vcol;
4459 }
4460 }
4461#endif
4462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4464 wp->w_p_rl);
4465 row++;
4466
4467 /*
4468 * Update w_cline_height and w_cline_folded if the cursor line was
4469 * updated (saves a call to plines() later).
4470 */
4471 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4472 {
4473 curwin->w_cline_row = startrow;
4474 curwin->w_cline_height = row - startrow;
4475#ifdef FEAT_FOLDING
4476 curwin->w_cline_folded = FALSE;
4477#endif
4478 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4479 }
4480
4481 break;
4482 }
4483
4484 /* line continues beyond line end */
4485 if (lcs_ext
4486 && !wp->w_p_wrap
4487#ifdef FEAT_DIFF
4488 && filler_todo <= 0
4489#endif
4490 && (
4491#ifdef FEAT_RIGHTLEFT
4492 wp->w_p_rl ? col == 0 :
4493#endif
4494 col == W_WIDTH(wp) - 1)
4495 && (*ptr != NUL
4496 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4497 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4498 {
4499 c = lcs_ext;
4500 char_attr = hl_attr(HLF_AT);
4501#ifdef FEAT_MBYTE
4502 mb_c = c;
4503 if (enc_utf8 && (*mb_char2len)(c) > 1)
4504 {
4505 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004506 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004507 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 else
4510 mb_utf8 = FALSE;
4511#endif
4512 }
4513
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004514#ifdef FEAT_SYN_HL
4515 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4516 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004517 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004518 && lnum != wp->w_cursor.lnum
4519 && draw_state == WL_LINE)
4520 {
4521 vcol_save_attr = char_attr;
4522 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4523 }
4524 else
4525 vcol_save_attr = -1;
4526#endif
4527
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 /*
4529 * Store character to be displayed.
4530 * Skip characters that are left of the screen for 'nowrap'.
4531 */
4532 vcol_prev = vcol;
4533 if (draw_state < WL_LINE || n_skip <= 0)
4534 {
4535 /*
4536 * Store the character.
4537 */
4538#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4539 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4540 {
4541 /* A double-wide character is: put first halve in left cell. */
4542 --off;
4543 --col;
4544 }
4545#endif
4546 ScreenLines[off] = c;
4547#ifdef FEAT_MBYTE
4548 if (enc_dbcs == DBCS_JPNU)
4549 ScreenLines2[off] = mb_c & 0xff;
4550 else if (enc_utf8)
4551 {
4552 if (mb_utf8)
4553 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004554 int i;
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004557 if ((c & 0xff) == 0)
4558 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004559 for (i = 0; i < Screen_mco; ++i)
4560 {
4561 ScreenLinesC[i][off] = u8cc[i];
4562 if (u8cc[i] == 0)
4563 break;
4564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566 else
4567 ScreenLinesUC[off] = 0;
4568 }
4569 if (multi_attr)
4570 {
4571 ScreenAttrs[off] = multi_attr;
4572 multi_attr = 0;
4573 }
4574 else
4575#endif
4576 ScreenAttrs[off] = char_attr;
4577
4578#ifdef FEAT_MBYTE
4579 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4580 {
4581 /* Need to fill two screen columns. */
4582 ++off;
4583 ++col;
4584 if (enc_utf8)
4585 /* UTF-8: Put a 0 in the second screen char. */
4586 ScreenLines[off] = 0;
4587 else
4588 /* DBCS: Put second byte in the second screen char. */
4589 ScreenLines[off] = mb_c & 0xff;
4590 ++vcol;
4591 /* When "tocol" is halfway a character, set it to the end of
4592 * the character, otherwise highlighting won't stop. */
4593 if (tocol == vcol)
4594 ++tocol;
4595#ifdef FEAT_RIGHTLEFT
4596 if (wp->w_p_rl)
4597 {
4598 /* now it's time to backup one cell */
4599 --off;
4600 --col;
4601 }
4602#endif
4603 }
4604#endif
4605#ifdef FEAT_RIGHTLEFT
4606 if (wp->w_p_rl)
4607 {
4608 --off;
4609 --col;
4610 }
4611 else
4612#endif
4613 {
4614 ++off;
4615 ++col;
4616 }
4617 }
4618 else
4619 --n_skip;
4620
4621 /* Only advance the "vcol" when after the 'number' column. */
4622 if (draw_state >= WL_SBR
4623#ifdef FEAT_DIFF
4624 && filler_todo <= 0
4625#endif
4626 )
4627 ++vcol;
4628
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004629#ifdef FEAT_SYN_HL
4630 if (vcol_save_attr >= 0)
4631 char_attr = vcol_save_attr;
4632#endif
4633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 /* restore attributes after "predeces" in 'listchars' */
4635 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4636 char_attr = saved_attr3;
4637
4638 /* restore attributes after last 'listchars' or 'number' char */
4639 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4640 char_attr = saved_attr2;
4641
4642 /*
4643 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004644 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 */
4646 if ((
4647#ifdef FEAT_RIGHTLEFT
4648 wp->w_p_rl ? (col < 0) :
4649#endif
4650 (col >= W_WIDTH(wp)))
4651 && (*ptr != NUL
4652#ifdef FEAT_DIFF
4653 || filler_todo > 0
4654#endif
4655 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4656 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4657 )
4658 {
4659 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4660 wp->w_p_rl);
4661 ++row;
4662 ++screen_row;
4663
4664 /* When not wrapping and finished diff lines, or when displayed
4665 * '$' and highlighting until last column, break here. */
4666 if ((!wp->w_p_wrap
4667#ifdef FEAT_DIFF
4668 && filler_todo <= 0
4669#endif
4670 ) || lcs_eol_one == -1)
4671 break;
4672
4673 /* When the window is too narrow draw all "@" lines. */
4674 if (draw_state != WL_LINE
4675#ifdef FEAT_DIFF
4676 && filler_todo <= 0
4677#endif
4678 )
4679 {
4680 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4681#ifdef FEAT_VERTSPLIT
4682 draw_vsep_win(wp, row);
4683#endif
4684 row = endrow;
4685 }
4686
4687 /* When line got too long for screen break here. */
4688 if (row == endrow)
4689 {
4690 ++row;
4691 break;
4692 }
4693
4694 if (screen_cur_row == screen_row - 1
4695#ifdef FEAT_DIFF
4696 && filler_todo <= 0
4697#endif
4698 && W_WIDTH(wp) == Columns)
4699 {
4700 /* Remember that the line wraps, used for modeless copy. */
4701 LineWraps[screen_row - 1] = TRUE;
4702
4703 /*
4704 * Special trick to make copy/paste of wrapped lines work with
4705 * xterm/screen: write an extra character beyond the end of
4706 * the line. This will work with all terminal types
4707 * (regardless of the xn,am settings).
4708 * Only do this on a fast tty.
4709 * Only do this if the cursor is on the current line
4710 * (something has been written in it).
4711 * Don't do this for the GUI.
4712 * Don't do this for double-width characters.
4713 * Don't do this for a window not at the right screen border.
4714 */
4715 if (p_tf
4716#ifdef FEAT_GUI
4717 && !gui.in_use
4718#endif
4719#ifdef FEAT_MBYTE
4720 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004721 && ((*mb_off2cells)(LineOffset[screen_row],
4722 LineOffset[screen_row] + screen_Columns)
4723 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004725 + (int)Columns - 2,
4726 LineOffset[screen_row] + screen_Columns)
4727 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728#endif
4729 )
4730 {
4731 /* First make sure we are at the end of the screen line,
4732 * then output the same character again to let the
4733 * terminal know about the wrap. If the terminal doesn't
4734 * auto-wrap, we overwrite the character. */
4735 if (screen_cur_col != W_WIDTH(wp))
4736 screen_char(LineOffset[screen_row - 1]
4737 + (unsigned)Columns - 1,
4738 screen_row - 1, (int)(Columns - 1));
4739
4740#ifdef FEAT_MBYTE
4741 /* When there is a multi-byte character, just output a
4742 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004743 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4744 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 out_char(' ');
4746 else
4747#endif
4748 out_char(ScreenLines[LineOffset[screen_row - 1]
4749 + (Columns - 1)]);
4750 /* force a redraw of the first char on the next line */
4751 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4752 screen_start(); /* don't know where cursor is now */
4753 }
4754 }
4755
4756 col = 0;
4757 off = (unsigned)(current_ScreenLine - ScreenLines);
4758#ifdef FEAT_RIGHTLEFT
4759 if (wp->w_p_rl)
4760 {
4761 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4762 off += col;
4763 }
4764#endif
4765
4766 /* reset the drawing state for the start of a wrapped line */
4767 draw_state = WL_START;
4768 saved_n_extra = n_extra;
4769 saved_p_extra = p_extra;
4770 saved_c_extra = c_extra;
4771 saved_char_attr = char_attr;
4772 n_extra = 0;
4773 lcs_prec_todo = lcs_prec;
4774#ifdef FEAT_LINEBREAK
4775# ifdef FEAT_DIFF
4776 if (filler_todo <= 0)
4777# endif
4778 need_showbreak = TRUE;
4779#endif
4780#ifdef FEAT_DIFF
4781 --filler_todo;
4782 /* When the filler lines are actually below the last line of the
4783 * file, don't draw the line itself, break here. */
4784 if (filler_todo == 0 && wp->w_botfill)
4785 break;
4786#endif
4787 }
4788
4789 } /* for every character in the line */
4790
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004791#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004792 /* After an empty line check first word for capital. */
4793 if (*skipwhite(line) == NUL)
4794 {
4795 capcol_lnum = lnum + 1;
4796 cap_col = 0;
4797 }
4798#endif
4799
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 return row;
4801}
4802
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004803#ifdef FEAT_MBYTE
4804static int comp_char_differs __ARGS((int, int));
4805
4806/*
4807 * Return if the composing characters at "off_from" and "off_to" differ.
4808 */
4809 static int
4810comp_char_differs(off_from, off_to)
4811 int off_from;
4812 int off_to;
4813{
4814 int i;
4815
4816 for (i = 0; i < Screen_mco; ++i)
4817 {
4818 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4819 return TRUE;
4820 if (ScreenLinesC[i][off_from] == 0)
4821 break;
4822 }
4823 return FALSE;
4824}
4825#endif
4826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827/*
4828 * Check whether the given character needs redrawing:
4829 * - the (first byte of the) character is different
4830 * - the attributes are different
4831 * - the character is multi-byte and the next byte is different
4832 */
4833 static int
4834char_needs_redraw(off_from, off_to, cols)
4835 int off_from;
4836 int off_to;
4837 int cols;
4838{
4839 if (cols > 0
4840 && ((ScreenLines[off_from] != ScreenLines[off_to]
4841 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4842
4843#ifdef FEAT_MBYTE
4844 || (enc_dbcs != 0
4845 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4846 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4847 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4848 : (cols > 1 && ScreenLines[off_from + 1]
4849 != ScreenLines[off_to + 1])))
4850 || (enc_utf8
4851 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4852 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004853 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#endif
4855 ))
4856 return TRUE;
4857 return FALSE;
4858}
4859
4860/*
4861 * Move one "cooked" screen line to the screen, but only the characters that
4862 * have actually changed. Handle insert/delete character.
4863 * "coloff" gives the first column on the screen for this line.
4864 * "endcol" gives the columns where valid characters are.
4865 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4866 * needs to be cleared, negative otherwise.
4867 * "rlflag" is TRUE in a rightleft window:
4868 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4869 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4870 */
4871 static void
4872screen_line(row, coloff, endcol, clear_width
4873#ifdef FEAT_RIGHTLEFT
4874 , rlflag
4875#endif
4876 )
4877 int row;
4878 int coloff;
4879 int endcol;
4880 int clear_width;
4881#ifdef FEAT_RIGHTLEFT
4882 int rlflag;
4883#endif
4884{
4885 unsigned off_from;
4886 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004887#ifdef FEAT_MBYTE
4888 unsigned max_off_from;
4889 unsigned max_off_to;
4890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 int col = 0;
4892#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4893 int hl;
4894#endif
4895 int force = FALSE; /* force update rest of the line */
4896 int redraw_this /* bool: does character need redraw? */
4897#ifdef FEAT_GUI
4898 = TRUE /* For GUI when while-loop empty */
4899#endif
4900 ;
4901 int redraw_next; /* redraw_this for next character */
4902#ifdef FEAT_MBYTE
4903 int clear_next = FALSE;
4904 int char_cells; /* 1: normal char */
4905 /* 2: occupies two display cells */
4906# define CHAR_CELLS char_cells
4907#else
4908# define CHAR_CELLS 1
4909#endif
4910
4911# ifdef FEAT_CLIPBOARD
4912 clip_may_clear_selection(row, row);
4913# endif
4914
4915 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4916 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004917#ifdef FEAT_MBYTE
4918 max_off_from = off_from + screen_Columns;
4919 max_off_to = LineOffset[row] + screen_Columns;
4920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
4922#ifdef FEAT_RIGHTLEFT
4923 if (rlflag)
4924 {
4925 /* Clear rest first, because it's left of the text. */
4926 if (clear_width > 0)
4927 {
4928 while (col <= endcol && ScreenLines[off_to] == ' '
4929 && ScreenAttrs[off_to] == 0
4930# ifdef FEAT_MBYTE
4931 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4932# endif
4933 )
4934 {
4935 ++off_to;
4936 ++col;
4937 }
4938 if (col <= endcol)
4939 screen_fill(row, row + 1, col + coloff,
4940 endcol + coloff + 1, ' ', ' ', 0);
4941 }
4942 col = endcol + 1;
4943 off_to = LineOffset[row] + col + coloff;
4944 off_from += col;
4945 endcol = (clear_width > 0 ? clear_width : -clear_width);
4946 }
4947#endif /* FEAT_RIGHTLEFT */
4948
4949 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4950
4951 while (col < endcol)
4952 {
4953#ifdef FEAT_MBYTE
4954 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00004955 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 else
4957 char_cells = 1;
4958#endif
4959
4960 redraw_this = redraw_next;
4961 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4962 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4963
4964#ifdef FEAT_GUI
4965 /* If the next character was bold, then redraw the current character to
4966 * remove any pixels that might have spilt over into us. This only
4967 * happens in the GUI.
4968 */
4969 if (redraw_next && gui.in_use)
4970 {
4971 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004972 if (hl > HL_ALL)
4973 hl = syn_attr2attr(hl);
4974 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 redraw_this = TRUE;
4976 }
4977#endif
4978
4979 if (redraw_this)
4980 {
4981 /*
4982 * Special handling when 'xs' termcap flag set (hpterm):
4983 * Attributes for characters are stored at the position where the
4984 * cursor is when writing the highlighting code. The
4985 * start-highlighting code must be written with the cursor on the
4986 * first highlighted character. The stop-highlighting code must
4987 * be written with the cursor just after the last highlighted
4988 * character.
4989 * Overwriting a character doesn't remove it's highlighting. Need
4990 * to clear the rest of the line, and force redrawing it
4991 * completely.
4992 */
4993 if ( p_wiv
4994 && !force
4995#ifdef FEAT_GUI
4996 && !gui.in_use
4997#endif
4998 && ScreenAttrs[off_to] != 0
4999 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5000 {
5001 /*
5002 * Need to remove highlighting attributes here.
5003 */
5004 windgoto(row, col + coloff);
5005 out_str(T_CE); /* clear rest of this screen line */
5006 screen_start(); /* don't know where cursor is now */
5007 force = TRUE; /* force redraw of rest of the line */
5008 redraw_next = TRUE; /* or else next char would miss out */
5009
5010 /*
5011 * If the previous character was highlighted, need to stop
5012 * highlighting at this character.
5013 */
5014 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5015 {
5016 screen_attr = ScreenAttrs[off_to - 1];
5017 term_windgoto(row, col + coloff);
5018 screen_stop_highlight();
5019 }
5020 else
5021 screen_attr = 0; /* highlighting has stopped */
5022 }
5023#ifdef FEAT_MBYTE
5024 if (enc_dbcs != 0)
5025 {
5026 /* Check if overwriting a double-byte with a single-byte or
5027 * the other way around requires another character to be
5028 * redrawn. For UTF-8 this isn't needed, because comparing
5029 * ScreenLinesUC[] is sufficient. */
5030 if (char_cells == 1
5031 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005032 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 /* Writing a single-cell character over a double-cell
5035 * character: need to redraw the next cell. */
5036 ScreenLines[off_to + 1] = 0;
5037 redraw_next = TRUE;
5038 }
5039 else if (char_cells == 2
5040 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005041 && (*mb_off2cells)(off_to, max_off_to) == 1
5042 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
5044 /* Writing the second half of a double-cell character over
5045 * a double-cell character: need to redraw the second
5046 * cell. */
5047 ScreenLines[off_to + 2] = 0;
5048 redraw_next = TRUE;
5049 }
5050
5051 if (enc_dbcs == DBCS_JPNU)
5052 ScreenLines2[off_to] = ScreenLines2[off_from];
5053 }
5054 /* When writing a single-width character over a double-width
5055 * character and at the end of the redrawn text, need to clear out
5056 * the right halve of the old character.
5057 * Also required when writing the right halve of a double-width
5058 * char over the left halve of an existing one. */
5059 if (has_mbyte && col + char_cells == endcol
5060 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005061 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005063 && (*mb_off2cells)(off_to, max_off_to) == 1
5064 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 clear_next = TRUE;
5066#endif
5067
5068 ScreenLines[off_to] = ScreenLines[off_from];
5069#ifdef FEAT_MBYTE
5070 if (enc_utf8)
5071 {
5072 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5073 if (ScreenLinesUC[off_from] != 0)
5074 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005075 int i;
5076
5077 for (i = 0; i < Screen_mco; ++i)
5078 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 }
5081 if (char_cells == 2)
5082 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5083#endif
5084
5085#if defined(FEAT_GUI) || defined(UNIX)
5086 /* The bold trick makes a single row of pixels appear in the next
5087 * character. When a bold character is removed, the next
5088 * character should be redrawn too. This happens for our own GUI
5089 * and for some xterms. */
5090 if (
5091# ifdef FEAT_GUI
5092 gui.in_use
5093# endif
5094# if defined(FEAT_GUI) && defined(UNIX)
5095 ||
5096# endif
5097# ifdef UNIX
5098 term_is_xterm
5099# endif
5100 )
5101 {
5102 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005103 if (hl > HL_ALL)
5104 hl = syn_attr2attr(hl);
5105 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 redraw_next = TRUE;
5107 }
5108#endif
5109 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5110#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005111 /* For simplicity set the attributes of second half of a
5112 * double-wide character equal to the first half. */
5113 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005115
5116 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 else
5119#endif
5120 screen_char(off_to, row, col + coloff);
5121 }
5122 else if ( p_wiv
5123#ifdef FEAT_GUI
5124 && !gui.in_use
5125#endif
5126 && col + coloff > 0)
5127 {
5128 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5129 {
5130 /*
5131 * Don't output stop-highlight when moving the cursor, it will
5132 * stop the highlighting when it should continue.
5133 */
5134 screen_attr = 0;
5135 }
5136 else if (screen_attr != 0)
5137 screen_stop_highlight();
5138 }
5139
5140 off_to += CHAR_CELLS;
5141 off_from += CHAR_CELLS;
5142 col += CHAR_CELLS;
5143 }
5144
5145#ifdef FEAT_MBYTE
5146 if (clear_next)
5147 {
5148 /* Clear the second half of a double-wide character of which the left
5149 * half was overwritten with a single-wide character. */
5150 ScreenLines[off_to] = ' ';
5151 if (enc_utf8)
5152 ScreenLinesUC[off_to] = 0;
5153 screen_char(off_to, row, col + coloff);
5154 }
5155#endif
5156
5157 if (clear_width > 0
5158#ifdef FEAT_RIGHTLEFT
5159 && !rlflag
5160#endif
5161 )
5162 {
5163#ifdef FEAT_GUI
5164 int startCol = col;
5165#endif
5166
5167 /* blank out the rest of the line */
5168 while (col < clear_width && ScreenLines[off_to] == ' '
5169 && ScreenAttrs[off_to] == 0
5170#ifdef FEAT_MBYTE
5171 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5172#endif
5173 )
5174 {
5175 ++off_to;
5176 ++col;
5177 }
5178 if (col < clear_width)
5179 {
5180#ifdef FEAT_GUI
5181 /*
5182 * In the GUI, clearing the rest of the line may leave pixels
5183 * behind if the first character cleared was bold. Some bold
5184 * fonts spill over the left. In this case we redraw the previous
5185 * character too. If we didn't skip any blanks above, then we
5186 * only redraw if the character wasn't already redrawn anyway.
5187 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005188 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
5190 hl = ScreenAttrs[off_to];
5191 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005192 {
5193 int prev_cells = 1;
5194# ifdef FEAT_MBYTE
5195 if (enc_utf8)
5196 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5197 * that its width is 2. */
5198 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5199 else if (enc_dbcs != 0)
5200 {
5201 /* find previous character by counting from first
5202 * column and get its width. */
5203 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005204 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005205
5206 while (off < off_to)
5207 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005208 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005209 off += prev_cells;
5210 }
5211 }
5212
5213 if (enc_dbcs != 0 && prev_cells > 1)
5214 screen_char_2(off_to - prev_cells, row,
5215 col + coloff - prev_cells);
5216 else
5217# endif
5218 screen_char(off_to - prev_cells, row,
5219 col + coloff - prev_cells);
5220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
5222#endif
5223 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5224 ' ', ' ', 0);
5225#ifdef FEAT_VERTSPLIT
5226 off_to += clear_width - col;
5227 col = clear_width;
5228#endif
5229 }
5230 }
5231
5232 if (clear_width > 0)
5233 {
5234#ifdef FEAT_VERTSPLIT
5235 /* For a window that's left of another, draw the separator char. */
5236 if (col + coloff < Columns)
5237 {
5238 int c;
5239
5240 c = fillchar_vsep(&hl);
5241 if (ScreenLines[off_to] != c
5242# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005243 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5244 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245# endif
5246 || ScreenAttrs[off_to] != hl)
5247 {
5248 ScreenLines[off_to] = c;
5249 ScreenAttrs[off_to] = hl;
5250# ifdef FEAT_MBYTE
5251 if (enc_utf8)
5252 {
5253 if (c >= 0x80)
5254 {
5255 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005256 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258 else
5259 ScreenLinesUC[off_to] = 0;
5260 }
5261# endif
5262 screen_char(off_to, row, col + coloff);
5263 }
5264 }
5265 else
5266#endif
5267 LineWraps[row] = FALSE;
5268 }
5269}
5270
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005271#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005273 * Mirror text "str" for right-left displaying.
5274 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005276 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277rl_mirror(str)
5278 char_u *str;
5279{
5280 char_u *p1, *p2;
5281 int t;
5282
5283 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5284 {
5285 t = *p1;
5286 *p1 = *p2;
5287 *p2 = t;
5288 }
5289}
5290#endif
5291
5292#if defined(FEAT_WINDOWS) || defined(PROTO)
5293/*
5294 * mark all status lines for redraw; used after first :cd
5295 */
5296 void
5297status_redraw_all()
5298{
5299 win_T *wp;
5300
5301 for (wp = firstwin; wp; wp = wp->w_next)
5302 if (wp->w_status_height)
5303 {
5304 wp->w_redr_status = TRUE;
5305 redraw_later(VALID);
5306 }
5307}
5308
5309/*
5310 * mark all status lines of the current buffer for redraw
5311 */
5312 void
5313status_redraw_curbuf()
5314{
5315 win_T *wp;
5316
5317 for (wp = firstwin; wp; wp = wp->w_next)
5318 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5319 {
5320 wp->w_redr_status = TRUE;
5321 redraw_later(VALID);
5322 }
5323}
5324
5325/*
5326 * Redraw all status lines that need to be redrawn.
5327 */
5328 void
5329redraw_statuslines()
5330{
5331 win_T *wp;
5332
5333 for (wp = firstwin; wp; wp = wp->w_next)
5334 if (wp->w_redr_status)
5335 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005336 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005337 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338}
5339#endif
5340
5341#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5342/*
5343 * Redraw all status lines at the bottom of frame "frp".
5344 */
5345 void
5346win_redraw_last_status(frp)
5347 frame_T *frp;
5348{
5349 if (frp->fr_layout == FR_LEAF)
5350 frp->fr_win->w_redr_status = TRUE;
5351 else if (frp->fr_layout == FR_ROW)
5352 {
5353 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5354 win_redraw_last_status(frp);
5355 }
5356 else /* frp->fr_layout == FR_COL */
5357 {
5358 frp = frp->fr_child;
5359 while (frp->fr_next != NULL)
5360 frp = frp->fr_next;
5361 win_redraw_last_status(frp);
5362 }
5363}
5364#endif
5365
5366#ifdef FEAT_VERTSPLIT
5367/*
5368 * Draw the verticap separator right of window "wp" starting with line "row".
5369 */
5370 static void
5371draw_vsep_win(wp, row)
5372 win_T *wp;
5373 int row;
5374{
5375 int hl;
5376 int c;
5377
5378 if (wp->w_vsep_width)
5379 {
5380 /* draw the vertical separator right of this window */
5381 c = fillchar_vsep(&hl);
5382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5383 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5384 c, ' ', hl);
5385 }
5386}
5387#endif
5388
5389#ifdef FEAT_WILDMENU
5390static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005391static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
5393/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005394 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 */
5396 static int
5397status_match_len(xp, s)
5398 expand_T *xp;
5399 char_u *s;
5400{
5401 int len = 0;
5402
5403#ifdef FEAT_MENU
5404 int emenu = (xp->xp_context == EXPAND_MENUS
5405 || xp->xp_context == EXPAND_MENUNAMES);
5406
5407 /* Check for menu separators - replace with '|'. */
5408 if (emenu && menu_is_separator(s))
5409 return 1;
5410#endif
5411
5412 while (*s != NUL)
5413 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005414 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005416 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005417 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
5419
5420 return len;
5421}
5422
5423/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005424 * Return TRUE for characters that are not displayed in a status match.
5425 * These are backslashes used for escaping. Do show backslashes in help tags.
5426 */
5427 static int
5428skip_status_match_char(xp, s)
5429 expand_T *xp;
5430 char_u *s;
5431{
5432 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5433#ifdef FEAT_MENU
5434 || ((xp->xp_context == EXPAND_MENUS
5435 || xp->xp_context == EXPAND_MENUNAMES)
5436 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5437#endif
5438 );
5439}
5440
5441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 * Show wildchar matches in the status line.
5443 * Show at least the "match" item.
5444 * We start at item 'first_match' in the list and show all matches that fit.
5445 *
5446 * If inversion is possible we use it. Else '=' characters are used.
5447 */
5448 void
5449win_redr_status_matches(xp, num_matches, matches, match, showtail)
5450 expand_T *xp;
5451 int num_matches;
5452 char_u **matches; /* list of matches */
5453 int match;
5454 int showtail;
5455{
5456#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5457 int row;
5458 char_u *buf;
5459 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005460 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 int fillchar;
5462 int attr;
5463 int i;
5464 int highlight = TRUE;
5465 char_u *selstart = NULL;
5466 int selstart_col = 0;
5467 char_u *selend = NULL;
5468 static int first_match = 0;
5469 int add_left = FALSE;
5470 char_u *s;
5471#ifdef FEAT_MENU
5472 int emenu;
5473#endif
5474#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5475 int l;
5476#endif
5477
5478 if (matches == NULL) /* interrupted completion? */
5479 return;
5480
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005481#ifdef FEAT_MBYTE
5482 if (has_mbyte)
5483 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5484 else
5485#endif
5486 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 if (buf == NULL)
5488 return;
5489
5490 if (match == -1) /* don't show match but original text */
5491 {
5492 match = 0;
5493 highlight = FALSE;
5494 }
5495 /* count 1 for the ending ">" */
5496 clen = status_match_len(xp, L_MATCH(match)) + 3;
5497 if (match == 0)
5498 first_match = 0;
5499 else if (match < first_match)
5500 {
5501 /* jumping left, as far as we can go */
5502 first_match = match;
5503 add_left = TRUE;
5504 }
5505 else
5506 {
5507 /* check if match fits on the screen */
5508 for (i = first_match; i < match; ++i)
5509 clen += status_match_len(xp, L_MATCH(i)) + 2;
5510 if (first_match > 0)
5511 clen += 2;
5512 /* jumping right, put match at the left */
5513 if ((long)clen > Columns)
5514 {
5515 first_match = match;
5516 /* if showing the last match, we can add some on the left */
5517 clen = 2;
5518 for (i = match; i < num_matches; ++i)
5519 {
5520 clen += status_match_len(xp, L_MATCH(i)) + 2;
5521 if ((long)clen >= Columns)
5522 break;
5523 }
5524 if (i == num_matches)
5525 add_left = TRUE;
5526 }
5527 }
5528 if (add_left)
5529 while (first_match > 0)
5530 {
5531 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5532 if ((long)clen >= Columns)
5533 break;
5534 --first_match;
5535 }
5536
5537 fillchar = fillchar_status(&attr, TRUE);
5538
5539 if (first_match == 0)
5540 {
5541 *buf = NUL;
5542 len = 0;
5543 }
5544 else
5545 {
5546 STRCPY(buf, "< ");
5547 len = 2;
5548 }
5549 clen = len;
5550
5551 i = first_match;
5552 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5553 {
5554 if (i == match)
5555 {
5556 selstart = buf + len;
5557 selstart_col = clen;
5558 }
5559
5560 s = L_MATCH(i);
5561 /* Check for menu separators - replace with '|' */
5562#ifdef FEAT_MENU
5563 emenu = (xp->xp_context == EXPAND_MENUS
5564 || xp->xp_context == EXPAND_MENUNAMES);
5565 if (emenu && menu_is_separator(s))
5566 {
5567 STRCPY(buf + len, transchar('|'));
5568 l = (int)STRLEN(buf + len);
5569 len += l;
5570 clen += l;
5571 }
5572 else
5573#endif
5574 for ( ; *s != NUL; ++s)
5575 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005576 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 ++s;
5578 clen += ptr2cells(s);
5579#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005580 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 STRNCPY(buf + len, s, l);
5583 s += l - 1;
5584 len += l;
5585 }
5586 else
5587#endif
5588 {
5589 STRCPY(buf + len, transchar_byte(*s));
5590 len += (int)STRLEN(buf + len);
5591 }
5592 }
5593 if (i == match)
5594 selend = buf + len;
5595
5596 *(buf + len++) = ' ';
5597 *(buf + len++) = ' ';
5598 clen += 2;
5599 if (++i == num_matches)
5600 break;
5601 }
5602
5603 if (i != num_matches)
5604 {
5605 *(buf + len++) = '>';
5606 ++clen;
5607 }
5608
5609 buf[len] = NUL;
5610
5611 row = cmdline_row - 1;
5612 if (row >= 0)
5613 {
5614 if (wild_menu_showing == 0)
5615 {
5616 if (msg_scrolled > 0)
5617 {
5618 /* Put the wildmenu just above the command line. If there is
5619 * no room, scroll the screen one line up. */
5620 if (cmdline_row == Rows - 1)
5621 {
5622 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5623 ++msg_scrolled;
5624 }
5625 else
5626 {
5627 ++cmdline_row;
5628 ++row;
5629 }
5630 wild_menu_showing = WM_SCROLLED;
5631 }
5632 else
5633 {
5634 /* Create status line if needed by setting 'laststatus' to 2.
5635 * Set 'winminheight' to zero to avoid that the window is
5636 * resized. */
5637 if (lastwin->w_status_height == 0)
5638 {
5639 save_p_ls = p_ls;
5640 save_p_wmh = p_wmh;
5641 p_ls = 2;
5642 p_wmh = 0;
5643 last_status(FALSE);
5644 }
5645 wild_menu_showing = WM_SHOWN;
5646 }
5647 }
5648
5649 screen_puts(buf, row, 0, attr);
5650 if (selstart != NULL && highlight)
5651 {
5652 *selend = NUL;
5653 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5654 }
5655
5656 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5657 }
5658
5659#ifdef FEAT_VERTSPLIT
5660 win_redraw_last_status(topframe);
5661#else
5662 lastwin->w_redr_status = TRUE;
5663#endif
5664 vim_free(buf);
5665}
5666#endif
5667
5668#if defined(FEAT_WINDOWS) || defined(PROTO)
5669/*
5670 * Redraw the status line of window wp.
5671 *
5672 * If inversion is possible we use it. Else '=' characters are used.
5673 */
5674 void
5675win_redr_status(wp)
5676 win_T *wp;
5677{
5678 int row;
5679 char_u *p;
5680 int len;
5681 int fillchar;
5682 int attr;
5683 int this_ru_col;
5684
5685 wp->w_redr_status = FALSE;
5686 if (wp->w_status_height == 0)
5687 {
5688 /* no status line, can only be last window */
5689 redraw_cmdline = TRUE;
5690 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005691 else if (!redrawing()
5692#ifdef FEAT_INS_EXPAND
5693 /* don't update status line when popup menu is visible and may be
5694 * drawn over it */
5695 || pum_visible()
5696#endif
5697 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
5699 /* Don't redraw right now, do it later. */
5700 wp->w_redr_status = TRUE;
5701 }
5702#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005703 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005706 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708#endif
5709 else
5710 {
5711 fillchar = fillchar_status(&attr, wp == curwin);
5712
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005713 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 p = NameBuff;
5715 len = (int)STRLEN(p);
5716
5717 if (wp->w_buffer->b_help
5718#ifdef FEAT_QUICKFIX
5719 || wp->w_p_pvw
5720#endif
5721 || bufIsChanged(wp->w_buffer)
5722 || wp->w_buffer->b_p_ro)
5723 *(p + len++) = ' ';
5724 if (wp->w_buffer->b_help)
5725 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005726 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 len += (int)STRLEN(p + len);
5728 }
5729#ifdef FEAT_QUICKFIX
5730 if (wp->w_p_pvw)
5731 {
5732 STRCPY(p + len, _("[Preview]"));
5733 len += (int)STRLEN(p + len);
5734 }
5735#endif
5736 if (bufIsChanged(wp->w_buffer))
5737 {
5738 STRCPY(p + len, "[+]");
5739 len += 3;
5740 }
5741 if (wp->w_buffer->b_p_ro)
5742 {
5743 STRCPY(p + len, "[RO]");
5744 len += 4;
5745 }
5746
5747#ifndef FEAT_VERTSPLIT
5748 this_ru_col = ru_col;
5749 if (this_ru_col < (Columns + 1) / 2)
5750 this_ru_col = (Columns + 1) / 2;
5751#else
5752 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5753 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5754 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5755 if (this_ru_col <= 1)
5756 {
5757 p = (char_u *)"<"; /* No room for file name! */
5758 len = 1;
5759 }
5760 else
5761#endif
5762#ifdef FEAT_MBYTE
5763 if (has_mbyte)
5764 {
5765 int clen = 0, i;
5766
5767 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005768 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 clen += (*mb_ptr2cells)(p + i);
5770 /* Find first character that will fit.
5771 * Going from start to end is much faster for DBCS. */
5772 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005773 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 clen -= (*mb_ptr2cells)(p + i);
5775 len = clen;
5776 if (i > 0)
5777 {
5778 p = p + i - 1;
5779 *p = '<';
5780 ++len;
5781 }
5782
5783 }
5784 else
5785#endif
5786 if (len > this_ru_col - 1)
5787 {
5788 p += len - (this_ru_col - 1);
5789 *p = '<';
5790 len = this_ru_col - 1;
5791 }
5792
5793 row = W_WINROW(wp) + wp->w_height;
5794 screen_puts(p, row, W_WINCOL(wp), attr);
5795 screen_fill(row, row + 1, len + W_WINCOL(wp),
5796 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5797
5798 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5799 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5800 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5801 - 1 + W_WINCOL(wp)), attr);
5802
5803#ifdef FEAT_CMDL_INFO
5804 win_redr_ruler(wp, TRUE);
5805#endif
5806 }
5807
5808#ifdef FEAT_VERTSPLIT
5809 /*
5810 * May need to draw the character below the vertical separator.
5811 */
5812 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5813 {
5814 if (stl_connected(wp))
5815 fillchar = fillchar_status(&attr, wp == curwin);
5816 else
5817 fillchar = fillchar_vsep(&attr);
5818 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5819 attr);
5820 }
5821#endif
5822}
5823
Bram Moolenaar238a5642006-02-21 22:12:05 +00005824#ifdef FEAT_STL_OPT
5825/*
5826 * Redraw the status line according to 'statusline' and take care of any
5827 * errors encountered.
5828 */
5829 static void
5830redraw_custum_statusline(wp)
5831 win_T *wp;
5832{
5833 int save_called_emsg = called_emsg;
5834
5835 called_emsg = FALSE;
5836 win_redr_custom(wp, FALSE);
5837 if (called_emsg)
5838 set_string_option_direct((char_u *)"statusline", -1,
5839 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005840 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005841 called_emsg |= save_called_emsg;
5842}
5843#endif
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845# ifdef FEAT_VERTSPLIT
5846/*
5847 * Return TRUE if the status line of window "wp" is connected to the status
5848 * line of the window right of it. If not, then it's a vertical separator.
5849 * Only call if (wp->w_vsep_width != 0).
5850 */
5851 int
5852stl_connected(wp)
5853 win_T *wp;
5854{
5855 frame_T *fr;
5856
5857 fr = wp->w_frame;
5858 while (fr->fr_parent != NULL)
5859 {
5860 if (fr->fr_parent->fr_layout == FR_COL)
5861 {
5862 if (fr->fr_next != NULL)
5863 break;
5864 }
5865 else
5866 {
5867 if (fr->fr_next != NULL)
5868 return TRUE;
5869 }
5870 fr = fr->fr_parent;
5871 }
5872 return FALSE;
5873}
5874# endif
5875
5876#endif /* FEAT_WINDOWS */
5877
5878#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5879/*
5880 * Get the value to show for the language mappings, active 'keymap'.
5881 */
5882 int
5883get_keymap_str(wp, buf, len)
5884 win_T *wp;
5885 char_u *buf; /* buffer for the result */
5886 int len; /* length of buffer */
5887{
5888 char_u *p;
5889
5890 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5891 return FALSE;
5892
5893 {
5894#ifdef FEAT_EVAL
5895 buf_T *old_curbuf = curbuf;
5896 win_T *old_curwin = curwin;
5897 char_u *s;
5898
5899 curbuf = wp->w_buffer;
5900 curwin = wp;
5901 STRCPY(buf, "b:keymap_name"); /* must be writable */
5902 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005903 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 --emsg_skip;
5905 curbuf = old_curbuf;
5906 curwin = old_curwin;
5907 if (p == NULL || *p == NUL)
5908#endif
5909 {
5910#ifdef FEAT_KEYMAP
5911 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5912 p = wp->w_buffer->b_p_keymap;
5913 else
5914#endif
5915 p = (char_u *)"lang";
5916 }
5917 if ((int)(STRLEN(p) + 3) < len)
5918 sprintf((char *)buf, "<%s>", p);
5919 else
5920 buf[0] = NUL;
5921#ifdef FEAT_EVAL
5922 vim_free(s);
5923#endif
5924 }
5925 return buf[0] != NUL;
5926}
5927#endif
5928
5929#if defined(FEAT_STL_OPT) || defined(PROTO)
5930/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005931 * Redraw the status line or ruler of window "wp".
5932 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 */
5934 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005935win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005937 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 int attr;
5940 int curattr;
5941 int row;
5942 int col = 0;
5943 int maxwidth;
5944 int width;
5945 int n;
5946 int len;
5947 int fillchar;
5948 char_u buf[MAXPATHL];
5949 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005950 struct stl_hlrec hltab[STL_MAX_ITEM];
5951 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005952 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
5954 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005955 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005957 /* Use 'tabline'. Always at the first line of the screen. */
5958 p = p_tal;
5959 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005960 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005961 attr = hl_attr(HLF_TPF);
5962 maxwidth = Columns;
5963# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005964 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005967 else
5968 {
5969 row = W_WINROW(wp) + wp->w_height;
5970 fillchar = fillchar_status(&attr, wp == curwin);
5971 maxwidth = W_WIDTH(wp);
5972
5973 if (draw_ruler)
5974 {
5975 p = p_ruf;
5976 /* advance past any leading group spec - implicit in ru_col */
5977 if (*p == '%')
5978 {
5979 if (*++p == '-')
5980 p++;
5981 if (atoi((char *) p))
5982 while (VIM_ISDIGIT(*p))
5983 p++;
5984 if (*p++ != '(')
5985 p = p_ruf;
5986 }
5987#ifdef FEAT_VERTSPLIT
5988 col = ru_col - (Columns - W_WIDTH(wp));
5989 if (col < (W_WIDTH(wp) + 1) / 2)
5990 col = (W_WIDTH(wp) + 1) / 2;
5991#else
5992 col = ru_col;
5993 if (col > (Columns + 1) / 2)
5994 col = (Columns + 1) / 2;
5995#endif
5996 maxwidth = W_WIDTH(wp) - col;
5997#ifdef FEAT_WINDOWS
5998 if (!wp->w_status_height)
5999#endif
6000 {
6001 row = Rows - 1;
6002 --maxwidth; /* writing in last column may cause scrolling */
6003 fillchar = ' ';
6004 attr = 0;
6005 }
6006
6007# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006008 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006009# endif
6010 }
6011 else
6012 {
6013 if (*wp->w_p_stl != NUL)
6014 p = wp->w_p_stl;
6015 else
6016 p = p_stl;
6017# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006018 use_sandbox = was_set_insecurely((char_u *)"statusline",
6019 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006020# endif
6021 }
6022
6023#ifdef FEAT_VERTSPLIT
6024 col += W_WINCOL(wp);
6025#endif
6026 }
6027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 if (maxwidth <= 0)
6029 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006031 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6032 buf, sizeof(buf),
6033 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006034 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006035 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036
6037 while (width < maxwidth && len < sizeof(buf) - 1)
6038 {
6039#ifdef FEAT_MBYTE
6040 len += (*mb_char2bytes)(fillchar, buf + len);
6041#else
6042 buf[len++] = fillchar;
6043#endif
6044 ++width;
6045 }
6046 buf[len] = NUL;
6047
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006048 /*
6049 * Draw each snippet with the specified highlighting.
6050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 curattr = attr;
6052 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006053 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006055 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 screen_puts_len(p, len, row, col, curattr);
6057 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006058 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006060 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006062 else if (hltab[n].userhl < 0)
6063 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006065 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006066 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#endif
6068 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006069 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 }
6071 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006072
6073 if (wp == NULL)
6074 {
6075 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6076 col = 0;
6077 len = 0;
6078 p = buf;
6079 fillchar = 0;
6080 for (n = 0; tabtab[n].start != NULL; n++)
6081 {
6082 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6083 while (col < len)
6084 TabPageIdxs[col++] = fillchar;
6085 p = tabtab[n].start;
6086 fillchar = tabtab[n].userhl;
6087 }
6088 while (col < Columns)
6089 TabPageIdxs[col++] = fillchar;
6090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091}
6092
6093#endif /* FEAT_STL_OPT */
6094
6095/*
6096 * Output a single character directly to the screen and update ScreenLines.
6097 */
6098 void
6099screen_putchar(c, row, col, attr)
6100 int c;
6101 int row, col;
6102 int attr;
6103{
6104#ifdef FEAT_MBYTE
6105 char_u buf[MB_MAXBYTES + 1];
6106
6107 buf[(*mb_char2bytes)(c, buf)] = NUL;
6108#else
6109 char_u buf[2];
6110
6111 buf[0] = c;
6112 buf[1] = NUL;
6113#endif
6114 screen_puts(buf, row, col, attr);
6115}
6116
6117/*
6118 * Get a single character directly from ScreenLines into "bytes[]".
6119 * Also return its attribute in *attrp;
6120 */
6121 void
6122screen_getbytes(row, col, bytes, attrp)
6123 int row, col;
6124 char_u *bytes;
6125 int *attrp;
6126{
6127 unsigned off;
6128
6129 /* safety check */
6130 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6131 {
6132 off = LineOffset[row] + col;
6133 *attrp = ScreenAttrs[off];
6134 bytes[0] = ScreenLines[off];
6135 bytes[1] = NUL;
6136
6137#ifdef FEAT_MBYTE
6138 if (enc_utf8 && ScreenLinesUC[off] != 0)
6139 bytes[utfc_char2bytes(off, bytes)] = NUL;
6140 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6141 {
6142 bytes[0] = ScreenLines[off];
6143 bytes[1] = ScreenLines2[off];
6144 bytes[2] = NUL;
6145 }
6146 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6147 {
6148 bytes[1] = ScreenLines[off + 1];
6149 bytes[2] = NUL;
6150 }
6151#endif
6152 }
6153}
6154
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006155#ifdef FEAT_MBYTE
6156static int screen_comp_differs __ARGS((int, int*));
6157
6158/*
6159 * Return TRUE if composing characters for screen posn "off" differs from
6160 * composing characters in "u8cc".
6161 */
6162 static int
6163screen_comp_differs(off, u8cc)
6164 int off;
6165 int *u8cc;
6166{
6167 int i;
6168
6169 for (i = 0; i < Screen_mco; ++i)
6170 {
6171 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6172 return TRUE;
6173 if (u8cc[i] == 0)
6174 break;
6175 }
6176 return FALSE;
6177}
6178#endif
6179
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180/*
6181 * Put string '*text' on the screen at position 'row' and 'col', with
6182 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6183 * Note: only outputs within one row, message is truncated at screen boundary!
6184 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6185 */
6186 void
6187screen_puts(text, row, col, attr)
6188 char_u *text;
6189 int row;
6190 int col;
6191 int attr;
6192{
6193 screen_puts_len(text, -1, row, col, attr);
6194}
6195
6196/*
6197 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6198 * a NUL.
6199 */
6200 void
6201screen_puts_len(text, len, row, col, attr)
6202 char_u *text;
6203 int len;
6204 int row;
6205 int col;
6206 int attr;
6207{
6208 unsigned off;
6209 char_u *ptr = text;
6210 int c;
6211#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006212 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 int mbyte_blen = 1;
6214 int mbyte_cells = 1;
6215 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006216 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 int clear_next_cell = FALSE;
6218# ifdef FEAT_ARABIC
6219 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006220 int pc, nc, nc1;
6221 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222# endif
6223#endif
6224
6225 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6226 return;
6227
6228 off = LineOffset[row] + col;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006229#ifdef FEAT_MBYTE
6230 max_off = LineOffset[row] + screen_Columns;
6231#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006232 while (col < screen_Columns
6233 && (len < 0 || (int)(ptr - text) < len)
6234 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
6236 c = *ptr;
6237#ifdef FEAT_MBYTE
6238 /* check if this is the first byte of a multibyte */
6239 if (has_mbyte)
6240 {
6241 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006242 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006244 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6246 mbyte_cells = 1;
6247 else if (enc_dbcs != 0)
6248 mbyte_cells = mbyte_blen;
6249 else /* enc_utf8 */
6250 {
6251 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006252 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 (int)((text + len) - ptr));
6254 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006255 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006257# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 /* Non-BMP character: display as ? or fullwidth ?. */
6259 if (u8c >= 0x10000)
6260 {
6261 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6262 if (attr == 0)
6263 attr = hl_attr(HLF_8);
6264 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266# ifdef FEAT_ARABIC
6267 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6268 {
6269 /* Do Arabic shaping. */
6270 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6271 {
6272 /* Past end of string to be displayed. */
6273 nc = NUL;
6274 nc1 = NUL;
6275 }
6276 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006277 {
6278 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6279 nc1 = pcc[0];
6280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 pc = prev_c;
6282 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006283 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
6285 else
6286 prev_c = u8c;
6287# endif
6288 }
6289 }
6290#endif
6291
6292 if (ScreenLines[off] != c
6293#ifdef FEAT_MBYTE
6294 || (mbyte_cells == 2
6295 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6296 || (enc_dbcs == DBCS_JPNU
6297 && c == 0x8e
6298 && ScreenLines2[off] != ptr[1])
6299 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006300 && (ScreenLinesUC[off] != (u8char_T)u8c
6301 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302#endif
6303 || ScreenAttrs[off] != attr
6304 || exmode_active
6305 )
6306 {
6307#if defined(FEAT_GUI) || defined(UNIX)
6308 /* The bold trick makes a single row of pixels appear in the next
6309 * character. When a bold character is removed, the next
6310 * character should be redrawn too. This happens for our own GUI
6311 * and for some xterms.
6312 * Force the redraw by setting the attribute to a different value
6313 * than "attr", the contents of ScreenLines[] may be needed by
6314 * mb_off2cells() further on.
6315 * Don't do this for the last drawn character, because the next
6316 * character may not be redrawn. */
6317 if (
6318# ifdef FEAT_GUI
6319 gui.in_use
6320# endif
6321# if defined(FEAT_GUI) && defined(UNIX)
6322 ||
6323# endif
6324# ifdef UNIX
6325 term_is_xterm
6326# endif
6327 )
6328 {
6329 int n;
6330
6331 n = ScreenAttrs[off];
6332# ifdef FEAT_MBYTE
6333 if (col + mbyte_cells < screen_Columns
6334 && (n > HL_ALL || (n & HL_BOLD))
6335 && (len < 0 ? ptr[mbyte_blen] != NUL
6336 : ptr + mbyte_blen < text + len))
6337 ScreenAttrs[off + mbyte_cells] = attr + 1;
6338# else
6339 if (col + 1 < screen_Columns
6340 && (n > HL_ALL || (n & HL_BOLD))
6341 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6342 ScreenLines[off + 1] = 0;
6343# endif
6344 }
6345#endif
6346#ifdef FEAT_MBYTE
6347 /* When at the end of the text and overwriting a two-cell
6348 * character with a one-cell character, need to clear the next
6349 * cell. Also when overwriting the left halve of a two-cell char
6350 * with the right halve of a two-cell char. Do this only once
6351 * (mb_off2cells() may return 2 on the right halve). */
6352 if (clear_next_cell)
6353 clear_next_cell = FALSE;
6354 else if (has_mbyte
6355 && (len < 0 ? ptr[mbyte_blen] == NUL
6356 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006357 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006359 && (*mb_off2cells)(off, max_off) == 1
6360 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 clear_next_cell = TRUE;
6362
6363 /* Make sure we never leave a second byte of a double-byte behind,
6364 * it confuses mb_off2cells(). */
6365 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006366 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006368 && (*mb_off2cells)(off, max_off) == 1
6369 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 ScreenLines[off + mbyte_blen] = 0;
6371#endif
6372 ScreenLines[off] = c;
6373 ScreenAttrs[off] = attr;
6374#ifdef FEAT_MBYTE
6375 if (enc_utf8)
6376 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006377 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 ScreenLinesUC[off] = 0;
6379 else
6380 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006381 int i;
6382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006384 for (i = 0; i < Screen_mco; ++i)
6385 {
6386 ScreenLinesC[i][off] = u8cc[i];
6387 if (u8cc[i] == 0)
6388 break;
6389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 }
6391 if (mbyte_cells == 2)
6392 {
6393 ScreenLines[off + 1] = 0;
6394 ScreenAttrs[off + 1] = attr;
6395 }
6396 screen_char(off, row, col);
6397 }
6398 else if (mbyte_cells == 2)
6399 {
6400 ScreenLines[off + 1] = ptr[1];
6401 ScreenAttrs[off + 1] = attr;
6402 screen_char_2(off, row, col);
6403 }
6404 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6405 {
6406 ScreenLines2[off] = ptr[1];
6407 screen_char(off, row, col);
6408 }
6409 else
6410#endif
6411 screen_char(off, row, col);
6412 }
6413#ifdef FEAT_MBYTE
6414 if (has_mbyte)
6415 {
6416 off += mbyte_cells;
6417 col += mbyte_cells;
6418 ptr += mbyte_blen;
6419 if (clear_next_cell)
6420 ptr = (char_u *)" ";
6421 }
6422 else
6423#endif
6424 {
6425 ++off;
6426 ++col;
6427 ++ptr;
6428 }
6429 }
6430}
6431
6432#ifdef FEAT_SEARCH_EXTRA
6433/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006434 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 */
6436 static void
6437start_search_hl()
6438{
6439 if (p_hls && !no_hlsearch)
6440 {
6441 last_pat_prog(&search_hl.rm);
6442 search_hl.attr = hl_attr(HLF_L);
6443 }
6444}
6445
6446/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006447 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 */
6449 static void
6450end_search_hl()
6451{
6452 if (search_hl.rm.regprog != NULL)
6453 {
6454 vim_free(search_hl.rm.regprog);
6455 search_hl.rm.regprog = NULL;
6456 }
6457}
6458
6459/*
6460 * Advance to the match in window "wp" line "lnum" or past it.
6461 */
6462 static void
6463prepare_search_hl(wp, lnum)
6464 win_T *wp;
6465 linenr_T lnum;
6466{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006467 matchitem_T *cur; /* points to the match list */
6468 match_T *shl; /* points to search_hl or a match */
6469 int shl_flag; /* flag to indicate whether search_hl
6470 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 int n;
6472
6473 /*
6474 * When using a multi-line pattern, start searching at the top
6475 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006476 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006478 cur = wp->w_match_head;
6479 shl_flag = FALSE;
6480 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006482 if (shl_flag == FALSE)
6483 {
6484 shl = &search_hl;
6485 shl_flag = TRUE;
6486 }
6487 else
6488 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 if (shl->rm.regprog != NULL
6490 && shl->lnum == 0
6491 && re_multiline(shl->rm.regprog))
6492 {
6493 if (shl->first_lnum == 0)
6494 {
6495# ifdef FEAT_FOLDING
6496 for (shl->first_lnum = lnum;
6497 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6498 if (hasFoldingWin(wp, shl->first_lnum - 1,
6499 NULL, NULL, TRUE, NULL))
6500 break;
6501# else
6502 shl->first_lnum = wp->w_topline;
6503# endif
6504 }
6505 n = 0;
6506 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6507 {
6508 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6509 if (shl->lnum != 0)
6510 {
6511 shl->first_lnum = shl->lnum
6512 + shl->rm.endpos[0].lnum
6513 - shl->rm.startpos[0].lnum;
6514 n = shl->rm.endpos[0].col;
6515 }
6516 else
6517 {
6518 ++shl->first_lnum;
6519 n = 0;
6520 }
6521 }
6522 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006523 if (shl != &search_hl && cur != NULL)
6524 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
6526}
6527
6528/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006529 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 * Uses shl->buf.
6531 * Sets shl->lnum and shl->rm contents.
6532 * Note: Assumes a previous match is always before "lnum", unless
6533 * shl->lnum is zero.
6534 * Careful: Any pointers for buffer lines will become invalid.
6535 */
6536 static void
6537next_search_hl(win, shl, lnum, mincol)
6538 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006539 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 linenr_T lnum;
6541 colnr_T mincol; /* minimal column for a match */
6542{
6543 linenr_T l;
6544 colnr_T matchcol;
6545 long nmatched;
6546
6547 if (shl->lnum != 0)
6548 {
6549 /* Check for three situations:
6550 * 1. If the "lnum" is below a previous match, start a new search.
6551 * 2. If the previous match includes "mincol", use it.
6552 * 3. Continue after the previous match.
6553 */
6554 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6555 if (lnum > l)
6556 shl->lnum = 0;
6557 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6558 return;
6559 }
6560
6561 /*
6562 * Repeat searching for a match until one is found that includes "mincol"
6563 * or none is found in this line.
6564 */
6565 called_emsg = FALSE;
6566 for (;;)
6567 {
6568 /* Three situations:
6569 * 1. No useful previous match: search from start of line.
6570 * 2. Not Vi compatible or empty match: continue at next character.
6571 * Break the loop if this is beyond the end of the line.
6572 * 3. Vi compatible searching: continue at end of previous match.
6573 */
6574 if (shl->lnum == 0)
6575 matchcol = 0;
6576 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6577 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006578 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006580 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006581
6582 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006583 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006584 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006586 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 shl->lnum = 0;
6588 break;
6589 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006590#ifdef FEAT_MBYTE
6591 if (has_mbyte)
6592 matchcol += mb_ptr2len(ml);
6593 else
6594#endif
6595 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
6597 else
6598 matchcol = shl->rm.endpos[0].col;
6599
6600 shl->lnum = lnum;
6601 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6602 if (called_emsg)
6603 {
6604 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006605 if (shl == &search_hl)
6606 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006607 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006608 vim_free(shl->rm.regprog);
6609 no_hlsearch = TRUE;
6610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006612 shl->lnum = 0;
6613 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 break;
6615 }
6616 if (nmatched == 0)
6617 {
6618 shl->lnum = 0; /* no match found */
6619 break;
6620 }
6621 if (shl->rm.startpos[0].lnum > 0
6622 || shl->rm.startpos[0].col >= mincol
6623 || nmatched > 1
6624 || shl->rm.endpos[0].col > mincol)
6625 {
6626 shl->lnum += shl->rm.startpos[0].lnum;
6627 break; /* useful match found */
6628 }
6629 }
6630}
6631#endif
6632
6633 static void
6634screen_start_highlight(attr)
6635 int attr;
6636{
6637 attrentry_T *aep = NULL;
6638
6639 screen_attr = attr;
6640 if (full_screen
6641#ifdef WIN3264
6642 && termcap_active
6643#endif
6644 )
6645 {
6646#ifdef FEAT_GUI
6647 if (gui.in_use)
6648 {
6649 char buf[20];
6650
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006651 /* The GUI handles this internally. */
6652 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 OUT_STR(buf);
6654 }
6655 else
6656#endif
6657 {
6658 if (attr > HL_ALL) /* special HL attr. */
6659 {
6660 if (t_colors > 1)
6661 aep = syn_cterm_attr2entry(attr);
6662 else
6663 aep = syn_term_attr2entry(attr);
6664 if (aep == NULL) /* did ":syntax clear" */
6665 attr = 0;
6666 else
6667 attr = aep->ae_attr;
6668 }
6669 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6670 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006671 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6672 && cterm_normal_fg_bold)
6673 /* If the Normal FG color has BOLD attribute and the new HL
6674 * has a FG color defined, clear BOLD. */
6675 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6677 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006678 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6679 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 out_str(T_US);
6681 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6682 out_str(T_CZH);
6683 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6684 out_str(T_MR);
6685
6686 /*
6687 * Output the color or start string after bold etc., in case the
6688 * bold etc. override the color setting.
6689 */
6690 if (aep != NULL)
6691 {
6692 if (t_colors > 1)
6693 {
6694 if (aep->ae_u.cterm.fg_color)
6695 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6696 if (aep->ae_u.cterm.bg_color)
6697 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6698 }
6699 else
6700 {
6701 if (aep->ae_u.term.start != NULL)
6702 out_str(aep->ae_u.term.start);
6703 }
6704 }
6705 }
6706 }
6707}
6708
6709 void
6710screen_stop_highlight()
6711{
6712 int do_ME = FALSE; /* output T_ME code */
6713
6714 if (screen_attr != 0
6715#ifdef WIN3264
6716 && termcap_active
6717#endif
6718 )
6719 {
6720#ifdef FEAT_GUI
6721 if (gui.in_use)
6722 {
6723 char buf[20];
6724
6725 /* use internal GUI code */
6726 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6727 OUT_STR(buf);
6728 }
6729 else
6730#endif
6731 {
6732 if (screen_attr > HL_ALL) /* special HL attr. */
6733 {
6734 attrentry_T *aep;
6735
6736 if (t_colors > 1)
6737 {
6738 /*
6739 * Assume that t_me restores the original colors!
6740 */
6741 aep = syn_cterm_attr2entry(screen_attr);
6742 if (aep != NULL && (aep->ae_u.cterm.fg_color
6743 || aep->ae_u.cterm.bg_color))
6744 do_ME = TRUE;
6745 }
6746 else
6747 {
6748 aep = syn_term_attr2entry(screen_attr);
6749 if (aep != NULL && aep->ae_u.term.stop != NULL)
6750 {
6751 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6752 do_ME = TRUE;
6753 else
6754 out_str(aep->ae_u.term.stop);
6755 }
6756 }
6757 if (aep == NULL) /* did ":syntax clear" */
6758 screen_attr = 0;
6759 else
6760 screen_attr = aep->ae_attr;
6761 }
6762
6763 /*
6764 * Often all ending-codes are equal to T_ME. Avoid outputting the
6765 * same sequence several times.
6766 */
6767 if (screen_attr & HL_STANDOUT)
6768 {
6769 if (STRCMP(T_SE, T_ME) == 0)
6770 do_ME = TRUE;
6771 else
6772 out_str(T_SE);
6773 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006774 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 {
6776 if (STRCMP(T_UE, T_ME) == 0)
6777 do_ME = TRUE;
6778 else
6779 out_str(T_UE);
6780 }
6781 if (screen_attr & HL_ITALIC)
6782 {
6783 if (STRCMP(T_CZR, T_ME) == 0)
6784 do_ME = TRUE;
6785 else
6786 out_str(T_CZR);
6787 }
6788 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6789 out_str(T_ME);
6790
6791 if (t_colors > 1)
6792 {
6793 /* set Normal cterm colors */
6794 if (cterm_normal_fg_color != 0)
6795 term_fg_color(cterm_normal_fg_color - 1);
6796 if (cterm_normal_bg_color != 0)
6797 term_bg_color(cterm_normal_bg_color - 1);
6798 if (cterm_normal_fg_bold)
6799 out_str(T_MD);
6800 }
6801 }
6802 }
6803 screen_attr = 0;
6804}
6805
6806/*
6807 * Reset the colors for a cterm. Used when leaving Vim.
6808 * The machine specific code may override this again.
6809 */
6810 void
6811reset_cterm_colors()
6812{
6813 if (t_colors > 1)
6814 {
6815 /* set Normal cterm colors */
6816 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6817 {
6818 out_str(T_OP);
6819 screen_attr = -1;
6820 }
6821 if (cterm_normal_fg_bold)
6822 {
6823 out_str(T_ME);
6824 screen_attr = -1;
6825 }
6826 }
6827}
6828
6829/*
6830 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6831 * using the attributes from ScreenAttrs["off"].
6832 */
6833 static void
6834screen_char(off, row, col)
6835 unsigned off;
6836 int row;
6837 int col;
6838{
6839 int attr;
6840
6841 /* Check for illegal values, just in case (could happen just after
6842 * resizing). */
6843 if (row >= screen_Rows || col >= screen_Columns)
6844 return;
6845
6846 /* Outputting the last character on the screen may scrollup the screen.
6847 * Don't to it! Mark the character invalid (update it when scrolled up) */
6848 if (row == screen_Rows - 1 && col == screen_Columns - 1
6849#ifdef FEAT_RIGHTLEFT
6850 /* account for first command-line character in rightleft mode */
6851 && !cmdmsg_rl
6852#endif
6853 )
6854 {
6855 ScreenAttrs[off] = (sattr_T)-1;
6856 return;
6857 }
6858
6859 /*
6860 * Stop highlighting first, so it's easier to move the cursor.
6861 */
6862#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6863 if (screen_char_attr != 0)
6864 attr = screen_char_attr;
6865 else
6866#endif
6867 attr = ScreenAttrs[off];
6868 if (screen_attr != attr)
6869 screen_stop_highlight();
6870
6871 windgoto(row, col);
6872
6873 if (screen_attr != attr)
6874 screen_start_highlight(attr);
6875
6876#ifdef FEAT_MBYTE
6877 if (enc_utf8 && ScreenLinesUC[off] != 0)
6878 {
6879 char_u buf[MB_MAXBYTES + 1];
6880
6881 /* Convert UTF-8 character to bytes and write it. */
6882
6883 buf[utfc_char2bytes(off, buf)] = NUL;
6884
6885 out_str(buf);
6886 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6887 ++screen_cur_col;
6888 }
6889 else
6890#endif
6891 {
6892#ifdef FEAT_MBYTE
6893 out_flush_check();
6894#endif
6895 out_char(ScreenLines[off]);
6896#ifdef FEAT_MBYTE
6897 /* double-byte character in single-width cell */
6898 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6899 out_char(ScreenLines2[off]);
6900#endif
6901 }
6902
6903 screen_cur_col++;
6904}
6905
6906#ifdef FEAT_MBYTE
6907
6908/*
6909 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6910 * on the screen at position 'row' and 'col'.
6911 * The attributes of the first byte is used for all. This is required to
6912 * output the two bytes of a double-byte character with nothing in between.
6913 */
6914 static void
6915screen_char_2(off, row, col)
6916 unsigned off;
6917 int row;
6918 int col;
6919{
6920 /* Check for illegal values (could be wrong when screen was resized). */
6921 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6922 return;
6923
6924 /* Outputting the last character on the screen may scrollup the screen.
6925 * Don't to it! Mark the character invalid (update it when scrolled up) */
6926 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6927 {
6928 ScreenAttrs[off] = (sattr_T)-1;
6929 return;
6930 }
6931
6932 /* Output the first byte normally (positions the cursor), then write the
6933 * second byte directly. */
6934 screen_char(off, row, col);
6935 out_char(ScreenLines[off + 1]);
6936 ++screen_cur_col;
6937}
6938#endif
6939
6940#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6941/*
6942 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6943 * This uses the contents of ScreenLines[] and doesn't change it.
6944 */
6945 void
6946screen_draw_rectangle(row, col, height, width, invert)
6947 int row;
6948 int col;
6949 int height;
6950 int width;
6951 int invert;
6952{
6953 int r, c;
6954 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006955#ifdef FEAT_MBYTE
6956 int max_off;
6957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006959 /* Can't use ScreenLines unless initialized */
6960 if (ScreenLines == NULL)
6961 return;
6962
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (invert)
6964 screen_char_attr = HL_INVERSE;
6965 for (r = row; r < row + height; ++r)
6966 {
6967 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006968#ifdef FEAT_MBYTE
6969 max_off = off + screen_Columns;
6970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 for (c = col; c < col + width; ++c)
6972 {
6973#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006974 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {
6976 screen_char_2(off + c, r, c);
6977 ++c;
6978 }
6979 else
6980#endif
6981 {
6982 screen_char(off + c, r, c);
6983#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006984 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 ++c;
6986#endif
6987 }
6988 }
6989 }
6990 screen_char_attr = 0;
6991}
6992#endif
6993
6994#ifdef FEAT_VERTSPLIT
6995/*
6996 * Redraw the characters for a vertically split window.
6997 */
6998 static void
6999redraw_block(row, end, wp)
7000 int row;
7001 int end;
7002 win_T *wp;
7003{
7004 int col;
7005 int width;
7006
7007# ifdef FEAT_CLIPBOARD
7008 clip_may_clear_selection(row, end - 1);
7009# endif
7010
7011 if (wp == NULL)
7012 {
7013 col = 0;
7014 width = Columns;
7015 }
7016 else
7017 {
7018 col = wp->w_wincol;
7019 width = wp->w_width;
7020 }
7021 screen_draw_rectangle(row, col, end - row, width, FALSE);
7022}
7023#endif
7024
7025/*
7026 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7027 * with character 'c1' in first column followed by 'c2' in the other columns.
7028 * Use attributes 'attr'.
7029 */
7030 void
7031screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7032 int start_row, end_row;
7033 int start_col, end_col;
7034 int c1, c2;
7035 int attr;
7036{
7037 int row;
7038 int col;
7039 int off;
7040 int end_off;
7041 int did_delete;
7042 int c;
7043 int norm_term;
7044#if defined(FEAT_GUI) || defined(UNIX)
7045 int force_next = FALSE;
7046#endif
7047
7048 if (end_row > screen_Rows) /* safety check */
7049 end_row = screen_Rows;
7050 if (end_col > screen_Columns) /* safety check */
7051 end_col = screen_Columns;
7052 if (ScreenLines == NULL
7053 || start_row >= end_row
7054 || start_col >= end_col) /* nothing to do */
7055 return;
7056
7057 /* it's a "normal" terminal when not in a GUI or cterm */
7058 norm_term = (
7059#ifdef FEAT_GUI
7060 !gui.in_use &&
7061#endif
7062 t_colors <= 1);
7063 for (row = start_row; row < end_row; ++row)
7064 {
7065 /*
7066 * Try to use delete-line termcap code, when no attributes or in a
7067 * "normal" terminal, where a bold/italic space is just a
7068 * space.
7069 */
7070 did_delete = FALSE;
7071 if (c2 == ' '
7072 && end_col == Columns
7073 && can_clear(T_CE)
7074 && (attr == 0
7075 || (norm_term
7076 && attr <= HL_ALL
7077 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7078 {
7079 /*
7080 * check if we really need to clear something
7081 */
7082 col = start_col;
7083 if (c1 != ' ') /* don't clear first char */
7084 ++col;
7085
7086 off = LineOffset[row] + col;
7087 end_off = LineOffset[row] + end_col;
7088
7089 /* skip blanks (used often, keep it fast!) */
7090#ifdef FEAT_MBYTE
7091 if (enc_utf8)
7092 while (off < end_off && ScreenLines[off] == ' '
7093 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7094 ++off;
7095 else
7096#endif
7097 while (off < end_off && ScreenLines[off] == ' '
7098 && ScreenAttrs[off] == 0)
7099 ++off;
7100 if (off < end_off) /* something to be cleared */
7101 {
7102 col = off - LineOffset[row];
7103 screen_stop_highlight();
7104 term_windgoto(row, col);/* clear rest of this screen line */
7105 out_str(T_CE);
7106 screen_start(); /* don't know where cursor is now */
7107 col = end_col - col;
7108 while (col--) /* clear chars in ScreenLines */
7109 {
7110 ScreenLines[off] = ' ';
7111#ifdef FEAT_MBYTE
7112 if (enc_utf8)
7113 ScreenLinesUC[off] = 0;
7114#endif
7115 ScreenAttrs[off] = 0;
7116 ++off;
7117 }
7118 }
7119 did_delete = TRUE; /* the chars are cleared now */
7120 }
7121
7122 off = LineOffset[row] + start_col;
7123 c = c1;
7124 for (col = start_col; col < end_col; ++col)
7125 {
7126 if (ScreenLines[off] != c
7127#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007128 || (enc_utf8 && (int)ScreenLinesUC[off]
7129 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130#endif
7131 || ScreenAttrs[off] != attr
7132#if defined(FEAT_GUI) || defined(UNIX)
7133 || force_next
7134#endif
7135 )
7136 {
7137#if defined(FEAT_GUI) || defined(UNIX)
7138 /* The bold trick may make a single row of pixels appear in
7139 * the next character. When a bold character is removed, the
7140 * next character should be redrawn too. This happens for our
7141 * own GUI and for some xterms. */
7142 if (
7143# ifdef FEAT_GUI
7144 gui.in_use
7145# endif
7146# if defined(FEAT_GUI) && defined(UNIX)
7147 ||
7148# endif
7149# ifdef UNIX
7150 term_is_xterm
7151# endif
7152 )
7153 {
7154 if (ScreenLines[off] != ' '
7155 && (ScreenAttrs[off] > HL_ALL
7156 || ScreenAttrs[off] & HL_BOLD))
7157 force_next = TRUE;
7158 else
7159 force_next = FALSE;
7160 }
7161#endif
7162 ScreenLines[off] = c;
7163#ifdef FEAT_MBYTE
7164 if (enc_utf8)
7165 {
7166 if (c >= 0x80)
7167 {
7168 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007169 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 }
7171 else
7172 ScreenLinesUC[off] = 0;
7173 }
7174#endif
7175 ScreenAttrs[off] = attr;
7176 if (!did_delete || c != ' ')
7177 screen_char(off, row, col);
7178 }
7179 ++off;
7180 if (col == start_col)
7181 {
7182 if (did_delete)
7183 break;
7184 c = c2;
7185 }
7186 }
7187 if (end_col == Columns)
7188 LineWraps[row] = FALSE;
7189 if (row == Rows - 1) /* overwritten the command line */
7190 {
7191 redraw_cmdline = TRUE;
7192 if (c1 == ' ' && c2 == ' ')
7193 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007194 if (start_col == 0)
7195 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
7197 }
7198}
7199
7200/*
7201 * Check if there should be a delay. Used before clearing or redrawing the
7202 * screen or the command line.
7203 */
7204 void
7205check_for_delay(check_msg_scroll)
7206 int check_msg_scroll;
7207{
7208 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7209 && !did_wait_return
7210 && emsg_silent == 0)
7211 {
7212 out_flush();
7213 ui_delay(1000L, TRUE);
7214 emsg_on_display = FALSE;
7215 if (check_msg_scroll)
7216 msg_scroll = FALSE;
7217 }
7218}
7219
7220/*
7221 * screen_valid - allocate screen buffers if size changed
7222 * If "clear" is TRUE: clear screen if it has been resized.
7223 * Returns TRUE if there is a valid screen to write to.
7224 * Returns FALSE when starting up and screen not initialized yet.
7225 */
7226 int
7227screen_valid(clear)
7228 int clear;
7229{
7230 screenalloc(clear); /* allocate screen buffers if size changed */
7231 return (ScreenLines != NULL);
7232}
7233
7234/*
7235 * Resize the shell to Rows and Columns.
7236 * Allocate ScreenLines[] and associated items.
7237 *
7238 * There may be some time between setting Rows and Columns and (re)allocating
7239 * ScreenLines[]. This happens when starting up and when (manually) changing
7240 * the shell size. Always use screen_Rows and screen_Columns to access items
7241 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7242 * final size of the shell is needed.
7243 */
7244 void
7245screenalloc(clear)
7246 int clear;
7247{
7248 int new_row, old_row;
7249#ifdef FEAT_GUI
7250 int old_Rows;
7251#endif
7252 win_T *wp;
7253 int outofmem = FALSE;
7254 int len;
7255 schar_T *new_ScreenLines;
7256#ifdef FEAT_MBYTE
7257 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007258 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007260 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261#endif
7262 sattr_T *new_ScreenAttrs;
7263 unsigned *new_LineOffset;
7264 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007265#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007266 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007267 tabpage_T *tp;
7268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007270 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
7272 /*
7273 * Allocation of the screen buffers is done only when the size changes and
7274 * when Rows and Columns have been set and we have started doing full
7275 * screen stuff.
7276 */
7277 if ((ScreenLines != NULL
7278 && Rows == screen_Rows
7279 && Columns == screen_Columns
7280#ifdef FEAT_MBYTE
7281 && enc_utf8 == (ScreenLinesUC != NULL)
7282 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007283 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284#endif
7285 )
7286 || Rows == 0
7287 || Columns == 0
7288 || (!full_screen && ScreenLines == NULL))
7289 return;
7290
7291 /*
7292 * It's possible that we produce an out-of-memory message below, which
7293 * will cause this function to be called again. To break the loop, just
7294 * return here.
7295 */
7296 if (entered)
7297 return;
7298 entered = TRUE;
7299
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007300 /*
7301 * Note that the window sizes are updated before reallocating the arrays,
7302 * thus we must not redraw here!
7303 */
7304 ++RedrawingDisabled;
7305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 win_new_shellsize(); /* fit the windows in the new sized shell */
7307
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 comp_col(); /* recompute columns for shown command and ruler */
7309
7310 /*
7311 * We're changing the size of the screen.
7312 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7313 * - Move lines from the old arrays into the new arrays, clear extra
7314 * lines (unless the screen is going to be cleared).
7315 * - Free the old arrays.
7316 *
7317 * If anything fails, make ScreenLines NULL, so we don't do anything!
7318 * Continuing with the old ScreenLines may result in a crash, because the
7319 * size is wrong.
7320 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007321 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
7324 new_ScreenLines = (schar_T *)lalloc((long_u)(
7325 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7326#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007327 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 if (enc_utf8)
7329 {
7330 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7331 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007332 for (i = 0; i < p_mco; ++i)
7333 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7335 }
7336 if (enc_dbcs == DBCS_JPNU)
7337 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7338 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7339#endif
7340 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7341 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7342 new_LineOffset = (unsigned *)lalloc((long_u)(
7343 Rows * sizeof(unsigned)), FALSE);
7344 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007345#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007346 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007349 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 {
7351 if (win_alloc_lines(wp) == FAIL)
7352 {
7353 outofmem = TRUE;
7354#ifdef FEAT_WINDOWS
7355 break;
7356#endif
7357 }
7358 }
7359
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007360#ifdef FEAT_MBYTE
7361 for (i = 0; i < p_mco; ++i)
7362 if (new_ScreenLinesC[i] == NULL)
7363 break;
7364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (new_ScreenLines == NULL
7366#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007367 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7369#endif
7370 || new_ScreenAttrs == NULL
7371 || new_LineOffset == NULL
7372 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007373#ifdef FEAT_WINDOWS
7374 || new_TabPageIdxs == NULL
7375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 || outofmem)
7377 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007378 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007379 {
7380 /* guess the size */
7381 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7382
7383 /* Remember we did this to avoid getting outofmem messages over
7384 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007385 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 vim_free(new_ScreenLines);
7388 new_ScreenLines = NULL;
7389#ifdef FEAT_MBYTE
7390 vim_free(new_ScreenLinesUC);
7391 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007392 for (i = 0; i < p_mco; ++i)
7393 {
7394 vim_free(new_ScreenLinesC[i]);
7395 new_ScreenLinesC[i] = NULL;
7396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 vim_free(new_ScreenLines2);
7398 new_ScreenLines2 = NULL;
7399#endif
7400 vim_free(new_ScreenAttrs);
7401 new_ScreenAttrs = NULL;
7402 vim_free(new_LineOffset);
7403 new_LineOffset = NULL;
7404 vim_free(new_LineWraps);
7405 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007406#ifdef FEAT_WINDOWS
7407 vim_free(new_TabPageIdxs);
7408 new_TabPageIdxs = NULL;
7409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 }
7411 else
7412 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007413 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 for (new_row = 0; new_row < Rows; ++new_row)
7416 {
7417 new_LineOffset[new_row] = new_row * Columns;
7418 new_LineWraps[new_row] = FALSE;
7419
7420 /*
7421 * If the screen is not going to be cleared, copy as much as
7422 * possible from the old screen to the new one and clear the rest
7423 * (used when resizing the window at the "--more--" prompt or when
7424 * executing an external command, for the GUI).
7425 */
7426 if (!clear)
7427 {
7428 (void)vim_memset(new_ScreenLines + new_row * Columns,
7429 ' ', (size_t)Columns * sizeof(schar_T));
7430#ifdef FEAT_MBYTE
7431 if (enc_utf8)
7432 {
7433 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7434 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007435 for (i = 0; i < p_mco; ++i)
7436 (void)vim_memset(new_ScreenLinesC[i]
7437 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 0, (size_t)Columns * sizeof(u8char_T));
7439 }
7440 if (enc_dbcs == DBCS_JPNU)
7441 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7442 0, (size_t)Columns * sizeof(schar_T));
7443#endif
7444 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7445 0, (size_t)Columns * sizeof(sattr_T));
7446 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007447 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
7449 if (screen_Columns < Columns)
7450 len = screen_Columns;
7451 else
7452 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007453#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007454 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007455 * may be invalid now. Also when p_mco changes. */
7456 if (!(enc_utf8 && ScreenLinesUC == NULL)
7457 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007458#endif
7459 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7460 ScreenLines + LineOffset[old_row],
7461 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463 if (enc_utf8 && ScreenLinesUC != NULL
7464 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
7466 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7467 ScreenLinesUC + LineOffset[old_row],
7468 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007469 for (i = 0; i < p_mco; ++i)
7470 mch_memmove(new_ScreenLinesC[i]
7471 + new_LineOffset[new_row],
7472 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 (size_t)len * sizeof(u8char_T));
7474 }
7475 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7476 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7477 ScreenLines2 + LineOffset[old_row],
7478 (size_t)len * sizeof(schar_T));
7479#endif
7480 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7481 ScreenAttrs + LineOffset[old_row],
7482 (size_t)len * sizeof(sattr_T));
7483 }
7484 }
7485 }
7486 /* Use the last line of the screen for the current line. */
7487 current_ScreenLine = new_ScreenLines + Rows * Columns;
7488 }
7489
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007490 free_screenlines();
7491
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 ScreenLines = new_ScreenLines;
7493#ifdef FEAT_MBYTE
7494 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007495 for (i = 0; i < p_mco; ++i)
7496 ScreenLinesC[i] = new_ScreenLinesC[i];
7497 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 ScreenLines2 = new_ScreenLines2;
7499#endif
7500 ScreenAttrs = new_ScreenAttrs;
7501 LineOffset = new_LineOffset;
7502 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007503#ifdef FEAT_WINDOWS
7504 TabPageIdxs = new_TabPageIdxs;
7505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
7507 /* It's important that screen_Rows and screen_Columns reflect the actual
7508 * size of ScreenLines[]. Set them before calling anything. */
7509#ifdef FEAT_GUI
7510 old_Rows = screen_Rows;
7511#endif
7512 screen_Rows = Rows;
7513 screen_Columns = Columns;
7514
7515 must_redraw = CLEAR; /* need to clear the screen later */
7516 if (clear)
7517 screenclear2();
7518
7519#ifdef FEAT_GUI
7520 else if (gui.in_use
7521 && !gui.starting
7522 && ScreenLines != NULL
7523 && old_Rows != Rows)
7524 {
7525 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7526 /*
7527 * Adjust the position of the cursor, for when executing an external
7528 * command.
7529 */
7530 if (msg_row >= Rows) /* Rows got smaller */
7531 msg_row = Rows - 1; /* put cursor at last row */
7532 else if (Rows > old_Rows) /* Rows got bigger */
7533 msg_row += Rows - old_Rows; /* put cursor in same place */
7534 if (msg_col >= Columns) /* Columns got smaller */
7535 msg_col = Columns - 1; /* put cursor at last column */
7536 }
7537#endif
7538
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007540 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007541
7542#ifdef FEAT_AUTOCMD
7543 if (starting == 0)
7544 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546}
7547
7548 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007549free_screenlines()
7550{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007551#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007552 int i;
7553
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007554 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007555 for (i = 0; i < Screen_mco; ++i)
7556 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007557 vim_free(ScreenLines2);
7558#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007559 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007560 vim_free(ScreenAttrs);
7561 vim_free(LineOffset);
7562 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007563#ifdef FEAT_WINDOWS
7564 vim_free(TabPageIdxs);
7565#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007566}
7567
7568 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569screenclear()
7570{
7571 check_for_delay(FALSE);
7572 screenalloc(FALSE); /* allocate screen buffers if size changed */
7573 screenclear2(); /* clear the screen */
7574}
7575
7576 static void
7577screenclear2()
7578{
7579 int i;
7580
7581 if (starting == NO_SCREEN || ScreenLines == NULL
7582#ifdef FEAT_GUI
7583 || (gui.in_use && gui.starting)
7584#endif
7585 )
7586 return;
7587
7588#ifdef FEAT_GUI
7589 if (!gui.in_use)
7590#endif
7591 screen_attr = -1; /* force setting the Normal colors */
7592 screen_stop_highlight(); /* don't want highlighting here */
7593
7594#ifdef FEAT_CLIPBOARD
7595 /* disable selection without redrawing it */
7596 clip_scroll_selection(9999);
7597#endif
7598
7599 /* blank out ScreenLines */
7600 for (i = 0; i < Rows; ++i)
7601 {
7602 lineclear(LineOffset[i], (int)Columns);
7603 LineWraps[i] = FALSE;
7604 }
7605
7606 if (can_clear(T_CL))
7607 {
7608 out_str(T_CL); /* clear the display */
7609 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007610 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
7612 else
7613 {
7614 /* can't clear the screen, mark all chars with invalid attributes */
7615 for (i = 0; i < Rows; ++i)
7616 lineinvalid(LineOffset[i], (int)Columns);
7617 clear_cmdline = TRUE;
7618 }
7619
7620 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7621
7622 win_rest_invalid(firstwin);
7623 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007624#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007625 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 if (must_redraw == CLEAR) /* no need to clear again */
7628 must_redraw = NOT_VALID;
7629 compute_cmdrow();
7630 msg_row = cmdline_row; /* put cursor on last line for messages */
7631 msg_col = 0;
7632 screen_start(); /* don't know where cursor is now */
7633 msg_scrolled = 0; /* can't scroll back */
7634 msg_didany = FALSE;
7635 msg_didout = FALSE;
7636}
7637
7638/*
7639 * Clear one line in ScreenLines.
7640 */
7641 static void
7642lineclear(off, width)
7643 unsigned off;
7644 int width;
7645{
7646 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7647#ifdef FEAT_MBYTE
7648 if (enc_utf8)
7649 (void)vim_memset(ScreenLinesUC + off, 0,
7650 (size_t)width * sizeof(u8char_T));
7651#endif
7652 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7653}
7654
7655/*
7656 * Mark one line in ScreenLines invalid by setting the attributes to an
7657 * invalid value.
7658 */
7659 static void
7660lineinvalid(off, width)
7661 unsigned off;
7662 int width;
7663{
7664 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7665}
7666
7667#ifdef FEAT_VERTSPLIT
7668/*
7669 * Copy part of a Screenline for vertically split window "wp".
7670 */
7671 static void
7672linecopy(to, from, wp)
7673 int to;
7674 int from;
7675 win_T *wp;
7676{
7677 unsigned off_to = LineOffset[to] + wp->w_wincol;
7678 unsigned off_from = LineOffset[from] + wp->w_wincol;
7679
7680 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7681 wp->w_width * sizeof(schar_T));
7682# ifdef FEAT_MBYTE
7683 if (enc_utf8)
7684 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007685 int i;
7686
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7688 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007689 for (i = 0; i < p_mco; ++i)
7690 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7691 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693 if (enc_dbcs == DBCS_JPNU)
7694 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7695 wp->w_width * sizeof(schar_T));
7696# endif
7697 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7698 wp->w_width * sizeof(sattr_T));
7699}
7700#endif
7701
7702/*
7703 * Return TRUE if clearing with term string "p" would work.
7704 * It can't work when the string is empty or it won't set the right background.
7705 */
7706 int
7707can_clear(p)
7708 char_u *p;
7709{
7710 return (*p != NUL && (t_colors <= 1
7711#ifdef FEAT_GUI
7712 || gui.in_use
7713#endif
7714 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7715}
7716
7717/*
7718 * Reset cursor position. Use whenever cursor was moved because of outputting
7719 * something directly to the screen (shell commands) or a terminal control
7720 * code.
7721 */
7722 void
7723screen_start()
7724{
7725 screen_cur_row = screen_cur_col = 9999;
7726}
7727
7728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 * Move the cursor to position "row","col" in the screen.
7730 * This tries to find the most efficient way to move, minimizing the number of
7731 * characters sent to the terminal.
7732 */
7733 void
7734windgoto(row, col)
7735 int row;
7736 int col;
7737{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007738 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 int i;
7740 int plan;
7741 int cost;
7742 int wouldbe_col;
7743 int noinvcurs;
7744 char_u *bs;
7745 int goto_cost;
7746 int attr;
7747
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007748#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7750
7751#define PLAN_LE 1
7752#define PLAN_CR 2
7753#define PLAN_NL 3
7754#define PLAN_WRITE 4
7755 /* Can't use ScreenLines unless initialized */
7756 if (ScreenLines == NULL)
7757 return;
7758
7759 if (col != screen_cur_col || row != screen_cur_row)
7760 {
7761 /* Check for valid position. */
7762 if (row < 0) /* window without text lines? */
7763 row = 0;
7764 if (row >= screen_Rows)
7765 row = screen_Rows - 1;
7766 if (col >= screen_Columns)
7767 col = screen_Columns - 1;
7768
7769 /* check if no cursor movement is allowed in highlight mode */
7770 if (screen_attr && *T_MS == NUL)
7771 noinvcurs = HIGHL_COST;
7772 else
7773 noinvcurs = 0;
7774 goto_cost = GOTO_COST + noinvcurs;
7775
7776 /*
7777 * Plan how to do the positioning:
7778 * 1. Use CR to move it to column 0, same row.
7779 * 2. Use T_LE to move it a few columns to the left.
7780 * 3. Use NL to move a few lines down, column 0.
7781 * 4. Move a few columns to the right with T_ND or by writing chars.
7782 *
7783 * Don't do this if the cursor went beyond the last column, the cursor
7784 * position is unknown then (some terminals wrap, some don't )
7785 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007786 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 * characters to move the cursor to the right.
7788 */
7789 if (row >= screen_cur_row && screen_cur_col < Columns)
7790 {
7791 /*
7792 * If the cursor is in the same row, bigger col, we can use CR
7793 * or T_LE.
7794 */
7795 bs = NULL; /* init for GCC */
7796 attr = screen_attr;
7797 if (row == screen_cur_row && col < screen_cur_col)
7798 {
7799 /* "le" is preferred over "bc", because "bc" is obsolete */
7800 if (*T_LE)
7801 bs = T_LE; /* "cursor left" */
7802 else
7803 bs = T_BC; /* "backspace character (old) */
7804 if (*bs)
7805 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7806 else
7807 cost = 999;
7808 if (col + 1 < cost) /* using CR is less characters */
7809 {
7810 plan = PLAN_CR;
7811 wouldbe_col = 0;
7812 cost = 1; /* CR is just one character */
7813 }
7814 else
7815 {
7816 plan = PLAN_LE;
7817 wouldbe_col = col;
7818 }
7819 if (noinvcurs) /* will stop highlighting */
7820 {
7821 cost += noinvcurs;
7822 attr = 0;
7823 }
7824 }
7825
7826 /*
7827 * If the cursor is above where we want to be, we can use CR LF.
7828 */
7829 else if (row > screen_cur_row)
7830 {
7831 plan = PLAN_NL;
7832 wouldbe_col = 0;
7833 cost = (row - screen_cur_row) * 2; /* CR LF */
7834 if (noinvcurs) /* will stop highlighting */
7835 {
7836 cost += noinvcurs;
7837 attr = 0;
7838 }
7839 }
7840
7841 /*
7842 * If the cursor is in the same row, smaller col, just use write.
7843 */
7844 else
7845 {
7846 plan = PLAN_WRITE;
7847 wouldbe_col = screen_cur_col;
7848 cost = 0;
7849 }
7850
7851 /*
7852 * Check if any characters that need to be written have the
7853 * correct attributes. Also avoid UTF-8 characters.
7854 */
7855 i = col - wouldbe_col;
7856 if (i > 0)
7857 cost += i;
7858 if (cost < goto_cost && i > 0)
7859 {
7860 /*
7861 * Check if the attributes are correct without additionally
7862 * stopping highlighting.
7863 */
7864 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7865 while (i && *p++ == attr)
7866 --i;
7867 if (i != 0)
7868 {
7869 /*
7870 * Try if it works when highlighting is stopped here.
7871 */
7872 if (*--p == 0)
7873 {
7874 cost += noinvcurs;
7875 while (i && *p++ == 0)
7876 --i;
7877 }
7878 if (i != 0)
7879 cost = 999; /* different attributes, don't do it */
7880 }
7881#ifdef FEAT_MBYTE
7882 if (enc_utf8)
7883 {
7884 /* Don't use an UTF-8 char for positioning, it's slow. */
7885 for (i = wouldbe_col; i < col; ++i)
7886 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7887 {
7888 cost = 999;
7889 break;
7890 }
7891 }
7892#endif
7893 }
7894
7895 /*
7896 * We can do it without term_windgoto()!
7897 */
7898 if (cost < goto_cost)
7899 {
7900 if (plan == PLAN_LE)
7901 {
7902 if (noinvcurs)
7903 screen_stop_highlight();
7904 while (screen_cur_col > col)
7905 {
7906 out_str(bs);
7907 --screen_cur_col;
7908 }
7909 }
7910 else if (plan == PLAN_CR)
7911 {
7912 if (noinvcurs)
7913 screen_stop_highlight();
7914 out_char('\r');
7915 screen_cur_col = 0;
7916 }
7917 else if (plan == PLAN_NL)
7918 {
7919 if (noinvcurs)
7920 screen_stop_highlight();
7921 while (screen_cur_row < row)
7922 {
7923 out_char('\n');
7924 ++screen_cur_row;
7925 }
7926 screen_cur_col = 0;
7927 }
7928
7929 i = col - screen_cur_col;
7930 if (i > 0)
7931 {
7932 /*
7933 * Use cursor-right if it's one character only. Avoids
7934 * removing a line of pixels from the last bold char, when
7935 * using the bold trick in the GUI.
7936 */
7937 if (T_ND[0] != NUL && T_ND[1] == NUL)
7938 {
7939 while (i-- > 0)
7940 out_char(*T_ND);
7941 }
7942 else
7943 {
7944 int off;
7945
7946 off = LineOffset[row] + screen_cur_col;
7947 while (i-- > 0)
7948 {
7949 if (ScreenAttrs[off] != screen_attr)
7950 screen_stop_highlight();
7951#ifdef FEAT_MBYTE
7952 out_flush_check();
7953#endif
7954 out_char(ScreenLines[off]);
7955#ifdef FEAT_MBYTE
7956 if (enc_dbcs == DBCS_JPNU
7957 && ScreenLines[off] == 0x8e)
7958 out_char(ScreenLines2[off]);
7959#endif
7960 ++off;
7961 }
7962 }
7963 }
7964 }
7965 }
7966 else
7967 cost = 999;
7968
7969 if (cost >= goto_cost)
7970 {
7971 if (noinvcurs)
7972 screen_stop_highlight();
7973 if (row == screen_cur_row && (col > screen_cur_col) &&
7974 *T_CRI != NUL)
7975 term_cursor_right(col - screen_cur_col);
7976 else
7977 term_windgoto(row, col);
7978 }
7979 screen_cur_row = row;
7980 screen_cur_col = col;
7981 }
7982}
7983
7984/*
7985 * Set cursor to its position in the current window.
7986 */
7987 void
7988setcursor()
7989{
7990 if (redrawing())
7991 {
7992 validate_cursor();
7993 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7994 W_WINCOL(curwin) + (
7995#ifdef FEAT_RIGHTLEFT
7996 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7997# ifdef FEAT_MBYTE
7998 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7999# endif
8000 1)) :
8001#endif
8002 curwin->w_wcol));
8003 }
8004}
8005
8006
8007/*
8008 * insert 'line_count' lines at 'row' in window 'wp'
8009 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8010 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8011 * scrolling.
8012 * Returns FAIL if the lines are not inserted, OK for success.
8013 */
8014 int
8015win_ins_lines(wp, row, line_count, invalid, mayclear)
8016 win_T *wp;
8017 int row;
8018 int line_count;
8019 int invalid;
8020 int mayclear;
8021{
8022 int did_delete;
8023 int nextrow;
8024 int lastrow;
8025 int retval;
8026
8027 if (invalid)
8028 wp->w_lines_valid = 0;
8029
8030 if (wp->w_height < 5)
8031 return FAIL;
8032
8033 if (line_count > wp->w_height - row)
8034 line_count = wp->w_height - row;
8035
8036 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8037 if (retval != MAYBE)
8038 return retval;
8039
8040 /*
8041 * If there is a next window or a status line, we first try to delete the
8042 * lines at the bottom to avoid messing what is after the window.
8043 * If this fails and there are following windows, don't do anything to avoid
8044 * messing up those windows, better just redraw.
8045 */
8046 did_delete = FALSE;
8047#ifdef FEAT_WINDOWS
8048 if (wp->w_next != NULL || wp->w_status_height)
8049 {
8050 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8051 line_count, (int)Rows, FALSE, NULL) == OK)
8052 did_delete = TRUE;
8053 else if (wp->w_next)
8054 return FAIL;
8055 }
8056#endif
8057 /*
8058 * if no lines deleted, blank the lines that will end up below the window
8059 */
8060 if (!did_delete)
8061 {
8062#ifdef FEAT_WINDOWS
8063 wp->w_redr_status = TRUE;
8064#endif
8065 redraw_cmdline = TRUE;
8066 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8067 lastrow = nextrow + line_count;
8068 if (lastrow > Rows)
8069 lastrow = Rows;
8070 screen_fill(nextrow - line_count, lastrow - line_count,
8071 W_WINCOL(wp), (int)W_ENDCOL(wp),
8072 ' ', ' ', 0);
8073 }
8074
8075 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8076 == FAIL)
8077 {
8078 /* deletion will have messed up other windows */
8079 if (did_delete)
8080 {
8081#ifdef FEAT_WINDOWS
8082 wp->w_redr_status = TRUE;
8083#endif
8084 win_rest_invalid(W_NEXT(wp));
8085 }
8086 return FAIL;
8087 }
8088
8089 return OK;
8090}
8091
8092/*
8093 * delete "line_count" window lines at "row" in window "wp"
8094 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8095 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8096 * scrolling
8097 * Return OK for success, FAIL if the lines are not deleted.
8098 */
8099 int
8100win_del_lines(wp, row, line_count, invalid, mayclear)
8101 win_T *wp;
8102 int row;
8103 int line_count;
8104 int invalid;
8105 int mayclear;
8106{
8107 int retval;
8108
8109 if (invalid)
8110 wp->w_lines_valid = 0;
8111
8112 if (line_count > wp->w_height - row)
8113 line_count = wp->w_height - row;
8114
8115 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8116 if (retval != MAYBE)
8117 return retval;
8118
8119 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8120 (int)Rows, FALSE, NULL) == FAIL)
8121 return FAIL;
8122
8123#ifdef FEAT_WINDOWS
8124 /*
8125 * If there are windows or status lines below, try to put them at the
8126 * correct place. If we can't do that, they have to be redrawn.
8127 */
8128 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8129 {
8130 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8131 line_count, (int)Rows, NULL) == FAIL)
8132 {
8133 wp->w_redr_status = TRUE;
8134 win_rest_invalid(wp->w_next);
8135 }
8136 }
8137 /*
8138 * If this is the last window and there is no status line, redraw the
8139 * command line later.
8140 */
8141 else
8142#endif
8143 redraw_cmdline = TRUE;
8144 return OK;
8145}
8146
8147/*
8148 * Common code for win_ins_lines() and win_del_lines().
8149 * Returns OK or FAIL when the work has been done.
8150 * Returns MAYBE when not finished yet.
8151 */
8152 static int
8153win_do_lines(wp, row, line_count, mayclear, del)
8154 win_T *wp;
8155 int row;
8156 int line_count;
8157 int mayclear;
8158 int del;
8159{
8160 int retval;
8161
8162 if (!redrawing() || line_count <= 0)
8163 return FAIL;
8164
8165 /* only a few lines left: redraw is faster */
8166 if (mayclear && Rows - line_count < 5
8167#ifdef FEAT_VERTSPLIT
8168 && wp->w_width == Columns
8169#endif
8170 )
8171 {
8172 screenclear(); /* will set wp->w_lines_valid to 0 */
8173 return FAIL;
8174 }
8175
8176 /*
8177 * Delete all remaining lines
8178 */
8179 if (row + line_count >= wp->w_height)
8180 {
8181 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8182 W_WINCOL(wp), (int)W_ENDCOL(wp),
8183 ' ', ' ', 0);
8184 return OK;
8185 }
8186
8187 /*
8188 * when scrolling, the message on the command line should be cleared,
8189 * otherwise it will stay there forever.
8190 */
8191 clear_cmdline = TRUE;
8192
8193 /*
8194 * If the terminal can set a scroll region, use that.
8195 * Always do this in a vertically split window. This will redraw from
8196 * ScreenLines[] when t_CV isn't defined. That's faster than using
8197 * win_line().
8198 * Don't use a scroll region when we are going to redraw the text, writing
8199 * a character in the lower right corner of the scroll region causes a
8200 * scroll-up in the DJGPP version.
8201 */
8202 if (scroll_region
8203#ifdef FEAT_VERTSPLIT
8204 || W_WIDTH(wp) != Columns
8205#endif
8206 )
8207 {
8208#ifdef FEAT_VERTSPLIT
8209 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8210#endif
8211 scroll_region_set(wp, row);
8212 if (del)
8213 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8214 wp->w_height - row, FALSE, wp);
8215 else
8216 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8217 wp->w_height - row, wp);
8218#ifdef FEAT_VERTSPLIT
8219 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8220#endif
8221 scroll_region_reset();
8222 return retval;
8223 }
8224
8225#ifdef FEAT_WINDOWS
8226 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8227 return FAIL;
8228#endif
8229
8230 return MAYBE;
8231}
8232
8233/*
8234 * window 'wp' and everything after it is messed up, mark it for redraw
8235 */
8236 static void
8237win_rest_invalid(wp)
8238 win_T *wp;
8239{
8240#ifdef FEAT_WINDOWS
8241 while (wp != NULL)
8242#else
8243 if (wp != NULL)
8244#endif
8245 {
8246 redraw_win_later(wp, NOT_VALID);
8247#ifdef FEAT_WINDOWS
8248 wp->w_redr_status = TRUE;
8249 wp = wp->w_next;
8250#endif
8251 }
8252 redraw_cmdline = TRUE;
8253}
8254
8255/*
8256 * The rest of the routines in this file perform screen manipulations. The
8257 * given operation is performed physically on the screen. The corresponding
8258 * change is also made to the internal screen image. In this way, the editor
8259 * anticipates the effect of editing changes on the appearance of the screen.
8260 * That way, when we call screenupdate a complete redraw isn't usually
8261 * necessary. Another advantage is that we can keep adding code to anticipate
8262 * screen changes, and in the meantime, everything still works.
8263 */
8264
8265/*
8266 * types for inserting or deleting lines
8267 */
8268#define USE_T_CAL 1
8269#define USE_T_CDL 2
8270#define USE_T_AL 3
8271#define USE_T_CE 4
8272#define USE_T_DL 5
8273#define USE_T_SR 6
8274#define USE_NL 7
8275#define USE_T_CD 8
8276#define USE_REDRAW 9
8277
8278/*
8279 * insert lines on the screen and update ScreenLines[]
8280 * 'end' is the line after the scrolled part. Normally it is Rows.
8281 * When scrolling region used 'off' is the offset from the top for the region.
8282 * 'row' and 'end' are relative to the start of the region.
8283 *
8284 * return FAIL for failure, OK for success.
8285 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008286 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287screen_ins_lines(off, row, line_count, end, wp)
8288 int off;
8289 int row;
8290 int line_count;
8291 int end;
8292 win_T *wp; /* NULL or window to use width from */
8293{
8294 int i;
8295 int j;
8296 unsigned temp;
8297 int cursor_row;
8298 int type;
8299 int result_empty;
8300 int can_ce = can_clear(T_CE);
8301
8302 /*
8303 * FAIL if
8304 * - there is no valid screen
8305 * - the screen has to be redrawn completely
8306 * - the line count is less than one
8307 * - the line count is more than 'ttyscroll'
8308 */
8309 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8310 return FAIL;
8311
8312 /*
8313 * There are seven ways to insert lines:
8314 * 0. When in a vertically split window and t_CV isn't set, redraw the
8315 * characters from ScreenLines[].
8316 * 1. Use T_CD (clear to end of display) if it exists and the result of
8317 * the insert is just empty lines
8318 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8319 * present or line_count > 1. It looks better if we do all the inserts
8320 * at once.
8321 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8322 * insert is just empty lines and T_CE is not present or line_count >
8323 * 1.
8324 * 4. Use T_AL (insert line) if it exists.
8325 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8326 * just empty lines.
8327 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8328 * just empty lines.
8329 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8330 * the 'da' flag is not set or we have clear line capability.
8331 * 8. redraw the characters from ScreenLines[].
8332 *
8333 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8334 * the scrollbar for the window. It does have insert line, use that if it
8335 * exists.
8336 */
8337 result_empty = (row + line_count >= end);
8338#ifdef FEAT_VERTSPLIT
8339 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8340 type = USE_REDRAW;
8341 else
8342#endif
8343 if (can_clear(T_CD) && result_empty)
8344 type = USE_T_CD;
8345 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8346 type = USE_T_CAL;
8347 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8348 type = USE_T_CDL;
8349 else if (*T_AL != NUL)
8350 type = USE_T_AL;
8351 else if (can_ce && result_empty)
8352 type = USE_T_CE;
8353 else if (*T_DL != NUL && result_empty)
8354 type = USE_T_DL;
8355 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8356 type = USE_T_SR;
8357 else
8358 return FAIL;
8359
8360 /*
8361 * For clearing the lines screen_del_lines() is used. This will also take
8362 * care of t_db if necessary.
8363 */
8364 if (type == USE_T_CD || type == USE_T_CDL ||
8365 type == USE_T_CE || type == USE_T_DL)
8366 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8367
8368 /*
8369 * If text is retained below the screen, first clear or delete as many
8370 * lines at the bottom of the window as are about to be inserted so that
8371 * the deleted lines won't later surface during a screen_del_lines.
8372 */
8373 if (*T_DB)
8374 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8375
8376#ifdef FEAT_CLIPBOARD
8377 /* Remove a modeless selection when inserting lines halfway the screen
8378 * or not the full width of the screen. */
8379 if (off + row > 0
8380# ifdef FEAT_VERTSPLIT
8381 || (wp != NULL && wp->w_width != Columns)
8382# endif
8383 )
8384 clip_clear_selection();
8385 else
8386 clip_scroll_selection(-line_count);
8387#endif
8388
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389#ifdef FEAT_GUI
8390 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8391 * scrolling is actually carried out. */
8392 gui_dont_update_cursor();
8393#endif
8394
8395 if (*T_CCS != NUL) /* cursor relative to region */
8396 cursor_row = row;
8397 else
8398 cursor_row = row + off;
8399
8400 /*
8401 * Shift LineOffset[] line_count down to reflect the inserted lines.
8402 * Clear the inserted lines in ScreenLines[].
8403 */
8404 row += off;
8405 end += off;
8406 for (i = 0; i < line_count; ++i)
8407 {
8408#ifdef FEAT_VERTSPLIT
8409 if (wp != NULL && wp->w_width != Columns)
8410 {
8411 /* need to copy part of a line */
8412 j = end - 1 - i;
8413 while ((j -= line_count) >= row)
8414 linecopy(j + line_count, j, wp);
8415 j += line_count;
8416 if (can_clear((char_u *)" "))
8417 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8418 else
8419 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8420 LineWraps[j] = FALSE;
8421 }
8422 else
8423#endif
8424 {
8425 j = end - 1 - i;
8426 temp = LineOffset[j];
8427 while ((j -= line_count) >= row)
8428 {
8429 LineOffset[j + line_count] = LineOffset[j];
8430 LineWraps[j + line_count] = LineWraps[j];
8431 }
8432 LineOffset[j + line_count] = temp;
8433 LineWraps[j + line_count] = FALSE;
8434 if (can_clear((char_u *)" "))
8435 lineclear(temp, (int)Columns);
8436 else
8437 lineinvalid(temp, (int)Columns);
8438 }
8439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
8441 screen_stop_highlight();
8442 windgoto(cursor_row, 0);
8443
8444#ifdef FEAT_VERTSPLIT
8445 /* redraw the characters */
8446 if (type == USE_REDRAW)
8447 redraw_block(row, end, wp);
8448 else
8449#endif
8450 if (type == USE_T_CAL)
8451 {
8452 term_append_lines(line_count);
8453 screen_start(); /* don't know where cursor is now */
8454 }
8455 else
8456 {
8457 for (i = 0; i < line_count; i++)
8458 {
8459 if (type == USE_T_AL)
8460 {
8461 if (i && cursor_row != 0)
8462 windgoto(cursor_row, 0);
8463 out_str(T_AL);
8464 }
8465 else /* type == USE_T_SR */
8466 out_str(T_SR);
8467 screen_start(); /* don't know where cursor is now */
8468 }
8469 }
8470
8471 /*
8472 * With scroll-reverse and 'da' flag set we need to clear the lines that
8473 * have been scrolled down into the region.
8474 */
8475 if (type == USE_T_SR && *T_DA)
8476 {
8477 for (i = 0; i < line_count; ++i)
8478 {
8479 windgoto(off + i, 0);
8480 out_str(T_CE);
8481 screen_start(); /* don't know where cursor is now */
8482 }
8483 }
8484
8485#ifdef FEAT_GUI
8486 gui_can_update_cursor();
8487 if (gui.in_use)
8488 out_flush(); /* always flush after a scroll */
8489#endif
8490 return OK;
8491}
8492
8493/*
8494 * delete lines on the screen and update ScreenLines[]
8495 * 'end' is the line after the scrolled part. Normally it is Rows.
8496 * When scrolling region used 'off' is the offset from the top for the region.
8497 * 'row' and 'end' are relative to the start of the region.
8498 *
8499 * Return OK for success, FAIL if the lines are not deleted.
8500 */
8501/*ARGSUSED*/
8502 int
8503screen_del_lines(off, row, line_count, end, force, wp)
8504 int off;
8505 int row;
8506 int line_count;
8507 int end;
8508 int force; /* even when line_count > p_ttyscroll */
8509 win_T *wp; /* NULL or window to use width from */
8510{
8511 int j;
8512 int i;
8513 unsigned temp;
8514 int cursor_row;
8515 int cursor_end;
8516 int result_empty; /* result is empty until end of region */
8517 int can_delete; /* deleting line codes can be used */
8518 int type;
8519
8520 /*
8521 * FAIL if
8522 * - there is no valid screen
8523 * - the screen has to be redrawn completely
8524 * - the line count is less than one
8525 * - the line count is more than 'ttyscroll'
8526 */
8527 if (!screen_valid(TRUE) || line_count <= 0 ||
8528 (!force && line_count > p_ttyscroll))
8529 return FAIL;
8530
8531 /*
8532 * Check if the rest of the current region will become empty.
8533 */
8534 result_empty = row + line_count >= end;
8535
8536 /*
8537 * We can delete lines only when 'db' flag not set or when 'ce' option
8538 * available.
8539 */
8540 can_delete = (*T_DB == NUL || can_clear(T_CE));
8541
8542 /*
8543 * There are six ways to delete lines:
8544 * 0. When in a vertically split window and t_CV isn't set, redraw the
8545 * characters from ScreenLines[].
8546 * 1. Use T_CD if it exists and the result is empty.
8547 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8548 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8549 * none of the other ways work.
8550 * 4. Use T_CE (erase line) if the result is empty.
8551 * 5. Use T_DL (delete line) if it exists.
8552 * 6. redraw the characters from ScreenLines[].
8553 */
8554#ifdef FEAT_VERTSPLIT
8555 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8556 type = USE_REDRAW;
8557 else
8558#endif
8559 if (can_clear(T_CD) && result_empty)
8560 type = USE_T_CD;
8561#if defined(__BEOS__) && defined(BEOS_DR8)
8562 /*
8563 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8564 * its internal termcap... this works okay for tests which test *T_DB !=
8565 * NUL. It has the disadvantage that the user cannot use any :set t_*
8566 * command to get T_DB (back) to empty_option, only :set term=... will do
8567 * the trick...
8568 * Anyway, this hack will hopefully go away with the next OS release.
8569 * (Olaf Seibert)
8570 */
8571 else if (row == 0 && T_DB == empty_option
8572 && (line_count == 1 || *T_CDL == NUL))
8573#else
8574 else if (row == 0 && (
8575#ifndef AMIGA
8576 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8577 * up, so use delete-line command */
8578 line_count == 1 ||
8579#endif
8580 *T_CDL == NUL))
8581#endif
8582 type = USE_NL;
8583 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8584 type = USE_T_CDL;
8585 else if (can_clear(T_CE) && result_empty
8586#ifdef FEAT_VERTSPLIT
8587 && (wp == NULL || wp->w_width == Columns)
8588#endif
8589 )
8590 type = USE_T_CE;
8591 else if (*T_DL != NUL && can_delete)
8592 type = USE_T_DL;
8593 else if (*T_CDL != NUL && can_delete)
8594 type = USE_T_CDL;
8595 else
8596 return FAIL;
8597
8598#ifdef FEAT_CLIPBOARD
8599 /* Remove a modeless selection when deleting lines halfway the screen or
8600 * not the full width of the screen. */
8601 if (off + row > 0
8602# ifdef FEAT_VERTSPLIT
8603 || (wp != NULL && wp->w_width != Columns)
8604# endif
8605 )
8606 clip_clear_selection();
8607 else
8608 clip_scroll_selection(line_count);
8609#endif
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611#ifdef FEAT_GUI
8612 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8613 * scrolling is actually carried out. */
8614 gui_dont_update_cursor();
8615#endif
8616
8617 if (*T_CCS != NUL) /* cursor relative to region */
8618 {
8619 cursor_row = row;
8620 cursor_end = end;
8621 }
8622 else
8623 {
8624 cursor_row = row + off;
8625 cursor_end = end + off;
8626 }
8627
8628 /*
8629 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8630 * Clear the inserted lines in ScreenLines[].
8631 */
8632 row += off;
8633 end += off;
8634 for (i = 0; i < line_count; ++i)
8635 {
8636#ifdef FEAT_VERTSPLIT
8637 if (wp != NULL && wp->w_width != Columns)
8638 {
8639 /* need to copy part of a line */
8640 j = row + i;
8641 while ((j += line_count) <= end - 1)
8642 linecopy(j - line_count, j, wp);
8643 j -= line_count;
8644 if (can_clear((char_u *)" "))
8645 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8646 else
8647 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8648 LineWraps[j] = FALSE;
8649 }
8650 else
8651#endif
8652 {
8653 /* whole width, moving the line pointers is faster */
8654 j = row + i;
8655 temp = LineOffset[j];
8656 while ((j += line_count) <= end - 1)
8657 {
8658 LineOffset[j - line_count] = LineOffset[j];
8659 LineWraps[j - line_count] = LineWraps[j];
8660 }
8661 LineOffset[j - line_count] = temp;
8662 LineWraps[j - line_count] = FALSE;
8663 if (can_clear((char_u *)" "))
8664 lineclear(temp, (int)Columns);
8665 else
8666 lineinvalid(temp, (int)Columns);
8667 }
8668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
8670 screen_stop_highlight();
8671
8672#ifdef FEAT_VERTSPLIT
8673 /* redraw the characters */
8674 if (type == USE_REDRAW)
8675 redraw_block(row, end, wp);
8676 else
8677#endif
8678 if (type == USE_T_CD) /* delete the lines */
8679 {
8680 windgoto(cursor_row, 0);
8681 out_str(T_CD);
8682 screen_start(); /* don't know where cursor is now */
8683 }
8684 else if (type == USE_T_CDL)
8685 {
8686 windgoto(cursor_row, 0);
8687 term_delete_lines(line_count);
8688 screen_start(); /* don't know where cursor is now */
8689 }
8690 /*
8691 * Deleting lines at top of the screen or scroll region: Just scroll
8692 * the whole screen (scroll region) up by outputting newlines on the
8693 * last line.
8694 */
8695 else if (type == USE_NL)
8696 {
8697 windgoto(cursor_end - 1, 0);
8698 for (i = line_count; --i >= 0; )
8699 out_char('\n'); /* cursor will remain on same line */
8700 }
8701 else
8702 {
8703 for (i = line_count; --i >= 0; )
8704 {
8705 if (type == USE_T_DL)
8706 {
8707 windgoto(cursor_row, 0);
8708 out_str(T_DL); /* delete a line */
8709 }
8710 else /* type == USE_T_CE */
8711 {
8712 windgoto(cursor_row + i, 0);
8713 out_str(T_CE); /* erase a line */
8714 }
8715 screen_start(); /* don't know where cursor is now */
8716 }
8717 }
8718
8719 /*
8720 * If the 'db' flag is set, we need to clear the lines that have been
8721 * scrolled up at the bottom of the region.
8722 */
8723 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8724 {
8725 for (i = line_count; i > 0; --i)
8726 {
8727 windgoto(cursor_end - i, 0);
8728 out_str(T_CE); /* erase a line */
8729 screen_start(); /* don't know where cursor is now */
8730 }
8731 }
8732
8733#ifdef FEAT_GUI
8734 gui_can_update_cursor();
8735 if (gui.in_use)
8736 out_flush(); /* always flush after a scroll */
8737#endif
8738
8739 return OK;
8740}
8741
8742/*
8743 * show the current mode and ruler
8744 *
8745 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8746 * If clear_cmdline is FALSE there may be a message there that needs to be
8747 * cleared only if a mode is shown.
8748 * Return the length of the message (0 if no message).
8749 */
8750 int
8751showmode()
8752{
8753 int need_clear;
8754 int length = 0;
8755 int do_mode;
8756 int attr;
8757 int nwr_save;
8758#ifdef FEAT_INS_EXPAND
8759 int sub_attr;
8760#endif
8761
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008762 do_mode = ((p_smd && msg_silent == 0)
8763 && ((State & INSERT)
8764 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765#ifdef FEAT_VISUAL
8766 || VIsual_active
8767#endif
8768 ));
8769 if (do_mode || Recording)
8770 {
8771 /*
8772 * Don't show mode right now, when not redrawing or inside a mapping.
8773 * Call char_avail() only when we are going to show something, because
8774 * it takes a bit of time.
8775 */
8776 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8777 {
8778 redraw_cmdline = TRUE; /* show mode later */
8779 return 0;
8780 }
8781
8782 nwr_save = need_wait_return;
8783
8784 /* wait a bit before overwriting an important message */
8785 check_for_delay(FALSE);
8786
8787 /* if the cmdline is more than one line high, erase top lines */
8788 need_clear = clear_cmdline;
8789 if (clear_cmdline && cmdline_row < Rows - 1)
8790 msg_clr_cmdline(); /* will reset clear_cmdline */
8791
8792 /* Position on the last line in the window, column 0 */
8793 msg_pos_mode();
8794 cursor_off();
8795 attr = hl_attr(HLF_CM); /* Highlight mode */
8796 if (do_mode)
8797 {
8798 MSG_PUTS_ATTR("--", attr);
8799#if defined(FEAT_XIM)
8800 if (xic != NULL && im_get_status() && !p_imdisable
8801 && curbuf->b_p_iminsert == B_IMODE_IM)
8802# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8803 MSG_PUTS_ATTR(" IM", attr);
8804# else
8805 MSG_PUTS_ATTR(" XIM", attr);
8806# endif
8807#endif
8808#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8809 if (gui.in_use)
8810 {
8811 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008812 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 }
8814#endif
8815#ifdef FEAT_INS_EXPAND
8816 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8817 {
8818 /* These messages can get long, avoid a wrap in a narrow
8819 * window. Prefer showing edit_submode_extra. */
8820 length = (Rows - msg_row) * Columns - 3;
8821 if (edit_submode_extra != NULL)
8822 length -= vim_strsize(edit_submode_extra);
8823 if (length > 0)
8824 {
8825 if (edit_submode_pre != NULL)
8826 length -= vim_strsize(edit_submode_pre);
8827 if (length - vim_strsize(edit_submode) > 0)
8828 {
8829 if (edit_submode_pre != NULL)
8830 msg_puts_attr(edit_submode_pre, attr);
8831 msg_puts_attr(edit_submode, attr);
8832 }
8833 if (edit_submode_extra != NULL)
8834 {
8835 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8836 if ((int)edit_submode_highl < (int)HLF_COUNT)
8837 sub_attr = hl_attr(edit_submode_highl);
8838 else
8839 sub_attr = attr;
8840 msg_puts_attr(edit_submode_extra, sub_attr);
8841 }
8842 }
8843 length = 0;
8844 }
8845 else
8846#endif
8847 {
8848#ifdef FEAT_VREPLACE
8849 if (State & VREPLACE_FLAG)
8850 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8851 else
8852#endif
8853 if (State & REPLACE_FLAG)
8854 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8855 else if (State & INSERT)
8856 {
8857#ifdef FEAT_RIGHTLEFT
8858 if (p_ri)
8859 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8860#endif
8861 MSG_PUTS_ATTR(_(" INSERT"), attr);
8862 }
8863 else if (restart_edit == 'I')
8864 MSG_PUTS_ATTR(_(" (insert)"), attr);
8865 else if (restart_edit == 'R')
8866 MSG_PUTS_ATTR(_(" (replace)"), attr);
8867 else if (restart_edit == 'V')
8868 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8869#ifdef FEAT_RIGHTLEFT
8870 if (p_hkmap)
8871 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8872# ifdef FEAT_FKMAP
8873 if (p_fkmap)
8874 MSG_PUTS_ATTR(farsi_text_5, attr);
8875# endif
8876#endif
8877#ifdef FEAT_KEYMAP
8878 if (State & LANGMAP)
8879 {
8880# ifdef FEAT_ARABIC
8881 if (curwin->w_p_arab)
8882 MSG_PUTS_ATTR(_(" Arabic"), attr);
8883 else
8884# endif
8885 MSG_PUTS_ATTR(_(" (lang)"), attr);
8886 }
8887#endif
8888 if ((State & INSERT) && p_paste)
8889 MSG_PUTS_ATTR(_(" (paste)"), attr);
8890
8891#ifdef FEAT_VISUAL
8892 if (VIsual_active)
8893 {
8894 char *p;
8895
8896 /* Don't concatenate separate words to avoid translation
8897 * problems. */
8898 switch ((VIsual_select ? 4 : 0)
8899 + (VIsual_mode == Ctrl_V) * 2
8900 + (VIsual_mode == 'V'))
8901 {
8902 case 0: p = N_(" VISUAL"); break;
8903 case 1: p = N_(" VISUAL LINE"); break;
8904 case 2: p = N_(" VISUAL BLOCK"); break;
8905 case 4: p = N_(" SELECT"); break;
8906 case 5: p = N_(" SELECT LINE"); break;
8907 default: p = N_(" SELECT BLOCK"); break;
8908 }
8909 MSG_PUTS_ATTR(_(p), attr);
8910 }
8911#endif
8912 MSG_PUTS_ATTR(" --", attr);
8913 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008914
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 need_clear = TRUE;
8916 }
8917 if (Recording
8918#ifdef FEAT_INS_EXPAND
8919 && edit_submode == NULL /* otherwise it gets too long */
8920#endif
8921 )
8922 {
8923 MSG_PUTS_ATTR(_("recording"), attr);
8924 need_clear = TRUE;
8925 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008926
8927 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 if (need_clear || clear_cmdline)
8929 msg_clr_eos();
8930 msg_didout = FALSE; /* overwrite this message */
8931 length = msg_col;
8932 msg_col = 0;
8933 need_wait_return = nwr_save; /* never ask for hit-return for this */
8934 }
8935 else if (clear_cmdline && msg_silent == 0)
8936 /* Clear the whole command line. Will reset "clear_cmdline". */
8937 msg_clr_cmdline();
8938
8939#ifdef FEAT_CMDL_INFO
8940# ifdef FEAT_VISUAL
8941 /* In Visual mode the size of the selected area must be redrawn. */
8942 if (VIsual_active)
8943 clear_showcmd();
8944# endif
8945
8946 /* If the last window has no status line, the ruler is after the mode
8947 * message and must be redrawn */
8948 if (redrawing()
8949# ifdef FEAT_WINDOWS
8950 && lastwin->w_status_height == 0
8951# endif
8952 )
8953 win_redr_ruler(lastwin, TRUE);
8954#endif
8955 redraw_cmdline = FALSE;
8956 clear_cmdline = FALSE;
8957
8958 return length;
8959}
8960
8961/*
8962 * Position for a mode message.
8963 */
8964 static void
8965msg_pos_mode()
8966{
8967 msg_col = 0;
8968 msg_row = Rows - 1;
8969}
8970
8971/*
8972 * Delete mode message. Used when ESC is typed which is expected to end
8973 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008974 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 */
8976 void
8977unshowmode(force)
8978 int force;
8979{
8980 /*
8981 * Don't delete it right now, when not redrawing or insided a mapping.
8982 */
8983 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8984 redraw_cmdline = TRUE; /* delete mode later */
8985 else
8986 {
8987 msg_pos_mode();
8988 if (Recording)
8989 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8990 msg_clr_eos();
8991 }
8992}
8993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008994#if defined(FEAT_WINDOWS)
8995/*
8996 * Draw the tab pages line at the top of the Vim window.
8997 */
8998 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008999draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009000{
9001 int tabcount = 0;
9002 tabpage_T *tp;
9003 int tabwidth;
9004 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009005 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009006 int attr;
9007 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009008 win_T *cwp;
9009 int wincount;
9010 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009011 int c;
9012 int len;
9013 int attr_sel = hl_attr(HLF_TPS);
9014 int attr_nosel = hl_attr(HLF_TP);
9015 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009016 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009017 int room;
9018 int use_sep_chars = (t_colors < 8
9019#ifdef FEAT_GUI
9020 && !gui.in_use
9021#endif
9022 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009023
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009024 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009025
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009026#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009027 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009028 if (gui_use_tabline())
9029 {
9030 gui_update_tabline();
9031 return;
9032 }
9033#endif
9034
9035 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009036 return;
9037
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009038#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009039
9040 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9041 for (scol = 0; scol < Columns; ++scol)
9042 TabPageIdxs[scol] = 0;
9043
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009044 /* Use the 'tabline' option if it's set. */
9045 if (*p_tal != NUL)
9046 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009047 int save_called_emsg = called_emsg;
9048
9049 /* Check for an error. If there is one we would loop in redrawing the
9050 * screen. Avoid that by making 'tabline' empty. */
9051 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009052 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009053 if (called_emsg)
9054 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009055 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009056 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009057 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009058 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009059#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009060 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009061 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9062 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009063
Bram Moolenaar238a5642006-02-21 22:12:05 +00009064 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9065 if (tabwidth < 6)
9066 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009067
Bram Moolenaar238a5642006-02-21 22:12:05 +00009068 attr = attr_nosel;
9069 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009070 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009071 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9072 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009073 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009074 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009075
Bram Moolenaar238a5642006-02-21 22:12:05 +00009076 if (tp->tp_topframe == topframe)
9077 attr = attr_sel;
9078 if (use_sep_chars && col > 0)
9079 screen_putchar('|', 0, col++, attr);
9080
9081 if (tp->tp_topframe != topframe)
9082 attr = attr_nosel;
9083
9084 screen_putchar(' ', 0, col++, attr);
9085
9086 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009087 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009088 cwp = curwin;
9089 wp = firstwin;
9090 }
9091 else
9092 {
9093 cwp = tp->tp_curwin;
9094 wp = tp->tp_firstwin;
9095 }
9096
9097 modified = FALSE;
9098 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9099 if (bufIsChanged(wp->w_buffer))
9100 modified = TRUE;
9101 if (modified || wincount > 1)
9102 {
9103 if (wincount > 1)
9104 {
9105 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009106 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009107 if (col + len >= Columns - 3)
9108 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009109 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009110#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009111 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009112#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009113 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009114#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009115 );
9116 col += len;
9117 }
9118 if (modified)
9119 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9120 screen_putchar(' ', 0, col++, attr);
9121 }
9122
9123 room = scol - col + tabwidth - 1;
9124 if (room > 0)
9125 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009126 /* Get buffer name in NameBuff[] */
9127 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009128 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009129 len = vim_strsize(NameBuff);
9130 p = NameBuff;
9131#ifdef FEAT_MBYTE
9132 if (has_mbyte)
9133 while (len > room)
9134 {
9135 len -= ptr2cells(p);
9136 mb_ptr_adv(p);
9137 }
9138 else
9139#endif
9140 if (len > room)
9141 {
9142 p += len - room;
9143 len = room;
9144 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009145 if (len > Columns - col - 1)
9146 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009148 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009149 col += len;
9150 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009151 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009152
9153 /* Store the tab page number in TabPageIdxs[], so that
9154 * jump_to_mouse() knows where each one is. */
9155 ++tabcount;
9156 while (scol < col)
9157 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009158 }
9159
Bram Moolenaar238a5642006-02-21 22:12:05 +00009160 if (use_sep_chars)
9161 c = '_';
9162 else
9163 c = ' ';
9164 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009165
9166 /* Put an "X" for closing the current tab if there are several. */
9167 if (first_tabpage->tp_next != NULL)
9168 {
9169 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9170 TabPageIdxs[Columns - 1] = -999;
9171 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009172 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009173
9174 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9175 * set. */
9176 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009177}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009178
9179/*
9180 * Get buffer name for "buf" into NameBuff[].
9181 * Takes care of special buffer names and translates special characters.
9182 */
9183 void
9184get_trans_bufname(buf)
9185 buf_T *buf;
9186{
9187 if (buf_spname(buf) != NULL)
9188 STRCPY(NameBuff, buf_spname(buf));
9189 else
9190 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9191 trans_characters(NameBuff, MAXPATHL);
9192}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009193#endif
9194
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9196/*
9197 * Get the character to use in a status line. Get its attributes in "*attr".
9198 */
9199 static int
9200fillchar_status(attr, is_curwin)
9201 int *attr;
9202 int is_curwin;
9203{
9204 int fill;
9205 if (is_curwin)
9206 {
9207 *attr = hl_attr(HLF_S);
9208 fill = fill_stl;
9209 }
9210 else
9211 {
9212 *attr = hl_attr(HLF_SNC);
9213 fill = fill_stlnc;
9214 }
9215 /* Use fill when there is highlighting, and highlighting of current
9216 * window differs, or the fillchars differ, or this is not the
9217 * current window */
9218 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9219 || !is_curwin || firstwin == lastwin)
9220 || (fill_stl != fill_stlnc)))
9221 return fill;
9222 if (is_curwin)
9223 return '^';
9224 return '=';
9225}
9226#endif
9227
9228#ifdef FEAT_VERTSPLIT
9229/*
9230 * Get the character to use in a separator between vertically split windows.
9231 * Get its attributes in "*attr".
9232 */
9233 static int
9234fillchar_vsep(attr)
9235 int *attr;
9236{
9237 *attr = hl_attr(HLF_C);
9238 if (*attr == 0 && fill_vert == ' ')
9239 return '|';
9240 else
9241 return fill_vert;
9242}
9243#endif
9244
9245/*
9246 * Return TRUE if redrawing should currently be done.
9247 */
9248 int
9249redrawing()
9250{
9251 return (!RedrawingDisabled
9252 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9253}
9254
9255/*
9256 * Return TRUE if printing messages should currently be done.
9257 */
9258 int
9259messaging()
9260{
9261 return (!(p_lz && char_avail() && !KeyTyped));
9262}
9263
9264/*
9265 * Show current status info in ruler and various other places
9266 * If always is FALSE, only show ruler if position has changed.
9267 */
9268 void
9269showruler(always)
9270 int always;
9271{
9272 if (!always && !redrawing())
9273 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009274#ifdef FEAT_INS_EXPAND
9275 if (pum_visible())
9276 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009277# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009278 /* Don't redraw right now, do it later. */
9279 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009280# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009281 return;
9282 }
9283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009285 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009286 {
9287 redraw_custum_statusline(curwin);
9288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 else
9290#endif
9291#ifdef FEAT_CMDL_INFO
9292 win_redr_ruler(curwin, always);
9293#endif
9294
9295#ifdef FEAT_TITLE
9296 if (need_maketitle
9297# ifdef FEAT_STL_OPT
9298 || (p_icon && (stl_syntax & STL_IN_ICON))
9299 || (p_title && (stl_syntax & STL_IN_TITLE))
9300# endif
9301 )
9302 maketitle();
9303#endif
9304}
9305
9306#ifdef FEAT_CMDL_INFO
9307 static void
9308win_redr_ruler(wp, always)
9309 win_T *wp;
9310 int always;
9311{
9312 char_u buffer[70];
9313 int row;
9314 int fillchar;
9315 int attr;
9316 int empty_line = FALSE;
9317 colnr_T virtcol;
9318 int i;
9319 int o;
9320#ifdef FEAT_VERTSPLIT
9321 int this_ru_col;
9322 int off = 0;
9323 int width = Columns;
9324# define WITH_OFF(x) x
9325# define WITH_WIDTH(x) x
9326#else
9327# define WITH_OFF(x) 0
9328# define WITH_WIDTH(x) Columns
9329# define this_ru_col ru_col
9330#endif
9331
9332 /* If 'ruler' off or redrawing disabled, don't do anything */
9333 if (!p_ru)
9334 return;
9335
9336 /*
9337 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9338 * after deleting lines, before cursor.lnum is corrected.
9339 */
9340 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9341 return;
9342
9343#ifdef FEAT_INS_EXPAND
9344 /* Don't draw the ruler while doing insert-completion, it might overwrite
9345 * the (long) mode message. */
9346# ifdef FEAT_WINDOWS
9347 if (wp == lastwin && lastwin->w_status_height == 0)
9348# endif
9349 if (edit_submode != NULL)
9350 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009351 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9352 if (pum_visible())
9353 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354#endif
9355
9356#ifdef FEAT_STL_OPT
9357 if (*p_ruf)
9358 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009359 int save_called_emsg = called_emsg;
9360
9361 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009363 if (called_emsg)
9364 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009365 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009366 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 return;
9368 }
9369#endif
9370
9371 /*
9372 * Check if not in Insert mode and the line is empty (will show "0-1").
9373 */
9374 if (!(State & INSERT)
9375 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9376 empty_line = TRUE;
9377
9378 /*
9379 * Only draw the ruler when something changed.
9380 */
9381 validate_virtcol_win(wp);
9382 if ( redraw_cmdline
9383 || always
9384 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9385 || wp->w_cursor.col != wp->w_ru_cursor.col
9386 || wp->w_virtcol != wp->w_ru_virtcol
9387#ifdef FEAT_VIRTUALEDIT
9388 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9389#endif
9390 || wp->w_topline != wp->w_ru_topline
9391 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9392#ifdef FEAT_DIFF
9393 || wp->w_topfill != wp->w_ru_topfill
9394#endif
9395 || empty_line != wp->w_ru_empty)
9396 {
9397 cursor_off();
9398#ifdef FEAT_WINDOWS
9399 if (wp->w_status_height)
9400 {
9401 row = W_WINROW(wp) + wp->w_height;
9402 fillchar = fillchar_status(&attr, wp == curwin);
9403# ifdef FEAT_VERTSPLIT
9404 off = W_WINCOL(wp);
9405 width = W_WIDTH(wp);
9406# endif
9407 }
9408 else
9409#endif
9410 {
9411 row = Rows - 1;
9412 fillchar = ' ';
9413 attr = 0;
9414#ifdef FEAT_VERTSPLIT
9415 width = Columns;
9416 off = 0;
9417#endif
9418 }
9419
9420 /* In list mode virtcol needs to be recomputed */
9421 virtcol = wp->w_virtcol;
9422 if (wp->w_p_list && lcs_tab1 == NUL)
9423 {
9424 wp->w_p_list = FALSE;
9425 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9426 wp->w_p_list = TRUE;
9427 }
9428
9429 /*
9430 * Some sprintfs return the length, some return a pointer.
9431 * To avoid portability problems we use strlen() here.
9432 */
9433 sprintf((char *)buffer, "%ld,",
9434 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9435 ? 0L
9436 : (long)(wp->w_cursor.lnum));
9437 col_print(buffer + STRLEN(buffer),
9438 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9439 (int)virtcol + 1);
9440
9441 /*
9442 * Add a "50%" if there is room for it.
9443 * On the last line, don't print in the last column (scrolls the
9444 * screen up on some terminals).
9445 */
9446 i = (int)STRLEN(buffer);
9447 get_rel_pos(wp, buffer + i + 1);
9448 o = i + vim_strsize(buffer + i + 1);
9449#ifdef FEAT_WINDOWS
9450 if (wp->w_status_height == 0) /* can't use last char of screen */
9451#endif
9452 ++o;
9453#ifdef FEAT_VERTSPLIT
9454 this_ru_col = ru_col - (Columns - width);
9455 if (this_ru_col < 0)
9456 this_ru_col = 0;
9457#endif
9458 /* Never use more than half the window/screen width, leave the other
9459 * half for the filename. */
9460 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9461 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9462 if (this_ru_col + o < WITH_WIDTH(width))
9463 {
9464 while (this_ru_col + o < WITH_WIDTH(width))
9465 {
9466#ifdef FEAT_MBYTE
9467 if (has_mbyte)
9468 i += (*mb_char2bytes)(fillchar, buffer + i);
9469 else
9470#endif
9471 buffer[i++] = fillchar;
9472 ++o;
9473 }
9474 get_rel_pos(wp, buffer + i);
9475 }
9476 /* Truncate at window boundary. */
9477#ifdef FEAT_MBYTE
9478 if (has_mbyte)
9479 {
9480 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009481 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 {
9483 o += (*mb_ptr2cells)(buffer + i);
9484 if (this_ru_col + o > WITH_WIDTH(width))
9485 {
9486 buffer[i] = NUL;
9487 break;
9488 }
9489 }
9490 }
9491 else
9492#endif
9493 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9494 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9495
9496 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9497 i = redraw_cmdline;
9498 screen_fill(row, row + 1,
9499 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9500 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9501 fillchar, fillchar, attr);
9502 /* don't redraw the cmdline because of showing the ruler */
9503 redraw_cmdline = i;
9504 wp->w_ru_cursor = wp->w_cursor;
9505 wp->w_ru_virtcol = wp->w_virtcol;
9506 wp->w_ru_empty = empty_line;
9507 wp->w_ru_topline = wp->w_topline;
9508 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9509#ifdef FEAT_DIFF
9510 wp->w_ru_topfill = wp->w_topfill;
9511#endif
9512 }
9513}
9514#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009515
9516#if defined(FEAT_LINEBREAK) || defined(PROTO)
9517/*
9518 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009519 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009520 * Otherwise it depends on 'numberwidth' and the line count.
9521 */
9522 int
9523number_width(wp)
9524 win_T *wp;
9525{
9526 int n;
9527 linenr_T lnum;
9528
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009529 lnum = wp->w_buffer->b_ml.ml_line_count;
9530 if (lnum == wp->w_nrwidth_line_count)
9531 return wp->w_nrwidth_width;
9532 wp->w_nrwidth_line_count = lnum;
9533
9534 n = 0;
9535 do
9536 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009537 lnum /= 10;
9538 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009539 } while (lnum > 0);
9540
9541 /* 'numberwidth' gives the minimal width plus one */
9542 if (n < wp->w_p_nuw - 1)
9543 n = wp->w_p_nuw - 1;
9544
9545 wp->w_nrwidth_width = n;
9546 return n;
9547}
9548#endif