blob: c2d7c20299bb24495fc49b2a5ffded0b6de68aae [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000030 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
34 *
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
37 *
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000040 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 *
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
47 *
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
51 *
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
56 *
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
63 *
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
68 *
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
72 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000073 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
77 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
80 *
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
83 *
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
87 */
88
89#include "vim.h"
90
91/*
92 * The attributes that are actually active for writing to the screen.
93 */
94static int screen_attr = 0;
95
96/*
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
99 */
100static int screen_cur_row, screen_cur_col; /* last known cursor position */
101
102#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
106#ifdef FEAT_FOLDING
107static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108#endif
109
110/*
111 * Buffer for one screen line (characters and attributes).
112 */
113static schar_T *current_ScreenLine;
114
115static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000116static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_FOLDING
118static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000122static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124#ifdef FEAT_RIGHTLEFT
125static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127#else
128static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#ifdef FEAT_VERTSPLIT
132static void draw_vsep_win __ARGS((win_T *wp, int row));
133#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
135static void redraw_custum_statusline __ARGS((win_T *wp));
136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000138#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void start_search_hl __ARGS((void));
140static void end_search_hl __ARGS((void));
141static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143#endif
144static void screen_start_highlight __ARGS((int attr));
145static void screen_char __ARGS((unsigned off, int row, int col));
146#ifdef FEAT_MBYTE
147static void screen_char_2 __ARGS((unsigned off, int row, int col));
148#endif
149static void screenclear2 __ARGS((void));
150static void lineclear __ARGS((unsigned off, int width));
151static void lineinvalid __ARGS((unsigned off, int width));
152#ifdef FEAT_VERTSPLIT
153static void linecopy __ARGS((int to, int from, win_T *wp));
154static void redraw_block __ARGS((int row, int end, win_T *wp));
155#endif
156static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000160static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163static int fillchar_status __ARGS((int *attr, int is_curwin));
164#endif
165#ifdef FEAT_VERTSPLIT
166static int fillchar_vsep __ARGS((int *attr));
167#endif
168#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000169static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#endif
171#ifdef FEAT_CMDL_INFO
172static void win_redr_ruler __ARGS((win_T *wp, int always));
173#endif
174
175#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176/* Ugly global: overrule attribute used by screen_char() */
177static int screen_char_attr = 0;
178#endif
179
180/*
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
184 */
185 void
186redraw_later(type)
187 int type;
188{
189 redraw_win_later(curwin, type);
190}
191
192 void
193redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
196{
197 if (wp->w_redr_type < type)
198 {
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
204 }
205}
206
207/*
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
210 */
211 void
212redraw_later_clear()
213{
214 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000215#ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221#endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224}
225
226/*
227 * Mark all windows to be redrawn later.
228 */
229 void
230redraw_all_later(type)
231 int type;
232{
233 win_T *wp;
234
235 FOR_ALL_WINDOWS(wp)
236 {
237 redraw_win_later(wp, type);
238 }
239}
240
241/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000242 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 */
244 void
245redraw_curbuf_later(type)
246 int type;
247{
248 redraw_buf_later(curbuf, type);
249}
250
251 void
252redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
255{
256 win_T *wp;
257
258 FOR_ALL_WINDOWS(wp)
259 {
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
262 }
263}
264
265/*
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
272 */
273/*ARGSUSED*/
274 void
275redrawWinline(lnum, invalid)
276 linenr_T lnum;
277 int invalid; /* window line height is invalid now */
278{
279#ifdef FEAT_FOLDING
280 int i;
281#endif
282
283 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
284 curwin->w_redraw_top = lnum;
285 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
286 curwin->w_redraw_bot = lnum;
287 redraw_later(VALID);
288
289#ifdef FEAT_FOLDING
290 if (invalid)
291 {
292 /* A w_lines[] entry for this lnum has become invalid. */
293 i = find_wl_entry(curwin, lnum);
294 if (i >= 0)
295 curwin->w_lines[i].wl_valid = FALSE;
296 }
297#endif
298}
299
300/*
301 * update all windows that are editing the current buffer
302 */
303 void
304update_curbuf(type)
305 int type;
306{
307 redraw_curbuf_later(type);
308 update_screen(type);
309}
310
311/*
312 * update_screen()
313 *
314 * Based on the current value of curwin->w_topline, transfer a screenfull
315 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
316 */
317 void
318update_screen(type)
319 int type;
320{
321 win_T *wp;
322 static int did_intro = FALSE;
323#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
324 int did_one;
325#endif
326
327 if (!screen_valid(TRUE))
328 return;
329
330 if (must_redraw)
331 {
332 if (type < must_redraw) /* use maximal type */
333 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000334
335 /* must_redraw is reset here, so that when we run into some weird
336 * reason to redraw while busy redrawing (e.g., asynchronous
337 * scrolling), or update_topline() in win_update() will cause a
338 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 must_redraw = 0;
340 }
341
342 /* Need to update w_lines[]. */
343 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
344 type = NOT_VALID;
345
346 if (!redrawing())
347 {
348 redraw_later(type); /* remember type for next time */
349 must_redraw = type;
350 if (type > INVERTED_ALL)
351 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
352 return;
353 }
354
355 updating_screen = TRUE;
356#ifdef FEAT_SYN_HL
357 ++display_tick; /* let syntax code know we're in a next round of
358 * display updating */
359#endif
360
361 /*
362 * if the screen was scrolled up when displaying a message, scroll it down
363 */
364 if (msg_scrolled)
365 {
366 clear_cmdline = TRUE;
367 if (msg_scrolled > Rows - 5) /* clearing is faster */
368 type = CLEAR;
369 else if (type != CLEAR)
370 {
371 check_for_delay(FALSE);
372 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
373 type = CLEAR;
374 FOR_ALL_WINDOWS(wp)
375 {
376 if (W_WINROW(wp) < msg_scrolled)
377 {
378 if (W_WINROW(wp) + wp->w_height > msg_scrolled
379 && wp->w_redr_type < REDRAW_TOP
380 && wp->w_lines_valid > 0
381 && wp->w_topline == wp->w_lines[0].wl_lnum)
382 {
383 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
384 wp->w_redr_type = REDRAW_TOP;
385 }
386 else
387 {
388 wp->w_redr_type = NOT_VALID;
389#ifdef FEAT_WINDOWS
390 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
391 <= msg_scrolled)
392 wp->w_redr_status = TRUE;
393#endif
394 }
395 }
396 }
397 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000398#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000399 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 }
402 msg_scrolled = 0;
403 need_wait_return = FALSE;
404 }
405
406 /* reset cmdline_row now (may have been changed temporarily) */
407 compute_cmdrow();
408
409 /* Check for changed highlighting */
410 if (need_highlight_changed)
411 highlight_changed();
412
413 if (type == CLEAR) /* first clear screen */
414 {
415 screenclear(); /* will reset clear_cmdline */
416 type = NOT_VALID;
417 }
418
419 if (clear_cmdline) /* going to clear cmdline (done below) */
420 check_for_delay(FALSE);
421
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000422#ifdef FEAT_LINEBREAK
423 /* Force redraw when width of 'number' column changes. */
424 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000425 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000426 curwin->w_redr_type = NOT_VALID;
427#endif
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 /*
430 * Only start redrawing if there is really something to do.
431 */
432 if (type == INVERTED)
433 update_curswant();
434 if (curwin->w_redr_type < type
435 && !((type == VALID
436 && curwin->w_lines[0].wl_valid
437#ifdef FEAT_DIFF
438 && curwin->w_topfill == curwin->w_old_topfill
439 && curwin->w_botfill == curwin->w_old_botfill
440#endif
441 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
442#ifdef FEAT_VISUAL
443 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000444 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
446 && curwin->w_old_visual_mode == VIsual_mode
447 && (curwin->w_valid & VALID_VIRTCOL)
448 && curwin->w_old_curswant == curwin->w_curswant)
449#endif
450 ))
451 curwin->w_redr_type = type;
452
Bram Moolenaar5a305422006-04-28 22:38:25 +0000453#ifdef FEAT_WINDOWS
454 /* Redraw the tab pages line if needed. */
455 if (redraw_tabline || type >= NOT_VALID)
456 draw_tabline();
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#ifdef FEAT_SYN_HL
460 /*
461 * Correct stored syntax highlighting info for changes in each displayed
462 * buffer. Each buffer must only be done once.
463 */
464 FOR_ALL_WINDOWS(wp)
465 {
466 if (wp->w_buffer->b_mod_set)
467 {
468# ifdef FEAT_WINDOWS
469 win_T *wwp;
470
471 /* Check if we already did this buffer. */
472 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
473 if (wwp->w_buffer == wp->w_buffer)
474 break;
475# endif
476 if (
477# ifdef FEAT_WINDOWS
478 wwp == wp &&
479# endif
480 syntax_present(wp->w_buffer))
481 syn_stack_apply_changes(wp->w_buffer);
482 }
483 }
484#endif
485
486 /*
487 * Go from top to bottom through the windows, redrawing the ones that need
488 * it.
489 */
490#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
491 did_one = FALSE;
492#endif
493#ifdef FEAT_SEARCH_EXTRA
494 search_hl.rm.regprog = NULL;
495#endif
496 FOR_ALL_WINDOWS(wp)
497 {
498 if (wp->w_redr_type != 0)
499 {
500 cursor_off();
501#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
502 if (!did_one)
503 {
504 did_one = TRUE;
505# ifdef FEAT_SEARCH_EXTRA
506 start_search_hl();
507# endif
508# ifdef FEAT_CLIPBOARD
509 /* When Visual area changed, may have to update selection. */
510 if (clip_star.available && clip_isautosel())
511 clip_update_selection();
512# endif
513#ifdef FEAT_GUI
514 /* Remove the cursor before starting to do anything, because
515 * scrolling may make it difficult to redraw the text under
516 * it. */
517 if (gui.in_use)
518 gui_undraw_cursor();
519#endif
520 }
521#endif
522 win_update(wp);
523 }
524
525#ifdef FEAT_WINDOWS
526 /* redraw status line after the window to minimize cursor movement */
527 if (wp->w_redr_status)
528 {
529 cursor_off();
530 win_redr_status(wp);
531 }
532#endif
533 }
534#if defined(FEAT_SEARCH_EXTRA)
535 end_search_hl();
536#endif
537
538#ifdef FEAT_WINDOWS
539 /* Reset b_mod_set flags. Going through all windows is probably faster
540 * than going through all buffers (there could be many buffers). */
541 for (wp = firstwin; wp != NULL; wp = wp->w_next)
542 wp->w_buffer->b_mod_set = FALSE;
543#else
544 curbuf->b_mod_set = FALSE;
545#endif
546
547 updating_screen = FALSE;
548#ifdef FEAT_GUI
549 gui_may_resize_shell();
550#endif
551
552 /* Clear or redraw the command line. Done last, because scrolling may
553 * mess up the command line. */
554 if (clear_cmdline || redraw_cmdline)
555 showmode();
556
557 /* May put up an introductory message when not editing a file */
558 if (!did_intro && bufempty()
559 && curbuf->b_fname == NULL
560#ifdef FEAT_WINDOWS
561 && firstwin->w_next == NULL
562#endif
563 && vim_strchr(p_shm, SHM_INTRO) == NULL)
564 intro_message(FALSE);
565 did_intro = TRUE;
566
567#ifdef FEAT_GUI
568 /* Redraw the cursor and update the scrollbars when all screen updating is
569 * done. */
570 if (gui.in_use)
571 {
572 out_flush(); /* required before updating the cursor */
573 if (did_one)
574 gui_update_cursor(FALSE, FALSE);
575 gui_update_scrollbars(FALSE);
576 }
577#endif
578}
579
580#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
581static void update_prepare __ARGS((void));
582static void update_finish __ARGS((void));
583
584/*
585 * Prepare for updating one or more windows.
586 */
587 static void
588update_prepare()
589{
590 cursor_off();
591 updating_screen = TRUE;
592#ifdef FEAT_GUI
593 /* Remove the cursor before starting to do anything, because scrolling may
594 * make it difficult to redraw the text under it. */
595 if (gui.in_use)
596 gui_undraw_cursor();
597#endif
598#ifdef FEAT_SEARCH_EXTRA
599 start_search_hl();
600#endif
601}
602
603/*
604 * Finish updating one or more windows.
605 */
606 static void
607update_finish()
608{
609 if (redraw_cmdline)
610 showmode();
611
612# ifdef FEAT_SEARCH_EXTRA
613 end_search_hl();
614# endif
615
616 updating_screen = FALSE;
617
618# ifdef FEAT_GUI
619 gui_may_resize_shell();
620
621 /* Redraw the cursor and update the scrollbars when all screen updating is
622 * done. */
623 if (gui.in_use)
624 {
625 out_flush(); /* required before updating the cursor */
626 gui_update_cursor(FALSE, FALSE);
627 gui_update_scrollbars(FALSE);
628 }
629# endif
630}
631#endif
632
633#if defined(FEAT_SIGNS) || defined(PROTO)
634 void
635update_debug_sign(buf, lnum)
636 buf_T *buf;
637 linenr_T lnum;
638{
639 win_T *wp;
640 int doit = FALSE;
641
642# ifdef FEAT_FOLDING
643 win_foldinfo.fi_level = 0;
644# endif
645
646 /* update/delete a specific mark */
647 FOR_ALL_WINDOWS(wp)
648 {
649 if (buf != NULL && lnum > 0)
650 {
651 if (wp->w_buffer == buf && lnum >= wp->w_topline
652 && lnum < wp->w_botline)
653 {
654 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
655 wp->w_redraw_top = lnum;
656 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
657 wp->w_redraw_bot = lnum;
658 redraw_win_later(wp, VALID);
659 }
660 }
661 else
662 redraw_win_later(wp, VALID);
663 if (wp->w_redr_type != 0)
664 doit = TRUE;
665 }
666
667 if (!doit)
668 return;
669
670 /* update all windows that need updating */
671 update_prepare();
672
673# ifdef FEAT_WINDOWS
674 for (wp = firstwin; wp; wp = wp->w_next)
675 {
676 if (wp->w_redr_type != 0)
677 win_update(wp);
678 if (wp->w_redr_status)
679 win_redr_status(wp);
680 }
681# else
682 if (curwin->w_redr_type != 0)
683 win_update(curwin);
684# endif
685
686 update_finish();
687}
688#endif
689
690
691#if defined(FEAT_GUI) || defined(PROTO)
692/*
693 * Update a single window, its status line and maybe the command line msg.
694 * Used for the GUI scrollbar.
695 */
696 void
697updateWindow(wp)
698 win_T *wp;
699{
700 update_prepare();
701
702#ifdef FEAT_CLIPBOARD
703 /* When Visual area changed, may have to update selection. */
704 if (clip_star.available && clip_isautosel())
705 clip_update_selection();
706#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000711 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000712 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000713 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (wp->w_redr_status
716# ifdef FEAT_CMDL_INFO
717 || p_ru
718# endif
719# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000720 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721# endif
722 )
723 win_redr_status(wp);
724#endif
725
726 update_finish();
727}
728#endif
729
730/*
731 * Update a single window.
732 *
733 * This may cause the windows below it also to be redrawn (when clearing the
734 * screen or scrolling lines).
735 *
736 * How the window is redrawn depends on wp->w_redr_type. Each type also
737 * implies the one below it.
738 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000739 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
741 * INVERTED redraw the changed part of the Visual area
742 * INVERTED_ALL redraw the whole Visual area
743 * VALID 1. scroll up/down to adjust for a changed w_topline
744 * 2. update lines at the top when scrolled down
745 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000746 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * b_mod_top and b_mod_bot.
748 * - if wp->w_redraw_top non-zero, redraw lines between
749 * wp->w_redraw_top and wp->w_redr_bot.
750 * - continue redrawing when syntax status is invalid.
751 * 4. if scrolled up, update lines at the bottom.
752 * This results in three areas that may need updating:
753 * top: from first row to top_end (when scrolled down)
754 * mid: from mid_start to mid_end (update inversion or changed text)
755 * bot: from bot_start to last row (when scrolled up)
756 */
757 static void
758win_update(wp)
759 win_T *wp;
760{
761 buf_T *buf = wp->w_buffer;
762 int type;
763 int top_end = 0; /* Below last row of the top area that needs
764 updating. 0 when no top area updating. */
765 int mid_start = 999;/* first row of the mid area that needs
766 updating. 999 when no mid area updating. */
767 int mid_end = 0; /* Below last row of the mid area that needs
768 updating. 0 when no mid area updating. */
769 int bot_start = 999;/* first row of the bot area that needs
770 updating. 999 when no bot area updating */
771#ifdef FEAT_VISUAL
772 int scrolled_down = FALSE; /* TRUE when scrolled down when
773 w_topline got smaller a bit */
774#endif
775#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000776 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 int top_to_mod = FALSE; /* redraw above mod_top */
778#endif
779
780 int row; /* current window row to display */
781 linenr_T lnum; /* current buffer lnum to display */
782 int idx; /* current index in w_lines[] */
783 int srow; /* starting row of the current line */
784
785 int eof = FALSE; /* if TRUE, we hit the end of the file */
786 int didline = FALSE; /* if TRUE, we finished the last line */
787 int i;
788 long j;
789 static int recursive = FALSE; /* being called recursively */
790 int old_botline = wp->w_botline;
791#ifdef FEAT_FOLDING
792 long fold_count;
793#endif
794#ifdef FEAT_SYN_HL
795 /* remember what happened to the previous line, to know if
796 * check_visual_highlight() can be used */
797#define DID_NONE 1 /* didn't update a line */
798#define DID_LINE 2 /* updated a normal line */
799#define DID_FOLD 3 /* updated a folded line */
800 int did_update = DID_NONE;
801 linenr_T syntax_last_parsed = 0; /* last parsed text line */
802#endif
803 linenr_T mod_top = 0;
804 linenr_T mod_bot = 0;
805#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
806 int save_got_int;
807#endif
808
809 type = wp->w_redr_type;
810
811 if (type == NOT_VALID)
812 {
813#ifdef FEAT_WINDOWS
814 wp->w_redr_status = TRUE;
815#endif
816 wp->w_lines_valid = 0;
817 }
818
819 /* Window is zero-height: nothing to draw. */
820 if (wp->w_height == 0)
821 {
822 wp->w_redr_type = 0;
823 return;
824 }
825
826#ifdef FEAT_VERTSPLIT
827 /* Window is zero-width: Only need to draw the separator. */
828 if (wp->w_width == 0)
829 {
830 /* draw the vertical separator right of this window */
831 draw_vsep_win(wp, 0);
832 wp->w_redr_type = 0;
833 return;
834 }
835#endif
836
837#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000838 /* Setup for match and 'hlsearch' highlighting. Disable any previous
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000839 * match */
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000840 cur = wp->w_match_head;
841 while (cur != NULL)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000842 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000843 cur->hl.rm = cur->match;
844 if (cur->hlg_id == 0)
845 cur->hl.attr = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000846 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000847 cur->hl.attr = syn_id2attr(cur->hlg_id);
848 cur->hl.buf = buf;
849 cur->hl.lnum = 0;
850 cur->hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000851# ifdef FEAT_RELTIME
852 /* Set the time limit to 'redrawtime'. */
853 profile_setlimit(p_rdt, &(cur->hl.tm));
854# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000855 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 search_hl.buf = buf;
858 search_hl.lnum = 0;
859 search_hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000860 /* time limit is set at the toplevel, for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#endif
862
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000863#ifdef FEAT_LINEBREAK
864 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000865 i = wp->w_p_nu ? number_width(wp) : 0;
866 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000867 {
868 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000869 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000870 }
871 else
872#endif
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
875 {
876 /*
877 * When there are both inserted/deleted lines and specific lines to be
878 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
879 * everything (only happens when redrawing is off for while).
880 */
881 type = NOT_VALID;
882 }
883 else
884 {
885 /*
886 * Set mod_top to the first line that needs displaying because of
887 * changes. Set mod_bot to the first line after the changes.
888 */
889 mod_top = wp->w_redraw_top;
890 if (wp->w_redraw_bot != 0)
891 mod_bot = wp->w_redraw_bot + 1;
892 else
893 mod_bot = 0;
894 wp->w_redraw_top = 0; /* reset for next time */
895 wp->w_redraw_bot = 0;
896 if (buf->b_mod_set)
897 {
898 if (mod_top == 0 || mod_top > buf->b_mod_top)
899 {
900 mod_top = buf->b_mod_top;
901#ifdef FEAT_SYN_HL
902 /* Need to redraw lines above the change that may be included
903 * in a pattern match. */
904 if (syntax_present(buf))
905 {
906 mod_top -= buf->b_syn_sync_linebreaks;
907 if (mod_top < 1)
908 mod_top = 1;
909 }
910#endif
911 }
912 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
913 mod_bot = buf->b_mod_bot;
914
915#ifdef FEAT_SEARCH_EXTRA
916 /* When 'hlsearch' is on and using a multi-line search pattern, a
917 * change in one line may make the Search highlighting in a
918 * previous line invalid. Simple solution: redraw all visible
919 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000920 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000922 if (search_hl.rm.regprog != NULL
923 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000925 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000926 {
927 cur = wp->w_match_head;
928 while (cur != NULL)
929 {
930 if (cur->match.regprog != NULL
931 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000932 {
933 top_to_mod = TRUE;
934 break;
935 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000936 cur = cur->next;
937 }
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#endif
940 }
941#ifdef FEAT_FOLDING
942 if (mod_top != 0 && hasAnyFolding(wp))
943 {
944 linenr_T lnumt, lnumb;
945
946 /*
947 * A change in a line can cause lines above it to become folded or
948 * unfolded. Find the top most buffer line that may be affected.
949 * If the line was previously folded and displayed, get the first
950 * line of that fold. If the line is folded now, get the first
951 * folded line. Use the minimum of these two.
952 */
953
954 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
955 * the line below it. If there is no valid entry, use w_topline.
956 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
957 * to this line. If there is no valid entry, use MAXLNUM. */
958 lnumt = wp->w_topline;
959 lnumb = MAXLNUM;
960 for (i = 0; i < wp->w_lines_valid; ++i)
961 if (wp->w_lines[i].wl_valid)
962 {
963 if (wp->w_lines[i].wl_lastlnum < mod_top)
964 lnumt = wp->w_lines[i].wl_lastlnum + 1;
965 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
966 {
967 lnumb = wp->w_lines[i].wl_lnum;
968 /* When there is a fold column it might need updating
969 * in the next line ("J" just above an open fold). */
970 if (wp->w_p_fdc > 0)
971 ++lnumb;
972 }
973 }
974
975 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
976 if (mod_top > lnumt)
977 mod_top = lnumt;
978
979 /* Now do the same for the bottom line (one above mod_bot). */
980 --mod_bot;
981 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
982 ++mod_bot;
983 if (mod_bot < lnumb)
984 mod_bot = lnumb;
985 }
986#endif
987
988 /* When a change starts above w_topline and the end is below
989 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000990 * If the end of the change is above w_topline: do like no change was
991 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (mod_top != 0 && mod_top < wp->w_topline)
993 {
994 if (mod_bot > wp->w_topline)
995 mod_top = wp->w_topline;
996#ifdef FEAT_SYN_HL
997 else if (syntax_present(buf))
998 top_end = 1;
999#endif
1000 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001001
1002 /* When line numbers are displayed need to redraw all lines below
1003 * inserted/deleted lines. */
1004 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1005 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007
1008 /*
1009 * When only displaying the lines at the top, set top_end. Used when
1010 * window has scrolled down for msg_scrolled.
1011 */
1012 if (type == REDRAW_TOP)
1013 {
1014 j = 0;
1015 for (i = 0; i < wp->w_lines_valid; ++i)
1016 {
1017 j += wp->w_lines[i].wl_size;
1018 if (j >= wp->w_upd_rows)
1019 {
1020 top_end = j;
1021 break;
1022 }
1023 }
1024 if (top_end == 0)
1025 /* not found (cannot happen?): redraw everything */
1026 type = NOT_VALID;
1027 else
1028 /* top area defined, the rest is VALID */
1029 type = VALID;
1030 }
1031
Bram Moolenaar367329b2007-08-30 11:53:22 +00001032 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001033 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1034 * non-zero and thus not FALSE) will indicate that screenclear() was not
1035 * called. */
1036 if (screen_cleared)
1037 screen_cleared = MAYBE;
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 /*
1040 * If there are no changes on the screen that require a complete redraw,
1041 * handle three cases:
1042 * 1: we are off the top of the screen by a few lines: scroll down
1043 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1044 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1045 * w_lines[] that needs updating.
1046 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001047 if ((type == VALID || type == SOME_VALID
1048 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#ifdef FEAT_DIFF
1050 && !wp->w_botfill && !wp->w_old_botfill
1051#endif
1052 )
1053 {
1054 if (mod_top != 0 && wp->w_topline == mod_top)
1055 {
1056 /*
1057 * w_topline is the first changed line, the scrolling will be done
1058 * further down.
1059 */
1060 }
1061 else if (wp->w_lines[0].wl_valid
1062 && (wp->w_topline < wp->w_lines[0].wl_lnum
1063#ifdef FEAT_DIFF
1064 || (wp->w_topline == wp->w_lines[0].wl_lnum
1065 && wp->w_topfill > wp->w_old_topfill)
1066#endif
1067 ))
1068 {
1069 /*
1070 * New topline is above old topline: May scroll down.
1071 */
1072#ifdef FEAT_FOLDING
1073 if (hasAnyFolding(wp))
1074 {
1075 linenr_T ln;
1076
1077 /* count the number of lines we are off, counting a sequence
1078 * of folded lines as one */
1079 j = 0;
1080 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1081 {
1082 ++j;
1083 if (j >= wp->w_height - 2)
1084 break;
1085 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1086 }
1087 }
1088 else
1089#endif
1090 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1091 if (j < wp->w_height - 2) /* not too far off */
1092 {
1093 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1094#ifdef FEAT_DIFF
1095 /* insert extra lines for previously invisible filler lines */
1096 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1097 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1098 - wp->w_old_topfill;
1099#endif
1100 if (i < wp->w_height - 2) /* less than a screen off */
1101 {
1102 /*
1103 * Try to insert the correct number of lines.
1104 * If not the last window, delete the lines at the bottom.
1105 * win_ins_lines may fail when the terminal can't do it.
1106 */
1107 if (i > 0)
1108 check_for_delay(FALSE);
1109 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1110 {
1111 if (wp->w_lines_valid != 0)
1112 {
1113 /* Need to update rows that are new, stop at the
1114 * first one that scrolled down. */
1115 top_end = i;
1116#ifdef FEAT_VISUAL
1117 scrolled_down = TRUE;
1118#endif
1119
1120 /* Move the entries that were scrolled, disable
1121 * the entries for the lines to be redrawn. */
1122 if ((wp->w_lines_valid += j) > wp->w_height)
1123 wp->w_lines_valid = wp->w_height;
1124 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1125 wp->w_lines[idx] = wp->w_lines[idx - j];
1126 while (idx >= 0)
1127 wp->w_lines[idx--].wl_valid = FALSE;
1128 }
1129 }
1130 else
1131 mid_start = 0; /* redraw all lines */
1132 }
1133 else
1134 mid_start = 0; /* redraw all lines */
1135 }
1136 else
1137 mid_start = 0; /* redraw all lines */
1138 }
1139 else
1140 {
1141 /*
1142 * New topline is at or below old topline: May scroll up.
1143 * When topline didn't change, find first entry in w_lines[] that
1144 * needs updating.
1145 */
1146
1147 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1148 j = -1;
1149 row = 0;
1150 for (i = 0; i < wp->w_lines_valid; i++)
1151 {
1152 if (wp->w_lines[i].wl_valid
1153 && wp->w_lines[i].wl_lnum == wp->w_topline)
1154 {
1155 j = i;
1156 break;
1157 }
1158 row += wp->w_lines[i].wl_size;
1159 }
1160 if (j == -1)
1161 {
1162 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1163 * lines */
1164 mid_start = 0;
1165 }
1166 else
1167 {
1168 /*
1169 * Try to delete the correct number of lines.
1170 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1171 */
1172#ifdef FEAT_DIFF
1173 /* If the topline didn't change, delete old filler lines,
1174 * otherwise delete filler lines of the new topline... */
1175 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1176 row += wp->w_old_topfill;
1177 else
1178 row += diff_check_fill(wp, wp->w_topline);
1179 /* ... but don't delete new filler lines. */
1180 row -= wp->w_topfill;
1181#endif
1182 if (row > 0)
1183 {
1184 check_for_delay(FALSE);
1185 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1186 bot_start = wp->w_height - row;
1187 else
1188 mid_start = 0; /* redraw all lines */
1189 }
1190 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1191 {
1192 /*
1193 * Skip the lines (below the deleted lines) that are still
1194 * valid and don't need redrawing. Copy their info
1195 * upwards, to compensate for the deleted lines. Set
1196 * bot_start to the first row that needs redrawing.
1197 */
1198 bot_start = 0;
1199 idx = 0;
1200 for (;;)
1201 {
1202 wp->w_lines[idx] = wp->w_lines[j];
1203 /* stop at line that didn't fit, unless it is still
1204 * valid (no lines deleted) */
1205 if (row > 0 && bot_start + row
1206 + (int)wp->w_lines[j].wl_size > wp->w_height)
1207 {
1208 wp->w_lines_valid = idx + 1;
1209 break;
1210 }
1211 bot_start += wp->w_lines[idx++].wl_size;
1212
1213 /* stop at the last valid entry in w_lines[].wl_size */
1214 if (++j >= wp->w_lines_valid)
1215 {
1216 wp->w_lines_valid = idx;
1217 break;
1218 }
1219 }
1220#ifdef FEAT_DIFF
1221 /* Correct the first entry for filler lines at the top
1222 * when it won't get updated below. */
1223 if (wp->w_p_diff && bot_start > 0)
1224 wp->w_lines[0].wl_size =
1225 plines_win_nofill(wp, wp->w_topline, TRUE)
1226 + wp->w_topfill;
1227#endif
1228 }
1229 }
1230 }
1231
1232 /* When starting redraw in the first line, redraw all lines. When
1233 * there is only one window it's probably faster to clear the screen
1234 * first. */
1235 if (mid_start == 0)
1236 {
1237 mid_end = wp->w_height;
1238 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001239 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001240 /* Clear the screen when it was not done by win_del_lines() or
1241 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1242 * then. */
1243 if (screen_cleared != TRUE)
1244 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001245#ifdef FEAT_WINDOWS
1246 /* The screen was cleared, redraw the tab pages line. */
1247 if (redraw_tabline)
1248 draw_tabline();
1249#endif
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001252
1253 /* When win_del_lines() or win_ins_lines() caused the screen to be
1254 * cleared (only happens for the first window) or when screenclear()
1255 * was called directly above, "must_redraw" will have been set to
1256 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1257 if (screen_cleared == TRUE)
1258 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260 else
1261 {
1262 /* Not VALID or INVERTED: redraw all lines. */
1263 mid_start = 0;
1264 mid_end = wp->w_height;
1265 }
1266
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001267 if (type == SOME_VALID)
1268 {
1269 /* SOME_VALID: redraw all lines. */
1270 mid_start = 0;
1271 mid_end = wp->w_height;
1272 type = NOT_VALID;
1273 }
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#ifdef FEAT_VISUAL
1276 /* check if we are updating or removing the inverted part */
1277 if ((VIsual_active && buf == curwin->w_buffer)
1278 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1279 {
1280 linenr_T from, to;
1281
1282 if (VIsual_active)
1283 {
1284 if (VIsual_active
1285 && (VIsual_mode != wp->w_old_visual_mode
1286 || type == INVERTED_ALL))
1287 {
1288 /*
1289 * If the type of Visual selection changed, redraw the whole
1290 * selection. Also when the ownership of the X selection is
1291 * gained or lost.
1292 */
1293 if (curwin->w_cursor.lnum < VIsual.lnum)
1294 {
1295 from = curwin->w_cursor.lnum;
1296 to = VIsual.lnum;
1297 }
1298 else
1299 {
1300 from = VIsual.lnum;
1301 to = curwin->w_cursor.lnum;
1302 }
1303 /* redraw more when the cursor moved as well */
1304 if (wp->w_old_cursor_lnum < from)
1305 from = wp->w_old_cursor_lnum;
1306 if (wp->w_old_cursor_lnum > to)
1307 to = wp->w_old_cursor_lnum;
1308 if (wp->w_old_visual_lnum < from)
1309 from = wp->w_old_visual_lnum;
1310 if (wp->w_old_visual_lnum > to)
1311 to = wp->w_old_visual_lnum;
1312 }
1313 else
1314 {
1315 /*
1316 * Find the line numbers that need to be updated: The lines
1317 * between the old cursor position and the current cursor
1318 * position. Also check if the Visual position changed.
1319 */
1320 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1321 {
1322 from = curwin->w_cursor.lnum;
1323 to = wp->w_old_cursor_lnum;
1324 }
1325 else
1326 {
1327 from = wp->w_old_cursor_lnum;
1328 to = curwin->w_cursor.lnum;
1329 if (from == 0) /* Visual mode just started */
1330 from = to;
1331 }
1332
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001333 if (VIsual.lnum != wp->w_old_visual_lnum
1334 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 if (wp->w_old_visual_lnum < from
1337 && wp->w_old_visual_lnum != 0)
1338 from = wp->w_old_visual_lnum;
1339 if (wp->w_old_visual_lnum > to)
1340 to = wp->w_old_visual_lnum;
1341 if (VIsual.lnum < from)
1342 from = VIsual.lnum;
1343 if (VIsual.lnum > to)
1344 to = VIsual.lnum;
1345 }
1346 }
1347
1348 /*
1349 * If in block mode and changed column or curwin->w_curswant:
1350 * update all lines.
1351 * First compute the actual start and end column.
1352 */
1353 if (VIsual_mode == Ctrl_V)
1354 {
1355 colnr_T fromc, toc;
1356
1357 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1358 ++toc;
1359 if (curwin->w_curswant == MAXCOL)
1360 toc = MAXCOL;
1361
1362 if (fromc != wp->w_old_cursor_fcol
1363 || toc != wp->w_old_cursor_lcol)
1364 {
1365 if (from > VIsual.lnum)
1366 from = VIsual.lnum;
1367 if (to < VIsual.lnum)
1368 to = VIsual.lnum;
1369 }
1370 wp->w_old_cursor_fcol = fromc;
1371 wp->w_old_cursor_lcol = toc;
1372 }
1373 }
1374 else
1375 {
1376 /* Use the line numbers of the old Visual area. */
1377 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1378 {
1379 from = wp->w_old_cursor_lnum;
1380 to = wp->w_old_visual_lnum;
1381 }
1382 else
1383 {
1384 from = wp->w_old_visual_lnum;
1385 to = wp->w_old_cursor_lnum;
1386 }
1387 }
1388
1389 /*
1390 * There is no need to update lines above the top of the window.
1391 */
1392 if (from < wp->w_topline)
1393 from = wp->w_topline;
1394
1395 /*
1396 * If we know the value of w_botline, use it to restrict the update to
1397 * the lines that are visible in the window.
1398 */
1399 if (wp->w_valid & VALID_BOTLINE)
1400 {
1401 if (from >= wp->w_botline)
1402 from = wp->w_botline - 1;
1403 if (to >= wp->w_botline)
1404 to = wp->w_botline - 1;
1405 }
1406
1407 /*
1408 * Find the minimal part to be updated.
1409 * Watch out for scrolling that made entries in w_lines[] invalid.
1410 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1411 * top_end; need to redraw from top_end to the "to" line.
1412 * A middle mouse click with a Visual selection may change the text
1413 * above the Visual area and reset wl_valid, do count these for
1414 * mid_end (in srow).
1415 */
1416 if (mid_start > 0)
1417 {
1418 lnum = wp->w_topline;
1419 idx = 0;
1420 srow = 0;
1421 if (scrolled_down)
1422 mid_start = top_end;
1423 else
1424 mid_start = 0;
1425 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1426 {
1427 if (wp->w_lines[idx].wl_valid)
1428 mid_start += wp->w_lines[idx].wl_size;
1429 else if (!scrolled_down)
1430 srow += wp->w_lines[idx].wl_size;
1431 ++idx;
1432# ifdef FEAT_FOLDING
1433 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1434 lnum = wp->w_lines[idx].wl_lnum;
1435 else
1436# endif
1437 ++lnum;
1438 }
1439 srow += mid_start;
1440 mid_end = wp->w_height;
1441 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1442 {
1443 if (wp->w_lines[idx].wl_valid
1444 && wp->w_lines[idx].wl_lnum >= to + 1)
1445 {
1446 /* Only update until first row of this line */
1447 mid_end = srow;
1448 break;
1449 }
1450 srow += wp->w_lines[idx].wl_size;
1451 }
1452 }
1453 }
1454
1455 if (VIsual_active && buf == curwin->w_buffer)
1456 {
1457 wp->w_old_visual_mode = VIsual_mode;
1458 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1459 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001460 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 wp->w_old_curswant = curwin->w_curswant;
1462 }
1463 else
1464 {
1465 wp->w_old_visual_mode = 0;
1466 wp->w_old_cursor_lnum = 0;
1467 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001468 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470#endif /* FEAT_VISUAL */
1471
1472#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1473 /* reset got_int, otherwise regexp won't work */
1474 save_got_int = got_int;
1475 got_int = 0;
1476#endif
1477#ifdef FEAT_FOLDING
1478 win_foldinfo.fi_level = 0;
1479#endif
1480
1481 /*
1482 * Update all the window rows.
1483 */
1484 idx = 0; /* first entry in w_lines[].wl_size */
1485 row = 0;
1486 srow = 0;
1487 lnum = wp->w_topline; /* first line shown in window */
1488 for (;;)
1489 {
1490 /* stop updating when reached the end of the window (check for _past_
1491 * the end of the window is at the end of the loop) */
1492 if (row == wp->w_height)
1493 {
1494 didline = TRUE;
1495 break;
1496 }
1497
1498 /* stop updating when hit the end of the file */
1499 if (lnum > buf->b_ml.ml_line_count)
1500 {
1501 eof = TRUE;
1502 break;
1503 }
1504
1505 /* Remember the starting row of the line that is going to be dealt
1506 * with. It is used further down when the line doesn't fit. */
1507 srow = row;
1508
1509 /*
1510 * Update a line when it is in an area that needs updating, when it
1511 * has changes or w_lines[idx] is invalid.
1512 * bot_start may be halfway a wrapped line after using
1513 * win_del_lines(), check if the current line includes it.
1514 * When syntax folding is being used, the saved syntax states will
1515 * already have been updated, we can't see where the syntax state is
1516 * the same again, just update until the end of the window.
1517 */
1518 if (row < top_end
1519 || (row >= mid_start && row < mid_end)
1520#ifdef FEAT_SEARCH_EXTRA
1521 || top_to_mod
1522#endif
1523 || idx >= wp->w_lines_valid
1524 || (row + wp->w_lines[idx].wl_size > bot_start)
1525 || (mod_top != 0
1526 && (lnum == mod_top
1527 || (lnum >= mod_top
1528 && (lnum < mod_bot
1529#ifdef FEAT_SYN_HL
1530 || did_update == DID_FOLD
1531 || (did_update == DID_LINE
1532 && syntax_present(buf)
1533 && (
1534# ifdef FEAT_FOLDING
1535 (foldmethodIsSyntax(wp)
1536 && hasAnyFolding(wp)) ||
1537# endif
1538 syntax_check_changed(lnum)))
1539#endif
1540 )))))
1541 {
1542#ifdef FEAT_SEARCH_EXTRA
1543 if (lnum == mod_top)
1544 top_to_mod = FALSE;
1545#endif
1546
1547 /*
1548 * When at start of changed lines: May scroll following lines
1549 * up or down to minimize redrawing.
1550 * Don't do this when the change continues until the end.
1551 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1552 */
1553 if (lnum == mod_top
1554 && mod_bot != MAXLNUM
1555 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1556 {
1557 int old_rows = 0;
1558 int new_rows = 0;
1559 int xtra_rows;
1560 linenr_T l;
1561
1562 /* Count the old number of window rows, using w_lines[], which
1563 * should still contain the sizes for the lines as they are
1564 * currently displayed. */
1565 for (i = idx; i < wp->w_lines_valid; ++i)
1566 {
1567 /* Only valid lines have a meaningful wl_lnum. Invalid
1568 * lines are part of the changed area. */
1569 if (wp->w_lines[i].wl_valid
1570 && wp->w_lines[i].wl_lnum == mod_bot)
1571 break;
1572 old_rows += wp->w_lines[i].wl_size;
1573#ifdef FEAT_FOLDING
1574 if (wp->w_lines[i].wl_valid
1575 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1576 {
1577 /* Must have found the last valid entry above mod_bot.
1578 * Add following invalid entries. */
1579 ++i;
1580 while (i < wp->w_lines_valid
1581 && !wp->w_lines[i].wl_valid)
1582 old_rows += wp->w_lines[i++].wl_size;
1583 break;
1584 }
1585#endif
1586 }
1587
1588 if (i >= wp->w_lines_valid)
1589 {
1590 /* We can't find a valid line below the changed lines,
1591 * need to redraw until the end of the window.
1592 * Inserting/deleting lines has no use. */
1593 bot_start = 0;
1594 }
1595 else
1596 {
1597 /* Able to count old number of rows: Count new window
1598 * rows, and may insert/delete lines */
1599 j = idx;
1600 for (l = lnum; l < mod_bot; ++l)
1601 {
1602#ifdef FEAT_FOLDING
1603 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1604 ++new_rows;
1605 else
1606#endif
1607#ifdef FEAT_DIFF
1608 if (l == wp->w_topline)
1609 new_rows += plines_win_nofill(wp, l, TRUE)
1610 + wp->w_topfill;
1611 else
1612#endif
1613 new_rows += plines_win(wp, l, TRUE);
1614 ++j;
1615 if (new_rows > wp->w_height - row - 2)
1616 {
1617 /* it's getting too much, must redraw the rest */
1618 new_rows = 9999;
1619 break;
1620 }
1621 }
1622 xtra_rows = new_rows - old_rows;
1623 if (xtra_rows < 0)
1624 {
1625 /* May scroll text up. If there is not enough
1626 * remaining text or scrolling fails, must redraw the
1627 * rest. If scrolling works, must redraw the text
1628 * below the scrolled text. */
1629 if (row - xtra_rows >= wp->w_height - 2)
1630 mod_bot = MAXLNUM;
1631 else
1632 {
1633 check_for_delay(FALSE);
1634 if (win_del_lines(wp, row,
1635 -xtra_rows, FALSE, FALSE) == FAIL)
1636 mod_bot = MAXLNUM;
1637 else
1638 bot_start = wp->w_height + xtra_rows;
1639 }
1640 }
1641 else if (xtra_rows > 0)
1642 {
1643 /* May scroll text down. If there is not enough
1644 * remaining text of scrolling fails, must redraw the
1645 * rest. */
1646 if (row + xtra_rows >= wp->w_height - 2)
1647 mod_bot = MAXLNUM;
1648 else
1649 {
1650 check_for_delay(FALSE);
1651 if (win_ins_lines(wp, row + old_rows,
1652 xtra_rows, FALSE, FALSE) == FAIL)
1653 mod_bot = MAXLNUM;
1654 else if (top_end > row + old_rows)
1655 /* Scrolled the part at the top that requires
1656 * updating down. */
1657 top_end += xtra_rows;
1658 }
1659 }
1660
1661 /* When not updating the rest, may need to move w_lines[]
1662 * entries. */
1663 if (mod_bot != MAXLNUM && i != j)
1664 {
1665 if (j < i)
1666 {
1667 int x = row + new_rows;
1668
1669 /* move entries in w_lines[] upwards */
1670 for (;;)
1671 {
1672 /* stop at last valid entry in w_lines[] */
1673 if (i >= wp->w_lines_valid)
1674 {
1675 wp->w_lines_valid = j;
1676 break;
1677 }
1678 wp->w_lines[j] = wp->w_lines[i];
1679 /* stop at a line that won't fit */
1680 if (x + (int)wp->w_lines[j].wl_size
1681 > wp->w_height)
1682 {
1683 wp->w_lines_valid = j + 1;
1684 break;
1685 }
1686 x += wp->w_lines[j++].wl_size;
1687 ++i;
1688 }
1689 if (bot_start > x)
1690 bot_start = x;
1691 }
1692 else /* j > i */
1693 {
1694 /* move entries in w_lines[] downwards */
1695 j -= i;
1696 wp->w_lines_valid += j;
1697 if (wp->w_lines_valid > wp->w_height)
1698 wp->w_lines_valid = wp->w_height;
1699 for (i = wp->w_lines_valid; i - j >= idx; --i)
1700 wp->w_lines[i] = wp->w_lines[i - j];
1701
1702 /* The w_lines[] entries for inserted lines are
1703 * now invalid, but wl_size may be used above.
1704 * Reset to zero. */
1705 while (i >= idx)
1706 {
1707 wp->w_lines[i].wl_size = 0;
1708 wp->w_lines[i--].wl_valid = FALSE;
1709 }
1710 }
1711 }
1712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714
1715#ifdef FEAT_FOLDING
1716 /*
1717 * When lines are folded, display one line for all of them.
1718 * Otherwise, display normally (can be several display lines when
1719 * 'wrap' is on).
1720 */
1721 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1722 if (fold_count != 0)
1723 {
1724 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1725 ++row;
1726 --fold_count;
1727 wp->w_lines[idx].wl_folded = TRUE;
1728 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1729# ifdef FEAT_SYN_HL
1730 did_update = DID_FOLD;
1731# endif
1732 }
1733 else
1734#endif
1735 if (idx < wp->w_lines_valid
1736 && wp->w_lines[idx].wl_valid
1737 && wp->w_lines[idx].wl_lnum == lnum
1738 && lnum > wp->w_topline
1739 && !(dy_flags & DY_LASTLINE)
1740 && srow + wp->w_lines[idx].wl_size > wp->w_height
1741#ifdef FEAT_DIFF
1742 && diff_check_fill(wp, lnum) == 0
1743#endif
1744 )
1745 {
1746 /* This line is not going to fit. Don't draw anything here,
1747 * will draw "@ " lines below. */
1748 row = wp->w_height + 1;
1749 }
1750 else
1751 {
1752#ifdef FEAT_SEARCH_EXTRA
1753 prepare_search_hl(wp, lnum);
1754#endif
1755#ifdef FEAT_SYN_HL
1756 /* Let the syntax stuff know we skipped a few lines. */
1757 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1758 && syntax_present(buf))
1759 syntax_end_parsing(syntax_last_parsed + 1);
1760#endif
1761
1762 /*
1763 * Display one line.
1764 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001765 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
1767#ifdef FEAT_FOLDING
1768 wp->w_lines[idx].wl_folded = FALSE;
1769 wp->w_lines[idx].wl_lastlnum = lnum;
1770#endif
1771#ifdef FEAT_SYN_HL
1772 did_update = DID_LINE;
1773 syntax_last_parsed = lnum;
1774#endif
1775 }
1776
1777 wp->w_lines[idx].wl_lnum = lnum;
1778 wp->w_lines[idx].wl_valid = TRUE;
1779 if (row > wp->w_height) /* past end of screen */
1780 {
1781 /* we may need the size of that too long line later on */
1782 if (dollar_vcol == 0)
1783 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1784 ++idx;
1785 break;
1786 }
1787 if (dollar_vcol == 0)
1788 wp->w_lines[idx].wl_size = row - srow;
1789 ++idx;
1790#ifdef FEAT_FOLDING
1791 lnum += fold_count + 1;
1792#else
1793 ++lnum;
1794#endif
1795 }
1796 else
1797 {
1798 /* This line does not need updating, advance to the next one */
1799 row += wp->w_lines[idx++].wl_size;
1800 if (row > wp->w_height) /* past end of screen */
1801 break;
1802#ifdef FEAT_FOLDING
1803 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1804#else
1805 ++lnum;
1806#endif
1807#ifdef FEAT_SYN_HL
1808 did_update = DID_NONE;
1809#endif
1810 }
1811
1812 if (lnum > buf->b_ml.ml_line_count)
1813 {
1814 eof = TRUE;
1815 break;
1816 }
1817 }
1818 /*
1819 * End of loop over all window lines.
1820 */
1821
1822
1823 if (idx > wp->w_lines_valid)
1824 wp->w_lines_valid = idx;
1825
1826#ifdef FEAT_SYN_HL
1827 /*
1828 * Let the syntax stuff know we stop parsing here.
1829 */
1830 if (syntax_last_parsed != 0 && syntax_present(buf))
1831 syntax_end_parsing(syntax_last_parsed + 1);
1832#endif
1833
1834 /*
1835 * If we didn't hit the end of the file, and we didn't finish the last
1836 * line we were working on, then the line didn't fit.
1837 */
1838 wp->w_empty_rows = 0;
1839#ifdef FEAT_DIFF
1840 wp->w_filler_rows = 0;
1841#endif
1842 if (!eof && !didline)
1843 {
1844 if (lnum == wp->w_topline)
1845 {
1846 /*
1847 * Single line that does not fit!
1848 * Don't overwrite it, it can be edited.
1849 */
1850 wp->w_botline = lnum + 1;
1851 }
1852#ifdef FEAT_DIFF
1853 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1854 {
1855 /* Window ends in filler lines. */
1856 wp->w_botline = lnum;
1857 wp->w_filler_rows = wp->w_height - srow;
1858 }
1859#endif
1860 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1861 {
1862 /*
1863 * Last line isn't finished: Display "@@@" at the end.
1864 */
1865 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1866 W_WINROW(wp) + wp->w_height,
1867 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1868 '@', '@', hl_attr(HLF_AT));
1869 set_empty_rows(wp, srow);
1870 wp->w_botline = lnum;
1871 }
1872 else
1873 {
1874 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1875 wp->w_botline = lnum;
1876 }
1877 }
1878 else
1879 {
1880#ifdef FEAT_VERTSPLIT
1881 draw_vsep_win(wp, row);
1882#endif
1883 if (eof) /* we hit the end of the file */
1884 {
1885 wp->w_botline = buf->b_ml.ml_line_count + 1;
1886#ifdef FEAT_DIFF
1887 j = diff_check_fill(wp, wp->w_botline);
1888 if (j > 0 && !wp->w_botfill)
1889 {
1890 /*
1891 * Display filler lines at the end of the file
1892 */
1893 if (char2cells(fill_diff) > 1)
1894 i = '-';
1895 else
1896 i = fill_diff;
1897 if (row + j > wp->w_height)
1898 j = wp->w_height - row;
1899 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1900 row += j;
1901 }
1902#endif
1903 }
1904 else if (dollar_vcol == 0)
1905 wp->w_botline = lnum;
1906
1907 /* make sure the rest of the screen is blank */
1908 /* put '~'s on rows that aren't part of the file. */
1909 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1910 }
1911
1912 /* Reset the type of redrawing required, the window has been updated. */
1913 wp->w_redr_type = 0;
1914#ifdef FEAT_DIFF
1915 wp->w_old_topfill = wp->w_topfill;
1916 wp->w_old_botfill = wp->w_botfill;
1917#endif
1918
1919 if (dollar_vcol == 0)
1920 {
1921 /*
1922 * There is a trick with w_botline. If we invalidate it on each
1923 * change that might modify it, this will cause a lot of expensive
1924 * calls to plines() in update_topline() each time. Therefore the
1925 * value of w_botline is often approximated, and this value is used to
1926 * compute the value of w_topline. If the value of w_botline was
1927 * wrong, check that the value of w_topline is correct (cursor is on
1928 * the visible part of the text). If it's not, we need to redraw
1929 * again. Mostly this just means scrolling up a few lines, so it
1930 * doesn't look too bad. Only do this for the current window (where
1931 * changes are relevant).
1932 */
1933 wp->w_valid |= VALID_BOTLINE;
1934 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1935 {
1936 recursive = TRUE;
1937 curwin->w_valid &= ~VALID_TOPLINE;
1938 update_topline(); /* may invalidate w_botline again */
1939 if (must_redraw != 0)
1940 {
1941 /* Don't update for changes in buffer again. */
1942 i = curbuf->b_mod_set;
1943 curbuf->b_mod_set = FALSE;
1944 win_update(curwin);
1945 must_redraw = 0;
1946 curbuf->b_mod_set = i;
1947 }
1948 recursive = FALSE;
1949 }
1950 }
1951
1952#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1953 /* restore got_int, unless CTRL-C was hit while redrawing */
1954 if (!got_int)
1955 got_int = save_got_int;
1956#endif
1957}
1958
1959#ifdef FEAT_SIGNS
1960static int draw_signcolumn __ARGS((win_T *wp));
1961
1962/*
1963 * Return TRUE when window "wp" has a column to draw signs in.
1964 */
1965 static int
1966draw_signcolumn(wp)
1967 win_T *wp;
1968{
1969 return (wp->w_buffer->b_signlist != NULL
1970# ifdef FEAT_NETBEANS_INTG
1971 || usingNetbeans
1972# endif
1973 );
1974}
1975#endif
1976
1977/*
1978 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1979 * as the filler character.
1980 */
1981 static void
1982win_draw_end(wp, c1, c2, row, endrow, hl)
1983 win_T *wp;
1984 int c1;
1985 int c2;
1986 int row;
1987 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001988 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1991 int n = 0;
1992# define FDC_OFF n
1993#else
1994# define FDC_OFF 0
1995#endif
1996
1997#ifdef FEAT_RIGHTLEFT
1998 if (wp->w_p_rl)
1999 {
2000 /* No check for cmdline window: should never be right-left. */
2001# ifdef FEAT_FOLDING
2002 n = wp->w_p_fdc;
2003
2004 if (n > 0)
2005 {
2006 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002007 if (n > W_WIDTH(wp))
2008 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2010 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2011 ' ', ' ', hl_attr(HLF_FC));
2012 }
2013# endif
2014# ifdef FEAT_SIGNS
2015 if (draw_signcolumn(wp))
2016 {
2017 int nn = n + 2;
2018
2019 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002020 if (nn > W_WIDTH(wp))
2021 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2023 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2024 ' ', ' ', hl_attr(HLF_SC));
2025 n = nn;
2026 }
2027# endif
2028 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2029 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2030 c2, c2, hl_attr(hl));
2031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2033 c1, c2, hl_attr(hl));
2034 }
2035 else
2036#endif
2037 {
2038#ifdef FEAT_CMDWIN
2039 if (cmdwin_type != 0 && wp == curwin)
2040 {
2041 /* draw the cmdline character in the leftmost column */
2042 n = 1;
2043 if (n > wp->w_width)
2044 n = wp->w_width;
2045 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2046 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2047 cmdwin_type, ' ', hl_attr(HLF_AT));
2048 }
2049#endif
2050#ifdef FEAT_FOLDING
2051 if (wp->w_p_fdc > 0)
2052 {
2053 int nn = n + wp->w_p_fdc;
2054
2055 /* draw the fold column at the left */
2056 if (nn > W_WIDTH(wp))
2057 nn = W_WIDTH(wp);
2058 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2059 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2060 ' ', ' ', hl_attr(HLF_FC));
2061 n = nn;
2062 }
2063#endif
2064#ifdef FEAT_SIGNS
2065 if (draw_signcolumn(wp))
2066 {
2067 int nn = n + 2;
2068
2069 /* draw the sign column after the fold column */
2070 if (nn > W_WIDTH(wp))
2071 nn = W_WIDTH(wp);
2072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2073 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2074 ' ', ' ', hl_attr(HLF_SC));
2075 n = nn;
2076 }
2077#endif
2078 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2079 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2080 c1, c2, hl_attr(hl));
2081 }
2082 set_empty_rows(wp, row);
2083}
2084
2085#ifdef FEAT_FOLDING
2086/*
2087 * Display one folded line.
2088 */
2089 static void
2090fold_line(wp, fold_count, foldinfo, lnum, row)
2091 win_T *wp;
2092 long fold_count;
2093 foldinfo_T *foldinfo;
2094 linenr_T lnum;
2095 int row;
2096{
2097 char_u buf[51];
2098 pos_T *top, *bot;
2099 linenr_T lnume = lnum + fold_count - 1;
2100 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002101 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 int col;
2104 int txtcol;
2105 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002106 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108 /* Build the fold line:
2109 * 1. Add the cmdwin_type for the command-line window
2110 * 2. Add the 'foldcolumn'
2111 * 3. Add the 'number' column
2112 * 4. Compose the text
2113 * 5. Add the text
2114 * 6. set highlighting for the Visual area an other text
2115 */
2116 col = 0;
2117
2118 /*
2119 * 1. Add the cmdwin_type for the command-line window
2120 * Ignores 'rightleft', this window is never right-left.
2121 */
2122#ifdef FEAT_CMDWIN
2123 if (cmdwin_type != 0 && wp == curwin)
2124 {
2125 ScreenLines[off] = cmdwin_type;
2126 ScreenAttrs[off] = hl_attr(HLF_AT);
2127#ifdef FEAT_MBYTE
2128 if (enc_utf8)
2129 ScreenLinesUC[off] = 0;
2130#endif
2131 ++col;
2132 }
2133#endif
2134
2135 /*
2136 * 2. Add the 'foldcolumn'
2137 */
2138 fdc = wp->w_p_fdc;
2139 if (fdc > W_WIDTH(wp) - col)
2140 fdc = W_WIDTH(wp) - col;
2141 if (fdc > 0)
2142 {
2143 fill_foldcolumn(buf, wp, TRUE, lnum);
2144#ifdef FEAT_RIGHTLEFT
2145 if (wp->w_p_rl)
2146 {
2147 int i;
2148
2149 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2150 hl_attr(HLF_FC));
2151 /* reverse the fold column */
2152 for (i = 0; i < fdc; ++i)
2153 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2154 }
2155 else
2156#endif
2157 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2158 col += fdc;
2159 }
2160
2161#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002162# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2163 for (ri = 0; ri < l; ++ri) \
2164 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2165 else \
2166 for (ri = 0; ri < l; ++ri) \
2167 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002169# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2170 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172
2173 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002174 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176#ifdef FEAT_SIGNS
2177 /* If signs are being displayed, add two spaces. */
2178 if (draw_signcolumn(wp))
2179 {
2180 len = W_WIDTH(wp) - col;
2181 if (len > 0)
2182 {
2183 if (len > 2)
2184 len = 2;
2185# ifdef FEAT_RIGHTLEFT
2186 if (wp->w_p_rl)
2187 /* the line number isn't reversed */
2188 copy_text_attr(off + W_WIDTH(wp) - len - col,
2189 (char_u *)" ", len, hl_attr(HLF_FL));
2190 else
2191# endif
2192 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2193 col += len;
2194 }
2195 }
2196#endif
2197
2198 /*
2199 * 3. Add the 'number' column
2200 */
2201 if (wp->w_p_nu)
2202 {
2203 len = W_WIDTH(wp) - col;
2204 if (len > 0)
2205 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002206 int w = number_width(wp);
2207
2208 if (len > w + 1)
2209 len = w + 1;
2210 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_RIGHTLEFT
2212 if (wp->w_p_rl)
2213 /* the line number isn't reversed */
2214 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2215 hl_attr(HLF_FL));
2216 else
2217#endif
2218 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2219 col += len;
2220 }
2221 }
2222
2223 /*
2224 * 4. Compose the folded-line string with 'foldtext', if set.
2225 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002226 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228 txtcol = col; /* remember where text starts */
2229
2230 /*
2231 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2232 * Right-left text is put in columns 0 - number-col, normal text is put
2233 * in columns number-col - window-width.
2234 */
2235#ifdef FEAT_MBYTE
2236 if (has_mbyte)
2237 {
2238 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002239 int u8c, u8cc[MAX_MCO];
2240 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 int idx;
2242 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002243 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244# ifdef FEAT_ARABIC
2245 int prev_c = 0; /* previous Arabic character */
2246 int prev_c1 = 0; /* first composing char for prev_c */
2247# endif
2248
2249# ifdef FEAT_RIGHTLEFT
2250 if (wp->w_p_rl)
2251 idx = off;
2252 else
2253# endif
2254 idx = off + col;
2255
2256 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2257 for (p = text; *p != NUL; )
2258 {
2259 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002260 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (col + cells > W_WIDTH(wp)
2262# ifdef FEAT_RIGHTLEFT
2263 - (wp->w_p_rl ? col : 0)
2264# endif
2265 )
2266 break;
2267 ScreenLines[idx] = *p;
2268 if (enc_utf8)
2269 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002270 u8c = utfc_ptr2char(p, u8cc);
2271 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 ScreenLinesUC[idx] = 0;
2274#ifdef FEAT_ARABIC
2275 prev_c = u8c;
2276#endif
2277 }
2278 else
2279 {
2280#ifdef FEAT_ARABIC
2281 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2282 {
2283 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002284 int pc, pc1, nc;
2285 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 int firstbyte = *p;
2287
2288 /* The idea of what is the previous and next
2289 * character depends on 'rightleft'. */
2290 if (wp->w_p_rl)
2291 {
2292 pc = prev_c;
2293 pc1 = prev_c1;
2294 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002295 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 else
2298 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002299 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002301 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 prev_c = u8c;
2304
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002305 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 pc, pc1, nc);
2307 ScreenLines[idx] = firstbyte;
2308 }
2309 else
2310 prev_c = u8c;
2311#endif
2312 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002313#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (u8c >= 0x10000)
2315 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2316 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002319 for (i = 0; i < Screen_mco; ++i)
2320 {
2321 ScreenLinesC[i][idx] = u8cc[i];
2322 if (u8cc[i] == 0)
2323 break;
2324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326 if (cells > 1)
2327 ScreenLines[idx + 1] = 0;
2328 }
2329 else if (cells > 1) /* double-byte character */
2330 {
2331 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2332 ScreenLines2[idx] = p[1];
2333 else
2334 ScreenLines[idx + 1] = p[1];
2335 }
2336 col += cells;
2337 idx += cells;
2338 p += c_len;
2339 }
2340 }
2341 else
2342#endif
2343 {
2344 len = (int)STRLEN(text);
2345 if (len > W_WIDTH(wp) - col)
2346 len = W_WIDTH(wp) - col;
2347 if (len > 0)
2348 {
2349#ifdef FEAT_RIGHTLEFT
2350 if (wp->w_p_rl)
2351 STRNCPY(current_ScreenLine, text, len);
2352 else
2353#endif
2354 STRNCPY(current_ScreenLine + col, text, len);
2355 col += len;
2356 }
2357 }
2358
2359 /* Fill the rest of the line with the fold filler */
2360#ifdef FEAT_RIGHTLEFT
2361 if (wp->w_p_rl)
2362 col -= txtcol;
2363#endif
2364 while (col < W_WIDTH(wp)
2365#ifdef FEAT_RIGHTLEFT
2366 - (wp->w_p_rl ? txtcol : 0)
2367#endif
2368 )
2369 {
2370#ifdef FEAT_MBYTE
2371 if (enc_utf8)
2372 {
2373 if (fill_fold >= 0x80)
2374 {
2375 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002376 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378 else
2379 ScreenLinesUC[off + col] = 0;
2380 }
2381#endif
2382 ScreenLines[off + col++] = fill_fold;
2383 }
2384
2385 if (text != buf)
2386 vim_free(text);
2387
2388 /*
2389 * 6. set highlighting for the Visual area an other text.
2390 * If all folded lines are in the Visual area, highlight the line.
2391 */
2392#ifdef FEAT_VISUAL
2393 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2394 {
2395 if (ltoreq(curwin->w_cursor, VIsual))
2396 {
2397 /* Visual is after curwin->w_cursor */
2398 top = &curwin->w_cursor;
2399 bot = &VIsual;
2400 }
2401 else
2402 {
2403 /* Visual is before curwin->w_cursor */
2404 top = &VIsual;
2405 bot = &curwin->w_cursor;
2406 }
2407 if (lnum >= top->lnum
2408 && lnume <= bot->lnum
2409 && (VIsual_mode != 'v'
2410 || ((lnum > top->lnum
2411 || (lnum == top->lnum
2412 && top->col == 0))
2413 && (lnume < bot->lnum
2414 || (lnume == bot->lnum
2415 && (bot->col - (*p_sel == 'e'))
2416 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2417 {
2418 if (VIsual_mode == Ctrl_V)
2419 {
2420 /* Visual block mode: highlight the chars part of the block */
2421 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2422 {
2423 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2424 len = wp->w_old_cursor_lcol;
2425 else
2426 len = W_WIDTH(wp) - txtcol;
2427 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002428 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430 }
2431 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002434 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 }
2438#endif
2439
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002440#ifdef FEAT_SYN_HL
2441 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002442 if (wp->w_p_cuc)
2443 {
2444 txtcol += wp->w_virtcol;
2445 if (wp->w_p_wrap)
2446 txtcol -= wp->w_skipcol;
2447 else
2448 txtcol -= wp->w_leftcol;
2449 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2450 ScreenAttrs[off + txtcol] = hl_combine_attr(
2451 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2452 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2456 (int)W_WIDTH(wp), FALSE);
2457
2458 /*
2459 * Update w_cline_height and w_cline_folded if the cursor line was
2460 * updated (saves a call to plines() later).
2461 */
2462 if (wp == curwin
2463 && lnum <= curwin->w_cursor.lnum
2464 && lnume >= curwin->w_cursor.lnum)
2465 {
2466 curwin->w_cline_row = row;
2467 curwin->w_cline_height = 1;
2468 curwin->w_cline_folded = TRUE;
2469 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2470 }
2471}
2472
2473/*
2474 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2475 */
2476 static void
2477copy_text_attr(off, buf, len, attr)
2478 int off;
2479 char_u *buf;
2480 int len;
2481 int attr;
2482{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002483 int i;
2484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 mch_memmove(ScreenLines + off, buf, (size_t)len);
2486# ifdef FEAT_MBYTE
2487 if (enc_utf8)
2488 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2489# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002490 for (i = 0; i < len; ++i)
2491 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492}
2493
2494/*
2495 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002496 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
2498 static void
2499fill_foldcolumn(p, wp, closed, lnum)
2500 char_u *p;
2501 win_T *wp;
2502 int closed; /* TRUE of FALSE */
2503 linenr_T lnum; /* current line number */
2504{
2505 int i = 0;
2506 int level;
2507 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002508 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 /* Init to all spaces. */
2511 copy_spaces(p, (size_t)wp->w_p_fdc);
2512
2513 level = win_foldinfo.fi_level;
2514 if (level > 0)
2515 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002516 /* If there is only one column put more info in it. */
2517 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2518
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 /* If the column is too narrow, we start at the lowest level that
2520 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002521 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (first_level < 1)
2523 first_level = 1;
2524
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002525 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 if (win_foldinfo.fi_lnum == lnum
2528 && first_level + i >= win_foldinfo.fi_low_level)
2529 p[i] = '-';
2530 else if (first_level == 1)
2531 p[i] = '|';
2532 else if (first_level + i <= 9)
2533 p[i] = '0' + first_level + i;
2534 else
2535 p[i] = '>';
2536 if (first_level + i == level)
2537 break;
2538 }
2539 }
2540 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002541 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542}
2543#endif /* FEAT_FOLDING */
2544
2545/*
2546 * Display line "lnum" of window 'wp' on the screen.
2547 * Start at row "startrow", stop when "endrow" is reached.
2548 * wp->w_virtcol needs to be valid.
2549 *
2550 * Return the number of last row the line occupies.
2551 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002552/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002554win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 win_T *wp;
2556 linenr_T lnum;
2557 int startrow;
2558 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002559 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 int col; /* visual column on screen */
2562 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2563 int c = 0; /* init for GCC */
2564 long vcol = 0; /* virtual column (for tabs) */
2565 long vcol_prev = -1; /* "vcol" of previous character */
2566 char_u *line; /* current line */
2567 char_u *ptr; /* current position in "line" */
2568 int row; /* row in the window, excl w_winrow */
2569 int screen_row; /* row on the screen, incl w_winrow */
2570
2571 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2572 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002573 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 int c_extra = NUL; /* extra chars, all the same */
2575 int extra_attr = 0; /* attributes when n_extra != 0 */
2576 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2577 displaying lcs_eol at end-of-line */
2578 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2579 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2580
2581 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2582 int saved_n_extra = 0;
2583 char_u *saved_p_extra = NULL;
2584 int saved_c_extra = 0;
2585 int saved_char_attr = 0;
2586
2587 int n_attr = 0; /* chars with special attr */
2588 int saved_attr2 = 0; /* char_attr saved for n_attr */
2589 int n_attr3 = 0; /* chars with overruling special attr */
2590 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2591
2592 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2593
2594 int fromcol, tocol; /* start/end of inverting */
2595 int fromcol_prev = -2; /* start of inverting after cursor */
2596 int noinvcur = FALSE; /* don't invert the cursor */
2597#ifdef FEAT_VISUAL
2598 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002599 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600#endif
2601 pos_T pos;
2602 long v;
2603
2604 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002605 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2607 in this line */
2608 int attr = 0; /* attributes for area highlighting */
2609 int area_attr = 0; /* attributes desired by highlighting */
2610 int search_attr = 0; /* attributes desired by 'hlsearch' */
2611#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002612 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 int syntax_attr = 0; /* attributes desired by syntax */
2614 int has_syntax = FALSE; /* this buffer has syntax highl. */
2615 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002616 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002617#endif
2618#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002619 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002620# define SPWORDLEN 150
2621 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002622 int nextlinecol = 0; /* column where nextline[] starts */
2623 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002624 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002625 int spell_attr = 0; /* attributes desired by spelling */
2626 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002627 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2628 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002629 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002630 static int cap_col = -1; /* column to check for Cap word */
2631 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002632 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#endif
2634 int extra_check; /* has syntax or linebreak */
2635#ifdef FEAT_MBYTE
2636 int multi_attr = 0; /* attributes desired by multibyte */
2637 int mb_l = 1; /* multi-byte byte length */
2638 int mb_c = 0; /* decoded multi-byte character */
2639 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002640 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641#endif
2642#ifdef FEAT_DIFF
2643 int filler_lines; /* nr of filler lines to be drawn */
2644 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002645 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 int change_start = MAXCOL; /* first col of changed area */
2647 int change_end = -1; /* last col of changed area */
2648#endif
2649 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2650#ifdef FEAT_LINEBREAK
2651 int need_showbreak = FALSE;
2652#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002653#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2654 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002656 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002659 matchitem_T *cur; /* points to the match list */
2660 match_T *shl; /* points to search_hl or a match */
2661 int shl_flag; /* flag to indicate whether search_hl
2662 has been processed or not */
2663 int prevcol_hl_flag; /* flag to indicate whether prevcol
2664 equals startcol of search_hl or one
2665 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666#endif
2667#ifdef FEAT_ARABIC
2668 int prev_c = 0; /* previous Arabic character */
2669 int prev_c1 = 0; /* first composing char for prev_c */
2670#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002671#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002672 int did_line_attr = 0;
2673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 /* draw_state: items that are drawn in sequence: */
2676#define WL_START 0 /* nothing done yet */
2677#ifdef FEAT_CMDWIN
2678# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2679#else
2680# define WL_CMDLINE WL_START
2681#endif
2682#ifdef FEAT_FOLDING
2683# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2684#else
2685# define WL_FOLD WL_CMDLINE
2686#endif
2687#ifdef FEAT_SIGNS
2688# define WL_SIGN WL_FOLD + 1 /* column for signs */
2689#else
2690# define WL_SIGN WL_FOLD /* column for signs */
2691#endif
2692#define WL_NR WL_SIGN + 1 /* line number */
2693#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2694# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2695#else
2696# define WL_SBR WL_NR
2697#endif
2698#define WL_LINE WL_SBR + 1 /* text in the line */
2699 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002700#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 int feedback_col = 0;
2702 int feedback_old_attr = -1;
2703#endif
2704
2705
2706 if (startrow > endrow) /* past the end already! */
2707 return startrow;
2708
2709 row = startrow;
2710 screen_row = row + W_WINROW(wp);
2711
2712 /*
2713 * To speed up the loop below, set extra_check when there is linebreak,
2714 * trailing white space and/or syntax processing to be done.
2715 */
2716#ifdef FEAT_LINEBREAK
2717 extra_check = wp->w_p_lbr;
2718#else
2719 extra_check = 0;
2720#endif
2721#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002722 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
2724 /* Prepare for syntax highlighting in this line. When there is an
2725 * error, stop syntax highlighting. */
2726 save_did_emsg = did_emsg;
2727 did_emsg = FALSE;
2728 syntax_start(wp, lnum);
2729 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002730 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 else
2732 {
2733 did_emsg = save_did_emsg;
2734 has_syntax = TRUE;
2735 extra_check = TRUE;
2736 }
2737 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002738#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002739
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002740#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002741 if (wp->w_p_spell
2742 && *wp->w_buffer->b_p_spl != NUL
2743 && wp->w_buffer->b_langp.ga_len > 0
2744 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002745 {
2746 /* Prepare for spell checking. */
2747 has_spell = TRUE;
2748 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002749
2750 /* Get the start of the next line, so that words that wrap to the next
2751 * line are found too: "et<line-break>al.".
2752 * Trick: skip a few chars for C/shell/Vim comments */
2753 nextline[SPWORDLEN] = NUL;
2754 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2755 {
2756 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2757 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2758 }
2759
2760 /* When a word wrapped from the previous line the start of the current
2761 * line is valid. */
2762 if (lnum == checked_lnum)
2763 cur_checked_col = checked_col;
2764 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002765
2766 /* When there was a sentence end in the previous line may require a
2767 * word starting with capital in this line. In line 1 always check
2768 * the first word. */
2769 if (lnum != capcol_lnum)
2770 cap_col = -1;
2771 if (lnum == 1)
2772 cap_col = 0;
2773 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775#endif
2776
2777 /*
2778 * handle visual active in this window
2779 */
2780 fromcol = -10;
2781 tocol = MAXCOL;
2782#ifdef FEAT_VISUAL
2783 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2784 {
2785 /* Visual is after curwin->w_cursor */
2786 if (ltoreq(curwin->w_cursor, VIsual))
2787 {
2788 top = &curwin->w_cursor;
2789 bot = &VIsual;
2790 }
2791 else /* Visual is before curwin->w_cursor */
2792 {
2793 top = &VIsual;
2794 bot = &curwin->w_cursor;
2795 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002796 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (VIsual_mode == Ctrl_V) /* block mode */
2798 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002799 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
2801 fromcol = wp->w_old_cursor_fcol;
2802 tocol = wp->w_old_cursor_lcol;
2803 }
2804 }
2805 else /* non-block mode */
2806 {
2807 if (lnum > top->lnum && lnum <= bot->lnum)
2808 fromcol = 0;
2809 else if (lnum == top->lnum)
2810 {
2811 if (VIsual_mode == 'V') /* linewise */
2812 fromcol = 0;
2813 else
2814 {
2815 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2816 if (gchar_pos(top) == NUL)
2817 tocol = fromcol + 1;
2818 }
2819 }
2820 if (VIsual_mode != 'V' && lnum == bot->lnum)
2821 {
2822 if (*p_sel == 'e' && bot->col == 0
2823#ifdef FEAT_VIRTUALEDIT
2824 && bot->coladd == 0
2825#endif
2826 )
2827 {
2828 fromcol = -10;
2829 tocol = MAXCOL;
2830 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002831 else if (bot->col == MAXCOL)
2832 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else
2834 {
2835 pos = *bot;
2836 if (*p_sel == 'e')
2837 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2838 else
2839 {
2840 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2841 ++tocol;
2842 }
2843 }
2844 }
2845 }
2846
2847#ifndef MSDOS
2848 /* Check if the character under the cursor should not be inverted */
2849 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2850# ifdef FEAT_GUI
2851 && !gui.in_use
2852# endif
2853 )
2854 noinvcur = TRUE;
2855#endif
2856
2857 /* if inverting in this line set area_highlighting */
2858 if (fromcol >= 0)
2859 {
2860 area_highlighting = TRUE;
2861 attr = hl_attr(HLF_V);
2862#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2863 if (clip_star.available && !clip_star.owned && clip_isautosel())
2864 attr = hl_attr(HLF_VNC);
2865#endif
2866 }
2867 }
2868
2869 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002870 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 */
2872 else
2873#endif /* FEAT_VISUAL */
2874 if (highlight_match
2875 && wp == curwin
2876 && lnum >= curwin->w_cursor.lnum
2877 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2878 {
2879 if (lnum == curwin->w_cursor.lnum)
2880 getvcol(curwin, &(curwin->w_cursor),
2881 (colnr_T *)&fromcol, NULL, NULL);
2882 else
2883 fromcol = 0;
2884 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2885 {
2886 pos.lnum = lnum;
2887 pos.col = search_match_endcol;
2888 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2889 }
2890 else
2891 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00002892 /* do at least one character; happens when past end of line */
2893 if (fromcol == tocol)
2894 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 area_highlighting = TRUE;
2896 attr = hl_attr(HLF_I);
2897 }
2898
2899#ifdef FEAT_DIFF
2900 filler_lines = diff_check(wp, lnum);
2901 if (filler_lines < 0)
2902 {
2903 if (filler_lines == -1)
2904 {
2905 if (diff_find_change(wp, lnum, &change_start, &change_end))
2906 diff_hlf = HLF_ADD; /* added line */
2907 else if (change_start == 0)
2908 diff_hlf = HLF_TXD; /* changed text */
2909 else
2910 diff_hlf = HLF_CHD; /* changed line */
2911 }
2912 else
2913 diff_hlf = HLF_ADD; /* added line */
2914 filler_lines = 0;
2915 area_highlighting = TRUE;
2916 }
2917 if (lnum == wp->w_topline)
2918 filler_lines = wp->w_topfill;
2919 filler_todo = filler_lines;
2920#endif
2921
2922#ifdef LINE_ATTR
2923# ifdef FEAT_SIGNS
2924 /* If this line has a sign with line highlighting set line_attr. */
2925 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2926 if (v != 0)
2927 line_attr = sign_get_attr((int)v, TRUE);
2928# endif
2929# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2930 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 line_attr = hl_attr(HLF_L);
2933# endif
2934 if (line_attr != 0)
2935 area_highlighting = TRUE;
2936#endif
2937
2938 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2939 ptr = line;
2940
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002941#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002942 if (has_spell)
2943 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002944 /* For checking first word with a capital skip white space. */
2945 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002946 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002947
Bram Moolenaar30abd282005-06-22 22:35:10 +00002948 /* To be able to spell-check over line boundaries copy the end of the
2949 * current line into nextline[]. Above the start of the next line was
2950 * copied to nextline[SPWORDLEN]. */
2951 if (nextline[SPWORDLEN] == NUL)
2952 {
2953 /* No next line or it is empty. */
2954 nextlinecol = MAXCOL;
2955 nextline_idx = 0;
2956 }
2957 else
2958 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002959 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002960 if (v < SPWORDLEN)
2961 {
2962 /* Short line, use it completely and append the start of the
2963 * next line. */
2964 nextlinecol = 0;
2965 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00002966 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002967 nextline_idx = v + 1;
2968 }
2969 else
2970 {
2971 /* Long line, use only the last SPWORDLEN bytes. */
2972 nextlinecol = v - SPWORDLEN;
2973 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2974 nextline_idx = SPWORDLEN + 1;
2975 }
2976 }
2977 }
2978#endif
2979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 /* find start of trailing whitespace */
2981 if (wp->w_p_list && lcs_trail)
2982 {
2983 trailcol = (colnr_T)STRLEN(ptr);
2984 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2985 --trailcol;
2986 trailcol += (colnr_T) (ptr - line);
2987 extra_check = TRUE;
2988 }
2989
2990 /*
2991 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2992 * first character to be displayed.
2993 */
2994 if (wp->w_p_wrap)
2995 v = wp->w_skipcol;
2996 else
2997 v = wp->w_leftcol;
2998 if (v > 0)
2999 {
3000#ifdef FEAT_MBYTE
3001 char_u *prev_ptr = ptr;
3002#endif
3003 while (vcol < v && *ptr != NUL)
3004 {
3005 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3006 vcol += c;
3007#ifdef FEAT_MBYTE
3008 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003010 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012
3013#ifdef FEAT_VIRTUALEDIT
3014 /* When 'virtualedit' is set the end of the line may be before the
3015 * start of the displayed part. */
3016 if (vcol < v && *ptr == NUL && virtual_active())
3017 vcol = v;
3018#endif
3019
3020 /* Handle a character that's not completely on the screen: Put ptr at
3021 * that character but skip the first few screen characters. */
3022 if (vcol > v)
3023 {
3024 vcol -= c;
3025#ifdef FEAT_MBYTE
3026 ptr = prev_ptr;
3027#else
3028 --ptr;
3029#endif
3030 n_skip = v - vcol;
3031 }
3032
3033 /*
3034 * Adjust for when the inverted text is before the screen,
3035 * and when the start of the inverted text is before the screen.
3036 */
3037 if (tocol <= vcol)
3038 fromcol = 0;
3039 else if (fromcol >= 0 && fromcol < vcol)
3040 fromcol = vcol;
3041
3042#ifdef FEAT_LINEBREAK
3043 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3044 if (wp->w_p_wrap)
3045 need_showbreak = TRUE;
3046#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003047#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003048 /* When spell checking a word we need to figure out the start of the
3049 * word and if it's badly spelled or not. */
3050 if (has_spell)
3051 {
3052 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003053 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003054 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003055
3056 pos = wp->w_cursor;
3057 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003058 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003059 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003060
3061 /* spell_move_to() may call ml_get() and make "line" invalid */
3062 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3063 ptr = line + linecol;
3064
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003065 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003066 {
3067 /* no bad word found at line start, don't check until end of a
3068 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003069 spell_hlf = HLF_COUNT;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003070 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3071 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003072 }
3073 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003074 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003075 /* bad word found, use attributes until end of word */
3076 word_end = wp->w_cursor.col + len + 1;
3077
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003078 /* Turn index into actual attributes. */
3079 if (spell_hlf != HLF_COUNT)
3080 spell_attr = highlight_attr[spell_hlf];
3081 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003082 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003083
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003084# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003085 /* Need to restart syntax highlighting for this line. */
3086 if (has_syntax)
3087 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003088# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003089 }
3090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092
3093 /*
3094 * Correct highlighting for cursor that can't be disabled.
3095 * Avoids having to check this for each character.
3096 */
3097 if (fromcol >= 0)
3098 {
3099 if (noinvcur)
3100 {
3101 if ((colnr_T)fromcol == wp->w_virtcol)
3102 {
3103 /* highlighting starts at cursor, let it start just after the
3104 * cursor */
3105 fromcol_prev = fromcol;
3106 fromcol = -1;
3107 }
3108 else if ((colnr_T)fromcol < wp->w_virtcol)
3109 /* restart highlighting after the cursor */
3110 fromcol_prev = wp->w_virtcol;
3111 }
3112 if (fromcol >= tocol)
3113 fromcol = -1;
3114 }
3115
3116#ifdef FEAT_SEARCH_EXTRA
3117 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003118 * Handle highlighting the last used search pattern and matches.
3119 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003121 cur = wp->w_match_head;
3122 shl_flag = FALSE;
3123 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003125 if (shl_flag == FALSE)
3126 {
3127 shl = &search_hl;
3128 shl_flag = TRUE;
3129 }
3130 else
3131 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003132 shl->startcol = MAXCOL;
3133 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 shl->attr_cur = 0;
3135 if (shl->rm.regprog != NULL)
3136 {
3137 v = (long)(ptr - line);
3138 next_search_hl(wp, shl, lnum, (colnr_T)v);
3139
3140 /* Need to get the line again, a multi-line regexp may have made it
3141 * invalid. */
3142 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3143 ptr = line + v;
3144
3145 if (shl->lnum != 0 && shl->lnum <= lnum)
3146 {
3147 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003148 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003150 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3152 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003153 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003155 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003157 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
3159#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003160 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003161 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 else
3163#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003164 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003166 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 shl->attr_cur = shl->attr;
3169 search_attr = shl->attr;
3170 }
3171 area_highlighting = TRUE;
3172 }
3173 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003174 if (shl != &search_hl && cur != NULL)
3175 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
3177#endif
3178
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003179#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003180 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3181 * active, because it's not clear what is selected then. */
3182 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003183 {
3184 line_attr = hl_attr(HLF_CUL);
3185 area_highlighting = TRUE;
3186 }
3187#endif
3188
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003189 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 col = 0;
3191#ifdef FEAT_RIGHTLEFT
3192 if (wp->w_p_rl)
3193 {
3194 /* Rightleft window: process the text in the normal direction, but put
3195 * it in current_ScreenLine[] from right to left. Start at the
3196 * rightmost column of the window. */
3197 col = W_WIDTH(wp) - 1;
3198 off += col;
3199 }
3200#endif
3201
3202 /*
3203 * Repeat for the whole displayed line.
3204 */
3205 for (;;)
3206 {
3207 /* Skip this quickly when working on the text. */
3208 if (draw_state != WL_LINE)
3209 {
3210#ifdef FEAT_CMDWIN
3211 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3212 {
3213 draw_state = WL_CMDLINE;
3214 if (cmdwin_type != 0 && wp == curwin)
3215 {
3216 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003218 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 char_attr = hl_attr(HLF_AT);
3220 }
3221 }
3222#endif
3223
3224#ifdef FEAT_FOLDING
3225 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3226 {
3227 draw_state = WL_FOLD;
3228 if (wp->w_p_fdc > 0)
3229 {
3230 /* Draw the 'foldcolumn'. */
3231 fill_foldcolumn(extra, wp, FALSE, lnum);
3232 n_extra = wp->w_p_fdc;
3233 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003234 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 c_extra = NUL;
3236 char_attr = hl_attr(HLF_FC);
3237 }
3238 }
3239#endif
3240
3241#ifdef FEAT_SIGNS
3242 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3243 {
3244 draw_state = WL_SIGN;
3245 /* Show the sign column when there are any signs in this
3246 * buffer or when using Netbeans. */
3247 if (draw_signcolumn(wp)
3248# ifdef FEAT_DIFF
3249 && filler_todo <= 0
3250# endif
3251 )
3252 {
3253 int_u text_sign;
3254# ifdef FEAT_SIGN_ICONS
3255 int_u icon_sign;
3256# endif
3257
3258 /* Draw two cells with the sign value or blank. */
3259 c_extra = ' ';
3260 char_attr = hl_attr(HLF_SC);
3261 n_extra = 2;
3262
3263 if (row == startrow)
3264 {
3265 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3266 SIGN_TEXT);
3267# ifdef FEAT_SIGN_ICONS
3268 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3269 SIGN_ICON);
3270 if (gui.in_use && icon_sign != 0)
3271 {
3272 /* Use the image in this position. */
3273 c_extra = SIGN_BYTE;
3274# ifdef FEAT_NETBEANS_INTG
3275 if (buf_signcount(wp->w_buffer, lnum) > 1)
3276 c_extra = MULTISIGN_BYTE;
3277# endif
3278 char_attr = icon_sign;
3279 }
3280 else
3281# endif
3282 if (text_sign != 0)
3283 {
3284 p_extra = sign_get_text(text_sign);
3285 if (p_extra != NULL)
3286 {
3287 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003288 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290 char_attr = sign_get_attr(text_sign, FALSE);
3291 }
3292 }
3293 }
3294 }
3295#endif
3296
3297 if (draw_state == WL_NR - 1 && n_extra == 0)
3298 {
3299 draw_state = WL_NR;
3300 /* Display the line number. After the first fill with blanks
3301 * when the 'n' flag isn't in 'cpo' */
3302 if (wp->w_p_nu
3303 && (row == startrow
3304#ifdef FEAT_DIFF
3305 + filler_lines
3306#endif
3307 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3308 {
3309 /* Draw the line number (empty space after wrapping). */
3310 if (row == startrow
3311#ifdef FEAT_DIFF
3312 + filler_lines
3313#endif
3314 )
3315 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003316 sprintf((char *)extra, "%*ld ",
3317 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (wp->w_skipcol > 0)
3319 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3320 *p_extra = '-';
3321#ifdef FEAT_RIGHTLEFT
3322 if (wp->w_p_rl) /* reverse line numbers */
3323 rl_mirror(extra);
3324#endif
3325 p_extra = extra;
3326 c_extra = NUL;
3327 }
3328 else
3329 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003330 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003332#ifdef FEAT_SYN_HL
3333 /* When 'cursorline' is set highlight the line number of
3334 * the current line differently. */
3335 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3336 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339 }
3340
3341#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3342 if (draw_state == WL_SBR - 1 && n_extra == 0)
3343 {
3344 draw_state = WL_SBR;
3345# ifdef FEAT_DIFF
3346 if (filler_todo > 0)
3347 {
3348 /* Draw "deleted" diff line(s). */
3349 if (char2cells(fill_diff) > 1)
3350 c_extra = '-';
3351 else
3352 c_extra = fill_diff;
3353# ifdef FEAT_RIGHTLEFT
3354 if (wp->w_p_rl)
3355 n_extra = col + 1;
3356 else
3357# endif
3358 n_extra = W_WIDTH(wp) - col;
3359 char_attr = hl_attr(HLF_DED);
3360 }
3361# endif
3362# ifdef FEAT_LINEBREAK
3363 if (*p_sbr != NUL && need_showbreak)
3364 {
3365 /* Draw 'showbreak' at the start of each broken line. */
3366 p_extra = p_sbr;
3367 c_extra = NUL;
3368 n_extra = (int)STRLEN(p_sbr);
3369 char_attr = hl_attr(HLF_AT);
3370 need_showbreak = FALSE;
3371 /* Correct end of highlighted area for 'showbreak',
3372 * required when 'linebreak' is also set. */
3373 if (tocol == vcol)
3374 tocol += n_extra;
3375 }
3376# endif
3377 }
3378#endif
3379
3380 if (draw_state == WL_LINE - 1 && n_extra == 0)
3381 {
3382 draw_state = WL_LINE;
3383 if (saved_n_extra)
3384 {
3385 /* Continue item from end of wrapped line. */
3386 n_extra = saved_n_extra;
3387 c_extra = saved_c_extra;
3388 p_extra = saved_p_extra;
3389 char_attr = saved_char_attr;
3390 }
3391 else
3392 char_attr = 0;
3393 }
3394 }
3395
3396 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003397 if (dollar_vcol != 0 && wp == curwin
3398 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#ifdef FEAT_DIFF
3400 && filler_todo <= 0
3401#endif
3402 )
3403 {
3404 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3405 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003406 /* Pretend we have finished updating the window. Except when
3407 * 'cursorcolumn' is set. */
3408#ifdef FEAT_SYN_HL
3409 if (wp->w_p_cuc)
3410 row = wp->w_cline_row + wp->w_cline_height;
3411 else
3412#endif
3413 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 break;
3415 }
3416
3417 if (draw_state == WL_LINE && area_highlighting)
3418 {
3419 /* handle Visual or match highlighting in this line */
3420 if (vcol == fromcol
3421#ifdef FEAT_MBYTE
3422 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3423 && (*mb_ptr2cells)(ptr) > 1)
3424#endif
3425 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003426 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 && vcol < tocol))
3428 area_attr = attr; /* start highlighting */
3429 else if (area_attr != 0
3430 && (vcol == tocol
3431 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434#ifdef FEAT_SEARCH_EXTRA
3435 if (!n_extra)
3436 {
3437 /*
3438 * Check for start/end of search pattern match.
3439 * After end, check for start/end of next match.
3440 * When another match, have to check for start again.
3441 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003442 * Do this for 'search_hl' and the match list (ordered by
3443 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003445 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003446 cur = wp->w_match_head;
3447 shl_flag = FALSE;
3448 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003450 if (shl_flag == FALSE
3451 && ((cur != NULL
3452 && cur->priority > SEARCH_HL_PRIORITY)
3453 || cur == NULL))
3454 {
3455 shl = &search_hl;
3456 shl_flag = TRUE;
3457 }
3458 else
3459 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 while (shl->rm.regprog != NULL)
3461 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003462 if (shl->startcol != MAXCOL
3463 && v >= (long)shl->startcol
3464 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 shl->attr_cur = shl->attr;
3467 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003468 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 shl->attr_cur = 0;
3471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 next_search_hl(wp, shl, lnum, (colnr_T)v);
3473
3474 /* Need to get the line again, a multi-line regexp
3475 * may have made it invalid. */
3476 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3477 ptr = line + v;
3478
3479 if (shl->lnum == lnum)
3480 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003481 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003483 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003485 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003487 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
3489 /* highlight empty match, try again after
3490 * it */
3491#ifdef FEAT_MBYTE
3492 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003493 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003494 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 else
3496#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003497 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499
3500 /* Loop to check if the match starts at the
3501 * current position */
3502 continue;
3503 }
3504 }
3505 break;
3506 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003507 if (shl != &search_hl && cur != NULL)
3508 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003510
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003511 /* Use attributes from match with highest priority among
3512 * 'search_hl' and the match list. */
3513 search_attr = search_hl.attr_cur;
3514 cur = wp->w_match_head;
3515 shl_flag = FALSE;
3516 while (cur != NULL || shl_flag == FALSE)
3517 {
3518 if (shl_flag == FALSE
3519 && ((cur != NULL
3520 && cur->priority > SEARCH_HL_PRIORITY)
3521 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003522 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003523 shl = &search_hl;
3524 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003525 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003526 else
3527 shl = &cur->hl;
3528 if (shl->attr_cur != 0)
3529 search_attr = shl->attr_cur;
3530 if (shl != &search_hl && cur != NULL)
3531 cur = cur->next;
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534#endif
3535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003537 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003539 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3540 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003542 if (diff_hlf == HLF_TXD && ptr - line > change_end
3543 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003545 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003548
3549 /* Decide which of the highlight attributes to use. */
3550 attr_pri = TRUE;
3551 if (area_attr != 0)
3552 char_attr = area_attr;
3553 else if (search_attr != 0)
3554 char_attr = search_attr;
3555#ifdef LINE_ATTR
3556 /* Use line_attr when not in the Visual or 'incsearch' area
3557 * (area_attr may be 0 when "noinvcur" is set). */
3558 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003559 || vcol < fromcol || vcol_prev < fromcol_prev
3560 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003561 char_attr = line_attr;
3562#endif
3563 else
3564 {
3565 attr_pri = FALSE;
3566#ifdef FEAT_SYN_HL
3567 if (has_syntax)
3568 char_attr = syntax_attr;
3569 else
3570#endif
3571 char_attr = 0;
3572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574
3575 /*
3576 * Get the next character to put on the screen.
3577 */
3578 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003579 * The "p_extra" points to the extra stuff that is inserted to
3580 * represent special characters (non-printable stuff) and other
3581 * things. When all characters are the same, c_extra is used.
3582 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3583 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3585 */
3586 if (n_extra > 0)
3587 {
3588 if (c_extra != NUL)
3589 {
3590 c = c_extra;
3591#ifdef FEAT_MBYTE
3592 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3593 if (enc_utf8 && (*mb_char2len)(c) > 1)
3594 {
3595 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003596 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003597 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599 else
3600 mb_utf8 = FALSE;
3601#endif
3602 }
3603 else
3604 {
3605 c = *p_extra;
3606#ifdef FEAT_MBYTE
3607 if (has_mbyte)
3608 {
3609 mb_c = c;
3610 if (enc_utf8)
3611 {
3612 /* If the UTF-8 character is more than one byte:
3613 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003614 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 mb_utf8 = FALSE;
3616 if (mb_l > n_extra)
3617 mb_l = 1;
3618 else if (mb_l > 1)
3619 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003620 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003622 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 }
3625 else
3626 {
3627 /* if this is a DBCS character, put it in "mb_c" */
3628 mb_l = MB_BYTE2LEN(c);
3629 if (mb_l >= n_extra)
3630 mb_l = 1;
3631 else if (mb_l > 1)
3632 mb_c = (c << 8) + p_extra[1];
3633 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003634 if (mb_l == 0) /* at the NUL at end-of-line */
3635 mb_l = 1;
3636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 /* If a double-width char doesn't fit display a '>' in the
3638 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003639 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640# ifdef FEAT_RIGHTLEFT
3641 wp->w_p_rl ? (col <= 0) :
3642# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003643 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 && (*mb_char2cells)(mb_c) == 2)
3645 {
3646 c = '>';
3647 mb_c = c;
3648 mb_l = 1;
3649 mb_utf8 = FALSE;
3650 multi_attr = hl_attr(HLF_AT);
3651 /* put the pointer back to output the double-width
3652 * character at the start of the next line. */
3653 ++n_extra;
3654 --p_extra;
3655 }
3656 else
3657 {
3658 n_extra -= mb_l - 1;
3659 p_extra += mb_l - 1;
3660 }
3661 }
3662#endif
3663 ++p_extra;
3664 }
3665 --n_extra;
3666 }
3667 else
3668 {
3669 /*
3670 * Get a character from the line itself.
3671 */
3672 c = *ptr;
3673#ifdef FEAT_MBYTE
3674 if (has_mbyte)
3675 {
3676 mb_c = c;
3677 if (enc_utf8)
3678 {
3679 /* If the UTF-8 character is more than one byte: Decode it
3680 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003681 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 mb_utf8 = FALSE;
3683 if (mb_l > 1)
3684 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003685 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 /* Overlong encoded ASCII or ASCII with composing char
3687 * is displayed normally, except a NUL. */
3688 if (mb_c < 0x80)
3689 c = mb_c;
3690 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003691
3692 /* At start of the line we can have a composing char.
3693 * Draw it as a space with a composing char. */
3694 if (utf_iscomposing(mb_c))
3695 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003696 int i;
3697
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003698 for (i = Screen_mco - 1; i > 0; --i)
3699 u8cc[i] = u8cc[i - 1];
3700 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003701 mb_c = ' ';
3702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704
3705 if ((mb_l == 1 && c >= 0x80)
3706 || (mb_l >= 1 && mb_c == 0)
3707 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003708# ifdef UNICODE16
3709 || mb_c >= 0x10000
3710# endif
3711 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
3713 /*
3714 * Illegal UTF-8 byte: display as <xx>.
3715 * Non-BMP character : display as ? or fullwidth ?.
3716 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003717# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
3721 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003722# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (wp->w_p_rl) /* reverse */
3724 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003727# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 else if (utf_char2cells(mb_c) != 2)
3729 STRCPY(extra, "?");
3730 else
3731 /* 0xff1f in UTF-8: full-width '?' */
3732 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 p_extra = extra;
3736 c = *p_extra;
3737 mb_c = mb_ptr2char_adv(&p_extra);
3738 mb_utf8 = (c >= 0x80);
3739 n_extra = (int)STRLEN(p_extra);
3740 c_extra = NUL;
3741 if (area_attr == 0 && search_attr == 0)
3742 {
3743 n_attr = n_extra + 1;
3744 extra_attr = hl_attr(HLF_8);
3745 saved_attr2 = char_attr; /* save current attr */
3746 }
3747 }
3748 else if (mb_l == 0) /* at the NUL at end-of-line */
3749 mb_l = 1;
3750#ifdef FEAT_ARABIC
3751 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3752 {
3753 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003754 int pc, pc1, nc;
3755 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
3757 /* The idea of what is the previous and next
3758 * character depends on 'rightleft'. */
3759 if (wp->w_p_rl)
3760 {
3761 pc = prev_c;
3762 pc1 = prev_c1;
3763 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003764 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 else
3767 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003768 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003770 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772 prev_c = mb_c;
3773
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003774 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 else
3777 prev_c = mb_c;
3778#endif
3779 }
3780 else /* enc_dbcs */
3781 {
3782 mb_l = MB_BYTE2LEN(c);
3783 if (mb_l == 0) /* at the NUL at end-of-line */
3784 mb_l = 1;
3785 else if (mb_l > 1)
3786 {
3787 /* We assume a second byte below 32 is illegal.
3788 * Hopefully this is OK for all double-byte encodings!
3789 */
3790 if (ptr[1] >= 32)
3791 mb_c = (c << 8) + ptr[1];
3792 else
3793 {
3794 if (ptr[1] == NUL)
3795 {
3796 /* head byte at end of line */
3797 mb_l = 1;
3798 transchar_nonprint(extra, c);
3799 }
3800 else
3801 {
3802 /* illegal tail byte */
3803 mb_l = 2;
3804 STRCPY(extra, "XX");
3805 }
3806 p_extra = extra;
3807 n_extra = (int)STRLEN(extra) - 1;
3808 c_extra = NUL;
3809 c = *p_extra++;
3810 if (area_attr == 0 && search_attr == 0)
3811 {
3812 n_attr = n_extra + 1;
3813 extra_attr = hl_attr(HLF_8);
3814 saved_attr2 = char_attr; /* save current attr */
3815 }
3816 mb_c = c;
3817 }
3818 }
3819 }
3820 /* If a double-width char doesn't fit display a '>' in the
3821 * last column; the character is displayed at the start of the
3822 * next line. */
3823 if ((
3824# ifdef FEAT_RIGHTLEFT
3825 wp->w_p_rl ? (col <= 0) :
3826# endif
3827 (col >= W_WIDTH(wp) - 1))
3828 && (*mb_char2cells)(mb_c) == 2)
3829 {
3830 c = '>';
3831 mb_c = c;
3832 mb_utf8 = FALSE;
3833 mb_l = 1;
3834 multi_attr = hl_attr(HLF_AT);
3835 /* Put pointer back so that the character will be
3836 * displayed at the start of the next line. */
3837 --ptr;
3838 }
3839 else if (*ptr != NUL)
3840 ptr += mb_l - 1;
3841
3842 /* If a double-width char doesn't fit at the left side display
3843 * a '<' in the first column. */
3844 if (n_skip > 0 && mb_l > 1)
3845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003847 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 c = ' ';
3849 if (area_attr == 0 && search_attr == 0)
3850 {
3851 n_attr = n_extra + 1;
3852 extra_attr = hl_attr(HLF_AT);
3853 saved_attr2 = char_attr; /* save current attr */
3854 }
3855 mb_c = c;
3856 mb_utf8 = FALSE;
3857 mb_l = 1;
3858 }
3859
3860 }
3861#endif
3862 ++ptr;
3863
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003864 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003865 if (wp->w_p_list && (c == 160
3866#ifdef FEAT_MBYTE
3867 || (mb_utf8 && mb_c == 160)
3868#endif
3869 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003870 {
3871 c = lcs_nbsp;
3872 if (area_attr == 0 && search_attr == 0)
3873 {
3874 n_attr = 1;
3875 extra_attr = hl_attr(HLF_8);
3876 saved_attr2 = char_attr; /* save current attr */
3877 }
3878#ifdef FEAT_MBYTE
3879 mb_c = c;
3880 if (enc_utf8 && (*mb_char2len)(c) > 1)
3881 {
3882 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003883 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003884 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003885 }
3886 else
3887 mb_utf8 = FALSE;
3888#endif
3889 }
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (extra_check)
3892 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003893#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003894 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003895#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003896
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003897#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 /* Get syntax attribute, unless still at the start of the line
3899 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003900 v = (long)(ptr - line);
3901 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
3903 /* Get the syntax attribute for the character. If there
3904 * is an error, disable syntax highlighting. */
3905 save_did_emsg = did_emsg;
3906 did_emsg = FALSE;
3907
Bram Moolenaar217ad922005-03-20 22:37:15 +00003908 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003909# ifdef FEAT_SPELL
3910 has_spell ? &can_spell :
3911# endif
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00003912 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003915 {
3916 wp->w_buffer->b_syn_error = TRUE;
3917 has_syntax = FALSE;
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
3920 did_emsg = save_did_emsg;
3921
3922 /* Need to get the line again, a multi-line regexp may
3923 * have made it invalid. */
3924 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3925 ptr = line + v;
3926
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003927 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003929 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003930 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003932#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003933
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003934#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003935 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003936 * Only do this when there is no syntax highlighting, the
3937 * @Spell cluster is not used or the current syntax item
3938 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003939 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003940 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003941 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003942# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003943 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003944 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003945# endif
3946 if (c != 0 && (
3947# ifdef FEAT_SYN_HL
3948 !has_syntax ||
3949# endif
3950 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003951 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003952 char_u *prev_ptr, *p;
3953 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003954 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003955# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003956 if (has_mbyte)
3957 {
3958 prev_ptr = ptr - mb_l;
3959 v -= mb_l - 1;
3960 }
3961 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003962# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003963 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003964
3965 /* Use nextline[] if possible, it has the start of the
3966 * next line concatenated. */
3967 if ((prev_ptr - line) - nextlinecol >= 0)
3968 p = nextline + (prev_ptr - line) - nextlinecol;
3969 else
3970 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003971 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003972 len = spell_check(wp, p, &spell_hlf, &cap_col,
3973 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003974 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003975
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003976 /* In Insert mode only highlight a word that
3977 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003978 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003979 && (State & INSERT) != 0
3980 && wp->w_cursor.lnum == lnum
3981 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003982 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003983 && wp->w_cursor.col < (colnr_T)word_end)
3984 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003985 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003986 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003987 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003988
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003989 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003990 && (p - nextline) + len > nextline_idx)
3991 {
3992 /* Remember that the good word continues at the
3993 * start of the next line. */
3994 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003995 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003996 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003997
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003998 /* Turn index into actual attributes. */
3999 if (spell_hlf != HLF_COUNT)
4000 spell_attr = highlight_attr[spell_hlf];
4001
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004002 if (cap_col > 0)
4003 {
4004 if (p != prev_ptr
4005 && (p - nextline) + cap_col >= nextline_idx)
4006 {
4007 /* Remember that the word in the next line
4008 * must start with a capital. */
4009 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004010 cap_col = (int)((p - nextline) + cap_col
4011 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004012 }
4013 else
4014 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004015 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004016 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004017 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004018 }
4019 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004020 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004021 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004022 char_attr = hl_combine_attr(char_attr, spell_attr);
4023 else
4024 char_attr = hl_combine_attr(spell_attr, char_attr);
4025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#endif
4027#ifdef FEAT_LINEBREAK
4028 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004029 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 */
4031 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
4034 n_extra = win_lbr_chartabsize(wp, ptr - (
4035# ifdef FEAT_MBYTE
4036 has_mbyte ? mb_l :
4037# endif
4038 1), (colnr_T)vcol, NULL) - 1;
4039 c_extra = ' ';
4040 if (vim_iswhite(c))
4041 c = ' ';
4042 }
4043#endif
4044
4045 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4046 {
4047 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004048 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
4050 n_attr = 1;
4051 extra_attr = hl_attr(HLF_8);
4052 saved_attr2 = char_attr; /* save current attr */
4053 }
4054#ifdef FEAT_MBYTE
4055 mb_c = c;
4056 if (enc_utf8 && (*mb_char2len)(c) > 1)
4057 {
4058 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004059 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 else
4063 mb_utf8 = FALSE;
4064#endif
4065 }
4066 }
4067
4068 /*
4069 * Handling of non-printable characters.
4070 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004071 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 {
4073 /*
4074 * when getting a character from the file, we may have to
4075 * turn it into something else on the way to putting it
4076 * into "ScreenLines".
4077 */
4078 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4079 {
4080 /* tab amount depends on current column */
4081 n_extra = (int)wp->w_buffer->b_p_ts
4082 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4083#ifdef FEAT_MBYTE
4084 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4085#endif
4086 if (wp->w_p_list)
4087 {
4088 c = lcs_tab1;
4089 c_extra = lcs_tab2;
4090 n_attr = n_extra + 1;
4091 extra_attr = hl_attr(HLF_8);
4092 saved_attr2 = char_attr; /* save current attr */
4093#ifdef FEAT_MBYTE
4094 mb_c = c;
4095 if (enc_utf8 && (*mb_char2len)(c) > 1)
4096 {
4097 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004098 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004099 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101#endif
4102 }
4103 else
4104 {
4105 c_extra = ' ';
4106 c = ' ';
4107 }
4108 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004109 else if (c == NUL
4110 && ((wp->w_p_list && lcs_eol > 0)
4111 || ((fromcol >= 0 || fromcol_prev >= 0)
4112 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004113#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004114 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004115#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004116 && (
4117# ifdef FEAT_RIGHTLEFT
4118 wp->w_p_rl ? (col >= 0) :
4119# endif
4120 (col < W_WIDTH(wp)))
4121 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004122 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004123 && (colnr_T)vcol == wp->w_virtcol)))
4124 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004126 /* Display a '$' after the line or highlight an extra
4127 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4129 /* For a diff line the highlighting continues after the
4130 * "$". */
4131 if (
4132# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004133 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134# ifdef LINE_ATTR
4135 &&
4136# endif
4137# endif
4138# ifdef LINE_ATTR
4139 line_attr == 0
4140# endif
4141 )
4142#endif
4143 {
4144#ifdef FEAT_VIRTUALEDIT
4145 /* In virtualedit, visual selections may extend
4146 * beyond end of line. */
4147 if (area_highlighting && virtual_active()
4148 && tocol != MAXCOL && vcol < tocol)
4149 n_extra = 0;
4150 else
4151#endif
4152 {
4153 p_extra = at_end_str;
4154 n_extra = 1;
4155 c_extra = NUL;
4156 }
4157 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004158 if (wp->w_p_list)
4159 c = lcs_eol;
4160 else
4161 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 lcs_eol_one = -1;
4163 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004164 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 extra_attr = hl_attr(HLF_AT);
4167 n_attr = 1;
4168 }
4169#ifdef FEAT_MBYTE
4170 mb_c = c;
4171 if (enc_utf8 && (*mb_char2len)(c) > 1)
4172 {
4173 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004174 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004175 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177 else
4178 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4179#endif
4180 }
4181 else if (c != NUL)
4182 {
4183 p_extra = transchar(c);
4184#ifdef FEAT_RIGHTLEFT
4185 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4186 rl_mirror(p_extra); /* reverse "<12>" */
4187#endif
4188 n_extra = byte2cells(c) - 1;
4189 c_extra = NUL;
4190 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004191 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
4193 n_attr = n_extra + 1;
4194 extra_attr = hl_attr(HLF_8);
4195 saved_attr2 = char_attr; /* save current attr */
4196 }
4197#ifdef FEAT_MBYTE
4198 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4199#endif
4200 }
4201#ifdef FEAT_VIRTUALEDIT
4202 else if (VIsual_active
4203 && (VIsual_mode == Ctrl_V
4204 || VIsual_mode == 'v')
4205 && virtual_active()
4206 && tocol != MAXCOL
4207 && vcol < tocol
4208 && (
4209# ifdef FEAT_RIGHTLEFT
4210 wp->w_p_rl ? (col >= 0) :
4211# endif
4212 (col < W_WIDTH(wp))))
4213 {
4214 c = ' ';
4215 --ptr; /* put it back at the NUL */
4216 }
4217#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004218#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 else if ((
4220# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004221 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 ) && (
4225# ifdef FEAT_RIGHTLEFT
4226 wp->w_p_rl ? (col >= 0) :
4227# endif
4228 (col < W_WIDTH(wp))))
4229 {
4230 /* Highlight until the right side of the window */
4231 c = ' ';
4232 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004233
4234 /* Remember we do the char for line highlighting. */
4235 ++did_line_attr;
4236
4237 /* don't do search HL for the rest of the line */
4238 if (line_attr != 0 && char_attr == search_attr && col > 0)
4239 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240# ifdef FEAT_DIFF
4241 if (diff_hlf == HLF_TXD)
4242 {
4243 diff_hlf = HLF_CHD;
4244 if (attr == 0 || char_attr != attr)
4245 char_attr = hl_attr(diff_hlf);
4246 }
4247# endif
4248 }
4249#endif
4250 }
4251 }
4252
4253 /* Don't override visual selection highlighting. */
4254 if (n_attr > 0
4255 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004256 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 char_attr = extra_attr;
4258
Bram Moolenaar81695252004-12-29 20:58:21 +00004259#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 /* XIM don't send preedit_start and preedit_end, but they send
4261 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4262 * im_is_preediting() here. */
4263 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004264 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 && (State & INSERT)
4266 && !p_imdisable
4267 && im_is_preediting()
4268 && draw_state == WL_LINE)
4269 {
4270 colnr_T tcol;
4271
4272 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004273 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 else
4275 tcol = preedit_end_col;
4276 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4277 {
4278 if (feedback_old_attr < 0)
4279 {
4280 feedback_col = 0;
4281 feedback_old_attr = char_attr;
4282 }
4283 char_attr = im_get_feedback_attr(feedback_col);
4284 if (char_attr < 0)
4285 char_attr = feedback_old_attr;
4286 feedback_col++;
4287 }
4288 else if (feedback_old_attr >= 0)
4289 {
4290 char_attr = feedback_old_attr;
4291 feedback_old_attr = -1;
4292 feedback_col = 0;
4293 }
4294 }
4295#endif
4296 /*
4297 * Handle the case where we are in column 0 but not on the first
4298 * character of the line and the user wants us to show us a
4299 * special character (via 'listchars' option "precedes:<char>".
4300 */
4301 if (lcs_prec_todo != NUL
4302 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4303#ifdef FEAT_DIFF
4304 && filler_todo <= 0
4305#endif
4306 && draw_state > WL_NR
4307 && c != NUL)
4308 {
4309 c = lcs_prec;
4310 lcs_prec_todo = NUL;
4311#ifdef FEAT_MBYTE
4312 mb_c = c;
4313 if (enc_utf8 && (*mb_char2len)(c) > 1)
4314 {
4315 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004316 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004317 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 else
4320 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4321#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004322 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
4324 saved_attr3 = char_attr; /* save current attr */
4325 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4326 n_attr3 = 1;
4327 }
4328 }
4329
4330 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004331 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004333 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004334#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004335 || did_line_attr == 1
4336#endif
4337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004339#ifdef FEAT_SEARCH_EXTRA
4340 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004341
4342 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004343 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004344 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004345#endif
4346
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 /* invert at least one char, used for Visual and empty line or
4348 * highlight match at end of line. If it's beyond the last
4349 * char on the screen, just overwrite that one (tricky!) Not
4350 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004351#ifdef FEAT_SEARCH_EXTRA
4352 prevcol_hl_flag = FALSE;
4353 if (prevcol == (long)search_hl.startcol)
4354 prevcol_hl_flag = TRUE;
4355 else
4356 {
4357 cur = wp->w_match_head;
4358 while (cur != NULL)
4359 {
4360 if (prevcol == (long)cur->hl.startcol)
4361 {
4362 prevcol_hl_flag = TRUE;
4363 break;
4364 }
4365 cur = cur->next;
4366 }
4367 }
4368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004370 && ((area_attr != 0 && vcol == fromcol
4371#ifdef FEAT_VISUAL
4372 && (VIsual_mode != Ctrl_V
4373 || lnum == VIsual.lnum
4374 || lnum == curwin->w_cursor.lnum)
4375#endif
4376 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377#ifdef FEAT_SEARCH_EXTRA
4378 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004379 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004380# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004381 && did_line_attr <= 1
4382# endif
4383 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#endif
4385 ))
4386 {
4387 int n = 0;
4388
4389#ifdef FEAT_RIGHTLEFT
4390 if (wp->w_p_rl)
4391 {
4392 if (col < 0)
4393 n = 1;
4394 }
4395 else
4396#endif
4397 {
4398 if (col >= W_WIDTH(wp))
4399 n = -1;
4400 }
4401 if (n != 0)
4402 {
4403 /* At the window boundary, highlight the last character
4404 * instead (better than nothing). */
4405 off += n;
4406 col += n;
4407 }
4408 else
4409 {
4410 /* Add a blank character to highlight. */
4411 ScreenLines[off] = ' ';
4412#ifdef FEAT_MBYTE
4413 if (enc_utf8)
4414 ScreenLinesUC[off] = 0;
4415#endif
4416 }
4417#ifdef FEAT_SEARCH_EXTRA
4418 if (area_attr == 0)
4419 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004420 /* Use attributes from match with highest priority among
4421 * 'search_hl' and the match list. */
4422 char_attr = search_hl.attr;
4423 cur = wp->w_match_head;
4424 shl_flag = FALSE;
4425 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004426 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004427 if (shl_flag == FALSE
4428 && ((cur != NULL
4429 && cur->priority > SEARCH_HL_PRIORITY)
4430 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004431 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004432 shl = &search_hl;
4433 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004434 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004435 else
4436 shl = &cur->hl;
4437 if ((ptr - line) - 1 == (long)shl->startcol)
4438 char_attr = shl->attr;
4439 if (shl != &search_hl && cur != NULL)
4440 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443#endif
4444 ScreenAttrs[off] = char_attr;
4445#ifdef FEAT_RIGHTLEFT
4446 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004449 --off;
4450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 else
4452#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004455 ++off;
4456 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004457 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004458#ifdef FEAT_SYN_HL
4459 eol_hl_off = 1;
4460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
Bram Moolenaar91170f82006-05-05 21:15:17 +00004464 /*
4465 * At end of the text line.
4466 */
4467 if (c == NUL)
4468 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004469#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004470 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4471 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004472 {
4473 /* highlight last char after line */
4474 --col;
4475 --off;
4476 --vcol;
4477 }
4478
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004479 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004480 if (wp->w_p_wrap)
4481 v = wp->w_skipcol;
4482 else
4483 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004484 /* check if line ends before left margin */
4485 if (vcol < v + col - win_col_off(wp))
4486
4487 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004488 if (wp->w_p_cuc
Bram Moolenaara443af82007-11-08 13:51:42 +00004489 && (int)wp->w_virtcol >= vcol - eol_hl_off
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004490 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4491 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004492 && lnum != wp->w_cursor.lnum
4493# ifdef FEAT_RIGHTLEFT
4494 && !wp->w_p_rl
4495# endif
4496 )
4497 {
4498 while (col < W_WIDTH(wp))
4499 {
4500 ScreenLines[off] = ' ';
4501#ifdef FEAT_MBYTE
4502 if (enc_utf8)
4503 ScreenLinesUC[off] = 0;
4504#endif
4505 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004506 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004507 {
4508 ScreenAttrs[off] = hl_attr(HLF_CUC);
4509 break;
4510 }
4511 ScreenAttrs[off++] = 0;
4512 ++vcol;
4513 }
4514 }
4515#endif
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4518 wp->w_p_rl);
4519 row++;
4520
4521 /*
4522 * Update w_cline_height and w_cline_folded if the cursor line was
4523 * updated (saves a call to plines() later).
4524 */
4525 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4526 {
4527 curwin->w_cline_row = startrow;
4528 curwin->w_cline_height = row - startrow;
4529#ifdef FEAT_FOLDING
4530 curwin->w_cline_folded = FALSE;
4531#endif
4532 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4533 }
4534
4535 break;
4536 }
4537
4538 /* line continues beyond line end */
4539 if (lcs_ext
4540 && !wp->w_p_wrap
4541#ifdef FEAT_DIFF
4542 && filler_todo <= 0
4543#endif
4544 && (
4545#ifdef FEAT_RIGHTLEFT
4546 wp->w_p_rl ? col == 0 :
4547#endif
4548 col == W_WIDTH(wp) - 1)
4549 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004550 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4552 {
4553 c = lcs_ext;
4554 char_attr = hl_attr(HLF_AT);
4555#ifdef FEAT_MBYTE
4556 mb_c = c;
4557 if (enc_utf8 && (*mb_char2len)(c) > 1)
4558 {
4559 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004560 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004561 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563 else
4564 mb_utf8 = FALSE;
4565#endif
4566 }
4567
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004568#ifdef FEAT_SYN_HL
4569 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4570 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004571 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004572 && lnum != wp->w_cursor.lnum
Bram Moolenaar54ef7112009-02-21 20:11:41 +00004573 && draw_state == WL_LINE
4574 && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004575 {
4576 vcol_save_attr = char_attr;
4577 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4578 }
4579 else
4580 vcol_save_attr = -1;
4581#endif
4582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /*
4584 * Store character to be displayed.
4585 * Skip characters that are left of the screen for 'nowrap'.
4586 */
4587 vcol_prev = vcol;
4588 if (draw_state < WL_LINE || n_skip <= 0)
4589 {
4590 /*
4591 * Store the character.
4592 */
4593#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4594 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4595 {
4596 /* A double-wide character is: put first halve in left cell. */
4597 --off;
4598 --col;
4599 }
4600#endif
4601 ScreenLines[off] = c;
4602#ifdef FEAT_MBYTE
4603 if (enc_dbcs == DBCS_JPNU)
4604 ScreenLines2[off] = mb_c & 0xff;
4605 else if (enc_utf8)
4606 {
4607 if (mb_utf8)
4608 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004609 int i;
4610
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004612 if ((c & 0xff) == 0)
4613 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004614 for (i = 0; i < Screen_mco; ++i)
4615 {
4616 ScreenLinesC[i][off] = u8cc[i];
4617 if (u8cc[i] == 0)
4618 break;
4619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621 else
4622 ScreenLinesUC[off] = 0;
4623 }
4624 if (multi_attr)
4625 {
4626 ScreenAttrs[off] = multi_attr;
4627 multi_attr = 0;
4628 }
4629 else
4630#endif
4631 ScreenAttrs[off] = char_attr;
4632
4633#ifdef FEAT_MBYTE
4634 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4635 {
4636 /* Need to fill two screen columns. */
4637 ++off;
4638 ++col;
4639 if (enc_utf8)
4640 /* UTF-8: Put a 0 in the second screen char. */
4641 ScreenLines[off] = 0;
4642 else
4643 /* DBCS: Put second byte in the second screen char. */
4644 ScreenLines[off] = mb_c & 0xff;
4645 ++vcol;
4646 /* When "tocol" is halfway a character, set it to the end of
4647 * the character, otherwise highlighting won't stop. */
4648 if (tocol == vcol)
4649 ++tocol;
4650#ifdef FEAT_RIGHTLEFT
4651 if (wp->w_p_rl)
4652 {
4653 /* now it's time to backup one cell */
4654 --off;
4655 --col;
4656 }
4657#endif
4658 }
4659#endif
4660#ifdef FEAT_RIGHTLEFT
4661 if (wp->w_p_rl)
4662 {
4663 --off;
4664 --col;
4665 }
4666 else
4667#endif
4668 {
4669 ++off;
4670 ++col;
4671 }
4672 }
4673 else
4674 --n_skip;
4675
4676 /* Only advance the "vcol" when after the 'number' column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00004677 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678#ifdef FEAT_DIFF
4679 && filler_todo <= 0
4680#endif
4681 )
4682 ++vcol;
4683
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004684#ifdef FEAT_SYN_HL
4685 if (vcol_save_attr >= 0)
4686 char_attr = vcol_save_attr;
4687#endif
4688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 /* restore attributes after "predeces" in 'listchars' */
4690 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4691 char_attr = saved_attr3;
4692
4693 /* restore attributes after last 'listchars' or 'number' char */
4694 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4695 char_attr = saved_attr2;
4696
4697 /*
4698 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004699 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 */
4701 if ((
4702#ifdef FEAT_RIGHTLEFT
4703 wp->w_p_rl ? (col < 0) :
4704#endif
4705 (col >= W_WIDTH(wp)))
4706 && (*ptr != NUL
4707#ifdef FEAT_DIFF
4708 || filler_todo > 0
4709#endif
4710 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4711 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4712 )
4713 {
4714 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4715 wp->w_p_rl);
4716 ++row;
4717 ++screen_row;
4718
4719 /* When not wrapping and finished diff lines, or when displayed
4720 * '$' and highlighting until last column, break here. */
4721 if ((!wp->w_p_wrap
4722#ifdef FEAT_DIFF
4723 && filler_todo <= 0
4724#endif
4725 ) || lcs_eol_one == -1)
4726 break;
4727
4728 /* When the window is too narrow draw all "@" lines. */
4729 if (draw_state != WL_LINE
4730#ifdef FEAT_DIFF
4731 && filler_todo <= 0
4732#endif
4733 )
4734 {
4735 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4736#ifdef FEAT_VERTSPLIT
4737 draw_vsep_win(wp, row);
4738#endif
4739 row = endrow;
4740 }
4741
4742 /* When line got too long for screen break here. */
4743 if (row == endrow)
4744 {
4745 ++row;
4746 break;
4747 }
4748
4749 if (screen_cur_row == screen_row - 1
4750#ifdef FEAT_DIFF
4751 && filler_todo <= 0
4752#endif
4753 && W_WIDTH(wp) == Columns)
4754 {
4755 /* Remember that the line wraps, used for modeless copy. */
4756 LineWraps[screen_row - 1] = TRUE;
4757
4758 /*
4759 * Special trick to make copy/paste of wrapped lines work with
4760 * xterm/screen: write an extra character beyond the end of
4761 * the line. This will work with all terminal types
4762 * (regardless of the xn,am settings).
4763 * Only do this on a fast tty.
4764 * Only do this if the cursor is on the current line
4765 * (something has been written in it).
4766 * Don't do this for the GUI.
4767 * Don't do this for double-width characters.
4768 * Don't do this for a window not at the right screen border.
4769 */
4770 if (p_tf
4771#ifdef FEAT_GUI
4772 && !gui.in_use
4773#endif
4774#ifdef FEAT_MBYTE
4775 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004776 && ((*mb_off2cells)(LineOffset[screen_row],
4777 LineOffset[screen_row] + screen_Columns)
4778 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004780 + (int)Columns - 2,
4781 LineOffset[screen_row] + screen_Columns)
4782 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783#endif
4784 )
4785 {
4786 /* First make sure we are at the end of the screen line,
4787 * then output the same character again to let the
4788 * terminal know about the wrap. If the terminal doesn't
4789 * auto-wrap, we overwrite the character. */
4790 if (screen_cur_col != W_WIDTH(wp))
4791 screen_char(LineOffset[screen_row - 1]
4792 + (unsigned)Columns - 1,
4793 screen_row - 1, (int)(Columns - 1));
4794
4795#ifdef FEAT_MBYTE
4796 /* When there is a multi-byte character, just output a
4797 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004798 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4799 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 out_char(' ');
4801 else
4802#endif
4803 out_char(ScreenLines[LineOffset[screen_row - 1]
4804 + (Columns - 1)]);
4805 /* force a redraw of the first char on the next line */
4806 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4807 screen_start(); /* don't know where cursor is now */
4808 }
4809 }
4810
4811 col = 0;
4812 off = (unsigned)(current_ScreenLine - ScreenLines);
4813#ifdef FEAT_RIGHTLEFT
4814 if (wp->w_p_rl)
4815 {
4816 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4817 off += col;
4818 }
4819#endif
4820
4821 /* reset the drawing state for the start of a wrapped line */
4822 draw_state = WL_START;
4823 saved_n_extra = n_extra;
4824 saved_p_extra = p_extra;
4825 saved_c_extra = c_extra;
4826 saved_char_attr = char_attr;
4827 n_extra = 0;
4828 lcs_prec_todo = lcs_prec;
4829#ifdef FEAT_LINEBREAK
4830# ifdef FEAT_DIFF
4831 if (filler_todo <= 0)
4832# endif
4833 need_showbreak = TRUE;
4834#endif
4835#ifdef FEAT_DIFF
4836 --filler_todo;
4837 /* When the filler lines are actually below the last line of the
4838 * file, don't draw the line itself, break here. */
4839 if (filler_todo == 0 && wp->w_botfill)
4840 break;
4841#endif
4842 }
4843
4844 } /* for every character in the line */
4845
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004846#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004847 /* After an empty line check first word for capital. */
4848 if (*skipwhite(line) == NUL)
4849 {
4850 capcol_lnum = lnum + 1;
4851 cap_col = 0;
4852 }
4853#endif
4854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 return row;
4856}
4857
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004858#ifdef FEAT_MBYTE
4859static int comp_char_differs __ARGS((int, int));
4860
4861/*
4862 * Return if the composing characters at "off_from" and "off_to" differ.
4863 */
4864 static int
4865comp_char_differs(off_from, off_to)
4866 int off_from;
4867 int off_to;
4868{
4869 int i;
4870
4871 for (i = 0; i < Screen_mco; ++i)
4872 {
4873 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4874 return TRUE;
4875 if (ScreenLinesC[i][off_from] == 0)
4876 break;
4877 }
4878 return FALSE;
4879}
4880#endif
4881
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882/*
4883 * Check whether the given character needs redrawing:
4884 * - the (first byte of the) character is different
4885 * - the attributes are different
4886 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004887 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 */
4889 static int
4890char_needs_redraw(off_from, off_to, cols)
4891 int off_from;
4892 int off_to;
4893 int cols;
4894{
4895 if (cols > 0
4896 && ((ScreenLines[off_from] != ScreenLines[off_to]
4897 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4898
4899#ifdef FEAT_MBYTE
4900 || (enc_dbcs != 0
4901 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4902 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4903 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4904 : (cols > 1 && ScreenLines[off_from + 1]
4905 != ScreenLines[off_to + 1])))
4906 || (enc_utf8
4907 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4908 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004909 && comp_char_differs(off_from, off_to))
4910 || (cols > 1 && ScreenLines[off_from + 1]
4911 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912#endif
4913 ))
4914 return TRUE;
4915 return FALSE;
4916}
4917
4918/*
4919 * Move one "cooked" screen line to the screen, but only the characters that
4920 * have actually changed. Handle insert/delete character.
4921 * "coloff" gives the first column on the screen for this line.
4922 * "endcol" gives the columns where valid characters are.
4923 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4924 * needs to be cleared, negative otherwise.
4925 * "rlflag" is TRUE in a rightleft window:
4926 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4927 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4928 */
4929 static void
4930screen_line(row, coloff, endcol, clear_width
4931#ifdef FEAT_RIGHTLEFT
4932 , rlflag
4933#endif
4934 )
4935 int row;
4936 int coloff;
4937 int endcol;
4938 int clear_width;
4939#ifdef FEAT_RIGHTLEFT
4940 int rlflag;
4941#endif
4942{
4943 unsigned off_from;
4944 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004945#ifdef FEAT_MBYTE
4946 unsigned max_off_from;
4947 unsigned max_off_to;
4948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 int col = 0;
4950#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4951 int hl;
4952#endif
4953 int force = FALSE; /* force update rest of the line */
4954 int redraw_this /* bool: does character need redraw? */
4955#ifdef FEAT_GUI
4956 = TRUE /* For GUI when while-loop empty */
4957#endif
4958 ;
4959 int redraw_next; /* redraw_this for next character */
4960#ifdef FEAT_MBYTE
4961 int clear_next = FALSE;
4962 int char_cells; /* 1: normal char */
4963 /* 2: occupies two display cells */
4964# define CHAR_CELLS char_cells
4965#else
4966# define CHAR_CELLS 1
4967#endif
4968
4969# ifdef FEAT_CLIPBOARD
4970 clip_may_clear_selection(row, row);
4971# endif
4972
4973 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4974 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004975#ifdef FEAT_MBYTE
4976 max_off_from = off_from + screen_Columns;
4977 max_off_to = LineOffset[row] + screen_Columns;
4978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
4980#ifdef FEAT_RIGHTLEFT
4981 if (rlflag)
4982 {
4983 /* Clear rest first, because it's left of the text. */
4984 if (clear_width > 0)
4985 {
4986 while (col <= endcol && ScreenLines[off_to] == ' '
4987 && ScreenAttrs[off_to] == 0
4988# ifdef FEAT_MBYTE
4989 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4990# endif
4991 )
4992 {
4993 ++off_to;
4994 ++col;
4995 }
4996 if (col <= endcol)
4997 screen_fill(row, row + 1, col + coloff,
4998 endcol + coloff + 1, ' ', ' ', 0);
4999 }
5000 col = endcol + 1;
5001 off_to = LineOffset[row] + col + coloff;
5002 off_from += col;
5003 endcol = (clear_width > 0 ? clear_width : -clear_width);
5004 }
5005#endif /* FEAT_RIGHTLEFT */
5006
5007 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5008
5009 while (col < endcol)
5010 {
5011#ifdef FEAT_MBYTE
5012 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005013 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 else
5015 char_cells = 1;
5016#endif
5017
5018 redraw_this = redraw_next;
5019 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5020 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5021
5022#ifdef FEAT_GUI
5023 /* If the next character was bold, then redraw the current character to
5024 * remove any pixels that might have spilt over into us. This only
5025 * happens in the GUI.
5026 */
5027 if (redraw_next && gui.in_use)
5028 {
5029 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005030 if (hl > HL_ALL)
5031 hl = syn_attr2attr(hl);
5032 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 redraw_this = TRUE;
5034 }
5035#endif
5036
5037 if (redraw_this)
5038 {
5039 /*
5040 * Special handling when 'xs' termcap flag set (hpterm):
5041 * Attributes for characters are stored at the position where the
5042 * cursor is when writing the highlighting code. The
5043 * start-highlighting code must be written with the cursor on the
5044 * first highlighted character. The stop-highlighting code must
5045 * be written with the cursor just after the last highlighted
5046 * character.
5047 * Overwriting a character doesn't remove it's highlighting. Need
5048 * to clear the rest of the line, and force redrawing it
5049 * completely.
5050 */
5051 if ( p_wiv
5052 && !force
5053#ifdef FEAT_GUI
5054 && !gui.in_use
5055#endif
5056 && ScreenAttrs[off_to] != 0
5057 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5058 {
5059 /*
5060 * Need to remove highlighting attributes here.
5061 */
5062 windgoto(row, col + coloff);
5063 out_str(T_CE); /* clear rest of this screen line */
5064 screen_start(); /* don't know where cursor is now */
5065 force = TRUE; /* force redraw of rest of the line */
5066 redraw_next = TRUE; /* or else next char would miss out */
5067
5068 /*
5069 * If the previous character was highlighted, need to stop
5070 * highlighting at this character.
5071 */
5072 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5073 {
5074 screen_attr = ScreenAttrs[off_to - 1];
5075 term_windgoto(row, col + coloff);
5076 screen_stop_highlight();
5077 }
5078 else
5079 screen_attr = 0; /* highlighting has stopped */
5080 }
5081#ifdef FEAT_MBYTE
5082 if (enc_dbcs != 0)
5083 {
5084 /* Check if overwriting a double-byte with a single-byte or
5085 * the other way around requires another character to be
5086 * redrawn. For UTF-8 this isn't needed, because comparing
5087 * ScreenLinesUC[] is sufficient. */
5088 if (char_cells == 1
5089 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005090 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 /* Writing a single-cell character over a double-cell
5093 * character: need to redraw the next cell. */
5094 ScreenLines[off_to + 1] = 0;
5095 redraw_next = TRUE;
5096 }
5097 else if (char_cells == 2
5098 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005099 && (*mb_off2cells)(off_to, max_off_to) == 1
5100 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
5102 /* Writing the second half of a double-cell character over
5103 * a double-cell character: need to redraw the second
5104 * cell. */
5105 ScreenLines[off_to + 2] = 0;
5106 redraw_next = TRUE;
5107 }
5108
5109 if (enc_dbcs == DBCS_JPNU)
5110 ScreenLines2[off_to] = ScreenLines2[off_from];
5111 }
5112 /* When writing a single-width character over a double-width
5113 * character and at the end of the redrawn text, need to clear out
5114 * the right halve of the old character.
5115 * Also required when writing the right halve of a double-width
5116 * char over the left halve of an existing one. */
5117 if (has_mbyte && col + char_cells == endcol
5118 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005119 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005121 && (*mb_off2cells)(off_to, max_off_to) == 1
5122 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 clear_next = TRUE;
5124#endif
5125
5126 ScreenLines[off_to] = ScreenLines[off_from];
5127#ifdef FEAT_MBYTE
5128 if (enc_utf8)
5129 {
5130 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5131 if (ScreenLinesUC[off_from] != 0)
5132 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005133 int i;
5134
5135 for (i = 0; i < Screen_mco; ++i)
5136 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138 }
5139 if (char_cells == 2)
5140 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5141#endif
5142
5143#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005144 /* The bold trick makes a single column of pixels appear in the
5145 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 * character should be redrawn too. This happens for our own GUI
5147 * and for some xterms. */
5148 if (
5149# ifdef FEAT_GUI
5150 gui.in_use
5151# endif
5152# if defined(FEAT_GUI) && defined(UNIX)
5153 ||
5154# endif
5155# ifdef UNIX
5156 term_is_xterm
5157# endif
5158 )
5159 {
5160 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005161 if (hl > HL_ALL)
5162 hl = syn_attr2attr(hl);
5163 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 redraw_next = TRUE;
5165 }
5166#endif
5167 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5168#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005169 /* For simplicity set the attributes of second half of a
5170 * double-wide character equal to the first half. */
5171 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005173
5174 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 else
5177#endif
5178 screen_char(off_to, row, col + coloff);
5179 }
5180 else if ( p_wiv
5181#ifdef FEAT_GUI
5182 && !gui.in_use
5183#endif
5184 && col + coloff > 0)
5185 {
5186 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5187 {
5188 /*
5189 * Don't output stop-highlight when moving the cursor, it will
5190 * stop the highlighting when it should continue.
5191 */
5192 screen_attr = 0;
5193 }
5194 else if (screen_attr != 0)
5195 screen_stop_highlight();
5196 }
5197
5198 off_to += CHAR_CELLS;
5199 off_from += CHAR_CELLS;
5200 col += CHAR_CELLS;
5201 }
5202
5203#ifdef FEAT_MBYTE
5204 if (clear_next)
5205 {
5206 /* Clear the second half of a double-wide character of which the left
5207 * half was overwritten with a single-wide character. */
5208 ScreenLines[off_to] = ' ';
5209 if (enc_utf8)
5210 ScreenLinesUC[off_to] = 0;
5211 screen_char(off_to, row, col + coloff);
5212 }
5213#endif
5214
5215 if (clear_width > 0
5216#ifdef FEAT_RIGHTLEFT
5217 && !rlflag
5218#endif
5219 )
5220 {
5221#ifdef FEAT_GUI
5222 int startCol = col;
5223#endif
5224
5225 /* blank out the rest of the line */
5226 while (col < clear_width && ScreenLines[off_to] == ' '
5227 && ScreenAttrs[off_to] == 0
5228#ifdef FEAT_MBYTE
5229 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5230#endif
5231 )
5232 {
5233 ++off_to;
5234 ++col;
5235 }
5236 if (col < clear_width)
5237 {
5238#ifdef FEAT_GUI
5239 /*
5240 * In the GUI, clearing the rest of the line may leave pixels
5241 * behind if the first character cleared was bold. Some bold
5242 * fonts spill over the left. In this case we redraw the previous
5243 * character too. If we didn't skip any blanks above, then we
5244 * only redraw if the character wasn't already redrawn anyway.
5245 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005246 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
5248 hl = ScreenAttrs[off_to];
5249 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005250 {
5251 int prev_cells = 1;
5252# ifdef FEAT_MBYTE
5253 if (enc_utf8)
5254 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5255 * that its width is 2. */
5256 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5257 else if (enc_dbcs != 0)
5258 {
5259 /* find previous character by counting from first
5260 * column and get its width. */
5261 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005262 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005263
5264 while (off < off_to)
5265 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005266 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005267 off += prev_cells;
5268 }
5269 }
5270
5271 if (enc_dbcs != 0 && prev_cells > 1)
5272 screen_char_2(off_to - prev_cells, row,
5273 col + coloff - prev_cells);
5274 else
5275# endif
5276 screen_char(off_to - prev_cells, row,
5277 col + coloff - prev_cells);
5278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280#endif
5281 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5282 ' ', ' ', 0);
5283#ifdef FEAT_VERTSPLIT
5284 off_to += clear_width - col;
5285 col = clear_width;
5286#endif
5287 }
5288 }
5289
5290 if (clear_width > 0)
5291 {
5292#ifdef FEAT_VERTSPLIT
5293 /* For a window that's left of another, draw the separator char. */
5294 if (col + coloff < Columns)
5295 {
5296 int c;
5297
5298 c = fillchar_vsep(&hl);
5299 if (ScreenLines[off_to] != c
5300# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005301 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5302 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303# endif
5304 || ScreenAttrs[off_to] != hl)
5305 {
5306 ScreenLines[off_to] = c;
5307 ScreenAttrs[off_to] = hl;
5308# ifdef FEAT_MBYTE
5309 if (enc_utf8)
5310 {
5311 if (c >= 0x80)
5312 {
5313 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005314 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316 else
5317 ScreenLinesUC[off_to] = 0;
5318 }
5319# endif
5320 screen_char(off_to, row, col + coloff);
5321 }
5322 }
5323 else
5324#endif
5325 LineWraps[row] = FALSE;
5326 }
5327}
5328
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005329#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005331 * Mirror text "str" for right-left displaying.
5332 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005334 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335rl_mirror(str)
5336 char_u *str;
5337{
5338 char_u *p1, *p2;
5339 int t;
5340
5341 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5342 {
5343 t = *p1;
5344 *p1 = *p2;
5345 *p2 = t;
5346 }
5347}
5348#endif
5349
5350#if defined(FEAT_WINDOWS) || defined(PROTO)
5351/*
5352 * mark all status lines for redraw; used after first :cd
5353 */
5354 void
5355status_redraw_all()
5356{
5357 win_T *wp;
5358
5359 for (wp = firstwin; wp; wp = wp->w_next)
5360 if (wp->w_status_height)
5361 {
5362 wp->w_redr_status = TRUE;
5363 redraw_later(VALID);
5364 }
5365}
5366
5367/*
5368 * mark all status lines of the current buffer for redraw
5369 */
5370 void
5371status_redraw_curbuf()
5372{
5373 win_T *wp;
5374
5375 for (wp = firstwin; wp; wp = wp->w_next)
5376 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5377 {
5378 wp->w_redr_status = TRUE;
5379 redraw_later(VALID);
5380 }
5381}
5382
5383/*
5384 * Redraw all status lines that need to be redrawn.
5385 */
5386 void
5387redraw_statuslines()
5388{
5389 win_T *wp;
5390
5391 for (wp = firstwin; wp; wp = wp->w_next)
5392 if (wp->w_redr_status)
5393 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005394 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005395 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396}
5397#endif
5398
5399#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5400/*
5401 * Redraw all status lines at the bottom of frame "frp".
5402 */
5403 void
5404win_redraw_last_status(frp)
5405 frame_T *frp;
5406{
5407 if (frp->fr_layout == FR_LEAF)
5408 frp->fr_win->w_redr_status = TRUE;
5409 else if (frp->fr_layout == FR_ROW)
5410 {
5411 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5412 win_redraw_last_status(frp);
5413 }
5414 else /* frp->fr_layout == FR_COL */
5415 {
5416 frp = frp->fr_child;
5417 while (frp->fr_next != NULL)
5418 frp = frp->fr_next;
5419 win_redraw_last_status(frp);
5420 }
5421}
5422#endif
5423
5424#ifdef FEAT_VERTSPLIT
5425/*
5426 * Draw the verticap separator right of window "wp" starting with line "row".
5427 */
5428 static void
5429draw_vsep_win(wp, row)
5430 win_T *wp;
5431 int row;
5432{
5433 int hl;
5434 int c;
5435
5436 if (wp->w_vsep_width)
5437 {
5438 /* draw the vertical separator right of this window */
5439 c = fillchar_vsep(&hl);
5440 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5441 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5442 c, ' ', hl);
5443 }
5444}
5445#endif
5446
5447#ifdef FEAT_WILDMENU
5448static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005449static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005452 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 */
5454 static int
5455status_match_len(xp, s)
5456 expand_T *xp;
5457 char_u *s;
5458{
5459 int len = 0;
5460
5461#ifdef FEAT_MENU
5462 int emenu = (xp->xp_context == EXPAND_MENUS
5463 || xp->xp_context == EXPAND_MENUNAMES);
5464
5465 /* Check for menu separators - replace with '|'. */
5466 if (emenu && menu_is_separator(s))
5467 return 1;
5468#endif
5469
5470 while (*s != NUL)
5471 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005472 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005473 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005474 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476
5477 return len;
5478}
5479
5480/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005481 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005482 * These are backslashes used for escaping. Do show backslashes in help tags.
5483 */
5484 static int
5485skip_status_match_char(xp, s)
5486 expand_T *xp;
5487 char_u *s;
5488{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005489 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005490#ifdef FEAT_MENU
5491 || ((xp->xp_context == EXPAND_MENUS
5492 || xp->xp_context == EXPAND_MENUNAMES)
5493 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5494#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005495 )
5496 {
5497#ifndef BACKSLASH_IN_FILENAME
5498 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5499 return 2;
5500#endif
5501 return 1;
5502 }
5503 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005504}
5505
5506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 * Show wildchar matches in the status line.
5508 * Show at least the "match" item.
5509 * We start at item 'first_match' in the list and show all matches that fit.
5510 *
5511 * If inversion is possible we use it. Else '=' characters are used.
5512 */
5513 void
5514win_redr_status_matches(xp, num_matches, matches, match, showtail)
5515 expand_T *xp;
5516 int num_matches;
5517 char_u **matches; /* list of matches */
5518 int match;
5519 int showtail;
5520{
5521#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5522 int row;
5523 char_u *buf;
5524 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005525 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 int fillchar;
5527 int attr;
5528 int i;
5529 int highlight = TRUE;
5530 char_u *selstart = NULL;
5531 int selstart_col = 0;
5532 char_u *selend = NULL;
5533 static int first_match = 0;
5534 int add_left = FALSE;
5535 char_u *s;
5536#ifdef FEAT_MENU
5537 int emenu;
5538#endif
5539#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5540 int l;
5541#endif
5542
5543 if (matches == NULL) /* interrupted completion? */
5544 return;
5545
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005546#ifdef FEAT_MBYTE
5547 if (has_mbyte)
5548 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5549 else
5550#endif
5551 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 if (buf == NULL)
5553 return;
5554
5555 if (match == -1) /* don't show match but original text */
5556 {
5557 match = 0;
5558 highlight = FALSE;
5559 }
5560 /* count 1 for the ending ">" */
5561 clen = status_match_len(xp, L_MATCH(match)) + 3;
5562 if (match == 0)
5563 first_match = 0;
5564 else if (match < first_match)
5565 {
5566 /* jumping left, as far as we can go */
5567 first_match = match;
5568 add_left = TRUE;
5569 }
5570 else
5571 {
5572 /* check if match fits on the screen */
5573 for (i = first_match; i < match; ++i)
5574 clen += status_match_len(xp, L_MATCH(i)) + 2;
5575 if (first_match > 0)
5576 clen += 2;
5577 /* jumping right, put match at the left */
5578 if ((long)clen > Columns)
5579 {
5580 first_match = match;
5581 /* if showing the last match, we can add some on the left */
5582 clen = 2;
5583 for (i = match; i < num_matches; ++i)
5584 {
5585 clen += status_match_len(xp, L_MATCH(i)) + 2;
5586 if ((long)clen >= Columns)
5587 break;
5588 }
5589 if (i == num_matches)
5590 add_left = TRUE;
5591 }
5592 }
5593 if (add_left)
5594 while (first_match > 0)
5595 {
5596 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5597 if ((long)clen >= Columns)
5598 break;
5599 --first_match;
5600 }
5601
5602 fillchar = fillchar_status(&attr, TRUE);
5603
5604 if (first_match == 0)
5605 {
5606 *buf = NUL;
5607 len = 0;
5608 }
5609 else
5610 {
5611 STRCPY(buf, "< ");
5612 len = 2;
5613 }
5614 clen = len;
5615
5616 i = first_match;
5617 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5618 {
5619 if (i == match)
5620 {
5621 selstart = buf + len;
5622 selstart_col = clen;
5623 }
5624
5625 s = L_MATCH(i);
5626 /* Check for menu separators - replace with '|' */
5627#ifdef FEAT_MENU
5628 emenu = (xp->xp_context == EXPAND_MENUS
5629 || xp->xp_context == EXPAND_MENUNAMES);
5630 if (emenu && menu_is_separator(s))
5631 {
5632 STRCPY(buf + len, transchar('|'));
5633 l = (int)STRLEN(buf + len);
5634 len += l;
5635 clen += l;
5636 }
5637 else
5638#endif
5639 for ( ; *s != NUL; ++s)
5640 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005641 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 clen += ptr2cells(s);
5643#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005644 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
5646 STRNCPY(buf + len, s, l);
5647 s += l - 1;
5648 len += l;
5649 }
5650 else
5651#endif
5652 {
5653 STRCPY(buf + len, transchar_byte(*s));
5654 len += (int)STRLEN(buf + len);
5655 }
5656 }
5657 if (i == match)
5658 selend = buf + len;
5659
5660 *(buf + len++) = ' ';
5661 *(buf + len++) = ' ';
5662 clen += 2;
5663 if (++i == num_matches)
5664 break;
5665 }
5666
5667 if (i != num_matches)
5668 {
5669 *(buf + len++) = '>';
5670 ++clen;
5671 }
5672
5673 buf[len] = NUL;
5674
5675 row = cmdline_row - 1;
5676 if (row >= 0)
5677 {
5678 if (wild_menu_showing == 0)
5679 {
5680 if (msg_scrolled > 0)
5681 {
5682 /* Put the wildmenu just above the command line. If there is
5683 * no room, scroll the screen one line up. */
5684 if (cmdline_row == Rows - 1)
5685 {
5686 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5687 ++msg_scrolled;
5688 }
5689 else
5690 {
5691 ++cmdline_row;
5692 ++row;
5693 }
5694 wild_menu_showing = WM_SCROLLED;
5695 }
5696 else
5697 {
5698 /* Create status line if needed by setting 'laststatus' to 2.
5699 * Set 'winminheight' to zero to avoid that the window is
5700 * resized. */
5701 if (lastwin->w_status_height == 0)
5702 {
5703 save_p_ls = p_ls;
5704 save_p_wmh = p_wmh;
5705 p_ls = 2;
5706 p_wmh = 0;
5707 last_status(FALSE);
5708 }
5709 wild_menu_showing = WM_SHOWN;
5710 }
5711 }
5712
5713 screen_puts(buf, row, 0, attr);
5714 if (selstart != NULL && highlight)
5715 {
5716 *selend = NUL;
5717 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5718 }
5719
5720 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5721 }
5722
5723#ifdef FEAT_VERTSPLIT
5724 win_redraw_last_status(topframe);
5725#else
5726 lastwin->w_redr_status = TRUE;
5727#endif
5728 vim_free(buf);
5729}
5730#endif
5731
5732#if defined(FEAT_WINDOWS) || defined(PROTO)
5733/*
5734 * Redraw the status line of window wp.
5735 *
5736 * If inversion is possible we use it. Else '=' characters are used.
5737 */
5738 void
5739win_redr_status(wp)
5740 win_T *wp;
5741{
5742 int row;
5743 char_u *p;
5744 int len;
5745 int fillchar;
5746 int attr;
5747 int this_ru_col;
5748
5749 wp->w_redr_status = FALSE;
5750 if (wp->w_status_height == 0)
5751 {
5752 /* no status line, can only be last window */
5753 redraw_cmdline = TRUE;
5754 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005755 else if (!redrawing()
5756#ifdef FEAT_INS_EXPAND
5757 /* don't update status line when popup menu is visible and may be
5758 * drawn over it */
5759 || pum_visible()
5760#endif
5761 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
5763 /* Don't redraw right now, do it later. */
5764 wp->w_redr_status = TRUE;
5765 }
5766#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005767 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {
5769 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005770 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 }
5772#endif
5773 else
5774 {
5775 fillchar = fillchar_status(&attr, wp == curwin);
5776
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005777 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 p = NameBuff;
5779 len = (int)STRLEN(p);
5780
5781 if (wp->w_buffer->b_help
5782#ifdef FEAT_QUICKFIX
5783 || wp->w_p_pvw
5784#endif
5785 || bufIsChanged(wp->w_buffer)
5786 || wp->w_buffer->b_p_ro)
5787 *(p + len++) = ' ';
5788 if (wp->w_buffer->b_help)
5789 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005790 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 len += (int)STRLEN(p + len);
5792 }
5793#ifdef FEAT_QUICKFIX
5794 if (wp->w_p_pvw)
5795 {
5796 STRCPY(p + len, _("[Preview]"));
5797 len += (int)STRLEN(p + len);
5798 }
5799#endif
5800 if (bufIsChanged(wp->w_buffer))
5801 {
5802 STRCPY(p + len, "[+]");
5803 len += 3;
5804 }
5805 if (wp->w_buffer->b_p_ro)
5806 {
5807 STRCPY(p + len, "[RO]");
5808 len += 4;
5809 }
5810
5811#ifndef FEAT_VERTSPLIT
5812 this_ru_col = ru_col;
5813 if (this_ru_col < (Columns + 1) / 2)
5814 this_ru_col = (Columns + 1) / 2;
5815#else
5816 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5817 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5818 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5819 if (this_ru_col <= 1)
5820 {
5821 p = (char_u *)"<"; /* No room for file name! */
5822 len = 1;
5823 }
5824 else
5825#endif
5826#ifdef FEAT_MBYTE
5827 if (has_mbyte)
5828 {
5829 int clen = 0, i;
5830
5831 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005832 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 clen += (*mb_ptr2cells)(p + i);
5834 /* Find first character that will fit.
5835 * Going from start to end is much faster for DBCS. */
5836 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005837 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 clen -= (*mb_ptr2cells)(p + i);
5839 len = clen;
5840 if (i > 0)
5841 {
5842 p = p + i - 1;
5843 *p = '<';
5844 ++len;
5845 }
5846
5847 }
5848 else
5849#endif
5850 if (len > this_ru_col - 1)
5851 {
5852 p += len - (this_ru_col - 1);
5853 *p = '<';
5854 len = this_ru_col - 1;
5855 }
5856
5857 row = W_WINROW(wp) + wp->w_height;
5858 screen_puts(p, row, W_WINCOL(wp), attr);
5859 screen_fill(row, row + 1, len + W_WINCOL(wp),
5860 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5861
5862 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5863 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5864 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5865 - 1 + W_WINCOL(wp)), attr);
5866
5867#ifdef FEAT_CMDL_INFO
5868 win_redr_ruler(wp, TRUE);
5869#endif
5870 }
5871
5872#ifdef FEAT_VERTSPLIT
5873 /*
5874 * May need to draw the character below the vertical separator.
5875 */
5876 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5877 {
5878 if (stl_connected(wp))
5879 fillchar = fillchar_status(&attr, wp == curwin);
5880 else
5881 fillchar = fillchar_vsep(&attr);
5882 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5883 attr);
5884 }
5885#endif
5886}
5887
Bram Moolenaar238a5642006-02-21 22:12:05 +00005888#ifdef FEAT_STL_OPT
5889/*
5890 * Redraw the status line according to 'statusline' and take care of any
5891 * errors encountered.
5892 */
5893 static void
5894redraw_custum_statusline(wp)
5895 win_T *wp;
5896{
5897 int save_called_emsg = called_emsg;
5898
5899 called_emsg = FALSE;
5900 win_redr_custom(wp, FALSE);
5901 if (called_emsg)
5902 set_string_option_direct((char_u *)"statusline", -1,
5903 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005904 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005905 called_emsg |= save_called_emsg;
5906}
5907#endif
5908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909# ifdef FEAT_VERTSPLIT
5910/*
5911 * Return TRUE if the status line of window "wp" is connected to the status
5912 * line of the window right of it. If not, then it's a vertical separator.
5913 * Only call if (wp->w_vsep_width != 0).
5914 */
5915 int
5916stl_connected(wp)
5917 win_T *wp;
5918{
5919 frame_T *fr;
5920
5921 fr = wp->w_frame;
5922 while (fr->fr_parent != NULL)
5923 {
5924 if (fr->fr_parent->fr_layout == FR_COL)
5925 {
5926 if (fr->fr_next != NULL)
5927 break;
5928 }
5929 else
5930 {
5931 if (fr->fr_next != NULL)
5932 return TRUE;
5933 }
5934 fr = fr->fr_parent;
5935 }
5936 return FALSE;
5937}
5938# endif
5939
5940#endif /* FEAT_WINDOWS */
5941
5942#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5943/*
5944 * Get the value to show for the language mappings, active 'keymap'.
5945 */
5946 int
5947get_keymap_str(wp, buf, len)
5948 win_T *wp;
5949 char_u *buf; /* buffer for the result */
5950 int len; /* length of buffer */
5951{
5952 char_u *p;
5953
5954 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5955 return FALSE;
5956
5957 {
5958#ifdef FEAT_EVAL
5959 buf_T *old_curbuf = curbuf;
5960 win_T *old_curwin = curwin;
5961 char_u *s;
5962
5963 curbuf = wp->w_buffer;
5964 curwin = wp;
5965 STRCPY(buf, "b:keymap_name"); /* must be writable */
5966 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005967 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 --emsg_skip;
5969 curbuf = old_curbuf;
5970 curwin = old_curwin;
5971 if (p == NULL || *p == NUL)
5972#endif
5973 {
5974#ifdef FEAT_KEYMAP
5975 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5976 p = wp->w_buffer->b_p_keymap;
5977 else
5978#endif
5979 p = (char_u *)"lang";
5980 }
5981 if ((int)(STRLEN(p) + 3) < len)
5982 sprintf((char *)buf, "<%s>", p);
5983 else
5984 buf[0] = NUL;
5985#ifdef FEAT_EVAL
5986 vim_free(s);
5987#endif
5988 }
5989 return buf[0] != NUL;
5990}
5991#endif
5992
5993#if defined(FEAT_STL_OPT) || defined(PROTO)
5994/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005995 * Redraw the status line or ruler of window "wp".
5996 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 */
5998 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005999win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006001 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 int attr;
6004 int curattr;
6005 int row;
6006 int col = 0;
6007 int maxwidth;
6008 int width;
6009 int n;
6010 int len;
6011 int fillchar;
6012 char_u buf[MAXPATHL];
6013 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006014 struct stl_hlrec hltab[STL_MAX_ITEM];
6015 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006016 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017
6018 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006019 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006021 /* Use 'tabline'. Always at the first line of the screen. */
6022 p = p_tal;
6023 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006024 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006025 attr = hl_attr(HLF_TPF);
6026 maxwidth = Columns;
6027# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006028 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006031 else
6032 {
6033 row = W_WINROW(wp) + wp->w_height;
6034 fillchar = fillchar_status(&attr, wp == curwin);
6035 maxwidth = W_WIDTH(wp);
6036
6037 if (draw_ruler)
6038 {
6039 p = p_ruf;
6040 /* advance past any leading group spec - implicit in ru_col */
6041 if (*p == '%')
6042 {
6043 if (*++p == '-')
6044 p++;
6045 if (atoi((char *) p))
6046 while (VIM_ISDIGIT(*p))
6047 p++;
6048 if (*p++ != '(')
6049 p = p_ruf;
6050 }
6051#ifdef FEAT_VERTSPLIT
6052 col = ru_col - (Columns - W_WIDTH(wp));
6053 if (col < (W_WIDTH(wp) + 1) / 2)
6054 col = (W_WIDTH(wp) + 1) / 2;
6055#else
6056 col = ru_col;
6057 if (col > (Columns + 1) / 2)
6058 col = (Columns + 1) / 2;
6059#endif
6060 maxwidth = W_WIDTH(wp) - col;
6061#ifdef FEAT_WINDOWS
6062 if (!wp->w_status_height)
6063#endif
6064 {
6065 row = Rows - 1;
6066 --maxwidth; /* writing in last column may cause scrolling */
6067 fillchar = ' ';
6068 attr = 0;
6069 }
6070
6071# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006072 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006073# endif
6074 }
6075 else
6076 {
6077 if (*wp->w_p_stl != NUL)
6078 p = wp->w_p_stl;
6079 else
6080 p = p_stl;
6081# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006082 use_sandbox = was_set_insecurely((char_u *)"statusline",
6083 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006084# endif
6085 }
6086
6087#ifdef FEAT_VERTSPLIT
6088 col += W_WINCOL(wp);
6089#endif
6090 }
6091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (maxwidth <= 0)
6093 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006095 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6096 buf, sizeof(buf),
6097 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006098 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006099 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 while (width < maxwidth && len < sizeof(buf) - 1)
6102 {
6103#ifdef FEAT_MBYTE
6104 len += (*mb_char2bytes)(fillchar, buf + len);
6105#else
6106 buf[len++] = fillchar;
6107#endif
6108 ++width;
6109 }
6110 buf[len] = NUL;
6111
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006112 /*
6113 * Draw each snippet with the specified highlighting.
6114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 curattr = attr;
6116 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006117 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006119 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 screen_puts_len(p, len, row, col, curattr);
6121 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006122 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006124 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006126 else if (hltab[n].userhl < 0)
6127 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006129 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006130 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131#endif
6132 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006133 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006136
6137 if (wp == NULL)
6138 {
6139 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6140 col = 0;
6141 len = 0;
6142 p = buf;
6143 fillchar = 0;
6144 for (n = 0; tabtab[n].start != NULL; n++)
6145 {
6146 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6147 while (col < len)
6148 TabPageIdxs[col++] = fillchar;
6149 p = tabtab[n].start;
6150 fillchar = tabtab[n].userhl;
6151 }
6152 while (col < Columns)
6153 TabPageIdxs[col++] = fillchar;
6154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155}
6156
6157#endif /* FEAT_STL_OPT */
6158
6159/*
6160 * Output a single character directly to the screen and update ScreenLines.
6161 */
6162 void
6163screen_putchar(c, row, col, attr)
6164 int c;
6165 int row, col;
6166 int attr;
6167{
6168#ifdef FEAT_MBYTE
6169 char_u buf[MB_MAXBYTES + 1];
6170
6171 buf[(*mb_char2bytes)(c, buf)] = NUL;
6172#else
6173 char_u buf[2];
6174
6175 buf[0] = c;
6176 buf[1] = NUL;
6177#endif
6178 screen_puts(buf, row, col, attr);
6179}
6180
6181/*
6182 * Get a single character directly from ScreenLines into "bytes[]".
6183 * Also return its attribute in *attrp;
6184 */
6185 void
6186screen_getbytes(row, col, bytes, attrp)
6187 int row, col;
6188 char_u *bytes;
6189 int *attrp;
6190{
6191 unsigned off;
6192
6193 /* safety check */
6194 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6195 {
6196 off = LineOffset[row] + col;
6197 *attrp = ScreenAttrs[off];
6198 bytes[0] = ScreenLines[off];
6199 bytes[1] = NUL;
6200
6201#ifdef FEAT_MBYTE
6202 if (enc_utf8 && ScreenLinesUC[off] != 0)
6203 bytes[utfc_char2bytes(off, bytes)] = NUL;
6204 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6205 {
6206 bytes[0] = ScreenLines[off];
6207 bytes[1] = ScreenLines2[off];
6208 bytes[2] = NUL;
6209 }
6210 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6211 {
6212 bytes[1] = ScreenLines[off + 1];
6213 bytes[2] = NUL;
6214 }
6215#endif
6216 }
6217}
6218
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006219#ifdef FEAT_MBYTE
6220static int screen_comp_differs __ARGS((int, int*));
6221
6222/*
6223 * Return TRUE if composing characters for screen posn "off" differs from
6224 * composing characters in "u8cc".
6225 */
6226 static int
6227screen_comp_differs(off, u8cc)
6228 int off;
6229 int *u8cc;
6230{
6231 int i;
6232
6233 for (i = 0; i < Screen_mco; ++i)
6234 {
6235 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6236 return TRUE;
6237 if (u8cc[i] == 0)
6238 break;
6239 }
6240 return FALSE;
6241}
6242#endif
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244/*
6245 * Put string '*text' on the screen at position 'row' and 'col', with
6246 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6247 * Note: only outputs within one row, message is truncated at screen boundary!
6248 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6249 */
6250 void
6251screen_puts(text, row, col, attr)
6252 char_u *text;
6253 int row;
6254 int col;
6255 int attr;
6256{
6257 screen_puts_len(text, -1, row, col, attr);
6258}
6259
6260/*
6261 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6262 * a NUL.
6263 */
6264 void
6265screen_puts_len(text, len, row, col, attr)
6266 char_u *text;
6267 int len;
6268 int row;
6269 int col;
6270 int attr;
6271{
6272 unsigned off;
6273 char_u *ptr = text;
6274 int c;
6275#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006276 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 int mbyte_blen = 1;
6278 int mbyte_cells = 1;
6279 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006280 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 int clear_next_cell = FALSE;
6282# ifdef FEAT_ARABIC
6283 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006284 int pc, nc, nc1;
6285 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286# endif
6287#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006288#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6289 int force_redraw_this;
6290 int force_redraw_next = FALSE;
6291#endif
6292 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
6294 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6295 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006296 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
Bram Moolenaarc236c162008-07-13 17:41:49 +00006298#ifdef FEAT_MBYTE
6299 /* When drawing over the right halve of a double-wide char clear out the
6300 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006301 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006302# ifdef FEAT_GUI
6303 && !gui.in_use
6304# endif
6305 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006306 {
6307 ScreenLines[off - 1] = ' ';
6308 ScreenAttrs[off - 1] = 0;
6309 if (enc_utf8)
6310 {
6311 ScreenLinesUC[off - 1] = 0;
6312 ScreenLinesC[0][off - 1] = 0;
6313 }
6314 /* redraw the previous cell, make it empty */
6315 screen_char(off - 1, row, col - 1);
6316 /* force the cell at "col" to be redrawn */
6317 force_redraw_next = TRUE;
6318 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006319#endif
6320
Bram Moolenaar367329b2007-08-30 11:53:22 +00006321#ifdef FEAT_MBYTE
6322 max_off = LineOffset[row] + screen_Columns;
6323#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006324 while (col < screen_Columns
6325 && (len < 0 || (int)(ptr - text) < len)
6326 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
6328 c = *ptr;
6329#ifdef FEAT_MBYTE
6330 /* check if this is the first byte of a multibyte */
6331 if (has_mbyte)
6332 {
6333 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006334 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006336 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6338 mbyte_cells = 1;
6339 else if (enc_dbcs != 0)
6340 mbyte_cells = mbyte_blen;
6341 else /* enc_utf8 */
6342 {
6343 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006344 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 (int)((text + len) - ptr));
6346 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006347 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006349# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 /* Non-BMP character: display as ? or fullwidth ?. */
6351 if (u8c >= 0x10000)
6352 {
6353 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6354 if (attr == 0)
6355 attr = hl_attr(HLF_8);
6356 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358# ifdef FEAT_ARABIC
6359 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6360 {
6361 /* Do Arabic shaping. */
6362 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6363 {
6364 /* Past end of string to be displayed. */
6365 nc = NUL;
6366 nc1 = NUL;
6367 }
6368 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006369 {
6370 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6371 nc1 = pcc[0];
6372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 pc = prev_c;
6374 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006375 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 }
6377 else
6378 prev_c = u8c;
6379# endif
6380 }
6381 }
6382#endif
6383
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006384#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6385 force_redraw_this = force_redraw_next;
6386 force_redraw_next = FALSE;
6387#endif
6388
6389 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390#ifdef FEAT_MBYTE
6391 || (mbyte_cells == 2
6392 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6393 || (enc_dbcs == DBCS_JPNU
6394 && c == 0x8e
6395 && ScreenLines2[off] != ptr[1])
6396 || (enc_utf8
Bram Moolenaarffcce302009-02-22 00:14:58 +00006397 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006398 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399#endif
6400 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006401 || exmode_active;
6402
6403 if (need_redraw
6404#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6405 || force_redraw_this
6406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 )
6408 {
6409#if defined(FEAT_GUI) || defined(UNIX)
6410 /* The bold trick makes a single row of pixels appear in the next
6411 * character. When a bold character is removed, the next
6412 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006413 * and for some xterms. */
6414 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415# ifdef FEAT_GUI
6416 gui.in_use
6417# endif
6418# if defined(FEAT_GUI) && defined(UNIX)
6419 ||
6420# endif
6421# ifdef UNIX
6422 term_is_xterm
6423# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006424 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006426 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006428 if (n > HL_ALL)
6429 n = syn_attr2attr(n);
6430 if (n & HL_BOLD)
6431 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
6433#endif
6434#ifdef FEAT_MBYTE
6435 /* When at the end of the text and overwriting a two-cell
6436 * character with a one-cell character, need to clear the next
6437 * cell. Also when overwriting the left halve of a two-cell char
6438 * with the right halve of a two-cell char. Do this only once
6439 * (mb_off2cells() may return 2 on the right halve). */
6440 if (clear_next_cell)
6441 clear_next_cell = FALSE;
6442 else if (has_mbyte
6443 && (len < 0 ? ptr[mbyte_blen] == NUL
6444 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006445 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006447 && (*mb_off2cells)(off, max_off) == 1
6448 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 clear_next_cell = TRUE;
6450
6451 /* Make sure we never leave a second byte of a double-byte behind,
6452 * it confuses mb_off2cells(). */
6453 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006454 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006456 && (*mb_off2cells)(off, max_off) == 1
6457 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 ScreenLines[off + mbyte_blen] = 0;
6459#endif
6460 ScreenLines[off] = c;
6461 ScreenAttrs[off] = attr;
6462#ifdef FEAT_MBYTE
6463 if (enc_utf8)
6464 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006465 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 ScreenLinesUC[off] = 0;
6467 else
6468 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006469 int i;
6470
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006472 for (i = 0; i < Screen_mco; ++i)
6473 {
6474 ScreenLinesC[i][off] = u8cc[i];
6475 if (u8cc[i] == 0)
6476 break;
6477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 if (mbyte_cells == 2)
6480 {
6481 ScreenLines[off + 1] = 0;
6482 ScreenAttrs[off + 1] = attr;
6483 }
6484 screen_char(off, row, col);
6485 }
6486 else if (mbyte_cells == 2)
6487 {
6488 ScreenLines[off + 1] = ptr[1];
6489 ScreenAttrs[off + 1] = attr;
6490 screen_char_2(off, row, col);
6491 }
6492 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6493 {
6494 ScreenLines2[off] = ptr[1];
6495 screen_char(off, row, col);
6496 }
6497 else
6498#endif
6499 screen_char(off, row, col);
6500 }
6501#ifdef FEAT_MBYTE
6502 if (has_mbyte)
6503 {
6504 off += mbyte_cells;
6505 col += mbyte_cells;
6506 ptr += mbyte_blen;
6507 if (clear_next_cell)
6508 ptr = (char_u *)" ";
6509 }
6510 else
6511#endif
6512 {
6513 ++off;
6514 ++col;
6515 ++ptr;
6516 }
6517 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006518
6519#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6520 /* If we detected the next character needs to be redrawn, but the text
6521 * doesn't extend up to there, update the character here. */
6522 if (force_redraw_next && col < screen_Columns)
6523 {
6524# ifdef FEAT_MBYTE
6525 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6526 screen_char_2(off, row, col);
6527 else
6528# endif
6529 screen_char(off, row, col);
6530 }
6531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532}
6533
6534#ifdef FEAT_SEARCH_EXTRA
6535/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006536 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 */
6538 static void
6539start_search_hl()
6540{
6541 if (p_hls && !no_hlsearch)
6542 {
6543 last_pat_prog(&search_hl.rm);
6544 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006545# ifdef FEAT_RELTIME
6546 /* Set the time limit to 'redrawtime'. */
6547 profile_setlimit(p_rdt, &search_hl.tm);
6548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
6550}
6551
6552/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006553 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 */
6555 static void
6556end_search_hl()
6557{
6558 if (search_hl.rm.regprog != NULL)
6559 {
6560 vim_free(search_hl.rm.regprog);
6561 search_hl.rm.regprog = NULL;
6562 }
6563}
6564
6565/*
6566 * Advance to the match in window "wp" line "lnum" or past it.
6567 */
6568 static void
6569prepare_search_hl(wp, lnum)
6570 win_T *wp;
6571 linenr_T lnum;
6572{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006573 matchitem_T *cur; /* points to the match list */
6574 match_T *shl; /* points to search_hl or a match */
6575 int shl_flag; /* flag to indicate whether search_hl
6576 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 int n;
6578
6579 /*
6580 * When using a multi-line pattern, start searching at the top
6581 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006582 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006584 cur = wp->w_match_head;
6585 shl_flag = FALSE;
6586 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006588 if (shl_flag == FALSE)
6589 {
6590 shl = &search_hl;
6591 shl_flag = TRUE;
6592 }
6593 else
6594 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 if (shl->rm.regprog != NULL
6596 && shl->lnum == 0
6597 && re_multiline(shl->rm.regprog))
6598 {
6599 if (shl->first_lnum == 0)
6600 {
6601# ifdef FEAT_FOLDING
6602 for (shl->first_lnum = lnum;
6603 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6604 if (hasFoldingWin(wp, shl->first_lnum - 1,
6605 NULL, NULL, TRUE, NULL))
6606 break;
6607# else
6608 shl->first_lnum = wp->w_topline;
6609# endif
6610 }
6611 n = 0;
6612 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6613 {
6614 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6615 if (shl->lnum != 0)
6616 {
6617 shl->first_lnum = shl->lnum
6618 + shl->rm.endpos[0].lnum
6619 - shl->rm.startpos[0].lnum;
6620 n = shl->rm.endpos[0].col;
6621 }
6622 else
6623 {
6624 ++shl->first_lnum;
6625 n = 0;
6626 }
6627 }
6628 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006629 if (shl != &search_hl && cur != NULL)
6630 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 }
6632}
6633
6634/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006635 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 * Uses shl->buf.
6637 * Sets shl->lnum and shl->rm contents.
6638 * Note: Assumes a previous match is always before "lnum", unless
6639 * shl->lnum is zero.
6640 * Careful: Any pointers for buffer lines will become invalid.
6641 */
6642 static void
6643next_search_hl(win, shl, lnum, mincol)
6644 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006645 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 linenr_T lnum;
6647 colnr_T mincol; /* minimal column for a match */
6648{
6649 linenr_T l;
6650 colnr_T matchcol;
6651 long nmatched;
6652
6653 if (shl->lnum != 0)
6654 {
6655 /* Check for three situations:
6656 * 1. If the "lnum" is below a previous match, start a new search.
6657 * 2. If the previous match includes "mincol", use it.
6658 * 3. Continue after the previous match.
6659 */
6660 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6661 if (lnum > l)
6662 shl->lnum = 0;
6663 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6664 return;
6665 }
6666
6667 /*
6668 * Repeat searching for a match until one is found that includes "mincol"
6669 * or none is found in this line.
6670 */
6671 called_emsg = FALSE;
6672 for (;;)
6673 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006674#ifdef FEAT_RELTIME
6675 /* Stop searching after passing the time limit. */
6676 if (profile_passed_limit(&(shl->tm)))
6677 {
6678 shl->lnum = 0; /* no match found in time */
6679 break;
6680 }
6681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 /* Three situations:
6683 * 1. No useful previous match: search from start of line.
6684 * 2. Not Vi compatible or empty match: continue at next character.
6685 * Break the loop if this is beyond the end of the line.
6686 * 3. Vi compatible searching: continue at end of previous match.
6687 */
6688 if (shl->lnum == 0)
6689 matchcol = 0;
6690 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6691 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006692 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006694 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006695
6696 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006697 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006698 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006700 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 shl->lnum = 0;
6702 break;
6703 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006704#ifdef FEAT_MBYTE
6705 if (has_mbyte)
6706 matchcol += mb_ptr2len(ml);
6707 else
6708#endif
6709 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 }
6711 else
6712 matchcol = shl->rm.endpos[0].col;
6713
6714 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006715 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6716#ifdef FEAT_RELTIME
6717 &(shl->tm)
6718#else
6719 NULL
6720#endif
6721 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (called_emsg)
6723 {
6724 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006725 if (shl == &search_hl)
6726 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006727 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006728 vim_free(shl->rm.regprog);
6729 no_hlsearch = TRUE;
6730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006732 shl->lnum = 0;
6733 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 break;
6735 }
6736 if (nmatched == 0)
6737 {
6738 shl->lnum = 0; /* no match found */
6739 break;
6740 }
6741 if (shl->rm.startpos[0].lnum > 0
6742 || shl->rm.startpos[0].col >= mincol
6743 || nmatched > 1
6744 || shl->rm.endpos[0].col > mincol)
6745 {
6746 shl->lnum += shl->rm.startpos[0].lnum;
6747 break; /* useful match found */
6748 }
6749 }
6750}
6751#endif
6752
6753 static void
6754screen_start_highlight(attr)
6755 int attr;
6756{
6757 attrentry_T *aep = NULL;
6758
6759 screen_attr = attr;
6760 if (full_screen
6761#ifdef WIN3264
6762 && termcap_active
6763#endif
6764 )
6765 {
6766#ifdef FEAT_GUI
6767 if (gui.in_use)
6768 {
6769 char buf[20];
6770
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006771 /* The GUI handles this internally. */
6772 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 OUT_STR(buf);
6774 }
6775 else
6776#endif
6777 {
6778 if (attr > HL_ALL) /* special HL attr. */
6779 {
6780 if (t_colors > 1)
6781 aep = syn_cterm_attr2entry(attr);
6782 else
6783 aep = syn_term_attr2entry(attr);
6784 if (aep == NULL) /* did ":syntax clear" */
6785 attr = 0;
6786 else
6787 attr = aep->ae_attr;
6788 }
6789 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6790 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006791 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6792 && cterm_normal_fg_bold)
6793 /* If the Normal FG color has BOLD attribute and the new HL
6794 * has a FG color defined, clear BOLD. */
6795 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6797 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006798 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6799 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 out_str(T_US);
6801 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6802 out_str(T_CZH);
6803 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6804 out_str(T_MR);
6805
6806 /*
6807 * Output the color or start string after bold etc., in case the
6808 * bold etc. override the color setting.
6809 */
6810 if (aep != NULL)
6811 {
6812 if (t_colors > 1)
6813 {
6814 if (aep->ae_u.cterm.fg_color)
6815 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6816 if (aep->ae_u.cterm.bg_color)
6817 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6818 }
6819 else
6820 {
6821 if (aep->ae_u.term.start != NULL)
6822 out_str(aep->ae_u.term.start);
6823 }
6824 }
6825 }
6826 }
6827}
6828
6829 void
6830screen_stop_highlight()
6831{
6832 int do_ME = FALSE; /* output T_ME code */
6833
6834 if (screen_attr != 0
6835#ifdef WIN3264
6836 && termcap_active
6837#endif
6838 )
6839 {
6840#ifdef FEAT_GUI
6841 if (gui.in_use)
6842 {
6843 char buf[20];
6844
6845 /* use internal GUI code */
6846 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6847 OUT_STR(buf);
6848 }
6849 else
6850#endif
6851 {
6852 if (screen_attr > HL_ALL) /* special HL attr. */
6853 {
6854 attrentry_T *aep;
6855
6856 if (t_colors > 1)
6857 {
6858 /*
6859 * Assume that t_me restores the original colors!
6860 */
6861 aep = syn_cterm_attr2entry(screen_attr);
6862 if (aep != NULL && (aep->ae_u.cterm.fg_color
6863 || aep->ae_u.cterm.bg_color))
6864 do_ME = TRUE;
6865 }
6866 else
6867 {
6868 aep = syn_term_attr2entry(screen_attr);
6869 if (aep != NULL && aep->ae_u.term.stop != NULL)
6870 {
6871 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6872 do_ME = TRUE;
6873 else
6874 out_str(aep->ae_u.term.stop);
6875 }
6876 }
6877 if (aep == NULL) /* did ":syntax clear" */
6878 screen_attr = 0;
6879 else
6880 screen_attr = aep->ae_attr;
6881 }
6882
6883 /*
6884 * Often all ending-codes are equal to T_ME. Avoid outputting the
6885 * same sequence several times.
6886 */
6887 if (screen_attr & HL_STANDOUT)
6888 {
6889 if (STRCMP(T_SE, T_ME) == 0)
6890 do_ME = TRUE;
6891 else
6892 out_str(T_SE);
6893 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006894 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
6896 if (STRCMP(T_UE, T_ME) == 0)
6897 do_ME = TRUE;
6898 else
6899 out_str(T_UE);
6900 }
6901 if (screen_attr & HL_ITALIC)
6902 {
6903 if (STRCMP(T_CZR, T_ME) == 0)
6904 do_ME = TRUE;
6905 else
6906 out_str(T_CZR);
6907 }
6908 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6909 out_str(T_ME);
6910
6911 if (t_colors > 1)
6912 {
6913 /* set Normal cterm colors */
6914 if (cterm_normal_fg_color != 0)
6915 term_fg_color(cterm_normal_fg_color - 1);
6916 if (cterm_normal_bg_color != 0)
6917 term_bg_color(cterm_normal_bg_color - 1);
6918 if (cterm_normal_fg_bold)
6919 out_str(T_MD);
6920 }
6921 }
6922 }
6923 screen_attr = 0;
6924}
6925
6926/*
6927 * Reset the colors for a cterm. Used when leaving Vim.
6928 * The machine specific code may override this again.
6929 */
6930 void
6931reset_cterm_colors()
6932{
6933 if (t_colors > 1)
6934 {
6935 /* set Normal cterm colors */
6936 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6937 {
6938 out_str(T_OP);
6939 screen_attr = -1;
6940 }
6941 if (cterm_normal_fg_bold)
6942 {
6943 out_str(T_ME);
6944 screen_attr = -1;
6945 }
6946 }
6947}
6948
6949/*
6950 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6951 * using the attributes from ScreenAttrs["off"].
6952 */
6953 static void
6954screen_char(off, row, col)
6955 unsigned off;
6956 int row;
6957 int col;
6958{
6959 int attr;
6960
6961 /* Check for illegal values, just in case (could happen just after
6962 * resizing). */
6963 if (row >= screen_Rows || col >= screen_Columns)
6964 return;
6965
6966 /* Outputting the last character on the screen may scrollup the screen.
6967 * Don't to it! Mark the character invalid (update it when scrolled up) */
6968 if (row == screen_Rows - 1 && col == screen_Columns - 1
6969#ifdef FEAT_RIGHTLEFT
6970 /* account for first command-line character in rightleft mode */
6971 && !cmdmsg_rl
6972#endif
6973 )
6974 {
6975 ScreenAttrs[off] = (sattr_T)-1;
6976 return;
6977 }
6978
6979 /*
6980 * Stop highlighting first, so it's easier to move the cursor.
6981 */
6982#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6983 if (screen_char_attr != 0)
6984 attr = screen_char_attr;
6985 else
6986#endif
6987 attr = ScreenAttrs[off];
6988 if (screen_attr != attr)
6989 screen_stop_highlight();
6990
6991 windgoto(row, col);
6992
6993 if (screen_attr != attr)
6994 screen_start_highlight(attr);
6995
6996#ifdef FEAT_MBYTE
6997 if (enc_utf8 && ScreenLinesUC[off] != 0)
6998 {
6999 char_u buf[MB_MAXBYTES + 1];
7000
7001 /* Convert UTF-8 character to bytes and write it. */
7002
7003 buf[utfc_char2bytes(off, buf)] = NUL;
7004
7005 out_str(buf);
7006 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7007 ++screen_cur_col;
7008 }
7009 else
7010#endif
7011 {
7012#ifdef FEAT_MBYTE
7013 out_flush_check();
7014#endif
7015 out_char(ScreenLines[off]);
7016#ifdef FEAT_MBYTE
7017 /* double-byte character in single-width cell */
7018 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7019 out_char(ScreenLines2[off]);
7020#endif
7021 }
7022
7023 screen_cur_col++;
7024}
7025
7026#ifdef FEAT_MBYTE
7027
7028/*
7029 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7030 * on the screen at position 'row' and 'col'.
7031 * The attributes of the first byte is used for all. This is required to
7032 * output the two bytes of a double-byte character with nothing in between.
7033 */
7034 static void
7035screen_char_2(off, row, col)
7036 unsigned off;
7037 int row;
7038 int col;
7039{
7040 /* Check for illegal values (could be wrong when screen was resized). */
7041 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7042 return;
7043
7044 /* Outputting the last character on the screen may scrollup the screen.
7045 * Don't to it! Mark the character invalid (update it when scrolled up) */
7046 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7047 {
7048 ScreenAttrs[off] = (sattr_T)-1;
7049 return;
7050 }
7051
7052 /* Output the first byte normally (positions the cursor), then write the
7053 * second byte directly. */
7054 screen_char(off, row, col);
7055 out_char(ScreenLines[off + 1]);
7056 ++screen_cur_col;
7057}
7058#endif
7059
7060#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7061/*
7062 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7063 * This uses the contents of ScreenLines[] and doesn't change it.
7064 */
7065 void
7066screen_draw_rectangle(row, col, height, width, invert)
7067 int row;
7068 int col;
7069 int height;
7070 int width;
7071 int invert;
7072{
7073 int r, c;
7074 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007075#ifdef FEAT_MBYTE
7076 int max_off;
7077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007079 /* Can't use ScreenLines unless initialized */
7080 if (ScreenLines == NULL)
7081 return;
7082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 if (invert)
7084 screen_char_attr = HL_INVERSE;
7085 for (r = row; r < row + height; ++r)
7086 {
7087 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007088#ifdef FEAT_MBYTE
7089 max_off = off + screen_Columns;
7090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 for (c = col; c < col + width; ++c)
7092 {
7093#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007094 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
7096 screen_char_2(off + c, r, c);
7097 ++c;
7098 }
7099 else
7100#endif
7101 {
7102 screen_char(off + c, r, c);
7103#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007104 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 ++c;
7106#endif
7107 }
7108 }
7109 }
7110 screen_char_attr = 0;
7111}
7112#endif
7113
7114#ifdef FEAT_VERTSPLIT
7115/*
7116 * Redraw the characters for a vertically split window.
7117 */
7118 static void
7119redraw_block(row, end, wp)
7120 int row;
7121 int end;
7122 win_T *wp;
7123{
7124 int col;
7125 int width;
7126
7127# ifdef FEAT_CLIPBOARD
7128 clip_may_clear_selection(row, end - 1);
7129# endif
7130
7131 if (wp == NULL)
7132 {
7133 col = 0;
7134 width = Columns;
7135 }
7136 else
7137 {
7138 col = wp->w_wincol;
7139 width = wp->w_width;
7140 }
7141 screen_draw_rectangle(row, col, end - row, width, FALSE);
7142}
7143#endif
7144
7145/*
7146 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7147 * with character 'c1' in first column followed by 'c2' in the other columns.
7148 * Use attributes 'attr'.
7149 */
7150 void
7151screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7152 int start_row, end_row;
7153 int start_col, end_col;
7154 int c1, c2;
7155 int attr;
7156{
7157 int row;
7158 int col;
7159 int off;
7160 int end_off;
7161 int did_delete;
7162 int c;
7163 int norm_term;
7164#if defined(FEAT_GUI) || defined(UNIX)
7165 int force_next = FALSE;
7166#endif
7167
7168 if (end_row > screen_Rows) /* safety check */
7169 end_row = screen_Rows;
7170 if (end_col > screen_Columns) /* safety check */
7171 end_col = screen_Columns;
7172 if (ScreenLines == NULL
7173 || start_row >= end_row
7174 || start_col >= end_col) /* nothing to do */
7175 return;
7176
7177 /* it's a "normal" terminal when not in a GUI or cterm */
7178 norm_term = (
7179#ifdef FEAT_GUI
7180 !gui.in_use &&
7181#endif
7182 t_colors <= 1);
7183 for (row = start_row; row < end_row; ++row)
7184 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007185#ifdef FEAT_MBYTE
7186 if (has_mbyte
7187# ifdef FEAT_GUI
7188 && !gui.in_use
7189# endif
7190 )
7191 {
7192 /* When drawing over the right halve of a double-wide char clear
7193 * out the left halve. When drawing over the left halve of a
7194 * double wide-char clear out the right halve. Only needed in a
7195 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007196 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007197 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007198 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007199 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007200 }
7201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 /*
7203 * Try to use delete-line termcap code, when no attributes or in a
7204 * "normal" terminal, where a bold/italic space is just a
7205 * space.
7206 */
7207 did_delete = FALSE;
7208 if (c2 == ' '
7209 && end_col == Columns
7210 && can_clear(T_CE)
7211 && (attr == 0
7212 || (norm_term
7213 && attr <= HL_ALL
7214 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7215 {
7216 /*
7217 * check if we really need to clear something
7218 */
7219 col = start_col;
7220 if (c1 != ' ') /* don't clear first char */
7221 ++col;
7222
7223 off = LineOffset[row] + col;
7224 end_off = LineOffset[row] + end_col;
7225
7226 /* skip blanks (used often, keep it fast!) */
7227#ifdef FEAT_MBYTE
7228 if (enc_utf8)
7229 while (off < end_off && ScreenLines[off] == ' '
7230 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7231 ++off;
7232 else
7233#endif
7234 while (off < end_off && ScreenLines[off] == ' '
7235 && ScreenAttrs[off] == 0)
7236 ++off;
7237 if (off < end_off) /* something to be cleared */
7238 {
7239 col = off - LineOffset[row];
7240 screen_stop_highlight();
7241 term_windgoto(row, col);/* clear rest of this screen line */
7242 out_str(T_CE);
7243 screen_start(); /* don't know where cursor is now */
7244 col = end_col - col;
7245 while (col--) /* clear chars in ScreenLines */
7246 {
7247 ScreenLines[off] = ' ';
7248#ifdef FEAT_MBYTE
7249 if (enc_utf8)
7250 ScreenLinesUC[off] = 0;
7251#endif
7252 ScreenAttrs[off] = 0;
7253 ++off;
7254 }
7255 }
7256 did_delete = TRUE; /* the chars are cleared now */
7257 }
7258
7259 off = LineOffset[row] + start_col;
7260 c = c1;
7261 for (col = start_col; col < end_col; ++col)
7262 {
7263 if (ScreenLines[off] != c
7264#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007265 || (enc_utf8 && (int)ScreenLinesUC[off]
7266 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267#endif
7268 || ScreenAttrs[off] != attr
7269#if defined(FEAT_GUI) || defined(UNIX)
7270 || force_next
7271#endif
7272 )
7273 {
7274#if defined(FEAT_GUI) || defined(UNIX)
7275 /* The bold trick may make a single row of pixels appear in
7276 * the next character. When a bold character is removed, the
7277 * next character should be redrawn too. This happens for our
7278 * own GUI and for some xterms. */
7279 if (
7280# ifdef FEAT_GUI
7281 gui.in_use
7282# endif
7283# if defined(FEAT_GUI) && defined(UNIX)
7284 ||
7285# endif
7286# ifdef UNIX
7287 term_is_xterm
7288# endif
7289 )
7290 {
7291 if (ScreenLines[off] != ' '
7292 && (ScreenAttrs[off] > HL_ALL
7293 || ScreenAttrs[off] & HL_BOLD))
7294 force_next = TRUE;
7295 else
7296 force_next = FALSE;
7297 }
7298#endif
7299 ScreenLines[off] = c;
7300#ifdef FEAT_MBYTE
7301 if (enc_utf8)
7302 {
7303 if (c >= 0x80)
7304 {
7305 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007306 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 }
7308 else
7309 ScreenLinesUC[off] = 0;
7310 }
7311#endif
7312 ScreenAttrs[off] = attr;
7313 if (!did_delete || c != ' ')
7314 screen_char(off, row, col);
7315 }
7316 ++off;
7317 if (col == start_col)
7318 {
7319 if (did_delete)
7320 break;
7321 c = c2;
7322 }
7323 }
7324 if (end_col == Columns)
7325 LineWraps[row] = FALSE;
7326 if (row == Rows - 1) /* overwritten the command line */
7327 {
7328 redraw_cmdline = TRUE;
7329 if (c1 == ' ' && c2 == ' ')
7330 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007331 if (start_col == 0)
7332 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 }
7334 }
7335}
7336
7337/*
7338 * Check if there should be a delay. Used before clearing or redrawing the
7339 * screen or the command line.
7340 */
7341 void
7342check_for_delay(check_msg_scroll)
7343 int check_msg_scroll;
7344{
7345 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7346 && !did_wait_return
7347 && emsg_silent == 0)
7348 {
7349 out_flush();
7350 ui_delay(1000L, TRUE);
7351 emsg_on_display = FALSE;
7352 if (check_msg_scroll)
7353 msg_scroll = FALSE;
7354 }
7355}
7356
7357/*
7358 * screen_valid - allocate screen buffers if size changed
7359 * If "clear" is TRUE: clear screen if it has been resized.
7360 * Returns TRUE if there is a valid screen to write to.
7361 * Returns FALSE when starting up and screen not initialized yet.
7362 */
7363 int
7364screen_valid(clear)
7365 int clear;
7366{
7367 screenalloc(clear); /* allocate screen buffers if size changed */
7368 return (ScreenLines != NULL);
7369}
7370
7371/*
7372 * Resize the shell to Rows and Columns.
7373 * Allocate ScreenLines[] and associated items.
7374 *
7375 * There may be some time between setting Rows and Columns and (re)allocating
7376 * ScreenLines[]. This happens when starting up and when (manually) changing
7377 * the shell size. Always use screen_Rows and screen_Columns to access items
7378 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7379 * final size of the shell is needed.
7380 */
7381 void
7382screenalloc(clear)
7383 int clear;
7384{
7385 int new_row, old_row;
7386#ifdef FEAT_GUI
7387 int old_Rows;
7388#endif
7389 win_T *wp;
7390 int outofmem = FALSE;
7391 int len;
7392 schar_T *new_ScreenLines;
7393#ifdef FEAT_MBYTE
7394 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007395 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007397 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398#endif
7399 sattr_T *new_ScreenAttrs;
7400 unsigned *new_LineOffset;
7401 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007402#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007403 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007404 tabpage_T *tp;
7405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007407 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007408#ifdef FEAT_AUTOCMD
7409 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007411retry:
7412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 /*
7414 * Allocation of the screen buffers is done only when the size changes and
7415 * when Rows and Columns have been set and we have started doing full
7416 * screen stuff.
7417 */
7418 if ((ScreenLines != NULL
7419 && Rows == screen_Rows
7420 && Columns == screen_Columns
7421#ifdef FEAT_MBYTE
7422 && enc_utf8 == (ScreenLinesUC != NULL)
7423 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007424 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425#endif
7426 )
7427 || Rows == 0
7428 || Columns == 0
7429 || (!full_screen && ScreenLines == NULL))
7430 return;
7431
7432 /*
7433 * It's possible that we produce an out-of-memory message below, which
7434 * will cause this function to be called again. To break the loop, just
7435 * return here.
7436 */
7437 if (entered)
7438 return;
7439 entered = TRUE;
7440
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007441 /*
7442 * Note that the window sizes are updated before reallocating the arrays,
7443 * thus we must not redraw here!
7444 */
7445 ++RedrawingDisabled;
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 win_new_shellsize(); /* fit the windows in the new sized shell */
7448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 comp_col(); /* recompute columns for shown command and ruler */
7450
7451 /*
7452 * We're changing the size of the screen.
7453 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7454 * - Move lines from the old arrays into the new arrays, clear extra
7455 * lines (unless the screen is going to be cleared).
7456 * - Free the old arrays.
7457 *
7458 * If anything fails, make ScreenLines NULL, so we don't do anything!
7459 * Continuing with the old ScreenLines may result in a crash, because the
7460 * size is wrong.
7461 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007462 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465 new_ScreenLines = (schar_T *)lalloc((long_u)(
7466 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7467#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007468 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 if (enc_utf8)
7470 {
7471 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7472 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007473 for (i = 0; i < p_mco; ++i)
7474 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7476 }
7477 if (enc_dbcs == DBCS_JPNU)
7478 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7479 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7480#endif
7481 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7482 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7483 new_LineOffset = (unsigned *)lalloc((long_u)(
7484 Rows * sizeof(unsigned)), FALSE);
7485 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007486#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007487 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007490 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {
7492 if (win_alloc_lines(wp) == FAIL)
7493 {
7494 outofmem = TRUE;
7495#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007496 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497#endif
7498 }
7499 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007500#ifdef FEAT_WINDOWS
7501give_up:
7502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007504#ifdef FEAT_MBYTE
7505 for (i = 0; i < p_mco; ++i)
7506 if (new_ScreenLinesC[i] == NULL)
7507 break;
7508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 if (new_ScreenLines == NULL
7510#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007511 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7513#endif
7514 || new_ScreenAttrs == NULL
7515 || new_LineOffset == NULL
7516 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007517#ifdef FEAT_WINDOWS
7518 || new_TabPageIdxs == NULL
7519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 || outofmem)
7521 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007522 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007523 {
7524 /* guess the size */
7525 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7526
7527 /* Remember we did this to avoid getting outofmem messages over
7528 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007529 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 vim_free(new_ScreenLines);
7532 new_ScreenLines = NULL;
7533#ifdef FEAT_MBYTE
7534 vim_free(new_ScreenLinesUC);
7535 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007536 for (i = 0; i < p_mco; ++i)
7537 {
7538 vim_free(new_ScreenLinesC[i]);
7539 new_ScreenLinesC[i] = NULL;
7540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 vim_free(new_ScreenLines2);
7542 new_ScreenLines2 = NULL;
7543#endif
7544 vim_free(new_ScreenAttrs);
7545 new_ScreenAttrs = NULL;
7546 vim_free(new_LineOffset);
7547 new_LineOffset = NULL;
7548 vim_free(new_LineWraps);
7549 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007550#ifdef FEAT_WINDOWS
7551 vim_free(new_TabPageIdxs);
7552 new_TabPageIdxs = NULL;
7553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
7555 else
7556 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007557 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007558
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 for (new_row = 0; new_row < Rows; ++new_row)
7560 {
7561 new_LineOffset[new_row] = new_row * Columns;
7562 new_LineWraps[new_row] = FALSE;
7563
7564 /*
7565 * If the screen is not going to be cleared, copy as much as
7566 * possible from the old screen to the new one and clear the rest
7567 * (used when resizing the window at the "--more--" prompt or when
7568 * executing an external command, for the GUI).
7569 */
7570 if (!clear)
7571 {
7572 (void)vim_memset(new_ScreenLines + new_row * Columns,
7573 ' ', (size_t)Columns * sizeof(schar_T));
7574#ifdef FEAT_MBYTE
7575 if (enc_utf8)
7576 {
7577 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7578 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007579 for (i = 0; i < p_mco; ++i)
7580 (void)vim_memset(new_ScreenLinesC[i]
7581 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 0, (size_t)Columns * sizeof(u8char_T));
7583 }
7584 if (enc_dbcs == DBCS_JPNU)
7585 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7586 0, (size_t)Columns * sizeof(schar_T));
7587#endif
7588 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7589 0, (size_t)Columns * sizeof(sattr_T));
7590 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007591 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 {
7593 if (screen_Columns < Columns)
7594 len = screen_Columns;
7595 else
7596 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007597#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007598 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007599 * may be invalid now. Also when p_mco changes. */
7600 if (!(enc_utf8 && ScreenLinesUC == NULL)
7601 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007602#endif
7603 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7604 ScreenLines + LineOffset[old_row],
7605 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007607 if (enc_utf8 && ScreenLinesUC != NULL
7608 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {
7610 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7611 ScreenLinesUC + LineOffset[old_row],
7612 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007613 for (i = 0; i < p_mco; ++i)
7614 mch_memmove(new_ScreenLinesC[i]
7615 + new_LineOffset[new_row],
7616 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 (size_t)len * sizeof(u8char_T));
7618 }
7619 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7620 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7621 ScreenLines2 + LineOffset[old_row],
7622 (size_t)len * sizeof(schar_T));
7623#endif
7624 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7625 ScreenAttrs + LineOffset[old_row],
7626 (size_t)len * sizeof(sattr_T));
7627 }
7628 }
7629 }
7630 /* Use the last line of the screen for the current line. */
7631 current_ScreenLine = new_ScreenLines + Rows * Columns;
7632 }
7633
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007634 free_screenlines();
7635
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 ScreenLines = new_ScreenLines;
7637#ifdef FEAT_MBYTE
7638 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007639 for (i = 0; i < p_mco; ++i)
7640 ScreenLinesC[i] = new_ScreenLinesC[i];
7641 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 ScreenLines2 = new_ScreenLines2;
7643#endif
7644 ScreenAttrs = new_ScreenAttrs;
7645 LineOffset = new_LineOffset;
7646 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007647#ifdef FEAT_WINDOWS
7648 TabPageIdxs = new_TabPageIdxs;
7649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
7651 /* It's important that screen_Rows and screen_Columns reflect the actual
7652 * size of ScreenLines[]. Set them before calling anything. */
7653#ifdef FEAT_GUI
7654 old_Rows = screen_Rows;
7655#endif
7656 screen_Rows = Rows;
7657 screen_Columns = Columns;
7658
7659 must_redraw = CLEAR; /* need to clear the screen later */
7660 if (clear)
7661 screenclear2();
7662
7663#ifdef FEAT_GUI
7664 else if (gui.in_use
7665 && !gui.starting
7666 && ScreenLines != NULL
7667 && old_Rows != Rows)
7668 {
7669 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7670 /*
7671 * Adjust the position of the cursor, for when executing an external
7672 * command.
7673 */
7674 if (msg_row >= Rows) /* Rows got smaller */
7675 msg_row = Rows - 1; /* put cursor at last row */
7676 else if (Rows > old_Rows) /* Rows got bigger */
7677 msg_row += Rows - old_Rows; /* put cursor in same place */
7678 if (msg_col >= Columns) /* Columns got smaller */
7679 msg_col = Columns - 1; /* put cursor at last column */
7680 }
7681#endif
7682
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007684 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007685
7686#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007687 /*
7688 * Do not apply autocommands more than 3 times to avoid an endless loop
7689 * in case applying autocommands always changes Rows or Columns.
7690 */
7691 if (starting == 0 && ++retry_count <= 3)
7692 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007693 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007694 /* In rare cases, autocommands may have altered Rows or Columns,
7695 * jump back to check if we need to allocate the screen again. */
7696 goto retry;
7697 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699}
7700
7701 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007702free_screenlines()
7703{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007704#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007705 int i;
7706
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007707 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007708 for (i = 0; i < Screen_mco; ++i)
7709 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007710 vim_free(ScreenLines2);
7711#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007712 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007713 vim_free(ScreenAttrs);
7714 vim_free(LineOffset);
7715 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007716#ifdef FEAT_WINDOWS
7717 vim_free(TabPageIdxs);
7718#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007719}
7720
7721 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722screenclear()
7723{
7724 check_for_delay(FALSE);
7725 screenalloc(FALSE); /* allocate screen buffers if size changed */
7726 screenclear2(); /* clear the screen */
7727}
7728
7729 static void
7730screenclear2()
7731{
7732 int i;
7733
7734 if (starting == NO_SCREEN || ScreenLines == NULL
7735#ifdef FEAT_GUI
7736 || (gui.in_use && gui.starting)
7737#endif
7738 )
7739 return;
7740
7741#ifdef FEAT_GUI
7742 if (!gui.in_use)
7743#endif
7744 screen_attr = -1; /* force setting the Normal colors */
7745 screen_stop_highlight(); /* don't want highlighting here */
7746
7747#ifdef FEAT_CLIPBOARD
7748 /* disable selection without redrawing it */
7749 clip_scroll_selection(9999);
7750#endif
7751
7752 /* blank out ScreenLines */
7753 for (i = 0; i < Rows; ++i)
7754 {
7755 lineclear(LineOffset[i], (int)Columns);
7756 LineWraps[i] = FALSE;
7757 }
7758
7759 if (can_clear(T_CL))
7760 {
7761 out_str(T_CL); /* clear the display */
7762 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007763 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 }
7765 else
7766 {
7767 /* can't clear the screen, mark all chars with invalid attributes */
7768 for (i = 0; i < Rows; ++i)
7769 lineinvalid(LineOffset[i], (int)Columns);
7770 clear_cmdline = TRUE;
7771 }
7772
7773 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7774
7775 win_rest_invalid(firstwin);
7776 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007777#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007778 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 if (must_redraw == CLEAR) /* no need to clear again */
7781 must_redraw = NOT_VALID;
7782 compute_cmdrow();
7783 msg_row = cmdline_row; /* put cursor on last line for messages */
7784 msg_col = 0;
7785 screen_start(); /* don't know where cursor is now */
7786 msg_scrolled = 0; /* can't scroll back */
7787 msg_didany = FALSE;
7788 msg_didout = FALSE;
7789}
7790
7791/*
7792 * Clear one line in ScreenLines.
7793 */
7794 static void
7795lineclear(off, width)
7796 unsigned off;
7797 int width;
7798{
7799 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7800#ifdef FEAT_MBYTE
7801 if (enc_utf8)
7802 (void)vim_memset(ScreenLinesUC + off, 0,
7803 (size_t)width * sizeof(u8char_T));
7804#endif
7805 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7806}
7807
7808/*
7809 * Mark one line in ScreenLines invalid by setting the attributes to an
7810 * invalid value.
7811 */
7812 static void
7813lineinvalid(off, width)
7814 unsigned off;
7815 int width;
7816{
7817 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7818}
7819
7820#ifdef FEAT_VERTSPLIT
7821/*
7822 * Copy part of a Screenline for vertically split window "wp".
7823 */
7824 static void
7825linecopy(to, from, wp)
7826 int to;
7827 int from;
7828 win_T *wp;
7829{
7830 unsigned off_to = LineOffset[to] + wp->w_wincol;
7831 unsigned off_from = LineOffset[from] + wp->w_wincol;
7832
7833 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7834 wp->w_width * sizeof(schar_T));
7835# ifdef FEAT_MBYTE
7836 if (enc_utf8)
7837 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007838 int i;
7839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7841 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007842 for (i = 0; i < p_mco; ++i)
7843 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7844 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
7846 if (enc_dbcs == DBCS_JPNU)
7847 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7848 wp->w_width * sizeof(schar_T));
7849# endif
7850 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7851 wp->w_width * sizeof(sattr_T));
7852}
7853#endif
7854
7855/*
7856 * Return TRUE if clearing with term string "p" would work.
7857 * It can't work when the string is empty or it won't set the right background.
7858 */
7859 int
7860can_clear(p)
7861 char_u *p;
7862{
7863 return (*p != NUL && (t_colors <= 1
7864#ifdef FEAT_GUI
7865 || gui.in_use
7866#endif
7867 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7868}
7869
7870/*
7871 * Reset cursor position. Use whenever cursor was moved because of outputting
7872 * something directly to the screen (shell commands) or a terminal control
7873 * code.
7874 */
7875 void
7876screen_start()
7877{
7878 screen_cur_row = screen_cur_col = 9999;
7879}
7880
7881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 * Move the cursor to position "row","col" in the screen.
7883 * This tries to find the most efficient way to move, minimizing the number of
7884 * characters sent to the terminal.
7885 */
7886 void
7887windgoto(row, col)
7888 int row;
7889 int col;
7890{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007891 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 int i;
7893 int plan;
7894 int cost;
7895 int wouldbe_col;
7896 int noinvcurs;
7897 char_u *bs;
7898 int goto_cost;
7899 int attr;
7900
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007901#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7903
7904#define PLAN_LE 1
7905#define PLAN_CR 2
7906#define PLAN_NL 3
7907#define PLAN_WRITE 4
7908 /* Can't use ScreenLines unless initialized */
7909 if (ScreenLines == NULL)
7910 return;
7911
7912 if (col != screen_cur_col || row != screen_cur_row)
7913 {
7914 /* Check for valid position. */
7915 if (row < 0) /* window without text lines? */
7916 row = 0;
7917 if (row >= screen_Rows)
7918 row = screen_Rows - 1;
7919 if (col >= screen_Columns)
7920 col = screen_Columns - 1;
7921
7922 /* check if no cursor movement is allowed in highlight mode */
7923 if (screen_attr && *T_MS == NUL)
7924 noinvcurs = HIGHL_COST;
7925 else
7926 noinvcurs = 0;
7927 goto_cost = GOTO_COST + noinvcurs;
7928
7929 /*
7930 * Plan how to do the positioning:
7931 * 1. Use CR to move it to column 0, same row.
7932 * 2. Use T_LE to move it a few columns to the left.
7933 * 3. Use NL to move a few lines down, column 0.
7934 * 4. Move a few columns to the right with T_ND or by writing chars.
7935 *
7936 * Don't do this if the cursor went beyond the last column, the cursor
7937 * position is unknown then (some terminals wrap, some don't )
7938 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007939 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 * characters to move the cursor to the right.
7941 */
7942 if (row >= screen_cur_row && screen_cur_col < Columns)
7943 {
7944 /*
7945 * If the cursor is in the same row, bigger col, we can use CR
7946 * or T_LE.
7947 */
7948 bs = NULL; /* init for GCC */
7949 attr = screen_attr;
7950 if (row == screen_cur_row && col < screen_cur_col)
7951 {
7952 /* "le" is preferred over "bc", because "bc" is obsolete */
7953 if (*T_LE)
7954 bs = T_LE; /* "cursor left" */
7955 else
7956 bs = T_BC; /* "backspace character (old) */
7957 if (*bs)
7958 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7959 else
7960 cost = 999;
7961 if (col + 1 < cost) /* using CR is less characters */
7962 {
7963 plan = PLAN_CR;
7964 wouldbe_col = 0;
7965 cost = 1; /* CR is just one character */
7966 }
7967 else
7968 {
7969 plan = PLAN_LE;
7970 wouldbe_col = col;
7971 }
7972 if (noinvcurs) /* will stop highlighting */
7973 {
7974 cost += noinvcurs;
7975 attr = 0;
7976 }
7977 }
7978
7979 /*
7980 * If the cursor is above where we want to be, we can use CR LF.
7981 */
7982 else if (row > screen_cur_row)
7983 {
7984 plan = PLAN_NL;
7985 wouldbe_col = 0;
7986 cost = (row - screen_cur_row) * 2; /* CR LF */
7987 if (noinvcurs) /* will stop highlighting */
7988 {
7989 cost += noinvcurs;
7990 attr = 0;
7991 }
7992 }
7993
7994 /*
7995 * If the cursor is in the same row, smaller col, just use write.
7996 */
7997 else
7998 {
7999 plan = PLAN_WRITE;
8000 wouldbe_col = screen_cur_col;
8001 cost = 0;
8002 }
8003
8004 /*
8005 * Check if any characters that need to be written have the
8006 * correct attributes. Also avoid UTF-8 characters.
8007 */
8008 i = col - wouldbe_col;
8009 if (i > 0)
8010 cost += i;
8011 if (cost < goto_cost && i > 0)
8012 {
8013 /*
8014 * Check if the attributes are correct without additionally
8015 * stopping highlighting.
8016 */
8017 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8018 while (i && *p++ == attr)
8019 --i;
8020 if (i != 0)
8021 {
8022 /*
8023 * Try if it works when highlighting is stopped here.
8024 */
8025 if (*--p == 0)
8026 {
8027 cost += noinvcurs;
8028 while (i && *p++ == 0)
8029 --i;
8030 }
8031 if (i != 0)
8032 cost = 999; /* different attributes, don't do it */
8033 }
8034#ifdef FEAT_MBYTE
8035 if (enc_utf8)
8036 {
8037 /* Don't use an UTF-8 char for positioning, it's slow. */
8038 for (i = wouldbe_col; i < col; ++i)
8039 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8040 {
8041 cost = 999;
8042 break;
8043 }
8044 }
8045#endif
8046 }
8047
8048 /*
8049 * We can do it without term_windgoto()!
8050 */
8051 if (cost < goto_cost)
8052 {
8053 if (plan == PLAN_LE)
8054 {
8055 if (noinvcurs)
8056 screen_stop_highlight();
8057 while (screen_cur_col > col)
8058 {
8059 out_str(bs);
8060 --screen_cur_col;
8061 }
8062 }
8063 else if (plan == PLAN_CR)
8064 {
8065 if (noinvcurs)
8066 screen_stop_highlight();
8067 out_char('\r');
8068 screen_cur_col = 0;
8069 }
8070 else if (plan == PLAN_NL)
8071 {
8072 if (noinvcurs)
8073 screen_stop_highlight();
8074 while (screen_cur_row < row)
8075 {
8076 out_char('\n');
8077 ++screen_cur_row;
8078 }
8079 screen_cur_col = 0;
8080 }
8081
8082 i = col - screen_cur_col;
8083 if (i > 0)
8084 {
8085 /*
8086 * Use cursor-right if it's one character only. Avoids
8087 * removing a line of pixels from the last bold char, when
8088 * using the bold trick in the GUI.
8089 */
8090 if (T_ND[0] != NUL && T_ND[1] == NUL)
8091 {
8092 while (i-- > 0)
8093 out_char(*T_ND);
8094 }
8095 else
8096 {
8097 int off;
8098
8099 off = LineOffset[row] + screen_cur_col;
8100 while (i-- > 0)
8101 {
8102 if (ScreenAttrs[off] != screen_attr)
8103 screen_stop_highlight();
8104#ifdef FEAT_MBYTE
8105 out_flush_check();
8106#endif
8107 out_char(ScreenLines[off]);
8108#ifdef FEAT_MBYTE
8109 if (enc_dbcs == DBCS_JPNU
8110 && ScreenLines[off] == 0x8e)
8111 out_char(ScreenLines2[off]);
8112#endif
8113 ++off;
8114 }
8115 }
8116 }
8117 }
8118 }
8119 else
8120 cost = 999;
8121
8122 if (cost >= goto_cost)
8123 {
8124 if (noinvcurs)
8125 screen_stop_highlight();
8126 if (row == screen_cur_row && (col > screen_cur_col) &&
8127 *T_CRI != NUL)
8128 term_cursor_right(col - screen_cur_col);
8129 else
8130 term_windgoto(row, col);
8131 }
8132 screen_cur_row = row;
8133 screen_cur_col = col;
8134 }
8135}
8136
8137/*
8138 * Set cursor to its position in the current window.
8139 */
8140 void
8141setcursor()
8142{
8143 if (redrawing())
8144 {
8145 validate_cursor();
8146 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8147 W_WINCOL(curwin) + (
8148#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008149 /* With 'rightleft' set and the cursor on a double-wide
8150 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8152# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008153 (has_mbyte
8154 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8155 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156# endif
8157 1)) :
8158#endif
8159 curwin->w_wcol));
8160 }
8161}
8162
8163
8164/*
8165 * insert 'line_count' lines at 'row' in window 'wp'
8166 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8167 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8168 * scrolling.
8169 * Returns FAIL if the lines are not inserted, OK for success.
8170 */
8171 int
8172win_ins_lines(wp, row, line_count, invalid, mayclear)
8173 win_T *wp;
8174 int row;
8175 int line_count;
8176 int invalid;
8177 int mayclear;
8178{
8179 int did_delete;
8180 int nextrow;
8181 int lastrow;
8182 int retval;
8183
8184 if (invalid)
8185 wp->w_lines_valid = 0;
8186
8187 if (wp->w_height < 5)
8188 return FAIL;
8189
8190 if (line_count > wp->w_height - row)
8191 line_count = wp->w_height - row;
8192
8193 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8194 if (retval != MAYBE)
8195 return retval;
8196
8197 /*
8198 * If there is a next window or a status line, we first try to delete the
8199 * lines at the bottom to avoid messing what is after the window.
8200 * If this fails and there are following windows, don't do anything to avoid
8201 * messing up those windows, better just redraw.
8202 */
8203 did_delete = FALSE;
8204#ifdef FEAT_WINDOWS
8205 if (wp->w_next != NULL || wp->w_status_height)
8206 {
8207 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8208 line_count, (int)Rows, FALSE, NULL) == OK)
8209 did_delete = TRUE;
8210 else if (wp->w_next)
8211 return FAIL;
8212 }
8213#endif
8214 /*
8215 * if no lines deleted, blank the lines that will end up below the window
8216 */
8217 if (!did_delete)
8218 {
8219#ifdef FEAT_WINDOWS
8220 wp->w_redr_status = TRUE;
8221#endif
8222 redraw_cmdline = TRUE;
8223 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8224 lastrow = nextrow + line_count;
8225 if (lastrow > Rows)
8226 lastrow = Rows;
8227 screen_fill(nextrow - line_count, lastrow - line_count,
8228 W_WINCOL(wp), (int)W_ENDCOL(wp),
8229 ' ', ' ', 0);
8230 }
8231
8232 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8233 == FAIL)
8234 {
8235 /* deletion will have messed up other windows */
8236 if (did_delete)
8237 {
8238#ifdef FEAT_WINDOWS
8239 wp->w_redr_status = TRUE;
8240#endif
8241 win_rest_invalid(W_NEXT(wp));
8242 }
8243 return FAIL;
8244 }
8245
8246 return OK;
8247}
8248
8249/*
8250 * delete "line_count" window lines at "row" in window "wp"
8251 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8252 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8253 * scrolling
8254 * Return OK for success, FAIL if the lines are not deleted.
8255 */
8256 int
8257win_del_lines(wp, row, line_count, invalid, mayclear)
8258 win_T *wp;
8259 int row;
8260 int line_count;
8261 int invalid;
8262 int mayclear;
8263{
8264 int retval;
8265
8266 if (invalid)
8267 wp->w_lines_valid = 0;
8268
8269 if (line_count > wp->w_height - row)
8270 line_count = wp->w_height - row;
8271
8272 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8273 if (retval != MAYBE)
8274 return retval;
8275
8276 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8277 (int)Rows, FALSE, NULL) == FAIL)
8278 return FAIL;
8279
8280#ifdef FEAT_WINDOWS
8281 /*
8282 * If there are windows or status lines below, try to put them at the
8283 * correct place. If we can't do that, they have to be redrawn.
8284 */
8285 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8286 {
8287 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8288 line_count, (int)Rows, NULL) == FAIL)
8289 {
8290 wp->w_redr_status = TRUE;
8291 win_rest_invalid(wp->w_next);
8292 }
8293 }
8294 /*
8295 * If this is the last window and there is no status line, redraw the
8296 * command line later.
8297 */
8298 else
8299#endif
8300 redraw_cmdline = TRUE;
8301 return OK;
8302}
8303
8304/*
8305 * Common code for win_ins_lines() and win_del_lines().
8306 * Returns OK or FAIL when the work has been done.
8307 * Returns MAYBE when not finished yet.
8308 */
8309 static int
8310win_do_lines(wp, row, line_count, mayclear, del)
8311 win_T *wp;
8312 int row;
8313 int line_count;
8314 int mayclear;
8315 int del;
8316{
8317 int retval;
8318
8319 if (!redrawing() || line_count <= 0)
8320 return FAIL;
8321
8322 /* only a few lines left: redraw is faster */
8323 if (mayclear && Rows - line_count < 5
8324#ifdef FEAT_VERTSPLIT
8325 && wp->w_width == Columns
8326#endif
8327 )
8328 {
8329 screenclear(); /* will set wp->w_lines_valid to 0 */
8330 return FAIL;
8331 }
8332
8333 /*
8334 * Delete all remaining lines
8335 */
8336 if (row + line_count >= wp->w_height)
8337 {
8338 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8339 W_WINCOL(wp), (int)W_ENDCOL(wp),
8340 ' ', ' ', 0);
8341 return OK;
8342 }
8343
8344 /*
8345 * when scrolling, the message on the command line should be cleared,
8346 * otherwise it will stay there forever.
8347 */
8348 clear_cmdline = TRUE;
8349
8350 /*
8351 * If the terminal can set a scroll region, use that.
8352 * Always do this in a vertically split window. This will redraw from
8353 * ScreenLines[] when t_CV isn't defined. That's faster than using
8354 * win_line().
8355 * Don't use a scroll region when we are going to redraw the text, writing
8356 * a character in the lower right corner of the scroll region causes a
8357 * scroll-up in the DJGPP version.
8358 */
8359 if (scroll_region
8360#ifdef FEAT_VERTSPLIT
8361 || W_WIDTH(wp) != Columns
8362#endif
8363 )
8364 {
8365#ifdef FEAT_VERTSPLIT
8366 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8367#endif
8368 scroll_region_set(wp, row);
8369 if (del)
8370 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8371 wp->w_height - row, FALSE, wp);
8372 else
8373 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8374 wp->w_height - row, wp);
8375#ifdef FEAT_VERTSPLIT
8376 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8377#endif
8378 scroll_region_reset();
8379 return retval;
8380 }
8381
8382#ifdef FEAT_WINDOWS
8383 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8384 return FAIL;
8385#endif
8386
8387 return MAYBE;
8388}
8389
8390/*
8391 * window 'wp' and everything after it is messed up, mark it for redraw
8392 */
8393 static void
8394win_rest_invalid(wp)
8395 win_T *wp;
8396{
8397#ifdef FEAT_WINDOWS
8398 while (wp != NULL)
8399#else
8400 if (wp != NULL)
8401#endif
8402 {
8403 redraw_win_later(wp, NOT_VALID);
8404#ifdef FEAT_WINDOWS
8405 wp->w_redr_status = TRUE;
8406 wp = wp->w_next;
8407#endif
8408 }
8409 redraw_cmdline = TRUE;
8410}
8411
8412/*
8413 * The rest of the routines in this file perform screen manipulations. The
8414 * given operation is performed physically on the screen. The corresponding
8415 * change is also made to the internal screen image. In this way, the editor
8416 * anticipates the effect of editing changes on the appearance of the screen.
8417 * That way, when we call screenupdate a complete redraw isn't usually
8418 * necessary. Another advantage is that we can keep adding code to anticipate
8419 * screen changes, and in the meantime, everything still works.
8420 */
8421
8422/*
8423 * types for inserting or deleting lines
8424 */
8425#define USE_T_CAL 1
8426#define USE_T_CDL 2
8427#define USE_T_AL 3
8428#define USE_T_CE 4
8429#define USE_T_DL 5
8430#define USE_T_SR 6
8431#define USE_NL 7
8432#define USE_T_CD 8
8433#define USE_REDRAW 9
8434
8435/*
8436 * insert lines on the screen and update ScreenLines[]
8437 * 'end' is the line after the scrolled part. Normally it is Rows.
8438 * When scrolling region used 'off' is the offset from the top for the region.
8439 * 'row' and 'end' are relative to the start of the region.
8440 *
8441 * return FAIL for failure, OK for success.
8442 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008443 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444screen_ins_lines(off, row, line_count, end, wp)
8445 int off;
8446 int row;
8447 int line_count;
8448 int end;
8449 win_T *wp; /* NULL or window to use width from */
8450{
8451 int i;
8452 int j;
8453 unsigned temp;
8454 int cursor_row;
8455 int type;
8456 int result_empty;
8457 int can_ce = can_clear(T_CE);
8458
8459 /*
8460 * FAIL if
8461 * - there is no valid screen
8462 * - the screen has to be redrawn completely
8463 * - the line count is less than one
8464 * - the line count is more than 'ttyscroll'
8465 */
8466 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8467 return FAIL;
8468
8469 /*
8470 * There are seven ways to insert lines:
8471 * 0. When in a vertically split window and t_CV isn't set, redraw the
8472 * characters from ScreenLines[].
8473 * 1. Use T_CD (clear to end of display) if it exists and the result of
8474 * the insert is just empty lines
8475 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8476 * present or line_count > 1. It looks better if we do all the inserts
8477 * at once.
8478 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8479 * insert is just empty lines and T_CE is not present or line_count >
8480 * 1.
8481 * 4. Use T_AL (insert line) if it exists.
8482 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8483 * just empty lines.
8484 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8485 * just empty lines.
8486 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8487 * the 'da' flag is not set or we have clear line capability.
8488 * 8. redraw the characters from ScreenLines[].
8489 *
8490 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8491 * the scrollbar for the window. It does have insert line, use that if it
8492 * exists.
8493 */
8494 result_empty = (row + line_count >= end);
8495#ifdef FEAT_VERTSPLIT
8496 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8497 type = USE_REDRAW;
8498 else
8499#endif
8500 if (can_clear(T_CD) && result_empty)
8501 type = USE_T_CD;
8502 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8503 type = USE_T_CAL;
8504 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8505 type = USE_T_CDL;
8506 else if (*T_AL != NUL)
8507 type = USE_T_AL;
8508 else if (can_ce && result_empty)
8509 type = USE_T_CE;
8510 else if (*T_DL != NUL && result_empty)
8511 type = USE_T_DL;
8512 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8513 type = USE_T_SR;
8514 else
8515 return FAIL;
8516
8517 /*
8518 * For clearing the lines screen_del_lines() is used. This will also take
8519 * care of t_db if necessary.
8520 */
8521 if (type == USE_T_CD || type == USE_T_CDL ||
8522 type == USE_T_CE || type == USE_T_DL)
8523 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8524
8525 /*
8526 * If text is retained below the screen, first clear or delete as many
8527 * lines at the bottom of the window as are about to be inserted so that
8528 * the deleted lines won't later surface during a screen_del_lines.
8529 */
8530 if (*T_DB)
8531 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8532
8533#ifdef FEAT_CLIPBOARD
8534 /* Remove a modeless selection when inserting lines halfway the screen
8535 * or not the full width of the screen. */
8536 if (off + row > 0
8537# ifdef FEAT_VERTSPLIT
8538 || (wp != NULL && wp->w_width != Columns)
8539# endif
8540 )
8541 clip_clear_selection();
8542 else
8543 clip_scroll_selection(-line_count);
8544#endif
8545
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#ifdef FEAT_GUI
8547 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8548 * scrolling is actually carried out. */
8549 gui_dont_update_cursor();
8550#endif
8551
8552 if (*T_CCS != NUL) /* cursor relative to region */
8553 cursor_row = row;
8554 else
8555 cursor_row = row + off;
8556
8557 /*
8558 * Shift LineOffset[] line_count down to reflect the inserted lines.
8559 * Clear the inserted lines in ScreenLines[].
8560 */
8561 row += off;
8562 end += off;
8563 for (i = 0; i < line_count; ++i)
8564 {
8565#ifdef FEAT_VERTSPLIT
8566 if (wp != NULL && wp->w_width != Columns)
8567 {
8568 /* need to copy part of a line */
8569 j = end - 1 - i;
8570 while ((j -= line_count) >= row)
8571 linecopy(j + line_count, j, wp);
8572 j += line_count;
8573 if (can_clear((char_u *)" "))
8574 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8575 else
8576 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8577 LineWraps[j] = FALSE;
8578 }
8579 else
8580#endif
8581 {
8582 j = end - 1 - i;
8583 temp = LineOffset[j];
8584 while ((j -= line_count) >= row)
8585 {
8586 LineOffset[j + line_count] = LineOffset[j];
8587 LineWraps[j + line_count] = LineWraps[j];
8588 }
8589 LineOffset[j + line_count] = temp;
8590 LineWraps[j + line_count] = FALSE;
8591 if (can_clear((char_u *)" "))
8592 lineclear(temp, (int)Columns);
8593 else
8594 lineinvalid(temp, (int)Columns);
8595 }
8596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597
8598 screen_stop_highlight();
8599 windgoto(cursor_row, 0);
8600
8601#ifdef FEAT_VERTSPLIT
8602 /* redraw the characters */
8603 if (type == USE_REDRAW)
8604 redraw_block(row, end, wp);
8605 else
8606#endif
8607 if (type == USE_T_CAL)
8608 {
8609 term_append_lines(line_count);
8610 screen_start(); /* don't know where cursor is now */
8611 }
8612 else
8613 {
8614 for (i = 0; i < line_count; i++)
8615 {
8616 if (type == USE_T_AL)
8617 {
8618 if (i && cursor_row != 0)
8619 windgoto(cursor_row, 0);
8620 out_str(T_AL);
8621 }
8622 else /* type == USE_T_SR */
8623 out_str(T_SR);
8624 screen_start(); /* don't know where cursor is now */
8625 }
8626 }
8627
8628 /*
8629 * With scroll-reverse and 'da' flag set we need to clear the lines that
8630 * have been scrolled down into the region.
8631 */
8632 if (type == USE_T_SR && *T_DA)
8633 {
8634 for (i = 0; i < line_count; ++i)
8635 {
8636 windgoto(off + i, 0);
8637 out_str(T_CE);
8638 screen_start(); /* don't know where cursor is now */
8639 }
8640 }
8641
8642#ifdef FEAT_GUI
8643 gui_can_update_cursor();
8644 if (gui.in_use)
8645 out_flush(); /* always flush after a scroll */
8646#endif
8647 return OK;
8648}
8649
8650/*
8651 * delete lines on the screen and update ScreenLines[]
8652 * 'end' is the line after the scrolled part. Normally it is Rows.
8653 * When scrolling region used 'off' is the offset from the top for the region.
8654 * 'row' and 'end' are relative to the start of the region.
8655 *
8656 * Return OK for success, FAIL if the lines are not deleted.
8657 */
8658/*ARGSUSED*/
8659 int
8660screen_del_lines(off, row, line_count, end, force, wp)
8661 int off;
8662 int row;
8663 int line_count;
8664 int end;
8665 int force; /* even when line_count > p_ttyscroll */
8666 win_T *wp; /* NULL or window to use width from */
8667{
8668 int j;
8669 int i;
8670 unsigned temp;
8671 int cursor_row;
8672 int cursor_end;
8673 int result_empty; /* result is empty until end of region */
8674 int can_delete; /* deleting line codes can be used */
8675 int type;
8676
8677 /*
8678 * FAIL if
8679 * - there is no valid screen
8680 * - the screen has to be redrawn completely
8681 * - the line count is less than one
8682 * - the line count is more than 'ttyscroll'
8683 */
8684 if (!screen_valid(TRUE) || line_count <= 0 ||
8685 (!force && line_count > p_ttyscroll))
8686 return FAIL;
8687
8688 /*
8689 * Check if the rest of the current region will become empty.
8690 */
8691 result_empty = row + line_count >= end;
8692
8693 /*
8694 * We can delete lines only when 'db' flag not set or when 'ce' option
8695 * available.
8696 */
8697 can_delete = (*T_DB == NUL || can_clear(T_CE));
8698
8699 /*
8700 * There are six ways to delete lines:
8701 * 0. When in a vertically split window and t_CV isn't set, redraw the
8702 * characters from ScreenLines[].
8703 * 1. Use T_CD if it exists and the result is empty.
8704 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8705 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8706 * none of the other ways work.
8707 * 4. Use T_CE (erase line) if the result is empty.
8708 * 5. Use T_DL (delete line) if it exists.
8709 * 6. redraw the characters from ScreenLines[].
8710 */
8711#ifdef FEAT_VERTSPLIT
8712 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8713 type = USE_REDRAW;
8714 else
8715#endif
8716 if (can_clear(T_CD) && result_empty)
8717 type = USE_T_CD;
8718#if defined(__BEOS__) && defined(BEOS_DR8)
8719 /*
8720 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8721 * its internal termcap... this works okay for tests which test *T_DB !=
8722 * NUL. It has the disadvantage that the user cannot use any :set t_*
8723 * command to get T_DB (back) to empty_option, only :set term=... will do
8724 * the trick...
8725 * Anyway, this hack will hopefully go away with the next OS release.
8726 * (Olaf Seibert)
8727 */
8728 else if (row == 0 && T_DB == empty_option
8729 && (line_count == 1 || *T_CDL == NUL))
8730#else
8731 else if (row == 0 && (
8732#ifndef AMIGA
8733 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8734 * up, so use delete-line command */
8735 line_count == 1 ||
8736#endif
8737 *T_CDL == NUL))
8738#endif
8739 type = USE_NL;
8740 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8741 type = USE_T_CDL;
8742 else if (can_clear(T_CE) && result_empty
8743#ifdef FEAT_VERTSPLIT
8744 && (wp == NULL || wp->w_width == Columns)
8745#endif
8746 )
8747 type = USE_T_CE;
8748 else if (*T_DL != NUL && can_delete)
8749 type = USE_T_DL;
8750 else if (*T_CDL != NUL && can_delete)
8751 type = USE_T_CDL;
8752 else
8753 return FAIL;
8754
8755#ifdef FEAT_CLIPBOARD
8756 /* Remove a modeless selection when deleting lines halfway the screen or
8757 * not the full width of the screen. */
8758 if (off + row > 0
8759# ifdef FEAT_VERTSPLIT
8760 || (wp != NULL && wp->w_width != Columns)
8761# endif
8762 )
8763 clip_clear_selection();
8764 else
8765 clip_scroll_selection(line_count);
8766#endif
8767
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768#ifdef FEAT_GUI
8769 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8770 * scrolling is actually carried out. */
8771 gui_dont_update_cursor();
8772#endif
8773
8774 if (*T_CCS != NUL) /* cursor relative to region */
8775 {
8776 cursor_row = row;
8777 cursor_end = end;
8778 }
8779 else
8780 {
8781 cursor_row = row + off;
8782 cursor_end = end + off;
8783 }
8784
8785 /*
8786 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8787 * Clear the inserted lines in ScreenLines[].
8788 */
8789 row += off;
8790 end += off;
8791 for (i = 0; i < line_count; ++i)
8792 {
8793#ifdef FEAT_VERTSPLIT
8794 if (wp != NULL && wp->w_width != Columns)
8795 {
8796 /* need to copy part of a line */
8797 j = row + i;
8798 while ((j += line_count) <= end - 1)
8799 linecopy(j - line_count, j, wp);
8800 j -= line_count;
8801 if (can_clear((char_u *)" "))
8802 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8803 else
8804 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8805 LineWraps[j] = FALSE;
8806 }
8807 else
8808#endif
8809 {
8810 /* whole width, moving the line pointers is faster */
8811 j = row + i;
8812 temp = LineOffset[j];
8813 while ((j += line_count) <= end - 1)
8814 {
8815 LineOffset[j - line_count] = LineOffset[j];
8816 LineWraps[j - line_count] = LineWraps[j];
8817 }
8818 LineOffset[j - line_count] = temp;
8819 LineWraps[j - line_count] = FALSE;
8820 if (can_clear((char_u *)" "))
8821 lineclear(temp, (int)Columns);
8822 else
8823 lineinvalid(temp, (int)Columns);
8824 }
8825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
8827 screen_stop_highlight();
8828
8829#ifdef FEAT_VERTSPLIT
8830 /* redraw the characters */
8831 if (type == USE_REDRAW)
8832 redraw_block(row, end, wp);
8833 else
8834#endif
8835 if (type == USE_T_CD) /* delete the lines */
8836 {
8837 windgoto(cursor_row, 0);
8838 out_str(T_CD);
8839 screen_start(); /* don't know where cursor is now */
8840 }
8841 else if (type == USE_T_CDL)
8842 {
8843 windgoto(cursor_row, 0);
8844 term_delete_lines(line_count);
8845 screen_start(); /* don't know where cursor is now */
8846 }
8847 /*
8848 * Deleting lines at top of the screen or scroll region: Just scroll
8849 * the whole screen (scroll region) up by outputting newlines on the
8850 * last line.
8851 */
8852 else if (type == USE_NL)
8853 {
8854 windgoto(cursor_end - 1, 0);
8855 for (i = line_count; --i >= 0; )
8856 out_char('\n'); /* cursor will remain on same line */
8857 }
8858 else
8859 {
8860 for (i = line_count; --i >= 0; )
8861 {
8862 if (type == USE_T_DL)
8863 {
8864 windgoto(cursor_row, 0);
8865 out_str(T_DL); /* delete a line */
8866 }
8867 else /* type == USE_T_CE */
8868 {
8869 windgoto(cursor_row + i, 0);
8870 out_str(T_CE); /* erase a line */
8871 }
8872 screen_start(); /* don't know where cursor is now */
8873 }
8874 }
8875
8876 /*
8877 * If the 'db' flag is set, we need to clear the lines that have been
8878 * scrolled up at the bottom of the region.
8879 */
8880 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8881 {
8882 for (i = line_count; i > 0; --i)
8883 {
8884 windgoto(cursor_end - i, 0);
8885 out_str(T_CE); /* erase a line */
8886 screen_start(); /* don't know where cursor is now */
8887 }
8888 }
8889
8890#ifdef FEAT_GUI
8891 gui_can_update_cursor();
8892 if (gui.in_use)
8893 out_flush(); /* always flush after a scroll */
8894#endif
8895
8896 return OK;
8897}
8898
8899/*
8900 * show the current mode and ruler
8901 *
8902 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8903 * If clear_cmdline is FALSE there may be a message there that needs to be
8904 * cleared only if a mode is shown.
8905 * Return the length of the message (0 if no message).
8906 */
8907 int
8908showmode()
8909{
8910 int need_clear;
8911 int length = 0;
8912 int do_mode;
8913 int attr;
8914 int nwr_save;
8915#ifdef FEAT_INS_EXPAND
8916 int sub_attr;
8917#endif
8918
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008919 do_mode = ((p_smd && msg_silent == 0)
8920 && ((State & INSERT)
8921 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922#ifdef FEAT_VISUAL
8923 || VIsual_active
8924#endif
8925 ));
8926 if (do_mode || Recording)
8927 {
8928 /*
8929 * Don't show mode right now, when not redrawing or inside a mapping.
8930 * Call char_avail() only when we are going to show something, because
8931 * it takes a bit of time.
8932 */
8933 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8934 {
8935 redraw_cmdline = TRUE; /* show mode later */
8936 return 0;
8937 }
8938
8939 nwr_save = need_wait_return;
8940
8941 /* wait a bit before overwriting an important message */
8942 check_for_delay(FALSE);
8943
8944 /* if the cmdline is more than one line high, erase top lines */
8945 need_clear = clear_cmdline;
8946 if (clear_cmdline && cmdline_row < Rows - 1)
8947 msg_clr_cmdline(); /* will reset clear_cmdline */
8948
8949 /* Position on the last line in the window, column 0 */
8950 msg_pos_mode();
8951 cursor_off();
8952 attr = hl_attr(HLF_CM); /* Highlight mode */
8953 if (do_mode)
8954 {
8955 MSG_PUTS_ATTR("--", attr);
8956#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00008957# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 if (xic != NULL && im_get_status() && !p_imdisable
8959 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00008960# else
8961 if (
8962# ifdef HAVE_GTK2
8963 preedit_get_status()
8964# else
8965 im_get_status()
8966# endif
8967 )
8968# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8970 MSG_PUTS_ATTR(" IM", attr);
8971# else
8972 MSG_PUTS_ATTR(" XIM", attr);
8973# endif
8974#endif
8975#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8976 if (gui.in_use)
8977 {
8978 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008979 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 }
8981#endif
8982#ifdef FEAT_INS_EXPAND
8983 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8984 {
8985 /* These messages can get long, avoid a wrap in a narrow
8986 * window. Prefer showing edit_submode_extra. */
8987 length = (Rows - msg_row) * Columns - 3;
8988 if (edit_submode_extra != NULL)
8989 length -= vim_strsize(edit_submode_extra);
8990 if (length > 0)
8991 {
8992 if (edit_submode_pre != NULL)
8993 length -= vim_strsize(edit_submode_pre);
8994 if (length - vim_strsize(edit_submode) > 0)
8995 {
8996 if (edit_submode_pre != NULL)
8997 msg_puts_attr(edit_submode_pre, attr);
8998 msg_puts_attr(edit_submode, attr);
8999 }
9000 if (edit_submode_extra != NULL)
9001 {
9002 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9003 if ((int)edit_submode_highl < (int)HLF_COUNT)
9004 sub_attr = hl_attr(edit_submode_highl);
9005 else
9006 sub_attr = attr;
9007 msg_puts_attr(edit_submode_extra, sub_attr);
9008 }
9009 }
9010 length = 0;
9011 }
9012 else
9013#endif
9014 {
9015#ifdef FEAT_VREPLACE
9016 if (State & VREPLACE_FLAG)
9017 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9018 else
9019#endif
9020 if (State & REPLACE_FLAG)
9021 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9022 else if (State & INSERT)
9023 {
9024#ifdef FEAT_RIGHTLEFT
9025 if (p_ri)
9026 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9027#endif
9028 MSG_PUTS_ATTR(_(" INSERT"), attr);
9029 }
9030 else if (restart_edit == 'I')
9031 MSG_PUTS_ATTR(_(" (insert)"), attr);
9032 else if (restart_edit == 'R')
9033 MSG_PUTS_ATTR(_(" (replace)"), attr);
9034 else if (restart_edit == 'V')
9035 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9036#ifdef FEAT_RIGHTLEFT
9037 if (p_hkmap)
9038 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9039# ifdef FEAT_FKMAP
9040 if (p_fkmap)
9041 MSG_PUTS_ATTR(farsi_text_5, attr);
9042# endif
9043#endif
9044#ifdef FEAT_KEYMAP
9045 if (State & LANGMAP)
9046 {
9047# ifdef FEAT_ARABIC
9048 if (curwin->w_p_arab)
9049 MSG_PUTS_ATTR(_(" Arabic"), attr);
9050 else
9051# endif
9052 MSG_PUTS_ATTR(_(" (lang)"), attr);
9053 }
9054#endif
9055 if ((State & INSERT) && p_paste)
9056 MSG_PUTS_ATTR(_(" (paste)"), attr);
9057
9058#ifdef FEAT_VISUAL
9059 if (VIsual_active)
9060 {
9061 char *p;
9062
9063 /* Don't concatenate separate words to avoid translation
9064 * problems. */
9065 switch ((VIsual_select ? 4 : 0)
9066 + (VIsual_mode == Ctrl_V) * 2
9067 + (VIsual_mode == 'V'))
9068 {
9069 case 0: p = N_(" VISUAL"); break;
9070 case 1: p = N_(" VISUAL LINE"); break;
9071 case 2: p = N_(" VISUAL BLOCK"); break;
9072 case 4: p = N_(" SELECT"); break;
9073 case 5: p = N_(" SELECT LINE"); break;
9074 default: p = N_(" SELECT BLOCK"); break;
9075 }
9076 MSG_PUTS_ATTR(_(p), attr);
9077 }
9078#endif
9079 MSG_PUTS_ATTR(" --", attr);
9080 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 need_clear = TRUE;
9083 }
9084 if (Recording
9085#ifdef FEAT_INS_EXPAND
9086 && edit_submode == NULL /* otherwise it gets too long */
9087#endif
9088 )
9089 {
9090 MSG_PUTS_ATTR(_("recording"), attr);
9091 need_clear = TRUE;
9092 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009093
9094 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 if (need_clear || clear_cmdline)
9096 msg_clr_eos();
9097 msg_didout = FALSE; /* overwrite this message */
9098 length = msg_col;
9099 msg_col = 0;
9100 need_wait_return = nwr_save; /* never ask for hit-return for this */
9101 }
9102 else if (clear_cmdline && msg_silent == 0)
9103 /* Clear the whole command line. Will reset "clear_cmdline". */
9104 msg_clr_cmdline();
9105
9106#ifdef FEAT_CMDL_INFO
9107# ifdef FEAT_VISUAL
9108 /* In Visual mode the size of the selected area must be redrawn. */
9109 if (VIsual_active)
9110 clear_showcmd();
9111# endif
9112
9113 /* If the last window has no status line, the ruler is after the mode
9114 * message and must be redrawn */
9115 if (redrawing()
9116# ifdef FEAT_WINDOWS
9117 && lastwin->w_status_height == 0
9118# endif
9119 )
9120 win_redr_ruler(lastwin, TRUE);
9121#endif
9122 redraw_cmdline = FALSE;
9123 clear_cmdline = FALSE;
9124
9125 return length;
9126}
9127
9128/*
9129 * Position for a mode message.
9130 */
9131 static void
9132msg_pos_mode()
9133{
9134 msg_col = 0;
9135 msg_row = Rows - 1;
9136}
9137
9138/*
9139 * Delete mode message. Used when ESC is typed which is expected to end
9140 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009141 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 */
9143 void
9144unshowmode(force)
9145 int force;
9146{
9147 /*
9148 * Don't delete it right now, when not redrawing or insided a mapping.
9149 */
9150 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9151 redraw_cmdline = TRUE; /* delete mode later */
9152 else
9153 {
9154 msg_pos_mode();
9155 if (Recording)
9156 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9157 msg_clr_eos();
9158 }
9159}
9160
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009161#if defined(FEAT_WINDOWS)
9162/*
9163 * Draw the tab pages line at the top of the Vim window.
9164 */
9165 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009166draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009167{
9168 int tabcount = 0;
9169 tabpage_T *tp;
9170 int tabwidth;
9171 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009172 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009173 int attr;
9174 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009175 win_T *cwp;
9176 int wincount;
9177 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009178 int c;
9179 int len;
9180 int attr_sel = hl_attr(HLF_TPS);
9181 int attr_nosel = hl_attr(HLF_TP);
9182 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009183 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009184 int room;
9185 int use_sep_chars = (t_colors < 8
9186#ifdef FEAT_GUI
9187 && !gui.in_use
9188#endif
9189 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009190
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009191 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009192
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009193#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009194 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009195 if (gui_use_tabline())
9196 {
9197 gui_update_tabline();
9198 return;
9199 }
9200#endif
9201
9202 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009203 return;
9204
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009205#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009206
9207 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9208 for (scol = 0; scol < Columns; ++scol)
9209 TabPageIdxs[scol] = 0;
9210
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009211 /* Use the 'tabline' option if it's set. */
9212 if (*p_tal != NUL)
9213 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009214 int save_called_emsg = called_emsg;
9215
9216 /* Check for an error. If there is one we would loop in redrawing the
9217 * screen. Avoid that by making 'tabline' empty. */
9218 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009219 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009220 if (called_emsg)
9221 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009222 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009223 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009224 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009225 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009226#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009227 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009228 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9229 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009230
Bram Moolenaar238a5642006-02-21 22:12:05 +00009231 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9232 if (tabwidth < 6)
9233 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009234
Bram Moolenaar238a5642006-02-21 22:12:05 +00009235 attr = attr_nosel;
9236 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009237 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009238 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9239 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009240 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009241 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009242
Bram Moolenaar238a5642006-02-21 22:12:05 +00009243 if (tp->tp_topframe == topframe)
9244 attr = attr_sel;
9245 if (use_sep_chars && col > 0)
9246 screen_putchar('|', 0, col++, attr);
9247
9248 if (tp->tp_topframe != topframe)
9249 attr = attr_nosel;
9250
9251 screen_putchar(' ', 0, col++, attr);
9252
9253 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009254 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009255 cwp = curwin;
9256 wp = firstwin;
9257 }
9258 else
9259 {
9260 cwp = tp->tp_curwin;
9261 wp = tp->tp_firstwin;
9262 }
9263
9264 modified = FALSE;
9265 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9266 if (bufIsChanged(wp->w_buffer))
9267 modified = TRUE;
9268 if (modified || wincount > 1)
9269 {
9270 if (wincount > 1)
9271 {
9272 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009273 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009274 if (col + len >= Columns - 3)
9275 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009276 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009277#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009278 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009279#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009280 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009281#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009282 );
9283 col += len;
9284 }
9285 if (modified)
9286 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9287 screen_putchar(' ', 0, col++, attr);
9288 }
9289
9290 room = scol - col + tabwidth - 1;
9291 if (room > 0)
9292 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009293 /* Get buffer name in NameBuff[] */
9294 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009295 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009296 len = vim_strsize(NameBuff);
9297 p = NameBuff;
9298#ifdef FEAT_MBYTE
9299 if (has_mbyte)
9300 while (len > room)
9301 {
9302 len -= ptr2cells(p);
9303 mb_ptr_adv(p);
9304 }
9305 else
9306#endif
9307 if (len > room)
9308 {
9309 p += len - room;
9310 len = room;
9311 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009312 if (len > Columns - col - 1)
9313 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009314
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009315 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009316 col += len;
9317 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009318 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009319
9320 /* Store the tab page number in TabPageIdxs[], so that
9321 * jump_to_mouse() knows where each one is. */
9322 ++tabcount;
9323 while (scol < col)
9324 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009325 }
9326
Bram Moolenaar238a5642006-02-21 22:12:05 +00009327 if (use_sep_chars)
9328 c = '_';
9329 else
9330 c = ' ';
9331 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009332
9333 /* Put an "X" for closing the current tab if there are several. */
9334 if (first_tabpage->tp_next != NULL)
9335 {
9336 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9337 TabPageIdxs[Columns - 1] = -999;
9338 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009339 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009340
9341 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9342 * set. */
9343 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009344}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009345
9346/*
9347 * Get buffer name for "buf" into NameBuff[].
9348 * Takes care of special buffer names and translates special characters.
9349 */
9350 void
9351get_trans_bufname(buf)
9352 buf_T *buf;
9353{
9354 if (buf_spname(buf) != NULL)
9355 STRCPY(NameBuff, buf_spname(buf));
9356 else
9357 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9358 trans_characters(NameBuff, MAXPATHL);
9359}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009360#endif
9361
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9363/*
9364 * Get the character to use in a status line. Get its attributes in "*attr".
9365 */
9366 static int
9367fillchar_status(attr, is_curwin)
9368 int *attr;
9369 int is_curwin;
9370{
9371 int fill;
9372 if (is_curwin)
9373 {
9374 *attr = hl_attr(HLF_S);
9375 fill = fill_stl;
9376 }
9377 else
9378 {
9379 *attr = hl_attr(HLF_SNC);
9380 fill = fill_stlnc;
9381 }
9382 /* Use fill when there is highlighting, and highlighting of current
9383 * window differs, or the fillchars differ, or this is not the
9384 * current window */
9385 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9386 || !is_curwin || firstwin == lastwin)
9387 || (fill_stl != fill_stlnc)))
9388 return fill;
9389 if (is_curwin)
9390 return '^';
9391 return '=';
9392}
9393#endif
9394
9395#ifdef FEAT_VERTSPLIT
9396/*
9397 * Get the character to use in a separator between vertically split windows.
9398 * Get its attributes in "*attr".
9399 */
9400 static int
9401fillchar_vsep(attr)
9402 int *attr;
9403{
9404 *attr = hl_attr(HLF_C);
9405 if (*attr == 0 && fill_vert == ' ')
9406 return '|';
9407 else
9408 return fill_vert;
9409}
9410#endif
9411
9412/*
9413 * Return TRUE if redrawing should currently be done.
9414 */
9415 int
9416redrawing()
9417{
9418 return (!RedrawingDisabled
9419 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9420}
9421
9422/*
9423 * Return TRUE if printing messages should currently be done.
9424 */
9425 int
9426messaging()
9427{
9428 return (!(p_lz && char_avail() && !KeyTyped));
9429}
9430
9431/*
9432 * Show current status info in ruler and various other places
9433 * If always is FALSE, only show ruler if position has changed.
9434 */
9435 void
9436showruler(always)
9437 int always;
9438{
9439 if (!always && !redrawing())
9440 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009441#ifdef FEAT_INS_EXPAND
9442 if (pum_visible())
9443 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009444# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009445 /* Don't redraw right now, do it later. */
9446 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009447# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009448 return;
9449 }
9450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009452 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009453 {
9454 redraw_custum_statusline(curwin);
9455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 else
9457#endif
9458#ifdef FEAT_CMDL_INFO
9459 win_redr_ruler(curwin, always);
9460#endif
9461
9462#ifdef FEAT_TITLE
9463 if (need_maketitle
9464# ifdef FEAT_STL_OPT
9465 || (p_icon && (stl_syntax & STL_IN_ICON))
9466 || (p_title && (stl_syntax & STL_IN_TITLE))
9467# endif
9468 )
9469 maketitle();
9470#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009471#ifdef FEAT_WINDOWS
9472 /* Redraw the tab pages line if needed. */
9473 if (redraw_tabline)
9474 draw_tabline();
9475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478#ifdef FEAT_CMDL_INFO
9479 static void
9480win_redr_ruler(wp, always)
9481 win_T *wp;
9482 int always;
9483{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009484#define RULER_BUF_LEN 70
9485 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 int row;
9487 int fillchar;
9488 int attr;
9489 int empty_line = FALSE;
9490 colnr_T virtcol;
9491 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009492 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 int o;
9494#ifdef FEAT_VERTSPLIT
9495 int this_ru_col;
9496 int off = 0;
9497 int width = Columns;
9498# define WITH_OFF(x) x
9499# define WITH_WIDTH(x) x
9500#else
9501# define WITH_OFF(x) 0
9502# define WITH_WIDTH(x) Columns
9503# define this_ru_col ru_col
9504#endif
9505
9506 /* If 'ruler' off or redrawing disabled, don't do anything */
9507 if (!p_ru)
9508 return;
9509
9510 /*
9511 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9512 * after deleting lines, before cursor.lnum is corrected.
9513 */
9514 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9515 return;
9516
9517#ifdef FEAT_INS_EXPAND
9518 /* Don't draw the ruler while doing insert-completion, it might overwrite
9519 * the (long) mode message. */
9520# ifdef FEAT_WINDOWS
9521 if (wp == lastwin && lastwin->w_status_height == 0)
9522# endif
9523 if (edit_submode != NULL)
9524 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009525 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9526 if (pum_visible())
9527 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528#endif
9529
9530#ifdef FEAT_STL_OPT
9531 if (*p_ruf)
9532 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009533 int save_called_emsg = called_emsg;
9534
9535 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009537 if (called_emsg)
9538 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009539 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009540 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 return;
9542 }
9543#endif
9544
9545 /*
9546 * Check if not in Insert mode and the line is empty (will show "0-1").
9547 */
9548 if (!(State & INSERT)
9549 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9550 empty_line = TRUE;
9551
9552 /*
9553 * Only draw the ruler when something changed.
9554 */
9555 validate_virtcol_win(wp);
9556 if ( redraw_cmdline
9557 || always
9558 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9559 || wp->w_cursor.col != wp->w_ru_cursor.col
9560 || wp->w_virtcol != wp->w_ru_virtcol
9561#ifdef FEAT_VIRTUALEDIT
9562 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9563#endif
9564 || wp->w_topline != wp->w_ru_topline
9565 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9566#ifdef FEAT_DIFF
9567 || wp->w_topfill != wp->w_ru_topfill
9568#endif
9569 || empty_line != wp->w_ru_empty)
9570 {
9571 cursor_off();
9572#ifdef FEAT_WINDOWS
9573 if (wp->w_status_height)
9574 {
9575 row = W_WINROW(wp) + wp->w_height;
9576 fillchar = fillchar_status(&attr, wp == curwin);
9577# ifdef FEAT_VERTSPLIT
9578 off = W_WINCOL(wp);
9579 width = W_WIDTH(wp);
9580# endif
9581 }
9582 else
9583#endif
9584 {
9585 row = Rows - 1;
9586 fillchar = ' ';
9587 attr = 0;
9588#ifdef FEAT_VERTSPLIT
9589 width = Columns;
9590 off = 0;
9591#endif
9592 }
9593
9594 /* In list mode virtcol needs to be recomputed */
9595 virtcol = wp->w_virtcol;
9596 if (wp->w_p_list && lcs_tab1 == NUL)
9597 {
9598 wp->w_p_list = FALSE;
9599 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9600 wp->w_p_list = TRUE;
9601 }
9602
9603 /*
9604 * Some sprintfs return the length, some return a pointer.
9605 * To avoid portability problems we use strlen() here.
9606 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009607 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9609 ? 0L
9610 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009611 len = STRLEN(buffer);
9612 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9614 (int)virtcol + 1);
9615
9616 /*
9617 * Add a "50%" if there is room for it.
9618 * On the last line, don't print in the last column (scrolls the
9619 * screen up on some terminals).
9620 */
9621 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009622 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 o = i + vim_strsize(buffer + i + 1);
9624#ifdef FEAT_WINDOWS
9625 if (wp->w_status_height == 0) /* can't use last char of screen */
9626#endif
9627 ++o;
9628#ifdef FEAT_VERTSPLIT
9629 this_ru_col = ru_col - (Columns - width);
9630 if (this_ru_col < 0)
9631 this_ru_col = 0;
9632#endif
9633 /* Never use more than half the window/screen width, leave the other
9634 * half for the filename. */
9635 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9636 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9637 if (this_ru_col + o < WITH_WIDTH(width))
9638 {
9639 while (this_ru_col + o < WITH_WIDTH(width))
9640 {
9641#ifdef FEAT_MBYTE
9642 if (has_mbyte)
9643 i += (*mb_char2bytes)(fillchar, buffer + i);
9644 else
9645#endif
9646 buffer[i++] = fillchar;
9647 ++o;
9648 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009649 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 }
9651 /* Truncate at window boundary. */
9652#ifdef FEAT_MBYTE
9653 if (has_mbyte)
9654 {
9655 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009656 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 {
9658 o += (*mb_ptr2cells)(buffer + i);
9659 if (this_ru_col + o > WITH_WIDTH(width))
9660 {
9661 buffer[i] = NUL;
9662 break;
9663 }
9664 }
9665 }
9666 else
9667#endif
9668 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9669 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9670
9671 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9672 i = redraw_cmdline;
9673 screen_fill(row, row + 1,
9674 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9675 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9676 fillchar, fillchar, attr);
9677 /* don't redraw the cmdline because of showing the ruler */
9678 redraw_cmdline = i;
9679 wp->w_ru_cursor = wp->w_cursor;
9680 wp->w_ru_virtcol = wp->w_virtcol;
9681 wp->w_ru_empty = empty_line;
9682 wp->w_ru_topline = wp->w_topline;
9683 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9684#ifdef FEAT_DIFF
9685 wp->w_ru_topfill = wp->w_topfill;
9686#endif
9687 }
9688}
9689#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009690
9691#if defined(FEAT_LINEBREAK) || defined(PROTO)
9692/*
9693 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009694 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009695 * Otherwise it depends on 'numberwidth' and the line count.
9696 */
9697 int
9698number_width(wp)
9699 win_T *wp;
9700{
9701 int n;
9702 linenr_T lnum;
9703
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009704 lnum = wp->w_buffer->b_ml.ml_line_count;
9705 if (lnum == wp->w_nrwidth_line_count)
9706 return wp->w_nrwidth_width;
9707 wp->w_nrwidth_line_count = lnum;
9708
9709 n = 0;
9710 do
9711 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009712 lnum /= 10;
9713 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009714 } while (lnum > 0);
9715
9716 /* 'numberwidth' gives the minimal width plus one */
9717 if (n < wp->w_p_nuw - 1)
9718 n = wp->w_p_nuw - 1;
9719
9720 wp->w_nrwidth_width = n;
9721 return n;
9722}
9723#endif