Monday, April 23, 2012

Re: foldmethod=syntax

Hi Chris!

On So, 22 Apr 2012, Chris Jones wrote:

> On Sun, Apr 22, 2012 at 07:47:19AM EDT, Christian Brabandt wrote:
>
> > Hi Chris!
>
> Hallo Christian, wie geht's..?

Ich bin beeindruckt ;)

>
> > On So, 22 Apr 2012, Chris Jones wrote:
>
> > > I have a few hundred text files, mostly documentation, and all of
> > > them basically have the following structure:
>
> > >
> > > | 1. first section
> > > | ...
> > > | 2. second section
> > > | ...
> > > | etc..
>
> > > I would like to have Vim automatically create folds so that I could
> > > use the usual 'z' commands to easily collapse 'first', 'second'..
> > > above, for a quick overview of the contents..
>
> > set fdm=expr fde=getline(v:lnum)=~'^\\d\\+.*section$'?'>1':'='
>
> It was getting really late last night and so I thought I'd figure out
> a more relevant 'Subject:' later.. and forgot to change it.. :-(
>
> Initially, I was planning on using fdm=syntax rather than fdm=expr for
> the sake of scalability, in case I needed to add stuff later: the sample
> above is a slightly simplified version as there are for instance nested
> levels in some of the actual files: 1.1 subsection.. 1.2.. etc. as well
> as some lists where items are prepended with a dash/asterisk/dot..
>
> 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.

The advantage of making an syntax script is, that you get syntax
coloring for free, although, this method is probably the most complex
one. If you could outline your file structure more explicitly, we can
probably design a syntax script for you.

> I tried playing with the rs=/re= feature (:h syn-pattern) to no avail.
>
> But I think I have already spent a lot more time with this than
> I intended without getting anywhere.. and so a simple fde that only
> handles top-level folds would be absolutely fine.
>
> Now, as to the fold expression you suggest:
>
> . 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?

The expression is evaluated for each line. That doesn't mean the content
of each line is compared with regard to the fold-expression.

> . Just for the record, the 'section' literal was only meant for
> clarification (?) but I shouldn't have any problem changing that bit
> to match just the leading digit(s) followed by a dot - '^\s\+\.', e.g.
> and hopefully, it won't match elsewhere in the files.

Benjamin already gave you a good improvement. Further improvements are
possible, but we would need to know your file better.

>
> I won't have the time to take the regex apart & test it until later
> tonight and find out why it's not matching anything against my sample
> file, even when I use it as a search pattern from the command line,
> magic or nomagic.. Could the double backslashes have something to do
> with it..?

Yes, it's a side effect of using set. This means, the expression is
parsed twice and so the backslashes need to be doubled. In general, it
is usually better and more readable to use :let-expressions (:h :let-&),
but this was just a small one-liner. If such an expression get's more
complicated, it is also better to make an extra function out of it.


regards,
Christian
--
Alle sind Irre; aber wer seinen Wahn zu analysieren versteht, wird
Philosoph genannt.
-- Ambrose Bierce

--
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:

Post a Comment