On Sun, 4 Jul 2010, Gary Johnson wrote:
> On 2010-07-04, Benjamin R. Haskell wrote:
> > On Sun, 4 Jul 2010, Gary Johnson wrote:
> >
> > > On 2010-07-03, John Little wrote:
> > > > Daniel D Jones wrote:
> > > >
> > > > Using Gvim under Gentoo Linux, if I open a file with a .txt
> > > > extension, Gvim sets the filetype to text and sets the appropriate
> > > > word wrap, etc. that I have configured. But if I open Gvim, create
> > > > a new buffer with :tabnew and then save that as a file with a .txt
> > > > extension, Gvim doesn't set the filetype...
> > > >
> > > > Gary replied:
> > > > > Vim assumes that you want to determine the file type when a file
> > > > > is opened or a new buffer is created, i.e., before you start
> > > > > viewing or editing, not when you write the file.
> > > >
> > > > I think that's quite wrong. I put
> > > >
> > > > au! BufRead,BufNewFile *.txt setfiletype text
> > > >
> > > > in my ~/.vim/filetype.vim according to :help new-filetype part C,
> > > > and upon :tabnew, :save (or :write) the file type is set to text.
> > >
> > > I stand corrected. I don't understand why the 'filetype' is being
> > > set, though. Some experimentation suggests that the BufRead event is
> > > being triggered by the :w command, but only when the BufRead
> > > autocommand is within the filetypedetect group. I don't get that from
> > > reading ":help BufRead" and it doesn't make sense to me. Why does
> > > writing a file trigger a BufRead event?
> >
> > Are you sure it's happening when the buffer is written, and not when the
> > buffer is created?
> >
> > Several of the BufNewFile autocmds in the filetypedetect group call
> > 'doau BufRead' (triggering their own BufRead events).
> >
> > Plus there's this entry: (among the long listing for :au filetypedetect)
> > filetypedetect BufNewFile
> > [...]
> > * if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat |
> > runtime! scripts.vim | endif
> >
> > I wouldn't be surprised if Daniel's ':tabnew, :save' is a ':tabnew
> > something.txt'?
>
> Here is one experiment.
>
> $ vim -N -u NONE
> :filetype on
> :aug filetypedetect
> :au BufRead * let g:x="hello"
> :aug END
> :echo x
> E121: Undefined variable: x
> E15: Invalid expression: x
> :w foo
> "foo" [New] 0L, 0C written
> :echo x
> hello
>
> My BufRead autocommand is clearly being triggered by my :w command.
The fact that 'foo' didn't already exist is key. (Try it with a 'touch
foo' beforehand.) The :w triggers a BufNewFile event. (It appears the
important filetypedetect group events are triggered on either BufRead or
BufNewFile.) So, the BufNewFile triggers the filetypedetect autocmds
that trigger their own BufRead events via :doau
--
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