Only some foldmethods can be set in modelines?

43 views
Skip to first unread message

Erik Christiansen

unread,
Jul 23, 2015, 6:22:46 AM7/23/15
to vim...@googlegroups.com
Let's take the simplest case. For each exception to my general case of
fdm = marker, I have begun to include in .vimrc e.g.:

au BufNewFile,BufRead ~/Embedded/dev_data/my_list call SimpleFold()

and one:

" Fold text blocks separated by EMPTY lines, so
" a line with whitespace unites two blocks.
function SimpleFold()
setlocal foldexpr=getline(v:lnum)=~'^$'&&getline(v:lnum+1)=~'\\S'?'<1':1
setlocal foldtext=getline(v:foldstart)
setlocal foldmethod=expr
endfunction

But that costs the labour, clutter, and maintenance nightmare of adding
a file-specific line for each file with a non-default foldmethod.

BUT, if I instead append the three lines of SimpleFold() as modelines in
the target file, then there are errors: lnum and foldstart are unknown
to Vim in modelines, at least in:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Apr 2 2015 20:12:09)
Included patches: 1-688

(OK, I'm on debian, so it's old. )

What I _could_ do is make the above the default fdm, and add the
following modeline to all the currently defaulted files:

vim: foldmethod=marker foldmarker={{{,}}} foldopen-=block commentstring=

That seems to work. All the previously defaulted files would come out of
the woodwork over time.

OTOH, is there a better way to do this, I can't help wondering?

Erik
(Pausing to put another log on the fire. It's midwinter down under.)

--
In Bavaria, there are over 700 small biomass plants dotted around the country.
- http://www.abc.net.au/news/2015-07-19/tasmanian-forest-industry-fired-up-about-biomass/6628822
(Hmmm ... that recognition might be quite popular south of the Weißwurstäquator.)

Nelo-Thara Wallus

unread,
Jul 23, 2015, 8:21:27 AM7/23/15
to vim...@googlegroups.com
> --
> --
> 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
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

In modelines you have to escape the colon (if your foldexpression is more
complicated than `getline(v:foldstart)` you should consider using its own
function for readability and extendability)

The best way is to give your files proper endings and to use filetype files
(`~/.vim/ftplugin/$filetype.vim`). If you use filetypes unkown to vim you have
to use a ftdetect file (`~/.vim/ftdetect/$filetype.vim`, more info[1] - it's
really just an augroup).

[1] http://learnvimscriptthehardway.stevelosh.com/chapters/44.html

--
Viktoriastrasse 22
76133 Karlsruhe
Nr.: 0721 / 96 55 63 95
Handy: 0178 / 53 17 067
/"\ ASCII Ribbon Campaign
\ / - against HTML emails
X - against proprietory attachments
/ \ http://en.wikipedia.org/wiki/ASCII_Ribbon_Campaign

Erik Christiansen

unread,
Jul 23, 2015, 9:30:56 AM7/23/15
to vim...@googlegroups.com
On 23.07.15 13:40, Nelo-Thara Wallus wrote:
> In modelines you have to escape the colon (if your foldexpression is more
> complicated than `getline(v:foldstart)`

Many thanks. I'll try that. (In the morning. It is growing late here.)

> you should consider using its own function for readability and
> extendability)

I'll consider anything which is more elegant than the starting point,
but as ":h modeline" hammers home, no matter how many times I go back to
look, "There are two forms of modelines." I.e. Explicit and implicit
"set" directives. Nothing else.

If you can suggest how to invoke from a modeline, the function I've
already been using, listed in the post to which you replied, then I'd be
both impressed and appreciative.

> The best way is to give your files proper endings and to use filetype files
> (`~/.vim/ftplugin/$filetype.vim`). If you use filetypes unkown to vim you have
> to use a ftdetect file (`~/.vim/ftdetect/$filetype.vim`, more info[1] - it's
> really just an augroup).

That looks like a value judgement from here. ;-)
It is not the *nix way, and seems to me a right royal PITA to
administer. Using a suffix identifying foldmethod seems a very limiting
solution, since that excludes other custom handling, unless multiple
suffixes are added to the filenames. (They'd then be modelines on the
outside of the file.) Alternatively, generating a different suffix for
each of my files is easy enough I suppose, since any random guff would
do, and will handle 238328 files, just using [a-zA-Z0-9]. But that is
merely an externally visible alternative to all the autocommands I
deprecated in my OP. And generating a filetype file for each text file
would have me thinking I'm on a Mac.

I'm very happy with using modelines. It's rather like *nix file magic,
just much more customisable, and can be tacked on the end of the file
in order to be less intrusive.

For the moment, I'm experimenting with a modeline of:

vim: foldmethod=marker foldopen-=block foldtext=MyFoldText() commentstring=

and the contents of the function in the OP as default. So long as I
don't come up with a complex third alternative, that works. It does
force me to add a modeline of " vim: foldmethod=manual" in files not
using folding, since the new default does not implicitly handle that.

Thanks again for taking time to think about my quandary, especially when
it's midsummer up there.

Erik

--
Victorian paramedics were called to more than 200 cases of children locked in
cars in the first five weeks of 2015. Last year more than 1,100 children were
rescued from inside locked cars.
- http://www.abc.net.au/news/2015-02-20/toddler-found-dead-kyneton-police-continue-investigations/6158496
Reply all
Reply to author
Forward
0 new messages