blob: b7107b62577aa47cbcc909a49919c3618fd467d7 [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
Bram Moolenaar362f3562009-11-03 16:20:34 +0000135static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 void
274redrawWinline(lnum, invalid)
275 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000276 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277{
278#ifdef FEAT_FOLDING
279 int i;
280#endif
281
282 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
283 curwin->w_redraw_top = lnum;
284 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
285 curwin->w_redraw_bot = lnum;
286 redraw_later(VALID);
287
288#ifdef FEAT_FOLDING
289 if (invalid)
290 {
291 /* A w_lines[] entry for this lnum has become invalid. */
292 i = find_wl_entry(curwin, lnum);
293 if (i >= 0)
294 curwin->w_lines[i].wl_valid = FALSE;
295 }
296#endif
297}
298
299/*
300 * update all windows that are editing the current buffer
301 */
302 void
303update_curbuf(type)
304 int type;
305{
306 redraw_curbuf_later(type);
307 update_screen(type);
308}
309
310/*
311 * update_screen()
312 *
313 * Based on the current value of curwin->w_topline, transfer a screenfull
314 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
315 */
316 void
317update_screen(type)
318 int type;
319{
320 win_T *wp;
321 static int did_intro = FALSE;
322#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
323 int did_one;
324#endif
325
326 if (!screen_valid(TRUE))
327 return;
328
329 if (must_redraw)
330 {
331 if (type < must_redraw) /* use maximal type */
332 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000333
334 /* must_redraw is reset here, so that when we run into some weird
335 * reason to redraw while busy redrawing (e.g., asynchronous
336 * scrolling), or update_topline() in win_update() will cause a
337 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 must_redraw = 0;
339 }
340
341 /* Need to update w_lines[]. */
342 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
343 type = NOT_VALID;
344
345 if (!redrawing())
346 {
347 redraw_later(type); /* remember type for next time */
348 must_redraw = type;
349 if (type > INVERTED_ALL)
350 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
351 return;
352 }
353
354 updating_screen = TRUE;
355#ifdef FEAT_SYN_HL
356 ++display_tick; /* let syntax code know we're in a next round of
357 * display updating */
358#endif
359
360 /*
361 * if the screen was scrolled up when displaying a message, scroll it down
362 */
363 if (msg_scrolled)
364 {
365 clear_cmdline = TRUE;
366 if (msg_scrolled > Rows - 5) /* clearing is faster */
367 type = CLEAR;
368 else if (type != CLEAR)
369 {
370 check_for_delay(FALSE);
371 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
372 type = CLEAR;
373 FOR_ALL_WINDOWS(wp)
374 {
375 if (W_WINROW(wp) < msg_scrolled)
376 {
377 if (W_WINROW(wp) + wp->w_height > msg_scrolled
378 && wp->w_redr_type < REDRAW_TOP
379 && wp->w_lines_valid > 0
380 && wp->w_topline == wp->w_lines[0].wl_lnum)
381 {
382 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
383 wp->w_redr_type = REDRAW_TOP;
384 }
385 else
386 {
387 wp->w_redr_type = NOT_VALID;
388#ifdef FEAT_WINDOWS
389 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
390 <= msg_scrolled)
391 wp->w_redr_status = TRUE;
392#endif
393 }
394 }
395 }
396 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000397#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000398 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
401 msg_scrolled = 0;
402 need_wait_return = FALSE;
403 }
404
405 /* reset cmdline_row now (may have been changed temporarily) */
406 compute_cmdrow();
407
408 /* Check for changed highlighting */
409 if (need_highlight_changed)
410 highlight_changed();
411
412 if (type == CLEAR) /* first clear screen */
413 {
414 screenclear(); /* will reset clear_cmdline */
415 type = NOT_VALID;
416 }
417
418 if (clear_cmdline) /* going to clear cmdline (done below) */
419 check_for_delay(FALSE);
420
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000421#ifdef FEAT_LINEBREAK
422 /* Force redraw when width of 'number' column changes. */
423 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000424 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000425 curwin->w_redr_type = NOT_VALID;
426#endif
427
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 /*
429 * Only start redrawing if there is really something to do.
430 */
431 if (type == INVERTED)
432 update_curswant();
433 if (curwin->w_redr_type < type
434 && !((type == VALID
435 && curwin->w_lines[0].wl_valid
436#ifdef FEAT_DIFF
437 && curwin->w_topfill == curwin->w_old_topfill
438 && curwin->w_botfill == curwin->w_old_botfill
439#endif
440 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
441#ifdef FEAT_VISUAL
442 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000443 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
445 && curwin->w_old_visual_mode == VIsual_mode
446 && (curwin->w_valid & VALID_VIRTCOL)
447 && curwin->w_old_curswant == curwin->w_curswant)
448#endif
449 ))
450 curwin->w_redr_type = type;
451
Bram Moolenaar5a305422006-04-28 22:38:25 +0000452#ifdef FEAT_WINDOWS
453 /* Redraw the tab pages line if needed. */
454 if (redraw_tabline || type >= NOT_VALID)
455 draw_tabline();
456#endif
457
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458#ifdef FEAT_SYN_HL
459 /*
460 * Correct stored syntax highlighting info for changes in each displayed
461 * buffer. Each buffer must only be done once.
462 */
463 FOR_ALL_WINDOWS(wp)
464 {
465 if (wp->w_buffer->b_mod_set)
466 {
467# ifdef FEAT_WINDOWS
468 win_T *wwp;
469
470 /* Check if we already did this buffer. */
471 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
472 if (wwp->w_buffer == wp->w_buffer)
473 break;
474# endif
475 if (
476# ifdef FEAT_WINDOWS
477 wwp == wp &&
478# endif
479 syntax_present(wp->w_buffer))
480 syn_stack_apply_changes(wp->w_buffer);
481 }
482 }
483#endif
484
485 /*
486 * Go from top to bottom through the windows, redrawing the ones that need
487 * it.
488 */
489#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
490 did_one = FALSE;
491#endif
492#ifdef FEAT_SEARCH_EXTRA
493 search_hl.rm.regprog = NULL;
494#endif
495 FOR_ALL_WINDOWS(wp)
496 {
497 if (wp->w_redr_type != 0)
498 {
499 cursor_off();
500#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
501 if (!did_one)
502 {
503 did_one = TRUE;
504# ifdef FEAT_SEARCH_EXTRA
505 start_search_hl();
506# endif
507# ifdef FEAT_CLIPBOARD
508 /* When Visual area changed, may have to update selection. */
509 if (clip_star.available && clip_isautosel())
510 clip_update_selection();
511# endif
512#ifdef FEAT_GUI
513 /* Remove the cursor before starting to do anything, because
514 * scrolling may make it difficult to redraw the text under
515 * it. */
516 if (gui.in_use)
517 gui_undraw_cursor();
518#endif
519 }
520#endif
521 win_update(wp);
522 }
523
524#ifdef FEAT_WINDOWS
525 /* redraw status line after the window to minimize cursor movement */
526 if (wp->w_redr_status)
527 {
528 cursor_off();
529 win_redr_status(wp);
530 }
531#endif
532 }
533#if defined(FEAT_SEARCH_EXTRA)
534 end_search_hl();
535#endif
536
537#ifdef FEAT_WINDOWS
538 /* Reset b_mod_set flags. Going through all windows is probably faster
539 * than going through all buffers (there could be many buffers). */
540 for (wp = firstwin; wp != NULL; wp = wp->w_next)
541 wp->w_buffer->b_mod_set = FALSE;
542#else
543 curbuf->b_mod_set = FALSE;
544#endif
545
546 updating_screen = FALSE;
547#ifdef FEAT_GUI
548 gui_may_resize_shell();
549#endif
550
551 /* Clear or redraw the command line. Done last, because scrolling may
552 * mess up the command line. */
553 if (clear_cmdline || redraw_cmdline)
554 showmode();
555
556 /* May put up an introductory message when not editing a file */
557 if (!did_intro && bufempty()
558 && curbuf->b_fname == NULL
559#ifdef FEAT_WINDOWS
560 && firstwin->w_next == NULL
561#endif
562 && vim_strchr(p_shm, SHM_INTRO) == NULL)
563 intro_message(FALSE);
564 did_intro = TRUE;
565
566#ifdef FEAT_GUI
567 /* Redraw the cursor and update the scrollbars when all screen updating is
568 * done. */
569 if (gui.in_use)
570 {
571 out_flush(); /* required before updating the cursor */
572 if (did_one)
573 gui_update_cursor(FALSE, FALSE);
574 gui_update_scrollbars(FALSE);
575 }
576#endif
577}
578
579#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
580static void update_prepare __ARGS((void));
581static void update_finish __ARGS((void));
582
583/*
584 * Prepare for updating one or more windows.
585 */
586 static void
587update_prepare()
588{
589 cursor_off();
590 updating_screen = TRUE;
591#ifdef FEAT_GUI
592 /* Remove the cursor before starting to do anything, because scrolling may
593 * make it difficult to redraw the text under it. */
594 if (gui.in_use)
595 gui_undraw_cursor();
596#endif
597#ifdef FEAT_SEARCH_EXTRA
598 start_search_hl();
599#endif
600}
601
602/*
603 * Finish updating one or more windows.
604 */
605 static void
606update_finish()
607{
608 if (redraw_cmdline)
609 showmode();
610
611# ifdef FEAT_SEARCH_EXTRA
612 end_search_hl();
613# endif
614
615 updating_screen = FALSE;
616
617# ifdef FEAT_GUI
618 gui_may_resize_shell();
619
620 /* Redraw the cursor and update the scrollbars when all screen updating is
621 * done. */
622 if (gui.in_use)
623 {
624 out_flush(); /* required before updating the cursor */
625 gui_update_cursor(FALSE, FALSE);
626 gui_update_scrollbars(FALSE);
627 }
628# endif
629}
630#endif
631
632#if defined(FEAT_SIGNS) || defined(PROTO)
633 void
634update_debug_sign(buf, lnum)
635 buf_T *buf;
636 linenr_T lnum;
637{
638 win_T *wp;
639 int doit = FALSE;
640
641# ifdef FEAT_FOLDING
642 win_foldinfo.fi_level = 0;
643# endif
644
645 /* update/delete a specific mark */
646 FOR_ALL_WINDOWS(wp)
647 {
648 if (buf != NULL && lnum > 0)
649 {
650 if (wp->w_buffer == buf && lnum >= wp->w_topline
651 && lnum < wp->w_botline)
652 {
653 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
654 wp->w_redraw_top = lnum;
655 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
656 wp->w_redraw_bot = lnum;
657 redraw_win_later(wp, VALID);
658 }
659 }
660 else
661 redraw_win_later(wp, VALID);
662 if (wp->w_redr_type != 0)
663 doit = TRUE;
664 }
665
666 if (!doit)
667 return;
668
669 /* update all windows that need updating */
670 update_prepare();
671
672# ifdef FEAT_WINDOWS
673 for (wp = firstwin; wp; wp = wp->w_next)
674 {
675 if (wp->w_redr_type != 0)
676 win_update(wp);
677 if (wp->w_redr_status)
678 win_redr_status(wp);
679 }
680# else
681 if (curwin->w_redr_type != 0)
682 win_update(curwin);
683# endif
684
685 update_finish();
686}
687#endif
688
689
690#if defined(FEAT_GUI) || defined(PROTO)
691/*
692 * Update a single window, its status line and maybe the command line msg.
693 * Used for the GUI scrollbar.
694 */
695 void
696updateWindow(wp)
697 win_T *wp;
698{
699 update_prepare();
700
701#ifdef FEAT_CLIPBOARD
702 /* When Visual area changed, may have to update selection. */
703 if (clip_star.available && clip_isautosel())
704 clip_update_selection();
705#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000710 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000711 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000712 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000713
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (wp->w_redr_status
715# ifdef FEAT_CMDL_INFO
716 || p_ru
717# endif
718# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000719 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720# endif
721 )
722 win_redr_status(wp);
723#endif
724
725 update_finish();
726}
727#endif
728
729/*
730 * Update a single window.
731 *
732 * This may cause the windows below it also to be redrawn (when clearing the
733 * screen or scrolling lines).
734 *
735 * How the window is redrawn depends on wp->w_redr_type. Each type also
736 * implies the one below it.
737 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000738 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
740 * INVERTED redraw the changed part of the Visual area
741 * INVERTED_ALL redraw the whole Visual area
742 * VALID 1. scroll up/down to adjust for a changed w_topline
743 * 2. update lines at the top when scrolled down
744 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000745 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * b_mod_top and b_mod_bot.
747 * - if wp->w_redraw_top non-zero, redraw lines between
748 * wp->w_redraw_top and wp->w_redr_bot.
749 * - continue redrawing when syntax status is invalid.
750 * 4. if scrolled up, update lines at the bottom.
751 * This results in three areas that may need updating:
752 * top: from first row to top_end (when scrolled down)
753 * mid: from mid_start to mid_end (update inversion or changed text)
754 * bot: from bot_start to last row (when scrolled up)
755 */
756 static void
757win_update(wp)
758 win_T *wp;
759{
760 buf_T *buf = wp->w_buffer;
761 int type;
762 int top_end = 0; /* Below last row of the top area that needs
763 updating. 0 when no top area updating. */
764 int mid_start = 999;/* first row of the mid area that needs
765 updating. 999 when no mid area updating. */
766 int mid_end = 0; /* Below last row of the mid area that needs
767 updating. 0 when no mid area updating. */
768 int bot_start = 999;/* first row of the bot area that needs
769 updating. 999 when no bot area updating */
770#ifdef FEAT_VISUAL
771 int scrolled_down = FALSE; /* TRUE when scrolled down when
772 w_topline got smaller a bit */
773#endif
774#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000775 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 int top_to_mod = FALSE; /* redraw above mod_top */
777#endif
778
779 int row; /* current window row to display */
780 linenr_T lnum; /* current buffer lnum to display */
781 int idx; /* current index in w_lines[] */
782 int srow; /* starting row of the current line */
783
784 int eof = FALSE; /* if TRUE, we hit the end of the file */
785 int didline = FALSE; /* if TRUE, we finished the last line */
786 int i;
787 long j;
788 static int recursive = FALSE; /* being called recursively */
789 int old_botline = wp->w_botline;
790#ifdef FEAT_FOLDING
791 long fold_count;
792#endif
793#ifdef FEAT_SYN_HL
794 /* remember what happened to the previous line, to know if
795 * check_visual_highlight() can be used */
796#define DID_NONE 1 /* didn't update a line */
797#define DID_LINE 2 /* updated a normal line */
798#define DID_FOLD 3 /* updated a folded line */
799 int did_update = DID_NONE;
800 linenr_T syntax_last_parsed = 0; /* last parsed text line */
801#endif
802 linenr_T mod_top = 0;
803 linenr_T mod_bot = 0;
804#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
805 int save_got_int;
806#endif
807
808 type = wp->w_redr_type;
809
810 if (type == NOT_VALID)
811 {
812#ifdef FEAT_WINDOWS
813 wp->w_redr_status = TRUE;
814#endif
815 wp->w_lines_valid = 0;
816 }
817
818 /* Window is zero-height: nothing to draw. */
819 if (wp->w_height == 0)
820 {
821 wp->w_redr_type = 0;
822 return;
823 }
824
825#ifdef FEAT_VERTSPLIT
826 /* Window is zero-width: Only need to draw the separator. */
827 if (wp->w_width == 0)
828 {
829 /* draw the vertical separator right of this window */
830 draw_vsep_win(wp, 0);
831 wp->w_redr_type = 0;
832 return;
833 }
834#endif
835
836#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000837 /* Setup for match and 'hlsearch' highlighting. Disable any previous
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000838 * match */
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000839 cur = wp->w_match_head;
840 while (cur != NULL)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000841 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000842 cur->hl.rm = cur->match;
843 if (cur->hlg_id == 0)
844 cur->hl.attr = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000845 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000846 cur->hl.attr = syn_id2attr(cur->hlg_id);
847 cur->hl.buf = buf;
848 cur->hl.lnum = 0;
849 cur->hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000850# ifdef FEAT_RELTIME
851 /* Set the time limit to 'redrawtime'. */
852 profile_setlimit(p_rdt, &(cur->hl.tm));
853# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000854 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 search_hl.buf = buf;
857 search_hl.lnum = 0;
858 search_hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000859 /* time limit is set at the toplevel, for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#endif
861
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000862#ifdef FEAT_LINEBREAK
863 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000864 i = wp->w_p_nu ? number_width(wp) : 0;
865 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000866 {
867 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000868 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000869 }
870 else
871#endif
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
874 {
875 /*
876 * When there are both inserted/deleted lines and specific lines to be
877 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
878 * everything (only happens when redrawing is off for while).
879 */
880 type = NOT_VALID;
881 }
882 else
883 {
884 /*
885 * Set mod_top to the first line that needs displaying because of
886 * changes. Set mod_bot to the first line after the changes.
887 */
888 mod_top = wp->w_redraw_top;
889 if (wp->w_redraw_bot != 0)
890 mod_bot = wp->w_redraw_bot + 1;
891 else
892 mod_bot = 0;
893 wp->w_redraw_top = 0; /* reset for next time */
894 wp->w_redraw_bot = 0;
895 if (buf->b_mod_set)
896 {
897 if (mod_top == 0 || mod_top > buf->b_mod_top)
898 {
899 mod_top = buf->b_mod_top;
900#ifdef FEAT_SYN_HL
901 /* Need to redraw lines above the change that may be included
902 * in a pattern match. */
903 if (syntax_present(buf))
904 {
905 mod_top -= buf->b_syn_sync_linebreaks;
906 if (mod_top < 1)
907 mod_top = 1;
908 }
909#endif
910 }
911 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
912 mod_bot = buf->b_mod_bot;
913
914#ifdef FEAT_SEARCH_EXTRA
915 /* When 'hlsearch' is on and using a multi-line search pattern, a
916 * change in one line may make the Search highlighting in a
917 * previous line invalid. Simple solution: redraw all visible
918 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000919 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000921 if (search_hl.rm.regprog != NULL
922 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000924 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000925 {
926 cur = wp->w_match_head;
927 while (cur != NULL)
928 {
929 if (cur->match.regprog != NULL
930 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000931 {
932 top_to_mod = TRUE;
933 break;
934 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000935 cur = cur->next;
936 }
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#endif
939 }
940#ifdef FEAT_FOLDING
941 if (mod_top != 0 && hasAnyFolding(wp))
942 {
943 linenr_T lnumt, lnumb;
944
945 /*
946 * A change in a line can cause lines above it to become folded or
947 * unfolded. Find the top most buffer line that may be affected.
948 * If the line was previously folded and displayed, get the first
949 * line of that fold. If the line is folded now, get the first
950 * folded line. Use the minimum of these two.
951 */
952
953 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
954 * the line below it. If there is no valid entry, use w_topline.
955 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
956 * to this line. If there is no valid entry, use MAXLNUM. */
957 lnumt = wp->w_topline;
958 lnumb = MAXLNUM;
959 for (i = 0; i < wp->w_lines_valid; ++i)
960 if (wp->w_lines[i].wl_valid)
961 {
962 if (wp->w_lines[i].wl_lastlnum < mod_top)
963 lnumt = wp->w_lines[i].wl_lastlnum + 1;
964 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
965 {
966 lnumb = wp->w_lines[i].wl_lnum;
967 /* When there is a fold column it might need updating
968 * in the next line ("J" just above an open fold). */
969 if (wp->w_p_fdc > 0)
970 ++lnumb;
971 }
972 }
973
974 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
975 if (mod_top > lnumt)
976 mod_top = lnumt;
977
978 /* Now do the same for the bottom line (one above mod_bot). */
979 --mod_bot;
980 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
981 ++mod_bot;
982 if (mod_bot < lnumb)
983 mod_bot = lnumb;
984 }
985#endif
986
987 /* When a change starts above w_topline and the end is below
988 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000989 * If the end of the change is above w_topline: do like no change was
990 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (mod_top != 0 && mod_top < wp->w_topline)
992 {
993 if (mod_bot > wp->w_topline)
994 mod_top = wp->w_topline;
995#ifdef FEAT_SYN_HL
996 else if (syntax_present(buf))
997 top_end = 1;
998#endif
999 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001000
1001 /* When line numbers are displayed need to redraw all lines below
1002 * inserted/deleted lines. */
1003 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1004 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006
1007 /*
1008 * When only displaying the lines at the top, set top_end. Used when
1009 * window has scrolled down for msg_scrolled.
1010 */
1011 if (type == REDRAW_TOP)
1012 {
1013 j = 0;
1014 for (i = 0; i < wp->w_lines_valid; ++i)
1015 {
1016 j += wp->w_lines[i].wl_size;
1017 if (j >= wp->w_upd_rows)
1018 {
1019 top_end = j;
1020 break;
1021 }
1022 }
1023 if (top_end == 0)
1024 /* not found (cannot happen?): redraw everything */
1025 type = NOT_VALID;
1026 else
1027 /* top area defined, the rest is VALID */
1028 type = VALID;
1029 }
1030
Bram Moolenaar367329b2007-08-30 11:53:22 +00001031 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001032 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1033 * non-zero and thus not FALSE) will indicate that screenclear() was not
1034 * called. */
1035 if (screen_cleared)
1036 screen_cleared = MAYBE;
1037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 /*
1039 * If there are no changes on the screen that require a complete redraw,
1040 * handle three cases:
1041 * 1: we are off the top of the screen by a few lines: scroll down
1042 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1043 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1044 * w_lines[] that needs updating.
1045 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001046 if ((type == VALID || type == SOME_VALID
1047 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#ifdef FEAT_DIFF
1049 && !wp->w_botfill && !wp->w_old_botfill
1050#endif
1051 )
1052 {
1053 if (mod_top != 0 && wp->w_topline == mod_top)
1054 {
1055 /*
1056 * w_topline is the first changed line, the scrolling will be done
1057 * further down.
1058 */
1059 }
1060 else if (wp->w_lines[0].wl_valid
1061 && (wp->w_topline < wp->w_lines[0].wl_lnum
1062#ifdef FEAT_DIFF
1063 || (wp->w_topline == wp->w_lines[0].wl_lnum
1064 && wp->w_topfill > wp->w_old_topfill)
1065#endif
1066 ))
1067 {
1068 /*
1069 * New topline is above old topline: May scroll down.
1070 */
1071#ifdef FEAT_FOLDING
1072 if (hasAnyFolding(wp))
1073 {
1074 linenr_T ln;
1075
1076 /* count the number of lines we are off, counting a sequence
1077 * of folded lines as one */
1078 j = 0;
1079 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1080 {
1081 ++j;
1082 if (j >= wp->w_height - 2)
1083 break;
1084 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1085 }
1086 }
1087 else
1088#endif
1089 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1090 if (j < wp->w_height - 2) /* not too far off */
1091 {
1092 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1093#ifdef FEAT_DIFF
1094 /* insert extra lines for previously invisible filler lines */
1095 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1096 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1097 - wp->w_old_topfill;
1098#endif
1099 if (i < wp->w_height - 2) /* less than a screen off */
1100 {
1101 /*
1102 * Try to insert the correct number of lines.
1103 * If not the last window, delete the lines at the bottom.
1104 * win_ins_lines may fail when the terminal can't do it.
1105 */
1106 if (i > 0)
1107 check_for_delay(FALSE);
1108 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1109 {
1110 if (wp->w_lines_valid != 0)
1111 {
1112 /* Need to update rows that are new, stop at the
1113 * first one that scrolled down. */
1114 top_end = i;
1115#ifdef FEAT_VISUAL
1116 scrolled_down = TRUE;
1117#endif
1118
1119 /* Move the entries that were scrolled, disable
1120 * the entries for the lines to be redrawn. */
1121 if ((wp->w_lines_valid += j) > wp->w_height)
1122 wp->w_lines_valid = wp->w_height;
1123 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1124 wp->w_lines[idx] = wp->w_lines[idx - j];
1125 while (idx >= 0)
1126 wp->w_lines[idx--].wl_valid = FALSE;
1127 }
1128 }
1129 else
1130 mid_start = 0; /* redraw all lines */
1131 }
1132 else
1133 mid_start = 0; /* redraw all lines */
1134 }
1135 else
1136 mid_start = 0; /* redraw all lines */
1137 }
1138 else
1139 {
1140 /*
1141 * New topline is at or below old topline: May scroll up.
1142 * When topline didn't change, find first entry in w_lines[] that
1143 * needs updating.
1144 */
1145
1146 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1147 j = -1;
1148 row = 0;
1149 for (i = 0; i < wp->w_lines_valid; i++)
1150 {
1151 if (wp->w_lines[i].wl_valid
1152 && wp->w_lines[i].wl_lnum == wp->w_topline)
1153 {
1154 j = i;
1155 break;
1156 }
1157 row += wp->w_lines[i].wl_size;
1158 }
1159 if (j == -1)
1160 {
1161 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1162 * lines */
1163 mid_start = 0;
1164 }
1165 else
1166 {
1167 /*
1168 * Try to delete the correct number of lines.
1169 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1170 */
1171#ifdef FEAT_DIFF
1172 /* If the topline didn't change, delete old filler lines,
1173 * otherwise delete filler lines of the new topline... */
1174 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1175 row += wp->w_old_topfill;
1176 else
1177 row += diff_check_fill(wp, wp->w_topline);
1178 /* ... but don't delete new filler lines. */
1179 row -= wp->w_topfill;
1180#endif
1181 if (row > 0)
1182 {
1183 check_for_delay(FALSE);
1184 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1185 bot_start = wp->w_height - row;
1186 else
1187 mid_start = 0; /* redraw all lines */
1188 }
1189 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1190 {
1191 /*
1192 * Skip the lines (below the deleted lines) that are still
1193 * valid and don't need redrawing. Copy their info
1194 * upwards, to compensate for the deleted lines. Set
1195 * bot_start to the first row that needs redrawing.
1196 */
1197 bot_start = 0;
1198 idx = 0;
1199 for (;;)
1200 {
1201 wp->w_lines[idx] = wp->w_lines[j];
1202 /* stop at line that didn't fit, unless it is still
1203 * valid (no lines deleted) */
1204 if (row > 0 && bot_start + row
1205 + (int)wp->w_lines[j].wl_size > wp->w_height)
1206 {
1207 wp->w_lines_valid = idx + 1;
1208 break;
1209 }
1210 bot_start += wp->w_lines[idx++].wl_size;
1211
1212 /* stop at the last valid entry in w_lines[].wl_size */
1213 if (++j >= wp->w_lines_valid)
1214 {
1215 wp->w_lines_valid = idx;
1216 break;
1217 }
1218 }
1219#ifdef FEAT_DIFF
1220 /* Correct the first entry for filler lines at the top
1221 * when it won't get updated below. */
1222 if (wp->w_p_diff && bot_start > 0)
1223 wp->w_lines[0].wl_size =
1224 plines_win_nofill(wp, wp->w_topline, TRUE)
1225 + wp->w_topfill;
1226#endif
1227 }
1228 }
1229 }
1230
1231 /* When starting redraw in the first line, redraw all lines. When
1232 * there is only one window it's probably faster to clear the screen
1233 * first. */
1234 if (mid_start == 0)
1235 {
1236 mid_end = wp->w_height;
1237 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001238 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001239 /* Clear the screen when it was not done by win_del_lines() or
1240 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1241 * then. */
1242 if (screen_cleared != TRUE)
1243 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001244#ifdef FEAT_WINDOWS
1245 /* The screen was cleared, redraw the tab pages line. */
1246 if (redraw_tabline)
1247 draw_tabline();
1248#endif
1249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001251
1252 /* When win_del_lines() or win_ins_lines() caused the screen to be
1253 * cleared (only happens for the first window) or when screenclear()
1254 * was called directly above, "must_redraw" will have been set to
1255 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1256 if (screen_cleared == TRUE)
1257 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259 else
1260 {
1261 /* Not VALID or INVERTED: redraw all lines. */
1262 mid_start = 0;
1263 mid_end = wp->w_height;
1264 }
1265
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001266 if (type == SOME_VALID)
1267 {
1268 /* SOME_VALID: redraw all lines. */
1269 mid_start = 0;
1270 mid_end = wp->w_height;
1271 type = NOT_VALID;
1272 }
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274#ifdef FEAT_VISUAL
1275 /* check if we are updating or removing the inverted part */
1276 if ((VIsual_active && buf == curwin->w_buffer)
1277 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1278 {
1279 linenr_T from, to;
1280
1281 if (VIsual_active)
1282 {
1283 if (VIsual_active
1284 && (VIsual_mode != wp->w_old_visual_mode
1285 || type == INVERTED_ALL))
1286 {
1287 /*
1288 * If the type of Visual selection changed, redraw the whole
1289 * selection. Also when the ownership of the X selection is
1290 * gained or lost.
1291 */
1292 if (curwin->w_cursor.lnum < VIsual.lnum)
1293 {
1294 from = curwin->w_cursor.lnum;
1295 to = VIsual.lnum;
1296 }
1297 else
1298 {
1299 from = VIsual.lnum;
1300 to = curwin->w_cursor.lnum;
1301 }
1302 /* redraw more when the cursor moved as well */
1303 if (wp->w_old_cursor_lnum < from)
1304 from = wp->w_old_cursor_lnum;
1305 if (wp->w_old_cursor_lnum > to)
1306 to = wp->w_old_cursor_lnum;
1307 if (wp->w_old_visual_lnum < from)
1308 from = wp->w_old_visual_lnum;
1309 if (wp->w_old_visual_lnum > to)
1310 to = wp->w_old_visual_lnum;
1311 }
1312 else
1313 {
1314 /*
1315 * Find the line numbers that need to be updated: The lines
1316 * between the old cursor position and the current cursor
1317 * position. Also check if the Visual position changed.
1318 */
1319 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1320 {
1321 from = curwin->w_cursor.lnum;
1322 to = wp->w_old_cursor_lnum;
1323 }
1324 else
1325 {
1326 from = wp->w_old_cursor_lnum;
1327 to = curwin->w_cursor.lnum;
1328 if (from == 0) /* Visual mode just started */
1329 from = to;
1330 }
1331
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001332 if (VIsual.lnum != wp->w_old_visual_lnum
1333 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
1335 if (wp->w_old_visual_lnum < from
1336 && wp->w_old_visual_lnum != 0)
1337 from = wp->w_old_visual_lnum;
1338 if (wp->w_old_visual_lnum > to)
1339 to = wp->w_old_visual_lnum;
1340 if (VIsual.lnum < from)
1341 from = VIsual.lnum;
1342 if (VIsual.lnum > to)
1343 to = VIsual.lnum;
1344 }
1345 }
1346
1347 /*
1348 * If in block mode and changed column or curwin->w_curswant:
1349 * update all lines.
1350 * First compute the actual start and end column.
1351 */
1352 if (VIsual_mode == Ctrl_V)
1353 {
1354 colnr_T fromc, toc;
1355
1356 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1357 ++toc;
1358 if (curwin->w_curswant == MAXCOL)
1359 toc = MAXCOL;
1360
1361 if (fromc != wp->w_old_cursor_fcol
1362 || toc != wp->w_old_cursor_lcol)
1363 {
1364 if (from > VIsual.lnum)
1365 from = VIsual.lnum;
1366 if (to < VIsual.lnum)
1367 to = VIsual.lnum;
1368 }
1369 wp->w_old_cursor_fcol = fromc;
1370 wp->w_old_cursor_lcol = toc;
1371 }
1372 }
1373 else
1374 {
1375 /* Use the line numbers of the old Visual area. */
1376 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1377 {
1378 from = wp->w_old_cursor_lnum;
1379 to = wp->w_old_visual_lnum;
1380 }
1381 else
1382 {
1383 from = wp->w_old_visual_lnum;
1384 to = wp->w_old_cursor_lnum;
1385 }
1386 }
1387
1388 /*
1389 * There is no need to update lines above the top of the window.
1390 */
1391 if (from < wp->w_topline)
1392 from = wp->w_topline;
1393
1394 /*
1395 * If we know the value of w_botline, use it to restrict the update to
1396 * the lines that are visible in the window.
1397 */
1398 if (wp->w_valid & VALID_BOTLINE)
1399 {
1400 if (from >= wp->w_botline)
1401 from = wp->w_botline - 1;
1402 if (to >= wp->w_botline)
1403 to = wp->w_botline - 1;
1404 }
1405
1406 /*
1407 * Find the minimal part to be updated.
1408 * Watch out for scrolling that made entries in w_lines[] invalid.
1409 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1410 * top_end; need to redraw from top_end to the "to" line.
1411 * A middle mouse click with a Visual selection may change the text
1412 * above the Visual area and reset wl_valid, do count these for
1413 * mid_end (in srow).
1414 */
1415 if (mid_start > 0)
1416 {
1417 lnum = wp->w_topline;
1418 idx = 0;
1419 srow = 0;
1420 if (scrolled_down)
1421 mid_start = top_end;
1422 else
1423 mid_start = 0;
1424 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1425 {
1426 if (wp->w_lines[idx].wl_valid)
1427 mid_start += wp->w_lines[idx].wl_size;
1428 else if (!scrolled_down)
1429 srow += wp->w_lines[idx].wl_size;
1430 ++idx;
1431# ifdef FEAT_FOLDING
1432 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1433 lnum = wp->w_lines[idx].wl_lnum;
1434 else
1435# endif
1436 ++lnum;
1437 }
1438 srow += mid_start;
1439 mid_end = wp->w_height;
1440 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1441 {
1442 if (wp->w_lines[idx].wl_valid
1443 && wp->w_lines[idx].wl_lnum >= to + 1)
1444 {
1445 /* Only update until first row of this line */
1446 mid_end = srow;
1447 break;
1448 }
1449 srow += wp->w_lines[idx].wl_size;
1450 }
1451 }
1452 }
1453
1454 if (VIsual_active && buf == curwin->w_buffer)
1455 {
1456 wp->w_old_visual_mode = VIsual_mode;
1457 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1458 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001459 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 wp->w_old_curswant = curwin->w_curswant;
1461 }
1462 else
1463 {
1464 wp->w_old_visual_mode = 0;
1465 wp->w_old_cursor_lnum = 0;
1466 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001467 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
1469#endif /* FEAT_VISUAL */
1470
1471#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1472 /* reset got_int, otherwise regexp won't work */
1473 save_got_int = got_int;
1474 got_int = 0;
1475#endif
1476#ifdef FEAT_FOLDING
1477 win_foldinfo.fi_level = 0;
1478#endif
1479
1480 /*
1481 * Update all the window rows.
1482 */
1483 idx = 0; /* first entry in w_lines[].wl_size */
1484 row = 0;
1485 srow = 0;
1486 lnum = wp->w_topline; /* first line shown in window */
1487 for (;;)
1488 {
1489 /* stop updating when reached the end of the window (check for _past_
1490 * the end of the window is at the end of the loop) */
1491 if (row == wp->w_height)
1492 {
1493 didline = TRUE;
1494 break;
1495 }
1496
1497 /* stop updating when hit the end of the file */
1498 if (lnum > buf->b_ml.ml_line_count)
1499 {
1500 eof = TRUE;
1501 break;
1502 }
1503
1504 /* Remember the starting row of the line that is going to be dealt
1505 * with. It is used further down when the line doesn't fit. */
1506 srow = row;
1507
1508 /*
1509 * Update a line when it is in an area that needs updating, when it
1510 * has changes or w_lines[idx] is invalid.
1511 * bot_start may be halfway a wrapped line after using
1512 * win_del_lines(), check if the current line includes it.
1513 * When syntax folding is being used, the saved syntax states will
1514 * already have been updated, we can't see where the syntax state is
1515 * the same again, just update until the end of the window.
1516 */
1517 if (row < top_end
1518 || (row >= mid_start && row < mid_end)
1519#ifdef FEAT_SEARCH_EXTRA
1520 || top_to_mod
1521#endif
1522 || idx >= wp->w_lines_valid
1523 || (row + wp->w_lines[idx].wl_size > bot_start)
1524 || (mod_top != 0
1525 && (lnum == mod_top
1526 || (lnum >= mod_top
1527 && (lnum < mod_bot
1528#ifdef FEAT_SYN_HL
1529 || did_update == DID_FOLD
1530 || (did_update == DID_LINE
1531 && syntax_present(buf)
1532 && (
1533# ifdef FEAT_FOLDING
1534 (foldmethodIsSyntax(wp)
1535 && hasAnyFolding(wp)) ||
1536# endif
1537 syntax_check_changed(lnum)))
1538#endif
1539 )))))
1540 {
1541#ifdef FEAT_SEARCH_EXTRA
1542 if (lnum == mod_top)
1543 top_to_mod = FALSE;
1544#endif
1545
1546 /*
1547 * When at start of changed lines: May scroll following lines
1548 * up or down to minimize redrawing.
1549 * Don't do this when the change continues until the end.
1550 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1551 */
1552 if (lnum == mod_top
1553 && mod_bot != MAXLNUM
1554 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1555 {
1556 int old_rows = 0;
1557 int new_rows = 0;
1558 int xtra_rows;
1559 linenr_T l;
1560
1561 /* Count the old number of window rows, using w_lines[], which
1562 * should still contain the sizes for the lines as they are
1563 * currently displayed. */
1564 for (i = idx; i < wp->w_lines_valid; ++i)
1565 {
1566 /* Only valid lines have a meaningful wl_lnum. Invalid
1567 * lines are part of the changed area. */
1568 if (wp->w_lines[i].wl_valid
1569 && wp->w_lines[i].wl_lnum == mod_bot)
1570 break;
1571 old_rows += wp->w_lines[i].wl_size;
1572#ifdef FEAT_FOLDING
1573 if (wp->w_lines[i].wl_valid
1574 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1575 {
1576 /* Must have found the last valid entry above mod_bot.
1577 * Add following invalid entries. */
1578 ++i;
1579 while (i < wp->w_lines_valid
1580 && !wp->w_lines[i].wl_valid)
1581 old_rows += wp->w_lines[i++].wl_size;
1582 break;
1583 }
1584#endif
1585 }
1586
1587 if (i >= wp->w_lines_valid)
1588 {
1589 /* We can't find a valid line below the changed lines,
1590 * need to redraw until the end of the window.
1591 * Inserting/deleting lines has no use. */
1592 bot_start = 0;
1593 }
1594 else
1595 {
1596 /* Able to count old number of rows: Count new window
1597 * rows, and may insert/delete lines */
1598 j = idx;
1599 for (l = lnum; l < mod_bot; ++l)
1600 {
1601#ifdef FEAT_FOLDING
1602 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1603 ++new_rows;
1604 else
1605#endif
1606#ifdef FEAT_DIFF
1607 if (l == wp->w_topline)
1608 new_rows += plines_win_nofill(wp, l, TRUE)
1609 + wp->w_topfill;
1610 else
1611#endif
1612 new_rows += plines_win(wp, l, TRUE);
1613 ++j;
1614 if (new_rows > wp->w_height - row - 2)
1615 {
1616 /* it's getting too much, must redraw the rest */
1617 new_rows = 9999;
1618 break;
1619 }
1620 }
1621 xtra_rows = new_rows - old_rows;
1622 if (xtra_rows < 0)
1623 {
1624 /* May scroll text up. If there is not enough
1625 * remaining text or scrolling fails, must redraw the
1626 * rest. If scrolling works, must redraw the text
1627 * below the scrolled text. */
1628 if (row - xtra_rows >= wp->w_height - 2)
1629 mod_bot = MAXLNUM;
1630 else
1631 {
1632 check_for_delay(FALSE);
1633 if (win_del_lines(wp, row,
1634 -xtra_rows, FALSE, FALSE) == FAIL)
1635 mod_bot = MAXLNUM;
1636 else
1637 bot_start = wp->w_height + xtra_rows;
1638 }
1639 }
1640 else if (xtra_rows > 0)
1641 {
1642 /* May scroll text down. If there is not enough
1643 * remaining text of scrolling fails, must redraw the
1644 * rest. */
1645 if (row + xtra_rows >= wp->w_height - 2)
1646 mod_bot = MAXLNUM;
1647 else
1648 {
1649 check_for_delay(FALSE);
1650 if (win_ins_lines(wp, row + old_rows,
1651 xtra_rows, FALSE, FALSE) == FAIL)
1652 mod_bot = MAXLNUM;
1653 else if (top_end > row + old_rows)
1654 /* Scrolled the part at the top that requires
1655 * updating down. */
1656 top_end += xtra_rows;
1657 }
1658 }
1659
1660 /* When not updating the rest, may need to move w_lines[]
1661 * entries. */
1662 if (mod_bot != MAXLNUM && i != j)
1663 {
1664 if (j < i)
1665 {
1666 int x = row + new_rows;
1667
1668 /* move entries in w_lines[] upwards */
1669 for (;;)
1670 {
1671 /* stop at last valid entry in w_lines[] */
1672 if (i >= wp->w_lines_valid)
1673 {
1674 wp->w_lines_valid = j;
1675 break;
1676 }
1677 wp->w_lines[j] = wp->w_lines[i];
1678 /* stop at a line that won't fit */
1679 if (x + (int)wp->w_lines[j].wl_size
1680 > wp->w_height)
1681 {
1682 wp->w_lines_valid = j + 1;
1683 break;
1684 }
1685 x += wp->w_lines[j++].wl_size;
1686 ++i;
1687 }
1688 if (bot_start > x)
1689 bot_start = x;
1690 }
1691 else /* j > i */
1692 {
1693 /* move entries in w_lines[] downwards */
1694 j -= i;
1695 wp->w_lines_valid += j;
1696 if (wp->w_lines_valid > wp->w_height)
1697 wp->w_lines_valid = wp->w_height;
1698 for (i = wp->w_lines_valid; i - j >= idx; --i)
1699 wp->w_lines[i] = wp->w_lines[i - j];
1700
1701 /* The w_lines[] entries for inserted lines are
1702 * now invalid, but wl_size may be used above.
1703 * Reset to zero. */
1704 while (i >= idx)
1705 {
1706 wp->w_lines[i].wl_size = 0;
1707 wp->w_lines[i--].wl_valid = FALSE;
1708 }
1709 }
1710 }
1711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713
1714#ifdef FEAT_FOLDING
1715 /*
1716 * When lines are folded, display one line for all of them.
1717 * Otherwise, display normally (can be several display lines when
1718 * 'wrap' is on).
1719 */
1720 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1721 if (fold_count != 0)
1722 {
1723 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1724 ++row;
1725 --fold_count;
1726 wp->w_lines[idx].wl_folded = TRUE;
1727 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1728# ifdef FEAT_SYN_HL
1729 did_update = DID_FOLD;
1730# endif
1731 }
1732 else
1733#endif
1734 if (idx < wp->w_lines_valid
1735 && wp->w_lines[idx].wl_valid
1736 && wp->w_lines[idx].wl_lnum == lnum
1737 && lnum > wp->w_topline
1738 && !(dy_flags & DY_LASTLINE)
1739 && srow + wp->w_lines[idx].wl_size > wp->w_height
1740#ifdef FEAT_DIFF
1741 && diff_check_fill(wp, lnum) == 0
1742#endif
1743 )
1744 {
1745 /* This line is not going to fit. Don't draw anything here,
1746 * will draw "@ " lines below. */
1747 row = wp->w_height + 1;
1748 }
1749 else
1750 {
1751#ifdef FEAT_SEARCH_EXTRA
1752 prepare_search_hl(wp, lnum);
1753#endif
1754#ifdef FEAT_SYN_HL
1755 /* Let the syntax stuff know we skipped a few lines. */
1756 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1757 && syntax_present(buf))
1758 syntax_end_parsing(syntax_last_parsed + 1);
1759#endif
1760
1761 /*
1762 * Display one line.
1763 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001764 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766#ifdef FEAT_FOLDING
1767 wp->w_lines[idx].wl_folded = FALSE;
1768 wp->w_lines[idx].wl_lastlnum = lnum;
1769#endif
1770#ifdef FEAT_SYN_HL
1771 did_update = DID_LINE;
1772 syntax_last_parsed = lnum;
1773#endif
1774 }
1775
1776 wp->w_lines[idx].wl_lnum = lnum;
1777 wp->w_lines[idx].wl_valid = TRUE;
1778 if (row > wp->w_height) /* past end of screen */
1779 {
1780 /* we may need the size of that too long line later on */
1781 if (dollar_vcol == 0)
1782 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1783 ++idx;
1784 break;
1785 }
1786 if (dollar_vcol == 0)
1787 wp->w_lines[idx].wl_size = row - srow;
1788 ++idx;
1789#ifdef FEAT_FOLDING
1790 lnum += fold_count + 1;
1791#else
1792 ++lnum;
1793#endif
1794 }
1795 else
1796 {
1797 /* This line does not need updating, advance to the next one */
1798 row += wp->w_lines[idx++].wl_size;
1799 if (row > wp->w_height) /* past end of screen */
1800 break;
1801#ifdef FEAT_FOLDING
1802 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1803#else
1804 ++lnum;
1805#endif
1806#ifdef FEAT_SYN_HL
1807 did_update = DID_NONE;
1808#endif
1809 }
1810
1811 if (lnum > buf->b_ml.ml_line_count)
1812 {
1813 eof = TRUE;
1814 break;
1815 }
1816 }
1817 /*
1818 * End of loop over all window lines.
1819 */
1820
1821
1822 if (idx > wp->w_lines_valid)
1823 wp->w_lines_valid = idx;
1824
1825#ifdef FEAT_SYN_HL
1826 /*
1827 * Let the syntax stuff know we stop parsing here.
1828 */
1829 if (syntax_last_parsed != 0 && syntax_present(buf))
1830 syntax_end_parsing(syntax_last_parsed + 1);
1831#endif
1832
1833 /*
1834 * If we didn't hit the end of the file, and we didn't finish the last
1835 * line we were working on, then the line didn't fit.
1836 */
1837 wp->w_empty_rows = 0;
1838#ifdef FEAT_DIFF
1839 wp->w_filler_rows = 0;
1840#endif
1841 if (!eof && !didline)
1842 {
1843 if (lnum == wp->w_topline)
1844 {
1845 /*
1846 * Single line that does not fit!
1847 * Don't overwrite it, it can be edited.
1848 */
1849 wp->w_botline = lnum + 1;
1850 }
1851#ifdef FEAT_DIFF
1852 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1853 {
1854 /* Window ends in filler lines. */
1855 wp->w_botline = lnum;
1856 wp->w_filler_rows = wp->w_height - srow;
1857 }
1858#endif
1859 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1860 {
1861 /*
1862 * Last line isn't finished: Display "@@@" at the end.
1863 */
1864 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1865 W_WINROW(wp) + wp->w_height,
1866 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1867 '@', '@', hl_attr(HLF_AT));
1868 set_empty_rows(wp, srow);
1869 wp->w_botline = lnum;
1870 }
1871 else
1872 {
1873 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1874 wp->w_botline = lnum;
1875 }
1876 }
1877 else
1878 {
1879#ifdef FEAT_VERTSPLIT
1880 draw_vsep_win(wp, row);
1881#endif
1882 if (eof) /* we hit the end of the file */
1883 {
1884 wp->w_botline = buf->b_ml.ml_line_count + 1;
1885#ifdef FEAT_DIFF
1886 j = diff_check_fill(wp, wp->w_botline);
1887 if (j > 0 && !wp->w_botfill)
1888 {
1889 /*
1890 * Display filler lines at the end of the file
1891 */
1892 if (char2cells(fill_diff) > 1)
1893 i = '-';
1894 else
1895 i = fill_diff;
1896 if (row + j > wp->w_height)
1897 j = wp->w_height - row;
1898 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1899 row += j;
1900 }
1901#endif
1902 }
1903 else if (dollar_vcol == 0)
1904 wp->w_botline = lnum;
1905
1906 /* make sure the rest of the screen is blank */
1907 /* put '~'s on rows that aren't part of the file. */
1908 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1909 }
1910
1911 /* Reset the type of redrawing required, the window has been updated. */
1912 wp->w_redr_type = 0;
1913#ifdef FEAT_DIFF
1914 wp->w_old_topfill = wp->w_topfill;
1915 wp->w_old_botfill = wp->w_botfill;
1916#endif
1917
1918 if (dollar_vcol == 0)
1919 {
1920 /*
1921 * There is a trick with w_botline. If we invalidate it on each
1922 * change that might modify it, this will cause a lot of expensive
1923 * calls to plines() in update_topline() each time. Therefore the
1924 * value of w_botline is often approximated, and this value is used to
1925 * compute the value of w_topline. If the value of w_botline was
1926 * wrong, check that the value of w_topline is correct (cursor is on
1927 * the visible part of the text). If it's not, we need to redraw
1928 * again. Mostly this just means scrolling up a few lines, so it
1929 * doesn't look too bad. Only do this for the current window (where
1930 * changes are relevant).
1931 */
1932 wp->w_valid |= VALID_BOTLINE;
1933 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1934 {
1935 recursive = TRUE;
1936 curwin->w_valid &= ~VALID_TOPLINE;
1937 update_topline(); /* may invalidate w_botline again */
1938 if (must_redraw != 0)
1939 {
1940 /* Don't update for changes in buffer again. */
1941 i = curbuf->b_mod_set;
1942 curbuf->b_mod_set = FALSE;
1943 win_update(curwin);
1944 must_redraw = 0;
1945 curbuf->b_mod_set = i;
1946 }
1947 recursive = FALSE;
1948 }
1949 }
1950
1951#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1952 /* restore got_int, unless CTRL-C was hit while redrawing */
1953 if (!got_int)
1954 got_int = save_got_int;
1955#endif
1956}
1957
1958#ifdef FEAT_SIGNS
1959static int draw_signcolumn __ARGS((win_T *wp));
1960
1961/*
1962 * Return TRUE when window "wp" has a column to draw signs in.
1963 */
1964 static int
1965draw_signcolumn(wp)
1966 win_T *wp;
1967{
1968 return (wp->w_buffer->b_signlist != NULL
1969# ifdef FEAT_NETBEANS_INTG
1970 || usingNetbeans
1971# endif
1972 );
1973}
1974#endif
1975
1976/*
1977 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1978 * as the filler character.
1979 */
1980 static void
1981win_draw_end(wp, c1, c2, row, endrow, hl)
1982 win_T *wp;
1983 int c1;
1984 int c2;
1985 int row;
1986 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001987 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1990 int n = 0;
1991# define FDC_OFF n
1992#else
1993# define FDC_OFF 0
1994#endif
1995
1996#ifdef FEAT_RIGHTLEFT
1997 if (wp->w_p_rl)
1998 {
1999 /* No check for cmdline window: should never be right-left. */
2000# ifdef FEAT_FOLDING
2001 n = wp->w_p_fdc;
2002
2003 if (n > 0)
2004 {
2005 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002006 if (n > W_WIDTH(wp))
2007 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2009 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2010 ' ', ' ', hl_attr(HLF_FC));
2011 }
2012# endif
2013# ifdef FEAT_SIGNS
2014 if (draw_signcolumn(wp))
2015 {
2016 int nn = n + 2;
2017
2018 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002019 if (nn > W_WIDTH(wp))
2020 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2022 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2023 ' ', ' ', hl_attr(HLF_SC));
2024 n = nn;
2025 }
2026# endif
2027 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2028 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2029 c2, c2, hl_attr(hl));
2030 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2031 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2032 c1, c2, hl_attr(hl));
2033 }
2034 else
2035#endif
2036 {
2037#ifdef FEAT_CMDWIN
2038 if (cmdwin_type != 0 && wp == curwin)
2039 {
2040 /* draw the cmdline character in the leftmost column */
2041 n = 1;
2042 if (n > wp->w_width)
2043 n = wp->w_width;
2044 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2045 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2046 cmdwin_type, ' ', hl_attr(HLF_AT));
2047 }
2048#endif
2049#ifdef FEAT_FOLDING
2050 if (wp->w_p_fdc > 0)
2051 {
2052 int nn = n + wp->w_p_fdc;
2053
2054 /* draw the fold column at the left */
2055 if (nn > W_WIDTH(wp))
2056 nn = W_WIDTH(wp);
2057 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2058 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2059 ' ', ' ', hl_attr(HLF_FC));
2060 n = nn;
2061 }
2062#endif
2063#ifdef FEAT_SIGNS
2064 if (draw_signcolumn(wp))
2065 {
2066 int nn = n + 2;
2067
2068 /* draw the sign column after the fold column */
2069 if (nn > W_WIDTH(wp))
2070 nn = W_WIDTH(wp);
2071 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2072 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2073 ' ', ' ', hl_attr(HLF_SC));
2074 n = nn;
2075 }
2076#endif
2077 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2078 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2079 c1, c2, hl_attr(hl));
2080 }
2081 set_empty_rows(wp, row);
2082}
2083
2084#ifdef FEAT_FOLDING
2085/*
2086 * Display one folded line.
2087 */
2088 static void
2089fold_line(wp, fold_count, foldinfo, lnum, row)
2090 win_T *wp;
2091 long fold_count;
2092 foldinfo_T *foldinfo;
2093 linenr_T lnum;
2094 int row;
2095{
2096 char_u buf[51];
2097 pos_T *top, *bot;
2098 linenr_T lnume = lnum + fold_count - 1;
2099 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002100 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 int col;
2103 int txtcol;
2104 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002105 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
2107 /* Build the fold line:
2108 * 1. Add the cmdwin_type for the command-line window
2109 * 2. Add the 'foldcolumn'
2110 * 3. Add the 'number' column
2111 * 4. Compose the text
2112 * 5. Add the text
2113 * 6. set highlighting for the Visual area an other text
2114 */
2115 col = 0;
2116
2117 /*
2118 * 1. Add the cmdwin_type for the command-line window
2119 * Ignores 'rightleft', this window is never right-left.
2120 */
2121#ifdef FEAT_CMDWIN
2122 if (cmdwin_type != 0 && wp == curwin)
2123 {
2124 ScreenLines[off] = cmdwin_type;
2125 ScreenAttrs[off] = hl_attr(HLF_AT);
2126#ifdef FEAT_MBYTE
2127 if (enc_utf8)
2128 ScreenLinesUC[off] = 0;
2129#endif
2130 ++col;
2131 }
2132#endif
2133
2134 /*
2135 * 2. Add the 'foldcolumn'
2136 */
2137 fdc = wp->w_p_fdc;
2138 if (fdc > W_WIDTH(wp) - col)
2139 fdc = W_WIDTH(wp) - col;
2140 if (fdc > 0)
2141 {
2142 fill_foldcolumn(buf, wp, TRUE, lnum);
2143#ifdef FEAT_RIGHTLEFT
2144 if (wp->w_p_rl)
2145 {
2146 int i;
2147
2148 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2149 hl_attr(HLF_FC));
2150 /* reverse the fold column */
2151 for (i = 0; i < fdc; ++i)
2152 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2153 }
2154 else
2155#endif
2156 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2157 col += fdc;
2158 }
2159
2160#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002161# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2162 for (ri = 0; ri < l; ++ri) \
2163 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2164 else \
2165 for (ri = 0; ri < l; ++ri) \
2166 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002168# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2169 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170#endif
2171
2172 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002173 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
2175#ifdef FEAT_SIGNS
2176 /* If signs are being displayed, add two spaces. */
2177 if (draw_signcolumn(wp))
2178 {
2179 len = W_WIDTH(wp) - col;
2180 if (len > 0)
2181 {
2182 if (len > 2)
2183 len = 2;
2184# ifdef FEAT_RIGHTLEFT
2185 if (wp->w_p_rl)
2186 /* the line number isn't reversed */
2187 copy_text_attr(off + W_WIDTH(wp) - len - col,
2188 (char_u *)" ", len, hl_attr(HLF_FL));
2189 else
2190# endif
2191 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2192 col += len;
2193 }
2194 }
2195#endif
2196
2197 /*
2198 * 3. Add the 'number' column
2199 */
2200 if (wp->w_p_nu)
2201 {
2202 len = W_WIDTH(wp) - col;
2203 if (len > 0)
2204 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002205 int w = number_width(wp);
2206
2207 if (len > w + 1)
2208 len = w + 1;
2209 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#ifdef FEAT_RIGHTLEFT
2211 if (wp->w_p_rl)
2212 /* the line number isn't reversed */
2213 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2214 hl_attr(HLF_FL));
2215 else
2216#endif
2217 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2218 col += len;
2219 }
2220 }
2221
2222 /*
2223 * 4. Compose the folded-line string with 'foldtext', if set.
2224 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002225 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 txtcol = col; /* remember where text starts */
2228
2229 /*
2230 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2231 * Right-left text is put in columns 0 - number-col, normal text is put
2232 * in columns number-col - window-width.
2233 */
2234#ifdef FEAT_MBYTE
2235 if (has_mbyte)
2236 {
2237 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002238 int u8c, u8cc[MAX_MCO];
2239 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 int idx;
2241 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002242 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243# ifdef FEAT_ARABIC
2244 int prev_c = 0; /* previous Arabic character */
2245 int prev_c1 = 0; /* first composing char for prev_c */
2246# endif
2247
2248# ifdef FEAT_RIGHTLEFT
2249 if (wp->w_p_rl)
2250 idx = off;
2251 else
2252# endif
2253 idx = off + col;
2254
2255 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2256 for (p = text; *p != NUL; )
2257 {
2258 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002259 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if (col + cells > W_WIDTH(wp)
2261# ifdef FEAT_RIGHTLEFT
2262 - (wp->w_p_rl ? col : 0)
2263# endif
2264 )
2265 break;
2266 ScreenLines[idx] = *p;
2267 if (enc_utf8)
2268 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002269 u8c = utfc_ptr2char(p, u8cc);
2270 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 ScreenLinesUC[idx] = 0;
2273#ifdef FEAT_ARABIC
2274 prev_c = u8c;
2275#endif
2276 }
2277 else
2278 {
2279#ifdef FEAT_ARABIC
2280 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2281 {
2282 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002283 int pc, pc1, nc;
2284 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 int firstbyte = *p;
2286
2287 /* The idea of what is the previous and next
2288 * character depends on 'rightleft'. */
2289 if (wp->w_p_rl)
2290 {
2291 pc = prev_c;
2292 pc1 = prev_c1;
2293 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002294 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296 else
2297 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002298 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002300 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302 prev_c = u8c;
2303
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002304 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 pc, pc1, nc);
2306 ScreenLines[idx] = firstbyte;
2307 }
2308 else
2309 prev_c = u8c;
2310#endif
2311 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002312#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (u8c >= 0x10000)
2314 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2315 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002318 for (i = 0; i < Screen_mco; ++i)
2319 {
2320 ScreenLinesC[i][idx] = u8cc[i];
2321 if (u8cc[i] == 0)
2322 break;
2323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325 if (cells > 1)
2326 ScreenLines[idx + 1] = 0;
2327 }
2328 else if (cells > 1) /* double-byte character */
2329 {
2330 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2331 ScreenLines2[idx] = p[1];
2332 else
2333 ScreenLines[idx + 1] = p[1];
2334 }
2335 col += cells;
2336 idx += cells;
2337 p += c_len;
2338 }
2339 }
2340 else
2341#endif
2342 {
2343 len = (int)STRLEN(text);
2344 if (len > W_WIDTH(wp) - col)
2345 len = W_WIDTH(wp) - col;
2346 if (len > 0)
2347 {
2348#ifdef FEAT_RIGHTLEFT
2349 if (wp->w_p_rl)
2350 STRNCPY(current_ScreenLine, text, len);
2351 else
2352#endif
2353 STRNCPY(current_ScreenLine + col, text, len);
2354 col += len;
2355 }
2356 }
2357
2358 /* Fill the rest of the line with the fold filler */
2359#ifdef FEAT_RIGHTLEFT
2360 if (wp->w_p_rl)
2361 col -= txtcol;
2362#endif
2363 while (col < W_WIDTH(wp)
2364#ifdef FEAT_RIGHTLEFT
2365 - (wp->w_p_rl ? txtcol : 0)
2366#endif
2367 )
2368 {
2369#ifdef FEAT_MBYTE
2370 if (enc_utf8)
2371 {
2372 if (fill_fold >= 0x80)
2373 {
2374 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002375 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 else
2378 ScreenLinesUC[off + col] = 0;
2379 }
2380#endif
2381 ScreenLines[off + col++] = fill_fold;
2382 }
2383
2384 if (text != buf)
2385 vim_free(text);
2386
2387 /*
2388 * 6. set highlighting for the Visual area an other text.
2389 * If all folded lines are in the Visual area, highlight the line.
2390 */
2391#ifdef FEAT_VISUAL
2392 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2393 {
2394 if (ltoreq(curwin->w_cursor, VIsual))
2395 {
2396 /* Visual is after curwin->w_cursor */
2397 top = &curwin->w_cursor;
2398 bot = &VIsual;
2399 }
2400 else
2401 {
2402 /* Visual is before curwin->w_cursor */
2403 top = &VIsual;
2404 bot = &curwin->w_cursor;
2405 }
2406 if (lnum >= top->lnum
2407 && lnume <= bot->lnum
2408 && (VIsual_mode != 'v'
2409 || ((lnum > top->lnum
2410 || (lnum == top->lnum
2411 && top->col == 0))
2412 && (lnume < bot->lnum
2413 || (lnume == bot->lnum
2414 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002415 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 {
2417 if (VIsual_mode == Ctrl_V)
2418 {
2419 /* Visual block mode: highlight the chars part of the block */
2420 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2421 {
2422 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2423 len = wp->w_old_cursor_lcol;
2424 else
2425 len = W_WIDTH(wp) - txtcol;
2426 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002427 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 }
2430 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002433 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436 }
2437#endif
2438
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002439#ifdef FEAT_SYN_HL
2440 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002441 if (wp->w_p_cuc)
2442 {
2443 txtcol += wp->w_virtcol;
2444 if (wp->w_p_wrap)
2445 txtcol -= wp->w_skipcol;
2446 else
2447 txtcol -= wp->w_leftcol;
2448 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2449 ScreenAttrs[off + txtcol] = hl_combine_attr(
2450 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2451 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2455 (int)W_WIDTH(wp), FALSE);
2456
2457 /*
2458 * Update w_cline_height and w_cline_folded if the cursor line was
2459 * updated (saves a call to plines() later).
2460 */
2461 if (wp == curwin
2462 && lnum <= curwin->w_cursor.lnum
2463 && lnume >= curwin->w_cursor.lnum)
2464 {
2465 curwin->w_cline_row = row;
2466 curwin->w_cline_height = 1;
2467 curwin->w_cline_folded = TRUE;
2468 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2469 }
2470}
2471
2472/*
2473 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2474 */
2475 static void
2476copy_text_attr(off, buf, len, attr)
2477 int off;
2478 char_u *buf;
2479 int len;
2480 int attr;
2481{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002482 int i;
2483
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 mch_memmove(ScreenLines + off, buf, (size_t)len);
2485# ifdef FEAT_MBYTE
2486 if (enc_utf8)
2487 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2488# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002489 for (i = 0; i < len; ++i)
2490 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491}
2492
2493/*
2494 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002495 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 */
2497 static void
2498fill_foldcolumn(p, wp, closed, lnum)
2499 char_u *p;
2500 win_T *wp;
2501 int closed; /* TRUE of FALSE */
2502 linenr_T lnum; /* current line number */
2503{
2504 int i = 0;
2505 int level;
2506 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002507 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 /* Init to all spaces. */
2510 copy_spaces(p, (size_t)wp->w_p_fdc);
2511
2512 level = win_foldinfo.fi_level;
2513 if (level > 0)
2514 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002515 /* If there is only one column put more info in it. */
2516 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 /* If the column is too narrow, we start at the lowest level that
2519 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002520 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (first_level < 1)
2522 first_level = 1;
2523
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002524 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 if (win_foldinfo.fi_lnum == lnum
2527 && first_level + i >= win_foldinfo.fi_low_level)
2528 p[i] = '-';
2529 else if (first_level == 1)
2530 p[i] = '|';
2531 else if (first_level + i <= 9)
2532 p[i] = '0' + first_level + i;
2533 else
2534 p[i] = '>';
2535 if (first_level + i == level)
2536 break;
2537 }
2538 }
2539 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002540 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541}
2542#endif /* FEAT_FOLDING */
2543
2544/*
2545 * Display line "lnum" of window 'wp' on the screen.
2546 * Start at row "startrow", stop when "endrow" is reached.
2547 * wp->w_virtcol needs to be valid.
2548 *
2549 * Return the number of last row the line occupies.
2550 */
2551 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002552win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 win_T *wp;
2554 linenr_T lnum;
2555 int startrow;
2556 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002557 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
2559 int col; /* visual column on screen */
2560 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2561 int c = 0; /* init for GCC */
2562 long vcol = 0; /* virtual column (for tabs) */
2563 long vcol_prev = -1; /* "vcol" of previous character */
2564 char_u *line; /* current line */
2565 char_u *ptr; /* current position in "line" */
2566 int row; /* row in the window, excl w_winrow */
2567 int screen_row; /* row on the screen, incl w_winrow */
2568
2569 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2570 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002571 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 int c_extra = NUL; /* extra chars, all the same */
2573 int extra_attr = 0; /* attributes when n_extra != 0 */
2574 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2575 displaying lcs_eol at end-of-line */
2576 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2577 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2578
2579 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2580 int saved_n_extra = 0;
2581 char_u *saved_p_extra = NULL;
2582 int saved_c_extra = 0;
2583 int saved_char_attr = 0;
2584
2585 int n_attr = 0; /* chars with special attr */
2586 int saved_attr2 = 0; /* char_attr saved for n_attr */
2587 int n_attr3 = 0; /* chars with overruling special attr */
2588 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2589
2590 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2591
2592 int fromcol, tocol; /* start/end of inverting */
2593 int fromcol_prev = -2; /* start of inverting after cursor */
2594 int noinvcur = FALSE; /* don't invert the cursor */
2595#ifdef FEAT_VISUAL
2596 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002597 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598#endif
2599 pos_T pos;
2600 long v;
2601
2602 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002603 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2605 in this line */
2606 int attr = 0; /* attributes for area highlighting */
2607 int area_attr = 0; /* attributes desired by highlighting */
2608 int search_attr = 0; /* attributes desired by 'hlsearch' */
2609#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002610 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 int syntax_attr = 0; /* attributes desired by syntax */
2612 int has_syntax = FALSE; /* this buffer has syntax highl. */
2613 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002614 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002615#endif
2616#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002617 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002618# define SPWORDLEN 150
2619 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002620 int nextlinecol = 0; /* column where nextline[] starts */
2621 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002622 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002623 int spell_attr = 0; /* attributes desired by spelling */
2624 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002625 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2626 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002627 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002628 static int cap_col = -1; /* column to check for Cap word */
2629 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002630 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631#endif
2632 int extra_check; /* has syntax or linebreak */
2633#ifdef FEAT_MBYTE
2634 int multi_attr = 0; /* attributes desired by multibyte */
2635 int mb_l = 1; /* multi-byte byte length */
2636 int mb_c = 0; /* decoded multi-byte character */
2637 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002638 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639#endif
2640#ifdef FEAT_DIFF
2641 int filler_lines; /* nr of filler lines to be drawn */
2642 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002643 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 int change_start = MAXCOL; /* first col of changed area */
2645 int change_end = -1; /* last col of changed area */
2646#endif
2647 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2648#ifdef FEAT_LINEBREAK
2649 int need_showbreak = FALSE;
2650#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002651#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2652 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002654 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655#endif
2656#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002657 matchitem_T *cur; /* points to the match list */
2658 match_T *shl; /* points to search_hl or a match */
2659 int shl_flag; /* flag to indicate whether search_hl
2660 has been processed or not */
2661 int prevcol_hl_flag; /* flag to indicate whether prevcol
2662 equals startcol of search_hl or one
2663 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665#ifdef FEAT_ARABIC
2666 int prev_c = 0; /* previous Arabic character */
2667 int prev_c1 = 0; /* first composing char for prev_c */
2668#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002669#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002670 int did_line_attr = 0;
2671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
2673 /* draw_state: items that are drawn in sequence: */
2674#define WL_START 0 /* nothing done yet */
2675#ifdef FEAT_CMDWIN
2676# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2677#else
2678# define WL_CMDLINE WL_START
2679#endif
2680#ifdef FEAT_FOLDING
2681# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2682#else
2683# define WL_FOLD WL_CMDLINE
2684#endif
2685#ifdef FEAT_SIGNS
2686# define WL_SIGN WL_FOLD + 1 /* column for signs */
2687#else
2688# define WL_SIGN WL_FOLD /* column for signs */
2689#endif
2690#define WL_NR WL_SIGN + 1 /* line number */
2691#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2692# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2693#else
2694# define WL_SBR WL_NR
2695#endif
2696#define WL_LINE WL_SBR + 1 /* text in the line */
2697 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002698#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 int feedback_col = 0;
2700 int feedback_old_attr = -1;
2701#endif
2702
2703
2704 if (startrow > endrow) /* past the end already! */
2705 return startrow;
2706
2707 row = startrow;
2708 screen_row = row + W_WINROW(wp);
2709
2710 /*
2711 * To speed up the loop below, set extra_check when there is linebreak,
2712 * trailing white space and/or syntax processing to be done.
2713 */
2714#ifdef FEAT_LINEBREAK
2715 extra_check = wp->w_p_lbr;
2716#else
2717 extra_check = 0;
2718#endif
2719#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002720 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 /* Prepare for syntax highlighting in this line. When there is an
2723 * error, stop syntax highlighting. */
2724 save_did_emsg = did_emsg;
2725 did_emsg = FALSE;
2726 syntax_start(wp, lnum);
2727 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002728 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 else
2730 {
2731 did_emsg = save_did_emsg;
2732 has_syntax = TRUE;
2733 extra_check = TRUE;
2734 }
2735 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002736#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002737
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002738#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002739 if (wp->w_p_spell
2740 && *wp->w_buffer->b_p_spl != NUL
2741 && wp->w_buffer->b_langp.ga_len > 0
2742 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002743 {
2744 /* Prepare for spell checking. */
2745 has_spell = TRUE;
2746 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002747
2748 /* Get the start of the next line, so that words that wrap to the next
2749 * line are found too: "et<line-break>al.".
2750 * Trick: skip a few chars for C/shell/Vim comments */
2751 nextline[SPWORDLEN] = NUL;
2752 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2753 {
2754 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2755 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2756 }
2757
2758 /* When a word wrapped from the previous line the start of the current
2759 * line is valid. */
2760 if (lnum == checked_lnum)
2761 cur_checked_col = checked_col;
2762 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002763
2764 /* When there was a sentence end in the previous line may require a
2765 * word starting with capital in this line. In line 1 always check
2766 * the first word. */
2767 if (lnum != capcol_lnum)
2768 cap_col = -1;
2769 if (lnum == 1)
2770 cap_col = 0;
2771 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#endif
2774
2775 /*
2776 * handle visual active in this window
2777 */
2778 fromcol = -10;
2779 tocol = MAXCOL;
2780#ifdef FEAT_VISUAL
2781 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2782 {
2783 /* Visual is after curwin->w_cursor */
2784 if (ltoreq(curwin->w_cursor, VIsual))
2785 {
2786 top = &curwin->w_cursor;
2787 bot = &VIsual;
2788 }
2789 else /* Visual is before curwin->w_cursor */
2790 {
2791 top = &VIsual;
2792 bot = &curwin->w_cursor;
2793 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002794 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (VIsual_mode == Ctrl_V) /* block mode */
2796 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002797 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
2799 fromcol = wp->w_old_cursor_fcol;
2800 tocol = wp->w_old_cursor_lcol;
2801 }
2802 }
2803 else /* non-block mode */
2804 {
2805 if (lnum > top->lnum && lnum <= bot->lnum)
2806 fromcol = 0;
2807 else if (lnum == top->lnum)
2808 {
2809 if (VIsual_mode == 'V') /* linewise */
2810 fromcol = 0;
2811 else
2812 {
2813 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2814 if (gchar_pos(top) == NUL)
2815 tocol = fromcol + 1;
2816 }
2817 }
2818 if (VIsual_mode != 'V' && lnum == bot->lnum)
2819 {
2820 if (*p_sel == 'e' && bot->col == 0
2821#ifdef FEAT_VIRTUALEDIT
2822 && bot->coladd == 0
2823#endif
2824 )
2825 {
2826 fromcol = -10;
2827 tocol = MAXCOL;
2828 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002829 else if (bot->col == MAXCOL)
2830 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 else
2832 {
2833 pos = *bot;
2834 if (*p_sel == 'e')
2835 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2836 else
2837 {
2838 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2839 ++tocol;
2840 }
2841 }
2842 }
2843 }
2844
2845#ifndef MSDOS
2846 /* Check if the character under the cursor should not be inverted */
2847 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2848# ifdef FEAT_GUI
2849 && !gui.in_use
2850# endif
2851 )
2852 noinvcur = TRUE;
2853#endif
2854
2855 /* if inverting in this line set area_highlighting */
2856 if (fromcol >= 0)
2857 {
2858 area_highlighting = TRUE;
2859 attr = hl_attr(HLF_V);
2860#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2861 if (clip_star.available && !clip_star.owned && clip_isautosel())
2862 attr = hl_attr(HLF_VNC);
2863#endif
2864 }
2865 }
2866
2867 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002868 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
2870 else
2871#endif /* FEAT_VISUAL */
2872 if (highlight_match
2873 && wp == curwin
2874 && lnum >= curwin->w_cursor.lnum
2875 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2876 {
2877 if (lnum == curwin->w_cursor.lnum)
2878 getvcol(curwin, &(curwin->w_cursor),
2879 (colnr_T *)&fromcol, NULL, NULL);
2880 else
2881 fromcol = 0;
2882 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2883 {
2884 pos.lnum = lnum;
2885 pos.col = search_match_endcol;
2886 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2887 }
2888 else
2889 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00002890 /* do at least one character; happens when past end of line */
2891 if (fromcol == tocol)
2892 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 area_highlighting = TRUE;
2894 attr = hl_attr(HLF_I);
2895 }
2896
2897#ifdef FEAT_DIFF
2898 filler_lines = diff_check(wp, lnum);
2899 if (filler_lines < 0)
2900 {
2901 if (filler_lines == -1)
2902 {
2903 if (diff_find_change(wp, lnum, &change_start, &change_end))
2904 diff_hlf = HLF_ADD; /* added line */
2905 else if (change_start == 0)
2906 diff_hlf = HLF_TXD; /* changed text */
2907 else
2908 diff_hlf = HLF_CHD; /* changed line */
2909 }
2910 else
2911 diff_hlf = HLF_ADD; /* added line */
2912 filler_lines = 0;
2913 area_highlighting = TRUE;
2914 }
2915 if (lnum == wp->w_topline)
2916 filler_lines = wp->w_topfill;
2917 filler_todo = filler_lines;
2918#endif
2919
2920#ifdef LINE_ATTR
2921# ifdef FEAT_SIGNS
2922 /* If this line has a sign with line highlighting set line_attr. */
2923 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2924 if (v != 0)
2925 line_attr = sign_get_attr((int)v, TRUE);
2926# endif
2927# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2928 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002929 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 line_attr = hl_attr(HLF_L);
2931# endif
2932 if (line_attr != 0)
2933 area_highlighting = TRUE;
2934#endif
2935
2936 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2937 ptr = line;
2938
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002939#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002940 if (has_spell)
2941 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002942 /* For checking first word with a capital skip white space. */
2943 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002944 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002945
Bram Moolenaar30abd282005-06-22 22:35:10 +00002946 /* To be able to spell-check over line boundaries copy the end of the
2947 * current line into nextline[]. Above the start of the next line was
2948 * copied to nextline[SPWORDLEN]. */
2949 if (nextline[SPWORDLEN] == NUL)
2950 {
2951 /* No next line or it is empty. */
2952 nextlinecol = MAXCOL;
2953 nextline_idx = 0;
2954 }
2955 else
2956 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002957 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002958 if (v < SPWORDLEN)
2959 {
2960 /* Short line, use it completely and append the start of the
2961 * next line. */
2962 nextlinecol = 0;
2963 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00002964 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002965 nextline_idx = v + 1;
2966 }
2967 else
2968 {
2969 /* Long line, use only the last SPWORDLEN bytes. */
2970 nextlinecol = v - SPWORDLEN;
2971 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2972 nextline_idx = SPWORDLEN + 1;
2973 }
2974 }
2975 }
2976#endif
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /* find start of trailing whitespace */
2979 if (wp->w_p_list && lcs_trail)
2980 {
2981 trailcol = (colnr_T)STRLEN(ptr);
2982 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2983 --trailcol;
2984 trailcol += (colnr_T) (ptr - line);
2985 extra_check = TRUE;
2986 }
2987
2988 /*
2989 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2990 * first character to be displayed.
2991 */
2992 if (wp->w_p_wrap)
2993 v = wp->w_skipcol;
2994 else
2995 v = wp->w_leftcol;
2996 if (v > 0)
2997 {
2998#ifdef FEAT_MBYTE
2999 char_u *prev_ptr = ptr;
3000#endif
3001 while (vcol < v && *ptr != NUL)
3002 {
3003 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3004 vcol += c;
3005#ifdef FEAT_MBYTE
3006 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003008 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003011#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3012 /* When:
3013 * - 'cuc' is set, or
3014 * - 'virtualedit' is set, or
3015 * - the visual mode is active,
3016 * the end of the line may be before the start of the displayed part.
3017 */
3018 if (vcol < v && (
3019# ifdef FEAT_SYN_HL
3020 wp->w_p_cuc
3021# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3022 ||
3023# endif
3024# endif
3025# ifdef FEAT_VIRTUALEDIT
3026 virtual_active()
3027# ifdef FEAT_VISUAL
3028 ||
3029# endif
3030# endif
3031# ifdef FEAT_VISUAL
3032 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3033# endif
3034 ))
3035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#endif
3039
3040 /* Handle a character that's not completely on the screen: Put ptr at
3041 * that character but skip the first few screen characters. */
3042 if (vcol > v)
3043 {
3044 vcol -= c;
3045#ifdef FEAT_MBYTE
3046 ptr = prev_ptr;
3047#else
3048 --ptr;
3049#endif
3050 n_skip = v - vcol;
3051 }
3052
3053 /*
3054 * Adjust for when the inverted text is before the screen,
3055 * and when the start of the inverted text is before the screen.
3056 */
3057 if (tocol <= vcol)
3058 fromcol = 0;
3059 else if (fromcol >= 0 && fromcol < vcol)
3060 fromcol = vcol;
3061
3062#ifdef FEAT_LINEBREAK
3063 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3064 if (wp->w_p_wrap)
3065 need_showbreak = TRUE;
3066#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003067#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003068 /* When spell checking a word we need to figure out the start of the
3069 * word and if it's badly spelled or not. */
3070 if (has_spell)
3071 {
3072 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003073 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003074 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003075
3076 pos = wp->w_cursor;
3077 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003078 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003079 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003080
3081 /* spell_move_to() may call ml_get() and make "line" invalid */
3082 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3083 ptr = line + linecol;
3084
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003085 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003086 {
3087 /* no bad word found at line start, don't check until end of a
3088 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003089 spell_hlf = HLF_COUNT;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003090 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3091 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003092 }
3093 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003094 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003095 /* bad word found, use attributes until end of word */
3096 word_end = wp->w_cursor.col + len + 1;
3097
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003098 /* Turn index into actual attributes. */
3099 if (spell_hlf != HLF_COUNT)
3100 spell_attr = highlight_attr[spell_hlf];
3101 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003102 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003103
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003104# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003105 /* Need to restart syntax highlighting for this line. */
3106 if (has_syntax)
3107 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003108# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003109 }
3110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112
3113 /*
3114 * Correct highlighting for cursor that can't be disabled.
3115 * Avoids having to check this for each character.
3116 */
3117 if (fromcol >= 0)
3118 {
3119 if (noinvcur)
3120 {
3121 if ((colnr_T)fromcol == wp->w_virtcol)
3122 {
3123 /* highlighting starts at cursor, let it start just after the
3124 * cursor */
3125 fromcol_prev = fromcol;
3126 fromcol = -1;
3127 }
3128 else if ((colnr_T)fromcol < wp->w_virtcol)
3129 /* restart highlighting after the cursor */
3130 fromcol_prev = wp->w_virtcol;
3131 }
3132 if (fromcol >= tocol)
3133 fromcol = -1;
3134 }
3135
3136#ifdef FEAT_SEARCH_EXTRA
3137 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003138 * Handle highlighting the last used search pattern and matches.
3139 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003141 cur = wp->w_match_head;
3142 shl_flag = FALSE;
3143 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003145 if (shl_flag == FALSE)
3146 {
3147 shl = &search_hl;
3148 shl_flag = TRUE;
3149 }
3150 else
3151 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003152 shl->startcol = MAXCOL;
3153 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 shl->attr_cur = 0;
3155 if (shl->rm.regprog != NULL)
3156 {
3157 v = (long)(ptr - line);
3158 next_search_hl(wp, shl, lnum, (colnr_T)v);
3159
3160 /* Need to get the line again, a multi-line regexp may have made it
3161 * invalid. */
3162 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3163 ptr = line + v;
3164
3165 if (shl->lnum != 0 && shl->lnum <= lnum)
3166 {
3167 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003168 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003170 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3172 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003173 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003175 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003177 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003180 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003181 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 else
3183#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003184 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003186 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 shl->attr_cur = shl->attr;
3189 search_attr = shl->attr;
3190 }
3191 area_highlighting = TRUE;
3192 }
3193 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003194 if (shl != &search_hl && cur != NULL)
3195 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197#endif
3198
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003199#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003200 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3201 * active, because it's not clear what is selected then. */
3202 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003203 {
3204 line_attr = hl_attr(HLF_CUL);
3205 area_highlighting = TRUE;
3206 }
3207#endif
3208
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003209 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 col = 0;
3211#ifdef FEAT_RIGHTLEFT
3212 if (wp->w_p_rl)
3213 {
3214 /* Rightleft window: process the text in the normal direction, but put
3215 * it in current_ScreenLine[] from right to left. Start at the
3216 * rightmost column of the window. */
3217 col = W_WIDTH(wp) - 1;
3218 off += col;
3219 }
3220#endif
3221
3222 /*
3223 * Repeat for the whole displayed line.
3224 */
3225 for (;;)
3226 {
3227 /* Skip this quickly when working on the text. */
3228 if (draw_state != WL_LINE)
3229 {
3230#ifdef FEAT_CMDWIN
3231 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3232 {
3233 draw_state = WL_CMDLINE;
3234 if (cmdwin_type != 0 && wp == curwin)
3235 {
3236 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003238 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 char_attr = hl_attr(HLF_AT);
3240 }
3241 }
3242#endif
3243
3244#ifdef FEAT_FOLDING
3245 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3246 {
3247 draw_state = WL_FOLD;
3248 if (wp->w_p_fdc > 0)
3249 {
3250 /* Draw the 'foldcolumn'. */
3251 fill_foldcolumn(extra, wp, FALSE, lnum);
3252 n_extra = wp->w_p_fdc;
3253 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003254 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 c_extra = NUL;
3256 char_attr = hl_attr(HLF_FC);
3257 }
3258 }
3259#endif
3260
3261#ifdef FEAT_SIGNS
3262 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3263 {
3264 draw_state = WL_SIGN;
3265 /* Show the sign column when there are any signs in this
3266 * buffer or when using Netbeans. */
3267 if (draw_signcolumn(wp)
3268# ifdef FEAT_DIFF
3269 && filler_todo <= 0
3270# endif
3271 )
3272 {
3273 int_u text_sign;
3274# ifdef FEAT_SIGN_ICONS
3275 int_u icon_sign;
3276# endif
3277
3278 /* Draw two cells with the sign value or blank. */
3279 c_extra = ' ';
3280 char_attr = hl_attr(HLF_SC);
3281 n_extra = 2;
3282
3283 if (row == startrow)
3284 {
3285 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3286 SIGN_TEXT);
3287# ifdef FEAT_SIGN_ICONS
3288 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3289 SIGN_ICON);
3290 if (gui.in_use && icon_sign != 0)
3291 {
3292 /* Use the image in this position. */
3293 c_extra = SIGN_BYTE;
3294# ifdef FEAT_NETBEANS_INTG
3295 if (buf_signcount(wp->w_buffer, lnum) > 1)
3296 c_extra = MULTISIGN_BYTE;
3297# endif
3298 char_attr = icon_sign;
3299 }
3300 else
3301# endif
3302 if (text_sign != 0)
3303 {
3304 p_extra = sign_get_text(text_sign);
3305 if (p_extra != NULL)
3306 {
3307 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003308 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
3310 char_attr = sign_get_attr(text_sign, FALSE);
3311 }
3312 }
3313 }
3314 }
3315#endif
3316
3317 if (draw_state == WL_NR - 1 && n_extra == 0)
3318 {
3319 draw_state = WL_NR;
3320 /* Display the line number. After the first fill with blanks
3321 * when the 'n' flag isn't in 'cpo' */
3322 if (wp->w_p_nu
3323 && (row == startrow
3324#ifdef FEAT_DIFF
3325 + filler_lines
3326#endif
3327 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3328 {
3329 /* Draw the line number (empty space after wrapping). */
3330 if (row == startrow
3331#ifdef FEAT_DIFF
3332 + filler_lines
3333#endif
3334 )
3335 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003336 sprintf((char *)extra, "%*ld ",
3337 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (wp->w_skipcol > 0)
3339 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3340 *p_extra = '-';
3341#ifdef FEAT_RIGHTLEFT
3342 if (wp->w_p_rl) /* reverse line numbers */
3343 rl_mirror(extra);
3344#endif
3345 p_extra = extra;
3346 c_extra = NUL;
3347 }
3348 else
3349 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003350 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003352#ifdef FEAT_SYN_HL
3353 /* When 'cursorline' is set highlight the line number of
3354 * the current line differently. */
3355 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3356 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359 }
3360
3361#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3362 if (draw_state == WL_SBR - 1 && n_extra == 0)
3363 {
3364 draw_state = WL_SBR;
3365# ifdef FEAT_DIFF
3366 if (filler_todo > 0)
3367 {
3368 /* Draw "deleted" diff line(s). */
3369 if (char2cells(fill_diff) > 1)
3370 c_extra = '-';
3371 else
3372 c_extra = fill_diff;
3373# ifdef FEAT_RIGHTLEFT
3374 if (wp->w_p_rl)
3375 n_extra = col + 1;
3376 else
3377# endif
3378 n_extra = W_WIDTH(wp) - col;
3379 char_attr = hl_attr(HLF_DED);
3380 }
3381# endif
3382# ifdef FEAT_LINEBREAK
3383 if (*p_sbr != NUL && need_showbreak)
3384 {
3385 /* Draw 'showbreak' at the start of each broken line. */
3386 p_extra = p_sbr;
3387 c_extra = NUL;
3388 n_extra = (int)STRLEN(p_sbr);
3389 char_attr = hl_attr(HLF_AT);
3390 need_showbreak = FALSE;
3391 /* Correct end of highlighted area for 'showbreak',
3392 * required when 'linebreak' is also set. */
3393 if (tocol == vcol)
3394 tocol += n_extra;
3395 }
3396# endif
3397 }
3398#endif
3399
3400 if (draw_state == WL_LINE - 1 && n_extra == 0)
3401 {
3402 draw_state = WL_LINE;
3403 if (saved_n_extra)
3404 {
3405 /* Continue item from end of wrapped line. */
3406 n_extra = saved_n_extra;
3407 c_extra = saved_c_extra;
3408 p_extra = saved_p_extra;
3409 char_attr = saved_char_attr;
3410 }
3411 else
3412 char_attr = 0;
3413 }
3414 }
3415
3416 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003417 if (dollar_vcol != 0 && wp == curwin
3418 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419#ifdef FEAT_DIFF
3420 && filler_todo <= 0
3421#endif
3422 )
3423 {
3424 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3425 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003426 /* Pretend we have finished updating the window. Except when
3427 * 'cursorcolumn' is set. */
3428#ifdef FEAT_SYN_HL
3429 if (wp->w_p_cuc)
3430 row = wp->w_cline_row + wp->w_cline_height;
3431 else
3432#endif
3433 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 break;
3435 }
3436
3437 if (draw_state == WL_LINE && area_highlighting)
3438 {
3439 /* handle Visual or match highlighting in this line */
3440 if (vcol == fromcol
3441#ifdef FEAT_MBYTE
3442 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3443 && (*mb_ptr2cells)(ptr) > 1)
3444#endif
3445 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003446 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 && vcol < tocol))
3448 area_attr = attr; /* start highlighting */
3449 else if (area_attr != 0
3450 && (vcol == tocol
3451 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454#ifdef FEAT_SEARCH_EXTRA
3455 if (!n_extra)
3456 {
3457 /*
3458 * Check for start/end of search pattern match.
3459 * After end, check for start/end of next match.
3460 * When another match, have to check for start again.
3461 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003462 * Do this for 'search_hl' and the match list (ordered by
3463 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003465 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003466 cur = wp->w_match_head;
3467 shl_flag = FALSE;
3468 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003470 if (shl_flag == FALSE
3471 && ((cur != NULL
3472 && cur->priority > SEARCH_HL_PRIORITY)
3473 || cur == NULL))
3474 {
3475 shl = &search_hl;
3476 shl_flag = TRUE;
3477 }
3478 else
3479 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 while (shl->rm.regprog != NULL)
3481 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003482 if (shl->startcol != MAXCOL
3483 && v >= (long)shl->startcol
3484 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
3486 shl->attr_cur = shl->attr;
3487 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003488 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
3490 shl->attr_cur = 0;
3491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 next_search_hl(wp, shl, lnum, (colnr_T)v);
3493
3494 /* Need to get the line again, a multi-line regexp
3495 * may have made it invalid. */
3496 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3497 ptr = line + v;
3498
3499 if (shl->lnum == lnum)
3500 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003501 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003503 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003505 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003507 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 /* highlight empty match, try again after
3510 * it */
3511#ifdef FEAT_MBYTE
3512 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003513 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003514 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 else
3516#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003517 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519
3520 /* Loop to check if the match starts at the
3521 * current position */
3522 continue;
3523 }
3524 }
3525 break;
3526 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003527 if (shl != &search_hl && cur != NULL)
3528 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003530
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003531 /* Use attributes from match with highest priority among
3532 * 'search_hl' and the match list. */
3533 search_attr = search_hl.attr_cur;
3534 cur = wp->w_match_head;
3535 shl_flag = FALSE;
3536 while (cur != NULL || shl_flag == FALSE)
3537 {
3538 if (shl_flag == FALSE
3539 && ((cur != NULL
3540 && cur->priority > SEARCH_HL_PRIORITY)
3541 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003542 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003543 shl = &search_hl;
3544 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003545 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003546 else
3547 shl = &cur->hl;
3548 if (shl->attr_cur != 0)
3549 search_attr = shl->attr_cur;
3550 if (shl != &search_hl && cur != NULL)
3551 cur = cur->next;
3552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554#endif
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003557 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003559 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3560 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003562 if (diff_hlf == HLF_TXD && ptr - line > change_end
3563 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003565 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003568
3569 /* Decide which of the highlight attributes to use. */
3570 attr_pri = TRUE;
3571 if (area_attr != 0)
3572 char_attr = area_attr;
3573 else if (search_attr != 0)
3574 char_attr = search_attr;
3575#ifdef LINE_ATTR
3576 /* Use line_attr when not in the Visual or 'incsearch' area
3577 * (area_attr may be 0 when "noinvcur" is set). */
3578 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003579 || vcol < fromcol || vcol_prev < fromcol_prev
3580 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003581 char_attr = line_attr;
3582#endif
3583 else
3584 {
3585 attr_pri = FALSE;
3586#ifdef FEAT_SYN_HL
3587 if (has_syntax)
3588 char_attr = syntax_attr;
3589 else
3590#endif
3591 char_attr = 0;
3592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
3594
3595 /*
3596 * Get the next character to put on the screen.
3597 */
3598 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003599 * The "p_extra" points to the extra stuff that is inserted to
3600 * represent special characters (non-printable stuff) and other
3601 * things. When all characters are the same, c_extra is used.
3602 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3603 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3605 */
3606 if (n_extra > 0)
3607 {
3608 if (c_extra != NUL)
3609 {
3610 c = c_extra;
3611#ifdef FEAT_MBYTE
3612 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3613 if (enc_utf8 && (*mb_char2len)(c) > 1)
3614 {
3615 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003616 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003617 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 else
3620 mb_utf8 = FALSE;
3621#endif
3622 }
3623 else
3624 {
3625 c = *p_extra;
3626#ifdef FEAT_MBYTE
3627 if (has_mbyte)
3628 {
3629 mb_c = c;
3630 if (enc_utf8)
3631 {
3632 /* If the UTF-8 character is more than one byte:
3633 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003634 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 mb_utf8 = FALSE;
3636 if (mb_l > n_extra)
3637 mb_l = 1;
3638 else if (mb_l > 1)
3639 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003640 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003642 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 }
3645 else
3646 {
3647 /* if this is a DBCS character, put it in "mb_c" */
3648 mb_l = MB_BYTE2LEN(c);
3649 if (mb_l >= n_extra)
3650 mb_l = 1;
3651 else if (mb_l > 1)
3652 mb_c = (c << 8) + p_extra[1];
3653 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003654 if (mb_l == 0) /* at the NUL at end-of-line */
3655 mb_l = 1;
3656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 /* If a double-width char doesn't fit display a '>' in the
3658 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003659 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660# ifdef FEAT_RIGHTLEFT
3661 wp->w_p_rl ? (col <= 0) :
3662# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003663 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 && (*mb_char2cells)(mb_c) == 2)
3665 {
3666 c = '>';
3667 mb_c = c;
3668 mb_l = 1;
3669 mb_utf8 = FALSE;
3670 multi_attr = hl_attr(HLF_AT);
3671 /* put the pointer back to output the double-width
3672 * character at the start of the next line. */
3673 ++n_extra;
3674 --p_extra;
3675 }
3676 else
3677 {
3678 n_extra -= mb_l - 1;
3679 p_extra += mb_l - 1;
3680 }
3681 }
3682#endif
3683 ++p_extra;
3684 }
3685 --n_extra;
3686 }
3687 else
3688 {
3689 /*
3690 * Get a character from the line itself.
3691 */
3692 c = *ptr;
3693#ifdef FEAT_MBYTE
3694 if (has_mbyte)
3695 {
3696 mb_c = c;
3697 if (enc_utf8)
3698 {
3699 /* If the UTF-8 character is more than one byte: Decode it
3700 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003701 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 mb_utf8 = FALSE;
3703 if (mb_l > 1)
3704 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003705 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 /* Overlong encoded ASCII or ASCII with composing char
3707 * is displayed normally, except a NUL. */
3708 if (mb_c < 0x80)
3709 c = mb_c;
3710 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003711
3712 /* At start of the line we can have a composing char.
3713 * Draw it as a space with a composing char. */
3714 if (utf_iscomposing(mb_c))
3715 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003716 int i;
3717
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003718 for (i = Screen_mco - 1; i > 0; --i)
3719 u8cc[i] = u8cc[i - 1];
3720 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003721 mb_c = ' ';
3722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724
3725 if ((mb_l == 1 && c >= 0x80)
3726 || (mb_l >= 1 && mb_c == 0)
3727 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003728# ifdef UNICODE16
3729 || mb_c >= 0x10000
3730# endif
3731 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
3733 /*
3734 * Illegal UTF-8 byte: display as <xx>.
3735 * Non-BMP character : display as ? or fullwidth ?.
3736 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003737# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
3741 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003742# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (wp->w_p_rl) /* reverse */
3744 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003747# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 else if (utf_char2cells(mb_c) != 2)
3749 STRCPY(extra, "?");
3750 else
3751 /* 0xff1f in UTF-8: full-width '?' */
3752 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003753# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
3755 p_extra = extra;
3756 c = *p_extra;
3757 mb_c = mb_ptr2char_adv(&p_extra);
3758 mb_utf8 = (c >= 0x80);
3759 n_extra = (int)STRLEN(p_extra);
3760 c_extra = NUL;
3761 if (area_attr == 0 && search_attr == 0)
3762 {
3763 n_attr = n_extra + 1;
3764 extra_attr = hl_attr(HLF_8);
3765 saved_attr2 = char_attr; /* save current attr */
3766 }
3767 }
3768 else if (mb_l == 0) /* at the NUL at end-of-line */
3769 mb_l = 1;
3770#ifdef FEAT_ARABIC
3771 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3772 {
3773 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003774 int pc, pc1, nc;
3775 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 /* The idea of what is the previous and next
3778 * character depends on 'rightleft'. */
3779 if (wp->w_p_rl)
3780 {
3781 pc = prev_c;
3782 pc1 = prev_c1;
3783 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003784 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 else
3787 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003788 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003790 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 prev_c = mb_c;
3793
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003794 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796 else
3797 prev_c = mb_c;
3798#endif
3799 }
3800 else /* enc_dbcs */
3801 {
3802 mb_l = MB_BYTE2LEN(c);
3803 if (mb_l == 0) /* at the NUL at end-of-line */
3804 mb_l = 1;
3805 else if (mb_l > 1)
3806 {
3807 /* We assume a second byte below 32 is illegal.
3808 * Hopefully this is OK for all double-byte encodings!
3809 */
3810 if (ptr[1] >= 32)
3811 mb_c = (c << 8) + ptr[1];
3812 else
3813 {
3814 if (ptr[1] == NUL)
3815 {
3816 /* head byte at end of line */
3817 mb_l = 1;
3818 transchar_nonprint(extra, c);
3819 }
3820 else
3821 {
3822 /* illegal tail byte */
3823 mb_l = 2;
3824 STRCPY(extra, "XX");
3825 }
3826 p_extra = extra;
3827 n_extra = (int)STRLEN(extra) - 1;
3828 c_extra = NUL;
3829 c = *p_extra++;
3830 if (area_attr == 0 && search_attr == 0)
3831 {
3832 n_attr = n_extra + 1;
3833 extra_attr = hl_attr(HLF_8);
3834 saved_attr2 = char_attr; /* save current attr */
3835 }
3836 mb_c = c;
3837 }
3838 }
3839 }
3840 /* If a double-width char doesn't fit display a '>' in the
3841 * last column; the character is displayed at the start of the
3842 * next line. */
3843 if ((
3844# ifdef FEAT_RIGHTLEFT
3845 wp->w_p_rl ? (col <= 0) :
3846# endif
3847 (col >= W_WIDTH(wp) - 1))
3848 && (*mb_char2cells)(mb_c) == 2)
3849 {
3850 c = '>';
3851 mb_c = c;
3852 mb_utf8 = FALSE;
3853 mb_l = 1;
3854 multi_attr = hl_attr(HLF_AT);
3855 /* Put pointer back so that the character will be
3856 * displayed at the start of the next line. */
3857 --ptr;
3858 }
3859 else if (*ptr != NUL)
3860 ptr += mb_l - 1;
3861
3862 /* If a double-width char doesn't fit at the left side display
3863 * a '<' in the first column. */
3864 if (n_skip > 0 && mb_l > 1)
3865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003867 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 c = ' ';
3869 if (area_attr == 0 && search_attr == 0)
3870 {
3871 n_attr = n_extra + 1;
3872 extra_attr = hl_attr(HLF_AT);
3873 saved_attr2 = char_attr; /* save current attr */
3874 }
3875 mb_c = c;
3876 mb_utf8 = FALSE;
3877 mb_l = 1;
3878 }
3879
3880 }
3881#endif
3882 ++ptr;
3883
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003884 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003885 if (wp->w_p_list && (c == 160
3886#ifdef FEAT_MBYTE
3887 || (mb_utf8 && mb_c == 160)
3888#endif
3889 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003890 {
3891 c = lcs_nbsp;
3892 if (area_attr == 0 && search_attr == 0)
3893 {
3894 n_attr = 1;
3895 extra_attr = hl_attr(HLF_8);
3896 saved_attr2 = char_attr; /* save current attr */
3897 }
3898#ifdef FEAT_MBYTE
3899 mb_c = c;
3900 if (enc_utf8 && (*mb_char2len)(c) > 1)
3901 {
3902 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003903 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003904 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003905 }
3906 else
3907 mb_utf8 = FALSE;
3908#endif
3909 }
3910
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (extra_check)
3912 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003913#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003914 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003915#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003916
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003917#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 /* Get syntax attribute, unless still at the start of the line
3919 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003920 v = (long)(ptr - line);
3921 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
3923 /* Get the syntax attribute for the character. If there
3924 * is an error, disable syntax highlighting. */
3925 save_did_emsg = did_emsg;
3926 did_emsg = FALSE;
3927
Bram Moolenaar217ad922005-03-20 22:37:15 +00003928 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003929# ifdef FEAT_SPELL
3930 has_spell ? &can_spell :
3931# endif
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00003932 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003935 {
3936 wp->w_buffer->b_syn_error = TRUE;
3937 has_syntax = FALSE;
3938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 else
3940 did_emsg = save_did_emsg;
3941
3942 /* Need to get the line again, a multi-line regexp may
3943 * have made it invalid. */
3944 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3945 ptr = line + v;
3946
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003947 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003949 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003950 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003952#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003953
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003954#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003955 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003956 * Only do this when there is no syntax highlighting, the
3957 * @Spell cluster is not used or the current syntax item
3958 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003959 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003960 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003961 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003962# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003963 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003964 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003965# endif
3966 if (c != 0 && (
3967# ifdef FEAT_SYN_HL
3968 !has_syntax ||
3969# endif
3970 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003971 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003972 char_u *prev_ptr, *p;
3973 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003974 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003975# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003976 if (has_mbyte)
3977 {
3978 prev_ptr = ptr - mb_l;
3979 v -= mb_l - 1;
3980 }
3981 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003982# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003983 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003984
3985 /* Use nextline[] if possible, it has the start of the
3986 * next line concatenated. */
3987 if ((prev_ptr - line) - nextlinecol >= 0)
3988 p = nextline + (prev_ptr - line) - nextlinecol;
3989 else
3990 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003991 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003992 len = spell_check(wp, p, &spell_hlf, &cap_col,
3993 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003994 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003995
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003996 /* In Insert mode only highlight a word that
3997 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003998 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003999 && (State & INSERT) != 0
4000 && wp->w_cursor.lnum == lnum
4001 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004002 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004003 && wp->w_cursor.col < (colnr_T)word_end)
4004 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004005 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004006 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004007 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004008
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004009 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004010 && (p - nextline) + len > nextline_idx)
4011 {
4012 /* Remember that the good word continues at the
4013 * start of the next line. */
4014 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004015 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004016 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004017
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004018 /* Turn index into actual attributes. */
4019 if (spell_hlf != HLF_COUNT)
4020 spell_attr = highlight_attr[spell_hlf];
4021
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004022 if (cap_col > 0)
4023 {
4024 if (p != prev_ptr
4025 && (p - nextline) + cap_col >= nextline_idx)
4026 {
4027 /* Remember that the word in the next line
4028 * must start with a capital. */
4029 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004030 cap_col = (int)((p - nextline) + cap_col
4031 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004032 }
4033 else
4034 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004035 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004036 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004037 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004038 }
4039 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004040 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004041 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004042 char_attr = hl_combine_attr(char_attr, spell_attr);
4043 else
4044 char_attr = hl_combine_attr(spell_attr, char_attr);
4045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#endif
4047#ifdef FEAT_LINEBREAK
4048 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004049 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 */
4051 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004052 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
4054 n_extra = win_lbr_chartabsize(wp, ptr - (
4055# ifdef FEAT_MBYTE
4056 has_mbyte ? mb_l :
4057# endif
4058 1), (colnr_T)vcol, NULL) - 1;
4059 c_extra = ' ';
4060 if (vim_iswhite(c))
4061 c = ' ';
4062 }
4063#endif
4064
4065 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4066 {
4067 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004068 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
4070 n_attr = 1;
4071 extra_attr = hl_attr(HLF_8);
4072 saved_attr2 = char_attr; /* save current attr */
4073 }
4074#ifdef FEAT_MBYTE
4075 mb_c = c;
4076 if (enc_utf8 && (*mb_char2len)(c) > 1)
4077 {
4078 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004079 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004080 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 else
4083 mb_utf8 = FALSE;
4084#endif
4085 }
4086 }
4087
4088 /*
4089 * Handling of non-printable characters.
4090 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004091 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
4093 /*
4094 * when getting a character from the file, we may have to
4095 * turn it into something else on the way to putting it
4096 * into "ScreenLines".
4097 */
4098 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4099 {
4100 /* tab amount depends on current column */
4101 n_extra = (int)wp->w_buffer->b_p_ts
4102 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4103#ifdef FEAT_MBYTE
4104 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4105#endif
4106 if (wp->w_p_list)
4107 {
4108 c = lcs_tab1;
4109 c_extra = lcs_tab2;
4110 n_attr = n_extra + 1;
4111 extra_attr = hl_attr(HLF_8);
4112 saved_attr2 = char_attr; /* save current attr */
4113#ifdef FEAT_MBYTE
4114 mb_c = c;
4115 if (enc_utf8 && (*mb_char2len)(c) > 1)
4116 {
4117 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004118 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004119 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121#endif
4122 }
4123 else
4124 {
4125 c_extra = ' ';
4126 c = ' ';
4127 }
4128 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004129 else if (c == NUL
4130 && ((wp->w_p_list && lcs_eol > 0)
4131 || ((fromcol >= 0 || fromcol_prev >= 0)
4132 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004133#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004134 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004135#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004136 && (
4137# ifdef FEAT_RIGHTLEFT
4138 wp->w_p_rl ? (col >= 0) :
4139# endif
4140 (col < W_WIDTH(wp)))
4141 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004142 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004143 && (colnr_T)vcol == wp->w_virtcol)))
4144 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004146 /* Display a '$' after the line or highlight an extra
4147 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4149 /* For a diff line the highlighting continues after the
4150 * "$". */
4151 if (
4152# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004153 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154# ifdef LINE_ATTR
4155 &&
4156# endif
4157# endif
4158# ifdef LINE_ATTR
4159 line_attr == 0
4160# endif
4161 )
4162#endif
4163 {
4164#ifdef FEAT_VIRTUALEDIT
4165 /* In virtualedit, visual selections may extend
4166 * beyond end of line. */
4167 if (area_highlighting && virtual_active()
4168 && tocol != MAXCOL && vcol < tocol)
4169 n_extra = 0;
4170 else
4171#endif
4172 {
4173 p_extra = at_end_str;
4174 n_extra = 1;
4175 c_extra = NUL;
4176 }
4177 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004178 if (wp->w_p_list)
4179 c = lcs_eol;
4180 else
4181 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 lcs_eol_one = -1;
4183 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004184 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
4186 extra_attr = hl_attr(HLF_AT);
4187 n_attr = 1;
4188 }
4189#ifdef FEAT_MBYTE
4190 mb_c = c;
4191 if (enc_utf8 && (*mb_char2len)(c) > 1)
4192 {
4193 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004194 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004195 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197 else
4198 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4199#endif
4200 }
4201 else if (c != NUL)
4202 {
4203 p_extra = transchar(c);
4204#ifdef FEAT_RIGHTLEFT
4205 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4206 rl_mirror(p_extra); /* reverse "<12>" */
4207#endif
4208 n_extra = byte2cells(c) - 1;
4209 c_extra = NUL;
4210 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004211 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
4213 n_attr = n_extra + 1;
4214 extra_attr = hl_attr(HLF_8);
4215 saved_attr2 = char_attr; /* save current attr */
4216 }
4217#ifdef FEAT_MBYTE
4218 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4219#endif
4220 }
4221#ifdef FEAT_VIRTUALEDIT
4222 else if (VIsual_active
4223 && (VIsual_mode == Ctrl_V
4224 || VIsual_mode == 'v')
4225 && virtual_active()
4226 && tocol != MAXCOL
4227 && vcol < tocol
4228 && (
4229# ifdef FEAT_RIGHTLEFT
4230 wp->w_p_rl ? (col >= 0) :
4231# endif
4232 (col < W_WIDTH(wp))))
4233 {
4234 c = ' ';
4235 --ptr; /* put it back at the NUL */
4236 }
4237#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004238#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 else if ((
4240# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004241 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 ) && (
4245# ifdef FEAT_RIGHTLEFT
4246 wp->w_p_rl ? (col >= 0) :
4247# endif
4248 (col < W_WIDTH(wp))))
4249 {
4250 /* Highlight until the right side of the window */
4251 c = ' ';
4252 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004253
4254 /* Remember we do the char for line highlighting. */
4255 ++did_line_attr;
4256
4257 /* don't do search HL for the rest of the line */
4258 if (line_attr != 0 && char_attr == search_attr && col > 0)
4259 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260# ifdef FEAT_DIFF
4261 if (diff_hlf == HLF_TXD)
4262 {
4263 diff_hlf = HLF_CHD;
4264 if (attr == 0 || char_attr != attr)
4265 char_attr = hl_attr(diff_hlf);
4266 }
4267# endif
4268 }
4269#endif
4270 }
4271 }
4272
4273 /* Don't override visual selection highlighting. */
4274 if (n_attr > 0
4275 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004276 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 char_attr = extra_attr;
4278
Bram Moolenaar81695252004-12-29 20:58:21 +00004279#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 /* XIM don't send preedit_start and preedit_end, but they send
4281 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4282 * im_is_preediting() here. */
4283 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004284 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 && (State & INSERT)
4286 && !p_imdisable
4287 && im_is_preediting()
4288 && draw_state == WL_LINE)
4289 {
4290 colnr_T tcol;
4291
4292 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004293 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 else
4295 tcol = preedit_end_col;
4296 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4297 {
4298 if (feedback_old_attr < 0)
4299 {
4300 feedback_col = 0;
4301 feedback_old_attr = char_attr;
4302 }
4303 char_attr = im_get_feedback_attr(feedback_col);
4304 if (char_attr < 0)
4305 char_attr = feedback_old_attr;
4306 feedback_col++;
4307 }
4308 else if (feedback_old_attr >= 0)
4309 {
4310 char_attr = feedback_old_attr;
4311 feedback_old_attr = -1;
4312 feedback_col = 0;
4313 }
4314 }
4315#endif
4316 /*
4317 * Handle the case where we are in column 0 but not on the first
4318 * character of the line and the user wants us to show us a
4319 * special character (via 'listchars' option "precedes:<char>".
4320 */
4321 if (lcs_prec_todo != NUL
4322 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4323#ifdef FEAT_DIFF
4324 && filler_todo <= 0
4325#endif
4326 && draw_state > WL_NR
4327 && c != NUL)
4328 {
4329 c = lcs_prec;
4330 lcs_prec_todo = NUL;
4331#ifdef FEAT_MBYTE
4332 mb_c = c;
4333 if (enc_utf8 && (*mb_char2len)(c) > 1)
4334 {
4335 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004336 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004337 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 else
4340 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4341#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004342 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
4344 saved_attr3 = char_attr; /* save current attr */
4345 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4346 n_attr3 = 1;
4347 }
4348 }
4349
4350 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004351 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004353 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004354#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004355 || did_line_attr == 1
4356#endif
4357 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004359#ifdef FEAT_SEARCH_EXTRA
4360 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004361
4362 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004363 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004364 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004365#endif
4366
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 /* invert at least one char, used for Visual and empty line or
4368 * highlight match at end of line. If it's beyond the last
4369 * char on the screen, just overwrite that one (tricky!) Not
4370 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004371#ifdef FEAT_SEARCH_EXTRA
4372 prevcol_hl_flag = FALSE;
4373 if (prevcol == (long)search_hl.startcol)
4374 prevcol_hl_flag = TRUE;
4375 else
4376 {
4377 cur = wp->w_match_head;
4378 while (cur != NULL)
4379 {
4380 if (prevcol == (long)cur->hl.startcol)
4381 {
4382 prevcol_hl_flag = TRUE;
4383 break;
4384 }
4385 cur = cur->next;
4386 }
4387 }
4388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004390 && ((area_attr != 0 && vcol == fromcol
4391#ifdef FEAT_VISUAL
4392 && (VIsual_mode != Ctrl_V
4393 || lnum == VIsual.lnum
4394 || lnum == curwin->w_cursor.lnum)
4395#endif
4396 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397#ifdef FEAT_SEARCH_EXTRA
4398 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004399 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004400# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004401 && did_line_attr <= 1
4402# endif
4403 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404#endif
4405 ))
4406 {
4407 int n = 0;
4408
4409#ifdef FEAT_RIGHTLEFT
4410 if (wp->w_p_rl)
4411 {
4412 if (col < 0)
4413 n = 1;
4414 }
4415 else
4416#endif
4417 {
4418 if (col >= W_WIDTH(wp))
4419 n = -1;
4420 }
4421 if (n != 0)
4422 {
4423 /* At the window boundary, highlight the last character
4424 * instead (better than nothing). */
4425 off += n;
4426 col += n;
4427 }
4428 else
4429 {
4430 /* Add a blank character to highlight. */
4431 ScreenLines[off] = ' ';
4432#ifdef FEAT_MBYTE
4433 if (enc_utf8)
4434 ScreenLinesUC[off] = 0;
4435#endif
4436 }
4437#ifdef FEAT_SEARCH_EXTRA
4438 if (area_attr == 0)
4439 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004440 /* Use attributes from match with highest priority among
4441 * 'search_hl' and the match list. */
4442 char_attr = search_hl.attr;
4443 cur = wp->w_match_head;
4444 shl_flag = FALSE;
4445 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004446 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004447 if (shl_flag == FALSE
4448 && ((cur != NULL
4449 && cur->priority > SEARCH_HL_PRIORITY)
4450 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004451 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004452 shl = &search_hl;
4453 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004454 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004455 else
4456 shl = &cur->hl;
4457 if ((ptr - line) - 1 == (long)shl->startcol)
4458 char_attr = shl->attr;
4459 if (shl != &search_hl && cur != NULL)
4460 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463#endif
4464 ScreenAttrs[off] = char_attr;
4465#ifdef FEAT_RIGHTLEFT
4466 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004469 --off;
4470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 else
4472#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004475 ++off;
4476 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004477 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004478#ifdef FEAT_SYN_HL
4479 eol_hl_off = 1;
4480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
Bram Moolenaar91170f82006-05-05 21:15:17 +00004484 /*
4485 * At end of the text line.
4486 */
4487 if (c == NUL)
4488 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004489#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004490 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4491 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004492 {
4493 /* highlight last char after line */
4494 --col;
4495 --off;
4496 --vcol;
4497 }
4498
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004499 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004500 if (wp->w_p_wrap)
4501 v = wp->w_skipcol;
4502 else
4503 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004504 /* check if line ends before left margin */
4505 if (vcol < v + col - win_col_off(wp))
4506
4507 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004508 if (wp->w_p_cuc
Bram Moolenaara443af82007-11-08 13:51:42 +00004509 && (int)wp->w_virtcol >= vcol - eol_hl_off
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004510 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4511 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004512 && lnum != wp->w_cursor.lnum
4513# ifdef FEAT_RIGHTLEFT
4514 && !wp->w_p_rl
4515# endif
4516 )
4517 {
4518 while (col < W_WIDTH(wp))
4519 {
4520 ScreenLines[off] = ' ';
4521#ifdef FEAT_MBYTE
4522 if (enc_utf8)
4523 ScreenLinesUC[off] = 0;
4524#endif
4525 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004526 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004527 {
4528 ScreenAttrs[off] = hl_attr(HLF_CUC);
4529 break;
4530 }
4531 ScreenAttrs[off++] = 0;
4532 ++vcol;
4533 }
4534 }
4535#endif
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4538 wp->w_p_rl);
4539 row++;
4540
4541 /*
4542 * Update w_cline_height and w_cline_folded if the cursor line was
4543 * updated (saves a call to plines() later).
4544 */
4545 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4546 {
4547 curwin->w_cline_row = startrow;
4548 curwin->w_cline_height = row - startrow;
4549#ifdef FEAT_FOLDING
4550 curwin->w_cline_folded = FALSE;
4551#endif
4552 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4553 }
4554
4555 break;
4556 }
4557
4558 /* line continues beyond line end */
4559 if (lcs_ext
4560 && !wp->w_p_wrap
4561#ifdef FEAT_DIFF
4562 && filler_todo <= 0
4563#endif
4564 && (
4565#ifdef FEAT_RIGHTLEFT
4566 wp->w_p_rl ? col == 0 :
4567#endif
4568 col == W_WIDTH(wp) - 1)
4569 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004570 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4572 {
4573 c = lcs_ext;
4574 char_attr = hl_attr(HLF_AT);
4575#ifdef FEAT_MBYTE
4576 mb_c = c;
4577 if (enc_utf8 && (*mb_char2len)(c) > 1)
4578 {
4579 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004580 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004581 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 else
4584 mb_utf8 = FALSE;
4585#endif
4586 }
4587
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004588#ifdef FEAT_SYN_HL
4589 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4590 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004591 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004592 && lnum != wp->w_cursor.lnum
Bram Moolenaar54ef7112009-02-21 20:11:41 +00004593 && draw_state == WL_LINE
4594 && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004595 {
4596 vcol_save_attr = char_attr;
4597 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4598 }
4599 else
4600 vcol_save_attr = -1;
4601#endif
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 /*
4604 * Store character to be displayed.
4605 * Skip characters that are left of the screen for 'nowrap'.
4606 */
4607 vcol_prev = vcol;
4608 if (draw_state < WL_LINE || n_skip <= 0)
4609 {
4610 /*
4611 * Store the character.
4612 */
4613#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4614 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4615 {
4616 /* A double-wide character is: put first halve in left cell. */
4617 --off;
4618 --col;
4619 }
4620#endif
4621 ScreenLines[off] = c;
4622#ifdef FEAT_MBYTE
4623 if (enc_dbcs == DBCS_JPNU)
4624 ScreenLines2[off] = mb_c & 0xff;
4625 else if (enc_utf8)
4626 {
4627 if (mb_utf8)
4628 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004629 int i;
4630
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004632 if ((c & 0xff) == 0)
4633 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004634 for (i = 0; i < Screen_mco; ++i)
4635 {
4636 ScreenLinesC[i][off] = u8cc[i];
4637 if (u8cc[i] == 0)
4638 break;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 else
4642 ScreenLinesUC[off] = 0;
4643 }
4644 if (multi_attr)
4645 {
4646 ScreenAttrs[off] = multi_attr;
4647 multi_attr = 0;
4648 }
4649 else
4650#endif
4651 ScreenAttrs[off] = char_attr;
4652
4653#ifdef FEAT_MBYTE
4654 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4655 {
4656 /* Need to fill two screen columns. */
4657 ++off;
4658 ++col;
4659 if (enc_utf8)
4660 /* UTF-8: Put a 0 in the second screen char. */
4661 ScreenLines[off] = 0;
4662 else
4663 /* DBCS: Put second byte in the second screen char. */
4664 ScreenLines[off] = mb_c & 0xff;
4665 ++vcol;
4666 /* When "tocol" is halfway a character, set it to the end of
4667 * the character, otherwise highlighting won't stop. */
4668 if (tocol == vcol)
4669 ++tocol;
4670#ifdef FEAT_RIGHTLEFT
4671 if (wp->w_p_rl)
4672 {
4673 /* now it's time to backup one cell */
4674 --off;
4675 --col;
4676 }
4677#endif
4678 }
4679#endif
4680#ifdef FEAT_RIGHTLEFT
4681 if (wp->w_p_rl)
4682 {
4683 --off;
4684 --col;
4685 }
4686 else
4687#endif
4688 {
4689 ++off;
4690 ++col;
4691 }
4692 }
4693 else
4694 --n_skip;
4695
4696 /* Only advance the "vcol" when after the 'number' column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00004697 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698#ifdef FEAT_DIFF
4699 && filler_todo <= 0
4700#endif
4701 )
4702 ++vcol;
4703
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004704#ifdef FEAT_SYN_HL
4705 if (vcol_save_attr >= 0)
4706 char_attr = vcol_save_attr;
4707#endif
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 /* restore attributes after "predeces" in 'listchars' */
4710 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4711 char_attr = saved_attr3;
4712
4713 /* restore attributes after last 'listchars' or 'number' char */
4714 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4715 char_attr = saved_attr2;
4716
4717 /*
4718 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004719 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 */
4721 if ((
4722#ifdef FEAT_RIGHTLEFT
4723 wp->w_p_rl ? (col < 0) :
4724#endif
4725 (col >= W_WIDTH(wp)))
4726 && (*ptr != NUL
4727#ifdef FEAT_DIFF
4728 || filler_todo > 0
4729#endif
4730 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4731 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4732 )
4733 {
4734 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4735 wp->w_p_rl);
4736 ++row;
4737 ++screen_row;
4738
4739 /* When not wrapping and finished diff lines, or when displayed
4740 * '$' and highlighting until last column, break here. */
4741 if ((!wp->w_p_wrap
4742#ifdef FEAT_DIFF
4743 && filler_todo <= 0
4744#endif
4745 ) || lcs_eol_one == -1)
4746 break;
4747
4748 /* When the window is too narrow draw all "@" lines. */
4749 if (draw_state != WL_LINE
4750#ifdef FEAT_DIFF
4751 && filler_todo <= 0
4752#endif
4753 )
4754 {
4755 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4756#ifdef FEAT_VERTSPLIT
4757 draw_vsep_win(wp, row);
4758#endif
4759 row = endrow;
4760 }
4761
4762 /* When line got too long for screen break here. */
4763 if (row == endrow)
4764 {
4765 ++row;
4766 break;
4767 }
4768
4769 if (screen_cur_row == screen_row - 1
4770#ifdef FEAT_DIFF
4771 && filler_todo <= 0
4772#endif
4773 && W_WIDTH(wp) == Columns)
4774 {
4775 /* Remember that the line wraps, used for modeless copy. */
4776 LineWraps[screen_row - 1] = TRUE;
4777
4778 /*
4779 * Special trick to make copy/paste of wrapped lines work with
4780 * xterm/screen: write an extra character beyond the end of
4781 * the line. This will work with all terminal types
4782 * (regardless of the xn,am settings).
4783 * Only do this on a fast tty.
4784 * Only do this if the cursor is on the current line
4785 * (something has been written in it).
4786 * Don't do this for the GUI.
4787 * Don't do this for double-width characters.
4788 * Don't do this for a window not at the right screen border.
4789 */
4790 if (p_tf
4791#ifdef FEAT_GUI
4792 && !gui.in_use
4793#endif
4794#ifdef FEAT_MBYTE
4795 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004796 && ((*mb_off2cells)(LineOffset[screen_row],
4797 LineOffset[screen_row] + screen_Columns)
4798 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004800 + (int)Columns - 2,
4801 LineOffset[screen_row] + screen_Columns)
4802 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803#endif
4804 )
4805 {
4806 /* First make sure we are at the end of the screen line,
4807 * then output the same character again to let the
4808 * terminal know about the wrap. If the terminal doesn't
4809 * auto-wrap, we overwrite the character. */
4810 if (screen_cur_col != W_WIDTH(wp))
4811 screen_char(LineOffset[screen_row - 1]
4812 + (unsigned)Columns - 1,
4813 screen_row - 1, (int)(Columns - 1));
4814
4815#ifdef FEAT_MBYTE
4816 /* When there is a multi-byte character, just output a
4817 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004818 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4819 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 out_char(' ');
4821 else
4822#endif
4823 out_char(ScreenLines[LineOffset[screen_row - 1]
4824 + (Columns - 1)]);
4825 /* force a redraw of the first char on the next line */
4826 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4827 screen_start(); /* don't know where cursor is now */
4828 }
4829 }
4830
4831 col = 0;
4832 off = (unsigned)(current_ScreenLine - ScreenLines);
4833#ifdef FEAT_RIGHTLEFT
4834 if (wp->w_p_rl)
4835 {
4836 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4837 off += col;
4838 }
4839#endif
4840
4841 /* reset the drawing state for the start of a wrapped line */
4842 draw_state = WL_START;
4843 saved_n_extra = n_extra;
4844 saved_p_extra = p_extra;
4845 saved_c_extra = c_extra;
4846 saved_char_attr = char_attr;
4847 n_extra = 0;
4848 lcs_prec_todo = lcs_prec;
4849#ifdef FEAT_LINEBREAK
4850# ifdef FEAT_DIFF
4851 if (filler_todo <= 0)
4852# endif
4853 need_showbreak = TRUE;
4854#endif
4855#ifdef FEAT_DIFF
4856 --filler_todo;
4857 /* When the filler lines are actually below the last line of the
4858 * file, don't draw the line itself, break here. */
4859 if (filler_todo == 0 && wp->w_botfill)
4860 break;
4861#endif
4862 }
4863
4864 } /* for every character in the line */
4865
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004866#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004867 /* After an empty line check first word for capital. */
4868 if (*skipwhite(line) == NUL)
4869 {
4870 capcol_lnum = lnum + 1;
4871 cap_col = 0;
4872 }
4873#endif
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 return row;
4876}
4877
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004878#ifdef FEAT_MBYTE
4879static int comp_char_differs __ARGS((int, int));
4880
4881/*
4882 * Return if the composing characters at "off_from" and "off_to" differ.
4883 */
4884 static int
4885comp_char_differs(off_from, off_to)
4886 int off_from;
4887 int off_to;
4888{
4889 int i;
4890
4891 for (i = 0; i < Screen_mco; ++i)
4892 {
4893 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4894 return TRUE;
4895 if (ScreenLinesC[i][off_from] == 0)
4896 break;
4897 }
4898 return FALSE;
4899}
4900#endif
4901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902/*
4903 * Check whether the given character needs redrawing:
4904 * - the (first byte of the) character is different
4905 * - the attributes are different
4906 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004907 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 */
4909 static int
4910char_needs_redraw(off_from, off_to, cols)
4911 int off_from;
4912 int off_to;
4913 int cols;
4914{
4915 if (cols > 0
4916 && ((ScreenLines[off_from] != ScreenLines[off_to]
4917 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4918
4919#ifdef FEAT_MBYTE
4920 || (enc_dbcs != 0
4921 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4922 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4923 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4924 : (cols > 1 && ScreenLines[off_from + 1]
4925 != ScreenLines[off_to + 1])))
4926 || (enc_utf8
4927 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4928 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004929 && comp_char_differs(off_from, off_to))
4930 || (cols > 1 && ScreenLines[off_from + 1]
4931 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932#endif
4933 ))
4934 return TRUE;
4935 return FALSE;
4936}
4937
4938/*
4939 * Move one "cooked" screen line to the screen, but only the characters that
4940 * have actually changed. Handle insert/delete character.
4941 * "coloff" gives the first column on the screen for this line.
4942 * "endcol" gives the columns where valid characters are.
4943 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4944 * needs to be cleared, negative otherwise.
4945 * "rlflag" is TRUE in a rightleft window:
4946 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4947 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4948 */
4949 static void
4950screen_line(row, coloff, endcol, clear_width
4951#ifdef FEAT_RIGHTLEFT
4952 , rlflag
4953#endif
4954 )
4955 int row;
4956 int coloff;
4957 int endcol;
4958 int clear_width;
4959#ifdef FEAT_RIGHTLEFT
4960 int rlflag;
4961#endif
4962{
4963 unsigned off_from;
4964 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004965#ifdef FEAT_MBYTE
4966 unsigned max_off_from;
4967 unsigned max_off_to;
4968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 int col = 0;
4970#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4971 int hl;
4972#endif
4973 int force = FALSE; /* force update rest of the line */
4974 int redraw_this /* bool: does character need redraw? */
4975#ifdef FEAT_GUI
4976 = TRUE /* For GUI when while-loop empty */
4977#endif
4978 ;
4979 int redraw_next; /* redraw_this for next character */
4980#ifdef FEAT_MBYTE
4981 int clear_next = FALSE;
4982 int char_cells; /* 1: normal char */
4983 /* 2: occupies two display cells */
4984# define CHAR_CELLS char_cells
4985#else
4986# define CHAR_CELLS 1
4987#endif
4988
4989# ifdef FEAT_CLIPBOARD
4990 clip_may_clear_selection(row, row);
4991# endif
4992
4993 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4994 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004995#ifdef FEAT_MBYTE
4996 max_off_from = off_from + screen_Columns;
4997 max_off_to = LineOffset[row] + screen_Columns;
4998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000#ifdef FEAT_RIGHTLEFT
5001 if (rlflag)
5002 {
5003 /* Clear rest first, because it's left of the text. */
5004 if (clear_width > 0)
5005 {
5006 while (col <= endcol && ScreenLines[off_to] == ' '
5007 && ScreenAttrs[off_to] == 0
5008# ifdef FEAT_MBYTE
5009 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5010# endif
5011 )
5012 {
5013 ++off_to;
5014 ++col;
5015 }
5016 if (col <= endcol)
5017 screen_fill(row, row + 1, col + coloff,
5018 endcol + coloff + 1, ' ', ' ', 0);
5019 }
5020 col = endcol + 1;
5021 off_to = LineOffset[row] + col + coloff;
5022 off_from += col;
5023 endcol = (clear_width > 0 ? clear_width : -clear_width);
5024 }
5025#endif /* FEAT_RIGHTLEFT */
5026
5027 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5028
5029 while (col < endcol)
5030 {
5031#ifdef FEAT_MBYTE
5032 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005033 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 else
5035 char_cells = 1;
5036#endif
5037
5038 redraw_this = redraw_next;
5039 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5040 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5041
5042#ifdef FEAT_GUI
5043 /* If the next character was bold, then redraw the current character to
5044 * remove any pixels that might have spilt over into us. This only
5045 * happens in the GUI.
5046 */
5047 if (redraw_next && gui.in_use)
5048 {
5049 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005050 if (hl > HL_ALL)
5051 hl = syn_attr2attr(hl);
5052 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 redraw_this = TRUE;
5054 }
5055#endif
5056
5057 if (redraw_this)
5058 {
5059 /*
5060 * Special handling when 'xs' termcap flag set (hpterm):
5061 * Attributes for characters are stored at the position where the
5062 * cursor is when writing the highlighting code. The
5063 * start-highlighting code must be written with the cursor on the
5064 * first highlighted character. The stop-highlighting code must
5065 * be written with the cursor just after the last highlighted
5066 * character.
5067 * Overwriting a character doesn't remove it's highlighting. Need
5068 * to clear the rest of the line, and force redrawing it
5069 * completely.
5070 */
5071 if ( p_wiv
5072 && !force
5073#ifdef FEAT_GUI
5074 && !gui.in_use
5075#endif
5076 && ScreenAttrs[off_to] != 0
5077 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5078 {
5079 /*
5080 * Need to remove highlighting attributes here.
5081 */
5082 windgoto(row, col + coloff);
5083 out_str(T_CE); /* clear rest of this screen line */
5084 screen_start(); /* don't know where cursor is now */
5085 force = TRUE; /* force redraw of rest of the line */
5086 redraw_next = TRUE; /* or else next char would miss out */
5087
5088 /*
5089 * If the previous character was highlighted, need to stop
5090 * highlighting at this character.
5091 */
5092 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5093 {
5094 screen_attr = ScreenAttrs[off_to - 1];
5095 term_windgoto(row, col + coloff);
5096 screen_stop_highlight();
5097 }
5098 else
5099 screen_attr = 0; /* highlighting has stopped */
5100 }
5101#ifdef FEAT_MBYTE
5102 if (enc_dbcs != 0)
5103 {
5104 /* Check if overwriting a double-byte with a single-byte or
5105 * the other way around requires another character to be
5106 * redrawn. For UTF-8 this isn't needed, because comparing
5107 * ScreenLinesUC[] is sufficient. */
5108 if (char_cells == 1
5109 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005110 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 /* Writing a single-cell character over a double-cell
5113 * character: need to redraw the next cell. */
5114 ScreenLines[off_to + 1] = 0;
5115 redraw_next = TRUE;
5116 }
5117 else if (char_cells == 2
5118 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005119 && (*mb_off2cells)(off_to, max_off_to) == 1
5120 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 /* Writing the second half of a double-cell character over
5123 * a double-cell character: need to redraw the second
5124 * cell. */
5125 ScreenLines[off_to + 2] = 0;
5126 redraw_next = TRUE;
5127 }
5128
5129 if (enc_dbcs == DBCS_JPNU)
5130 ScreenLines2[off_to] = ScreenLines2[off_from];
5131 }
5132 /* When writing a single-width character over a double-width
5133 * character and at the end of the redrawn text, need to clear out
5134 * the right halve of the old character.
5135 * Also required when writing the right halve of a double-width
5136 * char over the left halve of an existing one. */
5137 if (has_mbyte && col + char_cells == endcol
5138 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005139 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005141 && (*mb_off2cells)(off_to, max_off_to) == 1
5142 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 clear_next = TRUE;
5144#endif
5145
5146 ScreenLines[off_to] = ScreenLines[off_from];
5147#ifdef FEAT_MBYTE
5148 if (enc_utf8)
5149 {
5150 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5151 if (ScreenLinesUC[off_from] != 0)
5152 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005153 int i;
5154
5155 for (i = 0; i < Screen_mco; ++i)
5156 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 }
5159 if (char_cells == 2)
5160 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5161#endif
5162
5163#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005164 /* The bold trick makes a single column of pixels appear in the
5165 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 * character should be redrawn too. This happens for our own GUI
5167 * and for some xterms. */
5168 if (
5169# ifdef FEAT_GUI
5170 gui.in_use
5171# endif
5172# if defined(FEAT_GUI) && defined(UNIX)
5173 ||
5174# endif
5175# ifdef UNIX
5176 term_is_xterm
5177# endif
5178 )
5179 {
5180 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005181 if (hl > HL_ALL)
5182 hl = syn_attr2attr(hl);
5183 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 redraw_next = TRUE;
5185 }
5186#endif
5187 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5188#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005189 /* For simplicity set the attributes of second half of a
5190 * double-wide character equal to the first half. */
5191 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005193
5194 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 else
5197#endif
5198 screen_char(off_to, row, col + coloff);
5199 }
5200 else if ( p_wiv
5201#ifdef FEAT_GUI
5202 && !gui.in_use
5203#endif
5204 && col + coloff > 0)
5205 {
5206 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5207 {
5208 /*
5209 * Don't output stop-highlight when moving the cursor, it will
5210 * stop the highlighting when it should continue.
5211 */
5212 screen_attr = 0;
5213 }
5214 else if (screen_attr != 0)
5215 screen_stop_highlight();
5216 }
5217
5218 off_to += CHAR_CELLS;
5219 off_from += CHAR_CELLS;
5220 col += CHAR_CELLS;
5221 }
5222
5223#ifdef FEAT_MBYTE
5224 if (clear_next)
5225 {
5226 /* Clear the second half of a double-wide character of which the left
5227 * half was overwritten with a single-wide character. */
5228 ScreenLines[off_to] = ' ';
5229 if (enc_utf8)
5230 ScreenLinesUC[off_to] = 0;
5231 screen_char(off_to, row, col + coloff);
5232 }
5233#endif
5234
5235 if (clear_width > 0
5236#ifdef FEAT_RIGHTLEFT
5237 && !rlflag
5238#endif
5239 )
5240 {
5241#ifdef FEAT_GUI
5242 int startCol = col;
5243#endif
5244
5245 /* blank out the rest of the line */
5246 while (col < clear_width && ScreenLines[off_to] == ' '
5247 && ScreenAttrs[off_to] == 0
5248#ifdef FEAT_MBYTE
5249 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5250#endif
5251 )
5252 {
5253 ++off_to;
5254 ++col;
5255 }
5256 if (col < clear_width)
5257 {
5258#ifdef FEAT_GUI
5259 /*
5260 * In the GUI, clearing the rest of the line may leave pixels
5261 * behind if the first character cleared was bold. Some bold
5262 * fonts spill over the left. In this case we redraw the previous
5263 * character too. If we didn't skip any blanks above, then we
5264 * only redraw if the character wasn't already redrawn anyway.
5265 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005266 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
5268 hl = ScreenAttrs[off_to];
5269 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005270 {
5271 int prev_cells = 1;
5272# ifdef FEAT_MBYTE
5273 if (enc_utf8)
5274 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5275 * that its width is 2. */
5276 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5277 else if (enc_dbcs != 0)
5278 {
5279 /* find previous character by counting from first
5280 * column and get its width. */
5281 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005282 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005283
5284 while (off < off_to)
5285 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005286 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005287 off += prev_cells;
5288 }
5289 }
5290
5291 if (enc_dbcs != 0 && prev_cells > 1)
5292 screen_char_2(off_to - prev_cells, row,
5293 col + coloff - prev_cells);
5294 else
5295# endif
5296 screen_char(off_to - prev_cells, row,
5297 col + coloff - prev_cells);
5298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300#endif
5301 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5302 ' ', ' ', 0);
5303#ifdef FEAT_VERTSPLIT
5304 off_to += clear_width - col;
5305 col = clear_width;
5306#endif
5307 }
5308 }
5309
5310 if (clear_width > 0)
5311 {
5312#ifdef FEAT_VERTSPLIT
5313 /* For a window that's left of another, draw the separator char. */
5314 if (col + coloff < Columns)
5315 {
5316 int c;
5317
5318 c = fillchar_vsep(&hl);
5319 if (ScreenLines[off_to] != c
5320# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005321 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5322 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323# endif
5324 || ScreenAttrs[off_to] != hl)
5325 {
5326 ScreenLines[off_to] = c;
5327 ScreenAttrs[off_to] = hl;
5328# ifdef FEAT_MBYTE
5329 if (enc_utf8)
5330 {
5331 if (c >= 0x80)
5332 {
5333 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005334 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336 else
5337 ScreenLinesUC[off_to] = 0;
5338 }
5339# endif
5340 screen_char(off_to, row, col + coloff);
5341 }
5342 }
5343 else
5344#endif
5345 LineWraps[row] = FALSE;
5346 }
5347}
5348
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005349#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005351 * Mirror text "str" for right-left displaying.
5352 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005354 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355rl_mirror(str)
5356 char_u *str;
5357{
5358 char_u *p1, *p2;
5359 int t;
5360
5361 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5362 {
5363 t = *p1;
5364 *p1 = *p2;
5365 *p2 = t;
5366 }
5367}
5368#endif
5369
5370#if defined(FEAT_WINDOWS) || defined(PROTO)
5371/*
5372 * mark all status lines for redraw; used after first :cd
5373 */
5374 void
5375status_redraw_all()
5376{
5377 win_T *wp;
5378
5379 for (wp = firstwin; wp; wp = wp->w_next)
5380 if (wp->w_status_height)
5381 {
5382 wp->w_redr_status = TRUE;
5383 redraw_later(VALID);
5384 }
5385}
5386
5387/*
5388 * mark all status lines of the current buffer for redraw
5389 */
5390 void
5391status_redraw_curbuf()
5392{
5393 win_T *wp;
5394
5395 for (wp = firstwin; wp; wp = wp->w_next)
5396 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5397 {
5398 wp->w_redr_status = TRUE;
5399 redraw_later(VALID);
5400 }
5401}
5402
5403/*
5404 * Redraw all status lines that need to be redrawn.
5405 */
5406 void
5407redraw_statuslines()
5408{
5409 win_T *wp;
5410
5411 for (wp = firstwin; wp; wp = wp->w_next)
5412 if (wp->w_redr_status)
5413 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005414 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005415 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416}
5417#endif
5418
5419#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5420/*
5421 * Redraw all status lines at the bottom of frame "frp".
5422 */
5423 void
5424win_redraw_last_status(frp)
5425 frame_T *frp;
5426{
5427 if (frp->fr_layout == FR_LEAF)
5428 frp->fr_win->w_redr_status = TRUE;
5429 else if (frp->fr_layout == FR_ROW)
5430 {
5431 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5432 win_redraw_last_status(frp);
5433 }
5434 else /* frp->fr_layout == FR_COL */
5435 {
5436 frp = frp->fr_child;
5437 while (frp->fr_next != NULL)
5438 frp = frp->fr_next;
5439 win_redraw_last_status(frp);
5440 }
5441}
5442#endif
5443
5444#ifdef FEAT_VERTSPLIT
5445/*
5446 * Draw the verticap separator right of window "wp" starting with line "row".
5447 */
5448 static void
5449draw_vsep_win(wp, row)
5450 win_T *wp;
5451 int row;
5452{
5453 int hl;
5454 int c;
5455
5456 if (wp->w_vsep_width)
5457 {
5458 /* draw the vertical separator right of this window */
5459 c = fillchar_vsep(&hl);
5460 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5461 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5462 c, ' ', hl);
5463 }
5464}
5465#endif
5466
5467#ifdef FEAT_WILDMENU
5468static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005469static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
5471/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005472 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 */
5474 static int
5475status_match_len(xp, s)
5476 expand_T *xp;
5477 char_u *s;
5478{
5479 int len = 0;
5480
5481#ifdef FEAT_MENU
5482 int emenu = (xp->xp_context == EXPAND_MENUS
5483 || xp->xp_context == EXPAND_MENUNAMES);
5484
5485 /* Check for menu separators - replace with '|'. */
5486 if (emenu && menu_is_separator(s))
5487 return 1;
5488#endif
5489
5490 while (*s != NUL)
5491 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005492 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005493 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005494 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496
5497 return len;
5498}
5499
5500/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005501 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005502 * These are backslashes used for escaping. Do show backslashes in help tags.
5503 */
5504 static int
5505skip_status_match_char(xp, s)
5506 expand_T *xp;
5507 char_u *s;
5508{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005509 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005510#ifdef FEAT_MENU
5511 || ((xp->xp_context == EXPAND_MENUS
5512 || xp->xp_context == EXPAND_MENUNAMES)
5513 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5514#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005515 )
5516 {
5517#ifndef BACKSLASH_IN_FILENAME
5518 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5519 return 2;
5520#endif
5521 return 1;
5522 }
5523 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005524}
5525
5526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * Show wildchar matches in the status line.
5528 * Show at least the "match" item.
5529 * We start at item 'first_match' in the list and show all matches that fit.
5530 *
5531 * If inversion is possible we use it. Else '=' characters are used.
5532 */
5533 void
5534win_redr_status_matches(xp, num_matches, matches, match, showtail)
5535 expand_T *xp;
5536 int num_matches;
5537 char_u **matches; /* list of matches */
5538 int match;
5539 int showtail;
5540{
5541#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5542 int row;
5543 char_u *buf;
5544 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005545 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 int fillchar;
5547 int attr;
5548 int i;
5549 int highlight = TRUE;
5550 char_u *selstart = NULL;
5551 int selstart_col = 0;
5552 char_u *selend = NULL;
5553 static int first_match = 0;
5554 int add_left = FALSE;
5555 char_u *s;
5556#ifdef FEAT_MENU
5557 int emenu;
5558#endif
5559#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5560 int l;
5561#endif
5562
5563 if (matches == NULL) /* interrupted completion? */
5564 return;
5565
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005566#ifdef FEAT_MBYTE
5567 if (has_mbyte)
5568 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5569 else
5570#endif
5571 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if (buf == NULL)
5573 return;
5574
5575 if (match == -1) /* don't show match but original text */
5576 {
5577 match = 0;
5578 highlight = FALSE;
5579 }
5580 /* count 1 for the ending ">" */
5581 clen = status_match_len(xp, L_MATCH(match)) + 3;
5582 if (match == 0)
5583 first_match = 0;
5584 else if (match < first_match)
5585 {
5586 /* jumping left, as far as we can go */
5587 first_match = match;
5588 add_left = TRUE;
5589 }
5590 else
5591 {
5592 /* check if match fits on the screen */
5593 for (i = first_match; i < match; ++i)
5594 clen += status_match_len(xp, L_MATCH(i)) + 2;
5595 if (first_match > 0)
5596 clen += 2;
5597 /* jumping right, put match at the left */
5598 if ((long)clen > Columns)
5599 {
5600 first_match = match;
5601 /* if showing the last match, we can add some on the left */
5602 clen = 2;
5603 for (i = match; i < num_matches; ++i)
5604 {
5605 clen += status_match_len(xp, L_MATCH(i)) + 2;
5606 if ((long)clen >= Columns)
5607 break;
5608 }
5609 if (i == num_matches)
5610 add_left = TRUE;
5611 }
5612 }
5613 if (add_left)
5614 while (first_match > 0)
5615 {
5616 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5617 if ((long)clen >= Columns)
5618 break;
5619 --first_match;
5620 }
5621
5622 fillchar = fillchar_status(&attr, TRUE);
5623
5624 if (first_match == 0)
5625 {
5626 *buf = NUL;
5627 len = 0;
5628 }
5629 else
5630 {
5631 STRCPY(buf, "< ");
5632 len = 2;
5633 }
5634 clen = len;
5635
5636 i = first_match;
5637 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5638 {
5639 if (i == match)
5640 {
5641 selstart = buf + len;
5642 selstart_col = clen;
5643 }
5644
5645 s = L_MATCH(i);
5646 /* Check for menu separators - replace with '|' */
5647#ifdef FEAT_MENU
5648 emenu = (xp->xp_context == EXPAND_MENUS
5649 || xp->xp_context == EXPAND_MENUNAMES);
5650 if (emenu && menu_is_separator(s))
5651 {
5652 STRCPY(buf + len, transchar('|'));
5653 l = (int)STRLEN(buf + len);
5654 len += l;
5655 clen += l;
5656 }
5657 else
5658#endif
5659 for ( ; *s != NUL; ++s)
5660 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005661 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 clen += ptr2cells(s);
5663#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005664 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
5666 STRNCPY(buf + len, s, l);
5667 s += l - 1;
5668 len += l;
5669 }
5670 else
5671#endif
5672 {
5673 STRCPY(buf + len, transchar_byte(*s));
5674 len += (int)STRLEN(buf + len);
5675 }
5676 }
5677 if (i == match)
5678 selend = buf + len;
5679
5680 *(buf + len++) = ' ';
5681 *(buf + len++) = ' ';
5682 clen += 2;
5683 if (++i == num_matches)
5684 break;
5685 }
5686
5687 if (i != num_matches)
5688 {
5689 *(buf + len++) = '>';
5690 ++clen;
5691 }
5692
5693 buf[len] = NUL;
5694
5695 row = cmdline_row - 1;
5696 if (row >= 0)
5697 {
5698 if (wild_menu_showing == 0)
5699 {
5700 if (msg_scrolled > 0)
5701 {
5702 /* Put the wildmenu just above the command line. If there is
5703 * no room, scroll the screen one line up. */
5704 if (cmdline_row == Rows - 1)
5705 {
5706 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5707 ++msg_scrolled;
5708 }
5709 else
5710 {
5711 ++cmdline_row;
5712 ++row;
5713 }
5714 wild_menu_showing = WM_SCROLLED;
5715 }
5716 else
5717 {
5718 /* Create status line if needed by setting 'laststatus' to 2.
5719 * Set 'winminheight' to zero to avoid that the window is
5720 * resized. */
5721 if (lastwin->w_status_height == 0)
5722 {
5723 save_p_ls = p_ls;
5724 save_p_wmh = p_wmh;
5725 p_ls = 2;
5726 p_wmh = 0;
5727 last_status(FALSE);
5728 }
5729 wild_menu_showing = WM_SHOWN;
5730 }
5731 }
5732
5733 screen_puts(buf, row, 0, attr);
5734 if (selstart != NULL && highlight)
5735 {
5736 *selend = NUL;
5737 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5738 }
5739
5740 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5741 }
5742
5743#ifdef FEAT_VERTSPLIT
5744 win_redraw_last_status(topframe);
5745#else
5746 lastwin->w_redr_status = TRUE;
5747#endif
5748 vim_free(buf);
5749}
5750#endif
5751
5752#if defined(FEAT_WINDOWS) || defined(PROTO)
5753/*
5754 * Redraw the status line of window wp.
5755 *
5756 * If inversion is possible we use it. Else '=' characters are used.
5757 */
5758 void
5759win_redr_status(wp)
5760 win_T *wp;
5761{
5762 int row;
5763 char_u *p;
5764 int len;
5765 int fillchar;
5766 int attr;
5767 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00005768 static int busy = FALSE;
5769
5770 /* It's possible to get here recursively when 'statusline' (indirectly)
5771 * invokes ":redrawstatus". Simply ignore the call then. */
5772 if (busy)
5773 return;
5774 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
5776 wp->w_redr_status = FALSE;
5777 if (wp->w_status_height == 0)
5778 {
5779 /* no status line, can only be last window */
5780 redraw_cmdline = TRUE;
5781 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005782 else if (!redrawing()
5783#ifdef FEAT_INS_EXPAND
5784 /* don't update status line when popup menu is visible and may be
5785 * drawn over it */
5786 || pum_visible()
5787#endif
5788 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 /* Don't redraw right now, do it later. */
5791 wp->w_redr_status = TRUE;
5792 }
5793#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005794 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
5796 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00005797 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
5799#endif
5800 else
5801 {
5802 fillchar = fillchar_status(&attr, wp == curwin);
5803
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005804 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 p = NameBuff;
5806 len = (int)STRLEN(p);
5807
5808 if (wp->w_buffer->b_help
5809#ifdef FEAT_QUICKFIX
5810 || wp->w_p_pvw
5811#endif
5812 || bufIsChanged(wp->w_buffer)
5813 || wp->w_buffer->b_p_ro)
5814 *(p + len++) = ' ';
5815 if (wp->w_buffer->b_help)
5816 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005817 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 len += (int)STRLEN(p + len);
5819 }
5820#ifdef FEAT_QUICKFIX
5821 if (wp->w_p_pvw)
5822 {
5823 STRCPY(p + len, _("[Preview]"));
5824 len += (int)STRLEN(p + len);
5825 }
5826#endif
5827 if (bufIsChanged(wp->w_buffer))
5828 {
5829 STRCPY(p + len, "[+]");
5830 len += 3;
5831 }
5832 if (wp->w_buffer->b_p_ro)
5833 {
5834 STRCPY(p + len, "[RO]");
5835 len += 4;
5836 }
5837
5838#ifndef FEAT_VERTSPLIT
5839 this_ru_col = ru_col;
5840 if (this_ru_col < (Columns + 1) / 2)
5841 this_ru_col = (Columns + 1) / 2;
5842#else
5843 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5844 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5845 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5846 if (this_ru_col <= 1)
5847 {
5848 p = (char_u *)"<"; /* No room for file name! */
5849 len = 1;
5850 }
5851 else
5852#endif
5853#ifdef FEAT_MBYTE
5854 if (has_mbyte)
5855 {
5856 int clen = 0, i;
5857
5858 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005859 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 clen += (*mb_ptr2cells)(p + i);
5861 /* Find first character that will fit.
5862 * Going from start to end is much faster for DBCS. */
5863 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005864 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 clen -= (*mb_ptr2cells)(p + i);
5866 len = clen;
5867 if (i > 0)
5868 {
5869 p = p + i - 1;
5870 *p = '<';
5871 ++len;
5872 }
5873
5874 }
5875 else
5876#endif
5877 if (len > this_ru_col - 1)
5878 {
5879 p += len - (this_ru_col - 1);
5880 *p = '<';
5881 len = this_ru_col - 1;
5882 }
5883
5884 row = W_WINROW(wp) + wp->w_height;
5885 screen_puts(p, row, W_WINCOL(wp), attr);
5886 screen_fill(row, row + 1, len + W_WINCOL(wp),
5887 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5888
5889 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5890 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5891 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5892 - 1 + W_WINCOL(wp)), attr);
5893
5894#ifdef FEAT_CMDL_INFO
5895 win_redr_ruler(wp, TRUE);
5896#endif
5897 }
5898
5899#ifdef FEAT_VERTSPLIT
5900 /*
5901 * May need to draw the character below the vertical separator.
5902 */
5903 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5904 {
5905 if (stl_connected(wp))
5906 fillchar = fillchar_status(&attr, wp == curwin);
5907 else
5908 fillchar = fillchar_vsep(&attr);
5909 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5910 attr);
5911 }
5912#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00005913 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914}
5915
Bram Moolenaar238a5642006-02-21 22:12:05 +00005916#ifdef FEAT_STL_OPT
5917/*
5918 * Redraw the status line according to 'statusline' and take care of any
5919 * errors encountered.
5920 */
5921 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00005922redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00005923 win_T *wp;
5924{
Bram Moolenaar362f3562009-11-03 16:20:34 +00005925 static int entered = FALSE;
5926 int save_called_emsg = called_emsg;
5927
5928 /* When called recursively return. This can happen when the statusline
5929 * contains an expression that triggers a redraw. */
5930 if (entered)
5931 return;
5932 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00005933
5934 called_emsg = FALSE;
5935 win_redr_custom(wp, FALSE);
5936 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00005937 {
5938 /* When there is an error disable the statusline, otherwise the
5939 * display is messed up with errors and a redraw triggers the problem
5940 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005941 set_string_option_direct((char_u *)"statusline", -1,
5942 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005943 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00005944 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00005945 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00005946 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00005947}
5948#endif
5949
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950# ifdef FEAT_VERTSPLIT
5951/*
5952 * Return TRUE if the status line of window "wp" is connected to the status
5953 * line of the window right of it. If not, then it's a vertical separator.
5954 * Only call if (wp->w_vsep_width != 0).
5955 */
5956 int
5957stl_connected(wp)
5958 win_T *wp;
5959{
5960 frame_T *fr;
5961
5962 fr = wp->w_frame;
5963 while (fr->fr_parent != NULL)
5964 {
5965 if (fr->fr_parent->fr_layout == FR_COL)
5966 {
5967 if (fr->fr_next != NULL)
5968 break;
5969 }
5970 else
5971 {
5972 if (fr->fr_next != NULL)
5973 return TRUE;
5974 }
5975 fr = fr->fr_parent;
5976 }
5977 return FALSE;
5978}
5979# endif
5980
5981#endif /* FEAT_WINDOWS */
5982
5983#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5984/*
5985 * Get the value to show for the language mappings, active 'keymap'.
5986 */
5987 int
5988get_keymap_str(wp, buf, len)
5989 win_T *wp;
5990 char_u *buf; /* buffer for the result */
5991 int len; /* length of buffer */
5992{
5993 char_u *p;
5994
5995 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5996 return FALSE;
5997
5998 {
5999#ifdef FEAT_EVAL
6000 buf_T *old_curbuf = curbuf;
6001 win_T *old_curwin = curwin;
6002 char_u *s;
6003
6004 curbuf = wp->w_buffer;
6005 curwin = wp;
6006 STRCPY(buf, "b:keymap_name"); /* must be writable */
6007 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006008 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 --emsg_skip;
6010 curbuf = old_curbuf;
6011 curwin = old_curwin;
6012 if (p == NULL || *p == NUL)
6013#endif
6014 {
6015#ifdef FEAT_KEYMAP
6016 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6017 p = wp->w_buffer->b_p_keymap;
6018 else
6019#endif
6020 p = (char_u *)"lang";
6021 }
6022 if ((int)(STRLEN(p) + 3) < len)
6023 sprintf((char *)buf, "<%s>", p);
6024 else
6025 buf[0] = NUL;
6026#ifdef FEAT_EVAL
6027 vim_free(s);
6028#endif
6029 }
6030 return buf[0] != NUL;
6031}
6032#endif
6033
6034#if defined(FEAT_STL_OPT) || defined(PROTO)
6035/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006036 * Redraw the status line or ruler of window "wp".
6037 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 */
6039 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006040win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006042 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 int attr;
6045 int curattr;
6046 int row;
6047 int col = 0;
6048 int maxwidth;
6049 int width;
6050 int n;
6051 int len;
6052 int fillchar;
6053 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006054 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006056 struct stl_hlrec hltab[STL_MAX_ITEM];
6057 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006058 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006061 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006063 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006064 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006065 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006066 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006067 attr = hl_attr(HLF_TPF);
6068 maxwidth = Columns;
6069# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006070 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006073 else
6074 {
6075 row = W_WINROW(wp) + wp->w_height;
6076 fillchar = fillchar_status(&attr, wp == curwin);
6077 maxwidth = W_WIDTH(wp);
6078
6079 if (draw_ruler)
6080 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006081 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006082 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006083 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006084 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006085 if (*++stl == '-')
6086 stl++;
6087 if (atoi((char *)stl))
6088 while (VIM_ISDIGIT(*stl))
6089 stl++;
6090 if (*stl++ != '(')
6091 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006092 }
6093#ifdef FEAT_VERTSPLIT
6094 col = ru_col - (Columns - W_WIDTH(wp));
6095 if (col < (W_WIDTH(wp) + 1) / 2)
6096 col = (W_WIDTH(wp) + 1) / 2;
6097#else
6098 col = ru_col;
6099 if (col > (Columns + 1) / 2)
6100 col = (Columns + 1) / 2;
6101#endif
6102 maxwidth = W_WIDTH(wp) - col;
6103#ifdef FEAT_WINDOWS
6104 if (!wp->w_status_height)
6105#endif
6106 {
6107 row = Rows - 1;
6108 --maxwidth; /* writing in last column may cause scrolling */
6109 fillchar = ' ';
6110 attr = 0;
6111 }
6112
6113# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006114 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006115# endif
6116 }
6117 else
6118 {
6119 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006120 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006121 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006122 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006123# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006124 use_sandbox = was_set_insecurely((char_u *)"statusline",
6125 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006126# endif
6127 }
6128
6129#ifdef FEAT_VERTSPLIT
6130 col += W_WINCOL(wp);
6131#endif
6132 }
6133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (maxwidth <= 0)
6135 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
Bram Moolenaar362f3562009-11-03 16:20:34 +00006137 /* Make a copy, because the statusline may include a function call that
6138 * might change the option value and free the memory. */
6139 stl = vim_strsave(stl);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006140 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6141 buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006142 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006143 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006144 vim_free(stl);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006145 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006147 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
6149#ifdef FEAT_MBYTE
6150 len += (*mb_char2bytes)(fillchar, buf + len);
6151#else
6152 buf[len++] = fillchar;
6153#endif
6154 ++width;
6155 }
6156 buf[len] = NUL;
6157
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006158 /*
6159 * Draw each snippet with the specified highlighting.
6160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 curattr = attr;
6162 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006163 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006165 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 screen_puts_len(p, len, row, col, curattr);
6167 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006168 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006170 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006172 else if (hltab[n].userhl < 0)
6173 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006175 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006176 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177#endif
6178 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006179 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
6181 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006182
6183 if (wp == NULL)
6184 {
6185 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6186 col = 0;
6187 len = 0;
6188 p = buf;
6189 fillchar = 0;
6190 for (n = 0; tabtab[n].start != NULL; n++)
6191 {
6192 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6193 while (col < len)
6194 TabPageIdxs[col++] = fillchar;
6195 p = tabtab[n].start;
6196 fillchar = tabtab[n].userhl;
6197 }
6198 while (col < Columns)
6199 TabPageIdxs[col++] = fillchar;
6200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201}
6202
6203#endif /* FEAT_STL_OPT */
6204
6205/*
6206 * Output a single character directly to the screen and update ScreenLines.
6207 */
6208 void
6209screen_putchar(c, row, col, attr)
6210 int c;
6211 int row, col;
6212 int attr;
6213{
6214#ifdef FEAT_MBYTE
6215 char_u buf[MB_MAXBYTES + 1];
6216
6217 buf[(*mb_char2bytes)(c, buf)] = NUL;
6218#else
6219 char_u buf[2];
6220
6221 buf[0] = c;
6222 buf[1] = NUL;
6223#endif
6224 screen_puts(buf, row, col, attr);
6225}
6226
6227/*
6228 * Get a single character directly from ScreenLines into "bytes[]".
6229 * Also return its attribute in *attrp;
6230 */
6231 void
6232screen_getbytes(row, col, bytes, attrp)
6233 int row, col;
6234 char_u *bytes;
6235 int *attrp;
6236{
6237 unsigned off;
6238
6239 /* safety check */
6240 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6241 {
6242 off = LineOffset[row] + col;
6243 *attrp = ScreenAttrs[off];
6244 bytes[0] = ScreenLines[off];
6245 bytes[1] = NUL;
6246
6247#ifdef FEAT_MBYTE
6248 if (enc_utf8 && ScreenLinesUC[off] != 0)
6249 bytes[utfc_char2bytes(off, bytes)] = NUL;
6250 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6251 {
6252 bytes[0] = ScreenLines[off];
6253 bytes[1] = ScreenLines2[off];
6254 bytes[2] = NUL;
6255 }
6256 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6257 {
6258 bytes[1] = ScreenLines[off + 1];
6259 bytes[2] = NUL;
6260 }
6261#endif
6262 }
6263}
6264
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006265#ifdef FEAT_MBYTE
6266static int screen_comp_differs __ARGS((int, int*));
6267
6268/*
6269 * Return TRUE if composing characters for screen posn "off" differs from
6270 * composing characters in "u8cc".
6271 */
6272 static int
6273screen_comp_differs(off, u8cc)
6274 int off;
6275 int *u8cc;
6276{
6277 int i;
6278
6279 for (i = 0; i < Screen_mco; ++i)
6280 {
6281 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6282 return TRUE;
6283 if (u8cc[i] == 0)
6284 break;
6285 }
6286 return FALSE;
6287}
6288#endif
6289
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290/*
6291 * Put string '*text' on the screen at position 'row' and 'col', with
6292 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6293 * Note: only outputs within one row, message is truncated at screen boundary!
6294 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6295 */
6296 void
6297screen_puts(text, row, col, attr)
6298 char_u *text;
6299 int row;
6300 int col;
6301 int attr;
6302{
6303 screen_puts_len(text, -1, row, col, attr);
6304}
6305
6306/*
6307 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6308 * a NUL.
6309 */
6310 void
6311screen_puts_len(text, len, row, col, attr)
6312 char_u *text;
6313 int len;
6314 int row;
6315 int col;
6316 int attr;
6317{
6318 unsigned off;
6319 char_u *ptr = text;
6320 int c;
6321#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006322 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 int mbyte_blen = 1;
6324 int mbyte_cells = 1;
6325 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006326 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 int clear_next_cell = FALSE;
6328# ifdef FEAT_ARABIC
6329 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006330 int pc, nc, nc1;
6331 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332# endif
6333#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006334#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6335 int force_redraw_this;
6336 int force_redraw_next = FALSE;
6337#endif
6338 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
6340 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6341 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006342 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
Bram Moolenaarc236c162008-07-13 17:41:49 +00006344#ifdef FEAT_MBYTE
6345 /* When drawing over the right halve of a double-wide char clear out the
6346 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006347 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006348# ifdef FEAT_GUI
6349 && !gui.in_use
6350# endif
6351 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006352 {
6353 ScreenLines[off - 1] = ' ';
6354 ScreenAttrs[off - 1] = 0;
6355 if (enc_utf8)
6356 {
6357 ScreenLinesUC[off - 1] = 0;
6358 ScreenLinesC[0][off - 1] = 0;
6359 }
6360 /* redraw the previous cell, make it empty */
6361 screen_char(off - 1, row, col - 1);
6362 /* force the cell at "col" to be redrawn */
6363 force_redraw_next = TRUE;
6364 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006365#endif
6366
Bram Moolenaar367329b2007-08-30 11:53:22 +00006367#ifdef FEAT_MBYTE
6368 max_off = LineOffset[row] + screen_Columns;
6369#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006370 while (col < screen_Columns
6371 && (len < 0 || (int)(ptr - text) < len)
6372 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
6374 c = *ptr;
6375#ifdef FEAT_MBYTE
6376 /* check if this is the first byte of a multibyte */
6377 if (has_mbyte)
6378 {
6379 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006380 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006382 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6384 mbyte_cells = 1;
6385 else if (enc_dbcs != 0)
6386 mbyte_cells = mbyte_blen;
6387 else /* enc_utf8 */
6388 {
6389 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006390 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 (int)((text + len) - ptr));
6392 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006393 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006395# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 /* Non-BMP character: display as ? or fullwidth ?. */
6397 if (u8c >= 0x10000)
6398 {
6399 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6400 if (attr == 0)
6401 attr = hl_attr(HLF_8);
6402 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404# ifdef FEAT_ARABIC
6405 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6406 {
6407 /* Do Arabic shaping. */
6408 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6409 {
6410 /* Past end of string to be displayed. */
6411 nc = NUL;
6412 nc1 = NUL;
6413 }
6414 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006415 {
6416 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6417 nc1 = pcc[0];
6418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 pc = prev_c;
6420 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006421 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423 else
6424 prev_c = u8c;
6425# endif
6426 }
6427 }
6428#endif
6429
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006430#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6431 force_redraw_this = force_redraw_next;
6432 force_redraw_next = FALSE;
6433#endif
6434
6435 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436#ifdef FEAT_MBYTE
6437 || (mbyte_cells == 2
6438 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6439 || (enc_dbcs == DBCS_JPNU
6440 && c == 0x8e
6441 && ScreenLines2[off] != ptr[1])
6442 || (enc_utf8
Bram Moolenaarffcce302009-02-22 00:14:58 +00006443 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006444 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445#endif
6446 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006447 || exmode_active;
6448
6449 if (need_redraw
6450#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6451 || force_redraw_this
6452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 )
6454 {
6455#if defined(FEAT_GUI) || defined(UNIX)
6456 /* The bold trick makes a single row of pixels appear in the next
6457 * character. When a bold character is removed, the next
6458 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006459 * and for some xterms. */
6460 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461# ifdef FEAT_GUI
6462 gui.in_use
6463# endif
6464# if defined(FEAT_GUI) && defined(UNIX)
6465 ||
6466# endif
6467# ifdef UNIX
6468 term_is_xterm
6469# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006470 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006472 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006474 if (n > HL_ALL)
6475 n = syn_attr2attr(n);
6476 if (n & HL_BOLD)
6477 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479#endif
6480#ifdef FEAT_MBYTE
6481 /* When at the end of the text and overwriting a two-cell
6482 * character with a one-cell character, need to clear the next
6483 * cell. Also when overwriting the left halve of a two-cell char
6484 * with the right halve of a two-cell char. Do this only once
6485 * (mb_off2cells() may return 2 on the right halve). */
6486 if (clear_next_cell)
6487 clear_next_cell = FALSE;
6488 else if (has_mbyte
6489 && (len < 0 ? ptr[mbyte_blen] == NUL
6490 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006491 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006493 && (*mb_off2cells)(off, max_off) == 1
6494 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 clear_next_cell = TRUE;
6496
6497 /* Make sure we never leave a second byte of a double-byte behind,
6498 * it confuses mb_off2cells(). */
6499 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006500 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006502 && (*mb_off2cells)(off, max_off) == 1
6503 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 ScreenLines[off + mbyte_blen] = 0;
6505#endif
6506 ScreenLines[off] = c;
6507 ScreenAttrs[off] = attr;
6508#ifdef FEAT_MBYTE
6509 if (enc_utf8)
6510 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006511 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 ScreenLinesUC[off] = 0;
6513 else
6514 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006515 int i;
6516
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006518 for (i = 0; i < Screen_mco; ++i)
6519 {
6520 ScreenLinesC[i][off] = u8cc[i];
6521 if (u8cc[i] == 0)
6522 break;
6523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
6525 if (mbyte_cells == 2)
6526 {
6527 ScreenLines[off + 1] = 0;
6528 ScreenAttrs[off + 1] = attr;
6529 }
6530 screen_char(off, row, col);
6531 }
6532 else if (mbyte_cells == 2)
6533 {
6534 ScreenLines[off + 1] = ptr[1];
6535 ScreenAttrs[off + 1] = attr;
6536 screen_char_2(off, row, col);
6537 }
6538 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6539 {
6540 ScreenLines2[off] = ptr[1];
6541 screen_char(off, row, col);
6542 }
6543 else
6544#endif
6545 screen_char(off, row, col);
6546 }
6547#ifdef FEAT_MBYTE
6548 if (has_mbyte)
6549 {
6550 off += mbyte_cells;
6551 col += mbyte_cells;
6552 ptr += mbyte_blen;
6553 if (clear_next_cell)
6554 ptr = (char_u *)" ";
6555 }
6556 else
6557#endif
6558 {
6559 ++off;
6560 ++col;
6561 ++ptr;
6562 }
6563 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006564
6565#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6566 /* If we detected the next character needs to be redrawn, but the text
6567 * doesn't extend up to there, update the character here. */
6568 if (force_redraw_next && col < screen_Columns)
6569 {
6570# ifdef FEAT_MBYTE
6571 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6572 screen_char_2(off, row, col);
6573 else
6574# endif
6575 screen_char(off, row, col);
6576 }
6577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578}
6579
6580#ifdef FEAT_SEARCH_EXTRA
6581/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006582 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 */
6584 static void
6585start_search_hl()
6586{
6587 if (p_hls && !no_hlsearch)
6588 {
6589 last_pat_prog(&search_hl.rm);
6590 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006591# ifdef FEAT_RELTIME
6592 /* Set the time limit to 'redrawtime'. */
6593 profile_setlimit(p_rdt, &search_hl.tm);
6594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596}
6597
6598/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006599 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 */
6601 static void
6602end_search_hl()
6603{
6604 if (search_hl.rm.regprog != NULL)
6605 {
6606 vim_free(search_hl.rm.regprog);
6607 search_hl.rm.regprog = NULL;
6608 }
6609}
6610
6611/*
6612 * Advance to the match in window "wp" line "lnum" or past it.
6613 */
6614 static void
6615prepare_search_hl(wp, lnum)
6616 win_T *wp;
6617 linenr_T lnum;
6618{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006619 matchitem_T *cur; /* points to the match list */
6620 match_T *shl; /* points to search_hl or a match */
6621 int shl_flag; /* flag to indicate whether search_hl
6622 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 int n;
6624
6625 /*
6626 * When using a multi-line pattern, start searching at the top
6627 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006628 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006630 cur = wp->w_match_head;
6631 shl_flag = FALSE;
6632 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006634 if (shl_flag == FALSE)
6635 {
6636 shl = &search_hl;
6637 shl_flag = TRUE;
6638 }
6639 else
6640 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (shl->rm.regprog != NULL
6642 && shl->lnum == 0
6643 && re_multiline(shl->rm.regprog))
6644 {
6645 if (shl->first_lnum == 0)
6646 {
6647# ifdef FEAT_FOLDING
6648 for (shl->first_lnum = lnum;
6649 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6650 if (hasFoldingWin(wp, shl->first_lnum - 1,
6651 NULL, NULL, TRUE, NULL))
6652 break;
6653# else
6654 shl->first_lnum = wp->w_topline;
6655# endif
6656 }
6657 n = 0;
6658 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6659 {
6660 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6661 if (shl->lnum != 0)
6662 {
6663 shl->first_lnum = shl->lnum
6664 + shl->rm.endpos[0].lnum
6665 - shl->rm.startpos[0].lnum;
6666 n = shl->rm.endpos[0].col;
6667 }
6668 else
6669 {
6670 ++shl->first_lnum;
6671 n = 0;
6672 }
6673 }
6674 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006675 if (shl != &search_hl && cur != NULL)
6676 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 }
6678}
6679
6680/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006681 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 * Uses shl->buf.
6683 * Sets shl->lnum and shl->rm contents.
6684 * Note: Assumes a previous match is always before "lnum", unless
6685 * shl->lnum is zero.
6686 * Careful: Any pointers for buffer lines will become invalid.
6687 */
6688 static void
6689next_search_hl(win, shl, lnum, mincol)
6690 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006691 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 linenr_T lnum;
6693 colnr_T mincol; /* minimal column for a match */
6694{
6695 linenr_T l;
6696 colnr_T matchcol;
6697 long nmatched;
6698
6699 if (shl->lnum != 0)
6700 {
6701 /* Check for three situations:
6702 * 1. If the "lnum" is below a previous match, start a new search.
6703 * 2. If the previous match includes "mincol", use it.
6704 * 3. Continue after the previous match.
6705 */
6706 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6707 if (lnum > l)
6708 shl->lnum = 0;
6709 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6710 return;
6711 }
6712
6713 /*
6714 * Repeat searching for a match until one is found that includes "mincol"
6715 * or none is found in this line.
6716 */
6717 called_emsg = FALSE;
6718 for (;;)
6719 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006720#ifdef FEAT_RELTIME
6721 /* Stop searching after passing the time limit. */
6722 if (profile_passed_limit(&(shl->tm)))
6723 {
6724 shl->lnum = 0; /* no match found in time */
6725 break;
6726 }
6727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 /* Three situations:
6729 * 1. No useful previous match: search from start of line.
6730 * 2. Not Vi compatible or empty match: continue at next character.
6731 * Break the loop if this is beyond the end of the line.
6732 * 3. Vi compatible searching: continue at end of previous match.
6733 */
6734 if (shl->lnum == 0)
6735 matchcol = 0;
6736 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6737 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006738 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006740 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006741
6742 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006743 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006744 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006746 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 shl->lnum = 0;
6748 break;
6749 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006750#ifdef FEAT_MBYTE
6751 if (has_mbyte)
6752 matchcol += mb_ptr2len(ml);
6753 else
6754#endif
6755 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 }
6757 else
6758 matchcol = shl->rm.endpos[0].col;
6759
6760 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006761 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6762#ifdef FEAT_RELTIME
6763 &(shl->tm)
6764#else
6765 NULL
6766#endif
6767 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (called_emsg)
6769 {
6770 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006771 if (shl == &search_hl)
6772 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006773 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006774 vim_free(shl->rm.regprog);
6775 no_hlsearch = TRUE;
6776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006778 shl->lnum = 0;
6779 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 break;
6781 }
6782 if (nmatched == 0)
6783 {
6784 shl->lnum = 0; /* no match found */
6785 break;
6786 }
6787 if (shl->rm.startpos[0].lnum > 0
6788 || shl->rm.startpos[0].col >= mincol
6789 || nmatched > 1
6790 || shl->rm.endpos[0].col > mincol)
6791 {
6792 shl->lnum += shl->rm.startpos[0].lnum;
6793 break; /* useful match found */
6794 }
6795 }
6796}
6797#endif
6798
6799 static void
6800screen_start_highlight(attr)
6801 int attr;
6802{
6803 attrentry_T *aep = NULL;
6804
6805 screen_attr = attr;
6806 if (full_screen
6807#ifdef WIN3264
6808 && termcap_active
6809#endif
6810 )
6811 {
6812#ifdef FEAT_GUI
6813 if (gui.in_use)
6814 {
6815 char buf[20];
6816
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006817 /* The GUI handles this internally. */
6818 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 OUT_STR(buf);
6820 }
6821 else
6822#endif
6823 {
6824 if (attr > HL_ALL) /* special HL attr. */
6825 {
6826 if (t_colors > 1)
6827 aep = syn_cterm_attr2entry(attr);
6828 else
6829 aep = syn_term_attr2entry(attr);
6830 if (aep == NULL) /* did ":syntax clear" */
6831 attr = 0;
6832 else
6833 attr = aep->ae_attr;
6834 }
6835 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6836 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006837 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6838 && cterm_normal_fg_bold)
6839 /* If the Normal FG color has BOLD attribute and the new HL
6840 * has a FG color defined, clear BOLD. */
6841 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6843 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006844 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6845 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 out_str(T_US);
6847 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6848 out_str(T_CZH);
6849 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6850 out_str(T_MR);
6851
6852 /*
6853 * Output the color or start string after bold etc., in case the
6854 * bold etc. override the color setting.
6855 */
6856 if (aep != NULL)
6857 {
6858 if (t_colors > 1)
6859 {
6860 if (aep->ae_u.cterm.fg_color)
6861 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6862 if (aep->ae_u.cterm.bg_color)
6863 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6864 }
6865 else
6866 {
6867 if (aep->ae_u.term.start != NULL)
6868 out_str(aep->ae_u.term.start);
6869 }
6870 }
6871 }
6872 }
6873}
6874
6875 void
6876screen_stop_highlight()
6877{
6878 int do_ME = FALSE; /* output T_ME code */
6879
6880 if (screen_attr != 0
6881#ifdef WIN3264
6882 && termcap_active
6883#endif
6884 )
6885 {
6886#ifdef FEAT_GUI
6887 if (gui.in_use)
6888 {
6889 char buf[20];
6890
6891 /* use internal GUI code */
6892 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6893 OUT_STR(buf);
6894 }
6895 else
6896#endif
6897 {
6898 if (screen_attr > HL_ALL) /* special HL attr. */
6899 {
6900 attrentry_T *aep;
6901
6902 if (t_colors > 1)
6903 {
6904 /*
6905 * Assume that t_me restores the original colors!
6906 */
6907 aep = syn_cterm_attr2entry(screen_attr);
6908 if (aep != NULL && (aep->ae_u.cterm.fg_color
6909 || aep->ae_u.cterm.bg_color))
6910 do_ME = TRUE;
6911 }
6912 else
6913 {
6914 aep = syn_term_attr2entry(screen_attr);
6915 if (aep != NULL && aep->ae_u.term.stop != NULL)
6916 {
6917 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6918 do_ME = TRUE;
6919 else
6920 out_str(aep->ae_u.term.stop);
6921 }
6922 }
6923 if (aep == NULL) /* did ":syntax clear" */
6924 screen_attr = 0;
6925 else
6926 screen_attr = aep->ae_attr;
6927 }
6928
6929 /*
6930 * Often all ending-codes are equal to T_ME. Avoid outputting the
6931 * same sequence several times.
6932 */
6933 if (screen_attr & HL_STANDOUT)
6934 {
6935 if (STRCMP(T_SE, T_ME) == 0)
6936 do_ME = TRUE;
6937 else
6938 out_str(T_SE);
6939 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006940 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
6942 if (STRCMP(T_UE, T_ME) == 0)
6943 do_ME = TRUE;
6944 else
6945 out_str(T_UE);
6946 }
6947 if (screen_attr & HL_ITALIC)
6948 {
6949 if (STRCMP(T_CZR, T_ME) == 0)
6950 do_ME = TRUE;
6951 else
6952 out_str(T_CZR);
6953 }
6954 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6955 out_str(T_ME);
6956
6957 if (t_colors > 1)
6958 {
6959 /* set Normal cterm colors */
6960 if (cterm_normal_fg_color != 0)
6961 term_fg_color(cterm_normal_fg_color - 1);
6962 if (cterm_normal_bg_color != 0)
6963 term_bg_color(cterm_normal_bg_color - 1);
6964 if (cterm_normal_fg_bold)
6965 out_str(T_MD);
6966 }
6967 }
6968 }
6969 screen_attr = 0;
6970}
6971
6972/*
6973 * Reset the colors for a cterm. Used when leaving Vim.
6974 * The machine specific code may override this again.
6975 */
6976 void
6977reset_cterm_colors()
6978{
6979 if (t_colors > 1)
6980 {
6981 /* set Normal cterm colors */
6982 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6983 {
6984 out_str(T_OP);
6985 screen_attr = -1;
6986 }
6987 if (cterm_normal_fg_bold)
6988 {
6989 out_str(T_ME);
6990 screen_attr = -1;
6991 }
6992 }
6993}
6994
6995/*
6996 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6997 * using the attributes from ScreenAttrs["off"].
6998 */
6999 static void
7000screen_char(off, row, col)
7001 unsigned off;
7002 int row;
7003 int col;
7004{
7005 int attr;
7006
7007 /* Check for illegal values, just in case (could happen just after
7008 * resizing). */
7009 if (row >= screen_Rows || col >= screen_Columns)
7010 return;
7011
7012 /* Outputting the last character on the screen may scrollup the screen.
7013 * Don't to it! Mark the character invalid (update it when scrolled up) */
7014 if (row == screen_Rows - 1 && col == screen_Columns - 1
7015#ifdef FEAT_RIGHTLEFT
7016 /* account for first command-line character in rightleft mode */
7017 && !cmdmsg_rl
7018#endif
7019 )
7020 {
7021 ScreenAttrs[off] = (sattr_T)-1;
7022 return;
7023 }
7024
7025 /*
7026 * Stop highlighting first, so it's easier to move the cursor.
7027 */
7028#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7029 if (screen_char_attr != 0)
7030 attr = screen_char_attr;
7031 else
7032#endif
7033 attr = ScreenAttrs[off];
7034 if (screen_attr != attr)
7035 screen_stop_highlight();
7036
7037 windgoto(row, col);
7038
7039 if (screen_attr != attr)
7040 screen_start_highlight(attr);
7041
7042#ifdef FEAT_MBYTE
7043 if (enc_utf8 && ScreenLinesUC[off] != 0)
7044 {
7045 char_u buf[MB_MAXBYTES + 1];
7046
7047 /* Convert UTF-8 character to bytes and write it. */
7048
7049 buf[utfc_char2bytes(off, buf)] = NUL;
7050
7051 out_str(buf);
7052 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7053 ++screen_cur_col;
7054 }
7055 else
7056#endif
7057 {
7058#ifdef FEAT_MBYTE
7059 out_flush_check();
7060#endif
7061 out_char(ScreenLines[off]);
7062#ifdef FEAT_MBYTE
7063 /* double-byte character in single-width cell */
7064 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7065 out_char(ScreenLines2[off]);
7066#endif
7067 }
7068
7069 screen_cur_col++;
7070}
7071
7072#ifdef FEAT_MBYTE
7073
7074/*
7075 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7076 * on the screen at position 'row' and 'col'.
7077 * The attributes of the first byte is used for all. This is required to
7078 * output the two bytes of a double-byte character with nothing in between.
7079 */
7080 static void
7081screen_char_2(off, row, col)
7082 unsigned off;
7083 int row;
7084 int col;
7085{
7086 /* Check for illegal values (could be wrong when screen was resized). */
7087 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7088 return;
7089
7090 /* Outputting the last character on the screen may scrollup the screen.
7091 * Don't to it! Mark the character invalid (update it when scrolled up) */
7092 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7093 {
7094 ScreenAttrs[off] = (sattr_T)-1;
7095 return;
7096 }
7097
7098 /* Output the first byte normally (positions the cursor), then write the
7099 * second byte directly. */
7100 screen_char(off, row, col);
7101 out_char(ScreenLines[off + 1]);
7102 ++screen_cur_col;
7103}
7104#endif
7105
7106#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7107/*
7108 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7109 * This uses the contents of ScreenLines[] and doesn't change it.
7110 */
7111 void
7112screen_draw_rectangle(row, col, height, width, invert)
7113 int row;
7114 int col;
7115 int height;
7116 int width;
7117 int invert;
7118{
7119 int r, c;
7120 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007121#ifdef FEAT_MBYTE
7122 int max_off;
7123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007125 /* Can't use ScreenLines unless initialized */
7126 if (ScreenLines == NULL)
7127 return;
7128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (invert)
7130 screen_char_attr = HL_INVERSE;
7131 for (r = row; r < row + height; ++r)
7132 {
7133 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007134#ifdef FEAT_MBYTE
7135 max_off = off + screen_Columns;
7136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 for (c = col; c < col + width; ++c)
7138 {
7139#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007140 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {
7142 screen_char_2(off + c, r, c);
7143 ++c;
7144 }
7145 else
7146#endif
7147 {
7148 screen_char(off + c, r, c);
7149#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007150 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 ++c;
7152#endif
7153 }
7154 }
7155 }
7156 screen_char_attr = 0;
7157}
7158#endif
7159
7160#ifdef FEAT_VERTSPLIT
7161/*
7162 * Redraw the characters for a vertically split window.
7163 */
7164 static void
7165redraw_block(row, end, wp)
7166 int row;
7167 int end;
7168 win_T *wp;
7169{
7170 int col;
7171 int width;
7172
7173# ifdef FEAT_CLIPBOARD
7174 clip_may_clear_selection(row, end - 1);
7175# endif
7176
7177 if (wp == NULL)
7178 {
7179 col = 0;
7180 width = Columns;
7181 }
7182 else
7183 {
7184 col = wp->w_wincol;
7185 width = wp->w_width;
7186 }
7187 screen_draw_rectangle(row, col, end - row, width, FALSE);
7188}
7189#endif
7190
7191/*
7192 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7193 * with character 'c1' in first column followed by 'c2' in the other columns.
7194 * Use attributes 'attr'.
7195 */
7196 void
7197screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7198 int start_row, end_row;
7199 int start_col, end_col;
7200 int c1, c2;
7201 int attr;
7202{
7203 int row;
7204 int col;
7205 int off;
7206 int end_off;
7207 int did_delete;
7208 int c;
7209 int norm_term;
7210#if defined(FEAT_GUI) || defined(UNIX)
7211 int force_next = FALSE;
7212#endif
7213
7214 if (end_row > screen_Rows) /* safety check */
7215 end_row = screen_Rows;
7216 if (end_col > screen_Columns) /* safety check */
7217 end_col = screen_Columns;
7218 if (ScreenLines == NULL
7219 || start_row >= end_row
7220 || start_col >= end_col) /* nothing to do */
7221 return;
7222
7223 /* it's a "normal" terminal when not in a GUI or cterm */
7224 norm_term = (
7225#ifdef FEAT_GUI
7226 !gui.in_use &&
7227#endif
7228 t_colors <= 1);
7229 for (row = start_row; row < end_row; ++row)
7230 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007231#ifdef FEAT_MBYTE
7232 if (has_mbyte
7233# ifdef FEAT_GUI
7234 && !gui.in_use
7235# endif
7236 )
7237 {
7238 /* When drawing over the right halve of a double-wide char clear
7239 * out the left halve. When drawing over the left halve of a
7240 * double wide-char clear out the right halve. Only needed in a
7241 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007242 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007243 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007244 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007245 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007246 }
7247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 /*
7249 * Try to use delete-line termcap code, when no attributes or in a
7250 * "normal" terminal, where a bold/italic space is just a
7251 * space.
7252 */
7253 did_delete = FALSE;
7254 if (c2 == ' '
7255 && end_col == Columns
7256 && can_clear(T_CE)
7257 && (attr == 0
7258 || (norm_term
7259 && attr <= HL_ALL
7260 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7261 {
7262 /*
7263 * check if we really need to clear something
7264 */
7265 col = start_col;
7266 if (c1 != ' ') /* don't clear first char */
7267 ++col;
7268
7269 off = LineOffset[row] + col;
7270 end_off = LineOffset[row] + end_col;
7271
7272 /* skip blanks (used often, keep it fast!) */
7273#ifdef FEAT_MBYTE
7274 if (enc_utf8)
7275 while (off < end_off && ScreenLines[off] == ' '
7276 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7277 ++off;
7278 else
7279#endif
7280 while (off < end_off && ScreenLines[off] == ' '
7281 && ScreenAttrs[off] == 0)
7282 ++off;
7283 if (off < end_off) /* something to be cleared */
7284 {
7285 col = off - LineOffset[row];
7286 screen_stop_highlight();
7287 term_windgoto(row, col);/* clear rest of this screen line */
7288 out_str(T_CE);
7289 screen_start(); /* don't know where cursor is now */
7290 col = end_col - col;
7291 while (col--) /* clear chars in ScreenLines */
7292 {
7293 ScreenLines[off] = ' ';
7294#ifdef FEAT_MBYTE
7295 if (enc_utf8)
7296 ScreenLinesUC[off] = 0;
7297#endif
7298 ScreenAttrs[off] = 0;
7299 ++off;
7300 }
7301 }
7302 did_delete = TRUE; /* the chars are cleared now */
7303 }
7304
7305 off = LineOffset[row] + start_col;
7306 c = c1;
7307 for (col = start_col; col < end_col; ++col)
7308 {
7309 if (ScreenLines[off] != c
7310#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007311 || (enc_utf8 && (int)ScreenLinesUC[off]
7312 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313#endif
7314 || ScreenAttrs[off] != attr
7315#if defined(FEAT_GUI) || defined(UNIX)
7316 || force_next
7317#endif
7318 )
7319 {
7320#if defined(FEAT_GUI) || defined(UNIX)
7321 /* The bold trick may make a single row of pixels appear in
7322 * the next character. When a bold character is removed, the
7323 * next character should be redrawn too. This happens for our
7324 * own GUI and for some xterms. */
7325 if (
7326# ifdef FEAT_GUI
7327 gui.in_use
7328# endif
7329# if defined(FEAT_GUI) && defined(UNIX)
7330 ||
7331# endif
7332# ifdef UNIX
7333 term_is_xterm
7334# endif
7335 )
7336 {
7337 if (ScreenLines[off] != ' '
7338 && (ScreenAttrs[off] > HL_ALL
7339 || ScreenAttrs[off] & HL_BOLD))
7340 force_next = TRUE;
7341 else
7342 force_next = FALSE;
7343 }
7344#endif
7345 ScreenLines[off] = c;
7346#ifdef FEAT_MBYTE
7347 if (enc_utf8)
7348 {
7349 if (c >= 0x80)
7350 {
7351 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007352 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 }
7354 else
7355 ScreenLinesUC[off] = 0;
7356 }
7357#endif
7358 ScreenAttrs[off] = attr;
7359 if (!did_delete || c != ' ')
7360 screen_char(off, row, col);
7361 }
7362 ++off;
7363 if (col == start_col)
7364 {
7365 if (did_delete)
7366 break;
7367 c = c2;
7368 }
7369 }
7370 if (end_col == Columns)
7371 LineWraps[row] = FALSE;
7372 if (row == Rows - 1) /* overwritten the command line */
7373 {
7374 redraw_cmdline = TRUE;
7375 if (c1 == ' ' && c2 == ' ')
7376 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007377 if (start_col == 0)
7378 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 }
7380 }
7381}
7382
7383/*
7384 * Check if there should be a delay. Used before clearing or redrawing the
7385 * screen or the command line.
7386 */
7387 void
7388check_for_delay(check_msg_scroll)
7389 int check_msg_scroll;
7390{
7391 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7392 && !did_wait_return
7393 && emsg_silent == 0)
7394 {
7395 out_flush();
7396 ui_delay(1000L, TRUE);
7397 emsg_on_display = FALSE;
7398 if (check_msg_scroll)
7399 msg_scroll = FALSE;
7400 }
7401}
7402
7403/*
7404 * screen_valid - allocate screen buffers if size changed
7405 * If "clear" is TRUE: clear screen if it has been resized.
7406 * Returns TRUE if there is a valid screen to write to.
7407 * Returns FALSE when starting up and screen not initialized yet.
7408 */
7409 int
7410screen_valid(clear)
7411 int clear;
7412{
7413 screenalloc(clear); /* allocate screen buffers if size changed */
7414 return (ScreenLines != NULL);
7415}
7416
7417/*
7418 * Resize the shell to Rows and Columns.
7419 * Allocate ScreenLines[] and associated items.
7420 *
7421 * There may be some time between setting Rows and Columns and (re)allocating
7422 * ScreenLines[]. This happens when starting up and when (manually) changing
7423 * the shell size. Always use screen_Rows and screen_Columns to access items
7424 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7425 * final size of the shell is needed.
7426 */
7427 void
7428screenalloc(clear)
7429 int clear;
7430{
7431 int new_row, old_row;
7432#ifdef FEAT_GUI
7433 int old_Rows;
7434#endif
7435 win_T *wp;
7436 int outofmem = FALSE;
7437 int len;
7438 schar_T *new_ScreenLines;
7439#ifdef FEAT_MBYTE
7440 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007441 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007443 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444#endif
7445 sattr_T *new_ScreenAttrs;
7446 unsigned *new_LineOffset;
7447 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007448#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007449 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007450 tabpage_T *tp;
7451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007453 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007454#ifdef FEAT_AUTOCMD
7455 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007457retry:
7458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 /*
7460 * Allocation of the screen buffers is done only when the size changes and
7461 * when Rows and Columns have been set and we have started doing full
7462 * screen stuff.
7463 */
7464 if ((ScreenLines != NULL
7465 && Rows == screen_Rows
7466 && Columns == screen_Columns
7467#ifdef FEAT_MBYTE
7468 && enc_utf8 == (ScreenLinesUC != NULL)
7469 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007470 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471#endif
7472 )
7473 || Rows == 0
7474 || Columns == 0
7475 || (!full_screen && ScreenLines == NULL))
7476 return;
7477
7478 /*
7479 * It's possible that we produce an out-of-memory message below, which
7480 * will cause this function to be called again. To break the loop, just
7481 * return here.
7482 */
7483 if (entered)
7484 return;
7485 entered = TRUE;
7486
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007487 /*
7488 * Note that the window sizes are updated before reallocating the arrays,
7489 * thus we must not redraw here!
7490 */
7491 ++RedrawingDisabled;
7492
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 win_new_shellsize(); /* fit the windows in the new sized shell */
7494
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 comp_col(); /* recompute columns for shown command and ruler */
7496
7497 /*
7498 * We're changing the size of the screen.
7499 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7500 * - Move lines from the old arrays into the new arrays, clear extra
7501 * lines (unless the screen is going to be cleared).
7502 * - Free the old arrays.
7503 *
7504 * If anything fails, make ScreenLines NULL, so we don't do anything!
7505 * Continuing with the old ScreenLines may result in a crash, because the
7506 * size is wrong.
7507 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007508 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007510#ifdef FEAT_AUTOCMD
7511 if (aucmd_win != NULL)
7512 win_free_lsize(aucmd_win);
7513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
7515 new_ScreenLines = (schar_T *)lalloc((long_u)(
7516 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7517#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007518 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (enc_utf8)
7520 {
7521 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7522 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007523 for (i = 0; i < p_mco; ++i)
7524 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7526 }
7527 if (enc_dbcs == DBCS_JPNU)
7528 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7529 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7530#endif
7531 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7532 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7533 new_LineOffset = (unsigned *)lalloc((long_u)(
7534 Rows * sizeof(unsigned)), FALSE);
7535 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007536#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007537 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007540 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {
7542 if (win_alloc_lines(wp) == FAIL)
7543 {
7544 outofmem = TRUE;
7545#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007546 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547#endif
7548 }
7549 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007550#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007551 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7552 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007553 outofmem = TRUE;
7554#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007555#ifdef FEAT_WINDOWS
7556give_up:
7557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007559#ifdef FEAT_MBYTE
7560 for (i = 0; i < p_mco; ++i)
7561 if (new_ScreenLinesC[i] == NULL)
7562 break;
7563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 if (new_ScreenLines == NULL
7565#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007566 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7568#endif
7569 || new_ScreenAttrs == NULL
7570 || new_LineOffset == NULL
7571 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007572#ifdef FEAT_WINDOWS
7573 || new_TabPageIdxs == NULL
7574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 || outofmem)
7576 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007577 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007578 {
7579 /* guess the size */
7580 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7581
7582 /* Remember we did this to avoid getting outofmem messages over
7583 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007584 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 vim_free(new_ScreenLines);
7587 new_ScreenLines = NULL;
7588#ifdef FEAT_MBYTE
7589 vim_free(new_ScreenLinesUC);
7590 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007591 for (i = 0; i < p_mco; ++i)
7592 {
7593 vim_free(new_ScreenLinesC[i]);
7594 new_ScreenLinesC[i] = NULL;
7595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 vim_free(new_ScreenLines2);
7597 new_ScreenLines2 = NULL;
7598#endif
7599 vim_free(new_ScreenAttrs);
7600 new_ScreenAttrs = NULL;
7601 vim_free(new_LineOffset);
7602 new_LineOffset = NULL;
7603 vim_free(new_LineWraps);
7604 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007605#ifdef FEAT_WINDOWS
7606 vim_free(new_TabPageIdxs);
7607 new_TabPageIdxs = NULL;
7608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
7610 else
7611 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007612 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007613
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 for (new_row = 0; new_row < Rows; ++new_row)
7615 {
7616 new_LineOffset[new_row] = new_row * Columns;
7617 new_LineWraps[new_row] = FALSE;
7618
7619 /*
7620 * If the screen is not going to be cleared, copy as much as
7621 * possible from the old screen to the new one and clear the rest
7622 * (used when resizing the window at the "--more--" prompt or when
7623 * executing an external command, for the GUI).
7624 */
7625 if (!clear)
7626 {
7627 (void)vim_memset(new_ScreenLines + new_row * Columns,
7628 ' ', (size_t)Columns * sizeof(schar_T));
7629#ifdef FEAT_MBYTE
7630 if (enc_utf8)
7631 {
7632 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7633 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007634 for (i = 0; i < p_mco; ++i)
7635 (void)vim_memset(new_ScreenLinesC[i]
7636 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 0, (size_t)Columns * sizeof(u8char_T));
7638 }
7639 if (enc_dbcs == DBCS_JPNU)
7640 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7641 0, (size_t)Columns * sizeof(schar_T));
7642#endif
7643 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7644 0, (size_t)Columns * sizeof(sattr_T));
7645 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007646 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
7648 if (screen_Columns < Columns)
7649 len = screen_Columns;
7650 else
7651 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007652#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007653 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007654 * may be invalid now. Also when p_mco changes. */
7655 if (!(enc_utf8 && ScreenLinesUC == NULL)
7656 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007657#endif
7658 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7659 ScreenLines + LineOffset[old_row],
7660 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007662 if (enc_utf8 && ScreenLinesUC != NULL
7663 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
7665 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7666 ScreenLinesUC + LineOffset[old_row],
7667 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007668 for (i = 0; i < p_mco; ++i)
7669 mch_memmove(new_ScreenLinesC[i]
7670 + new_LineOffset[new_row],
7671 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 (size_t)len * sizeof(u8char_T));
7673 }
7674 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7675 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7676 ScreenLines2 + LineOffset[old_row],
7677 (size_t)len * sizeof(schar_T));
7678#endif
7679 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7680 ScreenAttrs + LineOffset[old_row],
7681 (size_t)len * sizeof(sattr_T));
7682 }
7683 }
7684 }
7685 /* Use the last line of the screen for the current line. */
7686 current_ScreenLine = new_ScreenLines + Rows * Columns;
7687 }
7688
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007689 free_screenlines();
7690
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 ScreenLines = new_ScreenLines;
7692#ifdef FEAT_MBYTE
7693 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007694 for (i = 0; i < p_mco; ++i)
7695 ScreenLinesC[i] = new_ScreenLinesC[i];
7696 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 ScreenLines2 = new_ScreenLines2;
7698#endif
7699 ScreenAttrs = new_ScreenAttrs;
7700 LineOffset = new_LineOffset;
7701 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007702#ifdef FEAT_WINDOWS
7703 TabPageIdxs = new_TabPageIdxs;
7704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705
7706 /* It's important that screen_Rows and screen_Columns reflect the actual
7707 * size of ScreenLines[]. Set them before calling anything. */
7708#ifdef FEAT_GUI
7709 old_Rows = screen_Rows;
7710#endif
7711 screen_Rows = Rows;
7712 screen_Columns = Columns;
7713
7714 must_redraw = CLEAR; /* need to clear the screen later */
7715 if (clear)
7716 screenclear2();
7717
7718#ifdef FEAT_GUI
7719 else if (gui.in_use
7720 && !gui.starting
7721 && ScreenLines != NULL
7722 && old_Rows != Rows)
7723 {
7724 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7725 /*
7726 * Adjust the position of the cursor, for when executing an external
7727 * command.
7728 */
7729 if (msg_row >= Rows) /* Rows got smaller */
7730 msg_row = Rows - 1; /* put cursor at last row */
7731 else if (Rows > old_Rows) /* Rows got bigger */
7732 msg_row += Rows - old_Rows; /* put cursor in same place */
7733 if (msg_col >= Columns) /* Columns got smaller */
7734 msg_col = Columns - 1; /* put cursor at last column */
7735 }
7736#endif
7737
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007739 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007740
7741#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007742 /*
7743 * Do not apply autocommands more than 3 times to avoid an endless loop
7744 * in case applying autocommands always changes Rows or Columns.
7745 */
7746 if (starting == 0 && ++retry_count <= 3)
7747 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007748 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007749 /* In rare cases, autocommands may have altered Rows or Columns,
7750 * jump back to check if we need to allocate the screen again. */
7751 goto retry;
7752 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754}
7755
7756 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007757free_screenlines()
7758{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007759#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007760 int i;
7761
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007762 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007763 for (i = 0; i < Screen_mco; ++i)
7764 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007765 vim_free(ScreenLines2);
7766#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007767 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007768 vim_free(ScreenAttrs);
7769 vim_free(LineOffset);
7770 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007771#ifdef FEAT_WINDOWS
7772 vim_free(TabPageIdxs);
7773#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007774}
7775
7776 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777screenclear()
7778{
7779 check_for_delay(FALSE);
7780 screenalloc(FALSE); /* allocate screen buffers if size changed */
7781 screenclear2(); /* clear the screen */
7782}
7783
7784 static void
7785screenclear2()
7786{
7787 int i;
7788
7789 if (starting == NO_SCREEN || ScreenLines == NULL
7790#ifdef FEAT_GUI
7791 || (gui.in_use && gui.starting)
7792#endif
7793 )
7794 return;
7795
7796#ifdef FEAT_GUI
7797 if (!gui.in_use)
7798#endif
7799 screen_attr = -1; /* force setting the Normal colors */
7800 screen_stop_highlight(); /* don't want highlighting here */
7801
7802#ifdef FEAT_CLIPBOARD
7803 /* disable selection without redrawing it */
7804 clip_scroll_selection(9999);
7805#endif
7806
7807 /* blank out ScreenLines */
7808 for (i = 0; i < Rows; ++i)
7809 {
7810 lineclear(LineOffset[i], (int)Columns);
7811 LineWraps[i] = FALSE;
7812 }
7813
7814 if (can_clear(T_CL))
7815 {
7816 out_str(T_CL); /* clear the display */
7817 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007818 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 }
7820 else
7821 {
7822 /* can't clear the screen, mark all chars with invalid attributes */
7823 for (i = 0; i < Rows; ++i)
7824 lineinvalid(LineOffset[i], (int)Columns);
7825 clear_cmdline = TRUE;
7826 }
7827
7828 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7829
7830 win_rest_invalid(firstwin);
7831 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007832#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007833 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 if (must_redraw == CLEAR) /* no need to clear again */
7836 must_redraw = NOT_VALID;
7837 compute_cmdrow();
7838 msg_row = cmdline_row; /* put cursor on last line for messages */
7839 msg_col = 0;
7840 screen_start(); /* don't know where cursor is now */
7841 msg_scrolled = 0; /* can't scroll back */
7842 msg_didany = FALSE;
7843 msg_didout = FALSE;
7844}
7845
7846/*
7847 * Clear one line in ScreenLines.
7848 */
7849 static void
7850lineclear(off, width)
7851 unsigned off;
7852 int width;
7853{
7854 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7855#ifdef FEAT_MBYTE
7856 if (enc_utf8)
7857 (void)vim_memset(ScreenLinesUC + off, 0,
7858 (size_t)width * sizeof(u8char_T));
7859#endif
7860 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7861}
7862
7863/*
7864 * Mark one line in ScreenLines invalid by setting the attributes to an
7865 * invalid value.
7866 */
7867 static void
7868lineinvalid(off, width)
7869 unsigned off;
7870 int width;
7871{
7872 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7873}
7874
7875#ifdef FEAT_VERTSPLIT
7876/*
7877 * Copy part of a Screenline for vertically split window "wp".
7878 */
7879 static void
7880linecopy(to, from, wp)
7881 int to;
7882 int from;
7883 win_T *wp;
7884{
7885 unsigned off_to = LineOffset[to] + wp->w_wincol;
7886 unsigned off_from = LineOffset[from] + wp->w_wincol;
7887
7888 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7889 wp->w_width * sizeof(schar_T));
7890# ifdef FEAT_MBYTE
7891 if (enc_utf8)
7892 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007893 int i;
7894
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7896 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007897 for (i = 0; i < p_mco; ++i)
7898 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7899 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 }
7901 if (enc_dbcs == DBCS_JPNU)
7902 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7903 wp->w_width * sizeof(schar_T));
7904# endif
7905 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7906 wp->w_width * sizeof(sattr_T));
7907}
7908#endif
7909
7910/*
7911 * Return TRUE if clearing with term string "p" would work.
7912 * It can't work when the string is empty or it won't set the right background.
7913 */
7914 int
7915can_clear(p)
7916 char_u *p;
7917{
7918 return (*p != NUL && (t_colors <= 1
7919#ifdef FEAT_GUI
7920 || gui.in_use
7921#endif
7922 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7923}
7924
7925/*
7926 * Reset cursor position. Use whenever cursor was moved because of outputting
7927 * something directly to the screen (shell commands) or a terminal control
7928 * code.
7929 */
7930 void
7931screen_start()
7932{
7933 screen_cur_row = screen_cur_col = 9999;
7934}
7935
7936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 * Move the cursor to position "row","col" in the screen.
7938 * This tries to find the most efficient way to move, minimizing the number of
7939 * characters sent to the terminal.
7940 */
7941 void
7942windgoto(row, col)
7943 int row;
7944 int col;
7945{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007946 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 int i;
7948 int plan;
7949 int cost;
7950 int wouldbe_col;
7951 int noinvcurs;
7952 char_u *bs;
7953 int goto_cost;
7954 int attr;
7955
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007956#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7958
7959#define PLAN_LE 1
7960#define PLAN_CR 2
7961#define PLAN_NL 3
7962#define PLAN_WRITE 4
7963 /* Can't use ScreenLines unless initialized */
7964 if (ScreenLines == NULL)
7965 return;
7966
7967 if (col != screen_cur_col || row != screen_cur_row)
7968 {
7969 /* Check for valid position. */
7970 if (row < 0) /* window without text lines? */
7971 row = 0;
7972 if (row >= screen_Rows)
7973 row = screen_Rows - 1;
7974 if (col >= screen_Columns)
7975 col = screen_Columns - 1;
7976
7977 /* check if no cursor movement is allowed in highlight mode */
7978 if (screen_attr && *T_MS == NUL)
7979 noinvcurs = HIGHL_COST;
7980 else
7981 noinvcurs = 0;
7982 goto_cost = GOTO_COST + noinvcurs;
7983
7984 /*
7985 * Plan how to do the positioning:
7986 * 1. Use CR to move it to column 0, same row.
7987 * 2. Use T_LE to move it a few columns to the left.
7988 * 3. Use NL to move a few lines down, column 0.
7989 * 4. Move a few columns to the right with T_ND or by writing chars.
7990 *
7991 * Don't do this if the cursor went beyond the last column, the cursor
7992 * position is unknown then (some terminals wrap, some don't )
7993 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007994 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 * characters to move the cursor to the right.
7996 */
7997 if (row >= screen_cur_row && screen_cur_col < Columns)
7998 {
7999 /*
8000 * If the cursor is in the same row, bigger col, we can use CR
8001 * or T_LE.
8002 */
8003 bs = NULL; /* init for GCC */
8004 attr = screen_attr;
8005 if (row == screen_cur_row && col < screen_cur_col)
8006 {
8007 /* "le" is preferred over "bc", because "bc" is obsolete */
8008 if (*T_LE)
8009 bs = T_LE; /* "cursor left" */
8010 else
8011 bs = T_BC; /* "backspace character (old) */
8012 if (*bs)
8013 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8014 else
8015 cost = 999;
8016 if (col + 1 < cost) /* using CR is less characters */
8017 {
8018 plan = PLAN_CR;
8019 wouldbe_col = 0;
8020 cost = 1; /* CR is just one character */
8021 }
8022 else
8023 {
8024 plan = PLAN_LE;
8025 wouldbe_col = col;
8026 }
8027 if (noinvcurs) /* will stop highlighting */
8028 {
8029 cost += noinvcurs;
8030 attr = 0;
8031 }
8032 }
8033
8034 /*
8035 * If the cursor is above where we want to be, we can use CR LF.
8036 */
8037 else if (row > screen_cur_row)
8038 {
8039 plan = PLAN_NL;
8040 wouldbe_col = 0;
8041 cost = (row - screen_cur_row) * 2; /* CR LF */
8042 if (noinvcurs) /* will stop highlighting */
8043 {
8044 cost += noinvcurs;
8045 attr = 0;
8046 }
8047 }
8048
8049 /*
8050 * If the cursor is in the same row, smaller col, just use write.
8051 */
8052 else
8053 {
8054 plan = PLAN_WRITE;
8055 wouldbe_col = screen_cur_col;
8056 cost = 0;
8057 }
8058
8059 /*
8060 * Check if any characters that need to be written have the
8061 * correct attributes. Also avoid UTF-8 characters.
8062 */
8063 i = col - wouldbe_col;
8064 if (i > 0)
8065 cost += i;
8066 if (cost < goto_cost && i > 0)
8067 {
8068 /*
8069 * Check if the attributes are correct without additionally
8070 * stopping highlighting.
8071 */
8072 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8073 while (i && *p++ == attr)
8074 --i;
8075 if (i != 0)
8076 {
8077 /*
8078 * Try if it works when highlighting is stopped here.
8079 */
8080 if (*--p == 0)
8081 {
8082 cost += noinvcurs;
8083 while (i && *p++ == 0)
8084 --i;
8085 }
8086 if (i != 0)
8087 cost = 999; /* different attributes, don't do it */
8088 }
8089#ifdef FEAT_MBYTE
8090 if (enc_utf8)
8091 {
8092 /* Don't use an UTF-8 char for positioning, it's slow. */
8093 for (i = wouldbe_col; i < col; ++i)
8094 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8095 {
8096 cost = 999;
8097 break;
8098 }
8099 }
8100#endif
8101 }
8102
8103 /*
8104 * We can do it without term_windgoto()!
8105 */
8106 if (cost < goto_cost)
8107 {
8108 if (plan == PLAN_LE)
8109 {
8110 if (noinvcurs)
8111 screen_stop_highlight();
8112 while (screen_cur_col > col)
8113 {
8114 out_str(bs);
8115 --screen_cur_col;
8116 }
8117 }
8118 else if (plan == PLAN_CR)
8119 {
8120 if (noinvcurs)
8121 screen_stop_highlight();
8122 out_char('\r');
8123 screen_cur_col = 0;
8124 }
8125 else if (plan == PLAN_NL)
8126 {
8127 if (noinvcurs)
8128 screen_stop_highlight();
8129 while (screen_cur_row < row)
8130 {
8131 out_char('\n');
8132 ++screen_cur_row;
8133 }
8134 screen_cur_col = 0;
8135 }
8136
8137 i = col - screen_cur_col;
8138 if (i > 0)
8139 {
8140 /*
8141 * Use cursor-right if it's one character only. Avoids
8142 * removing a line of pixels from the last bold char, when
8143 * using the bold trick in the GUI.
8144 */
8145 if (T_ND[0] != NUL && T_ND[1] == NUL)
8146 {
8147 while (i-- > 0)
8148 out_char(*T_ND);
8149 }
8150 else
8151 {
8152 int off;
8153
8154 off = LineOffset[row] + screen_cur_col;
8155 while (i-- > 0)
8156 {
8157 if (ScreenAttrs[off] != screen_attr)
8158 screen_stop_highlight();
8159#ifdef FEAT_MBYTE
8160 out_flush_check();
8161#endif
8162 out_char(ScreenLines[off]);
8163#ifdef FEAT_MBYTE
8164 if (enc_dbcs == DBCS_JPNU
8165 && ScreenLines[off] == 0x8e)
8166 out_char(ScreenLines2[off]);
8167#endif
8168 ++off;
8169 }
8170 }
8171 }
8172 }
8173 }
8174 else
8175 cost = 999;
8176
8177 if (cost >= goto_cost)
8178 {
8179 if (noinvcurs)
8180 screen_stop_highlight();
8181 if (row == screen_cur_row && (col > screen_cur_col) &&
8182 *T_CRI != NUL)
8183 term_cursor_right(col - screen_cur_col);
8184 else
8185 term_windgoto(row, col);
8186 }
8187 screen_cur_row = row;
8188 screen_cur_col = col;
8189 }
8190}
8191
8192/*
8193 * Set cursor to its position in the current window.
8194 */
8195 void
8196setcursor()
8197{
8198 if (redrawing())
8199 {
8200 validate_cursor();
8201 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8202 W_WINCOL(curwin) + (
8203#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008204 /* With 'rightleft' set and the cursor on a double-wide
8205 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8207# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008208 (has_mbyte
8209 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8210 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211# endif
8212 1)) :
8213#endif
8214 curwin->w_wcol));
8215 }
8216}
8217
8218
8219/*
8220 * insert 'line_count' lines at 'row' in window 'wp'
8221 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8222 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8223 * scrolling.
8224 * Returns FAIL if the lines are not inserted, OK for success.
8225 */
8226 int
8227win_ins_lines(wp, row, line_count, invalid, mayclear)
8228 win_T *wp;
8229 int row;
8230 int line_count;
8231 int invalid;
8232 int mayclear;
8233{
8234 int did_delete;
8235 int nextrow;
8236 int lastrow;
8237 int retval;
8238
8239 if (invalid)
8240 wp->w_lines_valid = 0;
8241
8242 if (wp->w_height < 5)
8243 return FAIL;
8244
8245 if (line_count > wp->w_height - row)
8246 line_count = wp->w_height - row;
8247
8248 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8249 if (retval != MAYBE)
8250 return retval;
8251
8252 /*
8253 * If there is a next window or a status line, we first try to delete the
8254 * lines at the bottom to avoid messing what is after the window.
8255 * If this fails and there are following windows, don't do anything to avoid
8256 * messing up those windows, better just redraw.
8257 */
8258 did_delete = FALSE;
8259#ifdef FEAT_WINDOWS
8260 if (wp->w_next != NULL || wp->w_status_height)
8261 {
8262 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8263 line_count, (int)Rows, FALSE, NULL) == OK)
8264 did_delete = TRUE;
8265 else if (wp->w_next)
8266 return FAIL;
8267 }
8268#endif
8269 /*
8270 * if no lines deleted, blank the lines that will end up below the window
8271 */
8272 if (!did_delete)
8273 {
8274#ifdef FEAT_WINDOWS
8275 wp->w_redr_status = TRUE;
8276#endif
8277 redraw_cmdline = TRUE;
8278 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8279 lastrow = nextrow + line_count;
8280 if (lastrow > Rows)
8281 lastrow = Rows;
8282 screen_fill(nextrow - line_count, lastrow - line_count,
8283 W_WINCOL(wp), (int)W_ENDCOL(wp),
8284 ' ', ' ', 0);
8285 }
8286
8287 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8288 == FAIL)
8289 {
8290 /* deletion will have messed up other windows */
8291 if (did_delete)
8292 {
8293#ifdef FEAT_WINDOWS
8294 wp->w_redr_status = TRUE;
8295#endif
8296 win_rest_invalid(W_NEXT(wp));
8297 }
8298 return FAIL;
8299 }
8300
8301 return OK;
8302}
8303
8304/*
8305 * delete "line_count" window lines at "row" in window "wp"
8306 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8307 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8308 * scrolling
8309 * Return OK for success, FAIL if the lines are not deleted.
8310 */
8311 int
8312win_del_lines(wp, row, line_count, invalid, mayclear)
8313 win_T *wp;
8314 int row;
8315 int line_count;
8316 int invalid;
8317 int mayclear;
8318{
8319 int retval;
8320
8321 if (invalid)
8322 wp->w_lines_valid = 0;
8323
8324 if (line_count > wp->w_height - row)
8325 line_count = wp->w_height - row;
8326
8327 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8328 if (retval != MAYBE)
8329 return retval;
8330
8331 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8332 (int)Rows, FALSE, NULL) == FAIL)
8333 return FAIL;
8334
8335#ifdef FEAT_WINDOWS
8336 /*
8337 * If there are windows or status lines below, try to put them at the
8338 * correct place. If we can't do that, they have to be redrawn.
8339 */
8340 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8341 {
8342 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8343 line_count, (int)Rows, NULL) == FAIL)
8344 {
8345 wp->w_redr_status = TRUE;
8346 win_rest_invalid(wp->w_next);
8347 }
8348 }
8349 /*
8350 * If this is the last window and there is no status line, redraw the
8351 * command line later.
8352 */
8353 else
8354#endif
8355 redraw_cmdline = TRUE;
8356 return OK;
8357}
8358
8359/*
8360 * Common code for win_ins_lines() and win_del_lines().
8361 * Returns OK or FAIL when the work has been done.
8362 * Returns MAYBE when not finished yet.
8363 */
8364 static int
8365win_do_lines(wp, row, line_count, mayclear, del)
8366 win_T *wp;
8367 int row;
8368 int line_count;
8369 int mayclear;
8370 int del;
8371{
8372 int retval;
8373
8374 if (!redrawing() || line_count <= 0)
8375 return FAIL;
8376
8377 /* only a few lines left: redraw is faster */
8378 if (mayclear && Rows - line_count < 5
8379#ifdef FEAT_VERTSPLIT
8380 && wp->w_width == Columns
8381#endif
8382 )
8383 {
8384 screenclear(); /* will set wp->w_lines_valid to 0 */
8385 return FAIL;
8386 }
8387
8388 /*
8389 * Delete all remaining lines
8390 */
8391 if (row + line_count >= wp->w_height)
8392 {
8393 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8394 W_WINCOL(wp), (int)W_ENDCOL(wp),
8395 ' ', ' ', 0);
8396 return OK;
8397 }
8398
8399 /*
8400 * when scrolling, the message on the command line should be cleared,
8401 * otherwise it will stay there forever.
8402 */
8403 clear_cmdline = TRUE;
8404
8405 /*
8406 * If the terminal can set a scroll region, use that.
8407 * Always do this in a vertically split window. This will redraw from
8408 * ScreenLines[] when t_CV isn't defined. That's faster than using
8409 * win_line().
8410 * Don't use a scroll region when we are going to redraw the text, writing
8411 * a character in the lower right corner of the scroll region causes a
8412 * scroll-up in the DJGPP version.
8413 */
8414 if (scroll_region
8415#ifdef FEAT_VERTSPLIT
8416 || W_WIDTH(wp) != Columns
8417#endif
8418 )
8419 {
8420#ifdef FEAT_VERTSPLIT
8421 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8422#endif
8423 scroll_region_set(wp, row);
8424 if (del)
8425 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8426 wp->w_height - row, FALSE, wp);
8427 else
8428 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8429 wp->w_height - row, wp);
8430#ifdef FEAT_VERTSPLIT
8431 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8432#endif
8433 scroll_region_reset();
8434 return retval;
8435 }
8436
8437#ifdef FEAT_WINDOWS
8438 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8439 return FAIL;
8440#endif
8441
8442 return MAYBE;
8443}
8444
8445/*
8446 * window 'wp' and everything after it is messed up, mark it for redraw
8447 */
8448 static void
8449win_rest_invalid(wp)
8450 win_T *wp;
8451{
8452#ifdef FEAT_WINDOWS
8453 while (wp != NULL)
8454#else
8455 if (wp != NULL)
8456#endif
8457 {
8458 redraw_win_later(wp, NOT_VALID);
8459#ifdef FEAT_WINDOWS
8460 wp->w_redr_status = TRUE;
8461 wp = wp->w_next;
8462#endif
8463 }
8464 redraw_cmdline = TRUE;
8465}
8466
8467/*
8468 * The rest of the routines in this file perform screen manipulations. The
8469 * given operation is performed physically on the screen. The corresponding
8470 * change is also made to the internal screen image. In this way, the editor
8471 * anticipates the effect of editing changes on the appearance of the screen.
8472 * That way, when we call screenupdate a complete redraw isn't usually
8473 * necessary. Another advantage is that we can keep adding code to anticipate
8474 * screen changes, and in the meantime, everything still works.
8475 */
8476
8477/*
8478 * types for inserting or deleting lines
8479 */
8480#define USE_T_CAL 1
8481#define USE_T_CDL 2
8482#define USE_T_AL 3
8483#define USE_T_CE 4
8484#define USE_T_DL 5
8485#define USE_T_SR 6
8486#define USE_NL 7
8487#define USE_T_CD 8
8488#define USE_REDRAW 9
8489
8490/*
8491 * insert lines on the screen and update ScreenLines[]
8492 * 'end' is the line after the scrolled part. Normally it is Rows.
8493 * When scrolling region used 'off' is the offset from the top for the region.
8494 * 'row' and 'end' are relative to the start of the region.
8495 *
8496 * return FAIL for failure, OK for success.
8497 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008498 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499screen_ins_lines(off, row, line_count, end, wp)
8500 int off;
8501 int row;
8502 int line_count;
8503 int end;
8504 win_T *wp; /* NULL or window to use width from */
8505{
8506 int i;
8507 int j;
8508 unsigned temp;
8509 int cursor_row;
8510 int type;
8511 int result_empty;
8512 int can_ce = can_clear(T_CE);
8513
8514 /*
8515 * FAIL if
8516 * - there is no valid screen
8517 * - the screen has to be redrawn completely
8518 * - the line count is less than one
8519 * - the line count is more than 'ttyscroll'
8520 */
8521 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8522 return FAIL;
8523
8524 /*
8525 * There are seven ways to insert lines:
8526 * 0. When in a vertically split window and t_CV isn't set, redraw the
8527 * characters from ScreenLines[].
8528 * 1. Use T_CD (clear to end of display) if it exists and the result of
8529 * the insert is just empty lines
8530 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8531 * present or line_count > 1. It looks better if we do all the inserts
8532 * at once.
8533 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8534 * insert is just empty lines and T_CE is not present or line_count >
8535 * 1.
8536 * 4. Use T_AL (insert line) if it exists.
8537 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8538 * just empty lines.
8539 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8540 * just empty lines.
8541 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8542 * the 'da' flag is not set or we have clear line capability.
8543 * 8. redraw the characters from ScreenLines[].
8544 *
8545 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8546 * the scrollbar for the window. It does have insert line, use that if it
8547 * exists.
8548 */
8549 result_empty = (row + line_count >= end);
8550#ifdef FEAT_VERTSPLIT
8551 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8552 type = USE_REDRAW;
8553 else
8554#endif
8555 if (can_clear(T_CD) && result_empty)
8556 type = USE_T_CD;
8557 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8558 type = USE_T_CAL;
8559 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8560 type = USE_T_CDL;
8561 else if (*T_AL != NUL)
8562 type = USE_T_AL;
8563 else if (can_ce && result_empty)
8564 type = USE_T_CE;
8565 else if (*T_DL != NUL && result_empty)
8566 type = USE_T_DL;
8567 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8568 type = USE_T_SR;
8569 else
8570 return FAIL;
8571
8572 /*
8573 * For clearing the lines screen_del_lines() is used. This will also take
8574 * care of t_db if necessary.
8575 */
8576 if (type == USE_T_CD || type == USE_T_CDL ||
8577 type == USE_T_CE || type == USE_T_DL)
8578 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8579
8580 /*
8581 * If text is retained below the screen, first clear or delete as many
8582 * lines at the bottom of the window as are about to be inserted so that
8583 * the deleted lines won't later surface during a screen_del_lines.
8584 */
8585 if (*T_DB)
8586 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8587
8588#ifdef FEAT_CLIPBOARD
8589 /* Remove a modeless selection when inserting lines halfway the screen
8590 * or not the full width of the screen. */
8591 if (off + row > 0
8592# ifdef FEAT_VERTSPLIT
8593 || (wp != NULL && wp->w_width != Columns)
8594# endif
8595 )
8596 clip_clear_selection();
8597 else
8598 clip_scroll_selection(-line_count);
8599#endif
8600
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601#ifdef FEAT_GUI
8602 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8603 * scrolling is actually carried out. */
8604 gui_dont_update_cursor();
8605#endif
8606
8607 if (*T_CCS != NUL) /* cursor relative to region */
8608 cursor_row = row;
8609 else
8610 cursor_row = row + off;
8611
8612 /*
8613 * Shift LineOffset[] line_count down to reflect the inserted lines.
8614 * Clear the inserted lines in ScreenLines[].
8615 */
8616 row += off;
8617 end += off;
8618 for (i = 0; i < line_count; ++i)
8619 {
8620#ifdef FEAT_VERTSPLIT
8621 if (wp != NULL && wp->w_width != Columns)
8622 {
8623 /* need to copy part of a line */
8624 j = end - 1 - i;
8625 while ((j -= line_count) >= row)
8626 linecopy(j + line_count, j, wp);
8627 j += line_count;
8628 if (can_clear((char_u *)" "))
8629 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8630 else
8631 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8632 LineWraps[j] = FALSE;
8633 }
8634 else
8635#endif
8636 {
8637 j = end - 1 - i;
8638 temp = LineOffset[j];
8639 while ((j -= line_count) >= row)
8640 {
8641 LineOffset[j + line_count] = LineOffset[j];
8642 LineWraps[j + line_count] = LineWraps[j];
8643 }
8644 LineOffset[j + line_count] = temp;
8645 LineWraps[j + line_count] = FALSE;
8646 if (can_clear((char_u *)" "))
8647 lineclear(temp, (int)Columns);
8648 else
8649 lineinvalid(temp, (int)Columns);
8650 }
8651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652
8653 screen_stop_highlight();
8654 windgoto(cursor_row, 0);
8655
8656#ifdef FEAT_VERTSPLIT
8657 /* redraw the characters */
8658 if (type == USE_REDRAW)
8659 redraw_block(row, end, wp);
8660 else
8661#endif
8662 if (type == USE_T_CAL)
8663 {
8664 term_append_lines(line_count);
8665 screen_start(); /* don't know where cursor is now */
8666 }
8667 else
8668 {
8669 for (i = 0; i < line_count; i++)
8670 {
8671 if (type == USE_T_AL)
8672 {
8673 if (i && cursor_row != 0)
8674 windgoto(cursor_row, 0);
8675 out_str(T_AL);
8676 }
8677 else /* type == USE_T_SR */
8678 out_str(T_SR);
8679 screen_start(); /* don't know where cursor is now */
8680 }
8681 }
8682
8683 /*
8684 * With scroll-reverse and 'da' flag set we need to clear the lines that
8685 * have been scrolled down into the region.
8686 */
8687 if (type == USE_T_SR && *T_DA)
8688 {
8689 for (i = 0; i < line_count; ++i)
8690 {
8691 windgoto(off + i, 0);
8692 out_str(T_CE);
8693 screen_start(); /* don't know where cursor is now */
8694 }
8695 }
8696
8697#ifdef FEAT_GUI
8698 gui_can_update_cursor();
8699 if (gui.in_use)
8700 out_flush(); /* always flush after a scroll */
8701#endif
8702 return OK;
8703}
8704
8705/*
8706 * delete lines on the screen and update ScreenLines[]
8707 * 'end' is the line after the scrolled part. Normally it is Rows.
8708 * When scrolling region used 'off' is the offset from the top for the region.
8709 * 'row' and 'end' are relative to the start of the region.
8710 *
8711 * Return OK for success, FAIL if the lines are not deleted.
8712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 int
8714screen_del_lines(off, row, line_count, end, force, wp)
8715 int off;
8716 int row;
8717 int line_count;
8718 int end;
8719 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00008720 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 int j;
8723 int i;
8724 unsigned temp;
8725 int cursor_row;
8726 int cursor_end;
8727 int result_empty; /* result is empty until end of region */
8728 int can_delete; /* deleting line codes can be used */
8729 int type;
8730
8731 /*
8732 * FAIL if
8733 * - there is no valid screen
8734 * - the screen has to be redrawn completely
8735 * - the line count is less than one
8736 * - the line count is more than 'ttyscroll'
8737 */
8738 if (!screen_valid(TRUE) || line_count <= 0 ||
8739 (!force && line_count > p_ttyscroll))
8740 return FAIL;
8741
8742 /*
8743 * Check if the rest of the current region will become empty.
8744 */
8745 result_empty = row + line_count >= end;
8746
8747 /*
8748 * We can delete lines only when 'db' flag not set or when 'ce' option
8749 * available.
8750 */
8751 can_delete = (*T_DB == NUL || can_clear(T_CE));
8752
8753 /*
8754 * There are six ways to delete lines:
8755 * 0. When in a vertically split window and t_CV isn't set, redraw the
8756 * characters from ScreenLines[].
8757 * 1. Use T_CD if it exists and the result is empty.
8758 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8759 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8760 * none of the other ways work.
8761 * 4. Use T_CE (erase line) if the result is empty.
8762 * 5. Use T_DL (delete line) if it exists.
8763 * 6. redraw the characters from ScreenLines[].
8764 */
8765#ifdef FEAT_VERTSPLIT
8766 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8767 type = USE_REDRAW;
8768 else
8769#endif
8770 if (can_clear(T_CD) && result_empty)
8771 type = USE_T_CD;
8772#if defined(__BEOS__) && defined(BEOS_DR8)
8773 /*
8774 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8775 * its internal termcap... this works okay for tests which test *T_DB !=
8776 * NUL. It has the disadvantage that the user cannot use any :set t_*
8777 * command to get T_DB (back) to empty_option, only :set term=... will do
8778 * the trick...
8779 * Anyway, this hack will hopefully go away with the next OS release.
8780 * (Olaf Seibert)
8781 */
8782 else if (row == 0 && T_DB == empty_option
8783 && (line_count == 1 || *T_CDL == NUL))
8784#else
8785 else if (row == 0 && (
8786#ifndef AMIGA
8787 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8788 * up, so use delete-line command */
8789 line_count == 1 ||
8790#endif
8791 *T_CDL == NUL))
8792#endif
8793 type = USE_NL;
8794 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8795 type = USE_T_CDL;
8796 else if (can_clear(T_CE) && result_empty
8797#ifdef FEAT_VERTSPLIT
8798 && (wp == NULL || wp->w_width == Columns)
8799#endif
8800 )
8801 type = USE_T_CE;
8802 else if (*T_DL != NUL && can_delete)
8803 type = USE_T_DL;
8804 else if (*T_CDL != NUL && can_delete)
8805 type = USE_T_CDL;
8806 else
8807 return FAIL;
8808
8809#ifdef FEAT_CLIPBOARD
8810 /* Remove a modeless selection when deleting lines halfway the screen or
8811 * not the full width of the screen. */
8812 if (off + row > 0
8813# ifdef FEAT_VERTSPLIT
8814 || (wp != NULL && wp->w_width != Columns)
8815# endif
8816 )
8817 clip_clear_selection();
8818 else
8819 clip_scroll_selection(line_count);
8820#endif
8821
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822#ifdef FEAT_GUI
8823 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8824 * scrolling is actually carried out. */
8825 gui_dont_update_cursor();
8826#endif
8827
8828 if (*T_CCS != NUL) /* cursor relative to region */
8829 {
8830 cursor_row = row;
8831 cursor_end = end;
8832 }
8833 else
8834 {
8835 cursor_row = row + off;
8836 cursor_end = end + off;
8837 }
8838
8839 /*
8840 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8841 * Clear the inserted lines in ScreenLines[].
8842 */
8843 row += off;
8844 end += off;
8845 for (i = 0; i < line_count; ++i)
8846 {
8847#ifdef FEAT_VERTSPLIT
8848 if (wp != NULL && wp->w_width != Columns)
8849 {
8850 /* need to copy part of a line */
8851 j = row + i;
8852 while ((j += line_count) <= end - 1)
8853 linecopy(j - line_count, j, wp);
8854 j -= line_count;
8855 if (can_clear((char_u *)" "))
8856 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8857 else
8858 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8859 LineWraps[j] = FALSE;
8860 }
8861 else
8862#endif
8863 {
8864 /* whole width, moving the line pointers is faster */
8865 j = row + i;
8866 temp = LineOffset[j];
8867 while ((j += line_count) <= end - 1)
8868 {
8869 LineOffset[j - line_count] = LineOffset[j];
8870 LineWraps[j - line_count] = LineWraps[j];
8871 }
8872 LineOffset[j - line_count] = temp;
8873 LineWraps[j - line_count] = FALSE;
8874 if (can_clear((char_u *)" "))
8875 lineclear(temp, (int)Columns);
8876 else
8877 lineinvalid(temp, (int)Columns);
8878 }
8879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
8881 screen_stop_highlight();
8882
8883#ifdef FEAT_VERTSPLIT
8884 /* redraw the characters */
8885 if (type == USE_REDRAW)
8886 redraw_block(row, end, wp);
8887 else
8888#endif
8889 if (type == USE_T_CD) /* delete the lines */
8890 {
8891 windgoto(cursor_row, 0);
8892 out_str(T_CD);
8893 screen_start(); /* don't know where cursor is now */
8894 }
8895 else if (type == USE_T_CDL)
8896 {
8897 windgoto(cursor_row, 0);
8898 term_delete_lines(line_count);
8899 screen_start(); /* don't know where cursor is now */
8900 }
8901 /*
8902 * Deleting lines at top of the screen or scroll region: Just scroll
8903 * the whole screen (scroll region) up by outputting newlines on the
8904 * last line.
8905 */
8906 else if (type == USE_NL)
8907 {
8908 windgoto(cursor_end - 1, 0);
8909 for (i = line_count; --i >= 0; )
8910 out_char('\n'); /* cursor will remain on same line */
8911 }
8912 else
8913 {
8914 for (i = line_count; --i >= 0; )
8915 {
8916 if (type == USE_T_DL)
8917 {
8918 windgoto(cursor_row, 0);
8919 out_str(T_DL); /* delete a line */
8920 }
8921 else /* type == USE_T_CE */
8922 {
8923 windgoto(cursor_row + i, 0);
8924 out_str(T_CE); /* erase a line */
8925 }
8926 screen_start(); /* don't know where cursor is now */
8927 }
8928 }
8929
8930 /*
8931 * If the 'db' flag is set, we need to clear the lines that have been
8932 * scrolled up at the bottom of the region.
8933 */
8934 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8935 {
8936 for (i = line_count; i > 0; --i)
8937 {
8938 windgoto(cursor_end - i, 0);
8939 out_str(T_CE); /* erase a line */
8940 screen_start(); /* don't know where cursor is now */
8941 }
8942 }
8943
8944#ifdef FEAT_GUI
8945 gui_can_update_cursor();
8946 if (gui.in_use)
8947 out_flush(); /* always flush after a scroll */
8948#endif
8949
8950 return OK;
8951}
8952
8953/*
8954 * show the current mode and ruler
8955 *
8956 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8957 * If clear_cmdline is FALSE there may be a message there that needs to be
8958 * cleared only if a mode is shown.
8959 * Return the length of the message (0 if no message).
8960 */
8961 int
8962showmode()
8963{
8964 int need_clear;
8965 int length = 0;
8966 int do_mode;
8967 int attr;
8968 int nwr_save;
8969#ifdef FEAT_INS_EXPAND
8970 int sub_attr;
8971#endif
8972
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008973 do_mode = ((p_smd && msg_silent == 0)
8974 && ((State & INSERT)
8975 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976#ifdef FEAT_VISUAL
8977 || VIsual_active
8978#endif
8979 ));
8980 if (do_mode || Recording)
8981 {
8982 /*
8983 * Don't show mode right now, when not redrawing or inside a mapping.
8984 * Call char_avail() only when we are going to show something, because
8985 * it takes a bit of time.
8986 */
8987 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8988 {
8989 redraw_cmdline = TRUE; /* show mode later */
8990 return 0;
8991 }
8992
8993 nwr_save = need_wait_return;
8994
8995 /* wait a bit before overwriting an important message */
8996 check_for_delay(FALSE);
8997
8998 /* if the cmdline is more than one line high, erase top lines */
8999 need_clear = clear_cmdline;
9000 if (clear_cmdline && cmdline_row < Rows - 1)
9001 msg_clr_cmdline(); /* will reset clear_cmdline */
9002
9003 /* Position on the last line in the window, column 0 */
9004 msg_pos_mode();
9005 cursor_off();
9006 attr = hl_attr(HLF_CM); /* Highlight mode */
9007 if (do_mode)
9008 {
9009 MSG_PUTS_ATTR("--", attr);
9010#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009011# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 if (xic != NULL && im_get_status() && !p_imdisable
9013 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009014# else
9015 if (
9016# ifdef HAVE_GTK2
9017 preedit_get_status()
9018# else
9019 im_get_status()
9020# endif
9021 )
9022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9024 MSG_PUTS_ATTR(" IM", attr);
9025# else
9026 MSG_PUTS_ATTR(" XIM", attr);
9027# endif
9028#endif
9029#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9030 if (gui.in_use)
9031 {
9032 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009033 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 }
9035#endif
9036#ifdef FEAT_INS_EXPAND
9037 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9038 {
9039 /* These messages can get long, avoid a wrap in a narrow
9040 * window. Prefer showing edit_submode_extra. */
9041 length = (Rows - msg_row) * Columns - 3;
9042 if (edit_submode_extra != NULL)
9043 length -= vim_strsize(edit_submode_extra);
9044 if (length > 0)
9045 {
9046 if (edit_submode_pre != NULL)
9047 length -= vim_strsize(edit_submode_pre);
9048 if (length - vim_strsize(edit_submode) > 0)
9049 {
9050 if (edit_submode_pre != NULL)
9051 msg_puts_attr(edit_submode_pre, attr);
9052 msg_puts_attr(edit_submode, attr);
9053 }
9054 if (edit_submode_extra != NULL)
9055 {
9056 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9057 if ((int)edit_submode_highl < (int)HLF_COUNT)
9058 sub_attr = hl_attr(edit_submode_highl);
9059 else
9060 sub_attr = attr;
9061 msg_puts_attr(edit_submode_extra, sub_attr);
9062 }
9063 }
9064 length = 0;
9065 }
9066 else
9067#endif
9068 {
9069#ifdef FEAT_VREPLACE
9070 if (State & VREPLACE_FLAG)
9071 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9072 else
9073#endif
9074 if (State & REPLACE_FLAG)
9075 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9076 else if (State & INSERT)
9077 {
9078#ifdef FEAT_RIGHTLEFT
9079 if (p_ri)
9080 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9081#endif
9082 MSG_PUTS_ATTR(_(" INSERT"), attr);
9083 }
9084 else if (restart_edit == 'I')
9085 MSG_PUTS_ATTR(_(" (insert)"), attr);
9086 else if (restart_edit == 'R')
9087 MSG_PUTS_ATTR(_(" (replace)"), attr);
9088 else if (restart_edit == 'V')
9089 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9090#ifdef FEAT_RIGHTLEFT
9091 if (p_hkmap)
9092 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9093# ifdef FEAT_FKMAP
9094 if (p_fkmap)
9095 MSG_PUTS_ATTR(farsi_text_5, attr);
9096# endif
9097#endif
9098#ifdef FEAT_KEYMAP
9099 if (State & LANGMAP)
9100 {
9101# ifdef FEAT_ARABIC
9102 if (curwin->w_p_arab)
9103 MSG_PUTS_ATTR(_(" Arabic"), attr);
9104 else
9105# endif
9106 MSG_PUTS_ATTR(_(" (lang)"), attr);
9107 }
9108#endif
9109 if ((State & INSERT) && p_paste)
9110 MSG_PUTS_ATTR(_(" (paste)"), attr);
9111
9112#ifdef FEAT_VISUAL
9113 if (VIsual_active)
9114 {
9115 char *p;
9116
9117 /* Don't concatenate separate words to avoid translation
9118 * problems. */
9119 switch ((VIsual_select ? 4 : 0)
9120 + (VIsual_mode == Ctrl_V) * 2
9121 + (VIsual_mode == 'V'))
9122 {
9123 case 0: p = N_(" VISUAL"); break;
9124 case 1: p = N_(" VISUAL LINE"); break;
9125 case 2: p = N_(" VISUAL BLOCK"); break;
9126 case 4: p = N_(" SELECT"); break;
9127 case 5: p = N_(" SELECT LINE"); break;
9128 default: p = N_(" SELECT BLOCK"); break;
9129 }
9130 MSG_PUTS_ATTR(_(p), attr);
9131 }
9132#endif
9133 MSG_PUTS_ATTR(" --", attr);
9134 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009135
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 need_clear = TRUE;
9137 }
9138 if (Recording
9139#ifdef FEAT_INS_EXPAND
9140 && edit_submode == NULL /* otherwise it gets too long */
9141#endif
9142 )
9143 {
9144 MSG_PUTS_ATTR(_("recording"), attr);
9145 need_clear = TRUE;
9146 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009147
9148 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 if (need_clear || clear_cmdline)
9150 msg_clr_eos();
9151 msg_didout = FALSE; /* overwrite this message */
9152 length = msg_col;
9153 msg_col = 0;
9154 need_wait_return = nwr_save; /* never ask for hit-return for this */
9155 }
9156 else if (clear_cmdline && msg_silent == 0)
9157 /* Clear the whole command line. Will reset "clear_cmdline". */
9158 msg_clr_cmdline();
9159
9160#ifdef FEAT_CMDL_INFO
9161# ifdef FEAT_VISUAL
9162 /* In Visual mode the size of the selected area must be redrawn. */
9163 if (VIsual_active)
9164 clear_showcmd();
9165# endif
9166
9167 /* If the last window has no status line, the ruler is after the mode
9168 * message and must be redrawn */
9169 if (redrawing()
9170# ifdef FEAT_WINDOWS
9171 && lastwin->w_status_height == 0
9172# endif
9173 )
9174 win_redr_ruler(lastwin, TRUE);
9175#endif
9176 redraw_cmdline = FALSE;
9177 clear_cmdline = FALSE;
9178
9179 return length;
9180}
9181
9182/*
9183 * Position for a mode message.
9184 */
9185 static void
9186msg_pos_mode()
9187{
9188 msg_col = 0;
9189 msg_row = Rows - 1;
9190}
9191
9192/*
9193 * Delete mode message. Used when ESC is typed which is expected to end
9194 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009195 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 */
9197 void
9198unshowmode(force)
9199 int force;
9200{
9201 /*
9202 * Don't delete it right now, when not redrawing or insided a mapping.
9203 */
9204 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9205 redraw_cmdline = TRUE; /* delete mode later */
9206 else
9207 {
9208 msg_pos_mode();
9209 if (Recording)
9210 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9211 msg_clr_eos();
9212 }
9213}
9214
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009215#if defined(FEAT_WINDOWS)
9216/*
9217 * Draw the tab pages line at the top of the Vim window.
9218 */
9219 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009220draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009221{
9222 int tabcount = 0;
9223 tabpage_T *tp;
9224 int tabwidth;
9225 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009226 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009227 int attr;
9228 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009229 win_T *cwp;
9230 int wincount;
9231 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009232 int c;
9233 int len;
9234 int attr_sel = hl_attr(HLF_TPS);
9235 int attr_nosel = hl_attr(HLF_TP);
9236 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009237 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009238 int room;
9239 int use_sep_chars = (t_colors < 8
9240#ifdef FEAT_GUI
9241 && !gui.in_use
9242#endif
9243 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009244
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009245 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009246
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009247#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009248 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009249 if (gui_use_tabline())
9250 {
9251 gui_update_tabline();
9252 return;
9253 }
9254#endif
9255
9256 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009257 return;
9258
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009259#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009260
9261 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9262 for (scol = 0; scol < Columns; ++scol)
9263 TabPageIdxs[scol] = 0;
9264
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009265 /* Use the 'tabline' option if it's set. */
9266 if (*p_tal != NUL)
9267 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009268 int save_called_emsg = called_emsg;
9269
9270 /* Check for an error. If there is one we would loop in redrawing the
9271 * screen. Avoid that by making 'tabline' empty. */
9272 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009273 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009274 if (called_emsg)
9275 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009276 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009277 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009278 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009279 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009280#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009281 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009282 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9283 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009284
Bram Moolenaar238a5642006-02-21 22:12:05 +00009285 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9286 if (tabwidth < 6)
9287 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009288
Bram Moolenaar238a5642006-02-21 22:12:05 +00009289 attr = attr_nosel;
9290 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009291 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009292 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9293 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009294 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009295 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009296
Bram Moolenaar238a5642006-02-21 22:12:05 +00009297 if (tp->tp_topframe == topframe)
9298 attr = attr_sel;
9299 if (use_sep_chars && col > 0)
9300 screen_putchar('|', 0, col++, attr);
9301
9302 if (tp->tp_topframe != topframe)
9303 attr = attr_nosel;
9304
9305 screen_putchar(' ', 0, col++, attr);
9306
9307 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009308 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009309 cwp = curwin;
9310 wp = firstwin;
9311 }
9312 else
9313 {
9314 cwp = tp->tp_curwin;
9315 wp = tp->tp_firstwin;
9316 }
9317
9318 modified = FALSE;
9319 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9320 if (bufIsChanged(wp->w_buffer))
9321 modified = TRUE;
9322 if (modified || wincount > 1)
9323 {
9324 if (wincount > 1)
9325 {
9326 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009327 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009328 if (col + len >= Columns - 3)
9329 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009330 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009331#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009332 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009333#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009334 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009335#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009336 );
9337 col += len;
9338 }
9339 if (modified)
9340 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9341 screen_putchar(' ', 0, col++, attr);
9342 }
9343
9344 room = scol - col + tabwidth - 1;
9345 if (room > 0)
9346 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009347 /* Get buffer name in NameBuff[] */
9348 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009349 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009350 len = vim_strsize(NameBuff);
9351 p = NameBuff;
9352#ifdef FEAT_MBYTE
9353 if (has_mbyte)
9354 while (len > room)
9355 {
9356 len -= ptr2cells(p);
9357 mb_ptr_adv(p);
9358 }
9359 else
9360#endif
9361 if (len > room)
9362 {
9363 p += len - room;
9364 len = room;
9365 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009366 if (len > Columns - col - 1)
9367 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009369 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009370 col += len;
9371 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009372 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009373
9374 /* Store the tab page number in TabPageIdxs[], so that
9375 * jump_to_mouse() knows where each one is. */
9376 ++tabcount;
9377 while (scol < col)
9378 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009379 }
9380
Bram Moolenaar238a5642006-02-21 22:12:05 +00009381 if (use_sep_chars)
9382 c = '_';
9383 else
9384 c = ' ';
9385 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009386
9387 /* Put an "X" for closing the current tab if there are several. */
9388 if (first_tabpage->tp_next != NULL)
9389 {
9390 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9391 TabPageIdxs[Columns - 1] = -999;
9392 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009393 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009394
9395 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9396 * set. */
9397 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009398}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009399
9400/*
9401 * Get buffer name for "buf" into NameBuff[].
9402 * Takes care of special buffer names and translates special characters.
9403 */
9404 void
9405get_trans_bufname(buf)
9406 buf_T *buf;
9407{
9408 if (buf_spname(buf) != NULL)
9409 STRCPY(NameBuff, buf_spname(buf));
9410 else
9411 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9412 trans_characters(NameBuff, MAXPATHL);
9413}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009414#endif
9415
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9417/*
9418 * Get the character to use in a status line. Get its attributes in "*attr".
9419 */
9420 static int
9421fillchar_status(attr, is_curwin)
9422 int *attr;
9423 int is_curwin;
9424{
9425 int fill;
9426 if (is_curwin)
9427 {
9428 *attr = hl_attr(HLF_S);
9429 fill = fill_stl;
9430 }
9431 else
9432 {
9433 *attr = hl_attr(HLF_SNC);
9434 fill = fill_stlnc;
9435 }
9436 /* Use fill when there is highlighting, and highlighting of current
9437 * window differs, or the fillchars differ, or this is not the
9438 * current window */
9439 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9440 || !is_curwin || firstwin == lastwin)
9441 || (fill_stl != fill_stlnc)))
9442 return fill;
9443 if (is_curwin)
9444 return '^';
9445 return '=';
9446}
9447#endif
9448
9449#ifdef FEAT_VERTSPLIT
9450/*
9451 * Get the character to use in a separator between vertically split windows.
9452 * Get its attributes in "*attr".
9453 */
9454 static int
9455fillchar_vsep(attr)
9456 int *attr;
9457{
9458 *attr = hl_attr(HLF_C);
9459 if (*attr == 0 && fill_vert == ' ')
9460 return '|';
9461 else
9462 return fill_vert;
9463}
9464#endif
9465
9466/*
9467 * Return TRUE if redrawing should currently be done.
9468 */
9469 int
9470redrawing()
9471{
9472 return (!RedrawingDisabled
9473 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9474}
9475
9476/*
9477 * Return TRUE if printing messages should currently be done.
9478 */
9479 int
9480messaging()
9481{
9482 return (!(p_lz && char_avail() && !KeyTyped));
9483}
9484
9485/*
9486 * Show current status info in ruler and various other places
9487 * If always is FALSE, only show ruler if position has changed.
9488 */
9489 void
9490showruler(always)
9491 int always;
9492{
9493 if (!always && !redrawing())
9494 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009495#ifdef FEAT_INS_EXPAND
9496 if (pum_visible())
9497 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009498# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009499 /* Don't redraw right now, do it later. */
9500 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009501# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009502 return;
9503 }
9504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009506 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009507 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009508 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 else
9511#endif
9512#ifdef FEAT_CMDL_INFO
9513 win_redr_ruler(curwin, always);
9514#endif
9515
9516#ifdef FEAT_TITLE
9517 if (need_maketitle
9518# ifdef FEAT_STL_OPT
9519 || (p_icon && (stl_syntax & STL_IN_ICON))
9520 || (p_title && (stl_syntax & STL_IN_TITLE))
9521# endif
9522 )
9523 maketitle();
9524#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009525#ifdef FEAT_WINDOWS
9526 /* Redraw the tab pages line if needed. */
9527 if (redraw_tabline)
9528 draw_tabline();
9529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530}
9531
9532#ifdef FEAT_CMDL_INFO
9533 static void
9534win_redr_ruler(wp, always)
9535 win_T *wp;
9536 int always;
9537{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009538#define RULER_BUF_LEN 70
9539 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 int row;
9541 int fillchar;
9542 int attr;
9543 int empty_line = FALSE;
9544 colnr_T virtcol;
9545 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009546 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 int o;
9548#ifdef FEAT_VERTSPLIT
9549 int this_ru_col;
9550 int off = 0;
9551 int width = Columns;
9552# define WITH_OFF(x) x
9553# define WITH_WIDTH(x) x
9554#else
9555# define WITH_OFF(x) 0
9556# define WITH_WIDTH(x) Columns
9557# define this_ru_col ru_col
9558#endif
9559
9560 /* If 'ruler' off or redrawing disabled, don't do anything */
9561 if (!p_ru)
9562 return;
9563
9564 /*
9565 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9566 * after deleting lines, before cursor.lnum is corrected.
9567 */
9568 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9569 return;
9570
9571#ifdef FEAT_INS_EXPAND
9572 /* Don't draw the ruler while doing insert-completion, it might overwrite
9573 * the (long) mode message. */
9574# ifdef FEAT_WINDOWS
9575 if (wp == lastwin && lastwin->w_status_height == 0)
9576# endif
9577 if (edit_submode != NULL)
9578 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009579 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9580 if (pum_visible())
9581 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582#endif
9583
9584#ifdef FEAT_STL_OPT
9585 if (*p_ruf)
9586 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009587 int save_called_emsg = called_emsg;
9588
9589 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009591 if (called_emsg)
9592 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009593 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009594 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 return;
9596 }
9597#endif
9598
9599 /*
9600 * Check if not in Insert mode and the line is empty (will show "0-1").
9601 */
9602 if (!(State & INSERT)
9603 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9604 empty_line = TRUE;
9605
9606 /*
9607 * Only draw the ruler when something changed.
9608 */
9609 validate_virtcol_win(wp);
9610 if ( redraw_cmdline
9611 || always
9612 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9613 || wp->w_cursor.col != wp->w_ru_cursor.col
9614 || wp->w_virtcol != wp->w_ru_virtcol
9615#ifdef FEAT_VIRTUALEDIT
9616 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9617#endif
9618 || wp->w_topline != wp->w_ru_topline
9619 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9620#ifdef FEAT_DIFF
9621 || wp->w_topfill != wp->w_ru_topfill
9622#endif
9623 || empty_line != wp->w_ru_empty)
9624 {
9625 cursor_off();
9626#ifdef FEAT_WINDOWS
9627 if (wp->w_status_height)
9628 {
9629 row = W_WINROW(wp) + wp->w_height;
9630 fillchar = fillchar_status(&attr, wp == curwin);
9631# ifdef FEAT_VERTSPLIT
9632 off = W_WINCOL(wp);
9633 width = W_WIDTH(wp);
9634# endif
9635 }
9636 else
9637#endif
9638 {
9639 row = Rows - 1;
9640 fillchar = ' ';
9641 attr = 0;
9642#ifdef FEAT_VERTSPLIT
9643 width = Columns;
9644 off = 0;
9645#endif
9646 }
9647
9648 /* In list mode virtcol needs to be recomputed */
9649 virtcol = wp->w_virtcol;
9650 if (wp->w_p_list && lcs_tab1 == NUL)
9651 {
9652 wp->w_p_list = FALSE;
9653 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9654 wp->w_p_list = TRUE;
9655 }
9656
9657 /*
9658 * Some sprintfs return the length, some return a pointer.
9659 * To avoid portability problems we use strlen() here.
9660 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009661 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9663 ? 0L
9664 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009665 len = STRLEN(buffer);
9666 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9668 (int)virtcol + 1);
9669
9670 /*
9671 * Add a "50%" if there is room for it.
9672 * On the last line, don't print in the last column (scrolls the
9673 * screen up on some terminals).
9674 */
9675 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009676 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 o = i + vim_strsize(buffer + i + 1);
9678#ifdef FEAT_WINDOWS
9679 if (wp->w_status_height == 0) /* can't use last char of screen */
9680#endif
9681 ++o;
9682#ifdef FEAT_VERTSPLIT
9683 this_ru_col = ru_col - (Columns - width);
9684 if (this_ru_col < 0)
9685 this_ru_col = 0;
9686#endif
9687 /* Never use more than half the window/screen width, leave the other
9688 * half for the filename. */
9689 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9690 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9691 if (this_ru_col + o < WITH_WIDTH(width))
9692 {
9693 while (this_ru_col + o < WITH_WIDTH(width))
9694 {
9695#ifdef FEAT_MBYTE
9696 if (has_mbyte)
9697 i += (*mb_char2bytes)(fillchar, buffer + i);
9698 else
9699#endif
9700 buffer[i++] = fillchar;
9701 ++o;
9702 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009703 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 }
9705 /* Truncate at window boundary. */
9706#ifdef FEAT_MBYTE
9707 if (has_mbyte)
9708 {
9709 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009710 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 {
9712 o += (*mb_ptr2cells)(buffer + i);
9713 if (this_ru_col + o > WITH_WIDTH(width))
9714 {
9715 buffer[i] = NUL;
9716 break;
9717 }
9718 }
9719 }
9720 else
9721#endif
9722 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9723 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9724
9725 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9726 i = redraw_cmdline;
9727 screen_fill(row, row + 1,
9728 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9729 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9730 fillchar, fillchar, attr);
9731 /* don't redraw the cmdline because of showing the ruler */
9732 redraw_cmdline = i;
9733 wp->w_ru_cursor = wp->w_cursor;
9734 wp->w_ru_virtcol = wp->w_virtcol;
9735 wp->w_ru_empty = empty_line;
9736 wp->w_ru_topline = wp->w_topline;
9737 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9738#ifdef FEAT_DIFF
9739 wp->w_ru_topfill = wp->w_topfill;
9740#endif
9741 }
9742}
9743#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009744
9745#if defined(FEAT_LINEBREAK) || defined(PROTO)
9746/*
9747 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009748 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009749 * Otherwise it depends on 'numberwidth' and the line count.
9750 */
9751 int
9752number_width(wp)
9753 win_T *wp;
9754{
9755 int n;
9756 linenr_T lnum;
9757
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009758 lnum = wp->w_buffer->b_ml.ml_line_count;
9759 if (lnum == wp->w_nrwidth_line_count)
9760 return wp->w_nrwidth_width;
9761 wp->w_nrwidth_line_count = lnum;
9762
9763 n = 0;
9764 do
9765 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009766 lnum /= 10;
9767 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009768 } while (lnum > 0);
9769
9770 /* 'numberwidth' gives the minimal width plus one */
9771 if (n < wp->w_p_nuw - 1)
9772 n = wp->w_p_nuw - 1;
9773
9774 wp->w_nrwidth_width = n;
9775 return n;
9776}
9777#endif