On 2012-05-26, Chris Jones wrote:
> On Sat, May 26, 2012 at 03:50:24PM EDT, Gary Johnson wrote:
> > On 2012-05-25, Chris Jones wrote:
> > 
> > > There are things I find a little unclear regarding autocommands but
> > > assuming the file extension is something like ‘*.mkdwn’ shouldn't it be
> > > possible to achieve this automatically when creating the file by placing
> > > something like this in a file named ~/.vim/ftdetect/markdown.vim:
> > > 
> > > | autocmd BufNewFile,BufRead *.mkdwn set filetype=markdown
> > > 
> > > Wouldn't this cause ‘:w myfile.mkdwn’ to automatically enable the
> > > markdown foldexpr and create the folds in one pass?
> > 
> > No.  BufNewFile will set the filetype to markdown when starting to
> > edit a new file with a name ending in .mkdwn; 
> 
> That's also how I understood it, yet..
> 
> I tested with a markdown file and BufNewFile and BufRead were the only
> two events defined to Vim. Non-customized Vim 7.3, no plugins, etc.
> 
> This is what I see in filetype.vim:
> 
> " Markdown
> au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,README.md setf markdown
> 
> I started a Vim session by typing ‘vim’ at the prompt and I entered some
> markdown in the [No Name] buffer.
> 
> At this point ‘set ft?’ said ‘filetype=’..
> 
> Now as soon as I had written the file to disk as ‘sample.mdown’ or
> .mkd.. .mkdn.. etc., ft was correctly set: ‘filetype=markdown’.
> 
> so is it the BufNewFile or the BufRead event that causes the filetype to
> be set? Between the two, I would tend to think it is BufNewFile, not
> because the file is written to disk, but rather because the Vim buffer
> now has a name.
> 
> I don't know if is meant to work this way (the doc would appear to
> suggest otherwise)  but I suspect that the BufNewFile event is also
> triggered when you issue a ‘:w’ command against a scratch buffer.

I don't know what to make of this at this point.  After discovering
that I was wrong in my understanding of what happens when you :write
and unnamed buffer to a file, and after reading your posts, I did
some experimenting, too.

I started vim and enabled syntax highlighting and filetype detection,

    $ vim -N -u NONE
    :syntax enable

entered a bit of markdown text into the buffer, then executed

    :set verbosefile=verbose.out
    :set verbose=9

and wrote the file:

    :w myfile.markdown

As soon as I did that, the markdown headers in the buffer became
highlighted, as we've seen before.  Then I quit vim,

    :set verbose=0
    :q

and opened the verbose.out file.  Here are the first 23 lines from
that file.

--------------------------------------------------------------------



chdir(/home/gary)
fchdir() to previous dir
chdir(/home/gary)
fchdir() to previous dir
Executing BufRead Auto commands for "*.markdown"
autocommand setf markdown

Executing FileType Auto commands for "*"
autocommand exe "set syntax=" . expand("<amatch>")

Executing Syntax Auto commands for "*"
autocommand call s:SynSet()

Searching for "syntax/markdown.vim syntax/markdown/*.vim" in 
"/home/gary/.vim,/usr/local/share/vim/vimfiles,/usr/local/share/vim/vim73,/usr/local/share/vim/vimfiles/after,/home/gary/.vim/after"
Searching for "/home/gary/.vim/syntax/markdown.vim"
Searching for "/home/gary/.vim/syntax/markdown/*.vim"
Searching for "/usr/local/share/vim/vimfiles/syntax/markdown.vim"
Searching for "/usr/local/share/vim/vimfiles/syntax/markdown/*.vim"
Searching for "/usr/local/share/vim/vim73/syntax/markdown.vim"
chdir(/usr/local/share/vim/vim73/syntax)
...
--------------------------------------------------------------------

It appears that filetype detection was triggered by the BufRead
autocommand!  Why on Earth would the BufRead autocommand be
triggered by a :write?

I searched the source code and found the following in the do_write()
function in ex_cmds.c:

    /* If 'filetype' was empty try detecting it now. */
    if (*curbuf->b_p_ft == NUL)
    {
        if (au_has_group((char_u *)"filetypedetect"))
            (void)do_doautocmd((char_u *)"filetypedetect BufRead",
                                                                TRUE);
        do_modelines(0);
    }

So not all BufRead autocommands are triggered by a :write; only
those in the filetypedetect group.

That behavior still seems strange, but now we know.

Regards,
Gary

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

Reply via email to