Fix placement of auxiliary IM window for Core Text
[MacVim.git] / src / menu.c
bloba34905489bb7dba6cdd1eff6eef5ad52980b0e6d
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 menu_path = arg;
259 while (*menu_path)
261 /* find the end of one part and check if it should be translated */
262 p = menu_skip_part(menu_path);
263 map_to = menutrans_lookup(menu_path, (int)(p - menu_path));
264 if (map_to != NULL)
266 /* found a match: replace with the translated part */
267 i = (int)STRLEN(map_to);
268 new_cmd = alloc((unsigned)STRLEN(arg) + i + 1);
269 if (new_cmd == NULL)
270 break;
271 mch_memmove(new_cmd, arg, menu_path - arg);
272 mch_memmove(new_cmd + (menu_path - arg), map_to, (size_t)i);
273 STRCPY(new_cmd + (menu_path - arg) + i, p);
274 p = new_cmd + (menu_path - arg) + i;
275 vim_free(tofree);
276 tofree = new_cmd;
277 arg = new_cmd;
279 if (*p != '.')
280 break;
281 menu_path = p + 1;
283 #endif
286 * Isolate the menu name.
287 * Skip the menu name, and translate <Tab> into a real TAB.
289 menu_path = arg;
290 if (*menu_path == '.')
292 EMSG2(_(e_invarg2), menu_path);
293 goto theend;
296 while (*arg && !vim_iswhite(*arg))
298 if ((*arg == '\\' || *arg == Ctrl_V) && arg[1] != NUL)
299 arg++;
300 else if (STRNICMP(arg, "<TAB>", 5) == 0)
302 *arg = TAB;
303 STRMOVE(arg + 1, arg + 5);
305 arg++;
307 if (*arg != NUL)
308 *arg++ = NUL;
309 arg = skipwhite(arg);
310 map_to = arg;
313 * If there is only a menu name, display menus with that name.
315 if (*map_to == NUL && !unmenu && enable == MAYBE)
317 show_menus(menu_path, modes);
318 goto theend;
320 else if (*map_to != NUL && (unmenu || enable != MAYBE))
322 EMSG(_(e_trailing));
323 goto theend;
325 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) \
326 || defined(FEAT_GUI_MACVIM))
327 old_menu_height = gui.menu_height;
328 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
329 old_toolbar_height = gui.toolbar_height;
330 # endif
331 #endif
333 if (enable != MAYBE)
336 * Change sensitivity of the menu.
337 * For the PopUp menu, remove a menu for each mode separately.
338 * Careful: menu_nable_recurse() changes menu_path.
340 if (STRCMP(menu_path, "*") == 0) /* meaning: do all menus */
341 menu_path = (char_u *)"";
343 if (menu_is_popup(menu_path))
345 for (i = 0; i < MENU_INDEX_TIP; ++i)
346 if (modes & (1 << i))
348 p = popup_mode_name(menu_path, i);
349 if (p != NULL)
351 menu_nable_recurse(root_menu, p, MENU_ALL_MODES,
352 enable);
353 vim_free(p);
357 menu_nable_recurse(root_menu, menu_path, modes, enable);
359 else if (unmenu)
362 * Delete menu(s).
364 if (STRCMP(menu_path, "*") == 0) /* meaning: remove all menus */
365 menu_path = (char_u *)"";
368 * For the PopUp menu, remove a menu for each mode separately.
370 if (menu_is_popup(menu_path))
372 for (i = 0; i < MENU_INDEX_TIP; ++i)
373 if (modes & (1 << i))
375 p = popup_mode_name(menu_path, i);
376 if (p != NULL)
378 remove_menu(&root_menu, p, MENU_ALL_MODES, TRUE);
379 vim_free(p);
384 /* Careful: remove_menu() changes menu_path */
385 remove_menu(&root_menu, menu_path, modes, FALSE);
387 else
390 * Add menu(s).
391 * Replace special key codes.
393 if (STRICMP(map_to, "<nop>") == 0) /* "<Nop>" means nothing */
395 map_to = (char_u *)"";
396 map_buf = NULL;
398 else if (modes & MENU_TIP_MODE)
399 map_buf = NULL; /* Menu tips are plain text. */
400 else
401 map_to = replace_termcodes(map_to, &map_buf, FALSE, TRUE, special);
402 menuarg.modes = modes;
403 #ifdef FEAT_TOOLBAR
404 menuarg.iconfile = icon;
405 #endif
406 menuarg.noremap[0] = noremap;
407 menuarg.silent[0] = silent;
408 add_menu_path(menu_path, &menuarg, pri_tab, map_to
409 #ifdef FEAT_GUI_W32
410 , TRUE
411 #endif
415 * For the PopUp menu, add a menu for each mode separately.
417 if (menu_is_popup(menu_path))
419 for (i = 0; i < MENU_INDEX_TIP; ++i)
420 if (modes & (1 << i))
422 p = popup_mode_name(menu_path, i);
423 if (p != NULL)
425 /* Include all modes, to make ":amenu" work */
426 menuarg.modes = modes;
427 #ifdef FEAT_TOOLBAR
428 menuarg.iconfile = NULL;
429 menuarg.iconidx = -1;
430 menuarg.icon_builtin = FALSE;
431 #endif
432 add_menu_path(p, &menuarg, pri_tab, map_to
433 #ifdef FEAT_GUI_W32
434 , TRUE
435 #endif
437 vim_free(p);
442 vim_free(map_buf);
445 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
446 /* If the menubar height changed, resize the window */
447 if (gui.in_use
448 && (gui.menu_height != old_menu_height
449 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
450 || gui.toolbar_height != old_toolbar_height
451 # endif
453 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
454 #endif
456 theend:
457 #ifdef FEAT_MULTI_LANG
458 vim_free(tofree);
459 #else
461 #endif
465 * Add the menu with the given name to the menu hierarchy
467 static int
468 add_menu_path(menu_path, menuarg, pri_tab, call_data
469 #ifdef FEAT_GUI_W32
470 , addtearoff
471 #endif
473 char_u *menu_path;
474 vimmenu_T *menuarg; /* passes modes, iconfile, iconidx,
475 icon_builtin, silent[0], noremap[0] */
476 int *pri_tab;
477 char_u *call_data;
478 #ifdef FEAT_GUI_W32
479 int addtearoff; /* may add tearoff item */
480 #endif
482 char_u *path_name;
483 int modes = menuarg->modes;
484 vimmenu_T **menup;
485 vimmenu_T *menu = NULL;
486 vimmenu_T *parent;
487 vimmenu_T **lower_pri;
488 char_u *p;
489 char_u *name;
490 char_u *dname;
491 char_u *next_name;
492 int i;
493 int c;
494 #ifdef FEAT_GUI
495 int idx;
496 int new_idx;
497 #endif
498 int pri_idx = 0;
499 int old_modes = 0;
500 int amenu;
502 /* Make a copy so we can stuff around with it, since it could be const */
503 path_name = vim_strsave(menu_path);
504 if (path_name == NULL)
505 return FAIL;
506 menup = &root_menu;
507 parent = NULL;
508 name = path_name;
509 while (*name)
511 /* Get name of this element in the menu hierarchy, and the simplified
512 * name (without mnemonic and accelerator text). */
513 next_name = menu_name_skip(name);
514 dname = menu_text(name, NULL, NULL);
515 if (dname == NULL)
516 goto erret;
517 if (*dname == NUL)
519 /* Only a mnemonic or accelerator is not valid. */
520 EMSG(_("E792: Empty menu name"));
521 goto erret;
524 /* See if it's already there */
525 lower_pri = menup;
526 #ifdef FEAT_GUI
527 idx = 0;
528 new_idx = 0;
529 #endif
530 menu = *menup;
531 while (menu != NULL)
533 if (menu_name_equal(name, menu) || menu_name_equal(dname, menu))
535 if (*next_name == NUL && menu->children != NULL)
537 if (!sys_menu)
538 EMSG(_("E330: Menu path must not lead to a sub-menu"));
539 goto erret;
541 if (*next_name != NUL && menu->children == NULL
542 #ifdef FEAT_GUI_W32
543 && addtearoff
544 #endif
547 if (!sys_menu)
548 EMSG(_(e_notsubmenu));
549 goto erret;
551 break;
553 menup = &menu->next;
555 /* Count menus, to find where this one needs to be inserted.
556 * Ignore menus that are not in the menubar (PopUp and Toolbar) */
557 if (parent != NULL || menu_is_menubar(menu->name))
559 #ifdef FEAT_GUI
560 ++idx;
561 #endif
562 if (menu->priority <= pri_tab[pri_idx])
564 lower_pri = menup;
565 #ifdef FEAT_GUI
566 new_idx = idx;
567 #endif
570 menu = menu->next;
573 if (menu == NULL)
575 if (*next_name == NUL && parent == NULL)
577 EMSG(_("E331: Must not add menu items directly to menu bar"));
578 goto erret;
581 if (menu_is_separator(dname) && *next_name != NUL)
583 EMSG(_("E332: Separator cannot be part of a menu path"));
584 goto erret;
587 /* Not already there, so lets add it */
588 menu = (vimmenu_T *)alloc_clear((unsigned)sizeof(vimmenu_T));
589 if (menu == NULL)
590 goto erret;
592 menu->modes = modes;
593 menu->enabled = MENU_ALL_MODES;
594 menu->name = vim_strsave(name);
595 /* separate mnemonic and accelerator text from actual menu name */
596 menu->dname = menu_text(name, &menu->mnemonic, &menu->actext);
597 menu->priority = pri_tab[pri_idx];
598 menu->parent = parent;
599 #ifdef FEAT_GUI_MOTIF
600 menu->sensitive = TRUE; /* the default */
601 #endif
602 #ifdef FEAT_BEVAL_TIP
603 menu->tip = NULL;
604 #endif
605 #ifdef FEAT_GUI_ATHENA
606 menu->image = None; /* X-Windows definition for NULL*/
607 #endif
610 * Add after menu that has lower priority.
612 menu->next = *lower_pri;
613 *lower_pri = menu;
615 old_modes = 0;
617 #ifdef FEAT_TOOLBAR
618 menu->iconidx = menuarg->iconidx;
619 menu->icon_builtin = menuarg->icon_builtin;
620 if (*next_name == NUL && menuarg->iconfile != NULL)
621 menu->iconfile = vim_strsave(menuarg->iconfile);
622 #endif
623 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
624 /* the tearoff item must be present in the modes of each item. */
625 if (parent != NULL && menu_is_tearoff(parent->children->dname))
626 parent->children->modes |= modes;
627 #endif
629 else
631 old_modes = menu->modes;
634 * If this menu option was previously only available in other
635 * modes, then make sure it's available for this one now
636 * Also enable a menu when it's created or changed.
638 #ifdef FEAT_GUI_W32
639 /* If adding a tearbar (addtearoff == FALSE) don't update modes */
640 if (addtearoff)
641 #endif
643 menu->modes |= modes;
644 menu->enabled |= modes;
648 #ifdef FEAT_GUI
650 * Add the menu item when it's used in one of the modes, but not when
651 * only a tooltip is defined.
653 if ((old_modes & MENU_ALL_MODES) == 0
654 && (menu->modes & MENU_ALL_MODES) != 0)
656 if (gui.in_use) /* Otherwise it will be added when GUI starts */
658 if (*next_name == NUL)
660 /* Real menu item, not sub-menu */
661 gui_mch_add_menu_item(menu, new_idx);
663 /* Want to update menus now even if mode not changed */
664 force_menu_update = TRUE;
666 else
668 /* Sub-menu (not at end of path yet) */
669 gui_mch_add_menu(menu, new_idx);
673 # if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
674 /* When adding a new submenu, may add a tearoff item */
675 if ( addtearoff
676 && *next_name
677 && vim_strchr(p_go, GO_TEAROFF) != NULL
678 && menu_is_menubar(name))
680 char_u *tearpath;
683 * The pointers next_name & path_name refer to a string with
684 * \'s and ^V's stripped out. But menu_path is a "raw"
685 * string, so we must correct for special characters.
687 tearpath = alloc((unsigned int)STRLEN(menu_path) + TEAR_LEN + 2);
688 if (tearpath != NULL)
690 char_u *s;
691 int idx;
693 STRCPY(tearpath, menu_path);
694 idx = (int)(next_name - path_name - 1);
695 for (s = tearpath; *s && s < tearpath + idx; mb_ptr_adv(s))
697 if ((*s == '\\' || *s == Ctrl_V) && s[1])
699 ++idx;
700 ++s;
703 tearpath[idx] = NUL;
704 gui_add_tearoff(tearpath, pri_tab, pri_idx);
705 vim_free(tearpath);
708 # endif
710 #endif /* FEAT_GUI */
712 menup = &menu->children;
713 parent = menu;
714 name = next_name;
715 vim_free(dname);
716 dname = NULL;
717 if (pri_tab[pri_idx + 1] != -1)
718 ++pri_idx;
720 vim_free(path_name);
723 * Only add system menu items which have not been defined yet.
724 * First check if this was an ":amenu".
726 amenu = ((modes & (MENU_NORMAL_MODE | MENU_INSERT_MODE)) ==
727 (MENU_NORMAL_MODE | MENU_INSERT_MODE));
728 if (sys_menu)
729 modes &= ~old_modes;
731 if (menu != NULL && modes)
733 #ifdef FEAT_GUI
734 menu->cb = gui_menu_cb;
735 #endif
736 p = (call_data == NULL) ? NULL : vim_strsave(call_data);
738 /* loop over all modes, may add more than one */
739 for (i = 0; i < MENU_MODES; ++i)
741 if (modes & (1 << i))
743 /* free any old menu */
744 free_menu_string(menu, i);
746 /* For "amenu", may insert an extra character.
747 * Don't do this if adding a tearbar (addtearoff == FALSE).
748 * Don't do this for "<Nop>". */
749 c = 0;
750 if (amenu && call_data != NULL && *call_data != NUL
751 #ifdef FEAT_GUI_W32
752 && addtearoff
753 #endif
756 switch (1 << i)
758 case MENU_VISUAL_MODE:
759 case MENU_SELECT_MODE:
760 case MENU_OP_PENDING_MODE:
761 case MENU_CMDLINE_MODE:
762 c = Ctrl_C;
763 break;
764 case MENU_INSERT_MODE:
765 c = Ctrl_O;
766 break;
770 if (c)
772 menu->strings[i] = alloc((unsigned)(STRLEN(call_data) + 4));
773 if (menu->strings[i] != NULL)
775 menu->strings[i][0] = c;
776 STRCPY(menu->strings[i] + 1, call_data);
777 if (c == Ctrl_C)
779 int len = (int)STRLEN(menu->strings[i]);
781 /* Append CTRL-\ CTRL-G to obey 'insertmode'. */
782 menu->strings[i][len] = Ctrl_BSL;
783 menu->strings[i][len + 1] = Ctrl_G;
784 menu->strings[i][len + 2] = NUL;
788 else
789 menu->strings[i] = p;
790 menu->noremap[i] = menuarg->noremap[0];
791 menu->silent[i] = menuarg->silent[0];
794 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
795 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
796 /* Need to update the menu tip. */
797 if (modes & MENU_TIP_MODE)
798 gui_mch_menu_set_tip(menu);
799 #endif
801 return OK;
803 erret:
804 vim_free(path_name);
805 vim_free(dname);
807 /* Delete any empty submenu we added before discovering the error. Repeat
808 * for higher levels. */
809 while (parent != NULL && parent->children == NULL)
811 if (parent->parent == NULL)
812 menup = &root_menu;
813 else
814 menup = &parent->parent->children;
815 for ( ; *menup != NULL && *menup != parent; menup = &((*menup)->next))
817 if (*menup == NULL) /* safety check */
818 break;
819 parent = parent->parent;
820 free_menu(menup);
822 return FAIL;
826 * Set the (sub)menu with the given name to enabled or disabled.
827 * Called recursively.
829 static int
830 menu_nable_recurse(menu, name, modes, enable)
831 vimmenu_T *menu;
832 char_u *name;
833 int modes;
834 int enable;
836 char_u *p;
838 if (menu == NULL)
839 return OK; /* Got to bottom of hierarchy */
841 /* Get name of this element in the menu hierarchy */
842 p = menu_name_skip(name);
844 /* Find the menu */
845 while (menu != NULL)
847 if (*name == NUL || *name == '*' || menu_name_equal(name, menu))
849 if (*p != NUL)
851 if (menu->children == NULL)
853 EMSG(_(e_notsubmenu));
854 return FAIL;
856 if (menu_nable_recurse(menu->children, p, modes, enable)
857 == FAIL)
858 return FAIL;
860 else
861 if (enable)
862 menu->enabled |= modes;
863 else
864 menu->enabled &= ~modes;
867 * When name is empty, we are doing all menu items for the given
868 * modes, so keep looping, otherwise we are just doing the named
869 * menu item (which has been found) so break here.
871 if (*name != NUL && *name != '*')
872 break;
874 menu = menu->next;
876 if (*name != NUL && *name != '*' && menu == NULL)
878 EMSG2(_(e_nomenu), name);
879 return FAIL;
882 #ifdef FEAT_GUI
883 /* Want to update menus now even if mode not changed */
884 force_menu_update = TRUE;
885 #endif
887 return OK;
891 * Remove the (sub)menu with the given name from the menu hierarchy
892 * Called recursively.
894 static int
895 remove_menu(menup, name, modes, silent)
896 vimmenu_T **menup;
897 char_u *name;
898 int modes;
899 int silent; /* don't give error messages */
901 vimmenu_T *menu;
902 vimmenu_T *child;
903 char_u *p;
905 if (*menup == NULL)
906 return OK; /* Got to bottom of hierarchy */
908 /* Get name of this element in the menu hierarchy */
909 p = menu_name_skip(name);
911 /* Find the menu */
912 while ((menu = *menup) != NULL)
914 if (*name == NUL || menu_name_equal(name, menu))
916 if (*p != NUL && menu->children == NULL)
918 if (!silent)
919 EMSG(_(e_notsubmenu));
920 return FAIL;
922 if ((menu->modes & modes) != 0x0)
924 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
926 * If we are removing all entries for this menu,MENU_ALL_MODES,
927 * Then kill any tearoff before we start
929 if (*p == NUL && modes == MENU_ALL_MODES)
931 if (IsWindow(menu->tearoff_handle))
932 DestroyWindow(menu->tearoff_handle);
934 #endif
935 if (remove_menu(&menu->children, p, modes, silent) == FAIL)
936 return FAIL;
938 else if (*name != NUL)
940 if (!silent)
941 EMSG(_(e_othermode));
942 return FAIL;
946 * When name is empty, we are removing all menu items for the given
947 * modes, so keep looping, otherwise we are just removing the named
948 * menu item (which has been found) so break here.
950 if (*name != NUL)
951 break;
953 /* Remove the menu item for the given mode[s]. If the menu item
954 * is no longer valid in ANY mode, delete it */
955 menu->modes &= ~modes;
956 if (modes & MENU_TIP_MODE)
957 free_menu_string(menu, MENU_INDEX_TIP);
958 if ((menu->modes & MENU_ALL_MODES) == 0)
959 free_menu(menup);
960 else
961 menup = &menu->next;
963 else
964 menup = &menu->next;
966 if (*name != NUL)
968 if (menu == NULL)
970 if (!silent)
971 EMSG2(_(e_nomenu), name);
972 return FAIL;
976 /* Recalculate modes for menu based on the new updated children */
977 menu->modes &= ~modes;
978 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
979 if ((s_tearoffs) && (menu->children != NULL)) /* there's a tear bar.. */
980 child = menu->children->next; /* don't count tearoff bar */
981 else
982 #endif
983 child = menu->children;
984 for ( ; child != NULL; child = child->next)
985 menu->modes |= child->modes;
986 if (modes & MENU_TIP_MODE)
988 free_menu_string(menu, MENU_INDEX_TIP);
989 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
990 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
991 /* Need to update the menu tip. */
992 if (gui.in_use)
993 gui_mch_menu_set_tip(menu);
994 #endif
996 if ((menu->modes & MENU_ALL_MODES) == 0)
998 /* The menu item is no longer valid in ANY mode, so delete it */
999 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
1000 if (s_tearoffs && menu->children != NULL) /* there's a tear bar.. */
1001 free_menu(&menu->children);
1002 #endif
1003 *menup = menu;
1004 free_menu(menup);
1008 return OK;
1012 * Free the given menu structure and remove it from the linked list.
1014 static void
1015 free_menu(menup)
1016 vimmenu_T **menup;
1018 int i;
1019 vimmenu_T *menu;
1021 menu = *menup;
1023 #ifdef FEAT_GUI
1024 /* Free machine specific menu structures (only when already created) */
1025 /* Also may rebuild a tearoff'ed menu */
1026 if (gui.in_use)
1027 gui_mch_destroy_menu(menu);
1028 #endif
1030 /* Don't change *menup until after calling gui_mch_destroy_menu(). The
1031 * MacOS code needs the original structure to properly delete the menu. */
1032 *menup = menu->next;
1033 vim_free(menu->name);
1034 vim_free(menu->dname);
1035 vim_free(menu->actext);
1036 #ifdef FEAT_TOOLBAR
1037 vim_free(menu->iconfile);
1038 # ifdef FEAT_GUI_MOTIF
1039 vim_free(menu->xpm_fname);
1040 # endif
1041 #ifdef FEAT_GUI_MACVIM
1042 vim_free(menu->mac_action);
1043 #endif
1044 #endif
1045 for (i = 0; i < MENU_MODES; i++)
1046 free_menu_string(menu, i);
1047 vim_free(menu);
1049 #ifdef FEAT_GUI
1050 /* Want to update menus now even if mode not changed */
1051 force_menu_update = TRUE;
1052 #endif
1056 * Free the menu->string with the given index.
1058 static void
1059 free_menu_string(menu, idx)
1060 vimmenu_T *menu;
1061 int idx;
1063 int count = 0;
1064 int i;
1066 for (i = 0; i < MENU_MODES; i++)
1067 if (menu->strings[i] == menu->strings[idx])
1068 count++;
1069 if (count == 1)
1070 vim_free(menu->strings[idx]);
1071 menu->strings[idx] = NULL;
1075 * Show the mapping associated with a menu item or hierarchy in a sub-menu.
1077 static int
1078 show_menus(path_name, modes)
1079 char_u *path_name;
1080 int modes;
1082 char_u *p;
1083 char_u *name;
1084 vimmenu_T *menu;
1085 vimmenu_T *parent = NULL;
1087 menu = root_menu;
1088 name = path_name = vim_strsave(path_name);
1089 if (path_name == NULL)
1090 return FAIL;
1092 /* First, find the (sub)menu with the given name */
1093 while (*name)
1095 p = menu_name_skip(name);
1096 while (menu != NULL)
1098 if (menu_name_equal(name, menu))
1100 /* Found menu */
1101 if (*p != NUL && menu->children == NULL)
1103 EMSG(_(e_notsubmenu));
1104 vim_free(path_name);
1105 return FAIL;
1107 else if ((menu->modes & modes) == 0x0)
1109 EMSG(_(e_othermode));
1110 vim_free(path_name);
1111 return FAIL;
1113 break;
1115 menu = menu->next;
1117 if (menu == NULL)
1119 EMSG2(_(e_nomenu), name);
1120 vim_free(path_name);
1121 return FAIL;
1123 name = p;
1124 parent = menu;
1125 menu = menu->children;
1127 vim_free(path_name);
1129 /* Now we have found the matching menu, and we list the mappings */
1130 /* Highlight title */
1131 MSG_PUTS_TITLE(_("\n--- Menus ---"));
1133 show_menus_recursive(parent, modes, 0);
1134 return OK;
1138 * Recursively show the mappings associated with the menus under the given one
1140 static void
1141 show_menus_recursive(menu, modes, depth)
1142 vimmenu_T *menu;
1143 int modes;
1144 int depth;
1146 int i;
1147 int bit;
1149 if (menu != NULL && (menu->modes & modes) == 0x0)
1150 return;
1152 if (menu != NULL)
1154 msg_putchar('\n');
1155 if (got_int) /* "q" hit for "--more--" */
1156 return;
1157 for (i = 0; i < depth; i++)
1158 MSG_PUTS(" ");
1159 if (menu->priority)
1161 msg_outnum((long)menu->priority);
1162 MSG_PUTS(" ");
1164 /* Same highlighting as for directories!? */
1165 msg_outtrans_attr(menu->name, hl_attr(HLF_D));
1168 if (menu != NULL && menu->children == NULL)
1170 for (bit = 0; bit < MENU_MODES; bit++)
1171 if ((menu->modes & modes & (1 << bit)) != 0)
1173 msg_putchar('\n');
1174 if (got_int) /* "q" hit for "--more--" */
1175 return;
1176 for (i = 0; i < depth + 2; i++)
1177 MSG_PUTS(" ");
1178 msg_putchar(menu_mode_chars[bit]);
1179 if (menu->noremap[bit] == REMAP_NONE)
1180 msg_putchar('*');
1181 else if (menu->noremap[bit] == REMAP_SCRIPT)
1182 msg_putchar('&');
1183 else
1184 msg_putchar(' ');
1185 if (menu->silent[bit])
1186 msg_putchar('s');
1187 else
1188 msg_putchar(' ');
1189 if ((menu->modes & menu->enabled & (1 << bit)) == 0)
1190 msg_putchar('-');
1191 else
1192 msg_putchar(' ');
1193 MSG_PUTS(" ");
1194 if (*menu->strings[bit] == NUL)
1195 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
1196 else
1197 msg_outtrans_special(menu->strings[bit], FALSE);
1200 else
1202 if (menu == NULL)
1204 menu = root_menu;
1205 depth--;
1207 else
1208 menu = menu->children;
1210 /* recursively show all children. Skip PopUp[nvoci]. */
1211 for (; menu != NULL && !got_int; menu = menu->next)
1212 if (!menu_is_hidden(menu->dname))
1213 show_menus_recursive(menu, modes, depth + 1);
1217 #ifdef FEAT_CMDL_COMPL
1220 * Used when expanding menu names.
1222 static vimmenu_T *expand_menu = NULL;
1223 static int expand_modes = 0x0;
1224 static int expand_emenu; /* TRUE for ":emenu" command */
1227 * Work out what to complete when doing command line completion of menu names.
1229 char_u *
1230 set_context_in_menu_cmd(xp, cmd, arg, forceit)
1231 expand_T *xp;
1232 char_u *cmd;
1233 char_u *arg;
1234 int forceit;
1236 char_u *after_dot;
1237 char_u *p;
1238 char_u *path_name = NULL;
1239 char_u *name;
1240 int unmenu;
1241 vimmenu_T *menu;
1242 int expand_menus;
1244 xp->xp_context = EXPAND_UNSUCCESSFUL;
1247 /* Check for priority numbers, enable and disable */
1248 for (p = arg; *p; ++p)
1249 if (!VIM_ISDIGIT(*p) && *p != '.')
1250 break;
1252 if (!vim_iswhite(*p))
1254 if (STRNCMP(arg, "enable", 6) == 0
1255 && (arg[6] == NUL || vim_iswhite(arg[6])))
1256 p = arg + 6;
1257 else if (STRNCMP(arg, "disable", 7) == 0
1258 && (arg[7] == NUL || vim_iswhite(arg[7])))
1259 p = arg + 7;
1260 else
1261 p = arg;
1264 while (*p != NUL && vim_iswhite(*p))
1265 ++p;
1267 arg = after_dot = p;
1269 for (; *p && !vim_iswhite(*p); ++p)
1271 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
1272 p++;
1273 else if (*p == '.')
1274 after_dot = p + 1;
1277 /* ":tearoff" and ":popup" only use menus, not entries */
1278 expand_menus = !((*cmd == 't' && cmd[1] == 'e') || *cmd == 'p');
1279 expand_emenu = (*cmd == 'e');
1280 if (expand_menus && vim_iswhite(*p))
1281 return NULL; /* TODO: check for next command? */
1282 if (*p == NUL) /* Complete the menu name */
1285 * With :unmenu, you only want to match menus for the appropriate mode.
1286 * With :menu though you might want to add a menu with the same name as
1287 * one in another mode, so match menus from other modes too.
1289 expand_modes = get_menu_cmd_modes(cmd, forceit, NULL, &unmenu);
1290 if (!unmenu)
1291 expand_modes = MENU_ALL_MODES;
1293 menu = root_menu;
1294 if (after_dot != arg)
1296 path_name = alloc((unsigned)(after_dot - arg));
1297 if (path_name == NULL)
1298 return NULL;
1299 vim_strncpy(path_name, arg, after_dot - arg - 1);
1301 name = path_name;
1302 while (name != NULL && *name)
1304 p = menu_name_skip(name);
1305 while (menu != NULL)
1307 if (menu_name_equal(name, menu))
1309 /* Found menu */
1310 if ((*p != NUL && menu->children == NULL)
1311 || ((menu->modes & expand_modes) == 0x0))
1314 * Menu path continues, but we have reached a leaf.
1315 * Or menu exists only in another mode.
1317 vim_free(path_name);
1318 return NULL;
1320 break;
1322 menu = menu->next;
1324 if (menu == NULL)
1326 /* No menu found with the name we were looking for */
1327 vim_free(path_name);
1328 return NULL;
1330 name = p;
1331 menu = menu->children;
1333 vim_free(path_name);
1335 xp->xp_context = expand_menus ? EXPAND_MENUNAMES : EXPAND_MENUS;
1336 xp->xp_pattern = after_dot;
1337 expand_menu = menu;
1339 else /* We're in the mapping part */
1340 xp->xp_context = EXPAND_NOTHING;
1341 return NULL;
1345 * Function given to ExpandGeneric() to obtain the list of (sub)menus (not
1346 * entries).
1348 char_u *
1349 get_menu_name(xp, idx)
1350 expand_T *xp UNUSED;
1351 int idx;
1353 static vimmenu_T *menu = NULL;
1354 char_u *str;
1356 if (idx == 0) /* first call: start at first item */
1357 menu = expand_menu;
1359 /* Skip PopUp[nvoci]. */
1360 while (menu != NULL && (menu_is_hidden(menu->dname)
1361 || menu_is_separator(menu->dname)
1362 || menu_is_tearoff(menu->dname)
1363 || menu->children == NULL))
1364 menu = menu->next;
1366 if (menu == NULL) /* at end of linked list */
1367 return NULL;
1369 if (menu->modes & expand_modes)
1370 str = menu->dname;
1371 else
1372 str = (char_u *)"";
1374 /* Advance to next menu entry. */
1375 menu = menu->next;
1377 return str;
1381 * Function given to ExpandGeneric() to obtain the list of menus and menu
1382 * entries.
1384 char_u *
1385 get_menu_names(xp, idx)
1386 expand_T *xp UNUSED;
1387 int idx;
1389 static vimmenu_T *menu = NULL;
1390 static char_u tbuffer[256]; /*hack*/
1391 char_u *str;
1393 if (idx == 0) /* first call: start at first item */
1394 menu = expand_menu;
1396 /* Skip Browse-style entries, popup menus and separators. */
1397 while (menu != NULL
1398 && ( menu_is_hidden(menu->dname)
1399 || (expand_emenu && menu_is_separator(menu->dname))
1400 || menu_is_tearoff(menu->dname)
1401 #ifndef FEAT_BROWSE
1402 || menu->dname[STRLEN(menu->dname) - 1] == '.'
1403 #endif
1405 menu = menu->next;
1407 if (menu == NULL) /* at end of linked list */
1408 return NULL;
1410 if (menu->modes & expand_modes)
1412 if (menu->children != NULL)
1414 STRCPY(tbuffer, menu->dname);
1415 /* hack on menu separators: use a 'magic' char for the separator
1416 * so that '.' in names gets escaped properly */
1417 STRCAT(tbuffer, "\001");
1418 str = tbuffer;
1420 else
1421 str = menu->dname;
1423 else
1424 str = (char_u *)"";
1426 /* Advance to next menu entry. */
1427 menu = menu->next;
1429 return str;
1431 #endif /* FEAT_CMDL_COMPL */
1434 * Skip over this element of the menu path and return the start of the next
1435 * element. Any \ and ^Vs are removed from the current element.
1436 * "name" may be modified.
1438 char_u *
1439 menu_name_skip(name)
1440 char_u *name;
1442 char_u *p;
1444 for (p = name; *p && *p != '.'; mb_ptr_adv(p))
1446 if (*p == '\\' || *p == Ctrl_V)
1448 STRMOVE(p, p + 1);
1449 if (*p == NUL)
1450 break;
1453 if (*p)
1454 *p++ = NUL;
1455 return p;
1459 * Return TRUE when "name" matches with menu "menu". The name is compared in
1460 * two ways: raw menu name and menu name without '&'. ignore part after a TAB.
1462 static int
1463 menu_name_equal(name, menu)
1464 char_u *name;
1465 vimmenu_T *menu;
1467 return (menu_namecmp(name, menu->name) || menu_namecmp(name, menu->dname));
1470 static int
1471 menu_namecmp(name, mname)
1472 char_u *name;
1473 char_u *mname;
1475 int i;
1477 for (i = 0; name[i] != NUL && name[i] != TAB; ++i)
1478 if (name[i] != mname[i])
1479 break;
1480 return ((name[i] == NUL || name[i] == TAB)
1481 && (mname[i] == NUL || mname[i] == TAB));
1485 * Return the modes specified by the given menu command (eg :menu! returns
1486 * MENU_CMDLINE_MODE | MENU_INSERT_MODE).
1487 * If "noremap" is not NULL, then the flag it points to is set according to
1488 * whether the command is a "nore" command.
1489 * If "unmenu" is not NULL, then the flag it points to is set according to
1490 * whether the command is an "unmenu" command.
1492 static int
1493 get_menu_cmd_modes(cmd, forceit, noremap, unmenu)
1494 char_u *cmd;
1495 int forceit; /* Was there a "!" after the command? */
1496 int *noremap;
1497 int *unmenu;
1499 int modes;
1501 switch (*cmd++)
1503 case 'v': /* vmenu, vunmenu, vnoremenu */
1504 modes = MENU_VISUAL_MODE | MENU_SELECT_MODE;
1505 break;
1506 case 'x': /* xmenu, xunmenu, xnoremenu */
1507 modes = MENU_VISUAL_MODE;
1508 break;
1509 case 's': /* smenu, sunmenu, snoremenu */
1510 modes = MENU_SELECT_MODE;
1511 break;
1512 case 'o': /* omenu */
1513 modes = MENU_OP_PENDING_MODE;
1514 break;
1515 case 'i': /* imenu */
1516 modes = MENU_INSERT_MODE;
1517 break;
1518 case 't':
1519 modes = MENU_TIP_MODE; /* tmenu */
1520 break;
1521 case 'c': /* cmenu */
1522 modes = MENU_CMDLINE_MODE;
1523 break;
1524 case 'a': /* amenu */
1525 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE | MENU_NORMAL_MODE
1526 | MENU_VISUAL_MODE | MENU_SELECT_MODE
1527 | MENU_OP_PENDING_MODE;
1528 break;
1529 case 'n':
1530 if (*cmd != 'o') /* nmenu, not noremenu */
1532 modes = MENU_NORMAL_MODE;
1533 break;
1535 /* FALLTHROUGH */
1536 default:
1537 --cmd;
1538 if (forceit) /* menu!! */
1539 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE;
1540 else /* menu */
1541 modes = MENU_NORMAL_MODE | MENU_VISUAL_MODE | MENU_SELECT_MODE
1542 | MENU_OP_PENDING_MODE;
1545 if (noremap != NULL)
1546 *noremap = (*cmd == 'n' ? REMAP_NONE : REMAP_YES);
1547 if (unmenu != NULL)
1548 *unmenu = (*cmd == 'u');
1549 return modes;
1553 * Modify a menu name starting with "PopUp" to include the mode character.
1554 * Returns the name in allocated memory (NULL for failure).
1556 static char_u *
1557 popup_mode_name(name, idx)
1558 char_u *name;
1559 int idx;
1561 char_u *p;
1562 int len = (int)STRLEN(name);
1564 p = vim_strnsave(name, len + 1);
1565 if (p != NULL)
1567 mch_memmove(p + 6, p + 5, (size_t)(len - 4));
1568 p[5] = menu_mode_chars[idx];
1570 return p;
1573 #if defined(FEAT_GUI) || defined(PROTO)
1575 * Return the index into the menu->strings or menu->noremap arrays for the
1576 * current state. Returns MENU_INDEX_INVALID if there is no mapping for the
1577 * given menu in the current mode.
1580 get_menu_index(menu, state)
1581 vimmenu_T *menu;
1582 int state;
1584 int idx;
1586 if ((state & INSERT))
1587 idx = MENU_INDEX_INSERT;
1588 else if (state & CMDLINE)
1589 idx = MENU_INDEX_CMDLINE;
1590 #ifdef FEAT_VISUAL
1591 else if (VIsual_active)
1593 if (VIsual_select)
1594 idx = MENU_INDEX_SELECT;
1595 else
1596 idx = MENU_INDEX_VISUAL;
1598 #endif
1599 else if (state == HITRETURN || state == ASKMORE)
1600 idx = MENU_INDEX_CMDLINE;
1601 else if (finish_op)
1602 idx = MENU_INDEX_OP_PENDING;
1603 else if ((state & NORMAL))
1604 idx = MENU_INDEX_NORMAL;
1605 else
1606 idx = MENU_INDEX_INVALID;
1608 if (idx != MENU_INDEX_INVALID && menu->strings[idx] == NULL)
1609 idx = MENU_INDEX_INVALID;
1610 return idx;
1612 #endif
1615 * Duplicate the menu item text and then process to see if a mnemonic key
1616 * and/or accelerator text has been identified.
1617 * Returns a pointer to allocated memory, or NULL for failure.
1618 * If mnemonic != NULL, *mnemonic is set to the character after the first '&'.
1619 * If actext != NULL, *actext is set to the text after the first TAB.
1621 static char_u *
1622 menu_text(str, mnemonic, actext)
1623 char_u *str;
1624 int *mnemonic;
1625 char_u **actext;
1627 char_u *p;
1628 char_u *text;
1630 /* Locate accelerator text, after the first TAB */
1631 p = vim_strchr(str, TAB);
1632 if (p != NULL)
1634 if (actext != NULL)
1635 *actext = vim_strsave(p + 1);
1636 text = vim_strnsave(str, (int)(p - str));
1638 else
1639 text = vim_strsave(str);
1641 /* Find mnemonic characters "&a" and reduce "&&" to "&". */
1642 for (p = text; p != NULL; )
1644 p = vim_strchr(p, '&');
1645 if (p != NULL)
1647 if (p[1] == NUL) /* trailing "&" */
1648 break;
1649 if (mnemonic != NULL && p[1] != '&')
1650 #if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED)
1651 *mnemonic = p[1];
1652 #else
1655 * Well there is a bug in the Motif libraries on OS390 Unix.
1656 * The mnemonic keys needs to be converted to ASCII values
1657 * first.
1658 * This behavior has been seen in 2.8 and 2.9.
1660 char c = p[1];
1661 __etoa_l(&c, 1);
1662 *mnemonic = c;
1664 #endif
1665 STRMOVE(p, p + 1);
1666 p = p + 1;
1669 return text;
1673 * Return TRUE if "name" can be a menu in the MenuBar.
1676 menu_is_menubar(name)
1677 char_u *name;
1679 return (!menu_is_popup(name)
1680 && !menu_is_toolbar(name)
1681 && *name != MNU_HIDDEN_CHAR);
1685 * Return TRUE if "name" is a popup menu name.
1688 menu_is_popup(name)
1689 char_u *name;
1691 return (STRNCMP(name, "PopUp", 5) == 0);
1694 #if (defined(FEAT_GUI_MOTIF) && (XmVersion <= 1002)) || defined(PROTO)
1696 * Return TRUE if "name" is part of a popup menu.
1699 menu_is_child_of_popup(menu)
1700 vimmenu_T *menu;
1702 while (menu->parent != NULL)
1703 menu = menu->parent;
1704 return menu_is_popup(menu->name);
1706 #endif
1709 * Return TRUE if "name" is a toolbar menu name.
1712 menu_is_toolbar(name)
1713 char_u *name;
1715 return (STRNCMP(name, "ToolBar", 7) == 0);
1719 * Return TRUE if the name is a menu separator identifier: Starts and ends
1720 * with '-'
1723 menu_is_separator(name)
1724 char_u *name;
1726 return (name[0] == '-' && name[STRLEN(name) - 1] == '-');
1730 * Return TRUE if the menu is hidden: Starts with ']'
1732 static int
1733 menu_is_hidden(name)
1734 char_u *name;
1736 return (name[0] == ']') || (menu_is_popup(name) && name[5] != NUL);
1739 #if defined(FEAT_CMDL_COMPL) \
1740 || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
1742 * Return TRUE if the menu is the tearoff menu.
1744 static int
1745 menu_is_tearoff(name)
1746 char_u *name UNUSED;
1748 #ifdef FEAT_GUI
1749 return (STRCMP(name, TEAR_STRING) == 0);
1750 #else
1751 return FALSE;
1752 #endif
1754 #endif
1756 #ifdef FEAT_GUI
1758 static int
1759 get_menu_mode()
1761 #ifdef FEAT_VISUAL
1762 if (VIsual_active)
1764 if (VIsual_select)
1765 return MENU_INDEX_SELECT;
1766 return MENU_INDEX_VISUAL;
1768 #endif
1769 if (State & INSERT)
1770 return MENU_INDEX_INSERT;
1771 if ((State & CMDLINE) || State == ASKMORE || State == HITRETURN)
1772 return MENU_INDEX_CMDLINE;
1773 if (finish_op)
1774 return MENU_INDEX_OP_PENDING;
1775 if (State & NORMAL)
1776 return MENU_INDEX_NORMAL;
1777 if (State & LANGMAP) /* must be a "r" command, like Insert mode */
1778 return MENU_INDEX_INSERT;
1779 return MENU_INDEX_INVALID;
1783 * Check that a pointer appears in the menu tree. Used to protect from using
1784 * a menu that was deleted after it was selected but before the event was
1785 * handled.
1786 * Return OK or FAIL. Used recursively.
1789 check_menu_pointer(root, menu_to_check)
1790 vimmenu_T *root;
1791 vimmenu_T *menu_to_check;
1793 vimmenu_T *p;
1795 for (p = root; p != NULL; p = p->next)
1796 if (p == menu_to_check
1797 || (p->children != NULL
1798 && check_menu_pointer(p->children, menu_to_check) == OK))
1799 return OK;
1800 return FAIL;
1804 * After we have started the GUI, then we can create any menus that have been
1805 * defined. This is done once here. add_menu_path() may have already been
1806 * called to define these menus, and may be called again. This function calls
1807 * itself recursively. Should be called at the top level with:
1808 * gui_create_initial_menus(root_menu, NULL);
1810 void
1811 gui_create_initial_menus(menu)
1812 vimmenu_T *menu;
1814 int idx = 0;
1816 while (menu != NULL)
1818 /* Don't add a menu when only a tip was defined. */
1819 if (menu->modes & MENU_ALL_MODES)
1821 if (menu->children != NULL)
1823 gui_mch_add_menu(menu, idx);
1824 gui_create_initial_menus(menu->children);
1826 else
1827 gui_mch_add_menu_item(menu, idx);
1829 menu = menu->next;
1830 ++idx;
1835 * Used recursively by gui_update_menus (see below)
1837 static void
1838 gui_update_menus_recurse(menu, mode)
1839 vimmenu_T *menu;
1840 int mode;
1842 int grey;
1844 while (menu)
1846 if ((menu->modes & menu->enabled & mode)
1847 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
1848 || menu_is_tearoff(menu->dname)
1849 #endif
1851 grey = FALSE;
1852 else
1853 grey = TRUE;
1854 #ifdef FEAT_GUI_ATHENA
1855 /* Hiding menus doesn't work for Athena, it can cause a crash. */
1856 gui_mch_menu_grey(menu, grey);
1857 #else
1858 /* Never hide a toplevel menu, it may make the menubar resize or
1859 * disappear. Same problem for ToolBar items. */
1860 if (vim_strchr(p_go, GO_GREY) != NULL || menu->parent == NULL
1861 # ifdef FEAT_TOOLBAR
1862 || menu_is_toolbar(menu->parent->name)
1863 # endif
1865 gui_mch_menu_grey(menu, grey);
1866 else
1867 gui_mch_menu_hidden(menu, grey);
1868 #endif
1869 gui_update_menus_recurse(menu->children, mode);
1870 menu = menu->next;
1875 * Make sure only the valid menu items appear for this mode. If
1876 * force_menu_update is not TRUE, then we only do this if the mode has changed
1877 * since last time. If "modes" is not 0, then we use these modes instead.
1879 void
1880 gui_update_menus(modes)
1881 int modes;
1883 static int prev_mode = -1;
1884 int mode = 0;
1886 if (modes != 0x0)
1887 mode = modes;
1888 else
1890 mode = get_menu_mode();
1891 if (mode == MENU_INDEX_INVALID)
1892 mode = 0;
1893 else
1894 mode = (1 << mode);
1897 if (force_menu_update || mode != prev_mode)
1899 gui_update_menus_recurse(root_menu, mode);
1900 gui_mch_draw_menubar();
1901 prev_mode = mode;
1902 force_menu_update = FALSE;
1903 #ifdef FEAT_GUI_W32
1904 /* This can leave a tearoff as active window - make sure we
1905 * have the focus <negri>*/
1906 gui_mch_activate_window();
1907 #endif
1911 #if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MOTIF) \
1912 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) || defined(PROTO)
1914 * Check if a key is used as a mnemonic for a toplevel menu.
1915 * Case of the key is ignored.
1918 gui_is_menu_shortcut(key)
1919 int key;
1921 vimmenu_T *menu;
1923 if (key < 256)
1924 key = TOLOWER_LOC(key);
1925 for (menu = root_menu; menu != NULL; menu = menu->next)
1926 if (menu->mnemonic == key
1927 || (menu->mnemonic < 256 && TOLOWER_LOC(menu->mnemonic) == key))
1928 return TRUE;
1929 return FALSE;
1931 #endif
1934 * Display the Special "PopUp" menu as a pop-up at the current mouse
1935 * position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode,
1936 * etc.
1938 void
1939 gui_show_popupmenu()
1941 vimmenu_T *menu;
1942 int mode;
1944 mode = get_menu_mode();
1945 if (mode == MENU_INDEX_INVALID)
1946 return;
1947 mode = menu_mode_chars[mode];
1949 #ifdef FEAT_AUTOCMD
1951 char_u ename[2];
1953 ename[0] = mode;
1954 ename[1] = NUL;
1955 apply_autocmds(EVENT_MENUPOPUP, ename, NULL, FALSE, curbuf);
1957 #endif
1959 for (menu = root_menu; menu != NULL; menu = menu->next)
1960 if (STRNCMP("PopUp", menu->name, 5) == 0 && menu->name[5] == mode)
1961 break;
1963 /* Only show a popup when it is defined and has entries */
1964 if (menu != NULL && menu->children != NULL)
1965 gui_mch_show_popupmenu(menu);
1967 #endif /* FEAT_GUI */
1969 #if (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)) || defined(PROTO)
1972 * Deal with tearoff items that are added like a menu item.
1973 * Currently only for Win32 GUI. Others may follow later.
1976 void
1977 gui_mch_toggle_tearoffs(int enable)
1979 int pri_tab[MENUDEPTH + 1];
1980 int i;
1982 if (enable)
1984 for (i = 0; i < MENUDEPTH; ++i)
1985 pri_tab[i] = 500;
1986 pri_tab[MENUDEPTH] = -1;
1987 gui_create_tearoffs_recurse(root_menu, (char_u *)"", pri_tab, 0);
1989 else
1990 gui_destroy_tearoffs_recurse(root_menu);
1991 s_tearoffs = enable;
1995 * Recursively add tearoff items
1997 static void
1998 gui_create_tearoffs_recurse(menu, pname, pri_tab, pri_idx)
1999 vimmenu_T *menu;
2000 const char_u *pname;
2001 int *pri_tab;
2002 int pri_idx;
2004 char_u *newpname = NULL;
2005 int len;
2006 char_u *s;
2007 char_u *d;
2009 if (pri_tab[pri_idx + 1] != -1)
2010 ++pri_idx;
2011 while (menu != NULL)
2013 if (menu->children != NULL && menu_is_menubar(menu->name))
2015 /* Add the menu name to the menu path. Insert a backslash before
2016 * dots (it's used to separate menu names). */
2017 len = (int)STRLEN(pname) + (int)STRLEN(menu->name);
2018 for (s = menu->name; *s; ++s)
2019 if (*s == '.' || *s == '\\')
2020 ++len;
2021 newpname = alloc(len + TEAR_LEN + 2);
2022 if (newpname != NULL)
2024 STRCPY(newpname, pname);
2025 d = newpname + STRLEN(newpname);
2026 for (s = menu->name; *s; ++s)
2028 if (*s == '.' || *s == '\\')
2029 *d++ = '\\';
2030 *d++ = *s;
2032 *d = NUL;
2034 /* check if tearoff already exists */
2035 if (STRCMP(menu->children->name, TEAR_STRING) != 0)
2037 gui_add_tearoff(newpname, pri_tab, pri_idx - 1);
2038 *d = NUL; /* remove TEAR_STRING */
2041 STRCAT(newpname, ".");
2042 gui_create_tearoffs_recurse(menu->children, newpname,
2043 pri_tab, pri_idx);
2044 vim_free(newpname);
2047 menu = menu->next;
2052 * Add tear-off menu item for a submenu.
2053 * "tearpath" is the menu path, and must have room to add TEAR_STRING.
2055 static void
2056 gui_add_tearoff(tearpath, pri_tab, pri_idx)
2057 char_u *tearpath;
2058 int *pri_tab;
2059 int pri_idx;
2061 char_u *tbuf;
2062 int t;
2063 vimmenu_T menuarg;
2065 tbuf = alloc(5 + (unsigned int)STRLEN(tearpath));
2066 if (tbuf != NULL)
2068 tbuf[0] = K_SPECIAL;
2069 tbuf[1] = K_SECOND(K_TEAROFF);
2070 tbuf[2] = K_THIRD(K_TEAROFF);
2071 STRCPY(tbuf + 3, tearpath);
2072 STRCAT(tbuf + 3, "\r");
2074 STRCAT(tearpath, ".");
2075 STRCAT(tearpath, TEAR_STRING);
2077 /* Priority of tear-off is always 1 */
2078 t = pri_tab[pri_idx + 1];
2079 pri_tab[pri_idx + 1] = 1;
2081 #ifdef FEAT_TOOLBAR
2082 menuarg.iconfile = NULL;
2083 menuarg.iconidx = -1;
2084 menuarg.icon_builtin = FALSE;
2085 #endif
2086 menuarg.noremap[0] = REMAP_NONE;
2087 menuarg.silent[0] = TRUE;
2089 menuarg.modes = MENU_ALL_MODES;
2090 add_menu_path(tearpath, &menuarg, pri_tab, tbuf, FALSE);
2092 menuarg.modes = MENU_TIP_MODE;
2093 add_menu_path(tearpath, &menuarg, pri_tab,
2094 (char_u *)_("Tear off this menu"), FALSE);
2096 pri_tab[pri_idx + 1] = t;
2097 vim_free(tbuf);
2102 * Recursively destroy tearoff items
2104 static void
2105 gui_destroy_tearoffs_recurse(menu)
2106 vimmenu_T *menu;
2108 while (menu)
2110 if (menu->children)
2112 /* check if tearoff exists */
2113 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
2115 /* Disconnect the item and free the memory */
2116 free_menu(&menu->children);
2118 if (menu->children != NULL) /* if not the last one */
2119 gui_destroy_tearoffs_recurse(menu->children);
2121 menu = menu->next;
2125 #endif /* FEAT_GUI_W32 && FEAT_TEAROFF */
2128 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2129 * execute it.
2131 void
2132 ex_emenu(eap)
2133 exarg_T *eap;
2135 vimmenu_T *menu;
2136 char_u *name;
2137 char_u *saved_name;
2138 char_u *p;
2139 int idx;
2140 char_u *mode;
2141 #ifdef FEAT_GUI_MACVIM
2142 char_u *old_arg;
2143 #endif
2145 saved_name = vim_strsave(eap->arg);
2146 if (saved_name == NULL)
2147 return;
2149 menu = root_menu;
2150 name = saved_name;
2151 while (*name)
2153 /* Find in the menu hierarchy */
2154 p = menu_name_skip(name);
2156 while (menu != NULL)
2158 if (menu_name_equal(name, menu))
2160 if (*p == NUL && menu->children != NULL)
2162 EMSG(_("E333: Menu path must lead to a menu item"));
2163 menu = NULL;
2165 else if (*p != NUL && menu->children == NULL)
2167 EMSG(_(e_notsubmenu));
2168 menu = NULL;
2170 break;
2172 menu = menu->next;
2174 if (menu == NULL || *p == NUL)
2175 break;
2176 menu = menu->children;
2177 name = p;
2179 vim_free(saved_name);
2180 if (menu == NULL)
2182 EMSG2(_("E334: Menu not found: %s"), eap->arg);
2183 return;
2186 /* Found the menu, so execute.
2187 * Use the Insert mode entry when returning to Insert mode. */
2188 if (restart_edit
2189 #ifdef FEAT_EVAL
2190 && !current_SID
2191 #endif
2194 mode = (char_u *)"Insert";
2195 idx = MENU_INDEX_INSERT;
2197 else if (eap->addr_count)
2199 pos_T tpos;
2201 mode = (char_u *)"Visual";
2202 idx = MENU_INDEX_VISUAL;
2204 /* GEDDES: This is not perfect - but it is a
2205 * quick way of detecting whether we are doing this from a
2206 * selection - see if the range matches up with the visual
2207 * select start and end. */
2208 if ((curbuf->b_visual.vi_start.lnum == eap->line1)
2209 && (curbuf->b_visual.vi_end.lnum) == eap->line2)
2211 /* Set it up for visual mode - equivalent to gv. */
2212 VIsual_mode = curbuf->b_visual.vi_mode;
2213 tpos = curbuf->b_visual.vi_end;
2214 curwin->w_cursor = curbuf->b_visual.vi_start;
2215 curwin->w_curswant = curbuf->b_visual.vi_curswant;
2217 else
2219 /* Set it up for line-wise visual mode */
2220 VIsual_mode = 'V';
2221 curwin->w_cursor.lnum = eap->line1;
2222 curwin->w_cursor.col = 1;
2223 tpos.lnum = eap->line2;
2224 tpos.col = MAXCOL;
2225 #ifdef FEAT_VIRTUALEDIT
2226 tpos.coladd = 0;
2227 #endif
2230 /* Activate visual mode */
2231 VIsual_active = TRUE;
2232 VIsual_reselect = TRUE;
2233 check_cursor();
2234 VIsual = curwin->w_cursor;
2235 curwin->w_cursor = tpos;
2237 check_cursor();
2239 /* Adjust the cursor to make sure it is in the correct pos
2240 * for exclusive mode */
2241 if (*p_sel == 'e' && gchar_cursor() != NUL)
2242 ++curwin->w_cursor.col;
2244 else
2246 mode = (char_u *)"Normal";
2247 idx = MENU_INDEX_NORMAL;
2250 if (idx != MENU_INDEX_INVALID && menu->strings[idx] != NULL &&
2251 menu->strings[idx][0] != NUL)
2254 /* When executing a script or function execute the commands right now.
2255 * Otherwise put them in the typeahead buffer. */
2256 #ifdef FEAT_EVAL
2257 if (current_SID != 0)
2258 exec_normal_cmd(menu->strings[idx], menu->noremap[idx],
2259 menu->silent[idx]);
2260 else
2261 #endif
2262 ins_typebuf(menu->strings[idx], menu->noremap[idx], 0,
2263 TRUE, menu->silent[idx]);
2265 #ifdef FEAT_GUI_MACVIM
2266 else if (menu->mac_action != NULL && menu->strings[idx][0] == NUL)
2268 /* This allows us to bind a menu to an action without mapping to
2269 * anything so that pressing the menu's key equivalent and typing
2270 * ":emenu ..." does the same thing. (HACK: We count on the fact that
2271 * ex_macaction() only looks at eap->arg.) */
2272 old_arg = eap->arg;
2273 eap->arg = menu->mac_action;
2274 ex_macaction(eap);
2275 eap->arg = old_arg;
2277 #endif /* FEAT_GUI_MACVIM */
2278 else
2279 EMSG2(_("E335: Menu not defined for %s mode"), mode);
2282 #if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MACVIM)\
2283 || (defined(FEAT_GUI_GTK) && defined(FEAT_MENU)) \
2284 || defined(FEAT_BEVAL_TIP) || defined(PROTO)
2286 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy.
2288 vimmenu_T *
2289 gui_find_menu(path_name)
2290 char_u *path_name;
2292 vimmenu_T *menu = NULL;
2293 char_u *name;
2294 char_u *saved_name;
2295 char_u *p;
2297 menu = root_menu;
2299 saved_name = vim_strsave(path_name);
2300 if (saved_name == NULL)
2301 return NULL;
2303 name = saved_name;
2304 while (*name)
2306 /* find the end of one dot-separated name and put a NUL at the dot */
2307 p = menu_name_skip(name);
2309 while (menu != NULL)
2311 if (STRCMP(name, menu->name) == 0 || STRCMP(name, menu->dname) == 0)
2313 if (menu->children == NULL)
2315 /* found a menu item instead of a sub-menu */
2316 if (*p == NUL)
2317 EMSG(_("E336: Menu path must lead to a sub-menu"));
2318 else
2319 EMSG(_(e_notsubmenu));
2320 menu = NULL;
2321 goto theend;
2323 if (*p == NUL) /* found a full match */
2324 goto theend;
2325 break;
2327 menu = menu->next;
2329 if (menu == NULL) /* didn't find it */
2330 break;
2332 /* Found a match, search the sub-menu. */
2333 menu = menu->children;
2334 name = p;
2337 if (menu == NULL)
2338 EMSG(_("E337: Menu not found - check menu names"));
2339 theend:
2340 vim_free(saved_name);
2341 return menu;
2343 #endif
2345 #ifdef FEAT_MULTI_LANG
2347 * Translation of menu names. Just a simple lookup table.
2350 typedef struct
2352 char_u *from; /* English name */
2353 char_u *from_noamp; /* same, without '&' */
2354 char_u *to; /* translated name */
2355 } menutrans_T;
2357 static garray_T menutrans_ga = {0, 0, 0, 0, NULL};
2358 #endif
2361 * ":menutrans".
2362 * This function is also defined without the +multi_lang feature, in which
2363 * case the commands are ignored.
2365 void
2366 ex_menutranslate(eap)
2367 exarg_T *eap UNUSED;
2369 #ifdef FEAT_MULTI_LANG
2370 char_u *arg = eap->arg;
2371 menutrans_T *tp;
2372 int i;
2373 char_u *from, *from_noamp, *to;
2375 if (menutrans_ga.ga_itemsize == 0)
2376 ga_init2(&menutrans_ga, (int)sizeof(menutrans_T), 5);
2379 * ":menutrans clear": clear all translations.
2381 if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5)))
2383 tp = (menutrans_T *)menutrans_ga.ga_data;
2384 for (i = 0; i < menutrans_ga.ga_len; ++i)
2386 vim_free(tp[i].from);
2387 vim_free(tp[i].from_noamp);
2388 vim_free(tp[i].to);
2390 ga_clear(&menutrans_ga);
2391 # ifdef FEAT_EVAL
2392 /* Delete all "menutrans_" global variables. */
2393 del_menutrans_vars();
2394 # endif
2396 else
2398 /* ":menutrans from to": add translation */
2399 from = arg;
2400 arg = menu_skip_part(arg);
2401 to = skipwhite(arg);
2402 *arg = NUL;
2403 arg = menu_skip_part(to);
2404 if (arg == to)
2405 EMSG(_(e_invarg));
2406 else
2408 if (ga_grow(&menutrans_ga, 1) == OK)
2410 tp = (menutrans_T *)menutrans_ga.ga_data;
2411 from = vim_strsave(from);
2412 if (from != NULL)
2414 from_noamp = menu_text(from, NULL, NULL);
2415 to = vim_strnsave(to, (int)(arg - to));
2416 if (from_noamp != NULL && to != NULL)
2418 tp[menutrans_ga.ga_len].from = from;
2419 tp[menutrans_ga.ga_len].from_noamp = from_noamp;
2420 tp[menutrans_ga.ga_len].to = to;
2421 ++menutrans_ga.ga_len;
2423 else
2425 vim_free(from);
2426 vim_free(from_noamp);
2427 vim_free(to);
2433 #endif
2436 #if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
2438 * Find the character just after one part of a menu name.
2440 static char_u *
2441 menu_skip_part(p)
2442 char_u *p;
2444 while (*p != NUL && *p != '.' && !vim_iswhite(*p))
2446 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
2447 ++p;
2448 ++p;
2450 return p;
2452 #endif
2454 #ifdef FEAT_MULTI_LANG
2456 * Lookup part of a menu name in the translations.
2457 * Return a pointer to the translation or NULL if not found.
2459 static char_u *
2460 menutrans_lookup(name, len)
2461 char_u *name;
2462 int len;
2464 menutrans_T *tp = (menutrans_T *)menutrans_ga.ga_data;
2465 int i;
2466 char_u *dname;
2468 for (i = 0; i < menutrans_ga.ga_len; ++i)
2469 if (STRNCMP(name, tp[i].from, len) == 0 && tp[i].from[len] == NUL)
2470 return tp[i].to;
2472 /* Now try again while ignoring '&' characters. */
2473 i = name[len];
2474 name[len] = NUL;
2475 dname = menu_text(name, NULL, NULL);
2476 name[len] = i;
2477 if (dname != NULL)
2479 for (i = 0; i < menutrans_ga.ga_len; ++i)
2480 if (STRCMP(dname, tp[i].from_noamp) == 0)
2482 vim_free(dname);
2483 return tp[i].to;
2485 vim_free(dname);
2488 return NULL;
2490 #endif /* FEAT_MULTI_LANG */
2493 #ifdef FEAT_GUI_MACVIM
2494 vimmenu_T *
2495 menu_for_path(char_u *menu_path)
2497 vimmenu_T *menu;
2498 char_u *name;
2499 char_u *saved_name;
2500 char_u *p;
2502 saved_name = vim_strsave(menu_path);
2503 if (saved_name == NULL)
2504 return NULL;
2506 menu = root_menu;
2507 name = saved_name;
2508 while (*name)
2510 /* Find in the menu hierarchy */
2511 p = menu_name_skip(name);
2513 while (menu != NULL)
2515 if (menu_name_equal(name, menu))
2517 if (*p == NUL && menu->children != NULL)
2519 EMSG(_("E333: Menu path must lead to a menu item"));
2520 menu = NULL;
2522 else if (*p != NUL && menu->children == NULL)
2524 EMSG(_(e_notsubmenu));
2525 menu = NULL;
2527 break;
2529 menu = menu->next;
2531 if (menu == NULL || *p == NUL)
2532 break;
2533 menu = menu->children;
2534 name = p;
2537 vim_free(saved_name);
2539 if (menu == NULL)
2541 EMSG2(_("E334: Menu not found: %s"), menu_path);
2542 return NULL;
2545 return menu;
2549 * Handle the ":macmenu" command.
2551 void
2552 ex_macmenu(eap)
2553 exarg_T *eap;
2555 vimmenu_T *menu = NULL;
2556 char_u *arg;
2557 char_u *menu_path;
2558 char_u *p;
2559 char_u *keys;
2560 int len;
2561 #ifdef FEAT_MULTI_LANG
2562 char_u *tofree = NULL;
2563 char_u *new_cmd;
2564 #endif
2565 char_u *linep;
2566 char_u *key_start;
2567 char_u *key = NULL;
2568 char_u *arg_start = NULL;
2569 int error = FALSE;
2570 char_u *action = NULL;
2571 int mac_key = 0;
2572 int mac_mods = 0;
2573 int mac_alternate = 0;
2574 char_u *last_dash;
2575 int bit;
2576 int set_action = FALSE;
2577 int set_key = FALSE;
2578 int set_alt = FALSE;
2580 arg = eap->arg;
2582 #ifdef FEAT_MULTI_LANG
2584 * Translate menu names as specified with ":menutrans" commands.
2586 menu_path = arg;
2587 while (*menu_path)
2589 /* find the end of one part and check if it should be translated */
2590 p = menu_skip_part(menu_path);
2591 keys = menutrans_lookup(menu_path, (int)(p - menu_path));
2592 if (keys != NULL)
2594 /* found a match: replace with the translated part */
2595 len = (int)STRLEN(keys);
2596 new_cmd = alloc((unsigned)STRLEN(arg) + len + 1);
2597 if (new_cmd == NULL)
2598 break;
2599 mch_memmove(new_cmd, arg, menu_path - arg);
2600 mch_memmove(new_cmd + (menu_path - arg), keys, (size_t)len);
2601 STRCPY(new_cmd + (menu_path - arg) + len, p);
2602 p = new_cmd + (menu_path - arg) + len;
2603 vim_free(tofree);
2604 tofree = new_cmd;
2605 arg = new_cmd;
2607 if (*p != '.')
2608 break;
2609 menu_path = p + 1;
2611 #endif
2614 * Isolate the menu name.
2615 * Skip the menu name, and translate <Tab> into a real TAB.
2617 menu_path = arg;
2618 if (*menu_path == '.')
2620 EMSG2(_(e_invarg2), menu_path);
2621 goto theend;
2624 while (*arg && !vim_iswhite(*arg))
2626 if ((*arg == '\\' || *arg == Ctrl_V) && arg[1] != NUL)
2627 arg++;
2628 else if (STRNICMP(arg, "<TAB>", 5) == 0)
2630 *arg = TAB;
2631 mch_memmove(arg + 1, arg + 5, STRLEN(arg + 4));
2633 arg++;
2635 if (*arg != NUL)
2636 *arg++ = NUL;
2637 arg = skipwhite(arg);
2638 keys = arg;
2640 menu = menu_for_path(menu_path);
2641 if (!menu) goto theend;
2644 * Parse all key=value arguments.
2646 arg = NULL;
2647 linep = keys;
2648 while (!ends_excmd(*linep))
2650 key_start = linep;
2651 if (*linep == '=')
2653 EMSG2(_("E415: unexpected equal sign: %s"), key_start);
2654 error = TRUE;
2655 break;
2659 * Isolate the key ("action", "alt", or "key").
2661 while (*linep && !vim_iswhite(*linep) && *linep != '=')
2662 ++linep;
2663 vim_free(key);
2664 key = vim_strnsave_up(key_start, (int)(linep - key_start));
2665 if (key == NULL)
2667 error = TRUE;
2668 break;
2670 linep = skipwhite(linep);
2673 * Check for the equal sign.
2675 if (*linep != '=')
2677 EMSG2(_("E416: missing equal sign: %s"), key_start);
2678 error = TRUE;
2679 break;
2681 ++linep;
2684 * Isolate the argument.
2686 linep = skipwhite(linep);
2687 arg_start = linep;
2688 linep = skiptowhite(linep);
2690 if (linep == arg_start)
2692 EMSG2(_("E417: missing argument: %s"), key_start);
2693 error = TRUE;
2694 break;
2696 vim_free(arg);
2697 arg = vim_strnsave(arg_start, (int)(linep - arg_start));
2698 if (arg == NULL)
2700 error = TRUE;
2701 break;
2705 * Store the argument.
2707 if (STRCMP(key, "ACTION") == 0)
2709 action = vim_strsave(arg);
2710 if (action == NULL)
2712 error = TRUE;
2713 break;
2716 if (!is_valid_macaction(action))
2718 EMSG2(_("E???: Invalid action: %s"), arg);
2719 error = TRUE;
2720 break;
2723 set_action = TRUE;
2725 else if (STRCMP(key, "ALT") == 0)
2727 len = (int)STRLEN(arg);
2728 if ( (len == 1 && (*arg == '0' || *arg == '1')) ||
2729 (STRICMP("no", arg) == 0 || STRICMP("yes", arg) == 0) )
2731 mac_alternate = (*arg == '1' || STRICMP("yes", arg) == 0);
2732 set_alt = TRUE;
2734 else
2736 EMSG2(_(e_invarg2), arg);
2737 error = TRUE;
2738 break;
2741 else if (STRCMP(key, "KEY") == 0)
2743 if (arg[0] != '<')
2745 EMSG2(_(e_invarg2), arg);
2746 error = TRUE;
2747 break;
2750 if (STRICMP("<nop>", arg) == 0)
2752 /* This is to let the user unset a key equivalent, thereby
2753 * freeing up that key combination to be used for a :map
2754 * command (which would otherwise not be possible since key
2755 * equivalents take precedence over :map). */
2756 mac_key = 0;
2757 mac_mods = 0;
2758 set_key = TRUE;
2759 continue;
2762 /* Find end of modifier list */
2763 last_dash = arg;
2764 for (p = arg + 1; *p == '-' || vim_isIDc(*p); p++)
2766 if (*p == '-')
2768 last_dash = p;
2769 if (p[1] != NUL && p[2] == '>')
2770 ++p; /* anything accepted, like <C-?> */
2772 if (p[0] == 't' && p[1] == '_' && p[2] && p[3])
2773 p += 3; /* skip t_xx, xx may be '-' or '>' */
2776 if (*p == '>') /* found matching '>' */
2778 /* Which modifiers are given? */
2779 mac_mods = 0x0;
2780 for (p = arg + 1; p < last_dash; p++)
2782 if (*p != '-')
2784 bit = name_to_mod_mask(*p);
2785 if (bit == 0x0)
2786 break; /* Illegal modifier name */
2787 mac_mods |= bit;
2792 * Legal modifier name.
2794 if (p >= last_dash)
2797 * Modifier with single letter, or special key name.
2799 if (mac_mods != 0 && last_dash[2] == '>')
2800 mac_key = last_dash[1];
2801 else
2803 mac_key = get_special_key_code(last_dash + 1);
2804 mac_key = handle_x_keys(mac_key);
2809 set_key = (mac_key != 0);
2811 if (mac_key == 0)
2813 EMSG2(_(e_invarg2), arg);
2814 error = TRUE;
2815 break;
2818 else
2820 EMSG2(_(e_invarg2), key_start);
2821 error = TRUE;
2822 break;
2826 * Continue with next argument.
2828 linep = skipwhite(linep);
2831 vim_free(key);
2832 vim_free(arg);
2835 * Store all the keys that were set in the menu item.
2837 if (!error)
2839 if (set_action)
2840 menu->mac_action = action;
2841 if (set_key)
2843 menu->mac_key = mac_key;
2844 menu->mac_mods = mac_mods;
2846 if (set_alt)
2847 menu->mac_alternate = mac_alternate;
2849 else
2851 vim_free(action);
2854 theend:
2855 #ifdef FEAT_MULTI_LANG
2856 vim_free(tofree);
2857 #else
2859 #endif
2863 char_u *
2864 lookup_toolbar_item(idx)
2865 int idx;
2867 if (idx >= 0 && idx < TOOLBAR_NAME_COUNT)
2868 return (char_u*)toolbar_names[idx];
2870 return NULL;
2873 #endif /* FEAT_GUI_MACVIM */
2875 #endif /* FEAT_MENU */