forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawline.c
4099 lines (3834 loc) · 108 KB
/
drawline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* drawline.c: Functions for drawing window lines on the screen.
* This is the middle level, drawscreen.c is the higher level and screen.c the
* lower level.
*/
#include "vim.h"
#ifdef FEAT_SYN_HL
/*
* Advance **color_cols and return TRUE when there are columns to draw.
*/
static int
advance_color_col(int vcol, int **color_cols)
{
while (**color_cols >= 0 && vcol > **color_cols)
++*color_cols;
return (**color_cols >= 0);
}
#endif
#ifdef FEAT_SYN_HL
/*
* Used when 'cursorlineopt' contains "screenline": compute the margins between
* which the highlighting is used.
*/
static void
margin_columns_win(win_T *wp, int *left_col, int *right_col)
{
// cache previous calculations depending on w_virtcol
static int saved_w_virtcol;
static win_T *prev_wp;
static int prev_left_col;
static int prev_right_col;
static int prev_col_off;
int cur_col_off = win_col_off(wp);
int width1;
int width2;
if (saved_w_virtcol == wp->w_virtcol
&& prev_wp == wp && prev_col_off == cur_col_off)
{
*right_col = prev_right_col;
*left_col = prev_left_col;
return;
}
width1 = wp->w_width - cur_col_off;
width2 = width1 + win_col_off2(wp);
*left_col = 0;
*right_col = width1;
if (wp->w_virtcol >= (colnr_T)width1)
*right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0)
*left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
// cache values
prev_left_col = *left_col;
prev_right_col = *right_col;
prev_wp = wp;
saved_w_virtcol = wp->w_virtcol;
prev_col_off = cur_col_off;
}
#endif
#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
|| defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
// using an attribute for the whole line
# define LINE_ATTR
#endif
// structure with variables passed between win_line() and other functions
typedef struct {
int draw_state; // what to draw next
linenr_T lnum; // line number to be drawn
int startrow; // first row in the window to be drawn
int row; // row in the window, excl w_winrow
int screen_row; // row on the screen, incl w_winrow
long vcol; // virtual column, before wrapping
int col; // visual column on screen, after wrapping
#ifdef FEAT_CONCEAL
int boguscols; // nonexistent columns added to "col" to force
// wrapping
int vcol_off_co; // offset for concealed characters
#endif
int vcol_off_tp; // offset for virtual text
#ifdef FEAT_SYN_HL
int draw_color_col; // highlight colorcolumn
int *color_cols; // pointer to according columns array
#endif
int eol_hl_off; // 1 if highlighted char after EOL
unsigned off; // offset in ScreenLines/ScreenAttrs
int win_attr; // background for the whole window, except
// margins and "~" lines.
int wcr_attr; // attributes from 'wincolor'
#ifdef FEAT_SYN_HL
int cul_attr; // set when 'cursorline' active
#endif
#ifdef LINE_ATTR
int line_attr; // for the whole line, includes 'cursorline'
#endif
int screen_line_flags; // flags for screen_line()
int fromcol; // start of inverting
int tocol; // end of inverting
#ifdef FEAT_LINEBREAK
long vcol_sbr; // virtual column after showbreak
int need_showbreak; // overlong line, skipping first x chars
int dont_use_showbreak; // do not use 'showbreak'
#endif
#ifdef FEAT_PROP_POPUP
int text_prop_above_count;
#endif
// TRUE when 'cursorlineopt' has "screenline" and cursor is in this line
int cul_screenline;
int char_attr; // attributes for the next character
int n_extra; // number of extra bytes
char_u *p_extra; // string of extra chars, plus NUL, only used
// when c_extra and c_final are NUL
char_u *p_extra_free; // p_extra buffer that needs to be freed
int extra_attr; // attributes for p_extra, should be combined
// with win_attr if needed
int n_attr_skip; // chars to skip before using extra_attr
int c_extra; // extra chars, all the same
int c_final; // final char, mandatory if set
int extra_for_textprop; // wlv.n_extra set for textprop
// saved "extra" items for when draw_state becomes WL_LINE (again)
int saved_n_extra;
char_u *saved_p_extra;
int saved_extra_attr;
int saved_n_attr_skip;
int saved_extra_for_textprop;
int saved_c_extra;
int saved_c_final;
int saved_char_attr;
char_u extra[NUMBUFLEN + MB_MAXBYTES];
// "%ld " and 'fdc' must fit in here, as well
// any text sign
#ifdef FEAT_DIFF
hlf_T diff_hlf; // type of diff highlighting
#endif
int filler_lines; // nr of filler lines to be drawn
int filler_todo; // nr of filler lines still to do + 1
#ifdef FEAT_SIGNS
sign_attrs_T sattr;
#endif
} winlinevars_T;
// draw_state values for items that are drawn in sequence:
#define WL_START 0 // nothing done yet, must be zero
#define WL_CMDLINE (WL_START + 1) // cmdline window column
#ifdef FEAT_FOLDING
# define WL_FOLD (WL_CMDLINE + 1) // 'foldcolumn'
#else
# define WL_FOLD WL_CMDLINE
#endif
#ifdef FEAT_SIGNS
# define WL_SIGN (WL_FOLD + 1) // column for signs
#else
# define WL_SIGN WL_FOLD // column for signs
#endif
#define WL_NR (WL_SIGN + 1) // line number
#ifdef FEAT_LINEBREAK
# define WL_BRI (WL_NR + 1) // 'breakindent'
#else
# define WL_BRI WL_NR
#endif
#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
# define WL_SBR (WL_BRI + 1) // 'showbreak' or 'diff'
#else
# define WL_SBR WL_BRI
#endif
#define WL_LINE (WL_SBR + 1) // text in the line
#if defined(FEAT_SIGNS) || defined(FEAT_FOLDING)
/*
* Return TRUE if CursorLineSign highlight is to be used.
*/
static int
use_cursor_line_highlight(win_T *wp, linenr_T lnum)
{
return wp->w_p_cul
&& lnum == wp->w_cursor.lnum
&& (wp->w_p_culopt_flags & CULOPT_NBR);
}
#endif
#ifdef FEAT_FOLDING
/*
* Setup for drawing the 'foldcolumn', if there is one.
*/
static void
handle_foldcolumn(win_T *wp, winlinevars_T *wlv)
{
int fdc = compute_foldcolumn(wp, 0);
if (fdc <= 0)
return;
// Allocate a buffer, "wlv->extra[]" may already be in use.
vim_free(wlv->p_extra_free);
wlv->p_extra_free = alloc(MAX_MCO * fdc + 1);
if (wlv->p_extra_free == NULL)
return;
wlv->n_extra = (int)fill_foldcolumn(wlv->p_extra_free,
wp, FALSE, wlv->lnum);
wlv->p_extra_free[wlv->n_extra] = NUL;
wlv->p_extra = wlv->p_extra_free;
wlv->c_extra = NUL;
wlv->c_final = NUL;
if (use_cursor_line_highlight(wp, wlv->lnum))
wlv->char_attr = hl_combine_attr(wlv->wcr_attr, HL_ATTR(HLF_CLF));
else
wlv->char_attr = hl_combine_attr(wlv->wcr_attr, HL_ATTR(HLF_FC));
}
#endif
#ifdef FEAT_SIGNS
/*
* Get information needed to display the sign in line "wlv->lnum" in window
* "wp".
* If "nrcol" is TRUE, the sign is going to be displayed in the number column.
* Otherwise the sign is going to be displayed in the sign column.
*/
static void
get_sign_display_info(
int nrcol,
win_T *wp,
winlinevars_T *wlv)
{
int text_sign;
# ifdef FEAT_SIGN_ICONS
int icon_sign;
# endif
// Draw two cells with the sign value or blank.
wlv->c_extra = ' ';
wlv->c_final = NUL;
if (nrcol)
wlv->n_extra = number_width(wp) + 1;
else
{
if (use_cursor_line_highlight(wp, wlv->lnum))
wlv->char_attr = hl_combine_attr(wlv->wcr_attr, HL_ATTR(HLF_CLS));
else
wlv->char_attr = hl_combine_attr(wlv->wcr_attr, HL_ATTR(HLF_SC));
wlv->n_extra = 2;
}
if (wlv->row == wlv->startrow
#ifdef FEAT_DIFF
+ wlv->filler_lines && wlv->filler_todo <= 0
#endif
)
{
text_sign = (wlv->sattr.sat_text != NULL) ? wlv->sattr.sat_typenr : 0;
# ifdef FEAT_SIGN_ICONS
icon_sign = (wlv->sattr.sat_icon != NULL) ? wlv->sattr.sat_typenr : 0;
if (gui.in_use && icon_sign != 0)
{
// Use the image in this position.
if (nrcol)
{
wlv->c_extra = NUL;
sprintf((char *)wlv->extra, "%-*c ",
number_width(wp), SIGN_BYTE);
wlv->p_extra = wlv->extra;
wlv->n_extra = (int)STRLEN(wlv->p_extra);
}
else
wlv->c_extra = SIGN_BYTE;
# ifdef FEAT_NETBEANS_INTG
if (netbeans_active() && (buf_signcount(wp->w_buffer, wlv->lnum)
> 1))
{
if (nrcol)
{
wlv->c_extra = NUL;
sprintf((char *)wlv->extra, "%-*c ", number_width(wp),
MULTISIGN_BYTE);
wlv->p_extra = wlv->extra;
wlv->n_extra = (int)STRLEN(wlv->p_extra);
}
else
wlv->c_extra = MULTISIGN_BYTE;
}
# endif
wlv->c_final = NUL;
wlv->char_attr = icon_sign;
}
else
# endif
if (text_sign != 0)
{
wlv->p_extra = wlv->sattr.sat_text;
if (wlv->p_extra != NULL)
{
if (nrcol)
{
int width = number_width(wp) - 2;
int n;
for (n = 0; n < width; n++)
wlv->extra[n] = ' ';
vim_snprintf((char *)wlv->extra + n,
sizeof(wlv->extra) - n, "%s ", wlv->p_extra);
wlv->p_extra = wlv->extra;
}
wlv->c_extra = NUL;
wlv->c_final = NUL;
wlv->n_extra = (int)STRLEN(wlv->p_extra);
}
if (use_cursor_line_highlight(wp, wlv->lnum)
&& wlv->sattr.sat_culhl > 0)
wlv->char_attr = wlv->sattr.sat_culhl;
else
wlv->char_attr = wlv->sattr.sat_texthl;
}
}
}
#endif
/*
* Display the absolute or relative line number. After the first row fill with
* blanks when the 'n' flag isn't in 'cpo'.
*/
static void
handle_lnum_col(
win_T *wp,
winlinevars_T *wlv,
int sign_present UNUSED,
int num_attr UNUSED)
{
int has_cpo_n = vim_strchr(p_cpo, CPO_NUMCOL) != NULL;
int lnum_row = wlv->startrow + wlv->filler_lines
#ifdef FEAT_PROP_POPUP
+ wlv->text_prop_above_count
#endif
;
if ((wp->w_p_nu || wp->w_p_rnu)
&& (wlv->row <= lnum_row || !has_cpo_n)
// there is no line number in a wrapped line when "n" is in
// 'cpoptions', but 'breakindent' assumes it anyway.
&& !((has_cpo_n
#ifdef FEAT_LINEBREAK
&& !wp->w_p_bri
#endif
) && wp->w_skipcol > 0 && wlv->lnum == wp->w_topline))
{
#ifdef FEAT_SIGNS
// If 'signcolumn' is set to 'number' and a sign is present
// in 'lnum', then display the sign instead of the line
// number.
if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u') && sign_present)
get_sign_display_info(TRUE, wp, wlv);
else
#endif
{
// Draw the line number (empty space after wrapping).
// When there are text properties above the line put the line number
// below them.
if (wlv->row == lnum_row
&& (wp->w_skipcol == 0 || wlv->row > wp->w_winrow
|| (wp->w_p_nu && wp->w_p_rnu)))
{
long num;
char *fmt = "%*ld ";
if (wp->w_p_nu && !wp->w_p_rnu)
// 'number' + 'norelativenumber'
num = (long)wlv->lnum;
else
{
// 'relativenumber', don't use negative numbers
num = labs((long)get_cursor_rel_lnum(wp, wlv->lnum));
if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
{
// 'number' + 'relativenumber'
num = wlv->lnum;
fmt = "%-*ld ";
}
}
sprintf((char *)wlv->extra, fmt, number_width(wp), num);
if (wp->w_skipcol > 0 && wlv->startrow == 0)
for (wlv->p_extra = wlv->extra; *wlv->p_extra == ' ';
++wlv->p_extra)
*wlv->p_extra = '-';
#ifdef FEAT_RIGHTLEFT
if (wp->w_p_rl) // reverse line numbers
{
char_u *p1, *p2;
int t;
// like rl_mirror(), but keep the space at the end
p2 = skipwhite(wlv->extra);
p2 = skiptowhite(p2) - 1;
for (p1 = skipwhite(wlv->extra); p1 < p2; ++p1, --p2)
{
t = *p1;
*p1 = *p2;
*p2 = t;
}
}
#endif
wlv->p_extra = wlv->extra;
wlv->c_extra = NUL;
wlv->c_final = NUL;
}
else
{
wlv->c_extra = ' ';
wlv->c_final = NUL;
}
wlv->n_extra = number_width(wp) + 1;
wlv->char_attr = hl_combine_attr(wlv->wcr_attr, HL_ATTR(HLF_N));
#ifdef FEAT_SYN_HL
// When 'cursorline' is set highlight the line number of
// the current line differently.
// When 'cursorlineopt' does not have "line" only
// highlight the line number itself.
// TODO: Can we use CursorLine instead of CursorLineNr
// when CursorLineNr isn't set?
if (wp->w_p_cul
&& wlv->lnum == wp->w_cursor.lnum
&& (wp->w_p_culopt_flags & CULOPT_NBR)
&& (wlv->row == wlv->startrow + wlv->filler_lines
|| (wlv->row > wlv->startrow + wlv->filler_lines
&& (wp->w_p_culopt_flags & CULOPT_LINE))))
wlv->char_attr = hl_combine_attr(wlv->wcr_attr, HL_ATTR(HLF_CLN));
#endif
if (wp->w_p_rnu && wlv->lnum < wp->w_cursor.lnum
&& HL_ATTR(HLF_LNA) != 0)
// Use LineNrAbove
wlv->char_attr = hl_combine_attr(wlv->wcr_attr, HL_ATTR(HLF_LNA));
if (wp->w_p_rnu && wlv->lnum > wp->w_cursor.lnum
&& HL_ATTR(HLF_LNB) != 0)
// Use LineNrBelow
wlv->char_attr = hl_combine_attr(wlv->wcr_attr, HL_ATTR(HLF_LNB));
}
#ifdef FEAT_SIGNS
if (num_attr)
wlv->char_attr = num_attr;
#endif
}
}
#ifdef FEAT_LINEBREAK
static void
handle_breakindent(win_T *wp, winlinevars_T *wlv)
{
if (wp->w_briopt_sbr && wlv->draw_state == WL_BRI - 1
&& *get_showbreak_value(wp) != NUL)
// draw indent after showbreak value
wlv->draw_state = WL_BRI;
else if (wp->w_briopt_sbr && wlv->draw_state == WL_SBR)
// After the showbreak, draw the breakindent
wlv->draw_state = WL_BRI - 1;
// draw 'breakindent': indent wrapped text accordingly
if (wlv->draw_state == WL_BRI - 1)
{
wlv->draw_state = WL_BRI;
// if wlv->need_showbreak is set, breakindent also applies
if (wp->w_p_bri && (wlv->row != wlv->startrow || wlv->need_showbreak)
# ifdef FEAT_DIFF
&& wlv->filler_lines == 0
# endif
# ifdef FEAT_PROP_POPUP
&& !wlv->dont_use_showbreak
# endif
)
{
wlv->char_attr = 0;
# ifdef FEAT_DIFF
if (wlv->diff_hlf != (hlf_T)0)
wlv->char_attr = HL_ATTR(wlv->diff_hlf);
# endif
wlv->p_extra = NULL;
wlv->c_extra = ' ';
wlv->c_final = NUL;
wlv->n_extra = get_breakindent_win(wp,
ml_get_buf(wp->w_buffer, wlv->lnum, FALSE));
if (wlv->row == wlv->startrow)
{
wlv->n_extra -= win_col_off2(wp);
if (wlv->n_extra < 0)
wlv->n_extra = 0;
}
if (wp->w_skipcol > 0 && wlv->startrow == 0
&& wp->w_p_wrap && wp->w_briopt_sbr)
wlv->need_showbreak = FALSE;
// Correct end of highlighted area for 'breakindent',
// required when 'linebreak' is also set.
if (wlv->tocol == wlv->vcol)
wlv->tocol += wlv->n_extra;
}
}
}
#endif
#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
static void
handle_showbreak_and_filler(win_T *wp, winlinevars_T *wlv)
{
# ifdef FEAT_DIFF
if (wlv->filler_todo > 0)
{
// Draw "deleted" diff line(s).
if (char2cells(wp->w_fill_chars.diff) > 1)
{
wlv->c_extra = '-';
wlv->c_final = NUL;
}
else
{
wlv->c_extra = wp->w_fill_chars.diff;
wlv->c_final = NUL;
}
# ifdef FEAT_RIGHTLEFT
if (wp->w_p_rl)
wlv->n_extra = wlv->col + 1;
else
# endif
wlv->n_extra = wp->w_width - wlv->col;
wlv->char_attr = HL_ATTR(HLF_DED);
}
# endif
# ifdef FEAT_LINEBREAK
char_u *sbr = get_showbreak_value(wp);
if (*sbr != NUL && wlv->need_showbreak)
{
// Draw 'showbreak' at the start of each broken line.
wlv->p_extra = sbr;
wlv->c_extra = NUL;
wlv->c_final = NUL;
wlv->n_extra = (int)STRLEN(sbr);
if (wp->w_skipcol == 0 || wlv->startrow != 0 || !wp->w_p_wrap)
wlv->need_showbreak = FALSE;
wlv->vcol_sbr = wlv->vcol + MB_CHARLEN(sbr);
// Correct end of highlighted area for 'showbreak',
// required when 'linebreak' is also set.
if (wlv->tocol == wlv->vcol)
wlv->tocol += wlv->n_extra;
// combine 'showbreak' with 'wincolor'
wlv->char_attr = hl_combine_attr(wlv->win_attr, HL_ATTR(HLF_AT));
# ifdef FEAT_SYN_HL
// combine 'showbreak' with 'cursorline'
if (wlv->cul_attr != 0)
wlv->char_attr = hl_combine_attr(wlv->char_attr, wlv->cul_attr);
# endif
}
# endif
}
#endif
#if defined(FEAT_PROP_POPUP) || defined(PROTO)
/*
* Return the cell size of virtual text after truncation.
*/
static int
textprop_size_after_trunc(
win_T *wp,
int flags, // TP_FLAG_ALIGN_*
int added,
int padding,
char_u *text,
int *n_used_ptr)
{
int space = (flags & (TP_FLAG_ALIGN_BELOW | TP_FLAG_ALIGN_ABOVE))
? wp->w_width - win_col_off(wp) : added;
int len = (int)STRLEN(text);
int strsize = 0;
int n_used;
// if the remaining size is to small and 'wrap' is set we wrap anyway and
// use the next line
if (space < PROP_TEXT_MIN_CELLS && wp->w_p_wrap)
space += wp->w_width;
if (flags & (TP_FLAG_ALIGN_BELOW | TP_FLAG_ALIGN_ABOVE))
space -= padding;
for (n_used = 0; n_used < len; n_used += (*mb_ptr2len)(text + n_used))
{
int clen = ptr2cells(text + n_used);
if (strsize + clen > space)
break;
strsize += clen;
}
*n_used_ptr = n_used;
return strsize;
}
/*
* Take care of padding, right-align and truncation of virtual text after a
* line.
* if "n_attr" is not NULL then "n_extra" and "p_extra" are adjusted for any
* padding, right-align and truncation. Otherwise only the size is computed.
* When "n_attr" is NULL returns the number of screen cells used.
* Otherwise returns TRUE when drawing continues on the next line.
*/
int
text_prop_position(
win_T *wp,
textprop_T *tp,
int vcol, // current text column
int scr_col, // current screen column
int *n_extra, // nr of bytes for virtual text
char_u **p_extra, // virtual text
int *n_attr, // attribute cells, NULL if not used
int *n_attr_skip, // cells to skip attr, NULL if not used
int do_skip) // skip_cells is not zero
{
int right = (tp->tp_flags & TP_FLAG_ALIGN_RIGHT);
int above = (tp->tp_flags & TP_FLAG_ALIGN_ABOVE);
int below = (tp->tp_flags & TP_FLAG_ALIGN_BELOW);
int wrap = tp->tp_col < MAXCOL || (tp->tp_flags & TP_FLAG_WRAP);
int padding = tp->tp_col == MAXCOL && tp->tp_len > 1
? tp->tp_len - 1 : 0;
int col_with_padding = scr_col + (below ? 0 : padding);
int room = wp->w_width - col_with_padding;
int before = room; // spaces before the text
int after = 0; // spaces after the text
int n_used = *n_extra;
char_u *l = NULL;
int strsize = vim_strsize(*p_extra);
int cells = wrap ? strsize : textprop_size_after_trunc(wp,
tp->tp_flags, before, padding, *p_extra, &n_used);
if (wrap || right || above || below || padding > 0 || n_used < *n_extra)
{
int col_off = win_col_off(wp) - win_col_off2(wp);
if (above)
{
before = 0;
after = wp->w_width - cells - win_col_off(wp) - padding;
}
else
{
// Right-align: fill with before
if (right)
before -= cells;
// Below-align: empty line add one character
if (below && vcol == 0 && col_with_padding == col_off
&& wp->w_width - col_off == before)
col_with_padding += 1;
if (before < 0
|| !(right || below)
|| (below ? (col_with_padding <= col_off || !wp->w_p_wrap)
: (n_used < *n_extra)))
{
if (right && (wrap
|| (room < PROP_TEXT_MIN_CELLS && wp->w_p_wrap)))
{
// right-align on next line instead of wrapping if possible
before = wp->w_width - col_off - strsize + room;
if (before < 0)
before = 0;
else
n_used = *n_extra;
}
else if (below && before > vcol && do_skip)
before -= vcol;
else
before = 0;
}
}
// With 'nowrap' add one to show the "extends" character if needed (it
// doesn't show if the text just fits).
if (!wp->w_p_wrap
&& n_used < *n_extra
&& wp->w_lcs_chars.ext != NUL
&& wp->w_p_list)
++n_used;
// add 1 for NUL, 2 for when '…' is used
if (n_attr != NULL)
l = alloc(n_used + before + after + padding + 3);
if (n_attr == NULL || l != NULL)
{
int off = 0;
if (n_attr != NULL)
{
vim_memset(l, ' ', before);
off += before;
if (padding > 0)
{
vim_memset(l + off, ' ', padding);
off += padding;
}
vim_strncpy(l + off, *p_extra, n_used);
off += n_used;
}
else
{
off = before + after + padding + n_used;
cells += before + after + padding;
}
if (n_attr != NULL)
{
if (n_used < *n_extra && wp->w_p_wrap)
{
char_u *lp = l + off - 1;
if (has_mbyte)
{
// change last character to '…'
lp -= (*mb_head_off)(l, lp);
STRCPY(lp, "…");
n_used = lp - l + 3 - before - padding;
}
else
// change last character to '>'
*lp = '>';
}
else if (after > 0)
{
vim_memset(l + off, ' ', after);
l[off + after] = NUL;
}
*p_extra = l;
*n_extra = n_used + before + after + padding;
*n_attr = mb_charlen(*p_extra);
if (above)
*n_attr -= padding + after;
// n_attr_skip will not be decremented before draw_state is
// WL_LINE
*n_attr_skip = before + padding;
}
}
}
if (n_attr == NULL)
return cells;
return (below && col_with_padding > win_col_off(wp) && !wp->w_p_wrap);
}
#endif
/*
* Call screen_line() using values from "wlv".
* Also takes care of putting "<<<" on the first line for 'smoothscroll'
* when 'showbreak' is not set.
*/
static void
wlv_screen_line(win_T *wp, winlinevars_T *wlv, int negative_width)
{
if (wlv->row == 0 && wp->w_skipcol > 0
#if defined(FEAT_LINEBREAK)
// do not overwrite the 'showbreak' text with "<<<"
&& *get_showbreak_value(wp) == NUL
#endif
// do not overwrite the 'listchars' "precedes" text with "<<<"
&& !(wp->w_p_list && wp->w_lcs_chars.prec != 0))
{
int off = (int)(current_ScreenLine - ScreenLines);
int skip = 0;
if (wp->w_p_nu && wp->w_p_rnu)
// Do not overwrite the line number, change "123 text" to
// "123>>>xt".
while (skip < wp->w_width && VIM_ISDIGIT(ScreenLines[off]))
{
++off;
++skip;
}
for (int i = 0; i < 3 && i + skip < wp->w_width; ++i)
{
ScreenLines[off] = '<';
if (enc_utf8)
ScreenLinesUC[off] = 0;
ScreenAttrs[off] = HL_ATTR(HLF_AT);
++off;
}
}
screen_line(wp, wlv->screen_row, wp->w_wincol, wlv->col,
negative_width ? -wp->w_width : wp->w_width,
wlv->screen_line_flags);
}
/*
* Called when finished with the line: draw the screen line and handle any
* highlighting until the right of the window.
*/
static void
draw_screen_line(win_T *wp, winlinevars_T *wlv)
{
#ifdef FEAT_SYN_HL
long v;
// Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
if (wp->w_p_wrap)
v = wlv->startrow == 0 ? wp->w_skipcol : 0;
else
v = wp->w_leftcol;
// check if line ends before left margin
if (wlv->vcol < v + wlv->col - win_col_off(wp))
wlv->vcol = v + wlv->col - win_col_off(wp);
# ifdef FEAT_CONCEAL
// Get rid of the boguscols now, we want to draw until the right
// edge for 'cursorcolumn'.
wlv->col -= wlv->boguscols;
wlv->boguscols = 0;
# define VCOL_HLC (wlv->vcol - wlv->vcol_off_co - wlv->vcol_off_tp)
# else
# define VCOL_HLC (wlv->vcol - wlv->vcol_off_tp)
# endif
if (wlv->draw_color_col)
wlv->draw_color_col = advance_color_col(VCOL_HLC, &wlv->color_cols);
if (((wp->w_p_cuc
&& (int)wp->w_virtcol >= VCOL_HLC - wlv->eol_hl_off
&& (int)wp->w_virtcol <
(long)wp->w_width * (wlv->row - wlv->startrow + 1) + v
&& wlv->lnum != wp->w_cursor.lnum)
|| wlv->draw_color_col
# ifdef LINE_ATTR
|| wlv->line_attr != 0
# endif
|| wlv->win_attr != 0)
# ifdef FEAT_RIGHTLEFT
&& !wp->w_p_rl
# endif
)
{
int rightmost_vcol = 0;
int i;
if (wp->w_p_cuc)
rightmost_vcol = wp->w_virtcol;
if (wlv->draw_color_col)
// determine rightmost colorcolumn to possibly draw
for (i = 0; wlv->color_cols[i] >= 0; ++i)
if (rightmost_vcol < wlv->color_cols[i])
rightmost_vcol = wlv->color_cols[i];
while (wlv->col < wp->w_width)
{
ScreenLines[wlv->off] = ' ';
if (enc_utf8)
ScreenLinesUC[wlv->off] = 0;
ScreenCols[wlv->off] = MAXCOL;
++wlv->col;
if (wlv->draw_color_col)
wlv->draw_color_col = advance_color_col(
VCOL_HLC, &wlv->color_cols);
int attr = wlv->win_attr;
if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
attr = HL_ATTR(HLF_CUC);
else if (wlv->draw_color_col && VCOL_HLC == *wlv->color_cols)
attr = HL_ATTR(HLF_MC);
# ifdef LINE_ATTR
else if (wlv->line_attr != 0)
attr = wlv->line_attr;
# endif
ScreenAttrs[wlv->off++] = attr;
if (VCOL_HLC >= rightmost_vcol
# ifdef LINE_ATTR
&& wlv->line_attr == 0
# endif
&& wlv->win_attr == 0)
break;
++wlv->vcol;
}
}
#endif
wlv_screen_line(wp, wlv, FALSE);
++wlv->row;
++wlv->screen_row;
}
#undef VCOL_HLC
/*
* Start a screen line at column zero.
* When "save_extra" is TRUE save and reset n_extra, p_extra, etc.
*/
static void
win_line_start(win_T *wp UNUSED, winlinevars_T *wlv, int save_extra)
{
wlv->col = 0;
wlv->off = (unsigned)(current_ScreenLine - ScreenLines);
#ifdef FEAT_RIGHTLEFT
if (wp->w_p_rl)
{
// Rightleft window: process the text in the normal direction, but put
// it in current_ScreenLine[] from right to left. Start at the
// rightmost column of the window.
wlv->col = wp->w_width - 1;
wlv->off += wlv->col;
wlv->screen_line_flags |= SLF_RIGHTLEFT;
}
#endif
if (save_extra)
{
// reset the drawing state for the start of a wrapped line
wlv->draw_state = WL_START;
wlv->saved_n_extra = wlv->n_extra;
wlv->saved_p_extra = wlv->p_extra;
wlv->saved_extra_attr = wlv->extra_attr;
wlv->saved_n_attr_skip = wlv->n_attr_skip;
wlv->saved_extra_for_textprop = wlv->extra_for_textprop;
wlv->saved_c_extra = wlv->c_extra;
wlv->saved_c_final = wlv->c_final;
#ifdef FEAT_SYN_HL
if (!(wlv->cul_screenline
# ifdef FEAT_DIFF
&& wlv->diff_hlf == (hlf_T)0
# endif
))
wlv->saved_char_attr = wlv->char_attr;
else
#endif
wlv->saved_char_attr = 0;
// these are not used until restored in win_line_continue()
wlv->n_extra = 0;
wlv->n_attr_skip = 0;
}
}
/*
* Called when wlv->draw_state is set to WL_LINE.
*/
static void
win_line_continue(winlinevars_T *wlv)
{
if (wlv->saved_n_extra > 0)
{
// Continue item from end of wrapped line.
wlv->n_extra = wlv->saved_n_extra;
wlv->saved_n_extra = 0;
wlv->c_extra = wlv->saved_c_extra;
wlv->c_final = wlv->saved_c_final;
wlv->p_extra = wlv->saved_p_extra;
wlv->extra_attr = wlv->saved_extra_attr;
wlv->n_attr_skip = wlv->saved_n_attr_skip;
wlv->extra_for_textprop = wlv->saved_extra_for_textprop;
wlv->char_attr = wlv->saved_char_attr;
}
else
wlv->char_attr = wlv->win_attr;
}
#ifdef FEAT_SYN_HL
static void
apply_cursorline_highlight(
winlinevars_T *wlv,
int sign_present UNUSED)
{
wlv->cul_attr = HL_ATTR(HLF_CUL);
# ifdef FEAT_SIGNS
// Combine the 'cursorline' and sign highlighting, depending on
// the sign priority.
if (sign_present && wlv->sattr.sat_linehl > 0)
{