blob: 2ad83f4b5e089c3ef895de46f353736d3e624740 [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/*
80 * Get information needed to display the sign in line 'lnum' in window 'wp'.
81 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
82 * Otherwise the sign is going to be displayed in the sign column.
83 */
84 static void
85get_sign_display_info(
86 int nrcol,
87 win_T *wp,
88 linenr_T lnum UNUSED,
89 sign_attrs_T *sattr,
90 int wcr_attr,
91 int row,
92 int startrow,
93 int filler_lines UNUSED,
94 int filler_todo UNUSED,
95 int *c_extrap,
96 int *c_finalp,
97 char_u *extra,
98 char_u **pp_extra,
99 int *n_extrap,
100 int *char_attrp)
101{
102 int text_sign;
103# ifdef FEAT_SIGN_ICONS
104 int icon_sign;
105# endif
106
107 // Draw two cells with the sign value or blank.
108 *c_extrap = ' ';
109 *c_finalp = NUL;
110 if (nrcol)
111 *n_extrap = number_width(wp) + 1;
112 else
113 {
114 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
115 *n_extrap = 2;
116 }
117
118 if (row == startrow
119#ifdef FEAT_DIFF
120 + filler_lines && filler_todo <= 0
121#endif
122 )
123 {
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200124 text_sign = (sattr->sat_text != NULL) ? sattr->sat_typenr : 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200125# ifdef FEAT_SIGN_ICONS
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200126 icon_sign = (sattr->sat_icon != NULL) ? sattr->sat_typenr : 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200127 if (gui.in_use && icon_sign != 0)
128 {
129 // Use the image in this position.
130 if (nrcol)
131 {
132 *c_extrap = NUL;
133 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
134 *pp_extra = extra;
135 *n_extrap = (int)STRLEN(*pp_extra);
136 }
137 else
138 *c_extrap = SIGN_BYTE;
139# ifdef FEAT_NETBEANS_INTG
140 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
141 {
142 if (nrcol)
143 {
144 *c_extrap = NUL;
145 sprintf((char *)extra, "%-*c ", number_width(wp),
146 MULTISIGN_BYTE);
147 *pp_extra = extra;
148 *n_extrap = (int)STRLEN(*pp_extra);
149 }
150 else
151 *c_extrap = MULTISIGN_BYTE;
152 }
153# endif
154 *c_finalp = NUL;
155 *char_attrp = icon_sign;
156 }
157 else
158# endif
159 if (text_sign != 0)
160 {
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200161 *pp_extra = sattr->sat_text;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200162 if (*pp_extra != NULL)
163 {
164 if (nrcol)
165 {
166 int n, width = number_width(wp) - 2;
167
168 for (n = 0; n < width; n++)
169 extra[n] = ' ';
170 extra[n] = 0;
171 STRCAT(extra, *pp_extra);
172 STRCAT(extra, " ");
173 *pp_extra = extra;
174 }
175 *c_extrap = NUL;
176 *c_finalp = NUL;
177 *n_extrap = (int)STRLEN(*pp_extra);
178 }
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200179 *char_attrp = sattr->sat_texthl;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200180 }
181 }
182}
183#endif
184
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100185#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200186static textprop_T *current_text_props = NULL;
187static buf_T *current_buf = NULL;
188
189 static int
190text_prop_compare(const void *s1, const void *s2)
191{
192 int idx1, idx2;
193 proptype_T *pt1, *pt2;
194 colnr_T col1, col2;
195
196 idx1 = *(int *)s1;
197 idx2 = *(int *)s2;
198 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
199 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
200 if (pt1 == pt2)
201 return 0;
202 if (pt1 == NULL)
203 return -1;
204 if (pt2 == NULL)
205 return 1;
206 if (pt1->pt_priority != pt2->pt_priority)
207 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
208 col1 = current_text_props[idx1].tp_col;
209 col2 = current_text_props[idx2].tp_col;
210 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
211}
212#endif
213
214/*
215 * Display line "lnum" of window 'wp' on the screen.
216 * Start at row "startrow", stop when "endrow" is reached.
217 * wp->w_virtcol needs to be valid.
218 *
219 * Return the number of last row the line occupies.
220 */
221 int
222win_line(
223 win_T *wp,
224 linenr_T lnum,
225 int startrow,
226 int endrow,
227 int nochange UNUSED, // not updating for changed text
228 int number_only) // only update the number column
229{
230 int col = 0; // visual column on screen
231 unsigned off; // offset in ScreenLines/ScreenAttrs
232 int c = 0; // init for GCC
233 long vcol = 0; // virtual column (for tabs)
234#ifdef FEAT_LINEBREAK
235 long vcol_sbr = -1; // virtual column after showbreak
236#endif
237 long vcol_prev = -1; // "vcol" of previous character
238 char_u *line; // current line
239 char_u *ptr; // current position in "line"
240 int row; // row in the window, excl w_winrow
241 int screen_row; // row on the screen, incl w_winrow
242
243 char_u extra[21]; // "%ld " and 'fdc' must fit in here
244 int n_extra = 0; // number of extra chars
245 char_u *p_extra = NULL; // string of extra chars, plus NUL
246 char_u *p_extra_free = NULL; // p_extra needs to be freed
247 int c_extra = NUL; // extra chars, all the same
248 int c_final = NUL; // final char, mandatory if set
249 int extra_attr = 0; // attributes when n_extra != 0
250 static char_u *at_end_str = (char_u *)""; // used for p_extra when
Bram Moolenaareed9d462021-02-15 20:38:25 +0100251 // displaying eol at end-of-line
252 int lcs_eol_one = wp->w_lcs_chars.eol; // eol until it's been used
253 int lcs_prec_todo = wp->w_lcs_chars.prec; // prec until it's been used
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200254
255 // saved "extra" items for when draw_state becomes WL_LINE (again)
256 int saved_n_extra = 0;
257 char_u *saved_p_extra = NULL;
258 int saved_c_extra = 0;
259 int saved_c_final = 0;
260 int saved_char_attr = 0;
261
262 int n_attr = 0; // chars with special attr
263 int saved_attr2 = 0; // char_attr saved for n_attr
264 int n_attr3 = 0; // chars with overruling special attr
265 int saved_attr3 = 0; // char_attr saved for n_attr3
266
267 int n_skip = 0; // nr of chars to skip for 'nowrap'
268
269 int fromcol = -10; // start of inverting
270 int tocol = MAXCOL; // end of inverting
271 int fromcol_prev = -2; // start of inverting after cursor
272 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200273 int lnum_in_visual_area = FALSE;
274 pos_T pos;
275 long v;
276
277 int char_attr = 0; // attributes for next character
278 int attr_pri = FALSE; // char_attr has priority
279 int area_highlighting = FALSE; // Visual or incsearch highlighting
280 // in this line
281 int vi_attr = 0; // attributes for Visual and incsearch
282 // highlighting
283 int wcr_attr = 0; // attributes from 'wincolor'
284 int win_attr = 0; // background for whole window, except
285 // margins and "~" lines.
286 int area_attr = 0; // attributes desired by highlighting
287 int search_attr = 0; // attributes desired by 'hlsearch'
288#ifdef FEAT_SYN_HL
289 int vcol_save_attr = 0; // saved attr for 'cursorcolumn'
290 int syntax_attr = 0; // attributes desired by syntax
Bram Moolenaar9115c612019-10-16 16:57:06 +0200291 int prev_syntax_col = -1; // column of prev_syntax_attr
292 int prev_syntax_attr = 0; // syntax_attr at prev_syntax_col
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200293 int has_syntax = FALSE; // this buffer has syntax highl.
294 int save_did_emsg;
295 int draw_color_col = FALSE; // highlight colorcolumn
296 int *color_cols = NULL; // pointer to according columns array
297#endif
298 int eol_hl_off = 0; // 1 if highlighted char after EOL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100299#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200300 int text_prop_count;
301 int text_prop_next = 0; // next text property to use
302 textprop_T *text_props = NULL;
303 int *text_prop_idxs = NULL;
304 int text_props_active = 0;
305 proptype_T *text_prop_type = NULL;
306 int text_prop_attr = 0;
307 int text_prop_combine = FALSE;
308#endif
309#ifdef FEAT_SPELL
310 int has_spell = FALSE; // this buffer has spell checking
Bram Moolenaar34390282019-10-16 14:38:26 +0200311 int can_spell;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200312# define SPWORDLEN 150
313 char_u nextline[SPWORDLEN * 2];// text with start of the next line
314 int nextlinecol = 0; // column where nextline[] starts
315 int nextline_idx = 0; // index in nextline[] where next line
316 // starts
317 int spell_attr = 0; // attributes desired by spelling
318 int word_end = 0; // last byte with same spell_attr
319 static linenr_T checked_lnum = 0; // line number for "checked_col"
320 static int checked_col = 0; // column in "checked_lnum" up to which
321 // there are no spell errors
322 static int cap_col = -1; // column to check for Cap word
323 static linenr_T capcol_lnum = 0; // line number where "cap_col" used
324 int cur_checked_col = 0; // checked column for current line
325#endif
326 int extra_check = 0; // has extra highlighting
327 int multi_attr = 0; // attributes desired by multibyte
328 int mb_l = 1; // multi-byte byte length
329 int mb_c = 0; // decoded multi-byte character
330 int mb_utf8 = FALSE; // screen char is UTF-8 char
331 int u8cc[MAX_MCO]; // composing UTF-8 chars
332#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
333 int filler_lines = 0; // nr of filler lines to be drawn
334 int filler_todo = 0; // nr of filler lines still to do + 1
335#endif
336#ifdef FEAT_DIFF
337 hlf_T diff_hlf = (hlf_T)0; // type of diff highlighting
338 int change_start = MAXCOL; // first col of changed area
339 int change_end = -1; // last col of changed area
340#endif
341 colnr_T trailcol = MAXCOL; // start of trailing spaces
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100342 colnr_T leadcol = 0; // start of leading spaces
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200343#ifdef FEAT_LINEBREAK
344 int need_showbreak = FALSE; // overlong line, skipping first x
345 // chars
346#endif
347#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
348 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
349# define LINE_ATTR
350 int line_attr = 0; // attribute for the whole line
351 int line_attr_save;
352#endif
353#ifdef FEAT_SIGNS
354 int sign_present = FALSE;
355 sign_attrs_T sattr;
356#endif
357#ifdef FEAT_ARABIC
358 int prev_c = 0; // previous Arabic character
359 int prev_c1 = 0; // first composing char for prev_c
360#endif
361#if defined(LINE_ATTR)
362 int did_line_attr = 0;
363#endif
364#ifdef FEAT_TERMINAL
365 int get_term_attr = FALSE;
366#endif
367#ifdef FEAT_SYN_HL
368 int cul_attr = 0; // set when 'cursorline' active
369
370 // 'cursorlineopt' has "screenline" and cursor is in this line
371 int cul_screenline = FALSE;
372
373 // margin columns for the screen line, needed for when 'cursorlineopt'
374 // contains "screenline"
375 int left_curline_col = 0;
376 int right_curline_col = 0;
377#endif
378
379 // draw_state: items that are drawn in sequence:
380#define WL_START 0 // nothing done yet
381#ifdef FEAT_CMDWIN
382# define WL_CMDLINE WL_START + 1 // cmdline window column
383#else
384# define WL_CMDLINE WL_START
385#endif
386#ifdef FEAT_FOLDING
387# define WL_FOLD WL_CMDLINE + 1 // 'foldcolumn'
388#else
389# define WL_FOLD WL_CMDLINE
390#endif
391#ifdef FEAT_SIGNS
392# define WL_SIGN WL_FOLD + 1 // column for signs
393#else
394# define WL_SIGN WL_FOLD // column for signs
395#endif
396#define WL_NR WL_SIGN + 1 // line number
397#ifdef FEAT_LINEBREAK
398# define WL_BRI WL_NR + 1 // 'breakindent'
399#else
400# define WL_BRI WL_NR
401#endif
402#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
403# define WL_SBR WL_BRI + 1 // 'showbreak' or 'diff'
404#else
405# define WL_SBR WL_BRI
406#endif
407#define WL_LINE WL_SBR + 1 // text in the line
408 int draw_state = WL_START; // what to draw next
409#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
410 int feedback_col = 0;
411 int feedback_old_attr = -1;
412#endif
413 int screen_line_flags = 0;
414
415#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
416 int match_conc = 0; // cchar for match functions
417#endif
418#ifdef FEAT_CONCEAL
419 int syntax_flags = 0;
420 int syntax_seqnr = 0;
421 int prev_syntax_id = 0;
422 int conceal_attr = HL_ATTR(HLF_CONCEAL);
423 int is_concealing = FALSE;
424 int boguscols = 0; // nonexistent columns added to force
425 // wrapping
426 int vcol_off = 0; // offset for concealed characters
427 int did_wcol = FALSE;
428 int old_boguscols = 0;
429# define VCOL_HLC (vcol - vcol_off)
430# define FIX_FOR_BOGUSCOLS \
431 { \
432 n_extra += vcol_off; \
433 vcol -= vcol_off; \
434 vcol_off = 0; \
435 col -= boguscols; \
436 old_boguscols = boguscols; \
437 boguscols = 0; \
438 }
439#else
440# define VCOL_HLC (vcol)
441#endif
442
443 if (startrow > endrow) // past the end already!
444 return startrow;
445
446 row = startrow;
447 screen_row = row + W_WINROW(wp);
448
449 if (!number_only)
450 {
451 // To speed up the loop below, set extra_check when there is linebreak,
452 // trailing white space and/or syntax processing to be done.
453#ifdef FEAT_LINEBREAK
454 extra_check = wp->w_p_lbr;
455#endif
456#ifdef FEAT_SYN_HL
457 if (syntax_present(wp) && !wp->w_s->b_syn_error
458# ifdef SYN_TIME_LIMIT
459 && !wp->w_s->b_syn_slow
460# endif
461 )
462 {
463 // Prepare for syntax highlighting in this line. When there is an
464 // error, stop syntax highlighting.
465 save_did_emsg = did_emsg;
466 did_emsg = FALSE;
467 syntax_start(wp, lnum);
468 if (did_emsg)
469 wp->w_s->b_syn_error = TRUE;
470 else
471 {
472 did_emsg = save_did_emsg;
473#ifdef SYN_TIME_LIMIT
474 if (!wp->w_s->b_syn_slow)
475#endif
476 {
477 has_syntax = TRUE;
478 extra_check = TRUE;
479 }
480 }
481 }
482
483 // Check for columns to display for 'colorcolumn'.
484 color_cols = wp->w_p_cc_cols;
485 if (color_cols != NULL)
486 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
487#endif
488
489#ifdef FEAT_TERMINAL
490 if (term_show_buffer(wp->w_buffer))
491 {
492 extra_check = TRUE;
493 get_term_attr = TRUE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +0100494 win_attr = term_get_attr(wp, lnum, -1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200495 }
496#endif
497
498#ifdef FEAT_SPELL
499 if (wp->w_p_spell
500 && *wp->w_s->b_p_spl != NUL
501 && wp->w_s->b_langp.ga_len > 0
502 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
503 {
504 // Prepare for spell checking.
505 has_spell = TRUE;
506 extra_check = TRUE;
507
508 // Get the start of the next line, so that words that wrap to the
509 // next line are found too: "et<line-break>al.".
510 // Trick: skip a few chars for C/shell/Vim comments
511 nextline[SPWORDLEN] = NUL;
512 if (lnum < wp->w_buffer->b_ml.ml_line_count)
513 {
514 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
515 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
516 }
517
518 // When a word wrapped from the previous line the start of the
519 // current line is valid.
520 if (lnum == checked_lnum)
521 cur_checked_col = checked_col;
522 checked_lnum = 0;
523
524 // When there was a sentence end in the previous line may require a
525 // word starting with capital in this line. In line 1 always check
526 // the first word.
527 if (lnum != capcol_lnum)
528 cap_col = -1;
529 if (lnum == 1)
530 cap_col = 0;
531 capcol_lnum = 0;
532 }
533#endif
534
535 // handle Visual active in this window
536 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
537 {
Bram Moolenaar14285cb2020-03-27 20:58:37 +0100538 pos_T *top, *bot;
539
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200540 if (LTOREQ_POS(curwin->w_cursor, VIsual))
541 {
542 // Visual is after curwin->w_cursor
543 top = &curwin->w_cursor;
544 bot = &VIsual;
545 }
546 else
547 {
548 // Visual is before curwin->w_cursor
549 top = &VIsual;
550 bot = &curwin->w_cursor;
551 }
552 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
553 if (VIsual_mode == Ctrl_V)
554 {
555 // block mode
556 if (lnum_in_visual_area)
557 {
558 fromcol = wp->w_old_cursor_fcol;
559 tocol = wp->w_old_cursor_lcol;
560 }
561 }
562 else
563 {
564 // non-block mode
565 if (lnum > top->lnum && lnum <= bot->lnum)
566 fromcol = 0;
567 else if (lnum == top->lnum)
568 {
569 if (VIsual_mode == 'V') // linewise
570 fromcol = 0;
571 else
572 {
573 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
574 if (gchar_pos(top) == NUL)
575 tocol = fromcol + 1;
576 }
577 }
578 if (VIsual_mode != 'V' && lnum == bot->lnum)
579 {
580 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
581 {
582 fromcol = -10;
583 tocol = MAXCOL;
584 }
585 else if (bot->col == MAXCOL)
586 tocol = MAXCOL;
587 else
588 {
589 pos = *bot;
590 if (*p_sel == 'e')
591 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
592 else
593 {
594 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
595 ++tocol;
596 }
597 }
598 }
599 }
600
601 // Check if the character under the cursor should not be inverted
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200602 if (!highlight_match && lnum == curwin->w_cursor.lnum
603 && wp == curwin
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200604#ifdef FEAT_GUI
605 && !gui.in_use
606#endif
607 )
608 noinvcur = TRUE;
609
610 // if inverting in this line set area_highlighting
611 if (fromcol >= 0)
612 {
613 area_highlighting = TRUE;
614 vi_attr = HL_ATTR(HLF_V);
615#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
616 if ((clip_star.available && !clip_star.owned
617 && clip_isautosel_star())
618 || (clip_plus.available && !clip_plus.owned
619 && clip_isautosel_plus()))
620 vi_attr = HL_ATTR(HLF_VNC);
621#endif
622 }
623 }
624
625 // handle 'incsearch' and ":s///c" highlighting
626 else if (highlight_match
627 && wp == curwin
628 && lnum >= curwin->w_cursor.lnum
629 && lnum <= curwin->w_cursor.lnum + search_match_lines)
630 {
631 if (lnum == curwin->w_cursor.lnum)
632 getvcol(curwin, &(curwin->w_cursor),
633 (colnr_T *)&fromcol, NULL, NULL);
634 else
635 fromcol = 0;
636 if (lnum == curwin->w_cursor.lnum + search_match_lines)
637 {
638 pos.lnum = lnum;
639 pos.col = search_match_endcol;
640 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
641 }
642 else
643 tocol = MAXCOL;
644 // do at least one character; happens when past end of line
Bram Moolenaar448465e2020-11-25 13:49:27 +0100645 if (fromcol == tocol && search_match_endcol)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200646 tocol = fromcol + 1;
647 area_highlighting = TRUE;
648 vi_attr = HL_ATTR(HLF_I);
649 }
650 }
651
652#ifdef FEAT_DIFF
653 filler_lines = diff_check(wp, lnum);
654 if (filler_lines < 0)
655 {
656 if (filler_lines == -1)
657 {
658 if (diff_find_change(wp, lnum, &change_start, &change_end))
659 diff_hlf = HLF_ADD; // added line
660 else if (change_start == 0)
661 diff_hlf = HLF_TXD; // changed text
662 else
663 diff_hlf = HLF_CHD; // changed line
664 }
665 else
666 diff_hlf = HLF_ADD; // added line
667 filler_lines = 0;
668 area_highlighting = TRUE;
669 }
670 if (lnum == wp->w_topline)
671 filler_lines = wp->w_topfill;
672 filler_todo = filler_lines;
673#endif
674
675#ifdef FEAT_SIGNS
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +0100676 sign_present = buf_get_signattrs(wp, lnum, &sattr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200677#endif
678
679#ifdef LINE_ATTR
680# ifdef FEAT_SIGNS
681 // If this line has a sign with line highlighting set line_attr.
682 if (sign_present)
Bram Moolenaar6656c2e2019-10-24 15:00:04 +0200683 line_attr = sattr.sat_linehl;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200684# endif
685# if defined(FEAT_QUICKFIX)
686 // Highlight the current line in the quickfix window.
687 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
688 line_attr = HL_ATTR(HLF_QFL);
689# endif
690 if (line_attr != 0)
691 area_highlighting = TRUE;
692#endif
693
694 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
695 ptr = line;
696
697#ifdef FEAT_SPELL
698 if (has_spell && !number_only)
699 {
700 // For checking first word with a capital skip white space.
701 if (cap_col == 0)
702 cap_col = getwhitecols(line);
703
704 // To be able to spell-check over line boundaries copy the end of the
705 // current line into nextline[]. Above the start of the next line was
706 // copied to nextline[SPWORDLEN].
707 if (nextline[SPWORDLEN] == NUL)
708 {
709 // No next line or it is empty.
710 nextlinecol = MAXCOL;
711 nextline_idx = 0;
712 }
713 else
714 {
715 v = (long)STRLEN(line);
716 if (v < SPWORDLEN)
717 {
718 // Short line, use it completely and append the start of the
719 // next line.
720 nextlinecol = 0;
721 mch_memmove(nextline, line, (size_t)v);
722 STRMOVE(nextline + v, nextline + SPWORDLEN);
723 nextline_idx = v + 1;
724 }
725 else
726 {
727 // Long line, use only the last SPWORDLEN bytes.
728 nextlinecol = v - SPWORDLEN;
729 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
730 nextline_idx = SPWORDLEN + 1;
731 }
732 }
733 }
734#endif
735
736 if (wp->w_p_list)
737 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100738 if (wp->w_lcs_chars.space
739 || wp->w_lcs_chars.trail
740 || wp->w_lcs_chars.lead
741 || wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200742 extra_check = TRUE;
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100743
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200744 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +0100745 if (wp->w_lcs_chars.trail)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200746 {
747 trailcol = (colnr_T)STRLEN(ptr);
748 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
749 --trailcol;
750 trailcol += (colnr_T) (ptr - line);
751 }
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100752 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +0100753 if (wp->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +0100754 {
755 leadcol = 0;
756 while (VIM_ISWHITE(ptr[leadcol]))
757 ++leadcol;
758 if (ptr[leadcol] == NUL)
759 // in a line full of spaces all of them are treated as trailing
760 leadcol = (colnr_T)0;
761 else
762 // keep track of the first column not filled with spaces
763 leadcol += (colnr_T) (ptr - line) + 1;
764 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200765 }
766
767 wcr_attr = get_wcr_attr(wp);
768 if (wcr_attr != 0)
769 {
770 win_attr = wcr_attr;
771 area_highlighting = TRUE;
772 }
Bram Moolenaar34390282019-10-16 14:38:26 +0200773
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100774#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200775 if (WIN_IS_POPUP(wp))
776 screen_line_flags |= SLF_POPUP;
777#endif
778
779 // 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
780 // first character to be displayed.
781 if (wp->w_p_wrap)
782 v = wp->w_skipcol;
783 else
784 v = wp->w_leftcol;
785 if (v > 0 && !number_only)
786 {
787 char_u *prev_ptr = ptr;
788
789 while (vcol < v && *ptr != NUL)
790 {
791 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
792 vcol += c;
793 prev_ptr = ptr;
794 MB_PTR_ADV(ptr);
795 }
796
797 // When:
798 // - 'cuc' is set, or
799 // - 'colorcolumn' is set, or
800 // - 'virtualedit' is set, or
801 // - the visual mode is active,
802 // the end of the line may be before the start of the displayed part.
803 if (vcol < v && (
804#ifdef FEAT_SYN_HL
805 wp->w_p_cuc || draw_color_col ||
806#endif
807 virtual_active() ||
808 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
809 vcol = v;
810
811 // Handle a character that's not completely on the screen: Put ptr at
812 // that character but skip the first few screen characters.
813 if (vcol > v)
814 {
815 vcol -= c;
816 ptr = prev_ptr;
817 // If the character fits on the screen, don't need to skip it.
818 // Except for a TAB.
819 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
820 n_skip = v - vcol;
821 }
822
823 // Adjust for when the inverted text is before the screen,
824 // and when the start of the inverted text is before the screen.
825 if (tocol <= vcol)
826 fromcol = 0;
827 else if (fromcol >= 0 && fromcol < vcol)
828 fromcol = vcol;
829
830#ifdef FEAT_LINEBREAK
831 // When w_skipcol is non-zero, first line needs 'showbreak'
832 if (wp->w_p_wrap)
833 need_showbreak = TRUE;
834#endif
835#ifdef FEAT_SPELL
836 // When spell checking a word we need to figure out the start of the
837 // word and if it's badly spelled or not.
838 if (has_spell)
839 {
840 int len;
841 colnr_T linecol = (colnr_T)(ptr - line);
842 hlf_T spell_hlf = HLF_COUNT;
843
844 pos = wp->w_cursor;
845 wp->w_cursor.lnum = lnum;
846 wp->w_cursor.col = linecol;
847 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
848
849 // spell_move_to() may call ml_get() and make "line" invalid
850 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
851 ptr = line + linecol;
852
853 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
854 {
855 // no bad word found at line start, don't check until end of a
856 // word
857 spell_hlf = HLF_COUNT;
858 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
859 }
860 else
861 {
862 // bad word found, use attributes until end of word
863 word_end = wp->w_cursor.col + len + 1;
864
865 // Turn index into actual attributes.
866 if (spell_hlf != HLF_COUNT)
867 spell_attr = highlight_attr[spell_hlf];
868 }
869 wp->w_cursor = pos;
870
871# ifdef FEAT_SYN_HL
872 // Need to restart syntax highlighting for this line.
873 if (has_syntax)
874 syntax_start(wp, lnum);
875# endif
876 }
877#endif
878 }
879
880 // Correct highlighting for cursor that can't be disabled.
881 // Avoids having to check this for each character.
882 if (fromcol >= 0)
883 {
884 if (noinvcur)
885 {
886 if ((colnr_T)fromcol == wp->w_virtcol)
887 {
888 // highlighting starts at cursor, let it start just after the
889 // cursor
890 fromcol_prev = fromcol;
891 fromcol = -1;
892 }
893 else if ((colnr_T)fromcol < wp->w_virtcol)
894 // restart highlighting after the cursor
895 fromcol_prev = wp->w_virtcol;
896 }
897 if (fromcol >= tocol)
898 fromcol = -1;
899 }
900
901#ifdef FEAT_SEARCH_EXTRA
902 if (!number_only)
903 {
904 v = (long)(ptr - line);
905 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
906 &line, &screen_search_hl,
907 &search_attr);
908 ptr = line + v; // "line" may have been updated
909 }
910#endif
911
912#ifdef FEAT_SYN_HL
913 // Cursor line highlighting for 'cursorline' in the current window.
914 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
915 {
916 // Do not show the cursor line in the text when Visual mode is active,
917 // because it's not clear what is selected then. Do update
918 // w_last_cursorline.
919 if (!(wp == curwin && VIsual_active)
920 && wp->w_p_culopt_flags != CULOPT_NBR)
921 {
922 cul_screenline = (wp->w_p_wrap
923 && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
924
925 // Only set line_attr here when "screenline" is not present in
926 // 'cursorlineopt'. Otherwise it's done later.
927 if (!cul_screenline)
928 {
929 cul_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar39f7aa32020-08-31 22:00:05 +0200930# ifdef FEAT_SIGNS
931 // Combine the 'cursorline' and sign highlighting, depending on
932 // the sign priority.
933 if (sign_present && sattr.sat_linehl > 0)
934 {
935 if (sattr.sat_priority >= 100)
936 line_attr = hl_combine_attr(cul_attr, line_attr);
937 else
938 line_attr = hl_combine_attr(line_attr, cul_attr);
939 }
940 else
941# endif
942 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200943 wp->w_last_cursorline = wp->w_cursor.lnum;
944 }
945 else
946 {
947 line_attr_save = line_attr;
948 wp->w_last_cursorline = 0;
949 margin_columns_win(wp, &left_curline_col, &right_curline_col);
950 }
951 area_highlighting = TRUE;
952 }
953 else
954 wp->w_last_cursorline = wp->w_cursor.lnum;
955 }
956#endif
957
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100958#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200959 {
960 char_u *prop_start;
961
962 text_prop_count = get_text_props(wp->w_buffer, lnum,
963 &prop_start, FALSE);
964 if (text_prop_count > 0)
965 {
966 // Make a copy of the properties, so that they are properly
967 // aligned.
968 text_props = ALLOC_MULT(textprop_T, text_prop_count);
969 if (text_props != NULL)
970 mch_memmove(text_props, prop_start,
971 text_prop_count * sizeof(textprop_T));
972
973 // Allocate an array for the indexes.
974 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
975 area_highlighting = TRUE;
976 extra_check = TRUE;
977 }
978 }
979#endif
980
981 off = (unsigned)(current_ScreenLine - ScreenLines);
982 col = 0;
983
984#ifdef FEAT_RIGHTLEFT
985 if (wp->w_p_rl)
986 {
987 // Rightleft window: process the text in the normal direction, but put
988 // it in current_ScreenLine[] from right to left. Start at the
989 // rightmost column of the window.
990 col = wp->w_width - 1;
991 off += col;
992 screen_line_flags |= SLF_RIGHTLEFT;
993 }
994#endif
995
996 // Repeat for the whole displayed line.
997 for (;;)
998 {
999#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02001000 int has_match_conc = 0; // match wants to conceal
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001001#endif
1002#ifdef FEAT_CONCEAL
1003 int did_decrement_ptr = FALSE;
1004#endif
1005 // Skip this quickly when working on the text.
1006 if (draw_state != WL_LINE)
1007 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001008#ifdef FEAT_SYN_HL
1009 if (cul_screenline)
1010 {
1011 cul_attr = 0;
1012 line_attr = line_attr_save;
1013 }
1014#endif
1015
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001016#ifdef FEAT_CMDWIN
1017 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
1018 {
1019 draw_state = WL_CMDLINE;
1020 if (cmdwin_type != 0 && wp == curwin)
1021 {
1022 // Draw the cmdline character.
1023 n_extra = 1;
1024 c_extra = cmdwin_type;
1025 c_final = NUL;
1026 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
1027 }
1028 }
1029#endif
1030
1031#ifdef FEAT_FOLDING
1032 if (draw_state == WL_FOLD - 1 && n_extra == 0)
1033 {
1034 int fdc = compute_foldcolumn(wp, 0);
1035
1036 draw_state = WL_FOLD;
1037 if (fdc > 0)
1038 {
1039 // Draw the 'foldcolumn'. Allocate a buffer, "extra" may
1040 // already be in use.
1041 vim_free(p_extra_free);
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001042 p_extra_free = alloc(MAX_MCO * fdc + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001043 if (p_extra_free != NULL)
1044 {
Bram Moolenaar9355ae42021-03-08 19:04:05 +01001045 n_extra = (int)fill_foldcolumn(p_extra_free, wp,
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001046 FALSE, lnum);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001047 p_extra_free[n_extra] = NUL;
1048 p_extra = p_extra_free;
1049 c_extra = NUL;
1050 c_final = NUL;
1051 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
1052 }
1053 }
1054 }
1055#endif
1056
1057#ifdef FEAT_SIGNS
1058 if (draw_state == WL_SIGN - 1 && n_extra == 0)
1059 {
1060 draw_state = WL_SIGN;
1061 // Show the sign column when there are any signs in this
1062 // buffer or when using Netbeans.
1063 if (signcolumn_on(wp))
1064 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
1065 row, startrow, filler_lines, filler_todo, &c_extra,
1066 &c_final, extra, &p_extra, &n_extra, &char_attr);
1067 }
1068#endif
1069
1070 if (draw_state == WL_NR - 1 && n_extra == 0)
1071 {
1072 draw_state = WL_NR;
1073 // Display the absolute or relative line number. After the
1074 // first fill with blanks when the 'n' flag isn't in 'cpo'
1075 if ((wp->w_p_nu || wp->w_p_rnu)
1076 && (row == startrow
1077#ifdef FEAT_DIFF
1078 + filler_lines
1079#endif
1080 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
1081 {
1082#ifdef FEAT_SIGNS
1083 // If 'signcolumn' is set to 'number' and a sign is present
1084 // in 'lnum', then display the sign instead of the line
1085 // number.
1086 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
1087 && sign_present)
1088 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
1089 row, startrow, filler_lines, filler_todo,
1090 &c_extra, &c_final, extra, &p_extra, &n_extra,
1091 &char_attr);
1092 else
1093#endif
1094 {
1095 // Draw the line number (empty space after wrapping).
1096 if (row == startrow
1097#ifdef FEAT_DIFF
1098 + filler_lines
1099#endif
1100 )
1101 {
1102 long num;
1103 char *fmt = "%*ld ";
1104
1105 if (wp->w_p_nu && !wp->w_p_rnu)
1106 // 'number' + 'norelativenumber'
1107 num = (long)lnum;
1108 else
1109 {
1110 // 'relativenumber', don't use negative numbers
1111 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1112 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1113 {
1114 // 'number' + 'relativenumber'
1115 num = lnum;
1116 fmt = "%-*ld ";
1117 }
1118 }
1119
1120 sprintf((char *)extra, fmt,
1121 number_width(wp), num);
1122 if (wp->w_skipcol > 0)
1123 for (p_extra = extra; *p_extra == ' '; ++p_extra)
1124 *p_extra = '-';
1125#ifdef FEAT_RIGHTLEFT
1126 if (wp->w_p_rl) // reverse line numbers
1127 {
1128 char_u *p1, *p2;
1129 int t;
1130
1131 // like rl_mirror(), but keep the space at the end
Christian Brabandt29f0dc32021-06-16 19:28:34 +02001132 p2 = skipwhite(extra);
1133 p2 = skiptowhite(p2) - 1;
1134 for (p1 = skipwhite(extra); p1 < p2; ++p1, --p2)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001135 {
1136 t = *p1;
1137 *p1 = *p2;
1138 *p2 = t;
1139 }
1140 }
1141#endif
1142 p_extra = extra;
1143 c_extra = NUL;
1144 c_final = NUL;
1145 }
1146 else
1147 {
1148 c_extra = ' ';
1149 c_final = NUL;
1150 }
1151 n_extra = number_width(wp) + 1;
1152 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
1153#ifdef FEAT_SYN_HL
1154 // When 'cursorline' is set highlight the line number of
1155 // the current line differently.
1156 // When 'cursorlineopt' has "screenline" only highlight
1157 // the line number itself.
1158 // TODO: Can we use CursorLine instead of CursorLineNr
1159 // when CursorLineNr isn't set?
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001160 if (wp->w_p_cul
1161 && lnum == wp->w_cursor.lnum
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001162 && (wp->w_p_culopt_flags & CULOPT_NBR)
1163 && (row == startrow
Bram Moolenaar49474ca2019-10-05 21:57:12 +02001164 || wp->w_p_culopt_flags & CULOPT_LINE))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001165 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
1166#endif
Bram Moolenaarefae76a2019-10-27 22:54:58 +01001167 if (wp->w_p_rnu && lnum < wp->w_cursor.lnum
1168 && HL_ATTR(HLF_LNA) != 0)
1169 // Use LineNrAbove
1170 char_attr = hl_combine_attr(wcr_attr,
1171 HL_ATTR(HLF_LNA));
1172 if (wp->w_p_rnu && lnum > wp->w_cursor.lnum
1173 && HL_ATTR(HLF_LNB) != 0)
1174 // Use LineNrBelow
1175 char_attr = hl_combine_attr(wcr_attr,
1176 HL_ATTR(HLF_LNB));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001177 }
1178 }
1179 }
1180
1181#ifdef FEAT_LINEBREAK
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001182 if (wp->w_briopt_sbr && draw_state == WL_BRI - 1
Bram Moolenaaree857022019-11-09 23:26:40 +01001183 && n_extra == 0 && *get_showbreak_value(wp) != NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001184 // draw indent after showbreak value
1185 draw_state = WL_BRI;
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001186 else if (wp->w_briopt_sbr && draw_state == WL_SBR && n_extra == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001187 // After the showbreak, draw the breakindent
1188 draw_state = WL_BRI - 1;
1189
1190 // draw 'breakindent': indent wrapped text accordingly
1191 if (draw_state == WL_BRI - 1 && n_extra == 0)
1192 {
1193 draw_state = WL_BRI;
1194 // if need_showbreak is set, breakindent also applies
1195 if (wp->w_p_bri && n_extra == 0
1196 && (row != startrow || need_showbreak)
1197# ifdef FEAT_DIFF
1198 && filler_lines == 0
1199# endif
1200 )
1201 {
1202 char_attr = 0;
1203# ifdef FEAT_DIFF
1204 if (diff_hlf != (hlf_T)0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001205 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001206# endif
1207 p_extra = NULL;
1208 c_extra = ' ';
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001209 c_final = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001210 n_extra = get_breakindent_win(wp,
1211 ml_get_buf(wp->w_buffer, lnum, FALSE));
Bram Moolenaare882f7a2020-05-16 14:07:39 +02001212 if (row == startrow)
1213 {
1214 n_extra -= win_col_off2(wp);
1215 if (n_extra < 0)
1216 n_extra = 0;
1217 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001218 if (wp->w_skipcol > 0 && wp->w_p_wrap && wp->w_briopt_sbr)
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001219 need_showbreak = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001220 // Correct end of highlighted area for 'breakindent',
1221 // required when 'linebreak' is also set.
1222 if (tocol == vcol)
1223 tocol += n_extra;
1224 }
1225 }
1226#endif
1227
1228#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
1229 if (draw_state == WL_SBR - 1 && n_extra == 0)
1230 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001231 char_u *sbr;
1232
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001233 draw_state = WL_SBR;
1234# ifdef FEAT_DIFF
1235 if (filler_todo > 0)
1236 {
1237 // Draw "deleted" diff line(s).
1238 if (char2cells(fill_diff) > 1)
1239 {
1240 c_extra = '-';
1241 c_final = NUL;
1242 }
1243 else
1244 {
1245 c_extra = fill_diff;
1246 c_final = NUL;
1247 }
1248# ifdef FEAT_RIGHTLEFT
1249 if (wp->w_p_rl)
1250 n_extra = col + 1;
1251 else
1252# endif
1253 n_extra = wp->w_width - col;
1254 char_attr = HL_ATTR(HLF_DED);
1255 }
1256# endif
1257# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001258 sbr = get_showbreak_value(wp);
1259 if (*sbr != NUL && need_showbreak)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001260 {
1261 // Draw 'showbreak' at the start of each broken line.
Bram Moolenaaree857022019-11-09 23:26:40 +01001262 p_extra = sbr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001263 c_extra = NUL;
1264 c_final = NUL;
Bram Moolenaaree857022019-11-09 23:26:40 +01001265 n_extra = (int)STRLEN(sbr);
Bram Moolenaardfede9a2020-01-23 19:59:22 +01001266 if (wp->w_skipcol == 0 || !wp->w_p_wrap)
1267 need_showbreak = FALSE;
Bram Moolenaaree857022019-11-09 23:26:40 +01001268 vcol_sbr = vcol + MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001269 // Correct end of highlighted area for 'showbreak',
1270 // required when 'linebreak' is also set.
1271 if (tocol == vcol)
1272 tocol += n_extra;
1273 // combine 'showbreak' with 'wincolor'
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001274 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001275# ifdef FEAT_SYN_HL
1276 // combine 'showbreak' with 'cursorline'
1277 if (cul_attr != 0)
1278 char_attr = hl_combine_attr(char_attr, cul_attr);
1279# endif
1280 }
1281# endif
1282 }
1283#endif
1284
1285 if (draw_state == WL_LINE - 1 && n_extra == 0)
1286 {
1287 draw_state = WL_LINE;
1288 if (saved_n_extra)
1289 {
1290 // Continue item from end of wrapped line.
1291 n_extra = saved_n_extra;
1292 c_extra = saved_c_extra;
1293 c_final = saved_c_final;
1294 p_extra = saved_p_extra;
1295 char_attr = saved_char_attr;
1296 }
1297 else
1298 char_attr = win_attr;
1299 }
1300 }
1301#ifdef FEAT_SYN_HL
zeertzjq4f33bc22021-08-05 17:57:02 +02001302 if (cul_screenline && draw_state == WL_LINE
1303 && vcol >= left_curline_col
1304 && vcol < right_curline_col)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001305 {
zeertzjq4f33bc22021-08-05 17:57:02 +02001306 cul_attr = HL_ATTR(HLF_CUL);
1307 line_attr = cul_attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001308 }
1309#endif
1310
1311 // When still displaying '$' of change command, stop at cursor.
1312 // When only displaying the (relative) line number and that's done,
1313 // stop here.
Bram Moolenaar511feec2020-06-18 19:15:27 +02001314 if (((dollar_vcol >= 0 && wp == curwin
1315 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol)
1316 || (number_only && draw_state > WL_NR))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001317#ifdef FEAT_DIFF
1318 && filler_todo <= 0
1319#endif
1320 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001321 {
1322 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
1323 screen_line_flags);
1324 // Pretend we have finished updating the window. Except when
1325 // 'cursorcolumn' is set.
1326#ifdef FEAT_SYN_HL
1327 if (wp->w_p_cuc)
1328 row = wp->w_cline_row + wp->w_cline_height;
1329 else
1330#endif
1331 row = wp->w_height;
1332 break;
1333 }
1334
Bram Moolenaar34390282019-10-16 14:38:26 +02001335 if (draw_state == WL_LINE && (area_highlighting || extra_check))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001336 {
1337 // handle Visual or match highlighting in this line
1338 if (vcol == fromcol
1339 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
1340 && (*mb_ptr2cells)(ptr) > 1)
1341 || ((int)vcol_prev == fromcol_prev
1342 && vcol_prev < vcol // not at margin
1343 && vcol < tocol))
1344 area_attr = vi_attr; // start highlighting
1345 else if (area_attr != 0
1346 && (vcol == tocol
1347 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
1348 area_attr = 0; // stop highlighting
1349
1350#ifdef FEAT_SEARCH_EXTRA
1351 if (!n_extra)
1352 {
1353 // Check for start/end of 'hlsearch' and other matches.
1354 // After end, check for start/end of next match.
1355 // When another match, have to check for start again.
1356 v = (long)(ptr - line);
1357 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
1358 &screen_search_hl, &has_match_conc,
1359 &match_conc, did_line_attr, lcs_eol_one);
1360 ptr = line + v; // "line" may have been changed
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001361
1362 // Do not allow a conceal over EOL otherwise EOL will be missed
1363 // and bad things happen.
1364 if (*ptr == NUL)
1365 has_match_conc = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001366 }
1367#endif
1368
1369#ifdef FEAT_DIFF
1370 if (diff_hlf != (hlf_T)0)
1371 {
1372 if (diff_hlf == HLF_CHD && ptr - line >= change_start
1373 && n_extra == 0)
1374 diff_hlf = HLF_TXD; // changed text
1375 if (diff_hlf == HLF_TXD && ptr - line > change_end
1376 && n_extra == 0)
1377 diff_hlf = HLF_CHD; // changed line
1378 line_attr = HL_ATTR(diff_hlf);
1379 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
1380 && wp->w_p_culopt_flags != CULOPT_NBR
1381 && (!cul_screenline || (vcol >= left_curline_col
1382 && vcol <= right_curline_col)))
1383 line_attr = hl_combine_attr(
1384 line_attr, HL_ATTR(HLF_CUL));
1385 }
1386#endif
1387
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001388#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001389 if (text_props != NULL)
1390 {
1391 int pi;
1392 int bcol = (int)(ptr - line);
1393
1394 if (n_extra > 0)
1395 --bcol; // still working on the previous char, e.g. Tab
1396
1397 // Check if any active property ends.
1398 for (pi = 0; pi < text_props_active; ++pi)
1399 {
1400 int tpi = text_prop_idxs[pi];
1401
1402 if (bcol >= text_props[tpi].tp_col - 1
1403 + text_props[tpi].tp_len)
1404 {
1405 if (pi + 1 < text_props_active)
1406 mch_memmove(text_prop_idxs + pi,
1407 text_prop_idxs + pi + 1,
1408 sizeof(int)
1409 * (text_props_active - (pi + 1)));
1410 --text_props_active;
1411 --pi;
1412 }
1413 }
1414
1415 // Add any text property that starts in this column.
1416 while (text_prop_next < text_prop_count
1417 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001418 {
1419 if (bcol <= text_props[text_prop_next].tp_col - 1
1420 + text_props[text_prop_next].tp_len)
1421 text_prop_idxs[text_props_active++] = text_prop_next;
1422 ++text_prop_next;
1423 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001424
1425 text_prop_attr = 0;
1426 text_prop_combine = FALSE;
Bram Moolenaar053f7122019-09-23 22:17:15 +02001427 text_prop_type = NULL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001428 if (text_props_active > 0)
1429 {
1430 // Sort the properties on priority and/or starting last.
1431 // Then combine the attributes, highest priority last.
1432 current_text_props = text_props;
1433 current_buf = wp->w_buffer;
1434 qsort((void *)text_prop_idxs, (size_t)text_props_active,
1435 sizeof(int), text_prop_compare);
1436
1437 for (pi = 0; pi < text_props_active; ++pi)
1438 {
1439 int tpi = text_prop_idxs[pi];
1440 proptype_T *pt = text_prop_type_by_id(
1441 wp->w_buffer, text_props[tpi].tp_type);
1442
1443 if (pt != NULL && pt->pt_hl_id > 0)
1444 {
1445 int pt_attr = syn_id2attr(pt->pt_hl_id);
1446
1447 text_prop_type = pt;
1448 text_prop_attr =
1449 hl_combine_attr(text_prop_attr, pt_attr);
1450 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
1451 }
1452 }
1453 }
1454 }
1455#endif
1456
Bram Moolenaara74fda62019-10-19 17:38:03 +02001457#ifdef FEAT_SYN_HL
Bram Moolenaara74fda62019-10-19 17:38:03 +02001458 if (extra_check && n_extra == 0)
Bram Moolenaar34390282019-10-16 14:38:26 +02001459 {
Bram Moolenaar82260af2019-10-20 13:16:22 +02001460 syntax_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001461# ifdef FEAT_TERMINAL
Bram Moolenaar34390282019-10-16 14:38:26 +02001462 if (get_term_attr)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001463 syntax_attr = term_get_attr(wp, lnum, vcol);
Bram Moolenaara74fda62019-10-19 17:38:03 +02001464# endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001465 // Get syntax attribute.
1466 if (has_syntax)
1467 {
1468 // Get the syntax attribute for the character. If there
1469 // is an error, disable syntax highlighting.
1470 save_did_emsg = did_emsg;
1471 did_emsg = FALSE;
1472
1473 v = (long)(ptr - line);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001474 if (v == prev_syntax_col)
1475 // at same column again
1476 syntax_attr = prev_syntax_attr;
1477 else
1478 {
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001479# ifdef FEAT_SPELL
Bram Moolenaar9115c612019-10-16 16:57:06 +02001480 can_spell = TRUE;
Bram Moolenaarb2fe1d62019-10-16 21:33:40 +02001481# endif
Bram Moolenaar9115c612019-10-16 16:57:06 +02001482 syntax_attr = get_syntax_attr((colnr_T)v,
Bram Moolenaar34390282019-10-16 14:38:26 +02001483# ifdef FEAT_SPELL
1484 has_spell ? &can_spell :
1485# endif
1486 NULL, FALSE);
Bram Moolenaar9115c612019-10-16 16:57:06 +02001487 prev_syntax_col = v;
1488 prev_syntax_attr = syntax_attr;
1489 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001490
Bram Moolenaar34390282019-10-16 14:38:26 +02001491 if (did_emsg)
1492 {
1493 wp->w_s->b_syn_error = TRUE;
1494 has_syntax = FALSE;
1495 syntax_attr = 0;
1496 }
1497 else
1498 did_emsg = save_did_emsg;
1499# ifdef SYN_TIME_LIMIT
1500 if (wp->w_s->b_syn_slow)
1501 has_syntax = FALSE;
1502# endif
1503
1504 // Need to get the line again, a multi-line regexp may
1505 // have made it invalid.
1506 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1507 ptr = line + v;
1508# ifdef FEAT_CONCEAL
1509 // no concealing past the end of the line, it interferes
1510 // with line highlighting
1511 if (*ptr == NUL)
1512 syntax_flags = 0;
1513 else
1514 syntax_flags = get_syntax_info(&syntax_seqnr);
1515# endif
1516 }
Bram Moolenaar34390282019-10-16 14:38:26 +02001517 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001518# ifdef FEAT_PROP_POPUP
Bram Moolenaardbd43162019-11-09 21:28:14 +01001519 // Combine text property highlight into syntax highlight.
1520 if (text_prop_type != NULL)
1521 {
1522 if (text_prop_combine)
1523 syntax_attr = hl_combine_attr(syntax_attr, text_prop_attr);
1524 else
1525 syntax_attr = text_prop_attr;
1526 }
1527# endif
Bram Moolenaara74fda62019-10-19 17:38:03 +02001528#endif
Bram Moolenaar34390282019-10-16 14:38:26 +02001529
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001530 // Decide which of the highlight attributes to use.
1531 attr_pri = TRUE;
1532#ifdef LINE_ATTR
1533 if (area_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001534 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001535 char_attr = hl_combine_attr(line_attr, area_attr);
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001536 if (!highlight_match)
1537 // let search highlight show in Visual area if possible
1538 char_attr = hl_combine_attr(search_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001539# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001540 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001541# endif
1542 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001543 else if (search_attr != 0)
Bram Moolenaar84590062019-10-18 23:12:20 +02001544 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001545 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001546# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001547 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar84590062019-10-18 23:12:20 +02001548# endif
1549 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001550 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
1551 || vcol < fromcol || vcol_prev < fromcol_prev
1552 || vcol >= tocol))
1553 {
1554 // Use line_attr when not in the Visual or 'incsearch' area
1555 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar34390282019-10-16 14:38:26 +02001556# ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001557 char_attr = hl_combine_attr(syntax_attr, line_attr);
1558# else
1559 char_attr = line_attr;
Bram Moolenaar34390282019-10-16 14:38:26 +02001560# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001561 attr_pri = FALSE;
1562 }
1563#else
1564 if (area_attr != 0)
1565 char_attr = area_attr;
1566 else if (search_attr != 0)
1567 char_attr = search_attr;
1568#endif
1569 else
1570 {
1571 attr_pri = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001572#ifdef FEAT_SYN_HL
Bram Moolenaardbd43162019-11-09 21:28:14 +01001573 char_attr = syntax_attr;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001574#else
Bram Moolenaardbd43162019-11-09 21:28:14 +01001575 char_attr = 0;
Bram Moolenaara74fda62019-10-19 17:38:03 +02001576#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001577 }
1578 }
Bram Moolenaar024dbd22019-11-02 22:00:15 +01001579
1580 // combine attribute with 'wincolor'
1581 if (win_attr != 0)
1582 {
1583 if (char_attr == 0)
1584 char_attr = win_attr;
1585 else
1586 char_attr = hl_combine_attr(win_attr, char_attr);
1587 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001588
1589 // Get the next character to put on the screen.
1590
1591 // The "p_extra" points to the extra stuff that is inserted to
1592 // represent special characters (non-printable stuff) and other
1593 // things. When all characters are the same, c_extra is used.
1594 // If c_final is set, it will compulsorily be used at the end.
1595 // "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
1596 // "p_extra[n_extra]".
1597 // For the '$' of the 'list' option, n_extra == 1, p_extra == "".
1598 if (n_extra > 0)
1599 {
1600 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
1601 {
1602 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
1603 mb_c = c; // doesn't handle non-utf-8 multi-byte!
1604 if (enc_utf8 && utf_char2len(c) > 1)
1605 {
1606 mb_utf8 = TRUE;
1607 u8cc[0] = 0;
1608 c = 0xc0;
1609 }
1610 else
1611 mb_utf8 = FALSE;
1612 }
1613 else
1614 {
1615 c = *p_extra;
1616 if (has_mbyte)
1617 {
1618 mb_c = c;
1619 if (enc_utf8)
1620 {
1621 // If the UTF-8 character is more than one byte:
1622 // Decode it into "mb_c".
1623 mb_l = utfc_ptr2len(p_extra);
1624 mb_utf8 = FALSE;
1625 if (mb_l > n_extra)
1626 mb_l = 1;
1627 else if (mb_l > 1)
1628 {
1629 mb_c = utfc_ptr2char(p_extra, u8cc);
1630 mb_utf8 = TRUE;
1631 c = 0xc0;
1632 }
1633 }
1634 else
1635 {
1636 // if this is a DBCS character, put it in "mb_c"
1637 mb_l = MB_BYTE2LEN(c);
1638 if (mb_l >= n_extra)
1639 mb_l = 1;
1640 else if (mb_l > 1)
1641 mb_c = (c << 8) + p_extra[1];
1642 }
1643 if (mb_l == 0) // at the NUL at end-of-line
1644 mb_l = 1;
1645
1646 // If a double-width char doesn't fit display a '>' in the
1647 // last column.
1648 if ((
1649# ifdef FEAT_RIGHTLEFT
1650 wp->w_p_rl ? (col <= 0) :
1651# endif
1652 (col >= wp->w_width - 1))
1653 && (*mb_char2cells)(mb_c) == 2)
1654 {
1655 c = '>';
1656 mb_c = c;
1657 mb_l = 1;
1658 mb_utf8 = FALSE;
1659 multi_attr = HL_ATTR(HLF_AT);
1660#ifdef FEAT_SYN_HL
1661 if (cul_attr)
1662 multi_attr = hl_combine_attr(multi_attr, cul_attr);
1663#endif
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001664 multi_attr = hl_combine_attr(win_attr, multi_attr);
1665
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001666 // put the pointer back to output the double-width
1667 // character at the start of the next line.
1668 ++n_extra;
1669 --p_extra;
1670 }
1671 else
1672 {
1673 n_extra -= mb_l - 1;
1674 p_extra += mb_l - 1;
1675 }
1676 }
1677 ++p_extra;
1678 }
1679 --n_extra;
1680 }
1681 else
1682 {
1683#ifdef FEAT_LINEBREAK
1684 int c0;
1685#endif
Bram Moolenaar2f7b7b12019-11-03 15:46:48 +01001686 VIM_CLEAR(p_extra_free);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001687
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001688 // Get a character from the line itself.
1689 c = *ptr;
1690#ifdef FEAT_LINEBREAK
1691 c0 = *ptr;
1692#endif
1693 if (has_mbyte)
1694 {
1695 mb_c = c;
1696 if (enc_utf8)
1697 {
1698 // If the UTF-8 character is more than one byte: Decode it
1699 // into "mb_c".
1700 mb_l = utfc_ptr2len(ptr);
1701 mb_utf8 = FALSE;
1702 if (mb_l > 1)
1703 {
1704 mb_c = utfc_ptr2char(ptr, u8cc);
1705 // Overlong encoded ASCII or ASCII with composing char
1706 // is displayed normally, except a NUL.
1707 if (mb_c < 0x80)
1708 {
1709 c = mb_c;
1710#ifdef FEAT_LINEBREAK
1711 c0 = mb_c;
1712#endif
1713 }
1714 mb_utf8 = TRUE;
1715
1716 // At start of the line we can have a composing char.
1717 // Draw it as a space with a composing char.
1718 if (utf_iscomposing(mb_c))
1719 {
1720 int i;
1721
1722 for (i = Screen_mco - 1; i > 0; --i)
1723 u8cc[i] = u8cc[i - 1];
1724 u8cc[0] = mb_c;
1725 mb_c = ' ';
1726 }
1727 }
1728
1729 if ((mb_l == 1 && c >= 0x80)
1730 || (mb_l >= 1 && mb_c == 0)
1731 || (mb_l > 1 && (!vim_isprintc(mb_c))))
1732 {
1733 // Illegal UTF-8 byte: display as <xx>.
1734 // Non-BMP character : display as ? or fullwidth ?.
1735 transchar_hex(extra, mb_c);
1736# ifdef FEAT_RIGHTLEFT
1737 if (wp->w_p_rl) // reverse
1738 rl_mirror(extra);
1739# endif
1740 p_extra = extra;
1741 c = *p_extra;
1742 mb_c = mb_ptr2char_adv(&p_extra);
1743 mb_utf8 = (c >= 0x80);
1744 n_extra = (int)STRLEN(p_extra);
1745 c_extra = NUL;
1746 c_final = NUL;
1747 if (area_attr == 0 && search_attr == 0)
1748 {
1749 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001750 extra_attr = hl_combine_attr(
1751 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001752 saved_attr2 = char_attr; // save current attr
1753 }
1754 }
1755 else if (mb_l == 0) // at the NUL at end-of-line
1756 mb_l = 1;
1757#ifdef FEAT_ARABIC
1758 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
1759 {
1760 // Do Arabic shaping.
1761 int pc, pc1, nc;
1762 int pcc[MAX_MCO];
1763
1764 // The idea of what is the previous and next
1765 // character depends on 'rightleft'.
1766 if (wp->w_p_rl)
1767 {
1768 pc = prev_c;
1769 pc1 = prev_c1;
1770 nc = utf_ptr2char(ptr + mb_l);
1771 prev_c1 = u8cc[0];
1772 }
1773 else
1774 {
1775 pc = utfc_ptr2char(ptr + mb_l, pcc);
1776 nc = prev_c;
1777 pc1 = pcc[0];
1778 }
1779 prev_c = mb_c;
1780
1781 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
1782 }
1783 else
1784 prev_c = mb_c;
1785#endif
1786 }
1787 else // enc_dbcs
1788 {
1789 mb_l = MB_BYTE2LEN(c);
1790 if (mb_l == 0) // at the NUL at end-of-line
1791 mb_l = 1;
1792 else if (mb_l > 1)
1793 {
1794 // We assume a second byte below 32 is illegal.
1795 // Hopefully this is OK for all double-byte encodings!
1796 if (ptr[1] >= 32)
1797 mb_c = (c << 8) + ptr[1];
1798 else
1799 {
1800 if (ptr[1] == NUL)
1801 {
1802 // head byte at end of line
1803 mb_l = 1;
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001804 transchar_nonprint(wp->w_buffer, extra, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001805 }
1806 else
1807 {
1808 // illegal tail byte
1809 mb_l = 2;
1810 STRCPY(extra, "XX");
1811 }
1812 p_extra = extra;
1813 n_extra = (int)STRLEN(extra) - 1;
1814 c_extra = NUL;
1815 c_final = NUL;
1816 c = *p_extra++;
1817 if (area_attr == 0 && search_attr == 0)
1818 {
1819 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001820 extra_attr = hl_combine_attr(
1821 win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001822 saved_attr2 = char_attr; // save current attr
1823 }
1824 mb_c = c;
1825 }
1826 }
1827 }
1828 // If a double-width char doesn't fit display a '>' in the
1829 // last column; the character is displayed at the start of the
1830 // next line.
1831 if ((
1832# ifdef FEAT_RIGHTLEFT
1833 wp->w_p_rl ? (col <= 0) :
1834# endif
1835 (col >= wp->w_width - 1))
1836 && (*mb_char2cells)(mb_c) == 2)
1837 {
1838 c = '>';
1839 mb_c = c;
1840 mb_utf8 = FALSE;
1841 mb_l = 1;
Bram Moolenaar92e25ab2019-11-26 22:39:10 +01001842 multi_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001843 // Put pointer back so that the character will be
1844 // displayed at the start of the next line.
1845 --ptr;
1846#ifdef FEAT_CONCEAL
1847 did_decrement_ptr = TRUE;
1848#endif
1849 }
1850 else if (*ptr != NUL)
1851 ptr += mb_l - 1;
1852
1853 // If a double-width char doesn't fit at the left side display
1854 // a '<' in the first column. Don't do this for unprintable
1855 // characters.
1856 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
1857 {
1858 n_extra = 1;
1859 c_extra = MB_FILLER_CHAR;
1860 c_final = NUL;
1861 c = ' ';
1862 if (area_attr == 0 && search_attr == 0)
1863 {
1864 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01001865 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001866 saved_attr2 = char_attr; // save current attr
1867 }
1868 mb_c = c;
1869 mb_utf8 = FALSE;
1870 mb_l = 1;
1871 }
1872
1873 }
1874 ++ptr;
1875
1876 if (extra_check)
1877 {
1878#ifdef FEAT_SPELL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001879 // Check spelling (unless at the end of the line).
1880 // Only do this when there is no syntax highlighting, the
1881 // @Spell cluster is not used or the current syntax item
1882 // contains the @Spell cluster.
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001883 v = (long)(ptr - line);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001884 if (has_spell && v >= word_end && v > cur_checked_col)
1885 {
1886 spell_attr = 0;
1887 if (c != 0 && (
1888# ifdef FEAT_SYN_HL
1889 !has_syntax ||
1890# endif
1891 can_spell))
1892 {
1893 char_u *prev_ptr, *p;
1894 int len;
1895 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001896
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001897 if (has_mbyte)
1898 {
1899 prev_ptr = ptr - mb_l;
1900 v -= mb_l - 1;
1901 }
1902 else
1903 prev_ptr = ptr - 1;
1904
1905 // Use nextline[] if possible, it has the start of the
1906 // next line concatenated.
1907 if ((prev_ptr - line) - nextlinecol >= 0)
1908 p = nextline + (prev_ptr - line) - nextlinecol;
1909 else
1910 p = prev_ptr;
1911 cap_col -= (int)(prev_ptr - line);
1912 len = spell_check(wp, p, &spell_hlf, &cap_col,
1913 nochange);
1914 word_end = v + len;
1915
1916 // In Insert mode only highlight a word that
1917 // doesn't touch the cursor.
1918 if (spell_hlf != HLF_COUNT
1919 && (State & INSERT) != 0
1920 && wp->w_cursor.lnum == lnum
1921 && wp->w_cursor.col >=
1922 (colnr_T)(prev_ptr - line)
1923 && wp->w_cursor.col < (colnr_T)word_end)
1924 {
1925 spell_hlf = HLF_COUNT;
1926 spell_redraw_lnum = lnum;
1927 }
1928
1929 if (spell_hlf == HLF_COUNT && p != prev_ptr
1930 && (p - nextline) + len > nextline_idx)
1931 {
1932 // Remember that the good word continues at the
1933 // start of the next line.
1934 checked_lnum = lnum + 1;
Bram Moolenaar7751d1d2019-10-18 20:37:08 +02001935 checked_col = (int)((p - nextline)
1936 + len - nextline_idx);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001937 }
1938
1939 // Turn index into actual attributes.
1940 if (spell_hlf != HLF_COUNT)
1941 spell_attr = highlight_attr[spell_hlf];
1942
1943 if (cap_col > 0)
1944 {
1945 if (p != prev_ptr
1946 && (p - nextline) + cap_col >= nextline_idx)
1947 {
1948 // Remember that the word in the next line
1949 // must start with a capital.
1950 capcol_lnum = lnum + 1;
1951 cap_col = (int)((p - nextline) + cap_col
1952 - nextline_idx);
1953 }
1954 else
1955 // Compute the actual column.
1956 cap_col += (int)(prev_ptr - line);
1957 }
1958 }
1959 }
1960 if (spell_attr != 0)
1961 {
1962 if (!attr_pri)
1963 char_attr = hl_combine_attr(char_attr, spell_attr);
1964 else
1965 char_attr = hl_combine_attr(spell_attr, char_attr);
1966 }
1967#endif
1968#ifdef FEAT_LINEBREAK
1969 // Found last space before word: check for line break.
1970 if (wp->w_p_lbr && c0 == c
1971 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
1972 {
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02001973 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1)
1974 : 0;
1975 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001976
1977 // TODO: is passing p for start of the line OK?
1978 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
1979 NULL) - 1;
Bram Moolenaara06e3452021-05-29 17:56:37 +02001980
1981 // We have just drawn the showbreak value, no need to add
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02001982 // space for it again.
Bram Moolenaara06e3452021-05-29 17:56:37 +02001983 if (vcol == vcol_sbr)
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02001984 {
Bram Moolenaara06e3452021-05-29 17:56:37 +02001985 n_extra -= MB_CHARLEN(get_showbreak_value(wp));
Bram Moolenaar20e0c3d2021-08-31 20:57:55 +02001986 if (n_extra < 0)
1987 n_extra = 0;
1988 }
Bram Moolenaara06e3452021-05-29 17:56:37 +02001989
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001990 if (c == TAB && n_extra + col > wp->w_width)
1991# ifdef FEAT_VARTABS
1992 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
1993 wp->w_buffer->b_p_vts_array) - 1;
1994# else
1995 n_extra = (int)wp->w_buffer->b_p_ts
1996 - vcol % (int)wp->w_buffer->b_p_ts - 1;
1997# endif
1998
1999 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
2000 c_final = NUL;
2001 if (VIM_ISWHITE(c))
2002 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002003# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002004 if (c == TAB)
2005 // See "Tab alignment" below.
2006 FIX_FOR_BOGUSCOLS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002007# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002008 if (!wp->w_p_list)
2009 c = ' ';
2010 }
2011 }
2012#endif
2013
Bram Moolenaareed9d462021-02-15 20:38:25 +01002014 // 'list': Change char 160 to 'nbsp' and space to 'space'
2015 // setting in 'listchars'. But not when the character is
2016 // followed by a composing character (use mb_l to check that).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002017 if (wp->w_p_list
2018 && ((((c == 160 && mb_l == 1)
2019 || (mb_utf8
2020 && ((mb_c == 160 && mb_l == 2)
2021 || (mb_c == 0x202f && mb_l == 3))))
Bram Moolenaareed9d462021-02-15 20:38:25 +01002022 && wp->w_lcs_chars.nbsp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002023 || (c == ' '
2024 && mb_l == 1
Bram Moolenaareed9d462021-02-15 20:38:25 +01002025 && wp->w_lcs_chars.space
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002026 && ptr - line >= leadcol
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002027 && ptr - line <= trailcol)))
2028 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002029 c = (c == ' ') ? wp->w_lcs_chars.space :
2030 wp->w_lcs_chars.nbsp;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002031 if (area_attr == 0 && search_attr == 0)
2032 {
2033 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002034 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002035 saved_attr2 = char_attr; // save current attr
2036 }
2037 mb_c = c;
2038 if (enc_utf8 && utf_char2len(c) > 1)
2039 {
2040 mb_utf8 = TRUE;
2041 u8cc[0] = 0;
2042 c = 0xc0;
2043 }
2044 else
2045 mb_utf8 = FALSE;
2046 }
2047
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002048 if ((trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
2049 || (leadcol != 0 && ptr < line + leadcol && c == ' '))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002050 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002051 c = (ptr > line + trailcol) ? wp->w_lcs_chars.trail
2052 : wp->w_lcs_chars.lead;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002053 if (!attr_pri)
2054 {
2055 n_attr = 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002056 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002057 saved_attr2 = char_attr; // save current attr
2058 }
2059 mb_c = c;
2060 if (enc_utf8 && utf_char2len(c) > 1)
2061 {
2062 mb_utf8 = TRUE;
2063 u8cc[0] = 0;
2064 c = 0xc0;
2065 }
2066 else
2067 mb_utf8 = FALSE;
2068 }
2069 }
2070
2071 // Handling of non-printable characters.
2072 if (!vim_isprintc(c))
2073 {
2074 // when getting a character from the file, we may have to
2075 // turn it into something else on the way to putting it
2076 // into "ScreenLines".
Bram Moolenaareed9d462021-02-15 20:38:25 +01002077 if (c == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002078 {
2079 int tab_len = 0;
2080 long vcol_adjusted = vcol; // removed showbreak length
2081#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01002082 char_u *sbr = get_showbreak_value(wp);
2083
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002084 // only adjust the tab_len, when at the first column
2085 // after the showbreak value was drawn
Bram Moolenaaree857022019-11-09 23:26:40 +01002086 if (*sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
2087 vcol_adjusted = vcol - MB_CHARLEN(sbr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002088#endif
2089 // tab amount depends on current column
2090#ifdef FEAT_VARTABS
2091 tab_len = tabstop_padding(vcol_adjusted,
2092 wp->w_buffer->b_p_ts,
2093 wp->w_buffer->b_p_vts_array) - 1;
2094#else
2095 tab_len = (int)wp->w_buffer->b_p_ts
2096 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
2097#endif
2098
2099#ifdef FEAT_LINEBREAK
2100 if (!wp->w_p_lbr || !wp->w_p_list)
2101#endif
2102 // tab amount depends on current column
2103 n_extra = tab_len;
2104#ifdef FEAT_LINEBREAK
2105 else
2106 {
2107 char_u *p;
2108 int len;
2109 int i;
2110 int saved_nextra = n_extra;
2111
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002112# ifdef FEAT_CONCEAL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002113 if (vcol_off > 0)
2114 // there are characters to conceal
2115 tab_len += vcol_off;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002116
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002117 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaareed9d462021-02-15 20:38:25 +01002118 if (wp->w_p_list && wp->w_lcs_chars.tab1
2119 && old_boguscols > 0
2120 && n_extra > tab_len)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002121 tab_len += n_extra - tab_len;
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002122# endif
2123 // If n_extra > 0, it gives the number of chars, to
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002124 // use for a tab, else we need to calculate the width
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002125 // for a tab.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002126 len = (tab_len * mb_char2len(wp->w_lcs_chars.tab2));
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002127 if (wp->w_lcs_chars.tab3)
2128 len += mb_char2len(wp->w_lcs_chars.tab3);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002129 if (n_extra > 0)
2130 len += n_extra - tab_len;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002131 c = wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002132 p = alloc(len + 1);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002133 if (p == NULL)
2134 n_extra = 0;
2135 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002136 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002137 vim_memset(p, ' ', len);
2138 p[len] = NUL;
2139 vim_free(p_extra_free);
2140 p_extra_free = p;
2141 for (i = 0; i < tab_len; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002142 {
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002143 int lcs = wp->w_lcs_chars.tab2;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002144
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002145 if (*p == NUL)
2146 {
2147 tab_len = i;
2148 break;
2149 }
2150
2151 // if tab3 is given, use it for the last char
2152 if (wp->w_lcs_chars.tab3 && i == tab_len - 1)
2153 lcs = wp->w_lcs_chars.tab3;
2154 p += mb_char2bytes(lcs, p);
2155 n_extra += mb_char2len(lcs)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002156 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar89a54b42021-09-07 20:45:31 +02002157 }
2158 p_extra = p_extra_free;
2159# ifdef FEAT_CONCEAL
2160 // n_extra will be increased by FIX_FOX_BOGUSCOLS
2161 // macro below, so need to adjust for that here
2162 if (vcol_off > 0)
2163 n_extra -= vcol_off;
2164# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002165 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002166 }
2167#endif
2168#ifdef FEAT_CONCEAL
2169 {
2170 int vc_saved = vcol_off;
2171
2172 // Tab alignment should be identical regardless of
2173 // 'conceallevel' value. So tab compensates of all
2174 // previous concealed characters, and thus resets
2175 // vcol_off and boguscols accumulated so far in the
2176 // line. Note that the tab can be longer than
2177 // 'tabstop' when there are concealed characters.
2178 FIX_FOR_BOGUSCOLS;
2179
2180 // Make sure, the highlighting for the tab char will be
2181 // correctly set further below (effectively reverts the
2182 // FIX_FOR_BOGSUCOLS macro
2183 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaareed9d462021-02-15 20:38:25 +01002184 && wp->w_lcs_chars.tab1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002185 tab_len += vc_saved;
2186 }
2187#endif
2188 mb_utf8 = FALSE; // don't draw as UTF-8
2189 if (wp->w_p_list)
2190 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002191 c = (n_extra == 0 && wp->w_lcs_chars.tab3)
2192 ? wp->w_lcs_chars.tab3
2193 : wp->w_lcs_chars.tab1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002194#ifdef FEAT_LINEBREAK
2195 if (wp->w_p_lbr)
2196 c_extra = NUL; // using p_extra from above
2197 else
2198#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01002199 c_extra = wp->w_lcs_chars.tab2;
2200 c_final = wp->w_lcs_chars.tab3;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002201 n_attr = tab_len + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002202 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002203 saved_attr2 = char_attr; // save current attr
2204 mb_c = c;
2205 if (enc_utf8 && utf_char2len(c) > 1)
2206 {
2207 mb_utf8 = TRUE;
2208 u8cc[0] = 0;
2209 c = 0xc0;
2210 }
2211 }
2212 else
2213 {
2214 c_final = NUL;
2215 c_extra = ' ';
2216 c = ' ';
2217 }
2218 }
2219 else if (c == NUL
2220 && (wp->w_p_list
2221 || ((fromcol >= 0 || fromcol_prev >= 0)
2222 && tocol > vcol
2223 && VIsual_mode != Ctrl_V
2224 && (
2225# ifdef FEAT_RIGHTLEFT
2226 wp->w_p_rl ? (col >= 0) :
2227# endif
2228 (col < wp->w_width))
2229 && !(noinvcur
2230 && lnum == wp->w_cursor.lnum
2231 && (colnr_T)vcol == wp->w_virtcol)))
2232 && lcs_eol_one > 0)
2233 {
2234 // Display a '$' after the line or highlight an extra
2235 // character if the line break is included.
2236#if defined(FEAT_DIFF) || defined(LINE_ATTR)
2237 // For a diff line the highlighting continues after the
2238 // "$".
2239 if (
2240# ifdef FEAT_DIFF
2241 diff_hlf == (hlf_T)0
2242# ifdef LINE_ATTR
2243 &&
2244# endif
2245# endif
2246# ifdef LINE_ATTR
2247 line_attr == 0
2248# endif
2249 )
2250#endif
2251 {
2252 // In virtualedit, visual selections may extend
2253 // beyond end of line.
2254 if (area_highlighting && virtual_active()
2255 && tocol != MAXCOL && vcol < tocol)
2256 n_extra = 0;
2257 else
2258 {
2259 p_extra = at_end_str;
2260 n_extra = 1;
2261 c_extra = NUL;
2262 c_final = NUL;
2263 }
2264 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002265 if (wp->w_p_list && wp->w_lcs_chars.eol > 0)
2266 c = wp->w_lcs_chars.eol;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002267 else
2268 c = ' ';
2269 lcs_eol_one = -1;
2270 --ptr; // put it back at the NUL
2271 if (!attr_pri)
2272 {
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002273 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002274 n_attr = 1;
2275 }
2276 mb_c = c;
2277 if (enc_utf8 && utf_char2len(c) > 1)
2278 {
2279 mb_utf8 = TRUE;
2280 u8cc[0] = 0;
2281 c = 0xc0;
2282 }
2283 else
2284 mb_utf8 = FALSE; // don't draw as UTF-8
2285 }
2286 else if (c != NUL)
2287 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02002288 p_extra = transchar_buf(wp->w_buffer, c);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002289 if (n_extra == 0)
2290 n_extra = byte2cells(c) - 1;
2291#ifdef FEAT_RIGHTLEFT
2292 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
2293 rl_mirror(p_extra); // reverse "<12>"
2294#endif
2295 c_extra = NUL;
2296 c_final = NUL;
2297#ifdef FEAT_LINEBREAK
2298 if (wp->w_p_lbr)
2299 {
2300 char_u *p;
2301
2302 c = *p_extra;
2303 p = alloc(n_extra + 1);
2304 vim_memset(p, ' ', n_extra);
2305 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
2306 p[n_extra] = NUL;
2307 vim_free(p_extra_free);
2308 p_extra_free = p_extra = p;
2309 }
2310 else
2311#endif
2312 {
2313 n_extra = byte2cells(c) - 1;
2314 c = *p_extra++;
2315 }
2316 if (!attr_pri)
2317 {
2318 n_attr = n_extra + 1;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002319 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_8));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002320 saved_attr2 = char_attr; // save current attr
2321 }
2322 mb_utf8 = FALSE; // don't draw as UTF-8
2323 }
2324 else if (VIsual_active
2325 && (VIsual_mode == Ctrl_V
2326 || VIsual_mode == 'v')
2327 && virtual_active()
2328 && tocol != MAXCOL
2329 && vcol < tocol
2330 && (
2331#ifdef FEAT_RIGHTLEFT
2332 wp->w_p_rl ? (col >= 0) :
2333#endif
2334 (col < wp->w_width)))
2335 {
2336 c = ' ';
2337 --ptr; // put it back at the NUL
2338 }
2339#if defined(LINE_ATTR)
2340 else if ((
2341# ifdef FEAT_DIFF
2342 diff_hlf != (hlf_T)0 ||
2343# endif
2344# ifdef FEAT_TERMINAL
2345 win_attr != 0 ||
2346# endif
2347 line_attr != 0
2348 ) && (
2349# ifdef FEAT_RIGHTLEFT
2350 wp->w_p_rl ? (col >= 0) :
2351# endif
2352 (col
2353# ifdef FEAT_CONCEAL
2354 - boguscols
2355# endif
2356 < wp->w_width)))
2357 {
2358 // Highlight until the right side of the window
2359 c = ' ';
2360 --ptr; // put it back at the NUL
2361
2362 // Remember we do the char for line highlighting.
2363 ++did_line_attr;
2364
2365 // don't do search HL for the rest of the line
2366 if (line_attr != 0 && char_attr == search_attr
2367 && (did_line_attr > 1
Bram Moolenaareed9d462021-02-15 20:38:25 +01002368 || (wp->w_p_list &&
2369 wp->w_lcs_chars.eol > 0)))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002370 char_attr = line_attr;
2371# ifdef FEAT_DIFF
2372 if (diff_hlf == HLF_TXD)
2373 {
2374 diff_hlf = HLF_CHD;
2375 if (vi_attr == 0 || char_attr != vi_attr)
2376 {
2377 char_attr = HL_ATTR(diff_hlf);
2378 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2379 && wp->w_p_culopt_flags != CULOPT_NBR
2380 && (!cul_screenline
2381 || (vcol >= left_curline_col
2382 && vcol <= right_curline_col)))
2383 char_attr = hl_combine_attr(
2384 char_attr, HL_ATTR(HLF_CUL));
2385 }
2386 }
2387# endif
2388# ifdef FEAT_TERMINAL
2389 if (win_attr != 0)
2390 {
2391 char_attr = win_attr;
Bram Moolenaara3817732019-10-16 16:31:44 +02002392 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
2393 && wp->w_p_culopt_flags != CULOPT_NBR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002394 {
2395 if (!cul_screenline || (vcol >= left_curline_col
2396 && vcol <= right_curline_col))
2397 char_attr = hl_combine_attr(
2398 char_attr, HL_ATTR(HLF_CUL));
2399 }
2400 else if (line_attr)
2401 char_attr = hl_combine_attr(char_attr, line_attr);
2402 }
2403# endif
2404 }
2405#endif
2406 }
2407
2408#ifdef FEAT_CONCEAL
2409 if ( wp->w_p_cole > 0
2410 && (wp != curwin || lnum != wp->w_cursor.lnum ||
2411 conceal_cursor_line(wp))
2412 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
2413 && !(lnum_in_visual_area
2414 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
2415 {
2416 char_attr = conceal_attr;
2417 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002418 && (syn_get_sub_char() != NUL
2419 || (has_match_conc && match_conc)
2420 || wp->w_p_cole == 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002421 && wp->w_p_cole != 3)
2422 {
2423 // First time at this concealed item: display one
2424 // character.
Bram Moolenaar211dd3f2020-06-25 20:07:04 +02002425 if (has_match_conc && match_conc)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002426 c = match_conc;
2427 else if (syn_get_sub_char() != NUL)
2428 c = syn_get_sub_char();
Bram Moolenaareed9d462021-02-15 20:38:25 +01002429 else if (wp->w_lcs_chars.conceal != NUL)
2430 c = wp->w_lcs_chars.conceal;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002431 else
2432 c = ' ';
2433
2434 prev_syntax_id = syntax_seqnr;
2435
2436 if (n_extra > 0)
2437 vcol_off += n_extra;
2438 vcol += n_extra;
2439 if (wp->w_p_wrap && n_extra > 0)
2440 {
2441# ifdef FEAT_RIGHTLEFT
2442 if (wp->w_p_rl)
2443 {
2444 col -= n_extra;
2445 boguscols -= n_extra;
2446 }
2447 else
2448# endif
2449 {
2450 boguscols += n_extra;
2451 col += n_extra;
2452 }
2453 }
2454 n_extra = 0;
2455 n_attr = 0;
2456 }
2457 else if (n_skip == 0)
2458 {
2459 is_concealing = TRUE;
2460 n_skip = 1;
2461 }
2462 mb_c = c;
2463 if (enc_utf8 && utf_char2len(c) > 1)
2464 {
2465 mb_utf8 = TRUE;
2466 u8cc[0] = 0;
2467 c = 0xc0;
2468 }
2469 else
2470 mb_utf8 = FALSE; // don't draw as UTF-8
2471 }
2472 else
2473 {
2474 prev_syntax_id = 0;
2475 is_concealing = FALSE;
2476 }
2477
2478 if (n_skip > 0 && did_decrement_ptr)
2479 // not showing the '>', put pointer back to avoid getting stuck
2480 ++ptr;
2481
2482#endif // FEAT_CONCEAL
2483 }
2484
2485#ifdef FEAT_CONCEAL
2486 // In the cursor line and we may be concealing characters: correct
2487 // the cursor column when we reach its position.
2488 if (!did_wcol && draw_state == WL_LINE
2489 && wp == curwin && lnum == wp->w_cursor.lnum
2490 && conceal_cursor_line(wp)
2491 && (int)wp->w_virtcol <= vcol + n_skip)
2492 {
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002493# ifdef FEAT_RIGHTLEFT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002494 if (wp->w_p_rl)
2495 wp->w_wcol = wp->w_width - col + boguscols - 1;
2496 else
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002497# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002498 wp->w_wcol = col - boguscols;
2499 wp->w_wrow = row;
2500 did_wcol = TRUE;
2501 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002502# ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +01002503 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar1efefda2020-11-17 19:22:06 +01002504# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002505 }
2506#endif
2507
2508 // Don't override visual selection highlighting.
2509 if (n_attr > 0
2510 && draw_state == WL_LINE
2511 && !attr_pri)
2512 {
2513#ifdef LINE_ATTR
2514 if (line_attr)
2515 char_attr = hl_combine_attr(extra_attr, line_attr);
2516 else
2517#endif
2518 char_attr = extra_attr;
2519 }
2520
2521#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2522 // XIM don't send preedit_start and preedit_end, but they send
2523 // preedit_changed and commit. Thus Vim can't set "im_is_active", use
2524 // im_is_preediting() here.
2525 if (p_imst == IM_ON_THE_SPOT
2526 && xic != NULL
2527 && lnum == wp->w_cursor.lnum
2528 && (State & INSERT)
2529 && !p_imdisable
2530 && im_is_preediting()
2531 && draw_state == WL_LINE)
2532 {
2533 colnr_T tcol;
2534
2535 if (preedit_end_col == MAXCOL)
2536 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
2537 else
2538 tcol = preedit_end_col;
2539 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
2540 {
2541 if (feedback_old_attr < 0)
2542 {
2543 feedback_col = 0;
2544 feedback_old_attr = char_attr;
2545 }
2546 char_attr = im_get_feedback_attr(feedback_col);
2547 if (char_attr < 0)
2548 char_attr = feedback_old_attr;
2549 feedback_col++;
2550 }
2551 else if (feedback_old_attr >= 0)
2552 {
2553 char_attr = feedback_old_attr;
2554 feedback_old_attr = -1;
2555 feedback_col = 0;
2556 }
2557 }
2558#endif
2559 // Handle the case where we are in column 0 but not on the first
2560 // character of the line and the user wants us to show us a
2561 // special character (via 'listchars' option "precedes:<char>".
2562 if (lcs_prec_todo != NUL
2563 && wp->w_p_list
Bram Moolenaarbffba7f2019-09-20 17:00:17 +02002564 && (wp->w_p_wrap ?
2565 (wp->w_skipcol > 0 && row == 0) :
2566 wp->w_leftcol > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002567#ifdef FEAT_DIFF
2568 && filler_todo <= 0
2569#endif
2570 && draw_state > WL_NR
2571 && c != NUL)
2572 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002573 c = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002574 lcs_prec_todo = NUL;
2575 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2576 {
2577 // Double-width character being overwritten by the "precedes"
2578 // character, need to fill up half the character.
2579 c_extra = MB_FILLER_CHAR;
2580 c_final = NUL;
2581 n_extra = 1;
2582 n_attr = 2;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002583 extra_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002584 }
2585 mb_c = c;
2586 if (enc_utf8 && utf_char2len(c) > 1)
2587 {
2588 mb_utf8 = TRUE;
2589 u8cc[0] = 0;
2590 c = 0xc0;
2591 }
2592 else
2593 mb_utf8 = FALSE; // don't draw as UTF-8
2594 if (!attr_pri)
2595 {
2596 saved_attr3 = char_attr; // save current attr
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002597 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002598 n_attr3 = 1;
2599 }
2600 }
2601
2602 // At end of the text line or just after the last character.
2603 if ((c == NUL
2604#if defined(LINE_ATTR)
2605 || did_line_attr == 1
2606#endif
2607 ) && eol_hl_off == 0)
2608 {
2609#ifdef FEAT_SEARCH_EXTRA
2610 // flag to indicate whether prevcol equals startcol of search_hl or
2611 // one of the matches
2612 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &screen_search_hl,
2613 (long)(ptr - line) - (c == NUL));
2614#endif
2615 // Invert at least one char, used for Visual and empty line or
2616 // highlight match at end of line. If it's beyond the last
2617 // char on the screen, just overwrite that one (tricky!) Not
2618 // needed when a '$' was displayed for 'list'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002619 if (wp->w_lcs_chars.eol == lcs_eol_one
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002620 && ((area_attr != 0 && vcol == fromcol
2621 && (VIsual_mode != Ctrl_V
2622 || lnum == VIsual.lnum
2623 || lnum == curwin->w_cursor.lnum)
2624 && c == NUL)
2625#ifdef FEAT_SEARCH_EXTRA
2626 // highlight 'hlsearch' match at end of line
2627 || (prevcol_hl_flag
2628# ifdef FEAT_SYN_HL
2629 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
2630 && !(wp == curwin && VIsual_active))
2631# endif
2632# ifdef FEAT_DIFF
2633 && diff_hlf == (hlf_T)0
2634# endif
2635# if defined(LINE_ATTR)
2636 && did_line_attr <= 1
2637# endif
2638 )
2639#endif
2640 ))
2641 {
2642 int n = 0;
2643
2644#ifdef FEAT_RIGHTLEFT
2645 if (wp->w_p_rl)
2646 {
2647 if (col < 0)
2648 n = 1;
2649 }
2650 else
2651#endif
2652 {
2653 if (col >= wp->w_width)
2654 n = -1;
2655 }
2656 if (n != 0)
2657 {
2658 // At the window boundary, highlight the last character
2659 // instead (better than nothing).
2660 off += n;
2661 col += n;
2662 }
2663 else
2664 {
2665 // Add a blank character to highlight.
2666 ScreenLines[off] = ' ';
2667 if (enc_utf8)
2668 ScreenLinesUC[off] = 0;
2669 }
2670#ifdef FEAT_SEARCH_EXTRA
2671 if (area_attr == 0)
2672 {
2673 // Use attributes from match with highest priority among
2674 // 'search_hl' and the match list.
2675 get_search_match_hl(wp, &screen_search_hl,
2676 (long)(ptr - line), &char_attr);
2677 }
2678#endif
2679 ScreenAttrs[off] = char_attr;
2680#ifdef FEAT_RIGHTLEFT
2681 if (wp->w_p_rl)
2682 {
2683 --col;
2684 --off;
2685 }
2686 else
2687#endif
2688 {
2689 ++col;
2690 ++off;
2691 }
2692 ++vcol;
2693 eol_hl_off = 1;
2694 }
2695 }
2696
2697 // At end of the text line.
2698 if (c == NUL)
2699 {
2700#ifdef FEAT_SYN_HL
2701 // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
2702 if (wp->w_p_wrap)
2703 v = wp->w_skipcol;
2704 else
2705 v = wp->w_leftcol;
2706
2707 // check if line ends before left margin
2708 if (vcol < v + col - win_col_off(wp))
2709 vcol = v + col - win_col_off(wp);
2710#ifdef FEAT_CONCEAL
2711 // Get rid of the boguscols now, we want to draw until the right
2712 // edge for 'cursorcolumn'.
2713 col -= boguscols;
2714 boguscols = 0;
2715#endif
2716
2717 if (draw_color_col)
2718 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2719
2720 if (((wp->w_p_cuc
2721 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
2722 && (int)wp->w_virtcol <
2723 wp->w_width * (row - startrow + 1) + v
2724 && lnum != wp->w_cursor.lnum)
2725 || draw_color_col
2726 || win_attr != 0)
2727# ifdef FEAT_RIGHTLEFT
2728 && !wp->w_p_rl
2729# endif
2730 )
2731 {
2732 int rightmost_vcol = 0;
2733 int i;
2734
2735 if (wp->w_p_cuc)
2736 rightmost_vcol = wp->w_virtcol;
2737 if (draw_color_col)
2738 // determine rightmost colorcolumn to possibly draw
2739 for (i = 0; color_cols[i] >= 0; ++i)
2740 if (rightmost_vcol < color_cols[i])
2741 rightmost_vcol = color_cols[i];
2742
2743 while (col < wp->w_width)
2744 {
2745 ScreenLines[off] = ' ';
2746 if (enc_utf8)
2747 ScreenLinesUC[off] = 0;
2748 ++col;
2749 if (draw_color_col)
2750 draw_color_col = advance_color_col(VCOL_HLC,
2751 &color_cols);
2752
2753 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
2754 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
2755 else if (draw_color_col && VCOL_HLC == *color_cols)
2756 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
2757 else
2758 ScreenAttrs[off++] = win_attr;
2759
2760 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
2761 break;
2762
2763 ++vcol;
2764 }
2765 }
2766#endif
2767
2768 screen_line(screen_row, wp->w_wincol, col,
2769 (int)wp->w_width, screen_line_flags);
2770 row++;
2771
2772 // Update w_cline_height and w_cline_folded if the cursor line was
2773 // updated (saves a call to plines() later).
2774 if (wp == curwin && lnum == curwin->w_cursor.lnum)
2775 {
2776 curwin->w_cline_row = startrow;
2777 curwin->w_cline_height = row - startrow;
2778#ifdef FEAT_FOLDING
2779 curwin->w_cline_folded = FALSE;
2780#endif
2781 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2782 }
2783
2784 break;
2785 }
2786
2787 // Show "extends" character from 'listchars' if beyond the line end and
2788 // 'list' is set.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002789 if (wp->w_lcs_chars.ext != NUL
Bram Moolenaar41fb7232021-07-08 12:40:05 +02002790 && draw_state == WL_LINE
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002791 && wp->w_p_list
2792 && !wp->w_p_wrap
2793#ifdef FEAT_DIFF
2794 && filler_todo <= 0
2795#endif
2796 && (
2797#ifdef FEAT_RIGHTLEFT
2798 wp->w_p_rl ? col == 0 :
2799#endif
2800 col == wp->w_width - 1)
2801 && (*ptr != NUL
2802 || (wp->w_p_list && lcs_eol_one > 0)
2803 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
2804 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002805 c = wp->w_lcs_chars.ext;
Bram Moolenaar42e931b2019-12-04 19:08:50 +01002806 char_attr = hl_combine_attr(win_attr, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002807 mb_c = c;
2808 if (enc_utf8 && utf_char2len(c) > 1)
2809 {
2810 mb_utf8 = TRUE;
2811 u8cc[0] = 0;
2812 c = 0xc0;
2813 }
2814 else
2815 mb_utf8 = FALSE;
2816 }
2817
2818#ifdef FEAT_SYN_HL
2819 // advance to the next 'colorcolumn'
2820 if (draw_color_col)
2821 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2822
2823 // Highlight the cursor column if 'cursorcolumn' is set. But don't
2824 // highlight the cursor position itself.
2825 // Also highlight the 'colorcolumn' if it is different than
2826 // 'cursorcolumn'
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002827 // Also highlight the 'colorcolumn' if 'breakindent' and/or 'showbreak'
2828 // options are set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002829 vcol_save_attr = -1;
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002830 if (((draw_state == WL_LINE ||
Bram Moolenaarad5e5632020-09-15 20:52:26 +02002831 draw_state == WL_BRI ||
2832 draw_state == WL_SBR) && !lnum_in_visual_area
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002833 && search_attr == 0 && area_attr == 0)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01002834# ifdef FEAT_DIFF
2835 && filler_todo <= 0
2836# endif
2837 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002838 {
2839 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
2840 && lnum != wp->w_cursor.lnum)
2841 {
2842 vcol_save_attr = char_attr;
2843 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
2844 }
2845 else if (draw_color_col && VCOL_HLC == *color_cols)
2846 {
2847 vcol_save_attr = char_attr;
2848 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
2849 }
2850 }
2851#endif
2852
2853 // Store character to be displayed.
2854 // Skip characters that are left of the screen for 'nowrap'.
2855 vcol_prev = vcol;
2856 if (draw_state < WL_LINE || n_skip <= 0)
2857 {
2858 // Store the character.
2859#if defined(FEAT_RIGHTLEFT)
2860 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
2861 {
2862 // A double-wide character is: put first halve in left cell.
2863 --off;
2864 --col;
2865 }
2866#endif
2867 ScreenLines[off] = c;
2868 if (enc_dbcs == DBCS_JPNU)
2869 {
2870 if ((mb_c & 0xff00) == 0x8e00)
2871 ScreenLines[off] = 0x8e;
2872 ScreenLines2[off] = mb_c & 0xff;
2873 }
2874 else if (enc_utf8)
2875 {
2876 if (mb_utf8)
2877 {
2878 int i;
2879
2880 ScreenLinesUC[off] = mb_c;
2881 if ((c & 0xff) == 0)
2882 ScreenLines[off] = 0x80; // avoid storing zero
2883 for (i = 0; i < Screen_mco; ++i)
2884 {
2885 ScreenLinesC[i][off] = u8cc[i];
2886 if (u8cc[i] == 0)
2887 break;
2888 }
2889 }
2890 else
2891 ScreenLinesUC[off] = 0;
2892 }
2893 if (multi_attr)
2894 {
2895 ScreenAttrs[off] = multi_attr;
2896 multi_attr = 0;
2897 }
2898 else
2899 ScreenAttrs[off] = char_attr;
2900
2901 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2902 {
2903 // Need to fill two screen columns.
2904 ++off;
2905 ++col;
2906 if (enc_utf8)
2907 // UTF-8: Put a 0 in the second screen char.
2908 ScreenLines[off] = 0;
2909 else
2910 // DBCS: Put second byte in the second screen char.
2911 ScreenLines[off] = mb_c & 0xff;
2912 if (draw_state > WL_NR
2913#ifdef FEAT_DIFF
2914 && filler_todo <= 0
2915#endif
2916 )
2917 ++vcol;
2918 // When "tocol" is halfway a character, set it to the end of
2919 // the character, otherwise highlighting won't stop.
2920 if (tocol == vcol)
2921 ++tocol;
2922#ifdef FEAT_RIGHTLEFT
2923 if (wp->w_p_rl)
2924 {
2925 // now it's time to backup one cell
2926 --off;
2927 --col;
2928 }
2929#endif
2930 }
2931#ifdef FEAT_RIGHTLEFT
2932 if (wp->w_p_rl)
2933 {
2934 --off;
2935 --col;
2936 }
2937 else
2938#endif
2939 {
2940 ++off;
2941 ++col;
2942 }
2943 }
2944#ifdef FEAT_CONCEAL
2945 else if (wp->w_p_cole > 0 && is_concealing)
2946 {
2947 --n_skip;
2948 ++vcol_off;
2949 if (n_extra > 0)
2950 vcol_off += n_extra;
2951 if (wp->w_p_wrap)
2952 {
2953 // Special voodoo required if 'wrap' is on.
2954 //
2955 // Advance the column indicator to force the line
2956 // drawing to wrap early. This will make the line
2957 // take up the same screen space when parts are concealed,
2958 // so that cursor line computations aren't messed up.
2959 //
2960 // To avoid the fictitious advance of 'col' causing
2961 // trailing junk to be written out of the screen line
2962 // we are building, 'boguscols' keeps track of the number
2963 // of bad columns we have advanced.
2964 if (n_extra > 0)
2965 {
2966 vcol += n_extra;
2967# ifdef FEAT_RIGHTLEFT
2968 if (wp->w_p_rl)
2969 {
2970 col -= n_extra;
2971 boguscols -= n_extra;
2972 }
2973 else
2974# endif
2975 {
2976 col += n_extra;
2977 boguscols += n_extra;
2978 }
2979 n_extra = 0;
2980 n_attr = 0;
2981 }
2982
2983
2984 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
2985 {
2986 // Need to fill two screen columns.
2987# ifdef FEAT_RIGHTLEFT
2988 if (wp->w_p_rl)
2989 {
2990 --boguscols;
2991 --col;
2992 }
2993 else
2994# endif
2995 {
2996 ++boguscols;
2997 ++col;
2998 }
2999 }
3000
3001# ifdef FEAT_RIGHTLEFT
3002 if (wp->w_p_rl)
3003 {
3004 --boguscols;
3005 --col;
3006 }
3007 else
3008# endif
3009 {
3010 ++boguscols;
3011 ++col;
3012 }
3013 }
3014 else
3015 {
3016 if (n_extra > 0)
3017 {
3018 vcol += n_extra;
3019 n_extra = 0;
3020 n_attr = 0;
3021 }
3022 }
3023
3024 }
3025#endif // FEAT_CONCEAL
3026 else
3027 --n_skip;
3028
3029 // Only advance the "vcol" when after the 'number' or 'relativenumber'
3030 // column.
3031 if (draw_state > WL_NR
3032#ifdef FEAT_DIFF
3033 && filler_todo <= 0
3034#endif
3035 )
3036 ++vcol;
3037
3038#ifdef FEAT_SYN_HL
3039 if (vcol_save_attr >= 0)
3040 char_attr = vcol_save_attr;
3041#endif
3042
3043 // restore attributes after "predeces" in 'listchars'
3044 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
3045 char_attr = saved_attr3;
3046
3047 // restore attributes after last 'listchars' or 'number' char
3048 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
3049 char_attr = saved_attr2;
3050
3051 // At end of screen line and there is more to come: Display the line
3052 // so far. If there is no more to display it is caught above.
3053 if ((
3054#ifdef FEAT_RIGHTLEFT
3055 wp->w_p_rl ? (col < 0) :
3056#endif
3057 (col >= wp->w_width))
Bram Moolenaar41fb7232021-07-08 12:40:05 +02003058 && (draw_state != WL_LINE
3059 || *ptr != NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003060#ifdef FEAT_DIFF
3061 || filler_todo > 0
3062#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01003063 || (wp->w_p_list && wp->w_lcs_chars.eol != NUL
3064 && p_extra != at_end_str)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003065 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
3066 )
3067 {
3068#ifdef FEAT_CONCEAL
3069 screen_line(screen_row, wp->w_wincol, col - boguscols,
3070 (int)wp->w_width, screen_line_flags);
3071 boguscols = 0;
3072#else
3073 screen_line(screen_row, wp->w_wincol, col,
3074 (int)wp->w_width, screen_line_flags);
3075#endif
3076 ++row;
3077 ++screen_row;
3078
3079 // When not wrapping and finished diff lines, or when displayed
3080 // '$' and highlighting until last column, break here.
3081 if ((!wp->w_p_wrap
3082#ifdef FEAT_DIFF
3083 && filler_todo <= 0
3084#endif
3085 ) || lcs_eol_one == -1)
3086 break;
3087
3088 // When the window is too narrow draw all "@" lines.
3089 if (draw_state != WL_LINE
3090#ifdef FEAT_DIFF
3091 && filler_todo <= 0
3092#endif
3093 )
3094 {
3095 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
3096 draw_vsep_win(wp, row);
3097 row = endrow;
3098 }
3099
3100 // When line got too long for screen break here.
3101 if (row == endrow)
3102 {
3103 ++row;
3104 break;
3105 }
3106
3107 if (screen_cur_row == screen_row - 1
3108#ifdef FEAT_DIFF
3109 && filler_todo <= 0
3110#endif
3111 && wp->w_width == Columns)
3112 {
3113 // Remember that the line wraps, used for modeless copy.
3114 LineWraps[screen_row - 1] = TRUE;
3115
3116 // Special trick to make copy/paste of wrapped lines work with
3117 // xterm/screen: write an extra character beyond the end of
3118 // the line. This will work with all terminal types
3119 // (regardless of the xn,am settings).
3120 // Only do this on a fast tty.
3121 // Only do this if the cursor is on the current line
3122 // (something has been written in it).
3123 // Don't do this for the GUI.
3124 // Don't do this for double-width characters.
3125 // Don't do this for a window not at the right screen border.
3126 if (p_tf
3127#ifdef FEAT_GUI
3128 && !gui.in_use
3129#endif
3130 && !(has_mbyte
3131 && ((*mb_off2cells)(LineOffset[screen_row],
3132 LineOffset[screen_row] + screen_Columns)
3133 == 2
3134 || (*mb_off2cells)(LineOffset[screen_row - 1]
3135 + (int)Columns - 2,
3136 LineOffset[screen_row] + screen_Columns)
3137 == 2)))
3138 {
3139 // First make sure we are at the end of the screen line,
3140 // then output the same character again to let the
3141 // terminal know about the wrap. If the terminal doesn't
3142 // auto-wrap, we overwrite the character.
3143 if (screen_cur_col != wp->w_width)
3144 screen_char(LineOffset[screen_row - 1]
3145 + (unsigned)Columns - 1,
3146 screen_row - 1, (int)(Columns - 1));
3147
3148 // When there is a multi-byte character, just output a
3149 // space to keep it simple.
3150 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
3151 screen_row - 1] + (Columns - 1)]) > 1)
3152 out_char(' ');
3153 else
3154 out_char(ScreenLines[LineOffset[screen_row - 1]
3155 + (Columns - 1)]);
3156 // force a redraw of the first char on the next line
3157 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
3158 screen_start(); // don't know where cursor is now
3159 }
3160 }
3161
3162 col = 0;
3163 off = (unsigned)(current_ScreenLine - ScreenLines);
3164#ifdef FEAT_RIGHTLEFT
3165 if (wp->w_p_rl)
3166 {
3167 col = wp->w_width - 1; // col is not used if breaking!
3168 off += col;
3169 }
3170#endif
3171
3172 // reset the drawing state for the start of a wrapped line
3173 draw_state = WL_START;
3174 saved_n_extra = n_extra;
3175 saved_p_extra = p_extra;
3176 saved_c_extra = c_extra;
3177 saved_c_final = c_final;
3178#ifdef FEAT_SYN_HL
3179 if (!(cul_screenline
3180# ifdef FEAT_DIFF
Bram Moolenaare7705982020-04-21 22:23:15 +02003181 && diff_hlf == (hlf_T)0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003182# endif
Bram Moolenaare7705982020-04-21 22:23:15 +02003183 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003184 saved_char_attr = char_attr;
3185 else
3186#endif
3187 saved_char_attr = 0;
3188 n_extra = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01003189 lcs_prec_todo = wp->w_lcs_chars.prec;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003190#ifdef FEAT_LINEBREAK
3191# ifdef FEAT_DIFF
3192 if (filler_todo <= 0)
3193# endif
3194 need_showbreak = TRUE;
3195#endif
3196#ifdef FEAT_DIFF
3197 --filler_todo;
3198 // When the filler lines are actually below the last line of the
3199 // file, don't draw the line itself, break here.
3200 if (filler_todo == 0 && wp->w_botfill)
3201 break;
3202#endif
3203 }
3204
3205 } // for every character in the line
3206
3207#ifdef FEAT_SPELL
3208 // After an empty line check first word for capital.
3209 if (*skipwhite(line) == NUL)
3210 {
3211 capcol_lnum = lnum + 1;
3212 cap_col = 0;
3213 }
3214#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003215#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003216 vim_free(text_props);
3217 vim_free(text_prop_idxs);
3218#endif
3219
3220 vim_free(p_extra_free);
3221 return row;
3222}