Skip to content

Commit b43fee4

Browse files
committed
Fix :emenu crash when it's associated with a macaction in a non-valid mode
The existing emenu code has this odd exception where if you invoke it on a menu with <Nop> bound in Vim, and has a macaction, it will call the macaction instead. However, it's not properly handling the situation when the menu is not bound for the mode and would crash when deferencing a null string ptr. Move it to the proper spot and fix up a previous upstream merge mistake (in Vim upstream, when you call :emenu on a <Nop> menu item it just does nothing now, instead of complaining about it) so that it all works correctly.
1 parent f1749a2 commit b43fee4

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/menu.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,9 +2376,24 @@ execute_menu(exarg_T *eap, vimmenu_T *menu, int mode_idx)
23762376
if (idx == MENU_INDEX_INVALID || eap == NULL)
23772377
idx = MENU_INDEX_NORMAL;
23782378

2379-
if (menu->strings[idx] != NULL && menu->strings[idx][0] != NUL
2380-
&& (menu->modes & (1 << idx)))
2379+
if (menu->strings[idx] != NULL && (menu->modes & (1 << idx)))
23812380
{
2381+
#ifdef FEAT_GUI_MACVIM
2382+
// When a menu is bound to <Nop>, we let :emenu fall through to execute
2383+
// the associated macaction (if one exists) instead. Otherwise, we
2384+
// simply execute the associated Vim command. Note that if you
2385+
// physically press the menu item (or the associated shortcut key), the
2386+
// macaction is always invoked even if the menu isn't bound to <Nop>.
2387+
if (menu->mac_action != NULL && menu->strings[idx] != NULL && menu->strings[idx][0] == NUL)
2388+
{
2389+
// Count on the fact taht ex_macaction() only looks at eap->arg.
2390+
old_arg = eap->arg;
2391+
eap->arg = menu->mac_action;
2392+
ex_macaction(eap);
2393+
eap->arg = old_arg;
2394+
}
2395+
#endif // FEAT_GUI_MACVIM
2396+
23822397
// When executing a script or function execute the commands right now.
23832398
// Also for the window toolbar.
23842399
// Otherwise put them in the typeahead buffer.
@@ -2425,20 +2440,6 @@ execute_menu(exarg_T *eap, vimmenu_T *menu, int mode_idx)
24252440
default:
24262441
mode = (char_u *)"Normal";
24272442
}
2428-
#ifdef FEAT_GUI_MACVIM
2429-
if (menu->mac_action != NULL && menu->strings[idx][0] == NUL)
2430-
{
2431-
// This allows us to bind a menu to an action without mapping to
2432-
// anything so that pressing the menu's key equivalent and typing
2433-
// ":emenu ..." does the same thing. (HACK: We count on the fact
2434-
// that ex_macaction() only looks at eap->arg.)
2435-
old_arg = eap->arg;
2436-
eap->arg = menu->mac_action;
2437-
ex_macaction(eap);
2438-
eap->arg = old_arg;
2439-
}
2440-
else
2441-
#endif // FEAT_GUI_MACVIM
24422443
semsg(_(e_menu_not_defined_for_str_mode), mode);
24432444
}
24442445
}

0 commit comments

Comments
 (0)