Monday, April 23, 2012

Re: foldmethod=syntax

On Sun, Apr 22, 2012 at 12:44:56PM EDT, Benjamin R. Haskell wrote:
> On Sun, 22 Apr 2012, Chris Jones wrote:

[..]

> >The difficulty with fdm=syntax is that there is no explicit 'end of
> >section marker' in my free-form, but nonetheless fairly 'standard'
> >files and as often with Vim, it looks like folding was mostly
> >designed to work with stuff like C code in mind.. Here,
> >unfortunagely, the end of the current section is also the start of
> >the next one.

>
> I'm not positive, but this might be a case for keepend.
>
> See: :help :syn-keepend

I've read that bit (again).. and still not sure what it means. Seems to
be more about nesting folds/blocks with different start/end markers.

[..]

> >. I'm not sure I understand why I need a getline(), since ':h fde'
> >states that the expression 'is evaluated for each line to obtain
> >its fold level'. I thought this meant that Vim would take care of
> >the matching against the entire file?
>
> 'fde' is only set up with the v:lnum variable (which tells the
> function what line it's being evaluated for). getline() gets the
> content of the line.

And there are examples under ':h fold-expr' to illustrate.. no excuse.

> The doubled backslashes are a side-effect of the way ':set' is
> evaluated. For anything where the quoting gets messy, I always tend
> to use ':let-&' instead. Or, better yet, a function.

Same..

> So, in this case, Christian's example might become:
>
> fun! FoldSimpleText()
> return getline(v:lnum) =~ '^\d\+.*section$' ? '>1' : '='
> endf
> setl fdm=expr fde=FoldSimpleText()
>
> If you have subsections, this can be improved to:
>
> fun! FoldSimpleText()
> " extract the section header
> let section_match = matchlist(getline(v:lnum), '^\s*\(\%(\d\+\.\)\+\)')
> if !len(section_match)
> return '='
> end
> " start a fold with the count of the '.'s in the section header
> return '>'.len(filter(split(section_match[1], '\zs'), 'v:val == "."'))
> endf
> setl fdm=expr fde=FoldSimpleText()

Writing code was what I was trying to avoid... but considering my lack
of knowledge of what Vim does behind the scenes when processing syntax
files, it looks like the only sensible approach.

Thanks,

CJ

--
WE GET SIGNAL

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: