On 5/21/06, Yakov Lerner <[EMAIL PROTECTED]> wrote:
I want to fold lines grouped at the top of file
and matching some pattern. I use foldmethod=expr.
But vim does not behave as expected.
In the testcase, /a/. In the testfile (2) below, I want first 3
lines (a\na\na\n) folded, and nothing else folded. But vim also folds
the last a\na\n lines. I think this is vim bug.
Yakov
-----------------------------
(1) the code
:set foldmethod=expr foldenable foldexpr=Foo()
function! Foo()
echomsg '>>'.v:lnum.(v:lnum>1 ? ' prev='.foldlevel(v:lnum-1) : '')
if v:lnum>1 && foldlevel(v:lnum-1) == 0
return 0
else
return getline(v:lnum) =~ 'a'
endif
endfun
(2) the testfile is
-------
a
a
a
b
b
b
a
a
-------
Well, I'm not entirely sure what is supposed to be happening from
looking at the docs, but this works:
function! Foo()
echomsg '>>'.v:lnum.(v:lnum>1 ? ' prev='.foldlevel(v:lnum-1) : '')
. ', foldlevel=' . foldlevel(v:lnum - 1 )
if (v:lnum>1 )&& (foldlevel(v:lnum-1) == -1)
echomsg 'returning 0'
return 0
else
echomsg 'returing ' . (getline(v:lnum) =~ 'a')
return getline(v:lnum) =~ 'a'
endif
endfun
If you just print out the value of foldlevel(v:lnum-1), you'll find
it's always -1 for unfolded lines in your test case.
It seems that the foldlevel isn't 0 when you think it is, and I'm not
sure whether that is right or wrong. The docs say
As a special case the level of the
previous line is usually available.
"usually available" is kinda hard to deal with.