Monday, August 24, 2009

Re: Test Existence of Autocommand?

On Fri, Aug 14, 2009 at 6:38 PM, Edward Beach<beach9000@hotmail.com> wrote:

> I'm trying to write a script that will toggle an autocommand on/off
> and I'm looking for a function to return it's existence.  I've found
> the built-in exists function and from the help it gives me this:
>                        #group#event    autocommand defined for this group and event.
> So from my command line I enter in the following
>        :aug TestGroup | aug END | echo exists("#TestGroup#BufWritePost")
> and it comes back 1 -- but shouldn't it be 0 since I haven't created a
> BufWritePost event and since
>        :au TestGroup
> returns nothing?

It looks like the #group#event code doesn't check for an autocommand
for that event being defined *within* the group, just whether both the
group and the autocommand exist.  The #group#event#pattern does check
for an autocommand defined within the specified group, though, so that
may be helpful to you.

It looks like the following patch changes the behavior to that which
you expected.  Warning:  this is a very naive change, and I have only
tested your single test case.

--- a/src/fileio.c
+++ b/src/fileio.c
@@ -9500,7 +9500,21 @@ au_exists(arg)
       goto theend;
    if (pattern == NULL)
    {
-       retval = TRUE;
+       if (group == AUGROUP_ALL)
+       {
+           retval = TRUE;
+       }
+       else
+       {
+           for ( ; ap != NULL; ap = ap->next)
+               /* only use a pattern when it has not been removed and has
+                * commands and is defined for the current group. */
+               if (ap->pat != NULL && ap->cmds != NULL && ap->group == group)
+               {
+                   retval = TRUE;
+                   break;
+               }
+       }
       goto theend;
    }

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

No comments: