Monday, September 19, 2011

Re: Remembering syntax when moving in and out of buffers

On 19/09/11 01:35, Benjamin R. Haskell wrote:
> On Mon, 19 Sep 2011, Tony Mechelynck wrote:
>
>> On 19/09/11 00:14, Benjamin R. Haskell wrote:
>>> = What [the OP was] doing: =
>>>
>>> 1. in your .vimrc (1 line):
>>>
>>> autocmd BufReadPost,BufNewFile *.myfile source ~/myfile.vim
>>>
>>> 2. in ~/myfile.vim (6 lines of code):
>>>
>>> if exists("b:myfile")
>>> finish
>>> endif
>>>
>>> let b:myfile = 1
>>> syntax match special /special/
>>> highlight special term=bold cterm=bold
>>>
>>>
>>> = What [the OP] could be doing (one version): =
>>>
>>> 1. in your .vimrc (3 lines):
>>
>> these 3 lines actually belong in ~/.vim/filetype.vim (for Unix) or in
>> ~/vimfiles/filetype.vim (for Windows) or in $VIM/vimfiles/filetype.vim
>> (for access by any user, on any OS) — in all cases, create missing
>> directories as you go along. The "other version" below (replacing
>> filetype.vim by ftdetect/myfile.vim and doing away with the augroup,
>> which Vim sets before sourcing everything in the ftdetect directory)
>> would also work.
>
> Interesting. I didn't know about ~/.vim/filetype.vim.
>
>
>> This way this autocommand will be correctly forgotten and remembered
>> back if ever you do ":filetype off" and later ":filetype on".
>>
>> See ":help new-filetype".
>
> This wasn't the version I was advocating. But, since the OP implied that
> creating the proper directories was more complex ("Thanks but if these
> are the alternatives then it's simpler to just [keep things as
> before]"), I was providing a way that would work without having to
> create more directories.
>
> For the record, my suggested "= What you could be doing (the other
> version): =" is the one I actually recommend (and is what new-filetype
> seems to advocate):

The way I understand it, having one filetype-detection script per
filetype and dropping them all in a ftdetect subfolder is the way to go
if you distribute your package (maybe as a vimball containing
ftdetect/foobarbaz.vim, ftplugin/foobarbaz.vim, syntax/foobarbaz.vim,
autoload/foobarbazFileHandlers.vim, autoload/foobarbazOptionsMenu.vim,
etc.). When defining filetypes for my own use only, I find it simpler
(but YMMV) to add just one filetype-detection autocommand per filetype
in a common ~/.vim/filetype.vim (single-user) and/or
$VIM/vimfiles/filetype.vim (system-wide), bracketed in both cases by
:augroup ftdetect ... :augroup END

I did not notice the ftdetect/ possibility immediately when coming to
Vim, either (not sure which) because it didn't yet exist at the time, or
because I imitated what Vim already does by means of
$VIMRUNTIME/filetype.vim (which users should of course not modify
in-place because, like anything under $VIMRUNTIME, it may be silently
replaced by any update, thus losing your house changes if you had any).

>
>
>>> = What you could be doing (the other version): =
>>>
>>> 1. in ~/.vim/ftdetect/myfile.vim (1 line):
>>>
>>> au BufReadPost,BufNewFile *.myfile setf myfile
>>>
>>> 2. put the last two lines of ~/myfile.vim into ~/.vim/syntax/myfile.vim
>>> (2 lines):
>>>
>>> syn match special /special/
>>> hi special term=bold cterm=bold


Best regards,
Tony.
--
It is one of the superstitions of the human mind to have imagined that
virginity could be a virtue.
-- Voltaire

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