Skip to content

Commit 91478ae

Browse files
committed
patch 8.2.2454: leading space can not be made visible
Problem: Leading space can not be made visible. Solution: Add "lead:" to 'listchars'. (closes #7772)
1 parent 148be9b commit 91478ae

7 files changed

Lines changed: 81 additions & 8 deletions

File tree

runtime/doc/options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,6 +4877,10 @@ A jump table for the options with a short description can be found at |Q_op|.
48774877
*lcs-space*
48784878
space:c Character to show for a space. When omitted, spaces
48794879
are left blank.
4880+
*lcs-lead*
4881+
lead:c Character to show for leading spaces. When omitted,
4882+
leading spaces are blank. Overrides the "space"
4883+
setting for leading spaces.
48804884
*lcs-trail*
48814885
trail:c Character to show for trailing spaces. When omitted,
48824886
trailing spaces are blank. Overrides the "space"

src/drawline.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ win_line(
339339
int change_end = -1; // last col of changed area
340340
#endif
341341
colnr_T trailcol = MAXCOL; // start of trailing spaces
342+
colnr_T leadcol = 0; // start of leading spaces
342343
#ifdef FEAT_LINEBREAK
343344
int need_showbreak = FALSE; // overlong line, skipping first x
344345
// chars
@@ -734,8 +735,9 @@ win_line(
734735

735736
if (wp->w_p_list)
736737
{
737-
if (lcs_space || lcs_trail || lcs_nbsp)
738+
if (lcs_space || lcs_trail || lcs_lead || lcs_nbsp)
738739
extra_check = TRUE;
740+
739741
// find start of trailing whitespace
740742
if (lcs_trail)
741743
{
@@ -744,6 +746,19 @@ win_line(
744746
--trailcol;
745747
trailcol += (colnr_T) (ptr - line);
746748
}
749+
// find end of leading whitespace
750+
if (lcs_lead)
751+
{
752+
leadcol = 0;
753+
while (VIM_ISWHITE(ptr[leadcol]))
754+
++leadcol;
755+
if (ptr[leadcol] == NUL)
756+
// in a line full of spaces all of them are treated as trailing
757+
leadcol = (colnr_T)0;
758+
else
759+
// keep track of the first column not filled with spaces
760+
leadcol += (colnr_T) (ptr - line) + 1;
761+
}
747762
}
748763

749764
wcr_attr = get_wcr_attr(wp);
@@ -1992,6 +2007,7 @@ win_line(
19922007
|| (c == ' '
19932008
&& mb_l == 1
19942009
&& lcs_space
2010+
&& ptr - line >= leadcol
19952011
&& ptr - line <= trailcol)))
19962012
{
19972013
c = (c == ' ') ? lcs_space : lcs_nbsp;
@@ -2012,9 +2028,10 @@ win_line(
20122028
mb_utf8 = FALSE;
20132029
}
20142030

2015-
if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
2031+
if ((trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
2032+
|| (leadcol != 0 && ptr < line + leadcol && c == ' '))
20162033
{
2017-
c = lcs_trail;
2034+
c = (ptr > line + trailcol) ? lcs_trail : lcs_lead;
20182035
if (!attr_pri)
20192036
{
20202037
n_attr = 1;

src/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,7 @@ EXTERN int lcs_tab1 INIT(= NUL);
13521352
EXTERN int lcs_tab2 INIT(= NUL);
13531353
EXTERN int lcs_tab3 INIT(= NUL);
13541354
EXTERN int lcs_trail INIT(= NUL);
1355+
EXTERN int lcs_lead INIT(= NUL);
13551356
#ifdef FEAT_CONCEAL
13561357
EXTERN int lcs_conceal INIT(= ' ');
13571358
#endif

src/message.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,18 +1831,32 @@ msg_prt_line(char_u *s, int list)
18311831
int n;
18321832
int attr = 0;
18331833
char_u *trail = NULL;
1834+
char_u *lead = NULL;
18341835
int l;
18351836
char_u buf[MB_MAXBYTES + 1];
18361837

18371838
if (curwin->w_p_list)
18381839
list = TRUE;
18391840

1840-
// find start of trailing whitespace
1841-
if (list && lcs_trail)
1841+
if (list)
18421842
{
1843-
trail = s + STRLEN(s);
1844-
while (trail > s && VIM_ISWHITE(trail[-1]))
1845-
--trail;
1843+
// find start of trailing whitespace
1844+
if (lcs_trail)
1845+
{
1846+
trail = s + STRLEN(s);
1847+
while (trail > s && VIM_ISWHITE(trail[-1]))
1848+
--trail;
1849+
}
1850+
// find end of leading whitespace
1851+
if (lcs_lead)
1852+
{
1853+
lead = s;
1854+
while (VIM_ISWHITE(lead[0]))
1855+
lead++;
1856+
// in a line full of spaces all of them are treated as trailing
1857+
if (*lead == NUL)
1858+
lead = NULL;
1859+
}
18461860
}
18471861

18481862
// output a space for an empty line, otherwise the line will be
@@ -1938,6 +1952,11 @@ msg_prt_line(char_u *s, int list)
19381952
// the same in plain text.
19391953
attr = HL_ATTR(HLF_8);
19401954
}
1955+
else if (c == ' ' && lead != NULL && s <= lead)
1956+
{
1957+
c = lcs_lead;
1958+
attr = HL_ATTR(HLF_8);
1959+
}
19411960
else if (c == ' ' && trail != NULL && s > trail)
19421961
{
19431962
c = lcs_trail;

src/screen.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4775,6 +4775,7 @@ set_chars_option(char_u **varp)
47754775
{&lcs_space, "space"},
47764776
{&lcs_tab2, "tab"},
47774777
{&lcs_trail, "trail"},
4778+
{&lcs_lead, "lead"},
47784779
#ifdef FEAT_CONCEAL
47794780
{&lcs_conceal, "conceal"},
47804781
#else

src/testdir/test_listchars.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,35 @@ func Test_listchars()
110110
\ '.....h>-$',
111111
\ 'iii<<<<><<$', '$'], l)
112112

113+
" Test lead and trail
114+
normal ggdG
115+
set listchars&
116+
set listchars+=lead:>,trail:<,space:x
117+
set list
118+
119+
call append(0, [
120+
\ ' ffff ',
121+
\ ' gg',
122+
\ 'h ',
123+
\ ' ',
124+
\ ' 0 0 ',
125+
\ ])
126+
127+
let expected = [
128+
\ '>>>>ffff<<<<$',
129+
\ '>>>>>>>>>>gg$',
130+
\ 'h<<<<<<<<<<<$',
131+
\ '<<<<<<<<<<<<$',
132+
\ '>>>>0xx0<<<<$',
133+
\ '$'
134+
\ ]
135+
redraw!
136+
for i in range(1, 5)
137+
call cursor(i, 1)
138+
call assert_equal([expected[i - 1]], ScreenLines(i, virtcol('$')))
139+
endfor
140+
141+
call assert_equal(expected, split(execute("%list"), "\n"))
113142

114143
" test nbsp
115144
normal ggdG

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2454,
753755
/**/
754756
2453,
755757
/**/

0 commit comments

Comments
 (0)