Sunday, September 18, 2011

Re: Remembering syntax when moving in and out of buffers

On Sun, 18 Sep 2011, Spiros Bousbouras wrote:

> On Sep 17, 10:25 pm, "Benjamin R. Haskell" wrote:
>> On Sat, 17 Sep 2011, Spiros Bousbouras wrote:
>>> If you define a syntax for a buffer , move away and then return is
>>> the syntax remembered or do all the syntax commands have to be
>>> executed again? Some tests I did indicate it's the latter.
>>
>> It should be the former.
>>
>>> Specifically (with simplifications) my .vimrc (on Linux) has
>>
>>> autocmd BufReadPost,BufNewFile *.myfile source ~/myfile.vim
>>
>>> and file ~/myfile.vim has
>>
>>> if exists("b:myfile")
>>> finish
>>> endif
>>
>>> let b:myfile = 1
>>> syntax match special /special/
>>> highlight special term=bold cterm=bold
>>
>> Either the simplifications have eliminated something important
>> (though I don't know what) or something else on your system is
>> interfering. The way you've set it up isn't the way a typical
>> filetype-related plugin should be laid out (see: :help new-filetype
>> ). The way you've coded it will work, but it will get messy,
>> quickly, if you want to add multiple filetypes.
>
> I did read :help new-filetype and I thought that the method
> mentioned there is too complicated that's why I didn't follow it. It
> seems much simpler to me to have different file types be recognised by
> extension and then load syntax and anything else I want by sourcing an
> appropriate script. Works well enough up to now but I only have 3-4
> different file types.

The problem is that the filetype is being detected again every time you
switch buffers. Apparently Debian (like most distros) has a bunch of
auto-detection on filetypes. With your:

:let &verbose=20

You can see there's an autocmd on BufRead that gets triggered every
time. It's conditioned on:

if !did_filetype()

And, since your filetype isn't set up the way Debian expects it (which
may or may not be the way Vim normally does it), did_filetype() returns
0.


>>> When I edit file.myfile the highlighting works correctly but if I
>>> move away and return then there's no highlighting.
>>
>> Does some plugin from your system set up some kind of autocmd on the
>> WinEnter, WinLeave, BufEnter, or BufLeave event(s)?
>>
>> :au WinEnter,WinLeave,BufEnter,BufLeave
>
> The above command returns
> --- Auto-Commands ---
>
> and nothing else. Apart from that , I have
> let &loadplugins=0
> in /etc/vim/vimrc so I don't think that any plugin is causing the
> behaviour.

I missed the guilty autocmd (BufRead), because I always set the 'hidden'
option. So, files aren't re-read everytime I unhide them -- they're
only read when they're first loaded. And I was testing by switching
between two windows, with both visible.


>
>> On my system, I see about a dozen BufEnter entries, but none that
>> would affect a *.myfile buffer. Two in the filetypedetect augroup,
>> one in FileExplorer, and the ones in no group are vimball-related.
>> WinEnter only lists an autocmd in the matchparen group.
>
> Here's a version of the problem with no simplifications:
> File a contains
> This is a special line
>
> File b is empty. I do
> vim -u NONE a b
> :let &verbose=20
> :syntax match special /special/
> :highlight special term=bold cterm=bold
>
> The word "special" gets highlighted.
> 2Ctrl-^
> "b" 0 lines, 0 characters
> Ctrl-^
> "a" 1 line, 23 characters
>
> Now "special" is no longer highlighted.
> :syntax
> No Syntax items defined for this buffer
> :syntax match special /special/
>
> The word "special" gets highlighted again. Note that this time I
> didn't have to enter the highlight command to get the highlighting.
>
>
> I would be especially interested if anyone who runs Debian Lenny would
> try the above test.

Is Debian Lenny the same as Debian 6? I don't use Debian, but I have
VM's of Debian 5 and 6 for testing things. Under 6 is where I observed
the BufRead problem.

I'm not sure which parts of this are necessary, but the following works
for me. Using your example with the 'a' and 'b' files, but moving 'a'
to 'a.myfile' (so it can be detected by extension):


1. Change your autocmd for detection from:

(old:) autocmd BufReadPost,BufNewFile *.myfile source ~/myfile.vim

to, either:

i. if you *really* want to keep it in vimrc (for some reason -- not
recommended, but it worked fine in testing):

aug filetypedetect
autocmd BufReadPost,BufNewFile *.myfile setf myfile
aug END

ii. or just put it in ~/.vim/ftdetect/myfile.vim (where you don't need
the augroup wrapper):

au BufReadPost,BufNewFile *.myfile setf myfile

" au is short for :autocmd
" setf is short for :setfiletype

2. And create a file ~/.vim/syntax/myfile.vim containing just the
following two lines:

syn match special /special/
hi special term=bold cterm=bold

Then it's properly detected for me under Debian 6.

--
Best,
Ben

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

Post a Comment