blob: f85bd082543f0e0b3cdb183a8774a8f2d088be15 [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 Moolenaara443af82007-11-08 13:51:42 +00002602 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002603#endif
2604#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002605 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002606# define SPWORDLEN 150
2607 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002608 int nextlinecol = 0; /* column where nextline[] starts */
2609 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002610 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002611 int spell_attr = 0; /* attributes desired by spelling */
2612 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002613 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2614 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002615 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002616 static int cap_col = -1; /* column to check for Cap word */
2617 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002618 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619#endif
2620 int extra_check; /* has syntax or linebreak */
2621#ifdef FEAT_MBYTE
2622 int multi_attr = 0; /* attributes desired by multibyte */
2623 int mb_l = 1; /* multi-byte byte length */
2624 int mb_c = 0; /* decoded multi-byte character */
2625 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002626 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627#endif
2628#ifdef FEAT_DIFF
2629 int filler_lines; /* nr of filler lines to be drawn */
2630 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002631 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 int change_start = MAXCOL; /* first col of changed area */
2633 int change_end = -1; /* last col of changed area */
2634#endif
2635 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2636#ifdef FEAT_LINEBREAK
2637 int need_showbreak = FALSE;
2638#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002639#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2640 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641# define LINE_ATTR
2642 int line_attr = 0; /* atrribute for the whole line */
2643#endif
2644#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002645 matchitem_T *cur; /* points to the match list */
2646 match_T *shl; /* points to search_hl or a match */
2647 int shl_flag; /* flag to indicate whether search_hl
2648 has been processed or not */
2649 int prevcol_hl_flag; /* flag to indicate whether prevcol
2650 equals startcol of search_hl or one
2651 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652#endif
2653#ifdef FEAT_ARABIC
2654 int prev_c = 0; /* previous Arabic character */
2655 int prev_c1 = 0; /* first composing char for prev_c */
2656#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002657#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002658 int did_line_attr = 0;
2659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 /* draw_state: items that are drawn in sequence: */
2662#define WL_START 0 /* nothing done yet */
2663#ifdef FEAT_CMDWIN
2664# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2665#else
2666# define WL_CMDLINE WL_START
2667#endif
2668#ifdef FEAT_FOLDING
2669# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2670#else
2671# define WL_FOLD WL_CMDLINE
2672#endif
2673#ifdef FEAT_SIGNS
2674# define WL_SIGN WL_FOLD + 1 /* column for signs */
2675#else
2676# define WL_SIGN WL_FOLD /* column for signs */
2677#endif
2678#define WL_NR WL_SIGN + 1 /* line number */
2679#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2680# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2681#else
2682# define WL_SBR WL_NR
2683#endif
2684#define WL_LINE WL_SBR + 1 /* text in the line */
2685 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002686#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 int feedback_col = 0;
2688 int feedback_old_attr = -1;
2689#endif
2690
2691
2692 if (startrow > endrow) /* past the end already! */
2693 return startrow;
2694
2695 row = startrow;
2696 screen_row = row + W_WINROW(wp);
2697
2698 /*
2699 * To speed up the loop below, set extra_check when there is linebreak,
2700 * trailing white space and/or syntax processing to be done.
2701 */
2702#ifdef FEAT_LINEBREAK
2703 extra_check = wp->w_p_lbr;
2704#else
2705 extra_check = 0;
2706#endif
2707#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002708 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
2710 /* Prepare for syntax highlighting in this line. When there is an
2711 * error, stop syntax highlighting. */
2712 save_did_emsg = did_emsg;
2713 did_emsg = FALSE;
2714 syntax_start(wp, lnum);
2715 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002716 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 else
2718 {
2719 did_emsg = save_did_emsg;
2720 has_syntax = TRUE;
2721 extra_check = TRUE;
2722 }
2723 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002724#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002725
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002726#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002727 if (wp->w_p_spell
2728 && *wp->w_buffer->b_p_spl != NUL
2729 && wp->w_buffer->b_langp.ga_len > 0
2730 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002731 {
2732 /* Prepare for spell checking. */
2733 has_spell = TRUE;
2734 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002735
2736 /* Get the start of the next line, so that words that wrap to the next
2737 * line are found too: "et<line-break>al.".
2738 * Trick: skip a few chars for C/shell/Vim comments */
2739 nextline[SPWORDLEN] = NUL;
2740 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2741 {
2742 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2743 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2744 }
2745
2746 /* When a word wrapped from the previous line the start of the current
2747 * line is valid. */
2748 if (lnum == checked_lnum)
2749 cur_checked_col = checked_col;
2750 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002751
2752 /* When there was a sentence end in the previous line may require a
2753 * word starting with capital in this line. In line 1 always check
2754 * the first word. */
2755 if (lnum != capcol_lnum)
2756 cap_col = -1;
2757 if (lnum == 1)
2758 cap_col = 0;
2759 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761#endif
2762
2763 /*
2764 * handle visual active in this window
2765 */
2766 fromcol = -10;
2767 tocol = MAXCOL;
2768#ifdef FEAT_VISUAL
2769 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2770 {
2771 /* Visual is after curwin->w_cursor */
2772 if (ltoreq(curwin->w_cursor, VIsual))
2773 {
2774 top = &curwin->w_cursor;
2775 bot = &VIsual;
2776 }
2777 else /* Visual is before curwin->w_cursor */
2778 {
2779 top = &VIsual;
2780 bot = &curwin->w_cursor;
2781 }
2782 if (VIsual_mode == Ctrl_V) /* block mode */
2783 {
2784 if (lnum >= top->lnum && lnum <= bot->lnum)
2785 {
2786 fromcol = wp->w_old_cursor_fcol;
2787 tocol = wp->w_old_cursor_lcol;
2788 }
2789 }
2790 else /* non-block mode */
2791 {
2792 if (lnum > top->lnum && lnum <= bot->lnum)
2793 fromcol = 0;
2794 else if (lnum == top->lnum)
2795 {
2796 if (VIsual_mode == 'V') /* linewise */
2797 fromcol = 0;
2798 else
2799 {
2800 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2801 if (gchar_pos(top) == NUL)
2802 tocol = fromcol + 1;
2803 }
2804 }
2805 if (VIsual_mode != 'V' && lnum == bot->lnum)
2806 {
2807 if (*p_sel == 'e' && bot->col == 0
2808#ifdef FEAT_VIRTUALEDIT
2809 && bot->coladd == 0
2810#endif
2811 )
2812 {
2813 fromcol = -10;
2814 tocol = MAXCOL;
2815 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002816 else if (bot->col == MAXCOL)
2817 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 else
2819 {
2820 pos = *bot;
2821 if (*p_sel == 'e')
2822 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2823 else
2824 {
2825 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2826 ++tocol;
2827 }
2828 }
2829 }
2830 }
2831
2832#ifndef MSDOS
2833 /* Check if the character under the cursor should not be inverted */
2834 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2835# ifdef FEAT_GUI
2836 && !gui.in_use
2837# endif
2838 )
2839 noinvcur = TRUE;
2840#endif
2841
2842 /* if inverting in this line set area_highlighting */
2843 if (fromcol >= 0)
2844 {
2845 area_highlighting = TRUE;
2846 attr = hl_attr(HLF_V);
2847#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2848 if (clip_star.available && !clip_star.owned && clip_isautosel())
2849 attr = hl_attr(HLF_VNC);
2850#endif
2851 }
2852 }
2853
2854 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002855 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
2857 else
2858#endif /* FEAT_VISUAL */
2859 if (highlight_match
2860 && wp == curwin
2861 && lnum >= curwin->w_cursor.lnum
2862 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2863 {
2864 if (lnum == curwin->w_cursor.lnum)
2865 getvcol(curwin, &(curwin->w_cursor),
2866 (colnr_T *)&fromcol, NULL, NULL);
2867 else
2868 fromcol = 0;
2869 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2870 {
2871 pos.lnum = lnum;
2872 pos.col = search_match_endcol;
2873 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2874 }
2875 else
2876 tocol = MAXCOL;
2877 if (fromcol == tocol) /* do at least one character */
2878 tocol = fromcol + 1; /* happens when past end of line */
2879 area_highlighting = TRUE;
2880 attr = hl_attr(HLF_I);
2881 }
2882
2883#ifdef FEAT_DIFF
2884 filler_lines = diff_check(wp, lnum);
2885 if (filler_lines < 0)
2886 {
2887 if (filler_lines == -1)
2888 {
2889 if (diff_find_change(wp, lnum, &change_start, &change_end))
2890 diff_hlf = HLF_ADD; /* added line */
2891 else if (change_start == 0)
2892 diff_hlf = HLF_TXD; /* changed text */
2893 else
2894 diff_hlf = HLF_CHD; /* changed line */
2895 }
2896 else
2897 diff_hlf = HLF_ADD; /* added line */
2898 filler_lines = 0;
2899 area_highlighting = TRUE;
2900 }
2901 if (lnum == wp->w_topline)
2902 filler_lines = wp->w_topfill;
2903 filler_todo = filler_lines;
2904#endif
2905
2906#ifdef LINE_ATTR
2907# ifdef FEAT_SIGNS
2908 /* If this line has a sign with line highlighting set line_attr. */
2909 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2910 if (v != 0)
2911 line_attr = sign_get_attr((int)v, TRUE);
2912# endif
2913# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2914 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002915 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 line_attr = hl_attr(HLF_L);
2917# endif
2918 if (line_attr != 0)
2919 area_highlighting = TRUE;
2920#endif
2921
2922 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2923 ptr = line;
2924
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002925#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002926 if (has_spell)
2927 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002928 /* For checking first word with a capital skip white space. */
2929 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002930 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002931
Bram Moolenaar30abd282005-06-22 22:35:10 +00002932 /* To be able to spell-check over line boundaries copy the end of the
2933 * current line into nextline[]. Above the start of the next line was
2934 * copied to nextline[SPWORDLEN]. */
2935 if (nextline[SPWORDLEN] == NUL)
2936 {
2937 /* No next line or it is empty. */
2938 nextlinecol = MAXCOL;
2939 nextline_idx = 0;
2940 }
2941 else
2942 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002943 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002944 if (v < SPWORDLEN)
2945 {
2946 /* Short line, use it completely and append the start of the
2947 * next line. */
2948 nextlinecol = 0;
2949 mch_memmove(nextline, line, (size_t)v);
2950 mch_memmove(nextline + v, nextline + SPWORDLEN,
2951 STRLEN(nextline + SPWORDLEN) + 1);
2952 nextline_idx = v + 1;
2953 }
2954 else
2955 {
2956 /* Long line, use only the last SPWORDLEN bytes. */
2957 nextlinecol = v - SPWORDLEN;
2958 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2959 nextline_idx = SPWORDLEN + 1;
2960 }
2961 }
2962 }
2963#endif
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 /* find start of trailing whitespace */
2966 if (wp->w_p_list && lcs_trail)
2967 {
2968 trailcol = (colnr_T)STRLEN(ptr);
2969 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2970 --trailcol;
2971 trailcol += (colnr_T) (ptr - line);
2972 extra_check = TRUE;
2973 }
2974
2975 /*
2976 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2977 * first character to be displayed.
2978 */
2979 if (wp->w_p_wrap)
2980 v = wp->w_skipcol;
2981 else
2982 v = wp->w_leftcol;
2983 if (v > 0)
2984 {
2985#ifdef FEAT_MBYTE
2986 char_u *prev_ptr = ptr;
2987#endif
2988 while (vcol < v && *ptr != NUL)
2989 {
2990 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2991 vcol += c;
2992#ifdef FEAT_MBYTE
2993 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002995 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997
2998#ifdef FEAT_VIRTUALEDIT
2999 /* When 'virtualedit' is set the end of the line may be before the
3000 * start of the displayed part. */
3001 if (vcol < v && *ptr == NUL && virtual_active())
3002 vcol = v;
3003#endif
3004
3005 /* Handle a character that's not completely on the screen: Put ptr at
3006 * that character but skip the first few screen characters. */
3007 if (vcol > v)
3008 {
3009 vcol -= c;
3010#ifdef FEAT_MBYTE
3011 ptr = prev_ptr;
3012#else
3013 --ptr;
3014#endif
3015 n_skip = v - vcol;
3016 }
3017
3018 /*
3019 * Adjust for when the inverted text is before the screen,
3020 * and when the start of the inverted text is before the screen.
3021 */
3022 if (tocol <= vcol)
3023 fromcol = 0;
3024 else if (fromcol >= 0 && fromcol < vcol)
3025 fromcol = vcol;
3026
3027#ifdef FEAT_LINEBREAK
3028 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3029 if (wp->w_p_wrap)
3030 need_showbreak = TRUE;
3031#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003032#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003033 /* When spell checking a word we need to figure out the start of the
3034 * word and if it's badly spelled or not. */
3035 if (has_spell)
3036 {
3037 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003038 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003039
3040 pos = wp->w_cursor;
3041 wp->w_cursor.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003042 wp->w_cursor.col = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003043 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003044 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003045 {
3046 /* no bad word found at line start, don't check until end of a
3047 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003048 spell_hlf = HLF_COUNT;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003049 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003050 }
3051 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003052 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003053 /* bad word found, use attributes until end of word */
3054 word_end = wp->w_cursor.col + len + 1;
3055
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003056 /* Turn index into actual attributes. */
3057 if (spell_hlf != HLF_COUNT)
3058 spell_attr = highlight_attr[spell_hlf];
3059 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003060 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003061
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003062# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003063 /* Need to restart syntax highlighting for this line. */
3064 if (has_syntax)
3065 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003066# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003067 }
3068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 }
3070
3071 /*
3072 * Correct highlighting for cursor that can't be disabled.
3073 * Avoids having to check this for each character.
3074 */
3075 if (fromcol >= 0)
3076 {
3077 if (noinvcur)
3078 {
3079 if ((colnr_T)fromcol == wp->w_virtcol)
3080 {
3081 /* highlighting starts at cursor, let it start just after the
3082 * cursor */
3083 fromcol_prev = fromcol;
3084 fromcol = -1;
3085 }
3086 else if ((colnr_T)fromcol < wp->w_virtcol)
3087 /* restart highlighting after the cursor */
3088 fromcol_prev = wp->w_virtcol;
3089 }
3090 if (fromcol >= tocol)
3091 fromcol = -1;
3092 }
3093
3094#ifdef FEAT_SEARCH_EXTRA
3095 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003096 * Handle highlighting the last used search pattern and matches.
3097 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003099 cur = wp->w_match_head;
3100 shl_flag = FALSE;
3101 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003103 if (shl_flag == FALSE)
3104 {
3105 shl = &search_hl;
3106 shl_flag = TRUE;
3107 }
3108 else
3109 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003110 shl->startcol = MAXCOL;
3111 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 shl->attr_cur = 0;
3113 if (shl->rm.regprog != NULL)
3114 {
3115 v = (long)(ptr - line);
3116 next_search_hl(wp, shl, lnum, (colnr_T)v);
3117
3118 /* Need to get the line again, a multi-line regexp may have made it
3119 * invalid. */
3120 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3121 ptr = line + v;
3122
3123 if (shl->lnum != 0 && shl->lnum <= lnum)
3124 {
3125 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003126 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003128 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3130 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003131 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003133 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003135 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
3137#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003138 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003139 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 else
3141#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003142 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003144 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146 shl->attr_cur = shl->attr;
3147 search_attr = shl->attr;
3148 }
3149 area_highlighting = TRUE;
3150 }
3151 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003152 if (shl != &search_hl && cur != NULL)
3153 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
3155#endif
3156
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003157#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003158 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3159 * active, because it's not clear what is selected then. */
3160 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003161 {
3162 line_attr = hl_attr(HLF_CUL);
3163 area_highlighting = TRUE;
3164 }
3165#endif
3166
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003167 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 col = 0;
3169#ifdef FEAT_RIGHTLEFT
3170 if (wp->w_p_rl)
3171 {
3172 /* Rightleft window: process the text in the normal direction, but put
3173 * it in current_ScreenLine[] from right to left. Start at the
3174 * rightmost column of the window. */
3175 col = W_WIDTH(wp) - 1;
3176 off += col;
3177 }
3178#endif
3179
3180 /*
3181 * Repeat for the whole displayed line.
3182 */
3183 for (;;)
3184 {
3185 /* Skip this quickly when working on the text. */
3186 if (draw_state != WL_LINE)
3187 {
3188#ifdef FEAT_CMDWIN
3189 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3190 {
3191 draw_state = WL_CMDLINE;
3192 if (cmdwin_type != 0 && wp == curwin)
3193 {
3194 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003196 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 char_attr = hl_attr(HLF_AT);
3198 }
3199 }
3200#endif
3201
3202#ifdef FEAT_FOLDING
3203 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3204 {
3205 draw_state = WL_FOLD;
3206 if (wp->w_p_fdc > 0)
3207 {
3208 /* Draw the 'foldcolumn'. */
3209 fill_foldcolumn(extra, wp, FALSE, lnum);
3210 n_extra = wp->w_p_fdc;
3211 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003212 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 c_extra = NUL;
3214 char_attr = hl_attr(HLF_FC);
3215 }
3216 }
3217#endif
3218
3219#ifdef FEAT_SIGNS
3220 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3221 {
3222 draw_state = WL_SIGN;
3223 /* Show the sign column when there are any signs in this
3224 * buffer or when using Netbeans. */
3225 if (draw_signcolumn(wp)
3226# ifdef FEAT_DIFF
3227 && filler_todo <= 0
3228# endif
3229 )
3230 {
3231 int_u text_sign;
3232# ifdef FEAT_SIGN_ICONS
3233 int_u icon_sign;
3234# endif
3235
3236 /* Draw two cells with the sign value or blank. */
3237 c_extra = ' ';
3238 char_attr = hl_attr(HLF_SC);
3239 n_extra = 2;
3240
3241 if (row == startrow)
3242 {
3243 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3244 SIGN_TEXT);
3245# ifdef FEAT_SIGN_ICONS
3246 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3247 SIGN_ICON);
3248 if (gui.in_use && icon_sign != 0)
3249 {
3250 /* Use the image in this position. */
3251 c_extra = SIGN_BYTE;
3252# ifdef FEAT_NETBEANS_INTG
3253 if (buf_signcount(wp->w_buffer, lnum) > 1)
3254 c_extra = MULTISIGN_BYTE;
3255# endif
3256 char_attr = icon_sign;
3257 }
3258 else
3259# endif
3260 if (text_sign != 0)
3261 {
3262 p_extra = sign_get_text(text_sign);
3263 if (p_extra != NULL)
3264 {
3265 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003266 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268 char_attr = sign_get_attr(text_sign, FALSE);
3269 }
3270 }
3271 }
3272 }
3273#endif
3274
3275 if (draw_state == WL_NR - 1 && n_extra == 0)
3276 {
3277 draw_state = WL_NR;
3278 /* Display the line number. After the first fill with blanks
3279 * when the 'n' flag isn't in 'cpo' */
3280 if (wp->w_p_nu
3281 && (row == startrow
3282#ifdef FEAT_DIFF
3283 + filler_lines
3284#endif
3285 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3286 {
3287 /* Draw the line number (empty space after wrapping). */
3288 if (row == startrow
3289#ifdef FEAT_DIFF
3290 + filler_lines
3291#endif
3292 )
3293 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003294 sprintf((char *)extra, "%*ld ",
3295 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (wp->w_skipcol > 0)
3297 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3298 *p_extra = '-';
3299#ifdef FEAT_RIGHTLEFT
3300 if (wp->w_p_rl) /* reverse line numbers */
3301 rl_mirror(extra);
3302#endif
3303 p_extra = extra;
3304 c_extra = NUL;
3305 }
3306 else
3307 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003308 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003310#ifdef FEAT_SYN_HL
3311 /* When 'cursorline' is set highlight the line number of
3312 * the current line differently. */
3313 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3314 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317 }
3318
3319#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3320 if (draw_state == WL_SBR - 1 && n_extra == 0)
3321 {
3322 draw_state = WL_SBR;
3323# ifdef FEAT_DIFF
3324 if (filler_todo > 0)
3325 {
3326 /* Draw "deleted" diff line(s). */
3327 if (char2cells(fill_diff) > 1)
3328 c_extra = '-';
3329 else
3330 c_extra = fill_diff;
3331# ifdef FEAT_RIGHTLEFT
3332 if (wp->w_p_rl)
3333 n_extra = col + 1;
3334 else
3335# endif
3336 n_extra = W_WIDTH(wp) - col;
3337 char_attr = hl_attr(HLF_DED);
3338 }
3339# endif
3340# ifdef FEAT_LINEBREAK
3341 if (*p_sbr != NUL && need_showbreak)
3342 {
3343 /* Draw 'showbreak' at the start of each broken line. */
3344 p_extra = p_sbr;
3345 c_extra = NUL;
3346 n_extra = (int)STRLEN(p_sbr);
3347 char_attr = hl_attr(HLF_AT);
3348 need_showbreak = FALSE;
3349 /* Correct end of highlighted area for 'showbreak',
3350 * required when 'linebreak' is also set. */
3351 if (tocol == vcol)
3352 tocol += n_extra;
3353 }
3354# endif
3355 }
3356#endif
3357
3358 if (draw_state == WL_LINE - 1 && n_extra == 0)
3359 {
3360 draw_state = WL_LINE;
3361 if (saved_n_extra)
3362 {
3363 /* Continue item from end of wrapped line. */
3364 n_extra = saved_n_extra;
3365 c_extra = saved_c_extra;
3366 p_extra = saved_p_extra;
3367 char_attr = saved_char_attr;
3368 }
3369 else
3370 char_attr = 0;
3371 }
3372 }
3373
3374 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003375 if (dollar_vcol != 0 && wp == curwin
3376 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#ifdef FEAT_DIFF
3378 && filler_todo <= 0
3379#endif
3380 )
3381 {
3382 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3383 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003384 /* Pretend we have finished updating the window. Except when
3385 * 'cursorcolumn' is set. */
3386#ifdef FEAT_SYN_HL
3387 if (wp->w_p_cuc)
3388 row = wp->w_cline_row + wp->w_cline_height;
3389 else
3390#endif
3391 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 break;
3393 }
3394
3395 if (draw_state == WL_LINE && area_highlighting)
3396 {
3397 /* handle Visual or match highlighting in this line */
3398 if (vcol == fromcol
3399#ifdef FEAT_MBYTE
3400 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3401 && (*mb_ptr2cells)(ptr) > 1)
3402#endif
3403 || ((int)vcol_prev == fromcol_prev
3404 && vcol < tocol))
3405 area_attr = attr; /* start highlighting */
3406 else if (area_attr != 0
3407 && (vcol == tocol
3408 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411#ifdef FEAT_SEARCH_EXTRA
3412 if (!n_extra)
3413 {
3414 /*
3415 * Check for start/end of search pattern match.
3416 * After end, check for start/end of next match.
3417 * When another match, have to check for start again.
3418 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003419 * Do this for 'search_hl' and the match list (ordered by
3420 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003422 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003423 cur = wp->w_match_head;
3424 shl_flag = FALSE;
3425 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003427 if (shl_flag == FALSE
3428 && ((cur != NULL
3429 && cur->priority > SEARCH_HL_PRIORITY)
3430 || cur == NULL))
3431 {
3432 shl = &search_hl;
3433 shl_flag = TRUE;
3434 }
3435 else
3436 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 while (shl->rm.regprog != NULL)
3438 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003439 if (shl->startcol != MAXCOL
3440 && v >= (long)shl->startcol
3441 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
3443 shl->attr_cur = shl->attr;
3444 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003445 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 shl->attr_cur = 0;
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 next_search_hl(wp, shl, lnum, (colnr_T)v);
3450
3451 /* Need to get the line again, a multi-line regexp
3452 * may have made it invalid. */
3453 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3454 ptr = line + v;
3455
3456 if (shl->lnum == lnum)
3457 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003458 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003460 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003462 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003464 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 /* highlight empty match, try again after
3467 * it */
3468#ifdef FEAT_MBYTE
3469 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003470 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003471 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 else
3473#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003474 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476
3477 /* Loop to check if the match starts at the
3478 * current position */
3479 continue;
3480 }
3481 }
3482 break;
3483 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003484 if (shl != &search_hl && cur != NULL)
3485 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003487
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003488 /* Use attributes from match with highest priority among
3489 * 'search_hl' and the match list. */
3490 search_attr = search_hl.attr_cur;
3491 cur = wp->w_match_head;
3492 shl_flag = FALSE;
3493 while (cur != NULL || shl_flag == FALSE)
3494 {
3495 if (shl_flag == FALSE
3496 && ((cur != NULL
3497 && cur->priority > SEARCH_HL_PRIORITY)
3498 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003499 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003500 shl = &search_hl;
3501 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003502 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003503 else
3504 shl = &cur->hl;
3505 if (shl->attr_cur != 0)
3506 search_attr = shl->attr_cur;
3507 if (shl != &search_hl && cur != NULL)
3508 cur = cur->next;
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511#endif
3512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003514 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003516 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3517 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003519 if (diff_hlf == HLF_TXD && ptr - line > change_end
3520 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003522 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003525
3526 /* Decide which of the highlight attributes to use. */
3527 attr_pri = TRUE;
3528 if (area_attr != 0)
3529 char_attr = area_attr;
3530 else if (search_attr != 0)
3531 char_attr = search_attr;
3532#ifdef LINE_ATTR
3533 /* Use line_attr when not in the Visual or 'incsearch' area
3534 * (area_attr may be 0 when "noinvcur" is set). */
3535 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3536 || (vcol < fromcol || vcol >= tocol)))
3537 char_attr = line_attr;
3538#endif
3539 else
3540 {
3541 attr_pri = FALSE;
3542#ifdef FEAT_SYN_HL
3543 if (has_syntax)
3544 char_attr = syntax_attr;
3545 else
3546#endif
3547 char_attr = 0;
3548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550
3551 /*
3552 * Get the next character to put on the screen.
3553 */
3554 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003555 * The "p_extra" points to the extra stuff that is inserted to
3556 * represent special characters (non-printable stuff) and other
3557 * things. When all characters are the same, c_extra is used.
3558 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3559 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3561 */
3562 if (n_extra > 0)
3563 {
3564 if (c_extra != NUL)
3565 {
3566 c = c_extra;
3567#ifdef FEAT_MBYTE
3568 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3569 if (enc_utf8 && (*mb_char2len)(c) > 1)
3570 {
3571 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003572 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003573 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 else
3576 mb_utf8 = FALSE;
3577#endif
3578 }
3579 else
3580 {
3581 c = *p_extra;
3582#ifdef FEAT_MBYTE
3583 if (has_mbyte)
3584 {
3585 mb_c = c;
3586 if (enc_utf8)
3587 {
3588 /* If the UTF-8 character is more than one byte:
3589 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003590 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 mb_utf8 = FALSE;
3592 if (mb_l > n_extra)
3593 mb_l = 1;
3594 else if (mb_l > 1)
3595 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003596 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003598 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
3600 }
3601 else
3602 {
3603 /* if this is a DBCS character, put it in "mb_c" */
3604 mb_l = MB_BYTE2LEN(c);
3605 if (mb_l >= n_extra)
3606 mb_l = 1;
3607 else if (mb_l > 1)
3608 mb_c = (c << 8) + p_extra[1];
3609 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003610 if (mb_l == 0) /* at the NUL at end-of-line */
3611 mb_l = 1;
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 /* If a double-width char doesn't fit display a '>' in the
3614 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003615 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616# ifdef FEAT_RIGHTLEFT
3617 wp->w_p_rl ? (col <= 0) :
3618# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003619 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 && (*mb_char2cells)(mb_c) == 2)
3621 {
3622 c = '>';
3623 mb_c = c;
3624 mb_l = 1;
3625 mb_utf8 = FALSE;
3626 multi_attr = hl_attr(HLF_AT);
3627 /* put the pointer back to output the double-width
3628 * character at the start of the next line. */
3629 ++n_extra;
3630 --p_extra;
3631 }
3632 else
3633 {
3634 n_extra -= mb_l - 1;
3635 p_extra += mb_l - 1;
3636 }
3637 }
3638#endif
3639 ++p_extra;
3640 }
3641 --n_extra;
3642 }
3643 else
3644 {
3645 /*
3646 * Get a character from the line itself.
3647 */
3648 c = *ptr;
3649#ifdef FEAT_MBYTE
3650 if (has_mbyte)
3651 {
3652 mb_c = c;
3653 if (enc_utf8)
3654 {
3655 /* If the UTF-8 character is more than one byte: Decode it
3656 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003657 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 mb_utf8 = FALSE;
3659 if (mb_l > 1)
3660 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003661 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 /* Overlong encoded ASCII or ASCII with composing char
3663 * is displayed normally, except a NUL. */
3664 if (mb_c < 0x80)
3665 c = mb_c;
3666 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003667
3668 /* At start of the line we can have a composing char.
3669 * Draw it as a space with a composing char. */
3670 if (utf_iscomposing(mb_c))
3671 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003672 int i;
3673
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003674 for (i = Screen_mco - 1; i > 0; --i)
3675 u8cc[i] = u8cc[i - 1];
3676 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003677 mb_c = ' ';
3678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680
3681 if ((mb_l == 1 && c >= 0x80)
3682 || (mb_l >= 1 && mb_c == 0)
3683 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003684# ifdef UNICODE16
3685 || mb_c >= 0x10000
3686# endif
3687 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
3689 /*
3690 * Illegal UTF-8 byte: display as <xx>.
3691 * Non-BMP character : display as ? or fullwidth ?.
3692 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003693# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003698# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 if (wp->w_p_rl) /* reverse */
3700 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003703# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 else if (utf_char2cells(mb_c) != 2)
3705 STRCPY(extra, "?");
3706 else
3707 /* 0xff1f in UTF-8: full-width '?' */
3708 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
3711 p_extra = extra;
3712 c = *p_extra;
3713 mb_c = mb_ptr2char_adv(&p_extra);
3714 mb_utf8 = (c >= 0x80);
3715 n_extra = (int)STRLEN(p_extra);
3716 c_extra = NUL;
3717 if (area_attr == 0 && search_attr == 0)
3718 {
3719 n_attr = n_extra + 1;
3720 extra_attr = hl_attr(HLF_8);
3721 saved_attr2 = char_attr; /* save current attr */
3722 }
3723 }
3724 else if (mb_l == 0) /* at the NUL at end-of-line */
3725 mb_l = 1;
3726#ifdef FEAT_ARABIC
3727 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3728 {
3729 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003730 int pc, pc1, nc;
3731 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 /* The idea of what is the previous and next
3734 * character depends on 'rightleft'. */
3735 if (wp->w_p_rl)
3736 {
3737 pc = prev_c;
3738 pc1 = prev_c1;
3739 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003740 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 else
3743 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003744 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003746 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748 prev_c = mb_c;
3749
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003750 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752 else
3753 prev_c = mb_c;
3754#endif
3755 }
3756 else /* enc_dbcs */
3757 {
3758 mb_l = MB_BYTE2LEN(c);
3759 if (mb_l == 0) /* at the NUL at end-of-line */
3760 mb_l = 1;
3761 else if (mb_l > 1)
3762 {
3763 /* We assume a second byte below 32 is illegal.
3764 * Hopefully this is OK for all double-byte encodings!
3765 */
3766 if (ptr[1] >= 32)
3767 mb_c = (c << 8) + ptr[1];
3768 else
3769 {
3770 if (ptr[1] == NUL)
3771 {
3772 /* head byte at end of line */
3773 mb_l = 1;
3774 transchar_nonprint(extra, c);
3775 }
3776 else
3777 {
3778 /* illegal tail byte */
3779 mb_l = 2;
3780 STRCPY(extra, "XX");
3781 }
3782 p_extra = extra;
3783 n_extra = (int)STRLEN(extra) - 1;
3784 c_extra = NUL;
3785 c = *p_extra++;
3786 if (area_attr == 0 && search_attr == 0)
3787 {
3788 n_attr = n_extra + 1;
3789 extra_attr = hl_attr(HLF_8);
3790 saved_attr2 = char_attr; /* save current attr */
3791 }
3792 mb_c = c;
3793 }
3794 }
3795 }
3796 /* If a double-width char doesn't fit display a '>' in the
3797 * last column; the character is displayed at the start of the
3798 * next line. */
3799 if ((
3800# ifdef FEAT_RIGHTLEFT
3801 wp->w_p_rl ? (col <= 0) :
3802# endif
3803 (col >= W_WIDTH(wp) - 1))
3804 && (*mb_char2cells)(mb_c) == 2)
3805 {
3806 c = '>';
3807 mb_c = c;
3808 mb_utf8 = FALSE;
3809 mb_l = 1;
3810 multi_attr = hl_attr(HLF_AT);
3811 /* Put pointer back so that the character will be
3812 * displayed at the start of the next line. */
3813 --ptr;
3814 }
3815 else if (*ptr != NUL)
3816 ptr += mb_l - 1;
3817
3818 /* If a double-width char doesn't fit at the left side display
3819 * a '<' in the first column. */
3820 if (n_skip > 0 && mb_l > 1)
3821 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003823 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 c = ' ';
3825 if (area_attr == 0 && search_attr == 0)
3826 {
3827 n_attr = n_extra + 1;
3828 extra_attr = hl_attr(HLF_AT);
3829 saved_attr2 = char_attr; /* save current attr */
3830 }
3831 mb_c = c;
3832 mb_utf8 = FALSE;
3833 mb_l = 1;
3834 }
3835
3836 }
3837#endif
3838 ++ptr;
3839
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003840 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003841 if (wp->w_p_list && (c == 160
3842#ifdef FEAT_MBYTE
3843 || (mb_utf8 && mb_c == 160)
3844#endif
3845 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003846 {
3847 c = lcs_nbsp;
3848 if (area_attr == 0 && search_attr == 0)
3849 {
3850 n_attr = 1;
3851 extra_attr = hl_attr(HLF_8);
3852 saved_attr2 = char_attr; /* save current attr */
3853 }
3854#ifdef FEAT_MBYTE
3855 mb_c = c;
3856 if (enc_utf8 && (*mb_char2len)(c) > 1)
3857 {
3858 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003859 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003860 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003861 }
3862 else
3863 mb_utf8 = FALSE;
3864#endif
3865 }
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (extra_check)
3868 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003869#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003870 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003871#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003872
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003873#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 /* Get syntax attribute, unless still at the start of the line
3875 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003876 v = (long)(ptr - line);
3877 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
3879 /* Get the syntax attribute for the character. If there
3880 * is an error, disable syntax highlighting. */
3881 save_did_emsg = did_emsg;
3882 did_emsg = FALSE;
3883
Bram Moolenaar217ad922005-03-20 22:37:15 +00003884 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003885# ifdef FEAT_SPELL
3886 has_spell ? &can_spell :
3887# endif
3888 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003891 {
3892 wp->w_buffer->b_syn_error = TRUE;
3893 has_syntax = FALSE;
3894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else
3896 did_emsg = save_did_emsg;
3897
3898 /* Need to get the line again, a multi-line regexp may
3899 * have made it invalid. */
3900 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3901 ptr = line + v;
3902
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003903 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003905 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003906 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003908#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003909
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003910#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003911 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003912 * Only do this when there is no syntax highlighting, the
3913 * @Spell cluster is not used or the current syntax item
3914 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003915 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003916 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003917 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003918# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003919 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003920 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003921# endif
3922 if (c != 0 && (
3923# ifdef FEAT_SYN_HL
3924 !has_syntax ||
3925# endif
3926 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003927 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003928 char_u *prev_ptr, *p;
3929 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003930 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003931# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003932 if (has_mbyte)
3933 {
3934 prev_ptr = ptr - mb_l;
3935 v -= mb_l - 1;
3936 }
3937 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003938# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003939 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003940
3941 /* Use nextline[] if possible, it has the start of the
3942 * next line concatenated. */
3943 if ((prev_ptr - line) - nextlinecol >= 0)
3944 p = nextline + (prev_ptr - line) - nextlinecol;
3945 else
3946 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003947 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003948 len = spell_check(wp, p, &spell_hlf, &cap_col,
3949 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003950 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003951
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003952 /* In Insert mode only highlight a word that
3953 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003954 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003955 && (State & INSERT) != 0
3956 && wp->w_cursor.lnum == lnum
3957 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003958 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003959 && wp->w_cursor.col < (colnr_T)word_end)
3960 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003961 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003962 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003963 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003964
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003965 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003966 && (p - nextline) + len > nextline_idx)
3967 {
3968 /* Remember that the good word continues at the
3969 * start of the next line. */
3970 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003971 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003972 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003973
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003974 /* Turn index into actual attributes. */
3975 if (spell_hlf != HLF_COUNT)
3976 spell_attr = highlight_attr[spell_hlf];
3977
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003978 if (cap_col > 0)
3979 {
3980 if (p != prev_ptr
3981 && (p - nextline) + cap_col >= nextline_idx)
3982 {
3983 /* Remember that the word in the next line
3984 * must start with a capital. */
3985 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003986 cap_col = (int)((p - nextline) + cap_col
3987 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003988 }
3989 else
3990 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003991 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003992 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003993 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003994 }
3995 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003996 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003997 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003998 char_attr = hl_combine_attr(char_attr, spell_attr);
3999 else
4000 char_attr = hl_combine_attr(spell_attr, char_attr);
4001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#endif
4003#ifdef FEAT_LINEBREAK
4004 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004005 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 */
4007 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004008 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
4010 n_extra = win_lbr_chartabsize(wp, ptr - (
4011# ifdef FEAT_MBYTE
4012 has_mbyte ? mb_l :
4013# endif
4014 1), (colnr_T)vcol, NULL) - 1;
4015 c_extra = ' ';
4016 if (vim_iswhite(c))
4017 c = ' ';
4018 }
4019#endif
4020
4021 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4022 {
4023 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004024 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026 n_attr = 1;
4027 extra_attr = hl_attr(HLF_8);
4028 saved_attr2 = char_attr; /* save current attr */
4029 }
4030#ifdef FEAT_MBYTE
4031 mb_c = c;
4032 if (enc_utf8 && (*mb_char2len)(c) > 1)
4033 {
4034 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004035 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 else
4039 mb_utf8 = FALSE;
4040#endif
4041 }
4042 }
4043
4044 /*
4045 * Handling of non-printable characters.
4046 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004047 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
4049 /*
4050 * when getting a character from the file, we may have to
4051 * turn it into something else on the way to putting it
4052 * into "ScreenLines".
4053 */
4054 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4055 {
4056 /* tab amount depends on current column */
4057 n_extra = (int)wp->w_buffer->b_p_ts
4058 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4059#ifdef FEAT_MBYTE
4060 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4061#endif
4062 if (wp->w_p_list)
4063 {
4064 c = lcs_tab1;
4065 c_extra = lcs_tab2;
4066 n_attr = n_extra + 1;
4067 extra_attr = hl_attr(HLF_8);
4068 saved_attr2 = char_attr; /* save current attr */
4069#ifdef FEAT_MBYTE
4070 mb_c = c;
4071 if (enc_utf8 && (*mb_char2len)(c) > 1)
4072 {
4073 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004074 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004075 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077#endif
4078 }
4079 else
4080 {
4081 c_extra = ' ';
4082 c = ' ';
4083 }
4084 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004085 else if (c == NUL
4086 && ((wp->w_p_list && lcs_eol > 0)
4087 || ((fromcol >= 0 || fromcol_prev >= 0)
4088 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004089#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004090 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004091#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004092 && (
4093# ifdef FEAT_RIGHTLEFT
4094 wp->w_p_rl ? (col >= 0) :
4095# endif
4096 (col < W_WIDTH(wp)))
4097 && !(noinvcur
4098 && (colnr_T)vcol == wp->w_virtcol)))
4099 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004101 /* Display a '$' after the line or highlight an extra
4102 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4104 /* For a diff line the highlighting continues after the
4105 * "$". */
4106 if (
4107# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004108 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109# ifdef LINE_ATTR
4110 &&
4111# endif
4112# endif
4113# ifdef LINE_ATTR
4114 line_attr == 0
4115# endif
4116 )
4117#endif
4118 {
4119#ifdef FEAT_VIRTUALEDIT
4120 /* In virtualedit, visual selections may extend
4121 * beyond end of line. */
4122 if (area_highlighting && virtual_active()
4123 && tocol != MAXCOL && vcol < tocol)
4124 n_extra = 0;
4125 else
4126#endif
4127 {
4128 p_extra = at_end_str;
4129 n_extra = 1;
4130 c_extra = NUL;
4131 }
4132 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004133 if (wp->w_p_list)
4134 c = lcs_eol;
4135 else
4136 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 lcs_eol_one = -1;
4138 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004139 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
4141 extra_attr = hl_attr(HLF_AT);
4142 n_attr = 1;
4143 }
4144#ifdef FEAT_MBYTE
4145 mb_c = c;
4146 if (enc_utf8 && (*mb_char2len)(c) > 1)
4147 {
4148 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004149 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004150 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 else
4153 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4154#endif
4155 }
4156 else if (c != NUL)
4157 {
4158 p_extra = transchar(c);
4159#ifdef FEAT_RIGHTLEFT
4160 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4161 rl_mirror(p_extra); /* reverse "<12>" */
4162#endif
4163 n_extra = byte2cells(c) - 1;
4164 c_extra = NUL;
4165 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004166 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
4168 n_attr = n_extra + 1;
4169 extra_attr = hl_attr(HLF_8);
4170 saved_attr2 = char_attr; /* save current attr */
4171 }
4172#ifdef FEAT_MBYTE
4173 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4174#endif
4175 }
4176#ifdef FEAT_VIRTUALEDIT
4177 else if (VIsual_active
4178 && (VIsual_mode == Ctrl_V
4179 || VIsual_mode == 'v')
4180 && virtual_active()
4181 && tocol != MAXCOL
4182 && vcol < tocol
4183 && (
4184# ifdef FEAT_RIGHTLEFT
4185 wp->w_p_rl ? (col >= 0) :
4186# endif
4187 (col < W_WIDTH(wp))))
4188 {
4189 c = ' ';
4190 --ptr; /* put it back at the NUL */
4191 }
4192#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004193#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 else if ((
4195# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004196 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 ) && (
4200# ifdef FEAT_RIGHTLEFT
4201 wp->w_p_rl ? (col >= 0) :
4202# endif
4203 (col < W_WIDTH(wp))))
4204 {
4205 /* Highlight until the right side of the window */
4206 c = ' ';
4207 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004208
4209 /* Remember we do the char for line highlighting. */
4210 ++did_line_attr;
4211
4212 /* don't do search HL for the rest of the line */
4213 if (line_attr != 0 && char_attr == search_attr && col > 0)
4214 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215# ifdef FEAT_DIFF
4216 if (diff_hlf == HLF_TXD)
4217 {
4218 diff_hlf = HLF_CHD;
4219 if (attr == 0 || char_attr != attr)
4220 char_attr = hl_attr(diff_hlf);
4221 }
4222# endif
4223 }
4224#endif
4225 }
4226 }
4227
4228 /* Don't override visual selection highlighting. */
4229 if (n_attr > 0
4230 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004231 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_attr = extra_attr;
4233
Bram Moolenaar81695252004-12-29 20:58:21 +00004234#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 /* XIM don't send preedit_start and preedit_end, but they send
4236 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4237 * im_is_preediting() here. */
4238 if (xic != NULL
4239 && lnum == curwin->w_cursor.lnum
4240 && (State & INSERT)
4241 && !p_imdisable
4242 && im_is_preediting()
4243 && draw_state == WL_LINE)
4244 {
4245 colnr_T tcol;
4246
4247 if (preedit_end_col == MAXCOL)
4248 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4249 else
4250 tcol = preedit_end_col;
4251 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4252 {
4253 if (feedback_old_attr < 0)
4254 {
4255 feedback_col = 0;
4256 feedback_old_attr = char_attr;
4257 }
4258 char_attr = im_get_feedback_attr(feedback_col);
4259 if (char_attr < 0)
4260 char_attr = feedback_old_attr;
4261 feedback_col++;
4262 }
4263 else if (feedback_old_attr >= 0)
4264 {
4265 char_attr = feedback_old_attr;
4266 feedback_old_attr = -1;
4267 feedback_col = 0;
4268 }
4269 }
4270#endif
4271 /*
4272 * Handle the case where we are in column 0 but not on the first
4273 * character of the line and the user wants us to show us a
4274 * special character (via 'listchars' option "precedes:<char>".
4275 */
4276 if (lcs_prec_todo != NUL
4277 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4278#ifdef FEAT_DIFF
4279 && filler_todo <= 0
4280#endif
4281 && draw_state > WL_NR
4282 && c != NUL)
4283 {
4284 c = lcs_prec;
4285 lcs_prec_todo = NUL;
4286#ifdef FEAT_MBYTE
4287 mb_c = c;
4288 if (enc_utf8 && (*mb_char2len)(c) > 1)
4289 {
4290 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004291 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004292 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 else
4295 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4296#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004297 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
4299 saved_attr3 = char_attr; /* save current attr */
4300 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4301 n_attr3 = 1;
4302 }
4303 }
4304
4305 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004306 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004308 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004309#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004310 || did_line_attr == 1
4311#endif
4312 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004314#ifdef FEAT_SEARCH_EXTRA
4315 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004316
4317 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004318 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004319 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004320#endif
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 /* invert at least one char, used for Visual and empty line or
4323 * highlight match at end of line. If it's beyond the last
4324 * char on the screen, just overwrite that one (tricky!) Not
4325 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004326#ifdef FEAT_SEARCH_EXTRA
4327 prevcol_hl_flag = FALSE;
4328 if (prevcol == (long)search_hl.startcol)
4329 prevcol_hl_flag = TRUE;
4330 else
4331 {
4332 cur = wp->w_match_head;
4333 while (cur != NULL)
4334 {
4335 if (prevcol == (long)cur->hl.startcol)
4336 {
4337 prevcol_hl_flag = TRUE;
4338 break;
4339 }
4340 cur = cur->next;
4341 }
4342 }
4343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004345 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346#ifdef FEAT_SEARCH_EXTRA
4347 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004348 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004349# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004350 && did_line_attr <= 1
4351# endif
4352 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#endif
4354 ))
4355 {
4356 int n = 0;
4357
4358#ifdef FEAT_RIGHTLEFT
4359 if (wp->w_p_rl)
4360 {
4361 if (col < 0)
4362 n = 1;
4363 }
4364 else
4365#endif
4366 {
4367 if (col >= W_WIDTH(wp))
4368 n = -1;
4369 }
4370 if (n != 0)
4371 {
4372 /* At the window boundary, highlight the last character
4373 * instead (better than nothing). */
4374 off += n;
4375 col += n;
4376 }
4377 else
4378 {
4379 /* Add a blank character to highlight. */
4380 ScreenLines[off] = ' ';
4381#ifdef FEAT_MBYTE
4382 if (enc_utf8)
4383 ScreenLinesUC[off] = 0;
4384#endif
4385 }
4386#ifdef FEAT_SEARCH_EXTRA
4387 if (area_attr == 0)
4388 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004389 /* Use attributes from match with highest priority among
4390 * 'search_hl' and the match list. */
4391 char_attr = search_hl.attr;
4392 cur = wp->w_match_head;
4393 shl_flag = FALSE;
4394 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004395 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004396 if (shl_flag == FALSE
4397 && ((cur != NULL
4398 && cur->priority > SEARCH_HL_PRIORITY)
4399 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004400 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004401 shl = &search_hl;
4402 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004403 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004404 else
4405 shl = &cur->hl;
4406 if ((ptr - line) - 1 == (long)shl->startcol)
4407 char_attr = shl->attr;
4408 if (shl != &search_hl && cur != NULL)
4409 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412#endif
4413 ScreenAttrs[off] = char_attr;
4414#ifdef FEAT_RIGHTLEFT
4415 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004418 --off;
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 else
4421#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004424 ++off;
4425 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004426 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004427#ifdef FEAT_SYN_HL
4428 eol_hl_off = 1;
4429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaar91170f82006-05-05 21:15:17 +00004433 /*
4434 * At end of the text line.
4435 */
4436 if (c == NUL)
4437 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004438#ifdef FEAT_SYN_HL
Bram Moolenaara443af82007-11-08 13:51:42 +00004439 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol)
4440 {
4441 /* highlight last char after line */
4442 --col;
4443 --off;
4444 --vcol;
4445 }
4446
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004447 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004448 if (wp->w_p_wrap)
4449 v = wp->w_skipcol;
4450 else
4451 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004452 /* check if line ends before left margin */
4453 if (vcol < v + col - win_col_off(wp))
4454
4455 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004456 if (wp->w_p_cuc
Bram Moolenaara443af82007-11-08 13:51:42 +00004457 && (int)wp->w_virtcol >= vcol - eol_hl_off
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004458 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4459 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004460 && lnum != wp->w_cursor.lnum
4461# ifdef FEAT_RIGHTLEFT
4462 && !wp->w_p_rl
4463# endif
4464 )
4465 {
4466 while (col < W_WIDTH(wp))
4467 {
4468 ScreenLines[off] = ' ';
4469#ifdef FEAT_MBYTE
4470 if (enc_utf8)
4471 ScreenLinesUC[off] = 0;
4472#endif
4473 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004474 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004475 {
4476 ScreenAttrs[off] = hl_attr(HLF_CUC);
4477 break;
4478 }
4479 ScreenAttrs[off++] = 0;
4480 ++vcol;
4481 }
4482 }
4483#endif
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4486 wp->w_p_rl);
4487 row++;
4488
4489 /*
4490 * Update w_cline_height and w_cline_folded if the cursor line was
4491 * updated (saves a call to plines() later).
4492 */
4493 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4494 {
4495 curwin->w_cline_row = startrow;
4496 curwin->w_cline_height = row - startrow;
4497#ifdef FEAT_FOLDING
4498 curwin->w_cline_folded = FALSE;
4499#endif
4500 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4501 }
4502
4503 break;
4504 }
4505
4506 /* line continues beyond line end */
4507 if (lcs_ext
4508 && !wp->w_p_wrap
4509#ifdef FEAT_DIFF
4510 && filler_todo <= 0
4511#endif
4512 && (
4513#ifdef FEAT_RIGHTLEFT
4514 wp->w_p_rl ? col == 0 :
4515#endif
4516 col == W_WIDTH(wp) - 1)
4517 && (*ptr != NUL
4518 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4519 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4520 {
4521 c = lcs_ext;
4522 char_attr = hl_attr(HLF_AT);
4523#ifdef FEAT_MBYTE
4524 mb_c = c;
4525 if (enc_utf8 && (*mb_char2len)(c) > 1)
4526 {
4527 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004528 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004529 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 else
4532 mb_utf8 = FALSE;
4533#endif
4534 }
4535
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004536#ifdef FEAT_SYN_HL
4537 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4538 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004539 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004540 && lnum != wp->w_cursor.lnum
4541 && draw_state == WL_LINE)
4542 {
4543 vcol_save_attr = char_attr;
4544 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4545 }
4546 else
4547 vcol_save_attr = -1;
4548#endif
4549
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 /*
4551 * Store character to be displayed.
4552 * Skip characters that are left of the screen for 'nowrap'.
4553 */
4554 vcol_prev = vcol;
4555 if (draw_state < WL_LINE || n_skip <= 0)
4556 {
4557 /*
4558 * Store the character.
4559 */
4560#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4561 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4562 {
4563 /* A double-wide character is: put first halve in left cell. */
4564 --off;
4565 --col;
4566 }
4567#endif
4568 ScreenLines[off] = c;
4569#ifdef FEAT_MBYTE
4570 if (enc_dbcs == DBCS_JPNU)
4571 ScreenLines2[off] = mb_c & 0xff;
4572 else if (enc_utf8)
4573 {
4574 if (mb_utf8)
4575 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004576 int i;
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004579 if ((c & 0xff) == 0)
4580 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004581 for (i = 0; i < Screen_mco; ++i)
4582 {
4583 ScreenLinesC[i][off] = u8cc[i];
4584 if (u8cc[i] == 0)
4585 break;
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 else
4589 ScreenLinesUC[off] = 0;
4590 }
4591 if (multi_attr)
4592 {
4593 ScreenAttrs[off] = multi_attr;
4594 multi_attr = 0;
4595 }
4596 else
4597#endif
4598 ScreenAttrs[off] = char_attr;
4599
4600#ifdef FEAT_MBYTE
4601 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4602 {
4603 /* Need to fill two screen columns. */
4604 ++off;
4605 ++col;
4606 if (enc_utf8)
4607 /* UTF-8: Put a 0 in the second screen char. */
4608 ScreenLines[off] = 0;
4609 else
4610 /* DBCS: Put second byte in the second screen char. */
4611 ScreenLines[off] = mb_c & 0xff;
4612 ++vcol;
4613 /* When "tocol" is halfway a character, set it to the end of
4614 * the character, otherwise highlighting won't stop. */
4615 if (tocol == vcol)
4616 ++tocol;
4617#ifdef FEAT_RIGHTLEFT
4618 if (wp->w_p_rl)
4619 {
4620 /* now it's time to backup one cell */
4621 --off;
4622 --col;
4623 }
4624#endif
4625 }
4626#endif
4627#ifdef FEAT_RIGHTLEFT
4628 if (wp->w_p_rl)
4629 {
4630 --off;
4631 --col;
4632 }
4633 else
4634#endif
4635 {
4636 ++off;
4637 ++col;
4638 }
4639 }
4640 else
4641 --n_skip;
4642
4643 /* Only advance the "vcol" when after the 'number' column. */
4644 if (draw_state >= WL_SBR
4645#ifdef FEAT_DIFF
4646 && filler_todo <= 0
4647#endif
4648 )
4649 ++vcol;
4650
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004651#ifdef FEAT_SYN_HL
4652 if (vcol_save_attr >= 0)
4653 char_attr = vcol_save_attr;
4654#endif
4655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 /* restore attributes after "predeces" in 'listchars' */
4657 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4658 char_attr = saved_attr3;
4659
4660 /* restore attributes after last 'listchars' or 'number' char */
4661 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4662 char_attr = saved_attr2;
4663
4664 /*
4665 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004666 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 */
4668 if ((
4669#ifdef FEAT_RIGHTLEFT
4670 wp->w_p_rl ? (col < 0) :
4671#endif
4672 (col >= W_WIDTH(wp)))
4673 && (*ptr != NUL
4674#ifdef FEAT_DIFF
4675 || filler_todo > 0
4676#endif
4677 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4678 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4679 )
4680 {
4681 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4682 wp->w_p_rl);
4683 ++row;
4684 ++screen_row;
4685
4686 /* When not wrapping and finished diff lines, or when displayed
4687 * '$' and highlighting until last column, break here. */
4688 if ((!wp->w_p_wrap
4689#ifdef FEAT_DIFF
4690 && filler_todo <= 0
4691#endif
4692 ) || lcs_eol_one == -1)
4693 break;
4694
4695 /* When the window is too narrow draw all "@" lines. */
4696 if (draw_state != WL_LINE
4697#ifdef FEAT_DIFF
4698 && filler_todo <= 0
4699#endif
4700 )
4701 {
4702 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4703#ifdef FEAT_VERTSPLIT
4704 draw_vsep_win(wp, row);
4705#endif
4706 row = endrow;
4707 }
4708
4709 /* When line got too long for screen break here. */
4710 if (row == endrow)
4711 {
4712 ++row;
4713 break;
4714 }
4715
4716 if (screen_cur_row == screen_row - 1
4717#ifdef FEAT_DIFF
4718 && filler_todo <= 0
4719#endif
4720 && W_WIDTH(wp) == Columns)
4721 {
4722 /* Remember that the line wraps, used for modeless copy. */
4723 LineWraps[screen_row - 1] = TRUE;
4724
4725 /*
4726 * Special trick to make copy/paste of wrapped lines work with
4727 * xterm/screen: write an extra character beyond the end of
4728 * the line. This will work with all terminal types
4729 * (regardless of the xn,am settings).
4730 * Only do this on a fast tty.
4731 * Only do this if the cursor is on the current line
4732 * (something has been written in it).
4733 * Don't do this for the GUI.
4734 * Don't do this for double-width characters.
4735 * Don't do this for a window not at the right screen border.
4736 */
4737 if (p_tf
4738#ifdef FEAT_GUI
4739 && !gui.in_use
4740#endif
4741#ifdef FEAT_MBYTE
4742 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004743 && ((*mb_off2cells)(LineOffset[screen_row],
4744 LineOffset[screen_row] + screen_Columns)
4745 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004747 + (int)Columns - 2,
4748 LineOffset[screen_row] + screen_Columns)
4749 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750#endif
4751 )
4752 {
4753 /* First make sure we are at the end of the screen line,
4754 * then output the same character again to let the
4755 * terminal know about the wrap. If the terminal doesn't
4756 * auto-wrap, we overwrite the character. */
4757 if (screen_cur_col != W_WIDTH(wp))
4758 screen_char(LineOffset[screen_row - 1]
4759 + (unsigned)Columns - 1,
4760 screen_row - 1, (int)(Columns - 1));
4761
4762#ifdef FEAT_MBYTE
4763 /* When there is a multi-byte character, just output a
4764 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004765 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4766 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 out_char(' ');
4768 else
4769#endif
4770 out_char(ScreenLines[LineOffset[screen_row - 1]
4771 + (Columns - 1)]);
4772 /* force a redraw of the first char on the next line */
4773 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4774 screen_start(); /* don't know where cursor is now */
4775 }
4776 }
4777
4778 col = 0;
4779 off = (unsigned)(current_ScreenLine - ScreenLines);
4780#ifdef FEAT_RIGHTLEFT
4781 if (wp->w_p_rl)
4782 {
4783 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4784 off += col;
4785 }
4786#endif
4787
4788 /* reset the drawing state for the start of a wrapped line */
4789 draw_state = WL_START;
4790 saved_n_extra = n_extra;
4791 saved_p_extra = p_extra;
4792 saved_c_extra = c_extra;
4793 saved_char_attr = char_attr;
4794 n_extra = 0;
4795 lcs_prec_todo = lcs_prec;
4796#ifdef FEAT_LINEBREAK
4797# ifdef FEAT_DIFF
4798 if (filler_todo <= 0)
4799# endif
4800 need_showbreak = TRUE;
4801#endif
4802#ifdef FEAT_DIFF
4803 --filler_todo;
4804 /* When the filler lines are actually below the last line of the
4805 * file, don't draw the line itself, break here. */
4806 if (filler_todo == 0 && wp->w_botfill)
4807 break;
4808#endif
4809 }
4810
4811 } /* for every character in the line */
4812
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004813#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004814 /* After an empty line check first word for capital. */
4815 if (*skipwhite(line) == NUL)
4816 {
4817 capcol_lnum = lnum + 1;
4818 cap_col = 0;
4819 }
4820#endif
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return row;
4823}
4824
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004825#ifdef FEAT_MBYTE
4826static int comp_char_differs __ARGS((int, int));
4827
4828/*
4829 * Return if the composing characters at "off_from" and "off_to" differ.
4830 */
4831 static int
4832comp_char_differs(off_from, off_to)
4833 int off_from;
4834 int off_to;
4835{
4836 int i;
4837
4838 for (i = 0; i < Screen_mco; ++i)
4839 {
4840 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4841 return TRUE;
4842 if (ScreenLinesC[i][off_from] == 0)
4843 break;
4844 }
4845 return FALSE;
4846}
4847#endif
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849/*
4850 * Check whether the given character needs redrawing:
4851 * - the (first byte of the) character is different
4852 * - the attributes are different
4853 * - the character is multi-byte and the next byte is different
4854 */
4855 static int
4856char_needs_redraw(off_from, off_to, cols)
4857 int off_from;
4858 int off_to;
4859 int cols;
4860{
4861 if (cols > 0
4862 && ((ScreenLines[off_from] != ScreenLines[off_to]
4863 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4864
4865#ifdef FEAT_MBYTE
4866 || (enc_dbcs != 0
4867 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4868 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4869 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4870 : (cols > 1 && ScreenLines[off_from + 1]
4871 != ScreenLines[off_to + 1])))
4872 || (enc_utf8
4873 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4874 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004875 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876#endif
4877 ))
4878 return TRUE;
4879 return FALSE;
4880}
4881
4882/*
4883 * Move one "cooked" screen line to the screen, but only the characters that
4884 * have actually changed. Handle insert/delete character.
4885 * "coloff" gives the first column on the screen for this line.
4886 * "endcol" gives the columns where valid characters are.
4887 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4888 * needs to be cleared, negative otherwise.
4889 * "rlflag" is TRUE in a rightleft window:
4890 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4891 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4892 */
4893 static void
4894screen_line(row, coloff, endcol, clear_width
4895#ifdef FEAT_RIGHTLEFT
4896 , rlflag
4897#endif
4898 )
4899 int row;
4900 int coloff;
4901 int endcol;
4902 int clear_width;
4903#ifdef FEAT_RIGHTLEFT
4904 int rlflag;
4905#endif
4906{
4907 unsigned off_from;
4908 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004909#ifdef FEAT_MBYTE
4910 unsigned max_off_from;
4911 unsigned max_off_to;
4912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 int col = 0;
4914#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4915 int hl;
4916#endif
4917 int force = FALSE; /* force update rest of the line */
4918 int redraw_this /* bool: does character need redraw? */
4919#ifdef FEAT_GUI
4920 = TRUE /* For GUI when while-loop empty */
4921#endif
4922 ;
4923 int redraw_next; /* redraw_this for next character */
4924#ifdef FEAT_MBYTE
4925 int clear_next = FALSE;
4926 int char_cells; /* 1: normal char */
4927 /* 2: occupies two display cells */
4928# define CHAR_CELLS char_cells
4929#else
4930# define CHAR_CELLS 1
4931#endif
4932
4933# ifdef FEAT_CLIPBOARD
4934 clip_may_clear_selection(row, row);
4935# endif
4936
4937 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4938 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004939#ifdef FEAT_MBYTE
4940 max_off_from = off_from + screen_Columns;
4941 max_off_to = LineOffset[row] + screen_Columns;
4942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944#ifdef FEAT_RIGHTLEFT
4945 if (rlflag)
4946 {
4947 /* Clear rest first, because it's left of the text. */
4948 if (clear_width > 0)
4949 {
4950 while (col <= endcol && ScreenLines[off_to] == ' '
4951 && ScreenAttrs[off_to] == 0
4952# ifdef FEAT_MBYTE
4953 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4954# endif
4955 )
4956 {
4957 ++off_to;
4958 ++col;
4959 }
4960 if (col <= endcol)
4961 screen_fill(row, row + 1, col + coloff,
4962 endcol + coloff + 1, ' ', ' ', 0);
4963 }
4964 col = endcol + 1;
4965 off_to = LineOffset[row] + col + coloff;
4966 off_from += col;
4967 endcol = (clear_width > 0 ? clear_width : -clear_width);
4968 }
4969#endif /* FEAT_RIGHTLEFT */
4970
4971 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4972
4973 while (col < endcol)
4974 {
4975#ifdef FEAT_MBYTE
4976 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00004977 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 else
4979 char_cells = 1;
4980#endif
4981
4982 redraw_this = redraw_next;
4983 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4984 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4985
4986#ifdef FEAT_GUI
4987 /* If the next character was bold, then redraw the current character to
4988 * remove any pixels that might have spilt over into us. This only
4989 * happens in the GUI.
4990 */
4991 if (redraw_next && gui.in_use)
4992 {
4993 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004994 if (hl > HL_ALL)
4995 hl = syn_attr2attr(hl);
4996 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 redraw_this = TRUE;
4998 }
4999#endif
5000
5001 if (redraw_this)
5002 {
5003 /*
5004 * Special handling when 'xs' termcap flag set (hpterm):
5005 * Attributes for characters are stored at the position where the
5006 * cursor is when writing the highlighting code. The
5007 * start-highlighting code must be written with the cursor on the
5008 * first highlighted character. The stop-highlighting code must
5009 * be written with the cursor just after the last highlighted
5010 * character.
5011 * Overwriting a character doesn't remove it's highlighting. Need
5012 * to clear the rest of the line, and force redrawing it
5013 * completely.
5014 */
5015 if ( p_wiv
5016 && !force
5017#ifdef FEAT_GUI
5018 && !gui.in_use
5019#endif
5020 && ScreenAttrs[off_to] != 0
5021 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5022 {
5023 /*
5024 * Need to remove highlighting attributes here.
5025 */
5026 windgoto(row, col + coloff);
5027 out_str(T_CE); /* clear rest of this screen line */
5028 screen_start(); /* don't know where cursor is now */
5029 force = TRUE; /* force redraw of rest of the line */
5030 redraw_next = TRUE; /* or else next char would miss out */
5031
5032 /*
5033 * If the previous character was highlighted, need to stop
5034 * highlighting at this character.
5035 */
5036 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5037 {
5038 screen_attr = ScreenAttrs[off_to - 1];
5039 term_windgoto(row, col + coloff);
5040 screen_stop_highlight();
5041 }
5042 else
5043 screen_attr = 0; /* highlighting has stopped */
5044 }
5045#ifdef FEAT_MBYTE
5046 if (enc_dbcs != 0)
5047 {
5048 /* Check if overwriting a double-byte with a single-byte or
5049 * the other way around requires another character to be
5050 * redrawn. For UTF-8 this isn't needed, because comparing
5051 * ScreenLinesUC[] is sufficient. */
5052 if (char_cells == 1
5053 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005054 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 /* Writing a single-cell character over a double-cell
5057 * character: need to redraw the next cell. */
5058 ScreenLines[off_to + 1] = 0;
5059 redraw_next = TRUE;
5060 }
5061 else if (char_cells == 2
5062 && col + 2 < endcol
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 {
5066 /* Writing the second half of a double-cell character over
5067 * a double-cell character: need to redraw the second
5068 * cell. */
5069 ScreenLines[off_to + 2] = 0;
5070 redraw_next = TRUE;
5071 }
5072
5073 if (enc_dbcs == DBCS_JPNU)
5074 ScreenLines2[off_to] = ScreenLines2[off_from];
5075 }
5076 /* When writing a single-width character over a double-width
5077 * character and at the end of the redrawn text, need to clear out
5078 * the right halve of the old character.
5079 * Also required when writing the right halve of a double-width
5080 * char over the left halve of an existing one. */
5081 if (has_mbyte && col + char_cells == endcol
5082 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005083 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005085 && (*mb_off2cells)(off_to, max_off_to) == 1
5086 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 clear_next = TRUE;
5088#endif
5089
5090 ScreenLines[off_to] = ScreenLines[off_from];
5091#ifdef FEAT_MBYTE
5092 if (enc_utf8)
5093 {
5094 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5095 if (ScreenLinesUC[off_from] != 0)
5096 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005097 int i;
5098
5099 for (i = 0; i < Screen_mco; ++i)
5100 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102 }
5103 if (char_cells == 2)
5104 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5105#endif
5106
5107#if defined(FEAT_GUI) || defined(UNIX)
5108 /* The bold trick makes a single row of pixels appear in the next
5109 * character. When a bold character is removed, the next
5110 * character should be redrawn too. This happens for our own GUI
5111 * and for some xterms. */
5112 if (
5113# ifdef FEAT_GUI
5114 gui.in_use
5115# endif
5116# if defined(FEAT_GUI) && defined(UNIX)
5117 ||
5118# endif
5119# ifdef UNIX
5120 term_is_xterm
5121# endif
5122 )
5123 {
5124 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005125 if (hl > HL_ALL)
5126 hl = syn_attr2attr(hl);
5127 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 redraw_next = TRUE;
5129 }
5130#endif
5131 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5132#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005133 /* For simplicity set the attributes of second half of a
5134 * double-wide character equal to the first half. */
5135 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005137
5138 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 else
5141#endif
5142 screen_char(off_to, row, col + coloff);
5143 }
5144 else if ( p_wiv
5145#ifdef FEAT_GUI
5146 && !gui.in_use
5147#endif
5148 && col + coloff > 0)
5149 {
5150 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5151 {
5152 /*
5153 * Don't output stop-highlight when moving the cursor, it will
5154 * stop the highlighting when it should continue.
5155 */
5156 screen_attr = 0;
5157 }
5158 else if (screen_attr != 0)
5159 screen_stop_highlight();
5160 }
5161
5162 off_to += CHAR_CELLS;
5163 off_from += CHAR_CELLS;
5164 col += CHAR_CELLS;
5165 }
5166
5167#ifdef FEAT_MBYTE
5168 if (clear_next)
5169 {
5170 /* Clear the second half of a double-wide character of which the left
5171 * half was overwritten with a single-wide character. */
5172 ScreenLines[off_to] = ' ';
5173 if (enc_utf8)
5174 ScreenLinesUC[off_to] = 0;
5175 screen_char(off_to, row, col + coloff);
5176 }
5177#endif
5178
5179 if (clear_width > 0
5180#ifdef FEAT_RIGHTLEFT
5181 && !rlflag
5182#endif
5183 )
5184 {
5185#ifdef FEAT_GUI
5186 int startCol = col;
5187#endif
5188
5189 /* blank out the rest of the line */
5190 while (col < clear_width && ScreenLines[off_to] == ' '
5191 && ScreenAttrs[off_to] == 0
5192#ifdef FEAT_MBYTE
5193 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5194#endif
5195 )
5196 {
5197 ++off_to;
5198 ++col;
5199 }
5200 if (col < clear_width)
5201 {
5202#ifdef FEAT_GUI
5203 /*
5204 * In the GUI, clearing the rest of the line may leave pixels
5205 * behind if the first character cleared was bold. Some bold
5206 * fonts spill over the left. In this case we redraw the previous
5207 * character too. If we didn't skip any blanks above, then we
5208 * only redraw if the character wasn't already redrawn anyway.
5209 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005210 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
5212 hl = ScreenAttrs[off_to];
5213 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005214 {
5215 int prev_cells = 1;
5216# ifdef FEAT_MBYTE
5217 if (enc_utf8)
5218 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5219 * that its width is 2. */
5220 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5221 else if (enc_dbcs != 0)
5222 {
5223 /* find previous character by counting from first
5224 * column and get its width. */
5225 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005226 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005227
5228 while (off < off_to)
5229 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005230 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005231 off += prev_cells;
5232 }
5233 }
5234
5235 if (enc_dbcs != 0 && prev_cells > 1)
5236 screen_char_2(off_to - prev_cells, row,
5237 col + coloff - prev_cells);
5238 else
5239# endif
5240 screen_char(off_to - prev_cells, row,
5241 col + coloff - prev_cells);
5242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244#endif
5245 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5246 ' ', ' ', 0);
5247#ifdef FEAT_VERTSPLIT
5248 off_to += clear_width - col;
5249 col = clear_width;
5250#endif
5251 }
5252 }
5253
5254 if (clear_width > 0)
5255 {
5256#ifdef FEAT_VERTSPLIT
5257 /* For a window that's left of another, draw the separator char. */
5258 if (col + coloff < Columns)
5259 {
5260 int c;
5261
5262 c = fillchar_vsep(&hl);
5263 if (ScreenLines[off_to] != c
5264# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005265 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5266 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267# endif
5268 || ScreenAttrs[off_to] != hl)
5269 {
5270 ScreenLines[off_to] = c;
5271 ScreenAttrs[off_to] = hl;
5272# ifdef FEAT_MBYTE
5273 if (enc_utf8)
5274 {
5275 if (c >= 0x80)
5276 {
5277 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005278 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280 else
5281 ScreenLinesUC[off_to] = 0;
5282 }
5283# endif
5284 screen_char(off_to, row, col + coloff);
5285 }
5286 }
5287 else
5288#endif
5289 LineWraps[row] = FALSE;
5290 }
5291}
5292
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005293#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005295 * Mirror text "str" for right-left displaying.
5296 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005298 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299rl_mirror(str)
5300 char_u *str;
5301{
5302 char_u *p1, *p2;
5303 int t;
5304
5305 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5306 {
5307 t = *p1;
5308 *p1 = *p2;
5309 *p2 = t;
5310 }
5311}
5312#endif
5313
5314#if defined(FEAT_WINDOWS) || defined(PROTO)
5315/*
5316 * mark all status lines for redraw; used after first :cd
5317 */
5318 void
5319status_redraw_all()
5320{
5321 win_T *wp;
5322
5323 for (wp = firstwin; wp; wp = wp->w_next)
5324 if (wp->w_status_height)
5325 {
5326 wp->w_redr_status = TRUE;
5327 redraw_later(VALID);
5328 }
5329}
5330
5331/*
5332 * mark all status lines of the current buffer for redraw
5333 */
5334 void
5335status_redraw_curbuf()
5336{
5337 win_T *wp;
5338
5339 for (wp = firstwin; wp; wp = wp->w_next)
5340 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5341 {
5342 wp->w_redr_status = TRUE;
5343 redraw_later(VALID);
5344 }
5345}
5346
5347/*
5348 * Redraw all status lines that need to be redrawn.
5349 */
5350 void
5351redraw_statuslines()
5352{
5353 win_T *wp;
5354
5355 for (wp = firstwin; wp; wp = wp->w_next)
5356 if (wp->w_redr_status)
5357 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005358 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005359 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360}
5361#endif
5362
5363#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5364/*
5365 * Redraw all status lines at the bottom of frame "frp".
5366 */
5367 void
5368win_redraw_last_status(frp)
5369 frame_T *frp;
5370{
5371 if (frp->fr_layout == FR_LEAF)
5372 frp->fr_win->w_redr_status = TRUE;
5373 else if (frp->fr_layout == FR_ROW)
5374 {
5375 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5376 win_redraw_last_status(frp);
5377 }
5378 else /* frp->fr_layout == FR_COL */
5379 {
5380 frp = frp->fr_child;
5381 while (frp->fr_next != NULL)
5382 frp = frp->fr_next;
5383 win_redraw_last_status(frp);
5384 }
5385}
5386#endif
5387
5388#ifdef FEAT_VERTSPLIT
5389/*
5390 * Draw the verticap separator right of window "wp" starting with line "row".
5391 */
5392 static void
5393draw_vsep_win(wp, row)
5394 win_T *wp;
5395 int row;
5396{
5397 int hl;
5398 int c;
5399
5400 if (wp->w_vsep_width)
5401 {
5402 /* draw the vertical separator right of this window */
5403 c = fillchar_vsep(&hl);
5404 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5405 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5406 c, ' ', hl);
5407 }
5408}
5409#endif
5410
5411#ifdef FEAT_WILDMENU
5412static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005413static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005416 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 */
5418 static int
5419status_match_len(xp, s)
5420 expand_T *xp;
5421 char_u *s;
5422{
5423 int len = 0;
5424
5425#ifdef FEAT_MENU
5426 int emenu = (xp->xp_context == EXPAND_MENUS
5427 || xp->xp_context == EXPAND_MENUNAMES);
5428
5429 /* Check for menu separators - replace with '|'. */
5430 if (emenu && menu_is_separator(s))
5431 return 1;
5432#endif
5433
5434 while (*s != NUL)
5435 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005436 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005438 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005439 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
5441
5442 return len;
5443}
5444
5445/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005446 * Return TRUE for characters that are not displayed in a status match.
5447 * These are backslashes used for escaping. Do show backslashes in help tags.
5448 */
5449 static int
5450skip_status_match_char(xp, s)
5451 expand_T *xp;
5452 char_u *s;
5453{
5454 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5455#ifdef FEAT_MENU
5456 || ((xp->xp_context == EXPAND_MENUS
5457 || xp->xp_context == EXPAND_MENUNAMES)
5458 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5459#endif
5460 );
5461}
5462
5463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 * Show wildchar matches in the status line.
5465 * Show at least the "match" item.
5466 * We start at item 'first_match' in the list and show all matches that fit.
5467 *
5468 * If inversion is possible we use it. Else '=' characters are used.
5469 */
5470 void
5471win_redr_status_matches(xp, num_matches, matches, match, showtail)
5472 expand_T *xp;
5473 int num_matches;
5474 char_u **matches; /* list of matches */
5475 int match;
5476 int showtail;
5477{
5478#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5479 int row;
5480 char_u *buf;
5481 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005482 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 int fillchar;
5484 int attr;
5485 int i;
5486 int highlight = TRUE;
5487 char_u *selstart = NULL;
5488 int selstart_col = 0;
5489 char_u *selend = NULL;
5490 static int first_match = 0;
5491 int add_left = FALSE;
5492 char_u *s;
5493#ifdef FEAT_MENU
5494 int emenu;
5495#endif
5496#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5497 int l;
5498#endif
5499
5500 if (matches == NULL) /* interrupted completion? */
5501 return;
5502
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005503#ifdef FEAT_MBYTE
5504 if (has_mbyte)
5505 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5506 else
5507#endif
5508 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (buf == NULL)
5510 return;
5511
5512 if (match == -1) /* don't show match but original text */
5513 {
5514 match = 0;
5515 highlight = FALSE;
5516 }
5517 /* count 1 for the ending ">" */
5518 clen = status_match_len(xp, L_MATCH(match)) + 3;
5519 if (match == 0)
5520 first_match = 0;
5521 else if (match < first_match)
5522 {
5523 /* jumping left, as far as we can go */
5524 first_match = match;
5525 add_left = TRUE;
5526 }
5527 else
5528 {
5529 /* check if match fits on the screen */
5530 for (i = first_match; i < match; ++i)
5531 clen += status_match_len(xp, L_MATCH(i)) + 2;
5532 if (first_match > 0)
5533 clen += 2;
5534 /* jumping right, put match at the left */
5535 if ((long)clen > Columns)
5536 {
5537 first_match = match;
5538 /* if showing the last match, we can add some on the left */
5539 clen = 2;
5540 for (i = match; i < num_matches; ++i)
5541 {
5542 clen += status_match_len(xp, L_MATCH(i)) + 2;
5543 if ((long)clen >= Columns)
5544 break;
5545 }
5546 if (i == num_matches)
5547 add_left = TRUE;
5548 }
5549 }
5550 if (add_left)
5551 while (first_match > 0)
5552 {
5553 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5554 if ((long)clen >= Columns)
5555 break;
5556 --first_match;
5557 }
5558
5559 fillchar = fillchar_status(&attr, TRUE);
5560
5561 if (first_match == 0)
5562 {
5563 *buf = NUL;
5564 len = 0;
5565 }
5566 else
5567 {
5568 STRCPY(buf, "< ");
5569 len = 2;
5570 }
5571 clen = len;
5572
5573 i = first_match;
5574 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5575 {
5576 if (i == match)
5577 {
5578 selstart = buf + len;
5579 selstart_col = clen;
5580 }
5581
5582 s = L_MATCH(i);
5583 /* Check for menu separators - replace with '|' */
5584#ifdef FEAT_MENU
5585 emenu = (xp->xp_context == EXPAND_MENUS
5586 || xp->xp_context == EXPAND_MENUNAMES);
5587 if (emenu && menu_is_separator(s))
5588 {
5589 STRCPY(buf + len, transchar('|'));
5590 l = (int)STRLEN(buf + len);
5591 len += l;
5592 clen += l;
5593 }
5594 else
5595#endif
5596 for ( ; *s != NUL; ++s)
5597 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005598 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 ++s;
5600 clen += ptr2cells(s);
5601#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005602 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
5604 STRNCPY(buf + len, s, l);
5605 s += l - 1;
5606 len += l;
5607 }
5608 else
5609#endif
5610 {
5611 STRCPY(buf + len, transchar_byte(*s));
5612 len += (int)STRLEN(buf + len);
5613 }
5614 }
5615 if (i == match)
5616 selend = buf + len;
5617
5618 *(buf + len++) = ' ';
5619 *(buf + len++) = ' ';
5620 clen += 2;
5621 if (++i == num_matches)
5622 break;
5623 }
5624
5625 if (i != num_matches)
5626 {
5627 *(buf + len++) = '>';
5628 ++clen;
5629 }
5630
5631 buf[len] = NUL;
5632
5633 row = cmdline_row - 1;
5634 if (row >= 0)
5635 {
5636 if (wild_menu_showing == 0)
5637 {
5638 if (msg_scrolled > 0)
5639 {
5640 /* Put the wildmenu just above the command line. If there is
5641 * no room, scroll the screen one line up. */
5642 if (cmdline_row == Rows - 1)
5643 {
5644 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5645 ++msg_scrolled;
5646 }
5647 else
5648 {
5649 ++cmdline_row;
5650 ++row;
5651 }
5652 wild_menu_showing = WM_SCROLLED;
5653 }
5654 else
5655 {
5656 /* Create status line if needed by setting 'laststatus' to 2.
5657 * Set 'winminheight' to zero to avoid that the window is
5658 * resized. */
5659 if (lastwin->w_status_height == 0)
5660 {
5661 save_p_ls = p_ls;
5662 save_p_wmh = p_wmh;
5663 p_ls = 2;
5664 p_wmh = 0;
5665 last_status(FALSE);
5666 }
5667 wild_menu_showing = WM_SHOWN;
5668 }
5669 }
5670
5671 screen_puts(buf, row, 0, attr);
5672 if (selstart != NULL && highlight)
5673 {
5674 *selend = NUL;
5675 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5676 }
5677
5678 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5679 }
5680
5681#ifdef FEAT_VERTSPLIT
5682 win_redraw_last_status(topframe);
5683#else
5684 lastwin->w_redr_status = TRUE;
5685#endif
5686 vim_free(buf);
5687}
5688#endif
5689
5690#if defined(FEAT_WINDOWS) || defined(PROTO)
5691/*
5692 * Redraw the status line of window wp.
5693 *
5694 * If inversion is possible we use it. Else '=' characters are used.
5695 */
5696 void
5697win_redr_status(wp)
5698 win_T *wp;
5699{
5700 int row;
5701 char_u *p;
5702 int len;
5703 int fillchar;
5704 int attr;
5705 int this_ru_col;
5706
5707 wp->w_redr_status = FALSE;
5708 if (wp->w_status_height == 0)
5709 {
5710 /* no status line, can only be last window */
5711 redraw_cmdline = TRUE;
5712 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005713 else if (!redrawing()
5714#ifdef FEAT_INS_EXPAND
5715 /* don't update status line when popup menu is visible and may be
5716 * drawn over it */
5717 || pum_visible()
5718#endif
5719 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 /* Don't redraw right now, do it later. */
5722 wp->w_redr_status = TRUE;
5723 }
5724#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005725 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 {
5727 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005728 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730#endif
5731 else
5732 {
5733 fillchar = fillchar_status(&attr, wp == curwin);
5734
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005735 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 p = NameBuff;
5737 len = (int)STRLEN(p);
5738
5739 if (wp->w_buffer->b_help
5740#ifdef FEAT_QUICKFIX
5741 || wp->w_p_pvw
5742#endif
5743 || bufIsChanged(wp->w_buffer)
5744 || wp->w_buffer->b_p_ro)
5745 *(p + len++) = ' ';
5746 if (wp->w_buffer->b_help)
5747 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005748 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 len += (int)STRLEN(p + len);
5750 }
5751#ifdef FEAT_QUICKFIX
5752 if (wp->w_p_pvw)
5753 {
5754 STRCPY(p + len, _("[Preview]"));
5755 len += (int)STRLEN(p + len);
5756 }
5757#endif
5758 if (bufIsChanged(wp->w_buffer))
5759 {
5760 STRCPY(p + len, "[+]");
5761 len += 3;
5762 }
5763 if (wp->w_buffer->b_p_ro)
5764 {
5765 STRCPY(p + len, "[RO]");
5766 len += 4;
5767 }
5768
5769#ifndef FEAT_VERTSPLIT
5770 this_ru_col = ru_col;
5771 if (this_ru_col < (Columns + 1) / 2)
5772 this_ru_col = (Columns + 1) / 2;
5773#else
5774 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5775 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5776 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5777 if (this_ru_col <= 1)
5778 {
5779 p = (char_u *)"<"; /* No room for file name! */
5780 len = 1;
5781 }
5782 else
5783#endif
5784#ifdef FEAT_MBYTE
5785 if (has_mbyte)
5786 {
5787 int clen = 0, i;
5788
5789 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005790 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 clen += (*mb_ptr2cells)(p + i);
5792 /* Find first character that will fit.
5793 * Going from start to end is much faster for DBCS. */
5794 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005795 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 clen -= (*mb_ptr2cells)(p + i);
5797 len = clen;
5798 if (i > 0)
5799 {
5800 p = p + i - 1;
5801 *p = '<';
5802 ++len;
5803 }
5804
5805 }
5806 else
5807#endif
5808 if (len > this_ru_col - 1)
5809 {
5810 p += len - (this_ru_col - 1);
5811 *p = '<';
5812 len = this_ru_col - 1;
5813 }
5814
5815 row = W_WINROW(wp) + wp->w_height;
5816 screen_puts(p, row, W_WINCOL(wp), attr);
5817 screen_fill(row, row + 1, len + W_WINCOL(wp),
5818 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5819
5820 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5821 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5822 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5823 - 1 + W_WINCOL(wp)), attr);
5824
5825#ifdef FEAT_CMDL_INFO
5826 win_redr_ruler(wp, TRUE);
5827#endif
5828 }
5829
5830#ifdef FEAT_VERTSPLIT
5831 /*
5832 * May need to draw the character below the vertical separator.
5833 */
5834 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5835 {
5836 if (stl_connected(wp))
5837 fillchar = fillchar_status(&attr, wp == curwin);
5838 else
5839 fillchar = fillchar_vsep(&attr);
5840 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5841 attr);
5842 }
5843#endif
5844}
5845
Bram Moolenaar238a5642006-02-21 22:12:05 +00005846#ifdef FEAT_STL_OPT
5847/*
5848 * Redraw the status line according to 'statusline' and take care of any
5849 * errors encountered.
5850 */
5851 static void
5852redraw_custum_statusline(wp)
5853 win_T *wp;
5854{
5855 int save_called_emsg = called_emsg;
5856
5857 called_emsg = FALSE;
5858 win_redr_custom(wp, FALSE);
5859 if (called_emsg)
5860 set_string_option_direct((char_u *)"statusline", -1,
5861 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005862 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005863 called_emsg |= save_called_emsg;
5864}
5865#endif
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867# ifdef FEAT_VERTSPLIT
5868/*
5869 * Return TRUE if the status line of window "wp" is connected to the status
5870 * line of the window right of it. If not, then it's a vertical separator.
5871 * Only call if (wp->w_vsep_width != 0).
5872 */
5873 int
5874stl_connected(wp)
5875 win_T *wp;
5876{
5877 frame_T *fr;
5878
5879 fr = wp->w_frame;
5880 while (fr->fr_parent != NULL)
5881 {
5882 if (fr->fr_parent->fr_layout == FR_COL)
5883 {
5884 if (fr->fr_next != NULL)
5885 break;
5886 }
5887 else
5888 {
5889 if (fr->fr_next != NULL)
5890 return TRUE;
5891 }
5892 fr = fr->fr_parent;
5893 }
5894 return FALSE;
5895}
5896# endif
5897
5898#endif /* FEAT_WINDOWS */
5899
5900#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5901/*
5902 * Get the value to show for the language mappings, active 'keymap'.
5903 */
5904 int
5905get_keymap_str(wp, buf, len)
5906 win_T *wp;
5907 char_u *buf; /* buffer for the result */
5908 int len; /* length of buffer */
5909{
5910 char_u *p;
5911
5912 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5913 return FALSE;
5914
5915 {
5916#ifdef FEAT_EVAL
5917 buf_T *old_curbuf = curbuf;
5918 win_T *old_curwin = curwin;
5919 char_u *s;
5920
5921 curbuf = wp->w_buffer;
5922 curwin = wp;
5923 STRCPY(buf, "b:keymap_name"); /* must be writable */
5924 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005925 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 --emsg_skip;
5927 curbuf = old_curbuf;
5928 curwin = old_curwin;
5929 if (p == NULL || *p == NUL)
5930#endif
5931 {
5932#ifdef FEAT_KEYMAP
5933 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5934 p = wp->w_buffer->b_p_keymap;
5935 else
5936#endif
5937 p = (char_u *)"lang";
5938 }
5939 if ((int)(STRLEN(p) + 3) < len)
5940 sprintf((char *)buf, "<%s>", p);
5941 else
5942 buf[0] = NUL;
5943#ifdef FEAT_EVAL
5944 vim_free(s);
5945#endif
5946 }
5947 return buf[0] != NUL;
5948}
5949#endif
5950
5951#if defined(FEAT_STL_OPT) || defined(PROTO)
5952/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005953 * Redraw the status line or ruler of window "wp".
5954 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 */
5956 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005957win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005959 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
5961 int attr;
5962 int curattr;
5963 int row;
5964 int col = 0;
5965 int maxwidth;
5966 int width;
5967 int n;
5968 int len;
5969 int fillchar;
5970 char_u buf[MAXPATHL];
5971 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005972 struct stl_hlrec hltab[STL_MAX_ITEM];
5973 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005974 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
5976 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005977 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005979 /* Use 'tabline'. Always at the first line of the screen. */
5980 p = p_tal;
5981 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005982 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005983 attr = hl_attr(HLF_TPF);
5984 maxwidth = Columns;
5985# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005986 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005989 else
5990 {
5991 row = W_WINROW(wp) + wp->w_height;
5992 fillchar = fillchar_status(&attr, wp == curwin);
5993 maxwidth = W_WIDTH(wp);
5994
5995 if (draw_ruler)
5996 {
5997 p = p_ruf;
5998 /* advance past any leading group spec - implicit in ru_col */
5999 if (*p == '%')
6000 {
6001 if (*++p == '-')
6002 p++;
6003 if (atoi((char *) p))
6004 while (VIM_ISDIGIT(*p))
6005 p++;
6006 if (*p++ != '(')
6007 p = p_ruf;
6008 }
6009#ifdef FEAT_VERTSPLIT
6010 col = ru_col - (Columns - W_WIDTH(wp));
6011 if (col < (W_WIDTH(wp) + 1) / 2)
6012 col = (W_WIDTH(wp) + 1) / 2;
6013#else
6014 col = ru_col;
6015 if (col > (Columns + 1) / 2)
6016 col = (Columns + 1) / 2;
6017#endif
6018 maxwidth = W_WIDTH(wp) - col;
6019#ifdef FEAT_WINDOWS
6020 if (!wp->w_status_height)
6021#endif
6022 {
6023 row = Rows - 1;
6024 --maxwidth; /* writing in last column may cause scrolling */
6025 fillchar = ' ';
6026 attr = 0;
6027 }
6028
6029# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006030 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006031# endif
6032 }
6033 else
6034 {
6035 if (*wp->w_p_stl != NUL)
6036 p = wp->w_p_stl;
6037 else
6038 p = p_stl;
6039# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006040 use_sandbox = was_set_insecurely((char_u *)"statusline",
6041 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006042# endif
6043 }
6044
6045#ifdef FEAT_VERTSPLIT
6046 col += W_WINCOL(wp);
6047#endif
6048 }
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (maxwidth <= 0)
6051 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006053 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6054 buf, sizeof(buf),
6055 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006056 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006057 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
6059 while (width < maxwidth && len < sizeof(buf) - 1)
6060 {
6061#ifdef FEAT_MBYTE
6062 len += (*mb_char2bytes)(fillchar, buf + len);
6063#else
6064 buf[len++] = fillchar;
6065#endif
6066 ++width;
6067 }
6068 buf[len] = NUL;
6069
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006070 /*
6071 * Draw each snippet with the specified highlighting.
6072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 curattr = attr;
6074 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006075 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006077 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 screen_puts_len(p, len, row, col, curattr);
6079 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006080 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006082 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006084 else if (hltab[n].userhl < 0)
6085 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006087 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006088 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089#endif
6090 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006091 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 }
6093 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006094
6095 if (wp == NULL)
6096 {
6097 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6098 col = 0;
6099 len = 0;
6100 p = buf;
6101 fillchar = 0;
6102 for (n = 0; tabtab[n].start != NULL; n++)
6103 {
6104 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6105 while (col < len)
6106 TabPageIdxs[col++] = fillchar;
6107 p = tabtab[n].start;
6108 fillchar = tabtab[n].userhl;
6109 }
6110 while (col < Columns)
6111 TabPageIdxs[col++] = fillchar;
6112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113}
6114
6115#endif /* FEAT_STL_OPT */
6116
6117/*
6118 * Output a single character directly to the screen and update ScreenLines.
6119 */
6120 void
6121screen_putchar(c, row, col, attr)
6122 int c;
6123 int row, col;
6124 int attr;
6125{
6126#ifdef FEAT_MBYTE
6127 char_u buf[MB_MAXBYTES + 1];
6128
6129 buf[(*mb_char2bytes)(c, buf)] = NUL;
6130#else
6131 char_u buf[2];
6132
6133 buf[0] = c;
6134 buf[1] = NUL;
6135#endif
6136 screen_puts(buf, row, col, attr);
6137}
6138
6139/*
6140 * Get a single character directly from ScreenLines into "bytes[]".
6141 * Also return its attribute in *attrp;
6142 */
6143 void
6144screen_getbytes(row, col, bytes, attrp)
6145 int row, col;
6146 char_u *bytes;
6147 int *attrp;
6148{
6149 unsigned off;
6150
6151 /* safety check */
6152 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6153 {
6154 off = LineOffset[row] + col;
6155 *attrp = ScreenAttrs[off];
6156 bytes[0] = ScreenLines[off];
6157 bytes[1] = NUL;
6158
6159#ifdef FEAT_MBYTE
6160 if (enc_utf8 && ScreenLinesUC[off] != 0)
6161 bytes[utfc_char2bytes(off, bytes)] = NUL;
6162 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6163 {
6164 bytes[0] = ScreenLines[off];
6165 bytes[1] = ScreenLines2[off];
6166 bytes[2] = NUL;
6167 }
6168 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6169 {
6170 bytes[1] = ScreenLines[off + 1];
6171 bytes[2] = NUL;
6172 }
6173#endif
6174 }
6175}
6176
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006177#ifdef FEAT_MBYTE
6178static int screen_comp_differs __ARGS((int, int*));
6179
6180/*
6181 * Return TRUE if composing characters for screen posn "off" differs from
6182 * composing characters in "u8cc".
6183 */
6184 static int
6185screen_comp_differs(off, u8cc)
6186 int off;
6187 int *u8cc;
6188{
6189 int i;
6190
6191 for (i = 0; i < Screen_mco; ++i)
6192 {
6193 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6194 return TRUE;
6195 if (u8cc[i] == 0)
6196 break;
6197 }
6198 return FALSE;
6199}
6200#endif
6201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202/*
6203 * Put string '*text' on the screen at position 'row' and 'col', with
6204 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6205 * Note: only outputs within one row, message is truncated at screen boundary!
6206 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6207 */
6208 void
6209screen_puts(text, row, col, attr)
6210 char_u *text;
6211 int row;
6212 int col;
6213 int attr;
6214{
6215 screen_puts_len(text, -1, row, col, attr);
6216}
6217
6218/*
6219 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6220 * a NUL.
6221 */
6222 void
6223screen_puts_len(text, len, row, col, attr)
6224 char_u *text;
6225 int len;
6226 int row;
6227 int col;
6228 int attr;
6229{
6230 unsigned off;
6231 char_u *ptr = text;
6232 int c;
6233#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006234 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 int mbyte_blen = 1;
6236 int mbyte_cells = 1;
6237 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006238 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 int clear_next_cell = FALSE;
6240# ifdef FEAT_ARABIC
6241 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006242 int pc, nc, nc1;
6243 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244# endif
6245#endif
6246
6247 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6248 return;
6249
6250 off = LineOffset[row] + col;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006251#ifdef FEAT_MBYTE
6252 max_off = LineOffset[row] + screen_Columns;
6253#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006254 while (col < screen_Columns
6255 && (len < 0 || (int)(ptr - text) < len)
6256 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
6258 c = *ptr;
6259#ifdef FEAT_MBYTE
6260 /* check if this is the first byte of a multibyte */
6261 if (has_mbyte)
6262 {
6263 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006264 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006266 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6268 mbyte_cells = 1;
6269 else if (enc_dbcs != 0)
6270 mbyte_cells = mbyte_blen;
6271 else /* enc_utf8 */
6272 {
6273 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006274 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 (int)((text + len) - ptr));
6276 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006277 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006279# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 /* Non-BMP character: display as ? or fullwidth ?. */
6281 if (u8c >= 0x10000)
6282 {
6283 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6284 if (attr == 0)
6285 attr = hl_attr(HLF_8);
6286 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288# ifdef FEAT_ARABIC
6289 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6290 {
6291 /* Do Arabic shaping. */
6292 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6293 {
6294 /* Past end of string to be displayed. */
6295 nc = NUL;
6296 nc1 = NUL;
6297 }
6298 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006299 {
6300 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6301 nc1 = pcc[0];
6302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 pc = prev_c;
6304 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006305 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 }
6307 else
6308 prev_c = u8c;
6309# endif
6310 }
6311 }
6312#endif
6313
6314 if (ScreenLines[off] != c
6315#ifdef FEAT_MBYTE
6316 || (mbyte_cells == 2
6317 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6318 || (enc_dbcs == DBCS_JPNU
6319 && c == 0x8e
6320 && ScreenLines2[off] != ptr[1])
6321 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006322 && (ScreenLinesUC[off] != (u8char_T)u8c
6323 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324#endif
6325 || ScreenAttrs[off] != attr
6326 || exmode_active
6327 )
6328 {
6329#if defined(FEAT_GUI) || defined(UNIX)
6330 /* The bold trick makes a single row of pixels appear in the next
6331 * character. When a bold character is removed, the next
6332 * character should be redrawn too. This happens for our own GUI
6333 * and for some xterms.
6334 * Force the redraw by setting the attribute to a different value
6335 * than "attr", the contents of ScreenLines[] may be needed by
6336 * mb_off2cells() further on.
6337 * Don't do this for the last drawn character, because the next
6338 * character may not be redrawn. */
6339 if (
6340# ifdef FEAT_GUI
6341 gui.in_use
6342# endif
6343# if defined(FEAT_GUI) && defined(UNIX)
6344 ||
6345# endif
6346# ifdef UNIX
6347 term_is_xterm
6348# endif
6349 )
6350 {
6351 int n;
6352
6353 n = ScreenAttrs[off];
6354# ifdef FEAT_MBYTE
6355 if (col + mbyte_cells < screen_Columns
6356 && (n > HL_ALL || (n & HL_BOLD))
6357 && (len < 0 ? ptr[mbyte_blen] != NUL
6358 : ptr + mbyte_blen < text + len))
6359 ScreenAttrs[off + mbyte_cells] = attr + 1;
6360# else
6361 if (col + 1 < screen_Columns
6362 && (n > HL_ALL || (n & HL_BOLD))
6363 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6364 ScreenLines[off + 1] = 0;
6365# endif
6366 }
6367#endif
6368#ifdef FEAT_MBYTE
6369 /* When at the end of the text and overwriting a two-cell
6370 * character with a one-cell character, need to clear the next
6371 * cell. Also when overwriting the left halve of a two-cell char
6372 * with the right halve of a two-cell char. Do this only once
6373 * (mb_off2cells() may return 2 on the right halve). */
6374 if (clear_next_cell)
6375 clear_next_cell = FALSE;
6376 else if (has_mbyte
6377 && (len < 0 ? ptr[mbyte_blen] == NUL
6378 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006379 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006381 && (*mb_off2cells)(off, max_off) == 1
6382 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 clear_next_cell = TRUE;
6384
6385 /* Make sure we never leave a second byte of a double-byte behind,
6386 * it confuses mb_off2cells(). */
6387 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006388 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006390 && (*mb_off2cells)(off, max_off) == 1
6391 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 ScreenLines[off + mbyte_blen] = 0;
6393#endif
6394 ScreenLines[off] = c;
6395 ScreenAttrs[off] = attr;
6396#ifdef FEAT_MBYTE
6397 if (enc_utf8)
6398 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006399 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 ScreenLinesUC[off] = 0;
6401 else
6402 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006403 int i;
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006406 for (i = 0; i < Screen_mco; ++i)
6407 {
6408 ScreenLinesC[i][off] = u8cc[i];
6409 if (u8cc[i] == 0)
6410 break;
6411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
6413 if (mbyte_cells == 2)
6414 {
6415 ScreenLines[off + 1] = 0;
6416 ScreenAttrs[off + 1] = attr;
6417 }
6418 screen_char(off, row, col);
6419 }
6420 else if (mbyte_cells == 2)
6421 {
6422 ScreenLines[off + 1] = ptr[1];
6423 ScreenAttrs[off + 1] = attr;
6424 screen_char_2(off, row, col);
6425 }
6426 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6427 {
6428 ScreenLines2[off] = ptr[1];
6429 screen_char(off, row, col);
6430 }
6431 else
6432#endif
6433 screen_char(off, row, col);
6434 }
6435#ifdef FEAT_MBYTE
6436 if (has_mbyte)
6437 {
6438 off += mbyte_cells;
6439 col += mbyte_cells;
6440 ptr += mbyte_blen;
6441 if (clear_next_cell)
6442 ptr = (char_u *)" ";
6443 }
6444 else
6445#endif
6446 {
6447 ++off;
6448 ++col;
6449 ++ptr;
6450 }
6451 }
6452}
6453
6454#ifdef FEAT_SEARCH_EXTRA
6455/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006456 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 */
6458 static void
6459start_search_hl()
6460{
6461 if (p_hls && !no_hlsearch)
6462 {
6463 last_pat_prog(&search_hl.rm);
6464 search_hl.attr = hl_attr(HLF_L);
6465 }
6466}
6467
6468/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006469 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 */
6471 static void
6472end_search_hl()
6473{
6474 if (search_hl.rm.regprog != NULL)
6475 {
6476 vim_free(search_hl.rm.regprog);
6477 search_hl.rm.regprog = NULL;
6478 }
6479}
6480
6481/*
6482 * Advance to the match in window "wp" line "lnum" or past it.
6483 */
6484 static void
6485prepare_search_hl(wp, lnum)
6486 win_T *wp;
6487 linenr_T lnum;
6488{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006489 matchitem_T *cur; /* points to the match list */
6490 match_T *shl; /* points to search_hl or a match */
6491 int shl_flag; /* flag to indicate whether search_hl
6492 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 int n;
6494
6495 /*
6496 * When using a multi-line pattern, start searching at the top
6497 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006498 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006500 cur = wp->w_match_head;
6501 shl_flag = FALSE;
6502 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006504 if (shl_flag == FALSE)
6505 {
6506 shl = &search_hl;
6507 shl_flag = TRUE;
6508 }
6509 else
6510 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 if (shl->rm.regprog != NULL
6512 && shl->lnum == 0
6513 && re_multiline(shl->rm.regprog))
6514 {
6515 if (shl->first_lnum == 0)
6516 {
6517# ifdef FEAT_FOLDING
6518 for (shl->first_lnum = lnum;
6519 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6520 if (hasFoldingWin(wp, shl->first_lnum - 1,
6521 NULL, NULL, TRUE, NULL))
6522 break;
6523# else
6524 shl->first_lnum = wp->w_topline;
6525# endif
6526 }
6527 n = 0;
6528 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6529 {
6530 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6531 if (shl->lnum != 0)
6532 {
6533 shl->first_lnum = shl->lnum
6534 + shl->rm.endpos[0].lnum
6535 - shl->rm.startpos[0].lnum;
6536 n = shl->rm.endpos[0].col;
6537 }
6538 else
6539 {
6540 ++shl->first_lnum;
6541 n = 0;
6542 }
6543 }
6544 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006545 if (shl != &search_hl && cur != NULL)
6546 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
6548}
6549
6550/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006551 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 * Uses shl->buf.
6553 * Sets shl->lnum and shl->rm contents.
6554 * Note: Assumes a previous match is always before "lnum", unless
6555 * shl->lnum is zero.
6556 * Careful: Any pointers for buffer lines will become invalid.
6557 */
6558 static void
6559next_search_hl(win, shl, lnum, mincol)
6560 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006561 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 linenr_T lnum;
6563 colnr_T mincol; /* minimal column for a match */
6564{
6565 linenr_T l;
6566 colnr_T matchcol;
6567 long nmatched;
6568
6569 if (shl->lnum != 0)
6570 {
6571 /* Check for three situations:
6572 * 1. If the "lnum" is below a previous match, start a new search.
6573 * 2. If the previous match includes "mincol", use it.
6574 * 3. Continue after the previous match.
6575 */
6576 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6577 if (lnum > l)
6578 shl->lnum = 0;
6579 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6580 return;
6581 }
6582
6583 /*
6584 * Repeat searching for a match until one is found that includes "mincol"
6585 * or none is found in this line.
6586 */
6587 called_emsg = FALSE;
6588 for (;;)
6589 {
6590 /* Three situations:
6591 * 1. No useful previous match: search from start of line.
6592 * 2. Not Vi compatible or empty match: continue at next character.
6593 * Break the loop if this is beyond the end of the line.
6594 * 3. Vi compatible searching: continue at end of previous match.
6595 */
6596 if (shl->lnum == 0)
6597 matchcol = 0;
6598 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6599 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006600 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006602 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006603
6604 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006605 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006606 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006608 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 shl->lnum = 0;
6610 break;
6611 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006612#ifdef FEAT_MBYTE
6613 if (has_mbyte)
6614 matchcol += mb_ptr2len(ml);
6615 else
6616#endif
6617 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 }
6619 else
6620 matchcol = shl->rm.endpos[0].col;
6621
6622 shl->lnum = lnum;
6623 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6624 if (called_emsg)
6625 {
6626 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006627 if (shl == &search_hl)
6628 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006629 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006630 vim_free(shl->rm.regprog);
6631 no_hlsearch = TRUE;
6632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006634 shl->lnum = 0;
6635 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 break;
6637 }
6638 if (nmatched == 0)
6639 {
6640 shl->lnum = 0; /* no match found */
6641 break;
6642 }
6643 if (shl->rm.startpos[0].lnum > 0
6644 || shl->rm.startpos[0].col >= mincol
6645 || nmatched > 1
6646 || shl->rm.endpos[0].col > mincol)
6647 {
6648 shl->lnum += shl->rm.startpos[0].lnum;
6649 break; /* useful match found */
6650 }
6651 }
6652}
6653#endif
6654
6655 static void
6656screen_start_highlight(attr)
6657 int attr;
6658{
6659 attrentry_T *aep = NULL;
6660
6661 screen_attr = attr;
6662 if (full_screen
6663#ifdef WIN3264
6664 && termcap_active
6665#endif
6666 )
6667 {
6668#ifdef FEAT_GUI
6669 if (gui.in_use)
6670 {
6671 char buf[20];
6672
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006673 /* The GUI handles this internally. */
6674 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 OUT_STR(buf);
6676 }
6677 else
6678#endif
6679 {
6680 if (attr > HL_ALL) /* special HL attr. */
6681 {
6682 if (t_colors > 1)
6683 aep = syn_cterm_attr2entry(attr);
6684 else
6685 aep = syn_term_attr2entry(attr);
6686 if (aep == NULL) /* did ":syntax clear" */
6687 attr = 0;
6688 else
6689 attr = aep->ae_attr;
6690 }
6691 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6692 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006693 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6694 && cterm_normal_fg_bold)
6695 /* If the Normal FG color has BOLD attribute and the new HL
6696 * has a FG color defined, clear BOLD. */
6697 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6699 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006700 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6701 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 out_str(T_US);
6703 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6704 out_str(T_CZH);
6705 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6706 out_str(T_MR);
6707
6708 /*
6709 * Output the color or start string after bold etc., in case the
6710 * bold etc. override the color setting.
6711 */
6712 if (aep != NULL)
6713 {
6714 if (t_colors > 1)
6715 {
6716 if (aep->ae_u.cterm.fg_color)
6717 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6718 if (aep->ae_u.cterm.bg_color)
6719 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6720 }
6721 else
6722 {
6723 if (aep->ae_u.term.start != NULL)
6724 out_str(aep->ae_u.term.start);
6725 }
6726 }
6727 }
6728 }
6729}
6730
6731 void
6732screen_stop_highlight()
6733{
6734 int do_ME = FALSE; /* output T_ME code */
6735
6736 if (screen_attr != 0
6737#ifdef WIN3264
6738 && termcap_active
6739#endif
6740 )
6741 {
6742#ifdef FEAT_GUI
6743 if (gui.in_use)
6744 {
6745 char buf[20];
6746
6747 /* use internal GUI code */
6748 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6749 OUT_STR(buf);
6750 }
6751 else
6752#endif
6753 {
6754 if (screen_attr > HL_ALL) /* special HL attr. */
6755 {
6756 attrentry_T *aep;
6757
6758 if (t_colors > 1)
6759 {
6760 /*
6761 * Assume that t_me restores the original colors!
6762 */
6763 aep = syn_cterm_attr2entry(screen_attr);
6764 if (aep != NULL && (aep->ae_u.cterm.fg_color
6765 || aep->ae_u.cterm.bg_color))
6766 do_ME = TRUE;
6767 }
6768 else
6769 {
6770 aep = syn_term_attr2entry(screen_attr);
6771 if (aep != NULL && aep->ae_u.term.stop != NULL)
6772 {
6773 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6774 do_ME = TRUE;
6775 else
6776 out_str(aep->ae_u.term.stop);
6777 }
6778 }
6779 if (aep == NULL) /* did ":syntax clear" */
6780 screen_attr = 0;
6781 else
6782 screen_attr = aep->ae_attr;
6783 }
6784
6785 /*
6786 * Often all ending-codes are equal to T_ME. Avoid outputting the
6787 * same sequence several times.
6788 */
6789 if (screen_attr & HL_STANDOUT)
6790 {
6791 if (STRCMP(T_SE, T_ME) == 0)
6792 do_ME = TRUE;
6793 else
6794 out_str(T_SE);
6795 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006796 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {
6798 if (STRCMP(T_UE, T_ME) == 0)
6799 do_ME = TRUE;
6800 else
6801 out_str(T_UE);
6802 }
6803 if (screen_attr & HL_ITALIC)
6804 {
6805 if (STRCMP(T_CZR, T_ME) == 0)
6806 do_ME = TRUE;
6807 else
6808 out_str(T_CZR);
6809 }
6810 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6811 out_str(T_ME);
6812
6813 if (t_colors > 1)
6814 {
6815 /* set Normal cterm colors */
6816 if (cterm_normal_fg_color != 0)
6817 term_fg_color(cterm_normal_fg_color - 1);
6818 if (cterm_normal_bg_color != 0)
6819 term_bg_color(cterm_normal_bg_color - 1);
6820 if (cterm_normal_fg_bold)
6821 out_str(T_MD);
6822 }
6823 }
6824 }
6825 screen_attr = 0;
6826}
6827
6828/*
6829 * Reset the colors for a cterm. Used when leaving Vim.
6830 * The machine specific code may override this again.
6831 */
6832 void
6833reset_cterm_colors()
6834{
6835 if (t_colors > 1)
6836 {
6837 /* set Normal cterm colors */
6838 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6839 {
6840 out_str(T_OP);
6841 screen_attr = -1;
6842 }
6843 if (cterm_normal_fg_bold)
6844 {
6845 out_str(T_ME);
6846 screen_attr = -1;
6847 }
6848 }
6849}
6850
6851/*
6852 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6853 * using the attributes from ScreenAttrs["off"].
6854 */
6855 static void
6856screen_char(off, row, col)
6857 unsigned off;
6858 int row;
6859 int col;
6860{
6861 int attr;
6862
6863 /* Check for illegal values, just in case (could happen just after
6864 * resizing). */
6865 if (row >= screen_Rows || col >= screen_Columns)
6866 return;
6867
6868 /* Outputting the last character on the screen may scrollup the screen.
6869 * Don't to it! Mark the character invalid (update it when scrolled up) */
6870 if (row == screen_Rows - 1 && col == screen_Columns - 1
6871#ifdef FEAT_RIGHTLEFT
6872 /* account for first command-line character in rightleft mode */
6873 && !cmdmsg_rl
6874#endif
6875 )
6876 {
6877 ScreenAttrs[off] = (sattr_T)-1;
6878 return;
6879 }
6880
6881 /*
6882 * Stop highlighting first, so it's easier to move the cursor.
6883 */
6884#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6885 if (screen_char_attr != 0)
6886 attr = screen_char_attr;
6887 else
6888#endif
6889 attr = ScreenAttrs[off];
6890 if (screen_attr != attr)
6891 screen_stop_highlight();
6892
6893 windgoto(row, col);
6894
6895 if (screen_attr != attr)
6896 screen_start_highlight(attr);
6897
6898#ifdef FEAT_MBYTE
6899 if (enc_utf8 && ScreenLinesUC[off] != 0)
6900 {
6901 char_u buf[MB_MAXBYTES + 1];
6902
6903 /* Convert UTF-8 character to bytes and write it. */
6904
6905 buf[utfc_char2bytes(off, buf)] = NUL;
6906
6907 out_str(buf);
6908 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6909 ++screen_cur_col;
6910 }
6911 else
6912#endif
6913 {
6914#ifdef FEAT_MBYTE
6915 out_flush_check();
6916#endif
6917 out_char(ScreenLines[off]);
6918#ifdef FEAT_MBYTE
6919 /* double-byte character in single-width cell */
6920 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6921 out_char(ScreenLines2[off]);
6922#endif
6923 }
6924
6925 screen_cur_col++;
6926}
6927
6928#ifdef FEAT_MBYTE
6929
6930/*
6931 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6932 * on the screen at position 'row' and 'col'.
6933 * The attributes of the first byte is used for all. This is required to
6934 * output the two bytes of a double-byte character with nothing in between.
6935 */
6936 static void
6937screen_char_2(off, row, col)
6938 unsigned off;
6939 int row;
6940 int col;
6941{
6942 /* Check for illegal values (could be wrong when screen was resized). */
6943 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6944 return;
6945
6946 /* Outputting the last character on the screen may scrollup the screen.
6947 * Don't to it! Mark the character invalid (update it when scrolled up) */
6948 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6949 {
6950 ScreenAttrs[off] = (sattr_T)-1;
6951 return;
6952 }
6953
6954 /* Output the first byte normally (positions the cursor), then write the
6955 * second byte directly. */
6956 screen_char(off, row, col);
6957 out_char(ScreenLines[off + 1]);
6958 ++screen_cur_col;
6959}
6960#endif
6961
6962#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6963/*
6964 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6965 * This uses the contents of ScreenLines[] and doesn't change it.
6966 */
6967 void
6968screen_draw_rectangle(row, col, height, width, invert)
6969 int row;
6970 int col;
6971 int height;
6972 int width;
6973 int invert;
6974{
6975 int r, c;
6976 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006977#ifdef FEAT_MBYTE
6978 int max_off;
6979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006981 /* Can't use ScreenLines unless initialized */
6982 if (ScreenLines == NULL)
6983 return;
6984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (invert)
6986 screen_char_attr = HL_INVERSE;
6987 for (r = row; r < row + height; ++r)
6988 {
6989 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006990#ifdef FEAT_MBYTE
6991 max_off = off + screen_Columns;
6992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 for (c = col; c < col + width; ++c)
6994 {
6995#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006996 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {
6998 screen_char_2(off + c, r, c);
6999 ++c;
7000 }
7001 else
7002#endif
7003 {
7004 screen_char(off + c, r, c);
7005#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007006 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 ++c;
7008#endif
7009 }
7010 }
7011 }
7012 screen_char_attr = 0;
7013}
7014#endif
7015
7016#ifdef FEAT_VERTSPLIT
7017/*
7018 * Redraw the characters for a vertically split window.
7019 */
7020 static void
7021redraw_block(row, end, wp)
7022 int row;
7023 int end;
7024 win_T *wp;
7025{
7026 int col;
7027 int width;
7028
7029# ifdef FEAT_CLIPBOARD
7030 clip_may_clear_selection(row, end - 1);
7031# endif
7032
7033 if (wp == NULL)
7034 {
7035 col = 0;
7036 width = Columns;
7037 }
7038 else
7039 {
7040 col = wp->w_wincol;
7041 width = wp->w_width;
7042 }
7043 screen_draw_rectangle(row, col, end - row, width, FALSE);
7044}
7045#endif
7046
7047/*
7048 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7049 * with character 'c1' in first column followed by 'c2' in the other columns.
7050 * Use attributes 'attr'.
7051 */
7052 void
7053screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7054 int start_row, end_row;
7055 int start_col, end_col;
7056 int c1, c2;
7057 int attr;
7058{
7059 int row;
7060 int col;
7061 int off;
7062 int end_off;
7063 int did_delete;
7064 int c;
7065 int norm_term;
7066#if defined(FEAT_GUI) || defined(UNIX)
7067 int force_next = FALSE;
7068#endif
7069
7070 if (end_row > screen_Rows) /* safety check */
7071 end_row = screen_Rows;
7072 if (end_col > screen_Columns) /* safety check */
7073 end_col = screen_Columns;
7074 if (ScreenLines == NULL
7075 || start_row >= end_row
7076 || start_col >= end_col) /* nothing to do */
7077 return;
7078
7079 /* it's a "normal" terminal when not in a GUI or cterm */
7080 norm_term = (
7081#ifdef FEAT_GUI
7082 !gui.in_use &&
7083#endif
7084 t_colors <= 1);
7085 for (row = start_row; row < end_row; ++row)
7086 {
7087 /*
7088 * Try to use delete-line termcap code, when no attributes or in a
7089 * "normal" terminal, where a bold/italic space is just a
7090 * space.
7091 */
7092 did_delete = FALSE;
7093 if (c2 == ' '
7094 && end_col == Columns
7095 && can_clear(T_CE)
7096 && (attr == 0
7097 || (norm_term
7098 && attr <= HL_ALL
7099 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7100 {
7101 /*
7102 * check if we really need to clear something
7103 */
7104 col = start_col;
7105 if (c1 != ' ') /* don't clear first char */
7106 ++col;
7107
7108 off = LineOffset[row] + col;
7109 end_off = LineOffset[row] + end_col;
7110
7111 /* skip blanks (used often, keep it fast!) */
7112#ifdef FEAT_MBYTE
7113 if (enc_utf8)
7114 while (off < end_off && ScreenLines[off] == ' '
7115 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7116 ++off;
7117 else
7118#endif
7119 while (off < end_off && ScreenLines[off] == ' '
7120 && ScreenAttrs[off] == 0)
7121 ++off;
7122 if (off < end_off) /* something to be cleared */
7123 {
7124 col = off - LineOffset[row];
7125 screen_stop_highlight();
7126 term_windgoto(row, col);/* clear rest of this screen line */
7127 out_str(T_CE);
7128 screen_start(); /* don't know where cursor is now */
7129 col = end_col - col;
7130 while (col--) /* clear chars in ScreenLines */
7131 {
7132 ScreenLines[off] = ' ';
7133#ifdef FEAT_MBYTE
7134 if (enc_utf8)
7135 ScreenLinesUC[off] = 0;
7136#endif
7137 ScreenAttrs[off] = 0;
7138 ++off;
7139 }
7140 }
7141 did_delete = TRUE; /* the chars are cleared now */
7142 }
7143
7144 off = LineOffset[row] + start_col;
7145 c = c1;
7146 for (col = start_col; col < end_col; ++col)
7147 {
7148 if (ScreenLines[off] != c
7149#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007150 || (enc_utf8 && (int)ScreenLinesUC[off]
7151 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152#endif
7153 || ScreenAttrs[off] != attr
7154#if defined(FEAT_GUI) || defined(UNIX)
7155 || force_next
7156#endif
7157 )
7158 {
7159#if defined(FEAT_GUI) || defined(UNIX)
7160 /* The bold trick may make a single row of pixels appear in
7161 * the next character. When a bold character is removed, the
7162 * next character should be redrawn too. This happens for our
7163 * own GUI and for some xterms. */
7164 if (
7165# ifdef FEAT_GUI
7166 gui.in_use
7167# endif
7168# if defined(FEAT_GUI) && defined(UNIX)
7169 ||
7170# endif
7171# ifdef UNIX
7172 term_is_xterm
7173# endif
7174 )
7175 {
7176 if (ScreenLines[off] != ' '
7177 && (ScreenAttrs[off] > HL_ALL
7178 || ScreenAttrs[off] & HL_BOLD))
7179 force_next = TRUE;
7180 else
7181 force_next = FALSE;
7182 }
7183#endif
7184 ScreenLines[off] = c;
7185#ifdef FEAT_MBYTE
7186 if (enc_utf8)
7187 {
7188 if (c >= 0x80)
7189 {
7190 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007191 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193 else
7194 ScreenLinesUC[off] = 0;
7195 }
7196#endif
7197 ScreenAttrs[off] = attr;
7198 if (!did_delete || c != ' ')
7199 screen_char(off, row, col);
7200 }
7201 ++off;
7202 if (col == start_col)
7203 {
7204 if (did_delete)
7205 break;
7206 c = c2;
7207 }
7208 }
7209 if (end_col == Columns)
7210 LineWraps[row] = FALSE;
7211 if (row == Rows - 1) /* overwritten the command line */
7212 {
7213 redraw_cmdline = TRUE;
7214 if (c1 == ' ' && c2 == ' ')
7215 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007216 if (start_col == 0)
7217 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 }
7219 }
7220}
7221
7222/*
7223 * Check if there should be a delay. Used before clearing or redrawing the
7224 * screen or the command line.
7225 */
7226 void
7227check_for_delay(check_msg_scroll)
7228 int check_msg_scroll;
7229{
7230 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7231 && !did_wait_return
7232 && emsg_silent == 0)
7233 {
7234 out_flush();
7235 ui_delay(1000L, TRUE);
7236 emsg_on_display = FALSE;
7237 if (check_msg_scroll)
7238 msg_scroll = FALSE;
7239 }
7240}
7241
7242/*
7243 * screen_valid - allocate screen buffers if size changed
7244 * If "clear" is TRUE: clear screen if it has been resized.
7245 * Returns TRUE if there is a valid screen to write to.
7246 * Returns FALSE when starting up and screen not initialized yet.
7247 */
7248 int
7249screen_valid(clear)
7250 int clear;
7251{
7252 screenalloc(clear); /* allocate screen buffers if size changed */
7253 return (ScreenLines != NULL);
7254}
7255
7256/*
7257 * Resize the shell to Rows and Columns.
7258 * Allocate ScreenLines[] and associated items.
7259 *
7260 * There may be some time between setting Rows and Columns and (re)allocating
7261 * ScreenLines[]. This happens when starting up and when (manually) changing
7262 * the shell size. Always use screen_Rows and screen_Columns to access items
7263 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7264 * final size of the shell is needed.
7265 */
7266 void
7267screenalloc(clear)
7268 int clear;
7269{
7270 int new_row, old_row;
7271#ifdef FEAT_GUI
7272 int old_Rows;
7273#endif
7274 win_T *wp;
7275 int outofmem = FALSE;
7276 int len;
7277 schar_T *new_ScreenLines;
7278#ifdef FEAT_MBYTE
7279 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007280 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007282 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283#endif
7284 sattr_T *new_ScreenAttrs;
7285 unsigned *new_LineOffset;
7286 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007287#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007288 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007289 tabpage_T *tp;
7290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007292 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294 /*
7295 * Allocation of the screen buffers is done only when the size changes and
7296 * when Rows and Columns have been set and we have started doing full
7297 * screen stuff.
7298 */
7299 if ((ScreenLines != NULL
7300 && Rows == screen_Rows
7301 && Columns == screen_Columns
7302#ifdef FEAT_MBYTE
7303 && enc_utf8 == (ScreenLinesUC != NULL)
7304 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007305 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306#endif
7307 )
7308 || Rows == 0
7309 || Columns == 0
7310 || (!full_screen && ScreenLines == NULL))
7311 return;
7312
7313 /*
7314 * It's possible that we produce an out-of-memory message below, which
7315 * will cause this function to be called again. To break the loop, just
7316 * return here.
7317 */
7318 if (entered)
7319 return;
7320 entered = TRUE;
7321
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007322 /*
7323 * Note that the window sizes are updated before reallocating the arrays,
7324 * thus we must not redraw here!
7325 */
7326 ++RedrawingDisabled;
7327
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 win_new_shellsize(); /* fit the windows in the new sized shell */
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 comp_col(); /* recompute columns for shown command and ruler */
7331
7332 /*
7333 * We're changing the size of the screen.
7334 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7335 * - Move lines from the old arrays into the new arrays, clear extra
7336 * lines (unless the screen is going to be cleared).
7337 * - Free the old arrays.
7338 *
7339 * If anything fails, make ScreenLines NULL, so we don't do anything!
7340 * Continuing with the old ScreenLines may result in a crash, because the
7341 * size is wrong.
7342 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007343 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
7346 new_ScreenLines = (schar_T *)lalloc((long_u)(
7347 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7348#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007349 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (enc_utf8)
7351 {
7352 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7353 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007354 for (i = 0; i < p_mco; ++i)
7355 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7357 }
7358 if (enc_dbcs == DBCS_JPNU)
7359 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7360 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7361#endif
7362 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7363 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7364 new_LineOffset = (unsigned *)lalloc((long_u)(
7365 Rows * sizeof(unsigned)), FALSE);
7366 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007367#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007368 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007371 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 {
7373 if (win_alloc_lines(wp) == FAIL)
7374 {
7375 outofmem = TRUE;
7376#ifdef FEAT_WINDOWS
7377 break;
7378#endif
7379 }
7380 }
7381
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007382#ifdef FEAT_MBYTE
7383 for (i = 0; i < p_mco; ++i)
7384 if (new_ScreenLinesC[i] == NULL)
7385 break;
7386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 if (new_ScreenLines == NULL
7388#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007389 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7391#endif
7392 || new_ScreenAttrs == NULL
7393 || new_LineOffset == NULL
7394 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007395#ifdef FEAT_WINDOWS
7396 || new_TabPageIdxs == NULL
7397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 || outofmem)
7399 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007400 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007401 {
7402 /* guess the size */
7403 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7404
7405 /* Remember we did this to avoid getting outofmem messages over
7406 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007407 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 vim_free(new_ScreenLines);
7410 new_ScreenLines = NULL;
7411#ifdef FEAT_MBYTE
7412 vim_free(new_ScreenLinesUC);
7413 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007414 for (i = 0; i < p_mco; ++i)
7415 {
7416 vim_free(new_ScreenLinesC[i]);
7417 new_ScreenLinesC[i] = NULL;
7418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 vim_free(new_ScreenLines2);
7420 new_ScreenLines2 = NULL;
7421#endif
7422 vim_free(new_ScreenAttrs);
7423 new_ScreenAttrs = NULL;
7424 vim_free(new_LineOffset);
7425 new_LineOffset = NULL;
7426 vim_free(new_LineWraps);
7427 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007428#ifdef FEAT_WINDOWS
7429 vim_free(new_TabPageIdxs);
7430 new_TabPageIdxs = NULL;
7431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 }
7433 else
7434 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007435 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007436
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 for (new_row = 0; new_row < Rows; ++new_row)
7438 {
7439 new_LineOffset[new_row] = new_row * Columns;
7440 new_LineWraps[new_row] = FALSE;
7441
7442 /*
7443 * If the screen is not going to be cleared, copy as much as
7444 * possible from the old screen to the new one and clear the rest
7445 * (used when resizing the window at the "--more--" prompt or when
7446 * executing an external command, for the GUI).
7447 */
7448 if (!clear)
7449 {
7450 (void)vim_memset(new_ScreenLines + new_row * Columns,
7451 ' ', (size_t)Columns * sizeof(schar_T));
7452#ifdef FEAT_MBYTE
7453 if (enc_utf8)
7454 {
7455 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7456 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007457 for (i = 0; i < p_mco; ++i)
7458 (void)vim_memset(new_ScreenLinesC[i]
7459 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 0, (size_t)Columns * sizeof(u8char_T));
7461 }
7462 if (enc_dbcs == DBCS_JPNU)
7463 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7464 0, (size_t)Columns * sizeof(schar_T));
7465#endif
7466 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7467 0, (size_t)Columns * sizeof(sattr_T));
7468 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007469 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {
7471 if (screen_Columns < Columns)
7472 len = screen_Columns;
7473 else
7474 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007475#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007476 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007477 * may be invalid now. Also when p_mco changes. */
7478 if (!(enc_utf8 && ScreenLinesUC == NULL)
7479 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007480#endif
7481 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7482 ScreenLines + LineOffset[old_row],
7483 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007485 if (enc_utf8 && ScreenLinesUC != NULL
7486 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 {
7488 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7489 ScreenLinesUC + LineOffset[old_row],
7490 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491 for (i = 0; i < p_mco; ++i)
7492 mch_memmove(new_ScreenLinesC[i]
7493 + new_LineOffset[new_row],
7494 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 (size_t)len * sizeof(u8char_T));
7496 }
7497 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7498 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7499 ScreenLines2 + LineOffset[old_row],
7500 (size_t)len * sizeof(schar_T));
7501#endif
7502 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7503 ScreenAttrs + LineOffset[old_row],
7504 (size_t)len * sizeof(sattr_T));
7505 }
7506 }
7507 }
7508 /* Use the last line of the screen for the current line. */
7509 current_ScreenLine = new_ScreenLines + Rows * Columns;
7510 }
7511
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007512 free_screenlines();
7513
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 ScreenLines = new_ScreenLines;
7515#ifdef FEAT_MBYTE
7516 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007517 for (i = 0; i < p_mco; ++i)
7518 ScreenLinesC[i] = new_ScreenLinesC[i];
7519 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 ScreenLines2 = new_ScreenLines2;
7521#endif
7522 ScreenAttrs = new_ScreenAttrs;
7523 LineOffset = new_LineOffset;
7524 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007525#ifdef FEAT_WINDOWS
7526 TabPageIdxs = new_TabPageIdxs;
7527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 /* It's important that screen_Rows and screen_Columns reflect the actual
7530 * size of ScreenLines[]. Set them before calling anything. */
7531#ifdef FEAT_GUI
7532 old_Rows = screen_Rows;
7533#endif
7534 screen_Rows = Rows;
7535 screen_Columns = Columns;
7536
7537 must_redraw = CLEAR; /* need to clear the screen later */
7538 if (clear)
7539 screenclear2();
7540
7541#ifdef FEAT_GUI
7542 else if (gui.in_use
7543 && !gui.starting
7544 && ScreenLines != NULL
7545 && old_Rows != Rows)
7546 {
7547 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7548 /*
7549 * Adjust the position of the cursor, for when executing an external
7550 * command.
7551 */
7552 if (msg_row >= Rows) /* Rows got smaller */
7553 msg_row = Rows - 1; /* put cursor at last row */
7554 else if (Rows > old_Rows) /* Rows got bigger */
7555 msg_row += Rows - old_Rows; /* put cursor in same place */
7556 if (msg_col >= Columns) /* Columns got smaller */
7557 msg_col = Columns - 1; /* put cursor at last column */
7558 }
7559#endif
7560
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007562 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007563
7564#ifdef FEAT_AUTOCMD
7565 if (starting == 0)
7566 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568}
7569
7570 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007571free_screenlines()
7572{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007573#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007574 int i;
7575
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007576 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007577 for (i = 0; i < Screen_mco; ++i)
7578 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007579 vim_free(ScreenLines2);
7580#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007581 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007582 vim_free(ScreenAttrs);
7583 vim_free(LineOffset);
7584 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007585#ifdef FEAT_WINDOWS
7586 vim_free(TabPageIdxs);
7587#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007588}
7589
7590 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591screenclear()
7592{
7593 check_for_delay(FALSE);
7594 screenalloc(FALSE); /* allocate screen buffers if size changed */
7595 screenclear2(); /* clear the screen */
7596}
7597
7598 static void
7599screenclear2()
7600{
7601 int i;
7602
7603 if (starting == NO_SCREEN || ScreenLines == NULL
7604#ifdef FEAT_GUI
7605 || (gui.in_use && gui.starting)
7606#endif
7607 )
7608 return;
7609
7610#ifdef FEAT_GUI
7611 if (!gui.in_use)
7612#endif
7613 screen_attr = -1; /* force setting the Normal colors */
7614 screen_stop_highlight(); /* don't want highlighting here */
7615
7616#ifdef FEAT_CLIPBOARD
7617 /* disable selection without redrawing it */
7618 clip_scroll_selection(9999);
7619#endif
7620
7621 /* blank out ScreenLines */
7622 for (i = 0; i < Rows; ++i)
7623 {
7624 lineclear(LineOffset[i], (int)Columns);
7625 LineWraps[i] = FALSE;
7626 }
7627
7628 if (can_clear(T_CL))
7629 {
7630 out_str(T_CL); /* clear the display */
7631 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007632 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 }
7634 else
7635 {
7636 /* can't clear the screen, mark all chars with invalid attributes */
7637 for (i = 0; i < Rows; ++i)
7638 lineinvalid(LineOffset[i], (int)Columns);
7639 clear_cmdline = TRUE;
7640 }
7641
7642 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7643
7644 win_rest_invalid(firstwin);
7645 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007646#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007647 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 if (must_redraw == CLEAR) /* no need to clear again */
7650 must_redraw = NOT_VALID;
7651 compute_cmdrow();
7652 msg_row = cmdline_row; /* put cursor on last line for messages */
7653 msg_col = 0;
7654 screen_start(); /* don't know where cursor is now */
7655 msg_scrolled = 0; /* can't scroll back */
7656 msg_didany = FALSE;
7657 msg_didout = FALSE;
7658}
7659
7660/*
7661 * Clear one line in ScreenLines.
7662 */
7663 static void
7664lineclear(off, width)
7665 unsigned off;
7666 int width;
7667{
7668 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7669#ifdef FEAT_MBYTE
7670 if (enc_utf8)
7671 (void)vim_memset(ScreenLinesUC + off, 0,
7672 (size_t)width * sizeof(u8char_T));
7673#endif
7674 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7675}
7676
7677/*
7678 * Mark one line in ScreenLines invalid by setting the attributes to an
7679 * invalid value.
7680 */
7681 static void
7682lineinvalid(off, width)
7683 unsigned off;
7684 int width;
7685{
7686 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7687}
7688
7689#ifdef FEAT_VERTSPLIT
7690/*
7691 * Copy part of a Screenline for vertically split window "wp".
7692 */
7693 static void
7694linecopy(to, from, wp)
7695 int to;
7696 int from;
7697 win_T *wp;
7698{
7699 unsigned off_to = LineOffset[to] + wp->w_wincol;
7700 unsigned off_from = LineOffset[from] + wp->w_wincol;
7701
7702 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7703 wp->w_width * sizeof(schar_T));
7704# ifdef FEAT_MBYTE
7705 if (enc_utf8)
7706 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007707 int i;
7708
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7710 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007711 for (i = 0; i < p_mco; ++i)
7712 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7713 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715 if (enc_dbcs == DBCS_JPNU)
7716 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7717 wp->w_width * sizeof(schar_T));
7718# endif
7719 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7720 wp->w_width * sizeof(sattr_T));
7721}
7722#endif
7723
7724/*
7725 * Return TRUE if clearing with term string "p" would work.
7726 * It can't work when the string is empty or it won't set the right background.
7727 */
7728 int
7729can_clear(p)
7730 char_u *p;
7731{
7732 return (*p != NUL && (t_colors <= 1
7733#ifdef FEAT_GUI
7734 || gui.in_use
7735#endif
7736 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7737}
7738
7739/*
7740 * Reset cursor position. Use whenever cursor was moved because of outputting
7741 * something directly to the screen (shell commands) or a terminal control
7742 * code.
7743 */
7744 void
7745screen_start()
7746{
7747 screen_cur_row = screen_cur_col = 9999;
7748}
7749
7750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 * Move the cursor to position "row","col" in the screen.
7752 * This tries to find the most efficient way to move, minimizing the number of
7753 * characters sent to the terminal.
7754 */
7755 void
7756windgoto(row, col)
7757 int row;
7758 int col;
7759{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007760 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 int i;
7762 int plan;
7763 int cost;
7764 int wouldbe_col;
7765 int noinvcurs;
7766 char_u *bs;
7767 int goto_cost;
7768 int attr;
7769
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007770#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7772
7773#define PLAN_LE 1
7774#define PLAN_CR 2
7775#define PLAN_NL 3
7776#define PLAN_WRITE 4
7777 /* Can't use ScreenLines unless initialized */
7778 if (ScreenLines == NULL)
7779 return;
7780
7781 if (col != screen_cur_col || row != screen_cur_row)
7782 {
7783 /* Check for valid position. */
7784 if (row < 0) /* window without text lines? */
7785 row = 0;
7786 if (row >= screen_Rows)
7787 row = screen_Rows - 1;
7788 if (col >= screen_Columns)
7789 col = screen_Columns - 1;
7790
7791 /* check if no cursor movement is allowed in highlight mode */
7792 if (screen_attr && *T_MS == NUL)
7793 noinvcurs = HIGHL_COST;
7794 else
7795 noinvcurs = 0;
7796 goto_cost = GOTO_COST + noinvcurs;
7797
7798 /*
7799 * Plan how to do the positioning:
7800 * 1. Use CR to move it to column 0, same row.
7801 * 2. Use T_LE to move it a few columns to the left.
7802 * 3. Use NL to move a few lines down, column 0.
7803 * 4. Move a few columns to the right with T_ND or by writing chars.
7804 *
7805 * Don't do this if the cursor went beyond the last column, the cursor
7806 * position is unknown then (some terminals wrap, some don't )
7807 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007808 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 * characters to move the cursor to the right.
7810 */
7811 if (row >= screen_cur_row && screen_cur_col < Columns)
7812 {
7813 /*
7814 * If the cursor is in the same row, bigger col, we can use CR
7815 * or T_LE.
7816 */
7817 bs = NULL; /* init for GCC */
7818 attr = screen_attr;
7819 if (row == screen_cur_row && col < screen_cur_col)
7820 {
7821 /* "le" is preferred over "bc", because "bc" is obsolete */
7822 if (*T_LE)
7823 bs = T_LE; /* "cursor left" */
7824 else
7825 bs = T_BC; /* "backspace character (old) */
7826 if (*bs)
7827 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7828 else
7829 cost = 999;
7830 if (col + 1 < cost) /* using CR is less characters */
7831 {
7832 plan = PLAN_CR;
7833 wouldbe_col = 0;
7834 cost = 1; /* CR is just one character */
7835 }
7836 else
7837 {
7838 plan = PLAN_LE;
7839 wouldbe_col = col;
7840 }
7841 if (noinvcurs) /* will stop highlighting */
7842 {
7843 cost += noinvcurs;
7844 attr = 0;
7845 }
7846 }
7847
7848 /*
7849 * If the cursor is above where we want to be, we can use CR LF.
7850 */
7851 else if (row > screen_cur_row)
7852 {
7853 plan = PLAN_NL;
7854 wouldbe_col = 0;
7855 cost = (row - screen_cur_row) * 2; /* CR LF */
7856 if (noinvcurs) /* will stop highlighting */
7857 {
7858 cost += noinvcurs;
7859 attr = 0;
7860 }
7861 }
7862
7863 /*
7864 * If the cursor is in the same row, smaller col, just use write.
7865 */
7866 else
7867 {
7868 plan = PLAN_WRITE;
7869 wouldbe_col = screen_cur_col;
7870 cost = 0;
7871 }
7872
7873 /*
7874 * Check if any characters that need to be written have the
7875 * correct attributes. Also avoid UTF-8 characters.
7876 */
7877 i = col - wouldbe_col;
7878 if (i > 0)
7879 cost += i;
7880 if (cost < goto_cost && i > 0)
7881 {
7882 /*
7883 * Check if the attributes are correct without additionally
7884 * stopping highlighting.
7885 */
7886 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7887 while (i && *p++ == attr)
7888 --i;
7889 if (i != 0)
7890 {
7891 /*
7892 * Try if it works when highlighting is stopped here.
7893 */
7894 if (*--p == 0)
7895 {
7896 cost += noinvcurs;
7897 while (i && *p++ == 0)
7898 --i;
7899 }
7900 if (i != 0)
7901 cost = 999; /* different attributes, don't do it */
7902 }
7903#ifdef FEAT_MBYTE
7904 if (enc_utf8)
7905 {
7906 /* Don't use an UTF-8 char for positioning, it's slow. */
7907 for (i = wouldbe_col; i < col; ++i)
7908 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7909 {
7910 cost = 999;
7911 break;
7912 }
7913 }
7914#endif
7915 }
7916
7917 /*
7918 * We can do it without term_windgoto()!
7919 */
7920 if (cost < goto_cost)
7921 {
7922 if (plan == PLAN_LE)
7923 {
7924 if (noinvcurs)
7925 screen_stop_highlight();
7926 while (screen_cur_col > col)
7927 {
7928 out_str(bs);
7929 --screen_cur_col;
7930 }
7931 }
7932 else if (plan == PLAN_CR)
7933 {
7934 if (noinvcurs)
7935 screen_stop_highlight();
7936 out_char('\r');
7937 screen_cur_col = 0;
7938 }
7939 else if (plan == PLAN_NL)
7940 {
7941 if (noinvcurs)
7942 screen_stop_highlight();
7943 while (screen_cur_row < row)
7944 {
7945 out_char('\n');
7946 ++screen_cur_row;
7947 }
7948 screen_cur_col = 0;
7949 }
7950
7951 i = col - screen_cur_col;
7952 if (i > 0)
7953 {
7954 /*
7955 * Use cursor-right if it's one character only. Avoids
7956 * removing a line of pixels from the last bold char, when
7957 * using the bold trick in the GUI.
7958 */
7959 if (T_ND[0] != NUL && T_ND[1] == NUL)
7960 {
7961 while (i-- > 0)
7962 out_char(*T_ND);
7963 }
7964 else
7965 {
7966 int off;
7967
7968 off = LineOffset[row] + screen_cur_col;
7969 while (i-- > 0)
7970 {
7971 if (ScreenAttrs[off] != screen_attr)
7972 screen_stop_highlight();
7973#ifdef FEAT_MBYTE
7974 out_flush_check();
7975#endif
7976 out_char(ScreenLines[off]);
7977#ifdef FEAT_MBYTE
7978 if (enc_dbcs == DBCS_JPNU
7979 && ScreenLines[off] == 0x8e)
7980 out_char(ScreenLines2[off]);
7981#endif
7982 ++off;
7983 }
7984 }
7985 }
7986 }
7987 }
7988 else
7989 cost = 999;
7990
7991 if (cost >= goto_cost)
7992 {
7993 if (noinvcurs)
7994 screen_stop_highlight();
7995 if (row == screen_cur_row && (col > screen_cur_col) &&
7996 *T_CRI != NUL)
7997 term_cursor_right(col - screen_cur_col);
7998 else
7999 term_windgoto(row, col);
8000 }
8001 screen_cur_row = row;
8002 screen_cur_col = col;
8003 }
8004}
8005
8006/*
8007 * Set cursor to its position in the current window.
8008 */
8009 void
8010setcursor()
8011{
8012 if (redrawing())
8013 {
8014 validate_cursor();
8015 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8016 W_WINCOL(curwin) + (
8017#ifdef FEAT_RIGHTLEFT
8018 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8019# ifdef FEAT_MBYTE
8020 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
8021# endif
8022 1)) :
8023#endif
8024 curwin->w_wcol));
8025 }
8026}
8027
8028
8029/*
8030 * insert 'line_count' lines at 'row' in window 'wp'
8031 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8032 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8033 * scrolling.
8034 * Returns FAIL if the lines are not inserted, OK for success.
8035 */
8036 int
8037win_ins_lines(wp, row, line_count, invalid, mayclear)
8038 win_T *wp;
8039 int row;
8040 int line_count;
8041 int invalid;
8042 int mayclear;
8043{
8044 int did_delete;
8045 int nextrow;
8046 int lastrow;
8047 int retval;
8048
8049 if (invalid)
8050 wp->w_lines_valid = 0;
8051
8052 if (wp->w_height < 5)
8053 return FAIL;
8054
8055 if (line_count > wp->w_height - row)
8056 line_count = wp->w_height - row;
8057
8058 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8059 if (retval != MAYBE)
8060 return retval;
8061
8062 /*
8063 * If there is a next window or a status line, we first try to delete the
8064 * lines at the bottom to avoid messing what is after the window.
8065 * If this fails and there are following windows, don't do anything to avoid
8066 * messing up those windows, better just redraw.
8067 */
8068 did_delete = FALSE;
8069#ifdef FEAT_WINDOWS
8070 if (wp->w_next != NULL || wp->w_status_height)
8071 {
8072 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8073 line_count, (int)Rows, FALSE, NULL) == OK)
8074 did_delete = TRUE;
8075 else if (wp->w_next)
8076 return FAIL;
8077 }
8078#endif
8079 /*
8080 * if no lines deleted, blank the lines that will end up below the window
8081 */
8082 if (!did_delete)
8083 {
8084#ifdef FEAT_WINDOWS
8085 wp->w_redr_status = TRUE;
8086#endif
8087 redraw_cmdline = TRUE;
8088 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8089 lastrow = nextrow + line_count;
8090 if (lastrow > Rows)
8091 lastrow = Rows;
8092 screen_fill(nextrow - line_count, lastrow - line_count,
8093 W_WINCOL(wp), (int)W_ENDCOL(wp),
8094 ' ', ' ', 0);
8095 }
8096
8097 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8098 == FAIL)
8099 {
8100 /* deletion will have messed up other windows */
8101 if (did_delete)
8102 {
8103#ifdef FEAT_WINDOWS
8104 wp->w_redr_status = TRUE;
8105#endif
8106 win_rest_invalid(W_NEXT(wp));
8107 }
8108 return FAIL;
8109 }
8110
8111 return OK;
8112}
8113
8114/*
8115 * delete "line_count" window lines at "row" in window "wp"
8116 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8117 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8118 * scrolling
8119 * Return OK for success, FAIL if the lines are not deleted.
8120 */
8121 int
8122win_del_lines(wp, row, line_count, invalid, mayclear)
8123 win_T *wp;
8124 int row;
8125 int line_count;
8126 int invalid;
8127 int mayclear;
8128{
8129 int retval;
8130
8131 if (invalid)
8132 wp->w_lines_valid = 0;
8133
8134 if (line_count > wp->w_height - row)
8135 line_count = wp->w_height - row;
8136
8137 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8138 if (retval != MAYBE)
8139 return retval;
8140
8141 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8142 (int)Rows, FALSE, NULL) == FAIL)
8143 return FAIL;
8144
8145#ifdef FEAT_WINDOWS
8146 /*
8147 * If there are windows or status lines below, try to put them at the
8148 * correct place. If we can't do that, they have to be redrawn.
8149 */
8150 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8151 {
8152 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8153 line_count, (int)Rows, NULL) == FAIL)
8154 {
8155 wp->w_redr_status = TRUE;
8156 win_rest_invalid(wp->w_next);
8157 }
8158 }
8159 /*
8160 * If this is the last window and there is no status line, redraw the
8161 * command line later.
8162 */
8163 else
8164#endif
8165 redraw_cmdline = TRUE;
8166 return OK;
8167}
8168
8169/*
8170 * Common code for win_ins_lines() and win_del_lines().
8171 * Returns OK or FAIL when the work has been done.
8172 * Returns MAYBE when not finished yet.
8173 */
8174 static int
8175win_do_lines(wp, row, line_count, mayclear, del)
8176 win_T *wp;
8177 int row;
8178 int line_count;
8179 int mayclear;
8180 int del;
8181{
8182 int retval;
8183
8184 if (!redrawing() || line_count <= 0)
8185 return FAIL;
8186
8187 /* only a few lines left: redraw is faster */
8188 if (mayclear && Rows - line_count < 5
8189#ifdef FEAT_VERTSPLIT
8190 && wp->w_width == Columns
8191#endif
8192 )
8193 {
8194 screenclear(); /* will set wp->w_lines_valid to 0 */
8195 return FAIL;
8196 }
8197
8198 /*
8199 * Delete all remaining lines
8200 */
8201 if (row + line_count >= wp->w_height)
8202 {
8203 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8204 W_WINCOL(wp), (int)W_ENDCOL(wp),
8205 ' ', ' ', 0);
8206 return OK;
8207 }
8208
8209 /*
8210 * when scrolling, the message on the command line should be cleared,
8211 * otherwise it will stay there forever.
8212 */
8213 clear_cmdline = TRUE;
8214
8215 /*
8216 * If the terminal can set a scroll region, use that.
8217 * Always do this in a vertically split window. This will redraw from
8218 * ScreenLines[] when t_CV isn't defined. That's faster than using
8219 * win_line().
8220 * Don't use a scroll region when we are going to redraw the text, writing
8221 * a character in the lower right corner of the scroll region causes a
8222 * scroll-up in the DJGPP version.
8223 */
8224 if (scroll_region
8225#ifdef FEAT_VERTSPLIT
8226 || W_WIDTH(wp) != Columns
8227#endif
8228 )
8229 {
8230#ifdef FEAT_VERTSPLIT
8231 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8232#endif
8233 scroll_region_set(wp, row);
8234 if (del)
8235 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8236 wp->w_height - row, FALSE, wp);
8237 else
8238 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8239 wp->w_height - row, wp);
8240#ifdef FEAT_VERTSPLIT
8241 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8242#endif
8243 scroll_region_reset();
8244 return retval;
8245 }
8246
8247#ifdef FEAT_WINDOWS
8248 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8249 return FAIL;
8250#endif
8251
8252 return MAYBE;
8253}
8254
8255/*
8256 * window 'wp' and everything after it is messed up, mark it for redraw
8257 */
8258 static void
8259win_rest_invalid(wp)
8260 win_T *wp;
8261{
8262#ifdef FEAT_WINDOWS
8263 while (wp != NULL)
8264#else
8265 if (wp != NULL)
8266#endif
8267 {
8268 redraw_win_later(wp, NOT_VALID);
8269#ifdef FEAT_WINDOWS
8270 wp->w_redr_status = TRUE;
8271 wp = wp->w_next;
8272#endif
8273 }
8274 redraw_cmdline = TRUE;
8275}
8276
8277/*
8278 * The rest of the routines in this file perform screen manipulations. The
8279 * given operation is performed physically on the screen. The corresponding
8280 * change is also made to the internal screen image. In this way, the editor
8281 * anticipates the effect of editing changes on the appearance of the screen.
8282 * That way, when we call screenupdate a complete redraw isn't usually
8283 * necessary. Another advantage is that we can keep adding code to anticipate
8284 * screen changes, and in the meantime, everything still works.
8285 */
8286
8287/*
8288 * types for inserting or deleting lines
8289 */
8290#define USE_T_CAL 1
8291#define USE_T_CDL 2
8292#define USE_T_AL 3
8293#define USE_T_CE 4
8294#define USE_T_DL 5
8295#define USE_T_SR 6
8296#define USE_NL 7
8297#define USE_T_CD 8
8298#define USE_REDRAW 9
8299
8300/*
8301 * insert lines on the screen and update ScreenLines[]
8302 * 'end' is the line after the scrolled part. Normally it is Rows.
8303 * When scrolling region used 'off' is the offset from the top for the region.
8304 * 'row' and 'end' are relative to the start of the region.
8305 *
8306 * return FAIL for failure, OK for success.
8307 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008308 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309screen_ins_lines(off, row, line_count, end, wp)
8310 int off;
8311 int row;
8312 int line_count;
8313 int end;
8314 win_T *wp; /* NULL or window to use width from */
8315{
8316 int i;
8317 int j;
8318 unsigned temp;
8319 int cursor_row;
8320 int type;
8321 int result_empty;
8322 int can_ce = can_clear(T_CE);
8323
8324 /*
8325 * FAIL if
8326 * - there is no valid screen
8327 * - the screen has to be redrawn completely
8328 * - the line count is less than one
8329 * - the line count is more than 'ttyscroll'
8330 */
8331 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8332 return FAIL;
8333
8334 /*
8335 * There are seven ways to insert lines:
8336 * 0. When in a vertically split window and t_CV isn't set, redraw the
8337 * characters from ScreenLines[].
8338 * 1. Use T_CD (clear to end of display) if it exists and the result of
8339 * the insert is just empty lines
8340 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8341 * present or line_count > 1. It looks better if we do all the inserts
8342 * at once.
8343 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8344 * insert is just empty lines and T_CE is not present or line_count >
8345 * 1.
8346 * 4. Use T_AL (insert line) if it exists.
8347 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8348 * just empty lines.
8349 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8350 * just empty lines.
8351 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8352 * the 'da' flag is not set or we have clear line capability.
8353 * 8. redraw the characters from ScreenLines[].
8354 *
8355 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8356 * the scrollbar for the window. It does have insert line, use that if it
8357 * exists.
8358 */
8359 result_empty = (row + line_count >= end);
8360#ifdef FEAT_VERTSPLIT
8361 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8362 type = USE_REDRAW;
8363 else
8364#endif
8365 if (can_clear(T_CD) && result_empty)
8366 type = USE_T_CD;
8367 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8368 type = USE_T_CAL;
8369 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8370 type = USE_T_CDL;
8371 else if (*T_AL != NUL)
8372 type = USE_T_AL;
8373 else if (can_ce && result_empty)
8374 type = USE_T_CE;
8375 else if (*T_DL != NUL && result_empty)
8376 type = USE_T_DL;
8377 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8378 type = USE_T_SR;
8379 else
8380 return FAIL;
8381
8382 /*
8383 * For clearing the lines screen_del_lines() is used. This will also take
8384 * care of t_db if necessary.
8385 */
8386 if (type == USE_T_CD || type == USE_T_CDL ||
8387 type == USE_T_CE || type == USE_T_DL)
8388 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8389
8390 /*
8391 * If text is retained below the screen, first clear or delete as many
8392 * lines at the bottom of the window as are about to be inserted so that
8393 * the deleted lines won't later surface during a screen_del_lines.
8394 */
8395 if (*T_DB)
8396 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8397
8398#ifdef FEAT_CLIPBOARD
8399 /* Remove a modeless selection when inserting lines halfway the screen
8400 * or not the full width of the screen. */
8401 if (off + row > 0
8402# ifdef FEAT_VERTSPLIT
8403 || (wp != NULL && wp->w_width != Columns)
8404# endif
8405 )
8406 clip_clear_selection();
8407 else
8408 clip_scroll_selection(-line_count);
8409#endif
8410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#ifdef FEAT_GUI
8412 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8413 * scrolling is actually carried out. */
8414 gui_dont_update_cursor();
8415#endif
8416
8417 if (*T_CCS != NUL) /* cursor relative to region */
8418 cursor_row = row;
8419 else
8420 cursor_row = row + off;
8421
8422 /*
8423 * Shift LineOffset[] line_count down to reflect the inserted lines.
8424 * Clear the inserted lines in ScreenLines[].
8425 */
8426 row += off;
8427 end += off;
8428 for (i = 0; i < line_count; ++i)
8429 {
8430#ifdef FEAT_VERTSPLIT
8431 if (wp != NULL && wp->w_width != Columns)
8432 {
8433 /* need to copy part of a line */
8434 j = end - 1 - i;
8435 while ((j -= line_count) >= row)
8436 linecopy(j + line_count, j, wp);
8437 j += line_count;
8438 if (can_clear((char_u *)" "))
8439 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8440 else
8441 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8442 LineWraps[j] = FALSE;
8443 }
8444 else
8445#endif
8446 {
8447 j = end - 1 - i;
8448 temp = LineOffset[j];
8449 while ((j -= line_count) >= row)
8450 {
8451 LineOffset[j + line_count] = LineOffset[j];
8452 LineWraps[j + line_count] = LineWraps[j];
8453 }
8454 LineOffset[j + line_count] = temp;
8455 LineWraps[j + line_count] = FALSE;
8456 if (can_clear((char_u *)" "))
8457 lineclear(temp, (int)Columns);
8458 else
8459 lineinvalid(temp, (int)Columns);
8460 }
8461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462
8463 screen_stop_highlight();
8464 windgoto(cursor_row, 0);
8465
8466#ifdef FEAT_VERTSPLIT
8467 /* redraw the characters */
8468 if (type == USE_REDRAW)
8469 redraw_block(row, end, wp);
8470 else
8471#endif
8472 if (type == USE_T_CAL)
8473 {
8474 term_append_lines(line_count);
8475 screen_start(); /* don't know where cursor is now */
8476 }
8477 else
8478 {
8479 for (i = 0; i < line_count; i++)
8480 {
8481 if (type == USE_T_AL)
8482 {
8483 if (i && cursor_row != 0)
8484 windgoto(cursor_row, 0);
8485 out_str(T_AL);
8486 }
8487 else /* type == USE_T_SR */
8488 out_str(T_SR);
8489 screen_start(); /* don't know where cursor is now */
8490 }
8491 }
8492
8493 /*
8494 * With scroll-reverse and 'da' flag set we need to clear the lines that
8495 * have been scrolled down into the region.
8496 */
8497 if (type == USE_T_SR && *T_DA)
8498 {
8499 for (i = 0; i < line_count; ++i)
8500 {
8501 windgoto(off + i, 0);
8502 out_str(T_CE);
8503 screen_start(); /* don't know where cursor is now */
8504 }
8505 }
8506
8507#ifdef FEAT_GUI
8508 gui_can_update_cursor();
8509 if (gui.in_use)
8510 out_flush(); /* always flush after a scroll */
8511#endif
8512 return OK;
8513}
8514
8515/*
8516 * delete lines on the screen and update ScreenLines[]
8517 * 'end' is the line after the scrolled part. Normally it is Rows.
8518 * When scrolling region used 'off' is the offset from the top for the region.
8519 * 'row' and 'end' are relative to the start of the region.
8520 *
8521 * Return OK for success, FAIL if the lines are not deleted.
8522 */
8523/*ARGSUSED*/
8524 int
8525screen_del_lines(off, row, line_count, end, force, wp)
8526 int off;
8527 int row;
8528 int line_count;
8529 int end;
8530 int force; /* even when line_count > p_ttyscroll */
8531 win_T *wp; /* NULL or window to use width from */
8532{
8533 int j;
8534 int i;
8535 unsigned temp;
8536 int cursor_row;
8537 int cursor_end;
8538 int result_empty; /* result is empty until end of region */
8539 int can_delete; /* deleting line codes can be used */
8540 int type;
8541
8542 /*
8543 * FAIL if
8544 * - there is no valid screen
8545 * - the screen has to be redrawn completely
8546 * - the line count is less than one
8547 * - the line count is more than 'ttyscroll'
8548 */
8549 if (!screen_valid(TRUE) || line_count <= 0 ||
8550 (!force && line_count > p_ttyscroll))
8551 return FAIL;
8552
8553 /*
8554 * Check if the rest of the current region will become empty.
8555 */
8556 result_empty = row + line_count >= end;
8557
8558 /*
8559 * We can delete lines only when 'db' flag not set or when 'ce' option
8560 * available.
8561 */
8562 can_delete = (*T_DB == NUL || can_clear(T_CE));
8563
8564 /*
8565 * There are six ways to delete lines:
8566 * 0. When in a vertically split window and t_CV isn't set, redraw the
8567 * characters from ScreenLines[].
8568 * 1. Use T_CD if it exists and the result is empty.
8569 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8570 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8571 * none of the other ways work.
8572 * 4. Use T_CE (erase line) if the result is empty.
8573 * 5. Use T_DL (delete line) if it exists.
8574 * 6. redraw the characters from ScreenLines[].
8575 */
8576#ifdef FEAT_VERTSPLIT
8577 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8578 type = USE_REDRAW;
8579 else
8580#endif
8581 if (can_clear(T_CD) && result_empty)
8582 type = USE_T_CD;
8583#if defined(__BEOS__) && defined(BEOS_DR8)
8584 /*
8585 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8586 * its internal termcap... this works okay for tests which test *T_DB !=
8587 * NUL. It has the disadvantage that the user cannot use any :set t_*
8588 * command to get T_DB (back) to empty_option, only :set term=... will do
8589 * the trick...
8590 * Anyway, this hack will hopefully go away with the next OS release.
8591 * (Olaf Seibert)
8592 */
8593 else if (row == 0 && T_DB == empty_option
8594 && (line_count == 1 || *T_CDL == NUL))
8595#else
8596 else if (row == 0 && (
8597#ifndef AMIGA
8598 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8599 * up, so use delete-line command */
8600 line_count == 1 ||
8601#endif
8602 *T_CDL == NUL))
8603#endif
8604 type = USE_NL;
8605 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8606 type = USE_T_CDL;
8607 else if (can_clear(T_CE) && result_empty
8608#ifdef FEAT_VERTSPLIT
8609 && (wp == NULL || wp->w_width == Columns)
8610#endif
8611 )
8612 type = USE_T_CE;
8613 else if (*T_DL != NUL && can_delete)
8614 type = USE_T_DL;
8615 else if (*T_CDL != NUL && can_delete)
8616 type = USE_T_CDL;
8617 else
8618 return FAIL;
8619
8620#ifdef FEAT_CLIPBOARD
8621 /* Remove a modeless selection when deleting lines halfway the screen or
8622 * not the full width of the screen. */
8623 if (off + row > 0
8624# ifdef FEAT_VERTSPLIT
8625 || (wp != NULL && wp->w_width != Columns)
8626# endif
8627 )
8628 clip_clear_selection();
8629 else
8630 clip_scroll_selection(line_count);
8631#endif
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633#ifdef FEAT_GUI
8634 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8635 * scrolling is actually carried out. */
8636 gui_dont_update_cursor();
8637#endif
8638
8639 if (*T_CCS != NUL) /* cursor relative to region */
8640 {
8641 cursor_row = row;
8642 cursor_end = end;
8643 }
8644 else
8645 {
8646 cursor_row = row + off;
8647 cursor_end = end + off;
8648 }
8649
8650 /*
8651 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8652 * Clear the inserted lines in ScreenLines[].
8653 */
8654 row += off;
8655 end += off;
8656 for (i = 0; i < line_count; ++i)
8657 {
8658#ifdef FEAT_VERTSPLIT
8659 if (wp != NULL && wp->w_width != Columns)
8660 {
8661 /* need to copy part of a line */
8662 j = row + i;
8663 while ((j += line_count) <= end - 1)
8664 linecopy(j - line_count, j, wp);
8665 j -= line_count;
8666 if (can_clear((char_u *)" "))
8667 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8668 else
8669 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8670 LineWraps[j] = FALSE;
8671 }
8672 else
8673#endif
8674 {
8675 /* whole width, moving the line pointers is faster */
8676 j = row + i;
8677 temp = LineOffset[j];
8678 while ((j += line_count) <= end - 1)
8679 {
8680 LineOffset[j - line_count] = LineOffset[j];
8681 LineWraps[j - line_count] = LineWraps[j];
8682 }
8683 LineOffset[j - line_count] = temp;
8684 LineWraps[j - line_count] = FALSE;
8685 if (can_clear((char_u *)" "))
8686 lineclear(temp, (int)Columns);
8687 else
8688 lineinvalid(temp, (int)Columns);
8689 }
8690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 screen_stop_highlight();
8693
8694#ifdef FEAT_VERTSPLIT
8695 /* redraw the characters */
8696 if (type == USE_REDRAW)
8697 redraw_block(row, end, wp);
8698 else
8699#endif
8700 if (type == USE_T_CD) /* delete the lines */
8701 {
8702 windgoto(cursor_row, 0);
8703 out_str(T_CD);
8704 screen_start(); /* don't know where cursor is now */
8705 }
8706 else if (type == USE_T_CDL)
8707 {
8708 windgoto(cursor_row, 0);
8709 term_delete_lines(line_count);
8710 screen_start(); /* don't know where cursor is now */
8711 }
8712 /*
8713 * Deleting lines at top of the screen or scroll region: Just scroll
8714 * the whole screen (scroll region) up by outputting newlines on the
8715 * last line.
8716 */
8717 else if (type == USE_NL)
8718 {
8719 windgoto(cursor_end - 1, 0);
8720 for (i = line_count; --i >= 0; )
8721 out_char('\n'); /* cursor will remain on same line */
8722 }
8723 else
8724 {
8725 for (i = line_count; --i >= 0; )
8726 {
8727 if (type == USE_T_DL)
8728 {
8729 windgoto(cursor_row, 0);
8730 out_str(T_DL); /* delete a line */
8731 }
8732 else /* type == USE_T_CE */
8733 {
8734 windgoto(cursor_row + i, 0);
8735 out_str(T_CE); /* erase a line */
8736 }
8737 screen_start(); /* don't know where cursor is now */
8738 }
8739 }
8740
8741 /*
8742 * If the 'db' flag is set, we need to clear the lines that have been
8743 * scrolled up at the bottom of the region.
8744 */
8745 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8746 {
8747 for (i = line_count; i > 0; --i)
8748 {
8749 windgoto(cursor_end - i, 0);
8750 out_str(T_CE); /* erase a line */
8751 screen_start(); /* don't know where cursor is now */
8752 }
8753 }
8754
8755#ifdef FEAT_GUI
8756 gui_can_update_cursor();
8757 if (gui.in_use)
8758 out_flush(); /* always flush after a scroll */
8759#endif
8760
8761 return OK;
8762}
8763
8764/*
8765 * show the current mode and ruler
8766 *
8767 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8768 * If clear_cmdline is FALSE there may be a message there that needs to be
8769 * cleared only if a mode is shown.
8770 * Return the length of the message (0 if no message).
8771 */
8772 int
8773showmode()
8774{
8775 int need_clear;
8776 int length = 0;
8777 int do_mode;
8778 int attr;
8779 int nwr_save;
8780#ifdef FEAT_INS_EXPAND
8781 int sub_attr;
8782#endif
8783
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008784 do_mode = ((p_smd && msg_silent == 0)
8785 && ((State & INSERT)
8786 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787#ifdef FEAT_VISUAL
8788 || VIsual_active
8789#endif
8790 ));
8791 if (do_mode || Recording)
8792 {
8793 /*
8794 * Don't show mode right now, when not redrawing or inside a mapping.
8795 * Call char_avail() only when we are going to show something, because
8796 * it takes a bit of time.
8797 */
8798 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8799 {
8800 redraw_cmdline = TRUE; /* show mode later */
8801 return 0;
8802 }
8803
8804 nwr_save = need_wait_return;
8805
8806 /* wait a bit before overwriting an important message */
8807 check_for_delay(FALSE);
8808
8809 /* if the cmdline is more than one line high, erase top lines */
8810 need_clear = clear_cmdline;
8811 if (clear_cmdline && cmdline_row < Rows - 1)
8812 msg_clr_cmdline(); /* will reset clear_cmdline */
8813
8814 /* Position on the last line in the window, column 0 */
8815 msg_pos_mode();
8816 cursor_off();
8817 attr = hl_attr(HLF_CM); /* Highlight mode */
8818 if (do_mode)
8819 {
8820 MSG_PUTS_ATTR("--", attr);
8821#if defined(FEAT_XIM)
8822 if (xic != NULL && im_get_status() && !p_imdisable
8823 && curbuf->b_p_iminsert == B_IMODE_IM)
8824# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8825 MSG_PUTS_ATTR(" IM", attr);
8826# else
8827 MSG_PUTS_ATTR(" XIM", attr);
8828# endif
8829#endif
8830#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8831 if (gui.in_use)
8832 {
8833 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008834 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 }
8836#endif
8837#ifdef FEAT_INS_EXPAND
8838 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8839 {
8840 /* These messages can get long, avoid a wrap in a narrow
8841 * window. Prefer showing edit_submode_extra. */
8842 length = (Rows - msg_row) * Columns - 3;
8843 if (edit_submode_extra != NULL)
8844 length -= vim_strsize(edit_submode_extra);
8845 if (length > 0)
8846 {
8847 if (edit_submode_pre != NULL)
8848 length -= vim_strsize(edit_submode_pre);
8849 if (length - vim_strsize(edit_submode) > 0)
8850 {
8851 if (edit_submode_pre != NULL)
8852 msg_puts_attr(edit_submode_pre, attr);
8853 msg_puts_attr(edit_submode, attr);
8854 }
8855 if (edit_submode_extra != NULL)
8856 {
8857 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8858 if ((int)edit_submode_highl < (int)HLF_COUNT)
8859 sub_attr = hl_attr(edit_submode_highl);
8860 else
8861 sub_attr = attr;
8862 msg_puts_attr(edit_submode_extra, sub_attr);
8863 }
8864 }
8865 length = 0;
8866 }
8867 else
8868#endif
8869 {
8870#ifdef FEAT_VREPLACE
8871 if (State & VREPLACE_FLAG)
8872 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8873 else
8874#endif
8875 if (State & REPLACE_FLAG)
8876 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8877 else if (State & INSERT)
8878 {
8879#ifdef FEAT_RIGHTLEFT
8880 if (p_ri)
8881 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8882#endif
8883 MSG_PUTS_ATTR(_(" INSERT"), attr);
8884 }
8885 else if (restart_edit == 'I')
8886 MSG_PUTS_ATTR(_(" (insert)"), attr);
8887 else if (restart_edit == 'R')
8888 MSG_PUTS_ATTR(_(" (replace)"), attr);
8889 else if (restart_edit == 'V')
8890 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8891#ifdef FEAT_RIGHTLEFT
8892 if (p_hkmap)
8893 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8894# ifdef FEAT_FKMAP
8895 if (p_fkmap)
8896 MSG_PUTS_ATTR(farsi_text_5, attr);
8897# endif
8898#endif
8899#ifdef FEAT_KEYMAP
8900 if (State & LANGMAP)
8901 {
8902# ifdef FEAT_ARABIC
8903 if (curwin->w_p_arab)
8904 MSG_PUTS_ATTR(_(" Arabic"), attr);
8905 else
8906# endif
8907 MSG_PUTS_ATTR(_(" (lang)"), attr);
8908 }
8909#endif
8910 if ((State & INSERT) && p_paste)
8911 MSG_PUTS_ATTR(_(" (paste)"), attr);
8912
8913#ifdef FEAT_VISUAL
8914 if (VIsual_active)
8915 {
8916 char *p;
8917
8918 /* Don't concatenate separate words to avoid translation
8919 * problems. */
8920 switch ((VIsual_select ? 4 : 0)
8921 + (VIsual_mode == Ctrl_V) * 2
8922 + (VIsual_mode == 'V'))
8923 {
8924 case 0: p = N_(" VISUAL"); break;
8925 case 1: p = N_(" VISUAL LINE"); break;
8926 case 2: p = N_(" VISUAL BLOCK"); break;
8927 case 4: p = N_(" SELECT"); break;
8928 case 5: p = N_(" SELECT LINE"); break;
8929 default: p = N_(" SELECT BLOCK"); break;
8930 }
8931 MSG_PUTS_ATTR(_(p), attr);
8932 }
8933#endif
8934 MSG_PUTS_ATTR(" --", attr);
8935 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 need_clear = TRUE;
8938 }
8939 if (Recording
8940#ifdef FEAT_INS_EXPAND
8941 && edit_submode == NULL /* otherwise it gets too long */
8942#endif
8943 )
8944 {
8945 MSG_PUTS_ATTR(_("recording"), attr);
8946 need_clear = TRUE;
8947 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008948
8949 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 if (need_clear || clear_cmdline)
8951 msg_clr_eos();
8952 msg_didout = FALSE; /* overwrite this message */
8953 length = msg_col;
8954 msg_col = 0;
8955 need_wait_return = nwr_save; /* never ask for hit-return for this */
8956 }
8957 else if (clear_cmdline && msg_silent == 0)
8958 /* Clear the whole command line. Will reset "clear_cmdline". */
8959 msg_clr_cmdline();
8960
8961#ifdef FEAT_CMDL_INFO
8962# ifdef FEAT_VISUAL
8963 /* In Visual mode the size of the selected area must be redrawn. */
8964 if (VIsual_active)
8965 clear_showcmd();
8966# endif
8967
8968 /* If the last window has no status line, the ruler is after the mode
8969 * message and must be redrawn */
8970 if (redrawing()
8971# ifdef FEAT_WINDOWS
8972 && lastwin->w_status_height == 0
8973# endif
8974 )
8975 win_redr_ruler(lastwin, TRUE);
8976#endif
8977 redraw_cmdline = FALSE;
8978 clear_cmdline = FALSE;
8979
8980 return length;
8981}
8982
8983/*
8984 * Position for a mode message.
8985 */
8986 static void
8987msg_pos_mode()
8988{
8989 msg_col = 0;
8990 msg_row = Rows - 1;
8991}
8992
8993/*
8994 * Delete mode message. Used when ESC is typed which is expected to end
8995 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008996 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 */
8998 void
8999unshowmode(force)
9000 int force;
9001{
9002 /*
9003 * Don't delete it right now, when not redrawing or insided a mapping.
9004 */
9005 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9006 redraw_cmdline = TRUE; /* delete mode later */
9007 else
9008 {
9009 msg_pos_mode();
9010 if (Recording)
9011 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9012 msg_clr_eos();
9013 }
9014}
9015
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009016#if defined(FEAT_WINDOWS)
9017/*
9018 * Draw the tab pages line at the top of the Vim window.
9019 */
9020 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009021draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009022{
9023 int tabcount = 0;
9024 tabpage_T *tp;
9025 int tabwidth;
9026 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009027 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009028 int attr;
9029 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009030 win_T *cwp;
9031 int wincount;
9032 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009033 int c;
9034 int len;
9035 int attr_sel = hl_attr(HLF_TPS);
9036 int attr_nosel = hl_attr(HLF_TP);
9037 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009038 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009039 int room;
9040 int use_sep_chars = (t_colors < 8
9041#ifdef FEAT_GUI
9042 && !gui.in_use
9043#endif
9044 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009045
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009046 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009047
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009048#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009049 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009050 if (gui_use_tabline())
9051 {
9052 gui_update_tabline();
9053 return;
9054 }
9055#endif
9056
9057 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009058 return;
9059
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009060#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009061
9062 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9063 for (scol = 0; scol < Columns; ++scol)
9064 TabPageIdxs[scol] = 0;
9065
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009066 /* Use the 'tabline' option if it's set. */
9067 if (*p_tal != NUL)
9068 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009069 int save_called_emsg = called_emsg;
9070
9071 /* Check for an error. If there is one we would loop in redrawing the
9072 * screen. Avoid that by making 'tabline' empty. */
9073 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009074 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009075 if (called_emsg)
9076 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009077 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009078 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009079 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009080 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009081#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009082 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009083 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9084 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009085
Bram Moolenaar238a5642006-02-21 22:12:05 +00009086 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9087 if (tabwidth < 6)
9088 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009089
Bram Moolenaar238a5642006-02-21 22:12:05 +00009090 attr = attr_nosel;
9091 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009092 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009093 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9094 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009095 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009096 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009097
Bram Moolenaar238a5642006-02-21 22:12:05 +00009098 if (tp->tp_topframe == topframe)
9099 attr = attr_sel;
9100 if (use_sep_chars && col > 0)
9101 screen_putchar('|', 0, col++, attr);
9102
9103 if (tp->tp_topframe != topframe)
9104 attr = attr_nosel;
9105
9106 screen_putchar(' ', 0, col++, attr);
9107
9108 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009109 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009110 cwp = curwin;
9111 wp = firstwin;
9112 }
9113 else
9114 {
9115 cwp = tp->tp_curwin;
9116 wp = tp->tp_firstwin;
9117 }
9118
9119 modified = FALSE;
9120 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9121 if (bufIsChanged(wp->w_buffer))
9122 modified = TRUE;
9123 if (modified || wincount > 1)
9124 {
9125 if (wincount > 1)
9126 {
9127 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009128 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009129 if (col + len >= Columns - 3)
9130 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009131 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009132#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009133 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009134#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009135 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009136#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009137 );
9138 col += len;
9139 }
9140 if (modified)
9141 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9142 screen_putchar(' ', 0, col++, attr);
9143 }
9144
9145 room = scol - col + tabwidth - 1;
9146 if (room > 0)
9147 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009148 /* Get buffer name in NameBuff[] */
9149 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009150 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009151 len = vim_strsize(NameBuff);
9152 p = NameBuff;
9153#ifdef FEAT_MBYTE
9154 if (has_mbyte)
9155 while (len > room)
9156 {
9157 len -= ptr2cells(p);
9158 mb_ptr_adv(p);
9159 }
9160 else
9161#endif
9162 if (len > room)
9163 {
9164 p += len - room;
9165 len = room;
9166 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009167 if (len > Columns - col - 1)
9168 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009169
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009170 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009171 col += len;
9172 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009173 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009174
9175 /* Store the tab page number in TabPageIdxs[], so that
9176 * jump_to_mouse() knows where each one is. */
9177 ++tabcount;
9178 while (scol < col)
9179 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009180 }
9181
Bram Moolenaar238a5642006-02-21 22:12:05 +00009182 if (use_sep_chars)
9183 c = '_';
9184 else
9185 c = ' ';
9186 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009187
9188 /* Put an "X" for closing the current tab if there are several. */
9189 if (first_tabpage->tp_next != NULL)
9190 {
9191 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9192 TabPageIdxs[Columns - 1] = -999;
9193 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009194 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009195
9196 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9197 * set. */
9198 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009199}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009200
9201/*
9202 * Get buffer name for "buf" into NameBuff[].
9203 * Takes care of special buffer names and translates special characters.
9204 */
9205 void
9206get_trans_bufname(buf)
9207 buf_T *buf;
9208{
9209 if (buf_spname(buf) != NULL)
9210 STRCPY(NameBuff, buf_spname(buf));
9211 else
9212 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9213 trans_characters(NameBuff, MAXPATHL);
9214}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009215#endif
9216
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9218/*
9219 * Get the character to use in a status line. Get its attributes in "*attr".
9220 */
9221 static int
9222fillchar_status(attr, is_curwin)
9223 int *attr;
9224 int is_curwin;
9225{
9226 int fill;
9227 if (is_curwin)
9228 {
9229 *attr = hl_attr(HLF_S);
9230 fill = fill_stl;
9231 }
9232 else
9233 {
9234 *attr = hl_attr(HLF_SNC);
9235 fill = fill_stlnc;
9236 }
9237 /* Use fill when there is highlighting, and highlighting of current
9238 * window differs, or the fillchars differ, or this is not the
9239 * current window */
9240 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9241 || !is_curwin || firstwin == lastwin)
9242 || (fill_stl != fill_stlnc)))
9243 return fill;
9244 if (is_curwin)
9245 return '^';
9246 return '=';
9247}
9248#endif
9249
9250#ifdef FEAT_VERTSPLIT
9251/*
9252 * Get the character to use in a separator between vertically split windows.
9253 * Get its attributes in "*attr".
9254 */
9255 static int
9256fillchar_vsep(attr)
9257 int *attr;
9258{
9259 *attr = hl_attr(HLF_C);
9260 if (*attr == 0 && fill_vert == ' ')
9261 return '|';
9262 else
9263 return fill_vert;
9264}
9265#endif
9266
9267/*
9268 * Return TRUE if redrawing should currently be done.
9269 */
9270 int
9271redrawing()
9272{
9273 return (!RedrawingDisabled
9274 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9275}
9276
9277/*
9278 * Return TRUE if printing messages should currently be done.
9279 */
9280 int
9281messaging()
9282{
9283 return (!(p_lz && char_avail() && !KeyTyped));
9284}
9285
9286/*
9287 * Show current status info in ruler and various other places
9288 * If always is FALSE, only show ruler if position has changed.
9289 */
9290 void
9291showruler(always)
9292 int always;
9293{
9294 if (!always && !redrawing())
9295 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009296#ifdef FEAT_INS_EXPAND
9297 if (pum_visible())
9298 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009299# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009300 /* Don't redraw right now, do it later. */
9301 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009302# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009303 return;
9304 }
9305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009307 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009308 {
9309 redraw_custum_statusline(curwin);
9310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 else
9312#endif
9313#ifdef FEAT_CMDL_INFO
9314 win_redr_ruler(curwin, always);
9315#endif
9316
9317#ifdef FEAT_TITLE
9318 if (need_maketitle
9319# ifdef FEAT_STL_OPT
9320 || (p_icon && (stl_syntax & STL_IN_ICON))
9321 || (p_title && (stl_syntax & STL_IN_TITLE))
9322# endif
9323 )
9324 maketitle();
9325#endif
9326}
9327
9328#ifdef FEAT_CMDL_INFO
9329 static void
9330win_redr_ruler(wp, always)
9331 win_T *wp;
9332 int always;
9333{
9334 char_u buffer[70];
9335 int row;
9336 int fillchar;
9337 int attr;
9338 int empty_line = FALSE;
9339 colnr_T virtcol;
9340 int i;
9341 int o;
9342#ifdef FEAT_VERTSPLIT
9343 int this_ru_col;
9344 int off = 0;
9345 int width = Columns;
9346# define WITH_OFF(x) x
9347# define WITH_WIDTH(x) x
9348#else
9349# define WITH_OFF(x) 0
9350# define WITH_WIDTH(x) Columns
9351# define this_ru_col ru_col
9352#endif
9353
9354 /* If 'ruler' off or redrawing disabled, don't do anything */
9355 if (!p_ru)
9356 return;
9357
9358 /*
9359 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9360 * after deleting lines, before cursor.lnum is corrected.
9361 */
9362 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9363 return;
9364
9365#ifdef FEAT_INS_EXPAND
9366 /* Don't draw the ruler while doing insert-completion, it might overwrite
9367 * the (long) mode message. */
9368# ifdef FEAT_WINDOWS
9369 if (wp == lastwin && lastwin->w_status_height == 0)
9370# endif
9371 if (edit_submode != NULL)
9372 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009373 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9374 if (pum_visible())
9375 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376#endif
9377
9378#ifdef FEAT_STL_OPT
9379 if (*p_ruf)
9380 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009381 int save_called_emsg = called_emsg;
9382
9383 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009385 if (called_emsg)
9386 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009387 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009388 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 return;
9390 }
9391#endif
9392
9393 /*
9394 * Check if not in Insert mode and the line is empty (will show "0-1").
9395 */
9396 if (!(State & INSERT)
9397 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9398 empty_line = TRUE;
9399
9400 /*
9401 * Only draw the ruler when something changed.
9402 */
9403 validate_virtcol_win(wp);
9404 if ( redraw_cmdline
9405 || always
9406 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9407 || wp->w_cursor.col != wp->w_ru_cursor.col
9408 || wp->w_virtcol != wp->w_ru_virtcol
9409#ifdef FEAT_VIRTUALEDIT
9410 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9411#endif
9412 || wp->w_topline != wp->w_ru_topline
9413 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9414#ifdef FEAT_DIFF
9415 || wp->w_topfill != wp->w_ru_topfill
9416#endif
9417 || empty_line != wp->w_ru_empty)
9418 {
9419 cursor_off();
9420#ifdef FEAT_WINDOWS
9421 if (wp->w_status_height)
9422 {
9423 row = W_WINROW(wp) + wp->w_height;
9424 fillchar = fillchar_status(&attr, wp == curwin);
9425# ifdef FEAT_VERTSPLIT
9426 off = W_WINCOL(wp);
9427 width = W_WIDTH(wp);
9428# endif
9429 }
9430 else
9431#endif
9432 {
9433 row = Rows - 1;
9434 fillchar = ' ';
9435 attr = 0;
9436#ifdef FEAT_VERTSPLIT
9437 width = Columns;
9438 off = 0;
9439#endif
9440 }
9441
9442 /* In list mode virtcol needs to be recomputed */
9443 virtcol = wp->w_virtcol;
9444 if (wp->w_p_list && lcs_tab1 == NUL)
9445 {
9446 wp->w_p_list = FALSE;
9447 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9448 wp->w_p_list = TRUE;
9449 }
9450
9451 /*
9452 * Some sprintfs return the length, some return a pointer.
9453 * To avoid portability problems we use strlen() here.
9454 */
9455 sprintf((char *)buffer, "%ld,",
9456 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9457 ? 0L
9458 : (long)(wp->w_cursor.lnum));
9459 col_print(buffer + STRLEN(buffer),
9460 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9461 (int)virtcol + 1);
9462
9463 /*
9464 * Add a "50%" if there is room for it.
9465 * On the last line, don't print in the last column (scrolls the
9466 * screen up on some terminals).
9467 */
9468 i = (int)STRLEN(buffer);
9469 get_rel_pos(wp, buffer + i + 1);
9470 o = i + vim_strsize(buffer + i + 1);
9471#ifdef FEAT_WINDOWS
9472 if (wp->w_status_height == 0) /* can't use last char of screen */
9473#endif
9474 ++o;
9475#ifdef FEAT_VERTSPLIT
9476 this_ru_col = ru_col - (Columns - width);
9477 if (this_ru_col < 0)
9478 this_ru_col = 0;
9479#endif
9480 /* Never use more than half the window/screen width, leave the other
9481 * half for the filename. */
9482 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9483 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9484 if (this_ru_col + o < WITH_WIDTH(width))
9485 {
9486 while (this_ru_col + o < WITH_WIDTH(width))
9487 {
9488#ifdef FEAT_MBYTE
9489 if (has_mbyte)
9490 i += (*mb_char2bytes)(fillchar, buffer + i);
9491 else
9492#endif
9493 buffer[i++] = fillchar;
9494 ++o;
9495 }
9496 get_rel_pos(wp, buffer + i);
9497 }
9498 /* Truncate at window boundary. */
9499#ifdef FEAT_MBYTE
9500 if (has_mbyte)
9501 {
9502 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009503 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 {
9505 o += (*mb_ptr2cells)(buffer + i);
9506 if (this_ru_col + o > WITH_WIDTH(width))
9507 {
9508 buffer[i] = NUL;
9509 break;
9510 }
9511 }
9512 }
9513 else
9514#endif
9515 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9516 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9517
9518 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9519 i = redraw_cmdline;
9520 screen_fill(row, row + 1,
9521 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9522 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9523 fillchar, fillchar, attr);
9524 /* don't redraw the cmdline because of showing the ruler */
9525 redraw_cmdline = i;
9526 wp->w_ru_cursor = wp->w_cursor;
9527 wp->w_ru_virtcol = wp->w_virtcol;
9528 wp->w_ru_empty = empty_line;
9529 wp->w_ru_topline = wp->w_topline;
9530 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9531#ifdef FEAT_DIFF
9532 wp->w_ru_topfill = wp->w_topfill;
9533#endif
9534 }
9535}
9536#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009537
9538#if defined(FEAT_LINEBREAK) || defined(PROTO)
9539/*
9540 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009541 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009542 * Otherwise it depends on 'numberwidth' and the line count.
9543 */
9544 int
9545number_width(wp)
9546 win_T *wp;
9547{
9548 int n;
9549 linenr_T lnum;
9550
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009551 lnum = wp->w_buffer->b_ml.ml_line_count;
9552 if (lnum == wp->w_nrwidth_line_count)
9553 return wp->w_nrwidth_width;
9554 wp->w_nrwidth_line_count = lnum;
9555
9556 n = 0;
9557 do
9558 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009559 lnum /= 10;
9560 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009561 } while (lnum > 0);
9562
9563 /* 'numberwidth' gives the minimal width plus one */
9564 if (n < wp->w_p_nuw - 1)
9565 n = wp->w_p_nuw - 1;
9566
9567 wp->w_nrwidth_width = n;
9568 return n;
9569}
9570#endif