blob: 3106196e9de46370f2129d39ca55c59829c18474 [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/*
Bram Moolenaare413ea02021-11-24 16:20:13 +000080 * Return TRUE if CursorLineSign highlight is to be used.
81 */
82 static int
83use_cursor_line_sign(win_T *wp, linenr_T lnum)
84{
85 return wp->w_p_cul
86 && lnum == wp->w_cursor.lnum
87 && (wp->w_p_culopt_flags & CULOPT_NBR);
88}
89
90/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020091 * Get information needed to display the sign in line 'lnum' in window 'wp'.
92 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
93 * Otherwise the sign is going to be displayed in the sign column.
94 */
95 static void
96get_sign_display_info(
97 int nrcol,
98 win_T *wp,
Bram Moolenaare413ea02021-11-24 16:20:13 +000099 linenr_T lnum,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200100 sign_attrs_T *sattr,
101 int wcr_attr,
102 int row,
103 int startrow,
104 int filler_lines UNUSED,
105 int filler_todo UNUSED,
106 int *c_extrap,
107 int *c_finalp,
108 char_u *extra,
109 char_u **pp_extra,
110 int *n_extrap,
111 int *char_attrp)
112{
113 int text_sign;
114# ifdef FEAT_SIGN_ICONS
115 int icon_sign;
116# endif
117
118 // Draw two cells with the sign value or blank.
119 *c_extrap = ' ';
120 *c_finalp = NUL;
121 if (nrcol)
122 *n_extrap = number_width(wp) + 1;
123 else
124 {
Bram Moolenaare413ea02021-11-24 16:20:13 +0000125 if (use_cursor_line_sign(wp, lnum))
126 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLS));
127 else
128 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200129 *n_extrap = 2;
130 }
131
132 if (row == startrow
133#ifdef FEAT_DIFF
134 + filler_lines && filler_todo <= 0
135#endif
136 )
137 {
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200138 text_sign = (sattr->sat_text != NULL) ? sattr->sat_typenr : 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200139# ifdef FEAT_SIGN_ICONS
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200140 icon_sign = (sattr->sat_icon != NULL) ? sattr->sat_typenr : 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200141 if (gui.in_use && icon_sign != 0)
142 {
143 // Use the image in this position.
144 if (nrcol)
145 {
146 *c_extrap = NUL;
147 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
148 *pp_extra = extra;
149 *n_extrap = (int)STRLEN(*pp_extra);
150 }
151 else
152 *c_extrap = SIGN_BYTE;
153# ifdef FEAT_NETBEANS_INTG
154 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
155 {
156 if (nrcol)
157 {
158 *c_extrap = NUL;
159 sprintf((char *)extra, "%-*c ", number_width(wp),
160 MULTISIGN_BYTE);
161 *pp_extra = extra;
162 *n_extrap = (int)STRLEN(*pp_extra);
163 }
164 else
165 *c_extrap = MULTISIGN_BYTE;
166 }
167# endif
168 *c_finalp = NUL;
169 *char_attrp = icon_sign;
170 }
171 else
172# endif
173 if (text_sign != 0)
174 {
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200175 *pp_extra = sattr->sat_text;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200176 if (*pp_extra != NULL)
177 {
178 if (nrcol)
179 {
180 int n, width = number_width(wp) - 2;
181
182 for (n = 0; n < width; n++)
183 extra[n] = ' ';
184 extra[n] = 0;
185 STRCAT(extra, *pp_extra);
186 STRCAT(extra, " ");
187 *pp_extra = extra;
188 }
189 *c_extrap = NUL;
190 *c_finalp = NUL;
191 *n_extrap = (int)STRLEN(*pp_extra);
192 }
Bram Moolenaare413ea02021-11-24 16:20:13 +0000193
194 if (use_cursor_line_sign(wp, lnum) && sattr->sat_culhl > 0)
195 *char_attrp = sattr->sat_culhl;
196 else
197 *char_attrp = sattr->sat_texthl;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200198 }
199 }
200}
201#endif
202
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100203#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200204static textprop_T *current_text_props = NULL;
205static buf_T *current_buf = NULL;
206
207 static int
208text_prop_compare(const void *s1, const void *s2)
209{
210 int idx1, idx2;
211 proptype_T *pt1, *pt2;
212 colnr_T col1, col2;
213
214 idx1 = *(int *)s1;
215 idx2 = *(int *)s2;
216 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
217 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
218 if (pt1 == pt2)
219 return 0;
220 if (pt1 == NULL)
221 return -1;
222 if (pt2 == NULL)
223 return 1;
224 if (pt1->pt_priority != pt2->pt_priority)
225 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
226 col1 = current_text_props[idx1].tp_col;
227 col2 = current_text_props[idx2].tp_col;
228 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
229}
230#endif
231
232/*
233 * Display line "lnum" of window 'wp' on the screen.
234 * Start at row "startrow", stop when "endrow" is reached.
235 * wp->w_virtcol needs to be valid.
236 *
237 * Return the number of last row the line occupies.
238 */
239 int
240win_line(
241 win_T *wp,
242 linenr_T lnum,
243 int startrow,
244 int endrow,
245 int nochange UNUSED, // not updating for changed text
246 int number_only) // only update the number column
247{
248 int col = 0; // visual column on screen
249 unsigned off; // offset in ScreenLines/ScreenAttrs
250 int c = 0; // init for GCC
251 long vcol = 0; // virtual column (for tabs)
252#ifdef FEAT_LINEBREAK
253 long vcol_sbr = -1; // virtual column after showbreak
254#endif
255 long vcol_prev = -1; // "vcol" of previous character
256 char_u *line; // current line
257 char_u *ptr; // current position in "line"
258 int row; // row in the window, excl w_winrow
259 int screen_row; // row on the screen, incl w_winrow
260
261 char_u extra[21]; // "%ld " and 'fdc' must fit in here
262 int n_extra = 0; // number of extra chars
263 char_u *p_extra = NULL; // string of extra chars, plus NUL
264 char_u *p_extra_free = NULL; // p_extra needs to be freed
265 int c_extra = NUL; // extra chars, all the same
266 int c_final = NUL; // final char, mandatory if set
267 int extra_attr = 0; // attributes when n_extra != 0
Bram Moolenaar3569c0d2021-12-02 11:34:21 +0000268#if defined(FEAT_LINEBREAK) && defined(FEAT_PROP_POPUP)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +0000269 int in_linebreak = FALSE; // n_extra set for showing linebreak
270#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200271 static char_u *at_end_str = (char_u *)""; // used for p_extra when
Bram Moolenaareed9d462021-02-15 20:38:25 +0100272 // displaying eol at end-of-line
273 int lcs_eol_one = wp->w_lcs_chars.eol; // eol until it's been used
Bram Moolenaar3569c0d2021-12-02 11:34:21 +0000274 int lcs_prec_todo = wp->w_lcs_chars.prec;
275 // prec until it's been used
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200276
277 // saved "extra" items for when draw_state becomes WL_LINE (again)
278 int saved_n_extra = 0;
279 char_u *saved_p_extra = NULL;
280 int saved_c_extra = 0;
281 int saved_c_final = 0;
282 int saved_char_attr = 0;
283
284 int n_attr = 0; // chars with special attr
285 int saved_attr2 = 0; // char_attr saved for n_attr
286 int n_attr3 = 0; // chars with overruling special attr
287 int saved_attr3 = 0; // char_attr saved for n_attr3
288
289 int n_skip = 0; // nr of chars to skip for 'nowrap'
290
291 int fromcol = -10; // start of inverting
292 int tocol = MAXCOL; // end of inverting
293 int fromcol_prev = -2; // start of inverting after cursor
294 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200295 int lnum_in_visual_area = FALSE;
296 pos_T pos;
297 long v;
298
299 int char_attr = 0; // attributes for next character
300 int attr_pri = FALSE; // char_attr has priority
301 int area_highlighting = FALSE; // Visual or incsearch highlighting
302 // in this line
303 int vi_attr = 0; // attributes for Visual and incsearch
304 // highlighting
305 int wcr_attr = 0; // attributes from 'wincolor'
306 int win_attr = 0; // background for whole window, except
307 // margins and "~" lines.
308 int area_attr = 0; // attributes desired by highlighting
309 int search_attr = 0; // attributes desired by 'hlsearch'
310#ifdef FEAT_SYN_HL
311 int vcol_save_attr = 0; // saved attr for 'cursorcolumn'
312 int syntax_attr = 0; // attributes desired by syntax
Bram Moolenaar9115c612019-10-16 16:57:06 +0200313 int prev_syntax_col = -1; // column of prev_syntax_attr
314 int prev_syntax_attr = 0; // syntax_attr at prev_syntax_col
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200315 int has_syntax = FALSE; // this buffer has syntax highl.
316 int save_did_emsg;
317 int draw_color_col = FALSE; // highlight colorcolumn
318 int *color_cols = NULL; // pointer to according columns array
319#endif
320 int eol_hl_off = 0; // 1 if highlighted char after EOL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100321#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200322 int text_prop_count;
323 int text_prop_next = 0; // next text property to use
324 textprop_T *text_props = NULL;
325 int *text_prop_idxs = NULL;
326 int text_props_active = 0;
327 proptype_T *text_prop_type = NULL;
328 int text_prop_attr = 0;
329 int text_prop_combine = FALSE;
330#endif
331#ifdef FEAT_SPELL
332 int has_spell = FALSE; // this buffer has spell checking
Bram Moolenaar34390282019-10-16 14:38:26 +0200333 int can_spell;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200334# define SPWORDLEN 150
335 char_u nextline[SPWORDLEN * 2];// text with start of the next line
336 int nextlinecol = 0; // column where nextline[] starts
337 int nextline_idx = 0; // index in nextline[] where next line
338 // starts
339 int spell_attr = 0; // attributes desired by spelling
340 int word_end = 0; // last byte with same spell_attr
341 static linenr_T checked_lnum = 0; // line number for "checked_col"
342 static int checked_col = 0; // column in "checked_lnum" up to which
343 // there are no spell errors
344 static int cap_col = -1; // column to check for Cap word
345 static linenr_T capcol_lnum = 0; // line number where "cap_col" used
346 int cur_checked_col = 0; // checked column for current line
347#endif
348 int extra_check = 0; // has extra highlighting
349 int multi_attr = 0; // attributes desired by multibyte
350 int mb_l = 1; // multi-byte byte length
351 int mb_c = 0; // decoded multi-byte character
352 int mb_utf8 = FALSE; // screen char is UTF-8 char
353 int u8cc[MAX_MCO]; // composing UTF-8 chars
354#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
355 int filler_lines = 0; // nr of filler lines to be drawn
356 int filler_todo = 0; // nr of filler lines still to do + 1
357#endif
358#ifdef FEAT_DIFF
359 hlf_T diff_hlf = (hlf_T)0; // type of diff highlighting
360 int change_start = MAXCOL; // first col of changed area
361 int change_end = -1; // last col of changed area
362#endif
363 colnr_T trailcol = MAXCOL; // start of trailing spaces
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100364 colnr_T leadcol = 0; // start of leading spaces
zeertzjqf14b8ba2021-09-10 16:58:30 +0200365 int in_multispace = FALSE; // in multiple consecutive spaces
366 int multispace_pos = 0; // position in lcs-multispace string
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200367#ifdef FEAT_LINEBREAK
368 int need_showbreak = FALSE; // overlong line, skipping first x
369 // chars
370#endif
371#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
372 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
373# define LINE_ATTR
374 int line_attr = 0; // attribute for the whole line
375 int line_attr_save;
376#endif
377#ifdef FEAT_SIGNS
378 int sign_present = FALSE;
379 sign_attrs_T sattr;
380#endif
381#ifdef FEAT_ARABIC
382 int prev_c = 0; // previous Arabic character
383 int prev_c1 = 0; // first composing char for prev_c
384#endif
385#if defined(LINE_ATTR)
386 int did_line_attr = 0;
387#endif
388#ifdef FEAT_TERMINAL
389 int get_term_attr = FALSE;
390#endif
391#ifdef FEAT_SYN_HL
392 int cul_attr = 0; // set when 'cursorline' active
393
394 // 'cursorlineopt' has "screenline" and cursor is in this line
395 int cul_screenline = FALSE;
396
397 // margin columns for the screen line, needed for when 'cursorlineopt'
398 // contains "screenline"
399 int left_curline_col = 0;
400 int right_curline_col = 0;
401#endif
402
403 // draw_state: items that are drawn in sequence:
404#define WL_START 0 // nothing done yet
405#ifdef FEAT_CMDWIN
406# define WL_CMDLINE WL_START + 1 // cmdline window column
407#else
408# define WL_CMDLINE WL_START
409#endif
410#ifdef FEAT_FOLDING
411# define WL_FOLD WL_CMDLINE + 1 // 'foldcolumn'
412#else
413# define WL_FOLD WL_CMDLINE
414#endif
415#ifdef FEAT_SIGNS
416# define WL_SIGN WL_FOLD + 1 // column for signs
417#else
418# define WL_SIGN WL_FOLD // column for signs
419#endif
420#define WL_NR WL_SIGN + 1 // line number
421#ifdef FEAT_LINEBREAK
422# define WL_BRI WL_NR + 1 // 'breakindent'
423#else
424# define WL_BRI WL_NR
425#endif
426#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
427# define WL_SBR WL_BRI + 1 // 'showbreak' or 'diff'
428#else
429# define WL_SBR WL_BRI
430#endif
431#define WL_LINE WL_SBR + 1 // text in the line
432 int draw_state = WL_START; // what to draw next
433#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
434 int feedback_col = 0;
435 int feedback_old_attr = -1;
436#endif
437 int screen_line_flags = 0;
438
439#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
440 int match_conc = 0; // cchar for match functions
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000441 int on_last_col = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200442#endif
443#ifdef FEAT_CONCEAL
444 int syntax_flags = 0;
445 int syntax_seqnr = 0;
446 int prev_syntax_id = 0;
447 int conceal_attr = HL_ATTR(HLF_CONCEAL);
448 int is_concealing = FALSE;
449 int boguscols = 0; // nonexistent columns added to force
450 // wrapping
451 int vcol_off = 0; // offset for concealed characters
452 int did_wcol = FALSE;
453 int old_boguscols = 0;
454# define VCOL_HLC (vcol - vcol_off)
455# define FIX_FOR_BOGUSCOLS \
456 { \
457 n_extra += vcol_off; \
458 vcol -= vcol_off; \
459 vcol_off = 0; \
460 col -= boguscols; \
461 old_boguscols = boguscols; \
462 boguscols = 0; \
463 }
464#else
465# define VCOL_HLC (vcol)
466#endif
467
468 if (startrow > endrow) // past the end already!
469 return startrow;
470
471 row = startrow;
472 screen_row = row + W_WINROW(wp);
473
474 if (!number_only)
475 {
476 // To speed up the loop below, set extra_check when there is linebreak,
477 // trailing white space and/or syntax processing to be done.
478#ifdef FEAT_LINEBREAK
479 extra_check = wp->w_p_lbr;
480#endif
481#ifdef FEAT_SYN_HL
482 if (syntax_present(wp) && !wp->w_s->b_syn_error
483# ifdef SYN_TIME_LIMIT
484 && !wp->w_s->b_syn_slow
485# endif
486 )
487 {
488 // Prepare for syntax highlighting in this line. When there is an
489 // error, stop syntax highlighting.
490 save_did_emsg = did_emsg;
491 did_emsg = FALSE;
492 syntax_start(wp, lnum);
493 if (did_emsg)
494 wp->w_s->b_syn_error = TRUE;
495 else
496 {
497 did_emsg = save_did_emsg;
498#ifdef SYN_TIME_LIMIT
499 if (!wp->w_s->b_syn_slow)
500#endif
501 {
502 has_syntax = TRUE;
503 extra_check = TRUE;
504 }
505 }
506 }
507
508 // Check for columns to display for 'colorcolumn'.
509 color_cols = wp->w_p_cc_cols;
510 if (color_cols != NULL)
511 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
512#endif
513
514#ifdef FEAT_TERMINAL
515 if (term_show_buffer(wp->w_buffer))
516 {
517 extra_check = TRUE;
518 get_term_attr = TRUE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +0100519 win_attr = term_get_attr(wp, lnum, -1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200520 }
521#endif
522
523#ifdef FEAT_SPELL
524 if (wp->w_p_spell
525 && *wp->w_s->b_p_spl != NUL
526 && wp->w_s->b_langp.ga_len > 0
527 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
528 {
529 // Prepare for spell checking.
530 has_spell = TRUE;
531 extra_check = TRUE;
532
533 // Get the start of the next line, so that words that wrap to the
534 // next line are found too: "et<line-break>al.".
535 // Trick: skip a few chars for C/shell/Vim comments
536 nextline[SPWORDLEN] = NUL;
537 if (lnum < wp->w_buffer->b_ml.ml_line_count)
538 {
539 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
540 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
541 }
542
543 // When a word wrapped from the previous line the start of the
544 // current line is valid.
545 if (lnum == checked_lnum)
546 cur_checked_col = checked_col;
547 checked_lnum = 0;
548
549 // When there was a sentence end in the previous line may require a
550 // word starting with capital in this line. In line 1 always check
551 // the first word.
552 if (lnum != capcol_lnum)
553 cap_col = -1;
554 if (lnum == 1)
555 cap_col = 0;
556 capcol_lnum = 0;
557 }
558#endif
559
560 // handle Visual active in this window
561 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
562 {
Bram Moolenaar14285cb2020-03-27 20:58:37 +0100563 pos_T *top, *bot;
564
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200565 if (LTOREQ_POS(curwin->w_cursor, VIsual))
566 {
567 // Visual is after curwin->w_cursor
568 top = &curwin->w_cursor;
569 bot = &VIsual;
570 }
571 else
572 {
573 // Visual is before curwin->w_cursor
574 top = &VIsual;
575 bot = &curwin->w_cursor;
576 }
577 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
578 if (VIsual_mode == Ctrl_V)
579 {
580 // block mode
581 if (lnum_in_visual_area)
582 {
583 fromcol = wp->w_old_cursor_fcol;
584 tocol = wp->w_old_cursor_lcol;
585 }
586 }
587 else
588 {
589 // non-block mode
590 if (lnum > top->lnum && lnum <= bot->lnum)
591 fromcol = 0;
592 else if (lnum == top->lnum)
593 {
594 if (VIsual_mode == 'V') // linewise
595 fromcol = 0;
596 else
597 {
598 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
599 if (gchar_pos(top) == NUL)
600 tocol = fromcol + 1;
601 }
602 }
603 if (VIsual_mode != 'V' && lnum == bot->lnum)
604 {
605 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
606 {
607 fromcol = -10;
608 tocol = MAXCOL;
609 }
610 else if (bot->col == MAXCOL)
611 tocol = MAXCOL;
612 else
613 {
614 pos = *bot;
615 if (*p_sel == 'e')
616 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
617 else
618 {
619 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
620 ++tocol;
621 }
622 }
623 }
624 }
625
626 // Check if the character under the cursor should not be inverted
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200627 if (!highlight_match && lnum == curwin->w_cursor.lnum
628 && wp == curwin
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200629#ifdef FEAT_GUI
630 && !gui.in_use
631#endif
632 )
633 noinvcur = TRUE;
634
635 // if inverting in this line set area_highlighting
636 if (fromcol >= 0)
637 {
638 area_highlighting = TRUE;
639 vi_attr = HL_ATTR(HLF_V);
640#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
641 if ((clip_star.available && !clip_star.owned
642 && clip_isautosel_star())
643 || (clip_plus.available && !clip_plus.owned
644 && clip_isautosel_plus()))
645 vi_attr = HL_ATTR(HLF_VNC);
646#endif
647 }
648 }
649
650 // handle 'incsearch' and ":s///c" highlighting
651 else if (highlight_match
652 && wp == curwin
653 && lnum >= curwin->w_cursor.lnum
654 && lnum <= curwin->w_cursor.lnum + search_match_lines)
655 {
656 if (lnum == curwin->w_cursor.lnum)
657 getvcol(curwin, &(curwin->w_cursor),
658 (colnr_T *)&fromcol, NULL, NULL);
659 else
660 fromcol = 0;
661 if (lnum == curwin->w_cursor.lnum + search_match_lines)
662 {
663 pos.lnum = lnum;
664 pos.col = search_match_endcol;
665 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
666 }
667 else
668 tocol = MAXCOL;
669 // do at least one character; happens when past end of line
Bram Moolenaar448465e2020-11-25 13:49:27 +0100670 if (fromcol == tocol && search_match_endcol)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200671 tocol = fromcol + 1;
672 area_highlighting = TRUE;
673 vi_attr = HL_ATTR(HLF_I);
674 }
675 }
676
677#ifdef FEAT_DIFF
678 filler_lines = diff_check(wp, lnum);
679 if (filler_lines < 0)
680 {
681 if (filler_lines == -1)
682 {
683 if (diff_find_change(wp, lnum, &change_start, &change_end))
684 diff_hlf = HLF_ADD; // added line
685 else if (change_start == 0)
686 diff_hlf = HLF_TXD; // changed text
687 else
688 diff_hlf = HLF_CHD; // changed line
689 }
690 else
691 diff_hlf = HLF_ADD; // added line
692 filler_lines = 0;
693 area_highlighting = TRUE;
694 }
695 if (lnum == wp->w_topline)
696 filler_lines = wp->w_topfill;
697 filler_todo = filler_lines;
698#endif
699
700#ifdef FEAT_SIGNS
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +0100701 sign_present = buf_get_signattrs(wp, lnum, &sattr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200702#endif
703
704#ifdef LINE_ATTR
705# ifdef FEAT_SIGNS
706 // If this line has a sign with line highlighting set line_attr.
707 if (sign_present)
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200708 line_attr = sattr.sat_linehl;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200709# endif
710# if defined(FEAT_QUICKFIX)
711 // Highlight the current line in the quickfix window.
712 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
713 line_attr = HL_ATTR(HLF_QFL);
714# endif
715 if (line_attr != 0)
716 area_highlighting = TRUE;
717#endif
718
719 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
720 ptr = line;
721
722#ifdef FEAT_SPELL
723 if (has_spell && !number_only)
724 {
725 // For checking first word with a capital skip white space.
726 if (cap_col == 0)
727 cap_col = getwhitecols(line);
728
729 // To be able to spell-check over line boundaries copy the end of the
730 // current line into nextline[]. Above the start of the next line was
731 // copied to nextline[SPWORDLEN].
732 if (nextline[SPWORDLEN] == NUL)
733 {
734 // No next line or it is empty.
735 nextlinecol = MAXCOL;
736 nextline_idx = 0;
737 }
738 else
739 {
740 v = (long)STRLEN(line);
741 if (v < SPWORDLEN)
742 {
743 // Short line, use it completely and append the start of the
744 // next line.
745 nextlinecol = 0;
746 mch_memmove(nextline, line, (size_t)v);
747 STRMOVE(nextline + v, nextline + SPWORDLEN);
748 nextline_idx = v + 1;
749 }
750 else
751 {
752 // Long line, use only the last SPWORDLEN bytes.
753 nextlinecol = v - SPWORDLEN;
754 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
755 nextline_idx = SPWORDLEN + 1;
756 }
757 }
758 }
759#endif
760
761 if (wp->w_p_list)
762 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100763 if (wp->w_lcs_chars.space
zeertzjqf14b8ba2021-09-10 16:58:30 +0200764 || wp->w_lcs_chars.multispace != NULL
Bram Moolenaareed9d462021-02-15 20:38:25 +0100765 || wp->w_lcs_chars.trail
766 || wp->w_lcs_chars.lead
767 || wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200768 extra_check = TRUE;
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100769
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200770 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +0100771 if (wp->w_lcs_chars.trail)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200772 {
773 trailcol = (colnr_T)STRLEN(ptr);
774 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
775 --trailcol;
776 trailcol += (colnr_T) (ptr - line);
777 }
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100778 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +0100779 if (wp->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100780 {
781 leadcol = 0;
782 while (VIM_ISWHITE(ptr[leadcol]))
783 ++leadcol;
784 if (ptr[leadcol] == NUL)
785 // in a line full of spaces all of them are treated as trailing
786 leadcol = (colnr_T)0;
787 else
788 // keep track of the first column not filled with spaces
789 leadcol += (colnr_T) (ptr - line) + 1;
790 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200791 }
792
793 wcr_attr = get_wcr_attr(wp);
794 if (wcr_attr != 0)
795 {
796 win_attr = wcr_attr;
797 area_highlighting = TRUE;
798 }
Bram Moolenaar34390282019-10-16 14:38:26 +0200799
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100800#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200801 if (WIN_IS_POPUP(wp))
802 screen_line_flags |= SLF_POPUP;
803#endif
804
805 // 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
806 // first character to be displayed.
807 if (wp->w_p_wrap)
808 v = wp->w_skipcol;
809 else
810 v = wp->w_leftcol;
811 if (v > 0 && !number_only)
812 {
813 char_u *prev_ptr = ptr;
814
815 while (vcol < v && *ptr != NUL)
816 {
817 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
818 vcol += c;
819 prev_ptr = ptr;
820 MB_PTR_ADV(ptr);
821 }
822
823 // When:
824 // - 'cuc' is set, or
825 // - 'colorcolumn' is set, or
826 // - 'virtualedit' is set, or
827 // - the visual mode is active,
828 // the end of the line may be before the start of the displayed part.
829 if (vcol < v && (
830#ifdef FEAT_SYN_HL
831 wp->w_p_cuc || draw_color_col ||
832#endif
833 virtual_active() ||
834 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
835 vcol = v;
836
837 // Handle a character that's not completely on the screen: Put ptr at
838 // that character but skip the first few screen characters.
839 if (vcol > v)
840 {
841 vcol -= c;
842 ptr = prev_ptr;
843 // If the character fits on the screen, don't need to skip it.
844 // Except for a TAB.
845 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
846 n_skip = v - vcol;
847 }
848
849 // Adjust for when the inverted text is before the screen,
850 // and when the start of the inverted text is before the screen.
851 if (tocol <= vcol)
852 fromcol = 0;
853 else if (fromcol >= 0 && fromcol < vcol)
854 fromcol = vcol;
855
856#ifdef FEAT_LINEBREAK
857 // When w_skipcol is non-zero, first line needs 'showbreak'
858 if (wp->w_p_wrap)
859 need_showbreak = TRUE;
860#endif
861#ifdef FEAT_SPELL
862 // When spell checking a word we need to figure out the start of the
863 // word and if it's badly spelled or not.
864 if (has_spell)
865 {
866 int len;
867 colnr_T linecol = (colnr_T)(ptr - line);
868 hlf_T spell_hlf = HLF_COUNT;
869
870 pos = wp->w_cursor;
871 wp->w_cursor.lnum = lnum;
872 wp->w_cursor.col = linecol;
873 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
874
875 // spell_move_to() may call ml_get() and make "line" invalid
876 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
877 ptr = line + linecol;
878
879 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
880 {
881 // no bad word found at line start, don't check until end of a
882 // word
883 spell_hlf = HLF_COUNT;
884 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
885 }
886 else
887 {
888 // bad word found, use attributes until end of word
889 word_end = wp->w_cursor.col + len + 1;
890
891 // Turn index into actual attributes.
892 if (spell_hlf != HLF_COUNT)
893 spell_attr = highlight_attr[spell_hlf];
894 }
895 wp->w_cursor = pos;
896
897# ifdef FEAT_SYN_HL
898 // Need to restart syntax highlighting for this line.
899 if (has_syntax)
900 syntax_start(wp, lnum);
901# endif
902 }
903#endif
904 }
905
906 // Correct highlighting for cursor that can't be disabled.
907 // Avoids having to check this for each character.
908 if (fromcol >= 0)
909 {
910 if (noinvcur)
911 {
912 if ((colnr_T)fromcol == wp->w_virtcol)
913 {
914 // highlighting starts at cursor, let it start just after the
915 // cursor
916 fromcol_prev = fromcol;
917 fromcol = -1;
918 }
919 else if ((colnr_T)fromcol < wp->w_virtcol)
920 // restart highlighting after the cursor
921 fromcol_prev = wp->w_virtcol;
922 }
923 if (fromcol >= tocol)
924 fromcol = -1;
925 }
926
927#ifdef FEAT_SEARCH_EXTRA
928 if (!number_only)
929 {
930 v = (long)(ptr - line);
931 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
932 &line, &screen_search_hl,
933 &search_attr);
934 ptr = line + v; // "line" may have been updated
935 }
936#endif
937
938#ifdef FEAT_SYN_HL
939 // Cursor line highlighting for 'cursorline' in the current window.
940 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
941 {
942 // Do not show the cursor line in the text when Visual mode is active,
943 // because it's not clear what is selected then. Do update
944 // w_last_cursorline.
945 if (!(wp == curwin && VIsual_active)
946 && wp->w_p_culopt_flags != CULOPT_NBR)
947 {
948 cul_screenline = (wp->w_p_wrap
949 && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
950
951 // Only set line_attr here when "screenline" is not present in
952 // 'cursorlineopt'. Otherwise it's done later.
953 if (!cul_screenline)
954 {
955 cul_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar39f7aa32020-08-31 22:00:05 +0200956# ifdef FEAT_SIGNS
957 // Combine the 'cursorline' and sign highlighting, depending on
958 // the sign priority.
959 if (sign_present && sattr.sat_linehl > 0)
960 {
961 if (sattr.sat_priority >= 100)
962 line_attr = hl_combine_attr(cul_attr, line_attr);
963 else
964 line_attr = hl_combine_attr(line_attr, cul_attr);
965 }
966 else
967# endif
968 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200969 wp->w_last_cursorline = wp->w_cursor.lnum;
970 }
971 else
972 {
973 line_attr_save = line_attr;
974 wp->w_last_cursorline = 0;
975 margin_columns_win(wp, &left_curline_col, &right_curline_col);
976 }
977 area_highlighting = TRUE;
978 }
979 else
980 wp->w_last_cursorline = wp->w_cursor.lnum;
981 }
982#endif
983
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100984#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200985 {
986 char_u *prop_start;
987
988 text_prop_count = get_text_props(wp->w_buffer, lnum,
989 &prop_start, FALSE);
990 if (text_prop_count > 0)
991 {
992 // Make a copy of the properties, so that they are properly
993 // aligned.
994 text_props = ALLOC_MULT(textprop_T, text_prop_count);
995 if (text_props != NULL)
996 mch_memmove(text_props, prop_start,
997 text_prop_count * sizeof(textprop_T));
998
999 // Allocate an array for the indexes.
1000 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
1001 area_highlighting = TRUE;
1002 extra_check = TRUE;
1003 }
1004 }
1005#endif
1006
1007 off = (unsigned)(current_ScreenLine - ScreenLines);
1008 col = 0;
1009
1010#ifdef FEAT_RIGHTLEFT
1011 if (wp->w_p_rl)
1012 {
1013 // Rightleft window: process the text in the normal direction, but put
1014 // it in current_ScreenLine[] from right to left. Start at the
1015 // rightmost column of the window.
1016 col = wp->w_width - 1;
1017 off += col;
1018 screen_line_flags |= SLF_RIGHTLEFT;
1019 }
1020#endif
1021
1022 // Repeat for the whole displayed line.
1023 for (;;)
1024 {
1025#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02001026 int has_match_conc = 0; // match wants to conceal
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001027#endif
1028#ifdef FEAT_CONCEAL
1029 int did_decrement_ptr = FALSE;
1030#endif
1031 // Skip this quickly when working on the text.
1032 if (draw_state != WL_LINE)
1033 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001034#ifdef FEAT_SYN_HL
1035 if (cul_screenline)
1036 {
1037 cul_attr = 0;
1038 line_attr = line_attr_save;
1039 }
1040#endif
1041
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001042#ifdef FEAT_CMDWIN
1043 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
1044 {
1045 draw_state = WL_CMDLINE;
1046 if (cmdwin_type != 0 && wp == curwin)
1047 {
1048 // Draw the cmdline character.
1049 n_extra = 1;
1050 c_extra = cmdwin_type;
1051 c_final = NUL;
1052 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
1053 }
1054 }
1055#endif
1056
1057#ifdef FEAT_FOLDING
1058 if (draw_state == WL_FOLD - 1 && n_extra == 0)
1059 {
1060 int fdc = compute_foldcolumn(wp, 0);
1061
1062 draw_state = WL_FOLD;
1063 if (fdc > 0)
1064 {
1065 // Draw the 'foldcolumn'. Allocate a buffer, "extra" may
1066 // already be in use.
1067 vim_free(p_extra_free);
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001068 p_extra_free = alloc(MAX_MCO * fdc + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001069 if (p_extra_free != NULL)
1070 {
Bram Moolenaar9355ae42021-03-08 19:04:05 +01001071 n_extra = (int)fill_foldcolumn(p_extra_free, wp,
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001072 FALSE, lnum);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001073 p_extra_free[n_extra] = NUL;
1074 p_extra = p_extra_free;
1075 c_extra = NUL;
1076 c_final = NUL;
Bram Moolenaare413ea02021-11-24 16:20:13 +00001077 if (use_cursor_line_sign(wp, lnum))
1078 char_attr =
1079 hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLF));
1080 else
1081 char_attr =
1082 hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001083 }
1084 }
1085 }
1086#endif
1087
1088#ifdef FEAT_SIGNS
1089 if (draw_state == WL_SIGN - 1 && n_extra == 0)
1090 {
1091 draw_state = WL_SIGN;
1092 // Show the sign column when there are any signs in this
1093 // buffer or when using Netbeans.
1094 if (signcolumn_on(wp))
1095 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
1096 row, startrow, filler_lines, filler_todo, &c_extra,
1097 &c_final, extra, &p_extra, &n_extra, &char_attr);
1098 }
1099#endif
1100
1101 if (draw_state == WL_NR - 1 && n_extra == 0)
1102 {
1103 draw_state = WL_NR;
1104 // Display the absolute or relative line number. After the
1105 // first fill with blanks when the 'n' flag isn't in 'cpo'
1106 if ((wp->w_p_nu || wp->w_p_rnu)
1107 && (row == startrow
1108#ifdef FEAT_DIFF
1109 + filler_lines
1110#endif
1111 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
1112 {
1113#ifdef FEAT_SIGNS
1114 // If 'signcolumn' is set to 'number' and a sign is present
1115 // in 'lnum', then display the sign instead of the line
1116 // number.
1117 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
1118 && sign_present)
1119 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
1120 row, startrow, filler_lines, filler_todo,
1121 &c_extra, &c_final, extra, &p_extra, &n_extra,
1122 &char_attr);
1123 else
1124#endif
1125 {
1126 // Draw the line number (empty space after wrapping).
1127 if (row == startrow
1128#ifdef FEAT_DIFF
1129 + filler_lines
1130#endif
1131 )
1132 {
1133 long num;
1134 char *fmt = "%*ld ";
1135
1136 if (wp->w_p_nu && !wp->w_p_rnu)
1137 // 'number' + 'norelativenumber'
1138 num = (long)lnum;
1139 else
1140 {
1141 // 'relativenumber', don't use negative numbers
1142 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1143 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1144 {
1145 // 'number' + 'relativenumber'
1146 num = lnum;
1147 fmt = "%-*ld ";
1148 }
1149 }
1150
1151 sprintf((char *)extra, fmt,
1152 number_width(wp), num);
1153 if (wp->w_skipcol > 0)
1154 for (p_extra = extra; *p_extra == ' '; ++p_extra)
1155 *p_extra = '-';
1156#ifdef FEAT_RIGHTLEFT
1157 if (wp->w_p_rl) // reverse line numbers
1158 {
1159 char_u *p1, *p2;
1160 int t;
1161
1162 // like rl_mirror(), but keep the space at the end
Christian Brabandt29f0dc32021-06-16 19:28:34 +02001163 p2 = skipwhite(extra);
1164 p2 = skiptowhite(p2) - 1;
1165 for (p1 = skipwhite(extra); p1 < p2; ++p1, --p2)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001166 {
1167 t = *p1;
1168 *p1 = *p2;
1169 *p2 = t;
1170 }
1171 }
1172#endif
1173 p_extra = extra;
1174 c_extra = NUL;
1175 c_final = NUL;
1176 }
1177 else
1178 {
1179 c_extra = ' ';
1180 c_final = NUL;
1181 }
1182 n_extra = number_width(wp) + 1;
1183 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
1184#ifdef FEAT_SYN_HL
1185 // When 'cursorline' is set highlight the line number of
1186 // the current line differently.
1187 // When 'cursorlineopt' has "screenline" only highlight
1188 // the line number itself.
1189 // TODO: Can we use CursorLine instead of CursorLineNr
1190 // when CursorLineNr isn't set?
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001191 if (wp->w_p_cul
1192 && lnum == wp->w_cursor.lnum
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001193 && (wp->w_p_culopt_flags & CULOPT_NBR)
1194 && (row == startrow
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001195 || wp->w_p_culopt_flags & CULOPT_LINE))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001196 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
1197#endif
Bram Moolenaarefae76a2019-10-27 22:54:58 +01001198 if (wp->w_p_rnu && lnum < wp->w_cursor.lnum
1199 && HL_ATTR(HLF_LNA) != 0)
1200 // Use LineNrAbove
1201 char_attr = hl_combine_attr(wcr_attr,
1202 HL_ATTR(HLF_LNA));
1203 if (wp->w_p_rnu && lnum > wp->w_cursor.lnum
1204 && HL_ATTR(HLF_LNB) != 0)
1205 // Use LineNrBelow
1206 char_attr = hl_combine_attr(wcr_attr,
1207 HL_ATTR(HLF_LNB));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001208 }
1209 }
1210 }
1211
1212#ifdef FEAT_LINEBREAK
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001213 if (wp->w_briopt_sbr && draw_state == WL_BRI - 1
Bram Moolenaaree857022019-11-09 23:26:40 +01001214 && n_extra == 0 && *get_showbreak_value(wp) != NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001215 // draw indent after showbreak value
1216 draw_state = WL_BRI;
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001217 else if (wp->w_briopt_sbr && draw_state == WL_SBR && n_extra == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001218 // After the showbreak, draw the breakindent
1219 draw_state = WL_BRI - 1;
1220
1221 // draw 'breakindent': indent wrapped text accordingly
1222 if (draw_state == WL_BRI - 1 && n_extra == 0)
1223 {
1224 draw_state = WL_BRI;
1225 // if need_showbreak is set, breakindent also applies
1226 if (wp->w_p_bri && n_extra == 0
1227 && (row != startrow || need_showbreak)
1228# ifdef FEAT_DIFF
1229 && filler_lines == 0
1230# endif
1231 )
1232 {
1233 char_attr = 0;
1234# ifdef FEAT_DIFF
1235 if (diff_hlf != (hlf_T)0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001236 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001237# endif
1238 p_extra = NULL;
1239 c_extra = ' ';
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001240 c_final = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001241 n_extra = get_breakindent_win(wp,
1242 ml_get_buf(wp->w_buffer, lnum, FALSE));
Bram Moolenaare882f7a2020-05-16 14:07:39 +02001243 if (row == startrow)
1244 {
1245 n_extra -= win_col_off2(wp);
1246 if (n_extra < 0)
1247 n_extra = 0;
1248 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001249 if (wp->w_skipcol > 0 && wp->w_p_wrap && wp->w_briopt_sbr)
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001250 need_showbreak = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001251 // Correct end of highlighted area for 'breakindent',
1252 // required when 'linebreak' is also set.
1253 if (tocol == vcol)
1254 tocol += n_extra;
1255 }
1256 }
1257#endif
1258
1259#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
1260 if (draw_state == WL_SBR - 1 && n_extra == 0)
1261 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001262 char_u *sbr;
1263
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001264 draw_state = WL_SBR;
1265# ifdef FEAT_DIFF
1266 if (filler_todo > 0)
1267 {
1268 // Draw "deleted" diff line(s).
1269 if (char2cells(fill_diff) > 1)
1270 {
1271 c_extra = '-';
1272 c_final = NUL;
1273 }
1274 else
1275 {
1276 c_extra = fill_diff;
1277 c_final = NUL;
1278 }
1279# ifdef FEAT_RIGHTLEFT
1280 if (wp->w_p_rl)
1281 n_extra = col + 1;
1282 else
1283# endif
1284 n_extra = wp->w_width - col;
1285 char_attr = HL_ATTR(HLF_DED);
1286 }
1287# endif
1288# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001289 sbr = get_showbreak_value(wp);
1290 if (*sbr != NUL && need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001291 {
1292 // Draw 'showbreak' at the start of each broken line.
Bram Moolenaaree857022019-11-09 23:26:40 +01001293 p_extra = sbr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001294 c_extra = NUL;
1295 c_final = NUL;
Bram Moolenaaree857022019-11-09 23:26:40 +01001296 n_extra = (int)STRLEN(sbr);
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001297 if (wp->w_skipcol == 0 || !wp->w_p_wrap)
1298 need_showbreak = FALSE;
Bram Moolenaaree857022019-11-09 23:26:40 +01001299 vcol_sbr = vcol + MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001300 // Correct end of highlighted area for 'showbreak',
1301 // required when 'linebreak' is also set.
1302 if (tocol == vcol)
1303 tocol += n_extra;
1304 // combine 'showbreak' with 'wincolor'
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001305 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001306# ifdef FEAT_SYN_HL
1307 // combine 'showbreak' with 'cursorline'
1308 if (cul_attr != 0)
1309 char_attr = hl_combine_attr(char_attr, cul_attr);
1310# endif
1311 }
1312# endif
1313 }
1314#endif
1315
1316 if (draw_state == WL_LINE - 1 && n_extra == 0)
1317 {
1318 draw_state = WL_LINE;
1319 if (saved_n_extra)
1320 {
1321 // Continue item from end of wrapped line.
1322 n_extra = saved_n_extra;
1323 c_extra = saved_c_extra;
1324 c_final = saved_c_final;
1325 p_extra = saved_p_extra;
1326 char_attr = saved_char_attr;
1327 }
1328 else
1329 char_attr = win_attr;
1330 }
1331 }
1332#ifdef FEAT_SYN_HL
zeertzjq4f33bc22021-08-05 17:57:02 +02001333 if (cul_screenline && draw_state == WL_LINE
1334 && vcol >= left_curline_col
1335 && vcol < right_curline_col)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001336 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001337 cul_attr = HL_ATTR(HLF_CUL);
1338 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001339 }
1340#endif
1341
1342 // When still displaying '$' of change command, stop at cursor.
1343 // When only displaying the (relative) line number and that's done,
1344 // stop here.
Bram Moolenaar511feec2020-06-18 19:15:27 +02001345 if (((dollar_vcol >= 0 && wp == curwin
1346 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol)
1347 || (number_only && draw_state > WL_NR))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001348#ifdef FEAT_DIFF
1349 && filler_todo <= 0
1350#endif
1351 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001352 {
1353 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
1354 screen_line_flags);
1355 // Pretend we have finished updating the window. Except when
1356 // 'cursorcolumn' is set.
1357#ifdef FEAT_SYN_HL
1358 if (wp->w_p_cuc)
1359 row = wp->w_cline_row + wp->w_cline_height;
1360 else
1361#endif
1362 row = wp->w_height;
1363 break;
1364 }
1365
Bram Moolenaar34390282019-10-16 14:38:26 +02001366 if (draw_state == WL_LINE && (area_highlighting || extra_check))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001367 {
1368 // handle Visual or match highlighting in this line
1369 if (vcol == fromcol
1370 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
1371 && (*mb_ptr2cells)(ptr) > 1)
1372 || ((int)vcol_prev == fromcol_prev
1373 && vcol_prev < vcol // not at margin
1374 && vcol < tocol))
1375 area_attr = vi_attr; // start highlighting
1376 else if (area_attr != 0
1377 && (vcol == tocol
1378 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
1379 area_attr = 0; // stop highlighting
1380
1381#ifdef FEAT_SEARCH_EXTRA
1382 if (!n_extra)
1383 {
1384 // Check for start/end of 'hlsearch' and other matches.
1385 // After end, check for start/end of next match.
1386 // When another match, have to check for start again.
1387 v = (long)(ptr - line);
1388 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
1389 &screen_search_hl, &has_match_conc,
Bram Moolenaar0c359af2021-11-29 19:18:57 +00001390 &match_conc, did_line_attr, lcs_eol_one,
1391 &on_last_col);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001392 ptr = line + v; // "line" may have been changed
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001393
1394 // Do not allow a conceal over EOL otherwise EOL will be missed
1395 // and bad things happen.
1396 if (*ptr == NUL)
1397 has_match_conc = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001398 }
1399#endif
1400
1401#ifdef FEAT_DIFF
1402 if (diff_hlf != (hlf_T)0)
1403 {
1404 if (diff_hlf == HLF_CHD && ptr - line >= change_start
1405 && n_extra == 0)
1406 diff_hlf = HLF_TXD; // changed text
1407 if (diff_hlf == HLF_TXD && ptr - line > change_end
1408 && n_extra == 0)
1409 diff_hlf = HLF_CHD; // changed line
1410 line_attr = HL_ATTR(diff_hlf);
1411 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
1412 && wp->w_p_culopt_flags != CULOPT_NBR
1413 && (!cul_screenline || (vcol >= left_curline_col
1414 && vcol <= right_curline_col)))
1415 line_attr = hl_combine_attr(
1416 line_attr, HL_ATTR(HLF_CUL));
1417 }
1418#endif
1419
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001420#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001421 if (text_props != NULL)
1422 {
1423 int pi;
1424 int bcol = (int)(ptr - line);
1425
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001426 if (n_extra > 0
1427# ifdef FEAT_LINEBREAK
1428 && !in_linebreak
1429# endif
1430 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001431 --bcol; // still working on the previous char, e.g. Tab
1432
1433 // Check if any active property ends.
1434 for (pi = 0; pi < text_props_active; ++pi)
1435 {
1436 int tpi = text_prop_idxs[pi];
1437
1438 if (bcol >= text_props[tpi].tp_col - 1
1439 + text_props[tpi].tp_len)
1440 {
1441 if (pi + 1 < text_props_active)
1442 mch_memmove(text_prop_idxs + pi,
1443 text_prop_idxs + pi + 1,
1444 sizeof(int)
1445 * (text_props_active - (pi + 1)));
1446 --text_props_active;
1447 --pi;
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001448# ifdef FEAT_LINEBREAK
1449 // not exactly right but should work in most cases
1450 if (in_linebreak && syntax_attr == text_prop_attr)
1451 syntax_attr = 0;
1452# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001453 }
1454 }
1455
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001456# ifdef FEAT_LINEBREAK
1457 if (n_extra > 0 && in_linebreak)
1458 // not on the next char yet, don't start another prop
1459 --bcol;
1460# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001461 // Add any text property that starts in this column.
1462 while (text_prop_next < text_prop_count
1463 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001464 {
1465 if (bcol <= text_props[text_prop_next].tp_col - 1
1466 + text_props[text_prop_next].tp_len)
1467 text_prop_idxs[text_props_active++] = text_prop_next;
1468 ++text_prop_next;
1469 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001470
1471 text_prop_attr = 0;
1472 text_prop_combine = FALSE;
Bram Moolenaar053f7122019-09-23 22:17:15 +02001473 text_prop_type = NULL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001474 if (text_props_active > 0)
1475 {
1476 // Sort the properties on priority and/or starting last.
1477 // Then combine the attributes, highest priority last.
1478 current_text_props = text_props;
1479 current_buf = wp->w_buffer;
1480 qsort((void *)text_prop_idxs, (size_t)text_props_active,
1481 sizeof(int), text_prop_compare);
1482
1483 for (pi = 0; pi < text_props_active; ++pi)
1484 {
1485 int tpi = text_prop_idxs[pi];
1486 proptype_T *pt = text_prop_type_by_id(
1487 wp->w_buffer, text_props[tpi].tp_type);
1488
1489 if (pt != NULL && pt->pt_hl_id > 0)
1490 {
1491 int pt_attr = syn_id2attr(pt->pt_hl_id);
1492
1493 text_prop_type = pt;
1494 text_prop_attr =
1495 hl_combine_attr(text_prop_attr, pt_attr);
1496 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
1497 }
1498 }
1499 }
1500 }
1501#endif
1502
Bram Moolenaara74fda62019-10-19 17:38:03 +02001503#ifdef FEAT_SYN_HL
Bram Moolenaara74fda62019-10-19 17:38:03 +02001504 if (extra_check && n_extra == 0)
Bram Moolenaar34390282019-10-16 14:38:26 +02001505 {
Bram Moolenaar82260af2019-10-20 13:16:22 +02001506 syntax_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001507# ifdef FEAT_TERMINAL
Bram Moolenaar34390282019-10-16 14:38:26 +02001508 if (get_term_attr)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001509 syntax_attr = term_get_attr(wp, lnum, vcol);
Bram Moolenaara74fda62019-10-19 17:38:03 +02001510# endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001511 // Get syntax attribute.
1512 if (has_syntax)
1513 {
1514 // Get the syntax attribute for the character. If there
1515 // is an error, disable syntax highlighting.
1516 save_did_emsg = did_emsg;
1517 did_emsg = FALSE;
1518
1519 v = (long)(ptr - line);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001520 if (v == prev_syntax_col)
1521 // at same column again
1522 syntax_attr = prev_syntax_attr;
1523 else
1524 {
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001525# ifdef FEAT_SPELL
Bram Moolenaar9115c612019-10-16 16:57:06 +02001526 can_spell = TRUE;
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001527# endif
Bram Moolenaar9115c612019-10-16 16:57:06 +02001528 syntax_attr = get_syntax_attr((colnr_T)v,
Bram Moolenaar34390282019-10-16 14:38:26 +02001529# ifdef FEAT_SPELL
1530 has_spell ? &can_spell :
1531# endif
1532 NULL, FALSE);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001533 prev_syntax_col = v;
1534 prev_syntax_attr = syntax_attr;
1535 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001536
Bram Moolenaar34390282019-10-16 14:38:26 +02001537 if (did_emsg)
1538 {
1539 wp->w_s->b_syn_error = TRUE;
1540 has_syntax = FALSE;
1541 syntax_attr = 0;
1542 }
1543 else
1544 did_emsg = save_did_emsg;
1545# ifdef SYN_TIME_LIMIT
1546 if (wp->w_s->b_syn_slow)
1547 has_syntax = FALSE;
1548# endif
1549
1550 // Need to get the line again, a multi-line regexp may
1551 // have made it invalid.
1552 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1553 ptr = line + v;
1554# ifdef FEAT_CONCEAL
1555 // no concealing past the end of the line, it interferes
1556 // with line highlighting
1557 if (*ptr == NUL)
1558 syntax_flags = 0;
1559 else
1560 syntax_flags = get_syntax_info(&syntax_seqnr);
1561# endif
1562 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001563 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001564# ifdef FEAT_PROP_POPUP
Bram Moolenaardbd43162019-11-09 21:28:14 +01001565 // Combine text property highlight into syntax highlight.
1566 if (text_prop_type != NULL)
1567 {
1568 if (text_prop_combine)
1569 syntax_attr = hl_combine_attr(syntax_attr, text_prop_attr);
1570 else
1571 syntax_attr = text_prop_attr;
1572 }
1573# endif
Bram Moolenaara74fda62019-10-19 17:38:03 +02001574#endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001575
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001576 // Decide which of the highlight attributes to use.
1577 attr_pri = TRUE;
1578#ifdef LINE_ATTR
1579 if (area_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001580 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001581 char_attr = hl_combine_attr(line_attr, area_attr);
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001582 if (!highlight_match)
1583 // let search highlight show in Visual area if possible
1584 char_attr = hl_combine_attr(search_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001585# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001586 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001587# endif
1588 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001589 else if (search_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001590 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001591 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001592# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001593 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001594# endif
1595 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001596 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
1597 || vcol < fromcol || vcol_prev < fromcol_prev
1598 || vcol >= tocol))
1599 {
1600 // Use line_attr when not in the Visual or 'incsearch' area
1601 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar34390282019-10-16 14:38:26 +02001602# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001603 char_attr = hl_combine_attr(syntax_attr, line_attr);
1604# else
1605 char_attr = line_attr;
Bram Moolenaar34390282019-10-16 14:38:26 +02001606# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001607 attr_pri = FALSE;
1608 }
1609#else
1610 if (area_attr != 0)
1611 char_attr = area_attr;
1612 else if (search_attr != 0)
1613 char_attr = search_attr;
1614#endif
1615 else
1616 {
1617 attr_pri = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001618#ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001619 char_attr = syntax_attr;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001620#else
Bram Moolenaardbd43162019-11-09 21:28:14 +01001621 char_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001622#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001623 }
1624 }
Bram Moolenaar024dbd22019-11-02 22:00:15 +01001625
1626 // combine attribute with 'wincolor'
1627 if (win_attr != 0)
1628 {
1629 if (char_attr == 0)
1630 char_attr = win_attr;
1631 else
1632 char_attr = hl_combine_attr(win_attr, char_attr);
1633 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001634
1635 // Get the next character to put on the screen.
1636
1637 // The "p_extra" points to the extra stuff that is inserted to
1638 // represent special characters (non-printable stuff) and other
1639 // things. When all characters are the same, c_extra is used.
1640 // If c_final is set, it will compulsorily be used at the end.
1641 // "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
1642 // "p_extra[n_extra]".
1643 // For the '$' of the 'list' option, n_extra == 1, p_extra == "".
1644 if (n_extra > 0)
1645 {
1646 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
1647 {
1648 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
1649 mb_c = c; // doesn't handle non-utf-8 multi-byte!
1650 if (enc_utf8 && utf_char2len(c) > 1)
1651 {
1652 mb_utf8 = TRUE;
1653 u8cc[0] = 0;
1654 c = 0xc0;
1655 }
1656 else
1657 mb_utf8 = FALSE;
1658 }
1659 else
1660 {
1661 c = *p_extra;
1662 if (has_mbyte)
1663 {
1664 mb_c = c;
1665 if (enc_utf8)
1666 {
1667 // If the UTF-8 character is more than one byte:
1668 // Decode it into "mb_c".
1669 mb_l = utfc_ptr2len(p_extra);
1670 mb_utf8 = FALSE;
1671 if (mb_l > n_extra)
1672 mb_l = 1;
1673 else if (mb_l > 1)
1674 {
1675 mb_c = utfc_ptr2char(p_extra, u8cc);
1676 mb_utf8 = TRUE;
1677 c = 0xc0;
1678 }
1679 }
1680 else
1681 {
1682 // if this is a DBCS character, put it in "mb_c"
1683 mb_l = MB_BYTE2LEN(c);
1684 if (mb_l >= n_extra)
1685 mb_l = 1;
1686 else if (mb_l > 1)
1687 mb_c = (c << 8) + p_extra[1];
1688 }
1689 if (mb_l == 0) // at the NUL at end-of-line
1690 mb_l = 1;
1691
1692 // If a double-width char doesn't fit display a '>' in the
1693 // last column.
1694 if ((
1695# ifdef FEAT_RIGHTLEFT
1696 wp->w_p_rl ? (col <= 0) :
1697# endif
1698 (col >= wp->w_width - 1))
1699 && (*mb_char2cells)(mb_c) == 2)
1700 {
1701 c = '>';
1702 mb_c = c;
1703 mb_l = 1;
1704 mb_utf8 = FALSE;
1705 multi_attr = HL_ATTR(HLF_AT);
1706#ifdef FEAT_SYN_HL
1707 if (cul_attr)
1708 multi_attr = hl_combine_attr(multi_attr, cul_attr);
1709#endif
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001710 multi_attr = hl_combine_attr(win_attr, multi_attr);
1711
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001712 // put the pointer back to output the double-width
1713 // character at the start of the next line.
1714 ++n_extra;
1715 --p_extra;
1716 }
1717 else
1718 {
1719 n_extra -= mb_l - 1;
1720 p_extra += mb_l - 1;
1721 }
1722 }
1723 ++p_extra;
1724 }
1725 --n_extra;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00001726#if defined(FEAT_LINEBREAK) && defined(FEAT_PROP_POPUP)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001727 if (n_extra <= 0)
1728 in_linebreak = FALSE;
1729#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001730 }
1731 else
1732 {
1733#ifdef FEAT_LINEBREAK
1734 int c0;
1735#endif
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001736 VIM_CLEAR(p_extra_free);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001737
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001738 // Get a character from the line itself.
1739 c = *ptr;
1740#ifdef FEAT_LINEBREAK
1741 c0 = *ptr;
1742#endif
1743 if (has_mbyte)
1744 {
1745 mb_c = c;
1746 if (enc_utf8)
1747 {
1748 // If the UTF-8 character is more than one byte: Decode it
1749 // into "mb_c".
1750 mb_l = utfc_ptr2len(ptr);
1751 mb_utf8 = FALSE;
1752 if (mb_l > 1)
1753 {
1754 mb_c = utfc_ptr2char(ptr, u8cc);
1755 // Overlong encoded ASCII or ASCII with composing char
1756 // is displayed normally, except a NUL.
1757 if (mb_c < 0x80)
1758 {
1759 c = mb_c;
1760#ifdef FEAT_LINEBREAK
1761 c0 = mb_c;
1762#endif
1763 }
1764 mb_utf8 = TRUE;
1765
1766 // At start of the line we can have a composing char.
1767 // Draw it as a space with a composing char.
1768 if (utf_iscomposing(mb_c))
1769 {
1770 int i;
1771
1772 for (i = Screen_mco - 1; i > 0; --i)
1773 u8cc[i] = u8cc[i - 1];
1774 u8cc[0] = mb_c;
1775 mb_c = ' ';
1776 }
1777 }
1778
1779 if ((mb_l == 1 && c >= 0x80)
1780 || (mb_l >= 1 && mb_c == 0)
1781 || (mb_l > 1 && (!vim_isprintc(mb_c))))
1782 {
1783 // Illegal UTF-8 byte: display as <xx>.
1784 // Non-BMP character : display as ? or fullwidth ?.
1785 transchar_hex(extra, mb_c);
1786# ifdef FEAT_RIGHTLEFT
1787 if (wp->w_p_rl) // reverse
1788 rl_mirror(extra);
1789# endif
1790 p_extra = extra;
1791 c = *p_extra;
1792 mb_c = mb_ptr2char_adv(&p_extra);
1793 mb_utf8 = (c >= 0x80);
1794 n_extra = (int)STRLEN(p_extra);
1795 c_extra = NUL;
1796 c_final = NUL;
1797 if (area_attr == 0 && search_attr == 0)
1798 {
1799 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001800 extra_attr = hl_combine_attr(
1801 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001802 saved_attr2 = char_attr; // save current attr
1803 }
1804 }
1805 else if (mb_l == 0) // at the NUL at end-of-line
1806 mb_l = 1;
1807#ifdef FEAT_ARABIC
1808 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
1809 {
1810 // Do Arabic shaping.
1811 int pc, pc1, nc;
1812 int pcc[MAX_MCO];
1813
1814 // The idea of what is the previous and next
1815 // character depends on 'rightleft'.
1816 if (wp->w_p_rl)
1817 {
1818 pc = prev_c;
1819 pc1 = prev_c1;
1820 nc = utf_ptr2char(ptr + mb_l);
1821 prev_c1 = u8cc[0];
1822 }
1823 else
1824 {
1825 pc = utfc_ptr2char(ptr + mb_l, pcc);
1826 nc = prev_c;
1827 pc1 = pcc[0];
1828 }
1829 prev_c = mb_c;
1830
1831 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
1832 }
1833 else
1834 prev_c = mb_c;
1835#endif
1836 }
1837 else // enc_dbcs
1838 {
1839 mb_l = MB_BYTE2LEN(c);
1840 if (mb_l == 0) // at the NUL at end-of-line
1841 mb_l = 1;
1842 else if (mb_l > 1)
1843 {
1844 // We assume a second byte below 32 is illegal.
1845 // Hopefully this is OK for all double-byte encodings!
1846 if (ptr[1] >= 32)
1847 mb_c = (c << 8) + ptr[1];
1848 else
1849 {
1850 if (ptr[1] == NUL)
1851 {
1852 // head byte at end of line
1853 mb_l = 1;
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001854 transchar_nonprint(wp->w_buffer, extra, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001855 }
1856 else
1857 {
1858 // illegal tail byte
1859 mb_l = 2;
1860 STRCPY(extra, "XX");
1861 }
1862 p_extra = extra;
1863 n_extra = (int)STRLEN(extra) - 1;
1864 c_extra = NUL;
1865 c_final = NUL;
1866 c = *p_extra++;
1867 if (area_attr == 0 && search_attr == 0)
1868 {
1869 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001870 extra_attr = hl_combine_attr(
1871 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001872 saved_attr2 = char_attr; // save current attr
1873 }
1874 mb_c = c;
1875 }
1876 }
1877 }
1878 // If a double-width char doesn't fit display a '>' in the
1879 // last column; the character is displayed at the start of the
1880 // next line.
1881 if ((
1882# ifdef FEAT_RIGHTLEFT
1883 wp->w_p_rl ? (col <= 0) :
1884# endif
1885 (col >= wp->w_width - 1))
1886 && (*mb_char2cells)(mb_c) == 2)
1887 {
1888 c = '>';
1889 mb_c = c;
1890 mb_utf8 = FALSE;
1891 mb_l = 1;
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001892 multi_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001893 // Put pointer back so that the character will be
1894 // displayed at the start of the next line.
1895 --ptr;
1896#ifdef FEAT_CONCEAL
1897 did_decrement_ptr = TRUE;
1898#endif
1899 }
1900 else if (*ptr != NUL)
1901 ptr += mb_l - 1;
1902
1903 // If a double-width char doesn't fit at the left side display
1904 // a '<' in the first column. Don't do this for unprintable
1905 // characters.
1906 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
1907 {
1908 n_extra = 1;
1909 c_extra = MB_FILLER_CHAR;
1910 c_final = NUL;
1911 c = ' ';
1912 if (area_attr == 0 && search_attr == 0)
1913 {
1914 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001915 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001916 saved_attr2 = char_attr; // save current attr
1917 }
1918 mb_c = c;
1919 mb_utf8 = FALSE;
1920 mb_l = 1;
1921 }
1922
1923 }
1924 ++ptr;
1925
1926 if (extra_check)
1927 {
1928#ifdef FEAT_SPELL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001929 // Check spelling (unless at the end of the line).
1930 // Only do this when there is no syntax highlighting, the
1931 // @Spell cluster is not used or the current syntax item
1932 // contains the @Spell cluster.
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001933 v = (long)(ptr - line);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001934 if (has_spell && v >= word_end && v > cur_checked_col)
1935 {
1936 spell_attr = 0;
1937 if (c != 0 && (
1938# ifdef FEAT_SYN_HL
1939 !has_syntax ||
1940# endif
1941 can_spell))
1942 {
1943 char_u *prev_ptr, *p;
1944 int len;
1945 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001946
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001947 if (has_mbyte)
1948 {
1949 prev_ptr = ptr - mb_l;
1950 v -= mb_l - 1;
1951 }
1952 else
1953 prev_ptr = ptr - 1;
1954
1955 // Use nextline[] if possible, it has the start of the
1956 // next line concatenated.
1957 if ((prev_ptr - line) - nextlinecol >= 0)
1958 p = nextline + (prev_ptr - line) - nextlinecol;
1959 else
1960 p = prev_ptr;
1961 cap_col -= (int)(prev_ptr - line);
1962 len = spell_check(wp, p, &spell_hlf, &cap_col,
1963 nochange);
1964 word_end = v + len;
1965
1966 // In Insert mode only highlight a word that
1967 // doesn't touch the cursor.
1968 if (spell_hlf != HLF_COUNT
1969 && (State & INSERT) != 0
1970 && wp->w_cursor.lnum == lnum
1971 && wp->w_cursor.col >=
1972 (colnr_T)(prev_ptr - line)
1973 && wp->w_cursor.col < (colnr_T)word_end)
1974 {
1975 spell_hlf = HLF_COUNT;
1976 spell_redraw_lnum = lnum;
1977 }
1978
1979 if (spell_hlf == HLF_COUNT && p != prev_ptr
1980 && (p - nextline) + len > nextline_idx)
1981 {
1982 // Remember that the good word continues at the
1983 // start of the next line.
1984 checked_lnum = lnum + 1;
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001985 checked_col = (int)((p - nextline)
1986 + len - nextline_idx);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001987 }
1988
1989 // Turn index into actual attributes.
1990 if (spell_hlf != HLF_COUNT)
1991 spell_attr = highlight_attr[spell_hlf];
1992
1993 if (cap_col > 0)
1994 {
1995 if (p != prev_ptr
1996 && (p - nextline) + cap_col >= nextline_idx)
1997 {
1998 // Remember that the word in the next line
1999 // must start with a capital.
2000 capcol_lnum = lnum + 1;
2001 cap_col = (int)((p - nextline) + cap_col
2002 - nextline_idx);
2003 }
2004 else
2005 // Compute the actual column.
2006 cap_col += (int)(prev_ptr - line);
2007 }
2008 }
2009 }
2010 if (spell_attr != 0)
2011 {
2012 if (!attr_pri)
2013 char_attr = hl_combine_attr(char_attr, spell_attr);
2014 else
2015 char_attr = hl_combine_attr(spell_attr, char_attr);
2016 }
2017#endif
2018#ifdef FEAT_LINEBREAK
2019 // Found last space before word: check for line break.
2020 if (wp->w_p_lbr && c0 == c
2021 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
2022 {
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002023 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1)
2024 : 0;
2025 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002026
2027 // TODO: is passing p for start of the line OK?
2028 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
2029 NULL) - 1;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002030
2031 // We have just drawn the showbreak value, no need to add
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002032 // space for it again.
Bram Moolenaara06e3452021-05-29 17:56:37 +02002033 if (vcol == vcol_sbr)
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002034 {
Bram Moolenaara06e3452021-05-29 17:56:37 +02002035 n_extra -= MB_CHARLEN(get_showbreak_value(wp));
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002036 if (n_extra < 0)
2037 n_extra = 0;
2038 }
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002039 if (on_last_col)
2040 // Do not continue search/match highlighting over the
2041 // line break.
2042 search_attr = 0;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002043
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002044 if (c == TAB && n_extra + col > wp->w_width)
2045# ifdef FEAT_VARTABS
2046 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
2047 wp->w_buffer->b_p_vts_array) - 1;
2048# else
2049 n_extra = (int)wp->w_buffer->b_p_ts
2050 - vcol % (int)wp->w_buffer->b_p_ts - 1;
2051# endif
2052
2053 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
2054 c_final = NUL;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002055# if defined(FEAT_PROP_POPUP)
Bram Moolenaar42eba042021-11-30 20:22:49 +00002056 if (n_extra > 0 && c != TAB)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00002057 in_linebreak = TRUE;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002058# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002059 if (VIM_ISWHITE(c))
2060 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002061# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002062 if (c == TAB)
2063 // See "Tab alignment" below.
2064 FIX_FOR_BOGUSCOLS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002065# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002066 if (!wp->w_p_list)
2067 c = ' ';
2068 }
2069 }
2070#endif
2071
zeertzjqf14b8ba2021-09-10 16:58:30 +02002072 in_multispace = c == ' '
2073 && ((ptr > line + 1 && ptr[-2] == ' ') || *ptr == ' ');
2074 if (!in_multispace)
2075 multispace_pos = 0;
2076
Bram Moolenaareed9d462021-02-15 20:38:25 +01002077 // 'list': Change char 160 to 'nbsp' and space to 'space'
2078 // setting in 'listchars'. But not when the character is
2079 // followed by a composing character (use mb_l to check that).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002080 if (wp->w_p_list
2081 && ((((c == 160 && mb_l == 1)
2082 || (mb_utf8
2083 && ((mb_c == 160 && mb_l == 2)
2084 || (mb_c == 0x202f && mb_l == 3))))
Bram Moolenaareed9d462021-02-15 20:38:25 +01002085 && wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002086 || (c == ' '
2087 && mb_l == 1
zeertzjqf14b8ba2021-09-10 16:58:30 +02002088 && (wp->w_lcs_chars.space
2089 || (in_multispace
2090 && wp->w_lcs_chars.multispace != NULL))
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002091 && ptr - line >= leadcol
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002092 && ptr - line <= trailcol)))
2093 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02002094 if (in_multispace && wp->w_lcs_chars.multispace != NULL)
2095 {
2096 c = wp->w_lcs_chars.multispace[multispace_pos++];
2097 if (wp->w_lcs_chars.multispace[multispace_pos] == NUL)
2098 multispace_pos = 0;
2099 }
2100 else
2101 c = (c == ' ') ? wp->w_lcs_chars.space
2102 : wp->w_lcs_chars.nbsp;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002103 if (area_attr == 0 && search_attr == 0)
2104 {
2105 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002106 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002107 saved_attr2 = char_attr; // save current attr
2108 }
2109 mb_c = c;
2110 if (enc_utf8 && utf_char2len(c) > 1)
2111 {
2112 mb_utf8 = TRUE;
2113 u8cc[0] = 0;
2114 c = 0xc0;
2115 }
2116 else
2117 mb_utf8 = FALSE;
2118 }
2119
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002120 if ((trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
2121 || (leadcol != 0 && ptr < line + leadcol && c == ' '))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002122 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002123 c = (ptr > line + trailcol) ? wp->w_lcs_chars.trail
2124 : wp->w_lcs_chars.lead;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002125 if (!attr_pri)
2126 {
2127 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002128 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002129 saved_attr2 = char_attr; // save current attr
2130 }
2131 mb_c = c;
2132 if (enc_utf8 && utf_char2len(c) > 1)
2133 {
2134 mb_utf8 = TRUE;
2135 u8cc[0] = 0;
2136 c = 0xc0;
2137 }
2138 else
2139 mb_utf8 = FALSE;
2140 }
2141 }
2142
2143 // Handling of non-printable characters.
2144 if (!vim_isprintc(c))
2145 {
2146 // when getting a character from the file, we may have to
2147 // turn it into something else on the way to putting it
2148 // into "ScreenLines".
Bram Moolenaareed9d462021-02-15 20:38:25 +01002149 if (c == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002150 {
2151 int tab_len = 0;
2152 long vcol_adjusted = vcol; // removed showbreak length
2153#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01002154 char_u *sbr = get_showbreak_value(wp);
2155
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002156 // only adjust the tab_len, when at the first column
2157 // after the showbreak value was drawn
Bram Moolenaaree857022019-11-09 23:26:40 +01002158 if (*sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
2159 vcol_adjusted = vcol - MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002160#endif
2161 // tab amount depends on current column
2162#ifdef FEAT_VARTABS
2163 tab_len = tabstop_padding(vcol_adjusted,
2164 wp->w_buffer->b_p_ts,
2165 wp->w_buffer->b_p_vts_array) - 1;
2166#else
2167 tab_len = (int)wp->w_buffer->b_p_ts
2168 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
2169#endif
2170
2171#ifdef FEAT_LINEBREAK
2172 if (!wp->w_p_lbr || !wp->w_p_list)
2173#endif
2174 // tab amount depends on current column
2175 n_extra = tab_len;
2176#ifdef FEAT_LINEBREAK
2177 else
2178 {
2179 char_u *p;
2180 int len;
2181 int i;
2182 int saved_nextra = n_extra;
2183
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002184# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002185 if (vcol_off > 0)
2186 // there are characters to conceal
2187 tab_len += vcol_off;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002188
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002189 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaareed9d462021-02-15 20:38:25 +01002190 if (wp->w_p_list && wp->w_lcs_chars.tab1
2191 && old_boguscols > 0
2192 && n_extra > tab_len)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002193 tab_len += n_extra - tab_len;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002194# endif
2195 // If n_extra > 0, it gives the number of chars, to
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002196 // use for a tab, else we need to calculate the width
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002197 // for a tab.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002198 len = (tab_len * mb_char2len(wp->w_lcs_chars.tab2));
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002199 if (wp->w_lcs_chars.tab3)
2200 len += mb_char2len(wp->w_lcs_chars.tab3);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002201 if (n_extra > 0)
2202 len += n_extra - tab_len;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002203 c = wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002204 p = alloc(len + 1);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002205 if (p == NULL)
2206 n_extra = 0;
2207 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002208 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002209 vim_memset(p, ' ', len);
2210 p[len] = NUL;
2211 vim_free(p_extra_free);
2212 p_extra_free = p;
2213 for (i = 0; i < tab_len; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002214 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002215 int lcs = wp->w_lcs_chars.tab2;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002216
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002217 if (*p == NUL)
2218 {
2219 tab_len = i;
2220 break;
2221 }
2222
2223 // if tab3 is given, use it for the last char
2224 if (wp->w_lcs_chars.tab3 && i == tab_len - 1)
2225 lcs = wp->w_lcs_chars.tab3;
2226 p += mb_char2bytes(lcs, p);
2227 n_extra += mb_char2len(lcs)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002228 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002229 }
2230 p_extra = p_extra_free;
2231# ifdef FEAT_CONCEAL
2232 // n_extra will be increased by FIX_FOX_BOGUSCOLS
2233 // macro below, so need to adjust for that here
2234 if (vcol_off > 0)
2235 n_extra -= vcol_off;
2236# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002237 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002238 }
2239#endif
2240#ifdef FEAT_CONCEAL
2241 {
2242 int vc_saved = vcol_off;
2243
2244 // Tab alignment should be identical regardless of
2245 // 'conceallevel' value. So tab compensates of all
2246 // previous concealed characters, and thus resets
2247 // vcol_off and boguscols accumulated so far in the
2248 // line. Note that the tab can be longer than
2249 // 'tabstop' when there are concealed characters.
2250 FIX_FOR_BOGUSCOLS;
2251
2252 // Make sure, the highlighting for the tab char will be
2253 // correctly set further below (effectively reverts the
2254 // FIX_FOR_BOGSUCOLS macro
2255 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaareed9d462021-02-15 20:38:25 +01002256 && wp->w_lcs_chars.tab1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002257 tab_len += vc_saved;
2258 }
2259#endif
2260 mb_utf8 = FALSE; // don't draw as UTF-8
2261 if (wp->w_p_list)
2262 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002263 c = (n_extra == 0 && wp->w_lcs_chars.tab3)
2264 ? wp->w_lcs_chars.tab3
2265 : wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002266#ifdef FEAT_LINEBREAK
2267 if (wp->w_p_lbr)
2268 c_extra = NUL; // using p_extra from above
2269 else
2270#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01002271 c_extra = wp->w_lcs_chars.tab2;
2272 c_final = wp->w_lcs_chars.tab3;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002273 n_attr = tab_len + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002274 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002275 saved_attr2 = char_attr; // save current attr
2276 mb_c = c;
2277 if (enc_utf8 && utf_char2len(c) > 1)
2278 {
2279 mb_utf8 = TRUE;
2280 u8cc[0] = 0;
2281 c = 0xc0;
2282 }
2283 }
2284 else
2285 {
2286 c_final = NUL;
2287 c_extra = ' ';
2288 c = ' ';
2289 }
2290 }
2291 else if (c == NUL
2292 && (wp->w_p_list
2293 || ((fromcol >= 0 || fromcol_prev >= 0)
2294 && tocol > vcol
2295 && VIsual_mode != Ctrl_V
2296 && (
2297# ifdef FEAT_RIGHTLEFT
2298 wp->w_p_rl ? (col >= 0) :
2299# endif
2300 (col < wp->w_width))
2301 && !(noinvcur
2302 && lnum == wp->w_cursor.lnum
2303 && (colnr_T)vcol == wp->w_virtcol)))
2304 && lcs_eol_one > 0)
2305 {
2306 // Display a '$' after the line or highlight an extra
2307 // character if the line break is included.
2308#if defined(FEAT_DIFF) || defined(LINE_ATTR)
2309 // For a diff line the highlighting continues after the
2310 // "$".
2311 if (
2312# ifdef FEAT_DIFF
2313 diff_hlf == (hlf_T)0
2314# ifdef LINE_ATTR
2315 &&
2316# endif
2317# endif
2318# ifdef LINE_ATTR
2319 line_attr == 0
2320# endif
2321 )
2322#endif
2323 {
2324 // In virtualedit, visual selections may extend
2325 // beyond end of line.
2326 if (area_highlighting && virtual_active()
2327 && tocol != MAXCOL && vcol < tocol)
2328 n_extra = 0;
2329 else
2330 {
2331 p_extra = at_end_str;
2332 n_extra = 1;
2333 c_extra = NUL;
2334 c_final = NUL;
2335 }
2336 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002337 if (wp->w_p_list && wp->w_lcs_chars.eol > 0)
2338 c = wp->w_lcs_chars.eol;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002339 else
2340 c = ' ';
2341 lcs_eol_one = -1;
2342 --ptr; // put it back at the NUL
2343 if (!attr_pri)
2344 {
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002345 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002346 n_attr = 1;
2347 }
2348 mb_c = c;
2349 if (enc_utf8 && utf_char2len(c) > 1)
2350 {
2351 mb_utf8 = TRUE;
2352 u8cc[0] = 0;
2353 c = 0xc0;
2354 }
2355 else
2356 mb_utf8 = FALSE; // don't draw as UTF-8
2357 }
2358 else if (c != NUL)
2359 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02002360 p_extra = transchar_buf(wp->w_buffer, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002361 if (n_extra == 0)
2362 n_extra = byte2cells(c) - 1;
2363#ifdef FEAT_RIGHTLEFT
2364 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
2365 rl_mirror(p_extra); // reverse "<12>"
2366#endif
2367 c_extra = NUL;
2368 c_final = NUL;
2369#ifdef FEAT_LINEBREAK
2370 if (wp->w_p_lbr)
2371 {
2372 char_u *p;
2373
2374 c = *p_extra;
2375 p = alloc(n_extra + 1);
2376 vim_memset(p, ' ', n_extra);
2377 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
2378 p[n_extra] = NUL;
2379 vim_free(p_extra_free);
2380 p_extra_free = p_extra = p;
2381 }
2382 else
2383#endif
2384 {
2385 n_extra = byte2cells(c) - 1;
2386 c = *p_extra++;
2387 }
2388 if (!attr_pri)
2389 {
2390 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002391 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002392 saved_attr2 = char_attr; // save current attr
2393 }
2394 mb_utf8 = FALSE; // don't draw as UTF-8
2395 }
2396 else if (VIsual_active
2397 && (VIsual_mode == Ctrl_V
2398 || VIsual_mode == 'v')
2399 && virtual_active()
2400 && tocol != MAXCOL
2401 && vcol < tocol
2402 && (
2403#ifdef FEAT_RIGHTLEFT
2404 wp->w_p_rl ? (col >= 0) :
2405#endif
2406 (col < wp->w_width)))
2407 {
2408 c = ' ';
2409 --ptr; // put it back at the NUL
2410 }
2411#if defined(LINE_ATTR)
2412 else if ((
2413# ifdef FEAT_DIFF
2414 diff_hlf != (hlf_T)0 ||
2415# endif
2416# ifdef FEAT_TERMINAL
2417 win_attr != 0 ||
2418# endif
2419 line_attr != 0
2420 ) && (
2421# ifdef FEAT_RIGHTLEFT
2422 wp->w_p_rl ? (col >= 0) :
2423# endif
2424 (col
2425# ifdef FEAT_CONCEAL
2426 - boguscols
2427# endif
2428 < wp->w_width)))
2429 {
2430 // Highlight until the right side of the window
2431 c = ' ';
2432 --ptr; // put it back at the NUL
2433
2434 // Remember we do the char for line highlighting.
2435 ++did_line_attr;
2436
2437 // don't do search HL for the rest of the line
2438 if (line_attr != 0 && char_attr == search_attr
2439 && (did_line_attr > 1
Bram Moolenaareed9d462021-02-15 20:38:25 +01002440 || (wp->w_p_list &&
2441 wp->w_lcs_chars.eol > 0)))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002442 char_attr = line_attr;
2443# ifdef FEAT_DIFF
2444 if (diff_hlf == HLF_TXD)
2445 {
2446 diff_hlf = HLF_CHD;
2447 if (vi_attr == 0 || char_attr != vi_attr)
2448 {
2449 char_attr = HL_ATTR(diff_hlf);
2450 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2451 && wp->w_p_culopt_flags != CULOPT_NBR
2452 && (!cul_screenline
2453 || (vcol >= left_curline_col
2454 && vcol <= right_curline_col)))
2455 char_attr = hl_combine_attr(
2456 char_attr, HL_ATTR(HLF_CUL));
2457 }
2458 }
2459# endif
2460# ifdef FEAT_TERMINAL
2461 if (win_attr != 0)
2462 {
2463 char_attr = win_attr;
Bram Moolenaara3817732019-10-16 16:31:44 +02002464 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2465 && wp->w_p_culopt_flags != CULOPT_NBR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002466 {
2467 if (!cul_screenline || (vcol >= left_curline_col
2468 && vcol <= right_curline_col))
2469 char_attr = hl_combine_attr(
2470 char_attr, HL_ATTR(HLF_CUL));
2471 }
2472 else if (line_attr)
2473 char_attr = hl_combine_attr(char_attr, line_attr);
2474 }
2475# endif
2476 }
2477#endif
2478 }
2479
2480#ifdef FEAT_CONCEAL
2481 if ( wp->w_p_cole > 0
2482 && (wp != curwin || lnum != wp->w_cursor.lnum ||
2483 conceal_cursor_line(wp))
2484 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
2485 && !(lnum_in_visual_area
2486 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
2487 {
2488 char_attr = conceal_attr;
2489 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002490 && (syn_get_sub_char() != NUL
2491 || (has_match_conc && match_conc)
2492 || wp->w_p_cole == 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002493 && wp->w_p_cole != 3)
2494 {
2495 // First time at this concealed item: display one
2496 // character.
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002497 if (has_match_conc && match_conc)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002498 c = match_conc;
2499 else if (syn_get_sub_char() != NUL)
2500 c = syn_get_sub_char();
Bram Moolenaareed9d462021-02-15 20:38:25 +01002501 else if (wp->w_lcs_chars.conceal != NUL)
2502 c = wp->w_lcs_chars.conceal;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002503 else
2504 c = ' ';
2505
2506 prev_syntax_id = syntax_seqnr;
2507
2508 if (n_extra > 0)
2509 vcol_off += n_extra;
2510 vcol += n_extra;
2511 if (wp->w_p_wrap && n_extra > 0)
2512 {
2513# ifdef FEAT_RIGHTLEFT
2514 if (wp->w_p_rl)
2515 {
2516 col -= n_extra;
2517 boguscols -= n_extra;
2518 }
2519 else
2520# endif
2521 {
2522 boguscols += n_extra;
2523 col += n_extra;
2524 }
2525 }
2526 n_extra = 0;
2527 n_attr = 0;
2528 }
2529 else if (n_skip == 0)
2530 {
2531 is_concealing = TRUE;
2532 n_skip = 1;
2533 }
2534 mb_c = c;
2535 if (enc_utf8 && utf_char2len(c) > 1)
2536 {
2537 mb_utf8 = TRUE;
2538 u8cc[0] = 0;
2539 c = 0xc0;
2540 }
2541 else
2542 mb_utf8 = FALSE; // don't draw as UTF-8
2543 }
2544 else
2545 {
2546 prev_syntax_id = 0;
2547 is_concealing = FALSE;
2548 }
2549
2550 if (n_skip > 0 && did_decrement_ptr)
2551 // not showing the '>', put pointer back to avoid getting stuck
2552 ++ptr;
2553
2554#endif // FEAT_CONCEAL
2555 }
2556
2557#ifdef FEAT_CONCEAL
2558 // In the cursor line and we may be concealing characters: correct
2559 // the cursor column when we reach its position.
2560 if (!did_wcol && draw_state == WL_LINE
2561 && wp == curwin && lnum == wp->w_cursor.lnum
2562 && conceal_cursor_line(wp)
2563 && (int)wp->w_virtcol <= vcol + n_skip)
2564 {
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002565# ifdef FEAT_RIGHTLEFT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002566 if (wp->w_p_rl)
2567 wp->w_wcol = wp->w_width - col + boguscols - 1;
2568 else
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002569# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002570 wp->w_wcol = col - boguscols;
2571 wp->w_wrow = row;
2572 did_wcol = TRUE;
2573 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002574# ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +01002575 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002576# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002577 }
2578#endif
2579
2580 // Don't override visual selection highlighting.
2581 if (n_attr > 0
2582 && draw_state == WL_LINE
2583 && !attr_pri)
2584 {
2585#ifdef LINE_ATTR
2586 if (line_attr)
2587 char_attr = hl_combine_attr(extra_attr, line_attr);
2588 else
2589#endif
2590 char_attr = extra_attr;
2591 }
2592
2593#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2594 // XIM don't send preedit_start and preedit_end, but they send
2595 // preedit_changed and commit. Thus Vim can't set "im_is_active", use
2596 // im_is_preediting() here.
2597 if (p_imst == IM_ON_THE_SPOT
2598 && xic != NULL
2599 && lnum == wp->w_cursor.lnum
2600 && (State & INSERT)
2601 && !p_imdisable
2602 && im_is_preediting()
2603 && draw_state == WL_LINE)
2604 {
2605 colnr_T tcol;
2606
2607 if (preedit_end_col == MAXCOL)
2608 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
2609 else
2610 tcol = preedit_end_col;
2611 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
2612 {
2613 if (feedback_old_attr < 0)
2614 {
2615 feedback_col = 0;
2616 feedback_old_attr = char_attr;
2617 }
2618 char_attr = im_get_feedback_attr(feedback_col);
2619 if (char_attr < 0)
2620 char_attr = feedback_old_attr;
2621 feedback_col++;
2622 }
2623 else if (feedback_old_attr >= 0)
2624 {
2625 char_attr = feedback_old_attr;
2626 feedback_old_attr = -1;
2627 feedback_col = 0;
2628 }
2629 }
2630#endif
2631 // Handle the case where we are in column 0 but not on the first
2632 // character of the line and the user wants us to show us a
2633 // special character (via 'listchars' option "precedes:<char>".
2634 if (lcs_prec_todo != NUL
2635 && wp->w_p_list
Bram Moolenaarbffba7f2019-09-20 17:00:17 +02002636 && (wp->w_p_wrap ?
2637 (wp->w_skipcol > 0 && row == 0) :
2638 wp->w_leftcol > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002639#ifdef FEAT_DIFF
2640 && filler_todo <= 0
2641#endif
2642 && draw_state > WL_NR
2643 && c != NUL)
2644 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002645 c = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002646 lcs_prec_todo = NUL;
2647 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2648 {
2649 // Double-width character being overwritten by the "precedes"
2650 // character, need to fill up half the character.
2651 c_extra = MB_FILLER_CHAR;
2652 c_final = NUL;
2653 n_extra = 1;
2654 n_attr = 2;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002655 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002656 }
2657 mb_c = c;
2658 if (enc_utf8 && utf_char2len(c) > 1)
2659 {
2660 mb_utf8 = TRUE;
2661 u8cc[0] = 0;
2662 c = 0xc0;
2663 }
2664 else
2665 mb_utf8 = FALSE; // don't draw as UTF-8
2666 if (!attr_pri)
2667 {
2668 saved_attr3 = char_attr; // save current attr
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002669 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002670 n_attr3 = 1;
2671 }
2672 }
2673
2674 // At end of the text line or just after the last character.
2675 if ((c == NUL
2676#if defined(LINE_ATTR)
2677 || did_line_attr == 1
2678#endif
2679 ) && eol_hl_off == 0)
2680 {
2681#ifdef FEAT_SEARCH_EXTRA
2682 // flag to indicate whether prevcol equals startcol of search_hl or
2683 // one of the matches
2684 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &screen_search_hl,
2685 (long)(ptr - line) - (c == NUL));
2686#endif
2687 // Invert at least one char, used for Visual and empty line or
2688 // highlight match at end of line. If it's beyond the last
2689 // char on the screen, just overwrite that one (tricky!) Not
2690 // needed when a '$' was displayed for 'list'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002691 if (wp->w_lcs_chars.eol == lcs_eol_one
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002692 && ((area_attr != 0 && vcol == fromcol
2693 && (VIsual_mode != Ctrl_V
2694 || lnum == VIsual.lnum
2695 || lnum == curwin->w_cursor.lnum)
2696 && c == NUL)
2697#ifdef FEAT_SEARCH_EXTRA
2698 // highlight 'hlsearch' match at end of line
2699 || (prevcol_hl_flag
2700# ifdef FEAT_SYN_HL
2701 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
2702 && !(wp == curwin && VIsual_active))
2703# endif
2704# ifdef FEAT_DIFF
2705 && diff_hlf == (hlf_T)0
2706# endif
2707# if defined(LINE_ATTR)
2708 && did_line_attr <= 1
2709# endif
2710 )
2711#endif
2712 ))
2713 {
2714 int n = 0;
2715
2716#ifdef FEAT_RIGHTLEFT
2717 if (wp->w_p_rl)
2718 {
2719 if (col < 0)
2720 n = 1;
2721 }
2722 else
2723#endif
2724 {
2725 if (col >= wp->w_width)
2726 n = -1;
2727 }
2728 if (n != 0)
2729 {
2730 // At the window boundary, highlight the last character
2731 // instead (better than nothing).
2732 off += n;
2733 col += n;
2734 }
2735 else
2736 {
2737 // Add a blank character to highlight.
2738 ScreenLines[off] = ' ';
2739 if (enc_utf8)
2740 ScreenLinesUC[off] = 0;
2741 }
2742#ifdef FEAT_SEARCH_EXTRA
2743 if (area_attr == 0)
2744 {
2745 // Use attributes from match with highest priority among
2746 // 'search_hl' and the match list.
2747 get_search_match_hl(wp, &screen_search_hl,
2748 (long)(ptr - line), &char_attr);
2749 }
2750#endif
2751 ScreenAttrs[off] = char_attr;
2752#ifdef FEAT_RIGHTLEFT
2753 if (wp->w_p_rl)
2754 {
2755 --col;
2756 --off;
2757 }
2758 else
2759#endif
2760 {
2761 ++col;
2762 ++off;
2763 }
2764 ++vcol;
2765 eol_hl_off = 1;
2766 }
2767 }
2768
2769 // At end of the text line.
2770 if (c == NUL)
2771 {
2772#ifdef FEAT_SYN_HL
2773 // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
2774 if (wp->w_p_wrap)
2775 v = wp->w_skipcol;
2776 else
2777 v = wp->w_leftcol;
2778
2779 // check if line ends before left margin
2780 if (vcol < v + col - win_col_off(wp))
2781 vcol = v + col - win_col_off(wp);
2782#ifdef FEAT_CONCEAL
2783 // Get rid of the boguscols now, we want to draw until the right
2784 // edge for 'cursorcolumn'.
2785 col -= boguscols;
2786 boguscols = 0;
2787#endif
2788
2789 if (draw_color_col)
2790 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2791
2792 if (((wp->w_p_cuc
2793 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
2794 && (int)wp->w_virtcol <
2795 wp->w_width * (row - startrow + 1) + v
2796 && lnum != wp->w_cursor.lnum)
2797 || draw_color_col
2798 || win_attr != 0)
2799# ifdef FEAT_RIGHTLEFT
2800 && !wp->w_p_rl
2801# endif
2802 )
2803 {
2804 int rightmost_vcol = 0;
2805 int i;
2806
2807 if (wp->w_p_cuc)
2808 rightmost_vcol = wp->w_virtcol;
2809 if (draw_color_col)
2810 // determine rightmost colorcolumn to possibly draw
2811 for (i = 0; color_cols[i] >= 0; ++i)
2812 if (rightmost_vcol < color_cols[i])
2813 rightmost_vcol = color_cols[i];
2814
2815 while (col < wp->w_width)
2816 {
2817 ScreenLines[off] = ' ';
2818 if (enc_utf8)
2819 ScreenLinesUC[off] = 0;
2820 ++col;
2821 if (draw_color_col)
2822 draw_color_col = advance_color_col(VCOL_HLC,
2823 &color_cols);
2824
2825 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
2826 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
2827 else if (draw_color_col && VCOL_HLC == *color_cols)
2828 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
2829 else
2830 ScreenAttrs[off++] = win_attr;
2831
2832 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
2833 break;
2834
2835 ++vcol;
2836 }
2837 }
2838#endif
2839
2840 screen_line(screen_row, wp->w_wincol, col,
2841 (int)wp->w_width, screen_line_flags);
2842 row++;
2843
2844 // Update w_cline_height and w_cline_folded if the cursor line was
2845 // updated (saves a call to plines() later).
2846 if (wp == curwin && lnum == curwin->w_cursor.lnum)
2847 {
2848 curwin->w_cline_row = startrow;
2849 curwin->w_cline_height = row - startrow;
2850#ifdef FEAT_FOLDING
2851 curwin->w_cline_folded = FALSE;
2852#endif
2853 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2854 }
2855
2856 break;
2857 }
2858
2859 // Show "extends" character from 'listchars' if beyond the line end and
2860 // 'list' is set.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002861 if (wp->w_lcs_chars.ext != NUL
Bram Moolenaar41fb7232021-07-08 12:40:05 +02002862 && draw_state == WL_LINE
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002863 && wp->w_p_list
2864 && !wp->w_p_wrap
2865#ifdef FEAT_DIFF
2866 && filler_todo <= 0
2867#endif
2868 && (
2869#ifdef FEAT_RIGHTLEFT
2870 wp->w_p_rl ? col == 0 :
2871#endif
2872 col == wp->w_width - 1)
2873 && (*ptr != NUL
2874 || (wp->w_p_list && lcs_eol_one > 0)
2875 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
2876 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002877 c = wp->w_lcs_chars.ext;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002878 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002879 mb_c = c;
2880 if (enc_utf8 && utf_char2len(c) > 1)
2881 {
2882 mb_utf8 = TRUE;
2883 u8cc[0] = 0;
2884 c = 0xc0;
2885 }
2886 else
2887 mb_utf8 = FALSE;
2888 }
2889
2890#ifdef FEAT_SYN_HL
2891 // advance to the next 'colorcolumn'
2892 if (draw_color_col)
2893 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2894
2895 // Highlight the cursor column if 'cursorcolumn' is set. But don't
2896 // highlight the cursor position itself.
2897 // Also highlight the 'colorcolumn' if it is different than
2898 // 'cursorcolumn'
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002899 // Also highlight the 'colorcolumn' if 'breakindent' and/or 'showbreak'
2900 // options are set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002901 vcol_save_attr = -1;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002902 if (((draw_state == WL_LINE ||
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002903 draw_state == WL_BRI ||
2904 draw_state == WL_SBR) && !lnum_in_visual_area
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002905 && search_attr == 0 && area_attr == 0)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002906# ifdef FEAT_DIFF
2907 && filler_todo <= 0
2908# endif
2909 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002910 {
2911 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
2912 && lnum != wp->w_cursor.lnum)
2913 {
2914 vcol_save_attr = char_attr;
2915 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
2916 }
2917 else if (draw_color_col && VCOL_HLC == *color_cols)
2918 {
2919 vcol_save_attr = char_attr;
2920 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
2921 }
2922 }
2923#endif
2924
2925 // Store character to be displayed.
2926 // Skip characters that are left of the screen for 'nowrap'.
2927 vcol_prev = vcol;
2928 if (draw_state < WL_LINE || n_skip <= 0)
2929 {
2930 // Store the character.
2931#if defined(FEAT_RIGHTLEFT)
2932 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
2933 {
2934 // A double-wide character is: put first halve in left cell.
2935 --off;
2936 --col;
2937 }
2938#endif
2939 ScreenLines[off] = c;
2940 if (enc_dbcs == DBCS_JPNU)
2941 {
2942 if ((mb_c & 0xff00) == 0x8e00)
2943 ScreenLines[off] = 0x8e;
2944 ScreenLines2[off] = mb_c & 0xff;
2945 }
2946 else if (enc_utf8)
2947 {
2948 if (mb_utf8)
2949 {
2950 int i;
2951
2952 ScreenLinesUC[off] = mb_c;
2953 if ((c & 0xff) == 0)
2954 ScreenLines[off] = 0x80; // avoid storing zero
2955 for (i = 0; i < Screen_mco; ++i)
2956 {
2957 ScreenLinesC[i][off] = u8cc[i];
2958 if (u8cc[i] == 0)
2959 break;
2960 }
2961 }
2962 else
2963 ScreenLinesUC[off] = 0;
2964 }
2965 if (multi_attr)
2966 {
2967 ScreenAttrs[off] = multi_attr;
2968 multi_attr = 0;
2969 }
2970 else
2971 ScreenAttrs[off] = char_attr;
2972
2973 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2974 {
2975 // Need to fill two screen columns.
2976 ++off;
2977 ++col;
2978 if (enc_utf8)
2979 // UTF-8: Put a 0 in the second screen char.
2980 ScreenLines[off] = 0;
2981 else
2982 // DBCS: Put second byte in the second screen char.
2983 ScreenLines[off] = mb_c & 0xff;
2984 if (draw_state > WL_NR
2985#ifdef FEAT_DIFF
2986 && filler_todo <= 0
2987#endif
2988 )
2989 ++vcol;
2990 // When "tocol" is halfway a character, set it to the end of
2991 // the character, otherwise highlighting won't stop.
2992 if (tocol == vcol)
2993 ++tocol;
2994#ifdef FEAT_RIGHTLEFT
2995 if (wp->w_p_rl)
2996 {
2997 // now it's time to backup one cell
2998 --off;
2999 --col;
3000 }
3001#endif
3002 }
3003#ifdef FEAT_RIGHTLEFT
3004 if (wp->w_p_rl)
3005 {
3006 --off;
3007 --col;
3008 }
3009 else
3010#endif
3011 {
3012 ++off;
3013 ++col;
3014 }
3015 }
3016#ifdef FEAT_CONCEAL
3017 else if (wp->w_p_cole > 0 && is_concealing)
3018 {
3019 --n_skip;
3020 ++vcol_off;
3021 if (n_extra > 0)
3022 vcol_off += n_extra;
3023 if (wp->w_p_wrap)
3024 {
3025 // Special voodoo required if 'wrap' is on.
3026 //
3027 // Advance the column indicator to force the line
3028 // drawing to wrap early. This will make the line
3029 // take up the same screen space when parts are concealed,
3030 // so that cursor line computations aren't messed up.
3031 //
3032 // To avoid the fictitious advance of 'col' causing
3033 // trailing junk to be written out of the screen line
3034 // we are building, 'boguscols' keeps track of the number
3035 // of bad columns we have advanced.
3036 if (n_extra > 0)
3037 {
3038 vcol += n_extra;
3039# ifdef FEAT_RIGHTLEFT
3040 if (wp->w_p_rl)
3041 {
3042 col -= n_extra;
3043 boguscols -= n_extra;
3044 }
3045 else
3046# endif
3047 {
3048 col += n_extra;
3049 boguscols += n_extra;
3050 }
3051 n_extra = 0;
3052 n_attr = 0;
3053 }
3054
3055
3056 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
3057 {
3058 // Need to fill two screen columns.
3059# ifdef FEAT_RIGHTLEFT
3060 if (wp->w_p_rl)
3061 {
3062 --boguscols;
3063 --col;
3064 }
3065 else
3066# endif
3067 {
3068 ++boguscols;
3069 ++col;
3070 }
3071 }
3072
3073# ifdef FEAT_RIGHTLEFT
3074 if (wp->w_p_rl)
3075 {
3076 --boguscols;
3077 --col;
3078 }
3079 else
3080# endif
3081 {
3082 ++boguscols;
3083 ++col;
3084 }
3085 }
3086 else
3087 {
3088 if (n_extra > 0)
3089 {
3090 vcol += n_extra;
3091 n_extra = 0;
3092 n_attr = 0;
3093 }
3094 }
3095
3096 }
3097#endif // FEAT_CONCEAL
3098 else
3099 --n_skip;
3100
3101 // Only advance the "vcol" when after the 'number' or 'relativenumber'
3102 // column.
3103 if (draw_state > WL_NR
3104#ifdef FEAT_DIFF
3105 && filler_todo <= 0
3106#endif
3107 )
3108 ++vcol;
3109
3110#ifdef FEAT_SYN_HL
3111 if (vcol_save_attr >= 0)
3112 char_attr = vcol_save_attr;
3113#endif
3114
3115 // restore attributes after "predeces" in 'listchars'
3116 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
3117 char_attr = saved_attr3;
3118
3119 // restore attributes after last 'listchars' or 'number' char
3120 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
3121 char_attr = saved_attr2;
3122
3123 // At end of screen line and there is more to come: Display the line
3124 // so far. If there is no more to display it is caught above.
3125 if ((
3126#ifdef FEAT_RIGHTLEFT
3127 wp->w_p_rl ? (col < 0) :
3128#endif
3129 (col >= wp->w_width))
Bram Moolenaar41fb7232021-07-08 12:40:05 +02003130 && (draw_state != WL_LINE
3131 || *ptr != NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003132#ifdef FEAT_DIFF
3133 || filler_todo > 0
3134#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01003135 || (wp->w_p_list && wp->w_lcs_chars.eol != NUL
3136 && p_extra != at_end_str)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003137 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
3138 )
3139 {
3140#ifdef FEAT_CONCEAL
3141 screen_line(screen_row, wp->w_wincol, col - boguscols,
3142 (int)wp->w_width, screen_line_flags);
3143 boguscols = 0;
3144#else
3145 screen_line(screen_row, wp->w_wincol, col,
3146 (int)wp->w_width, screen_line_flags);
3147#endif
3148 ++row;
3149 ++screen_row;
3150
3151 // When not wrapping and finished diff lines, or when displayed
3152 // '$' and highlighting until last column, break here.
3153 if ((!wp->w_p_wrap
3154#ifdef FEAT_DIFF
3155 && filler_todo <= 0
3156#endif
3157 ) || lcs_eol_one == -1)
3158 break;
3159
3160 // When the window is too narrow draw all "@" lines.
3161 if (draw_state != WL_LINE
3162#ifdef FEAT_DIFF
3163 && filler_todo <= 0
3164#endif
3165 )
3166 {
3167 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
3168 draw_vsep_win(wp, row);
3169 row = endrow;
3170 }
3171
3172 // When line got too long for screen break here.
3173 if (row == endrow)
3174 {
3175 ++row;
3176 break;
3177 }
3178
3179 if (screen_cur_row == screen_row - 1
3180#ifdef FEAT_DIFF
3181 && filler_todo <= 0
3182#endif
3183 && wp->w_width == Columns)
3184 {
3185 // Remember that the line wraps, used for modeless copy.
3186 LineWraps[screen_row - 1] = TRUE;
3187
3188 // Special trick to make copy/paste of wrapped lines work with
3189 // xterm/screen: write an extra character beyond the end of
3190 // the line. This will work with all terminal types
3191 // (regardless of the xn,am settings).
3192 // Only do this on a fast tty.
3193 // Only do this if the cursor is on the current line
3194 // (something has been written in it).
3195 // Don't do this for the GUI.
3196 // Don't do this for double-width characters.
3197 // Don't do this for a window not at the right screen border.
3198 if (p_tf
3199#ifdef FEAT_GUI
3200 && !gui.in_use
3201#endif
3202 && !(has_mbyte
3203 && ((*mb_off2cells)(LineOffset[screen_row],
3204 LineOffset[screen_row] + screen_Columns)
3205 == 2
3206 || (*mb_off2cells)(LineOffset[screen_row - 1]
3207 + (int)Columns - 2,
3208 LineOffset[screen_row] + screen_Columns)
3209 == 2)))
3210 {
3211 // First make sure we are at the end of the screen line,
3212 // then output the same character again to let the
3213 // terminal know about the wrap. If the terminal doesn't
3214 // auto-wrap, we overwrite the character.
3215 if (screen_cur_col != wp->w_width)
3216 screen_char(LineOffset[screen_row - 1]
3217 + (unsigned)Columns - 1,
3218 screen_row - 1, (int)(Columns - 1));
3219
3220 // When there is a multi-byte character, just output a
3221 // space to keep it simple.
3222 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
3223 screen_row - 1] + (Columns - 1)]) > 1)
3224 out_char(' ');
3225 else
3226 out_char(ScreenLines[LineOffset[screen_row - 1]
3227 + (Columns - 1)]);
3228 // force a redraw of the first char on the next line
3229 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
3230 screen_start(); // don't know where cursor is now
3231 }
3232 }
3233
3234 col = 0;
3235 off = (unsigned)(current_ScreenLine - ScreenLines);
3236#ifdef FEAT_RIGHTLEFT
3237 if (wp->w_p_rl)
3238 {
3239 col = wp->w_width - 1; // col is not used if breaking!
3240 off += col;
3241 }
3242#endif
3243
3244 // reset the drawing state for the start of a wrapped line
3245 draw_state = WL_START;
3246 saved_n_extra = n_extra;
3247 saved_p_extra = p_extra;
3248 saved_c_extra = c_extra;
3249 saved_c_final = c_final;
3250#ifdef FEAT_SYN_HL
3251 if (!(cul_screenline
3252# ifdef FEAT_DIFF
Bram Moolenaare7705982020-04-21 22:23:15 +02003253 && diff_hlf == (hlf_T)0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003254# endif
Bram Moolenaare7705982020-04-21 22:23:15 +02003255 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003256 saved_char_attr = char_attr;
3257 else
3258#endif
3259 saved_char_attr = 0;
3260 n_extra = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01003261 lcs_prec_todo = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003262#ifdef FEAT_LINEBREAK
3263# ifdef FEAT_DIFF
3264 if (filler_todo <= 0)
3265# endif
3266 need_showbreak = TRUE;
3267#endif
3268#ifdef FEAT_DIFF
3269 --filler_todo;
3270 // When the filler lines are actually below the last line of the
3271 // file, don't draw the line itself, break here.
3272 if (filler_todo == 0 && wp->w_botfill)
3273 break;
3274#endif
3275 }
3276
3277 } // for every character in the line
3278
3279#ifdef FEAT_SPELL
3280 // After an empty line check first word for capital.
3281 if (*skipwhite(line) == NUL)
3282 {
3283 capcol_lnum = lnum + 1;
3284 cap_col = 0;
3285 }
3286#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003287#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003288 vim_free(text_props);
3289 vim_free(text_prop_idxs);
3290#endif
3291
3292 vim_free(p_extra_free);
3293 return row;
3294}