On 19/09/12 00:04, bilibop project wrote:
> Hi,
>
> I have written some syntax files (in /etc/vim/syntax/), and copied the
> runtime filetype.vim in /etc/vim/ to tweak it.
>
> I take the following example:
> syntax file: /etc/vim/syntax/aptconf.vim
> and, in filetype.vim:
>
> ========== BEGIN ==========
> au BufNewFile,BufRead apt.conf setf aptconf
> au BufNewFile,BufRead */apt.conf.d/* setf aptconf
> =========== END ===========
>
> But the apt.conf(5) manual page says that the file names must follow
> some basic rules, such as:
> - have either no or "conf" as filename extension
> - only contain alphanumeric, hyphen (-), underscore (_) and period (.)
> characters.
>
> This means the filename must match the following BRE:
> '^\([-_[:alnum:]]\+\|[-_.[:alnum:]]\+\.conf\)$'
>
> So, I have written a function to check that and set the filetype:
>
> ========== BEGIN ==========
> func! s:MatchNameSetf(ft,regexp)
> if expand("<afile>:t") =~ a:regexp
> exe 'setf ' . a:ft
> endif
> endfunc
> =========== END ===========
>
> And replaced the autocommand above by this one:
>
> ========== BEGIN ==========
> au BufNewFile,BufRead */apt.conf.d/*
> \ call s:MatchNameSetf('aptconf','^\([-_[:alnum:]]\+\|[-_.[:alnum:]]\+\.conf\)$')
> =========== END ===========
>
> It works fine. But I'm curious, and tried that:
>
> ========== BEGIN ==========
> au BufNewFile,BufRead */apt.conf.d/\([-_[:alnum:]]\+\|[-_.[:alnum:]]\+\.conf\) setf aptconf
> =========== END ===========
>
> It works fine too... So now my question is: how filenames are they parsed in the 'au'
> command ? I thought regexp were not allowed, but only glob() wildcards and braces
> (as in the shell): see for cmusrc for example. And, from the two last solutions, is there
> someone to say me what is the best ?
>
> Thanks
> quidame
>
See ":help file-pattern": A backslash has special meaning as in Vim
regular expressions. So IIUC the fact of wrapping your Vim regex in \(
\) in the autocommand filename was enough to have Vim understand it
correctly.
As for what is the best… That last example is more self-contained, and
(compared to the length of autocommand + function in the other example)
shorter (though neither more nor less easy to understand at first
sight), so it looks more "elegant" to me, but that could be my
mathematician's training, where shortness one of the criteria of
elegance for a geometry demonstration or of an algebra problem solution.
Beauty, as is well known, is in the eye of the beholder.
Best regards,
Tony.
--
Pedaeration, n.:
The perfect body heat achieved by having one leg under the
sheet and one hanging off the edge of the bed.
-- Rich Hall, "Sniglets"
--
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
Tuesday, September 18, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment