synced with MacVim/master (next/previous tab)
[MacVim/KaoriYa.git] / src / menu.c
blob025200fd64751b327a98c0b20fb3804e329e6f07
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
12 * Code for menus. Used for the GUI and 'wildmenu'.
15 #include "vim.h"
17 #if defined(FEAT_MENU) || defined(PROTO)
19 #define MENUDEPTH 10 /* maximum depth of menus */
21 #ifdef FEAT_GUI_W32
22 static int add_menu_path __ARGS((char_u *, vimmenu_T *, int *, char_u *, int));
23 #else
24 static int add_menu_path __ARGS((char_u *, vimmenu_T *, int *, char_u *));
25 #endif
26 static int menu_nable_recurse __ARGS((vimmenu_T *menu, char_u *name, int modes, int enable));
27 static int remove_menu __ARGS((vimmenu_T **, char_u *, int, int silent));
28 static void free_menu __ARGS((vimmenu_T **menup));
29 static void free_menu_string __ARGS((vimmenu_T *, int));
30 static int show_menus __ARGS((char_u *, int));
31 static void show_menus_recursive __ARGS((vimmenu_T *, int, int));
32 static int menu_name_equal __ARGS((char_u *name, vimmenu_T *menu));
33 static int menu_namecmp __ARGS((char_u *name, char_u *mname));
34 static int get_menu_cmd_modes __ARGS((char_u *, int, int *, int *));
35 static char_u *popup_mode_name __ARGS((char_u *name, int idx));
36 static char_u *menu_text __ARGS((char_u *text, int *mnemonic, char_u **actext));
37 #ifdef FEAT_GUI
38 static int get_menu_mode __ARGS((void));
39 static void gui_update_menus_recurse __ARGS((vimmenu_T *, int));
40 #endif
42 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
43 static void gui_create_tearoffs_recurse __ARGS((vimmenu_T *menu, const char_u *pname, int *pri_tab, int pri_idx));
44 static void gui_add_tearoff __ARGS((char_u *tearpath, int *pri_tab, int pri_idx));
45 static void gui_destroy_tearoffs_recurse __ARGS((vimmenu_T *menu));
46 static int s_tearoffs = FALSE;
47 #endif
49 static int menu_is_hidden __ARGS((char_u *name));
50 #if defined(FEAT_CMDL_COMPL) || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
51 static int menu_is_tearoff __ARGS((char_u *name));
52 #endif
54 #if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
55 static char_u *menu_skip_part __ARGS((char_u *p));
56 #endif
57 #ifdef FEAT_MULTI_LANG
58 static char_u *menutrans_lookup __ARGS((char_u *name, int len));
59 #endif
61 /* The character for each menu mode */
62 static char_u menu_mode_chars[] = {'n', 'v', 's', 'o', 'i', 'c', 't'};
64 static char_u e_notsubmenu[] = N_("E327: Part of menu-item path is not sub-menu");
65 static char_u e_othermode[] = N_("E328: Menu only exists in another mode");
66 static char_u e_nomenu[] = N_("E329: No menu \"%s\"");
68 #ifdef FEAT_TOOLBAR
69 static const char *toolbar_names[] =
71 /* 0 */ "New", "Open", "Save", "Undo", "Redo",
72 /* 5 */ "Cut", "Copy", "Paste", "Print", "Help",
73 /* 10 */ "Find", "SaveAll", "SaveSesn", "NewSesn", "LoadSesn",
74 /* 15 */ "RunScript", "Replace", "WinClose", "WinMax", "WinMin",
75 /* 20 */ "WinSplit", "Shell", "FindPrev", "FindNext", "FindHelp",
76 /* 25 */ "Make", "TagJump", "RunCtags", "WinVSplit", "WinMaxWidth",
77 /* 30 */ "WinMinWidth", "Exit"
79 # define TOOLBAR_NAME_COUNT (sizeof(toolbar_names) / sizeof(char *))
80 #endif
83 * Do the :menu command and relatives.
85 void
86 ex_menu(eap)
87 exarg_T *eap; /* Ex command arguments */
89 char_u *menu_path;
90 int modes;
91 char_u *map_to;
92 int noremap;
93 int silent = FALSE;
94 int special = FALSE;
95 int unmenu;
96 char_u *map_buf;
97 char_u *arg;
98 char_u *p;
99 int i;
100 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
101 int old_menu_height;
102 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
103 int old_toolbar_height;
104 # endif
105 #endif
106 int pri_tab[MENUDEPTH + 1];
107 int enable = MAYBE; /* TRUE for "menu enable", FALSE for "menu
108 * disable */
109 #ifdef FEAT_MULTI_LANG
110 char_u *tofree = NULL;
111 char_u *new_cmd;
112 #endif
113 #ifdef FEAT_TOOLBAR
114 char_u *icon = NULL;
115 #endif
116 vimmenu_T menuarg;
118 modes = get_menu_cmd_modes(eap->cmd, eap->forceit, &noremap, &unmenu);
119 arg = eap->arg;
121 for (;;)
123 if (STRNCMP(arg, "<script>", 8) == 0)
125 noremap = REMAP_SCRIPT;
126 arg = skipwhite(arg + 8);
127 continue;
129 if (STRNCMP(arg, "<silent>", 8) == 0)
131 silent = TRUE;
132 arg = skipwhite(arg + 8);
133 continue;
135 if (STRNCMP(arg, "<special>", 9) == 0)
137 special = TRUE;
138 arg = skipwhite(arg + 9);
139 continue;
141 break;
145 /* Locate an optional "icon=filename" argument. */
146 if (STRNCMP(arg, "icon=", 5) == 0)
148 arg += 5;
149 #ifdef FEAT_TOOLBAR
150 icon = arg;
151 #endif
152 while (*arg != NUL && *arg != ' ')
154 if (*arg == '\\')
155 STRMOVE(arg, arg + 1);
156 mb_ptr_adv(arg);
158 if (*arg != NUL)
160 *arg++ = NUL;
161 arg = skipwhite(arg);
166 * Fill in the priority table.
168 for (p = arg; *p; ++p)
169 if (!VIM_ISDIGIT(*p) && *p != '.')
170 break;
171 if (vim_iswhite(*p))
173 for (i = 0; i < MENUDEPTH && !vim_iswhite(*arg); ++i)
175 pri_tab[i] = getdigits(&arg);
176 if (pri_tab[i] == 0)
177 pri_tab[i] = 500;
178 if (*arg == '.')
179 ++arg;
181 arg = skipwhite(arg);
183 else if (eap->addr_count && eap->line2 != 0)
185 pri_tab[0] = eap->line2;
186 i = 1;
188 else
189 i = 0;
190 while (i < MENUDEPTH)
191 pri_tab[i++] = 500;
192 pri_tab[MENUDEPTH] = -1; /* mark end of the table */
195 * Check for "disable" or "enable" argument.
197 if (STRNCMP(arg, "enable", 6) == 0 && vim_iswhite(arg[6]))
199 enable = TRUE;
200 arg = skipwhite(arg + 6);
202 else if (STRNCMP(arg, "disable", 7) == 0 && vim_iswhite(arg[7]))
204 enable = FALSE;
205 arg = skipwhite(arg + 7);
209 * If there is no argument, display all menus.
211 if (*arg == NUL)
213 show_menus(arg, modes);
214 return;
217 #ifdef FEAT_TOOLBAR
219 * Need to get the toolbar icon index before doing the translation.
221 menuarg.iconidx = -1;
222 menuarg.icon_builtin = FALSE;
223 if (menu_is_toolbar(arg))
225 menu_path = menu_skip_part(arg);
226 if (*menu_path == '.')
228 p = menu_skip_part(++menu_path);
229 if (STRNCMP(menu_path, "BuiltIn", 7) == 0)
231 if (skipdigits(menu_path + 7) == p)
233 menuarg.iconidx = atoi((char *)menu_path + 7);
234 if (menuarg.iconidx >= (int)TOOLBAR_NAME_COUNT)
235 menuarg.iconidx = -1;
236 else
237 menuarg.icon_builtin = TRUE;
240 else
242 for (i = 0; i < (int)TOOLBAR_NAME_COUNT; ++i)
243 if (STRNCMP(toolbar_names[i], menu_path, p - menu_path)
244 == 0)
246 menuarg.iconidx = i;
247 break;
252 #endif
254 #ifdef FEAT_MULTI_LANG
256 * Translate menu names as specified with ":menutrans" commands.
258 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MACVIM)
259 /* Skip to translate toolbar for MacVim toolbar icons */
260 if (!menu_is_toolbar(arg)) {
261 #endif
262 menu_path = arg;
263 while (*menu_path)
265 /* find the end of one part and check if it should be translated */
266 p = menu_skip_part(menu_path);
267 map_to = menutrans_lookup(menu_path, (int)(p - menu_path));
268 if (map_to != NULL)
270 /* found a match: replace with the translated part */
271 i = (int)STRLEN(map_to);
272 new_cmd = alloc((unsigned)STRLEN(arg) + i + 1);
273 if (new_cmd == NULL)
274 break;
275 mch_memmove(new_cmd, arg, menu_path - arg);
276 mch_memmove(new_cmd + (menu_path - arg), map_to, (size_t)i);
277 STRCPY(new_cmd + (menu_path - arg) + i, p);
278 p = new_cmd + (menu_path - arg) + i;
279 vim_free(tofree);
280 tofree = new_cmd;
281 arg = new_cmd;
283 if (*p != '.')
284 break;
285 menu_path = p + 1;
287 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MACVIM)
289 #endif
290 #endif
293 * Isolate the menu name.
294 * Skip the menu name, and translate <Tab> into a real TAB.
296 menu_path = arg;
297 if (*menu_path == '.')
299 EMSG2(_(e_invarg2), menu_path);
300 goto theend;
303 while (*arg && !vim_iswhite(*arg))
305 if ((*arg == '\\' || *arg == Ctrl_V) && arg[1] != NUL)
306 arg++;
307 else if (STRNICMP(arg, "<TAB>", 5) == 0)
309 *arg = TAB;
310 STRMOVE(arg + 1, arg + 5);
312 arg++;
314 if (*arg != NUL)
315 *arg++ = NUL;
316 arg = skipwhite(arg);
317 map_to = arg;
320 * If there is only a menu name, display menus with that name.
322 if (*map_to == NUL && !unmenu && enable == MAYBE)
324 show_menus(menu_path, modes);
325 goto theend;
327 else if (*map_to != NUL && (unmenu || enable != MAYBE))
329 EMSG(_(e_trailing));
330 goto theend;
332 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) \
333 || defined(FEAT_GUI_MACVIM))
334 old_menu_height = gui.menu_height;
335 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
336 old_toolbar_height = gui.toolbar_height;
337 # endif
338 #endif
340 if (enable != MAYBE)
343 * Change sensitivity of the menu.
344 * For the PopUp menu, remove a menu for each mode separately.
345 * Careful: menu_nable_recurse() changes menu_path.
347 if (STRCMP(menu_path, "*") == 0) /* meaning: do all menus */
348 menu_path = (char_u *)"";
350 if (menu_is_popup(menu_path))
352 for (i = 0; i < MENU_INDEX_TIP; ++i)
353 if (modes & (1 << i))
355 p = popup_mode_name(menu_path, i);
356 if (p != NULL)
358 menu_nable_recurse(root_menu, p, MENU_ALL_MODES,
359 enable);
360 vim_free(p);
364 menu_nable_recurse(root_menu, menu_path, modes, enable);
366 else if (unmenu)
369 * Delete menu(s).
371 if (STRCMP(menu_path, "*") == 0) /* meaning: remove all menus */
372 menu_path = (char_u *)"";
375 * For the PopUp menu, remove a menu for each mode separately.
377 if (menu_is_popup(menu_path))
379 for (i = 0; i < MENU_INDEX_TIP; ++i)
380 if (modes & (1 << i))
382 p = popup_mode_name(menu_path, i);
383 if (p != NULL)
385 remove_menu(&root_menu, p, MENU_ALL_MODES, TRUE);
386 vim_free(p);
391 /* Careful: remove_menu() changes menu_path */
392 remove_menu(&root_menu, menu_path, modes, FALSE);
394 else
397 * Add menu(s).
398 * Replace special key codes.
400 if (STRICMP(map_to, "<nop>") == 0) /* "<Nop>" means nothing */
402 map_to = (char_u *)"";
403 map_buf = NULL;
405 else if (modes & MENU_TIP_MODE)
406 map_buf = NULL; /* Menu tips are plain text. */
407 else
408 map_to = replace_termcodes(map_to, &map_buf, FALSE, TRUE, special);
409 menuarg.modes = modes;
410 #ifdef FEAT_TOOLBAR
411 menuarg.iconfile = icon;
412 #endif
413 menuarg.noremap[0] = noremap;
414 menuarg.silent[0] = silent;
415 add_menu_path(menu_path, &menuarg, pri_tab, map_to
416 #ifdef FEAT_GUI_W32
417 , TRUE
418 #endif
422 * For the PopUp menu, add a menu for each mode separately.
424 if (menu_is_popup(menu_path))
426 for (i = 0; i < MENU_INDEX_TIP; ++i)
427 if (modes & (1 << i))
429 p = popup_mode_name(menu_path, i);
430 if (p != NULL)
432 /* Include all modes, to make ":amenu" work */
433 menuarg.modes = modes;
434 #ifdef FEAT_TOOLBAR
435 menuarg.iconfile = NULL;
436 menuarg.iconidx = -1;
437 menuarg.icon_builtin = FALSE;
438 #endif
439 add_menu_path(p, &menuarg, pri_tab, map_to
440 #ifdef FEAT_GUI_W32
441 , TRUE
442 #endif
444 vim_free(p);
449 vim_free(map_buf);
452 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
453 /* If the menubar height changed, resize the window */
454 if (gui.in_use
455 && (gui.menu_height != old_menu_height
456 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
457 || gui.toolbar_height != old_toolbar_height
458 # endif
460 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
461 #endif
463 theend:
464 #ifdef FEAT_MULTI_LANG
465 vim_free(tofree);
466 #else
468 #endif
472 * Add the menu with the given name to the menu hierarchy
474 static int
475 add_menu_path(menu_path, menuarg, pri_tab, call_data
476 #ifdef FEAT_GUI_W32
477 , addtearoff
478 #endif
480 char_u *menu_path;
481 vimmenu_T *menuarg; /* passes modes, iconfile, iconidx,
482 icon_builtin, silent[0], noremap[0] */
483 int *pri_tab;
484 char_u *call_data;
485 #ifdef FEAT_GUI_W32
486 int addtearoff; /* may add tearoff item */
487 #endif
489 char_u *path_name;
490 int modes = menuarg->modes;
491 vimmenu_T **menup;
492 vimmenu_T *menu = NULL;
493 vimmenu_T *parent;
494 vimmenu_T **lower_pri;
495 char_u *p;
496 char_u *name;
497 char_u *dname;
498 char_u *next_name;
499 int i;
500 int c;
501 #ifdef FEAT_GUI
502 int idx;
503 int new_idx;
504 #endif
505 int pri_idx = 0;
506 int old_modes = 0;
507 int amenu;
509 /* Make a copy so we can stuff around with it, since it could be const */
510 path_name = vim_strsave(menu_path);
511 if (path_name == NULL)
512 return FAIL;
513 menup = &root_menu;
514 parent = NULL;
515 name = path_name;
516 while (*name)
518 /* Get name of this element in the menu hierarchy, and the simplified
519 * name (without mnemonic and accelerator text). */
520 next_name = menu_name_skip(name);
521 dname = menu_text(name, NULL, NULL);
522 if (dname == NULL)
523 goto erret;
524 if (*dname == NUL)
526 /* Only a mnemonic or accelerator is not valid. */
527 EMSG(_("E792: Empty menu name"));
528 goto erret;
531 /* See if it's already there */
532 lower_pri = menup;
533 #ifdef FEAT_GUI
534 idx = 0;
535 new_idx = 0;
536 #endif
537 menu = *menup;
538 while (menu != NULL)
540 if (menu_name_equal(name, menu) || menu_name_equal(dname, menu))
542 if (*next_name == NUL && menu->children != NULL)
544 if (!sys_menu)
545 EMSG(_("E330: Menu path must not lead to a sub-menu"));
546 goto erret;
548 if (*next_name != NUL && menu->children == NULL
549 #ifdef FEAT_GUI_W32
550 && addtearoff
551 #endif
554 if (!sys_menu)
555 EMSG(_(e_notsubmenu));
556 goto erret;
558 break;
560 menup = &menu->next;
562 /* Count menus, to find where this one needs to be inserted.
563 * Ignore menus that are not in the menubar (PopUp and Toolbar) */
564 if (parent != NULL || menu_is_menubar(menu->name))
566 #ifdef FEAT_GUI
567 ++idx;
568 #endif
569 if (menu->priority <= pri_tab[pri_idx])
571 lower_pri = menup;
572 #ifdef FEAT_GUI
573 new_idx = idx;
574 #endif
577 menu = menu->next;
580 if (menu == NULL)
582 if (*next_name == NUL && parent == NULL)
584 EMSG(_("E331: Must not add menu items directly to menu bar"));
585 goto erret;
588 if (menu_is_separator(dname) && *next_name != NUL)
590 EMSG(_("E332: Separator cannot be part of a menu path"));
591 goto erret;
594 /* Not already there, so lets add it */
595 menu = (vimmenu_T *)alloc_clear((unsigned)sizeof(vimmenu_T));
596 if (menu == NULL)
597 goto erret;
599 menu->modes = modes;
600 menu->enabled = MENU_ALL_MODES;
601 menu->name = vim_strsave(name);
602 /* separate mnemonic and accelerator text from actual menu name */
603 menu->dname = menu_text(name, &menu->mnemonic, &menu->actext);
604 menu->priority = pri_tab[pri_idx];
605 menu->parent = parent;
606 #ifdef FEAT_GUI_MOTIF
607 menu->sensitive = TRUE; /* the default */
608 #endif
609 #ifdef FEAT_BEVAL_TIP
610 menu->tip = NULL;
611 #endif
612 #ifdef FEAT_GUI_ATHENA
613 menu->image = None; /* X-Windows definition for NULL*/
614 #endif
617 * Add after menu that has lower priority.
619 menu->next = *lower_pri;
620 *lower_pri = menu;
622 old_modes = 0;
624 #ifdef FEAT_TOOLBAR
625 menu->iconidx = menuarg->iconidx;
626 menu->icon_builtin = menuarg->icon_builtin;
627 if (*next_name == NUL && menuarg->iconfile != NULL)
628 menu->iconfile = vim_strsave(menuarg->iconfile);
629 #endif
630 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
631 /* the tearoff item must be present in the modes of each item. */
632 if (parent != NULL && menu_is_tearoff(parent->children->dname))
633 parent->children->modes |= modes;
634 #endif
636 else
638 old_modes = menu->modes;
641 * If this menu option was previously only available in other
642 * modes, then make sure it's available for this one now
643 * Also enable a menu when it's created or changed.
645 #ifdef FEAT_GUI_W32
646 /* If adding a tearbar (addtearoff == FALSE) don't update modes */
647 if (addtearoff)
648 #endif
650 menu->modes |= modes;
651 menu->enabled |= modes;
655 #ifdef FEAT_GUI
657 * Add the menu item when it's used in one of the modes, but not when
658 * only a tooltip is defined.
660 if ((old_modes & MENU_ALL_MODES) == 0
661 && (menu->modes & MENU_ALL_MODES) != 0)
663 if (gui.in_use) /* Otherwise it will be added when GUI starts */
665 if (*next_name == NUL)
667 /* Real menu item, not sub-menu */
668 gui_mch_add_menu_item(menu, new_idx);
670 /* Want to update menus now even if mode not changed */
671 force_menu_update = TRUE;
673 else
675 /* Sub-menu (not at end of path yet) */
676 gui_mch_add_menu(menu, new_idx);
680 # if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
681 /* When adding a new submenu, may add a tearoff item */
682 if ( addtearoff
683 && *next_name
684 && vim_strchr(p_go, GO_TEAROFF) != NULL
685 && menu_is_menubar(name))
687 char_u *tearpath;
690 * The pointers next_name & path_name refer to a string with
691 * \'s and ^V's stripped out. But menu_path is a "raw"
692 * string, so we must correct for special characters.
694 tearpath = alloc((unsigned int)STRLEN(menu_path) + TEAR_LEN + 2);
695 if (tearpath != NULL)
697 char_u *s;
698 int idx;
700 STRCPY(tearpath, menu_path);
701 idx = (int)(next_name - path_name - 1);
702 for (s = tearpath; *s && s < tearpath + idx; mb_ptr_adv(s))
704 if ((*s == '\\' || *s == Ctrl_V) && s[1])
706 ++idx;
707 ++s;
710 tearpath[idx] = NUL;
711 gui_add_tearoff(tearpath, pri_tab, pri_idx);
712 vim_free(tearpath);
715 # endif
717 #endif /* FEAT_GUI */
719 menup = &menu->children;
720 parent = menu;
721 name = next_name;
722 vim_free(dname);
723 dname = NULL;
724 if (pri_tab[pri_idx + 1] != -1)
725 ++pri_idx;
727 vim_free(path_name);
730 * Only add system menu items which have not been defined yet.
731 * First check if this was an ":amenu".
733 amenu = ((modes & (MENU_NORMAL_MODE | MENU_INSERT_MODE)) ==
734 (MENU_NORMAL_MODE | MENU_INSERT_MODE));
735 if (sys_menu)
736 modes &= ~old_modes;
738 if (menu != NULL && modes)
740 #ifdef FEAT_GUI
741 menu->cb = gui_menu_cb;
742 #endif
743 p = (call_data == NULL) ? NULL : vim_strsave(call_data);
745 /* loop over all modes, may add more than one */
746 for (i = 0; i < MENU_MODES; ++i)
748 if (modes & (1 << i))
750 /* free any old menu */
751 free_menu_string(menu, i);
753 /* For "amenu", may insert an extra character.
754 * Don't do this if adding a tearbar (addtearoff == FALSE).
755 * Don't do this for "<Nop>". */
756 c = 0;
757 if (amenu && call_data != NULL && *call_data != NUL
758 #ifdef FEAT_GUI_W32
759 && addtearoff
760 #endif
763 switch (1 << i)
765 case MENU_VISUAL_MODE:
766 case MENU_SELECT_MODE:
767 case MENU_OP_PENDING_MODE:
768 case MENU_CMDLINE_MODE:
769 c = Ctrl_C;
770 break;
771 case MENU_INSERT_MODE:
772 c = Ctrl_O;
773 break;
777 if (c)
779 menu->strings[i] = alloc((unsigned)(STRLEN(call_data) + 4));
780 if (menu->strings[i] != NULL)
782 menu->strings[i][0] = c;
783 STRCPY(menu->strings[i] + 1, call_data);
784 if (c == Ctrl_C)
786 int len = (int)STRLEN(menu->strings[i]);
788 /* Append CTRL-\ CTRL-G to obey 'insertmode'. */
789 menu->strings[i][len] = Ctrl_BSL;
790 menu->strings[i][len + 1] = Ctrl_G;
791 menu->strings[i][len + 2] = NUL;
795 else
796 menu->strings[i] = p;
797 menu->noremap[i] = menuarg->noremap[0];
798 menu->silent[i] = menuarg->silent[0];
801 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
802 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
803 /* Need to update the menu tip. */
804 if (modes & MENU_TIP_MODE)
805 gui_mch_menu_set_tip(menu);
806 #endif
808 return OK;
810 erret:
811 vim_free(path_name);
812 vim_free(dname);
814 /* Delete any empty submenu we added before discovering the error. Repeat
815 * for higher levels. */
816 while (parent != NULL && parent->children == NULL)
818 if (parent->parent == NULL)
819 menup = &root_menu;
820 else
821 menup = &parent->parent->children;
822 for ( ; *menup != NULL && *menup != parent; menup = &((*menup)->next))
824 if (*menup == NULL) /* safety check */
825 break;
826 parent = parent->parent;
827 free_menu(menup);
829 return FAIL;
833 * Set the (sub)menu with the given name to enabled or disabled.
834 * Called recursively.
836 static int
837 menu_nable_recurse(menu, name, modes, enable)
838 vimmenu_T *menu;
839 char_u *name;
840 int modes;
841 int enable;
843 char_u *p;
845 if (menu == NULL)
846 return OK; /* Got to bottom of hierarchy */
848 /* Get name of this element in the menu hierarchy */
849 p = menu_name_skip(name);
851 /* Find the menu */
852 while (menu != NULL)
854 if (*name == NUL || *name == '*' || menu_name_equal(name, menu))
856 if (*p != NUL)
858 if (menu->children == NULL)
860 EMSG(_(e_notsubmenu));
861 return FAIL;
863 if (menu_nable_recurse(menu->children, p, modes, enable)
864 == FAIL)
865 return FAIL;
867 else
868 if (enable)
869 menu->enabled |= modes;
870 else
871 menu->enabled &= ~modes;
874 * When name is empty, we are doing all menu items for the given
875 * modes, so keep looping, otherwise we are just doing the named
876 * menu item (which has been found) so break here.
878 if (*name != NUL && *name != '*')
879 break;
881 menu = menu->next;
883 if (*name != NUL && *name != '*' && menu == NULL)
885 EMSG2(_(e_nomenu), name);
886 return FAIL;
889 #ifdef FEAT_GUI
890 /* Want to update menus now even if mode not changed */
891 force_menu_update = TRUE;
892 #endif
894 return OK;
898 * Remove the (sub)menu with the given name from the menu hierarchy
899 * Called recursively.
901 static int
902 remove_menu(menup, name, modes, silent)
903 vimmenu_T **menup;
904 char_u *name;
905 int modes;
906 int silent; /* don't give error messages */
908 vimmenu_T *menu;
909 vimmenu_T *child;
910 char_u *p;
912 if (*menup == NULL)
913 return OK; /* Got to bottom of hierarchy */
915 /* Get name of this element in the menu hierarchy */
916 p = menu_name_skip(name);
918 /* Find the menu */
919 while ((menu = *menup) != NULL)
921 if (*name == NUL || menu_name_equal(name, menu))
923 if (*p != NUL && menu->children == NULL)
925 if (!silent)
926 EMSG(_(e_notsubmenu));
927 return FAIL;
929 if ((menu->modes & modes) != 0x0)
931 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
933 * If we are removing all entries for this menu,MENU_ALL_MODES,
934 * Then kill any tearoff before we start
936 if (*p == NUL && modes == MENU_ALL_MODES)
938 if (IsWindow(menu->tearoff_handle))
939 DestroyWindow(menu->tearoff_handle);
941 #endif
942 if (remove_menu(&menu->children, p, modes, silent) == FAIL)
943 return FAIL;
945 else if (*name != NUL)
947 if (!silent)
948 EMSG(_(e_othermode));
949 return FAIL;
953 * When name is empty, we are removing all menu items for the given
954 * modes, so keep looping, otherwise we are just removing the named
955 * menu item (which has been found) so break here.
957 if (*name != NUL)
958 break;
960 /* Remove the menu item for the given mode[s]. If the menu item
961 * is no longer valid in ANY mode, delete it */
962 menu->modes &= ~modes;
963 if (modes & MENU_TIP_MODE)
964 free_menu_string(menu, MENU_INDEX_TIP);
965 if ((menu->modes & MENU_ALL_MODES) == 0)
966 free_menu(menup);
967 else
968 menup = &menu->next;
970 else
971 menup = &menu->next;
973 if (*name != NUL)
975 if (menu == NULL)
977 if (!silent)
978 EMSG2(_(e_nomenu), name);
979 return FAIL;
983 /* Recalculate modes for menu based on the new updated children */
984 menu->modes &= ~modes;
985 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
986 if ((s_tearoffs) && (menu->children != NULL)) /* there's a tear bar.. */
987 child = menu->children->next; /* don't count tearoff bar */
988 else
989 #endif
990 child = menu->children;
991 for ( ; child != NULL; child = child->next)
992 menu->modes |= child->modes;
993 if (modes & MENU_TIP_MODE)
995 free_menu_string(menu, MENU_INDEX_TIP);
996 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
997 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
998 /* Need to update the menu tip. */
999 if (gui.in_use)
1000 gui_mch_menu_set_tip(menu);
1001 #endif
1003 if ((menu->modes & MENU_ALL_MODES) == 0)
1005 /* The menu item is no longer valid in ANY mode, so delete it */
1006 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
1007 if (s_tearoffs && menu->children != NULL) /* there's a tear bar.. */
1008 free_menu(&menu->children);
1009 #endif
1010 *menup = menu;
1011 free_menu(menup);
1015 return OK;
1019 * Free the given menu structure and remove it from the linked list.
1021 static void
1022 free_menu(menup)
1023 vimmenu_T **menup;
1025 int i;
1026 vimmenu_T *menu;
1028 menu = *menup;
1030 #ifdef FEAT_GUI
1031 /* Free machine specific menu structures (only when already created) */
1032 /* Also may rebuild a tearoff'ed menu */
1033 if (gui.in_use)
1034 gui_mch_destroy_menu(menu);
1035 #endif
1037 /* Don't change *menup until after calling gui_mch_destroy_menu(). The
1038 * MacOS code needs the original structure to properly delete the menu. */
1039 *menup = menu->next;
1040 vim_free(menu->name);
1041 vim_free(menu->dname);
1042 vim_free(menu->actext);
1043 #ifdef FEAT_TOOLBAR
1044 vim_free(menu->iconfile);
1045 # ifdef FEAT_GUI_MOTIF
1046 vim_free(menu->xpm_fname);
1047 # endif
1048 #ifdef FEAT_GUI_MACVIM
1049 vim_free(menu->mac_action);
1050 #endif
1051 #endif
1052 for (i = 0; i < MENU_MODES; i++)
1053 free_menu_string(menu, i);
1054 vim_free(menu);
1056 #ifdef FEAT_GUI
1057 /* Want to update menus now even if mode not changed */
1058 force_menu_update = TRUE;
1059 #endif
1063 * Free the menu->string with the given index.
1065 static void
1066 free_menu_string(menu, idx)
1067 vimmenu_T *menu;
1068 int idx;
1070 int count = 0;
1071 int i;
1073 for (i = 0; i < MENU_MODES; i++)
1074 if (menu->strings[i] == menu->strings[idx])
1075 count++;
1076 if (count == 1)
1077 vim_free(menu->strings[idx]);
1078 menu->strings[idx] = NULL;
1082 * Show the mapping associated with a menu item or hierarchy in a sub-menu.
1084 static int
1085 show_menus(path_name, modes)
1086 char_u *path_name;
1087 int modes;
1089 char_u *p;
1090 char_u *name;
1091 vimmenu_T *menu;
1092 vimmenu_T *parent = NULL;
1094 menu = root_menu;
1095 name = path_name = vim_strsave(path_name);
1096 if (path_name == NULL)
1097 return FAIL;
1099 /* First, find the (sub)menu with the given name */
1100 while (*name)
1102 p = menu_name_skip(name);
1103 while (menu != NULL)
1105 if (menu_name_equal(name, menu))
1107 /* Found menu */
1108 if (*p != NUL && menu->children == NULL)
1110 EMSG(_(e_notsubmenu));
1111 vim_free(path_name);
1112 return FAIL;
1114 else if ((menu->modes & modes) == 0x0)
1116 EMSG(_(e_othermode));
1117 vim_free(path_name);
1118 return FAIL;
1120 break;
1122 menu = menu->next;
1124 if (menu == NULL)
1126 EMSG2(_(e_nomenu), name);
1127 vim_free(path_name);
1128 return FAIL;
1130 name = p;
1131 parent = menu;
1132 menu = menu->children;
1134 vim_free(path_name);
1136 /* Now we have found the matching menu, and we list the mappings */
1137 /* Highlight title */
1138 MSG_PUTS_TITLE(_("\n--- Menus ---"));
1140 show_menus_recursive(parent, modes, 0);
1141 return OK;
1145 * Recursively show the mappings associated with the menus under the given one
1147 static void
1148 show_menus_recursive(menu, modes, depth)
1149 vimmenu_T *menu;
1150 int modes;
1151 int depth;
1153 int i;
1154 int bit;
1156 if (menu != NULL && (menu->modes & modes) == 0x0)
1157 return;
1159 if (menu != NULL)
1161 msg_putchar('\n');
1162 if (got_int) /* "q" hit for "--more--" */
1163 return;
1164 for (i = 0; i < depth; i++)
1165 MSG_PUTS(" ");
1166 if (menu->priority)
1168 msg_outnum((long)menu->priority);
1169 MSG_PUTS(" ");
1171 /* Same highlighting as for directories!? */
1172 msg_outtrans_attr(menu->name, hl_attr(HLF_D));
1175 if (menu != NULL && menu->children == NULL)
1177 for (bit = 0; bit < MENU_MODES; bit++)
1178 if ((menu->modes & modes & (1 << bit)) != 0)
1180 msg_putchar('\n');
1181 if (got_int) /* "q" hit for "--more--" */
1182 return;
1183 for (i = 0; i < depth + 2; i++)
1184 MSG_PUTS(" ");
1185 msg_putchar(menu_mode_chars[bit]);
1186 if (menu->noremap[bit] == REMAP_NONE)
1187 msg_putchar('*');
1188 else if (menu->noremap[bit] == REMAP_SCRIPT)
1189 msg_putchar('&');
1190 else
1191 msg_putchar(' ');
1192 if (menu->silent[bit])
1193 msg_putchar('s');
1194 else
1195 msg_putchar(' ');
1196 if ((menu->modes & menu->enabled & (1 << bit)) == 0)
1197 msg_putchar('-');
1198 else
1199 msg_putchar(' ');
1200 MSG_PUTS(" ");
1201 if (*menu->strings[bit] == NUL)
1202 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
1203 else
1204 msg_outtrans_special(menu->strings[bit], FALSE);
1207 else
1209 if (menu == NULL)
1211 menu = root_menu;
1212 depth--;
1214 else
1215 menu = menu->children;
1217 /* recursively show all children. Skip PopUp[nvoci]. */
1218 for (; menu != NULL && !got_int; menu = menu->next)
1219 if (!menu_is_hidden(menu->dname))
1220 show_menus_recursive(menu, modes, depth + 1);
1224 #ifdef FEAT_CMDL_COMPL
1227 * Used when expanding menu names.
1229 static vimmenu_T *expand_menu = NULL;
1230 static int expand_modes = 0x0;
1231 static int expand_emenu; /* TRUE for ":emenu" command */
1234 * Work out what to complete when doing command line completion of menu names.
1236 char_u *
1237 set_context_in_menu_cmd(xp, cmd, arg, forceit)
1238 expand_T *xp;
1239 char_u *cmd;
1240 char_u *arg;
1241 int forceit;
1243 char_u *after_dot;
1244 char_u *p;
1245 char_u *path_name = NULL;
1246 char_u *name;
1247 int unmenu;
1248 vimmenu_T *menu;
1249 int expand_menus;
1251 xp->xp_context = EXPAND_UNSUCCESSFUL;
1254 /* Check for priority numbers, enable and disable */
1255 for (p = arg; *p; ++p)
1256 if (!VIM_ISDIGIT(*p) && *p != '.')
1257 break;
1259 if (!vim_iswhite(*p))
1261 if (STRNCMP(arg, "enable", 6) == 0
1262 && (arg[6] == NUL || vim_iswhite(arg[6])))
1263 p = arg + 6;
1264 else if (STRNCMP(arg, "disable", 7) == 0
1265 && (arg[7] == NUL || vim_iswhite(arg[7])))
1266 p = arg + 7;
1267 else
1268 p = arg;
1271 while (*p != NUL && vim_iswhite(*p))
1272 ++p;
1274 arg = after_dot = p;
1276 for (; *p && !vim_iswhite(*p); ++p)
1278 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
1279 p++;
1280 else if (*p == '.')
1281 after_dot = p + 1;
1284 /* ":tearoff" and ":popup" only use menus, not entries */
1285 expand_menus = !((*cmd == 't' && cmd[1] == 'e') || *cmd == 'p');
1286 expand_emenu = (*cmd == 'e');
1287 if (expand_menus && vim_iswhite(*p))
1288 return NULL; /* TODO: check for next command? */
1289 if (*p == NUL) /* Complete the menu name */
1292 * With :unmenu, you only want to match menus for the appropriate mode.
1293 * With :menu though you might want to add a menu with the same name as
1294 * one in another mode, so match menus from other modes too.
1296 expand_modes = get_menu_cmd_modes(cmd, forceit, NULL, &unmenu);
1297 if (!unmenu)
1298 expand_modes = MENU_ALL_MODES;
1300 menu = root_menu;
1301 if (after_dot != arg)
1303 path_name = alloc((unsigned)(after_dot - arg));
1304 if (path_name == NULL)
1305 return NULL;
1306 vim_strncpy(path_name, arg, after_dot - arg - 1);
1308 name = path_name;
1309 while (name != NULL && *name)
1311 p = menu_name_skip(name);
1312 while (menu != NULL)
1314 if (menu_name_equal(name, menu))
1316 /* Found menu */
1317 if ((*p != NUL && menu->children == NULL)
1318 || ((menu->modes & expand_modes) == 0x0))
1321 * Menu path continues, but we have reached a leaf.
1322 * Or menu exists only in another mode.
1324 vim_free(path_name);
1325 return NULL;
1327 break;
1329 menu = menu->next;
1331 if (menu == NULL)
1333 /* No menu found with the name we were looking for */
1334 vim_free(path_name);
1335 return NULL;
1337 name = p;
1338 menu = menu->children;
1340 vim_free(path_name);
1342 xp->xp_context = expand_menus ? EXPAND_MENUNAMES : EXPAND_MENUS;
1343 xp->xp_pattern = after_dot;
1344 expand_menu = menu;
1346 else /* We're in the mapping part */
1347 xp->xp_context = EXPAND_NOTHING;
1348 return NULL;
1352 * Function given to ExpandGeneric() to obtain the list of (sub)menus (not
1353 * entries).
1355 char_u *
1356 get_menu_name(xp, idx)
1357 expand_T *xp UNUSED;
1358 int idx;
1360 static vimmenu_T *menu = NULL;
1361 char_u *str;
1363 if (idx == 0) /* first call: start at first item */
1364 menu = expand_menu;
1366 /* Skip PopUp[nvoci]. */
1367 while (menu != NULL && (menu_is_hidden(menu->dname)
1368 || menu_is_separator(menu->dname)
1369 || menu_is_tearoff(menu->dname)
1370 || menu->children == NULL))
1371 menu = menu->next;
1373 if (menu == NULL) /* at end of linked list */
1374 return NULL;
1376 if (menu->modes & expand_modes)
1377 str = menu->dname;
1378 else
1379 str = (char_u *)"";
1381 /* Advance to next menu entry. */
1382 menu = menu->next;
1384 return str;
1388 * Function given to ExpandGeneric() to obtain the list of menus and menu
1389 * entries.
1391 char_u *
1392 get_menu_names(xp, idx)
1393 expand_T *xp UNUSED;
1394 int idx;
1396 static vimmenu_T *menu = NULL;
1397 static char_u tbuffer[256]; /*hack*/
1398 char_u *str;
1400 if (idx == 0) /* first call: start at first item */
1401 menu = expand_menu;
1403 /* Skip Browse-style entries, popup menus and separators. */
1404 while (menu != NULL
1405 && ( menu_is_hidden(menu->dname)
1406 || (expand_emenu && menu_is_separator(menu->dname))
1407 || menu_is_tearoff(menu->dname)
1408 #ifndef FEAT_BROWSE
1409 || menu->dname[STRLEN(menu->dname) - 1] == '.'
1410 #endif
1412 menu = menu->next;
1414 if (menu == NULL) /* at end of linked list */
1415 return NULL;
1417 if (menu->modes & expand_modes)
1419 if (menu->children != NULL)
1421 STRCPY(tbuffer, menu->dname);
1422 /* hack on menu separators: use a 'magic' char for the separator
1423 * so that '.' in names gets escaped properly */
1424 STRCAT(tbuffer, "\001");
1425 str = tbuffer;
1427 else
1428 str = menu->dname;
1430 else
1431 str = (char_u *)"";
1433 /* Advance to next menu entry. */
1434 menu = menu->next;
1436 return str;
1438 #endif /* FEAT_CMDL_COMPL */
1441 * Skip over this element of the menu path and return the start of the next
1442 * element. Any \ and ^Vs are removed from the current element.
1443 * "name" may be modified.
1445 char_u *
1446 menu_name_skip(name)
1447 char_u *name;
1449 char_u *p;
1451 for (p = name; *p && *p != '.'; mb_ptr_adv(p))
1453 if (*p == '\\' || *p == Ctrl_V)
1455 STRMOVE(p, p + 1);
1456 if (*p == NUL)
1457 break;
1460 if (*p)
1461 *p++ = NUL;
1462 return p;
1466 * Return TRUE when "name" matches with menu "menu". The name is compared in
1467 * two ways: raw menu name and menu name without '&'. ignore part after a TAB.
1469 static int
1470 menu_name_equal(name, menu)
1471 char_u *name;
1472 vimmenu_T *menu;
1474 return (menu_namecmp(name, menu->name) || menu_namecmp(name, menu->dname));
1477 static int
1478 menu_namecmp(name, mname)
1479 char_u *name;
1480 char_u *mname;
1482 int i;
1484 for (i = 0; name[i] != NUL && name[i] != TAB; ++i)
1485 if (name[i] != mname[i])
1486 break;
1487 return ((name[i] == NUL || name[i] == TAB)
1488 && (mname[i] == NUL || mname[i] == TAB));
1492 * Return the modes specified by the given menu command (eg :menu! returns
1493 * MENU_CMDLINE_MODE | MENU_INSERT_MODE).
1494 * If "noremap" is not NULL, then the flag it points to is set according to
1495 * whether the command is a "nore" command.
1496 * If "unmenu" is not NULL, then the flag it points to is set according to
1497 * whether the command is an "unmenu" command.
1499 static int
1500 get_menu_cmd_modes(cmd, forceit, noremap, unmenu)
1501 char_u *cmd;
1502 int forceit; /* Was there a "!" after the command? */
1503 int *noremap;
1504 int *unmenu;
1506 int modes;
1508 switch (*cmd++)
1510 case 'v': /* vmenu, vunmenu, vnoremenu */
1511 modes = MENU_VISUAL_MODE | MENU_SELECT_MODE;
1512 break;
1513 case 'x': /* xmenu, xunmenu, xnoremenu */
1514 modes = MENU_VISUAL_MODE;
1515 break;
1516 case 's': /* smenu, sunmenu, snoremenu */
1517 modes = MENU_SELECT_MODE;
1518 break;
1519 case 'o': /* omenu */
1520 modes = MENU_OP_PENDING_MODE;
1521 break;
1522 case 'i': /* imenu */
1523 modes = MENU_INSERT_MODE;
1524 break;
1525 case 't':
1526 modes = MENU_TIP_MODE; /* tmenu */
1527 break;
1528 case 'c': /* cmenu */
1529 modes = MENU_CMDLINE_MODE;
1530 break;
1531 case 'a': /* amenu */
1532 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE | MENU_NORMAL_MODE
1533 | MENU_VISUAL_MODE | MENU_SELECT_MODE
1534 | MENU_OP_PENDING_MODE;
1535 break;
1536 case 'n':
1537 if (*cmd != 'o') /* nmenu, not noremenu */
1539 modes = MENU_NORMAL_MODE;
1540 break;
1542 /* FALLTHROUGH */
1543 default:
1544 --cmd;
1545 if (forceit) /* menu!! */
1546 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE;
1547 else /* menu */
1548 modes = MENU_NORMAL_MODE | MENU_VISUAL_MODE | MENU_SELECT_MODE
1549 | MENU_OP_PENDING_MODE;
1552 if (noremap != NULL)
1553 *noremap = (*cmd == 'n' ? REMAP_NONE : REMAP_YES);
1554 if (unmenu != NULL)
1555 *unmenu = (*cmd == 'u');
1556 return modes;
1560 * Modify a menu name starting with "PopUp" to include the mode character.
1561 * Returns the name in allocated memory (NULL for failure).
1563 static char_u *
1564 popup_mode_name(name, idx)
1565 char_u *name;
1566 int idx;
1568 char_u *p;
1569 int len = (int)STRLEN(name);
1571 p = vim_strnsave(name, len + 1);
1572 if (p != NULL)
1574 mch_memmove(p + 6, p + 5, (size_t)(len - 4));
1575 p[5] = menu_mode_chars[idx];
1577 return p;
1580 #if defined(FEAT_GUI) || defined(PROTO)
1582 * Return the index into the menu->strings or menu->noremap arrays for the
1583 * current state. Returns MENU_INDEX_INVALID if there is no mapping for the
1584 * given menu in the current mode.
1587 get_menu_index(menu, state)
1588 vimmenu_T *menu;
1589 int state;
1591 int idx;
1593 if ((state & INSERT))
1594 idx = MENU_INDEX_INSERT;
1595 else if (state & CMDLINE)
1596 idx = MENU_INDEX_CMDLINE;
1597 #ifdef FEAT_VISUAL
1598 else if (VIsual_active)
1600 if (VIsual_select)
1601 idx = MENU_INDEX_SELECT;
1602 else
1603 idx = MENU_INDEX_VISUAL;
1605 #endif
1606 else if (state == HITRETURN || state == ASKMORE)
1607 idx = MENU_INDEX_CMDLINE;
1608 else if (finish_op)
1609 idx = MENU_INDEX_OP_PENDING;
1610 else if ((state & NORMAL))
1611 idx = MENU_INDEX_NORMAL;
1612 else
1613 idx = MENU_INDEX_INVALID;
1615 if (idx != MENU_INDEX_INVALID && menu->strings[idx] == NULL)
1616 idx = MENU_INDEX_INVALID;
1617 return idx;
1619 #endif
1622 * Duplicate the menu item text and then process to see if a mnemonic key
1623 * and/or accelerator text has been identified.
1624 * Returns a pointer to allocated memory, or NULL for failure.
1625 * If mnemonic != NULL, *mnemonic is set to the character after the first '&'.
1626 * If actext != NULL, *actext is set to the text after the first TAB.
1628 static char_u *
1629 menu_text(str, mnemonic, actext)
1630 char_u *str;
1631 int *mnemonic;
1632 char_u **actext;
1634 char_u *p;
1635 char_u *text;
1637 /* Locate accelerator text, after the first TAB */
1638 p = vim_strchr(str, TAB);
1639 if (p != NULL)
1641 if (actext != NULL)
1642 *actext = vim_strsave(p + 1);
1643 text = vim_strnsave(str, (int)(p - str));
1645 else
1646 text = vim_strsave(str);
1648 /* Find mnemonic characters "&a" and reduce "&&" to "&". */
1649 for (p = text; p != NULL; )
1651 p = vim_strchr(p, '&');
1652 if (p != NULL)
1654 if (p[1] == NUL) /* trailing "&" */
1655 break;
1656 if (mnemonic != NULL && p[1] != '&')
1657 #if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED)
1658 *mnemonic = p[1];
1659 #else
1662 * Well there is a bug in the Motif libraries on OS390 Unix.
1663 * The mnemonic keys needs to be converted to ASCII values
1664 * first.
1665 * This behavior has been seen in 2.8 and 2.9.
1667 char c = p[1];
1668 __etoa_l(&c, 1);
1669 *mnemonic = c;
1671 #endif
1672 STRMOVE(p, p + 1);
1673 p = p + 1;
1676 return text;
1680 * Return TRUE if "name" can be a menu in the MenuBar.
1683 menu_is_menubar(name)
1684 char_u *name;
1686 return (!menu_is_popup(name)
1687 && !menu_is_toolbar(name)
1688 && *name != MNU_HIDDEN_CHAR);
1692 * Return TRUE if "name" is a popup menu name.
1695 menu_is_popup(name)
1696 char_u *name;
1698 return (STRNCMP(name, "PopUp", 5) == 0);
1701 #if (defined(FEAT_GUI_MOTIF) && (XmVersion <= 1002)) || defined(PROTO)
1703 * Return TRUE if "name" is part of a popup menu.
1706 menu_is_child_of_popup(menu)
1707 vimmenu_T *menu;
1709 while (menu->parent != NULL)
1710 menu = menu->parent;
1711 return menu_is_popup(menu->name);
1713 #endif
1716 * Return TRUE if "name" is a toolbar menu name.
1719 menu_is_toolbar(name)
1720 char_u *name;
1722 return (STRNCMP(name, "ToolBar", 7) == 0);
1726 * Return TRUE if the name is a menu separator identifier: Starts and ends
1727 * with '-'
1730 menu_is_separator(name)
1731 char_u *name;
1733 return (name[0] == '-' && name[STRLEN(name) - 1] == '-');
1737 * Return TRUE if the menu is hidden: Starts with ']'
1739 static int
1740 menu_is_hidden(name)
1741 char_u *name;
1743 return (name[0] == ']') || (menu_is_popup(name) && name[5] != NUL);
1746 #if defined(FEAT_CMDL_COMPL) \
1747 || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
1749 * Return TRUE if the menu is the tearoff menu.
1751 static int
1752 menu_is_tearoff(name)
1753 char_u *name UNUSED;
1755 #ifdef FEAT_GUI
1756 return (STRCMP(name, TEAR_STRING) == 0);
1757 #else
1758 return FALSE;
1759 #endif
1761 #endif
1763 #ifdef FEAT_GUI
1765 static int
1766 get_menu_mode()
1768 #ifdef FEAT_VISUAL
1769 if (VIsual_active)
1771 if (VIsual_select)
1772 return MENU_INDEX_SELECT;
1773 return MENU_INDEX_VISUAL;
1775 #endif
1776 if (State & INSERT)
1777 return MENU_INDEX_INSERT;
1778 if ((State & CMDLINE) || State == ASKMORE || State == HITRETURN)
1779 return MENU_INDEX_CMDLINE;
1780 if (finish_op)
1781 return MENU_INDEX_OP_PENDING;
1782 if (State & NORMAL)
1783 return MENU_INDEX_NORMAL;
1784 if (State & LANGMAP) /* must be a "r" command, like Insert mode */
1785 return MENU_INDEX_INSERT;
1786 return MENU_INDEX_INVALID;
1790 * Check that a pointer appears in the menu tree. Used to protect from using
1791 * a menu that was deleted after it was selected but before the event was
1792 * handled.
1793 * Return OK or FAIL. Used recursively.
1796 check_menu_pointer(root, menu_to_check)
1797 vimmenu_T *root;
1798 vimmenu_T *menu_to_check;
1800 vimmenu_T *p;
1802 for (p = root; p != NULL; p = p->next)
1803 if (p == menu_to_check
1804 || (p->children != NULL
1805 && check_menu_pointer(p->children, menu_to_check) == OK))
1806 return OK;
1807 return FAIL;
1811 * After we have started the GUI, then we can create any menus that have been
1812 * defined. This is done once here. add_menu_path() may have already been
1813 * called to define these menus, and may be called again. This function calls
1814 * itself recursively. Should be called at the top level with:
1815 * gui_create_initial_menus(root_menu, NULL);
1817 void
1818 gui_create_initial_menus(menu)
1819 vimmenu_T *menu;
1821 int idx = 0;
1823 while (menu != NULL)
1825 /* Don't add a menu when only a tip was defined. */
1826 if (menu->modes & MENU_ALL_MODES)
1828 if (menu->children != NULL)
1830 gui_mch_add_menu(menu, idx);
1831 gui_create_initial_menus(menu->children);
1833 else
1834 gui_mch_add_menu_item(menu, idx);
1836 menu = menu->next;
1837 ++idx;
1842 * Used recursively by gui_update_menus (see below)
1844 static void
1845 gui_update_menus_recurse(menu, mode)
1846 vimmenu_T *menu;
1847 int mode;
1849 int grey;
1851 while (menu)
1853 if ((menu->modes & menu->enabled & mode)
1854 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
1855 || menu_is_tearoff(menu->dname)
1856 #endif
1858 grey = FALSE;
1859 else
1860 grey = TRUE;
1861 #ifdef FEAT_GUI_ATHENA
1862 /* Hiding menus doesn't work for Athena, it can cause a crash. */
1863 gui_mch_menu_grey(menu, grey);
1864 #else
1865 /* Never hide a toplevel menu, it may make the menubar resize or
1866 * disappear. Same problem for ToolBar items. */
1867 if (vim_strchr(p_go, GO_GREY) != NULL || menu->parent == NULL
1868 # ifdef FEAT_TOOLBAR
1869 || menu_is_toolbar(menu->parent->name)
1870 # endif
1872 gui_mch_menu_grey(menu, grey);
1873 else
1874 gui_mch_menu_hidden(menu, grey);
1875 #endif
1876 gui_update_menus_recurse(menu->children, mode);
1877 menu = menu->next;
1882 * Make sure only the valid menu items appear for this mode. If
1883 * force_menu_update is not TRUE, then we only do this if the mode has changed
1884 * since last time. If "modes" is not 0, then we use these modes instead.
1886 void
1887 gui_update_menus(modes)
1888 int modes;
1890 static int prev_mode = -1;
1891 int mode = 0;
1893 if (modes != 0x0)
1894 mode = modes;
1895 else
1897 mode = get_menu_mode();
1898 if (mode == MENU_INDEX_INVALID)
1899 mode = 0;
1900 else
1901 mode = (1 << mode);
1904 if (force_menu_update || mode != prev_mode)
1906 gui_update_menus_recurse(root_menu, mode);
1907 gui_mch_draw_menubar();
1908 prev_mode = mode;
1909 force_menu_update = FALSE;
1910 #ifdef FEAT_GUI_W32
1911 /* This can leave a tearoff as active window - make sure we
1912 * have the focus <negri>*/
1913 gui_mch_activate_window();
1914 #endif
1918 #if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MOTIF) \
1919 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) || defined(PROTO)
1921 * Check if a key is used as a mnemonic for a toplevel menu.
1922 * Case of the key is ignored.
1925 gui_is_menu_shortcut(key)
1926 int key;
1928 vimmenu_T *menu;
1930 if (key < 256)
1931 key = TOLOWER_LOC(key);
1932 for (menu = root_menu; menu != NULL; menu = menu->next)
1933 if (menu->mnemonic == key
1934 || (menu->mnemonic < 256 && TOLOWER_LOC(menu->mnemonic) == key))
1935 return TRUE;
1936 return FALSE;
1938 #endif
1941 * Display the Special "PopUp" menu as a pop-up at the current mouse
1942 * position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode,
1943 * etc.
1945 void
1946 gui_show_popupmenu()
1948 vimmenu_T *menu;
1949 int mode;
1951 mode = get_menu_mode();
1952 if (mode == MENU_INDEX_INVALID)
1953 return;
1954 mode = menu_mode_chars[mode];
1956 #ifdef FEAT_AUTOCMD
1958 char_u ename[2];
1960 ename[0] = mode;
1961 ename[1] = NUL;
1962 apply_autocmds(EVENT_MENUPOPUP, ename, NULL, FALSE, curbuf);
1964 #endif
1966 for (menu = root_menu; menu != NULL; menu = menu->next)
1967 if (STRNCMP("PopUp", menu->name, 5) == 0 && menu->name[5] == mode)
1968 break;
1970 /* Only show a popup when it is defined and has entries */
1971 if (menu != NULL && menu->children != NULL)
1972 gui_mch_show_popupmenu(menu);
1974 #endif /* FEAT_GUI */
1976 #if (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)) || defined(PROTO)
1979 * Deal with tearoff items that are added like a menu item.
1980 * Currently only for Win32 GUI. Others may follow later.
1983 void
1984 gui_mch_toggle_tearoffs(int enable)
1986 int pri_tab[MENUDEPTH + 1];
1987 int i;
1989 if (enable)
1991 for (i = 0; i < MENUDEPTH; ++i)
1992 pri_tab[i] = 500;
1993 pri_tab[MENUDEPTH] = -1;
1994 gui_create_tearoffs_recurse(root_menu, (char_u *)"", pri_tab, 0);
1996 else
1997 gui_destroy_tearoffs_recurse(root_menu);
1998 s_tearoffs = enable;
2002 * Recursively add tearoff items
2004 static void
2005 gui_create_tearoffs_recurse(menu, pname, pri_tab, pri_idx)
2006 vimmenu_T *menu;
2007 const char_u *pname;
2008 int *pri_tab;
2009 int pri_idx;
2011 char_u *newpname = NULL;
2012 int len;
2013 char_u *s;
2014 char_u *d;
2016 if (pri_tab[pri_idx + 1] != -1)
2017 ++pri_idx;
2018 while (menu != NULL)
2020 if (menu->children != NULL && menu_is_menubar(menu->name))
2022 /* Add the menu name to the menu path. Insert a backslash before
2023 * dots (it's used to separate menu names). */
2024 len = (int)STRLEN(pname) + (int)STRLEN(menu->name);
2025 for (s = menu->name; *s; ++s)
2026 if (*s == '.' || *s == '\\')
2027 ++len;
2028 newpname = alloc(len + TEAR_LEN + 2);
2029 if (newpname != NULL)
2031 STRCPY(newpname, pname);
2032 d = newpname + STRLEN(newpname);
2033 for (s = menu->name; *s; ++s)
2035 if (*s == '.' || *s == '\\')
2036 *d++ = '\\';
2037 *d++ = *s;
2039 *d = NUL;
2041 /* check if tearoff already exists */
2042 if (STRCMP(menu->children->name, TEAR_STRING) != 0)
2044 gui_add_tearoff(newpname, pri_tab, pri_idx - 1);
2045 *d = NUL; /* remove TEAR_STRING */
2048 STRCAT(newpname, ".");
2049 gui_create_tearoffs_recurse(menu->children, newpname,
2050 pri_tab, pri_idx);
2051 vim_free(newpname);
2054 menu = menu->next;
2059 * Add tear-off menu item for a submenu.
2060 * "tearpath" is the menu path, and must have room to add TEAR_STRING.
2062 static void
2063 gui_add_tearoff(tearpath, pri_tab, pri_idx)
2064 char_u *tearpath;
2065 int *pri_tab;
2066 int pri_idx;
2068 char_u *tbuf;
2069 int t;
2070 vimmenu_T menuarg;
2072 tbuf = alloc(5 + (unsigned int)STRLEN(tearpath));
2073 if (tbuf != NULL)
2075 tbuf[0] = K_SPECIAL;
2076 tbuf[1] = K_SECOND(K_TEAROFF);
2077 tbuf[2] = K_THIRD(K_TEAROFF);
2078 STRCPY(tbuf + 3, tearpath);
2079 STRCAT(tbuf + 3, "\r");
2081 STRCAT(tearpath, ".");
2082 STRCAT(tearpath, TEAR_STRING);
2084 /* Priority of tear-off is always 1 */
2085 t = pri_tab[pri_idx + 1];
2086 pri_tab[pri_idx + 1] = 1;
2088 #ifdef FEAT_TOOLBAR
2089 menuarg.iconfile = NULL;
2090 menuarg.iconidx = -1;
2091 menuarg.icon_builtin = FALSE;
2092 #endif
2093 menuarg.noremap[0] = REMAP_NONE;
2094 menuarg.silent[0] = TRUE;
2096 menuarg.modes = MENU_ALL_MODES;
2097 add_menu_path(tearpath, &menuarg, pri_tab, tbuf, FALSE);
2099 menuarg.modes = MENU_TIP_MODE;
2100 add_menu_path(tearpath, &menuarg, pri_tab,
2101 (char_u *)_("Tear off this menu"), FALSE);
2103 pri_tab[pri_idx + 1] = t;
2104 vim_free(tbuf);
2109 * Recursively destroy tearoff items
2111 static void
2112 gui_destroy_tearoffs_recurse(menu)
2113 vimmenu_T *menu;
2115 while (menu)
2117 if (menu->children)
2119 /* check if tearoff exists */
2120 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
2122 /* Disconnect the item and free the memory */
2123 free_menu(&menu->children);
2125 if (menu->children != NULL) /* if not the last one */
2126 gui_destroy_tearoffs_recurse(menu->children);
2128 menu = menu->next;
2132 #endif /* FEAT_GUI_W32 && FEAT_TEAROFF */
2135 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2136 * execute it.
2138 void
2139 ex_emenu(eap)
2140 exarg_T *eap;
2142 vimmenu_T *menu;
2143 char_u *name;
2144 char_u *saved_name;
2145 char_u *p;
2146 int idx;
2147 char_u *mode;
2148 #ifdef FEAT_GUI_MACVIM
2149 char_u *old_arg;
2150 #endif
2152 saved_name = vim_strsave(eap->arg);
2153 if (saved_name == NULL)
2154 return;
2156 menu = root_menu;
2157 name = saved_name;
2158 while (*name)
2160 /* Find in the menu hierarchy */
2161 p = menu_name_skip(name);
2163 while (menu != NULL)
2165 if (menu_name_equal(name, menu))
2167 if (*p == NUL && menu->children != NULL)
2169 EMSG(_("E333: Menu path must lead to a menu item"));
2170 menu = NULL;
2172 else if (*p != NUL && menu->children == NULL)
2174 EMSG(_(e_notsubmenu));
2175 menu = NULL;
2177 break;
2179 menu = menu->next;
2181 if (menu == NULL || *p == NUL)
2182 break;
2183 menu = menu->children;
2184 name = p;
2186 vim_free(saved_name);
2187 if (menu == NULL)
2189 EMSG2(_("E334: Menu not found: %s"), eap->arg);
2190 return;
2193 /* Found the menu, so execute.
2194 * Use the Insert mode entry when returning to Insert mode. */
2195 if (restart_edit
2196 #ifdef FEAT_EVAL
2197 && !current_SID
2198 #endif
2201 mode = (char_u *)"Insert";
2202 idx = MENU_INDEX_INSERT;
2204 else if (eap->addr_count)
2206 pos_T tpos;
2208 mode = (char_u *)"Visual";
2209 idx = MENU_INDEX_VISUAL;
2211 /* GEDDES: This is not perfect - but it is a
2212 * quick way of detecting whether we are doing this from a
2213 * selection - see if the range matches up with the visual
2214 * select start and end. */
2215 if ((curbuf->b_visual.vi_start.lnum == eap->line1)
2216 && (curbuf->b_visual.vi_end.lnum) == eap->line2)
2218 /* Set it up for visual mode - equivalent to gv. */
2219 VIsual_mode = curbuf->b_visual.vi_mode;
2220 tpos = curbuf->b_visual.vi_end;
2221 curwin->w_cursor = curbuf->b_visual.vi_start;
2222 curwin->w_curswant = curbuf->b_visual.vi_curswant;
2224 else
2226 /* Set it up for line-wise visual mode */
2227 VIsual_mode = 'V';
2228 curwin->w_cursor.lnum = eap->line1;
2229 curwin->w_cursor.col = 1;
2230 tpos.lnum = eap->line2;
2231 tpos.col = MAXCOL;
2232 #ifdef FEAT_VIRTUALEDIT
2233 tpos.coladd = 0;
2234 #endif
2237 /* Activate visual mode */
2238 VIsual_active = TRUE;
2239 VIsual_reselect = TRUE;
2240 check_cursor();
2241 VIsual = curwin->w_cursor;
2242 curwin->w_cursor = tpos;
2244 check_cursor();
2246 /* Adjust the cursor to make sure it is in the correct pos
2247 * for exclusive mode */
2248 if (*p_sel == 'e' && gchar_cursor() != NUL)
2249 ++curwin->w_cursor.col;
2251 else
2253 mode = (char_u *)"Normal";
2254 idx = MENU_INDEX_NORMAL;
2257 if (idx != MENU_INDEX_INVALID && menu->strings[idx] != NULL &&
2258 menu->strings[idx][0] != NUL)
2261 /* When executing a script or function execute the commands right now.
2262 * Otherwise put them in the typeahead buffer. */
2263 #ifdef FEAT_EVAL
2264 if (current_SID != 0)
2265 exec_normal_cmd(menu->strings[idx], menu->noremap[idx],
2266 menu->silent[idx]);
2267 else
2268 #endif
2269 ins_typebuf(menu->strings[idx], menu->noremap[idx], 0,
2270 TRUE, menu->silent[idx]);
2272 #ifdef FEAT_GUI_MACVIM
2273 else if (menu->mac_action != NULL && menu->strings[idx][0] == NUL)
2275 /* This allows us to bind a menu to an action without mapping to
2276 * anything so that pressing the menu's key equivalent and typing
2277 * ":emenu ..." does the same thing. (HACK: We count on the fact that
2278 * ex_macaction() only looks at eap->arg.) */
2279 old_arg = eap->arg;
2280 eap->arg = menu->mac_action;
2281 ex_macaction(eap);
2282 eap->arg = old_arg;
2284 #endif /* FEAT_GUI_MACVIM */
2285 else
2286 EMSG2(_("E335: Menu not defined for %s mode"), mode);
2289 #if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MACVIM)\
2290 || (defined(FEAT_GUI_GTK) && defined(FEAT_MENU)) \
2291 || defined(FEAT_BEVAL_TIP) || defined(PROTO)
2293 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy.
2295 vimmenu_T *
2296 gui_find_menu(path_name)
2297 char_u *path_name;
2299 vimmenu_T *menu = NULL;
2300 char_u *name;
2301 char_u *saved_name;
2302 char_u *p;
2304 menu = root_menu;
2306 saved_name = vim_strsave(path_name);
2307 if (saved_name == NULL)
2308 return NULL;
2310 name = saved_name;
2311 while (*name)
2313 /* find the end of one dot-separated name and put a NUL at the dot */
2314 p = menu_name_skip(name);
2316 while (menu != NULL)
2318 if (STRCMP(name, menu->name) == 0 || STRCMP(name, menu->dname) == 0)
2320 if (menu->children == NULL)
2322 /* found a menu item instead of a sub-menu */
2323 if (*p == NUL)
2324 EMSG(_("E336: Menu path must lead to a sub-menu"));
2325 else
2326 EMSG(_(e_notsubmenu));
2327 menu = NULL;
2328 goto theend;
2330 if (*p == NUL) /* found a full match */
2331 goto theend;
2332 break;
2334 menu = menu->next;
2336 if (menu == NULL) /* didn't find it */
2337 break;
2339 /* Found a match, search the sub-menu. */
2340 menu = menu->children;
2341 name = p;
2344 if (menu == NULL)
2345 EMSG(_("E337: Menu not found - check menu names"));
2346 theend:
2347 vim_free(saved_name);
2348 return menu;
2350 #endif
2352 #ifdef FEAT_MULTI_LANG
2354 * Translation of menu names. Just a simple lookup table.
2357 typedef struct
2359 char_u *from; /* English name */
2360 char_u *from_noamp; /* same, without '&' */
2361 char_u *to; /* translated name */
2362 } menutrans_T;
2364 static garray_T menutrans_ga = {0, 0, 0, 0, NULL};
2365 #endif
2368 * ":menutrans".
2369 * This function is also defined without the +multi_lang feature, in which
2370 * case the commands are ignored.
2372 void
2373 ex_menutranslate(eap)
2374 exarg_T *eap UNUSED;
2376 #ifdef FEAT_MULTI_LANG
2377 char_u *arg = eap->arg;
2378 menutrans_T *tp;
2379 int i;
2380 char_u *from, *from_noamp, *to;
2382 if (menutrans_ga.ga_itemsize == 0)
2383 ga_init2(&menutrans_ga, (int)sizeof(menutrans_T), 5);
2386 * ":menutrans clear": clear all translations.
2388 if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5)))
2390 tp = (menutrans_T *)menutrans_ga.ga_data;
2391 for (i = 0; i < menutrans_ga.ga_len; ++i)
2393 vim_free(tp[i].from);
2394 vim_free(tp[i].from_noamp);
2395 vim_free(tp[i].to);
2397 ga_clear(&menutrans_ga);
2398 # ifdef FEAT_EVAL
2399 /* Delete all "menutrans_" global variables. */
2400 del_menutrans_vars();
2401 # endif
2403 else
2405 /* ":menutrans from to": add translation */
2406 from = arg;
2407 arg = menu_skip_part(arg);
2408 to = skipwhite(arg);
2409 *arg = NUL;
2410 arg = menu_skip_part(to);
2411 if (arg == to)
2412 EMSG(_(e_invarg));
2413 else
2415 if (ga_grow(&menutrans_ga, 1) == OK)
2417 tp = (menutrans_T *)menutrans_ga.ga_data;
2418 from = vim_strsave(from);
2419 if (from != NULL)
2421 from_noamp = menu_text(from, NULL, NULL);
2422 to = vim_strnsave(to, (int)(arg - to));
2423 if (from_noamp != NULL && to != NULL)
2425 tp[menutrans_ga.ga_len].from = from;
2426 tp[menutrans_ga.ga_len].from_noamp = from_noamp;
2427 tp[menutrans_ga.ga_len].to = to;
2428 ++menutrans_ga.ga_len;
2430 else
2432 vim_free(from);
2433 vim_free(from_noamp);
2434 vim_free(to);
2440 #endif
2443 #if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
2445 * Find the character just after one part of a menu name.
2447 static char_u *
2448 menu_skip_part(p)
2449 char_u *p;
2451 while (*p != NUL && *p != '.' && !vim_iswhite(*p))
2453 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
2454 ++p;
2455 ++p;
2457 return p;
2459 #endif
2461 #ifdef FEAT_MULTI_LANG
2463 * Lookup part of a menu name in the translations.
2464 * Return a pointer to the translation or NULL if not found.
2466 static char_u *
2467 menutrans_lookup(name, len)
2468 char_u *name;
2469 int len;
2471 menutrans_T *tp = (menutrans_T *)menutrans_ga.ga_data;
2472 int i;
2473 char_u *dname;
2475 for (i = 0; i < menutrans_ga.ga_len; ++i)
2476 if (STRNCMP(name, tp[i].from, len) == 0 && tp[i].from[len] == NUL)
2477 return tp[i].to;
2479 /* Now try again while ignoring '&' characters. */
2480 i = name[len];
2481 name[len] = NUL;
2482 dname = menu_text(name, NULL, NULL);
2483 name[len] = i;
2484 if (dname != NULL)
2486 for (i = 0; i < menutrans_ga.ga_len; ++i)
2487 if (STRCMP(dname, tp[i].from_noamp) == 0)
2489 vim_free(dname);
2490 return tp[i].to;
2492 vim_free(dname);
2495 return NULL;
2497 #endif /* FEAT_MULTI_LANG */
2500 #ifdef FEAT_GUI_MACVIM
2501 vimmenu_T *
2502 menu_for_path(char_u *menu_path)
2504 vimmenu_T *menu;
2505 char_u *name;
2506 char_u *saved_name;
2507 char_u *p;
2509 saved_name = vim_strsave(menu_path);
2510 if (saved_name == NULL)
2511 return NULL;
2513 menu = root_menu;
2514 name = saved_name;
2515 while (*name)
2517 /* Find in the menu hierarchy */
2518 p = menu_name_skip(name);
2520 while (menu != NULL)
2522 if (menu_name_equal(name, menu))
2524 if (*p == NUL && menu->children != NULL)
2526 EMSG(_("E333: Menu path must lead to a menu item"));
2527 menu = NULL;
2529 else if (*p != NUL && menu->children == NULL)
2531 EMSG(_(e_notsubmenu));
2532 menu = NULL;
2534 break;
2536 menu = menu->next;
2538 if (menu == NULL || *p == NUL)
2539 break;
2540 menu = menu->children;
2541 name = p;
2544 vim_free(saved_name);
2546 if (menu == NULL)
2548 EMSG2(_("E334: Menu not found: %s"), menu_path);
2549 return NULL;
2552 return menu;
2556 * Handle the ":macmenu" command.
2558 void
2559 ex_macmenu(eap)
2560 exarg_T *eap;
2562 vimmenu_T *menu = NULL;
2563 char_u *arg;
2564 char_u *menu_path;
2565 char_u *p;
2566 char_u *keys;
2567 int len;
2568 #ifdef FEAT_MULTI_LANG
2569 char_u *tofree = NULL;
2570 char_u *new_cmd;
2571 #endif
2572 char_u *linep;
2573 char_u *key_start;
2574 char_u *key = NULL;
2575 char_u *arg_start = NULL;
2576 int error = FALSE;
2577 char_u *action = NULL;
2578 int mac_key = 0;
2579 int mac_mods = 0;
2580 int mac_alternate = 0;
2581 char_u *last_dash;
2582 int bit;
2583 int set_action = FALSE;
2584 int set_key = FALSE;
2585 int set_alt = FALSE;
2587 arg = eap->arg;
2589 #ifdef FEAT_MULTI_LANG
2591 * Translate menu names as specified with ":menutrans" commands.
2593 menu_path = arg;
2594 while (*menu_path)
2596 /* find the end of one part and check if it should be translated */
2597 p = menu_skip_part(menu_path);
2598 keys = menutrans_lookup(menu_path, (int)(p - menu_path));
2599 if (keys != NULL)
2601 /* found a match: replace with the translated part */
2602 len = (int)STRLEN(keys);
2603 new_cmd = alloc((unsigned)STRLEN(arg) + len + 1);
2604 if (new_cmd == NULL)
2605 break;
2606 mch_memmove(new_cmd, arg, menu_path - arg);
2607 mch_memmove(new_cmd + (menu_path - arg), keys, (size_t)len);
2608 STRCPY(new_cmd + (menu_path - arg) + len, p);
2609 p = new_cmd + (menu_path - arg) + len;
2610 vim_free(tofree);
2611 tofree = new_cmd;
2612 arg = new_cmd;
2614 if (*p != '.')
2615 break;
2616 menu_path = p + 1;
2618 #endif
2621 * Isolate the menu name.
2622 * Skip the menu name, and translate <Tab> into a real TAB.
2624 menu_path = arg;
2625 if (*menu_path == '.')
2627 EMSG2(_(e_invarg2), menu_path);
2628 goto theend;
2631 while (*arg && !vim_iswhite(*arg))
2633 if ((*arg == '\\' || *arg == Ctrl_V) && arg[1] != NUL)
2634 arg++;
2635 else if (STRNICMP(arg, "<TAB>", 5) == 0)
2637 *arg = TAB;
2638 mch_memmove(arg + 1, arg + 5, STRLEN(arg + 4));
2640 arg++;
2642 if (*arg != NUL)
2643 *arg++ = NUL;
2644 arg = skipwhite(arg);
2645 keys = arg;
2647 menu = menu_for_path(menu_path);
2648 if (!menu) goto theend;
2651 * Parse all key=value arguments.
2653 arg = NULL;
2654 linep = keys;
2655 while (!ends_excmd(*linep))
2657 key_start = linep;
2658 if (*linep == '=')
2660 EMSG2(_("E415: unexpected equal sign: %s"), key_start);
2661 error = TRUE;
2662 break;
2666 * Isolate the key ("action", "alt", or "key").
2668 while (*linep && !vim_iswhite(*linep) && *linep != '=')
2669 ++linep;
2670 vim_free(key);
2671 key = vim_strnsave_up(key_start, (int)(linep - key_start));
2672 if (key == NULL)
2674 error = TRUE;
2675 break;
2677 linep = skipwhite(linep);
2680 * Check for the equal sign.
2682 if (*linep != '=')
2684 EMSG2(_("E416: missing equal sign: %s"), key_start);
2685 error = TRUE;
2686 break;
2688 ++linep;
2691 * Isolate the argument.
2693 linep = skipwhite(linep);
2694 arg_start = linep;
2695 linep = skiptowhite(linep);
2697 if (linep == arg_start)
2699 EMSG2(_("E417: missing argument: %s"), key_start);
2700 error = TRUE;
2701 break;
2703 vim_free(arg);
2704 arg = vim_strnsave(arg_start, (int)(linep - arg_start));
2705 if (arg == NULL)
2707 error = TRUE;
2708 break;
2712 * Store the argument.
2714 if (STRCMP(key, "ACTION") == 0)
2716 action = vim_strsave(arg);
2717 if (action == NULL)
2719 error = TRUE;
2720 break;
2723 if (!is_valid_macaction(action))
2725 EMSG2(_("E???: Invalid action: %s"), arg);
2726 error = TRUE;
2727 break;
2730 set_action = TRUE;
2732 else if (STRCMP(key, "ALT") == 0)
2734 len = (int)STRLEN(arg);
2735 if ( (len == 1 && (*arg == '0' || *arg == '1')) ||
2736 (STRICMP("no", arg) == 0 || STRICMP("yes", arg) == 0) )
2738 mac_alternate = (*arg == '1' || STRICMP("yes", arg) == 0);
2739 set_alt = TRUE;
2741 else
2743 EMSG2(_(e_invarg2), arg);
2744 error = TRUE;
2745 break;
2748 else if (STRCMP(key, "KEY") == 0)
2750 if (arg[0] != '<')
2752 EMSG2(_(e_invarg2), arg);
2753 error = TRUE;
2754 break;
2757 if (STRICMP("<nop>", arg) == 0)
2759 /* This is to let the user unset a key equivalent, thereby
2760 * freeing up that key combination to be used for a :map
2761 * command (which would otherwise not be possible since key
2762 * equivalents take precedence over :map). */
2763 mac_key = 0;
2764 mac_mods = 0;
2765 set_key = TRUE;
2766 continue;
2769 /* Find end of modifier list */
2770 last_dash = arg;
2771 for (p = arg + 1; *p == '-' || vim_isIDc(*p); p++)
2773 if (*p == '-')
2775 last_dash = p;
2776 if (p[1] != NUL && p[2] == '>')
2777 ++p; /* anything accepted, like <C-?> */
2779 if (p[0] == 't' && p[1] == '_' && p[2] && p[3])
2780 p += 3; /* skip t_xx, xx may be '-' or '>' */
2783 if (*p == '>') /* found matching '>' */
2785 /* Which modifiers are given? */
2786 mac_mods = 0x0;
2787 for (p = arg + 1; p < last_dash; p++)
2789 if (*p != '-')
2791 bit = name_to_mod_mask(*p);
2792 if (bit == 0x0)
2793 break; /* Illegal modifier name */
2794 mac_mods |= bit;
2799 * Legal modifier name.
2801 if (p >= last_dash)
2804 * Modifier with single letter, or special key name.
2806 if (mac_mods != 0 && last_dash[2] == '>')
2807 mac_key = last_dash[1];
2808 else
2810 mac_key = get_special_key_code(last_dash + 1);
2811 mac_key = handle_x_keys(mac_key);
2816 set_key = (mac_key != 0);
2818 if (mac_key == 0)
2820 EMSG2(_(e_invarg2), arg);
2821 error = TRUE;
2822 break;
2825 else
2827 EMSG2(_(e_invarg2), key_start);
2828 error = TRUE;
2829 break;
2833 * Continue with next argument.
2835 linep = skipwhite(linep);
2838 vim_free(key);
2839 vim_free(arg);
2842 * Store all the keys that were set in the menu item.
2844 if (!error)
2846 if (set_action)
2847 menu->mac_action = action;
2848 if (set_key)
2850 menu->mac_key = mac_key;
2851 menu->mac_mods = mac_mods;
2853 if (set_alt)
2854 menu->mac_alternate = mac_alternate;
2856 else
2858 vim_free(action);
2861 theend:
2862 #ifdef FEAT_MULTI_LANG
2863 vim_free(tofree);
2864 #else
2866 #endif
2869 #endif /* FEAT_GUI_MACVIM */
2871 #endif /* FEAT_MENU */