blob: 9a4ff7dc5d9d0ef4e0e9d80edd6cee8563d3ec3d [file] [log] [blame]
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001/* vi:set ts=8 sts=4 sw=4 noet:
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 * drawline.c: Functions for drawing window lines on the screen.
12 * This is the middle level, drawscreen. is the higher level and screen.c the
13 * lower level.
14 */
15
16#include "vim.h"
17
18#ifdef FEAT_SYN_HL
19/*
20 * Advance **color_cols and return TRUE when there are columns to draw.
21 */
22 static int
23advance_color_col(int vcol, int **color_cols)
24{
25 while (**color_cols >= 0 && vcol > **color_cols)
26 ++*color_cols;
27 return (**color_cols >= 0);
28}
29#endif
30
31#ifdef FEAT_SYN_HL
32/*
33 * Used when 'cursorlineopt' contains "screenline": compute the margins between
34 * which the highlighting is used.
35 */
36 static void
37margin_columns_win(win_T *wp, int *left_col, int *right_col)
38{
39 // cache previous calculations depending on w_virtcol
40 static int saved_w_virtcol;
41 static win_T *prev_wp;
42 static int prev_left_col;
43 static int prev_right_col;
44 static int prev_col_off;
45
46 int cur_col_off = win_col_off(wp);
47 int width1;
48 int width2;
49
50 if (saved_w_virtcol == wp->w_virtcol
51 && prev_wp == wp && prev_col_off == cur_col_off)
52 {
53 *right_col = prev_right_col;
54 *left_col = prev_left_col;
55 return;
56 }
57
58 width1 = wp->w_width - cur_col_off;
59 width2 = width1 + win_col_off2(wp);
60
61 *left_col = 0;
62 *right_col = width1;
63
64 if (wp->w_virtcol >= (colnr_T)width1)
65 *right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
66 if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0)
67 *left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
68
69 // cache values
70 prev_left_col = *left_col;
71 prev_right_col = *right_col;
72 prev_wp = wp;
73 saved_w_virtcol = wp->w_virtcol;
74 prev_col_off = cur_col_off;
75}
76#endif
77
78#ifdef FEAT_SIGNS
79/*
80 * Get information needed to display the sign in line 'lnum' in window 'wp'.
81 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
82 * Otherwise the sign is going to be displayed in the sign column.
83 */
84 static void
85get_sign_display_info(
86 int nrcol,
87 win_T *wp,
88 linenr_T lnum UNUSED,
89 sign_attrs_T *sattr,
90 int wcr_attr,
91 int row,
92 int startrow,
93 int filler_lines UNUSED,
94 int filler_todo UNUSED,
95 int *c_extrap,
96 int *c_finalp,
97 char_u *extra,
98 char_u **pp_extra,
99 int *n_extrap,
100 int *char_attrp)
101{
102 int text_sign;
103# ifdef FEAT_SIGN_ICONS
104 int icon_sign;
105# endif
106
107 // Draw two cells with the sign value or blank.
108 *c_extrap = ' ';
109 *c_finalp = NUL;
110 if (nrcol)
111 *n_extrap = number_width(wp) + 1;
112 else
113 {
114 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
115 *n_extrap = 2;
116 }
117
118 if (row == startrow
119#ifdef FEAT_DIFF
120 + filler_lines && filler_todo <= 0
121#endif
122 )
123 {
124 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
125# ifdef FEAT_SIGN_ICONS
126 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
127 if (gui.in_use && icon_sign != 0)
128 {
129 // Use the image in this position.
130 if (nrcol)
131 {
132 *c_extrap = NUL;
133 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
134 *pp_extra = extra;
135 *n_extrap = (int)STRLEN(*pp_extra);
136 }
137 else
138 *c_extrap = SIGN_BYTE;
139# ifdef FEAT_NETBEANS_INTG
140 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
141 {
142 if (nrcol)
143 {
144 *c_extrap = NUL;
145 sprintf((char *)extra, "%-*c ", number_width(wp),
146 MULTISIGN_BYTE);
147 *pp_extra = extra;
148 *n_extrap = (int)STRLEN(*pp_extra);
149 }
150 else
151 *c_extrap = MULTISIGN_BYTE;
152 }
153# endif
154 *c_finalp = NUL;
155 *char_attrp = icon_sign;
156 }
157 else
158# endif
159 if (text_sign != 0)
160 {
161 *pp_extra = sattr->text;
162 if (*pp_extra != NULL)
163 {
164 if (nrcol)
165 {
166 int n, width = number_width(wp) - 2;
167
168 for (n = 0; n < width; n++)
169 extra[n] = ' ';
170 extra[n] = 0;
171 STRCAT(extra, *pp_extra);
172 STRCAT(extra, " ");
173 *pp_extra = extra;
174 }
175 *c_extrap = NUL;
176 *c_finalp = NUL;
177 *n_extrap = (int)STRLEN(*pp_extra);
178 }
179 *char_attrp = sattr->texthl;
180 }
181 }
182}
183#endif
184
185#ifdef FEAT_TEXT_PROP
186static textprop_T *current_text_props = NULL;
187static buf_T *current_buf = NULL;
188
189 static int
190text_prop_compare(const void *s1, const void *s2)
191{
192 int idx1, idx2;
193 proptype_T *pt1, *pt2;
194 colnr_T col1, col2;
195
196 idx1 = *(int *)s1;
197 idx2 = *(int *)s2;
198 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
199 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
200 if (pt1 == pt2)
201 return 0;
202 if (pt1 == NULL)
203 return -1;
204 if (pt2 == NULL)
205 return 1;
206 if (pt1->pt_priority != pt2->pt_priority)
207 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
208 col1 = current_text_props[idx1].tp_col;
209 col2 = current_text_props[idx2].tp_col;
210 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
211}
212#endif
213
214/*
215 * Display line "lnum" of window 'wp' on the screen.
216 * Start at row "startrow", stop when "endrow" is reached.
217 * wp->w_virtcol needs to be valid.
218 *
219 * Return the number of last row the line occupies.
220 */
221 int
222win_line(
223 win_T *wp,
224 linenr_T lnum,
225 int startrow,
226 int endrow,
227 int nochange UNUSED, // not updating for changed text
228 int number_only) // only update the number column
229{
230 int col = 0; // visual column on screen
231 unsigned off; // offset in ScreenLines/ScreenAttrs
232 int c = 0; // init for GCC
233 long vcol = 0; // virtual column (for tabs)
234#ifdef FEAT_LINEBREAK
235 long vcol_sbr = -1; // virtual column after showbreak
236#endif
237 long vcol_prev = -1; // "vcol" of previous character
238 char_u *line; // current line
239 char_u *ptr; // current position in "line"
240 int row; // row in the window, excl w_winrow
241 int screen_row; // row on the screen, incl w_winrow
242
243 char_u extra[21]; // "%ld " and 'fdc' must fit in here
244 int n_extra = 0; // number of extra chars
245 char_u *p_extra = NULL; // string of extra chars, plus NUL
246 char_u *p_extra_free = NULL; // p_extra needs to be freed
247 int c_extra = NUL; // extra chars, all the same
248 int c_final = NUL; // final char, mandatory if set
249 int extra_attr = 0; // attributes when n_extra != 0
250 static char_u *at_end_str = (char_u *)""; // used for p_extra when
251 // displaying lcs_eol at end-of-line
252 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
253 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
254
255 // saved "extra" items for when draw_state becomes WL_LINE (again)
256 int saved_n_extra = 0;
257 char_u *saved_p_extra = NULL;
258 int saved_c_extra = 0;
259 int saved_c_final = 0;
260 int saved_char_attr = 0;
261
262 int n_attr = 0; // chars with special attr
263 int saved_attr2 = 0; // char_attr saved for n_attr
264 int n_attr3 = 0; // chars with overruling special attr
265 int saved_attr3 = 0; // char_attr saved for n_attr3
266
267 int n_skip = 0; // nr of chars to skip for 'nowrap'
268
269 int fromcol = -10; // start of inverting
270 int tocol = MAXCOL; // end of inverting
271 int fromcol_prev = -2; // start of inverting after cursor
272 int noinvcur = FALSE; // don't invert the cursor
273 pos_T *top, *bot;
274 int lnum_in_visual_area = FALSE;
275 pos_T pos;
276 long v;
277
278 int char_attr = 0; // attributes for next character
279 int attr_pri = FALSE; // char_attr has priority
280 int area_highlighting = FALSE; // Visual or incsearch highlighting
281 // in this line
282 int vi_attr = 0; // attributes for Visual and incsearch
283 // highlighting
284 int wcr_attr = 0; // attributes from 'wincolor'
285 int win_attr = 0; // background for whole window, except
286 // margins and "~" lines.
287 int area_attr = 0; // attributes desired by highlighting
288 int search_attr = 0; // attributes desired by 'hlsearch'
289#ifdef FEAT_SYN_HL
290 int vcol_save_attr = 0; // saved attr for 'cursorcolumn'
291 int syntax_attr = 0; // attributes desired by syntax
Bram Moolenaar9115c612019-10-16 16:57:06 +0200292 int prev_syntax_col = -1; // column of prev_syntax_attr
293 int prev_syntax_attr = 0; // syntax_attr at prev_syntax_col
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200294 int has_syntax = FALSE; // this buffer has syntax highl.
295 int save_did_emsg;
296 int draw_color_col = FALSE; // highlight colorcolumn
297 int *color_cols = NULL; // pointer to according columns array
298#endif
299 int eol_hl_off = 0; // 1 if highlighted char after EOL
300#ifdef FEAT_TEXT_PROP
301 int text_prop_count;
302 int text_prop_next = 0; // next text property to use
303 textprop_T *text_props = NULL;
304 int *text_prop_idxs = NULL;
305 int text_props_active = 0;
306 proptype_T *text_prop_type = NULL;
307 int text_prop_attr = 0;
308 int text_prop_combine = FALSE;
309#endif
310#ifdef FEAT_SPELL
311 int has_spell = FALSE; // this buffer has spell checking
Bram Moolenaar34390282019-10-16 14:38:26 +0200312 int can_spell;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200313# define SPWORDLEN 150
314 char_u nextline[SPWORDLEN * 2];// text with start of the next line
315 int nextlinecol = 0; // column where nextline[] starts
316 int nextline_idx = 0; // index in nextline[] where next line
317 // starts
318 int spell_attr = 0; // attributes desired by spelling
319 int word_end = 0; // last byte with same spell_attr
320 static linenr_T checked_lnum = 0; // line number for "checked_col"
321 static int checked_col = 0; // column in "checked_lnum" up to which
322 // there are no spell errors
323 static int cap_col = -1; // column to check for Cap word
324 static linenr_T capcol_lnum = 0; // line number where "cap_col" used
325 int cur_checked_col = 0; // checked column for current line
326#endif
327 int extra_check = 0; // has extra highlighting
328 int multi_attr = 0; // attributes desired by multibyte
329 int mb_l = 1; // multi-byte byte length
330 int mb_c = 0; // decoded multi-byte character
331 int mb_utf8 = FALSE; // screen char is UTF-8 char
332 int u8cc[MAX_MCO]; // composing UTF-8 chars
333#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
334 int filler_lines = 0; // nr of filler lines to be drawn
335 int filler_todo = 0; // nr of filler lines still to do + 1
336#endif
337#ifdef FEAT_DIFF
338 hlf_T diff_hlf = (hlf_T)0; // type of diff highlighting
339 int change_start = MAXCOL; // first col of changed area
340 int change_end = -1; // last col of changed area
341#endif
342 colnr_T trailcol = MAXCOL; // start of trailing spaces
343#ifdef FEAT_LINEBREAK
344 int need_showbreak = FALSE; // overlong line, skipping first x
345 // chars
346#endif
347#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
348 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
349# define LINE_ATTR
350 int line_attr = 0; // attribute for the whole line
351 int line_attr_save;
352#endif
353#ifdef FEAT_SIGNS
354 int sign_present = FALSE;
355 sign_attrs_T sattr;
356#endif
357#ifdef FEAT_ARABIC
358 int prev_c = 0; // previous Arabic character
359 int prev_c1 = 0; // first composing char for prev_c
360#endif
361#if defined(LINE_ATTR)
362 int did_line_attr = 0;
363#endif
364#ifdef FEAT_TERMINAL
365 int get_term_attr = FALSE;
366#endif
367#ifdef FEAT_SYN_HL
368 int cul_attr = 0; // set when 'cursorline' active
369
370 // 'cursorlineopt' has "screenline" and cursor is in this line
371 int cul_screenline = FALSE;
372
373 // margin columns for the screen line, needed for when 'cursorlineopt'
374 // contains "screenline"
375 int left_curline_col = 0;
376 int right_curline_col = 0;
377#endif
378
379 // draw_state: items that are drawn in sequence:
380#define WL_START 0 // nothing done yet
381#ifdef FEAT_CMDWIN
382# define WL_CMDLINE WL_START + 1 // cmdline window column
383#else
384# define WL_CMDLINE WL_START
385#endif
386#ifdef FEAT_FOLDING
387# define WL_FOLD WL_CMDLINE + 1 // 'foldcolumn'
388#else
389# define WL_FOLD WL_CMDLINE
390#endif
391#ifdef FEAT_SIGNS
392# define WL_SIGN WL_FOLD + 1 // column for signs
393#else
394# define WL_SIGN WL_FOLD // column for signs
395#endif
396#define WL_NR WL_SIGN + 1 // line number
397#ifdef FEAT_LINEBREAK
398# define WL_BRI WL_NR + 1 // 'breakindent'
399#else
400# define WL_BRI WL_NR
401#endif
402#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
403# define WL_SBR WL_BRI + 1 // 'showbreak' or 'diff'
404#else
405# define WL_SBR WL_BRI
406#endif
407#define WL_LINE WL_SBR + 1 // text in the line
408 int draw_state = WL_START; // what to draw next
409#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
410 int feedback_col = 0;
411 int feedback_old_attr = -1;
412#endif
413 int screen_line_flags = 0;
414
415#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
416 int match_conc = 0; // cchar for match functions
417#endif
418#ifdef FEAT_CONCEAL
419 int syntax_flags = 0;
420 int syntax_seqnr = 0;
421 int prev_syntax_id = 0;
422 int conceal_attr = HL_ATTR(HLF_CONCEAL);
423 int is_concealing = FALSE;
424 int boguscols = 0; // nonexistent columns added to force
425 // wrapping
426 int vcol_off = 0; // offset for concealed characters
427 int did_wcol = FALSE;
428 int old_boguscols = 0;
429# define VCOL_HLC (vcol - vcol_off)
430# define FIX_FOR_BOGUSCOLS \
431 { \
432 n_extra += vcol_off; \
433 vcol -= vcol_off; \
434 vcol_off = 0; \
435 col -= boguscols; \
436 old_boguscols = boguscols; \
437 boguscols = 0; \
438 }
439#else
440# define VCOL_HLC (vcol)
441#endif
442
443 if (startrow > endrow) // past the end already!
444 return startrow;
445
446 row = startrow;
447 screen_row = row + W_WINROW(wp);
448
449 if (!number_only)
450 {
451 // To speed up the loop below, set extra_check when there is linebreak,
452 // trailing white space and/or syntax processing to be done.
453#ifdef FEAT_LINEBREAK
454 extra_check = wp->w_p_lbr;
455#endif
456#ifdef FEAT_SYN_HL
457 if (syntax_present(wp) && !wp->w_s->b_syn_error
458# ifdef SYN_TIME_LIMIT
459 && !wp->w_s->b_syn_slow
460# endif
461 )
462 {
463 // Prepare for syntax highlighting in this line. When there is an
464 // error, stop syntax highlighting.
465 save_did_emsg = did_emsg;
466 did_emsg = FALSE;
467 syntax_start(wp, lnum);
468 if (did_emsg)
469 wp->w_s->b_syn_error = TRUE;
470 else
471 {
472 did_emsg = save_did_emsg;
473#ifdef SYN_TIME_LIMIT
474 if (!wp->w_s->b_syn_slow)
475#endif
476 {
477 has_syntax = TRUE;
478 extra_check = TRUE;
479 }
480 }
481 }
482
483 // Check for columns to display for 'colorcolumn'.
484 color_cols = wp->w_p_cc_cols;
485 if (color_cols != NULL)
486 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
487#endif
488
489#ifdef FEAT_TERMINAL
490 if (term_show_buffer(wp->w_buffer))
491 {
492 extra_check = TRUE;
493 get_term_attr = TRUE;
494 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
495 }
496#endif
497
498#ifdef FEAT_SPELL
499 if (wp->w_p_spell
500 && *wp->w_s->b_p_spl != NUL
501 && wp->w_s->b_langp.ga_len > 0
502 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
503 {
504 // Prepare for spell checking.
505 has_spell = TRUE;
506 extra_check = TRUE;
507
508 // Get the start of the next line, so that words that wrap to the
509 // next line are found too: "et<line-break>al.".
510 // Trick: skip a few chars for C/shell/Vim comments
511 nextline[SPWORDLEN] = NUL;
512 if (lnum < wp->w_buffer->b_ml.ml_line_count)
513 {
514 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
515 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
516 }
517
518 // When a word wrapped from the previous line the start of the
519 // current line is valid.
520 if (lnum == checked_lnum)
521 cur_checked_col = checked_col;
522 checked_lnum = 0;
523
524 // When there was a sentence end in the previous line may require a
525 // word starting with capital in this line. In line 1 always check
526 // the first word.
527 if (lnum != capcol_lnum)
528 cap_col = -1;
529 if (lnum == 1)
530 cap_col = 0;
531 capcol_lnum = 0;
532 }
533#endif
534
535 // handle Visual active in this window
536 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
537 {
538 if (LTOREQ_POS(curwin->w_cursor, VIsual))
539 {
540 // Visual is after curwin->w_cursor
541 top = &curwin->w_cursor;
542 bot = &VIsual;
543 }
544 else
545 {
546 // Visual is before curwin->w_cursor
547 top = &VIsual;
548 bot = &curwin->w_cursor;
549 }
550 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
551 if (VIsual_mode == Ctrl_V)
552 {
553 // block mode
554 if (lnum_in_visual_area)
555 {
556 fromcol = wp->w_old_cursor_fcol;
557 tocol = wp->w_old_cursor_lcol;
558 }
559 }
560 else
561 {
562 // non-block mode
563 if (lnum > top->lnum && lnum <= bot->lnum)
564 fromcol = 0;
565 else if (lnum == top->lnum)
566 {
567 if (VIsual_mode == 'V') // linewise
568 fromcol = 0;
569 else
570 {
571 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
572 if (gchar_pos(top) == NUL)
573 tocol = fromcol + 1;
574 }
575 }
576 if (VIsual_mode != 'V' && lnum == bot->lnum)
577 {
578 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
579 {
580 fromcol = -10;
581 tocol = MAXCOL;
582 }
583 else if (bot->col == MAXCOL)
584 tocol = MAXCOL;
585 else
586 {
587 pos = *bot;
588 if (*p_sel == 'e')
589 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
590 else
591 {
592 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
593 ++tocol;
594 }
595 }
596 }
597 }
598
599 // Check if the character under the cursor should not be inverted
600 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
601#ifdef FEAT_GUI
602 && !gui.in_use
603#endif
604 )
605 noinvcur = TRUE;
606
607 // if inverting in this line set area_highlighting
608 if (fromcol >= 0)
609 {
610 area_highlighting = TRUE;
611 vi_attr = HL_ATTR(HLF_V);
612#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
613 if ((clip_star.available && !clip_star.owned
614 && clip_isautosel_star())
615 || (clip_plus.available && !clip_plus.owned
616 && clip_isautosel_plus()))
617 vi_attr = HL_ATTR(HLF_VNC);
618#endif
619 }
620 }
621
622 // handle 'incsearch' and ":s///c" highlighting
623 else if (highlight_match
624 && wp == curwin
625 && lnum >= curwin->w_cursor.lnum
626 && lnum <= curwin->w_cursor.lnum + search_match_lines)
627 {
628 if (lnum == curwin->w_cursor.lnum)
629 getvcol(curwin, &(curwin->w_cursor),
630 (colnr_T *)&fromcol, NULL, NULL);
631 else
632 fromcol = 0;
633 if (lnum == curwin->w_cursor.lnum + search_match_lines)
634 {
635 pos.lnum = lnum;
636 pos.col = search_match_endcol;
637 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
638 }
639 else
640 tocol = MAXCOL;
641 // do at least one character; happens when past end of line
642 if (fromcol == tocol)
643 tocol = fromcol + 1;
644 area_highlighting = TRUE;
645 vi_attr = HL_ATTR(HLF_I);
646 }
647 }
648
649#ifdef FEAT_DIFF
650 filler_lines = diff_check(wp, lnum);
651 if (filler_lines < 0)
652 {
653 if (filler_lines == -1)
654 {
655 if (diff_find_change(wp, lnum, &change_start, &change_end))
656 diff_hlf = HLF_ADD; // added line
657 else if (change_start == 0)
658 diff_hlf = HLF_TXD; // changed text
659 else
660 diff_hlf = HLF_CHD; // changed line
661 }
662 else
663 diff_hlf = HLF_ADD; // added line
664 filler_lines = 0;
665 area_highlighting = TRUE;
666 }
667 if (lnum == wp->w_topline)
668 filler_lines = wp->w_topfill;
669 filler_todo = filler_lines;
670#endif
671
672#ifdef FEAT_SIGNS
673 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
674#endif
675
676#ifdef LINE_ATTR
677# ifdef FEAT_SIGNS
678 // If this line has a sign with line highlighting set line_attr.
679 if (sign_present)
680 line_attr = sattr.linehl;
681# endif
682# if defined(FEAT_QUICKFIX)
683 // Highlight the current line in the quickfix window.
684 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
685 line_attr = HL_ATTR(HLF_QFL);
686# endif
687 if (line_attr != 0)
688 area_highlighting = TRUE;
689#endif
690
691 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
692 ptr = line;
693
694#ifdef FEAT_SPELL
695 if (has_spell && !number_only)
696 {
697 // For checking first word with a capital skip white space.
698 if (cap_col == 0)
699 cap_col = getwhitecols(line);
700
701 // To be able to spell-check over line boundaries copy the end of the
702 // current line into nextline[]. Above the start of the next line was
703 // copied to nextline[SPWORDLEN].
704 if (nextline[SPWORDLEN] == NUL)
705 {
706 // No next line or it is empty.
707 nextlinecol = MAXCOL;
708 nextline_idx = 0;
709 }
710 else
711 {
712 v = (long)STRLEN(line);
713 if (v < SPWORDLEN)
714 {
715 // Short line, use it completely and append the start of the
716 // next line.
717 nextlinecol = 0;
718 mch_memmove(nextline, line, (size_t)v);
719 STRMOVE(nextline + v, nextline + SPWORDLEN);
720 nextline_idx = v + 1;
721 }
722 else
723 {
724 // Long line, use only the last SPWORDLEN bytes.
725 nextlinecol = v - SPWORDLEN;
726 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
727 nextline_idx = SPWORDLEN + 1;
728 }
729 }
730 }
731#endif
732
733 if (wp->w_p_list)
734 {
735 if (lcs_space || lcs_trail || lcs_nbsp)
736 extra_check = TRUE;
737 // find start of trailing whitespace
738 if (lcs_trail)
739 {
740 trailcol = (colnr_T)STRLEN(ptr);
741 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
742 --trailcol;
743 trailcol += (colnr_T) (ptr - line);
744 }
745 }
746
747 wcr_attr = get_wcr_attr(wp);
748 if (wcr_attr != 0)
749 {
750 win_attr = wcr_attr;
751 area_highlighting = TRUE;
752 }
Bram Moolenaar34390282019-10-16 14:38:26 +0200753 if (vi_attr != 0 && win_attr != 0)
754 vi_attr = hl_combine_attr(win_attr, vi_attr);
755
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200756#ifdef FEAT_TEXT_PROP
757 if (WIN_IS_POPUP(wp))
758 screen_line_flags |= SLF_POPUP;
759#endif
760
761 // 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
762 // first character to be displayed.
763 if (wp->w_p_wrap)
764 v = wp->w_skipcol;
765 else
766 v = wp->w_leftcol;
767 if (v > 0 && !number_only)
768 {
769 char_u *prev_ptr = ptr;
770
771 while (vcol < v && *ptr != NUL)
772 {
773 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
774 vcol += c;
775 prev_ptr = ptr;
776 MB_PTR_ADV(ptr);
777 }
778
779 // When:
780 // - 'cuc' is set, or
781 // - 'colorcolumn' is set, or
782 // - 'virtualedit' is set, or
783 // - the visual mode is active,
784 // the end of the line may be before the start of the displayed part.
785 if (vcol < v && (
786#ifdef FEAT_SYN_HL
787 wp->w_p_cuc || draw_color_col ||
788#endif
789 virtual_active() ||
790 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
791 vcol = v;
792
793 // Handle a character that's not completely on the screen: Put ptr at
794 // that character but skip the first few screen characters.
795 if (vcol > v)
796 {
797 vcol -= c;
798 ptr = prev_ptr;
799 // If the character fits on the screen, don't need to skip it.
800 // Except for a TAB.
801 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
802 n_skip = v - vcol;
803 }
804
805 // Adjust for when the inverted text is before the screen,
806 // and when the start of the inverted text is before the screen.
807 if (tocol <= vcol)
808 fromcol = 0;
809 else if (fromcol >= 0 && fromcol < vcol)
810 fromcol = vcol;
811
812#ifdef FEAT_LINEBREAK
813 // When w_skipcol is non-zero, first line needs 'showbreak'
814 if (wp->w_p_wrap)
815 need_showbreak = TRUE;
816#endif
817#ifdef FEAT_SPELL
818 // When spell checking a word we need to figure out the start of the
819 // word and if it's badly spelled or not.
820 if (has_spell)
821 {
822 int len;
823 colnr_T linecol = (colnr_T)(ptr - line);
824 hlf_T spell_hlf = HLF_COUNT;
825
826 pos = wp->w_cursor;
827 wp->w_cursor.lnum = lnum;
828 wp->w_cursor.col = linecol;
829 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
830
831 // spell_move_to() may call ml_get() and make "line" invalid
832 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
833 ptr = line + linecol;
834
835 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
836 {
837 // no bad word found at line start, don't check until end of a
838 // word
839 spell_hlf = HLF_COUNT;
840 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
841 }
842 else
843 {
844 // bad word found, use attributes until end of word
845 word_end = wp->w_cursor.col + len + 1;
846
847 // Turn index into actual attributes.
848 if (spell_hlf != HLF_COUNT)
849 spell_attr = highlight_attr[spell_hlf];
850 }
851 wp->w_cursor = pos;
852
853# ifdef FEAT_SYN_HL
854 // Need to restart syntax highlighting for this line.
855 if (has_syntax)
856 syntax_start(wp, lnum);
857# endif
858 }
859#endif
860 }
861
862 // Correct highlighting for cursor that can't be disabled.
863 // Avoids having to check this for each character.
864 if (fromcol >= 0)
865 {
866 if (noinvcur)
867 {
868 if ((colnr_T)fromcol == wp->w_virtcol)
869 {
870 // highlighting starts at cursor, let it start just after the
871 // cursor
872 fromcol_prev = fromcol;
873 fromcol = -1;
874 }
875 else if ((colnr_T)fromcol < wp->w_virtcol)
876 // restart highlighting after the cursor
877 fromcol_prev = wp->w_virtcol;
878 }
879 if (fromcol >= tocol)
880 fromcol = -1;
881 }
882
883#ifdef FEAT_SEARCH_EXTRA
884 if (!number_only)
885 {
886 v = (long)(ptr - line);
887 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
888 &line, &screen_search_hl,
889 &search_attr);
890 ptr = line + v; // "line" may have been updated
891 }
892#endif
893
894#ifdef FEAT_SYN_HL
895 // Cursor line highlighting for 'cursorline' in the current window.
896 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
897 {
898 // Do not show the cursor line in the text when Visual mode is active,
899 // because it's not clear what is selected then. Do update
900 // w_last_cursorline.
901 if (!(wp == curwin && VIsual_active)
902 && wp->w_p_culopt_flags != CULOPT_NBR)
903 {
904 cul_screenline = (wp->w_p_wrap
905 && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
906
907 // Only set line_attr here when "screenline" is not present in
908 // 'cursorlineopt'. Otherwise it's done later.
909 if (!cul_screenline)
910 {
911 cul_attr = HL_ATTR(HLF_CUL);
912 line_attr = cul_attr;
913 wp->w_last_cursorline = wp->w_cursor.lnum;
914 }
915 else
916 {
917 line_attr_save = line_attr;
918 wp->w_last_cursorline = 0;
919 margin_columns_win(wp, &left_curline_col, &right_curline_col);
920 }
921 area_highlighting = TRUE;
922 }
923 else
924 wp->w_last_cursorline = wp->w_cursor.lnum;
925 }
926#endif
927
928#ifdef FEAT_TEXT_PROP
929 {
930 char_u *prop_start;
931
932 text_prop_count = get_text_props(wp->w_buffer, lnum,
933 &prop_start, FALSE);
934 if (text_prop_count > 0)
935 {
936 // Make a copy of the properties, so that they are properly
937 // aligned.
938 text_props = ALLOC_MULT(textprop_T, text_prop_count);
939 if (text_props != NULL)
940 mch_memmove(text_props, prop_start,
941 text_prop_count * sizeof(textprop_T));
942
943 // Allocate an array for the indexes.
944 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
945 area_highlighting = TRUE;
946 extra_check = TRUE;
947 }
948 }
949#endif
950
951 off = (unsigned)(current_ScreenLine - ScreenLines);
952 col = 0;
953
954#ifdef FEAT_RIGHTLEFT
955 if (wp->w_p_rl)
956 {
957 // Rightleft window: process the text in the normal direction, but put
958 // it in current_ScreenLine[] from right to left. Start at the
959 // rightmost column of the window.
960 col = wp->w_width - 1;
961 off += col;
962 screen_line_flags |= SLF_RIGHTLEFT;
963 }
964#endif
965
966 // Repeat for the whole displayed line.
967 for (;;)
968 {
969#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
970 int has_match_conc = 0; // match wants to conceal
971#endif
972#ifdef FEAT_CONCEAL
973 int did_decrement_ptr = FALSE;
974#endif
975 // Skip this quickly when working on the text.
976 if (draw_state != WL_LINE)
977 {
978#ifdef FEAT_CMDWIN
979 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
980 {
981 draw_state = WL_CMDLINE;
982 if (cmdwin_type != 0 && wp == curwin)
983 {
984 // Draw the cmdline character.
985 n_extra = 1;
986 c_extra = cmdwin_type;
987 c_final = NUL;
988 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
989 }
990 }
991#endif
992
993#ifdef FEAT_FOLDING
994 if (draw_state == WL_FOLD - 1 && n_extra == 0)
995 {
996 int fdc = compute_foldcolumn(wp, 0);
997
998 draw_state = WL_FOLD;
999 if (fdc > 0)
1000 {
1001 // Draw the 'foldcolumn'. Allocate a buffer, "extra" may
1002 // already be in use.
1003 vim_free(p_extra_free);
1004 p_extra_free = alloc(12 + 1);
1005
1006 if (p_extra_free != NULL)
1007 {
1008 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
1009 n_extra = fdc;
1010 p_extra_free[n_extra] = NUL;
1011 p_extra = p_extra_free;
1012 c_extra = NUL;
1013 c_final = NUL;
1014 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
1015 }
1016 }
1017 }
1018#endif
1019
1020#ifdef FEAT_SIGNS
1021 if (draw_state == WL_SIGN - 1 && n_extra == 0)
1022 {
1023 draw_state = WL_SIGN;
1024 // Show the sign column when there are any signs in this
1025 // buffer or when using Netbeans.
1026 if (signcolumn_on(wp))
1027 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
1028 row, startrow, filler_lines, filler_todo, &c_extra,
1029 &c_final, extra, &p_extra, &n_extra, &char_attr);
1030 }
1031#endif
1032
1033 if (draw_state == WL_NR - 1 && n_extra == 0)
1034 {
1035 draw_state = WL_NR;
1036 // Display the absolute or relative line number. After the
1037 // first fill with blanks when the 'n' flag isn't in 'cpo'
1038 if ((wp->w_p_nu || wp->w_p_rnu)
1039 && (row == startrow
1040#ifdef FEAT_DIFF
1041 + filler_lines
1042#endif
1043 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
1044 {
1045#ifdef FEAT_SIGNS
1046 // If 'signcolumn' is set to 'number' and a sign is present
1047 // in 'lnum', then display the sign instead of the line
1048 // number.
1049 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
1050 && sign_present)
1051 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
1052 row, startrow, filler_lines, filler_todo,
1053 &c_extra, &c_final, extra, &p_extra, &n_extra,
1054 &char_attr);
1055 else
1056#endif
1057 {
1058 // Draw the line number (empty space after wrapping).
1059 if (row == startrow
1060#ifdef FEAT_DIFF
1061 + filler_lines
1062#endif
1063 )
1064 {
1065 long num;
1066 char *fmt = "%*ld ";
1067
1068 if (wp->w_p_nu && !wp->w_p_rnu)
1069 // 'number' + 'norelativenumber'
1070 num = (long)lnum;
1071 else
1072 {
1073 // 'relativenumber', don't use negative numbers
1074 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1075 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1076 {
1077 // 'number' + 'relativenumber'
1078 num = lnum;
1079 fmt = "%-*ld ";
1080 }
1081 }
1082
1083 sprintf((char *)extra, fmt,
1084 number_width(wp), num);
1085 if (wp->w_skipcol > 0)
1086 for (p_extra = extra; *p_extra == ' '; ++p_extra)
1087 *p_extra = '-';
1088#ifdef FEAT_RIGHTLEFT
1089 if (wp->w_p_rl) // reverse line numbers
1090 {
1091 char_u *p1, *p2;
1092 int t;
1093
1094 // like rl_mirror(), but keep the space at the end
1095 p2 = skiptowhite(extra) - 1;
1096 for (p1 = extra; p1 < p2; ++p1, --p2)
1097 {
1098 t = *p1;
1099 *p1 = *p2;
1100 *p2 = t;
1101 }
1102 }
1103#endif
1104 p_extra = extra;
1105 c_extra = NUL;
1106 c_final = NUL;
1107 }
1108 else
1109 {
1110 c_extra = ' ';
1111 c_final = NUL;
1112 }
1113 n_extra = number_width(wp) + 1;
1114 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
1115#ifdef FEAT_SYN_HL
1116 // When 'cursorline' is set highlight the line number of
1117 // the current line differently.
1118 // When 'cursorlineopt' has "screenline" only highlight
1119 // the line number itself.
1120 // TODO: Can we use CursorLine instead of CursorLineNr
1121 // when CursorLineNr isn't set?
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001122 if (wp->w_p_cul
1123 && lnum == wp->w_cursor.lnum
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001124 && (wp->w_p_culopt_flags & CULOPT_NBR)
1125 && (row == startrow
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001126 || wp->w_p_culopt_flags & CULOPT_LINE))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001127 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
1128#endif
1129 }
1130 }
1131 }
1132
1133#ifdef FEAT_LINEBREAK
1134 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
1135 && n_extra == 0 && *p_sbr != NUL)
1136 // draw indent after showbreak value
1137 draw_state = WL_BRI;
1138 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
1139 // After the showbreak, draw the breakindent
1140 draw_state = WL_BRI - 1;
1141
1142 // draw 'breakindent': indent wrapped text accordingly
1143 if (draw_state == WL_BRI - 1 && n_extra == 0)
1144 {
1145 draw_state = WL_BRI;
1146 // if need_showbreak is set, breakindent also applies
1147 if (wp->w_p_bri && n_extra == 0
1148 && (row != startrow || need_showbreak)
1149# ifdef FEAT_DIFF
1150 && filler_lines == 0
1151# endif
1152 )
1153 {
1154 char_attr = 0;
1155# ifdef FEAT_DIFF
1156 if (diff_hlf != (hlf_T)0)
1157 {
1158 char_attr = HL_ATTR(diff_hlf);
1159# ifdef FEAT_SYN_HL
1160 if (cul_attr != 0)
1161 char_attr = hl_combine_attr(char_attr, cul_attr);
1162# endif
1163 }
1164# endif
1165 p_extra = NULL;
1166 c_extra = ' ';
1167 n_extra = get_breakindent_win(wp,
1168 ml_get_buf(wp->w_buffer, lnum, FALSE));
1169 // Correct end of highlighted area for 'breakindent',
1170 // required when 'linebreak' is also set.
1171 if (tocol == vcol)
1172 tocol += n_extra;
1173 }
1174 }
1175#endif
1176
1177#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
1178 if (draw_state == WL_SBR - 1 && n_extra == 0)
1179 {
1180 draw_state = WL_SBR;
1181# ifdef FEAT_DIFF
1182 if (filler_todo > 0)
1183 {
1184 // Draw "deleted" diff line(s).
1185 if (char2cells(fill_diff) > 1)
1186 {
1187 c_extra = '-';
1188 c_final = NUL;
1189 }
1190 else
1191 {
1192 c_extra = fill_diff;
1193 c_final = NUL;
1194 }
1195# ifdef FEAT_RIGHTLEFT
1196 if (wp->w_p_rl)
1197 n_extra = col + 1;
1198 else
1199# endif
1200 n_extra = wp->w_width - col;
1201 char_attr = HL_ATTR(HLF_DED);
1202 }
1203# endif
1204# ifdef FEAT_LINEBREAK
1205 if (*p_sbr != NUL && need_showbreak)
1206 {
1207 // Draw 'showbreak' at the start of each broken line.
1208 p_extra = p_sbr;
1209 c_extra = NUL;
1210 c_final = NUL;
1211 n_extra = (int)STRLEN(p_sbr);
1212 char_attr = HL_ATTR(HLF_AT);
1213 need_showbreak = FALSE;
1214 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
1215 // Correct end of highlighted area for 'showbreak',
1216 // required when 'linebreak' is also set.
1217 if (tocol == vcol)
1218 tocol += n_extra;
1219 // combine 'showbreak' with 'wincolor'
1220 if (win_attr != 0)
1221 char_attr = hl_combine_attr(win_attr, char_attr);
1222# ifdef FEAT_SYN_HL
1223 // combine 'showbreak' with 'cursorline'
1224 if (cul_attr != 0)
1225 char_attr = hl_combine_attr(char_attr, cul_attr);
1226# endif
1227 }
1228# endif
1229 }
1230#endif
1231
1232 if (draw_state == WL_LINE - 1 && n_extra == 0)
1233 {
1234 draw_state = WL_LINE;
1235 if (saved_n_extra)
1236 {
1237 // Continue item from end of wrapped line.
1238 n_extra = saved_n_extra;
1239 c_extra = saved_c_extra;
1240 c_final = saved_c_final;
1241 p_extra = saved_p_extra;
1242 char_attr = saved_char_attr;
1243 }
1244 else
1245 char_attr = win_attr;
1246 }
1247 }
1248#ifdef FEAT_SYN_HL
1249 if (cul_screenline)
1250 {
1251 if (draw_state == WL_LINE
1252 && vcol >= left_curline_col
1253 && vcol < right_curline_col)
1254 {
1255 cul_attr = HL_ATTR(HLF_CUL);
1256 line_attr = cul_attr;
1257 }
1258 else
1259 {
1260 cul_attr = 0;
1261 line_attr = line_attr_save;
1262 }
1263 }
1264#endif
1265
1266 // When still displaying '$' of change command, stop at cursor.
1267 // When only displaying the (relative) line number and that's done,
1268 // stop here.
1269 if ((dollar_vcol >= 0 && wp == curwin
1270 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
1271#ifdef FEAT_DIFF
1272 && filler_todo <= 0
1273#endif
1274 )
1275 || (number_only && draw_state > WL_NR))
1276 {
1277 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
1278 screen_line_flags);
1279 // Pretend we have finished updating the window. Except when
1280 // 'cursorcolumn' is set.
1281#ifdef FEAT_SYN_HL
1282 if (wp->w_p_cuc)
1283 row = wp->w_cline_row + wp->w_cline_height;
1284 else
1285#endif
1286 row = wp->w_height;
1287 break;
1288 }
1289
Bram Moolenaar34390282019-10-16 14:38:26 +02001290 if (draw_state == WL_LINE && (area_highlighting || extra_check))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001291 {
1292 // handle Visual or match highlighting in this line
1293 if (vcol == fromcol
1294 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
1295 && (*mb_ptr2cells)(ptr) > 1)
1296 || ((int)vcol_prev == fromcol_prev
1297 && vcol_prev < vcol // not at margin
1298 && vcol < tocol))
1299 area_attr = vi_attr; // start highlighting
1300 else if (area_attr != 0
1301 && (vcol == tocol
1302 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
1303 area_attr = 0; // stop highlighting
1304
1305#ifdef FEAT_SEARCH_EXTRA
1306 if (!n_extra)
1307 {
1308 // Check for start/end of 'hlsearch' and other matches.
1309 // After end, check for start/end of next match.
1310 // When another match, have to check for start again.
1311 v = (long)(ptr - line);
1312 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
1313 &screen_search_hl, &has_match_conc,
1314 &match_conc, did_line_attr, lcs_eol_one);
1315 ptr = line + v; // "line" may have been changed
1316 }
1317#endif
1318
1319#ifdef FEAT_DIFF
1320 if (diff_hlf != (hlf_T)0)
1321 {
1322 if (diff_hlf == HLF_CHD && ptr - line >= change_start
1323 && n_extra == 0)
1324 diff_hlf = HLF_TXD; // changed text
1325 if (diff_hlf == HLF_TXD && ptr - line > change_end
1326 && n_extra == 0)
1327 diff_hlf = HLF_CHD; // changed line
1328 line_attr = HL_ATTR(diff_hlf);
1329 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
1330 && wp->w_p_culopt_flags != CULOPT_NBR
1331 && (!cul_screenline || (vcol >= left_curline_col
1332 && vcol <= right_curline_col)))
1333 line_attr = hl_combine_attr(
1334 line_attr, HL_ATTR(HLF_CUL));
1335 }
1336#endif
1337
1338#ifdef FEAT_TEXT_PROP
1339 if (text_props != NULL)
1340 {
1341 int pi;
1342 int bcol = (int)(ptr - line);
1343
1344 if (n_extra > 0)
1345 --bcol; // still working on the previous char, e.g. Tab
1346
1347 // Check if any active property ends.
1348 for (pi = 0; pi < text_props_active; ++pi)
1349 {
1350 int tpi = text_prop_idxs[pi];
1351
1352 if (bcol >= text_props[tpi].tp_col - 1
1353 + text_props[tpi].tp_len)
1354 {
1355 if (pi + 1 < text_props_active)
1356 mch_memmove(text_prop_idxs + pi,
1357 text_prop_idxs + pi + 1,
1358 sizeof(int)
1359 * (text_props_active - (pi + 1)));
1360 --text_props_active;
1361 --pi;
1362 }
1363 }
1364
1365 // Add any text property that starts in this column.
1366 while (text_prop_next < text_prop_count
1367 && bcol >= text_props[text_prop_next].tp_col - 1)
1368 text_prop_idxs[text_props_active++] = text_prop_next++;
1369
1370 text_prop_attr = 0;
1371 text_prop_combine = FALSE;
Bram Moolenaar053f7122019-09-23 22:17:15 +02001372 text_prop_type = NULL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001373 if (text_props_active > 0)
1374 {
1375 // Sort the properties on priority and/or starting last.
1376 // Then combine the attributes, highest priority last.
1377 current_text_props = text_props;
1378 current_buf = wp->w_buffer;
1379 qsort((void *)text_prop_idxs, (size_t)text_props_active,
1380 sizeof(int), text_prop_compare);
1381
1382 for (pi = 0; pi < text_props_active; ++pi)
1383 {
1384 int tpi = text_prop_idxs[pi];
1385 proptype_T *pt = text_prop_type_by_id(
1386 wp->w_buffer, text_props[tpi].tp_type);
1387
1388 if (pt != NULL && pt->pt_hl_id > 0)
1389 {
1390 int pt_attr = syn_id2attr(pt->pt_hl_id);
1391
1392 text_prop_type = pt;
1393 text_prop_attr =
1394 hl_combine_attr(text_prop_attr, pt_attr);
1395 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
1396 }
1397 }
1398 }
1399 }
1400#endif
1401
Bram Moolenaar34390282019-10-16 14:38:26 +02001402 if (extra_check)
1403 {
1404#ifdef FEAT_TERMINAL
1405 if (get_term_attr)
Bram Moolenaar34390282019-10-16 14:38:26 +02001406 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar34390282019-10-16 14:38:26 +02001407#endif
1408
1409#ifdef FEAT_SYN_HL
1410 // Get syntax attribute.
1411 if (has_syntax)
1412 {
1413 // Get the syntax attribute for the character. If there
1414 // is an error, disable syntax highlighting.
1415 save_did_emsg = did_emsg;
1416 did_emsg = FALSE;
1417
1418 v = (long)(ptr - line);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001419 if (v == prev_syntax_col)
1420 // at same column again
1421 syntax_attr = prev_syntax_attr;
1422 else
1423 {
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001424# ifdef FEAT_SPELL
Bram Moolenaar9115c612019-10-16 16:57:06 +02001425 can_spell = TRUE;
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001426# endif
Bram Moolenaar9115c612019-10-16 16:57:06 +02001427 syntax_attr = get_syntax_attr((colnr_T)v,
Bram Moolenaar34390282019-10-16 14:38:26 +02001428# ifdef FEAT_SPELL
1429 has_spell ? &can_spell :
1430# endif
1431 NULL, FALSE);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001432 prev_syntax_col = v;
1433 prev_syntax_attr = syntax_attr;
1434 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001435
1436 // combine syntax attribute with 'wincolor'
1437 if (syntax_attr != 0 && win_attr != 0)
1438 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
1439
1440 if (did_emsg)
1441 {
1442 wp->w_s->b_syn_error = TRUE;
1443 has_syntax = FALSE;
1444 syntax_attr = 0;
1445 }
1446 else
1447 did_emsg = save_did_emsg;
1448# ifdef SYN_TIME_LIMIT
1449 if (wp->w_s->b_syn_slow)
1450 has_syntax = FALSE;
1451# endif
1452
1453 // Need to get the line again, a multi-line regexp may
1454 // have made it invalid.
1455 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1456 ptr = line + v;
1457# ifdef FEAT_CONCEAL
1458 // no concealing past the end of the line, it interferes
1459 // with line highlighting
1460 if (*ptr == NUL)
1461 syntax_flags = 0;
1462 else
1463 syntax_flags = get_syntax_info(&syntax_seqnr);
1464# endif
1465 }
1466#endif
1467 }
1468
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001469 // Decide which of the highlight attributes to use.
1470 attr_pri = TRUE;
1471#ifdef LINE_ATTR
1472 if (area_attr != 0)
1473 char_attr = hl_combine_attr(line_attr, area_attr);
1474 else if (search_attr != 0)
1475 char_attr = hl_combine_attr(line_attr, search_attr);
1476# ifdef FEAT_TEXT_PROP
1477 else if (text_prop_type != NULL)
1478 {
Bram Moolenaar053f7122019-09-23 22:17:15 +02001479 char_attr = hl_combine_attr(line_attr != 0
1480 ? line_attr
1481 : syntax_attr != 0
1482 ? syntax_attr
1483 : win_attr, text_prop_attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001484 }
1485# endif
1486 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
1487 || vcol < fromcol || vcol_prev < fromcol_prev
1488 || vcol >= tocol))
1489 {
1490 // Use line_attr when not in the Visual or 'incsearch' area
1491 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar34390282019-10-16 14:38:26 +02001492# ifdef FEAT_SYN_HL
1493 if (has_syntax)
1494 char_attr = hl_combine_attr(syntax_attr, line_attr);
1495 else
1496# endif
1497 char_attr = line_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001498 attr_pri = FALSE;
1499 }
1500#else
1501 if (area_attr != 0)
1502 char_attr = area_attr;
1503 else if (search_attr != 0)
1504 char_attr = search_attr;
1505#endif
1506 else
1507 {
1508 attr_pri = FALSE;
1509#ifdef FEAT_TEXT_PROP
1510 if (text_prop_type != NULL)
1511 {
1512 if (text_prop_combine)
1513 char_attr = hl_combine_attr(
1514 syntax_attr, text_prop_attr);
1515 else
1516 char_attr = hl_combine_attr(
1517 win_attr, text_prop_attr);
1518 }
1519 else
1520#endif
1521#ifdef FEAT_SYN_HL
Bram Moolenaara6cc5be2019-10-16 18:42:26 +02001522 if (has_syntax
1523# ifdef FEAT_TERMINAL
1524 || get_term_attr
1525# endif
1526 )
1527 char_attr = syntax_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001528 else
1529#endif
1530 char_attr = 0;
1531 }
1532 }
1533 if (char_attr == 0)
1534 char_attr = win_attr;
1535
1536 // Get the next character to put on the screen.
1537
1538 // The "p_extra" points to the extra stuff that is inserted to
1539 // represent special characters (non-printable stuff) and other
1540 // things. When all characters are the same, c_extra is used.
1541 // If c_final is set, it will compulsorily be used at the end.
1542 // "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
1543 // "p_extra[n_extra]".
1544 // For the '$' of the 'list' option, n_extra == 1, p_extra == "".
1545 if (n_extra > 0)
1546 {
1547 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
1548 {
1549 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
1550 mb_c = c; // doesn't handle non-utf-8 multi-byte!
1551 if (enc_utf8 && utf_char2len(c) > 1)
1552 {
1553 mb_utf8 = TRUE;
1554 u8cc[0] = 0;
1555 c = 0xc0;
1556 }
1557 else
1558 mb_utf8 = FALSE;
1559 }
1560 else
1561 {
1562 c = *p_extra;
1563 if (has_mbyte)
1564 {
1565 mb_c = c;
1566 if (enc_utf8)
1567 {
1568 // If the UTF-8 character is more than one byte:
1569 // Decode it into "mb_c".
1570 mb_l = utfc_ptr2len(p_extra);
1571 mb_utf8 = FALSE;
1572 if (mb_l > n_extra)
1573 mb_l = 1;
1574 else if (mb_l > 1)
1575 {
1576 mb_c = utfc_ptr2char(p_extra, u8cc);
1577 mb_utf8 = TRUE;
1578 c = 0xc0;
1579 }
1580 }
1581 else
1582 {
1583 // if this is a DBCS character, put it in "mb_c"
1584 mb_l = MB_BYTE2LEN(c);
1585 if (mb_l >= n_extra)
1586 mb_l = 1;
1587 else if (mb_l > 1)
1588 mb_c = (c << 8) + p_extra[1];
1589 }
1590 if (mb_l == 0) // at the NUL at end-of-line
1591 mb_l = 1;
1592
1593 // If a double-width char doesn't fit display a '>' in the
1594 // last column.
1595 if ((
1596# ifdef FEAT_RIGHTLEFT
1597 wp->w_p_rl ? (col <= 0) :
1598# endif
1599 (col >= wp->w_width - 1))
1600 && (*mb_char2cells)(mb_c) == 2)
1601 {
1602 c = '>';
1603 mb_c = c;
1604 mb_l = 1;
1605 mb_utf8 = FALSE;
1606 multi_attr = HL_ATTR(HLF_AT);
1607#ifdef FEAT_SYN_HL
1608 if (cul_attr)
1609 multi_attr = hl_combine_attr(multi_attr, cul_attr);
1610#endif
1611 // put the pointer back to output the double-width
1612 // character at the start of the next line.
1613 ++n_extra;
1614 --p_extra;
1615 }
1616 else
1617 {
1618 n_extra -= mb_l - 1;
1619 p_extra += mb_l - 1;
1620 }
1621 }
1622 ++p_extra;
1623 }
1624 --n_extra;
1625 }
1626 else
1627 {
1628#ifdef FEAT_LINEBREAK
1629 int c0;
1630#endif
1631
1632 if (p_extra_free != NULL)
1633 VIM_CLEAR(p_extra_free);
1634 // Get a character from the line itself.
1635 c = *ptr;
1636#ifdef FEAT_LINEBREAK
1637 c0 = *ptr;
1638#endif
1639 if (has_mbyte)
1640 {
1641 mb_c = c;
1642 if (enc_utf8)
1643 {
1644 // If the UTF-8 character is more than one byte: Decode it
1645 // into "mb_c".
1646 mb_l = utfc_ptr2len(ptr);
1647 mb_utf8 = FALSE;
1648 if (mb_l > 1)
1649 {
1650 mb_c = utfc_ptr2char(ptr, u8cc);
1651 // Overlong encoded ASCII or ASCII with composing char
1652 // is displayed normally, except a NUL.
1653 if (mb_c < 0x80)
1654 {
1655 c = mb_c;
1656#ifdef FEAT_LINEBREAK
1657 c0 = mb_c;
1658#endif
1659 }
1660 mb_utf8 = TRUE;
1661
1662 // At start of the line we can have a composing char.
1663 // Draw it as a space with a composing char.
1664 if (utf_iscomposing(mb_c))
1665 {
1666 int i;
1667
1668 for (i = Screen_mco - 1; i > 0; --i)
1669 u8cc[i] = u8cc[i - 1];
1670 u8cc[0] = mb_c;
1671 mb_c = ' ';
1672 }
1673 }
1674
1675 if ((mb_l == 1 && c >= 0x80)
1676 || (mb_l >= 1 && mb_c == 0)
1677 || (mb_l > 1 && (!vim_isprintc(mb_c))))
1678 {
1679 // Illegal UTF-8 byte: display as <xx>.
1680 // Non-BMP character : display as ? or fullwidth ?.
1681 transchar_hex(extra, mb_c);
1682# ifdef FEAT_RIGHTLEFT
1683 if (wp->w_p_rl) // reverse
1684 rl_mirror(extra);
1685# endif
1686 p_extra = extra;
1687 c = *p_extra;
1688 mb_c = mb_ptr2char_adv(&p_extra);
1689 mb_utf8 = (c >= 0x80);
1690 n_extra = (int)STRLEN(p_extra);
1691 c_extra = NUL;
1692 c_final = NUL;
1693 if (area_attr == 0 && search_attr == 0)
1694 {
1695 n_attr = n_extra + 1;
1696 extra_attr = HL_ATTR(HLF_8);
1697 saved_attr2 = char_attr; // save current attr
1698 }
1699 }
1700 else if (mb_l == 0) // at the NUL at end-of-line
1701 mb_l = 1;
1702#ifdef FEAT_ARABIC
1703 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
1704 {
1705 // Do Arabic shaping.
1706 int pc, pc1, nc;
1707 int pcc[MAX_MCO];
1708
1709 // The idea of what is the previous and next
1710 // character depends on 'rightleft'.
1711 if (wp->w_p_rl)
1712 {
1713 pc = prev_c;
1714 pc1 = prev_c1;
1715 nc = utf_ptr2char(ptr + mb_l);
1716 prev_c1 = u8cc[0];
1717 }
1718 else
1719 {
1720 pc = utfc_ptr2char(ptr + mb_l, pcc);
1721 nc = prev_c;
1722 pc1 = pcc[0];
1723 }
1724 prev_c = mb_c;
1725
1726 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
1727 }
1728 else
1729 prev_c = mb_c;
1730#endif
1731 }
1732 else // enc_dbcs
1733 {
1734 mb_l = MB_BYTE2LEN(c);
1735 if (mb_l == 0) // at the NUL at end-of-line
1736 mb_l = 1;
1737 else if (mb_l > 1)
1738 {
1739 // We assume a second byte below 32 is illegal.
1740 // Hopefully this is OK for all double-byte encodings!
1741 if (ptr[1] >= 32)
1742 mb_c = (c << 8) + ptr[1];
1743 else
1744 {
1745 if (ptr[1] == NUL)
1746 {
1747 // head byte at end of line
1748 mb_l = 1;
1749 transchar_nonprint(extra, c);
1750 }
1751 else
1752 {
1753 // illegal tail byte
1754 mb_l = 2;
1755 STRCPY(extra, "XX");
1756 }
1757 p_extra = extra;
1758 n_extra = (int)STRLEN(extra) - 1;
1759 c_extra = NUL;
1760 c_final = NUL;
1761 c = *p_extra++;
1762 if (area_attr == 0 && search_attr == 0)
1763 {
1764 n_attr = n_extra + 1;
1765 extra_attr = HL_ATTR(HLF_8);
1766 saved_attr2 = char_attr; // save current attr
1767 }
1768 mb_c = c;
1769 }
1770 }
1771 }
1772 // If a double-width char doesn't fit display a '>' in the
1773 // last column; the character is displayed at the start of the
1774 // next line.
1775 if ((
1776# ifdef FEAT_RIGHTLEFT
1777 wp->w_p_rl ? (col <= 0) :
1778# endif
1779 (col >= wp->w_width - 1))
1780 && (*mb_char2cells)(mb_c) == 2)
1781 {
1782 c = '>';
1783 mb_c = c;
1784 mb_utf8 = FALSE;
1785 mb_l = 1;
1786 multi_attr = HL_ATTR(HLF_AT);
1787 // Put pointer back so that the character will be
1788 // displayed at the start of the next line.
1789 --ptr;
1790#ifdef FEAT_CONCEAL
1791 did_decrement_ptr = TRUE;
1792#endif
1793 }
1794 else if (*ptr != NUL)
1795 ptr += mb_l - 1;
1796
1797 // If a double-width char doesn't fit at the left side display
1798 // a '<' in the first column. Don't do this for unprintable
1799 // characters.
1800 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
1801 {
1802 n_extra = 1;
1803 c_extra = MB_FILLER_CHAR;
1804 c_final = NUL;
1805 c = ' ';
1806 if (area_attr == 0 && search_attr == 0)
1807 {
1808 n_attr = n_extra + 1;
1809 extra_attr = HL_ATTR(HLF_AT);
1810 saved_attr2 = char_attr; // save current attr
1811 }
1812 mb_c = c;
1813 mb_utf8 = FALSE;
1814 mb_l = 1;
1815 }
1816
1817 }
1818 ++ptr;
1819
1820 if (extra_check)
1821 {
1822#ifdef FEAT_SPELL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001823 // Check spelling (unless at the end of the line).
1824 // Only do this when there is no syntax highlighting, the
1825 // @Spell cluster is not used or the current syntax item
1826 // contains the @Spell cluster.
1827 if (has_spell && v >= word_end && v > cur_checked_col)
1828 {
1829 spell_attr = 0;
1830 if (c != 0 && (
1831# ifdef FEAT_SYN_HL
1832 !has_syntax ||
1833# endif
1834 can_spell))
1835 {
1836 char_u *prev_ptr, *p;
1837 int len;
1838 hlf_T spell_hlf = HLF_COUNT;
1839 if (has_mbyte)
1840 {
1841 prev_ptr = ptr - mb_l;
1842 v -= mb_l - 1;
1843 }
1844 else
1845 prev_ptr = ptr - 1;
1846
1847 // Use nextline[] if possible, it has the start of the
1848 // next line concatenated.
1849 if ((prev_ptr - line) - nextlinecol >= 0)
1850 p = nextline + (prev_ptr - line) - nextlinecol;
1851 else
1852 p = prev_ptr;
1853 cap_col -= (int)(prev_ptr - line);
1854 len = spell_check(wp, p, &spell_hlf, &cap_col,
1855 nochange);
1856 word_end = v + len;
1857
1858 // In Insert mode only highlight a word that
1859 // doesn't touch the cursor.
1860 if (spell_hlf != HLF_COUNT
1861 && (State & INSERT) != 0
1862 && wp->w_cursor.lnum == lnum
1863 && wp->w_cursor.col >=
1864 (colnr_T)(prev_ptr - line)
1865 && wp->w_cursor.col < (colnr_T)word_end)
1866 {
1867 spell_hlf = HLF_COUNT;
1868 spell_redraw_lnum = lnum;
1869 }
1870
1871 if (spell_hlf == HLF_COUNT && p != prev_ptr
1872 && (p - nextline) + len > nextline_idx)
1873 {
1874 // Remember that the good word continues at the
1875 // start of the next line.
1876 checked_lnum = lnum + 1;
1877 checked_col = (int)((p - nextline) + len - nextline_idx);
1878 }
1879
1880 // Turn index into actual attributes.
1881 if (spell_hlf != HLF_COUNT)
1882 spell_attr = highlight_attr[spell_hlf];
1883
1884 if (cap_col > 0)
1885 {
1886 if (p != prev_ptr
1887 && (p - nextline) + cap_col >= nextline_idx)
1888 {
1889 // Remember that the word in the next line
1890 // must start with a capital.
1891 capcol_lnum = lnum + 1;
1892 cap_col = (int)((p - nextline) + cap_col
1893 - nextline_idx);
1894 }
1895 else
1896 // Compute the actual column.
1897 cap_col += (int)(prev_ptr - line);
1898 }
1899 }
1900 }
1901 if (spell_attr != 0)
1902 {
1903 if (!attr_pri)
1904 char_attr = hl_combine_attr(char_attr, spell_attr);
1905 else
1906 char_attr = hl_combine_attr(spell_attr, char_attr);
1907 }
1908#endif
1909#ifdef FEAT_LINEBREAK
1910 // Found last space before word: check for line break.
1911 if (wp->w_p_lbr && c0 == c
1912 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
1913 {
1914 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
1915 char_u *p = ptr - (mb_off + 1);
1916
1917 // TODO: is passing p for start of the line OK?
1918 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
1919 NULL) - 1;
1920 if (c == TAB && n_extra + col > wp->w_width)
1921# ifdef FEAT_VARTABS
1922 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
1923 wp->w_buffer->b_p_vts_array) - 1;
1924# else
1925 n_extra = (int)wp->w_buffer->b_p_ts
1926 - vcol % (int)wp->w_buffer->b_p_ts - 1;
1927# endif
1928
1929 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
1930 c_final = NUL;
1931 if (VIM_ISWHITE(c))
1932 {
1933#ifdef FEAT_CONCEAL
1934 if (c == TAB)
1935 // See "Tab alignment" below.
1936 FIX_FOR_BOGUSCOLS;
1937#endif
1938 if (!wp->w_p_list)
1939 c = ' ';
1940 }
1941 }
1942#endif
1943
1944 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
1945 // But not when the character is followed by a composing
1946 // character (use mb_l to check that).
1947 if (wp->w_p_list
1948 && ((((c == 160 && mb_l == 1)
1949 || (mb_utf8
1950 && ((mb_c == 160 && mb_l == 2)
1951 || (mb_c == 0x202f && mb_l == 3))))
1952 && lcs_nbsp)
1953 || (c == ' '
1954 && mb_l == 1
1955 && lcs_space
1956 && ptr - line <= trailcol)))
1957 {
1958 c = (c == ' ') ? lcs_space : lcs_nbsp;
1959 if (area_attr == 0 && search_attr == 0)
1960 {
1961 n_attr = 1;
1962 extra_attr = HL_ATTR(HLF_8);
1963 saved_attr2 = char_attr; // save current attr
1964 }
1965 mb_c = c;
1966 if (enc_utf8 && utf_char2len(c) > 1)
1967 {
1968 mb_utf8 = TRUE;
1969 u8cc[0] = 0;
1970 c = 0xc0;
1971 }
1972 else
1973 mb_utf8 = FALSE;
1974 }
1975
1976 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
1977 {
1978 c = lcs_trail;
1979 if (!attr_pri)
1980 {
1981 n_attr = 1;
1982 extra_attr = HL_ATTR(HLF_8);
1983 saved_attr2 = char_attr; // save current attr
1984 }
1985 mb_c = c;
1986 if (enc_utf8 && utf_char2len(c) > 1)
1987 {
1988 mb_utf8 = TRUE;
1989 u8cc[0] = 0;
1990 c = 0xc0;
1991 }
1992 else
1993 mb_utf8 = FALSE;
1994 }
1995 }
1996
1997 // Handling of non-printable characters.
1998 if (!vim_isprintc(c))
1999 {
2000 // when getting a character from the file, we may have to
2001 // turn it into something else on the way to putting it
2002 // into "ScreenLines".
2003 if (c == TAB && (!wp->w_p_list || lcs_tab1))
2004 {
2005 int tab_len = 0;
2006 long vcol_adjusted = vcol; // removed showbreak length
2007#ifdef FEAT_LINEBREAK
2008 // only adjust the tab_len, when at the first column
2009 // after the showbreak value was drawn
2010 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
2011 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
2012#endif
2013 // tab amount depends on current column
2014#ifdef FEAT_VARTABS
2015 tab_len = tabstop_padding(vcol_adjusted,
2016 wp->w_buffer->b_p_ts,
2017 wp->w_buffer->b_p_vts_array) - 1;
2018#else
2019 tab_len = (int)wp->w_buffer->b_p_ts
2020 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
2021#endif
2022
2023#ifdef FEAT_LINEBREAK
2024 if (!wp->w_p_lbr || !wp->w_p_list)
2025#endif
2026 // tab amount depends on current column
2027 n_extra = tab_len;
2028#ifdef FEAT_LINEBREAK
2029 else
2030 {
2031 char_u *p;
2032 int len;
2033 int i;
2034 int saved_nextra = n_extra;
2035
2036#ifdef FEAT_CONCEAL
2037 if (vcol_off > 0)
2038 // there are characters to conceal
2039 tab_len += vcol_off;
2040 // boguscols before FIX_FOR_BOGUSCOLS macro from above
2041 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
2042 && n_extra > tab_len)
2043 tab_len += n_extra - tab_len;
2044#endif
2045
2046 // if n_extra > 0, it gives the number of chars, to
2047 // use for a tab, else we need to calculate the width
2048 // for a tab
2049 len = (tab_len * mb_char2len(lcs_tab2));
2050 if (n_extra > 0)
2051 len += n_extra - tab_len;
2052 c = lcs_tab1;
2053 p = alloc(len + 1);
2054 vim_memset(p, ' ', len);
2055 p[len] = NUL;
2056 vim_free(p_extra_free);
2057 p_extra_free = p;
2058 for (i = 0; i < tab_len; i++)
2059 {
2060 int lcs = lcs_tab2;
2061
2062 if (*p == NUL)
2063 {
2064 tab_len = i;
2065 break;
2066 }
2067
2068 // if lcs_tab3 is given, need to change the char
2069 // for tab
2070 if (lcs_tab3 && i == tab_len - 1)
2071 lcs = lcs_tab3;
2072 mb_char2bytes(lcs, p);
2073 p += mb_char2len(lcs);
2074 n_extra += mb_char2len(lcs)
2075 - (saved_nextra > 0 ? 1 : 0);
2076 }
2077 p_extra = p_extra_free;
2078#ifdef FEAT_CONCEAL
2079 // n_extra will be increased by FIX_FOX_BOGUSCOLS
2080 // macro below, so need to adjust for that here
2081 if (vcol_off > 0)
2082 n_extra -= vcol_off;
2083#endif
2084 }
2085#endif
2086#ifdef FEAT_CONCEAL
2087 {
2088 int vc_saved = vcol_off;
2089
2090 // Tab alignment should be identical regardless of
2091 // 'conceallevel' value. So tab compensates of all
2092 // previous concealed characters, and thus resets
2093 // vcol_off and boguscols accumulated so far in the
2094 // line. Note that the tab can be longer than
2095 // 'tabstop' when there are concealed characters.
2096 FIX_FOR_BOGUSCOLS;
2097
2098 // Make sure, the highlighting for the tab char will be
2099 // correctly set further below (effectively reverts the
2100 // FIX_FOR_BOGSUCOLS macro
2101 if (n_extra == tab_len + vc_saved && wp->w_p_list
2102 && lcs_tab1)
2103 tab_len += vc_saved;
2104 }
2105#endif
2106 mb_utf8 = FALSE; // don't draw as UTF-8
2107 if (wp->w_p_list)
2108 {
2109 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
2110#ifdef FEAT_LINEBREAK
2111 if (wp->w_p_lbr)
2112 c_extra = NUL; // using p_extra from above
2113 else
2114#endif
2115 c_extra = lcs_tab2;
2116 c_final = lcs_tab3;
2117 n_attr = tab_len + 1;
2118 extra_attr = HL_ATTR(HLF_8);
2119 saved_attr2 = char_attr; // save current attr
2120 mb_c = c;
2121 if (enc_utf8 && utf_char2len(c) > 1)
2122 {
2123 mb_utf8 = TRUE;
2124 u8cc[0] = 0;
2125 c = 0xc0;
2126 }
2127 }
2128 else
2129 {
2130 c_final = NUL;
2131 c_extra = ' ';
2132 c = ' ';
2133 }
2134 }
2135 else if (c == NUL
2136 && (wp->w_p_list
2137 || ((fromcol >= 0 || fromcol_prev >= 0)
2138 && tocol > vcol
2139 && VIsual_mode != Ctrl_V
2140 && (
2141# ifdef FEAT_RIGHTLEFT
2142 wp->w_p_rl ? (col >= 0) :
2143# endif
2144 (col < wp->w_width))
2145 && !(noinvcur
2146 && lnum == wp->w_cursor.lnum
2147 && (colnr_T)vcol == wp->w_virtcol)))
2148 && lcs_eol_one > 0)
2149 {
2150 // Display a '$' after the line or highlight an extra
2151 // character if the line break is included.
2152#if defined(FEAT_DIFF) || defined(LINE_ATTR)
2153 // For a diff line the highlighting continues after the
2154 // "$".
2155 if (
2156# ifdef FEAT_DIFF
2157 diff_hlf == (hlf_T)0
2158# ifdef LINE_ATTR
2159 &&
2160# endif
2161# endif
2162# ifdef LINE_ATTR
2163 line_attr == 0
2164# endif
2165 )
2166#endif
2167 {
2168 // In virtualedit, visual selections may extend
2169 // beyond end of line.
2170 if (area_highlighting && virtual_active()
2171 && tocol != MAXCOL && vcol < tocol)
2172 n_extra = 0;
2173 else
2174 {
2175 p_extra = at_end_str;
2176 n_extra = 1;
2177 c_extra = NUL;
2178 c_final = NUL;
2179 }
2180 }
2181 if (wp->w_p_list && lcs_eol > 0)
2182 c = lcs_eol;
2183 else
2184 c = ' ';
2185 lcs_eol_one = -1;
2186 --ptr; // put it back at the NUL
2187 if (!attr_pri)
2188 {
2189 extra_attr = HL_ATTR(HLF_AT);
2190 n_attr = 1;
2191 }
2192 mb_c = c;
2193 if (enc_utf8 && utf_char2len(c) > 1)
2194 {
2195 mb_utf8 = TRUE;
2196 u8cc[0] = 0;
2197 c = 0xc0;
2198 }
2199 else
2200 mb_utf8 = FALSE; // don't draw as UTF-8
2201 }
2202 else if (c != NUL)
2203 {
2204 p_extra = transchar(c);
2205 if (n_extra == 0)
2206 n_extra = byte2cells(c) - 1;
2207#ifdef FEAT_RIGHTLEFT
2208 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
2209 rl_mirror(p_extra); // reverse "<12>"
2210#endif
2211 c_extra = NUL;
2212 c_final = NUL;
2213#ifdef FEAT_LINEBREAK
2214 if (wp->w_p_lbr)
2215 {
2216 char_u *p;
2217
2218 c = *p_extra;
2219 p = alloc(n_extra + 1);
2220 vim_memset(p, ' ', n_extra);
2221 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
2222 p[n_extra] = NUL;
2223 vim_free(p_extra_free);
2224 p_extra_free = p_extra = p;
2225 }
2226 else
2227#endif
2228 {
2229 n_extra = byte2cells(c) - 1;
2230 c = *p_extra++;
2231 }
2232 if (!attr_pri)
2233 {
2234 n_attr = n_extra + 1;
2235 extra_attr = HL_ATTR(HLF_8);
2236 saved_attr2 = char_attr; // save current attr
2237 }
2238 mb_utf8 = FALSE; // don't draw as UTF-8
2239 }
2240 else if (VIsual_active
2241 && (VIsual_mode == Ctrl_V
2242 || VIsual_mode == 'v')
2243 && virtual_active()
2244 && tocol != MAXCOL
2245 && vcol < tocol
2246 && (
2247#ifdef FEAT_RIGHTLEFT
2248 wp->w_p_rl ? (col >= 0) :
2249#endif
2250 (col < wp->w_width)))
2251 {
2252 c = ' ';
2253 --ptr; // put it back at the NUL
2254 }
2255#if defined(LINE_ATTR)
2256 else if ((
2257# ifdef FEAT_DIFF
2258 diff_hlf != (hlf_T)0 ||
2259# endif
2260# ifdef FEAT_TERMINAL
2261 win_attr != 0 ||
2262# endif
2263 line_attr != 0
2264 ) && (
2265# ifdef FEAT_RIGHTLEFT
2266 wp->w_p_rl ? (col >= 0) :
2267# endif
2268 (col
2269# ifdef FEAT_CONCEAL
2270 - boguscols
2271# endif
2272 < wp->w_width)))
2273 {
2274 // Highlight until the right side of the window
2275 c = ' ';
2276 --ptr; // put it back at the NUL
2277
2278 // Remember we do the char for line highlighting.
2279 ++did_line_attr;
2280
2281 // don't do search HL for the rest of the line
2282 if (line_attr != 0 && char_attr == search_attr
2283 && (did_line_attr > 1
2284 || (wp->w_p_list && lcs_eol > 0)))
2285 char_attr = line_attr;
2286# ifdef FEAT_DIFF
2287 if (diff_hlf == HLF_TXD)
2288 {
2289 diff_hlf = HLF_CHD;
2290 if (vi_attr == 0 || char_attr != vi_attr)
2291 {
2292 char_attr = HL_ATTR(diff_hlf);
2293 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2294 && wp->w_p_culopt_flags != CULOPT_NBR
2295 && (!cul_screenline
2296 || (vcol >= left_curline_col
2297 && vcol <= right_curline_col)))
2298 char_attr = hl_combine_attr(
2299 char_attr, HL_ATTR(HLF_CUL));
2300 }
2301 }
2302# endif
2303# ifdef FEAT_TERMINAL
2304 if (win_attr != 0)
2305 {
2306 char_attr = win_attr;
Bram Moolenaara3817732019-10-16 16:31:44 +02002307 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2308 && wp->w_p_culopt_flags != CULOPT_NBR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002309 {
2310 if (!cul_screenline || (vcol >= left_curline_col
2311 && vcol <= right_curline_col))
2312 char_attr = hl_combine_attr(
2313 char_attr, HL_ATTR(HLF_CUL));
2314 }
2315 else if (line_attr)
2316 char_attr = hl_combine_attr(char_attr, line_attr);
2317 }
2318# endif
2319 }
2320#endif
2321 }
2322
2323#ifdef FEAT_CONCEAL
2324 if ( wp->w_p_cole > 0
2325 && (wp != curwin || lnum != wp->w_cursor.lnum ||
2326 conceal_cursor_line(wp))
2327 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
2328 && !(lnum_in_visual_area
2329 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
2330 {
2331 char_attr = conceal_attr;
2332 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
2333 && (syn_get_sub_char() != NUL || match_conc
2334 || wp->w_p_cole == 1)
2335 && wp->w_p_cole != 3)
2336 {
2337 // First time at this concealed item: display one
2338 // character.
2339 if (match_conc)
2340 c = match_conc;
2341 else if (syn_get_sub_char() != NUL)
2342 c = syn_get_sub_char();
2343 else if (lcs_conceal != NUL)
2344 c = lcs_conceal;
2345 else
2346 c = ' ';
2347
2348 prev_syntax_id = syntax_seqnr;
2349
2350 if (n_extra > 0)
2351 vcol_off += n_extra;
2352 vcol += n_extra;
2353 if (wp->w_p_wrap && n_extra > 0)
2354 {
2355# ifdef FEAT_RIGHTLEFT
2356 if (wp->w_p_rl)
2357 {
2358 col -= n_extra;
2359 boguscols -= n_extra;
2360 }
2361 else
2362# endif
2363 {
2364 boguscols += n_extra;
2365 col += n_extra;
2366 }
2367 }
2368 n_extra = 0;
2369 n_attr = 0;
2370 }
2371 else if (n_skip == 0)
2372 {
2373 is_concealing = TRUE;
2374 n_skip = 1;
2375 }
2376 mb_c = c;
2377 if (enc_utf8 && utf_char2len(c) > 1)
2378 {
2379 mb_utf8 = TRUE;
2380 u8cc[0] = 0;
2381 c = 0xc0;
2382 }
2383 else
2384 mb_utf8 = FALSE; // don't draw as UTF-8
2385 }
2386 else
2387 {
2388 prev_syntax_id = 0;
2389 is_concealing = FALSE;
2390 }
2391
2392 if (n_skip > 0 && did_decrement_ptr)
2393 // not showing the '>', put pointer back to avoid getting stuck
2394 ++ptr;
2395
2396#endif // FEAT_CONCEAL
2397 }
2398
2399#ifdef FEAT_CONCEAL
2400 // In the cursor line and we may be concealing characters: correct
2401 // the cursor column when we reach its position.
2402 if (!did_wcol && draw_state == WL_LINE
2403 && wp == curwin && lnum == wp->w_cursor.lnum
2404 && conceal_cursor_line(wp)
2405 && (int)wp->w_virtcol <= vcol + n_skip)
2406 {
2407# ifdef FEAT_RIGHTLEFT
2408 if (wp->w_p_rl)
2409 wp->w_wcol = wp->w_width - col + boguscols - 1;
2410 else
2411# endif
2412 wp->w_wcol = col - boguscols;
2413 wp->w_wrow = row;
2414 did_wcol = TRUE;
2415 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
2416 }
2417#endif
2418
2419 // Don't override visual selection highlighting.
2420 if (n_attr > 0
2421 && draw_state == WL_LINE
2422 && !attr_pri)
2423 {
2424#ifdef LINE_ATTR
2425 if (line_attr)
2426 char_attr = hl_combine_attr(extra_attr, line_attr);
2427 else
2428#endif
2429 char_attr = extra_attr;
2430 }
2431
2432#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2433 // XIM don't send preedit_start and preedit_end, but they send
2434 // preedit_changed and commit. Thus Vim can't set "im_is_active", use
2435 // im_is_preediting() here.
2436 if (p_imst == IM_ON_THE_SPOT
2437 && xic != NULL
2438 && lnum == wp->w_cursor.lnum
2439 && (State & INSERT)
2440 && !p_imdisable
2441 && im_is_preediting()
2442 && draw_state == WL_LINE)
2443 {
2444 colnr_T tcol;
2445
2446 if (preedit_end_col == MAXCOL)
2447 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
2448 else
2449 tcol = preedit_end_col;
2450 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
2451 {
2452 if (feedback_old_attr < 0)
2453 {
2454 feedback_col = 0;
2455 feedback_old_attr = char_attr;
2456 }
2457 char_attr = im_get_feedback_attr(feedback_col);
2458 if (char_attr < 0)
2459 char_attr = feedback_old_attr;
2460 feedback_col++;
2461 }
2462 else if (feedback_old_attr >= 0)
2463 {
2464 char_attr = feedback_old_attr;
2465 feedback_old_attr = -1;
2466 feedback_col = 0;
2467 }
2468 }
2469#endif
2470 // Handle the case where we are in column 0 but not on the first
2471 // character of the line and the user wants us to show us a
2472 // special character (via 'listchars' option "precedes:<char>".
2473 if (lcs_prec_todo != NUL
2474 && wp->w_p_list
Bram Moolenaarbffba7f2019-09-20 17:00:17 +02002475 && (wp->w_p_wrap ?
2476 (wp->w_skipcol > 0 && row == 0) :
2477 wp->w_leftcol > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002478#ifdef FEAT_DIFF
2479 && filler_todo <= 0
2480#endif
2481 && draw_state > WL_NR
2482 && c != NUL)
2483 {
2484 c = lcs_prec;
2485 lcs_prec_todo = NUL;
2486 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2487 {
2488 // Double-width character being overwritten by the "precedes"
2489 // character, need to fill up half the character.
2490 c_extra = MB_FILLER_CHAR;
2491 c_final = NUL;
2492 n_extra = 1;
2493 n_attr = 2;
2494 extra_attr = HL_ATTR(HLF_AT);
2495 }
2496 mb_c = c;
2497 if (enc_utf8 && utf_char2len(c) > 1)
2498 {
2499 mb_utf8 = TRUE;
2500 u8cc[0] = 0;
2501 c = 0xc0;
2502 }
2503 else
2504 mb_utf8 = FALSE; // don't draw as UTF-8
2505 if (!attr_pri)
2506 {
2507 saved_attr3 = char_attr; // save current attr
2508 char_attr = HL_ATTR(HLF_AT); // later copied to char_attr
2509 n_attr3 = 1;
2510 }
2511 }
2512
2513 // At end of the text line or just after the last character.
2514 if ((c == NUL
2515#if defined(LINE_ATTR)
2516 || did_line_attr == 1
2517#endif
2518 ) && eol_hl_off == 0)
2519 {
2520#ifdef FEAT_SEARCH_EXTRA
2521 // flag to indicate whether prevcol equals startcol of search_hl or
2522 // one of the matches
2523 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &screen_search_hl,
2524 (long)(ptr - line) - (c == NUL));
2525#endif
2526 // Invert at least one char, used for Visual and empty line or
2527 // highlight match at end of line. If it's beyond the last
2528 // char on the screen, just overwrite that one (tricky!) Not
2529 // needed when a '$' was displayed for 'list'.
2530 if (lcs_eol == lcs_eol_one
2531 && ((area_attr != 0 && vcol == fromcol
2532 && (VIsual_mode != Ctrl_V
2533 || lnum == VIsual.lnum
2534 || lnum == curwin->w_cursor.lnum)
2535 && c == NUL)
2536#ifdef FEAT_SEARCH_EXTRA
2537 // highlight 'hlsearch' match at end of line
2538 || (prevcol_hl_flag
2539# ifdef FEAT_SYN_HL
2540 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
2541 && !(wp == curwin && VIsual_active))
2542# endif
2543# ifdef FEAT_DIFF
2544 && diff_hlf == (hlf_T)0
2545# endif
2546# if defined(LINE_ATTR)
2547 && did_line_attr <= 1
2548# endif
2549 )
2550#endif
2551 ))
2552 {
2553 int n = 0;
2554
2555#ifdef FEAT_RIGHTLEFT
2556 if (wp->w_p_rl)
2557 {
2558 if (col < 0)
2559 n = 1;
2560 }
2561 else
2562#endif
2563 {
2564 if (col >= wp->w_width)
2565 n = -1;
2566 }
2567 if (n != 0)
2568 {
2569 // At the window boundary, highlight the last character
2570 // instead (better than nothing).
2571 off += n;
2572 col += n;
2573 }
2574 else
2575 {
2576 // Add a blank character to highlight.
2577 ScreenLines[off] = ' ';
2578 if (enc_utf8)
2579 ScreenLinesUC[off] = 0;
2580 }
2581#ifdef FEAT_SEARCH_EXTRA
2582 if (area_attr == 0)
2583 {
2584 // Use attributes from match with highest priority among
2585 // 'search_hl' and the match list.
2586 get_search_match_hl(wp, &screen_search_hl,
2587 (long)(ptr - line), &char_attr);
2588 }
2589#endif
2590 ScreenAttrs[off] = char_attr;
2591#ifdef FEAT_RIGHTLEFT
2592 if (wp->w_p_rl)
2593 {
2594 --col;
2595 --off;
2596 }
2597 else
2598#endif
2599 {
2600 ++col;
2601 ++off;
2602 }
2603 ++vcol;
2604 eol_hl_off = 1;
2605 }
2606 }
2607
2608 // At end of the text line.
2609 if (c == NUL)
2610 {
2611#ifdef FEAT_SYN_HL
2612 // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
2613 if (wp->w_p_wrap)
2614 v = wp->w_skipcol;
2615 else
2616 v = wp->w_leftcol;
2617
2618 // check if line ends before left margin
2619 if (vcol < v + col - win_col_off(wp))
2620 vcol = v + col - win_col_off(wp);
2621#ifdef FEAT_CONCEAL
2622 // Get rid of the boguscols now, we want to draw until the right
2623 // edge for 'cursorcolumn'.
2624 col -= boguscols;
2625 boguscols = 0;
2626#endif
2627
2628 if (draw_color_col)
2629 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2630
2631 if (((wp->w_p_cuc
2632 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
2633 && (int)wp->w_virtcol <
2634 wp->w_width * (row - startrow + 1) + v
2635 && lnum != wp->w_cursor.lnum)
2636 || draw_color_col
2637 || win_attr != 0)
2638# ifdef FEAT_RIGHTLEFT
2639 && !wp->w_p_rl
2640# endif
2641 )
2642 {
2643 int rightmost_vcol = 0;
2644 int i;
2645
2646 if (wp->w_p_cuc)
2647 rightmost_vcol = wp->w_virtcol;
2648 if (draw_color_col)
2649 // determine rightmost colorcolumn to possibly draw
2650 for (i = 0; color_cols[i] >= 0; ++i)
2651 if (rightmost_vcol < color_cols[i])
2652 rightmost_vcol = color_cols[i];
2653
2654 while (col < wp->w_width)
2655 {
2656 ScreenLines[off] = ' ';
2657 if (enc_utf8)
2658 ScreenLinesUC[off] = 0;
2659 ++col;
2660 if (draw_color_col)
2661 draw_color_col = advance_color_col(VCOL_HLC,
2662 &color_cols);
2663
2664 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
2665 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
2666 else if (draw_color_col && VCOL_HLC == *color_cols)
2667 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
2668 else
2669 ScreenAttrs[off++] = win_attr;
2670
2671 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
2672 break;
2673
2674 ++vcol;
2675 }
2676 }
2677#endif
2678
2679 screen_line(screen_row, wp->w_wincol, col,
2680 (int)wp->w_width, screen_line_flags);
2681 row++;
2682
2683 // Update w_cline_height and w_cline_folded if the cursor line was
2684 // updated (saves a call to plines() later).
2685 if (wp == curwin && lnum == curwin->w_cursor.lnum)
2686 {
2687 curwin->w_cline_row = startrow;
2688 curwin->w_cline_height = row - startrow;
2689#ifdef FEAT_FOLDING
2690 curwin->w_cline_folded = FALSE;
2691#endif
2692 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2693 }
2694
2695 break;
2696 }
2697
2698 // Show "extends" character from 'listchars' if beyond the line end and
2699 // 'list' is set.
2700 if (lcs_ext != NUL
2701 && wp->w_p_list
2702 && !wp->w_p_wrap
2703#ifdef FEAT_DIFF
2704 && filler_todo <= 0
2705#endif
2706 && (
2707#ifdef FEAT_RIGHTLEFT
2708 wp->w_p_rl ? col == 0 :
2709#endif
2710 col == wp->w_width - 1)
2711 && (*ptr != NUL
2712 || (wp->w_p_list && lcs_eol_one > 0)
2713 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
2714 {
2715 c = lcs_ext;
2716 char_attr = HL_ATTR(HLF_AT);
2717 mb_c = c;
2718 if (enc_utf8 && utf_char2len(c) > 1)
2719 {
2720 mb_utf8 = TRUE;
2721 u8cc[0] = 0;
2722 c = 0xc0;
2723 }
2724 else
2725 mb_utf8 = FALSE;
2726 }
2727
2728#ifdef FEAT_SYN_HL
2729 // advance to the next 'colorcolumn'
2730 if (draw_color_col)
2731 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2732
2733 // Highlight the cursor column if 'cursorcolumn' is set. But don't
2734 // highlight the cursor position itself.
2735 // Also highlight the 'colorcolumn' if it is different than
2736 // 'cursorcolumn'
2737 vcol_save_attr = -1;
2738 if (draw_state == WL_LINE && !lnum_in_visual_area
2739 && search_attr == 0 && area_attr == 0)
2740 {
2741 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
2742 && lnum != wp->w_cursor.lnum)
2743 {
2744 vcol_save_attr = char_attr;
2745 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
2746 }
2747 else if (draw_color_col && VCOL_HLC == *color_cols)
2748 {
2749 vcol_save_attr = char_attr;
2750 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
2751 }
2752 }
2753#endif
2754
2755 // Store character to be displayed.
2756 // Skip characters that are left of the screen for 'nowrap'.
2757 vcol_prev = vcol;
2758 if (draw_state < WL_LINE || n_skip <= 0)
2759 {
2760 // Store the character.
2761#if defined(FEAT_RIGHTLEFT)
2762 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
2763 {
2764 // A double-wide character is: put first halve in left cell.
2765 --off;
2766 --col;
2767 }
2768#endif
2769 ScreenLines[off] = c;
2770 if (enc_dbcs == DBCS_JPNU)
2771 {
2772 if ((mb_c & 0xff00) == 0x8e00)
2773 ScreenLines[off] = 0x8e;
2774 ScreenLines2[off] = mb_c & 0xff;
2775 }
2776 else if (enc_utf8)
2777 {
2778 if (mb_utf8)
2779 {
2780 int i;
2781
2782 ScreenLinesUC[off] = mb_c;
2783 if ((c & 0xff) == 0)
2784 ScreenLines[off] = 0x80; // avoid storing zero
2785 for (i = 0; i < Screen_mco; ++i)
2786 {
2787 ScreenLinesC[i][off] = u8cc[i];
2788 if (u8cc[i] == 0)
2789 break;
2790 }
2791 }
2792 else
2793 ScreenLinesUC[off] = 0;
2794 }
2795 if (multi_attr)
2796 {
2797 ScreenAttrs[off] = multi_attr;
2798 multi_attr = 0;
2799 }
2800 else
2801 ScreenAttrs[off] = char_attr;
2802
2803 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2804 {
2805 // Need to fill two screen columns.
2806 ++off;
2807 ++col;
2808 if (enc_utf8)
2809 // UTF-8: Put a 0 in the second screen char.
2810 ScreenLines[off] = 0;
2811 else
2812 // DBCS: Put second byte in the second screen char.
2813 ScreenLines[off] = mb_c & 0xff;
2814 if (draw_state > WL_NR
2815#ifdef FEAT_DIFF
2816 && filler_todo <= 0
2817#endif
2818 )
2819 ++vcol;
2820 // When "tocol" is halfway a character, set it to the end of
2821 // the character, otherwise highlighting won't stop.
2822 if (tocol == vcol)
2823 ++tocol;
2824#ifdef FEAT_RIGHTLEFT
2825 if (wp->w_p_rl)
2826 {
2827 // now it's time to backup one cell
2828 --off;
2829 --col;
2830 }
2831#endif
2832 }
2833#ifdef FEAT_RIGHTLEFT
2834 if (wp->w_p_rl)
2835 {
2836 --off;
2837 --col;
2838 }
2839 else
2840#endif
2841 {
2842 ++off;
2843 ++col;
2844 }
2845 }
2846#ifdef FEAT_CONCEAL
2847 else if (wp->w_p_cole > 0 && is_concealing)
2848 {
2849 --n_skip;
2850 ++vcol_off;
2851 if (n_extra > 0)
2852 vcol_off += n_extra;
2853 if (wp->w_p_wrap)
2854 {
2855 // Special voodoo required if 'wrap' is on.
2856 //
2857 // Advance the column indicator to force the line
2858 // drawing to wrap early. This will make the line
2859 // take up the same screen space when parts are concealed,
2860 // so that cursor line computations aren't messed up.
2861 //
2862 // To avoid the fictitious advance of 'col' causing
2863 // trailing junk to be written out of the screen line
2864 // we are building, 'boguscols' keeps track of the number
2865 // of bad columns we have advanced.
2866 if (n_extra > 0)
2867 {
2868 vcol += n_extra;
2869# ifdef FEAT_RIGHTLEFT
2870 if (wp->w_p_rl)
2871 {
2872 col -= n_extra;
2873 boguscols -= n_extra;
2874 }
2875 else
2876# endif
2877 {
2878 col += n_extra;
2879 boguscols += n_extra;
2880 }
2881 n_extra = 0;
2882 n_attr = 0;
2883 }
2884
2885
2886 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2887 {
2888 // Need to fill two screen columns.
2889# ifdef FEAT_RIGHTLEFT
2890 if (wp->w_p_rl)
2891 {
2892 --boguscols;
2893 --col;
2894 }
2895 else
2896# endif
2897 {
2898 ++boguscols;
2899 ++col;
2900 }
2901 }
2902
2903# ifdef FEAT_RIGHTLEFT
2904 if (wp->w_p_rl)
2905 {
2906 --boguscols;
2907 --col;
2908 }
2909 else
2910# endif
2911 {
2912 ++boguscols;
2913 ++col;
2914 }
2915 }
2916 else
2917 {
2918 if (n_extra > 0)
2919 {
2920 vcol += n_extra;
2921 n_extra = 0;
2922 n_attr = 0;
2923 }
2924 }
2925
2926 }
2927#endif // FEAT_CONCEAL
2928 else
2929 --n_skip;
2930
2931 // Only advance the "vcol" when after the 'number' or 'relativenumber'
2932 // column.
2933 if (draw_state > WL_NR
2934#ifdef FEAT_DIFF
2935 && filler_todo <= 0
2936#endif
2937 )
2938 ++vcol;
2939
2940#ifdef FEAT_SYN_HL
2941 if (vcol_save_attr >= 0)
2942 char_attr = vcol_save_attr;
2943#endif
2944
2945 // restore attributes after "predeces" in 'listchars'
2946 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
2947 char_attr = saved_attr3;
2948
2949 // restore attributes after last 'listchars' or 'number' char
2950 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
2951 char_attr = saved_attr2;
2952
2953 // At end of screen line and there is more to come: Display the line
2954 // so far. If there is no more to display it is caught above.
2955 if ((
2956#ifdef FEAT_RIGHTLEFT
2957 wp->w_p_rl ? (col < 0) :
2958#endif
2959 (col >= wp->w_width))
2960 && (*ptr != NUL
2961#ifdef FEAT_DIFF
2962 || filler_todo > 0
2963#endif
2964 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
2965 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
2966 )
2967 {
2968#ifdef FEAT_CONCEAL
2969 screen_line(screen_row, wp->w_wincol, col - boguscols,
2970 (int)wp->w_width, screen_line_flags);
2971 boguscols = 0;
2972#else
2973 screen_line(screen_row, wp->w_wincol, col,
2974 (int)wp->w_width, screen_line_flags);
2975#endif
2976 ++row;
2977 ++screen_row;
2978
2979 // When not wrapping and finished diff lines, or when displayed
2980 // '$' and highlighting until last column, break here.
2981 if ((!wp->w_p_wrap
2982#ifdef FEAT_DIFF
2983 && filler_todo <= 0
2984#endif
2985 ) || lcs_eol_one == -1)
2986 break;
2987
2988 // When the window is too narrow draw all "@" lines.
2989 if (draw_state != WL_LINE
2990#ifdef FEAT_DIFF
2991 && filler_todo <= 0
2992#endif
2993 )
2994 {
2995 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
2996 draw_vsep_win(wp, row);
2997 row = endrow;
2998 }
2999
3000 // When line got too long for screen break here.
3001 if (row == endrow)
3002 {
3003 ++row;
3004 break;
3005 }
3006
3007 if (screen_cur_row == screen_row - 1
3008#ifdef FEAT_DIFF
3009 && filler_todo <= 0
3010#endif
3011 && wp->w_width == Columns)
3012 {
3013 // Remember that the line wraps, used for modeless copy.
3014 LineWraps[screen_row - 1] = TRUE;
3015
3016 // Special trick to make copy/paste of wrapped lines work with
3017 // xterm/screen: write an extra character beyond the end of
3018 // the line. This will work with all terminal types
3019 // (regardless of the xn,am settings).
3020 // Only do this on a fast tty.
3021 // Only do this if the cursor is on the current line
3022 // (something has been written in it).
3023 // Don't do this for the GUI.
3024 // Don't do this for double-width characters.
3025 // Don't do this for a window not at the right screen border.
3026 if (p_tf
3027#ifdef FEAT_GUI
3028 && !gui.in_use
3029#endif
3030 && !(has_mbyte
3031 && ((*mb_off2cells)(LineOffset[screen_row],
3032 LineOffset[screen_row] + screen_Columns)
3033 == 2
3034 || (*mb_off2cells)(LineOffset[screen_row - 1]
3035 + (int)Columns - 2,
3036 LineOffset[screen_row] + screen_Columns)
3037 == 2)))
3038 {
3039 // First make sure we are at the end of the screen line,
3040 // then output the same character again to let the
3041 // terminal know about the wrap. If the terminal doesn't
3042 // auto-wrap, we overwrite the character.
3043 if (screen_cur_col != wp->w_width)
3044 screen_char(LineOffset[screen_row - 1]
3045 + (unsigned)Columns - 1,
3046 screen_row - 1, (int)(Columns - 1));
3047
3048 // When there is a multi-byte character, just output a
3049 // space to keep it simple.
3050 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
3051 screen_row - 1] + (Columns - 1)]) > 1)
3052 out_char(' ');
3053 else
3054 out_char(ScreenLines[LineOffset[screen_row - 1]
3055 + (Columns - 1)]);
3056 // force a redraw of the first char on the next line
3057 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
3058 screen_start(); // don't know where cursor is now
3059 }
3060 }
3061
3062 col = 0;
3063 off = (unsigned)(current_ScreenLine - ScreenLines);
3064#ifdef FEAT_RIGHTLEFT
3065 if (wp->w_p_rl)
3066 {
3067 col = wp->w_width - 1; // col is not used if breaking!
3068 off += col;
3069 }
3070#endif
3071
3072 // reset the drawing state for the start of a wrapped line
3073 draw_state = WL_START;
3074 saved_n_extra = n_extra;
3075 saved_p_extra = p_extra;
3076 saved_c_extra = c_extra;
3077 saved_c_final = c_final;
3078#ifdef FEAT_SYN_HL
3079 if (!(cul_screenline
3080# ifdef FEAT_DIFF
3081 && diff_hlf == (hlf_T)0)
3082# endif
3083 )
3084 saved_char_attr = char_attr;
3085 else
3086#endif
3087 saved_char_attr = 0;
3088 n_extra = 0;
3089 lcs_prec_todo = lcs_prec;
3090#ifdef FEAT_LINEBREAK
3091# ifdef FEAT_DIFF
3092 if (filler_todo <= 0)
3093# endif
3094 need_showbreak = TRUE;
3095#endif
3096#ifdef FEAT_DIFF
3097 --filler_todo;
3098 // When the filler lines are actually below the last line of the
3099 // file, don't draw the line itself, break here.
3100 if (filler_todo == 0 && wp->w_botfill)
3101 break;
3102#endif
3103 }
3104
3105 } // for every character in the line
3106
3107#ifdef FEAT_SPELL
3108 // After an empty line check first word for capital.
3109 if (*skipwhite(line) == NUL)
3110 {
3111 capcol_lnum = lnum + 1;
3112 cap_col = 0;
3113 }
3114#endif
3115#ifdef FEAT_TEXT_PROP
3116 vim_free(text_props);
3117 vim_free(text_prop_idxs);
3118#endif
3119
3120 vim_free(p_extra_free);
3121 return row;
3122}
3123