Elliot Shank wrote:
What I'm trying to do is create a plugin that acts based upon the
contents of an arbitrary file buffer as soon after it has loaded its
contents as possible.
For existing files, using a BufReadPost autocmd is fine.
For new file buffers that have text loaded into them via a BufNewFile
autocmd, I can't just create a BufNewFile autocmd myself because I can't
guarantee that mine will run last.
So, the obvious thing to do is use BufEnter and/or BufWinEnter.
However, I've got other plugins that will need the result of the new
plugin and that want to use BufEnter and BufWinEnter themselves, which
leads me back to the ordering problem. I can't make sure that the
plugin will run first.
It looks like FileType and Syntax events can occur between BufNewFile
and BufWinEnter, but this only happens for files that a file type can be
figured out for. If I edit a non-existent file called "blah", there
aren't any events between BufNewFile and BufWinEnter.
Any suggestions?
To run an autocommand after the last of the Syntax autocommands have
been run, define it at the VimEnter event:
au VimEnter * au Syntax foobar call MyFoobarFunc()
Similarly, to run it last at the BufNewFile or BufReadPost events:
au VimEnter * au BufRead,BufNewFile *.foo call MyFooFunc()
(Note: BufRead and BufReadPost are synonymous).
The filetype detection mechanism (:filetype on) sets up a lot of
autocommands for the BufRead and BufNewFile autocommands, to set the
filetype. Setting the filetype triggers the FileType event, and if
syntax highlighting is on (:syntax on) there is a "Filetype *"
autocommand to set the syntax to the same as the filetype. In turn,
setting 'syntax' triggers the Syntax autocommand.
BufWinEnter is triggered when a buffer gets displayed in a window, even
if it is already displayed in another window (e.g. after ":split" with
no arguments). Thus it can be triggered more than once for the same
buffer, while BufReadPre, BufRead, Filetype and Syntax (among others)
are normally triggered only once in that order. (Of course, if the
filetype cannot be detected for some file, neither Filetype nor Syntax
will fire).
Best regards,
Tony.