Wednesday, September 21, 2011

Re: Remembering syntax when moving in and out of buffers

On Sep 18, 11:14 pm, "Benjamin R. Haskell" <vim@benizi.com> wrote:
> On Sun, 18 Sep 2011, Spiros Bousbouras wrote:
> > On Sep 18, 5:45 pm, "Benjamin R. Haskell" wrote:
> >> On Sun, 18 Sep 2011, Spiros Bousbouras wrote:
>
> >> 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.

Actually I have disabled running the Debian specific stuff in
/etc/vim/vimrc so the explanation even for what I describe in my
opening post must be the same as below. I tried again running what I
describe in the opening post with verbose set to 20 and the only
autocommand executed is the one I set up explicitly , there's no
if !did_filetype() .

> > I'm afraid I don't follow your explanation. Don't the Debian specific
> > stuff become irrelevant when I do vim -u NONE ? Or do you mean that
> > Debian have modified the source of vim before compiling so that even
> > when I start vim with -u NONE some filetype detection stuff still
> > happens ? In the experiment with vim -u NONE a b I described why
> > is it that when I return to a then issuing only the syntax command
> > (but not the highlight command) turns on highlighting ? And I didn't
> > see any autocommands being executed either , the output was exactly as
> > I described it.
>
> I wasn't using -u NONE, sorry. I was explaining what goes wrong without
> it. In the -u NONE case that you present, it gets cleared because
> syntax starts fresh prior to loading each new buffer, so that filetype
> detection can detect the file and load the proper syntax without having
> to worry about clearing out the syntax of whatever other file or files
> are open.

Right. This brings us back to what I was saying in the OP namely that
it is a strange design choice. Note in particular that buffer local
variables are remembered yet syntax is not. As I understand it ,
syntax is buffer specific. So why would syntax from other files have
to be cleared and why would proper syntax have to be reloaded ? I
can't think of a scenario where I'm editing a buffer , set up a
syntax , move away , return to the buffer and now a different syntax
has to be valid.

> There are a few ways to avoid having Vim load the buffer from scratch
> each time you switch to it. One way is to set the 'hidden' option.
> Another is to set bufhidden=hide. Both of those alter how buffer
> management works slightly. (You can switch away from a buffer that
> contains unsaved changes, for example, though Vim will prompt you about
> that if you try to exit.)

I have autowriteall set. Up to today I had &hidden == 0 and when I
moved away from a buffer the buffer was automatically saved. With
&hidden == 1 this no longer happens. Is there a way this can bite
me ? I don't think there is but just to make sure. In particular ,
doing ZZ or :q still seems to save all my buffers.

> >>> 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.
>
> > Debian 6 is squeeze , Debian 5 is lenny.
>
> Works the same on Lenny for me as it did on Squeeze.
>
>
>
> >> 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.
>
> > Thanks but if these are the alternatives then it's simpler just to
> > reexecute the syntax commands every time the buffer is loaded.
>
> Maybe I'm missing something, but my suggestion is simpler than (or at
> worst "as simple as") what you're already doing. And it has the benefit
> of being better-contained: All your Vim-related settings are in your
> ~/.vimrc file and ~/.vim directory (and its subdirs).

I do actually have a ~/vim-scripts directory where I have all my ,
you guessed it , vim scripts ! Not mentioning it was one of the
"simplifications" of my OP i.e. I was leaving out irrelevant details.
I chose the name vim-scripts a while ago when I didn't know that
~/.vim has special significance.

> = What you said you were 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 you could be doing (one version): =
>
> 1. in your .vimrc (3 lines):
>
> aug filetypedetect
> au BufReadPost,BufNewFile *.myfile setf myfile
> aug END
>
> " The aug[roup] might not be necessary, but it makes management easier
>
> 2. put ~/myfile.vim into a different directory ( ~/.vim/syntax/ ) and
> remove all but the last two lines (2 lines):
>
> syntax match special /special/
> highlight special term=bold cterm=bold

Your version seems to me more roundabout than mine. Instead of
directly sourcing the file you want sourced you set some option and
then vim on its own will source the file. So what does the
intermediate step of setting the filetype option buy you ? Nothing
that I can see.

Another thing I don't like about your version is the change of the
flow of execution which happens behind the scenes. This is something
I'm very wary of in any language. I don't like it when the natural
flow of execution (i.e. commands get executed or expressions
evaluated one after the other as they appear in the file) gets
changed in a manner which is not immediately obvious. :source somefile
is explicit but setting an option is not because setting most vim
options does not change the flow of execution.

Finally , your version ties you up to a directory having a specific
name.

The only thing your version has over mine is that you avoid the
if exists("b:myfile") guard but in my full script I need this
anyway for reasons apart from highlighting. And your version also has
the aug filetypedetect part. So all things considered I would say
that your version is more complicated than mine.

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

All the comments above (apart from using aug filetypedetect) still
apply. And here you need 3 directories having specific names , ~/.vim
, ~/.vim/ftdetect and ~/.vim/syntax .

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