Tuesday, September 18, 2012

How filenames are parsed in 'autocommand' ?

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

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