Sunday, December 23, 2012

Re: Fold expression called too often

On 2012–12–23 Ben Fritz wrote:

> From :help fold-expr:
>
> > Try to avoid the "=", "a" and "s" return values, since Vim often has to search
> > backwards for a line for which the fold level is defined. This can be slow.
>
> The problem is that Vim calls your foldexpr once for every line,
> but for each line using =, a, or s, it must call it recursively on
> the previous line to get the foldlevel of that line, which calls
> it recursively again to get the previous line for its own
> foldlevel, etc. where the recursion only ends when a foldlevel is
> defined explicitly (at the first line in the file).

That explains it, thanks.

> To avoid this you could store foldlevels of previous lines in a
> lookup table internally to your function, and look up the
> foldlevel from your variable if present, or using the foldlevel()
> function instead of returning =, a, and s items.

I read the manual entry about foldlevel() but I didn't really grasp
the idea (it's my first attempt at writing a fold expression). How
to use foldlevel() to create an efficient expression for a file like
the following?

start
alpha
start
beta
stop
stop
start
gamma
start
delta
stop
stop

Marco

No comments: