Skip to content

Commit 8e29c35

Browse files
mattnchrisbra
authored andcommitted
patch 9.2.0263: hlset() cannot handle attributes with spaces
Problem: hlset() cannot handle attributes with spaces Solution: Handle attributes with spaces by quoting those (Yasuhiro Matsumoto). hlset(hlget('Normal')) fails with E416 when a highlight attribute value contains spaces (e.g. font name "Monospace 10"). hlg_add_or_update() builds a string like "font=Monospace 10" and passes it to do_highlight(), whose parser splits on whitespace and treats "10" as a separate key without "=". Fix by quoting values with single quotes (e.g. font='Monospace 10') when the value contains spaces and the attribute is a key=value pair. do_highlight() already supports single-quoted values. closes: #19843 Signed-off-by: Yasuhiro Matsumoto <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 80a0c35 commit 8e29c35

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

src/highlight.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5309,7 +5309,23 @@ add_attr_and_value(char_u *dptr, char_u *attr, int attrlen, char_u *value)
53095309
return dptr;
53105310

53115311
vallen = STRLEN(value);
5312-
if (dptr + attrlen + vallen + 1 < hlsetBuf + HLSETBUFSZ)
5312+
// When the value contains a space and the attribute has an "=" (i.e. it
5313+
// is a key=value pair), surround the value with single quotes so that
5314+
// do_highlight() can parse it correctly.
5315+
if (vim_strchr(value, ' ') != NULL && vim_strchr(attr, '=') != NULL)
5316+
{
5317+
if (dptr + attrlen + vallen + 3 < hlsetBuf + HLSETBUFSZ)
5318+
{
5319+
STRCPY(dptr, attr);
5320+
dptr += attrlen;
5321+
*dptr++ = '\'';
5322+
STRCPY(dptr, value);
5323+
dptr += vallen;
5324+
*dptr++ = '\'';
5325+
*dptr = NUL;
5326+
}
5327+
}
5328+
else if (dptr + attrlen + vallen + 1 < hlsetBuf + HLSETBUFSZ)
53135329
{
53145330
STRCPY(dptr, attr);
53155331
dptr += attrlen;

src/testdir/test_highlight.vim

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,17 +1142,9 @@ endfunc
11421142

11431143
" Test for the hlset() function
11441144
func Test_hlset()
1145-
" FIXME: With GVim, _current_ test cases that are run before this one may
1146-
" influence the result of calling "hlset(hlget())", depending on what
1147-
" "&guifont" is set to. For example, introduce SetUp() as follows:
1148-
"
1149-
" if CanRunVimInTerminal() && has('gui_running') && has('gui_gtk')
1150-
" def SetUp()
1151-
" set guifont=Monospace\ 10
1152-
" enddef
1153-
" endif
1154-
"
1155-
" and see "E416: Missing equal sign: ... line 4" for this test case.
1145+
" Note: hlset() now correctly handles attribute values containing spaces
1146+
" by quoting them, so hlset(hlget()) works even with font names like
1147+
" "Monospace 10".
11561148
let lines =<< trim END
11571149
call assert_equal(0, hlset(test_null_list()))
11581150
call assert_equal(0, hlset([]))
@@ -1353,6 +1345,15 @@ func Test_hlset()
13531345
call hlset([{'name': 'hlg11', 'stop': ''}])
13541346
call hlset([{'name': 'hlg11', 'term': {}}])
13551347
call assert_true(hlget('hlg11')[0].cleared)
1348+
1349+
" Test that hlset() handles attribute values containing spaces
1350+
call hlset([{'name': 'hlg12', 'guifg': 'light blue'}])
1351+
call assert_equal('light blue', hlget('hlg12')[0].guifg)
1352+
call hlset([{'name': 'hlg12', 'guibg': 'dark red'}])
1353+
call assert_equal('dark red', hlget('hlg12')[0].guibg)
1354+
call hlset([{'name': 'hlg12', 'guisp': 'sea green'}])
1355+
call assert_equal('sea green', hlget('hlg12')[0].guisp)
1356+
highlight clear hlg12
13561357
endfunc
13571358

13581359
" Test for the 'winhighlight' option

src/version.c

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

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
263,
737739
/**/
738740
262,
739741
/**/

0 commit comments

Comments
 (0)