Best event to use in autocmd to modify parts of the file

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Best event to use in autocmd to modify parts of the file

Raúl Núñez de Arenas Coronado

Hi all:)

I want to automatically modify the format of the attribution line and
some details of the signature when replying to emails. This can be done
using a couple of simple "s" commands. My problem is on the
"automatically" part...

For doing the "automatic edition" I've set an autocommand:

autocmd BufRead *.eml /pattern/s/something/otherthing/

I've put this in "~/.vim/after/ftplugin/mail.vim

The *.eml file are properly detected as "mail" and the rest of lines of
"~/.vim/after/ftplugin/mail.vim" are properly executed. In fact, the
autocommand appear when doing ":au BufRead *.eml", but the autocommand
is not run.

Just in case the problem was on the command, I tried this:

autocmd BufRead *.eml echo "BufRead hit!"

Still, it doesn't work.

I don't want to use "BufEnter" because after the substitution has been
done it won't match again, so it doesn't make sense running the auto
command every time I enter the buffer.

What am I doing wrong? Is BufRead the problem and then I should use
another event? Is the placement the problema and I should put the
autocommand in another file? Given that I only want to do the
substitution once at the beginning: should I get rid of autocommands and
do the job directly in "~/.vim/after/ftplugin/mail.vim" (which is what
I'm doing right now)?

Thanks a lot in advance :)

Raúl "DervishD" Núñez de Arenas Coronado

Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Best event to use in autocmd to modify parts of the file

Teemu Likonen

On 2009-02-24 19:06 (+0100), Raúl Núñez de Arenas Coronado wrote:

> I want to automatically modify the format of the attribution line and
> some details of the signature when replying to emails. This can be
> done using a couple of simple "s" commands. My problem is on the
> "automatically" part...
>
> For doing the "automatic edition" I've set an autocommand:
>
> autocmd BufRead *.eml /pattern/s/something/otherthing/
>
> I've put this in "~/.vim/after/ftplugin/mail.vim

If you add an autocmd in ftplugin and buffer's context it would probably
be better idea to add buffer-local autocmd:

    augroup MyShinyGroup
            autocmd! * <buffer> " Remove possible previous ones
            autocmd! WhateverEvent <buffer> [...]
    augroup END

Or maybe conditionally:

    if expand('%:t:e') == 'eml'
            augroup MyShinyGroup
                    autocmd! * <buffer>
                    autocmd! WhateverEvent <buffer> [...]
            augroup END
    endif

Buffer-local autocmds in filetype plugins are usually necessary because
user may edit several files of same filetype.

But...

> Given that I only want to do the substitution once at the beginning:
> should I get rid of autocommands and do the job directly in
> "~/.vim/after/ftplugin/mail.vim" (which is what I'm doing right now)?

...in your case I think autocmds are not useful. Just have the ftplugin
check that this is the right kind of file and modify the buffer.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Best event to use in autocmd to modify parts of the file

Benjamin Fritz
In reply to this post by Raúl Núñez de Arenas Coronado



On Feb 24, 12:06 pm, Raúl Núñez de Arenas Coronado <[hidden email]>
wrote:

>
> I want to automatically modify the format of the attribution line and
> some details of the signature when replying to emails. This can be done
> using a couple of simple "s" commands. My problem is on the
> "automatically" part...
>
> For doing the "automatic edition" I've set an autocommand:
>
> autocmd BufRead *.eml /pattern/s/something/otherthing/

You mean g/pattern/s/... or %s/..., right?

>
> I've put this in "~/.vim/after/ftplugin/mail.vim
>
> The *.eml file are properly detected as "mail" and the rest of lines of
> "~/.vim/after/ftplugin/mail.vim" are properly executed. In fact, the
> autocommand appear when doing ":au BufRead *.eml", but the autocommand
> is not run.

Correct. The buffer completely read before setting the filetype.
"BufRead" is the wrong event.

>
> I don't want to use "BufEnter" because after the substitution has been
> done it won't match again, so it doesn't make sense running the auto
> command every time I enter the buffer.
>

Right, this is probably a bad idea.

> What am I doing wrong? Is BufRead the problem and then I should use
> another event? Is the placement the problema and I should put the
> autocommand in another file? Given that I only want to do the
> substitution once at the beginning: should I get rid of autocommands and
> do the job directly in "~/.vim/after/ftplugin/mail.vim" (which is what
> I'm doing right now)?
>

One method would be as you suggest: ditch the autocmd. If you want to
use an autocmd, the following events might work:

BufWinEnter
Filetype
BufWritePre
BufRead (from your .vimrc instead of from an ftplugin file)
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Best event to use in autocmd to modify parts of the file

Raúl Núñez de Arenas Coronado
In reply to this post by Teemu Likonen

Hi Teemu :)

On Tue, Feb 24, 2009 at 19:42, Teemu Likonen <[hidden email]> dixit:

> On 2009-02-24 19:06 (+0100), Raúl Núñez de Arenas Coronado wrote:
>
>> I want to automatically modify the format of the attribution line and
>> some details of the signature when replying to emails. This can be
>> done using a couple of simple "s" commands. My problem is on the
>> "automatically" part...
>>
>> For doing the "automatic edition" I've set an autocommand:
>>
>> autocmd BufRead *.eml /pattern/s/something/otherthing/
>>
>> I've put this in "~/.vim/after/ftplugin/mail.vim
>
> If you add an autocmd in ftplugin and buffer's context it would probably
> be better idea to add buffer-local autocmd:

Well, that was my first try but it didn't work either (the autocommand
was not run). I thought that the <buffer> part was not defined at the
time the autocommand was defined and that it was the reason why it was
not working.

Probably the problem was, in that case, that the event I was using
(BufRead) was the wrong one.

>> Given that I only want to do the substitution once at the beginning:
>> should I get rid of autocommands and do the job directly in
>> "~/.vim/after/ftplugin/mail.vim" (which is what I'm doing right
>> now)?
>
> ..in your case I think autocmds are not useful. Just have the ftplugin
> check that this is the right kind of file and modify the buffer.

Right now I'm doing it exactly in that way and it works, so I suppose
I'm going to do that instead of using autocmd.

Thanks!

Raúl "DervishD" Núñez de Arenas Coronado

Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Best event to use in autocmd to modify parts of the file

Raúl Núñez de Arenas Coronado
In reply to this post by Benjamin Fritz

Hi Ben!

On Tue, Feb 24, 2009 at 19:48, Ben Fritz dixit:

> On Feb 24, 12:06 pm, Raúl Núñez de Arenas Coronado <[hidden email]>
> wrote:
>>
>> I want to automatically modify the format of the attribution line and
>> some details of the signature when replying to emails. This can be done
>> using a couple of simple "s" commands. My problem is on the
>> "automatically" part...
>>
>> For doing the "automatic edition" I've set an autocommand:
>>
>> autocmd BufRead *.eml /pattern/s/something/otherthing/
>
> You mean g/pattern/s/... or %s/..., right?

No, I mean /pattern/s/... That is, perform the substitution only on the
first line that matches that pattern. This is for tweaking the
attribution line (to get rid of email addresses, for example and not
including them in the reply) and I want to change only the first
attribution line, which will be the one inserted by the email client.

>> What am I doing wrong? Is BufRead the problem and then I should use
>> another event? Is the placement the problema and I should put the
>> autocommand in another file? Given that I only want to do the
>> substitution once at the beginning: should I get rid of autocommands and
>> do the job directly in "~/.vim/after/ftplugin/mail.vim" (which is what
>> I'm doing right now)?
>
> One method would be as you suggest: ditch the autocmd.

I'll do that by now.

> If you want to use an autocmd, the following events might work:
>
> BufWinEnter

Yep, I forgot this one, probably due to the name. It sounds like "this
autocommand will be run everytime you enter the window the buffer is
in".

> Filetype

I didn't notice this one in the documentation...

> BufWritePre

This won't work for me, because I want to see the result of the
substitution, just in case it wasn't done right. I don't want to build a
very complex regex to match the attribution line and right now I'm using
a very simple one.

Thanks a lot for the advice :)

Raúl "DervishD" Núñez de Arenas Coronado

Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Best event to use in autocmd to modify parts of the file

Gary Johnson-4
In reply to this post by Raúl Núñez de Arenas Coronado

On 2009-02-24, Raúl Núñez de Arenas Coronado <[hidden email]> wrote:

> Hi all:)
>
> I want to automatically modify the format of the attribution line and
> some details of the signature when replying to emails. This can be done
> using a couple of simple "s" commands. My problem is on the
> "automatically" part...
>
> For doing the "automatic edition" I've set an autocommand:
>
> autocmd BufRead *.eml /pattern/s/something/otherthing/
>
> I've put this in "~/.vim/after/ftplugin/mail.vim
>
> The *.eml file are properly detected as "mail" and the rest of lines of
> "~/.vim/after/ftplugin/mail.vim" are properly executed. In fact, the
> autocommand appear when doing ":au BufRead *.eml", but the autocommand
> is not run.
>
> Just in case the problem was on the command, I tried this:
>
> autocmd BufRead *.eml echo "BufRead hit!"
>
> Still, it doesn't work.
>
> I don't want to use "BufEnter" because after the substitution has been
> done it won't match again, so it doesn't make sense running the auto
> command every time I enter the buffer.
>
> What am I doing wrong? Is BufRead the problem and then I should use
> another event? Is the placement the problema and I should put the
> autocommand in another file? Given that I only want to do the
> substitution once at the beginning: should I get rid of autocommands and
> do the job directly in "~/.vim/after/ftplugin/mail.vim" (which is what
> I'm doing right now)?

The problem is your use of an autocommand within an ftplugin file.
Ftplugin files are sourced as a result of a FileType autocommand,
which was triggered by a BufNewFile or BufRead autocommand.
Therefore, by the time the ftplugin is read, the filetype-dependent
autocommand event has already occurred; any filetype-dependent
autocommand defined in your ftplugin won't be triggered.

If you want to execute some command as a result of loading a
particular type of file, EITHER put the command in a filetype
plugin, OR put the command in an autocommand in your ~/.vimrc.
Don't use an autocommand in a filetype plugin.

The only reason to use an autocommand within a filetype plugin is to
catch some event that occurs after the filetype plugin is sourced.

In your particular case, just put

    /pattern/s/something/otherthing/

in ~/.vim/after/ftplugin/mail.vim, as you said you're doing at the
moment.

Regards,
Gary



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Best event to use in autocmd to modify parts of the file

Raúl Núñez de Arenas Coronado

Saluton Gary Johnson :)

On Tue 24 Feb 2009 @20:22 +0100 Gary Johnson dixit:
> The only reason to use an autocommand within a filetype plugin is to
> catch some event that occurs after the filetype plugin is sourced.
>
> In your particular case, just put
>
>    /pattern/s/something/otherthing/
>
> in ~/.vim/after/ftplugin/mail.vim, as you said you're doing at the
> moment.

Thanks for confirming. You may not notice if you're not used to the
default GMail attributions, but the attribution of this email has been
tweaked (changed date format, added timezone, no email address) and a
salutation has been included, using no autocommands, just adding the
needed commands to ~/.vim/after/ftplugin/mail.vim

Raúl "DervishD" Núñez de Arenas Coronado

Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---