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.
12 * Code for menus. Used for the GUI and 'wildmenu'.
17 #if defined(FEAT_MENU) || defined(PROTO)
19 #define MENUDEPTH 10 /* maximum depth of menus */
22 static int add_menu_path
__ARGS((char_u
*, vimmenu_T
*, int *, char_u
*, int));
24 static int add_menu_path
__ARGS((char_u
*, vimmenu_T
*, int *, char_u
*));
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
));
38 static int get_menu_mode
__ARGS((void));
39 static void gui_update_menus_recurse
__ARGS((vimmenu_T
*, int));
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
;
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
));
54 #if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
55 static char_u
*menu_skip_part
__ARGS((char_u
*p
));
57 #ifdef FEAT_MULTI_LANG
58 static char_u
*menutrans_lookup
__ARGS((char_u
*name
, int len
));
61 static char_u
*menu_translate_tab_and_shift
__ARGS((char_u
*arg_start
));
62 static void menu_unescape_name
__ARGS((char_u
*p
));
64 /* The character for each menu mode */
65 static char_u menu_mode_chars
[] = {'n', 'v', 's', 'o', 'i', 'c', 't'};
67 static char_u e_notsubmenu
[] = N_("E327: Part of menu-item path is not sub-menu");
68 static char_u e_othermode
[] = N_("E328: Menu only exists in another mode");
69 static char_u e_nomenu
[] = N_("E329: No menu \"%s\"");
72 static const char *toolbar_names
[] =
74 /* 0 */ "New", "Open", "Save", "Undo", "Redo",
75 /* 5 */ "Cut", "Copy", "Paste", "Print", "Help",
76 /* 10 */ "Find", "SaveAll", "SaveSesn", "NewSesn", "LoadSesn",
77 /* 15 */ "RunScript", "Replace", "WinClose", "WinMax", "WinMin",
78 /* 20 */ "WinSplit", "Shell", "FindPrev", "FindNext", "FindHelp",
79 /* 25 */ "Make", "TagJump", "RunCtags", "WinVSplit", "WinMaxWidth",
80 /* 30 */ "WinMinWidth", "Exit"
82 # define TOOLBAR_NAME_COUNT (sizeof(toolbar_names) / sizeof(char *))
86 * Do the :menu command and relatives.
90 exarg_T
*eap
; /* Ex command arguments */
103 #if defined(FEAT_GUI) && !defined(FEAT_GUI_GTK)
105 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
106 int old_toolbar_height
;
109 int pri_tab
[MENUDEPTH
+ 1];
110 int enable
= MAYBE
; /* TRUE for "menu enable", FALSE for "menu
117 modes
= get_menu_cmd_modes(eap
->cmd
, eap
->forceit
, &noremap
, &unmenu
);
122 if (STRNCMP(arg
, "<script>", 8) == 0)
124 noremap
= REMAP_SCRIPT
;
125 arg
= skipwhite(arg
+ 8);
128 if (STRNCMP(arg
, "<silent>", 8) == 0)
131 arg
= skipwhite(arg
+ 8);
134 if (STRNCMP(arg
, "<special>", 9) == 0)
137 arg
= skipwhite(arg
+ 9);
144 /* Locate an optional "icon=filename" argument. */
145 if (STRNCMP(arg
, "icon=", 5) == 0)
151 while (*arg
!= NUL
&& *arg
!= ' ')
154 STRMOVE(arg
, arg
+ 1);
160 arg
= skipwhite(arg
);
165 * Fill in the priority table.
167 for (p
= arg
; *p
; ++p
)
168 if (!VIM_ISDIGIT(*p
) && *p
!= '.')
172 for (i
= 0; i
< MENUDEPTH
&& !vim_iswhite(*arg
); ++i
)
174 pri_tab
[i
] = getdigits(&arg
);
180 arg
= skipwhite(arg
);
182 else if (eap
->addr_count
&& eap
->line2
!= 0)
184 pri_tab
[0] = eap
->line2
;
189 while (i
< MENUDEPTH
)
191 pri_tab
[MENUDEPTH
] = -1; /* mark end of the table */
194 * Check for "disable" or "enable" argument.
196 if (STRNCMP(arg
, "enable", 6) == 0 && vim_iswhite(arg
[6]))
199 arg
= skipwhite(arg
+ 6);
201 else if (STRNCMP(arg
, "disable", 7) == 0 && vim_iswhite(arg
[7]))
204 arg
= skipwhite(arg
+ 7);
208 * If there is no argument, display all menus.
212 show_menus(arg
, modes
);
218 * Need to get the toolbar icon index before doing the translation.
220 menuarg
.iconidx
= -1;
221 menuarg
.icon_builtin
= FALSE
;
222 if (menu_is_toolbar(arg
))
224 menu_path
= menu_skip_part(arg
);
225 if (*menu_path
== '.')
227 p
= menu_skip_part(++menu_path
);
228 if (STRNCMP(menu_path
, "BuiltIn", 7) == 0)
230 if (skipdigits(menu_path
+ 7) == p
)
232 menuarg
.iconidx
= atoi((char *)menu_path
+ 7);
233 if (menuarg
.iconidx
>= (int)TOOLBAR_NAME_COUNT
)
234 menuarg
.iconidx
= -1;
236 menuarg
.icon_builtin
= TRUE
;
241 for (i
= 0; i
< (int)TOOLBAR_NAME_COUNT
; ++i
)
242 if (STRNCMP(toolbar_names
[i
], menu_path
, p
- menu_path
)
254 if (*menu_path
== '.')
256 EMSG2(_(e_invarg2
), menu_path
);
260 map_to
= menu_translate_tab_and_shift(arg
);
263 * If there is only a menu name, display menus with that name.
265 if (*map_to
== NUL
&& !unmenu
&& enable
== MAYBE
)
267 show_menus(menu_path
, modes
);
270 else if (*map_to
!= NUL
&& (unmenu
|| enable
!= MAYBE
))
275 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
276 old_menu_height
= gui
.menu_height
;
277 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
278 old_toolbar_height
= gui
.toolbar_height
;
285 * Change sensitivity of the menu.
286 * For the PopUp menu, remove a menu for each mode separately.
287 * Careful: menu_nable_recurse() changes menu_path.
289 if (STRCMP(menu_path
, "*") == 0) /* meaning: do all menus */
290 menu_path
= (char_u
*)"";
292 if (menu_is_popup(menu_path
))
294 for (i
= 0; i
< MENU_INDEX_TIP
; ++i
)
295 if (modes
& (1 << i
))
297 p
= popup_mode_name(menu_path
, i
);
300 menu_nable_recurse(root_menu
, p
, MENU_ALL_MODES
,
306 menu_nable_recurse(root_menu
, menu_path
, modes
, enable
);
313 if (STRCMP(menu_path
, "*") == 0) /* meaning: remove all menus */
314 menu_path
= (char_u
*)"";
317 * For the PopUp menu, remove a menu for each mode separately.
319 if (menu_is_popup(menu_path
))
321 for (i
= 0; i
< MENU_INDEX_TIP
; ++i
)
322 if (modes
& (1 << i
))
324 p
= popup_mode_name(menu_path
, i
);
327 remove_menu(&root_menu
, p
, MENU_ALL_MODES
, TRUE
);
333 /* Careful: remove_menu() changes menu_path */
334 remove_menu(&root_menu
, menu_path
, modes
, FALSE
);
340 * Replace special key codes.
342 if (STRICMP(map_to
, "<nop>") == 0) /* "<Nop>" means nothing */
344 map_to
= (char_u
*)"";
347 else if (modes
& MENU_TIP_MODE
)
348 map_buf
= NULL
; /* Menu tips are plain text. */
350 map_to
= replace_termcodes(map_to
, &map_buf
, FALSE
, TRUE
, special
);
351 menuarg
.modes
= modes
;
353 menuarg
.iconfile
= icon
;
355 menuarg
.noremap
[0] = noremap
;
356 menuarg
.silent
[0] = silent
;
357 add_menu_path(menu_path
, &menuarg
, pri_tab
, map_to
364 * For the PopUp menu, add a menu for each mode separately.
366 if (menu_is_popup(menu_path
))
368 for (i
= 0; i
< MENU_INDEX_TIP
; ++i
)
369 if (modes
& (1 << i
))
371 p
= popup_mode_name(menu_path
, i
);
374 /* Include all modes, to make ":amenu" work */
375 menuarg
.modes
= modes
;
377 menuarg
.iconfile
= NULL
;
378 menuarg
.iconidx
= -1;
379 menuarg
.icon_builtin
= FALSE
;
381 add_menu_path(p
, &menuarg
, pri_tab
, map_to
394 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK))
395 /* If the menubar height changed, resize the window */
397 && (gui
.menu_height
!= old_menu_height
398 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
399 || gui
.toolbar_height
!= old_toolbar_height
402 gui_set_shellsize(FALSE
, FALSE
, RESIZE_VERT
);
410 * Add the menu with the given name to the menu hierarchy
413 add_menu_path(menu_path
, menuarg
, pri_tab
, call_data
419 vimmenu_T
*menuarg
; /* passes modes, iconfile, iconidx,
420 icon_builtin, silent[0], noremap[0] */
424 int addtearoff
; /* may add tearoff item */
428 int modes
= menuarg
->modes
;
430 vimmenu_T
*menu
= NULL
;
432 vimmenu_T
**lower_pri
;
447 #ifdef FEAT_MULTI_LANG
449 char_u
*map_to
= NULL
;
452 /* Make a copy so we can stuff around with it, since it could be const */
453 path_name
= vim_strsave(menu_path
);
454 if (path_name
== NULL
)
461 /* Get name of this element in the menu hierarchy, and the simplified
462 * name (without mnemonic and accelerator text). */
463 next_name
= menu_name_skip(name
);
464 #ifdef FEAT_MULTI_LANG
465 map_to
= menutrans_lookup(name
,STRLEN(name
));
474 dname
= menu_text(name
, NULL
, NULL
);
479 /* Only a mnemonic or accelerator is not valid. */
480 EMSG(_("E792: Empty menu name"));
484 /* See if it's already there */
493 if (menu_name_equal(name
, menu
) || menu_name_equal(dname
, menu
))
495 if (*next_name
== NUL
&& menu
->children
!= NULL
)
498 EMSG(_("E330: Menu path must not lead to a sub-menu"));
501 if (*next_name
!= NUL
&& menu
->children
== NULL
508 EMSG(_(e_notsubmenu
));
515 /* Count menus, to find where this one needs to be inserted.
516 * Ignore menus that are not in the menubar (PopUp and Toolbar) */
517 if (parent
!= NULL
|| menu_is_menubar(menu
->name
))
522 if (menu
->priority
<= pri_tab
[pri_idx
])
535 if (*next_name
== NUL
&& parent
== NULL
)
537 EMSG(_("E331: Must not add menu items directly to menu bar"));
541 if (menu_is_separator(dname
) && *next_name
!= NUL
)
543 EMSG(_("E332: Separator cannot be part of a menu path"));
547 /* Not already there, so lets add it */
548 menu
= (vimmenu_T
*)alloc_clear((unsigned)sizeof(vimmenu_T
));
553 menu
->enabled
= MENU_ALL_MODES
;
554 menu
->name
= vim_strsave(name
);
555 /* separate mnemonic and accelerator text from actual menu name */
556 menu
->dname
= menu_text(name
, &menu
->mnemonic
, &menu
->actext
);
557 #ifdef FEAT_MULTI_LANG
560 menu
->en_name
= vim_strsave(en_name
);
561 menu
->en_dname
= menu_text(en_name
, NULL
, NULL
);
565 menu
->en_name
= NULL
;
566 menu
->en_dname
= NULL
;
569 menu
->priority
= pri_tab
[pri_idx
];
570 menu
->parent
= parent
;
571 #ifdef FEAT_GUI_MOTIF
572 menu
->sensitive
= TRUE
; /* the default */
574 #ifdef FEAT_BEVAL_TIP
577 #ifdef FEAT_GUI_ATHENA
578 menu
->image
= None
; /* X-Windows definition for NULL*/
582 * Add after menu that has lower priority.
584 menu
->next
= *lower_pri
;
590 menu
->iconidx
= menuarg
->iconidx
;
591 menu
->icon_builtin
= menuarg
->icon_builtin
;
592 if (*next_name
== NUL
&& menuarg
->iconfile
!= NULL
)
593 menu
->iconfile
= vim_strsave(menuarg
->iconfile
);
595 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
596 /* the tearoff item must be present in the modes of each item. */
597 if (parent
!= NULL
&& menu_is_tearoff(parent
->children
->dname
))
598 parent
->children
->modes
|= modes
;
603 old_modes
= menu
->modes
;
606 * If this menu option was previously only available in other
607 * modes, then make sure it's available for this one now
608 * Also enable a menu when it's created or changed.
611 /* If adding a tearbar (addtearoff == FALSE) don't update modes */
615 menu
->modes
|= modes
;
616 menu
->enabled
|= modes
;
622 * Add the menu item when it's used in one of the modes, but not when
623 * only a tooltip is defined.
625 if ((old_modes
& MENU_ALL_MODES
) == 0
626 && (menu
->modes
& MENU_ALL_MODES
) != 0)
628 if (gui
.in_use
) /* Otherwise it will be added when GUI starts */
630 if (*next_name
== NUL
)
632 /* Real menu item, not sub-menu */
633 gui_mch_add_menu_item(menu
, new_idx
);
635 /* Want to update menus now even if mode not changed */
636 force_menu_update
= TRUE
;
640 /* Sub-menu (not at end of path yet) */
641 gui_mch_add_menu(menu
, new_idx
);
645 # if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
646 /* When adding a new submenu, may add a tearoff item */
649 && vim_strchr(p_go
, GO_TEAROFF
) != NULL
650 && menu_is_menubar(name
))
655 * The pointers next_name & path_name refer to a string with
656 * \'s and ^V's stripped out. But menu_path is a "raw"
657 * string, so we must correct for special characters.
659 tearpath
= alloc((unsigned int)STRLEN(menu_path
) + TEAR_LEN
+ 2);
660 if (tearpath
!= NULL
)
665 STRCPY(tearpath
, menu_path
);
666 idx
= (int)(next_name
- path_name
- 1);
667 for (s
= tearpath
; *s
&& s
< tearpath
+ idx
; mb_ptr_adv(s
))
669 if ((*s
== '\\' || *s
== Ctrl_V
) && s
[1])
676 gui_add_tearoff(tearpath
, pri_tab
, pri_idx
);
682 #endif /* FEAT_GUI */
684 menup
= &menu
->children
;
689 if (pri_tab
[pri_idx
+ 1] != -1)
695 * Only add system menu items which have not been defined yet.
696 * First check if this was an ":amenu".
698 amenu
= ((modes
& (MENU_NORMAL_MODE
| MENU_INSERT_MODE
)) ==
699 (MENU_NORMAL_MODE
| MENU_INSERT_MODE
));
703 if (menu
!= NULL
&& modes
)
706 menu
->cb
= gui_menu_cb
;
708 p
= (call_data
== NULL
) ? NULL
: vim_strsave(call_data
);
710 /* loop over all modes, may add more than one */
711 for (i
= 0; i
< MENU_MODES
; ++i
)
713 if (modes
& (1 << i
))
715 /* free any old menu */
716 free_menu_string(menu
, i
);
718 /* For "amenu", may insert an extra character.
719 * Don't do this if adding a tearbar (addtearoff == FALSE).
720 * Don't do this for "<Nop>". */
723 if (amenu
&& call_data
!= NULL
&& *call_data
!= NUL
731 case MENU_VISUAL_MODE
:
732 case MENU_SELECT_MODE
:
733 case MENU_OP_PENDING_MODE
:
734 case MENU_CMDLINE_MODE
:
737 case MENU_INSERT_MODE
:
746 menu
->strings
[i
] = alloc((unsigned)(STRLEN(call_data
) + 5 ));
747 if (menu
->strings
[i
] != NULL
)
749 menu
->strings
[i
][0] = c
;
751 STRCPY(menu
->strings
[i
] + 1, call_data
);
754 menu
->strings
[i
][1] = d
;
755 STRCPY(menu
->strings
[i
] + 2, call_data
);
759 int len
= (int)STRLEN(menu
->strings
[i
]);
761 /* Append CTRL-\ CTRL-G to obey 'insertmode'. */
762 menu
->strings
[i
][len
] = Ctrl_BSL
;
763 menu
->strings
[i
][len
+ 1] = Ctrl_G
;
764 menu
->strings
[i
][len
+ 2] = NUL
;
769 menu
->strings
[i
] = p
;
770 menu
->noremap
[i
] = menuarg
->noremap
[0];
771 menu
->silent
[i
] = menuarg
->silent
[0];
774 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
775 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
776 /* Need to update the menu tip. */
777 if (modes
& MENU_TIP_MODE
)
778 gui_mch_menu_set_tip(menu
);
787 /* Delete any empty submenu we added before discovering the error. Repeat
788 * for higher levels. */
789 while (parent
!= NULL
&& parent
->children
== NULL
)
791 if (parent
->parent
== NULL
)
794 menup
= &parent
->parent
->children
;
795 for ( ; *menup
!= NULL
&& *menup
!= parent
; menup
= &((*menup
)->next
))
797 if (*menup
== NULL
) /* safety check */
799 parent
= parent
->parent
;
806 * Set the (sub)menu with the given name to enabled or disabled.
807 * Called recursively.
810 menu_nable_recurse(menu
, name
, modes
, enable
)
819 return OK
; /* Got to bottom of hierarchy */
821 /* Get name of this element in the menu hierarchy */
822 p
= menu_name_skip(name
);
827 if (*name
== NUL
|| *name
== '*' || menu_name_equal(name
, menu
))
831 if (menu
->children
== NULL
)
833 EMSG(_(e_notsubmenu
));
836 if (menu_nable_recurse(menu
->children
, p
, modes
, enable
)
842 menu
->enabled
|= modes
;
844 menu
->enabled
&= ~modes
;
847 * When name is empty, we are doing all menu items for the given
848 * modes, so keep looping, otherwise we are just doing the named
849 * menu item (which has been found) so break here.
851 if (*name
!= NUL
&& *name
!= '*')
856 if (*name
!= NUL
&& *name
!= '*' && menu
== NULL
)
858 EMSG2(_(e_nomenu
), name
);
863 /* Want to update menus now even if mode not changed */
864 force_menu_update
= TRUE
;
871 * Remove the (sub)menu with the given name from the menu hierarchy
872 * Called recursively.
875 remove_menu(menup
, name
, modes
, silent
)
879 int silent
; /* don't give error messages */
886 return OK
; /* Got to bottom of hierarchy */
888 /* Get name of this element in the menu hierarchy */
889 p
= menu_name_skip(name
);
892 while ((menu
= *menup
) != NULL
)
894 if (*name
== NUL
|| menu_name_equal(name
, menu
))
896 if (*p
!= NUL
&& menu
->children
== NULL
)
899 EMSG(_(e_notsubmenu
));
902 if ((menu
->modes
& modes
) != 0x0)
904 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
906 * If we are removing all entries for this menu,MENU_ALL_MODES,
907 * Then kill any tearoff before we start
909 if (*p
== NUL
&& modes
== MENU_ALL_MODES
)
911 if (IsWindow(menu
->tearoff_handle
))
912 DestroyWindow(menu
->tearoff_handle
);
915 if (remove_menu(&menu
->children
, p
, modes
, silent
) == FAIL
)
918 else if (*name
!= NUL
)
921 EMSG(_(e_othermode
));
926 * When name is empty, we are removing all menu items for the given
927 * modes, so keep looping, otherwise we are just removing the named
928 * menu item (which has been found) so break here.
933 /* Remove the menu item for the given mode[s]. If the menu item
934 * is no longer valid in ANY mode, delete it */
935 menu
->modes
&= ~modes
;
936 if (modes
& MENU_TIP_MODE
)
937 free_menu_string(menu
, MENU_INDEX_TIP
);
938 if ((menu
->modes
& MENU_ALL_MODES
) == 0)
951 EMSG2(_(e_nomenu
), name
);
956 /* Recalculate modes for menu based on the new updated children */
957 menu
->modes
&= ~modes
;
958 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
959 if ((s_tearoffs
) && (menu
->children
!= NULL
)) /* there's a tear bar.. */
960 child
= menu
->children
->next
; /* don't count tearoff bar */
963 child
= menu
->children
;
964 for ( ; child
!= NULL
; child
= child
->next
)
965 menu
->modes
|= child
->modes
;
966 if (modes
& MENU_TIP_MODE
)
968 free_menu_string(menu
, MENU_INDEX_TIP
);
969 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
970 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
971 /* Need to update the menu tip. */
973 gui_mch_menu_set_tip(menu
);
976 if ((menu
->modes
& MENU_ALL_MODES
) == 0)
978 /* The menu item is no longer valid in ANY mode, so delete it */
979 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
980 if (s_tearoffs
&& menu
->children
!= NULL
) /* there's a tear bar.. */
981 free_menu(&menu
->children
);
992 * Free the given menu structure and remove it from the linked list.
1004 /* Free machine specific menu structures (only when already created) */
1005 /* Also may rebuild a tearoff'ed menu */
1007 gui_mch_destroy_menu(menu
);
1010 /* Don't change *menup until after calling gui_mch_destroy_menu(). The
1011 * MacOS code needs the original structure to properly delete the menu. */
1012 *menup
= menu
->next
;
1013 vim_free(menu
->name
);
1014 vim_free(menu
->dname
);
1015 #ifdef FEAT_MULTI_LANG
1016 vim_free(menu
->en_name
);
1017 vim_free(menu
->en_dname
);
1019 vim_free(menu
->actext
);
1021 vim_free(menu
->iconfile
);
1022 # ifdef FEAT_GUI_MOTIF
1023 vim_free(menu
->xpm_fname
);
1026 for (i
= 0; i
< MENU_MODES
; i
++)
1027 free_menu_string(menu
, i
);
1031 /* Want to update menus now even if mode not changed */
1032 force_menu_update
= TRUE
;
1037 * Free the menu->string with the given index.
1040 free_menu_string(menu
, idx
)
1047 for (i
= 0; i
< MENU_MODES
; i
++)
1048 if (menu
->strings
[i
] == menu
->strings
[idx
])
1051 vim_free(menu
->strings
[idx
]);
1052 menu
->strings
[idx
] = NULL
;
1056 * Show the mapping associated with a menu item or hierarchy in a sub-menu.
1059 show_menus(path_name
, modes
)
1066 vimmenu_T
*parent
= NULL
;
1069 name
= path_name
= vim_strsave(path_name
);
1070 if (path_name
== NULL
)
1073 /* First, find the (sub)menu with the given name */
1076 p
= menu_name_skip(name
);
1077 while (menu
!= NULL
)
1079 if (menu_name_equal(name
, menu
))
1082 if (*p
!= NUL
&& menu
->children
== NULL
)
1084 EMSG(_(e_notsubmenu
));
1085 vim_free(path_name
);
1088 else if ((menu
->modes
& modes
) == 0x0)
1090 EMSG(_(e_othermode
));
1091 vim_free(path_name
);
1100 EMSG2(_(e_nomenu
), name
);
1101 vim_free(path_name
);
1106 menu
= menu
->children
;
1108 vim_free(path_name
);
1110 /* Now we have found the matching menu, and we list the mappings */
1111 /* Highlight title */
1112 MSG_PUTS_TITLE(_("\n--- Menus ---"));
1114 show_menus_recursive(parent
, modes
, 0);
1119 * Recursively show the mappings associated with the menus under the given one
1122 show_menus_recursive(menu
, modes
, depth
)
1130 if (menu
!= NULL
&& (menu
->modes
& modes
) == 0x0)
1136 if (got_int
) /* "q" hit for "--more--" */
1138 for (i
= 0; i
< depth
; i
++)
1142 msg_outnum((long)menu
->priority
);
1145 /* Same highlighting as for directories!? */
1146 msg_outtrans_attr(menu
->name
, hl_attr(HLF_D
));
1149 if (menu
!= NULL
&& menu
->children
== NULL
)
1151 for (bit
= 0; bit
< MENU_MODES
; bit
++)
1152 if ((menu
->modes
& modes
& (1 << bit
)) != 0)
1155 if (got_int
) /* "q" hit for "--more--" */
1157 for (i
= 0; i
< depth
+ 2; i
++)
1159 msg_putchar(menu_mode_chars
[bit
]);
1160 if (menu
->noremap
[bit
] == REMAP_NONE
)
1162 else if (menu
->noremap
[bit
] == REMAP_SCRIPT
)
1166 if (menu
->silent
[bit
])
1170 if ((menu
->modes
& menu
->enabled
& (1 << bit
)) == 0)
1175 if (*menu
->strings
[bit
] == NUL
)
1176 msg_puts_attr((char_u
*)"<Nop>", hl_attr(HLF_8
));
1178 msg_outtrans_special(menu
->strings
[bit
], FALSE
);
1189 menu
= menu
->children
;
1191 /* recursively show all children. Skip PopUp[nvoci]. */
1192 for (; menu
!= NULL
&& !got_int
; menu
= menu
->next
)
1193 if (!menu_is_hidden(menu
->dname
))
1194 show_menus_recursive(menu
, modes
, depth
+ 1);
1198 #ifdef FEAT_CMDL_COMPL
1201 * Used when expanding menu names.
1203 static vimmenu_T
*expand_menu
= NULL
;
1204 static int expand_modes
= 0x0;
1205 static int expand_emenu
; /* TRUE for ":emenu" command */
1208 * Work out what to complete when doing command line completion of menu names.
1211 set_context_in_menu_cmd(xp
, cmd
, arg
, forceit
)
1219 char_u
*path_name
= NULL
;
1225 xp
->xp_context
= EXPAND_UNSUCCESSFUL
;
1228 /* Check for priority numbers, enable and disable */
1229 for (p
= arg
; *p
; ++p
)
1230 if (!VIM_ISDIGIT(*p
) && *p
!= '.')
1233 if (!vim_iswhite(*p
))
1235 if (STRNCMP(arg
, "enable", 6) == 0
1236 && (arg
[6] == NUL
|| vim_iswhite(arg
[6])))
1238 else if (STRNCMP(arg
, "disable", 7) == 0
1239 && (arg
[7] == NUL
|| vim_iswhite(arg
[7])))
1245 while (*p
!= NUL
&& vim_iswhite(*p
))
1248 arg
= after_dot
= p
;
1250 for (; *p
&& !vim_iswhite(*p
); ++p
)
1252 if ((*p
== '\\' || *p
== Ctrl_V
) && p
[1] != NUL
)
1258 /* ":tearoff" and ":popup" only use menus, not entries */
1259 expand_menus
= !((*cmd
== 't' && cmd
[1] == 'e') || *cmd
== 'p');
1260 expand_emenu
= (*cmd
== 'e');
1261 if (expand_menus
&& vim_iswhite(*p
))
1262 return NULL
; /* TODO: check for next command? */
1263 if (*p
== NUL
) /* Complete the menu name */
1266 * With :unmenu, you only want to match menus for the appropriate mode.
1267 * With :menu though you might want to add a menu with the same name as
1268 * one in another mode, so match menus from other modes too.
1270 expand_modes
= get_menu_cmd_modes(cmd
, forceit
, NULL
, &unmenu
);
1272 expand_modes
= MENU_ALL_MODES
;
1275 if (after_dot
!= arg
)
1277 path_name
= alloc((unsigned)(after_dot
- arg
));
1278 if (path_name
== NULL
)
1280 vim_strncpy(path_name
, arg
, after_dot
- arg
- 1);
1283 while (name
!= NULL
&& *name
)
1285 p
= menu_name_skip(name
);
1286 while (menu
!= NULL
)
1288 if (menu_name_equal(name
, menu
))
1291 if ((*p
!= NUL
&& menu
->children
== NULL
)
1292 || ((menu
->modes
& expand_modes
) == 0x0))
1295 * Menu path continues, but we have reached a leaf.
1296 * Or menu exists only in another mode.
1298 vim_free(path_name
);
1307 /* No menu found with the name we were looking for */
1308 vim_free(path_name
);
1312 menu
= menu
->children
;
1314 vim_free(path_name
);
1316 xp
->xp_context
= expand_menus
? EXPAND_MENUNAMES
: EXPAND_MENUS
;
1317 xp
->xp_pattern
= after_dot
;
1320 else /* We're in the mapping part */
1321 xp
->xp_context
= EXPAND_NOTHING
;
1326 * Function given to ExpandGeneric() to obtain the list of (sub)menus (not
1330 get_menu_name(xp
, idx
)
1331 expand_T
*xp UNUSED
;
1334 static vimmenu_T
*menu
= NULL
;
1336 #ifdef FEAT_MULTI_LANG
1337 static int should_advance
= FALSE
;
1340 if (idx
== 0) /* first call: start at first item */
1343 #ifdef FEAT_MULTI_LANG
1344 should_advance
= FALSE
;
1348 /* Skip PopUp[nvoci]. */
1349 while (menu
!= NULL
&& (menu_is_hidden(menu
->dname
)
1350 || menu_is_separator(menu
->dname
)
1351 || menu_is_tearoff(menu
->dname
)
1352 || menu
->children
== NULL
))
1355 if (menu
== NULL
) /* at end of linked list */
1358 if (menu
->modes
& expand_modes
)
1359 #ifdef FEAT_MULTI_LANG
1361 str
= menu
->en_dname
;
1366 #ifdef FEAT_MULTI_LANG
1367 if (menu
->en_dname
== NULL
)
1368 should_advance
= TRUE
;
1374 #ifdef FEAT_MULTI_LANG
1377 /* Advance to next menu entry. */
1380 #ifdef FEAT_MULTI_LANG
1381 should_advance
= !should_advance
;
1388 * Function given to ExpandGeneric() to obtain the list of menus and menu
1392 get_menu_names(xp
, idx
)
1393 expand_T
*xp UNUSED
;
1396 static vimmenu_T
*menu
= NULL
;
1397 static char_u tbuffer
[256]; /*hack*/
1399 #ifdef FEAT_MULTI_LANG
1400 static int should_advance
= FALSE
;
1403 if (idx
== 0) /* first call: start at first item */
1406 #ifdef FEAT_MULTI_LANG
1407 should_advance
= FALSE
;
1411 /* Skip Browse-style entries, popup menus and separators. */
1413 && ( menu_is_hidden(menu
->dname
)
1414 || (expand_emenu
&& menu_is_separator(menu
->dname
))
1415 || menu_is_tearoff(menu
->dname
)
1417 || menu
->dname
[STRLEN(menu
->dname
) - 1] == '.'
1422 if (menu
== NULL
) /* at end of linked list */
1425 if (menu
->modes
& expand_modes
)
1427 if (menu
->children
!= NULL
)
1429 #ifdef FEAT_MULTI_LANG
1431 STRCPY(tbuffer
, menu
->en_dname
);
1435 STRCPY(tbuffer
, menu
->dname
);
1436 #ifdef FEAT_MULTI_LANG
1437 if (menu
->en_dname
== NULL
)
1438 should_advance
= TRUE
;
1441 /* hack on menu separators: use a 'magic' char for the separator
1442 * so that '.' in names gets escaped properly */
1443 STRCAT(tbuffer
, "\001");
1447 #ifdef FEAT_MULTI_LANG
1450 str
= menu
->en_dname
;
1455 #ifdef FEAT_MULTI_LANG
1456 if (menu
->en_dname
== NULL
)
1457 should_advance
= TRUE
;
1465 #ifdef FEAT_MULTI_LANG
1468 /* Advance to next menu entry. */
1471 #ifdef FEAT_MULTI_LANG
1472 should_advance
= !should_advance
;
1477 #endif /* FEAT_CMDL_COMPL */
1480 * Skip over this element of the menu path and return the start of the next
1481 * element. Any \ and ^Vs are removed from the current element.
1482 * "name" may be modified.
1485 menu_name_skip(name
)
1490 for (p
= name
; *p
&& *p
!= '.'; mb_ptr_adv(p
))
1492 if (*p
== '\\' || *p
== Ctrl_V
)
1505 * Return TRUE when "name" matches with menu "menu". The name is compared in
1506 * two ways: raw menu name and menu name without '&'. ignore part after a TAB.
1509 menu_name_equal(name
, menu
)
1513 #ifdef FEAT_MULTI_LANG
1514 if (menu
->en_name
!= NULL
1515 && (menu_namecmp(name
,menu
->en_name
)
1516 || menu_namecmp(name
,menu
->en_dname
)))
1519 return menu_namecmp(name
, menu
->name
) || menu_namecmp(name
, menu
->dname
);
1523 menu_namecmp(name
, mname
)
1529 for (i
= 0; name
[i
] != NUL
&& name
[i
] != TAB
; ++i
)
1530 if (name
[i
] != mname
[i
])
1532 return ((name
[i
] == NUL
|| name
[i
] == TAB
)
1533 && (mname
[i
] == NUL
|| mname
[i
] == TAB
));
1537 * Return the modes specified by the given menu command (eg :menu! returns
1538 * MENU_CMDLINE_MODE | MENU_INSERT_MODE).
1539 * If "noremap" is not NULL, then the flag it points to is set according to
1540 * whether the command is a "nore" command.
1541 * If "unmenu" is not NULL, then the flag it points to is set according to
1542 * whether the command is an "unmenu" command.
1545 get_menu_cmd_modes(cmd
, forceit
, noremap
, unmenu
)
1547 int forceit
; /* Was there a "!" after the command? */
1555 case 'v': /* vmenu, vunmenu, vnoremenu */
1556 modes
= MENU_VISUAL_MODE
| MENU_SELECT_MODE
;
1558 case 'x': /* xmenu, xunmenu, xnoremenu */
1559 modes
= MENU_VISUAL_MODE
;
1561 case 's': /* smenu, sunmenu, snoremenu */
1562 modes
= MENU_SELECT_MODE
;
1564 case 'o': /* omenu */
1565 modes
= MENU_OP_PENDING_MODE
;
1567 case 'i': /* imenu */
1568 modes
= MENU_INSERT_MODE
;
1571 modes
= MENU_TIP_MODE
; /* tmenu */
1573 case 'c': /* cmenu */
1574 modes
= MENU_CMDLINE_MODE
;
1576 case 'a': /* amenu */
1577 modes
= MENU_INSERT_MODE
| MENU_CMDLINE_MODE
| MENU_NORMAL_MODE
1578 | MENU_VISUAL_MODE
| MENU_SELECT_MODE
1579 | MENU_OP_PENDING_MODE
;
1582 if (*cmd
!= 'o') /* nmenu, not noremenu */
1584 modes
= MENU_NORMAL_MODE
;
1590 if (forceit
) /* menu!! */
1591 modes
= MENU_INSERT_MODE
| MENU_CMDLINE_MODE
;
1593 modes
= MENU_NORMAL_MODE
| MENU_VISUAL_MODE
| MENU_SELECT_MODE
1594 | MENU_OP_PENDING_MODE
;
1597 if (noremap
!= NULL
)
1598 *noremap
= (*cmd
== 'n' ? REMAP_NONE
: REMAP_YES
);
1600 *unmenu
= (*cmd
== 'u');
1605 * Modify a menu name starting with "PopUp" to include the mode character.
1606 * Returns the name in allocated memory (NULL for failure).
1609 popup_mode_name(name
, idx
)
1614 int len
= (int)STRLEN(name
);
1616 p
= vim_strnsave(name
, len
+ 1);
1619 mch_memmove(p
+ 6, p
+ 5, (size_t)(len
- 4));
1620 p
[5] = menu_mode_chars
[idx
];
1625 #if defined(FEAT_GUI) || defined(PROTO)
1627 * Return the index into the menu->strings or menu->noremap arrays for the
1628 * current state. Returns MENU_INDEX_INVALID if there is no mapping for the
1629 * given menu in the current mode.
1632 get_menu_index(menu
, state
)
1638 if ((state
& INSERT
))
1639 idx
= MENU_INDEX_INSERT
;
1640 else if (state
& CMDLINE
)
1641 idx
= MENU_INDEX_CMDLINE
;
1643 else if (VIsual_active
)
1646 idx
= MENU_INDEX_SELECT
;
1648 idx
= MENU_INDEX_VISUAL
;
1651 else if (state
== HITRETURN
|| state
== ASKMORE
)
1652 idx
= MENU_INDEX_CMDLINE
;
1654 idx
= MENU_INDEX_OP_PENDING
;
1655 else if ((state
& NORMAL
))
1656 idx
= MENU_INDEX_NORMAL
;
1658 idx
= MENU_INDEX_INVALID
;
1660 if (idx
!= MENU_INDEX_INVALID
&& menu
->strings
[idx
] == NULL
)
1661 idx
= MENU_INDEX_INVALID
;
1667 * Duplicate the menu item text and then process to see if a mnemonic key
1668 * and/or accelerator text has been identified.
1669 * Returns a pointer to allocated memory, or NULL for failure.
1670 * If mnemonic != NULL, *mnemonic is set to the character after the first '&'.
1671 * If actext != NULL, *actext is set to the text after the first TAB.
1674 menu_text(str
, mnemonic
, actext
)
1682 /* Locate accelerator text, after the first TAB */
1683 p
= vim_strchr(str
, TAB
);
1687 *actext
= vim_strsave(p
+ 1);
1688 text
= vim_strnsave(str
, (int)(p
- str
));
1691 text
= vim_strsave(str
);
1693 /* Find mnemonic characters "&a" and reduce "&&" to "&". */
1694 for (p
= text
; p
!= NULL
; )
1696 p
= vim_strchr(p
, '&');
1699 if (p
[1] == NUL
) /* trailing "&" */
1701 if (mnemonic
!= NULL
&& p
[1] != '&')
1702 #if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED)
1707 * Well there is a bug in the Motif libraries on OS390 Unix.
1708 * The mnemonic keys needs to be converted to ASCII values
1710 * This behavior has been seen in 2.8 and 2.9.
1725 * Return TRUE if "name" can be a menu in the MenuBar.
1728 menu_is_menubar(name
)
1731 return (!menu_is_popup(name
)
1732 && !menu_is_toolbar(name
)
1733 && *name
!= MNU_HIDDEN_CHAR
);
1737 * Return TRUE if "name" is a popup menu name.
1743 return (STRNCMP(name
, "PopUp", 5) == 0);
1746 #if (defined(FEAT_GUI_MOTIF) && (XmVersion <= 1002)) || defined(PROTO)
1748 * Return TRUE if "name" is part of a popup menu.
1751 menu_is_child_of_popup(menu
)
1754 while (menu
->parent
!= NULL
)
1755 menu
= menu
->parent
;
1756 return menu_is_popup(menu
->name
);
1761 * Return TRUE if "name" is a toolbar menu name.
1764 menu_is_toolbar(name
)
1767 return (STRNCMP(name
, "ToolBar", 7) == 0);
1771 * Return TRUE if the name is a menu separator identifier: Starts and ends
1775 menu_is_separator(name
)
1778 return (name
[0] == '-' && name
[STRLEN(name
) - 1] == '-');
1782 * Return TRUE if the menu is hidden: Starts with ']'
1785 menu_is_hidden(name
)
1788 return (name
[0] == ']') || (menu_is_popup(name
) && name
[5] != NUL
);
1791 #if defined(FEAT_CMDL_COMPL) \
1792 || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
1794 * Return TRUE if the menu is the tearoff menu.
1797 menu_is_tearoff(name
)
1798 char_u
*name UNUSED
;
1801 return (STRCMP(name
, TEAR_STRING
) == 0);
1817 return MENU_INDEX_SELECT
;
1818 return MENU_INDEX_VISUAL
;
1822 return MENU_INDEX_INSERT
;
1823 if ((State
& CMDLINE
) || State
== ASKMORE
|| State
== HITRETURN
)
1824 return MENU_INDEX_CMDLINE
;
1826 return MENU_INDEX_OP_PENDING
;
1828 return MENU_INDEX_NORMAL
;
1829 if (State
& LANGMAP
) /* must be a "r" command, like Insert mode */
1830 return MENU_INDEX_INSERT
;
1831 return MENU_INDEX_INVALID
;
1835 * Check that a pointer appears in the menu tree. Used to protect from using
1836 * a menu that was deleted after it was selected but before the event was
1838 * Return OK or FAIL. Used recursively.
1841 check_menu_pointer(root
, menu_to_check
)
1843 vimmenu_T
*menu_to_check
;
1847 for (p
= root
; p
!= NULL
; p
= p
->next
)
1848 if (p
== menu_to_check
1849 || (p
->children
!= NULL
1850 && check_menu_pointer(p
->children
, menu_to_check
) == OK
))
1856 * After we have started the GUI, then we can create any menus that have been
1857 * defined. This is done once here. add_menu_path() may have already been
1858 * called to define these menus, and may be called again. This function calls
1859 * itself recursively. Should be called at the top level with:
1860 * gui_create_initial_menus(root_menu, NULL);
1863 gui_create_initial_menus(menu
)
1868 while (menu
!= NULL
)
1870 /* Don't add a menu when only a tip was defined. */
1871 if (menu
->modes
& MENU_ALL_MODES
)
1873 if (menu
->children
!= NULL
)
1875 gui_mch_add_menu(menu
, idx
);
1876 gui_create_initial_menus(menu
->children
);
1879 gui_mch_add_menu_item(menu
, idx
);
1887 * Used recursively by gui_update_menus (see below)
1890 gui_update_menus_recurse(menu
, mode
)
1898 if ((menu
->modes
& menu
->enabled
& mode
)
1899 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
1900 || menu_is_tearoff(menu
->dname
)
1906 #ifdef FEAT_GUI_ATHENA
1907 /* Hiding menus doesn't work for Athena, it can cause a crash. */
1908 gui_mch_menu_grey(menu
, grey
);
1910 /* Never hide a toplevel menu, it may make the menubar resize or
1911 * disappear. Same problem for ToolBar items. */
1912 if (vim_strchr(p_go
, GO_GREY
) != NULL
|| menu
->parent
== NULL
1913 # ifdef FEAT_TOOLBAR
1914 || menu_is_toolbar(menu
->parent
->name
)
1917 gui_mch_menu_grey(menu
, grey
);
1919 gui_mch_menu_hidden(menu
, grey
);
1921 gui_update_menus_recurse(menu
->children
, mode
);
1927 * Make sure only the valid menu items appear for this mode. If
1928 * force_menu_update is not TRUE, then we only do this if the mode has changed
1929 * since last time. If "modes" is not 0, then we use these modes instead.
1932 gui_update_menus(modes
)
1935 static int prev_mode
= -1;
1942 mode
= get_menu_mode();
1943 if (mode
== MENU_INDEX_INVALID
)
1949 if (force_menu_update
|| mode
!= prev_mode
)
1951 gui_update_menus_recurse(root_menu
, mode
);
1952 gui_mch_draw_menubar();
1954 force_menu_update
= FALSE
;
1956 /* This can leave a tearoff as active window - make sure we
1957 * have the focus <negri>*/
1958 gui_mch_activate_window();
1963 #if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MOTIF) \
1964 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) || defined(PROTO)
1966 * Check if a key is used as a mnemonic for a toplevel menu.
1967 * Case of the key is ignored.
1970 gui_is_menu_shortcut(key
)
1976 key
= TOLOWER_LOC(key
);
1977 for (menu
= root_menu
; menu
!= NULL
; menu
= menu
->next
)
1978 if (menu
->mnemonic
== key
1979 || (menu
->mnemonic
< 256 && TOLOWER_LOC(menu
->mnemonic
) == key
))
1986 * Display the Special "PopUp" menu as a pop-up at the current mouse
1987 * position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode,
1991 gui_show_popupmenu()
1996 mode
= get_menu_mode();
1997 if (mode
== MENU_INDEX_INVALID
)
1999 mode
= menu_mode_chars
[mode
];
2007 apply_autocmds(EVENT_MENUPOPUP
, ename
, NULL
, FALSE
, curbuf
);
2011 for (menu
= root_menu
; menu
!= NULL
; menu
= menu
->next
)
2012 if (STRNCMP("PopUp", menu
->name
, 5) == 0 && menu
->name
[5] == mode
)
2015 /* Only show a popup when it is defined and has entries */
2016 if (menu
!= NULL
&& menu
->children
!= NULL
)
2017 gui_mch_show_popupmenu(menu
);
2019 #endif /* FEAT_GUI */
2021 #if (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)) || defined(PROTO)
2024 * Deal with tearoff items that are added like a menu item.
2025 * Currently only for Win32 GUI. Others may follow later.
2029 gui_mch_toggle_tearoffs(int enable
)
2031 int pri_tab
[MENUDEPTH
+ 1];
2036 for (i
= 0; i
< MENUDEPTH
; ++i
)
2038 pri_tab
[MENUDEPTH
] = -1;
2039 gui_create_tearoffs_recurse(root_menu
, (char_u
*)"", pri_tab
, 0);
2042 gui_destroy_tearoffs_recurse(root_menu
);
2043 s_tearoffs
= enable
;
2047 * Recursively add tearoff items
2050 gui_create_tearoffs_recurse(menu
, pname
, pri_tab
, pri_idx
)
2052 const char_u
*pname
;
2056 char_u
*newpname
= NULL
;
2061 if (pri_tab
[pri_idx
+ 1] != -1)
2063 while (menu
!= NULL
)
2065 if (menu
->children
!= NULL
&& menu_is_menubar(menu
->name
))
2067 /* Add the menu name to the menu path. Insert a backslash before
2068 * dots (it's used to separate menu names). */
2069 len
= (int)STRLEN(pname
) + (int)STRLEN(menu
->name
);
2070 for (s
= menu
->name
; *s
; ++s
)
2071 if (*s
== '.' || *s
== '\\')
2073 newpname
= alloc(len
+ TEAR_LEN
+ 2);
2074 if (newpname
!= NULL
)
2076 STRCPY(newpname
, pname
);
2077 d
= newpname
+ STRLEN(newpname
);
2078 for (s
= menu
->name
; *s
; ++s
)
2080 if (*s
== '.' || *s
== '\\')
2086 /* check if tearoff already exists */
2087 if (STRCMP(menu
->children
->name
, TEAR_STRING
) != 0)
2089 gui_add_tearoff(newpname
, pri_tab
, pri_idx
- 1);
2090 *d
= NUL
; /* remove TEAR_STRING */
2093 STRCAT(newpname
, ".");
2094 gui_create_tearoffs_recurse(menu
->children
, newpname
,
2104 * Add tear-off menu item for a submenu.
2105 * "tearpath" is the menu path, and must have room to add TEAR_STRING.
2108 gui_add_tearoff(tearpath
, pri_tab
, pri_idx
)
2117 tbuf
= alloc(5 + (unsigned int)STRLEN(tearpath
));
2120 tbuf
[0] = K_SPECIAL
;
2121 tbuf
[1] = K_SECOND(K_TEAROFF
);
2122 tbuf
[2] = K_THIRD(K_TEAROFF
);
2123 STRCPY(tbuf
+ 3, tearpath
);
2124 STRCAT(tbuf
+ 3, "\r");
2126 STRCAT(tearpath
, ".");
2127 STRCAT(tearpath
, TEAR_STRING
);
2129 /* Priority of tear-off is always 1 */
2130 t
= pri_tab
[pri_idx
+ 1];
2131 pri_tab
[pri_idx
+ 1] = 1;
2134 menuarg
.iconfile
= NULL
;
2135 menuarg
.iconidx
= -1;
2136 menuarg
.icon_builtin
= FALSE
;
2138 menuarg
.noremap
[0] = REMAP_NONE
;
2139 menuarg
.silent
[0] = TRUE
;
2141 menuarg
.modes
= MENU_ALL_MODES
;
2142 add_menu_path(tearpath
, &menuarg
, pri_tab
, tbuf
, FALSE
);
2144 menuarg
.modes
= MENU_TIP_MODE
;
2145 add_menu_path(tearpath
, &menuarg
, pri_tab
,
2146 (char_u
*)_("Tear off this menu"), FALSE
);
2148 pri_tab
[pri_idx
+ 1] = t
;
2154 * Recursively destroy tearoff items
2157 gui_destroy_tearoffs_recurse(menu
)
2164 /* check if tearoff exists */
2165 if (STRCMP(menu
->children
->name
, TEAR_STRING
) == 0)
2167 /* Disconnect the item and free the memory */
2168 free_menu(&menu
->children
);
2170 if (menu
->children
!= NULL
) /* if not the last one */
2171 gui_destroy_tearoffs_recurse(menu
->children
);
2177 #endif /* FEAT_GUI_W32 && FEAT_TEAROFF */
2180 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2194 saved_name
= vim_strsave(eap
->arg
);
2195 if (saved_name
== NULL
)
2202 /* Find in the menu hierarchy */
2203 p
= menu_name_skip(name
);
2205 while (menu
!= NULL
)
2207 if (menu_name_equal(name
, menu
))
2209 if (*p
== NUL
&& menu
->children
!= NULL
)
2211 EMSG(_("E333: Menu path must lead to a menu item"));
2214 else if (*p
!= NUL
&& menu
->children
== NULL
)
2216 EMSG(_(e_notsubmenu
));
2223 if (menu
== NULL
|| *p
== NUL
)
2225 menu
= menu
->children
;
2228 vim_free(saved_name
);
2231 EMSG2(_("E334: Menu not found: %s"), eap
->arg
);
2235 /* Found the menu, so execute.
2236 * Use the Insert mode entry when returning to Insert mode. */
2243 mode
= (char_u
*)"Insert";
2244 idx
= MENU_INDEX_INSERT
;
2246 else if (eap
->addr_count
)
2250 mode
= (char_u
*)"Visual";
2251 idx
= MENU_INDEX_VISUAL
;
2253 /* GEDDES: This is not perfect - but it is a
2254 * quick way of detecting whether we are doing this from a
2255 * selection - see if the range matches up with the visual
2256 * select start and end. */
2257 if ((curbuf
->b_visual
.vi_start
.lnum
== eap
->line1
)
2258 && (curbuf
->b_visual
.vi_end
.lnum
) == eap
->line2
)
2260 /* Set it up for visual mode - equivalent to gv. */
2261 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
2262 tpos
= curbuf
->b_visual
.vi_end
;
2263 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
2264 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
2268 /* Set it up for line-wise visual mode */
2270 curwin
->w_cursor
.lnum
= eap
->line1
;
2271 curwin
->w_cursor
.col
= 1;
2272 tpos
.lnum
= eap
->line2
;
2274 #ifdef FEAT_VIRTUALEDIT
2279 /* Activate visual mode */
2280 VIsual_active
= TRUE
;
2281 VIsual_reselect
= TRUE
;
2283 VIsual
= curwin
->w_cursor
;
2284 curwin
->w_cursor
= tpos
;
2288 /* Adjust the cursor to make sure it is in the correct pos
2289 * for exclusive mode */
2290 if (*p_sel
== 'e' && gchar_cursor() != NUL
)
2291 ++curwin
->w_cursor
.col
;
2295 mode
= (char_u
*)"Normal";
2296 idx
= MENU_INDEX_NORMAL
;
2299 if (idx
!= MENU_INDEX_INVALID
&& menu
->strings
[idx
] != NULL
)
2301 /* When executing a script or function execute the commands right now.
2302 * Otherwise put them in the typeahead buffer. */
2304 if (current_SID
!= 0)
2305 exec_normal_cmd(menu
->strings
[idx
], menu
->noremap
[idx
],
2309 ins_typebuf(menu
->strings
[idx
], menu
->noremap
[idx
], 0,
2310 TRUE
, menu
->silent
[idx
]);
2313 EMSG2(_("E335: Menu not defined for %s mode"), mode
);
2316 #if defined(FEAT_GUI_MSWIN) \
2317 || (defined(FEAT_GUI_GTK) && defined(FEAT_MENU)) \
2318 || defined(FEAT_BEVAL_TIP) || defined(PROTO)
2320 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy.
2323 gui_find_menu(path_name
)
2326 vimmenu_T
*menu
= NULL
;
2333 saved_name
= vim_strsave(path_name
);
2334 if (saved_name
== NULL
)
2340 /* find the end of one dot-separated name and put a NUL at the dot */
2341 p
= menu_name_skip(name
);
2343 while (menu
!= NULL
)
2345 if (STRCMP(name
, menu
->name
) == 0 || STRCMP(name
, menu
->dname
) == 0)
2347 if (menu
->children
== NULL
)
2349 /* found a menu item instead of a sub-menu */
2351 EMSG(_("E336: Menu path must lead to a sub-menu"));
2353 EMSG(_(e_notsubmenu
));
2357 if (*p
== NUL
) /* found a full match */
2363 if (menu
== NULL
) /* didn't find it */
2366 /* Found a match, search the sub-menu. */
2367 menu
= menu
->children
;
2372 EMSG(_("E337: Menu not found - check menu names"));
2374 vim_free(saved_name
);
2379 #ifdef FEAT_MULTI_LANG
2381 * Translation of menu names. Just a simple lookup table.
2386 char_u
*from
; /* English name */
2387 char_u
*from_noamp
; /* same, without '&' */
2388 char_u
*to
; /* translated name */
2391 static garray_T menutrans_ga
= {0, 0, 0, 0, NULL
};
2396 * This function is also defined without the +multi_lang feature, in which
2397 * case the commands are ignored.
2400 ex_menutranslate(eap
)
2401 exarg_T
*eap UNUSED
;
2403 #ifdef FEAT_MULTI_LANG
2404 char_u
*arg
= eap
->arg
;
2407 char_u
*from
, *from_noamp
, *to
;
2409 if (menutrans_ga
.ga_itemsize
== 0)
2410 ga_init2(&menutrans_ga
, (int)sizeof(menutrans_T
), 5);
2413 * ":menutrans clear": clear all translations.
2415 if (STRNCMP(arg
, "clear", 5) == 0 && ends_excmd(*skipwhite(arg
+ 5)))
2417 tp
= (menutrans_T
*)menutrans_ga
.ga_data
;
2418 for (i
= 0; i
< menutrans_ga
.ga_len
; ++i
)
2420 vim_free(tp
[i
].from
);
2421 vim_free(tp
[i
].from_noamp
);
2424 ga_clear(&menutrans_ga
);
2426 /* Delete all "menutrans_" global variables. */
2427 del_menutrans_vars();
2432 /* ":menutrans from to": add translation */
2434 arg
= menu_skip_part(arg
);
2435 to
= skipwhite(arg
);
2437 arg
= menu_skip_part(to
);
2442 if (ga_grow(&menutrans_ga
, 1) == OK
)
2444 tp
= (menutrans_T
*)menutrans_ga
.ga_data
;
2445 from
= vim_strsave(from
);
2448 from_noamp
= menu_text(from
, NULL
, NULL
);
2449 to
= vim_strnsave(to
, (int)(arg
- to
));
2450 if (from_noamp
!= NULL
&& to
!= NULL
)
2452 menu_translate_tab_and_shift(from
);
2453 menu_translate_tab_and_shift(to
);
2454 menu_unescape_name(from
);
2455 menu_unescape_name(to
);
2456 tp
[menutrans_ga
.ga_len
].from
= from
;
2457 tp
[menutrans_ga
.ga_len
].from_noamp
= from_noamp
;
2458 tp
[menutrans_ga
.ga_len
].to
= to
;
2459 ++menutrans_ga
.ga_len
;
2464 vim_free(from_noamp
);
2474 #if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
2476 * Find the character just after one part of a menu name.
2482 while (*p
!= NUL
&& *p
!= '.' && !vim_iswhite(*p
))
2484 if ((*p
== '\\' || *p
== Ctrl_V
) && p
[1] != NUL
)
2492 #ifdef FEAT_MULTI_LANG
2494 * Lookup part of a menu name in the translations.
2495 * Return a pointer to the translation or NULL if not found.
2498 menutrans_lookup(name
, len
)
2502 menutrans_T
*tp
= (menutrans_T
*)menutrans_ga
.ga_data
;
2506 for (i
= 0; i
< menutrans_ga
.ga_len
; ++i
)
2507 if (STRNCMP(name
, tp
[i
].from
, len
) == 0 && tp
[i
].from
[len
] == NUL
)
2510 /* Now try again while ignoring '&' characters. */
2513 dname
= menu_text(name
, NULL
, NULL
);
2517 for (i
= 0; i
< menutrans_ga
.ga_len
; ++i
)
2518 if (STRCMP(dname
, tp
[i
].from_noamp
) == 0)
2528 #endif /* FEAT_MULTI_LANG */
2531 * Unescape the name in the translate dictionary table.
2534 menu_unescape_name(name
)
2539 for (p
= name
; *p
&& *p
!= '.'; mb_ptr_adv(p
))
2545 * Isolate the menu name.
2546 * Skip the menu name, and translate <Tab> into a real TAB.
2549 menu_translate_tab_and_shift(arg_start
)
2552 char_u
*arg
= arg_start
;
2554 while (*arg
&& !vim_iswhite(*arg
))
2556 if ((*arg
== '\\' || *arg
== Ctrl_V
) && arg
[1] != NUL
)
2558 else if (STRNICMP(arg
, "<TAB>", 5) == 0)
2561 STRMOVE(arg
+ 1, arg
+ 5);
2567 arg
= skipwhite(arg
);
2572 #endif /* FEAT_MENU */