question about foldexpr and documentation

48 views
Skip to first unread message

Christian Brabandt

unread,
Nov 20, 2015, 4:39:35 AM11/20/15
to vim...@vim.org
Bram,
this question came up on stackexchange:
http://vi.stackexchange.com/q/5543/71

In short take this file foldexpr.vim:
#v+
$ cat foldexpr.vim
"" This is a demo, the fold starts here. This line has foldlevel 2
" This line is folded wrong, it should have foldlevel 1, but has foldlevel 2
" Another test line. What foldlevel does it have?
" Another test line. What foldlevel does it have?
function! VimFolds(lnum)
let thisline = getline(a:lnum)
if match(thisline, '^""') >= 0
return '>2'
elseif match(thisline, '^" ') >= 0
return 's1'
else
return '='
endif
endfunction
setlocal foldmethod=expr
setlocal foldexpr=VimFolds(v:lnum)
set foldcolumn=3
set foldminlines=0
$
vim -u NONE -N -c ':so %' foldexpr.vim
#v-

Note the second line has foldlevel 2 instead of 1

It looks like the special string 's1' does substract one after the
current line. So either the documentation:

,----[ :h fold-expr ]-
| "s1", "s2", .. subtract one, two, .. from the fold level of the
| previous line
`----

or the code is wrong. Looking at foldlevelExpr() I see this code:


,----[ fold.c ... foldlevelExpr() ]-
| /* "s1", "s2", .. : subtract from the fold level */
| case 's': if (flp->lvl >= 0)
| {
| if (n > flp->lvl)
| flp->lvl_next = 0;
| else
| flp->lvl_next = flp->lvl - n;
| flp->end = flp->lvl_next + 1;
| }
| break;
`----

Note, that flp->lvl is not set and remains at the current value. So what
do you think: A bug or a (undocumented) feature?


Best,
Christian
--
Bescheidenheit ist so beliebt, weil sie einem die Arroganz erleichtert.

Bram Moolenaar

unread,
Nov 24, 2015, 8:35:53 AM11/24/15
to Christian Brabandt, vim...@vim.org
The idea of the "s1" kind of values is that this matches a line that's
the end of a fold. Thus it indeed applies to the following line, not
the current one. Good example is a C comment:

/*
* comment
*/

Here matching "/*" with "a1" and "*/" with "s1" should work.

I'll adjust the documentatin.

--
hundred-and-one symptoms of being an internet addict:
128. You can access the Net -- via your portable and cellular phone.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages