Sunday, May 27, 2012

Re: Folding on markdown headers

On Sun, May 27, 2012 at 04:10:43PM EDT, Gary Johnson wrote:
> On 2012-05-26, Chris Jones wrote:

[..]

> > 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 opened the verbose.out file. Here are the first 23 lines from
> that file.

[..]

> | Executing BufRead Auto commands for "*.markdown"
> | autocommand setf markdown

[..]

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

Surprise.. surprise.. :-)

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

Not all that much. I suspected BufNewFile, and I guessed wrong. It looks
like the telltale bit in the name of these events is the 'Buf' prefix.
It's really all about Vim's internal management of buffers, more than
about the actual writing or loading of the file. And Vim does what's
more convenient... from the point of view of Vim.

In any case, thanks much for figuring this out and providing details re:
verbose/verbosefile. Made a good note of this technique for future use.

CJ

--
WHAT YOU SAY??

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