blob: 789893b0b2bba3372c0eea7473444f9b57523657 [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 Moolenaarae49aa82022-02-25 21:05:36 +0000333 int can_spell = FALSE;
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
Bram Moolenaar936dc602022-03-06 20:47:01 +0000357#else
358# define filler_lines 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200359#endif
360#ifdef FEAT_DIFF
361 hlf_T diff_hlf = (hlf_T)0; // type of diff highlighting
362 int change_start = MAXCOL; // first col of changed area
363 int change_end = -1; // last col of changed area
364#endif
365 colnr_T trailcol = MAXCOL; // start of trailing spaces
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100366 colnr_T leadcol = 0; // start of leading spaces
zeertzjqf14b8ba2021-09-10 16:58:30 +0200367 int in_multispace = FALSE; // in multiple consecutive spaces
368 int multispace_pos = 0; // position in lcs-multispace string
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200369#ifdef FEAT_LINEBREAK
370 int need_showbreak = FALSE; // overlong line, skipping first x
371 // chars
372#endif
373#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
374 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
375# define LINE_ATTR
376 int line_attr = 0; // attribute for the whole line
377 int line_attr_save;
378#endif
379#ifdef FEAT_SIGNS
380 int sign_present = FALSE;
381 sign_attrs_T sattr;
James McCoya80aad72021-12-22 19:45:28 +0000382 int num_attr = 0; // attribute for the number column
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200383#endif
384#ifdef FEAT_ARABIC
385 int prev_c = 0; // previous Arabic character
386 int prev_c1 = 0; // first composing char for prev_c
387#endif
388#if defined(LINE_ATTR)
389 int did_line_attr = 0;
390#endif
391#ifdef FEAT_TERMINAL
392 int get_term_attr = FALSE;
393#endif
394#ifdef FEAT_SYN_HL
395 int cul_attr = 0; // set when 'cursorline' active
396
397 // 'cursorlineopt' has "screenline" and cursor is in this line
398 int cul_screenline = FALSE;
399
400 // margin columns for the screen line, needed for when 'cursorlineopt'
401 // contains "screenline"
402 int left_curline_col = 0;
403 int right_curline_col = 0;
404#endif
405
406 // draw_state: items that are drawn in sequence:
407#define WL_START 0 // nothing done yet
408#ifdef FEAT_CMDWIN
kylo252ae6f1d82022-02-16 19:24:07 +0000409# define WL_CMDLINE (WL_START + 1) // cmdline window column
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200410#else
411# define WL_CMDLINE WL_START
412#endif
413#ifdef FEAT_FOLDING
kylo252ae6f1d82022-02-16 19:24:07 +0000414# define WL_FOLD (WL_CMDLINE + 1) // 'foldcolumn'
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200415#else
416# define WL_FOLD WL_CMDLINE
417#endif
418#ifdef FEAT_SIGNS
kylo252ae6f1d82022-02-16 19:24:07 +0000419# define WL_SIGN (WL_FOLD + 1) // column for signs
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200420#else
421# define WL_SIGN WL_FOLD // column for signs
422#endif
kylo252ae6f1d82022-02-16 19:24:07 +0000423#define WL_NR (WL_SIGN + 1) // line number
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200424#ifdef FEAT_LINEBREAK
kylo252ae6f1d82022-02-16 19:24:07 +0000425# define WL_BRI (WL_NR + 1) // 'breakindent'
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200426#else
427# define WL_BRI WL_NR
428#endif
429#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
kylo252ae6f1d82022-02-16 19:24:07 +0000430# define WL_SBR (WL_BRI + 1) // 'showbreak' or 'diff'
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200431#else
432# define WL_SBR WL_BRI
433#endif
kylo252ae6f1d82022-02-16 19:24:07 +0000434#define WL_LINE (WL_SBR + 1) // text in the line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200435 int draw_state = WL_START; // what to draw next
436#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
437 int feedback_col = 0;
438 int feedback_old_attr = -1;
439#endif
440 int screen_line_flags = 0;
441
442#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
443 int match_conc = 0; // cchar for match functions
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000444 int on_last_col = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200445#endif
446#ifdef FEAT_CONCEAL
447 int syntax_flags = 0;
448 int syntax_seqnr = 0;
449 int prev_syntax_id = 0;
450 int conceal_attr = HL_ATTR(HLF_CONCEAL);
451 int is_concealing = FALSE;
452 int boguscols = 0; // nonexistent columns added to force
453 // wrapping
454 int vcol_off = 0; // offset for concealed characters
455 int did_wcol = FALSE;
456 int old_boguscols = 0;
457# define VCOL_HLC (vcol - vcol_off)
458# define FIX_FOR_BOGUSCOLS \
459 { \
460 n_extra += vcol_off; \
461 vcol -= vcol_off; \
462 vcol_off = 0; \
463 col -= boguscols; \
464 old_boguscols = boguscols; \
465 boguscols = 0; \
466 }
467#else
468# define VCOL_HLC (vcol)
469#endif
470
471 if (startrow > endrow) // past the end already!
472 return startrow;
473
474 row = startrow;
475 screen_row = row + W_WINROW(wp);
476
477 if (!number_only)
478 {
479 // To speed up the loop below, set extra_check when there is linebreak,
480 // trailing white space and/or syntax processing to be done.
481#ifdef FEAT_LINEBREAK
482 extra_check = wp->w_p_lbr;
483#endif
484#ifdef FEAT_SYN_HL
485 if (syntax_present(wp) && !wp->w_s->b_syn_error
486# ifdef SYN_TIME_LIMIT
487 && !wp->w_s->b_syn_slow
488# endif
489 )
490 {
491 // Prepare for syntax highlighting in this line. When there is an
492 // error, stop syntax highlighting.
493 save_did_emsg = did_emsg;
494 did_emsg = FALSE;
495 syntax_start(wp, lnum);
496 if (did_emsg)
497 wp->w_s->b_syn_error = TRUE;
498 else
499 {
500 did_emsg = save_did_emsg;
501#ifdef SYN_TIME_LIMIT
502 if (!wp->w_s->b_syn_slow)
503#endif
504 {
505 has_syntax = TRUE;
506 extra_check = TRUE;
507 }
508 }
509 }
510
511 // Check for columns to display for 'colorcolumn'.
512 color_cols = wp->w_p_cc_cols;
513 if (color_cols != NULL)
514 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
515#endif
516
517#ifdef FEAT_TERMINAL
518 if (term_show_buffer(wp->w_buffer))
519 {
520 extra_check = TRUE;
521 get_term_attr = TRUE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +0100522 win_attr = term_get_attr(wp, lnum, -1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200523 }
524#endif
525
526#ifdef FEAT_SPELL
527 if (wp->w_p_spell
528 && *wp->w_s->b_p_spl != NUL
529 && wp->w_s->b_langp.ga_len > 0
530 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
531 {
532 // Prepare for spell checking.
533 has_spell = TRUE;
534 extra_check = TRUE;
535
536 // Get the start of the next line, so that words that wrap to the
537 // next line are found too: "et<line-break>al.".
538 // Trick: skip a few chars for C/shell/Vim comments
539 nextline[SPWORDLEN] = NUL;
540 if (lnum < wp->w_buffer->b_ml.ml_line_count)
541 {
542 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
543 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
544 }
545
546 // When a word wrapped from the previous line the start of the
547 // current line is valid.
548 if (lnum == checked_lnum)
549 cur_checked_col = checked_col;
550 checked_lnum = 0;
551
552 // When there was a sentence end in the previous line may require a
553 // word starting with capital in this line. In line 1 always check
554 // the first word.
555 if (lnum != capcol_lnum)
556 cap_col = -1;
557 if (lnum == 1)
558 cap_col = 0;
559 capcol_lnum = 0;
560 }
561#endif
562
563 // handle Visual active in this window
564 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
565 {
Bram Moolenaar14285cb2020-03-27 20:58:37 +0100566 pos_T *top, *bot;
567
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200568 if (LTOREQ_POS(curwin->w_cursor, VIsual))
569 {
570 // Visual is after curwin->w_cursor
571 top = &curwin->w_cursor;
572 bot = &VIsual;
573 }
574 else
575 {
576 // Visual is before curwin->w_cursor
577 top = &VIsual;
578 bot = &curwin->w_cursor;
579 }
580 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
581 if (VIsual_mode == Ctrl_V)
582 {
583 // block mode
584 if (lnum_in_visual_area)
585 {
586 fromcol = wp->w_old_cursor_fcol;
587 tocol = wp->w_old_cursor_lcol;
588 }
589 }
590 else
591 {
592 // non-block mode
593 if (lnum > top->lnum && lnum <= bot->lnum)
594 fromcol = 0;
595 else if (lnum == top->lnum)
596 {
597 if (VIsual_mode == 'V') // linewise
598 fromcol = 0;
599 else
600 {
601 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
602 if (gchar_pos(top) == NUL)
603 tocol = fromcol + 1;
604 }
605 }
606 if (VIsual_mode != 'V' && lnum == bot->lnum)
607 {
608 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
609 {
610 fromcol = -10;
611 tocol = MAXCOL;
612 }
613 else if (bot->col == MAXCOL)
614 tocol = MAXCOL;
615 else
616 {
617 pos = *bot;
618 if (*p_sel == 'e')
619 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
620 else
621 {
622 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
623 ++tocol;
624 }
625 }
626 }
627 }
628
629 // Check if the character under the cursor should not be inverted
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200630 if (!highlight_match && lnum == curwin->w_cursor.lnum
631 && wp == curwin
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200632#ifdef FEAT_GUI
633 && !gui.in_use
634#endif
635 )
636 noinvcur = TRUE;
637
638 // if inverting in this line set area_highlighting
639 if (fromcol >= 0)
640 {
641 area_highlighting = TRUE;
642 vi_attr = HL_ATTR(HLF_V);
643#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
644 if ((clip_star.available && !clip_star.owned
645 && clip_isautosel_star())
646 || (clip_plus.available && !clip_plus.owned
647 && clip_isautosel_plus()))
648 vi_attr = HL_ATTR(HLF_VNC);
649#endif
650 }
651 }
652
653 // handle 'incsearch' and ":s///c" highlighting
654 else if (highlight_match
655 && wp == curwin
656 && lnum >= curwin->w_cursor.lnum
657 && lnum <= curwin->w_cursor.lnum + search_match_lines)
658 {
659 if (lnum == curwin->w_cursor.lnum)
660 getvcol(curwin, &(curwin->w_cursor),
661 (colnr_T *)&fromcol, NULL, NULL);
662 else
663 fromcol = 0;
664 if (lnum == curwin->w_cursor.lnum + search_match_lines)
665 {
666 pos.lnum = lnum;
667 pos.col = search_match_endcol;
668 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
669 }
670 else
671 tocol = MAXCOL;
672 // do at least one character; happens when past end of line
Bram Moolenaar448465e2020-11-25 13:49:27 +0100673 if (fromcol == tocol && search_match_endcol)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200674 tocol = fromcol + 1;
675 area_highlighting = TRUE;
676 vi_attr = HL_ATTR(HLF_I);
677 }
678 }
679
680#ifdef FEAT_DIFF
681 filler_lines = diff_check(wp, lnum);
682 if (filler_lines < 0)
683 {
684 if (filler_lines == -1)
685 {
686 if (diff_find_change(wp, lnum, &change_start, &change_end))
687 diff_hlf = HLF_ADD; // added line
688 else if (change_start == 0)
689 diff_hlf = HLF_TXD; // changed text
690 else
691 diff_hlf = HLF_CHD; // changed line
692 }
693 else
694 diff_hlf = HLF_ADD; // added line
695 filler_lines = 0;
696 area_highlighting = TRUE;
697 }
698 if (lnum == wp->w_topline)
699 filler_lines = wp->w_topfill;
700 filler_todo = filler_lines;
701#endif
702
703#ifdef FEAT_SIGNS
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +0100704 sign_present = buf_get_signattrs(wp, lnum, &sattr);
James McCoya80aad72021-12-22 19:45:28 +0000705 if (sign_present)
706 num_attr = sattr.sat_numhl;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200707#endif
708
709#ifdef LINE_ATTR
710# ifdef FEAT_SIGNS
711 // If this line has a sign with line highlighting set line_attr.
712 if (sign_present)
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200713 line_attr = sattr.sat_linehl;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200714# endif
715# if defined(FEAT_QUICKFIX)
716 // Highlight the current line in the quickfix window.
717 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
718 line_attr = HL_ATTR(HLF_QFL);
719# endif
720 if (line_attr != 0)
721 area_highlighting = TRUE;
722#endif
723
724 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
725 ptr = line;
726
727#ifdef FEAT_SPELL
728 if (has_spell && !number_only)
729 {
730 // For checking first word with a capital skip white space.
731 if (cap_col == 0)
732 cap_col = getwhitecols(line);
733
734 // To be able to spell-check over line boundaries copy the end of the
735 // current line into nextline[]. Above the start of the next line was
736 // copied to nextline[SPWORDLEN].
737 if (nextline[SPWORDLEN] == NUL)
738 {
739 // No next line or it is empty.
740 nextlinecol = MAXCOL;
741 nextline_idx = 0;
742 }
743 else
744 {
745 v = (long)STRLEN(line);
746 if (v < SPWORDLEN)
747 {
748 // Short line, use it completely and append the start of the
749 // next line.
750 nextlinecol = 0;
751 mch_memmove(nextline, line, (size_t)v);
752 STRMOVE(nextline + v, nextline + SPWORDLEN);
753 nextline_idx = v + 1;
754 }
755 else
756 {
757 // Long line, use only the last SPWORDLEN bytes.
758 nextlinecol = v - SPWORDLEN;
759 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
760 nextline_idx = SPWORDLEN + 1;
761 }
762 }
763 }
764#endif
765
766 if (wp->w_p_list)
767 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100768 if (wp->w_lcs_chars.space
zeertzjqf14b8ba2021-09-10 16:58:30 +0200769 || wp->w_lcs_chars.multispace != NULL
Bram Moolenaaraca12fd2022-06-07 10:16:15 +0100770 || wp->w_lcs_chars.leadmultispace != NULL
Bram Moolenaareed9d462021-02-15 20:38:25 +0100771 || wp->w_lcs_chars.trail
772 || wp->w_lcs_chars.lead
773 || wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200774 extra_check = TRUE;
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100775
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200776 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +0100777 if (wp->w_lcs_chars.trail)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200778 {
779 trailcol = (colnr_T)STRLEN(ptr);
780 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
781 --trailcol;
782 trailcol += (colnr_T) (ptr - line);
783 }
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100784 // find end of leading whitespace
Bram Moolenaaraca12fd2022-06-07 10:16:15 +0100785 if (wp->w_lcs_chars.lead || wp->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100786 {
787 leadcol = 0;
788 while (VIM_ISWHITE(ptr[leadcol]))
789 ++leadcol;
790 if (ptr[leadcol] == NUL)
791 // in a line full of spaces all of them are treated as trailing
792 leadcol = (colnr_T)0;
793 else
794 // keep track of the first column not filled with spaces
795 leadcol += (colnr_T) (ptr - line) + 1;
796 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200797 }
798
799 wcr_attr = get_wcr_attr(wp);
800 if (wcr_attr != 0)
801 {
802 win_attr = wcr_attr;
803 area_highlighting = TRUE;
804 }
Bram Moolenaar34390282019-10-16 14:38:26 +0200805
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100806#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200807 if (WIN_IS_POPUP(wp))
808 screen_line_flags |= SLF_POPUP;
809#endif
810
811 // 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
812 // first character to be displayed.
813 if (wp->w_p_wrap)
814 v = wp->w_skipcol;
815 else
816 v = wp->w_leftcol;
817 if (v > 0 && !number_only)
818 {
819 char_u *prev_ptr = ptr;
820
821 while (vcol < v && *ptr != NUL)
822 {
823 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
824 vcol += c;
825 prev_ptr = ptr;
826 MB_PTR_ADV(ptr);
827 }
828
829 // When:
830 // - 'cuc' is set, or
831 // - 'colorcolumn' is set, or
832 // - 'virtualedit' is set, or
833 // - the visual mode is active,
834 // the end of the line may be before the start of the displayed part.
835 if (vcol < v && (
836#ifdef FEAT_SYN_HL
837 wp->w_p_cuc || draw_color_col ||
838#endif
839 virtual_active() ||
840 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
841 vcol = v;
842
843 // Handle a character that's not completely on the screen: Put ptr at
844 // that character but skip the first few screen characters.
845 if (vcol > v)
846 {
847 vcol -= c;
848 ptr = prev_ptr;
849 // If the character fits on the screen, don't need to skip it.
850 // Except for a TAB.
851 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
852 n_skip = v - vcol;
853 }
854
855 // Adjust for when the inverted text is before the screen,
856 // and when the start of the inverted text is before the screen.
857 if (tocol <= vcol)
858 fromcol = 0;
859 else if (fromcol >= 0 && fromcol < vcol)
860 fromcol = vcol;
861
862#ifdef FEAT_LINEBREAK
863 // When w_skipcol is non-zero, first line needs 'showbreak'
864 if (wp->w_p_wrap)
865 need_showbreak = TRUE;
866#endif
867#ifdef FEAT_SPELL
868 // When spell checking a word we need to figure out the start of the
869 // word and if it's badly spelled or not.
870 if (has_spell)
871 {
872 int len;
873 colnr_T linecol = (colnr_T)(ptr - line);
874 hlf_T spell_hlf = HLF_COUNT;
875
876 pos = wp->w_cursor;
877 wp->w_cursor.lnum = lnum;
878 wp->w_cursor.col = linecol;
879 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
880
881 // spell_move_to() may call ml_get() and make "line" invalid
882 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
883 ptr = line + linecol;
884
885 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
886 {
887 // no bad word found at line start, don't check until end of a
888 // word
889 spell_hlf = HLF_COUNT;
890 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
891 }
892 else
893 {
894 // bad word found, use attributes until end of word
895 word_end = wp->w_cursor.col + len + 1;
896
897 // Turn index into actual attributes.
898 if (spell_hlf != HLF_COUNT)
899 spell_attr = highlight_attr[spell_hlf];
900 }
901 wp->w_cursor = pos;
902
903# ifdef FEAT_SYN_HL
904 // Need to restart syntax highlighting for this line.
905 if (has_syntax)
906 syntax_start(wp, lnum);
907# endif
908 }
909#endif
910 }
911
912 // Correct highlighting for cursor that can't be disabled.
913 // Avoids having to check this for each character.
914 if (fromcol >= 0)
915 {
916 if (noinvcur)
917 {
918 if ((colnr_T)fromcol == wp->w_virtcol)
919 {
920 // highlighting starts at cursor, let it start just after the
921 // cursor
922 fromcol_prev = fromcol;
923 fromcol = -1;
924 }
925 else if ((colnr_T)fromcol < wp->w_virtcol)
926 // restart highlighting after the cursor
927 fromcol_prev = wp->w_virtcol;
928 }
929 if (fromcol >= tocol)
930 fromcol = -1;
931 }
932
933#ifdef FEAT_SEARCH_EXTRA
934 if (!number_only)
935 {
936 v = (long)(ptr - line);
937 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
938 &line, &screen_search_hl,
939 &search_attr);
940 ptr = line + v; // "line" may have been updated
941 }
942#endif
943
944#ifdef FEAT_SYN_HL
945 // Cursor line highlighting for 'cursorline' in the current window.
946 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
947 {
948 // Do not show the cursor line in the text when Visual mode is active,
zeertzjqc20e46a2022-03-23 14:55:23 +0000949 // because it's not clear what is selected then.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200950 if (!(wp == curwin && VIsual_active)
951 && wp->w_p_culopt_flags != CULOPT_NBR)
952 {
953 cul_screenline = (wp->w_p_wrap
954 && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
955
956 // Only set line_attr here when "screenline" is not present in
957 // 'cursorlineopt'. Otherwise it's done later.
958 if (!cul_screenline)
959 {
960 cul_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar39f7aa32020-08-31 22:00:05 +0200961# ifdef FEAT_SIGNS
962 // Combine the 'cursorline' and sign highlighting, depending on
963 // the sign priority.
964 if (sign_present && sattr.sat_linehl > 0)
965 {
966 if (sattr.sat_priority >= 100)
967 line_attr = hl_combine_attr(cul_attr, line_attr);
968 else
969 line_attr = hl_combine_attr(line_attr, cul_attr);
970 }
971 else
972# endif
Bram Moolenaar7fe956d2022-07-03 14:21:09 +0100973# if defined(FEAT_QUICKFIX)
974 line_attr = hl_combine_attr(line_attr, cul_attr);
975# else
Bram Moolenaar39f7aa32020-08-31 22:00:05 +0200976 line_attr = cul_attr;
Bram Moolenaar7fe956d2022-07-03 14:21:09 +0100977# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200978 }
979 else
980 {
981 line_attr_save = line_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200982 margin_columns_win(wp, &left_curline_col, &right_curline_col);
983 }
984 area_highlighting = TRUE;
985 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200986 }
987#endif
988
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100989#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200990 {
991 char_u *prop_start;
992
993 text_prop_count = get_text_props(wp->w_buffer, lnum,
994 &prop_start, FALSE);
995 if (text_prop_count > 0)
996 {
997 // Make a copy of the properties, so that they are properly
998 // aligned.
999 text_props = ALLOC_MULT(textprop_T, text_prop_count);
1000 if (text_props != NULL)
1001 mch_memmove(text_props, prop_start,
1002 text_prop_count * sizeof(textprop_T));
1003
1004 // Allocate an array for the indexes.
1005 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
1006 area_highlighting = TRUE;
1007 extra_check = TRUE;
1008 }
1009 }
1010#endif
1011
1012 off = (unsigned)(current_ScreenLine - ScreenLines);
1013 col = 0;
1014
1015#ifdef FEAT_RIGHTLEFT
1016 if (wp->w_p_rl)
1017 {
1018 // Rightleft window: process the text in the normal direction, but put
1019 // it in current_ScreenLine[] from right to left. Start at the
1020 // rightmost column of the window.
1021 col = wp->w_width - 1;
1022 off += col;
1023 screen_line_flags |= SLF_RIGHTLEFT;
1024 }
1025#endif
1026
1027 // Repeat for the whole displayed line.
1028 for (;;)
1029 {
1030#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02001031 int has_match_conc = 0; // match wants to conceal
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001032#endif
1033#ifdef FEAT_CONCEAL
1034 int did_decrement_ptr = FALSE;
1035#endif
1036 // Skip this quickly when working on the text.
1037 if (draw_state != WL_LINE)
1038 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001039#ifdef FEAT_SYN_HL
1040 if (cul_screenline)
1041 {
1042 cul_attr = 0;
1043 line_attr = line_attr_save;
1044 }
1045#endif
1046
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001047#ifdef FEAT_CMDWIN
1048 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
1049 {
1050 draw_state = WL_CMDLINE;
1051 if (cmdwin_type != 0 && wp == curwin)
1052 {
1053 // Draw the cmdline character.
1054 n_extra = 1;
1055 c_extra = cmdwin_type;
1056 c_final = NUL;
1057 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
1058 }
1059 }
1060#endif
1061
1062#ifdef FEAT_FOLDING
1063 if (draw_state == WL_FOLD - 1 && n_extra == 0)
1064 {
1065 int fdc = compute_foldcolumn(wp, 0);
1066
1067 draw_state = WL_FOLD;
1068 if (fdc > 0)
1069 {
1070 // Draw the 'foldcolumn'. Allocate a buffer, "extra" may
1071 // already be in use.
1072 vim_free(p_extra_free);
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001073 p_extra_free = alloc(MAX_MCO * fdc + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001074 if (p_extra_free != NULL)
1075 {
Bram Moolenaar9355ae42021-03-08 19:04:05 +01001076 n_extra = (int)fill_foldcolumn(p_extra_free, wp,
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001077 FALSE, lnum);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001078 p_extra_free[n_extra] = NUL;
1079 p_extra = p_extra_free;
1080 c_extra = NUL;
1081 c_final = NUL;
Bram Moolenaare413ea02021-11-24 16:20:13 +00001082 if (use_cursor_line_sign(wp, lnum))
1083 char_attr =
1084 hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLF));
1085 else
1086 char_attr =
1087 hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001088 }
1089 }
1090 }
1091#endif
1092
1093#ifdef FEAT_SIGNS
1094 if (draw_state == WL_SIGN - 1 && n_extra == 0)
1095 {
1096 draw_state = WL_SIGN;
1097 // Show the sign column when there are any signs in this
1098 // buffer or when using Netbeans.
1099 if (signcolumn_on(wp))
1100 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
1101 row, startrow, filler_lines, filler_todo, &c_extra,
1102 &c_final, extra, &p_extra, &n_extra, &char_attr);
1103 }
1104#endif
1105
1106 if (draw_state == WL_NR - 1 && n_extra == 0)
1107 {
1108 draw_state = WL_NR;
1109 // Display the absolute or relative line number. After the
1110 // first fill with blanks when the 'n' flag isn't in 'cpo'
1111 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar936dc602022-03-06 20:47:01 +00001112 && (row == startrow + filler_lines
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001113 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
1114 {
1115#ifdef FEAT_SIGNS
1116 // If 'signcolumn' is set to 'number' and a sign is present
1117 // in 'lnum', then display the sign instead of the line
1118 // number.
1119 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
1120 && sign_present)
1121 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
1122 row, startrow, filler_lines, filler_todo,
1123 &c_extra, &c_final, extra, &p_extra, &n_extra,
1124 &char_attr);
1125 else
1126#endif
1127 {
1128 // Draw the line number (empty space after wrapping).
Bram Moolenaar936dc602022-03-06 20:47:01 +00001129 if (row == startrow + filler_lines)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001130 {
1131 long num;
1132 char *fmt = "%*ld ";
1133
1134 if (wp->w_p_nu && !wp->w_p_rnu)
1135 // 'number' + 'norelativenumber'
1136 num = (long)lnum;
1137 else
1138 {
1139 // 'relativenumber', don't use negative numbers
1140 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1141 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1142 {
1143 // 'number' + 'relativenumber'
1144 num = lnum;
1145 fmt = "%-*ld ";
1146 }
1147 }
1148
1149 sprintf((char *)extra, fmt,
1150 number_width(wp), num);
1151 if (wp->w_skipcol > 0)
1152 for (p_extra = extra; *p_extra == ' '; ++p_extra)
1153 *p_extra = '-';
1154#ifdef FEAT_RIGHTLEFT
1155 if (wp->w_p_rl) // reverse line numbers
1156 {
1157 char_u *p1, *p2;
1158 int t;
1159
1160 // like rl_mirror(), but keep the space at the end
Christian Brabandt29f0dc32021-06-16 19:28:34 +02001161 p2 = skipwhite(extra);
1162 p2 = skiptowhite(p2) - 1;
1163 for (p1 = skipwhite(extra); p1 < p2; ++p1, --p2)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001164 {
1165 t = *p1;
1166 *p1 = *p2;
1167 *p2 = t;
1168 }
1169 }
1170#endif
1171 p_extra = extra;
1172 c_extra = NUL;
1173 c_final = NUL;
1174 }
1175 else
1176 {
1177 c_extra = ' ';
1178 c_final = NUL;
1179 }
1180 n_extra = number_width(wp) + 1;
1181 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
1182#ifdef FEAT_SYN_HL
1183 // When 'cursorline' is set highlight the line number of
1184 // the current line differently.
zeertzjq754d2b42022-03-13 13:40:45 +00001185 // When 'cursorlineopt' does not have "line" only
1186 // highlight the line number itself.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001187 // TODO: Can we use CursorLine instead of CursorLineNr
1188 // when CursorLineNr isn't set?
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001189 if (wp->w_p_cul
1190 && lnum == wp->w_cursor.lnum
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001191 && (wp->w_p_culopt_flags & CULOPT_NBR)
Bram Moolenaar127969c2022-03-06 19:54:13 +00001192 && (row == startrow + filler_lines
1193 || (row > startrow + filler_lines
zeertzjq754d2b42022-03-13 13:40:45 +00001194 && (wp->w_p_culopt_flags & CULOPT_LINE))))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001195 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
1196#endif
Bram Moolenaarefae76a2019-10-27 22:54:58 +01001197 if (wp->w_p_rnu && lnum < wp->w_cursor.lnum
1198 && HL_ATTR(HLF_LNA) != 0)
1199 // Use LineNrAbove
1200 char_attr = hl_combine_attr(wcr_attr,
1201 HL_ATTR(HLF_LNA));
1202 if (wp->w_p_rnu && lnum > wp->w_cursor.lnum
1203 && HL_ATTR(HLF_LNB) != 0)
1204 // Use LineNrBelow
1205 char_attr = hl_combine_attr(wcr_attr,
1206 HL_ATTR(HLF_LNB));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001207 }
James McCoya80aad72021-12-22 19:45:28 +00001208#ifdef FEAT_SIGNS
1209 if (num_attr)
1210 char_attr = num_attr;
1211#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001212 }
1213 }
1214
1215#ifdef FEAT_LINEBREAK
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001216 if (wp->w_briopt_sbr && draw_state == WL_BRI - 1
Bram Moolenaaree857022019-11-09 23:26:40 +01001217 && n_extra == 0 && *get_showbreak_value(wp) != NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001218 // draw indent after showbreak value
1219 draw_state = WL_BRI;
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001220 else if (wp->w_briopt_sbr && draw_state == WL_SBR && n_extra == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001221 // After the showbreak, draw the breakindent
1222 draw_state = WL_BRI - 1;
1223
1224 // draw 'breakindent': indent wrapped text accordingly
1225 if (draw_state == WL_BRI - 1 && n_extra == 0)
1226 {
1227 draw_state = WL_BRI;
1228 // if need_showbreak is set, breakindent also applies
Bram Moolenaarfe154992022-03-22 20:42:12 +00001229 if (wp->w_p_bri && (row != startrow || need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001230# ifdef FEAT_DIFF
1231 && filler_lines == 0
1232# endif
1233 )
1234 {
1235 char_attr = 0;
1236# ifdef FEAT_DIFF
1237 if (diff_hlf != (hlf_T)0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001238 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001239# endif
1240 p_extra = NULL;
1241 c_extra = ' ';
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001242 c_final = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001243 n_extra = get_breakindent_win(wp,
1244 ml_get_buf(wp->w_buffer, lnum, FALSE));
Bram Moolenaare882f7a2020-05-16 14:07:39 +02001245 if (row == startrow)
1246 {
1247 n_extra -= win_col_off2(wp);
1248 if (n_extra < 0)
1249 n_extra = 0;
1250 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001251 if (wp->w_skipcol > 0 && wp->w_p_wrap && wp->w_briopt_sbr)
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001252 need_showbreak = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001253 // Correct end of highlighted area for 'breakindent',
1254 // required when 'linebreak' is also set.
1255 if (tocol == vcol)
1256 tocol += n_extra;
1257 }
1258 }
1259#endif
1260
1261#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
1262 if (draw_state == WL_SBR - 1 && n_extra == 0)
1263 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001264 char_u *sbr;
1265
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001266 draw_state = WL_SBR;
1267# ifdef FEAT_DIFF
1268 if (filler_todo > 0)
1269 {
1270 // Draw "deleted" diff line(s).
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001271 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001272 {
1273 c_extra = '-';
1274 c_final = NUL;
1275 }
1276 else
1277 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001278 c_extra = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001279 c_final = NUL;
1280 }
1281# ifdef FEAT_RIGHTLEFT
1282 if (wp->w_p_rl)
1283 n_extra = col + 1;
1284 else
1285# endif
1286 n_extra = wp->w_width - col;
1287 char_attr = HL_ATTR(HLF_DED);
1288 }
1289# endif
1290# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001291 sbr = get_showbreak_value(wp);
1292 if (*sbr != NUL && need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001293 {
1294 // Draw 'showbreak' at the start of each broken line.
Bram Moolenaaree857022019-11-09 23:26:40 +01001295 p_extra = sbr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001296 c_extra = NUL;
1297 c_final = NUL;
Bram Moolenaaree857022019-11-09 23:26:40 +01001298 n_extra = (int)STRLEN(sbr);
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001299 if (wp->w_skipcol == 0 || !wp->w_p_wrap)
1300 need_showbreak = FALSE;
Bram Moolenaaree857022019-11-09 23:26:40 +01001301 vcol_sbr = vcol + MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001302 // Correct end of highlighted area for 'showbreak',
1303 // required when 'linebreak' is also set.
1304 if (tocol == vcol)
1305 tocol += n_extra;
1306 // combine 'showbreak' with 'wincolor'
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001307 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001308# ifdef FEAT_SYN_HL
1309 // combine 'showbreak' with 'cursorline'
1310 if (cul_attr != 0)
1311 char_attr = hl_combine_attr(char_attr, cul_attr);
1312# endif
1313 }
1314# endif
1315 }
1316#endif
1317
1318 if (draw_state == WL_LINE - 1 && n_extra == 0)
1319 {
1320 draw_state = WL_LINE;
1321 if (saved_n_extra)
1322 {
1323 // Continue item from end of wrapped line.
1324 n_extra = saved_n_extra;
1325 c_extra = saved_c_extra;
1326 c_final = saved_c_final;
1327 p_extra = saved_p_extra;
1328 char_attr = saved_char_attr;
1329 }
1330 else
1331 char_attr = win_attr;
1332 }
1333 }
1334#ifdef FEAT_SYN_HL
zeertzjq4f33bc22021-08-05 17:57:02 +02001335 if (cul_screenline && draw_state == WL_LINE
1336 && vcol >= left_curline_col
1337 && vcol < right_curline_col)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001338 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001339 cul_attr = HL_ATTR(HLF_CUL);
1340 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001341 }
1342#endif
1343
1344 // When still displaying '$' of change command, stop at cursor.
1345 // When only displaying the (relative) line number and that's done,
1346 // stop here.
Bram Moolenaar511feec2020-06-18 19:15:27 +02001347 if (((dollar_vcol >= 0 && wp == curwin
1348 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol)
1349 || (number_only && draw_state > WL_NR))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001350#ifdef FEAT_DIFF
1351 && filler_todo <= 0
1352#endif
1353 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001354 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001355 screen_line(wp, screen_row, wp->w_wincol, col, -wp->w_width,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001356 screen_line_flags);
1357 // Pretend we have finished updating the window. Except when
1358 // 'cursorcolumn' is set.
1359#ifdef FEAT_SYN_HL
1360 if (wp->w_p_cuc)
1361 row = wp->w_cline_row + wp->w_cline_height;
1362 else
1363#endif
1364 row = wp->w_height;
1365 break;
1366 }
1367
Bram Moolenaar34390282019-10-16 14:38:26 +02001368 if (draw_state == WL_LINE && (area_highlighting || extra_check))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001369 {
1370 // handle Visual or match highlighting in this line
1371 if (vcol == fromcol
1372 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
1373 && (*mb_ptr2cells)(ptr) > 1)
1374 || ((int)vcol_prev == fromcol_prev
1375 && vcol_prev < vcol // not at margin
1376 && vcol < tocol))
1377 area_attr = vi_attr; // start highlighting
1378 else if (area_attr != 0
1379 && (vcol == tocol
1380 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
1381 area_attr = 0; // stop highlighting
1382
1383#ifdef FEAT_SEARCH_EXTRA
1384 if (!n_extra)
1385 {
1386 // Check for start/end of 'hlsearch' and other matches.
1387 // After end, check for start/end of next match.
1388 // When another match, have to check for start again.
1389 v = (long)(ptr - line);
1390 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
1391 &screen_search_hl, &has_match_conc,
Bram Moolenaar0c359af2021-11-29 19:18:57 +00001392 &match_conc, did_line_attr, lcs_eol_one,
1393 &on_last_col);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001394 ptr = line + v; // "line" may have been changed
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001395
1396 // Do not allow a conceal over EOL otherwise EOL will be missed
1397 // and bad things happen.
1398 if (*ptr == NUL)
1399 has_match_conc = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001400 }
1401#endif
1402
1403#ifdef FEAT_DIFF
1404 if (diff_hlf != (hlf_T)0)
1405 {
1406 if (diff_hlf == HLF_CHD && ptr - line >= change_start
1407 && n_extra == 0)
1408 diff_hlf = HLF_TXD; // changed text
1409 if (diff_hlf == HLF_TXD && ptr - line > change_end
1410 && n_extra == 0)
1411 diff_hlf = HLF_CHD; // changed line
1412 line_attr = HL_ATTR(diff_hlf);
1413 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
1414 && wp->w_p_culopt_flags != CULOPT_NBR
1415 && (!cul_screenline || (vcol >= left_curline_col
1416 && vcol <= right_curline_col)))
1417 line_attr = hl_combine_attr(
1418 line_attr, HL_ATTR(HLF_CUL));
1419 }
1420#endif
1421
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001422#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001423 if (text_props != NULL)
1424 {
1425 int pi;
1426 int bcol = (int)(ptr - line);
1427
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001428 if (n_extra > 0
1429# ifdef FEAT_LINEBREAK
1430 && !in_linebreak
1431# endif
1432 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001433 --bcol; // still working on the previous char, e.g. Tab
1434
1435 // Check if any active property ends.
1436 for (pi = 0; pi < text_props_active; ++pi)
1437 {
1438 int tpi = text_prop_idxs[pi];
1439
1440 if (bcol >= text_props[tpi].tp_col - 1
1441 + text_props[tpi].tp_len)
1442 {
1443 if (pi + 1 < text_props_active)
1444 mch_memmove(text_prop_idxs + pi,
1445 text_prop_idxs + pi + 1,
1446 sizeof(int)
1447 * (text_props_active - (pi + 1)));
1448 --text_props_active;
1449 --pi;
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001450# ifdef FEAT_LINEBREAK
1451 // not exactly right but should work in most cases
1452 if (in_linebreak && syntax_attr == text_prop_attr)
1453 syntax_attr = 0;
1454# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001455 }
1456 }
1457
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001458# ifdef FEAT_LINEBREAK
1459 if (n_extra > 0 && in_linebreak)
1460 // not on the next char yet, don't start another prop
1461 --bcol;
1462# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001463 // Add any text property that starts in this column.
1464 while (text_prop_next < text_prop_count
1465 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001466 {
1467 if (bcol <= text_props[text_prop_next].tp_col - 1
1468 + text_props[text_prop_next].tp_len)
1469 text_prop_idxs[text_props_active++] = text_prop_next;
1470 ++text_prop_next;
1471 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001472
1473 text_prop_attr = 0;
1474 text_prop_combine = FALSE;
Bram Moolenaar053f7122019-09-23 22:17:15 +02001475 text_prop_type = NULL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001476 if (text_props_active > 0)
1477 {
1478 // Sort the properties on priority and/or starting last.
1479 // Then combine the attributes, highest priority last.
1480 current_text_props = text_props;
1481 current_buf = wp->w_buffer;
1482 qsort((void *)text_prop_idxs, (size_t)text_props_active,
1483 sizeof(int), text_prop_compare);
1484
1485 for (pi = 0; pi < text_props_active; ++pi)
1486 {
1487 int tpi = text_prop_idxs[pi];
1488 proptype_T *pt = text_prop_type_by_id(
1489 wp->w_buffer, text_props[tpi].tp_type);
1490
1491 if (pt != NULL && pt->pt_hl_id > 0)
1492 {
1493 int pt_attr = syn_id2attr(pt->pt_hl_id);
1494
1495 text_prop_type = pt;
1496 text_prop_attr =
1497 hl_combine_attr(text_prop_attr, pt_attr);
1498 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
1499 }
1500 }
1501 }
1502 }
1503#endif
1504
Bram Moolenaara74fda62019-10-19 17:38:03 +02001505#ifdef FEAT_SYN_HL
Bram Moolenaara74fda62019-10-19 17:38:03 +02001506 if (extra_check && n_extra == 0)
Bram Moolenaar34390282019-10-16 14:38:26 +02001507 {
Bram Moolenaar82260af2019-10-20 13:16:22 +02001508 syntax_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001509# ifdef FEAT_TERMINAL
Bram Moolenaar34390282019-10-16 14:38:26 +02001510 if (get_term_attr)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001511 syntax_attr = term_get_attr(wp, lnum, vcol);
Bram Moolenaara74fda62019-10-19 17:38:03 +02001512# endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001513 // Get syntax attribute.
1514 if (has_syntax)
1515 {
1516 // Get the syntax attribute for the character. If there
1517 // is an error, disable syntax highlighting.
1518 save_did_emsg = did_emsg;
1519 did_emsg = FALSE;
1520
1521 v = (long)(ptr - line);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001522 if (v == prev_syntax_col)
1523 // at same column again
1524 syntax_attr = prev_syntax_attr;
1525 else
1526 {
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001527# ifdef FEAT_SPELL
Bram Moolenaar9115c612019-10-16 16:57:06 +02001528 can_spell = TRUE;
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001529# endif
Bram Moolenaar9115c612019-10-16 16:57:06 +02001530 syntax_attr = get_syntax_attr((colnr_T)v,
Bram Moolenaar34390282019-10-16 14:38:26 +02001531# ifdef FEAT_SPELL
1532 has_spell ? &can_spell :
1533# endif
1534 NULL, FALSE);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001535 prev_syntax_col = v;
1536 prev_syntax_attr = syntax_attr;
1537 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001538
Bram Moolenaar34390282019-10-16 14:38:26 +02001539 if (did_emsg)
1540 {
1541 wp->w_s->b_syn_error = TRUE;
1542 has_syntax = FALSE;
1543 syntax_attr = 0;
1544 }
1545 else
1546 did_emsg = save_did_emsg;
1547# ifdef SYN_TIME_LIMIT
1548 if (wp->w_s->b_syn_slow)
1549 has_syntax = FALSE;
1550# endif
1551
1552 // Need to get the line again, a multi-line regexp may
1553 // have made it invalid.
1554 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1555 ptr = line + v;
1556# ifdef FEAT_CONCEAL
1557 // no concealing past the end of the line, it interferes
1558 // with line highlighting
1559 if (*ptr == NUL)
1560 syntax_flags = 0;
1561 else
1562 syntax_flags = get_syntax_info(&syntax_seqnr);
1563# endif
1564 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001565 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001566# ifdef FEAT_PROP_POPUP
Bram Moolenaardbd43162019-11-09 21:28:14 +01001567 // Combine text property highlight into syntax highlight.
1568 if (text_prop_type != NULL)
1569 {
1570 if (text_prop_combine)
1571 syntax_attr = hl_combine_attr(syntax_attr, text_prop_attr);
1572 else
1573 syntax_attr = text_prop_attr;
1574 }
1575# endif
Bram Moolenaara74fda62019-10-19 17:38:03 +02001576#endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001577
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001578 // Decide which of the highlight attributes to use.
1579 attr_pri = TRUE;
1580#ifdef LINE_ATTR
1581 if (area_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001582 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001583 char_attr = hl_combine_attr(line_attr, area_attr);
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001584 if (!highlight_match)
1585 // let search highlight show in Visual area if possible
1586 char_attr = hl_combine_attr(search_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001587# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001588 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001589# endif
1590 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001591 else if (search_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001592 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001593 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001594# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001595 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001596# endif
1597 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001598 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
1599 || vcol < fromcol || vcol_prev < fromcol_prev
1600 || vcol >= tocol))
1601 {
1602 // Use line_attr when not in the Visual or 'incsearch' area
1603 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar34390282019-10-16 14:38:26 +02001604# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001605 char_attr = hl_combine_attr(syntax_attr, line_attr);
1606# else
1607 char_attr = line_attr;
Bram Moolenaar34390282019-10-16 14:38:26 +02001608# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001609 attr_pri = FALSE;
1610 }
1611#else
1612 if (area_attr != 0)
1613 char_attr = area_attr;
1614 else if (search_attr != 0)
1615 char_attr = search_attr;
1616#endif
1617 else
1618 {
1619 attr_pri = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001620#ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001621 char_attr = syntax_attr;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001622#else
Bram Moolenaardbd43162019-11-09 21:28:14 +01001623 char_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001624#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001625 }
1626 }
Bram Moolenaar024dbd22019-11-02 22:00:15 +01001627
1628 // combine attribute with 'wincolor'
1629 if (win_attr != 0)
1630 {
1631 if (char_attr == 0)
1632 char_attr = win_attr;
1633 else
1634 char_attr = hl_combine_attr(win_attr, char_attr);
1635 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001636
1637 // Get the next character to put on the screen.
1638
1639 // The "p_extra" points to the extra stuff that is inserted to
1640 // represent special characters (non-printable stuff) and other
1641 // things. When all characters are the same, c_extra is used.
1642 // If c_final is set, it will compulsorily be used at the end.
1643 // "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
1644 // "p_extra[n_extra]".
1645 // For the '$' of the 'list' option, n_extra == 1, p_extra == "".
1646 if (n_extra > 0)
1647 {
1648 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
1649 {
1650 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
1651 mb_c = c; // doesn't handle non-utf-8 multi-byte!
1652 if (enc_utf8 && utf_char2len(c) > 1)
1653 {
1654 mb_utf8 = TRUE;
1655 u8cc[0] = 0;
1656 c = 0xc0;
1657 }
1658 else
1659 mb_utf8 = FALSE;
1660 }
1661 else
1662 {
1663 c = *p_extra;
1664 if (has_mbyte)
1665 {
1666 mb_c = c;
1667 if (enc_utf8)
1668 {
1669 // If the UTF-8 character is more than one byte:
1670 // Decode it into "mb_c".
1671 mb_l = utfc_ptr2len(p_extra);
1672 mb_utf8 = FALSE;
1673 if (mb_l > n_extra)
1674 mb_l = 1;
1675 else if (mb_l > 1)
1676 {
1677 mb_c = utfc_ptr2char(p_extra, u8cc);
1678 mb_utf8 = TRUE;
1679 c = 0xc0;
1680 }
1681 }
1682 else
1683 {
1684 // if this is a DBCS character, put it in "mb_c"
1685 mb_l = MB_BYTE2LEN(c);
1686 if (mb_l >= n_extra)
1687 mb_l = 1;
1688 else if (mb_l > 1)
1689 mb_c = (c << 8) + p_extra[1];
1690 }
1691 if (mb_l == 0) // at the NUL at end-of-line
1692 mb_l = 1;
1693
1694 // If a double-width char doesn't fit display a '>' in the
1695 // last column.
1696 if ((
1697# ifdef FEAT_RIGHTLEFT
1698 wp->w_p_rl ? (col <= 0) :
1699# endif
1700 (col >= wp->w_width - 1))
1701 && (*mb_char2cells)(mb_c) == 2)
1702 {
1703 c = '>';
1704 mb_c = c;
1705 mb_l = 1;
1706 mb_utf8 = FALSE;
1707 multi_attr = HL_ATTR(HLF_AT);
1708#ifdef FEAT_SYN_HL
1709 if (cul_attr)
1710 multi_attr = hl_combine_attr(multi_attr, cul_attr);
1711#endif
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001712 multi_attr = hl_combine_attr(win_attr, multi_attr);
1713
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001714 // put the pointer back to output the double-width
1715 // character at the start of the next line.
1716 ++n_extra;
1717 --p_extra;
1718 }
1719 else
1720 {
1721 n_extra -= mb_l - 1;
1722 p_extra += mb_l - 1;
1723 }
1724 }
1725 ++p_extra;
1726 }
1727 --n_extra;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00001728#if defined(FEAT_LINEBREAK) && defined(FEAT_PROP_POPUP)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001729 if (n_extra <= 0)
1730 in_linebreak = FALSE;
1731#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001732 }
1733 else
1734 {
1735#ifdef FEAT_LINEBREAK
1736 int c0;
1737#endif
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001738 VIM_CLEAR(p_extra_free);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001739
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001740 // Get a character from the line itself.
1741 c = *ptr;
1742#ifdef FEAT_LINEBREAK
1743 c0 = *ptr;
1744#endif
1745 if (has_mbyte)
1746 {
1747 mb_c = c;
1748 if (enc_utf8)
1749 {
1750 // If the UTF-8 character is more than one byte: Decode it
1751 // into "mb_c".
1752 mb_l = utfc_ptr2len(ptr);
1753 mb_utf8 = FALSE;
1754 if (mb_l > 1)
1755 {
1756 mb_c = utfc_ptr2char(ptr, u8cc);
1757 // Overlong encoded ASCII or ASCII with composing char
1758 // is displayed normally, except a NUL.
1759 if (mb_c < 0x80)
1760 {
1761 c = mb_c;
1762#ifdef FEAT_LINEBREAK
1763 c0 = mb_c;
1764#endif
1765 }
1766 mb_utf8 = TRUE;
1767
1768 // At start of the line we can have a composing char.
1769 // Draw it as a space with a composing char.
1770 if (utf_iscomposing(mb_c))
1771 {
1772 int i;
1773
1774 for (i = Screen_mco - 1; i > 0; --i)
1775 u8cc[i] = u8cc[i - 1];
1776 u8cc[0] = mb_c;
1777 mb_c = ' ';
1778 }
1779 }
1780
1781 if ((mb_l == 1 && c >= 0x80)
1782 || (mb_l >= 1 && mb_c == 0)
1783 || (mb_l > 1 && (!vim_isprintc(mb_c))))
1784 {
1785 // Illegal UTF-8 byte: display as <xx>.
1786 // Non-BMP character : display as ? or fullwidth ?.
1787 transchar_hex(extra, mb_c);
1788# ifdef FEAT_RIGHTLEFT
1789 if (wp->w_p_rl) // reverse
1790 rl_mirror(extra);
1791# endif
1792 p_extra = extra;
1793 c = *p_extra;
1794 mb_c = mb_ptr2char_adv(&p_extra);
1795 mb_utf8 = (c >= 0x80);
1796 n_extra = (int)STRLEN(p_extra);
1797 c_extra = NUL;
1798 c_final = NUL;
1799 if (area_attr == 0 && search_attr == 0)
1800 {
1801 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001802 extra_attr = hl_combine_attr(
1803 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001804 saved_attr2 = char_attr; // save current attr
1805 }
1806 }
1807 else if (mb_l == 0) // at the NUL at end-of-line
1808 mb_l = 1;
1809#ifdef FEAT_ARABIC
1810 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
1811 {
1812 // Do Arabic shaping.
1813 int pc, pc1, nc;
1814 int pcc[MAX_MCO];
1815
1816 // The idea of what is the previous and next
1817 // character depends on 'rightleft'.
1818 if (wp->w_p_rl)
1819 {
1820 pc = prev_c;
1821 pc1 = prev_c1;
1822 nc = utf_ptr2char(ptr + mb_l);
1823 prev_c1 = u8cc[0];
1824 }
1825 else
1826 {
1827 pc = utfc_ptr2char(ptr + mb_l, pcc);
1828 nc = prev_c;
1829 pc1 = pcc[0];
1830 }
1831 prev_c = mb_c;
1832
1833 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
1834 }
1835 else
1836 prev_c = mb_c;
1837#endif
1838 }
1839 else // enc_dbcs
1840 {
1841 mb_l = MB_BYTE2LEN(c);
1842 if (mb_l == 0) // at the NUL at end-of-line
1843 mb_l = 1;
1844 else if (mb_l > 1)
1845 {
1846 // We assume a second byte below 32 is illegal.
1847 // Hopefully this is OK for all double-byte encodings!
1848 if (ptr[1] >= 32)
1849 mb_c = (c << 8) + ptr[1];
1850 else
1851 {
1852 if (ptr[1] == NUL)
1853 {
1854 // head byte at end of line
1855 mb_l = 1;
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001856 transchar_nonprint(wp->w_buffer, extra, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001857 }
1858 else
1859 {
1860 // illegal tail byte
1861 mb_l = 2;
1862 STRCPY(extra, "XX");
1863 }
1864 p_extra = extra;
1865 n_extra = (int)STRLEN(extra) - 1;
1866 c_extra = NUL;
1867 c_final = NUL;
1868 c = *p_extra++;
1869 if (area_attr == 0 && search_attr == 0)
1870 {
1871 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001872 extra_attr = hl_combine_attr(
1873 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001874 saved_attr2 = char_attr; // save current attr
1875 }
1876 mb_c = c;
1877 }
1878 }
1879 }
1880 // If a double-width char doesn't fit display a '>' in the
1881 // last column; the character is displayed at the start of the
1882 // next line.
1883 if ((
1884# ifdef FEAT_RIGHTLEFT
1885 wp->w_p_rl ? (col <= 0) :
1886# endif
1887 (col >= wp->w_width - 1))
1888 && (*mb_char2cells)(mb_c) == 2)
1889 {
1890 c = '>';
1891 mb_c = c;
1892 mb_utf8 = FALSE;
1893 mb_l = 1;
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001894 multi_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001895 // Put pointer back so that the character will be
1896 // displayed at the start of the next line.
1897 --ptr;
1898#ifdef FEAT_CONCEAL
1899 did_decrement_ptr = TRUE;
1900#endif
1901 }
1902 else if (*ptr != NUL)
1903 ptr += mb_l - 1;
1904
1905 // If a double-width char doesn't fit at the left side display
1906 // a '<' in the first column. Don't do this for unprintable
1907 // characters.
1908 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
1909 {
1910 n_extra = 1;
1911 c_extra = MB_FILLER_CHAR;
1912 c_final = NUL;
1913 c = ' ';
1914 if (area_attr == 0 && search_attr == 0)
1915 {
1916 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001917 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001918 saved_attr2 = char_attr; // save current attr
1919 }
1920 mb_c = c;
1921 mb_utf8 = FALSE;
1922 mb_l = 1;
1923 }
1924
1925 }
1926 ++ptr;
1927
1928 if (extra_check)
1929 {
1930#ifdef FEAT_SPELL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001931 // Check spelling (unless at the end of the line).
1932 // Only do this when there is no syntax highlighting, the
1933 // @Spell cluster is not used or the current syntax item
1934 // contains the @Spell cluster.
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001935 v = (long)(ptr - line);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001936 if (has_spell && v >= word_end && v > cur_checked_col)
1937 {
1938 spell_attr = 0;
1939 if (c != 0 && (
1940# ifdef FEAT_SYN_HL
1941 !has_syntax ||
1942# endif
1943 can_spell))
1944 {
1945 char_u *prev_ptr, *p;
1946 int len;
1947 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001948
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001949 if (has_mbyte)
1950 {
1951 prev_ptr = ptr - mb_l;
1952 v -= mb_l - 1;
1953 }
1954 else
1955 prev_ptr = ptr - 1;
1956
1957 // Use nextline[] if possible, it has the start of the
1958 // next line concatenated.
1959 if ((prev_ptr - line) - nextlinecol >= 0)
1960 p = nextline + (prev_ptr - line) - nextlinecol;
1961 else
1962 p = prev_ptr;
1963 cap_col -= (int)(prev_ptr - line);
1964 len = spell_check(wp, p, &spell_hlf, &cap_col,
1965 nochange);
1966 word_end = v + len;
1967
1968 // In Insert mode only highlight a word that
1969 // doesn't touch the cursor.
1970 if (spell_hlf != HLF_COUNT
Bram Moolenaar24959102022-05-07 20:01:16 +01001971 && (State & MODE_INSERT)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001972 && wp->w_cursor.lnum == lnum
1973 && wp->w_cursor.col >=
1974 (colnr_T)(prev_ptr - line)
1975 && wp->w_cursor.col < (colnr_T)word_end)
1976 {
1977 spell_hlf = HLF_COUNT;
1978 spell_redraw_lnum = lnum;
1979 }
1980
1981 if (spell_hlf == HLF_COUNT && p != prev_ptr
1982 && (p - nextline) + len > nextline_idx)
1983 {
1984 // Remember that the good word continues at the
1985 // start of the next line.
1986 checked_lnum = lnum + 1;
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001987 checked_col = (int)((p - nextline)
1988 + len - nextline_idx);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001989 }
1990
1991 // Turn index into actual attributes.
1992 if (spell_hlf != HLF_COUNT)
1993 spell_attr = highlight_attr[spell_hlf];
1994
1995 if (cap_col > 0)
1996 {
1997 if (p != prev_ptr
1998 && (p - nextline) + cap_col >= nextline_idx)
1999 {
2000 // Remember that the word in the next line
2001 // must start with a capital.
2002 capcol_lnum = lnum + 1;
2003 cap_col = (int)((p - nextline) + cap_col
2004 - nextline_idx);
2005 }
2006 else
2007 // Compute the actual column.
2008 cap_col += (int)(prev_ptr - line);
2009 }
2010 }
2011 }
2012 if (spell_attr != 0)
2013 {
2014 if (!attr_pri)
2015 char_attr = hl_combine_attr(char_attr, spell_attr);
2016 else
2017 char_attr = hl_combine_attr(spell_attr, char_attr);
2018 }
2019#endif
2020#ifdef FEAT_LINEBREAK
2021 // Found last space before word: check for line break.
2022 if (wp->w_p_lbr && c0 == c
2023 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
2024 {
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002025 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1)
2026 : 0;
2027 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002028
2029 // TODO: is passing p for start of the line OK?
2030 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
2031 NULL) - 1;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002032
2033 // We have just drawn the showbreak value, no need to add
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002034 // space for it again.
Bram Moolenaara06e3452021-05-29 17:56:37 +02002035 if (vcol == vcol_sbr)
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002036 {
Bram Moolenaara06e3452021-05-29 17:56:37 +02002037 n_extra -= MB_CHARLEN(get_showbreak_value(wp));
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002038 if (n_extra < 0)
2039 n_extra = 0;
2040 }
Bram Moolenaar0bbca542022-01-11 13:14:54 +00002041 if (on_last_col && c != TAB)
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002042 // Do not continue search/match highlighting over the
Bram Moolenaar0bbca542022-01-11 13:14:54 +00002043 // line break, but for TABs the highlighting should
2044 // include the complete width of the character
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002045 search_attr = 0;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002046
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002047 if (c == TAB && n_extra + col > wp->w_width)
2048# ifdef FEAT_VARTABS
2049 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
2050 wp->w_buffer->b_p_vts_array) - 1;
2051# else
2052 n_extra = (int)wp->w_buffer->b_p_ts
2053 - vcol % (int)wp->w_buffer->b_p_ts - 1;
2054# endif
2055
2056 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
2057 c_final = NUL;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002058# if defined(FEAT_PROP_POPUP)
Bram Moolenaar42eba042021-11-30 20:22:49 +00002059 if (n_extra > 0 && c != TAB)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00002060 in_linebreak = TRUE;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002061# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002062 if (VIM_ISWHITE(c))
2063 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002064# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002065 if (c == TAB)
2066 // See "Tab alignment" below.
2067 FIX_FOR_BOGUSCOLS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002068# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002069 if (!wp->w_p_list)
2070 c = ' ';
2071 }
2072 }
2073#endif
2074
zeertzjqf14b8ba2021-09-10 16:58:30 +02002075 in_multispace = c == ' '
2076 && ((ptr > line + 1 && ptr[-2] == ' ') || *ptr == ' ');
2077 if (!in_multispace)
2078 multispace_pos = 0;
2079
Bram Moolenaareed9d462021-02-15 20:38:25 +01002080 // 'list': Change char 160 to 'nbsp' and space to 'space'
2081 // setting in 'listchars'. But not when the character is
2082 // followed by a composing character (use mb_l to check that).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002083 if (wp->w_p_list
2084 && ((((c == 160 && mb_l == 1)
2085 || (mb_utf8
2086 && ((mb_c == 160 && mb_l == 2)
2087 || (mb_c == 0x202f && mb_l == 3))))
Bram Moolenaareed9d462021-02-15 20:38:25 +01002088 && wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002089 || (c == ' '
2090 && mb_l == 1
zeertzjqf14b8ba2021-09-10 16:58:30 +02002091 && (wp->w_lcs_chars.space
2092 || (in_multispace
2093 && wp->w_lcs_chars.multispace != NULL))
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002094 && ptr - line >= leadcol
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002095 && ptr - line <= trailcol)))
2096 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02002097 if (in_multispace && wp->w_lcs_chars.multispace != NULL)
2098 {
2099 c = wp->w_lcs_chars.multispace[multispace_pos++];
2100 if (wp->w_lcs_chars.multispace[multispace_pos] == NUL)
2101 multispace_pos = 0;
2102 }
2103 else
2104 c = (c == ' ') ? wp->w_lcs_chars.space
2105 : wp->w_lcs_chars.nbsp;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002106 if (area_attr == 0 && search_attr == 0)
2107 {
2108 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002109 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002110 saved_attr2 = char_attr; // save current attr
2111 }
2112 mb_c = c;
2113 if (enc_utf8 && utf_char2len(c) > 1)
2114 {
2115 mb_utf8 = TRUE;
2116 u8cc[0] = 0;
2117 c = 0xc0;
2118 }
2119 else
2120 mb_utf8 = FALSE;
2121 }
2122
zeertzjq2e7cba32022-06-10 15:30:32 +01002123 if (c == ' ' && ((trailcol != MAXCOL && ptr > line + trailcol)
2124 || (leadcol != 0 && ptr < line + leadcol)))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002125 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002126 if (leadcol != 0 && in_multispace && ptr < line + leadcol
2127 && wp->w_lcs_chars.leadmultispace != NULL)
2128 {
2129 c = wp->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjq2e7cba32022-06-10 15:30:32 +01002130 if (wp->w_lcs_chars.leadmultispace[multispace_pos]
2131 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002132 multispace_pos = 0;
2133 }
2134
2135 else if (ptr > line + trailcol && wp->w_lcs_chars.trail)
2136 c = wp->w_lcs_chars.trail;
2137
2138 else if (ptr < line + leadcol && wp->w_lcs_chars.lead)
2139 c = wp->w_lcs_chars.lead;
2140
zeertzjq2e7cba32022-06-10 15:30:32 +01002141 else if (leadcol != 0 && wp->w_lcs_chars.space)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002142 c = wp->w_lcs_chars.space;
2143
2144
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002145 if (!attr_pri)
2146 {
2147 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002148 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002149 saved_attr2 = char_attr; // save current attr
2150 }
2151 mb_c = c;
2152 if (enc_utf8 && utf_char2len(c) > 1)
2153 {
2154 mb_utf8 = TRUE;
2155 u8cc[0] = 0;
2156 c = 0xc0;
2157 }
2158 else
2159 mb_utf8 = FALSE;
2160 }
2161 }
2162
2163 // Handling of non-printable characters.
2164 if (!vim_isprintc(c))
2165 {
2166 // when getting a character from the file, we may have to
2167 // turn it into something else on the way to putting it
2168 // into "ScreenLines".
Bram Moolenaareed9d462021-02-15 20:38:25 +01002169 if (c == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002170 {
2171 int tab_len = 0;
2172 long vcol_adjusted = vcol; // removed showbreak length
2173#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01002174 char_u *sbr = get_showbreak_value(wp);
2175
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002176 // only adjust the tab_len, when at the first column
2177 // after the showbreak value was drawn
Bram Moolenaaree857022019-11-09 23:26:40 +01002178 if (*sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
2179 vcol_adjusted = vcol - MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002180#endif
2181 // tab amount depends on current column
2182#ifdef FEAT_VARTABS
2183 tab_len = tabstop_padding(vcol_adjusted,
2184 wp->w_buffer->b_p_ts,
2185 wp->w_buffer->b_p_vts_array) - 1;
2186#else
2187 tab_len = (int)wp->w_buffer->b_p_ts
2188 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
2189#endif
2190
2191#ifdef FEAT_LINEBREAK
2192 if (!wp->w_p_lbr || !wp->w_p_list)
2193#endif
2194 // tab amount depends on current column
2195 n_extra = tab_len;
2196#ifdef FEAT_LINEBREAK
2197 else
2198 {
2199 char_u *p;
2200 int len;
2201 int i;
2202 int saved_nextra = n_extra;
2203
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002204# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002205 if (vcol_off > 0)
2206 // there are characters to conceal
2207 tab_len += vcol_off;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002208
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002209 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaareed9d462021-02-15 20:38:25 +01002210 if (wp->w_p_list && wp->w_lcs_chars.tab1
2211 && old_boguscols > 0
2212 && n_extra > tab_len)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002213 tab_len += n_extra - tab_len;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002214# endif
2215 // If n_extra > 0, it gives the number of chars, to
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002216 // use for a tab, else we need to calculate the width
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002217 // for a tab.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002218 len = (tab_len * mb_char2len(wp->w_lcs_chars.tab2));
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002219 if (wp->w_lcs_chars.tab3)
2220 len += mb_char2len(wp->w_lcs_chars.tab3);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002221 if (n_extra > 0)
2222 len += n_extra - tab_len;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002223 c = wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002224 p = alloc(len + 1);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002225 if (p == NULL)
2226 n_extra = 0;
2227 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002228 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002229 vim_memset(p, ' ', len);
2230 p[len] = NUL;
2231 vim_free(p_extra_free);
2232 p_extra_free = p;
2233 for (i = 0; i < tab_len; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002234 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002235 int lcs = wp->w_lcs_chars.tab2;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002236
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002237 if (*p == NUL)
2238 {
2239 tab_len = i;
2240 break;
2241 }
2242
2243 // if tab3 is given, use it for the last char
2244 if (wp->w_lcs_chars.tab3 && i == tab_len - 1)
2245 lcs = wp->w_lcs_chars.tab3;
2246 p += mb_char2bytes(lcs, p);
2247 n_extra += mb_char2len(lcs)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002248 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002249 }
2250 p_extra = p_extra_free;
2251# ifdef FEAT_CONCEAL
2252 // n_extra will be increased by FIX_FOX_BOGUSCOLS
2253 // macro below, so need to adjust for that here
2254 if (vcol_off > 0)
2255 n_extra -= vcol_off;
2256# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002257 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002258 }
2259#endif
2260#ifdef FEAT_CONCEAL
2261 {
2262 int vc_saved = vcol_off;
2263
2264 // Tab alignment should be identical regardless of
2265 // 'conceallevel' value. So tab compensates of all
2266 // previous concealed characters, and thus resets
2267 // vcol_off and boguscols accumulated so far in the
2268 // line. Note that the tab can be longer than
2269 // 'tabstop' when there are concealed characters.
2270 FIX_FOR_BOGUSCOLS;
2271
2272 // Make sure, the highlighting for the tab char will be
2273 // correctly set further below (effectively reverts the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002274 // FIX_FOR_BOGSUCOLS macro).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002275 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaareed9d462021-02-15 20:38:25 +01002276 && wp->w_lcs_chars.tab1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002277 tab_len += vc_saved;
2278 }
2279#endif
2280 mb_utf8 = FALSE; // don't draw as UTF-8
2281 if (wp->w_p_list)
2282 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002283 c = (n_extra == 0 && wp->w_lcs_chars.tab3)
2284 ? wp->w_lcs_chars.tab3
2285 : wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002286#ifdef FEAT_LINEBREAK
2287 if (wp->w_p_lbr)
2288 c_extra = NUL; // using p_extra from above
2289 else
2290#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01002291 c_extra = wp->w_lcs_chars.tab2;
2292 c_final = wp->w_lcs_chars.tab3;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002293 n_attr = tab_len + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002294 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002295 saved_attr2 = char_attr; // save current attr
2296 mb_c = c;
2297 if (enc_utf8 && utf_char2len(c) > 1)
2298 {
2299 mb_utf8 = TRUE;
2300 u8cc[0] = 0;
2301 c = 0xc0;
2302 }
2303 }
2304 else
2305 {
2306 c_final = NUL;
2307 c_extra = ' ';
2308 c = ' ';
2309 }
2310 }
2311 else if (c == NUL
2312 && (wp->w_p_list
2313 || ((fromcol >= 0 || fromcol_prev >= 0)
2314 && tocol > vcol
2315 && VIsual_mode != Ctrl_V
2316 && (
2317# ifdef FEAT_RIGHTLEFT
2318 wp->w_p_rl ? (col >= 0) :
2319# endif
2320 (col < wp->w_width))
2321 && !(noinvcur
2322 && lnum == wp->w_cursor.lnum
2323 && (colnr_T)vcol == wp->w_virtcol)))
2324 && lcs_eol_one > 0)
2325 {
2326 // Display a '$' after the line or highlight an extra
2327 // character if the line break is included.
2328#if defined(FEAT_DIFF) || defined(LINE_ATTR)
2329 // For a diff line the highlighting continues after the
2330 // "$".
2331 if (
2332# ifdef FEAT_DIFF
2333 diff_hlf == (hlf_T)0
2334# ifdef LINE_ATTR
2335 &&
2336# endif
2337# endif
2338# ifdef LINE_ATTR
2339 line_attr == 0
2340# endif
2341 )
2342#endif
2343 {
2344 // In virtualedit, visual selections may extend
2345 // beyond end of line.
2346 if (area_highlighting && virtual_active()
2347 && tocol != MAXCOL && vcol < tocol)
2348 n_extra = 0;
2349 else
2350 {
2351 p_extra = at_end_str;
2352 n_extra = 1;
2353 c_extra = NUL;
2354 c_final = NUL;
2355 }
2356 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002357 if (wp->w_p_list && wp->w_lcs_chars.eol > 0)
2358 c = wp->w_lcs_chars.eol;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002359 else
2360 c = ' ';
2361 lcs_eol_one = -1;
2362 --ptr; // put it back at the NUL
2363 if (!attr_pri)
2364 {
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002365 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002366 n_attr = 1;
2367 }
2368 mb_c = c;
2369 if (enc_utf8 && utf_char2len(c) > 1)
2370 {
2371 mb_utf8 = TRUE;
2372 u8cc[0] = 0;
2373 c = 0xc0;
2374 }
2375 else
2376 mb_utf8 = FALSE; // don't draw as UTF-8
2377 }
2378 else if (c != NUL)
2379 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02002380 p_extra = transchar_buf(wp->w_buffer, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002381 if (n_extra == 0)
2382 n_extra = byte2cells(c) - 1;
2383#ifdef FEAT_RIGHTLEFT
2384 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
2385 rl_mirror(p_extra); // reverse "<12>"
2386#endif
2387 c_extra = NUL;
2388 c_final = NUL;
2389#ifdef FEAT_LINEBREAK
2390 if (wp->w_p_lbr)
2391 {
2392 char_u *p;
2393
2394 c = *p_extra;
2395 p = alloc(n_extra + 1);
2396 vim_memset(p, ' ', n_extra);
2397 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
2398 p[n_extra] = NUL;
2399 vim_free(p_extra_free);
2400 p_extra_free = p_extra = p;
2401 }
2402 else
2403#endif
2404 {
2405 n_extra = byte2cells(c) - 1;
2406 c = *p_extra++;
2407 }
2408 if (!attr_pri)
2409 {
2410 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002411 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002412 saved_attr2 = char_attr; // save current attr
2413 }
2414 mb_utf8 = FALSE; // don't draw as UTF-8
2415 }
2416 else if (VIsual_active
2417 && (VIsual_mode == Ctrl_V
2418 || VIsual_mode == 'v')
2419 && virtual_active()
2420 && tocol != MAXCOL
2421 && vcol < tocol
2422 && (
2423#ifdef FEAT_RIGHTLEFT
2424 wp->w_p_rl ? (col >= 0) :
2425#endif
2426 (col < wp->w_width)))
2427 {
2428 c = ' ';
2429 --ptr; // put it back at the NUL
2430 }
2431#if defined(LINE_ATTR)
2432 else if ((
2433# ifdef FEAT_DIFF
2434 diff_hlf != (hlf_T)0 ||
2435# endif
2436# ifdef FEAT_TERMINAL
2437 win_attr != 0 ||
2438# endif
2439 line_attr != 0
2440 ) && (
2441# ifdef FEAT_RIGHTLEFT
2442 wp->w_p_rl ? (col >= 0) :
2443# endif
2444 (col
2445# ifdef FEAT_CONCEAL
2446 - boguscols
2447# endif
2448 < wp->w_width)))
2449 {
2450 // Highlight until the right side of the window
2451 c = ' ';
2452 --ptr; // put it back at the NUL
2453
2454 // Remember we do the char for line highlighting.
2455 ++did_line_attr;
2456
2457 // don't do search HL for the rest of the line
2458 if (line_attr != 0 && char_attr == search_attr
2459 && (did_line_attr > 1
Bram Moolenaareed9d462021-02-15 20:38:25 +01002460 || (wp->w_p_list &&
2461 wp->w_lcs_chars.eol > 0)))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002462 char_attr = line_attr;
2463# ifdef FEAT_DIFF
2464 if (diff_hlf == HLF_TXD)
2465 {
2466 diff_hlf = HLF_CHD;
2467 if (vi_attr == 0 || char_attr != vi_attr)
2468 {
2469 char_attr = HL_ATTR(diff_hlf);
2470 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2471 && wp->w_p_culopt_flags != CULOPT_NBR
2472 && (!cul_screenline
2473 || (vcol >= left_curline_col
2474 && vcol <= right_curline_col)))
2475 char_attr = hl_combine_attr(
2476 char_attr, HL_ATTR(HLF_CUL));
2477 }
2478 }
2479# endif
2480# ifdef FEAT_TERMINAL
2481 if (win_attr != 0)
2482 {
2483 char_attr = win_attr;
Bram Moolenaara3817732019-10-16 16:31:44 +02002484 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2485 && wp->w_p_culopt_flags != CULOPT_NBR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002486 {
2487 if (!cul_screenline || (vcol >= left_curline_col
2488 && vcol <= right_curline_col))
2489 char_attr = hl_combine_attr(
2490 char_attr, HL_ATTR(HLF_CUL));
2491 }
2492 else if (line_attr)
2493 char_attr = hl_combine_attr(char_attr, line_attr);
2494 }
2495# endif
2496 }
2497#endif
2498 }
2499
2500#ifdef FEAT_CONCEAL
2501 if ( wp->w_p_cole > 0
LemonBoy9830db62022-05-08 21:25:20 +01002502 && (wp != curwin || lnum != wp->w_cursor.lnum
2503 || conceal_cursor_line(wp))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002504 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
2505 && !(lnum_in_visual_area
2506 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
2507 {
2508 char_attr = conceal_attr;
LemonBoy9830db62022-05-08 21:25:20 +01002509 if (((prev_syntax_id != syntax_seqnr
2510 && (syntax_flags & HL_CONCEAL) != 0)
2511 || has_match_conc > 1)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002512 && (syn_get_sub_char() != NUL
2513 || (has_match_conc && match_conc)
2514 || wp->w_p_cole == 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002515 && wp->w_p_cole != 3)
2516 {
2517 // First time at this concealed item: display one
2518 // character.
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002519 if (has_match_conc && match_conc)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002520 c = match_conc;
2521 else if (syn_get_sub_char() != NUL)
2522 c = syn_get_sub_char();
Bram Moolenaareed9d462021-02-15 20:38:25 +01002523 else if (wp->w_lcs_chars.conceal != NUL)
2524 c = wp->w_lcs_chars.conceal;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002525 else
2526 c = ' ';
2527
2528 prev_syntax_id = syntax_seqnr;
2529
2530 if (n_extra > 0)
2531 vcol_off += n_extra;
2532 vcol += n_extra;
2533 if (wp->w_p_wrap && n_extra > 0)
2534 {
2535# ifdef FEAT_RIGHTLEFT
2536 if (wp->w_p_rl)
2537 {
2538 col -= n_extra;
2539 boguscols -= n_extra;
2540 }
2541 else
2542# endif
2543 {
2544 boguscols += n_extra;
2545 col += n_extra;
2546 }
2547 }
2548 n_extra = 0;
2549 n_attr = 0;
2550 }
2551 else if (n_skip == 0)
2552 {
2553 is_concealing = TRUE;
2554 n_skip = 1;
2555 }
2556 mb_c = c;
2557 if (enc_utf8 && utf_char2len(c) > 1)
2558 {
2559 mb_utf8 = TRUE;
2560 u8cc[0] = 0;
2561 c = 0xc0;
2562 }
2563 else
2564 mb_utf8 = FALSE; // don't draw as UTF-8
2565 }
2566 else
2567 {
2568 prev_syntax_id = 0;
2569 is_concealing = FALSE;
2570 }
2571
2572 if (n_skip > 0 && did_decrement_ptr)
2573 // not showing the '>', put pointer back to avoid getting stuck
2574 ++ptr;
2575
2576#endif // FEAT_CONCEAL
2577 }
2578
2579#ifdef FEAT_CONCEAL
2580 // In the cursor line and we may be concealing characters: correct
2581 // the cursor column when we reach its position.
2582 if (!did_wcol && draw_state == WL_LINE
2583 && wp == curwin && lnum == wp->w_cursor.lnum
2584 && conceal_cursor_line(wp)
2585 && (int)wp->w_virtcol <= vcol + n_skip)
2586 {
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002587# ifdef FEAT_RIGHTLEFT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002588 if (wp->w_p_rl)
2589 wp->w_wcol = wp->w_width - col + boguscols - 1;
2590 else
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002591# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002592 wp->w_wcol = col - boguscols;
2593 wp->w_wrow = row;
2594 did_wcol = TRUE;
2595 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002596# ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +01002597 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002598# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002599 }
2600#endif
2601
2602 // Don't override visual selection highlighting.
2603 if (n_attr > 0
2604 && draw_state == WL_LINE
2605 && !attr_pri)
2606 {
2607#ifdef LINE_ATTR
2608 if (line_attr)
2609 char_attr = hl_combine_attr(extra_attr, line_attr);
2610 else
2611#endif
2612 char_attr = extra_attr;
2613 }
2614
2615#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2616 // XIM don't send preedit_start and preedit_end, but they send
2617 // preedit_changed and commit. Thus Vim can't set "im_is_active", use
2618 // im_is_preediting() here.
2619 if (p_imst == IM_ON_THE_SPOT
2620 && xic != NULL
2621 && lnum == wp->w_cursor.lnum
Bram Moolenaar24959102022-05-07 20:01:16 +01002622 && (State & MODE_INSERT)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002623 && !p_imdisable
2624 && im_is_preediting()
2625 && draw_state == WL_LINE)
2626 {
2627 colnr_T tcol;
2628
2629 if (preedit_end_col == MAXCOL)
2630 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
2631 else
2632 tcol = preedit_end_col;
2633 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
2634 {
2635 if (feedback_old_attr < 0)
2636 {
2637 feedback_col = 0;
2638 feedback_old_attr = char_attr;
2639 }
2640 char_attr = im_get_feedback_attr(feedback_col);
2641 if (char_attr < 0)
2642 char_attr = feedback_old_attr;
2643 feedback_col++;
2644 }
2645 else if (feedback_old_attr >= 0)
2646 {
2647 char_attr = feedback_old_attr;
2648 feedback_old_attr = -1;
2649 feedback_col = 0;
2650 }
2651 }
2652#endif
2653 // Handle the case where we are in column 0 but not on the first
2654 // character of the line and the user wants us to show us a
2655 // special character (via 'listchars' option "precedes:<char>".
2656 if (lcs_prec_todo != NUL
2657 && wp->w_p_list
Bram Moolenaarbffba7f2019-09-20 17:00:17 +02002658 && (wp->w_p_wrap ?
2659 (wp->w_skipcol > 0 && row == 0) :
2660 wp->w_leftcol > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002661#ifdef FEAT_DIFF
2662 && filler_todo <= 0
2663#endif
2664 && draw_state > WL_NR
2665 && c != NUL)
2666 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002667 c = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002668 lcs_prec_todo = NUL;
2669 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2670 {
2671 // Double-width character being overwritten by the "precedes"
2672 // character, need to fill up half the character.
2673 c_extra = MB_FILLER_CHAR;
2674 c_final = NUL;
2675 n_extra = 1;
2676 n_attr = 2;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002677 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002678 }
2679 mb_c = c;
2680 if (enc_utf8 && utf_char2len(c) > 1)
2681 {
2682 mb_utf8 = TRUE;
2683 u8cc[0] = 0;
2684 c = 0xc0;
2685 }
2686 else
2687 mb_utf8 = FALSE; // don't draw as UTF-8
2688 if (!attr_pri)
2689 {
2690 saved_attr3 = char_attr; // save current attr
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002691 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002692 n_attr3 = 1;
2693 }
2694 }
2695
2696 // At end of the text line or just after the last character.
2697 if ((c == NUL
2698#if defined(LINE_ATTR)
2699 || did_line_attr == 1
2700#endif
2701 ) && eol_hl_off == 0)
2702 {
2703#ifdef FEAT_SEARCH_EXTRA
2704 // flag to indicate whether prevcol equals startcol of search_hl or
2705 // one of the matches
2706 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &screen_search_hl,
2707 (long)(ptr - line) - (c == NUL));
2708#endif
2709 // Invert at least one char, used for Visual and empty line or
2710 // highlight match at end of line. If it's beyond the last
2711 // char on the screen, just overwrite that one (tricky!) Not
2712 // needed when a '$' was displayed for 'list'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002713 if (wp->w_lcs_chars.eol == lcs_eol_one
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002714 && ((area_attr != 0 && vcol == fromcol
2715 && (VIsual_mode != Ctrl_V
2716 || lnum == VIsual.lnum
2717 || lnum == curwin->w_cursor.lnum)
2718 && c == NUL)
2719#ifdef FEAT_SEARCH_EXTRA
2720 // highlight 'hlsearch' match at end of line
2721 || (prevcol_hl_flag
2722# ifdef FEAT_SYN_HL
2723 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
2724 && !(wp == curwin && VIsual_active))
2725# endif
2726# ifdef FEAT_DIFF
2727 && diff_hlf == (hlf_T)0
2728# endif
2729# if defined(LINE_ATTR)
2730 && did_line_attr <= 1
2731# endif
2732 )
2733#endif
2734 ))
2735 {
2736 int n = 0;
2737
2738#ifdef FEAT_RIGHTLEFT
2739 if (wp->w_p_rl)
2740 {
2741 if (col < 0)
2742 n = 1;
2743 }
2744 else
2745#endif
2746 {
2747 if (col >= wp->w_width)
2748 n = -1;
2749 }
2750 if (n != 0)
2751 {
2752 // At the window boundary, highlight the last character
2753 // instead (better than nothing).
2754 off += n;
2755 col += n;
2756 }
2757 else
2758 {
2759 // Add a blank character to highlight.
2760 ScreenLines[off] = ' ';
2761 if (enc_utf8)
2762 ScreenLinesUC[off] = 0;
2763 }
2764#ifdef FEAT_SEARCH_EXTRA
2765 if (area_attr == 0)
2766 {
2767 // Use attributes from match with highest priority among
2768 // 'search_hl' and the match list.
2769 get_search_match_hl(wp, &screen_search_hl,
2770 (long)(ptr - line), &char_attr);
2771 }
2772#endif
2773 ScreenAttrs[off] = char_attr;
2774#ifdef FEAT_RIGHTLEFT
2775 if (wp->w_p_rl)
2776 {
2777 --col;
2778 --off;
2779 }
2780 else
2781#endif
2782 {
2783 ++col;
2784 ++off;
2785 }
2786 ++vcol;
2787 eol_hl_off = 1;
2788 }
2789 }
2790
2791 // At end of the text line.
2792 if (c == NUL)
2793 {
2794#ifdef FEAT_SYN_HL
2795 // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
2796 if (wp->w_p_wrap)
2797 v = wp->w_skipcol;
2798 else
2799 v = wp->w_leftcol;
2800
2801 // check if line ends before left margin
2802 if (vcol < v + col - win_col_off(wp))
2803 vcol = v + col - win_col_off(wp);
2804#ifdef FEAT_CONCEAL
2805 // Get rid of the boguscols now, we want to draw until the right
2806 // edge for 'cursorcolumn'.
2807 col -= boguscols;
2808 boguscols = 0;
2809#endif
2810
2811 if (draw_color_col)
2812 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2813
2814 if (((wp->w_p_cuc
2815 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
2816 && (int)wp->w_virtcol <
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002817 (long)wp->w_width * (row - startrow + 1) + v
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002818 && lnum != wp->w_cursor.lnum)
2819 || draw_color_col
2820 || win_attr != 0)
2821# ifdef FEAT_RIGHTLEFT
2822 && !wp->w_p_rl
2823# endif
2824 )
2825 {
2826 int rightmost_vcol = 0;
2827 int i;
2828
2829 if (wp->w_p_cuc)
2830 rightmost_vcol = wp->w_virtcol;
2831 if (draw_color_col)
2832 // determine rightmost colorcolumn to possibly draw
2833 for (i = 0; color_cols[i] >= 0; ++i)
2834 if (rightmost_vcol < color_cols[i])
2835 rightmost_vcol = color_cols[i];
2836
2837 while (col < wp->w_width)
2838 {
2839 ScreenLines[off] = ' ';
2840 if (enc_utf8)
2841 ScreenLinesUC[off] = 0;
2842 ++col;
2843 if (draw_color_col)
2844 draw_color_col = advance_color_col(VCOL_HLC,
2845 &color_cols);
2846
2847 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
2848 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
2849 else if (draw_color_col && VCOL_HLC == *color_cols)
2850 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
2851 else
2852 ScreenAttrs[off++] = win_attr;
2853
2854 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
2855 break;
2856
2857 ++vcol;
2858 }
2859 }
2860#endif
2861
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002862 screen_line(wp, screen_row, wp->w_wincol, col,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002863 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002864 row++;
2865
2866 // Update w_cline_height and w_cline_folded if the cursor line was
2867 // updated (saves a call to plines() later).
2868 if (wp == curwin && lnum == curwin->w_cursor.lnum)
2869 {
2870 curwin->w_cline_row = startrow;
2871 curwin->w_cline_height = row - startrow;
2872#ifdef FEAT_FOLDING
2873 curwin->w_cline_folded = FALSE;
2874#endif
2875 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2876 }
2877
2878 break;
2879 }
2880
2881 // Show "extends" character from 'listchars' if beyond the line end and
2882 // 'list' is set.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002883 if (wp->w_lcs_chars.ext != NUL
Bram Moolenaar41fb7232021-07-08 12:40:05 +02002884 && draw_state == WL_LINE
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002885 && wp->w_p_list
2886 && !wp->w_p_wrap
2887#ifdef FEAT_DIFF
2888 && filler_todo <= 0
2889#endif
2890 && (
2891#ifdef FEAT_RIGHTLEFT
2892 wp->w_p_rl ? col == 0 :
2893#endif
2894 col == wp->w_width - 1)
2895 && (*ptr != NUL
2896 || (wp->w_p_list && lcs_eol_one > 0)
2897 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
2898 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002899 c = wp->w_lcs_chars.ext;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002900 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002901 mb_c = c;
2902 if (enc_utf8 && utf_char2len(c) > 1)
2903 {
2904 mb_utf8 = TRUE;
2905 u8cc[0] = 0;
2906 c = 0xc0;
2907 }
2908 else
2909 mb_utf8 = FALSE;
2910 }
2911
2912#ifdef FEAT_SYN_HL
2913 // advance to the next 'colorcolumn'
2914 if (draw_color_col)
2915 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2916
2917 // Highlight the cursor column if 'cursorcolumn' is set. But don't
2918 // highlight the cursor position itself.
2919 // Also highlight the 'colorcolumn' if it is different than
2920 // 'cursorcolumn'
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002921 // Also highlight the 'colorcolumn' if 'breakindent' and/or 'showbreak'
2922 // options are set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002923 vcol_save_attr = -1;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002924 if (((draw_state == WL_LINE ||
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002925 draw_state == WL_BRI ||
2926 draw_state == WL_SBR) && !lnum_in_visual_area
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002927 && search_attr == 0 && area_attr == 0)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002928# ifdef FEAT_DIFF
2929 && filler_todo <= 0
2930# endif
2931 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002932 {
2933 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
2934 && lnum != wp->w_cursor.lnum)
2935 {
2936 vcol_save_attr = char_attr;
2937 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
2938 }
2939 else if (draw_color_col && VCOL_HLC == *color_cols)
2940 {
2941 vcol_save_attr = char_attr;
2942 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
2943 }
2944 }
2945#endif
2946
2947 // Store character to be displayed.
2948 // Skip characters that are left of the screen for 'nowrap'.
2949 vcol_prev = vcol;
2950 if (draw_state < WL_LINE || n_skip <= 0)
2951 {
2952 // Store the character.
2953#if defined(FEAT_RIGHTLEFT)
2954 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
2955 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002956 // A double-wide character is: put first half in left cell.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002957 --off;
2958 --col;
2959 }
2960#endif
2961 ScreenLines[off] = c;
2962 if (enc_dbcs == DBCS_JPNU)
2963 {
2964 if ((mb_c & 0xff00) == 0x8e00)
2965 ScreenLines[off] = 0x8e;
2966 ScreenLines2[off] = mb_c & 0xff;
2967 }
2968 else if (enc_utf8)
2969 {
2970 if (mb_utf8)
2971 {
2972 int i;
2973
2974 ScreenLinesUC[off] = mb_c;
2975 if ((c & 0xff) == 0)
2976 ScreenLines[off] = 0x80; // avoid storing zero
2977 for (i = 0; i < Screen_mco; ++i)
2978 {
2979 ScreenLinesC[i][off] = u8cc[i];
2980 if (u8cc[i] == 0)
2981 break;
2982 }
2983 }
2984 else
2985 ScreenLinesUC[off] = 0;
2986 }
2987 if (multi_attr)
2988 {
2989 ScreenAttrs[off] = multi_attr;
2990 multi_attr = 0;
2991 }
2992 else
2993 ScreenAttrs[off] = char_attr;
2994
2995 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2996 {
2997 // Need to fill two screen columns.
2998 ++off;
2999 ++col;
3000 if (enc_utf8)
3001 // UTF-8: Put a 0 in the second screen char.
3002 ScreenLines[off] = 0;
3003 else
3004 // DBCS: Put second byte in the second screen char.
3005 ScreenLines[off] = mb_c & 0xff;
3006 if (draw_state > WL_NR
3007#ifdef FEAT_DIFF
3008 && filler_todo <= 0
3009#endif
3010 )
3011 ++vcol;
3012 // When "tocol" is halfway a character, set it to the end of
3013 // the character, otherwise highlighting won't stop.
3014 if (tocol == vcol)
3015 ++tocol;
3016#ifdef FEAT_RIGHTLEFT
3017 if (wp->w_p_rl)
3018 {
3019 // now it's time to backup one cell
3020 --off;
3021 --col;
3022 }
3023#endif
3024 }
3025#ifdef FEAT_RIGHTLEFT
3026 if (wp->w_p_rl)
3027 {
3028 --off;
3029 --col;
3030 }
3031 else
3032#endif
3033 {
3034 ++off;
3035 ++col;
3036 }
3037 }
3038#ifdef FEAT_CONCEAL
3039 else if (wp->w_p_cole > 0 && is_concealing)
3040 {
3041 --n_skip;
3042 ++vcol_off;
3043 if (n_extra > 0)
3044 vcol_off += n_extra;
3045 if (wp->w_p_wrap)
3046 {
3047 // Special voodoo required if 'wrap' is on.
3048 //
3049 // Advance the column indicator to force the line
3050 // drawing to wrap early. This will make the line
3051 // take up the same screen space when parts are concealed,
3052 // so that cursor line computations aren't messed up.
3053 //
3054 // To avoid the fictitious advance of 'col' causing
3055 // trailing junk to be written out of the screen line
3056 // we are building, 'boguscols' keeps track of the number
3057 // of bad columns we have advanced.
3058 if (n_extra > 0)
3059 {
3060 vcol += n_extra;
3061# ifdef FEAT_RIGHTLEFT
3062 if (wp->w_p_rl)
3063 {
3064 col -= n_extra;
3065 boguscols -= n_extra;
3066 }
3067 else
3068# endif
3069 {
3070 col += n_extra;
3071 boguscols += n_extra;
3072 }
3073 n_extra = 0;
3074 n_attr = 0;
3075 }
3076
3077
3078 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
3079 {
3080 // Need to fill two screen columns.
3081# ifdef FEAT_RIGHTLEFT
3082 if (wp->w_p_rl)
3083 {
3084 --boguscols;
3085 --col;
3086 }
3087 else
3088# endif
3089 {
3090 ++boguscols;
3091 ++col;
3092 }
3093 }
3094
3095# ifdef FEAT_RIGHTLEFT
3096 if (wp->w_p_rl)
3097 {
3098 --boguscols;
3099 --col;
3100 }
3101 else
3102# endif
3103 {
3104 ++boguscols;
3105 ++col;
3106 }
3107 }
3108 else
3109 {
3110 if (n_extra > 0)
3111 {
3112 vcol += n_extra;
3113 n_extra = 0;
3114 n_attr = 0;
3115 }
3116 }
3117
3118 }
3119#endif // FEAT_CONCEAL
3120 else
3121 --n_skip;
3122
3123 // Only advance the "vcol" when after the 'number' or 'relativenumber'
3124 // column.
3125 if (draw_state > WL_NR
3126#ifdef FEAT_DIFF
3127 && filler_todo <= 0
3128#endif
3129 )
3130 ++vcol;
3131
3132#ifdef FEAT_SYN_HL
3133 if (vcol_save_attr >= 0)
3134 char_attr = vcol_save_attr;
3135#endif
3136
3137 // restore attributes after "predeces" in 'listchars'
3138 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
3139 char_attr = saved_attr3;
3140
3141 // restore attributes after last 'listchars' or 'number' char
3142 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
3143 char_attr = saved_attr2;
3144
3145 // At end of screen line and there is more to come: Display the line
3146 // so far. If there is no more to display it is caught above.
3147 if ((
3148#ifdef FEAT_RIGHTLEFT
3149 wp->w_p_rl ? (col < 0) :
3150#endif
3151 (col >= wp->w_width))
Bram Moolenaar41fb7232021-07-08 12:40:05 +02003152 && (draw_state != WL_LINE
3153 || *ptr != NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003154#ifdef FEAT_DIFF
3155 || filler_todo > 0
3156#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01003157 || (wp->w_p_list && wp->w_lcs_chars.eol != NUL
3158 && p_extra != at_end_str)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003159 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
3160 )
3161 {
3162#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003163 screen_line(wp, screen_row, wp->w_wincol, col - boguscols,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003164 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003165 boguscols = 0;
3166#else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003167 screen_line(wp, screen_row, wp->w_wincol, col,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003168 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003169#endif
3170 ++row;
3171 ++screen_row;
3172
3173 // When not wrapping and finished diff lines, or when displayed
3174 // '$' and highlighting until last column, break here.
3175 if ((!wp->w_p_wrap
3176#ifdef FEAT_DIFF
3177 && filler_todo <= 0
3178#endif
3179 ) || lcs_eol_one == -1)
3180 break;
3181
3182 // When the window is too narrow draw all "@" lines.
3183 if (draw_state != WL_LINE
3184#ifdef FEAT_DIFF
3185 && filler_todo <= 0
3186#endif
3187 )
3188 {
3189 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
3190 draw_vsep_win(wp, row);
3191 row = endrow;
3192 }
3193
3194 // When line got too long for screen break here.
3195 if (row == endrow)
3196 {
3197 ++row;
3198 break;
3199 }
3200
3201 if (screen_cur_row == screen_row - 1
3202#ifdef FEAT_DIFF
3203 && filler_todo <= 0
3204#endif
3205 && wp->w_width == Columns)
3206 {
3207 // Remember that the line wraps, used for modeless copy.
3208 LineWraps[screen_row - 1] = TRUE;
3209
3210 // Special trick to make copy/paste of wrapped lines work with
3211 // xterm/screen: write an extra character beyond the end of
3212 // the line. This will work with all terminal types
3213 // (regardless of the xn,am settings).
3214 // Only do this on a fast tty.
3215 // Only do this if the cursor is on the current line
3216 // (something has been written in it).
3217 // Don't do this for the GUI.
3218 // Don't do this for double-width characters.
3219 // Don't do this for a window not at the right screen border.
3220 if (p_tf
3221#ifdef FEAT_GUI
3222 && !gui.in_use
3223#endif
3224 && !(has_mbyte
3225 && ((*mb_off2cells)(LineOffset[screen_row],
3226 LineOffset[screen_row] + screen_Columns)
3227 == 2
3228 || (*mb_off2cells)(LineOffset[screen_row - 1]
3229 + (int)Columns - 2,
3230 LineOffset[screen_row] + screen_Columns)
3231 == 2)))
3232 {
3233 // First make sure we are at the end of the screen line,
3234 // then output the same character again to let the
3235 // terminal know about the wrap. If the terminal doesn't
3236 // auto-wrap, we overwrite the character.
3237 if (screen_cur_col != wp->w_width)
3238 screen_char(LineOffset[screen_row - 1]
3239 + (unsigned)Columns - 1,
3240 screen_row - 1, (int)(Columns - 1));
3241
3242 // When there is a multi-byte character, just output a
3243 // space to keep it simple.
3244 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
3245 screen_row - 1] + (Columns - 1)]) > 1)
3246 out_char(' ');
3247 else
3248 out_char(ScreenLines[LineOffset[screen_row - 1]
3249 + (Columns - 1)]);
3250 // force a redraw of the first char on the next line
3251 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
3252 screen_start(); // don't know where cursor is now
3253 }
3254 }
3255
3256 col = 0;
3257 off = (unsigned)(current_ScreenLine - ScreenLines);
3258#ifdef FEAT_RIGHTLEFT
3259 if (wp->w_p_rl)
3260 {
3261 col = wp->w_width - 1; // col is not used if breaking!
3262 off += col;
3263 }
3264#endif
3265
3266 // reset the drawing state for the start of a wrapped line
3267 draw_state = WL_START;
3268 saved_n_extra = n_extra;
3269 saved_p_extra = p_extra;
3270 saved_c_extra = c_extra;
3271 saved_c_final = c_final;
3272#ifdef FEAT_SYN_HL
3273 if (!(cul_screenline
3274# ifdef FEAT_DIFF
Bram Moolenaare7705982020-04-21 22:23:15 +02003275 && diff_hlf == (hlf_T)0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003276# endif
Bram Moolenaare7705982020-04-21 22:23:15 +02003277 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003278 saved_char_attr = char_attr;
3279 else
3280#endif
3281 saved_char_attr = 0;
3282 n_extra = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01003283 lcs_prec_todo = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003284#ifdef FEAT_LINEBREAK
3285# ifdef FEAT_DIFF
3286 if (filler_todo <= 0)
3287# endif
3288 need_showbreak = TRUE;
3289#endif
3290#ifdef FEAT_DIFF
3291 --filler_todo;
3292 // When the filler lines are actually below the last line of the
3293 // file, don't draw the line itself, break here.
3294 if (filler_todo == 0 && wp->w_botfill)
3295 break;
3296#endif
3297 }
3298
3299 } // for every character in the line
3300
3301#ifdef FEAT_SPELL
3302 // After an empty line check first word for capital.
3303 if (*skipwhite(line) == NUL)
3304 {
3305 capcol_lnum = lnum + 1;
3306 cap_col = 0;
3307 }
3308#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003309#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003310 vim_free(text_props);
3311 vim_free(text_prop_idxs);
3312#endif
3313
3314 vim_free(p_extra_free);
3315 return row;
3316}