blob: caff9abc70abc0dfe1f165829e44a2502c78031a [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 Moolenaareed9d462021-02-15 20:38:25 +0100770 || wp->w_lcs_chars.trail
771 || wp->w_lcs_chars.lead
772 || wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200773 extra_check = TRUE;
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100774
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200775 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +0100776 if (wp->w_lcs_chars.trail)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200777 {
778 trailcol = (colnr_T)STRLEN(ptr);
779 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
780 --trailcol;
781 trailcol += (colnr_T) (ptr - line);
782 }
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100783 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +0100784 if (wp->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100785 {
786 leadcol = 0;
787 while (VIM_ISWHITE(ptr[leadcol]))
788 ++leadcol;
789 if (ptr[leadcol] == NUL)
790 // in a line full of spaces all of them are treated as trailing
791 leadcol = (colnr_T)0;
792 else
793 // keep track of the first column not filled with spaces
794 leadcol += (colnr_T) (ptr - line) + 1;
795 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200796 }
797
798 wcr_attr = get_wcr_attr(wp);
799 if (wcr_attr != 0)
800 {
801 win_attr = wcr_attr;
802 area_highlighting = TRUE;
803 }
Bram Moolenaar34390282019-10-16 14:38:26 +0200804
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100805#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200806 if (WIN_IS_POPUP(wp))
807 screen_line_flags |= SLF_POPUP;
808#endif
809
810 // 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
811 // first character to be displayed.
812 if (wp->w_p_wrap)
813 v = wp->w_skipcol;
814 else
815 v = wp->w_leftcol;
816 if (v > 0 && !number_only)
817 {
818 char_u *prev_ptr = ptr;
819
820 while (vcol < v && *ptr != NUL)
821 {
822 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
823 vcol += c;
824 prev_ptr = ptr;
825 MB_PTR_ADV(ptr);
826 }
827
828 // When:
829 // - 'cuc' is set, or
830 // - 'colorcolumn' is set, or
831 // - 'virtualedit' is set, or
832 // - the visual mode is active,
833 // the end of the line may be before the start of the displayed part.
834 if (vcol < v && (
835#ifdef FEAT_SYN_HL
836 wp->w_p_cuc || draw_color_col ||
837#endif
838 virtual_active() ||
839 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
840 vcol = v;
841
842 // Handle a character that's not completely on the screen: Put ptr at
843 // that character but skip the first few screen characters.
844 if (vcol > v)
845 {
846 vcol -= c;
847 ptr = prev_ptr;
848 // If the character fits on the screen, don't need to skip it.
849 // Except for a TAB.
850 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
851 n_skip = v - vcol;
852 }
853
854 // Adjust for when the inverted text is before the screen,
855 // and when the start of the inverted text is before the screen.
856 if (tocol <= vcol)
857 fromcol = 0;
858 else if (fromcol >= 0 && fromcol < vcol)
859 fromcol = vcol;
860
861#ifdef FEAT_LINEBREAK
862 // When w_skipcol is non-zero, first line needs 'showbreak'
863 if (wp->w_p_wrap)
864 need_showbreak = TRUE;
865#endif
866#ifdef FEAT_SPELL
867 // When spell checking a word we need to figure out the start of the
868 // word and if it's badly spelled or not.
869 if (has_spell)
870 {
871 int len;
872 colnr_T linecol = (colnr_T)(ptr - line);
873 hlf_T spell_hlf = HLF_COUNT;
874
875 pos = wp->w_cursor;
876 wp->w_cursor.lnum = lnum;
877 wp->w_cursor.col = linecol;
878 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
879
880 // spell_move_to() may call ml_get() and make "line" invalid
881 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
882 ptr = line + linecol;
883
884 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
885 {
886 // no bad word found at line start, don't check until end of a
887 // word
888 spell_hlf = HLF_COUNT;
889 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
890 }
891 else
892 {
893 // bad word found, use attributes until end of word
894 word_end = wp->w_cursor.col + len + 1;
895
896 // Turn index into actual attributes.
897 if (spell_hlf != HLF_COUNT)
898 spell_attr = highlight_attr[spell_hlf];
899 }
900 wp->w_cursor = pos;
901
902# ifdef FEAT_SYN_HL
903 // Need to restart syntax highlighting for this line.
904 if (has_syntax)
905 syntax_start(wp, lnum);
906# endif
907 }
908#endif
909 }
910
911 // Correct highlighting for cursor that can't be disabled.
912 // Avoids having to check this for each character.
913 if (fromcol >= 0)
914 {
915 if (noinvcur)
916 {
917 if ((colnr_T)fromcol == wp->w_virtcol)
918 {
919 // highlighting starts at cursor, let it start just after the
920 // cursor
921 fromcol_prev = fromcol;
922 fromcol = -1;
923 }
924 else if ((colnr_T)fromcol < wp->w_virtcol)
925 // restart highlighting after the cursor
926 fromcol_prev = wp->w_virtcol;
927 }
928 if (fromcol >= tocol)
929 fromcol = -1;
930 }
931
932#ifdef FEAT_SEARCH_EXTRA
933 if (!number_only)
934 {
935 v = (long)(ptr - line);
936 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
937 &line, &screen_search_hl,
938 &search_attr);
939 ptr = line + v; // "line" may have been updated
940 }
941#endif
942
943#ifdef FEAT_SYN_HL
944 // Cursor line highlighting for 'cursorline' in the current window.
945 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
946 {
947 // Do not show the cursor line in the text when Visual mode is active,
zeertzjqc20e46a2022-03-23 14:55:23 +0000948 // because it's not clear what is selected then.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200949 if (!(wp == curwin && VIsual_active)
950 && wp->w_p_culopt_flags != CULOPT_NBR)
951 {
952 cul_screenline = (wp->w_p_wrap
953 && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
954
955 // Only set line_attr here when "screenline" is not present in
956 // 'cursorlineopt'. Otherwise it's done later.
957 if (!cul_screenline)
958 {
959 cul_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar39f7aa32020-08-31 22:00:05 +0200960# ifdef FEAT_SIGNS
961 // Combine the 'cursorline' and sign highlighting, depending on
962 // the sign priority.
963 if (sign_present && sattr.sat_linehl > 0)
964 {
965 if (sattr.sat_priority >= 100)
966 line_attr = hl_combine_attr(cul_attr, line_attr);
967 else
968 line_attr = hl_combine_attr(line_attr, cul_attr);
969 }
970 else
971# endif
972 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200973 }
974 else
975 {
976 line_attr_save = line_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200977 margin_columns_win(wp, &left_curline_col, &right_curline_col);
978 }
979 area_highlighting = TRUE;
980 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200981 }
982#endif
983
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100984#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200985 {
986 char_u *prop_start;
987
988 text_prop_count = get_text_props(wp->w_buffer, lnum,
989 &prop_start, FALSE);
990 if (text_prop_count > 0)
991 {
992 // Make a copy of the properties, so that they are properly
993 // aligned.
994 text_props = ALLOC_MULT(textprop_T, text_prop_count);
995 if (text_props != NULL)
996 mch_memmove(text_props, prop_start,
997 text_prop_count * sizeof(textprop_T));
998
999 // Allocate an array for the indexes.
1000 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
1001 area_highlighting = TRUE;
1002 extra_check = TRUE;
1003 }
1004 }
1005#endif
1006
1007 off = (unsigned)(current_ScreenLine - ScreenLines);
1008 col = 0;
1009
1010#ifdef FEAT_RIGHTLEFT
1011 if (wp->w_p_rl)
1012 {
1013 // Rightleft window: process the text in the normal direction, but put
1014 // it in current_ScreenLine[] from right to left. Start at the
1015 // rightmost column of the window.
1016 col = wp->w_width - 1;
1017 off += col;
1018 screen_line_flags |= SLF_RIGHTLEFT;
1019 }
1020#endif
1021
1022 // Repeat for the whole displayed line.
1023 for (;;)
1024 {
1025#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02001026 int has_match_conc = 0; // match wants to conceal
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001027#endif
1028#ifdef FEAT_CONCEAL
1029 int did_decrement_ptr = FALSE;
1030#endif
1031 // Skip this quickly when working on the text.
1032 if (draw_state != WL_LINE)
1033 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001034#ifdef FEAT_SYN_HL
1035 if (cul_screenline)
1036 {
1037 cul_attr = 0;
1038 line_attr = line_attr_save;
1039 }
1040#endif
1041
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001042#ifdef FEAT_CMDWIN
1043 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
1044 {
1045 draw_state = WL_CMDLINE;
1046 if (cmdwin_type != 0 && wp == curwin)
1047 {
1048 // Draw the cmdline character.
1049 n_extra = 1;
1050 c_extra = cmdwin_type;
1051 c_final = NUL;
1052 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
1053 }
1054 }
1055#endif
1056
1057#ifdef FEAT_FOLDING
1058 if (draw_state == WL_FOLD - 1 && n_extra == 0)
1059 {
1060 int fdc = compute_foldcolumn(wp, 0);
1061
1062 draw_state = WL_FOLD;
1063 if (fdc > 0)
1064 {
1065 // Draw the 'foldcolumn'. Allocate a buffer, "extra" may
1066 // already be in use.
1067 vim_free(p_extra_free);
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001068 p_extra_free = alloc(MAX_MCO * fdc + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001069 if (p_extra_free != NULL)
1070 {
Bram Moolenaar9355ae42021-03-08 19:04:05 +01001071 n_extra = (int)fill_foldcolumn(p_extra_free, wp,
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001072 FALSE, lnum);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001073 p_extra_free[n_extra] = NUL;
1074 p_extra = p_extra_free;
1075 c_extra = NUL;
1076 c_final = NUL;
Bram Moolenaare413ea02021-11-24 16:20:13 +00001077 if (use_cursor_line_sign(wp, lnum))
1078 char_attr =
1079 hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLF));
1080 else
1081 char_attr =
1082 hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001083 }
1084 }
1085 }
1086#endif
1087
1088#ifdef FEAT_SIGNS
1089 if (draw_state == WL_SIGN - 1 && n_extra == 0)
1090 {
1091 draw_state = WL_SIGN;
1092 // Show the sign column when there are any signs in this
1093 // buffer or when using Netbeans.
1094 if (signcolumn_on(wp))
1095 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
1096 row, startrow, filler_lines, filler_todo, &c_extra,
1097 &c_final, extra, &p_extra, &n_extra, &char_attr);
1098 }
1099#endif
1100
1101 if (draw_state == WL_NR - 1 && n_extra == 0)
1102 {
1103 draw_state = WL_NR;
1104 // Display the absolute or relative line number. After the
1105 // first fill with blanks when the 'n' flag isn't in 'cpo'
1106 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar936dc602022-03-06 20:47:01 +00001107 && (row == startrow + filler_lines
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001108 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
1109 {
1110#ifdef FEAT_SIGNS
1111 // If 'signcolumn' is set to 'number' and a sign is present
1112 // in 'lnum', then display the sign instead of the line
1113 // number.
1114 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
1115 && sign_present)
1116 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
1117 row, startrow, filler_lines, filler_todo,
1118 &c_extra, &c_final, extra, &p_extra, &n_extra,
1119 &char_attr);
1120 else
1121#endif
1122 {
1123 // Draw the line number (empty space after wrapping).
Bram Moolenaar936dc602022-03-06 20:47:01 +00001124 if (row == startrow + filler_lines)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001125 {
1126 long num;
1127 char *fmt = "%*ld ";
1128
1129 if (wp->w_p_nu && !wp->w_p_rnu)
1130 // 'number' + 'norelativenumber'
1131 num = (long)lnum;
1132 else
1133 {
1134 // 'relativenumber', don't use negative numbers
1135 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1136 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1137 {
1138 // 'number' + 'relativenumber'
1139 num = lnum;
1140 fmt = "%-*ld ";
1141 }
1142 }
1143
1144 sprintf((char *)extra, fmt,
1145 number_width(wp), num);
1146 if (wp->w_skipcol > 0)
1147 for (p_extra = extra; *p_extra == ' '; ++p_extra)
1148 *p_extra = '-';
1149#ifdef FEAT_RIGHTLEFT
1150 if (wp->w_p_rl) // reverse line numbers
1151 {
1152 char_u *p1, *p2;
1153 int t;
1154
1155 // like rl_mirror(), but keep the space at the end
Christian Brabandt29f0dc32021-06-16 19:28:34 +02001156 p2 = skipwhite(extra);
1157 p2 = skiptowhite(p2) - 1;
1158 for (p1 = skipwhite(extra); p1 < p2; ++p1, --p2)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001159 {
1160 t = *p1;
1161 *p1 = *p2;
1162 *p2 = t;
1163 }
1164 }
1165#endif
1166 p_extra = extra;
1167 c_extra = NUL;
1168 c_final = NUL;
1169 }
1170 else
1171 {
1172 c_extra = ' ';
1173 c_final = NUL;
1174 }
1175 n_extra = number_width(wp) + 1;
1176 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
1177#ifdef FEAT_SYN_HL
1178 // When 'cursorline' is set highlight the line number of
1179 // the current line differently.
zeertzjq754d2b42022-03-13 13:40:45 +00001180 // When 'cursorlineopt' does not have "line" only
1181 // highlight the line number itself.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001182 // TODO: Can we use CursorLine instead of CursorLineNr
1183 // when CursorLineNr isn't set?
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001184 if (wp->w_p_cul
1185 && lnum == wp->w_cursor.lnum
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001186 && (wp->w_p_culopt_flags & CULOPT_NBR)
Bram Moolenaar127969c2022-03-06 19:54:13 +00001187 && (row == startrow + filler_lines
1188 || (row > startrow + filler_lines
zeertzjq754d2b42022-03-13 13:40:45 +00001189 && (wp->w_p_culopt_flags & CULOPT_LINE))))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001190 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
1191#endif
Bram Moolenaarefae76a2019-10-27 22:54:58 +01001192 if (wp->w_p_rnu && lnum < wp->w_cursor.lnum
1193 && HL_ATTR(HLF_LNA) != 0)
1194 // Use LineNrAbove
1195 char_attr = hl_combine_attr(wcr_attr,
1196 HL_ATTR(HLF_LNA));
1197 if (wp->w_p_rnu && lnum > wp->w_cursor.lnum
1198 && HL_ATTR(HLF_LNB) != 0)
1199 // Use LineNrBelow
1200 char_attr = hl_combine_attr(wcr_attr,
1201 HL_ATTR(HLF_LNB));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001202 }
James McCoya80aad72021-12-22 19:45:28 +00001203#ifdef FEAT_SIGNS
1204 if (num_attr)
1205 char_attr = num_attr;
1206#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001207 }
1208 }
1209
1210#ifdef FEAT_LINEBREAK
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001211 if (wp->w_briopt_sbr && draw_state == WL_BRI - 1
Bram Moolenaaree857022019-11-09 23:26:40 +01001212 && n_extra == 0 && *get_showbreak_value(wp) != NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001213 // draw indent after showbreak value
1214 draw_state = WL_BRI;
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001215 else if (wp->w_briopt_sbr && draw_state == WL_SBR && n_extra == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001216 // After the showbreak, draw the breakindent
1217 draw_state = WL_BRI - 1;
1218
1219 // draw 'breakindent': indent wrapped text accordingly
1220 if (draw_state == WL_BRI - 1 && n_extra == 0)
1221 {
1222 draw_state = WL_BRI;
1223 // if need_showbreak is set, breakindent also applies
Bram Moolenaarfe154992022-03-22 20:42:12 +00001224 if (wp->w_p_bri && (row != startrow || need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001225# ifdef FEAT_DIFF
1226 && filler_lines == 0
1227# endif
1228 )
1229 {
1230 char_attr = 0;
1231# ifdef FEAT_DIFF
1232 if (diff_hlf != (hlf_T)0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001233 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001234# endif
1235 p_extra = NULL;
1236 c_extra = ' ';
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001237 c_final = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001238 n_extra = get_breakindent_win(wp,
1239 ml_get_buf(wp->w_buffer, lnum, FALSE));
Bram Moolenaare882f7a2020-05-16 14:07:39 +02001240 if (row == startrow)
1241 {
1242 n_extra -= win_col_off2(wp);
1243 if (n_extra < 0)
1244 n_extra = 0;
1245 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001246 if (wp->w_skipcol > 0 && wp->w_p_wrap && wp->w_briopt_sbr)
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001247 need_showbreak = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001248 // Correct end of highlighted area for 'breakindent',
1249 // required when 'linebreak' is also set.
1250 if (tocol == vcol)
1251 tocol += n_extra;
1252 }
1253 }
1254#endif
1255
1256#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
1257 if (draw_state == WL_SBR - 1 && n_extra == 0)
1258 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001259 char_u *sbr;
1260
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001261 draw_state = WL_SBR;
1262# ifdef FEAT_DIFF
1263 if (filler_todo > 0)
1264 {
1265 // Draw "deleted" diff line(s).
1266 if (char2cells(fill_diff) > 1)
1267 {
1268 c_extra = '-';
1269 c_final = NUL;
1270 }
1271 else
1272 {
1273 c_extra = fill_diff;
1274 c_final = NUL;
1275 }
1276# ifdef FEAT_RIGHTLEFT
1277 if (wp->w_p_rl)
1278 n_extra = col + 1;
1279 else
1280# endif
1281 n_extra = wp->w_width - col;
1282 char_attr = HL_ATTR(HLF_DED);
1283 }
1284# endif
1285# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001286 sbr = get_showbreak_value(wp);
1287 if (*sbr != NUL && need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001288 {
1289 // Draw 'showbreak' at the start of each broken line.
Bram Moolenaaree857022019-11-09 23:26:40 +01001290 p_extra = sbr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001291 c_extra = NUL;
1292 c_final = NUL;
Bram Moolenaaree857022019-11-09 23:26:40 +01001293 n_extra = (int)STRLEN(sbr);
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001294 if (wp->w_skipcol == 0 || !wp->w_p_wrap)
1295 need_showbreak = FALSE;
Bram Moolenaaree857022019-11-09 23:26:40 +01001296 vcol_sbr = vcol + MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001297 // Correct end of highlighted area for 'showbreak',
1298 // required when 'linebreak' is also set.
1299 if (tocol == vcol)
1300 tocol += n_extra;
1301 // combine 'showbreak' with 'wincolor'
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001302 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001303# ifdef FEAT_SYN_HL
1304 // combine 'showbreak' with 'cursorline'
1305 if (cul_attr != 0)
1306 char_attr = hl_combine_attr(char_attr, cul_attr);
1307# endif
1308 }
1309# endif
1310 }
1311#endif
1312
1313 if (draw_state == WL_LINE - 1 && n_extra == 0)
1314 {
1315 draw_state = WL_LINE;
1316 if (saved_n_extra)
1317 {
1318 // Continue item from end of wrapped line.
1319 n_extra = saved_n_extra;
1320 c_extra = saved_c_extra;
1321 c_final = saved_c_final;
1322 p_extra = saved_p_extra;
1323 char_attr = saved_char_attr;
1324 }
1325 else
1326 char_attr = win_attr;
1327 }
1328 }
1329#ifdef FEAT_SYN_HL
zeertzjq4f33bc22021-08-05 17:57:02 +02001330 if (cul_screenline && draw_state == WL_LINE
1331 && vcol >= left_curline_col
1332 && vcol < right_curline_col)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001333 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001334 cul_attr = HL_ATTR(HLF_CUL);
1335 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001336 }
1337#endif
1338
1339 // When still displaying '$' of change command, stop at cursor.
1340 // When only displaying the (relative) line number and that's done,
1341 // stop here.
Bram Moolenaar511feec2020-06-18 19:15:27 +02001342 if (((dollar_vcol >= 0 && wp == curwin
1343 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol)
1344 || (number_only && draw_state > WL_NR))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001345#ifdef FEAT_DIFF
1346 && filler_todo <= 0
1347#endif
1348 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001349 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001350 screen_line(screen_row, wp->w_wincol, col, -wp->w_width,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001351 screen_line_flags);
1352 // Pretend we have finished updating the window. Except when
1353 // 'cursorcolumn' is set.
1354#ifdef FEAT_SYN_HL
1355 if (wp->w_p_cuc)
1356 row = wp->w_cline_row + wp->w_cline_height;
1357 else
1358#endif
1359 row = wp->w_height;
1360 break;
1361 }
1362
Bram Moolenaar34390282019-10-16 14:38:26 +02001363 if (draw_state == WL_LINE && (area_highlighting || extra_check))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001364 {
1365 // handle Visual or match highlighting in this line
1366 if (vcol == fromcol
1367 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
1368 && (*mb_ptr2cells)(ptr) > 1)
1369 || ((int)vcol_prev == fromcol_prev
1370 && vcol_prev < vcol // not at margin
1371 && vcol < tocol))
1372 area_attr = vi_attr; // start highlighting
1373 else if (area_attr != 0
1374 && (vcol == tocol
1375 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
1376 area_attr = 0; // stop highlighting
1377
1378#ifdef FEAT_SEARCH_EXTRA
1379 if (!n_extra)
1380 {
1381 // Check for start/end of 'hlsearch' and other matches.
1382 // After end, check for start/end of next match.
1383 // When another match, have to check for start again.
1384 v = (long)(ptr - line);
1385 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
1386 &screen_search_hl, &has_match_conc,
Bram Moolenaar0c359af2021-11-29 19:18:57 +00001387 &match_conc, did_line_attr, lcs_eol_one,
1388 &on_last_col);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001389 ptr = line + v; // "line" may have been changed
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001390
1391 // Do not allow a conceal over EOL otherwise EOL will be missed
1392 // and bad things happen.
1393 if (*ptr == NUL)
1394 has_match_conc = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001395 }
1396#endif
1397
1398#ifdef FEAT_DIFF
1399 if (diff_hlf != (hlf_T)0)
1400 {
1401 if (diff_hlf == HLF_CHD && ptr - line >= change_start
1402 && n_extra == 0)
1403 diff_hlf = HLF_TXD; // changed text
1404 if (diff_hlf == HLF_TXD && ptr - line > change_end
1405 && n_extra == 0)
1406 diff_hlf = HLF_CHD; // changed line
1407 line_attr = HL_ATTR(diff_hlf);
1408 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
1409 && wp->w_p_culopt_flags != CULOPT_NBR
1410 && (!cul_screenline || (vcol >= left_curline_col
1411 && vcol <= right_curline_col)))
1412 line_attr = hl_combine_attr(
1413 line_attr, HL_ATTR(HLF_CUL));
1414 }
1415#endif
1416
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001417#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001418 if (text_props != NULL)
1419 {
1420 int pi;
1421 int bcol = (int)(ptr - line);
1422
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001423 if (n_extra > 0
1424# ifdef FEAT_LINEBREAK
1425 && !in_linebreak
1426# endif
1427 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001428 --bcol; // still working on the previous char, e.g. Tab
1429
1430 // Check if any active property ends.
1431 for (pi = 0; pi < text_props_active; ++pi)
1432 {
1433 int tpi = text_prop_idxs[pi];
1434
1435 if (bcol >= text_props[tpi].tp_col - 1
1436 + text_props[tpi].tp_len)
1437 {
1438 if (pi + 1 < text_props_active)
1439 mch_memmove(text_prop_idxs + pi,
1440 text_prop_idxs + pi + 1,
1441 sizeof(int)
1442 * (text_props_active - (pi + 1)));
1443 --text_props_active;
1444 --pi;
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001445# ifdef FEAT_LINEBREAK
1446 // not exactly right but should work in most cases
1447 if (in_linebreak && syntax_attr == text_prop_attr)
1448 syntax_attr = 0;
1449# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001450 }
1451 }
1452
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001453# ifdef FEAT_LINEBREAK
1454 if (n_extra > 0 && in_linebreak)
1455 // not on the next char yet, don't start another prop
1456 --bcol;
1457# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001458 // Add any text property that starts in this column.
1459 while (text_prop_next < text_prop_count
1460 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001461 {
1462 if (bcol <= text_props[text_prop_next].tp_col - 1
1463 + text_props[text_prop_next].tp_len)
1464 text_prop_idxs[text_props_active++] = text_prop_next;
1465 ++text_prop_next;
1466 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001467
1468 text_prop_attr = 0;
1469 text_prop_combine = FALSE;
Bram Moolenaar053f7122019-09-23 22:17:15 +02001470 text_prop_type = NULL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001471 if (text_props_active > 0)
1472 {
1473 // Sort the properties on priority and/or starting last.
1474 // Then combine the attributes, highest priority last.
1475 current_text_props = text_props;
1476 current_buf = wp->w_buffer;
1477 qsort((void *)text_prop_idxs, (size_t)text_props_active,
1478 sizeof(int), text_prop_compare);
1479
1480 for (pi = 0; pi < text_props_active; ++pi)
1481 {
1482 int tpi = text_prop_idxs[pi];
1483 proptype_T *pt = text_prop_type_by_id(
1484 wp->w_buffer, text_props[tpi].tp_type);
1485
1486 if (pt != NULL && pt->pt_hl_id > 0)
1487 {
1488 int pt_attr = syn_id2attr(pt->pt_hl_id);
1489
1490 text_prop_type = pt;
1491 text_prop_attr =
1492 hl_combine_attr(text_prop_attr, pt_attr);
1493 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
1494 }
1495 }
1496 }
1497 }
1498#endif
1499
Bram Moolenaara74fda62019-10-19 17:38:03 +02001500#ifdef FEAT_SYN_HL
Bram Moolenaara74fda62019-10-19 17:38:03 +02001501 if (extra_check && n_extra == 0)
Bram Moolenaar34390282019-10-16 14:38:26 +02001502 {
Bram Moolenaar82260af2019-10-20 13:16:22 +02001503 syntax_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001504# ifdef FEAT_TERMINAL
Bram Moolenaar34390282019-10-16 14:38:26 +02001505 if (get_term_attr)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001506 syntax_attr = term_get_attr(wp, lnum, vcol);
Bram Moolenaara74fda62019-10-19 17:38:03 +02001507# endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001508 // Get syntax attribute.
1509 if (has_syntax)
1510 {
1511 // Get the syntax attribute for the character. If there
1512 // is an error, disable syntax highlighting.
1513 save_did_emsg = did_emsg;
1514 did_emsg = FALSE;
1515
1516 v = (long)(ptr - line);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001517 if (v == prev_syntax_col)
1518 // at same column again
1519 syntax_attr = prev_syntax_attr;
1520 else
1521 {
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001522# ifdef FEAT_SPELL
Bram Moolenaar9115c612019-10-16 16:57:06 +02001523 can_spell = TRUE;
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001524# endif
Bram Moolenaar9115c612019-10-16 16:57:06 +02001525 syntax_attr = get_syntax_attr((colnr_T)v,
Bram Moolenaar34390282019-10-16 14:38:26 +02001526# ifdef FEAT_SPELL
1527 has_spell ? &can_spell :
1528# endif
1529 NULL, FALSE);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001530 prev_syntax_col = v;
1531 prev_syntax_attr = syntax_attr;
1532 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001533
Bram Moolenaar34390282019-10-16 14:38:26 +02001534 if (did_emsg)
1535 {
1536 wp->w_s->b_syn_error = TRUE;
1537 has_syntax = FALSE;
1538 syntax_attr = 0;
1539 }
1540 else
1541 did_emsg = save_did_emsg;
1542# ifdef SYN_TIME_LIMIT
1543 if (wp->w_s->b_syn_slow)
1544 has_syntax = FALSE;
1545# endif
1546
1547 // Need to get the line again, a multi-line regexp may
1548 // have made it invalid.
1549 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1550 ptr = line + v;
1551# ifdef FEAT_CONCEAL
1552 // no concealing past the end of the line, it interferes
1553 // with line highlighting
1554 if (*ptr == NUL)
1555 syntax_flags = 0;
1556 else
1557 syntax_flags = get_syntax_info(&syntax_seqnr);
1558# endif
1559 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001560 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001561# ifdef FEAT_PROP_POPUP
Bram Moolenaardbd43162019-11-09 21:28:14 +01001562 // Combine text property highlight into syntax highlight.
1563 if (text_prop_type != NULL)
1564 {
1565 if (text_prop_combine)
1566 syntax_attr = hl_combine_attr(syntax_attr, text_prop_attr);
1567 else
1568 syntax_attr = text_prop_attr;
1569 }
1570# endif
Bram Moolenaara74fda62019-10-19 17:38:03 +02001571#endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001572
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001573 // Decide which of the highlight attributes to use.
1574 attr_pri = TRUE;
1575#ifdef LINE_ATTR
1576 if (area_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001577 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001578 char_attr = hl_combine_attr(line_attr, area_attr);
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001579 if (!highlight_match)
1580 // let search highlight show in Visual area if possible
1581 char_attr = hl_combine_attr(search_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001582# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001583 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001584# endif
1585 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001586 else if (search_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001587 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001588 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001589# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001590 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001591# endif
1592 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001593 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
1594 || vcol < fromcol || vcol_prev < fromcol_prev
1595 || vcol >= tocol))
1596 {
1597 // Use line_attr when not in the Visual or 'incsearch' area
1598 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar34390282019-10-16 14:38:26 +02001599# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001600 char_attr = hl_combine_attr(syntax_attr, line_attr);
1601# else
1602 char_attr = line_attr;
Bram Moolenaar34390282019-10-16 14:38:26 +02001603# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001604 attr_pri = FALSE;
1605 }
1606#else
1607 if (area_attr != 0)
1608 char_attr = area_attr;
1609 else if (search_attr != 0)
1610 char_attr = search_attr;
1611#endif
1612 else
1613 {
1614 attr_pri = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001615#ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001616 char_attr = syntax_attr;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001617#else
Bram Moolenaardbd43162019-11-09 21:28:14 +01001618 char_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001619#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001620 }
1621 }
Bram Moolenaar024dbd22019-11-02 22:00:15 +01001622
1623 // combine attribute with 'wincolor'
1624 if (win_attr != 0)
1625 {
1626 if (char_attr == 0)
1627 char_attr = win_attr;
1628 else
1629 char_attr = hl_combine_attr(win_attr, char_attr);
1630 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001631
1632 // Get the next character to put on the screen.
1633
1634 // The "p_extra" points to the extra stuff that is inserted to
1635 // represent special characters (non-printable stuff) and other
1636 // things. When all characters are the same, c_extra is used.
1637 // If c_final is set, it will compulsorily be used at the end.
1638 // "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
1639 // "p_extra[n_extra]".
1640 // For the '$' of the 'list' option, n_extra == 1, p_extra == "".
1641 if (n_extra > 0)
1642 {
1643 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
1644 {
1645 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
1646 mb_c = c; // doesn't handle non-utf-8 multi-byte!
1647 if (enc_utf8 && utf_char2len(c) > 1)
1648 {
1649 mb_utf8 = TRUE;
1650 u8cc[0] = 0;
1651 c = 0xc0;
1652 }
1653 else
1654 mb_utf8 = FALSE;
1655 }
1656 else
1657 {
1658 c = *p_extra;
1659 if (has_mbyte)
1660 {
1661 mb_c = c;
1662 if (enc_utf8)
1663 {
1664 // If the UTF-8 character is more than one byte:
1665 // Decode it into "mb_c".
1666 mb_l = utfc_ptr2len(p_extra);
1667 mb_utf8 = FALSE;
1668 if (mb_l > n_extra)
1669 mb_l = 1;
1670 else if (mb_l > 1)
1671 {
1672 mb_c = utfc_ptr2char(p_extra, u8cc);
1673 mb_utf8 = TRUE;
1674 c = 0xc0;
1675 }
1676 }
1677 else
1678 {
1679 // if this is a DBCS character, put it in "mb_c"
1680 mb_l = MB_BYTE2LEN(c);
1681 if (mb_l >= n_extra)
1682 mb_l = 1;
1683 else if (mb_l > 1)
1684 mb_c = (c << 8) + p_extra[1];
1685 }
1686 if (mb_l == 0) // at the NUL at end-of-line
1687 mb_l = 1;
1688
1689 // If a double-width char doesn't fit display a '>' in the
1690 // last column.
1691 if ((
1692# ifdef FEAT_RIGHTLEFT
1693 wp->w_p_rl ? (col <= 0) :
1694# endif
1695 (col >= wp->w_width - 1))
1696 && (*mb_char2cells)(mb_c) == 2)
1697 {
1698 c = '>';
1699 mb_c = c;
1700 mb_l = 1;
1701 mb_utf8 = FALSE;
1702 multi_attr = HL_ATTR(HLF_AT);
1703#ifdef FEAT_SYN_HL
1704 if (cul_attr)
1705 multi_attr = hl_combine_attr(multi_attr, cul_attr);
1706#endif
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001707 multi_attr = hl_combine_attr(win_attr, multi_attr);
1708
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001709 // put the pointer back to output the double-width
1710 // character at the start of the next line.
1711 ++n_extra;
1712 --p_extra;
1713 }
1714 else
1715 {
1716 n_extra -= mb_l - 1;
1717 p_extra += mb_l - 1;
1718 }
1719 }
1720 ++p_extra;
1721 }
1722 --n_extra;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00001723#if defined(FEAT_LINEBREAK) && defined(FEAT_PROP_POPUP)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001724 if (n_extra <= 0)
1725 in_linebreak = FALSE;
1726#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001727 }
1728 else
1729 {
1730#ifdef FEAT_LINEBREAK
1731 int c0;
1732#endif
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001733 VIM_CLEAR(p_extra_free);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001734
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001735 // Get a character from the line itself.
1736 c = *ptr;
1737#ifdef FEAT_LINEBREAK
1738 c0 = *ptr;
1739#endif
1740 if (has_mbyte)
1741 {
1742 mb_c = c;
1743 if (enc_utf8)
1744 {
1745 // If the UTF-8 character is more than one byte: Decode it
1746 // into "mb_c".
1747 mb_l = utfc_ptr2len(ptr);
1748 mb_utf8 = FALSE;
1749 if (mb_l > 1)
1750 {
1751 mb_c = utfc_ptr2char(ptr, u8cc);
1752 // Overlong encoded ASCII or ASCII with composing char
1753 // is displayed normally, except a NUL.
1754 if (mb_c < 0x80)
1755 {
1756 c = mb_c;
1757#ifdef FEAT_LINEBREAK
1758 c0 = mb_c;
1759#endif
1760 }
1761 mb_utf8 = TRUE;
1762
1763 // At start of the line we can have a composing char.
1764 // Draw it as a space with a composing char.
1765 if (utf_iscomposing(mb_c))
1766 {
1767 int i;
1768
1769 for (i = Screen_mco - 1; i > 0; --i)
1770 u8cc[i] = u8cc[i - 1];
1771 u8cc[0] = mb_c;
1772 mb_c = ' ';
1773 }
1774 }
1775
1776 if ((mb_l == 1 && c >= 0x80)
1777 || (mb_l >= 1 && mb_c == 0)
1778 || (mb_l > 1 && (!vim_isprintc(mb_c))))
1779 {
1780 // Illegal UTF-8 byte: display as <xx>.
1781 // Non-BMP character : display as ? or fullwidth ?.
1782 transchar_hex(extra, mb_c);
1783# ifdef FEAT_RIGHTLEFT
1784 if (wp->w_p_rl) // reverse
1785 rl_mirror(extra);
1786# endif
1787 p_extra = extra;
1788 c = *p_extra;
1789 mb_c = mb_ptr2char_adv(&p_extra);
1790 mb_utf8 = (c >= 0x80);
1791 n_extra = (int)STRLEN(p_extra);
1792 c_extra = NUL;
1793 c_final = NUL;
1794 if (area_attr == 0 && search_attr == 0)
1795 {
1796 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001797 extra_attr = hl_combine_attr(
1798 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001799 saved_attr2 = char_attr; // save current attr
1800 }
1801 }
1802 else if (mb_l == 0) // at the NUL at end-of-line
1803 mb_l = 1;
1804#ifdef FEAT_ARABIC
1805 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
1806 {
1807 // Do Arabic shaping.
1808 int pc, pc1, nc;
1809 int pcc[MAX_MCO];
1810
1811 // The idea of what is the previous and next
1812 // character depends on 'rightleft'.
1813 if (wp->w_p_rl)
1814 {
1815 pc = prev_c;
1816 pc1 = prev_c1;
1817 nc = utf_ptr2char(ptr + mb_l);
1818 prev_c1 = u8cc[0];
1819 }
1820 else
1821 {
1822 pc = utfc_ptr2char(ptr + mb_l, pcc);
1823 nc = prev_c;
1824 pc1 = pcc[0];
1825 }
1826 prev_c = mb_c;
1827
1828 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
1829 }
1830 else
1831 prev_c = mb_c;
1832#endif
1833 }
1834 else // enc_dbcs
1835 {
1836 mb_l = MB_BYTE2LEN(c);
1837 if (mb_l == 0) // at the NUL at end-of-line
1838 mb_l = 1;
1839 else if (mb_l > 1)
1840 {
1841 // We assume a second byte below 32 is illegal.
1842 // Hopefully this is OK for all double-byte encodings!
1843 if (ptr[1] >= 32)
1844 mb_c = (c << 8) + ptr[1];
1845 else
1846 {
1847 if (ptr[1] == NUL)
1848 {
1849 // head byte at end of line
1850 mb_l = 1;
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001851 transchar_nonprint(wp->w_buffer, extra, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001852 }
1853 else
1854 {
1855 // illegal tail byte
1856 mb_l = 2;
1857 STRCPY(extra, "XX");
1858 }
1859 p_extra = extra;
1860 n_extra = (int)STRLEN(extra) - 1;
1861 c_extra = NUL;
1862 c_final = NUL;
1863 c = *p_extra++;
1864 if (area_attr == 0 && search_attr == 0)
1865 {
1866 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001867 extra_attr = hl_combine_attr(
1868 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001869 saved_attr2 = char_attr; // save current attr
1870 }
1871 mb_c = c;
1872 }
1873 }
1874 }
1875 // If a double-width char doesn't fit display a '>' in the
1876 // last column; the character is displayed at the start of the
1877 // next line.
1878 if ((
1879# ifdef FEAT_RIGHTLEFT
1880 wp->w_p_rl ? (col <= 0) :
1881# endif
1882 (col >= wp->w_width - 1))
1883 && (*mb_char2cells)(mb_c) == 2)
1884 {
1885 c = '>';
1886 mb_c = c;
1887 mb_utf8 = FALSE;
1888 mb_l = 1;
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001889 multi_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001890 // Put pointer back so that the character will be
1891 // displayed at the start of the next line.
1892 --ptr;
1893#ifdef FEAT_CONCEAL
1894 did_decrement_ptr = TRUE;
1895#endif
1896 }
1897 else if (*ptr != NUL)
1898 ptr += mb_l - 1;
1899
1900 // If a double-width char doesn't fit at the left side display
1901 // a '<' in the first column. Don't do this for unprintable
1902 // characters.
1903 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
1904 {
1905 n_extra = 1;
1906 c_extra = MB_FILLER_CHAR;
1907 c_final = NUL;
1908 c = ' ';
1909 if (area_attr == 0 && search_attr == 0)
1910 {
1911 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001912 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001913 saved_attr2 = char_attr; // save current attr
1914 }
1915 mb_c = c;
1916 mb_utf8 = FALSE;
1917 mb_l = 1;
1918 }
1919
1920 }
1921 ++ptr;
1922
1923 if (extra_check)
1924 {
1925#ifdef FEAT_SPELL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001926 // Check spelling (unless at the end of the line).
1927 // Only do this when there is no syntax highlighting, the
1928 // @Spell cluster is not used or the current syntax item
1929 // contains the @Spell cluster.
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001930 v = (long)(ptr - line);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001931 if (has_spell && v >= word_end && v > cur_checked_col)
1932 {
1933 spell_attr = 0;
1934 if (c != 0 && (
1935# ifdef FEAT_SYN_HL
1936 !has_syntax ||
1937# endif
1938 can_spell))
1939 {
1940 char_u *prev_ptr, *p;
1941 int len;
1942 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001943
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001944 if (has_mbyte)
1945 {
1946 prev_ptr = ptr - mb_l;
1947 v -= mb_l - 1;
1948 }
1949 else
1950 prev_ptr = ptr - 1;
1951
1952 // Use nextline[] if possible, it has the start of the
1953 // next line concatenated.
1954 if ((prev_ptr - line) - nextlinecol >= 0)
1955 p = nextline + (prev_ptr - line) - nextlinecol;
1956 else
1957 p = prev_ptr;
1958 cap_col -= (int)(prev_ptr - line);
1959 len = spell_check(wp, p, &spell_hlf, &cap_col,
1960 nochange);
1961 word_end = v + len;
1962
1963 // In Insert mode only highlight a word that
1964 // doesn't touch the cursor.
1965 if (spell_hlf != HLF_COUNT
Bram Moolenaar24959102022-05-07 20:01:16 +01001966 && (State & MODE_INSERT)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001967 && wp->w_cursor.lnum == lnum
1968 && wp->w_cursor.col >=
1969 (colnr_T)(prev_ptr - line)
1970 && wp->w_cursor.col < (colnr_T)word_end)
1971 {
1972 spell_hlf = HLF_COUNT;
1973 spell_redraw_lnum = lnum;
1974 }
1975
1976 if (spell_hlf == HLF_COUNT && p != prev_ptr
1977 && (p - nextline) + len > nextline_idx)
1978 {
1979 // Remember that the good word continues at the
1980 // start of the next line.
1981 checked_lnum = lnum + 1;
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001982 checked_col = (int)((p - nextline)
1983 + len - nextline_idx);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001984 }
1985
1986 // Turn index into actual attributes.
1987 if (spell_hlf != HLF_COUNT)
1988 spell_attr = highlight_attr[spell_hlf];
1989
1990 if (cap_col > 0)
1991 {
1992 if (p != prev_ptr
1993 && (p - nextline) + cap_col >= nextline_idx)
1994 {
1995 // Remember that the word in the next line
1996 // must start with a capital.
1997 capcol_lnum = lnum + 1;
1998 cap_col = (int)((p - nextline) + cap_col
1999 - nextline_idx);
2000 }
2001 else
2002 // Compute the actual column.
2003 cap_col += (int)(prev_ptr - line);
2004 }
2005 }
2006 }
2007 if (spell_attr != 0)
2008 {
2009 if (!attr_pri)
2010 char_attr = hl_combine_attr(char_attr, spell_attr);
2011 else
2012 char_attr = hl_combine_attr(spell_attr, char_attr);
2013 }
2014#endif
2015#ifdef FEAT_LINEBREAK
2016 // Found last space before word: check for line break.
2017 if (wp->w_p_lbr && c0 == c
2018 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
2019 {
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002020 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1)
2021 : 0;
2022 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002023
2024 // TODO: is passing p for start of the line OK?
2025 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
2026 NULL) - 1;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002027
2028 // We have just drawn the showbreak value, no need to add
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002029 // space for it again.
Bram Moolenaara06e3452021-05-29 17:56:37 +02002030 if (vcol == vcol_sbr)
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002031 {
Bram Moolenaara06e3452021-05-29 17:56:37 +02002032 n_extra -= MB_CHARLEN(get_showbreak_value(wp));
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02002033 if (n_extra < 0)
2034 n_extra = 0;
2035 }
Bram Moolenaar0bbca542022-01-11 13:14:54 +00002036 if (on_last_col && c != TAB)
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002037 // Do not continue search/match highlighting over the
Bram Moolenaar0bbca542022-01-11 13:14:54 +00002038 // line break, but for TABs the highlighting should
2039 // include the complete width of the character
Bram Moolenaar0c359af2021-11-29 19:18:57 +00002040 search_attr = 0;
Bram Moolenaara06e3452021-05-29 17:56:37 +02002041
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002042 if (c == TAB && n_extra + col > wp->w_width)
2043# ifdef FEAT_VARTABS
2044 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
2045 wp->w_buffer->b_p_vts_array) - 1;
2046# else
2047 n_extra = (int)wp->w_buffer->b_p_ts
2048 - vcol % (int)wp->w_buffer->b_p_ts - 1;
2049# endif
2050
2051 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
2052 c_final = NUL;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002053# if defined(FEAT_PROP_POPUP)
Bram Moolenaar42eba042021-11-30 20:22:49 +00002054 if (n_extra > 0 && c != TAB)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00002055 in_linebreak = TRUE;
Bram Moolenaar3569c0d2021-12-02 11:34:21 +00002056# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002057 if (VIM_ISWHITE(c))
2058 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002059# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002060 if (c == TAB)
2061 // See "Tab alignment" below.
2062 FIX_FOR_BOGUSCOLS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002063# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002064 if (!wp->w_p_list)
2065 c = ' ';
2066 }
2067 }
2068#endif
2069
zeertzjqf14b8ba2021-09-10 16:58:30 +02002070 in_multispace = c == ' '
2071 && ((ptr > line + 1 && ptr[-2] == ' ') || *ptr == ' ');
2072 if (!in_multispace)
2073 multispace_pos = 0;
2074
Bram Moolenaareed9d462021-02-15 20:38:25 +01002075 // 'list': Change char 160 to 'nbsp' and space to 'space'
2076 // setting in 'listchars'. But not when the character is
2077 // followed by a composing character (use mb_l to check that).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002078 if (wp->w_p_list
2079 && ((((c == 160 && mb_l == 1)
2080 || (mb_utf8
2081 && ((mb_c == 160 && mb_l == 2)
2082 || (mb_c == 0x202f && mb_l == 3))))
Bram Moolenaareed9d462021-02-15 20:38:25 +01002083 && wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002084 || (c == ' '
2085 && mb_l == 1
zeertzjqf14b8ba2021-09-10 16:58:30 +02002086 && (wp->w_lcs_chars.space
2087 || (in_multispace
2088 && wp->w_lcs_chars.multispace != NULL))
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002089 && ptr - line >= leadcol
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002090 && ptr - line <= trailcol)))
2091 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02002092 if (in_multispace && wp->w_lcs_chars.multispace != NULL)
2093 {
2094 c = wp->w_lcs_chars.multispace[multispace_pos++];
2095 if (wp->w_lcs_chars.multispace[multispace_pos] == NUL)
2096 multispace_pos = 0;
2097 }
2098 else
2099 c = (c == ' ') ? wp->w_lcs_chars.space
2100 : wp->w_lcs_chars.nbsp;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002101 if (area_attr == 0 && search_attr == 0)
2102 {
2103 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002104 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002105 saved_attr2 = char_attr; // save current attr
2106 }
2107 mb_c = c;
2108 if (enc_utf8 && utf_char2len(c) > 1)
2109 {
2110 mb_utf8 = TRUE;
2111 u8cc[0] = 0;
2112 c = 0xc0;
2113 }
2114 else
2115 mb_utf8 = FALSE;
2116 }
2117
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002118 if ((trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
2119 || (leadcol != 0 && ptr < line + leadcol && c == ' '))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002120 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002121 c = (ptr > line + trailcol) ? wp->w_lcs_chars.trail
2122 : wp->w_lcs_chars.lead;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002123 if (!attr_pri)
2124 {
2125 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002126 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002127 saved_attr2 = char_attr; // save current attr
2128 }
2129 mb_c = c;
2130 if (enc_utf8 && utf_char2len(c) > 1)
2131 {
2132 mb_utf8 = TRUE;
2133 u8cc[0] = 0;
2134 c = 0xc0;
2135 }
2136 else
2137 mb_utf8 = FALSE;
2138 }
2139 }
2140
2141 // Handling of non-printable characters.
2142 if (!vim_isprintc(c))
2143 {
2144 // when getting a character from the file, we may have to
2145 // turn it into something else on the way to putting it
2146 // into "ScreenLines".
Bram Moolenaareed9d462021-02-15 20:38:25 +01002147 if (c == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002148 {
2149 int tab_len = 0;
2150 long vcol_adjusted = vcol; // removed showbreak length
2151#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01002152 char_u *sbr = get_showbreak_value(wp);
2153
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002154 // only adjust the tab_len, when at the first column
2155 // after the showbreak value was drawn
Bram Moolenaaree857022019-11-09 23:26:40 +01002156 if (*sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
2157 vcol_adjusted = vcol - MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002158#endif
2159 // tab amount depends on current column
2160#ifdef FEAT_VARTABS
2161 tab_len = tabstop_padding(vcol_adjusted,
2162 wp->w_buffer->b_p_ts,
2163 wp->w_buffer->b_p_vts_array) - 1;
2164#else
2165 tab_len = (int)wp->w_buffer->b_p_ts
2166 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
2167#endif
2168
2169#ifdef FEAT_LINEBREAK
2170 if (!wp->w_p_lbr || !wp->w_p_list)
2171#endif
2172 // tab amount depends on current column
2173 n_extra = tab_len;
2174#ifdef FEAT_LINEBREAK
2175 else
2176 {
2177 char_u *p;
2178 int len;
2179 int i;
2180 int saved_nextra = n_extra;
2181
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002182# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002183 if (vcol_off > 0)
2184 // there are characters to conceal
2185 tab_len += vcol_off;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002186
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002187 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaareed9d462021-02-15 20:38:25 +01002188 if (wp->w_p_list && wp->w_lcs_chars.tab1
2189 && old_boguscols > 0
2190 && n_extra > tab_len)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002191 tab_len += n_extra - tab_len;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002192# endif
2193 // If n_extra > 0, it gives the number of chars, to
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002194 // use for a tab, else we need to calculate the width
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002195 // for a tab.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002196 len = (tab_len * mb_char2len(wp->w_lcs_chars.tab2));
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002197 if (wp->w_lcs_chars.tab3)
2198 len += mb_char2len(wp->w_lcs_chars.tab3);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002199 if (n_extra > 0)
2200 len += n_extra - tab_len;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002201 c = wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002202 p = alloc(len + 1);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002203 if (p == NULL)
2204 n_extra = 0;
2205 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002206 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002207 vim_memset(p, ' ', len);
2208 p[len] = NUL;
2209 vim_free(p_extra_free);
2210 p_extra_free = p;
2211 for (i = 0; i < tab_len; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002212 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002213 int lcs = wp->w_lcs_chars.tab2;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002214
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002215 if (*p == NUL)
2216 {
2217 tab_len = i;
2218 break;
2219 }
2220
2221 // if tab3 is given, use it for the last char
2222 if (wp->w_lcs_chars.tab3 && i == tab_len - 1)
2223 lcs = wp->w_lcs_chars.tab3;
2224 p += mb_char2bytes(lcs, p);
2225 n_extra += mb_char2len(lcs)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002226 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002227 }
2228 p_extra = p_extra_free;
2229# ifdef FEAT_CONCEAL
2230 // n_extra will be increased by FIX_FOX_BOGUSCOLS
2231 // macro below, so need to adjust for that here
2232 if (vcol_off > 0)
2233 n_extra -= vcol_off;
2234# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002235 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002236 }
2237#endif
2238#ifdef FEAT_CONCEAL
2239 {
2240 int vc_saved = vcol_off;
2241
2242 // Tab alignment should be identical regardless of
2243 // 'conceallevel' value. So tab compensates of all
2244 // previous concealed characters, and thus resets
2245 // vcol_off and boguscols accumulated so far in the
2246 // line. Note that the tab can be longer than
2247 // 'tabstop' when there are concealed characters.
2248 FIX_FOR_BOGUSCOLS;
2249
2250 // Make sure, the highlighting for the tab char will be
2251 // correctly set further below (effectively reverts the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002252 // FIX_FOR_BOGSUCOLS macro).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002253 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaareed9d462021-02-15 20:38:25 +01002254 && wp->w_lcs_chars.tab1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002255 tab_len += vc_saved;
2256 }
2257#endif
2258 mb_utf8 = FALSE; // don't draw as UTF-8
2259 if (wp->w_p_list)
2260 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002261 c = (n_extra == 0 && wp->w_lcs_chars.tab3)
2262 ? wp->w_lcs_chars.tab3
2263 : wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002264#ifdef FEAT_LINEBREAK
2265 if (wp->w_p_lbr)
2266 c_extra = NUL; // using p_extra from above
2267 else
2268#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01002269 c_extra = wp->w_lcs_chars.tab2;
2270 c_final = wp->w_lcs_chars.tab3;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002271 n_attr = tab_len + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002272 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002273 saved_attr2 = char_attr; // save current attr
2274 mb_c = c;
2275 if (enc_utf8 && utf_char2len(c) > 1)
2276 {
2277 mb_utf8 = TRUE;
2278 u8cc[0] = 0;
2279 c = 0xc0;
2280 }
2281 }
2282 else
2283 {
2284 c_final = NUL;
2285 c_extra = ' ';
2286 c = ' ';
2287 }
2288 }
2289 else if (c == NUL
2290 && (wp->w_p_list
2291 || ((fromcol >= 0 || fromcol_prev >= 0)
2292 && tocol > vcol
2293 && VIsual_mode != Ctrl_V
2294 && (
2295# ifdef FEAT_RIGHTLEFT
2296 wp->w_p_rl ? (col >= 0) :
2297# endif
2298 (col < wp->w_width))
2299 && !(noinvcur
2300 && lnum == wp->w_cursor.lnum
2301 && (colnr_T)vcol == wp->w_virtcol)))
2302 && lcs_eol_one > 0)
2303 {
2304 // Display a '$' after the line or highlight an extra
2305 // character if the line break is included.
2306#if defined(FEAT_DIFF) || defined(LINE_ATTR)
2307 // For a diff line the highlighting continues after the
2308 // "$".
2309 if (
2310# ifdef FEAT_DIFF
2311 diff_hlf == (hlf_T)0
2312# ifdef LINE_ATTR
2313 &&
2314# endif
2315# endif
2316# ifdef LINE_ATTR
2317 line_attr == 0
2318# endif
2319 )
2320#endif
2321 {
2322 // In virtualedit, visual selections may extend
2323 // beyond end of line.
2324 if (area_highlighting && virtual_active()
2325 && tocol != MAXCOL && vcol < tocol)
2326 n_extra = 0;
2327 else
2328 {
2329 p_extra = at_end_str;
2330 n_extra = 1;
2331 c_extra = NUL;
2332 c_final = NUL;
2333 }
2334 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002335 if (wp->w_p_list && wp->w_lcs_chars.eol > 0)
2336 c = wp->w_lcs_chars.eol;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002337 else
2338 c = ' ';
2339 lcs_eol_one = -1;
2340 --ptr; // put it back at the NUL
2341 if (!attr_pri)
2342 {
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002343 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002344 n_attr = 1;
2345 }
2346 mb_c = c;
2347 if (enc_utf8 && utf_char2len(c) > 1)
2348 {
2349 mb_utf8 = TRUE;
2350 u8cc[0] = 0;
2351 c = 0xc0;
2352 }
2353 else
2354 mb_utf8 = FALSE; // don't draw as UTF-8
2355 }
2356 else if (c != NUL)
2357 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02002358 p_extra = transchar_buf(wp->w_buffer, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002359 if (n_extra == 0)
2360 n_extra = byte2cells(c) - 1;
2361#ifdef FEAT_RIGHTLEFT
2362 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
2363 rl_mirror(p_extra); // reverse "<12>"
2364#endif
2365 c_extra = NUL;
2366 c_final = NUL;
2367#ifdef FEAT_LINEBREAK
2368 if (wp->w_p_lbr)
2369 {
2370 char_u *p;
2371
2372 c = *p_extra;
2373 p = alloc(n_extra + 1);
2374 vim_memset(p, ' ', n_extra);
2375 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
2376 p[n_extra] = NUL;
2377 vim_free(p_extra_free);
2378 p_extra_free = p_extra = p;
2379 }
2380 else
2381#endif
2382 {
2383 n_extra = byte2cells(c) - 1;
2384 c = *p_extra++;
2385 }
2386 if (!attr_pri)
2387 {
2388 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002389 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002390 saved_attr2 = char_attr; // save current attr
2391 }
2392 mb_utf8 = FALSE; // don't draw as UTF-8
2393 }
2394 else if (VIsual_active
2395 && (VIsual_mode == Ctrl_V
2396 || VIsual_mode == 'v')
2397 && virtual_active()
2398 && tocol != MAXCOL
2399 && vcol < tocol
2400 && (
2401#ifdef FEAT_RIGHTLEFT
2402 wp->w_p_rl ? (col >= 0) :
2403#endif
2404 (col < wp->w_width)))
2405 {
2406 c = ' ';
2407 --ptr; // put it back at the NUL
2408 }
2409#if defined(LINE_ATTR)
2410 else if ((
2411# ifdef FEAT_DIFF
2412 diff_hlf != (hlf_T)0 ||
2413# endif
2414# ifdef FEAT_TERMINAL
2415 win_attr != 0 ||
2416# endif
2417 line_attr != 0
2418 ) && (
2419# ifdef FEAT_RIGHTLEFT
2420 wp->w_p_rl ? (col >= 0) :
2421# endif
2422 (col
2423# ifdef FEAT_CONCEAL
2424 - boguscols
2425# endif
2426 < wp->w_width)))
2427 {
2428 // Highlight until the right side of the window
2429 c = ' ';
2430 --ptr; // put it back at the NUL
2431
2432 // Remember we do the char for line highlighting.
2433 ++did_line_attr;
2434
2435 // don't do search HL for the rest of the line
2436 if (line_attr != 0 && char_attr == search_attr
2437 && (did_line_attr > 1
Bram Moolenaareed9d462021-02-15 20:38:25 +01002438 || (wp->w_p_list &&
2439 wp->w_lcs_chars.eol > 0)))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002440 char_attr = line_attr;
2441# ifdef FEAT_DIFF
2442 if (diff_hlf == HLF_TXD)
2443 {
2444 diff_hlf = HLF_CHD;
2445 if (vi_attr == 0 || char_attr != vi_attr)
2446 {
2447 char_attr = HL_ATTR(diff_hlf);
2448 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2449 && wp->w_p_culopt_flags != CULOPT_NBR
2450 && (!cul_screenline
2451 || (vcol >= left_curline_col
2452 && vcol <= right_curline_col)))
2453 char_attr = hl_combine_attr(
2454 char_attr, HL_ATTR(HLF_CUL));
2455 }
2456 }
2457# endif
2458# ifdef FEAT_TERMINAL
2459 if (win_attr != 0)
2460 {
2461 char_attr = win_attr;
Bram Moolenaara3817732019-10-16 16:31:44 +02002462 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2463 && wp->w_p_culopt_flags != CULOPT_NBR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002464 {
2465 if (!cul_screenline || (vcol >= left_curline_col
2466 && vcol <= right_curline_col))
2467 char_attr = hl_combine_attr(
2468 char_attr, HL_ATTR(HLF_CUL));
2469 }
2470 else if (line_attr)
2471 char_attr = hl_combine_attr(char_attr, line_attr);
2472 }
2473# endif
2474 }
2475#endif
2476 }
2477
2478#ifdef FEAT_CONCEAL
2479 if ( wp->w_p_cole > 0
2480 && (wp != curwin || lnum != wp->w_cursor.lnum ||
2481 conceal_cursor_line(wp))
2482 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
2483 && !(lnum_in_visual_area
2484 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
2485 {
2486 char_attr = conceal_attr;
2487 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002488 && (syn_get_sub_char() != NUL
2489 || (has_match_conc && match_conc)
2490 || wp->w_p_cole == 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002491 && wp->w_p_cole != 3)
2492 {
2493 // First time at this concealed item: display one
2494 // character.
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002495 if (has_match_conc && match_conc)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002496 c = match_conc;
2497 else if (syn_get_sub_char() != NUL)
2498 c = syn_get_sub_char();
Bram Moolenaareed9d462021-02-15 20:38:25 +01002499 else if (wp->w_lcs_chars.conceal != NUL)
2500 c = wp->w_lcs_chars.conceal;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002501 else
2502 c = ' ';
2503
2504 prev_syntax_id = syntax_seqnr;
2505
2506 if (n_extra > 0)
2507 vcol_off += n_extra;
2508 vcol += n_extra;
2509 if (wp->w_p_wrap && n_extra > 0)
2510 {
2511# ifdef FEAT_RIGHTLEFT
2512 if (wp->w_p_rl)
2513 {
2514 col -= n_extra;
2515 boguscols -= n_extra;
2516 }
2517 else
2518# endif
2519 {
2520 boguscols += n_extra;
2521 col += n_extra;
2522 }
2523 }
2524 n_extra = 0;
2525 n_attr = 0;
2526 }
2527 else if (n_skip == 0)
2528 {
2529 is_concealing = TRUE;
2530 n_skip = 1;
2531 }
2532 mb_c = c;
2533 if (enc_utf8 && utf_char2len(c) > 1)
2534 {
2535 mb_utf8 = TRUE;
2536 u8cc[0] = 0;
2537 c = 0xc0;
2538 }
2539 else
2540 mb_utf8 = FALSE; // don't draw as UTF-8
2541 }
2542 else
2543 {
2544 prev_syntax_id = 0;
2545 is_concealing = FALSE;
2546 }
2547
2548 if (n_skip > 0 && did_decrement_ptr)
2549 // not showing the '>', put pointer back to avoid getting stuck
2550 ++ptr;
2551
2552#endif // FEAT_CONCEAL
2553 }
2554
2555#ifdef FEAT_CONCEAL
2556 // In the cursor line and we may be concealing characters: correct
2557 // the cursor column when we reach its position.
2558 if (!did_wcol && draw_state == WL_LINE
2559 && wp == curwin && lnum == wp->w_cursor.lnum
2560 && conceal_cursor_line(wp)
2561 && (int)wp->w_virtcol <= vcol + n_skip)
2562 {
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002563# ifdef FEAT_RIGHTLEFT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002564 if (wp->w_p_rl)
2565 wp->w_wcol = wp->w_width - col + boguscols - 1;
2566 else
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002567# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002568 wp->w_wcol = col - boguscols;
2569 wp->w_wrow = row;
2570 did_wcol = TRUE;
2571 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002572# ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +01002573 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002574# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002575 }
2576#endif
2577
2578 // Don't override visual selection highlighting.
2579 if (n_attr > 0
2580 && draw_state == WL_LINE
2581 && !attr_pri)
2582 {
2583#ifdef LINE_ATTR
2584 if (line_attr)
2585 char_attr = hl_combine_attr(extra_attr, line_attr);
2586 else
2587#endif
2588 char_attr = extra_attr;
2589 }
2590
2591#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2592 // XIM don't send preedit_start and preedit_end, but they send
2593 // preedit_changed and commit. Thus Vim can't set "im_is_active", use
2594 // im_is_preediting() here.
2595 if (p_imst == IM_ON_THE_SPOT
2596 && xic != NULL
2597 && lnum == wp->w_cursor.lnum
Bram Moolenaar24959102022-05-07 20:01:16 +01002598 && (State & MODE_INSERT)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002599 && !p_imdisable
2600 && im_is_preediting()
2601 && draw_state == WL_LINE)
2602 {
2603 colnr_T tcol;
2604
2605 if (preedit_end_col == MAXCOL)
2606 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
2607 else
2608 tcol = preedit_end_col;
2609 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
2610 {
2611 if (feedback_old_attr < 0)
2612 {
2613 feedback_col = 0;
2614 feedback_old_attr = char_attr;
2615 }
2616 char_attr = im_get_feedback_attr(feedback_col);
2617 if (char_attr < 0)
2618 char_attr = feedback_old_attr;
2619 feedback_col++;
2620 }
2621 else if (feedback_old_attr >= 0)
2622 {
2623 char_attr = feedback_old_attr;
2624 feedback_old_attr = -1;
2625 feedback_col = 0;
2626 }
2627 }
2628#endif
2629 // Handle the case where we are in column 0 but not on the first
2630 // character of the line and the user wants us to show us a
2631 // special character (via 'listchars' option "precedes:<char>".
2632 if (lcs_prec_todo != NUL
2633 && wp->w_p_list
Bram Moolenaarbffba7f2019-09-20 17:00:17 +02002634 && (wp->w_p_wrap ?
2635 (wp->w_skipcol > 0 && row == 0) :
2636 wp->w_leftcol > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002637#ifdef FEAT_DIFF
2638 && filler_todo <= 0
2639#endif
2640 && draw_state > WL_NR
2641 && c != NUL)
2642 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002643 c = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002644 lcs_prec_todo = NUL;
2645 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2646 {
2647 // Double-width character being overwritten by the "precedes"
2648 // character, need to fill up half the character.
2649 c_extra = MB_FILLER_CHAR;
2650 c_final = NUL;
2651 n_extra = 1;
2652 n_attr = 2;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002653 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002654 }
2655 mb_c = c;
2656 if (enc_utf8 && utf_char2len(c) > 1)
2657 {
2658 mb_utf8 = TRUE;
2659 u8cc[0] = 0;
2660 c = 0xc0;
2661 }
2662 else
2663 mb_utf8 = FALSE; // don't draw as UTF-8
2664 if (!attr_pri)
2665 {
2666 saved_attr3 = char_attr; // save current attr
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002667 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002668 n_attr3 = 1;
2669 }
2670 }
2671
2672 // At end of the text line or just after the last character.
2673 if ((c == NUL
2674#if defined(LINE_ATTR)
2675 || did_line_attr == 1
2676#endif
2677 ) && eol_hl_off == 0)
2678 {
2679#ifdef FEAT_SEARCH_EXTRA
2680 // flag to indicate whether prevcol equals startcol of search_hl or
2681 // one of the matches
2682 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &screen_search_hl,
2683 (long)(ptr - line) - (c == NUL));
2684#endif
2685 // Invert at least one char, used for Visual and empty line or
2686 // highlight match at end of line. If it's beyond the last
2687 // char on the screen, just overwrite that one (tricky!) Not
2688 // needed when a '$' was displayed for 'list'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002689 if (wp->w_lcs_chars.eol == lcs_eol_one
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002690 && ((area_attr != 0 && vcol == fromcol
2691 && (VIsual_mode != Ctrl_V
2692 || lnum == VIsual.lnum
2693 || lnum == curwin->w_cursor.lnum)
2694 && c == NUL)
2695#ifdef FEAT_SEARCH_EXTRA
2696 // highlight 'hlsearch' match at end of line
2697 || (prevcol_hl_flag
2698# ifdef FEAT_SYN_HL
2699 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
2700 && !(wp == curwin && VIsual_active))
2701# endif
2702# ifdef FEAT_DIFF
2703 && diff_hlf == (hlf_T)0
2704# endif
2705# if defined(LINE_ATTR)
2706 && did_line_attr <= 1
2707# endif
2708 )
2709#endif
2710 ))
2711 {
2712 int n = 0;
2713
2714#ifdef FEAT_RIGHTLEFT
2715 if (wp->w_p_rl)
2716 {
2717 if (col < 0)
2718 n = 1;
2719 }
2720 else
2721#endif
2722 {
2723 if (col >= wp->w_width)
2724 n = -1;
2725 }
2726 if (n != 0)
2727 {
2728 // At the window boundary, highlight the last character
2729 // instead (better than nothing).
2730 off += n;
2731 col += n;
2732 }
2733 else
2734 {
2735 // Add a blank character to highlight.
2736 ScreenLines[off] = ' ';
2737 if (enc_utf8)
2738 ScreenLinesUC[off] = 0;
2739 }
2740#ifdef FEAT_SEARCH_EXTRA
2741 if (area_attr == 0)
2742 {
2743 // Use attributes from match with highest priority among
2744 // 'search_hl' and the match list.
2745 get_search_match_hl(wp, &screen_search_hl,
2746 (long)(ptr - line), &char_attr);
2747 }
2748#endif
2749 ScreenAttrs[off] = char_attr;
2750#ifdef FEAT_RIGHTLEFT
2751 if (wp->w_p_rl)
2752 {
2753 --col;
2754 --off;
2755 }
2756 else
2757#endif
2758 {
2759 ++col;
2760 ++off;
2761 }
2762 ++vcol;
2763 eol_hl_off = 1;
2764 }
2765 }
2766
2767 // At end of the text line.
2768 if (c == NUL)
2769 {
2770#ifdef FEAT_SYN_HL
2771 // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
2772 if (wp->w_p_wrap)
2773 v = wp->w_skipcol;
2774 else
2775 v = wp->w_leftcol;
2776
2777 // check if line ends before left margin
2778 if (vcol < v + col - win_col_off(wp))
2779 vcol = v + col - win_col_off(wp);
2780#ifdef FEAT_CONCEAL
2781 // Get rid of the boguscols now, we want to draw until the right
2782 // edge for 'cursorcolumn'.
2783 col -= boguscols;
2784 boguscols = 0;
2785#endif
2786
2787 if (draw_color_col)
2788 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2789
2790 if (((wp->w_p_cuc
2791 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
2792 && (int)wp->w_virtcol <
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002793 (long)wp->w_width * (row - startrow + 1) + v
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002794 && lnum != wp->w_cursor.lnum)
2795 || draw_color_col
2796 || win_attr != 0)
2797# ifdef FEAT_RIGHTLEFT
2798 && !wp->w_p_rl
2799# endif
2800 )
2801 {
2802 int rightmost_vcol = 0;
2803 int i;
2804
2805 if (wp->w_p_cuc)
2806 rightmost_vcol = wp->w_virtcol;
2807 if (draw_color_col)
2808 // determine rightmost colorcolumn to possibly draw
2809 for (i = 0; color_cols[i] >= 0; ++i)
2810 if (rightmost_vcol < color_cols[i])
2811 rightmost_vcol = color_cols[i];
2812
2813 while (col < wp->w_width)
2814 {
2815 ScreenLines[off] = ' ';
2816 if (enc_utf8)
2817 ScreenLinesUC[off] = 0;
2818 ++col;
2819 if (draw_color_col)
2820 draw_color_col = advance_color_col(VCOL_HLC,
2821 &color_cols);
2822
2823 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
2824 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
2825 else if (draw_color_col && VCOL_HLC == *color_cols)
2826 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
2827 else
2828 ScreenAttrs[off++] = win_attr;
2829
2830 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
2831 break;
2832
2833 ++vcol;
2834 }
2835 }
2836#endif
2837
2838 screen_line(screen_row, wp->w_wincol, col,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002839 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002840 row++;
2841
2842 // Update w_cline_height and w_cline_folded if the cursor line was
2843 // updated (saves a call to plines() later).
2844 if (wp == curwin && lnum == curwin->w_cursor.lnum)
2845 {
2846 curwin->w_cline_row = startrow;
2847 curwin->w_cline_height = row - startrow;
2848#ifdef FEAT_FOLDING
2849 curwin->w_cline_folded = FALSE;
2850#endif
2851 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2852 }
2853
2854 break;
2855 }
2856
2857 // Show "extends" character from 'listchars' if beyond the line end and
2858 // 'list' is set.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002859 if (wp->w_lcs_chars.ext != NUL
Bram Moolenaar41fb7232021-07-08 12:40:05 +02002860 && draw_state == WL_LINE
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002861 && wp->w_p_list
2862 && !wp->w_p_wrap
2863#ifdef FEAT_DIFF
2864 && filler_todo <= 0
2865#endif
2866 && (
2867#ifdef FEAT_RIGHTLEFT
2868 wp->w_p_rl ? col == 0 :
2869#endif
2870 col == wp->w_width - 1)
2871 && (*ptr != NUL
2872 || (wp->w_p_list && lcs_eol_one > 0)
2873 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
2874 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002875 c = wp->w_lcs_chars.ext;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002876 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002877 mb_c = c;
2878 if (enc_utf8 && utf_char2len(c) > 1)
2879 {
2880 mb_utf8 = TRUE;
2881 u8cc[0] = 0;
2882 c = 0xc0;
2883 }
2884 else
2885 mb_utf8 = FALSE;
2886 }
2887
2888#ifdef FEAT_SYN_HL
2889 // advance to the next 'colorcolumn'
2890 if (draw_color_col)
2891 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2892
2893 // Highlight the cursor column if 'cursorcolumn' is set. But don't
2894 // highlight the cursor position itself.
2895 // Also highlight the 'colorcolumn' if it is different than
2896 // 'cursorcolumn'
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002897 // Also highlight the 'colorcolumn' if 'breakindent' and/or 'showbreak'
2898 // options are set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002899 vcol_save_attr = -1;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002900 if (((draw_state == WL_LINE ||
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002901 draw_state == WL_BRI ||
2902 draw_state == WL_SBR) && !lnum_in_visual_area
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002903 && search_attr == 0 && area_attr == 0)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002904# ifdef FEAT_DIFF
2905 && filler_todo <= 0
2906# endif
2907 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002908 {
2909 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
2910 && lnum != wp->w_cursor.lnum)
2911 {
2912 vcol_save_attr = char_attr;
2913 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
2914 }
2915 else if (draw_color_col && VCOL_HLC == *color_cols)
2916 {
2917 vcol_save_attr = char_attr;
2918 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
2919 }
2920 }
2921#endif
2922
2923 // Store character to be displayed.
2924 // Skip characters that are left of the screen for 'nowrap'.
2925 vcol_prev = vcol;
2926 if (draw_state < WL_LINE || n_skip <= 0)
2927 {
2928 // Store the character.
2929#if defined(FEAT_RIGHTLEFT)
2930 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
2931 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002932 // A double-wide character is: put first half in left cell.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002933 --off;
2934 --col;
2935 }
2936#endif
2937 ScreenLines[off] = c;
2938 if (enc_dbcs == DBCS_JPNU)
2939 {
2940 if ((mb_c & 0xff00) == 0x8e00)
2941 ScreenLines[off] = 0x8e;
2942 ScreenLines2[off] = mb_c & 0xff;
2943 }
2944 else if (enc_utf8)
2945 {
2946 if (mb_utf8)
2947 {
2948 int i;
2949
2950 ScreenLinesUC[off] = mb_c;
2951 if ((c & 0xff) == 0)
2952 ScreenLines[off] = 0x80; // avoid storing zero
2953 for (i = 0; i < Screen_mco; ++i)
2954 {
2955 ScreenLinesC[i][off] = u8cc[i];
2956 if (u8cc[i] == 0)
2957 break;
2958 }
2959 }
2960 else
2961 ScreenLinesUC[off] = 0;
2962 }
2963 if (multi_attr)
2964 {
2965 ScreenAttrs[off] = multi_attr;
2966 multi_attr = 0;
2967 }
2968 else
2969 ScreenAttrs[off] = char_attr;
2970
2971 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2972 {
2973 // Need to fill two screen columns.
2974 ++off;
2975 ++col;
2976 if (enc_utf8)
2977 // UTF-8: Put a 0 in the second screen char.
2978 ScreenLines[off] = 0;
2979 else
2980 // DBCS: Put second byte in the second screen char.
2981 ScreenLines[off] = mb_c & 0xff;
2982 if (draw_state > WL_NR
2983#ifdef FEAT_DIFF
2984 && filler_todo <= 0
2985#endif
2986 )
2987 ++vcol;
2988 // When "tocol" is halfway a character, set it to the end of
2989 // the character, otherwise highlighting won't stop.
2990 if (tocol == vcol)
2991 ++tocol;
2992#ifdef FEAT_RIGHTLEFT
2993 if (wp->w_p_rl)
2994 {
2995 // now it's time to backup one cell
2996 --off;
2997 --col;
2998 }
2999#endif
3000 }
3001#ifdef FEAT_RIGHTLEFT
3002 if (wp->w_p_rl)
3003 {
3004 --off;
3005 --col;
3006 }
3007 else
3008#endif
3009 {
3010 ++off;
3011 ++col;
3012 }
3013 }
3014#ifdef FEAT_CONCEAL
3015 else if (wp->w_p_cole > 0 && is_concealing)
3016 {
3017 --n_skip;
3018 ++vcol_off;
3019 if (n_extra > 0)
3020 vcol_off += n_extra;
3021 if (wp->w_p_wrap)
3022 {
3023 // Special voodoo required if 'wrap' is on.
3024 //
3025 // Advance the column indicator to force the line
3026 // drawing to wrap early. This will make the line
3027 // take up the same screen space when parts are concealed,
3028 // so that cursor line computations aren't messed up.
3029 //
3030 // To avoid the fictitious advance of 'col' causing
3031 // trailing junk to be written out of the screen line
3032 // we are building, 'boguscols' keeps track of the number
3033 // of bad columns we have advanced.
3034 if (n_extra > 0)
3035 {
3036 vcol += n_extra;
3037# ifdef FEAT_RIGHTLEFT
3038 if (wp->w_p_rl)
3039 {
3040 col -= n_extra;
3041 boguscols -= n_extra;
3042 }
3043 else
3044# endif
3045 {
3046 col += n_extra;
3047 boguscols += n_extra;
3048 }
3049 n_extra = 0;
3050 n_attr = 0;
3051 }
3052
3053
3054 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
3055 {
3056 // Need to fill two screen columns.
3057# ifdef FEAT_RIGHTLEFT
3058 if (wp->w_p_rl)
3059 {
3060 --boguscols;
3061 --col;
3062 }
3063 else
3064# endif
3065 {
3066 ++boguscols;
3067 ++col;
3068 }
3069 }
3070
3071# ifdef FEAT_RIGHTLEFT
3072 if (wp->w_p_rl)
3073 {
3074 --boguscols;
3075 --col;
3076 }
3077 else
3078# endif
3079 {
3080 ++boguscols;
3081 ++col;
3082 }
3083 }
3084 else
3085 {
3086 if (n_extra > 0)
3087 {
3088 vcol += n_extra;
3089 n_extra = 0;
3090 n_attr = 0;
3091 }
3092 }
3093
3094 }
3095#endif // FEAT_CONCEAL
3096 else
3097 --n_skip;
3098
3099 // Only advance the "vcol" when after the 'number' or 'relativenumber'
3100 // column.
3101 if (draw_state > WL_NR
3102#ifdef FEAT_DIFF
3103 && filler_todo <= 0
3104#endif
3105 )
3106 ++vcol;
3107
3108#ifdef FEAT_SYN_HL
3109 if (vcol_save_attr >= 0)
3110 char_attr = vcol_save_attr;
3111#endif
3112
3113 // restore attributes after "predeces" in 'listchars'
3114 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
3115 char_attr = saved_attr3;
3116
3117 // restore attributes after last 'listchars' or 'number' char
3118 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
3119 char_attr = saved_attr2;
3120
3121 // At end of screen line and there is more to come: Display the line
3122 // so far. If there is no more to display it is caught above.
3123 if ((
3124#ifdef FEAT_RIGHTLEFT
3125 wp->w_p_rl ? (col < 0) :
3126#endif
3127 (col >= wp->w_width))
Bram Moolenaar41fb7232021-07-08 12:40:05 +02003128 && (draw_state != WL_LINE
3129 || *ptr != NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003130#ifdef FEAT_DIFF
3131 || filler_todo > 0
3132#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01003133 || (wp->w_p_list && wp->w_lcs_chars.eol != NUL
3134 && p_extra != at_end_str)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003135 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
3136 )
3137 {
3138#ifdef FEAT_CONCEAL
3139 screen_line(screen_row, wp->w_wincol, col - boguscols,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003140 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003141 boguscols = 0;
3142#else
3143 screen_line(screen_row, wp->w_wincol, col,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003144 wp->w_width, screen_line_flags);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003145#endif
3146 ++row;
3147 ++screen_row;
3148
3149 // When not wrapping and finished diff lines, or when displayed
3150 // '$' and highlighting until last column, break here.
3151 if ((!wp->w_p_wrap
3152#ifdef FEAT_DIFF
3153 && filler_todo <= 0
3154#endif
3155 ) || lcs_eol_one == -1)
3156 break;
3157
3158 // When the window is too narrow draw all "@" lines.
3159 if (draw_state != WL_LINE
3160#ifdef FEAT_DIFF
3161 && filler_todo <= 0
3162#endif
3163 )
3164 {
3165 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
3166 draw_vsep_win(wp, row);
3167 row = endrow;
3168 }
3169
3170 // When line got too long for screen break here.
3171 if (row == endrow)
3172 {
3173 ++row;
3174 break;
3175 }
3176
3177 if (screen_cur_row == screen_row - 1
3178#ifdef FEAT_DIFF
3179 && filler_todo <= 0
3180#endif
3181 && wp->w_width == Columns)
3182 {
3183 // Remember that the line wraps, used for modeless copy.
3184 LineWraps[screen_row - 1] = TRUE;
3185
3186 // Special trick to make copy/paste of wrapped lines work with
3187 // xterm/screen: write an extra character beyond the end of
3188 // the line. This will work with all terminal types
3189 // (regardless of the xn,am settings).
3190 // Only do this on a fast tty.
3191 // Only do this if the cursor is on the current line
3192 // (something has been written in it).
3193 // Don't do this for the GUI.
3194 // Don't do this for double-width characters.
3195 // Don't do this for a window not at the right screen border.
3196 if (p_tf
3197#ifdef FEAT_GUI
3198 && !gui.in_use
3199#endif
3200 && !(has_mbyte
3201 && ((*mb_off2cells)(LineOffset[screen_row],
3202 LineOffset[screen_row] + screen_Columns)
3203 == 2
3204 || (*mb_off2cells)(LineOffset[screen_row - 1]
3205 + (int)Columns - 2,
3206 LineOffset[screen_row] + screen_Columns)
3207 == 2)))
3208 {
3209 // First make sure we are at the end of the screen line,
3210 // then output the same character again to let the
3211 // terminal know about the wrap. If the terminal doesn't
3212 // auto-wrap, we overwrite the character.
3213 if (screen_cur_col != wp->w_width)
3214 screen_char(LineOffset[screen_row - 1]
3215 + (unsigned)Columns - 1,
3216 screen_row - 1, (int)(Columns - 1));
3217
3218 // When there is a multi-byte character, just output a
3219 // space to keep it simple.
3220 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
3221 screen_row - 1] + (Columns - 1)]) > 1)
3222 out_char(' ');
3223 else
3224 out_char(ScreenLines[LineOffset[screen_row - 1]
3225 + (Columns - 1)]);
3226 // force a redraw of the first char on the next line
3227 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
3228 screen_start(); // don't know where cursor is now
3229 }
3230 }
3231
3232 col = 0;
3233 off = (unsigned)(current_ScreenLine - ScreenLines);
3234#ifdef FEAT_RIGHTLEFT
3235 if (wp->w_p_rl)
3236 {
3237 col = wp->w_width - 1; // col is not used if breaking!
3238 off += col;
3239 }
3240#endif
3241
3242 // reset the drawing state for the start of a wrapped line
3243 draw_state = WL_START;
3244 saved_n_extra = n_extra;
3245 saved_p_extra = p_extra;
3246 saved_c_extra = c_extra;
3247 saved_c_final = c_final;
3248#ifdef FEAT_SYN_HL
3249 if (!(cul_screenline
3250# ifdef FEAT_DIFF
Bram Moolenaare7705982020-04-21 22:23:15 +02003251 && diff_hlf == (hlf_T)0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003252# endif
Bram Moolenaare7705982020-04-21 22:23:15 +02003253 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003254 saved_char_attr = char_attr;
3255 else
3256#endif
3257 saved_char_attr = 0;
3258 n_extra = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01003259 lcs_prec_todo = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003260#ifdef FEAT_LINEBREAK
3261# ifdef FEAT_DIFF
3262 if (filler_todo <= 0)
3263# endif
3264 need_showbreak = TRUE;
3265#endif
3266#ifdef FEAT_DIFF
3267 --filler_todo;
3268 // When the filler lines are actually below the last line of the
3269 // file, don't draw the line itself, break here.
3270 if (filler_todo == 0 && wp->w_botfill)
3271 break;
3272#endif
3273 }
3274
3275 } // for every character in the line
3276
3277#ifdef FEAT_SPELL
3278 // After an empty line check first word for capital.
3279 if (*skipwhite(line) == NUL)
3280 {
3281 capcol_lnum = lnum + 1;
3282 cap_col = 0;
3283 }
3284#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003285#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003286 vim_free(text_props);
3287 vim_free(text_prop_idxs);
3288#endif
3289
3290 vim_free(p_extra_free);
3291 return row;
3292}