On 2011-06-17, Eric Weir wrote:
> On Jun 16, 2011, at 6:44 PM, Gary Johnson wrote:
> 
> > If the goal is to fold each entry to a single line, why not have Vim
> > do that automatically?  You could put a modeline near the top or
> > bottom of your journal like this example:
> > 
> > vim: foldmethod=expr foldexpr=getline(v\:lnum)=~'^\\(Mon\\|Thu\\)\ 
> > \\(Jan\\|Jun\\)\ \\d\\d\ \\d\\d\:\\d\\d\:\\d\\d\ 
> > \\u\\u\\u\\\d\\d\\d\\d$'?'>1'\:'1'
> > 
> > That uses a fold expression that matches dates of the form output
> > from the Unix 'date' command.  It starts a new fold at every line
> > that matches that expression and sets the fold level of every
> > subsequent line to be the same.  The result looks like this:
> > 
> > +--  4 lines: Thu Jun 16 15:01:39 PDT 2011--------------------------
> > +--  4 lines: Thu Jun 16 15:01:56 PDT 2011--------------------------
> > +--  4 lines: Thu Jun 16 15:02:47 PDT 2011--------------------------
> 
> Thanks, Gary. 
> 
> That's what I want, though I'd want to leave out the time report
> if I could.

That's no problem--you can use any date format you like.  I just
used the default output of 'date' as an example.  The only thing
important about the format is that it should be unlikely that it
would appear in the body of a journal entry.

> I wonder, also, how this way of doing it would be affected by
> editing of an entry at a later date. Would it change the date of
> the fold?

That's one of the advantages to using expression folding instead of
manual folding.  The folded region is determined automatically (in
this case by a regular expression) and will adjust as you edit the
file.

The date won't be affected unless you change it yourself.

> And what if, as is likely, the edited was to just one paragraph of
> an entry. Would it get separated off into its own fold? I wouldn't
> want either.

Nope.  In this example, the fold boundaries are determined by the
date header lines.

> I'd be willing to give it a try, just to see how it works, if I
> knew how to fill out the expression for the rest of the year. As
> non-programming Vim novice, I haven't a clue how to go about that.

One disadvantage of this method is that writing the correct
expression can be tricky.  The expression I included in my reply was
certainly not my first attempt.  Also, the expression doesn't have
to account for every element of the entry header line; it just has
to be able to distinguish that line from any line in the body of any
entry.

The expression I used matches "Mon" or "Thu" at the start of a line
with this regular expression:

    ^\(Mon\|Thu\)

where the \( and \) are for grouping and \| is the logical OR
operator.
Because the expression is the argument to a :set command, the
backslashes have to be quoted:

    ^\\(Mon\\|Thu\\)

To add the rest of the days of the week, you can just extend that
pattern like this:

    ^\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\)

Alternatively, you could choose some simple pattern for your header
lines for which it would be easy to write an expression.  For
example, if you used a header line like this,

    - Friday, 17 June 2011 -

the pattern could be simply

    ^-.*-$

(a hyphen at the beginning at at the end of the line) which would
make your modeline this:

    vim: foldmethod=expr foldexpr=getline(v\:lnum)=~'^-.*-$'?'>1'\:'1'

You can read more about this here:

    :help 28.8
    :help fold-expr
    :help pattern.txt

Regards,
Gary

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

Reply via email to