How to detect if an menu already exists?

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

How to detect if an menu already exists?

Bruce Who
Hi,

        I write function which is called in ftplugin scripts. This function adds a menu for buffers of this filetype. If user switch to another buffer, the previous one is deleted and the new one is created.

    But there is a snag. If we switch to another buffer with the same filetype, the function will try to create the same menu again, this cause an error!

    So I need a means to test if an menu already exists. But I don't know how. Could anybody figure me out?
               
    Any help would be appreciated!

Best regards,

????????????????Bruce Who
????????????????????2005-06-28
Reply | Threaded
Open this post in threaded view
|

Re: How to detect if an menu already exists?

A.J.Mechelynck
Bruce Who wrote:

> Hi,
>
> I write function which is called in ftplugin scripts. This function adds a menu for buffers of this filetype. If user switch to another buffer, the previous one is deleted and the new one is created.
>
>     But there is a snag. If we switch to another buffer with the same filetype, the function will try to create the same menu again, this cause an error!
>
>     So I need a means to test if an menu already exists. But I don't know how. Could anybody figure me out?
>
>     Any help would be appreciated!
>
> Best regards,
>
> ????????????????Bruce Who
> ????????????????????2005-06-28
>
>
>

Method A: (untested)
---------
1. Try using buffer-local menus. I'm not sure whether this works,
because it's not mentioned under ":help :menu". The latter, however,
resends to the ":map" command syntax, which can use

        :map <buffer> {lhs} {rhs}

to create a buffer-local mapping.

2. If #1 works (i.e., does not produce an error on the <buffer>
argument), use it for autocommand events which create the buffer (such
as BufReadPost and BufNewFile) rather than when switching buffers (such
as BufEnter).

Method B:
---------
Create the menu at the BufEnter event and delete it at the BufLeave
event. Thus when leaving one buffer for a different one of the same
type, the menu will be deleted and re-created, which ought to generate
no error.

Method C:
---------
When entering a buffer, delete the concerned menu, then re-create it.
Ignore or catch (see ":help :silent" and ":help :try") the case when an
error is generated because it doesn't yet exist.

Try method A first because if it works, it is more economical.


Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|

Re: How to detect if an menu already exists?

Bruce Who
In reply to this post by Bruce Who
Hi, Tony:

    Thanks for your help.

    How to create a buffer-local menu? I tried:

         menu <buffer> something.something :w<CR>

    and got E331.

    Method C is quite simple, and I choose that one.

======= 2005-06-28 11:37:27 A. J. Mechelynck wrote: =======

>Method A: (untested)
>---------
>1. Try using buffer-local menus. I'm not sure whether this works,
>because it's not mentioned under ":help :menu". The latter, however,
>resends to the ":map" command syntax, which can use
>
> :map <buffer> {lhs} {rhs}
>
>to create a buffer-local mapping.
>
>2. If #1 works (i.e., does not produce an error on the <buffer>
>argument), use it for autocommand events which create the buffer (such
>as BufReadPost and BufNewFile) rather than when switching buffers (such
>as BufEnter).
>
>Method B:
>---------
>Create the menu at the BufEnter event and delete it at the BufLeave
>event. Thus when leaving one buffer for a different one of the same
>type, the menu will be deleted and re-created, which ought to generate
>no error.
>
>Method C:
>---------
>When entering a buffer, delete the concerned menu, then re-create it.
>Ignore or catch (see ":help :silent" and ":help :try") the case when an
>error is generated because it doesn't yet exist.
>
>Try method A first because if it works, it is more economical.
>
>
>Best regards,
>Tony.
>
>.

= = = = = = = = = = = = = = = = = = = =
                       
Best regards,

????????????????Bruce Who
????????????????????2005-06-28

Reply | Threaded
Open this post in threaded view
|

Re: How to detect if an menu already exists?

A.J.Mechelynck
Bruce Who wrote:
> Hi, Tony:
>
>     Thanks for your help.
>
>     How to create a buffer-local menu? I tried:
>
>          menu <buffer> something.something :w<CR>
>
>     and got E331.

I said it was untested. Maybe the <buffer> modifier is allowed for
mappings but not for menus.

>
>     Method C is quite simple, and I choose that one.
>
[...]

... Whatever works for you ...


Best regards,
Tony.