RFC: Function to get information about menu items

37 views
Skip to first unread message

Yegappan Lakshmanan

unread,
Mar 11, 2020, 11:30:19 AM3/11/20
to vim_dev
Hi all,

When trying to develop tests for the menu functionality, I realized
that there is no function to get information about the menu items.
As part of PR #5760, I have added a new menu_getprop() function
that returns information about the specified menu item(s).
The help text for this new function is below. Let me know if you have
any comments/suggestions.

Thanks,
Yegappan

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

menu_getprop([{name}])

Returns the properties of the specified menu item {name}.
If {name} is not specified or empty, then returns the
properties of all the menu items. The menu item name should be
specified without the shortcut character ('&').  Returns a
|Dictionary| containing the following items:

         accel menu item accelerator text |menu-text|
         display display name (name without '&')
         icon name of the icon file (for toolbar) |toolbar-icon|
         modes |Dictionary| containing mode-specific
                     information for this menu item. See
                     below for more information. |map-modes|
        name menu item name
        priority menu order priority |menu-priority|
        shortcut shortcut key (character after '&' in
                      the menu name) |menu-shortcut|
        submenus |List| containing the properties of
                          all the submenus. Each list item is a
                          Dictionary with the above items.
                          Present only if the menu item has submenus.

The {modes} field is a |Dictionary| containing the following
fields:

       enabled v:true if this menu item is enabled
                     Refer to |:menu-enable|
       noremenu 1 if remapping in {rhs} is allowed.
                        2 if remapping in {rhs} is not allowed
                        3 if script-local remapping is allowed
                        See |:menu-script|
       rhs right-hand-side of the menu item
       silent v:true if the menu item is created
                with <silent> argument |:menu-silent|

For a top-level menu, the {modes} field will not be present.

Returns an empty dictionary on error.

Examples: >
        :echo menu_getprop('Edit.Cut')
        :echo menu_getprop('File')
        :echo menu_getprop()
<

Can also be used as a |method|: >
       GetMenuName()->menu_getprop()

Bram Moolenaar

unread,
Mar 11, 2020, 2:17:36 PM3/11/20
to vim...@googlegroups.com, Yegappan Lakshmanan

Yegappan wrote:

> When trying to develop tests for the menu functionality, I realized
> that there is no function to get information about the menu items.
> As part of PR #5760, I have added a new menu_getprop() function
> that returns information about the specified menu item(s).
> The help text for this new function is below. Let me know if you have
> any comments/suggestions.

Menus are very much like mappings. Can we make this work more or less
like maparg() with the {dict} argument TRUE?


--
Get a life? What is the URL where it can be downloaded?

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Yegappan Lakshmanan

unread,
Mar 11, 2020, 9:45:13 PM3/11/20
to Bram Moolenaar, vim_dev
Hi Bram,

On Wed, Mar 11, 2020 at 11:17 AM Bram Moolenaar <Br...@moolenaar.net> wrote:

Yegappan wrote:

> When trying to develop tests for the menu functionality, I realized
> that there is no function to get information about the menu items.
> As part of PR #5760, I have added a new menu_getprop() function
> that returns information about the specified menu item(s).
> The help text for this new function is below. Let me know if you have
> any comments/suggestions.

Menus are very much like mappings.  Can we make this work more or less
like maparg() with the {dict} argument TRUE?


One difference compared to the maps is that a given menu can have
submenus. So we need to return information about the submenus also.
Otherwise it will be difficult to iterate over all the submenus of a given
menu item.

Regards,
Yegappan 

Bram Moolenaar

unread,
Mar 12, 2020, 2:43:05 PM3/12/20
to vim...@googlegroups.com, Yegappan Lakshmanan

Yegappan wrote:

> > > When trying to develop tests for the menu functionality, I realized
> > > that there is no function to get information about the menu items.
> > > As part of PR #5760, I have added a new menu_getprop() function
> > > that returns information about the specified menu item(s).
> > > The help text for this new function is below. Let me know if you have
> > > any comments/suggestions.
> >
> > Menus are very much like mappings. Can we make this work more or less
> > like maparg() with the {dict} argument TRUE?
> >
> >
> One difference compared to the maps is that a given menu can have
> submenus. So we need to return information about the submenus also.
> Otherwise it will be difficult to iterate over all the submenus of a given
> menu item.

Sure, it won't be exactly the same.

I think we always identify menus by their name, or path of names. Thus
the submenus could be returned as a list of names. You could then get
more info by using the function on the root name plus the submenu name.
That would need to be supported anyway. Does that sound right?

--
Marriage isn't a word. It's a sentence.

Yegappan Lakshmanan

unread,
Mar 13, 2020, 4:22:21 PM3/13/20
to Bram Moolenaar, vim_dev
Hi Bram,

On Thu, Mar 12, 2020 at 11:43 AM Bram Moolenaar <Br...@moolenaar.net> wrote:

Yegappan wrote:

> > > When trying to develop tests for the menu functionality, I realized
> > > that there is no function to get information about the menu items.
> > > As part of PR #5760, I have added a new menu_getprop() function
> > > that returns information about the specified menu item(s).
> > > The help text for this new function is below. Let me know if you have
> > > any comments/suggestions.
> >
> > Menus are very much like mappings.  Can we make this work more or less
> > like maparg() with the {dict} argument TRUE?
> >
> >
> One difference compared to the maps is that a given menu can have
> submenus. So we need to return information about the submenus also.
> Otherwise it will be difficult to iterate over all the submenus of a given
> menu item.

Sure, it won't be exactly the same.

I think we always identify menus by their name, or path of names.  Thus
the submenus could be returned as a list of names.  You could then get
more info by using the function on the root name plus the submenu name.
That would need to be supported anyway.  Does that sound right?

Yes. I have updated the PR to add the menu_info() function which is modeled
after the maparg() function incorporating your comments.

Regards,
Yegappan

Yegappan Lakshmanan

unread,
Mar 15, 2020, 1:20:50 AM3/15/20
to Bram Moolenaar, vim_dev
Hi Bram,

The updated help text for the menu_info() function is below. I have updated
the PR with this change and the new tests for menu.c. It is now ready for
inclusion.

Regards,
Yegappan

===================================================================
menu_info({name} [, {mode}]) *menu_info()*  
Return information about the specified menu {name} in mode {mode}. The menu
name should be specified without the shortcut character ('&').

    {mode} can be one of these strings:
        "n" Normal
        "v" Visual (including Select)
        "o" Operator-pending
        "i" Insert
        "c" Cmd-line
        "s" Select
        "x" Visual
        "t" Terminal-Job
        "" Normal, Visual and Operator-pending
       "!" Insert and Cmd-line

When {mode} is omitted, the modes for "" are used.


Returns a |Dictionary| containing the following items:

    accel menu item accelerator text |menu-text|
    display display name (name without '&')
    enabled v:true if this menu item is enabled
                 Refer to |:menu-enable|
    icon name of the icon file (for toolbar) |toolbar-icon|
    iconidx index of a built-in icon
    modes modes for which the menu is defined. In addition 
                to the modes mentioned above, these characters
                will be used:
                       " " Normal, Visual and Operator-pending
    name menu item name.
    noremenu v:true if the {rhs} of the menu item is not
                     remappable else v:false.

    priority menu order priority |menu-priority|
    rhs right-hand-side of the menu item. The returned
          string has special characters translated like
          in the output of the ":menu" command listing.
          When the {rhs} of a menu item is empty, then
          "<Nop>" is returned.
    script v:true if script-local remapping of {rhs} is
             allowed else v:false.  See |:menu-script|.

    shortcut shortcut key (character after '&' in
                 the menu name) |menu-shortcut|
    silent v:true if the menu item is created
             with <silent> argument |:menu-silent|
    submenus |List| containing the names of
                     all the submenus.  Present only if the menu
                     item has submenus.

Returns an empty dictionary if the menu item is not found.

Examples: >
    :echo maparg('Edit.Cut')
    :echo maparg('File.Save', 'n')

<
Can also be used as a |method|: >
    GetMenuName()->maparg('v')

Reply all
Reply to author
Forward
0 new messages