blob: e91ef291dcc6db91775ccc823667546f2397cd3e [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;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100782 trailcol += (colnr_T)(ptr - line);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200783 }
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
Bram Moolenaarb9081882022-07-09 04:56:24 +0100795 leadcol += (colnr_T)(ptr - line) + 1;
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100796 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200797 }
798
799 wcr_attr = get_wcr_attr(wp);
800 if (wcr_attr != 0)
801 {
802 win_attr = wcr_attr;
803 area_highlighting = TRUE;
804 }
Bram Moolenaar34390282019-10-16 14:38:26 +0200805
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100806#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200807 if (WIN_IS_POPUP(wp))
808 screen_line_flags |= SLF_POPUP;
809#endif
810
811 // 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
812 // first character to be displayed.
813 if (wp->w_p_wrap)
814 v = wp->w_skipcol;
815 else
816 v = wp->w_leftcol;
817 if (v > 0 && !number_only)
818 {
819 char_u *prev_ptr = ptr;
820
821 while (vcol < v && *ptr != NUL)
822 {
823 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
824 vcol += c;
825 prev_ptr = ptr;
826 MB_PTR_ADV(ptr);
827 }
828
829 // When:
830 // - 'cuc' is set, or
831 // - 'colorcolumn' is set, or
832 // - 'virtualedit' is set, or
833 // - the visual mode is active,
834 // the end of the line may be before the start of the displayed part.
835 if (vcol < v && (
836#ifdef FEAT_SYN_HL
837 wp->w_p_cuc || draw_color_col ||
838#endif
839 virtual_active() ||
840 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
841 vcol = v;
842
843 // Handle a character that's not completely on the screen: Put ptr at
844 // that character but skip the first few screen characters.
845 if (vcol > v)
846 {
847 vcol -= c;
848 ptr = prev_ptr;
849 // If the character fits on the screen, don't need to skip it.
850 // Except for a TAB.
851 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
852 n_skip = v - vcol;
853 }
854
855 // Adjust for when the inverted text is before the screen,
856 // and when the start of the inverted text is before the screen.
857 if (tocol <= vcol)
858 fromcol = 0;
859 else if (fromcol >= 0 && fromcol < vcol)
860 fromcol = vcol;
861
862#ifdef FEAT_LINEBREAK
863 // When w_skipcol is non-zero, first line needs 'showbreak'
864 if (wp->w_p_wrap)
865 need_showbreak = TRUE;
866#endif
867#ifdef FEAT_SPELL
868 // When spell checking a word we need to figure out the start of the
869 // word and if it's badly spelled or not.
870 if (has_spell)
871 {
872 int len;
873 colnr_T linecol = (colnr_T)(ptr - line);
874 hlf_T spell_hlf = HLF_COUNT;
875
876 pos = wp->w_cursor;
877 wp->w_cursor.lnum = lnum;
878 wp->w_cursor.col = linecol;
879 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
880
881 // spell_move_to() may call ml_get() and make "line" invalid
882 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
883 ptr = line + linecol;
884
885 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
886 {
887 // no bad word found at line start, don't check until end of a
888 // word
889 spell_hlf = HLF_COUNT;
890 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
891 }
892 else
893 {
894 // bad word found, use attributes until end of word
895 word_end = wp->w_cursor.col + len + 1;
896
897 // Turn index into actual attributes.
898 if (spell_hlf != HLF_COUNT)
899 spell_attr = highlight_attr[spell_hlf];
900 }
901 wp->w_cursor = pos;
902
903# ifdef FEAT_SYN_HL
904 // Need to restart syntax highlighting for this line.
905 if (has_syntax)
906 syntax_start(wp, lnum);
907# endif
908 }
909#endif
910 }
911
912 // Correct highlighting for cursor that can't be disabled.
913 // Avoids having to check this for each character.
914 if (fromcol >= 0)
915 {
916 if (noinvcur)
917 {
918 if ((colnr_T)fromcol == wp->w_virtcol)
919 {
920 // highlighting starts at cursor, let it start just after the
921 // cursor
922 fromcol_prev = fromcol;
923 fromcol = -1;
924 }
925 else if ((colnr_T)fromcol < wp->w_virtcol)
926 // restart highlighting after the cursor
927 fromcol_prev = wp->w_virtcol;
928 }
929 if (fromcol >= tocol)
930 fromcol = -1;
931 }
932
933#ifdef FEAT_SEARCH_EXTRA
934 if (!number_only)
935 {
936 v = (long)(ptr - line);
937 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
938 &line, &screen_search_hl,
939 &search_attr);
940 ptr = line + v; // "line" may have been updated
941 }
942#endif
943
944#ifdef FEAT_SYN_HL
945 // Cursor line highlighting for 'cursorline' in the current window.
946 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
947 {
948 // Do not show the cursor line in the text when Visual mode is active,
zeertzjqc20e46a2022-03-23 14:55:23 +0000949 // because it's not clear what is selected then.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200950 if (!(wp == curwin && VIsual_active)
951 && wp->w_p_culopt_flags != CULOPT_NBR)
952 {
953 cul_screenline = (wp->w_p_wrap
954 && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
955
956 // Only set line_attr here when "screenline" is not present in
957 // 'cursorlineopt'. Otherwise it's done later.
958 if (!cul_screenline)
959 {
960 cul_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar39f7aa32020-08-31 22:00:05 +0200961# ifdef FEAT_SIGNS
962 // Combine the 'cursorline' and sign highlighting, depending on
963 // the sign priority.
964 if (sign_present && sattr.sat_linehl > 0)
965 {
966 if (sattr.sat_priority >= 100)
967 line_attr = hl_combine_attr(cul_attr, line_attr);
968 else
969 line_attr = hl_combine_attr(line_attr, cul_attr);
970 }
971 else
972# endif
Bram Moolenaar7fe956d2022-07-03 14:21:09 +0100973# if defined(FEAT_QUICKFIX)
974 line_attr = hl_combine_attr(line_attr, cul_attr);
975# else
Bram Moolenaar39f7aa32020-08-31 22:00:05 +0200976 line_attr = cul_attr;
Bram Moolenaar7fe956d2022-07-03 14:21:09 +0100977# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200978 }
979 else
980 {
981 line_attr_save = line_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200982 margin_columns_win(wp, &left_curline_col, &right_curline_col);
983 }
984 area_highlighting = TRUE;
985 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200986 }
987#endif
988
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100989#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200990 {
991 char_u *prop_start;
992
993 text_prop_count = get_text_props(wp->w_buffer, lnum,
994 &prop_start, FALSE);
995 if (text_prop_count > 0)
996 {
997 // Make a copy of the properties, so that they are properly
998 // aligned.
999 text_props = ALLOC_MULT(textprop_T, text_prop_count);
1000 if (text_props != NULL)
1001 mch_memmove(text_props, prop_start,
1002 text_prop_count * sizeof(textprop_T));
1003
1004 // Allocate an array for the indexes.
1005 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
1006 area_highlighting = TRUE;
1007 extra_check = TRUE;
1008 }
1009 }
1010#endif
1011
1012 off = (unsigned)(current_ScreenLine - ScreenLines);
1013 col = 0;
1014
1015#ifdef FEAT_RIGHTLEFT
1016 if (wp->w_p_rl)
1017 {
1018 // Rightleft window: process the text in the normal direction, but put
1019 // it in current_ScreenLine[] from right to left. Start at the
1020 // rightmost column of the window.
1021 col = wp->w_width - 1;
1022 off += col;
1023 screen_line_flags |= SLF_RIGHTLEFT;
1024 }
1025#endif
1026
1027 // Repeat for the whole displayed line.
1028 for (;;)
1029 {
Bram Moolenaarb9081882022-07-09 04:56:24 +01001030 char_u *prev_ptr = ptr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001031#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaarb9081882022-07-09 04:56:24 +01001032 int has_match_conc = 0; // match wants to conceal
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001033#endif
1034#ifdef FEAT_CONCEAL
Bram Moolenaarb9081882022-07-09 04:56:24 +01001035 int did_decrement_ptr = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001036#endif
Bram Moolenaarb9081882022-07-09 04:56:24 +01001037
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001038 // Skip this quickly when working on the text.
1039 if (draw_state != WL_LINE)
1040 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001041#ifdef FEAT_SYN_HL
1042 if (cul_screenline)
1043 {
1044 cul_attr = 0;
1045 line_attr = line_attr_save;
1046 }
1047#endif
1048
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001049#ifdef FEAT_CMDWIN
1050 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
1051 {
1052 draw_state = WL_CMDLINE;
1053 if (cmdwin_type != 0 && wp == curwin)
1054 {
1055 // Draw the cmdline character.
1056 n_extra = 1;
1057 c_extra = cmdwin_type;
1058 c_final = NUL;
1059 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
1060 }
1061 }
1062#endif
1063
1064#ifdef FEAT_FOLDING
1065 if (draw_state == WL_FOLD - 1 && n_extra == 0)
1066 {
1067 int fdc = compute_foldcolumn(wp, 0);
1068
1069 draw_state = WL_FOLD;
1070 if (fdc > 0)
1071 {
1072 // Draw the 'foldcolumn'. Allocate a buffer, "extra" may
1073 // already be in use.
1074 vim_free(p_extra_free);
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001075 p_extra_free = alloc(MAX_MCO * fdc + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001076 if (p_extra_free != NULL)
1077 {
Bram Moolenaar9355ae42021-03-08 19:04:05 +01001078 n_extra = (int)fill_foldcolumn(p_extra_free, wp,
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001079 FALSE, lnum);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001080 p_extra_free[n_extra] = NUL;
1081 p_extra = p_extra_free;
1082 c_extra = NUL;
1083 c_final = NUL;
Bram Moolenaare413ea02021-11-24 16:20:13 +00001084 if (use_cursor_line_sign(wp, lnum))
1085 char_attr =
1086 hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLF));
1087 else
1088 char_attr =
1089 hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001090 }
1091 }
1092 }
1093#endif
1094
1095#ifdef FEAT_SIGNS
1096 if (draw_state == WL_SIGN - 1 && n_extra == 0)
1097 {
1098 draw_state = WL_SIGN;
1099 // Show the sign column when there are any signs in this
1100 // buffer or when using Netbeans.
1101 if (signcolumn_on(wp))
1102 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
1103 row, startrow, filler_lines, filler_todo, &c_extra,
1104 &c_final, extra, &p_extra, &n_extra, &char_attr);
1105 }
1106#endif
1107
1108 if (draw_state == WL_NR - 1 && n_extra == 0)
1109 {
1110 draw_state = WL_NR;
1111 // Display the absolute or relative line number. After the
1112 // first fill with blanks when the 'n' flag isn't in 'cpo'
1113 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar936dc602022-03-06 20:47:01 +00001114 && (row == startrow + filler_lines
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001115 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
1116 {
1117#ifdef FEAT_SIGNS
1118 // If 'signcolumn' is set to 'number' and a sign is present
1119 // in 'lnum', then display the sign instead of the line
1120 // number.
1121 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
1122 && sign_present)
1123 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
1124 row, startrow, filler_lines, filler_todo,
1125 &c_extra, &c_final, extra, &p_extra, &n_extra,
1126 &char_attr);
1127 else
1128#endif
1129 {
1130 // Draw the line number (empty space after wrapping).
Bram Moolenaar936dc602022-03-06 20:47:01 +00001131 if (row == startrow + filler_lines)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001132 {
1133 long num;
1134 char *fmt = "%*ld ";
1135
1136 if (wp->w_p_nu && !wp->w_p_rnu)
1137 // 'number' + 'norelativenumber'
1138 num = (long)lnum;
1139 else
1140 {
1141 // 'relativenumber', don't use negative numbers
1142 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1143 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1144 {
1145 // 'number' + 'relativenumber'
1146 num = lnum;
1147 fmt = "%-*ld ";
1148 }
1149 }
1150
1151 sprintf((char *)extra, fmt,
1152 number_width(wp), num);
1153 if (wp->w_skipcol > 0)
1154 for (p_extra = extra; *p_extra == ' '; ++p_extra)
1155 *p_extra = '-';
1156#ifdef FEAT_RIGHTLEFT
1157 if (wp->w_p_rl) // reverse line numbers
1158 {
1159 char_u *p1, *p2;
1160 int t;
1161
1162 // like rl_mirror(), but keep the space at the end
Christian Brabandt29f0dc32021-06-16 19:28:34 +02001163 p2 = skipwhite(extra);
1164 p2 = skiptowhite(p2) - 1;
1165 for (p1 = skipwhite(extra); p1 < p2; ++p1, --p2)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001166 {
1167 t = *p1;
1168 *p1 = *p2;
1169 *p2 = t;
1170 }
1171 }
1172#endif
1173 p_extra = extra;
1174 c_extra = NUL;
1175 c_final = NUL;
1176 }
1177 else
1178 {
1179 c_extra = ' ';
1180 c_final = NUL;
1181 }
1182 n_extra = number_width(wp) + 1;
1183 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
1184#ifdef FEAT_SYN_HL
1185 // When 'cursorline' is set highlight the line number of
1186 // the current line differently.
zeertzjq754d2b42022-03-13 13:40:45 +00001187 // When 'cursorlineopt' does not have "line" only
1188 // highlight the line number itself.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001189 // TODO: Can we use CursorLine instead of CursorLineNr
1190 // when CursorLineNr isn't set?
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001191 if (wp->w_p_cul
1192 && lnum == wp->w_cursor.lnum
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001193 && (wp->w_p_culopt_flags & CULOPT_NBR)
Bram Moolenaar127969c2022-03-06 19:54:13 +00001194 && (row == startrow + filler_lines
1195 || (row > startrow + filler_lines
zeertzjq754d2b42022-03-13 13:40:45 +00001196 && (wp->w_p_culopt_flags & CULOPT_LINE))))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001197 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
1198#endif
Bram Moolenaarefae76a2019-10-27 22:54:58 +01001199 if (wp->w_p_rnu && lnum < wp->w_cursor.lnum
1200 && HL_ATTR(HLF_LNA) != 0)
1201 // Use LineNrAbove
1202 char_attr = hl_combine_attr(wcr_attr,
1203 HL_ATTR(HLF_LNA));
1204 if (wp->w_p_rnu && lnum > wp->w_cursor.lnum
1205 && HL_ATTR(HLF_LNB) != 0)
1206 // Use LineNrBelow
1207 char_attr = hl_combine_attr(wcr_attr,
1208 HL_ATTR(HLF_LNB));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001209 }
James McCoya80aad72021-12-22 19:45:28 +00001210#ifdef FEAT_SIGNS
1211 if (num_attr)
1212 char_attr = num_attr;
1213#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001214 }
1215 }
1216
1217#ifdef FEAT_LINEBREAK
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001218 if (wp->w_briopt_sbr && draw_state == WL_BRI - 1
Bram Moolenaaree857022019-11-09 23:26:40 +01001219 && n_extra == 0 && *get_showbreak_value(wp) != NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001220 // draw indent after showbreak value
1221 draw_state = WL_BRI;
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001222 else if (wp->w_briopt_sbr && draw_state == WL_SBR && n_extra == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001223 // After the showbreak, draw the breakindent
1224 draw_state = WL_BRI - 1;
1225
1226 // draw 'breakindent': indent wrapped text accordingly
1227 if (draw_state == WL_BRI - 1 && n_extra == 0)
1228 {
1229 draw_state = WL_BRI;
1230 // if need_showbreak is set, breakindent also applies
Bram Moolenaarfe154992022-03-22 20:42:12 +00001231 if (wp->w_p_bri && (row != startrow || need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001232# ifdef FEAT_DIFF
1233 && filler_lines == 0
1234# endif
1235 )
1236 {
1237 char_attr = 0;
1238# ifdef FEAT_DIFF
1239 if (diff_hlf != (hlf_T)0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001240 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001241# endif
1242 p_extra = NULL;
1243 c_extra = ' ';
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001244 c_final = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001245 n_extra = get_breakindent_win(wp,
1246 ml_get_buf(wp->w_buffer, lnum, FALSE));
Bram Moolenaare882f7a2020-05-16 14:07:39 +02001247 if (row == startrow)
1248 {
1249 n_extra -= win_col_off2(wp);
1250 if (n_extra < 0)
1251 n_extra = 0;
1252 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001253 if (wp->w_skipcol > 0 && wp->w_p_wrap && wp->w_briopt_sbr)
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001254 need_showbreak = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001255 // Correct end of highlighted area for 'breakindent',
1256 // required when 'linebreak' is also set.
1257 if (tocol == vcol)
1258 tocol += n_extra;
1259 }
1260 }
1261#endif
1262
1263#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
1264 if (draw_state == WL_SBR - 1 && n_extra == 0)
1265 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001266 char_u *sbr;
1267
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001268 draw_state = WL_SBR;
1269# ifdef FEAT_DIFF
1270 if (filler_todo > 0)
1271 {
1272 // Draw "deleted" diff line(s).
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001273 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001274 {
1275 c_extra = '-';
1276 c_final = NUL;
1277 }
1278 else
1279 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001280 c_extra = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001281 c_final = NUL;
1282 }
1283# ifdef FEAT_RIGHTLEFT
1284 if (wp->w_p_rl)
1285 n_extra = col + 1;
1286 else
1287# endif
1288 n_extra = wp->w_width - col;
1289 char_attr = HL_ATTR(HLF_DED);
1290 }
1291# endif
1292# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001293 sbr = get_showbreak_value(wp);
1294 if (*sbr != NUL && need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001295 {
1296 // Draw 'showbreak' at the start of each broken line.
Bram Moolenaaree857022019-11-09 23:26:40 +01001297 p_extra = sbr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001298 c_extra = NUL;
1299 c_final = NUL;
Bram Moolenaaree857022019-11-09 23:26:40 +01001300 n_extra = (int)STRLEN(sbr);
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001301 if (wp->w_skipcol == 0 || !wp->w_p_wrap)
1302 need_showbreak = FALSE;
Bram Moolenaaree857022019-11-09 23:26:40 +01001303 vcol_sbr = vcol + MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001304 // Correct end of highlighted area for 'showbreak',
1305 // required when 'linebreak' is also set.
1306 if (tocol == vcol)
1307 tocol += n_extra;
1308 // combine 'showbreak' with 'wincolor'
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001309 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001310# ifdef FEAT_SYN_HL
1311 // combine 'showbreak' with 'cursorline'
1312 if (cul_attr != 0)
1313 char_attr = hl_combine_attr(char_attr, cul_attr);
1314# endif
1315 }
1316# endif
1317 }
1318#endif
1319
1320 if (draw_state == WL_LINE - 1 && n_extra == 0)
1321 {
1322 draw_state = WL_LINE;
1323 if (saved_n_extra)
1324 {
1325 // Continue item from end of wrapped line.
1326 n_extra = saved_n_extra;
1327 c_extra = saved_c_extra;
1328 c_final = saved_c_final;
1329 p_extra = saved_p_extra;
1330 char_attr = saved_char_attr;
1331 }
1332 else
1333 char_attr = win_attr;
1334 }
1335 }
1336#ifdef FEAT_SYN_HL
zeertzjq4f33bc22021-08-05 17:57:02 +02001337 if (cul_screenline && draw_state == WL_LINE
1338 && vcol >= left_curline_col
1339 && vcol < right_curline_col)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001340 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001341 cul_attr = HL_ATTR(HLF_CUL);
1342 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001343 }
1344#endif
1345
1346 // When still displaying '$' of change command, stop at cursor.
1347 // When only displaying the (relative) line number and that's done,
1348 // stop here.
Bram Moolenaar511feec2020-06-18 19:15:27 +02001349 if (((dollar_vcol >= 0 && wp == curwin
1350 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol)
1351 || (number_only && draw_state > WL_NR))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001352#ifdef FEAT_DIFF
1353 && filler_todo <= 0
1354#endif
1355 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001356 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001357 screen_line(wp, screen_row, wp->w_wincol, col, -wp->w_width,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001358 screen_line_flags);
1359 // Pretend we have finished updating the window. Except when
1360 // 'cursorcolumn' is set.
1361#ifdef FEAT_SYN_HL
1362 if (wp->w_p_cuc)
1363 row = wp->w_cline_row + wp->w_cline_height;
1364 else
1365#endif
1366 row = wp->w_height;
1367 break;
1368 }
1369
Bram Moolenaar34390282019-10-16 14:38:26 +02001370 if (draw_state == WL_LINE && (area_highlighting || extra_check))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001371 {
1372 // handle Visual or match highlighting in this line
1373 if (vcol == fromcol
1374 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
1375 && (*mb_ptr2cells)(ptr) > 1)
1376 || ((int)vcol_prev == fromcol_prev
1377 && vcol_prev < vcol // not at margin
1378 && vcol < tocol))
1379 area_attr = vi_attr; // start highlighting
1380 else if (area_attr != 0
1381 && (vcol == tocol
1382 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
1383 area_attr = 0; // stop highlighting
1384
1385#ifdef FEAT_SEARCH_EXTRA
1386 if (!n_extra)
1387 {
1388 // Check for start/end of 'hlsearch' and other matches.
1389 // After end, check for start/end of next match.
1390 // When another match, have to check for start again.
1391 v = (long)(ptr - line);
1392 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
1393 &screen_search_hl, &has_match_conc,
Bram Moolenaar0c359af2021-11-29 19:18:57 +00001394 &match_conc, did_line_attr, lcs_eol_one,
1395 &on_last_col);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001396 ptr = line + v; // "line" may have been changed
Bram Moolenaarb9081882022-07-09 04:56:24 +01001397 prev_ptr = ptr;
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001398
1399 // Do not allow a conceal over EOL otherwise EOL will be missed
1400 // and bad things happen.
1401 if (*ptr == NUL)
1402 has_match_conc = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001403 }
1404#endif
1405
1406#ifdef FEAT_DIFF
1407 if (diff_hlf != (hlf_T)0)
1408 {
1409 if (diff_hlf == HLF_CHD && ptr - line >= change_start
1410 && n_extra == 0)
1411 diff_hlf = HLF_TXD; // changed text
1412 if (diff_hlf == HLF_TXD && ptr - line > change_end
1413 && n_extra == 0)
1414 diff_hlf = HLF_CHD; // changed line
1415 line_attr = HL_ATTR(diff_hlf);
1416 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
1417 && wp->w_p_culopt_flags != CULOPT_NBR
1418 && (!cul_screenline || (vcol >= left_curline_col
1419 && vcol <= right_curline_col)))
1420 line_attr = hl_combine_attr(
1421 line_attr, HL_ATTR(HLF_CUL));
1422 }
1423#endif
1424
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001425#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001426 if (text_props != NULL)
1427 {
1428 int pi;
1429 int bcol = (int)(ptr - line);
1430
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001431 if (n_extra > 0
1432# ifdef FEAT_LINEBREAK
1433 && !in_linebreak
1434# endif
1435 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001436 --bcol; // still working on the previous char, e.g. Tab
1437
1438 // Check if any active property ends.
1439 for (pi = 0; pi < text_props_active; ++pi)
1440 {
1441 int tpi = text_prop_idxs[pi];
1442
1443 if (bcol >= text_props[tpi].tp_col - 1
1444 + text_props[tpi].tp_len)
1445 {
1446 if (pi + 1 < text_props_active)
1447 mch_memmove(text_prop_idxs + pi,
1448 text_prop_idxs + pi + 1,
1449 sizeof(int)
1450 * (text_props_active - (pi + 1)));
1451 --text_props_active;
1452 --pi;
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001453# ifdef FEAT_LINEBREAK
1454 // not exactly right but should work in most cases
1455 if (in_linebreak && syntax_attr == text_prop_attr)
1456 syntax_attr = 0;
1457# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001458 }
1459 }
1460
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001461# ifdef FEAT_LINEBREAK
1462 if (n_extra > 0 && in_linebreak)
1463 // not on the next char yet, don't start another prop
1464 --bcol;
1465# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001466 // Add any text property that starts in this column.
1467 while (text_prop_next < text_prop_count
1468 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001469 {
1470 if (bcol <= text_props[text_prop_next].tp_col - 1
1471 + text_props[text_prop_next].tp_len)
1472 text_prop_idxs[text_props_active++] = text_prop_next;
1473 ++text_prop_next;
1474 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001475
1476 text_prop_attr = 0;
1477 text_prop_combine = FALSE;
Bram Moolenaar053f7122019-09-23 22:17:15 +02001478 text_prop_type = NULL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001479 if (text_props_active > 0)
1480 {
1481 // Sort the properties on priority and/or starting last.
1482 // Then combine the attributes, highest priority last.
1483 current_text_props = text_props;
1484 current_buf = wp->w_buffer;
1485 qsort((void *)text_prop_idxs, (size_t)text_props_active,
1486 sizeof(int), text_prop_compare);
1487
1488 for (pi = 0; pi < text_props_active; ++pi)
1489 {
1490 int tpi = text_prop_idxs[pi];
1491 proptype_T *pt = text_prop_type_by_id(
1492 wp->w_buffer, text_props[tpi].tp_type);
1493
1494 if (pt != NULL && pt->pt_hl_id > 0)
1495 {
1496 int pt_attr = syn_id2attr(pt->pt_hl_id);
1497
1498 text_prop_type = pt;
1499 text_prop_attr =
1500 hl_combine_attr(text_prop_attr, pt_attr);
1501 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
1502 }
1503 }
1504 }
1505 }
1506#endif
1507
Bram Moolenaara74fda62019-10-19 17:38:03 +02001508#ifdef FEAT_SYN_HL
Bram Moolenaara74fda62019-10-19 17:38:03 +02001509 if (extra_check && n_extra == 0)
Bram Moolenaar34390282019-10-16 14:38:26 +02001510 {
Bram Moolenaar82260af2019-10-20 13:16:22 +02001511 syntax_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001512# ifdef FEAT_TERMINAL
Bram Moolenaar34390282019-10-16 14:38:26 +02001513 if (get_term_attr)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001514 syntax_attr = term_get_attr(wp, lnum, vcol);
Bram Moolenaara74fda62019-10-19 17:38:03 +02001515# endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001516 // Get syntax attribute.
1517 if (has_syntax)
1518 {
1519 // Get the syntax attribute for the character. If there
1520 // is an error, disable syntax highlighting.
1521 save_did_emsg = did_emsg;
1522 did_emsg = FALSE;
1523
1524 v = (long)(ptr - line);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001525 if (v == prev_syntax_col)
1526 // at same column again
1527 syntax_attr = prev_syntax_attr;
1528 else
1529 {
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001530# ifdef FEAT_SPELL
Bram Moolenaar9115c612019-10-16 16:57:06 +02001531 can_spell = TRUE;
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001532# endif
Bram Moolenaar9115c612019-10-16 16:57:06 +02001533 syntax_attr = get_syntax_attr((colnr_T)v,
Bram Moolenaar34390282019-10-16 14:38:26 +02001534# ifdef FEAT_SPELL
1535 has_spell ? &can_spell :
1536# endif
1537 NULL, FALSE);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001538 prev_syntax_col = v;
1539 prev_syntax_attr = syntax_attr;
1540 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001541
Bram Moolenaar34390282019-10-16 14:38:26 +02001542 if (did_emsg)
1543 {
1544 wp->w_s->b_syn_error = TRUE;
1545 has_syntax = FALSE;
1546 syntax_attr = 0;
1547 }
1548 else
1549 did_emsg = save_did_emsg;
1550# ifdef SYN_TIME_LIMIT
1551 if (wp->w_s->b_syn_slow)
1552 has_syntax = FALSE;
1553# endif
1554
1555 // Need to get the line again, a multi-line regexp may
1556 // have made it invalid.
1557 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1558 ptr = line + v;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001559 prev_ptr = ptr;
Bram Moolenaar34390282019-10-16 14:38:26 +02001560# ifdef FEAT_CONCEAL
1561 // no concealing past the end of the line, it interferes
1562 // with line highlighting
1563 if (*ptr == NUL)
1564 syntax_flags = 0;
1565 else
1566 syntax_flags = get_syntax_info(&syntax_seqnr);
1567# endif
1568 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001569 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001570# ifdef FEAT_PROP_POPUP
Bram Moolenaardbd43162019-11-09 21:28:14 +01001571 // Combine text property highlight into syntax highlight.
1572 if (text_prop_type != NULL)
1573 {
1574 if (text_prop_combine)
1575 syntax_attr = hl_combine_attr(syntax_attr, text_prop_attr);
1576 else
1577 syntax_attr = text_prop_attr;
1578 }
1579# endif
Bram Moolenaara74fda62019-10-19 17:38:03 +02001580#endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001581
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001582 // Decide which of the highlight attributes to use.
1583 attr_pri = TRUE;
1584#ifdef LINE_ATTR
1585 if (area_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001586 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001587 char_attr = hl_combine_attr(line_attr, area_attr);
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001588 if (!highlight_match)
1589 // let search highlight show in Visual area if possible
1590 char_attr = hl_combine_attr(search_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001591# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001592 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001593# endif
1594 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001595 else if (search_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001596 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001597 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001598# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001599 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001600# endif
1601 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001602 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
1603 || vcol < fromcol || vcol_prev < fromcol_prev
1604 || vcol >= tocol))
1605 {
1606 // Use line_attr when not in the Visual or 'incsearch' area
1607 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar34390282019-10-16 14:38:26 +02001608# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001609 char_attr = hl_combine_attr(syntax_attr, line_attr);
1610# else
1611 char_attr = line_attr;
Bram Moolenaar34390282019-10-16 14:38:26 +02001612# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001613 attr_pri = FALSE;
1614 }
1615#else
1616 if (area_attr != 0)
1617 char_attr = area_attr;
1618 else if (search_attr != 0)
1619 char_attr = search_attr;
1620#endif
1621 else
1622 {
1623 attr_pri = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001624#ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001625 char_attr = syntax_attr;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001626#else
Bram Moolenaardbd43162019-11-09 21:28:14 +01001627 char_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001628#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001629 }
1630 }
Bram Moolenaar024dbd22019-11-02 22:00:15 +01001631
1632 // combine attribute with 'wincolor'
1633 if (win_attr != 0)
1634 {
1635 if (char_attr == 0)
1636 char_attr = win_attr;
1637 else
1638 char_attr = hl_combine_attr(win_attr, char_attr);
1639 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001640
1641 // Get the next character to put on the screen.
1642
1643 // The "p_extra" points to the extra stuff that is inserted to
1644 // represent special characters (non-printable stuff) and other
1645 // things. When all characters are the same, c_extra is used.
1646 // If c_final is set, it will compulsorily be used at the end.
1647 // "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
1648 // "p_extra[n_extra]".
1649 // For the '$' of the 'list' option, n_extra == 1, p_extra == "".
1650 if (n_extra > 0)
1651 {
1652 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
1653 {
1654 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
1655 mb_c = c; // doesn't handle non-utf-8 multi-byte!
1656 if (enc_utf8 && utf_char2len(c) > 1)
1657 {
1658 mb_utf8 = TRUE;
1659 u8cc[0] = 0;
1660 c = 0xc0;
1661 }
1662 else
1663 mb_utf8 = FALSE;
1664 }
1665 else
1666 {
1667 c = *p_extra;
1668 if (has_mbyte)
1669 {
1670 mb_c = c;
1671 if (enc_utf8)
1672 {
1673 // If the UTF-8 character is more than one byte:
1674 // Decode it into "mb_c".
1675 mb_l = utfc_ptr2len(p_extra);
1676 mb_utf8 = FALSE;
1677 if (mb_l > n_extra)
1678 mb_l = 1;
1679 else if (mb_l > 1)
1680 {
1681 mb_c = utfc_ptr2char(p_extra, u8cc);
1682 mb_utf8 = TRUE;
1683 c = 0xc0;
1684 }
1685 }
1686 else
1687 {
1688 // if this is a DBCS character, put it in "mb_c"
1689 mb_l = MB_BYTE2LEN(c);
1690 if (mb_l >= n_extra)
1691 mb_l = 1;
1692 else if (mb_l > 1)
1693 mb_c = (c << 8) + p_extra[1];
1694 }
1695 if (mb_l == 0) // at the NUL at end-of-line
1696 mb_l = 1;
1697
1698 // If a double-width char doesn't fit display a '>' in the
1699 // last column.
1700 if ((
1701# ifdef FEAT_RIGHTLEFT
1702 wp->w_p_rl ? (col <= 0) :
1703# endif
1704 (col >= wp->w_width - 1))
1705 && (*mb_char2cells)(mb_c) == 2)
1706 {
1707 c = '>';
1708 mb_c = c;
1709 mb_l = 1;
1710 mb_utf8 = FALSE;
1711 multi_attr = HL_ATTR(HLF_AT);
1712#ifdef FEAT_SYN_HL
1713 if (cul_attr)
1714 multi_attr = hl_combine_attr(multi_attr, cul_attr);
1715#endif
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001716 multi_attr = hl_combine_attr(win_attr, multi_attr);
1717
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001718 // put the pointer back to output the double-width
1719 // character at the start of the next line.
1720 ++n_extra;
1721 --p_extra;
1722 }
1723 else
1724 {
1725 n_extra -= mb_l - 1;
1726 p_extra += mb_l - 1;
1727 }
1728 }
1729 ++p_extra;
1730 }
1731 --n_extra;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00001732#if defined(FEAT_LINEBREAK) && defined(FEAT_PROP_POPUP)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001733 if (n_extra <= 0)
1734 in_linebreak = FALSE;
1735#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001736 }
1737 else
1738 {
1739#ifdef FEAT_LINEBREAK
Bram Moolenaarb9081882022-07-09 04:56:24 +01001740 int c0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001741#endif
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001742 VIM_CLEAR(p_extra_free);
Bram Moolenaarb9081882022-07-09 04:56:24 +01001743 prev_ptr = ptr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001744
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001745 // Get a character from the line itself.
1746 c = *ptr;
1747#ifdef FEAT_LINEBREAK
1748 c0 = *ptr;
1749#endif
1750 if (has_mbyte)
1751 {
1752 mb_c = c;
1753 if (enc_utf8)
1754 {
1755 // If the UTF-8 character is more than one byte: Decode it
1756 // into "mb_c".
1757 mb_l = utfc_ptr2len(ptr);
1758 mb_utf8 = FALSE;
1759 if (mb_l > 1)
1760 {
1761 mb_c = utfc_ptr2char(ptr, u8cc);
1762 // Overlong encoded ASCII or ASCII with composing char
1763 // is displayed normally, except a NUL.
1764 if (mb_c < 0x80)
1765 {
1766 c = mb_c;
1767#ifdef FEAT_LINEBREAK
1768 c0 = mb_c;
1769#endif
1770 }
1771 mb_utf8 = TRUE;
1772
1773 // At start of the line we can have a composing char.
1774 // Draw it as a space with a composing char.
1775 if (utf_iscomposing(mb_c))
1776 {
1777 int i;
1778
1779 for (i = Screen_mco - 1; i > 0; --i)
1780 u8cc[i] = u8cc[i - 1];
1781 u8cc[0] = mb_c;
1782 mb_c = ' ';
1783 }
1784 }
1785
1786 if ((mb_l == 1 && c >= 0x80)
1787 || (mb_l >= 1 && mb_c == 0)
1788 || (mb_l > 1 && (!vim_isprintc(mb_c))))
1789 {
1790 // Illegal UTF-8 byte: display as <xx>.
1791 // Non-BMP character : display as ? or fullwidth ?.
1792 transchar_hex(extra, mb_c);
1793# ifdef FEAT_RIGHTLEFT
1794 if (wp->w_p_rl) // reverse
1795 rl_mirror(extra);
1796# endif
1797 p_extra = extra;
1798 c = *p_extra;
1799 mb_c = mb_ptr2char_adv(&p_extra);
1800 mb_utf8 = (c >= 0x80);
1801 n_extra = (int)STRLEN(p_extra);
1802 c_extra = NUL;
1803 c_final = NUL;
1804 if (area_attr == 0 && search_attr == 0)
1805 {
1806 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001807 extra_attr = hl_combine_attr(
1808 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001809 saved_attr2 = char_attr; // save current attr
1810 }
1811 }
1812 else if (mb_l == 0) // at the NUL at end-of-line
1813 mb_l = 1;
1814#ifdef FEAT_ARABIC
1815 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
1816 {
1817 // Do Arabic shaping.
1818 int pc, pc1, nc;
1819 int pcc[MAX_MCO];
1820
1821 // The idea of what is the previous and next
1822 // character depends on 'rightleft'.
1823 if (wp->w_p_rl)
1824 {
1825 pc = prev_c;
1826 pc1 = prev_c1;
1827 nc = utf_ptr2char(ptr + mb_l);
1828 prev_c1 = u8cc[0];
1829 }
1830 else
1831 {
1832 pc = utfc_ptr2char(ptr + mb_l, pcc);
1833 nc = prev_c;
1834 pc1 = pcc[0];
1835 }
1836 prev_c = mb_c;
1837
1838 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
1839 }
1840 else
1841 prev_c = mb_c;
1842#endif
1843 }
1844 else // enc_dbcs
1845 {
1846 mb_l = MB_BYTE2LEN(c);
1847 if (mb_l == 0) // at the NUL at end-of-line
1848 mb_l = 1;
1849 else if (mb_l > 1)
1850 {
1851 // We assume a second byte below 32 is illegal.
1852 // Hopefully this is OK for all double-byte encodings!
1853 if (ptr[1] >= 32)
1854 mb_c = (c << 8) + ptr[1];
1855 else
1856 {
1857 if (ptr[1] == NUL)
1858 {
1859 // head byte at end of line
1860 mb_l = 1;
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001861 transchar_nonprint(wp->w_buffer, extra, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001862 }
1863 else
1864 {
1865 // illegal tail byte
1866 mb_l = 2;
1867 STRCPY(extra, "XX");
1868 }
1869 p_extra = extra;
1870 n_extra = (int)STRLEN(extra) - 1;
1871 c_extra = NUL;
1872 c_final = NUL;
1873 c = *p_extra++;
1874 if (area_attr == 0 && search_attr == 0)
1875 {
1876 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001877 extra_attr = hl_combine_attr(
1878 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001879 saved_attr2 = char_attr; // save current attr
1880 }
1881 mb_c = c;
1882 }
1883 }
1884 }
1885 // If a double-width char doesn't fit display a '>' in the
1886 // last column; the character is displayed at the start of the
1887 // next line.
1888 if ((
1889# ifdef FEAT_RIGHTLEFT
1890 wp->w_p_rl ? (col <= 0) :
1891# endif
1892 (col >= wp->w_width - 1))
1893 && (*mb_char2cells)(mb_c) == 2)
1894 {
1895 c = '>';
1896 mb_c = c;
1897 mb_utf8 = FALSE;
1898 mb_l = 1;
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001899 multi_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001900 // Put pointer back so that the character will be
1901 // displayed at the start of the next line.
1902 --ptr;
1903#ifdef FEAT_CONCEAL
1904 did_decrement_ptr = TRUE;
1905#endif
1906 }
1907 else if (*ptr != NUL)
1908 ptr += mb_l - 1;
1909
1910 // If a double-width char doesn't fit at the left side display
1911 // a '<' in the first column. Don't do this for unprintable
1912 // characters.
1913 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
1914 {
1915 n_extra = 1;
1916 c_extra = MB_FILLER_CHAR;
1917 c_final = NUL;
1918 c = ' ';
1919 if (area_attr == 0 && search_attr == 0)
1920 {
1921 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001922 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001923 saved_attr2 = char_attr; // save current attr
1924 }
1925 mb_c = c;
1926 mb_utf8 = FALSE;
1927 mb_l = 1;
1928 }
1929
1930 }
1931 ++ptr;
1932
1933 if (extra_check)
1934 {
1935#ifdef FEAT_SPELL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001936 // Check spelling (unless at the end of the line).
1937 // Only do this when there is no syntax highlighting, the
1938 // @Spell cluster is not used or the current syntax item
1939 // contains the @Spell cluster.
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001940 v = (long)(ptr - line);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001941 if (has_spell && v >= word_end && v > cur_checked_col)
1942 {
1943 spell_attr = 0;
1944 if (c != 0 && (
1945# ifdef FEAT_SYN_HL
1946 !has_syntax ||
1947# endif
1948 can_spell))
1949 {
Bram Moolenaarb9081882022-07-09 04:56:24 +01001950 char_u *p;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001951 int len;
1952 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001953
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001954 if (has_mbyte)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001955 v -= mb_l - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001956
1957 // Use nextline[] if possible, it has the start of the
1958 // next line concatenated.
1959 if ((prev_ptr - line) - nextlinecol >= 0)
1960 p = nextline + (prev_ptr - line) - nextlinecol;
1961 else
1962 p = prev_ptr;
1963 cap_col -= (int)(prev_ptr - line);
1964 len = spell_check(wp, p, &spell_hlf, &cap_col,
1965 nochange);
1966 word_end = v + len;
1967
1968 // In Insert mode only highlight a word that
1969 // doesn't touch the cursor.
1970 if (spell_hlf != HLF_COUNT
Bram Moolenaar24959102022-05-07 20:01:16 +01001971 && (State & MODE_INSERT)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001972 && wp->w_cursor.lnum == lnum
1973 && wp->w_cursor.col >=
1974 (colnr_T)(prev_ptr - line)
1975 && wp->w_cursor.col < (colnr_T)word_end)
1976 {
1977 spell_hlf = HLF_COUNT;
1978 spell_redraw_lnum = lnum;
1979 }
1980
1981 if (spell_hlf == HLF_COUNT && p != prev_ptr
1982 && (p - nextline) + len > nextline_idx)
1983 {
1984 // Remember that the good word continues at the
1985 // start of the next line.
1986 checked_lnum = lnum + 1;
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001987 checked_col = (int)((p - nextline)
1988 + len - nextline_idx);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001989 }
1990
1991 // Turn index into actual attributes.
1992 if (spell_hlf != HLF_COUNT)
1993 spell_attr = highlight_attr[spell_hlf];
1994
1995 if (cap_col > 0)
1996 {
1997 if (p != prev_ptr
1998 && (p - nextline) + cap_col >= nextline_idx)
1999 {
2000 // Remember that the word in the next line
2001 // must start with a capital.
2002 capcol_lnum = lnum + 1;
2003 cap_col = (int)((p - nextline) + cap_col
2004 - nextline_idx);
2005 }
2006 else
2007 // Compute the actual column.
2008 cap_col += (int)(prev_ptr - line);
2009 }
2010 }
2011 }
2012 if (spell_attr != 0)
2013 {
2014 if (!attr_pri)
2015 char_attr = hl_combine_attr(char_attr, spell_attr);
2016 else
2017 char_attr = hl_combine_attr(spell_attr, char_attr);
2018 }
2019#endif
2020#ifdef FEAT_LINEBREAK
2021 // Found last space before word: check for line break.
2022 if (wp->w_p_lbr && c0 == c
2023 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
2024 {
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002025 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1)
2026 : 0;
2027 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002028
2029 // TODO: is passing p for start of the line OK?
2030 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
2031 NULL) - 1;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002032
2033 // We have just drawn the showbreak value, no need to add
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002034 // space for it again.
Bram Moolenaara06e3452021-05-29 17:56:37 +02002035 if (vcol == vcol_sbr)
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002036 {
Bram Moolenaara06e3452021-05-29 17:56:37 +02002037 n_extra -= MB_CHARLEN(get_showbreak_value(wp));
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002038 if (n_extra < 0)
2039 n_extra = 0;
2040 }
Bram Moolenaar0bbca542022-01-11 13:14:54 +00002041 if (on_last_col && c != TAB)
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002042 // Do not continue search/match highlighting over the
Bram Moolenaar0bbca542022-01-11 13:14:54 +00002043 // line break, but for TABs the highlighting should
2044 // include the complete width of the character
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002045 search_attr = 0;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002046
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002047 if (c == TAB && n_extra + col > wp->w_width)
2048# ifdef FEAT_VARTABS
2049 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
2050 wp->w_buffer->b_p_vts_array) - 1;
2051# else
2052 n_extra = (int)wp->w_buffer->b_p_ts
2053 - vcol % (int)wp->w_buffer->b_p_ts - 1;
2054# endif
2055
2056 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
2057 c_final = NUL;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002058# if defined(FEAT_PROP_POPUP)
Bram Moolenaar42eba042021-11-30 20:22:49 +00002059 if (n_extra > 0 && c != TAB)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00002060 in_linebreak = TRUE;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002061# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002062 if (VIM_ISWHITE(c))
2063 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002064# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002065 if (c == TAB)
2066 // See "Tab alignment" below.
2067 FIX_FOR_BOGUSCOLS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002068# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002069 if (!wp->w_p_list)
2070 c = ' ';
2071 }
2072 }
2073#endif
2074
zeertzjqf14b8ba2021-09-10 16:58:30 +02002075 in_multispace = c == ' '
2076 && ((ptr > line + 1 && ptr[-2] == ' ') || *ptr == ' ');
2077 if (!in_multispace)
2078 multispace_pos = 0;
2079
Bram Moolenaareed9d462021-02-15 20:38:25 +01002080 // 'list': Change char 160 to 'nbsp' and space to 'space'
2081 // setting in 'listchars'. But not when the character is
2082 // followed by a composing character (use mb_l to check that).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002083 if (wp->w_p_list
2084 && ((((c == 160 && mb_l == 1)
2085 || (mb_utf8
2086 && ((mb_c == 160 && mb_l == 2)
2087 || (mb_c == 0x202f && mb_l == 3))))
Bram Moolenaareed9d462021-02-15 20:38:25 +01002088 && wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002089 || (c == ' '
2090 && mb_l == 1
zeertzjqf14b8ba2021-09-10 16:58:30 +02002091 && (wp->w_lcs_chars.space
2092 || (in_multispace
2093 && wp->w_lcs_chars.multispace != NULL))
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002094 && ptr - line >= leadcol
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002095 && ptr - line <= trailcol)))
2096 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02002097 if (in_multispace && wp->w_lcs_chars.multispace != NULL)
2098 {
2099 c = wp->w_lcs_chars.multispace[multispace_pos++];
2100 if (wp->w_lcs_chars.multispace[multispace_pos] == NUL)
2101 multispace_pos = 0;
2102 }
2103 else
2104 c = (c == ' ') ? wp->w_lcs_chars.space
2105 : wp->w_lcs_chars.nbsp;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002106 if (area_attr == 0 && search_attr == 0)
2107 {
2108 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002109 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002110 saved_attr2 = char_attr; // save current attr
2111 }
2112 mb_c = c;
2113 if (enc_utf8 && utf_char2len(c) > 1)
2114 {
2115 mb_utf8 = TRUE;
2116 u8cc[0] = 0;
2117 c = 0xc0;
2118 }
2119 else
2120 mb_utf8 = FALSE;
2121 }
2122
zeertzjq2e7cba32022-06-10 15:30:32 +01002123 if (c == ' ' && ((trailcol != MAXCOL && ptr > line + trailcol)
2124 || (leadcol != 0 && ptr < line + leadcol)))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002125 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002126 if (leadcol != 0 && in_multispace && ptr < line + leadcol
2127 && wp->w_lcs_chars.leadmultispace != NULL)
2128 {
2129 c = wp->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjq2e7cba32022-06-10 15:30:32 +01002130 if (wp->w_lcs_chars.leadmultispace[multispace_pos]
2131 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002132 multispace_pos = 0;
2133 }
2134
2135 else if (ptr > line + trailcol && wp->w_lcs_chars.trail)
2136 c = wp->w_lcs_chars.trail;
2137
2138 else if (ptr < line + leadcol && wp->w_lcs_chars.lead)
2139 c = wp->w_lcs_chars.lead;
2140
zeertzjq2e7cba32022-06-10 15:30:32 +01002141 else if (leadcol != 0 && wp->w_lcs_chars.space)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002142 c = wp->w_lcs_chars.space;
2143
2144
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002145 if (!attr_pri)
2146 {
2147 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002148 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002149 saved_attr2 = char_attr; // save current attr
2150 }
2151 mb_c = c;
2152 if (enc_utf8 && utf_char2len(c) > 1)
2153 {
2154 mb_utf8 = TRUE;
2155 u8cc[0] = 0;
2156 c = 0xc0;
2157 }
2158 else
2159 mb_utf8 = FALSE;
2160 }
2161 }
2162
2163 // Handling of non-printable characters.
2164 if (!vim_isprintc(c))
2165 {
2166 // when getting a character from the file, we may have to
2167 // turn it into something else on the way to putting it
2168 // into "ScreenLines".
Bram Moolenaareed9d462021-02-15 20:38:25 +01002169 if (c == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002170 {
2171 int tab_len = 0;
2172 long vcol_adjusted = vcol; // removed showbreak length
2173#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01002174 char_u *sbr = get_showbreak_value(wp);
2175
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002176 // only adjust the tab_len, when at the first column
2177 // after the showbreak value was drawn
Bram Moolenaaree857022019-11-09 23:26:40 +01002178 if (*sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
2179 vcol_adjusted = vcol - MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002180#endif
2181 // tab amount depends on current column
2182#ifdef FEAT_VARTABS
2183 tab_len = tabstop_padding(vcol_adjusted,
2184 wp->w_buffer->b_p_ts,
2185 wp->w_buffer->b_p_vts_array) - 1;
2186#else
2187 tab_len = (int)wp->w_buffer->b_p_ts
2188 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
2189#endif
2190
2191#ifdef FEAT_LINEBREAK
2192 if (!wp->w_p_lbr || !wp->w_p_list)
2193#endif
2194 // tab amount depends on current column
2195 n_extra = tab_len;
2196#ifdef FEAT_LINEBREAK
2197 else
2198 {
2199 char_u *p;
2200 int len;
2201 int i;
2202 int saved_nextra = n_extra;
2203
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002204# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002205 if (vcol_off > 0)
2206 // there are characters to conceal
2207 tab_len += vcol_off;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002208
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002209 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaareed9d462021-02-15 20:38:25 +01002210 if (wp->w_p_list && wp->w_lcs_chars.tab1
2211 && old_boguscols > 0
2212 && n_extra > tab_len)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002213 tab_len += n_extra - tab_len;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002214# endif
2215 // If n_extra > 0, it gives the number of chars, to
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002216 // use for a tab, else we need to calculate the width
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002217 // for a tab.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002218 len = (tab_len * mb_char2len(wp->w_lcs_chars.tab2));
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002219 if (wp->w_lcs_chars.tab3)
2220 len += mb_char2len(wp->w_lcs_chars.tab3);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002221 if (n_extra > 0)
2222 len += n_extra - tab_len;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002223 c = wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002224 p = alloc(len + 1);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002225 if (p == NULL)
2226 n_extra = 0;
2227 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002228 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002229 vim_memset(p, ' ', len);
2230 p[len] = NUL;
2231 vim_free(p_extra_free);
2232 p_extra_free = p;
2233 for (i = 0; i < tab_len; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002234 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002235 int lcs = wp->w_lcs_chars.tab2;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002236
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002237 if (*p == NUL)
2238 {
2239 tab_len = i;
2240 break;
2241 }
2242
2243 // if tab3 is given, use it for the last char
2244 if (wp->w_lcs_chars.tab3 && i == tab_len - 1)
2245 lcs = wp->w_lcs_chars.tab3;
2246 p += mb_char2bytes(lcs, p);
2247 n_extra += mb_char2len(lcs)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002248 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002249 }
2250 p_extra = p_extra_free;
2251# ifdef FEAT_CONCEAL
2252 // n_extra will be increased by FIX_FOX_BOGUSCOLS
2253 // macro below, so need to adjust for that here
2254 if (vcol_off > 0)
2255 n_extra -= vcol_off;
2256# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002257 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002258 }
2259#endif
2260#ifdef FEAT_CONCEAL
2261 {
2262 int vc_saved = vcol_off;
2263
2264 // Tab alignment should be identical regardless of
2265 // 'conceallevel' value. So tab compensates of all
2266 // previous concealed characters, and thus resets
2267 // vcol_off and boguscols accumulated so far in the
2268 // line. Note that the tab can be longer than
2269 // 'tabstop' when there are concealed characters.
2270 FIX_FOR_BOGUSCOLS;
2271
2272 // Make sure, the highlighting for the tab char will be
2273 // correctly set further below (effectively reverts the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002274 // FIX_FOR_BOGSUCOLS macro).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002275 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaareed9d462021-02-15 20:38:25 +01002276 && wp->w_lcs_chars.tab1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002277 tab_len += vc_saved;
2278 }
2279#endif
2280 mb_utf8 = FALSE; // don't draw as UTF-8
2281 if (wp->w_p_list)
2282 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002283 c = (n_extra == 0 && wp->w_lcs_chars.tab3)
2284 ? wp->w_lcs_chars.tab3
2285 : wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002286#ifdef FEAT_LINEBREAK
2287 if (wp->w_p_lbr)
2288 c_extra = NUL; // using p_extra from above
2289 else
2290#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01002291 c_extra = wp->w_lcs_chars.tab2;
2292 c_final = wp->w_lcs_chars.tab3;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002293 n_attr = tab_len + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002294 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002295 saved_attr2 = char_attr; // save current attr
2296 mb_c = c;
2297 if (enc_utf8 && utf_char2len(c) > 1)
2298 {
2299 mb_utf8 = TRUE;
2300 u8cc[0] = 0;
2301 c = 0xc0;
2302 }
2303 }
2304 else
2305 {
2306 c_final = NUL;
2307 c_extra = ' ';
2308 c = ' ';
2309 }
2310 }
2311 else if (c == NUL
2312 && (wp->w_p_list
2313 || ((fromcol >= 0 || fromcol_prev >= 0)
2314 && tocol > vcol
2315 && VIsual_mode != Ctrl_V
2316 && (
2317# ifdef FEAT_RIGHTLEFT
2318 wp->w_p_rl ? (col >= 0) :
2319# endif
2320 (col < wp->w_width))
2321 && !(noinvcur
2322 && lnum == wp->w_cursor.lnum
2323 && (colnr_T)vcol == wp->w_virtcol)))
2324 && lcs_eol_one > 0)
2325 {
2326 // Display a '$' after the line or highlight an extra
2327 // character if the line break is included.
2328#if defined(FEAT_DIFF) || defined(LINE_ATTR)
2329 // For a diff line the highlighting continues after the
2330 // "$".
2331 if (
2332# ifdef FEAT_DIFF
2333 diff_hlf == (hlf_T)0
2334# ifdef LINE_ATTR
2335 &&
2336# endif
2337# endif
2338# ifdef LINE_ATTR
2339 line_attr == 0
2340# endif
2341 )
2342#endif
2343 {
2344 // In virtualedit, visual selections may extend
2345 // beyond end of line.
2346 if (area_highlighting && virtual_active()
2347 && tocol != MAXCOL && vcol < tocol)
2348 n_extra = 0;
2349 else
2350 {
2351 p_extra = at_end_str;
2352 n_extra = 1;
2353 c_extra = NUL;
2354 c_final = NUL;
2355 }
2356 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002357 if (wp->w_p_list && wp->w_lcs_chars.eol > 0)
2358 c = wp->w_lcs_chars.eol;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002359 else
2360 c = ' ';
2361 lcs_eol_one = -1;
2362 --ptr; // put it back at the NUL
2363 if (!attr_pri)
2364 {
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002365 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002366 n_attr = 1;
2367 }
2368 mb_c = c;
2369 if (enc_utf8 && utf_char2len(c) > 1)
2370 {
2371 mb_utf8 = TRUE;
2372 u8cc[0] = 0;
2373 c = 0xc0;
2374 }
2375 else
2376 mb_utf8 = FALSE; // don't draw as UTF-8
2377 }
2378 else if (c != NUL)
2379 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02002380 p_extra = transchar_buf(wp->w_buffer, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002381 if (n_extra == 0)
2382 n_extra = byte2cells(c) - 1;
2383#ifdef FEAT_RIGHTLEFT
2384 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
2385 rl_mirror(p_extra); // reverse "<12>"
2386#endif
2387 c_extra = NUL;
2388 c_final = NUL;
2389#ifdef FEAT_LINEBREAK
2390 if (wp->w_p_lbr)
2391 {
2392 char_u *p;
2393
2394 c = *p_extra;
2395 p = alloc(n_extra + 1);
2396 vim_memset(p, ' ', n_extra);
2397 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
2398 p[n_extra] = NUL;
2399 vim_free(p_extra_free);
2400 p_extra_free = p_extra = p;
2401 }
2402 else
2403#endif
2404 {
2405 n_extra = byte2cells(c) - 1;
2406 c = *p_extra++;
2407 }
2408 if (!attr_pri)
2409 {
2410 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002411 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002412 saved_attr2 = char_attr; // save current attr
2413 }
2414 mb_utf8 = FALSE; // don't draw as UTF-8
2415 }
2416 else if (VIsual_active
2417 && (VIsual_mode == Ctrl_V
2418 || VIsual_mode == 'v')
2419 && virtual_active()
2420 && tocol != MAXCOL
2421 && vcol < tocol
2422 && (
2423#ifdef FEAT_RIGHTLEFT
2424 wp->w_p_rl ? (col >= 0) :
2425#endif
2426 (col < wp->w_width)))
2427 {
2428 c = ' ';
2429 --ptr; // put it back at the NUL
2430 }
2431#if defined(LINE_ATTR)
2432 else if ((
2433# ifdef FEAT_DIFF
2434 diff_hlf != (hlf_T)0 ||
2435# endif
2436# ifdef FEAT_TERMINAL
2437 win_attr != 0 ||
2438# endif
2439 line_attr != 0
2440 ) && (
2441# ifdef FEAT_RIGHTLEFT
2442 wp->w_p_rl ? (col >= 0) :
2443# endif
2444 (col
2445# ifdef FEAT_CONCEAL
2446 - boguscols
2447# endif
2448 < wp->w_width)))
2449 {
2450 // Highlight until the right side of the window
2451 c = ' ';
2452 --ptr; // put it back at the NUL
2453
2454 // Remember we do the char for line highlighting.
2455 ++did_line_attr;
2456
2457 // don't do search HL for the rest of the line
2458 if (line_attr != 0 && char_attr == search_attr
2459 && (did_line_attr > 1
Bram Moolenaareed9d462021-02-15 20:38:25 +01002460 || (wp->w_p_list &&
2461 wp->w_lcs_chars.eol > 0)))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002462 char_attr = line_attr;
2463# ifdef FEAT_DIFF
2464 if (diff_hlf == HLF_TXD)
2465 {
2466 diff_hlf = HLF_CHD;
2467 if (vi_attr == 0 || char_attr != vi_attr)
2468 {
2469 char_attr = HL_ATTR(diff_hlf);
2470 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2471 && wp->w_p_culopt_flags != CULOPT_NBR
2472 && (!cul_screenline
2473 || (vcol >= left_curline_col
2474 && vcol <= right_curline_col)))
2475 char_attr = hl_combine_attr(
2476 char_attr, HL_ATTR(HLF_CUL));
2477 }
2478 }
2479# endif
2480# ifdef FEAT_TERMINAL
2481 if (win_attr != 0)
2482 {
2483 char_attr = win_attr;
Bram Moolenaara3817732019-10-16 16:31:44 +02002484 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2485 && wp->w_p_culopt_flags != CULOPT_NBR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002486 {
2487 if (!cul_screenline || (vcol >= left_curline_col
2488 && vcol <= right_curline_col))
2489 char_attr = hl_combine_attr(
2490 char_attr, HL_ATTR(HLF_CUL));
2491 }
2492 else if (line_attr)
2493 char_attr = hl_combine_attr(char_attr, line_attr);
2494 }
2495# endif
2496 }
2497#endif
2498 }
2499
2500#ifdef FEAT_CONCEAL
2501 if ( wp->w_p_cole > 0
LemonBoy9830db62022-05-08 21:25:20 +01002502 && (wp != curwin || lnum != wp->w_cursor.lnum
2503 || conceal_cursor_line(wp))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002504 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
2505 && !(lnum_in_visual_area
2506 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
2507 {
2508 char_attr = conceal_attr;
LemonBoy9830db62022-05-08 21:25:20 +01002509 if (((prev_syntax_id != syntax_seqnr
2510 && (syntax_flags & HL_CONCEAL) != 0)
2511 || has_match_conc > 1)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002512 && (syn_get_sub_char() != NUL
2513 || (has_match_conc && match_conc)
2514 || wp->w_p_cole == 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002515 && wp->w_p_cole != 3)
2516 {
2517 // First time at this concealed item: display one
2518 // character.
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002519 if (has_match_conc && match_conc)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002520 c = match_conc;
2521 else if (syn_get_sub_char() != NUL)
2522 c = syn_get_sub_char();
Bram Moolenaareed9d462021-02-15 20:38:25 +01002523 else if (wp->w_lcs_chars.conceal != NUL)
2524 c = wp->w_lcs_chars.conceal;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002525 else
2526 c = ' ';
2527
2528 prev_syntax_id = syntax_seqnr;
2529
2530 if (n_extra > 0)
2531 vcol_off += n_extra;
2532 vcol += n_extra;
2533 if (wp->w_p_wrap && n_extra > 0)
2534 {
2535# ifdef FEAT_RIGHTLEFT
2536 if (wp->w_p_rl)
2537 {
2538 col -= n_extra;
2539 boguscols -= n_extra;
2540 }
2541 else
2542# endif
2543 {
2544 boguscols += n_extra;
2545 col += n_extra;
2546 }
2547 }
2548 n_extra = 0;
2549 n_attr = 0;
2550 }
2551 else if (n_skip == 0)
2552 {
2553 is_concealing = TRUE;
2554 n_skip = 1;
2555 }
2556 mb_c = c;
2557 if (enc_utf8 && utf_char2len(c) > 1)
2558 {
2559 mb_utf8 = TRUE;
2560 u8cc[0] = 0;
2561 c = 0xc0;
2562 }
2563 else
2564 mb_utf8 = FALSE; // don't draw as UTF-8
2565 }
2566 else
2567 {
2568 prev_syntax_id = 0;
2569 is_concealing = FALSE;
2570 }
2571
2572 if (n_skip > 0 && did_decrement_ptr)
2573 // not showing the '>', put pointer back to avoid getting stuck
2574 ++ptr;
2575
2576#endif // FEAT_CONCEAL
2577 }
2578
2579#ifdef FEAT_CONCEAL
2580 // In the cursor line and we may be concealing characters: correct
2581 // the cursor column when we reach its position.
2582 if (!did_wcol && draw_state == WL_LINE
2583 && wp == curwin && lnum == wp->w_cursor.lnum
2584 && conceal_cursor_line(wp)
2585 && (int)wp->w_virtcol <= vcol + n_skip)
2586 {
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002587# ifdef FEAT_RIGHTLEFT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002588 if (wp->w_p_rl)
2589 wp->w_wcol = wp->w_width - col + boguscols - 1;
2590 else
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002591# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002592 wp->w_wcol = col - boguscols;
2593 wp->w_wrow = row;
2594 did_wcol = TRUE;
2595 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002596# ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +01002597 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002598# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002599 }
2600#endif
2601
2602 // Don't override visual selection highlighting.
2603 if (n_attr > 0
2604 && draw_state == WL_LINE
2605 && !attr_pri)
2606 {
2607#ifdef LINE_ATTR
2608 if (line_attr)
2609 char_attr = hl_combine_attr(extra_attr, line_attr);
2610 else
2611#endif
2612 char_attr = extra_attr;
2613 }
2614
2615#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2616 // XIM don't send preedit_start and preedit_end, but they send
2617 // preedit_changed and commit. Thus Vim can't set "im_is_active", use
2618 // im_is_preediting() here.
2619 if (p_imst == IM_ON_THE_SPOT
2620 && xic != NULL
2621 && lnum == wp->w_cursor.lnum
Bram Moolenaar24959102022-05-07 20:01:16 +01002622 && (State & MODE_INSERT)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002623 && !p_imdisable
2624 && im_is_preediting()
2625 && draw_state == WL_LINE)
2626 {
2627 colnr_T tcol;
2628
2629 if (preedit_end_col == MAXCOL)
2630 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
2631 else
2632 tcol = preedit_end_col;
2633 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
2634 {
2635 if (feedback_old_attr < 0)
2636 {
2637 feedback_col = 0;
2638 feedback_old_attr = char_attr;
2639 }
2640 char_attr = im_get_feedback_attr(feedback_col);
2641 if (char_attr < 0)
2642 char_attr = feedback_old_attr;
2643 feedback_col++;
2644 }
2645 else if (feedback_old_attr >= 0)
2646 {
2647 char_attr = feedback_old_attr;
2648 feedback_old_attr = -1;
2649 feedback_col = 0;
2650 }
2651 }
2652#endif
2653 // Handle the case where we are in column 0 but not on the first
2654 // character of the line and the user wants us to show us a
2655 // special character (via 'listchars' option "precedes:<char>".
2656 if (lcs_prec_todo != NUL
2657 && wp->w_p_list
Bram Moolenaarbffba7f2019-09-20 17:00:17 +02002658 && (wp->w_p_wrap ?
2659 (wp->w_skipcol > 0 && row == 0) :
2660 wp->w_leftcol > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002661#ifdef FEAT_DIFF
2662 && filler_todo <= 0
2663#endif
2664 && draw_state > WL_NR
2665 && c != NUL)
2666 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002667 c = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002668 lcs_prec_todo = NUL;
2669 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2670 {
2671 // Double-width character being overwritten by the "precedes"
2672 // character, need to fill up half the character.
2673 c_extra = MB_FILLER_CHAR;
2674 c_final = NUL;
2675 n_extra = 1;
2676 n_attr = 2;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002677 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002678 }
2679 mb_c = c;
2680 if (enc_utf8 && utf_char2len(c) > 1)
2681 {
2682 mb_utf8 = TRUE;
2683 u8cc[0] = 0;
2684 c = 0xc0;
2685 }
2686 else
2687 mb_utf8 = FALSE; // don't draw as UTF-8
2688 if (!attr_pri)
2689 {
2690 saved_attr3 = char_attr; // save current attr
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002691 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002692 n_attr3 = 1;
2693 }
2694 }
2695
2696 // At end of the text line or just after the last character.
2697 if ((c == NUL
2698#if defined(LINE_ATTR)
2699 || did_line_attr == 1
2700#endif
2701 ) && eol_hl_off == 0)
2702 {
2703#ifdef FEAT_SEARCH_EXTRA
2704 // flag to indicate whether prevcol equals startcol of search_hl or
2705 // one of the matches
2706 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &screen_search_hl,
2707 (long)(ptr - line) - (c == NUL));
2708#endif
2709 // Invert at least one char, used for Visual and empty line or
2710 // highlight match at end of line. If it's beyond the last
2711 // char on the screen, just overwrite that one (tricky!) Not
2712 // needed when a '$' was displayed for 'list'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002713 if (wp->w_lcs_chars.eol == lcs_eol_one
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002714 && ((area_attr != 0 && vcol == fromcol
2715 && (VIsual_mode != Ctrl_V
2716 || lnum == VIsual.lnum
2717 || lnum == curwin->w_cursor.lnum)
2718 && c == NUL)
2719#ifdef FEAT_SEARCH_EXTRA
2720 // highlight 'hlsearch' match at end of line
2721 || (prevcol_hl_flag
2722# ifdef FEAT_SYN_HL
2723 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
2724 && !(wp == curwin && VIsual_active))
2725# endif
2726# ifdef FEAT_DIFF
2727 && diff_hlf == (hlf_T)0
2728# endif
2729# if defined(LINE_ATTR)
2730 && did_line_attr <= 1
2731# endif
2732 )
2733#endif
2734 ))
2735 {
2736 int n = 0;
2737
2738#ifdef FEAT_RIGHTLEFT
2739 if (wp->w_p_rl)
2740 {
2741 if (col < 0)
2742 n = 1;
2743 }
2744 else
2745#endif
2746 {
2747 if (col >= wp->w_width)
2748 n = -1;
2749 }
2750 if (n != 0)
2751 {
2752 // At the window boundary, highlight the last character
2753 // instead (better than nothing).
2754 off += n;
2755 col += n;
2756 }
2757 else
2758 {
2759 // Add a blank character to highlight.
2760 ScreenLines[off] = ' ';
2761 if (enc_utf8)
2762 ScreenLinesUC[off] = 0;
2763 }
2764#ifdef FEAT_SEARCH_EXTRA
2765 if (area_attr == 0)
2766 {
2767 // Use attributes from match with highest priority among
2768 // 'search_hl' and the match list.
2769 get_search_match_hl(wp, &screen_search_hl,
2770 (long)(ptr - line), &char_attr);
2771 }
2772#endif
2773 ScreenAttrs[off] = char_attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002774 ScreenCols[off] = MAXCOL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002775#ifdef FEAT_RIGHTLEFT
2776 if (wp->w_p_rl)
2777 {
2778 --col;
2779 --off;
2780 }
2781 else
2782#endif
2783 {
2784 ++col;
2785 ++off;
2786 }
2787 ++vcol;
2788 eol_hl_off = 1;
2789 }
2790 }
2791
2792 // At end of the text line.
2793 if (c == NUL)
2794 {
2795#ifdef FEAT_SYN_HL
2796 // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
2797 if (wp->w_p_wrap)
2798 v = wp->w_skipcol;
2799 else
2800 v = wp->w_leftcol;
2801
2802 // check if line ends before left margin
2803 if (vcol < v + col - win_col_off(wp))
2804 vcol = v + col - win_col_off(wp);
2805#ifdef FEAT_CONCEAL
2806 // Get rid of the boguscols now, we want to draw until the right
2807 // edge for 'cursorcolumn'.
2808 col -= boguscols;
2809 boguscols = 0;
2810#endif
2811
2812 if (draw_color_col)
2813 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2814
2815 if (((wp->w_p_cuc
2816 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
2817 && (int)wp->w_virtcol <
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002818 (long)wp->w_width * (row - startrow + 1) + v
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002819 && lnum != wp->w_cursor.lnum)
2820 || draw_color_col
2821 || win_attr != 0)
2822# ifdef FEAT_RIGHTLEFT
2823 && !wp->w_p_rl
2824# endif
2825 )
2826 {
2827 int rightmost_vcol = 0;
2828 int i;
2829
2830 if (wp->w_p_cuc)
2831 rightmost_vcol = wp->w_virtcol;
2832 if (draw_color_col)
2833 // determine rightmost colorcolumn to possibly draw
2834 for (i = 0; color_cols[i] >= 0; ++i)
2835 if (rightmost_vcol < color_cols[i])
2836 rightmost_vcol = color_cols[i];
2837
2838 while (col < wp->w_width)
2839 {
2840 ScreenLines[off] = ' ';
2841 if (enc_utf8)
2842 ScreenLinesUC[off] = 0;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002843 ScreenCols[off] = MAXCOL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002844 ++col;
2845 if (draw_color_col)
2846 draw_color_col = advance_color_col(VCOL_HLC,
2847 &color_cols);
2848
2849 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
2850 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
2851 else if (draw_color_col && VCOL_HLC == *color_cols)
2852 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
2853 else
2854 ScreenAttrs[off++] = win_attr;
2855
2856 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
2857 break;
2858
2859 ++vcol;
2860 }
2861 }
2862#endif
2863
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002864 screen_line(wp, screen_row, wp->w_wincol, col,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002865 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002866 row++;
2867
2868 // Update w_cline_height and w_cline_folded if the cursor line was
2869 // updated (saves a call to plines() later).
2870 if (wp == curwin && lnum == curwin->w_cursor.lnum)
2871 {
2872 curwin->w_cline_row = startrow;
2873 curwin->w_cline_height = row - startrow;
2874#ifdef FEAT_FOLDING
2875 curwin->w_cline_folded = FALSE;
2876#endif
2877 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2878 }
2879
2880 break;
2881 }
2882
2883 // Show "extends" character from 'listchars' if beyond the line end and
2884 // 'list' is set.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002885 if (wp->w_lcs_chars.ext != NUL
Bram Moolenaar41fb7232021-07-08 12:40:05 +02002886 && draw_state == WL_LINE
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002887 && wp->w_p_list
2888 && !wp->w_p_wrap
2889#ifdef FEAT_DIFF
2890 && filler_todo <= 0
2891#endif
2892 && (
2893#ifdef FEAT_RIGHTLEFT
2894 wp->w_p_rl ? col == 0 :
2895#endif
2896 col == wp->w_width - 1)
2897 && (*ptr != NUL
2898 || (wp->w_p_list && lcs_eol_one > 0)
2899 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
2900 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002901 c = wp->w_lcs_chars.ext;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002902 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002903 mb_c = c;
2904 if (enc_utf8 && utf_char2len(c) > 1)
2905 {
2906 mb_utf8 = TRUE;
2907 u8cc[0] = 0;
2908 c = 0xc0;
2909 }
2910 else
2911 mb_utf8 = FALSE;
2912 }
2913
2914#ifdef FEAT_SYN_HL
2915 // advance to the next 'colorcolumn'
2916 if (draw_color_col)
2917 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2918
2919 // Highlight the cursor column if 'cursorcolumn' is set. But don't
2920 // highlight the cursor position itself.
2921 // Also highlight the 'colorcolumn' if it is different than
2922 // 'cursorcolumn'
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002923 // Also highlight the 'colorcolumn' if 'breakindent' and/or 'showbreak'
2924 // options are set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002925 vcol_save_attr = -1;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002926 if (((draw_state == WL_LINE ||
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002927 draw_state == WL_BRI ||
2928 draw_state == WL_SBR) && !lnum_in_visual_area
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002929 && search_attr == 0 && area_attr == 0)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002930# ifdef FEAT_DIFF
2931 && filler_todo <= 0
2932# endif
2933 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002934 {
2935 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
2936 && lnum != wp->w_cursor.lnum)
2937 {
2938 vcol_save_attr = char_attr;
2939 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
2940 }
2941 else if (draw_color_col && VCOL_HLC == *color_cols)
2942 {
2943 vcol_save_attr = char_attr;
2944 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
2945 }
2946 }
2947#endif
2948
2949 // Store character to be displayed.
2950 // Skip characters that are left of the screen for 'nowrap'.
2951 vcol_prev = vcol;
2952 if (draw_state < WL_LINE || n_skip <= 0)
2953 {
2954 // Store the character.
2955#if defined(FEAT_RIGHTLEFT)
2956 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
2957 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002958 // A double-wide character is: put first half in left cell.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002959 --off;
2960 --col;
2961 }
2962#endif
2963 ScreenLines[off] = c;
2964 if (enc_dbcs == DBCS_JPNU)
2965 {
2966 if ((mb_c & 0xff00) == 0x8e00)
2967 ScreenLines[off] = 0x8e;
2968 ScreenLines2[off] = mb_c & 0xff;
2969 }
2970 else if (enc_utf8)
2971 {
2972 if (mb_utf8)
2973 {
2974 int i;
2975
2976 ScreenLinesUC[off] = mb_c;
2977 if ((c & 0xff) == 0)
2978 ScreenLines[off] = 0x80; // avoid storing zero
2979 for (i = 0; i < Screen_mco; ++i)
2980 {
2981 ScreenLinesC[i][off] = u8cc[i];
2982 if (u8cc[i] == 0)
2983 break;
2984 }
2985 }
2986 else
2987 ScreenLinesUC[off] = 0;
2988 }
2989 if (multi_attr)
2990 {
2991 ScreenAttrs[off] = multi_attr;
2992 multi_attr = 0;
2993 }
2994 else
2995 ScreenAttrs[off] = char_attr;
2996
Bram Moolenaarb9081882022-07-09 04:56:24 +01002997 ScreenCols[off] = (colnr_T)(prev_ptr - line);
2998
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002999 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
3000 {
3001 // Need to fill two screen columns.
3002 ++off;
3003 ++col;
3004 if (enc_utf8)
3005 // UTF-8: Put a 0 in the second screen char.
3006 ScreenLines[off] = 0;
3007 else
3008 // DBCS: Put second byte in the second screen char.
3009 ScreenLines[off] = mb_c & 0xff;
3010 if (draw_state > WL_NR
3011#ifdef FEAT_DIFF
3012 && filler_todo <= 0
3013#endif
3014 )
3015 ++vcol;
3016 // When "tocol" is halfway a character, set it to the end of
3017 // the character, otherwise highlighting won't stop.
3018 if (tocol == vcol)
3019 ++tocol;
Bram Moolenaarb9081882022-07-09 04:56:24 +01003020
3021 ScreenCols[off] = (colnr_T)(prev_ptr - line);
3022
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003023#ifdef FEAT_RIGHTLEFT
3024 if (wp->w_p_rl)
3025 {
3026 // now it's time to backup one cell
3027 --off;
3028 --col;
3029 }
3030#endif
3031 }
3032#ifdef FEAT_RIGHTLEFT
3033 if (wp->w_p_rl)
3034 {
3035 --off;
3036 --col;
3037 }
3038 else
3039#endif
3040 {
3041 ++off;
3042 ++col;
3043 }
3044 }
3045#ifdef FEAT_CONCEAL
3046 else if (wp->w_p_cole > 0 && is_concealing)
3047 {
3048 --n_skip;
3049 ++vcol_off;
3050 if (n_extra > 0)
3051 vcol_off += n_extra;
3052 if (wp->w_p_wrap)
3053 {
3054 // Special voodoo required if 'wrap' is on.
3055 //
3056 // Advance the column indicator to force the line
3057 // drawing to wrap early. This will make the line
3058 // take up the same screen space when parts are concealed,
3059 // so that cursor line computations aren't messed up.
3060 //
3061 // To avoid the fictitious advance of 'col' causing
3062 // trailing junk to be written out of the screen line
3063 // we are building, 'boguscols' keeps track of the number
3064 // of bad columns we have advanced.
3065 if (n_extra > 0)
3066 {
3067 vcol += n_extra;
3068# ifdef FEAT_RIGHTLEFT
3069 if (wp->w_p_rl)
3070 {
3071 col -= n_extra;
3072 boguscols -= n_extra;
3073 }
3074 else
3075# endif
3076 {
3077 col += n_extra;
3078 boguscols += n_extra;
3079 }
3080 n_extra = 0;
3081 n_attr = 0;
3082 }
3083
3084
3085 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
3086 {
3087 // Need to fill two screen columns.
3088# ifdef FEAT_RIGHTLEFT
3089 if (wp->w_p_rl)
3090 {
3091 --boguscols;
3092 --col;
3093 }
3094 else
3095# endif
3096 {
3097 ++boguscols;
3098 ++col;
3099 }
3100 }
3101
3102# ifdef FEAT_RIGHTLEFT
3103 if (wp->w_p_rl)
3104 {
3105 --boguscols;
3106 --col;
3107 }
3108 else
3109# endif
3110 {
3111 ++boguscols;
3112 ++col;
3113 }
3114 }
3115 else
3116 {
3117 if (n_extra > 0)
3118 {
3119 vcol += n_extra;
3120 n_extra = 0;
3121 n_attr = 0;
3122 }
3123 }
3124
3125 }
3126#endif // FEAT_CONCEAL
3127 else
3128 --n_skip;
3129
3130 // Only advance the "vcol" when after the 'number' or 'relativenumber'
3131 // column.
3132 if (draw_state > WL_NR
3133#ifdef FEAT_DIFF
3134 && filler_todo <= 0
3135#endif
3136 )
3137 ++vcol;
3138
3139#ifdef FEAT_SYN_HL
3140 if (vcol_save_attr >= 0)
3141 char_attr = vcol_save_attr;
3142#endif
3143
3144 // restore attributes after "predeces" in 'listchars'
3145 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
3146 char_attr = saved_attr3;
3147
3148 // restore attributes after last 'listchars' or 'number' char
3149 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
3150 char_attr = saved_attr2;
3151
3152 // At end of screen line and there is more to come: Display the line
3153 // so far. If there is no more to display it is caught above.
3154 if ((
3155#ifdef FEAT_RIGHTLEFT
3156 wp->w_p_rl ? (col < 0) :
3157#endif
3158 (col >= wp->w_width))
Bram Moolenaar41fb7232021-07-08 12:40:05 +02003159 && (draw_state != WL_LINE
3160 || *ptr != NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003161#ifdef FEAT_DIFF
3162 || filler_todo > 0
3163#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01003164 || (wp->w_p_list && wp->w_lcs_chars.eol != NUL
3165 && p_extra != at_end_str)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003166 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
3167 )
3168 {
3169#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003170 screen_line(wp, screen_row, wp->w_wincol, col - boguscols,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003171 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003172 boguscols = 0;
3173#else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003174 screen_line(wp, screen_row, wp->w_wincol, col,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003175 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003176#endif
3177 ++row;
3178 ++screen_row;
3179
3180 // When not wrapping and finished diff lines, or when displayed
3181 // '$' and highlighting until last column, break here.
3182 if ((!wp->w_p_wrap
3183#ifdef FEAT_DIFF
3184 && filler_todo <= 0
3185#endif
3186 ) || lcs_eol_one == -1)
3187 break;
3188
3189 // When the window is too narrow draw all "@" lines.
3190 if (draw_state != WL_LINE
3191#ifdef FEAT_DIFF
3192 && filler_todo <= 0
3193#endif
3194 )
3195 {
3196 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
3197 draw_vsep_win(wp, row);
3198 row = endrow;
3199 }
3200
3201 // When line got too long for screen break here.
3202 if (row == endrow)
3203 {
3204 ++row;
3205 break;
3206 }
3207
3208 if (screen_cur_row == screen_row - 1
3209#ifdef FEAT_DIFF
3210 && filler_todo <= 0
3211#endif
3212 && wp->w_width == Columns)
3213 {
3214 // Remember that the line wraps, used for modeless copy.
3215 LineWraps[screen_row - 1] = TRUE;
3216
3217 // Special trick to make copy/paste of wrapped lines work with
3218 // xterm/screen: write an extra character beyond the end of
3219 // the line. This will work with all terminal types
3220 // (regardless of the xn,am settings).
3221 // Only do this on a fast tty.
3222 // Only do this if the cursor is on the current line
3223 // (something has been written in it).
3224 // Don't do this for the GUI.
3225 // Don't do this for double-width characters.
3226 // Don't do this for a window not at the right screen border.
3227 if (p_tf
3228#ifdef FEAT_GUI
3229 && !gui.in_use
3230#endif
3231 && !(has_mbyte
3232 && ((*mb_off2cells)(LineOffset[screen_row],
3233 LineOffset[screen_row] + screen_Columns)
3234 == 2
3235 || (*mb_off2cells)(LineOffset[screen_row - 1]
3236 + (int)Columns - 2,
3237 LineOffset[screen_row] + screen_Columns)
3238 == 2)))
3239 {
3240 // First make sure we are at the end of the screen line,
3241 // then output the same character again to let the
3242 // terminal know about the wrap. If the terminal doesn't
3243 // auto-wrap, we overwrite the character.
3244 if (screen_cur_col != wp->w_width)
3245 screen_char(LineOffset[screen_row - 1]
3246 + (unsigned)Columns - 1,
3247 screen_row - 1, (int)(Columns - 1));
3248
3249 // When there is a multi-byte character, just output a
3250 // space to keep it simple.
3251 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
3252 screen_row - 1] + (Columns - 1)]) > 1)
3253 out_char(' ');
3254 else
3255 out_char(ScreenLines[LineOffset[screen_row - 1]
3256 + (Columns - 1)]);
3257 // force a redraw of the first char on the next line
3258 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
3259 screen_start(); // don't know where cursor is now
3260 }
3261 }
3262
3263 col = 0;
3264 off = (unsigned)(current_ScreenLine - ScreenLines);
3265#ifdef FEAT_RIGHTLEFT
3266 if (wp->w_p_rl)
3267 {
3268 col = wp->w_width - 1; // col is not used if breaking!
3269 off += col;
3270 }
3271#endif
3272
3273 // reset the drawing state for the start of a wrapped line
3274 draw_state = WL_START;
3275 saved_n_extra = n_extra;
3276 saved_p_extra = p_extra;
3277 saved_c_extra = c_extra;
3278 saved_c_final = c_final;
3279#ifdef FEAT_SYN_HL
3280 if (!(cul_screenline
3281# ifdef FEAT_DIFF
Bram Moolenaare7705982020-04-21 22:23:15 +02003282 && diff_hlf == (hlf_T)0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003283# endif
Bram Moolenaare7705982020-04-21 22:23:15 +02003284 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003285 saved_char_attr = char_attr;
3286 else
3287#endif
3288 saved_char_attr = 0;
3289 n_extra = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01003290 lcs_prec_todo = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003291#ifdef FEAT_LINEBREAK
3292# ifdef FEAT_DIFF
3293 if (filler_todo <= 0)
3294# endif
3295 need_showbreak = TRUE;
3296#endif
3297#ifdef FEAT_DIFF
3298 --filler_todo;
3299 // When the filler lines are actually below the last line of the
3300 // file, don't draw the line itself, break here.
3301 if (filler_todo == 0 && wp->w_botfill)
3302 break;
3303#endif
3304 }
3305
3306 } // for every character in the line
3307
3308#ifdef FEAT_SPELL
3309 // After an empty line check first word for capital.
3310 if (*skipwhite(line) == NUL)
3311 {
3312 capcol_lnum = lnum + 1;
3313 cap_col = 0;
3314 }
3315#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003316#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003317 vim_free(text_props);
3318 vim_free(text_prop_idxs);
3319#endif
3320
3321 vim_free(p_extra_free);
3322 return row;
3323}