blob: 59c62c79d378049bf41b59cbfb29a956e681dd28 [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
973 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200974 }
975 else
976 {
977 line_attr_save = line_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200978 margin_columns_win(wp, &left_curline_col, &right_curline_col);
979 }
980 area_highlighting = TRUE;
981 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200982 }
983#endif
984
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100985#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200986 {
987 char_u *prop_start;
988
989 text_prop_count = get_text_props(wp->w_buffer, lnum,
990 &prop_start, FALSE);
991 if (text_prop_count > 0)
992 {
993 // Make a copy of the properties, so that they are properly
994 // aligned.
995 text_props = ALLOC_MULT(textprop_T, text_prop_count);
996 if (text_props != NULL)
997 mch_memmove(text_props, prop_start,
998 text_prop_count * sizeof(textprop_T));
999
1000 // Allocate an array for the indexes.
1001 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
1002 area_highlighting = TRUE;
1003 extra_check = TRUE;
1004 }
1005 }
1006#endif
1007
1008 off = (unsigned)(current_ScreenLine - ScreenLines);
1009 col = 0;
1010
1011#ifdef FEAT_RIGHTLEFT
1012 if (wp->w_p_rl)
1013 {
1014 // Rightleft window: process the text in the normal direction, but put
1015 // it in current_ScreenLine[] from right to left. Start at the
1016 // rightmost column of the window.
1017 col = wp->w_width - 1;
1018 off += col;
1019 screen_line_flags |= SLF_RIGHTLEFT;
1020 }
1021#endif
1022
1023 // Repeat for the whole displayed line.
1024 for (;;)
1025 {
1026#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02001027 int has_match_conc = 0; // match wants to conceal
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001028#endif
1029#ifdef FEAT_CONCEAL
1030 int did_decrement_ptr = FALSE;
1031#endif
1032 // Skip this quickly when working on the text.
1033 if (draw_state != WL_LINE)
1034 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001035#ifdef FEAT_SYN_HL
1036 if (cul_screenline)
1037 {
1038 cul_attr = 0;
1039 line_attr = line_attr_save;
1040 }
1041#endif
1042
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001043#ifdef FEAT_CMDWIN
1044 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
1045 {
1046 draw_state = WL_CMDLINE;
1047 if (cmdwin_type != 0 && wp == curwin)
1048 {
1049 // Draw the cmdline character.
1050 n_extra = 1;
1051 c_extra = cmdwin_type;
1052 c_final = NUL;
1053 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
1054 }
1055 }
1056#endif
1057
1058#ifdef FEAT_FOLDING
1059 if (draw_state == WL_FOLD - 1 && n_extra == 0)
1060 {
1061 int fdc = compute_foldcolumn(wp, 0);
1062
1063 draw_state = WL_FOLD;
1064 if (fdc > 0)
1065 {
1066 // Draw the 'foldcolumn'. Allocate a buffer, "extra" may
1067 // already be in use.
1068 vim_free(p_extra_free);
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001069 p_extra_free = alloc(MAX_MCO * fdc + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001070 if (p_extra_free != NULL)
1071 {
Bram Moolenaar9355ae42021-03-08 19:04:05 +01001072 n_extra = (int)fill_foldcolumn(p_extra_free, wp,
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001073 FALSE, lnum);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001074 p_extra_free[n_extra] = NUL;
1075 p_extra = p_extra_free;
1076 c_extra = NUL;
1077 c_final = NUL;
Bram Moolenaare413ea02021-11-24 16:20:13 +00001078 if (use_cursor_line_sign(wp, lnum))
1079 char_attr =
1080 hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLF));
1081 else
1082 char_attr =
1083 hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001084 }
1085 }
1086 }
1087#endif
1088
1089#ifdef FEAT_SIGNS
1090 if (draw_state == WL_SIGN - 1 && n_extra == 0)
1091 {
1092 draw_state = WL_SIGN;
1093 // Show the sign column when there are any signs in this
1094 // buffer or when using Netbeans.
1095 if (signcolumn_on(wp))
1096 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
1097 row, startrow, filler_lines, filler_todo, &c_extra,
1098 &c_final, extra, &p_extra, &n_extra, &char_attr);
1099 }
1100#endif
1101
1102 if (draw_state == WL_NR - 1 && n_extra == 0)
1103 {
1104 draw_state = WL_NR;
1105 // Display the absolute or relative line number. After the
1106 // first fill with blanks when the 'n' flag isn't in 'cpo'
1107 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar936dc602022-03-06 20:47:01 +00001108 && (row == startrow + filler_lines
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001109 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
1110 {
1111#ifdef FEAT_SIGNS
1112 // If 'signcolumn' is set to 'number' and a sign is present
1113 // in 'lnum', then display the sign instead of the line
1114 // number.
1115 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
1116 && sign_present)
1117 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
1118 row, startrow, filler_lines, filler_todo,
1119 &c_extra, &c_final, extra, &p_extra, &n_extra,
1120 &char_attr);
1121 else
1122#endif
1123 {
1124 // Draw the line number (empty space after wrapping).
Bram Moolenaar936dc602022-03-06 20:47:01 +00001125 if (row == startrow + filler_lines)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001126 {
1127 long num;
1128 char *fmt = "%*ld ";
1129
1130 if (wp->w_p_nu && !wp->w_p_rnu)
1131 // 'number' + 'norelativenumber'
1132 num = (long)lnum;
1133 else
1134 {
1135 // 'relativenumber', don't use negative numbers
1136 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1137 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1138 {
1139 // 'number' + 'relativenumber'
1140 num = lnum;
1141 fmt = "%-*ld ";
1142 }
1143 }
1144
1145 sprintf((char *)extra, fmt,
1146 number_width(wp), num);
1147 if (wp->w_skipcol > 0)
1148 for (p_extra = extra; *p_extra == ' '; ++p_extra)
1149 *p_extra = '-';
1150#ifdef FEAT_RIGHTLEFT
1151 if (wp->w_p_rl) // reverse line numbers
1152 {
1153 char_u *p1, *p2;
1154 int t;
1155
1156 // like rl_mirror(), but keep the space at the end
Christian Brabandt29f0dc32021-06-16 19:28:34 +02001157 p2 = skipwhite(extra);
1158 p2 = skiptowhite(p2) - 1;
1159 for (p1 = skipwhite(extra); p1 < p2; ++p1, --p2)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001160 {
1161 t = *p1;
1162 *p1 = *p2;
1163 *p2 = t;
1164 }
1165 }
1166#endif
1167 p_extra = extra;
1168 c_extra = NUL;
1169 c_final = NUL;
1170 }
1171 else
1172 {
1173 c_extra = ' ';
1174 c_final = NUL;
1175 }
1176 n_extra = number_width(wp) + 1;
1177 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
1178#ifdef FEAT_SYN_HL
1179 // When 'cursorline' is set highlight the line number of
1180 // the current line differently.
zeertzjq754d2b42022-03-13 13:40:45 +00001181 // When 'cursorlineopt' does not have "line" only
1182 // highlight the line number itself.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001183 // TODO: Can we use CursorLine instead of CursorLineNr
1184 // when CursorLineNr isn't set?
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001185 if (wp->w_p_cul
1186 && lnum == wp->w_cursor.lnum
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001187 && (wp->w_p_culopt_flags & CULOPT_NBR)
Bram Moolenaar127969c2022-03-06 19:54:13 +00001188 && (row == startrow + filler_lines
1189 || (row > startrow + filler_lines
zeertzjq754d2b42022-03-13 13:40:45 +00001190 && (wp->w_p_culopt_flags & CULOPT_LINE))))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001191 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
1192#endif
Bram Moolenaarefae76a2019-10-27 22:54:58 +01001193 if (wp->w_p_rnu && lnum < wp->w_cursor.lnum
1194 && HL_ATTR(HLF_LNA) != 0)
1195 // Use LineNrAbove
1196 char_attr = hl_combine_attr(wcr_attr,
1197 HL_ATTR(HLF_LNA));
1198 if (wp->w_p_rnu && lnum > wp->w_cursor.lnum
1199 && HL_ATTR(HLF_LNB) != 0)
1200 // Use LineNrBelow
1201 char_attr = hl_combine_attr(wcr_attr,
1202 HL_ATTR(HLF_LNB));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001203 }
James McCoya80aad72021-12-22 19:45:28 +00001204#ifdef FEAT_SIGNS
1205 if (num_attr)
1206 char_attr = num_attr;
1207#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001208 }
1209 }
1210
1211#ifdef FEAT_LINEBREAK
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001212 if (wp->w_briopt_sbr && draw_state == WL_BRI - 1
Bram Moolenaaree857022019-11-09 23:26:40 +01001213 && n_extra == 0 && *get_showbreak_value(wp) != NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001214 // draw indent after showbreak value
1215 draw_state = WL_BRI;
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001216 else if (wp->w_briopt_sbr && draw_state == WL_SBR && n_extra == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001217 // After the showbreak, draw the breakindent
1218 draw_state = WL_BRI - 1;
1219
1220 // draw 'breakindent': indent wrapped text accordingly
1221 if (draw_state == WL_BRI - 1 && n_extra == 0)
1222 {
1223 draw_state = WL_BRI;
1224 // if need_showbreak is set, breakindent also applies
Bram Moolenaarfe154992022-03-22 20:42:12 +00001225 if (wp->w_p_bri && (row != startrow || need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001226# ifdef FEAT_DIFF
1227 && filler_lines == 0
1228# endif
1229 )
1230 {
1231 char_attr = 0;
1232# ifdef FEAT_DIFF
1233 if (diff_hlf != (hlf_T)0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001234 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001235# endif
1236 p_extra = NULL;
1237 c_extra = ' ';
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001238 c_final = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001239 n_extra = get_breakindent_win(wp,
1240 ml_get_buf(wp->w_buffer, lnum, FALSE));
Bram Moolenaare882f7a2020-05-16 14:07:39 +02001241 if (row == startrow)
1242 {
1243 n_extra -= win_col_off2(wp);
1244 if (n_extra < 0)
1245 n_extra = 0;
1246 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001247 if (wp->w_skipcol > 0 && wp->w_p_wrap && wp->w_briopt_sbr)
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001248 need_showbreak = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001249 // Correct end of highlighted area for 'breakindent',
1250 // required when 'linebreak' is also set.
1251 if (tocol == vcol)
1252 tocol += n_extra;
1253 }
1254 }
1255#endif
1256
1257#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
1258 if (draw_state == WL_SBR - 1 && n_extra == 0)
1259 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001260 char_u *sbr;
1261
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001262 draw_state = WL_SBR;
1263# ifdef FEAT_DIFF
1264 if (filler_todo > 0)
1265 {
1266 // Draw "deleted" diff line(s).
1267 if (char2cells(fill_diff) > 1)
1268 {
1269 c_extra = '-';
1270 c_final = NUL;
1271 }
1272 else
1273 {
1274 c_extra = fill_diff;
1275 c_final = NUL;
1276 }
1277# ifdef FEAT_RIGHTLEFT
1278 if (wp->w_p_rl)
1279 n_extra = col + 1;
1280 else
1281# endif
1282 n_extra = wp->w_width - col;
1283 char_attr = HL_ATTR(HLF_DED);
1284 }
1285# endif
1286# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001287 sbr = get_showbreak_value(wp);
1288 if (*sbr != NUL && need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001289 {
1290 // Draw 'showbreak' at the start of each broken line.
Bram Moolenaaree857022019-11-09 23:26:40 +01001291 p_extra = sbr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001292 c_extra = NUL;
1293 c_final = NUL;
Bram Moolenaaree857022019-11-09 23:26:40 +01001294 n_extra = (int)STRLEN(sbr);
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001295 if (wp->w_skipcol == 0 || !wp->w_p_wrap)
1296 need_showbreak = FALSE;
Bram Moolenaaree857022019-11-09 23:26:40 +01001297 vcol_sbr = vcol + MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001298 // Correct end of highlighted area for 'showbreak',
1299 // required when 'linebreak' is also set.
1300 if (tocol == vcol)
1301 tocol += n_extra;
1302 // combine 'showbreak' with 'wincolor'
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001303 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001304# ifdef FEAT_SYN_HL
1305 // combine 'showbreak' with 'cursorline'
1306 if (cul_attr != 0)
1307 char_attr = hl_combine_attr(char_attr, cul_attr);
1308# endif
1309 }
1310# endif
1311 }
1312#endif
1313
1314 if (draw_state == WL_LINE - 1 && n_extra == 0)
1315 {
1316 draw_state = WL_LINE;
1317 if (saved_n_extra)
1318 {
1319 // Continue item from end of wrapped line.
1320 n_extra = saved_n_extra;
1321 c_extra = saved_c_extra;
1322 c_final = saved_c_final;
1323 p_extra = saved_p_extra;
1324 char_attr = saved_char_attr;
1325 }
1326 else
1327 char_attr = win_attr;
1328 }
1329 }
1330#ifdef FEAT_SYN_HL
zeertzjq4f33bc22021-08-05 17:57:02 +02001331 if (cul_screenline && draw_state == WL_LINE
1332 && vcol >= left_curline_col
1333 && vcol < right_curline_col)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001334 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001335 cul_attr = HL_ATTR(HLF_CUL);
1336 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001337 }
1338#endif
1339
1340 // When still displaying '$' of change command, stop at cursor.
1341 // When only displaying the (relative) line number and that's done,
1342 // stop here.
Bram Moolenaar511feec2020-06-18 19:15:27 +02001343 if (((dollar_vcol >= 0 && wp == curwin
1344 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol)
1345 || (number_only && draw_state > WL_NR))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001346#ifdef FEAT_DIFF
1347 && filler_todo <= 0
1348#endif
1349 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001350 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001351 screen_line(screen_row, wp->w_wincol, col, -wp->w_width,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001352 screen_line_flags);
1353 // Pretend we have finished updating the window. Except when
1354 // 'cursorcolumn' is set.
1355#ifdef FEAT_SYN_HL
1356 if (wp->w_p_cuc)
1357 row = wp->w_cline_row + wp->w_cline_height;
1358 else
1359#endif
1360 row = wp->w_height;
1361 break;
1362 }
1363
Bram Moolenaar34390282019-10-16 14:38:26 +02001364 if (draw_state == WL_LINE && (area_highlighting || extra_check))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001365 {
1366 // handle Visual or match highlighting in this line
1367 if (vcol == fromcol
1368 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
1369 && (*mb_ptr2cells)(ptr) > 1)
1370 || ((int)vcol_prev == fromcol_prev
1371 && vcol_prev < vcol // not at margin
1372 && vcol < tocol))
1373 area_attr = vi_attr; // start highlighting
1374 else if (area_attr != 0
1375 && (vcol == tocol
1376 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
1377 area_attr = 0; // stop highlighting
1378
1379#ifdef FEAT_SEARCH_EXTRA
1380 if (!n_extra)
1381 {
1382 // Check for start/end of 'hlsearch' and other matches.
1383 // After end, check for start/end of next match.
1384 // When another match, have to check for start again.
1385 v = (long)(ptr - line);
1386 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
1387 &screen_search_hl, &has_match_conc,
Bram Moolenaar0c359af2021-11-29 19:18:57 +00001388 &match_conc, did_line_attr, lcs_eol_one,
1389 &on_last_col);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001390 ptr = line + v; // "line" may have been changed
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001391
1392 // Do not allow a conceal over EOL otherwise EOL will be missed
1393 // and bad things happen.
1394 if (*ptr == NUL)
1395 has_match_conc = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001396 }
1397#endif
1398
1399#ifdef FEAT_DIFF
1400 if (diff_hlf != (hlf_T)0)
1401 {
1402 if (diff_hlf == HLF_CHD && ptr - line >= change_start
1403 && n_extra == 0)
1404 diff_hlf = HLF_TXD; // changed text
1405 if (diff_hlf == HLF_TXD && ptr - line > change_end
1406 && n_extra == 0)
1407 diff_hlf = HLF_CHD; // changed line
1408 line_attr = HL_ATTR(diff_hlf);
1409 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
1410 && wp->w_p_culopt_flags != CULOPT_NBR
1411 && (!cul_screenline || (vcol >= left_curline_col
1412 && vcol <= right_curline_col)))
1413 line_attr = hl_combine_attr(
1414 line_attr, HL_ATTR(HLF_CUL));
1415 }
1416#endif
1417
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001418#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001419 if (text_props != NULL)
1420 {
1421 int pi;
1422 int bcol = (int)(ptr - line);
1423
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001424 if (n_extra > 0
1425# ifdef FEAT_LINEBREAK
1426 && !in_linebreak
1427# endif
1428 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001429 --bcol; // still working on the previous char, e.g. Tab
1430
1431 // Check if any active property ends.
1432 for (pi = 0; pi < text_props_active; ++pi)
1433 {
1434 int tpi = text_prop_idxs[pi];
1435
1436 if (bcol >= text_props[tpi].tp_col - 1
1437 + text_props[tpi].tp_len)
1438 {
1439 if (pi + 1 < text_props_active)
1440 mch_memmove(text_prop_idxs + pi,
1441 text_prop_idxs + pi + 1,
1442 sizeof(int)
1443 * (text_props_active - (pi + 1)));
1444 --text_props_active;
1445 --pi;
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001446# ifdef FEAT_LINEBREAK
1447 // not exactly right but should work in most cases
1448 if (in_linebreak && syntax_attr == text_prop_attr)
1449 syntax_attr = 0;
1450# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001451 }
1452 }
1453
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001454# ifdef FEAT_LINEBREAK
1455 if (n_extra > 0 && in_linebreak)
1456 // not on the next char yet, don't start another prop
1457 --bcol;
1458# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001459 // Add any text property that starts in this column.
1460 while (text_prop_next < text_prop_count
1461 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001462 {
1463 if (bcol <= text_props[text_prop_next].tp_col - 1
1464 + text_props[text_prop_next].tp_len)
1465 text_prop_idxs[text_props_active++] = text_prop_next;
1466 ++text_prop_next;
1467 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001468
1469 text_prop_attr = 0;
1470 text_prop_combine = FALSE;
Bram Moolenaar053f7122019-09-23 22:17:15 +02001471 text_prop_type = NULL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001472 if (text_props_active > 0)
1473 {
1474 // Sort the properties on priority and/or starting last.
1475 // Then combine the attributes, highest priority last.
1476 current_text_props = text_props;
1477 current_buf = wp->w_buffer;
1478 qsort((void *)text_prop_idxs, (size_t)text_props_active,
1479 sizeof(int), text_prop_compare);
1480
1481 for (pi = 0; pi < text_props_active; ++pi)
1482 {
1483 int tpi = text_prop_idxs[pi];
1484 proptype_T *pt = text_prop_type_by_id(
1485 wp->w_buffer, text_props[tpi].tp_type);
1486
1487 if (pt != NULL && pt->pt_hl_id > 0)
1488 {
1489 int pt_attr = syn_id2attr(pt->pt_hl_id);
1490
1491 text_prop_type = pt;
1492 text_prop_attr =
1493 hl_combine_attr(text_prop_attr, pt_attr);
1494 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
1495 }
1496 }
1497 }
1498 }
1499#endif
1500
Bram Moolenaara74fda62019-10-19 17:38:03 +02001501#ifdef FEAT_SYN_HL
Bram Moolenaara74fda62019-10-19 17:38:03 +02001502 if (extra_check && n_extra == 0)
Bram Moolenaar34390282019-10-16 14:38:26 +02001503 {
Bram Moolenaar82260af2019-10-20 13:16:22 +02001504 syntax_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001505# ifdef FEAT_TERMINAL
Bram Moolenaar34390282019-10-16 14:38:26 +02001506 if (get_term_attr)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001507 syntax_attr = term_get_attr(wp, lnum, vcol);
Bram Moolenaara74fda62019-10-19 17:38:03 +02001508# endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001509 // Get syntax attribute.
1510 if (has_syntax)
1511 {
1512 // Get the syntax attribute for the character. If there
1513 // is an error, disable syntax highlighting.
1514 save_did_emsg = did_emsg;
1515 did_emsg = FALSE;
1516
1517 v = (long)(ptr - line);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001518 if (v == prev_syntax_col)
1519 // at same column again
1520 syntax_attr = prev_syntax_attr;
1521 else
1522 {
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001523# ifdef FEAT_SPELL
Bram Moolenaar9115c612019-10-16 16:57:06 +02001524 can_spell = TRUE;
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001525# endif
Bram Moolenaar9115c612019-10-16 16:57:06 +02001526 syntax_attr = get_syntax_attr((colnr_T)v,
Bram Moolenaar34390282019-10-16 14:38:26 +02001527# ifdef FEAT_SPELL
1528 has_spell ? &can_spell :
1529# endif
1530 NULL, FALSE);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001531 prev_syntax_col = v;
1532 prev_syntax_attr = syntax_attr;
1533 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001534
Bram Moolenaar34390282019-10-16 14:38:26 +02001535 if (did_emsg)
1536 {
1537 wp->w_s->b_syn_error = TRUE;
1538 has_syntax = FALSE;
1539 syntax_attr = 0;
1540 }
1541 else
1542 did_emsg = save_did_emsg;
1543# ifdef SYN_TIME_LIMIT
1544 if (wp->w_s->b_syn_slow)
1545 has_syntax = FALSE;
1546# endif
1547
1548 // Need to get the line again, a multi-line regexp may
1549 // have made it invalid.
1550 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1551 ptr = line + v;
1552# ifdef FEAT_CONCEAL
1553 // no concealing past the end of the line, it interferes
1554 // with line highlighting
1555 if (*ptr == NUL)
1556 syntax_flags = 0;
1557 else
1558 syntax_flags = get_syntax_info(&syntax_seqnr);
1559# endif
1560 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001561 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001562# ifdef FEAT_PROP_POPUP
Bram Moolenaardbd43162019-11-09 21:28:14 +01001563 // Combine text property highlight into syntax highlight.
1564 if (text_prop_type != NULL)
1565 {
1566 if (text_prop_combine)
1567 syntax_attr = hl_combine_attr(syntax_attr, text_prop_attr);
1568 else
1569 syntax_attr = text_prop_attr;
1570 }
1571# endif
Bram Moolenaara74fda62019-10-19 17:38:03 +02001572#endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001573
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001574 // Decide which of the highlight attributes to use.
1575 attr_pri = TRUE;
1576#ifdef LINE_ATTR
1577 if (area_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001578 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001579 char_attr = hl_combine_attr(line_attr, area_attr);
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001580 if (!highlight_match)
1581 // let search highlight show in Visual area if possible
1582 char_attr = hl_combine_attr(search_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001583# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001584 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001585# endif
1586 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001587 else if (search_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001588 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001589 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001590# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001591 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001592# endif
1593 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001594 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
1595 || vcol < fromcol || vcol_prev < fromcol_prev
1596 || vcol >= tocol))
1597 {
1598 // Use line_attr when not in the Visual or 'incsearch' area
1599 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar34390282019-10-16 14:38:26 +02001600# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001601 char_attr = hl_combine_attr(syntax_attr, line_attr);
1602# else
1603 char_attr = line_attr;
Bram Moolenaar34390282019-10-16 14:38:26 +02001604# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001605 attr_pri = FALSE;
1606 }
1607#else
1608 if (area_attr != 0)
1609 char_attr = area_attr;
1610 else if (search_attr != 0)
1611 char_attr = search_attr;
1612#endif
1613 else
1614 {
1615 attr_pri = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001616#ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001617 char_attr = syntax_attr;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001618#else
Bram Moolenaardbd43162019-11-09 21:28:14 +01001619 char_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001620#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001621 }
1622 }
Bram Moolenaar024dbd22019-11-02 22:00:15 +01001623
1624 // combine attribute with 'wincolor'
1625 if (win_attr != 0)
1626 {
1627 if (char_attr == 0)
1628 char_attr = win_attr;
1629 else
1630 char_attr = hl_combine_attr(win_attr, char_attr);
1631 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001632
1633 // Get the next character to put on the screen.
1634
1635 // The "p_extra" points to the extra stuff that is inserted to
1636 // represent special characters (non-printable stuff) and other
1637 // things. When all characters are the same, c_extra is used.
1638 // If c_final is set, it will compulsorily be used at the end.
1639 // "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
1640 // "p_extra[n_extra]".
1641 // For the '$' of the 'list' option, n_extra == 1, p_extra == "".
1642 if (n_extra > 0)
1643 {
1644 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
1645 {
1646 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
1647 mb_c = c; // doesn't handle non-utf-8 multi-byte!
1648 if (enc_utf8 && utf_char2len(c) > 1)
1649 {
1650 mb_utf8 = TRUE;
1651 u8cc[0] = 0;
1652 c = 0xc0;
1653 }
1654 else
1655 mb_utf8 = FALSE;
1656 }
1657 else
1658 {
1659 c = *p_extra;
1660 if (has_mbyte)
1661 {
1662 mb_c = c;
1663 if (enc_utf8)
1664 {
1665 // If the UTF-8 character is more than one byte:
1666 // Decode it into "mb_c".
1667 mb_l = utfc_ptr2len(p_extra);
1668 mb_utf8 = FALSE;
1669 if (mb_l > n_extra)
1670 mb_l = 1;
1671 else if (mb_l > 1)
1672 {
1673 mb_c = utfc_ptr2char(p_extra, u8cc);
1674 mb_utf8 = TRUE;
1675 c = 0xc0;
1676 }
1677 }
1678 else
1679 {
1680 // if this is a DBCS character, put it in "mb_c"
1681 mb_l = MB_BYTE2LEN(c);
1682 if (mb_l >= n_extra)
1683 mb_l = 1;
1684 else if (mb_l > 1)
1685 mb_c = (c << 8) + p_extra[1];
1686 }
1687 if (mb_l == 0) // at the NUL at end-of-line
1688 mb_l = 1;
1689
1690 // If a double-width char doesn't fit display a '>' in the
1691 // last column.
1692 if ((
1693# ifdef FEAT_RIGHTLEFT
1694 wp->w_p_rl ? (col <= 0) :
1695# endif
1696 (col >= wp->w_width - 1))
1697 && (*mb_char2cells)(mb_c) == 2)
1698 {
1699 c = '>';
1700 mb_c = c;
1701 mb_l = 1;
1702 mb_utf8 = FALSE;
1703 multi_attr = HL_ATTR(HLF_AT);
1704#ifdef FEAT_SYN_HL
1705 if (cul_attr)
1706 multi_attr = hl_combine_attr(multi_attr, cul_attr);
1707#endif
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001708 multi_attr = hl_combine_attr(win_attr, multi_attr);
1709
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001710 // put the pointer back to output the double-width
1711 // character at the start of the next line.
1712 ++n_extra;
1713 --p_extra;
1714 }
1715 else
1716 {
1717 n_extra -= mb_l - 1;
1718 p_extra += mb_l - 1;
1719 }
1720 }
1721 ++p_extra;
1722 }
1723 --n_extra;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00001724#if defined(FEAT_LINEBREAK) && defined(FEAT_PROP_POPUP)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001725 if (n_extra <= 0)
1726 in_linebreak = FALSE;
1727#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001728 }
1729 else
1730 {
1731#ifdef FEAT_LINEBREAK
1732 int c0;
1733#endif
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001734 VIM_CLEAR(p_extra_free);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001735
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001736 // Get a character from the line itself.
1737 c = *ptr;
1738#ifdef FEAT_LINEBREAK
1739 c0 = *ptr;
1740#endif
1741 if (has_mbyte)
1742 {
1743 mb_c = c;
1744 if (enc_utf8)
1745 {
1746 // If the UTF-8 character is more than one byte: Decode it
1747 // into "mb_c".
1748 mb_l = utfc_ptr2len(ptr);
1749 mb_utf8 = FALSE;
1750 if (mb_l > 1)
1751 {
1752 mb_c = utfc_ptr2char(ptr, u8cc);
1753 // Overlong encoded ASCII or ASCII with composing char
1754 // is displayed normally, except a NUL.
1755 if (mb_c < 0x80)
1756 {
1757 c = mb_c;
1758#ifdef FEAT_LINEBREAK
1759 c0 = mb_c;
1760#endif
1761 }
1762 mb_utf8 = TRUE;
1763
1764 // At start of the line we can have a composing char.
1765 // Draw it as a space with a composing char.
1766 if (utf_iscomposing(mb_c))
1767 {
1768 int i;
1769
1770 for (i = Screen_mco - 1; i > 0; --i)
1771 u8cc[i] = u8cc[i - 1];
1772 u8cc[0] = mb_c;
1773 mb_c = ' ';
1774 }
1775 }
1776
1777 if ((mb_l == 1 && c >= 0x80)
1778 || (mb_l >= 1 && mb_c == 0)
1779 || (mb_l > 1 && (!vim_isprintc(mb_c))))
1780 {
1781 // Illegal UTF-8 byte: display as <xx>.
1782 // Non-BMP character : display as ? or fullwidth ?.
1783 transchar_hex(extra, mb_c);
1784# ifdef FEAT_RIGHTLEFT
1785 if (wp->w_p_rl) // reverse
1786 rl_mirror(extra);
1787# endif
1788 p_extra = extra;
1789 c = *p_extra;
1790 mb_c = mb_ptr2char_adv(&p_extra);
1791 mb_utf8 = (c >= 0x80);
1792 n_extra = (int)STRLEN(p_extra);
1793 c_extra = NUL;
1794 c_final = NUL;
1795 if (area_attr == 0 && search_attr == 0)
1796 {
1797 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001798 extra_attr = hl_combine_attr(
1799 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001800 saved_attr2 = char_attr; // save current attr
1801 }
1802 }
1803 else if (mb_l == 0) // at the NUL at end-of-line
1804 mb_l = 1;
1805#ifdef FEAT_ARABIC
1806 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
1807 {
1808 // Do Arabic shaping.
1809 int pc, pc1, nc;
1810 int pcc[MAX_MCO];
1811
1812 // The idea of what is the previous and next
1813 // character depends on 'rightleft'.
1814 if (wp->w_p_rl)
1815 {
1816 pc = prev_c;
1817 pc1 = prev_c1;
1818 nc = utf_ptr2char(ptr + mb_l);
1819 prev_c1 = u8cc[0];
1820 }
1821 else
1822 {
1823 pc = utfc_ptr2char(ptr + mb_l, pcc);
1824 nc = prev_c;
1825 pc1 = pcc[0];
1826 }
1827 prev_c = mb_c;
1828
1829 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
1830 }
1831 else
1832 prev_c = mb_c;
1833#endif
1834 }
1835 else // enc_dbcs
1836 {
1837 mb_l = MB_BYTE2LEN(c);
1838 if (mb_l == 0) // at the NUL at end-of-line
1839 mb_l = 1;
1840 else if (mb_l > 1)
1841 {
1842 // We assume a second byte below 32 is illegal.
1843 // Hopefully this is OK for all double-byte encodings!
1844 if (ptr[1] >= 32)
1845 mb_c = (c << 8) + ptr[1];
1846 else
1847 {
1848 if (ptr[1] == NUL)
1849 {
1850 // head byte at end of line
1851 mb_l = 1;
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001852 transchar_nonprint(wp->w_buffer, extra, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001853 }
1854 else
1855 {
1856 // illegal tail byte
1857 mb_l = 2;
1858 STRCPY(extra, "XX");
1859 }
1860 p_extra = extra;
1861 n_extra = (int)STRLEN(extra) - 1;
1862 c_extra = NUL;
1863 c_final = NUL;
1864 c = *p_extra++;
1865 if (area_attr == 0 && search_attr == 0)
1866 {
1867 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001868 extra_attr = hl_combine_attr(
1869 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001870 saved_attr2 = char_attr; // save current attr
1871 }
1872 mb_c = c;
1873 }
1874 }
1875 }
1876 // If a double-width char doesn't fit display a '>' in the
1877 // last column; the character is displayed at the start of the
1878 // next line.
1879 if ((
1880# ifdef FEAT_RIGHTLEFT
1881 wp->w_p_rl ? (col <= 0) :
1882# endif
1883 (col >= wp->w_width - 1))
1884 && (*mb_char2cells)(mb_c) == 2)
1885 {
1886 c = '>';
1887 mb_c = c;
1888 mb_utf8 = FALSE;
1889 mb_l = 1;
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001890 multi_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001891 // Put pointer back so that the character will be
1892 // displayed at the start of the next line.
1893 --ptr;
1894#ifdef FEAT_CONCEAL
1895 did_decrement_ptr = TRUE;
1896#endif
1897 }
1898 else if (*ptr != NUL)
1899 ptr += mb_l - 1;
1900
1901 // If a double-width char doesn't fit at the left side display
1902 // a '<' in the first column. Don't do this for unprintable
1903 // characters.
1904 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
1905 {
1906 n_extra = 1;
1907 c_extra = MB_FILLER_CHAR;
1908 c_final = NUL;
1909 c = ' ';
1910 if (area_attr == 0 && search_attr == 0)
1911 {
1912 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001913 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001914 saved_attr2 = char_attr; // save current attr
1915 }
1916 mb_c = c;
1917 mb_utf8 = FALSE;
1918 mb_l = 1;
1919 }
1920
1921 }
1922 ++ptr;
1923
1924 if (extra_check)
1925 {
1926#ifdef FEAT_SPELL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001927 // Check spelling (unless at the end of the line).
1928 // Only do this when there is no syntax highlighting, the
1929 // @Spell cluster is not used or the current syntax item
1930 // contains the @Spell cluster.
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001931 v = (long)(ptr - line);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001932 if (has_spell && v >= word_end && v > cur_checked_col)
1933 {
1934 spell_attr = 0;
1935 if (c != 0 && (
1936# ifdef FEAT_SYN_HL
1937 !has_syntax ||
1938# endif
1939 can_spell))
1940 {
1941 char_u *prev_ptr, *p;
1942 int len;
1943 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001944
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001945 if (has_mbyte)
1946 {
1947 prev_ptr = ptr - mb_l;
1948 v -= mb_l - 1;
1949 }
1950 else
1951 prev_ptr = ptr - 1;
1952
1953 // Use nextline[] if possible, it has the start of the
1954 // next line concatenated.
1955 if ((prev_ptr - line) - nextlinecol >= 0)
1956 p = nextline + (prev_ptr - line) - nextlinecol;
1957 else
1958 p = prev_ptr;
1959 cap_col -= (int)(prev_ptr - line);
1960 len = spell_check(wp, p, &spell_hlf, &cap_col,
1961 nochange);
1962 word_end = v + len;
1963
1964 // In Insert mode only highlight a word that
1965 // doesn't touch the cursor.
1966 if (spell_hlf != HLF_COUNT
Bram Moolenaar24959102022-05-07 20:01:16 +01001967 && (State & MODE_INSERT)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001968 && wp->w_cursor.lnum == lnum
1969 && wp->w_cursor.col >=
1970 (colnr_T)(prev_ptr - line)
1971 && wp->w_cursor.col < (colnr_T)word_end)
1972 {
1973 spell_hlf = HLF_COUNT;
1974 spell_redraw_lnum = lnum;
1975 }
1976
1977 if (spell_hlf == HLF_COUNT && p != prev_ptr
1978 && (p - nextline) + len > nextline_idx)
1979 {
1980 // Remember that the good word continues at the
1981 // start of the next line.
1982 checked_lnum = lnum + 1;
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001983 checked_col = (int)((p - nextline)
1984 + len - nextline_idx);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001985 }
1986
1987 // Turn index into actual attributes.
1988 if (spell_hlf != HLF_COUNT)
1989 spell_attr = highlight_attr[spell_hlf];
1990
1991 if (cap_col > 0)
1992 {
1993 if (p != prev_ptr
1994 && (p - nextline) + cap_col >= nextline_idx)
1995 {
1996 // Remember that the word in the next line
1997 // must start with a capital.
1998 capcol_lnum = lnum + 1;
1999 cap_col = (int)((p - nextline) + cap_col
2000 - nextline_idx);
2001 }
2002 else
2003 // Compute the actual column.
2004 cap_col += (int)(prev_ptr - line);
2005 }
2006 }
2007 }
2008 if (spell_attr != 0)
2009 {
2010 if (!attr_pri)
2011 char_attr = hl_combine_attr(char_attr, spell_attr);
2012 else
2013 char_attr = hl_combine_attr(spell_attr, char_attr);
2014 }
2015#endif
2016#ifdef FEAT_LINEBREAK
2017 // Found last space before word: check for line break.
2018 if (wp->w_p_lbr && c0 == c
2019 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
2020 {
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002021 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1)
2022 : 0;
2023 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002024
2025 // TODO: is passing p for start of the line OK?
2026 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
2027 NULL) - 1;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002028
2029 // We have just drawn the showbreak value, no need to add
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002030 // space for it again.
Bram Moolenaara06e3452021-05-29 17:56:37 +02002031 if (vcol == vcol_sbr)
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002032 {
Bram Moolenaara06e3452021-05-29 17:56:37 +02002033 n_extra -= MB_CHARLEN(get_showbreak_value(wp));
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002034 if (n_extra < 0)
2035 n_extra = 0;
2036 }
Bram Moolenaar0bbca542022-01-11 13:14:54 +00002037 if (on_last_col && c != TAB)
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002038 // Do not continue search/match highlighting over the
Bram Moolenaar0bbca542022-01-11 13:14:54 +00002039 // line break, but for TABs the highlighting should
2040 // include the complete width of the character
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002041 search_attr = 0;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002042
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002043 if (c == TAB && n_extra + col > wp->w_width)
2044# ifdef FEAT_VARTABS
2045 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
2046 wp->w_buffer->b_p_vts_array) - 1;
2047# else
2048 n_extra = (int)wp->w_buffer->b_p_ts
2049 - vcol % (int)wp->w_buffer->b_p_ts - 1;
2050# endif
2051
2052 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
2053 c_final = NUL;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002054# if defined(FEAT_PROP_POPUP)
Bram Moolenaar42eba042021-11-30 20:22:49 +00002055 if (n_extra > 0 && c != TAB)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00002056 in_linebreak = TRUE;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002057# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002058 if (VIM_ISWHITE(c))
2059 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002060# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002061 if (c == TAB)
2062 // See "Tab alignment" below.
2063 FIX_FOR_BOGUSCOLS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002064# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002065 if (!wp->w_p_list)
2066 c = ' ';
2067 }
2068 }
2069#endif
2070
zeertzjqf14b8ba2021-09-10 16:58:30 +02002071 in_multispace = c == ' '
2072 && ((ptr > line + 1 && ptr[-2] == ' ') || *ptr == ' ');
2073 if (!in_multispace)
2074 multispace_pos = 0;
2075
Bram Moolenaareed9d462021-02-15 20:38:25 +01002076 // 'list': Change char 160 to 'nbsp' and space to 'space'
2077 // setting in 'listchars'. But not when the character is
2078 // followed by a composing character (use mb_l to check that).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002079 if (wp->w_p_list
2080 && ((((c == 160 && mb_l == 1)
2081 || (mb_utf8
2082 && ((mb_c == 160 && mb_l == 2)
2083 || (mb_c == 0x202f && mb_l == 3))))
Bram Moolenaareed9d462021-02-15 20:38:25 +01002084 && wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002085 || (c == ' '
2086 && mb_l == 1
zeertzjqf14b8ba2021-09-10 16:58:30 +02002087 && (wp->w_lcs_chars.space
2088 || (in_multispace
2089 && wp->w_lcs_chars.multispace != NULL))
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002090 && ptr - line >= leadcol
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002091 && ptr - line <= trailcol)))
2092 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02002093 if (in_multispace && wp->w_lcs_chars.multispace != NULL)
2094 {
2095 c = wp->w_lcs_chars.multispace[multispace_pos++];
2096 if (wp->w_lcs_chars.multispace[multispace_pos] == NUL)
2097 multispace_pos = 0;
2098 }
2099 else
2100 c = (c == ' ') ? wp->w_lcs_chars.space
2101 : wp->w_lcs_chars.nbsp;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002102 if (area_attr == 0 && search_attr == 0)
2103 {
2104 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002105 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002106 saved_attr2 = char_attr; // save current attr
2107 }
2108 mb_c = c;
2109 if (enc_utf8 && utf_char2len(c) > 1)
2110 {
2111 mb_utf8 = TRUE;
2112 u8cc[0] = 0;
2113 c = 0xc0;
2114 }
2115 else
2116 mb_utf8 = FALSE;
2117 }
2118
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002119 if ((trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
2120 || (leadcol != 0 && ptr < line + leadcol && c == ' '))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002121 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002122 if (leadcol != 0 && in_multispace && ptr < line + leadcol
2123 && wp->w_lcs_chars.leadmultispace != NULL)
2124 {
2125 c = wp->w_lcs_chars.leadmultispace[multispace_pos++];
2126 if (wp->w_lcs_chars.leadmultispace[multispace_pos] == NUL)
2127 multispace_pos = 0;
2128 }
2129
2130 else if (ptr > line + trailcol && wp->w_lcs_chars.trail)
2131 c = wp->w_lcs_chars.trail;
2132
2133 else if (ptr < line + leadcol && wp->w_lcs_chars.lead)
2134 c = wp->w_lcs_chars.lead;
2135
2136 else if (leadcol != 0 && c == ' ' && wp->w_lcs_chars.space)
2137 c = wp->w_lcs_chars.space;
2138
2139
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002140 if (!attr_pri)
2141 {
2142 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002143 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002144 saved_attr2 = char_attr; // save current attr
2145 }
2146 mb_c = c;
2147 if (enc_utf8 && utf_char2len(c) > 1)
2148 {
2149 mb_utf8 = TRUE;
2150 u8cc[0] = 0;
2151 c = 0xc0;
2152 }
2153 else
2154 mb_utf8 = FALSE;
2155 }
2156 }
2157
2158 // Handling of non-printable characters.
2159 if (!vim_isprintc(c))
2160 {
2161 // when getting a character from the file, we may have to
2162 // turn it into something else on the way to putting it
2163 // into "ScreenLines".
Bram Moolenaareed9d462021-02-15 20:38:25 +01002164 if (c == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002165 {
2166 int tab_len = 0;
2167 long vcol_adjusted = vcol; // removed showbreak length
2168#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01002169 char_u *sbr = get_showbreak_value(wp);
2170
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002171 // only adjust the tab_len, when at the first column
2172 // after the showbreak value was drawn
Bram Moolenaaree857022019-11-09 23:26:40 +01002173 if (*sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
2174 vcol_adjusted = vcol - MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002175#endif
2176 // tab amount depends on current column
2177#ifdef FEAT_VARTABS
2178 tab_len = tabstop_padding(vcol_adjusted,
2179 wp->w_buffer->b_p_ts,
2180 wp->w_buffer->b_p_vts_array) - 1;
2181#else
2182 tab_len = (int)wp->w_buffer->b_p_ts
2183 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
2184#endif
2185
2186#ifdef FEAT_LINEBREAK
2187 if (!wp->w_p_lbr || !wp->w_p_list)
2188#endif
2189 // tab amount depends on current column
2190 n_extra = tab_len;
2191#ifdef FEAT_LINEBREAK
2192 else
2193 {
2194 char_u *p;
2195 int len;
2196 int i;
2197 int saved_nextra = n_extra;
2198
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002199# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002200 if (vcol_off > 0)
2201 // there are characters to conceal
2202 tab_len += vcol_off;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002203
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002204 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaareed9d462021-02-15 20:38:25 +01002205 if (wp->w_p_list && wp->w_lcs_chars.tab1
2206 && old_boguscols > 0
2207 && n_extra > tab_len)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002208 tab_len += n_extra - tab_len;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002209# endif
2210 // If n_extra > 0, it gives the number of chars, to
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002211 // use for a tab, else we need to calculate the width
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002212 // for a tab.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002213 len = (tab_len * mb_char2len(wp->w_lcs_chars.tab2));
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002214 if (wp->w_lcs_chars.tab3)
2215 len += mb_char2len(wp->w_lcs_chars.tab3);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002216 if (n_extra > 0)
2217 len += n_extra - tab_len;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002218 c = wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002219 p = alloc(len + 1);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002220 if (p == NULL)
2221 n_extra = 0;
2222 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002223 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002224 vim_memset(p, ' ', len);
2225 p[len] = NUL;
2226 vim_free(p_extra_free);
2227 p_extra_free = p;
2228 for (i = 0; i < tab_len; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002229 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002230 int lcs = wp->w_lcs_chars.tab2;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002231
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002232 if (*p == NUL)
2233 {
2234 tab_len = i;
2235 break;
2236 }
2237
2238 // if tab3 is given, use it for the last char
2239 if (wp->w_lcs_chars.tab3 && i == tab_len - 1)
2240 lcs = wp->w_lcs_chars.tab3;
2241 p += mb_char2bytes(lcs, p);
2242 n_extra += mb_char2len(lcs)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002243 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002244 }
2245 p_extra = p_extra_free;
2246# ifdef FEAT_CONCEAL
2247 // n_extra will be increased by FIX_FOX_BOGUSCOLS
2248 // macro below, so need to adjust for that here
2249 if (vcol_off > 0)
2250 n_extra -= vcol_off;
2251# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002252 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002253 }
2254#endif
2255#ifdef FEAT_CONCEAL
2256 {
2257 int vc_saved = vcol_off;
2258
2259 // Tab alignment should be identical regardless of
2260 // 'conceallevel' value. So tab compensates of all
2261 // previous concealed characters, and thus resets
2262 // vcol_off and boguscols accumulated so far in the
2263 // line. Note that the tab can be longer than
2264 // 'tabstop' when there are concealed characters.
2265 FIX_FOR_BOGUSCOLS;
2266
2267 // Make sure, the highlighting for the tab char will be
2268 // correctly set further below (effectively reverts the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002269 // FIX_FOR_BOGSUCOLS macro).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002270 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaareed9d462021-02-15 20:38:25 +01002271 && wp->w_lcs_chars.tab1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002272 tab_len += vc_saved;
2273 }
2274#endif
2275 mb_utf8 = FALSE; // don't draw as UTF-8
2276 if (wp->w_p_list)
2277 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002278 c = (n_extra == 0 && wp->w_lcs_chars.tab3)
2279 ? wp->w_lcs_chars.tab3
2280 : wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002281#ifdef FEAT_LINEBREAK
2282 if (wp->w_p_lbr)
2283 c_extra = NUL; // using p_extra from above
2284 else
2285#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01002286 c_extra = wp->w_lcs_chars.tab2;
2287 c_final = wp->w_lcs_chars.tab3;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002288 n_attr = tab_len + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002289 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002290 saved_attr2 = char_attr; // save current attr
2291 mb_c = c;
2292 if (enc_utf8 && utf_char2len(c) > 1)
2293 {
2294 mb_utf8 = TRUE;
2295 u8cc[0] = 0;
2296 c = 0xc0;
2297 }
2298 }
2299 else
2300 {
2301 c_final = NUL;
2302 c_extra = ' ';
2303 c = ' ';
2304 }
2305 }
2306 else if (c == NUL
2307 && (wp->w_p_list
2308 || ((fromcol >= 0 || fromcol_prev >= 0)
2309 && tocol > vcol
2310 && VIsual_mode != Ctrl_V
2311 && (
2312# ifdef FEAT_RIGHTLEFT
2313 wp->w_p_rl ? (col >= 0) :
2314# endif
2315 (col < wp->w_width))
2316 && !(noinvcur
2317 && lnum == wp->w_cursor.lnum
2318 && (colnr_T)vcol == wp->w_virtcol)))
2319 && lcs_eol_one > 0)
2320 {
2321 // Display a '$' after the line or highlight an extra
2322 // character if the line break is included.
2323#if defined(FEAT_DIFF) || defined(LINE_ATTR)
2324 // For a diff line the highlighting continues after the
2325 // "$".
2326 if (
2327# ifdef FEAT_DIFF
2328 diff_hlf == (hlf_T)0
2329# ifdef LINE_ATTR
2330 &&
2331# endif
2332# endif
2333# ifdef LINE_ATTR
2334 line_attr == 0
2335# endif
2336 )
2337#endif
2338 {
2339 // In virtualedit, visual selections may extend
2340 // beyond end of line.
2341 if (area_highlighting && virtual_active()
2342 && tocol != MAXCOL && vcol < tocol)
2343 n_extra = 0;
2344 else
2345 {
2346 p_extra = at_end_str;
2347 n_extra = 1;
2348 c_extra = NUL;
2349 c_final = NUL;
2350 }
2351 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002352 if (wp->w_p_list && wp->w_lcs_chars.eol > 0)
2353 c = wp->w_lcs_chars.eol;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002354 else
2355 c = ' ';
2356 lcs_eol_one = -1;
2357 --ptr; // put it back at the NUL
2358 if (!attr_pri)
2359 {
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002360 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002361 n_attr = 1;
2362 }
2363 mb_c = c;
2364 if (enc_utf8 && utf_char2len(c) > 1)
2365 {
2366 mb_utf8 = TRUE;
2367 u8cc[0] = 0;
2368 c = 0xc0;
2369 }
2370 else
2371 mb_utf8 = FALSE; // don't draw as UTF-8
2372 }
2373 else if (c != NUL)
2374 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02002375 p_extra = transchar_buf(wp->w_buffer, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002376 if (n_extra == 0)
2377 n_extra = byte2cells(c) - 1;
2378#ifdef FEAT_RIGHTLEFT
2379 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
2380 rl_mirror(p_extra); // reverse "<12>"
2381#endif
2382 c_extra = NUL;
2383 c_final = NUL;
2384#ifdef FEAT_LINEBREAK
2385 if (wp->w_p_lbr)
2386 {
2387 char_u *p;
2388
2389 c = *p_extra;
2390 p = alloc(n_extra + 1);
2391 vim_memset(p, ' ', n_extra);
2392 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
2393 p[n_extra] = NUL;
2394 vim_free(p_extra_free);
2395 p_extra_free = p_extra = p;
2396 }
2397 else
2398#endif
2399 {
2400 n_extra = byte2cells(c) - 1;
2401 c = *p_extra++;
2402 }
2403 if (!attr_pri)
2404 {
2405 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002406 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002407 saved_attr2 = char_attr; // save current attr
2408 }
2409 mb_utf8 = FALSE; // don't draw as UTF-8
2410 }
2411 else if (VIsual_active
2412 && (VIsual_mode == Ctrl_V
2413 || VIsual_mode == 'v')
2414 && virtual_active()
2415 && tocol != MAXCOL
2416 && vcol < tocol
2417 && (
2418#ifdef FEAT_RIGHTLEFT
2419 wp->w_p_rl ? (col >= 0) :
2420#endif
2421 (col < wp->w_width)))
2422 {
2423 c = ' ';
2424 --ptr; // put it back at the NUL
2425 }
2426#if defined(LINE_ATTR)
2427 else if ((
2428# ifdef FEAT_DIFF
2429 diff_hlf != (hlf_T)0 ||
2430# endif
2431# ifdef FEAT_TERMINAL
2432 win_attr != 0 ||
2433# endif
2434 line_attr != 0
2435 ) && (
2436# ifdef FEAT_RIGHTLEFT
2437 wp->w_p_rl ? (col >= 0) :
2438# endif
2439 (col
2440# ifdef FEAT_CONCEAL
2441 - boguscols
2442# endif
2443 < wp->w_width)))
2444 {
2445 // Highlight until the right side of the window
2446 c = ' ';
2447 --ptr; // put it back at the NUL
2448
2449 // Remember we do the char for line highlighting.
2450 ++did_line_attr;
2451
2452 // don't do search HL for the rest of the line
2453 if (line_attr != 0 && char_attr == search_attr
2454 && (did_line_attr > 1
Bram Moolenaareed9d462021-02-15 20:38:25 +01002455 || (wp->w_p_list &&
2456 wp->w_lcs_chars.eol > 0)))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002457 char_attr = line_attr;
2458# ifdef FEAT_DIFF
2459 if (diff_hlf == HLF_TXD)
2460 {
2461 diff_hlf = HLF_CHD;
2462 if (vi_attr == 0 || char_attr != vi_attr)
2463 {
2464 char_attr = HL_ATTR(diff_hlf);
2465 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2466 && wp->w_p_culopt_flags != CULOPT_NBR
2467 && (!cul_screenline
2468 || (vcol >= left_curline_col
2469 && vcol <= right_curline_col)))
2470 char_attr = hl_combine_attr(
2471 char_attr, HL_ATTR(HLF_CUL));
2472 }
2473 }
2474# endif
2475# ifdef FEAT_TERMINAL
2476 if (win_attr != 0)
2477 {
2478 char_attr = win_attr;
Bram Moolenaara3817732019-10-16 16:31:44 +02002479 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2480 && wp->w_p_culopt_flags != CULOPT_NBR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002481 {
2482 if (!cul_screenline || (vcol >= left_curline_col
2483 && vcol <= right_curline_col))
2484 char_attr = hl_combine_attr(
2485 char_attr, HL_ATTR(HLF_CUL));
2486 }
2487 else if (line_attr)
2488 char_attr = hl_combine_attr(char_attr, line_attr);
2489 }
2490# endif
2491 }
2492#endif
2493 }
2494
2495#ifdef FEAT_CONCEAL
2496 if ( wp->w_p_cole > 0
LemonBoy9830db62022-05-08 21:25:20 +01002497 && (wp != curwin || lnum != wp->w_cursor.lnum
2498 || conceal_cursor_line(wp))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002499 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
2500 && !(lnum_in_visual_area
2501 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
2502 {
2503 char_attr = conceal_attr;
LemonBoy9830db62022-05-08 21:25:20 +01002504 if (((prev_syntax_id != syntax_seqnr
2505 && (syntax_flags & HL_CONCEAL) != 0)
2506 || has_match_conc > 1)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002507 && (syn_get_sub_char() != NUL
2508 || (has_match_conc && match_conc)
2509 || wp->w_p_cole == 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002510 && wp->w_p_cole != 3)
2511 {
2512 // First time at this concealed item: display one
2513 // character.
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002514 if (has_match_conc && match_conc)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002515 c = match_conc;
2516 else if (syn_get_sub_char() != NUL)
2517 c = syn_get_sub_char();
Bram Moolenaareed9d462021-02-15 20:38:25 +01002518 else if (wp->w_lcs_chars.conceal != NUL)
2519 c = wp->w_lcs_chars.conceal;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002520 else
2521 c = ' ';
2522
2523 prev_syntax_id = syntax_seqnr;
2524
2525 if (n_extra > 0)
2526 vcol_off += n_extra;
2527 vcol += n_extra;
2528 if (wp->w_p_wrap && n_extra > 0)
2529 {
2530# ifdef FEAT_RIGHTLEFT
2531 if (wp->w_p_rl)
2532 {
2533 col -= n_extra;
2534 boguscols -= n_extra;
2535 }
2536 else
2537# endif
2538 {
2539 boguscols += n_extra;
2540 col += n_extra;
2541 }
2542 }
2543 n_extra = 0;
2544 n_attr = 0;
2545 }
2546 else if (n_skip == 0)
2547 {
2548 is_concealing = TRUE;
2549 n_skip = 1;
2550 }
2551 mb_c = c;
2552 if (enc_utf8 && utf_char2len(c) > 1)
2553 {
2554 mb_utf8 = TRUE;
2555 u8cc[0] = 0;
2556 c = 0xc0;
2557 }
2558 else
2559 mb_utf8 = FALSE; // don't draw as UTF-8
2560 }
2561 else
2562 {
2563 prev_syntax_id = 0;
2564 is_concealing = FALSE;
2565 }
2566
2567 if (n_skip > 0 && did_decrement_ptr)
2568 // not showing the '>', put pointer back to avoid getting stuck
2569 ++ptr;
2570
2571#endif // FEAT_CONCEAL
2572 }
2573
2574#ifdef FEAT_CONCEAL
2575 // In the cursor line and we may be concealing characters: correct
2576 // the cursor column when we reach its position.
2577 if (!did_wcol && draw_state == WL_LINE
2578 && wp == curwin && lnum == wp->w_cursor.lnum
2579 && conceal_cursor_line(wp)
2580 && (int)wp->w_virtcol <= vcol + n_skip)
2581 {
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002582# ifdef FEAT_RIGHTLEFT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002583 if (wp->w_p_rl)
2584 wp->w_wcol = wp->w_width - col + boguscols - 1;
2585 else
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002586# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002587 wp->w_wcol = col - boguscols;
2588 wp->w_wrow = row;
2589 did_wcol = TRUE;
2590 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002591# ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +01002592 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002593# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002594 }
2595#endif
2596
2597 // Don't override visual selection highlighting.
2598 if (n_attr > 0
2599 && draw_state == WL_LINE
2600 && !attr_pri)
2601 {
2602#ifdef LINE_ATTR
2603 if (line_attr)
2604 char_attr = hl_combine_attr(extra_attr, line_attr);
2605 else
2606#endif
2607 char_attr = extra_attr;
2608 }
2609
2610#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2611 // XIM don't send preedit_start and preedit_end, but they send
2612 // preedit_changed and commit. Thus Vim can't set "im_is_active", use
2613 // im_is_preediting() here.
2614 if (p_imst == IM_ON_THE_SPOT
2615 && xic != NULL
2616 && lnum == wp->w_cursor.lnum
Bram Moolenaar24959102022-05-07 20:01:16 +01002617 && (State & MODE_INSERT)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002618 && !p_imdisable
2619 && im_is_preediting()
2620 && draw_state == WL_LINE)
2621 {
2622 colnr_T tcol;
2623
2624 if (preedit_end_col == MAXCOL)
2625 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
2626 else
2627 tcol = preedit_end_col;
2628 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
2629 {
2630 if (feedback_old_attr < 0)
2631 {
2632 feedback_col = 0;
2633 feedback_old_attr = char_attr;
2634 }
2635 char_attr = im_get_feedback_attr(feedback_col);
2636 if (char_attr < 0)
2637 char_attr = feedback_old_attr;
2638 feedback_col++;
2639 }
2640 else if (feedback_old_attr >= 0)
2641 {
2642 char_attr = feedback_old_attr;
2643 feedback_old_attr = -1;
2644 feedback_col = 0;
2645 }
2646 }
2647#endif
2648 // Handle the case where we are in column 0 but not on the first
2649 // character of the line and the user wants us to show us a
2650 // special character (via 'listchars' option "precedes:<char>".
2651 if (lcs_prec_todo != NUL
2652 && wp->w_p_list
Bram Moolenaarbffba7f2019-09-20 17:00:17 +02002653 && (wp->w_p_wrap ?
2654 (wp->w_skipcol > 0 && row == 0) :
2655 wp->w_leftcol > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002656#ifdef FEAT_DIFF
2657 && filler_todo <= 0
2658#endif
2659 && draw_state > WL_NR
2660 && c != NUL)
2661 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002662 c = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002663 lcs_prec_todo = NUL;
2664 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2665 {
2666 // Double-width character being overwritten by the "precedes"
2667 // character, need to fill up half the character.
2668 c_extra = MB_FILLER_CHAR;
2669 c_final = NUL;
2670 n_extra = 1;
2671 n_attr = 2;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002672 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002673 }
2674 mb_c = c;
2675 if (enc_utf8 && utf_char2len(c) > 1)
2676 {
2677 mb_utf8 = TRUE;
2678 u8cc[0] = 0;
2679 c = 0xc0;
2680 }
2681 else
2682 mb_utf8 = FALSE; // don't draw as UTF-8
2683 if (!attr_pri)
2684 {
2685 saved_attr3 = char_attr; // save current attr
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002686 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002687 n_attr3 = 1;
2688 }
2689 }
2690
2691 // At end of the text line or just after the last character.
2692 if ((c == NUL
2693#if defined(LINE_ATTR)
2694 || did_line_attr == 1
2695#endif
2696 ) && eol_hl_off == 0)
2697 {
2698#ifdef FEAT_SEARCH_EXTRA
2699 // flag to indicate whether prevcol equals startcol of search_hl or
2700 // one of the matches
2701 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &screen_search_hl,
2702 (long)(ptr - line) - (c == NUL));
2703#endif
2704 // Invert at least one char, used for Visual and empty line or
2705 // highlight match at end of line. If it's beyond the last
2706 // char on the screen, just overwrite that one (tricky!) Not
2707 // needed when a '$' was displayed for 'list'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002708 if (wp->w_lcs_chars.eol == lcs_eol_one
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002709 && ((area_attr != 0 && vcol == fromcol
2710 && (VIsual_mode != Ctrl_V
2711 || lnum == VIsual.lnum
2712 || lnum == curwin->w_cursor.lnum)
2713 && c == NUL)
2714#ifdef FEAT_SEARCH_EXTRA
2715 // highlight 'hlsearch' match at end of line
2716 || (prevcol_hl_flag
2717# ifdef FEAT_SYN_HL
2718 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
2719 && !(wp == curwin && VIsual_active))
2720# endif
2721# ifdef FEAT_DIFF
2722 && diff_hlf == (hlf_T)0
2723# endif
2724# if defined(LINE_ATTR)
2725 && did_line_attr <= 1
2726# endif
2727 )
2728#endif
2729 ))
2730 {
2731 int n = 0;
2732
2733#ifdef FEAT_RIGHTLEFT
2734 if (wp->w_p_rl)
2735 {
2736 if (col < 0)
2737 n = 1;
2738 }
2739 else
2740#endif
2741 {
2742 if (col >= wp->w_width)
2743 n = -1;
2744 }
2745 if (n != 0)
2746 {
2747 // At the window boundary, highlight the last character
2748 // instead (better than nothing).
2749 off += n;
2750 col += n;
2751 }
2752 else
2753 {
2754 // Add a blank character to highlight.
2755 ScreenLines[off] = ' ';
2756 if (enc_utf8)
2757 ScreenLinesUC[off] = 0;
2758 }
2759#ifdef FEAT_SEARCH_EXTRA
2760 if (area_attr == 0)
2761 {
2762 // Use attributes from match with highest priority among
2763 // 'search_hl' and the match list.
2764 get_search_match_hl(wp, &screen_search_hl,
2765 (long)(ptr - line), &char_attr);
2766 }
2767#endif
2768 ScreenAttrs[off] = char_attr;
2769#ifdef FEAT_RIGHTLEFT
2770 if (wp->w_p_rl)
2771 {
2772 --col;
2773 --off;
2774 }
2775 else
2776#endif
2777 {
2778 ++col;
2779 ++off;
2780 }
2781 ++vcol;
2782 eol_hl_off = 1;
2783 }
2784 }
2785
2786 // At end of the text line.
2787 if (c == NUL)
2788 {
2789#ifdef FEAT_SYN_HL
2790 // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
2791 if (wp->w_p_wrap)
2792 v = wp->w_skipcol;
2793 else
2794 v = wp->w_leftcol;
2795
2796 // check if line ends before left margin
2797 if (vcol < v + col - win_col_off(wp))
2798 vcol = v + col - win_col_off(wp);
2799#ifdef FEAT_CONCEAL
2800 // Get rid of the boguscols now, we want to draw until the right
2801 // edge for 'cursorcolumn'.
2802 col -= boguscols;
2803 boguscols = 0;
2804#endif
2805
2806 if (draw_color_col)
2807 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2808
2809 if (((wp->w_p_cuc
2810 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
2811 && (int)wp->w_virtcol <
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002812 (long)wp->w_width * (row - startrow + 1) + v
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002813 && lnum != wp->w_cursor.lnum)
2814 || draw_color_col
2815 || win_attr != 0)
2816# ifdef FEAT_RIGHTLEFT
2817 && !wp->w_p_rl
2818# endif
2819 )
2820 {
2821 int rightmost_vcol = 0;
2822 int i;
2823
2824 if (wp->w_p_cuc)
2825 rightmost_vcol = wp->w_virtcol;
2826 if (draw_color_col)
2827 // determine rightmost colorcolumn to possibly draw
2828 for (i = 0; color_cols[i] >= 0; ++i)
2829 if (rightmost_vcol < color_cols[i])
2830 rightmost_vcol = color_cols[i];
2831
2832 while (col < wp->w_width)
2833 {
2834 ScreenLines[off] = ' ';
2835 if (enc_utf8)
2836 ScreenLinesUC[off] = 0;
2837 ++col;
2838 if (draw_color_col)
2839 draw_color_col = advance_color_col(VCOL_HLC,
2840 &color_cols);
2841
2842 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
2843 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
2844 else if (draw_color_col && VCOL_HLC == *color_cols)
2845 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
2846 else
2847 ScreenAttrs[off++] = win_attr;
2848
2849 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
2850 break;
2851
2852 ++vcol;
2853 }
2854 }
2855#endif
2856
2857 screen_line(screen_row, wp->w_wincol, col,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002858 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002859 row++;
2860
2861 // Update w_cline_height and w_cline_folded if the cursor line was
2862 // updated (saves a call to plines() later).
2863 if (wp == curwin && lnum == curwin->w_cursor.lnum)
2864 {
2865 curwin->w_cline_row = startrow;
2866 curwin->w_cline_height = row - startrow;
2867#ifdef FEAT_FOLDING
2868 curwin->w_cline_folded = FALSE;
2869#endif
2870 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2871 }
2872
2873 break;
2874 }
2875
2876 // Show "extends" character from 'listchars' if beyond the line end and
2877 // 'list' is set.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002878 if (wp->w_lcs_chars.ext != NUL
Bram Moolenaar41fb7232021-07-08 12:40:05 +02002879 && draw_state == WL_LINE
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002880 && wp->w_p_list
2881 && !wp->w_p_wrap
2882#ifdef FEAT_DIFF
2883 && filler_todo <= 0
2884#endif
2885 && (
2886#ifdef FEAT_RIGHTLEFT
2887 wp->w_p_rl ? col == 0 :
2888#endif
2889 col == wp->w_width - 1)
2890 && (*ptr != NUL
2891 || (wp->w_p_list && lcs_eol_one > 0)
2892 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
2893 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002894 c = wp->w_lcs_chars.ext;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002895 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002896 mb_c = c;
2897 if (enc_utf8 && utf_char2len(c) > 1)
2898 {
2899 mb_utf8 = TRUE;
2900 u8cc[0] = 0;
2901 c = 0xc0;
2902 }
2903 else
2904 mb_utf8 = FALSE;
2905 }
2906
2907#ifdef FEAT_SYN_HL
2908 // advance to the next 'colorcolumn'
2909 if (draw_color_col)
2910 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2911
2912 // Highlight the cursor column if 'cursorcolumn' is set. But don't
2913 // highlight the cursor position itself.
2914 // Also highlight the 'colorcolumn' if it is different than
2915 // 'cursorcolumn'
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002916 // Also highlight the 'colorcolumn' if 'breakindent' and/or 'showbreak'
2917 // options are set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002918 vcol_save_attr = -1;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002919 if (((draw_state == WL_LINE ||
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002920 draw_state == WL_BRI ||
2921 draw_state == WL_SBR) && !lnum_in_visual_area
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002922 && search_attr == 0 && area_attr == 0)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002923# ifdef FEAT_DIFF
2924 && filler_todo <= 0
2925# endif
2926 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002927 {
2928 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
2929 && lnum != wp->w_cursor.lnum)
2930 {
2931 vcol_save_attr = char_attr;
2932 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
2933 }
2934 else if (draw_color_col && VCOL_HLC == *color_cols)
2935 {
2936 vcol_save_attr = char_attr;
2937 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
2938 }
2939 }
2940#endif
2941
2942 // Store character to be displayed.
2943 // Skip characters that are left of the screen for 'nowrap'.
2944 vcol_prev = vcol;
2945 if (draw_state < WL_LINE || n_skip <= 0)
2946 {
2947 // Store the character.
2948#if defined(FEAT_RIGHTLEFT)
2949 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
2950 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002951 // A double-wide character is: put first half in left cell.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002952 --off;
2953 --col;
2954 }
2955#endif
2956 ScreenLines[off] = c;
2957 if (enc_dbcs == DBCS_JPNU)
2958 {
2959 if ((mb_c & 0xff00) == 0x8e00)
2960 ScreenLines[off] = 0x8e;
2961 ScreenLines2[off] = mb_c & 0xff;
2962 }
2963 else if (enc_utf8)
2964 {
2965 if (mb_utf8)
2966 {
2967 int i;
2968
2969 ScreenLinesUC[off] = mb_c;
2970 if ((c & 0xff) == 0)
2971 ScreenLines[off] = 0x80; // avoid storing zero
2972 for (i = 0; i < Screen_mco; ++i)
2973 {
2974 ScreenLinesC[i][off] = u8cc[i];
2975 if (u8cc[i] == 0)
2976 break;
2977 }
2978 }
2979 else
2980 ScreenLinesUC[off] = 0;
2981 }
2982 if (multi_attr)
2983 {
2984 ScreenAttrs[off] = multi_attr;
2985 multi_attr = 0;
2986 }
2987 else
2988 ScreenAttrs[off] = char_attr;
2989
2990 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2991 {
2992 // Need to fill two screen columns.
2993 ++off;
2994 ++col;
2995 if (enc_utf8)
2996 // UTF-8: Put a 0 in the second screen char.
2997 ScreenLines[off] = 0;
2998 else
2999 // DBCS: Put second byte in the second screen char.
3000 ScreenLines[off] = mb_c & 0xff;
3001 if (draw_state > WL_NR
3002#ifdef FEAT_DIFF
3003 && filler_todo <= 0
3004#endif
3005 )
3006 ++vcol;
3007 // When "tocol" is halfway a character, set it to the end of
3008 // the character, otherwise highlighting won't stop.
3009 if (tocol == vcol)
3010 ++tocol;
3011#ifdef FEAT_RIGHTLEFT
3012 if (wp->w_p_rl)
3013 {
3014 // now it's time to backup one cell
3015 --off;
3016 --col;
3017 }
3018#endif
3019 }
3020#ifdef FEAT_RIGHTLEFT
3021 if (wp->w_p_rl)
3022 {
3023 --off;
3024 --col;
3025 }
3026 else
3027#endif
3028 {
3029 ++off;
3030 ++col;
3031 }
3032 }
3033#ifdef FEAT_CONCEAL
3034 else if (wp->w_p_cole > 0 && is_concealing)
3035 {
3036 --n_skip;
3037 ++vcol_off;
3038 if (n_extra > 0)
3039 vcol_off += n_extra;
3040 if (wp->w_p_wrap)
3041 {
3042 // Special voodoo required if 'wrap' is on.
3043 //
3044 // Advance the column indicator to force the line
3045 // drawing to wrap early. This will make the line
3046 // take up the same screen space when parts are concealed,
3047 // so that cursor line computations aren't messed up.
3048 //
3049 // To avoid the fictitious advance of 'col' causing
3050 // trailing junk to be written out of the screen line
3051 // we are building, 'boguscols' keeps track of the number
3052 // of bad columns we have advanced.
3053 if (n_extra > 0)
3054 {
3055 vcol += n_extra;
3056# ifdef FEAT_RIGHTLEFT
3057 if (wp->w_p_rl)
3058 {
3059 col -= n_extra;
3060 boguscols -= n_extra;
3061 }
3062 else
3063# endif
3064 {
3065 col += n_extra;
3066 boguscols += n_extra;
3067 }
3068 n_extra = 0;
3069 n_attr = 0;
3070 }
3071
3072
3073 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
3074 {
3075 // Need to fill two screen columns.
3076# ifdef FEAT_RIGHTLEFT
3077 if (wp->w_p_rl)
3078 {
3079 --boguscols;
3080 --col;
3081 }
3082 else
3083# endif
3084 {
3085 ++boguscols;
3086 ++col;
3087 }
3088 }
3089
3090# ifdef FEAT_RIGHTLEFT
3091 if (wp->w_p_rl)
3092 {
3093 --boguscols;
3094 --col;
3095 }
3096 else
3097# endif
3098 {
3099 ++boguscols;
3100 ++col;
3101 }
3102 }
3103 else
3104 {
3105 if (n_extra > 0)
3106 {
3107 vcol += n_extra;
3108 n_extra = 0;
3109 n_attr = 0;
3110 }
3111 }
3112
3113 }
3114#endif // FEAT_CONCEAL
3115 else
3116 --n_skip;
3117
3118 // Only advance the "vcol" when after the 'number' or 'relativenumber'
3119 // column.
3120 if (draw_state > WL_NR
3121#ifdef FEAT_DIFF
3122 && filler_todo <= 0
3123#endif
3124 )
3125 ++vcol;
3126
3127#ifdef FEAT_SYN_HL
3128 if (vcol_save_attr >= 0)
3129 char_attr = vcol_save_attr;
3130#endif
3131
3132 // restore attributes after "predeces" in 'listchars'
3133 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
3134 char_attr = saved_attr3;
3135
3136 // restore attributes after last 'listchars' or 'number' char
3137 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
3138 char_attr = saved_attr2;
3139
3140 // At end of screen line and there is more to come: Display the line
3141 // so far. If there is no more to display it is caught above.
3142 if ((
3143#ifdef FEAT_RIGHTLEFT
3144 wp->w_p_rl ? (col < 0) :
3145#endif
3146 (col >= wp->w_width))
Bram Moolenaar41fb7232021-07-08 12:40:05 +02003147 && (draw_state != WL_LINE
3148 || *ptr != NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003149#ifdef FEAT_DIFF
3150 || filler_todo > 0
3151#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01003152 || (wp->w_p_list && wp->w_lcs_chars.eol != NUL
3153 && p_extra != at_end_str)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003154 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
3155 )
3156 {
3157#ifdef FEAT_CONCEAL
3158 screen_line(screen_row, wp->w_wincol, col - boguscols,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003159 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003160 boguscols = 0;
3161#else
3162 screen_line(screen_row, wp->w_wincol, col,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003163 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003164#endif
3165 ++row;
3166 ++screen_row;
3167
3168 // When not wrapping and finished diff lines, or when displayed
3169 // '$' and highlighting until last column, break here.
3170 if ((!wp->w_p_wrap
3171#ifdef FEAT_DIFF
3172 && filler_todo <= 0
3173#endif
3174 ) || lcs_eol_one == -1)
3175 break;
3176
3177 // When the window is too narrow draw all "@" lines.
3178 if (draw_state != WL_LINE
3179#ifdef FEAT_DIFF
3180 && filler_todo <= 0
3181#endif
3182 )
3183 {
3184 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
3185 draw_vsep_win(wp, row);
3186 row = endrow;
3187 }
3188
3189 // When line got too long for screen break here.
3190 if (row == endrow)
3191 {
3192 ++row;
3193 break;
3194 }
3195
3196 if (screen_cur_row == screen_row - 1
3197#ifdef FEAT_DIFF
3198 && filler_todo <= 0
3199#endif
3200 && wp->w_width == Columns)
3201 {
3202 // Remember that the line wraps, used for modeless copy.
3203 LineWraps[screen_row - 1] = TRUE;
3204
3205 // Special trick to make copy/paste of wrapped lines work with
3206 // xterm/screen: write an extra character beyond the end of
3207 // the line. This will work with all terminal types
3208 // (regardless of the xn,am settings).
3209 // Only do this on a fast tty.
3210 // Only do this if the cursor is on the current line
3211 // (something has been written in it).
3212 // Don't do this for the GUI.
3213 // Don't do this for double-width characters.
3214 // Don't do this for a window not at the right screen border.
3215 if (p_tf
3216#ifdef FEAT_GUI
3217 && !gui.in_use
3218#endif
3219 && !(has_mbyte
3220 && ((*mb_off2cells)(LineOffset[screen_row],
3221 LineOffset[screen_row] + screen_Columns)
3222 == 2
3223 || (*mb_off2cells)(LineOffset[screen_row - 1]
3224 + (int)Columns - 2,
3225 LineOffset[screen_row] + screen_Columns)
3226 == 2)))
3227 {
3228 // First make sure we are at the end of the screen line,
3229 // then output the same character again to let the
3230 // terminal know about the wrap. If the terminal doesn't
3231 // auto-wrap, we overwrite the character.
3232 if (screen_cur_col != wp->w_width)
3233 screen_char(LineOffset[screen_row - 1]
3234 + (unsigned)Columns - 1,
3235 screen_row - 1, (int)(Columns - 1));
3236
3237 // When there is a multi-byte character, just output a
3238 // space to keep it simple.
3239 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
3240 screen_row - 1] + (Columns - 1)]) > 1)
3241 out_char(' ');
3242 else
3243 out_char(ScreenLines[LineOffset[screen_row - 1]
3244 + (Columns - 1)]);
3245 // force a redraw of the first char on the next line
3246 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
3247 screen_start(); // don't know where cursor is now
3248 }
3249 }
3250
3251 col = 0;
3252 off = (unsigned)(current_ScreenLine - ScreenLines);
3253#ifdef FEAT_RIGHTLEFT
3254 if (wp->w_p_rl)
3255 {
3256 col = wp->w_width - 1; // col is not used if breaking!
3257 off += col;
3258 }
3259#endif
3260
3261 // reset the drawing state for the start of a wrapped line
3262 draw_state = WL_START;
3263 saved_n_extra = n_extra;
3264 saved_p_extra = p_extra;
3265 saved_c_extra = c_extra;
3266 saved_c_final = c_final;
3267#ifdef FEAT_SYN_HL
3268 if (!(cul_screenline
3269# ifdef FEAT_DIFF
Bram Moolenaare7705982020-04-21 22:23:15 +02003270 && diff_hlf == (hlf_T)0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003271# endif
Bram Moolenaare7705982020-04-21 22:23:15 +02003272 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003273 saved_char_attr = char_attr;
3274 else
3275#endif
3276 saved_char_attr = 0;
3277 n_extra = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01003278 lcs_prec_todo = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003279#ifdef FEAT_LINEBREAK
3280# ifdef FEAT_DIFF
3281 if (filler_todo <= 0)
3282# endif
3283 need_showbreak = TRUE;
3284#endif
3285#ifdef FEAT_DIFF
3286 --filler_todo;
3287 // When the filler lines are actually below the last line of the
3288 // file, don't draw the line itself, break here.
3289 if (filler_todo == 0 && wp->w_botfill)
3290 break;
3291#endif
3292 }
3293
3294 } // for every character in the line
3295
3296#ifdef FEAT_SPELL
3297 // After an empty line check first word for capital.
3298 if (*skipwhite(line) == NUL)
3299 {
3300 capcol_lnum = lnum + 1;
3301 cap_col = 0;
3302 }
3303#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003304#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003305 vim_free(text_props);
3306 vim_free(text_prop_idxs);
3307#endif
3308
3309 vim_free(p_extra_free);
3310 return row;
3311}