Saturday, April 9, 2011

Re: force use of scripts.vim to find filetype

Am 05.04.2011 18:51, schrieb Alvin Kerber:
> I have a bunch of these executable scripts, with shebang lines, and I
> want configure vim to detect the filetype using the shebang line. The
> problem is that these scripts all must have the .foo extension
> (they're run automatically by a build system), and the system-wide
> filetype.vim recognizes .foo files as having a particular filetype,
> which is not the one I want. Therefore, vim thinks it knows what kind
> of files these are and never reads the shebang line.
>
> How can I configure vim so that this incorrect recognition stops
> happening? Basically, I want things so that when I open a .foo file,
> it identifies the filetype only from the shebang line.
>
> I tried using an autocmd to ignore the incorrect filetype:
>
> autocmd BufNewFile,BufRead *.foo set filetype=ignored
>
> But this leaves the files without any useful filetype at all. I
> suspect that I need to somehow unset the filetype and then tell vim to
> set it only from scripts.vim. However, I have no idea how to do this.
> Any help would be much appreciated.
>
> Alvin Kerber

:h new-filetype

You should do the checking right with the autocmd, because, as you
noticed, scripts.vim can't take action if the filetype is already set.


autocmd BufNewFile,BufRead *.foo call s:SetfFoo()

func! s:SetfFoo()
let line1 = getline(1)
if line1 =~ '...'
setf ...
elseif line1 =~ '...'
setf ...
endif
endfunc


If you need something that is easy to install, you can put the above
code in:
ftdetect/foo.vim
(early in the 'runtimepath'; no surrounding augroup command needed)


But you can also have your own filetype.vim script:


" allow users to ignore these settings:
if exists("did_load_filetypes")
finish
endif

augroup filetypedetect
autocmd BufNewFile,BufRead *.foo call s:SetfFoo()
augroup END

func! s:SetfFoo()
...
endfunc


A gentle :setf is usually preferred over a harsh :set ft=xyz, again to
please users who want their own filetype for *.foo files. When there
are several conflicting autocmds to detect *.foo files, the first :setf
will win (possibly defined by the user); otherwise the last :set ft=xyz
would win (defined by you in this case).

--
Andy

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