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 /* 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\"");
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 *))
83 * Do the :menu command and relatives.
87 exarg_T
*eap
; /* Ex command arguments */
100 #if defined(FEAT_GUI) && !defined(FEAT_GUI_GTK)
102 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
103 int old_toolbar_height
;
106 int pri_tab
[MENUDEPTH
+ 1];
107 int enable
= MAYBE
; /* TRUE for "menu enable", FALSE for "menu
109 #ifdef FEAT_MULTI_LANG
110 char_u
*tofree
= NULL
;
118 modes
= get_menu_cmd_modes(eap
->cmd
, eap
->forceit
, &noremap
, &unmenu
);
123 if (STRNCMP(arg
, "<script>", 8) == 0)
125 noremap
= REMAP_SCRIPT
;
126 arg
= skipwhite(arg
+ 8);
129 if (STRNCMP(arg
, "<silent>", 8) == 0)
132 arg
= skipwhite(arg
+ 8);
135 if (STRNCMP(arg
, "<special>", 9) == 0)
138 arg
= skipwhite(arg
+ 9);
145 /* Locate an optional "icon=filename" argument. */
146 if (STRNCMP(arg
, "icon=", 5) == 0)
152 while (*arg
!= NUL
&& *arg
!= ' ')
155 STRMOVE(arg
, arg
+ 1);
161 arg
= skipwhite(arg
);
166 * Fill in the priority table.
168 for (p
= arg
; *p
; ++p
)
169 if (!VIM_ISDIGIT(*p
) && *p
!= '.')
173 for (i
= 0; i
< MENUDEPTH
&& !vim_iswhite(*arg
); ++i
)
175 pri_tab
[i
] = getdigits(&arg
);
181 arg
= skipwhite(arg
);
183 else if (eap
->addr_count
&& eap
->line2
!= 0)
185 pri_tab
[0] = eap
->line2
;
190 while (i
< MENUDEPTH
)
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]))
200 arg
= skipwhite(arg
+ 6);
202 else if (STRNCMP(arg
, "disable", 7) == 0 && vim_iswhite(arg
[7]))
205 arg
= skipwhite(arg
+ 7);
209 * If there is no argument, display all menus.
213 show_menus(arg
, modes
);
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
>= TOOLBAR_NAME_COUNT
)
235 menuarg
.iconidx
= -1;
237 menuarg
.icon_builtin
= TRUE
;
242 for (i
= 0; i
< TOOLBAR_NAME_COUNT
; ++i
)
243 if (STRNCMP(toolbar_names
[i
], menu_path
, p
- menu_path
)
254 #ifdef FEAT_MULTI_LANG
256 * Translate menu names as specified with ":menutrans" commands.
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
));
266 /* found a match: replace with the translated part */
267 i
= (int)STRLEN(map_to
);
268 new_cmd
= alloc((unsigned)STRLEN(arg
) + i
+ 1);
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
;
286 * Isolate the menu name.
287 * Skip the menu name, and translate <Tab> into a real TAB.
290 if (*menu_path
== '.')
292 EMSG2(_(e_invarg2
), menu_path
);
296 while (*arg
&& !vim_iswhite(*arg
))
298 if ((*arg
== '\\' || *arg
== Ctrl_V
) && arg
[1] != NUL
)
300 else if (STRNICMP(arg
, "<TAB>", 5) == 0)
303 STRMOVE(arg
+ 1, arg
+ 5);
309 arg
= skipwhite(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
);
320 else if (*map_to
!= NUL
&& (unmenu
|| enable
!= MAYBE
))
325 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
326 old_menu_height
= gui
.menu_height
;
327 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
328 old_toolbar_height
= gui
.toolbar_height
;
335 * Change sensitivity of the menu.
336 * For the PopUp menu, remove a menu for each mode separately.
337 * Careful: menu_nable_recurse() changes menu_path.
339 if (STRCMP(menu_path
, "*") == 0) /* meaning: do all menus */
340 menu_path
= (char_u
*)"";
342 if (menu_is_popup(menu_path
))
344 for (i
= 0; i
< MENU_INDEX_TIP
; ++i
)
345 if (modes
& (1 << i
))
347 p
= popup_mode_name(menu_path
, i
);
350 menu_nable_recurse(root_menu
, p
, MENU_ALL_MODES
,
356 menu_nable_recurse(root_menu
, menu_path
, modes
, enable
);
363 if (STRCMP(menu_path
, "*") == 0) /* meaning: remove all menus */
364 menu_path
= (char_u
*)"";
367 * For the PopUp menu, remove a menu for each mode separately.
369 if (menu_is_popup(menu_path
))
371 for (i
= 0; i
< MENU_INDEX_TIP
; ++i
)
372 if (modes
& (1 << i
))
374 p
= popup_mode_name(menu_path
, i
);
377 remove_menu(&root_menu
, p
, MENU_ALL_MODES
, TRUE
);
383 /* Careful: remove_menu() changes menu_path */
384 remove_menu(&root_menu
, menu_path
, modes
, FALSE
);
390 * Replace special key codes.
392 if (STRICMP(map_to
, "<nop>") == 0) /* "<Nop>" means nothing */
394 map_to
= (char_u
*)"";
397 else if (modes
& MENU_TIP_MODE
)
398 map_buf
= NULL
; /* Menu tips are plain text. */
400 map_to
= replace_termcodes(map_to
, &map_buf
, FALSE
, TRUE
, special
);
401 menuarg
.modes
= modes
;
403 menuarg
.iconfile
= icon
;
405 menuarg
.noremap
[0] = noremap
;
406 menuarg
.silent
[0] = silent
;
407 add_menu_path(menu_path
, &menuarg
, pri_tab
, map_to
414 * For the PopUp menu, add a menu for each mode separately.
416 if (menu_is_popup(menu_path
))
418 for (i
= 0; i
< MENU_INDEX_TIP
; ++i
)
419 if (modes
& (1 << i
))
421 p
= popup_mode_name(menu_path
, i
);
424 /* Include all modes, to make ":amenu" work */
425 menuarg
.modes
= modes
;
427 menuarg
.iconfile
= NULL
;
428 menuarg
.iconidx
= -1;
429 menuarg
.icon_builtin
= FALSE
;
431 add_menu_path(p
, &menuarg
, pri_tab
, map_to
444 #if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK))
445 /* If the menubar height changed, resize the window */
447 && (gui
.menu_height
!= old_menu_height
448 # if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
449 || gui
.toolbar_height
!= old_toolbar_height
452 gui_set_shellsize(FALSE
, FALSE
, RESIZE_VERT
);
456 #ifdef FEAT_MULTI_LANG
464 * Add the menu with the given name to the menu hierarchy
467 add_menu_path(menu_path
, menuarg
, pri_tab
, call_data
473 vimmenu_T
*menuarg
; /* passes modes, iconfile, iconidx,
474 icon_builtin, silent[0], noremap[0] */
478 int addtearoff
; /* may add tearoff item */
482 int modes
= menuarg
->modes
;
484 vimmenu_T
*menu
= NULL
;
486 vimmenu_T
**lower_pri
;
501 /* Make a copy so we can stuff around with it, since it could be const */
502 path_name
= vim_strsave(menu_path
);
503 if (path_name
== NULL
)
510 /* Get name of this element in the menu hierarchy, and the simplified
511 * name (without mnemonic and accelerator text). */
512 next_name
= menu_name_skip(name
);
513 dname
= menu_text(name
, NULL
, NULL
);
518 /* Only a mnemonic or accelerator is not valid. */
519 EMSG(_("E792: Empty menu name"));
523 /* See if it's already there */
532 if (menu_name_equal(name
, menu
) || menu_name_equal(dname
, menu
))
534 if (*next_name
== NUL
&& menu
->children
!= NULL
)
537 EMSG(_("E330: Menu path must not lead to a sub-menu"));
540 if (*next_name
!= NUL
&& menu
->children
== NULL
547 EMSG(_(e_notsubmenu
));
554 /* Count menus, to find where this one needs to be inserted.
555 * Ignore menus that are not in the menubar (PopUp and Toolbar) */
556 if (parent
!= NULL
|| menu_is_menubar(menu
->name
))
561 if (menu
->priority
<= pri_tab
[pri_idx
])
574 if (*next_name
== NUL
&& parent
== NULL
)
576 EMSG(_("E331: Must not add menu items directly to menu bar"));
580 if (menu_is_separator(dname
) && *next_name
!= NUL
)
582 EMSG(_("E332: Separator cannot be part of a menu path"));
586 /* Not already there, so lets add it */
587 menu
= (vimmenu_T
*)alloc_clear((unsigned)sizeof(vimmenu_T
));
592 menu
->enabled
= MENU_ALL_MODES
;
593 menu
->name
= vim_strsave(name
);
594 /* separate mnemonic and accelerator text from actual menu name */
595 menu
->dname
= menu_text(name
, &menu
->mnemonic
, &menu
->actext
);
596 menu
->priority
= pri_tab
[pri_idx
];
597 menu
->parent
= parent
;
598 #ifdef FEAT_GUI_MOTIF
599 menu
->sensitive
= TRUE
; /* the default */
601 #ifdef FEAT_BEVAL_TIP
604 #ifdef FEAT_GUI_ATHENA
605 menu
->image
= None
; /* X-Windows definition for NULL*/
609 * Add after menu that has lower priority.
611 menu
->next
= *lower_pri
;
617 menu
->iconidx
= menuarg
->iconidx
;
618 menu
->icon_builtin
= menuarg
->icon_builtin
;
619 if (*next_name
== NUL
&& menuarg
->iconfile
!= NULL
)
620 menu
->iconfile
= vim_strsave(menuarg
->iconfile
);
622 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
623 /* the tearoff item must be present in the modes of each item. */
624 if (parent
!= NULL
&& menu_is_tearoff(parent
->children
->dname
))
625 parent
->children
->modes
|= modes
;
630 old_modes
= menu
->modes
;
633 * If this menu option was previously only available in other
634 * modes, then make sure it's available for this one now
635 * Also enable a menu when it's created or changed.
638 /* If adding a tearbar (addtearoff == FALSE) don't update modes */
642 menu
->modes
|= modes
;
643 menu
->enabled
|= modes
;
649 * Add the menu item when it's used in one of the modes, but not when
650 * only a tooltip is defined.
652 if ((old_modes
& MENU_ALL_MODES
) == 0
653 && (menu
->modes
& MENU_ALL_MODES
) != 0)
655 if (gui
.in_use
) /* Otherwise it will be added when GUI starts */
657 if (*next_name
== NUL
)
659 /* Real menu item, not sub-menu */
660 gui_mch_add_menu_item(menu
, new_idx
);
662 /* Want to update menus now even if mode not changed */
663 force_menu_update
= TRUE
;
667 /* Sub-menu (not at end of path yet) */
668 gui_mch_add_menu(menu
, new_idx
);
672 # if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
673 /* When adding a new submenu, may add a tearoff item */
676 && vim_strchr(p_go
, GO_TEAROFF
) != NULL
677 && menu_is_menubar(name
))
682 * The pointers next_name & path_name refer to a string with
683 * \'s and ^V's stripped out. But menu_path is a "raw"
684 * string, so we must correct for special characters.
686 tearpath
= alloc((unsigned int)STRLEN(menu_path
) + TEAR_LEN
+ 2);
687 if (tearpath
!= NULL
)
692 STRCPY(tearpath
, menu_path
);
693 idx
= (int)(next_name
- path_name
- 1);
694 for (s
= tearpath
; *s
&& s
< tearpath
+ idx
; mb_ptr_adv(s
))
696 if ((*s
== '\\' || *s
== Ctrl_V
) && s
[1])
703 gui_add_tearoff(tearpath
, pri_tab
, pri_idx
);
709 #endif /* FEAT_GUI */
711 menup
= &menu
->children
;
716 if (pri_tab
[pri_idx
+ 1] != -1)
722 * Only add system menu items which have not been defined yet.
723 * First check if this was an ":amenu".
725 amenu
= ((modes
& (MENU_NORMAL_MODE
| MENU_INSERT_MODE
)) ==
726 (MENU_NORMAL_MODE
| MENU_INSERT_MODE
));
730 if (menu
!= NULL
&& modes
)
733 menu
->cb
= gui_menu_cb
;
735 p
= (call_data
== NULL
) ? NULL
: vim_strsave(call_data
);
737 /* loop over all modes, may add more than one */
738 for (i
= 0; i
< MENU_MODES
; ++i
)
740 if (modes
& (1 << i
))
742 /* free any old menu */
743 free_menu_string(menu
, i
);
745 /* For "amenu", may insert an extra character.
746 * Don't do this if adding a tearbar (addtearoff == FALSE).
747 * Don't do this for "<Nop>". */
749 if (amenu
&& call_data
!= NULL
&& *call_data
!= NUL
757 case MENU_VISUAL_MODE
:
758 case MENU_SELECT_MODE
:
759 case MENU_OP_PENDING_MODE
:
760 case MENU_CMDLINE_MODE
:
763 case MENU_INSERT_MODE
:
771 menu
->strings
[i
] = alloc((unsigned)(STRLEN(call_data
) + 4));
772 if (menu
->strings
[i
] != NULL
)
774 menu
->strings
[i
][0] = c
;
775 STRCPY(menu
->strings
[i
] + 1, call_data
);
778 int len
= (int)STRLEN(menu
->strings
[i
]);
780 /* Append CTRL-\ CTRL-G to obey 'insertmode'. */
781 menu
->strings
[i
][len
] = Ctrl_BSL
;
782 menu
->strings
[i
][len
+ 1] = Ctrl_G
;
783 menu
->strings
[i
][len
+ 2] = NUL
;
788 menu
->strings
[i
] = p
;
789 menu
->noremap
[i
] = menuarg
->noremap
[0];
790 menu
->silent
[i
] = menuarg
->silent
[0];
793 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
794 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
795 /* Need to update the menu tip. */
796 if (modes
& MENU_TIP_MODE
)
797 gui_mch_menu_set_tip(menu
);
806 /* Delete any empty submenu we added before discovering the error. Repeat
807 * for higher levels. */
808 while (parent
!= NULL
&& parent
->children
== NULL
)
810 if (parent
->parent
== NULL
)
813 menup
= &parent
->parent
->children
;
814 for ( ; *menup
!= NULL
&& *menup
!= parent
; menup
= &((*menup
)->next
))
816 if (*menup
== NULL
) /* safety check */
818 parent
= parent
->parent
;
825 * Set the (sub)menu with the given name to enabled or disabled.
826 * Called recursively.
829 menu_nable_recurse(menu
, name
, modes
, enable
)
838 return OK
; /* Got to bottom of hierarchy */
840 /* Get name of this element in the menu hierarchy */
841 p
= menu_name_skip(name
);
846 if (*name
== NUL
|| *name
== '*' || menu_name_equal(name
, menu
))
850 if (menu
->children
== NULL
)
852 EMSG(_(e_notsubmenu
));
855 if (menu_nable_recurse(menu
->children
, p
, modes
, enable
)
861 menu
->enabled
|= modes
;
863 menu
->enabled
&= ~modes
;
866 * When name is empty, we are doing all menu items for the given
867 * modes, so keep looping, otherwise we are just doing the named
868 * menu item (which has been found) so break here.
870 if (*name
!= NUL
&& *name
!= '*')
875 if (*name
!= NUL
&& *name
!= '*' && menu
== NULL
)
877 EMSG2(_(e_nomenu
), name
);
882 /* Want to update menus now even if mode not changed */
883 force_menu_update
= TRUE
;
890 * Remove the (sub)menu with the given name from the menu hierarchy
891 * Called recursively.
894 remove_menu(menup
, name
, modes
, silent
)
898 int silent
; /* don't give error messages */
905 return OK
; /* Got to bottom of hierarchy */
907 /* Get name of this element in the menu hierarchy */
908 p
= menu_name_skip(name
);
911 while ((menu
= *menup
) != NULL
)
913 if (*name
== NUL
|| menu_name_equal(name
, menu
))
915 if (*p
!= NUL
&& menu
->children
== NULL
)
918 EMSG(_(e_notsubmenu
));
921 if ((menu
->modes
& modes
) != 0x0)
923 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
925 * If we are removing all entries for this menu,MENU_ALL_MODES,
926 * Then kill any tearoff before we start
928 if (*p
== NUL
&& modes
== MENU_ALL_MODES
)
930 if (IsWindow(menu
->tearoff_handle
))
931 DestroyWindow(menu
->tearoff_handle
);
934 if (remove_menu(&menu
->children
, p
, modes
, silent
) == FAIL
)
937 else if (*name
!= NUL
)
940 EMSG(_(e_othermode
));
945 * When name is empty, we are removing all menu items for the given
946 * modes, so keep looping, otherwise we are just removing the named
947 * menu item (which has been found) so break here.
952 /* Remove the menu item for the given mode[s]. If the menu item
953 * is no longer valid in ANY mode, delete it */
954 menu
->modes
&= ~modes
;
955 if (modes
& MENU_TIP_MODE
)
956 free_menu_string(menu
, MENU_INDEX_TIP
);
957 if ((menu
->modes
& MENU_ALL_MODES
) == 0)
970 EMSG2(_(e_nomenu
), name
);
975 /* Recalculate modes for menu based on the new updated children */
976 menu
->modes
&= ~modes
;
977 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
978 if ((s_tearoffs
) && (menu
->children
!= NULL
)) /* there's a tear bar.. */
979 child
= menu
->children
->next
; /* don't count tearoff bar */
982 child
= menu
->children
;
983 for ( ; child
!= NULL
; child
= child
->next
)
984 menu
->modes
|= child
->modes
;
985 if (modes
& MENU_TIP_MODE
)
987 free_menu_string(menu
, MENU_INDEX_TIP
);
988 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
989 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
990 /* Need to update the menu tip. */
992 gui_mch_menu_set_tip(menu
);
995 if ((menu
->modes
& MENU_ALL_MODES
) == 0)
997 /* The menu item is no longer valid in ANY mode, so delete it */
998 #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
999 if (s_tearoffs
&& menu
->children
!= NULL
) /* there's a tear bar.. */
1000 free_menu(&menu
->children
);
1011 * Free the given menu structure and remove it from the linked list.
1023 /* Free machine specific menu structures (only when already created) */
1024 /* Also may rebuild a tearoff'ed menu */
1026 gui_mch_destroy_menu(menu
);
1029 /* Don't change *menup until after calling gui_mch_destroy_menu(). The
1030 * MacOS code needs the original structure to properly delete the menu. */
1031 *menup
= menu
->next
;
1032 vim_free(menu
->name
);
1033 vim_free(menu
->dname
);
1034 vim_free(menu
->actext
);
1036 vim_free(menu
->iconfile
);
1037 # ifdef FEAT_GUI_MOTIF
1038 vim_free(menu
->xpm_fname
);
1041 for (i
= 0; i
< MENU_MODES
; i
++)
1042 free_menu_string(menu
, i
);
1046 /* Want to update menus now even if mode not changed */
1047 force_menu_update
= TRUE
;
1052 * Free the menu->string with the given index.
1055 free_menu_string(menu
, idx
)
1062 for (i
= 0; i
< MENU_MODES
; i
++)
1063 if (menu
->strings
[i
] == menu
->strings
[idx
])
1066 vim_free(menu
->strings
[idx
]);
1067 menu
->strings
[idx
] = NULL
;
1071 * Show the mapping associated with a menu item or hierarchy in a sub-menu.
1074 show_menus(path_name
, modes
)
1081 vimmenu_T
*parent
= NULL
;
1084 name
= path_name
= vim_strsave(path_name
);
1085 if (path_name
== NULL
)
1088 /* First, find the (sub)menu with the given name */
1091 p
= menu_name_skip(name
);
1092 while (menu
!= NULL
)
1094 if (menu_name_equal(name
, menu
))
1097 if (*p
!= NUL
&& menu
->children
== NULL
)
1099 EMSG(_(e_notsubmenu
));
1100 vim_free(path_name
);
1103 else if ((menu
->modes
& modes
) == 0x0)
1105 EMSG(_(e_othermode
));
1106 vim_free(path_name
);
1115 EMSG2(_(e_nomenu
), name
);
1116 vim_free(path_name
);
1121 menu
= menu
->children
;
1123 vim_free(path_name
);
1125 /* Now we have found the matching menu, and we list the mappings */
1126 /* Highlight title */
1127 MSG_PUTS_TITLE(_("\n--- Menus ---"));
1129 show_menus_recursive(parent
, modes
, 0);
1134 * Recursively show the mappings associated with the menus under the given one
1137 show_menus_recursive(menu
, modes
, depth
)
1145 if (menu
!= NULL
&& (menu
->modes
& modes
) == 0x0)
1151 if (got_int
) /* "q" hit for "--more--" */
1153 for (i
= 0; i
< depth
; i
++)
1157 msg_outnum((long)menu
->priority
);
1160 /* Same highlighting as for directories!? */
1161 msg_outtrans_attr(menu
->name
, hl_attr(HLF_D
));
1164 if (menu
!= NULL
&& menu
->children
== NULL
)
1166 for (bit
= 0; bit
< MENU_MODES
; bit
++)
1167 if ((menu
->modes
& modes
& (1 << bit
)) != 0)
1170 if (got_int
) /* "q" hit for "--more--" */
1172 for (i
= 0; i
< depth
+ 2; i
++)
1174 msg_putchar(menu_mode_chars
[bit
]);
1175 if (menu
->noremap
[bit
] == REMAP_NONE
)
1177 else if (menu
->noremap
[bit
] == REMAP_SCRIPT
)
1181 if (menu
->silent
[bit
])
1185 if ((menu
->modes
& menu
->enabled
& (1 << bit
)) == 0)
1190 if (*menu
->strings
[bit
] == NUL
)
1191 msg_puts_attr((char_u
*)"<Nop>", hl_attr(HLF_8
));
1193 msg_outtrans_special(menu
->strings
[bit
], FALSE
);
1204 menu
= menu
->children
;
1206 /* recursively show all children. Skip PopUp[nvoci]. */
1207 for (; menu
!= NULL
&& !got_int
; menu
= menu
->next
)
1208 if (!menu_is_hidden(menu
->dname
))
1209 show_menus_recursive(menu
, modes
, depth
+ 1);
1213 #ifdef FEAT_CMDL_COMPL
1216 * Used when expanding menu names.
1218 static vimmenu_T
*expand_menu
= NULL
;
1219 static int expand_modes
= 0x0;
1220 static int expand_emenu
; /* TRUE for ":emenu" command */
1223 * Work out what to complete when doing command line completion of menu names.
1226 set_context_in_menu_cmd(xp
, cmd
, arg
, forceit
)
1234 char_u
*path_name
= NULL
;
1240 xp
->xp_context
= EXPAND_UNSUCCESSFUL
;
1243 /* Check for priority numbers, enable and disable */
1244 for (p
= arg
; *p
; ++p
)
1245 if (!VIM_ISDIGIT(*p
) && *p
!= '.')
1248 if (!vim_iswhite(*p
))
1250 if (STRNCMP(arg
, "enable", 6) == 0
1251 && (arg
[6] == NUL
|| vim_iswhite(arg
[6])))
1253 else if (STRNCMP(arg
, "disable", 7) == 0
1254 && (arg
[7] == NUL
|| vim_iswhite(arg
[7])))
1260 while (*p
!= NUL
&& vim_iswhite(*p
))
1263 arg
= after_dot
= p
;
1265 for (; *p
&& !vim_iswhite(*p
); ++p
)
1267 if ((*p
== '\\' || *p
== Ctrl_V
) && p
[1] != NUL
)
1273 /* ":tearoff" and ":popup" only use menus, not entries */
1274 expand_menus
= !((*cmd
== 't' && cmd
[1] == 'e') || *cmd
== 'p');
1275 expand_emenu
= (*cmd
== 'e');
1276 if (expand_menus
&& vim_iswhite(*p
))
1277 return NULL
; /* TODO: check for next command? */
1278 if (*p
== NUL
) /* Complete the menu name */
1281 * With :unmenu, you only want to match menus for the appropriate mode.
1282 * With :menu though you might want to add a menu with the same name as
1283 * one in another mode, so match menus from other modes too.
1285 expand_modes
= get_menu_cmd_modes(cmd
, forceit
, NULL
, &unmenu
);
1287 expand_modes
= MENU_ALL_MODES
;
1290 if (after_dot
!= arg
)
1292 path_name
= alloc((unsigned)(after_dot
- arg
));
1293 if (path_name
== NULL
)
1295 vim_strncpy(path_name
, arg
, after_dot
- arg
- 1);
1298 while (name
!= NULL
&& *name
)
1300 p
= menu_name_skip(name
);
1301 while (menu
!= NULL
)
1303 if (menu_name_equal(name
, menu
))
1306 if ((*p
!= NUL
&& menu
->children
== NULL
)
1307 || ((menu
->modes
& expand_modes
) == 0x0))
1310 * Menu path continues, but we have reached a leaf.
1311 * Or menu exists only in another mode.
1313 vim_free(path_name
);
1322 /* No menu found with the name we were looking for */
1323 vim_free(path_name
);
1327 menu
= menu
->children
;
1329 vim_free(path_name
);
1331 xp
->xp_context
= expand_menus
? EXPAND_MENUNAMES
: EXPAND_MENUS
;
1332 xp
->xp_pattern
= after_dot
;
1335 else /* We're in the mapping part */
1336 xp
->xp_context
= EXPAND_NOTHING
;
1341 * Function given to ExpandGeneric() to obtain the list of (sub)menus (not
1346 get_menu_name(xp
, idx
)
1350 static vimmenu_T
*menu
= NULL
;
1353 if (idx
== 0) /* first call: start at first item */
1356 /* Skip PopUp[nvoci]. */
1357 while (menu
!= NULL
&& (menu_is_hidden(menu
->dname
)
1358 || menu_is_separator(menu
->dname
)
1359 || menu_is_tearoff(menu
->dname
)
1360 || menu
->children
== NULL
))
1363 if (menu
== NULL
) /* at end of linked list */
1366 if (menu
->modes
& expand_modes
)
1371 /* Advance to next menu entry. */
1378 * Function given to ExpandGeneric() to obtain the list of menus and menu
1383 get_menu_names(xp
, idx
)
1387 static vimmenu_T
*menu
= NULL
;
1388 static char_u tbuffer
[256]; /*hack*/
1391 if (idx
== 0) /* first call: start at first item */
1394 /* Skip Browse-style entries, popup menus and separators. */
1396 && ( menu_is_hidden(menu
->dname
)
1397 || (expand_emenu
&& menu_is_separator(menu
->dname
))
1398 || menu_is_tearoff(menu
->dname
)
1400 || menu
->dname
[STRLEN(menu
->dname
) - 1] == '.'
1405 if (menu
== NULL
) /* at end of linked list */
1408 if (menu
->modes
& expand_modes
)
1410 if (menu
->children
!= NULL
)
1412 STRCPY(tbuffer
, menu
->dname
);
1413 /* hack on menu separators: use a 'magic' char for the separator
1414 * so that '.' in names gets escaped properly */
1415 STRCAT(tbuffer
, "\001");
1424 /* Advance to next menu entry. */
1429 #endif /* FEAT_CMDL_COMPL */
1432 * Skip over this element of the menu path and return the start of the next
1433 * element. Any \ and ^Vs are removed from the current element.
1434 * "name" may be modified.
1437 menu_name_skip(name
)
1442 for (p
= name
; *p
&& *p
!= '.'; mb_ptr_adv(p
))
1444 if (*p
== '\\' || *p
== Ctrl_V
)
1457 * Return TRUE when "name" matches with menu "menu". The name is compared in
1458 * two ways: raw menu name and menu name without '&'. ignore part after a TAB.
1461 menu_name_equal(name
, menu
)
1465 return (menu_namecmp(name
, menu
->name
) || menu_namecmp(name
, menu
->dname
));
1469 menu_namecmp(name
, mname
)
1475 for (i
= 0; name
[i
] != NUL
&& name
[i
] != TAB
; ++i
)
1476 if (name
[i
] != mname
[i
])
1478 return ((name
[i
] == NUL
|| name
[i
] == TAB
)
1479 && (mname
[i
] == NUL
|| mname
[i
] == TAB
));
1483 * Return the modes specified by the given menu command (eg :menu! returns
1484 * MENU_CMDLINE_MODE | MENU_INSERT_MODE).
1485 * If "noremap" is not NULL, then the flag it points to is set according to
1486 * whether the command is a "nore" command.
1487 * If "unmenu" is not NULL, then the flag it points to is set according to
1488 * whether the command is an "unmenu" command.
1491 get_menu_cmd_modes(cmd
, forceit
, noremap
, unmenu
)
1493 int forceit
; /* Was there a "!" after the command? */
1501 case 'v': /* vmenu, vunmenu, vnoremenu */
1502 modes
= MENU_VISUAL_MODE
| MENU_SELECT_MODE
;
1504 case 'x': /* xmenu, xunmenu, xnoremenu */
1505 modes
= MENU_VISUAL_MODE
;
1507 case 's': /* smenu, sunmenu, snoremenu */
1508 modes
= MENU_SELECT_MODE
;
1510 case 'o': /* omenu */
1511 modes
= MENU_OP_PENDING_MODE
;
1513 case 'i': /* imenu */
1514 modes
= MENU_INSERT_MODE
;
1517 modes
= MENU_TIP_MODE
; /* tmenu */
1519 case 'c': /* cmenu */
1520 modes
= MENU_CMDLINE_MODE
;
1522 case 'a': /* amenu */
1523 modes
= MENU_INSERT_MODE
| MENU_CMDLINE_MODE
| MENU_NORMAL_MODE
1524 | MENU_VISUAL_MODE
| MENU_SELECT_MODE
1525 | MENU_OP_PENDING_MODE
;
1528 if (*cmd
!= 'o') /* nmenu, not noremenu */
1530 modes
= MENU_NORMAL_MODE
;
1536 if (forceit
) /* menu!! */
1537 modes
= MENU_INSERT_MODE
| MENU_CMDLINE_MODE
;
1539 modes
= MENU_NORMAL_MODE
| MENU_VISUAL_MODE
| MENU_SELECT_MODE
1540 | MENU_OP_PENDING_MODE
;
1543 if (noremap
!= NULL
)
1544 *noremap
= (*cmd
== 'n' ? REMAP_NONE
: REMAP_YES
);
1546 *unmenu
= (*cmd
== 'u');
1551 * Modify a menu name starting with "PopUp" to include the mode character.
1552 * Returns the name in allocated memory (NULL for failure).
1555 popup_mode_name(name
, idx
)
1560 int len
= (int)STRLEN(name
);
1562 p
= vim_strnsave(name
, len
+ 1);
1565 mch_memmove(p
+ 6, p
+ 5, (size_t)(len
- 4));
1566 p
[5] = menu_mode_chars
[idx
];
1571 #if defined(FEAT_GUI) || defined(PROTO)
1573 * Return the index into the menu->strings or menu->noremap arrays for the
1574 * current state. Returns MENU_INDEX_INVALID if there is no mapping for the
1575 * given menu in the current mode.
1578 get_menu_index(menu
, state
)
1584 if ((state
& INSERT
))
1585 idx
= MENU_INDEX_INSERT
;
1586 else if (state
& CMDLINE
)
1587 idx
= MENU_INDEX_CMDLINE
;
1589 else if (VIsual_active
)
1592 idx
= MENU_INDEX_SELECT
;
1594 idx
= MENU_INDEX_VISUAL
;
1597 else if (state
== HITRETURN
|| state
== ASKMORE
)
1598 idx
= MENU_INDEX_CMDLINE
;
1600 idx
= MENU_INDEX_OP_PENDING
;
1601 else if ((state
& NORMAL
))
1602 idx
= MENU_INDEX_NORMAL
;
1604 idx
= MENU_INDEX_INVALID
;
1606 if (idx
!= MENU_INDEX_INVALID
&& menu
->strings
[idx
] == NULL
)
1607 idx
= MENU_INDEX_INVALID
;
1613 * Duplicate the menu item text and then process to see if a mnemonic key
1614 * and/or accelerator text has been identified.
1615 * Returns a pointer to allocated memory, or NULL for failure.
1616 * If mnemonic != NULL, *mnemonic is set to the character after the first '&'.
1617 * If actext != NULL, *actext is set to the text after the first TAB.
1620 menu_text(str
, mnemonic
, actext
)
1628 /* Locate accelerator text, after the first TAB */
1629 p
= vim_strchr(str
, TAB
);
1633 *actext
= vim_strsave(p
+ 1);
1634 text
= vim_strnsave(str
, (int)(p
- str
));
1637 text
= vim_strsave(str
);
1639 /* Find mnemonic characters "&a" and reduce "&&" to "&". */
1640 for (p
= text
; p
!= NULL
; )
1642 p
= vim_strchr(p
, '&');
1645 if (p
[1] == NUL
) /* trailing "&" */
1647 if (mnemonic
!= NULL
&& p
[1] != '&')
1648 #if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED)
1653 * Well there is a bug in the Motif libraries on OS390 Unix.
1654 * The mnemonic keys needs to be converted to ASCII values
1656 * This behavior has been seen in 2.8 and 2.9.
1671 * Return TRUE if "name" can be a menu in the MenuBar.
1674 menu_is_menubar(name
)
1677 return (!menu_is_popup(name
)
1678 && !menu_is_toolbar(name
)
1679 && *name
!= MNU_HIDDEN_CHAR
);
1683 * Return TRUE if "name" is a popup menu name.
1689 return (STRNCMP(name
, "PopUp", 5) == 0);
1692 #if (defined(FEAT_GUI_MOTIF) && (XmVersion <= 1002)) || defined(PROTO)
1694 * Return TRUE if "name" is part of a popup menu.
1697 menu_is_child_of_popup(menu
)
1700 while (menu
->parent
!= NULL
)
1701 menu
= menu
->parent
;
1702 return menu_is_popup(menu
->name
);
1707 * Return TRUE if "name" is a toolbar menu name.
1710 menu_is_toolbar(name
)
1713 return (STRNCMP(name
, "ToolBar", 7) == 0);
1717 * Return TRUE if the name is a menu separator identifier: Starts and ends
1721 menu_is_separator(name
)
1724 return (name
[0] == '-' && name
[STRLEN(name
) - 1] == '-');
1728 * Return TRUE if the menu is hidden: Starts with ']'
1731 menu_is_hidden(name
)
1734 return (name
[0] == ']') || (menu_is_popup(name
) && name
[5] != NUL
);
1737 #if defined(FEAT_CMDL_COMPL) \
1738 || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
1740 * Return TRUE if the menu is the tearoff menu.
1744 menu_is_tearoff(name
)
1748 return (STRCMP(name
, TEAR_STRING
) == 0);
1764 return MENU_INDEX_SELECT
;
1765 return MENU_INDEX_VISUAL
;
1769 return MENU_INDEX_INSERT
;
1770 if ((State
& CMDLINE
) || State
== ASKMORE
|| State
== HITRETURN
)
1771 return MENU_INDEX_CMDLINE
;
1773 return MENU_INDEX_OP_PENDING
;
1775 return MENU_INDEX_NORMAL
;
1776 if (State
& LANGMAP
) /* must be a "r" command, like Insert mode */
1777 return MENU_INDEX_INSERT
;
1778 return MENU_INDEX_INVALID
;
1782 * Check that a pointer appears in the menu tree. Used to protect from using
1783 * a menu that was deleted after it was selected but before the event was
1785 * Return OK or FAIL. Used recursively.
1788 check_menu_pointer(root
, menu_to_check
)
1790 vimmenu_T
*menu_to_check
;
1794 for (p
= root
; p
!= NULL
; p
= p
->next
)
1795 if (p
== menu_to_check
1796 || (p
->children
!= NULL
1797 && check_menu_pointer(p
->children
, menu_to_check
) == OK
))
1803 * After we have started the GUI, then we can create any menus that have been
1804 * defined. This is done once here. add_menu_path() may have already been
1805 * called to define these menus, and may be called again. This function calls
1806 * itself recursively. Should be called at the top level with:
1807 * gui_create_initial_menus(root_menu, NULL);
1810 gui_create_initial_menus(menu
)
1815 while (menu
!= NULL
)
1817 /* Don't add a menu when only a tip was defined. */
1818 if (menu
->modes
& MENU_ALL_MODES
)
1820 if (menu
->children
!= NULL
)
1822 gui_mch_add_menu(menu
, idx
);
1823 gui_create_initial_menus(menu
->children
);
1826 gui_mch_add_menu_item(menu
, idx
);
1834 * Used recursively by gui_update_menus (see below)
1837 gui_update_menus_recurse(menu
, mode
)
1845 if ((menu
->modes
& menu
->enabled
& mode
)
1846 #if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
1847 || menu_is_tearoff(menu
->dname
)
1853 #ifdef FEAT_GUI_ATHENA
1854 /* Hiding menus doesn't work for Athena, it can cause a crash. */
1855 gui_mch_menu_grey(menu
, grey
);
1857 /* Never hide a toplevel menu, it may make the menubar resize or
1858 * disappear. Same problem for ToolBar items. */
1859 if (vim_strchr(p_go
, GO_GREY
) != NULL
|| menu
->parent
== NULL
1860 # ifdef FEAT_TOOLBAR
1861 || menu_is_toolbar(menu
->parent
->name
)
1864 gui_mch_menu_grey(menu
, grey
);
1866 gui_mch_menu_hidden(menu
, grey
);
1868 gui_update_menus_recurse(menu
->children
, mode
);
1874 * Make sure only the valid menu items appear for this mode. If
1875 * force_menu_update is not TRUE, then we only do this if the mode has changed
1876 * since last time. If "modes" is not 0, then we use these modes instead.
1879 gui_update_menus(modes
)
1882 static int prev_mode
= -1;
1889 mode
= get_menu_mode();
1890 if (mode
== MENU_INDEX_INVALID
)
1896 if (force_menu_update
|| mode
!= prev_mode
)
1898 gui_update_menus_recurse(root_menu
, mode
);
1899 gui_mch_draw_menubar();
1901 force_menu_update
= FALSE
;
1903 /* This can leave a tearoff as active window - make sure we
1904 * have the focus <negri>*/
1905 gui_mch_activate_window();
1910 #if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MOTIF) \
1911 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) || defined(PROTO)
1913 * Check if a key is used as a mnemonic for a toplevel menu.
1914 * Case of the key is ignored.
1917 gui_is_menu_shortcut(key
)
1923 key
= TOLOWER_LOC(key
);
1924 for (menu
= root_menu
; menu
!= NULL
; menu
= menu
->next
)
1925 if (menu
->mnemonic
== key
1926 || (menu
->mnemonic
< 256 && TOLOWER_LOC(menu
->mnemonic
) == key
))
1933 * Display the Special "PopUp" menu as a pop-up at the current mouse
1934 * position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode,
1938 gui_show_popupmenu()
1943 mode
= get_menu_mode();
1944 if (mode
== MENU_INDEX_INVALID
)
1946 mode
= menu_mode_chars
[mode
];
1954 apply_autocmds(EVENT_MENUPOPUP
, ename
, NULL
, FALSE
, curbuf
);
1958 for (menu
= root_menu
; menu
!= NULL
; menu
= menu
->next
)
1959 if (STRNCMP("PopUp", menu
->name
, 5) == 0 && menu
->name
[5] == mode
)
1962 /* Only show a popup when it is defined and has entries */
1963 if (menu
!= NULL
&& menu
->children
!= NULL
)
1964 gui_mch_show_popupmenu(menu
);
1966 #endif /* FEAT_GUI */
1968 #if (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)) || defined(PROTO)
1971 * Deal with tearoff items that are added like a menu item.
1972 * Currently only for Win32 GUI. Others may follow later.
1976 gui_mch_toggle_tearoffs(int enable
)
1978 int pri_tab
[MENUDEPTH
+ 1];
1983 for (i
= 0; i
< MENUDEPTH
; ++i
)
1985 pri_tab
[MENUDEPTH
] = -1;
1986 gui_create_tearoffs_recurse(root_menu
, (char_u
*)"", pri_tab
, 0);
1989 gui_destroy_tearoffs_recurse(root_menu
);
1990 s_tearoffs
= enable
;
1994 * Recursively add tearoff items
1997 gui_create_tearoffs_recurse(menu
, pname
, pri_tab
, pri_idx
)
1999 const char_u
*pname
;
2003 char_u
*newpname
= NULL
;
2008 if (pri_tab
[pri_idx
+ 1] != -1)
2010 while (menu
!= NULL
)
2012 if (menu
->children
!= NULL
&& menu_is_menubar(menu
->name
))
2014 /* Add the menu name to the menu path. Insert a backslash before
2015 * dots (it's used to separate menu names). */
2016 len
= (int)STRLEN(pname
) + (int)STRLEN(menu
->name
);
2017 for (s
= menu
->name
; *s
; ++s
)
2018 if (*s
== '.' || *s
== '\\')
2020 newpname
= alloc(len
+ TEAR_LEN
+ 2);
2021 if (newpname
!= NULL
)
2023 STRCPY(newpname
, pname
);
2024 d
= newpname
+ STRLEN(newpname
);
2025 for (s
= menu
->name
; *s
; ++s
)
2027 if (*s
== '.' || *s
== '\\')
2033 /* check if tearoff already exists */
2034 if (STRCMP(menu
->children
->name
, TEAR_STRING
) != 0)
2036 gui_add_tearoff(newpname
, pri_tab
, pri_idx
- 1);
2037 *d
= NUL
; /* remove TEAR_STRING */
2040 STRCAT(newpname
, ".");
2041 gui_create_tearoffs_recurse(menu
->children
, newpname
,
2051 * Add tear-off menu item for a submenu.
2052 * "tearpath" is the menu path, and must have room to add TEAR_STRING.
2055 gui_add_tearoff(tearpath
, pri_tab
, pri_idx
)
2064 tbuf
= alloc(5 + (unsigned int)STRLEN(tearpath
));
2067 tbuf
[0] = K_SPECIAL
;
2068 tbuf
[1] = K_SECOND(K_TEAROFF
);
2069 tbuf
[2] = K_THIRD(K_TEAROFF
);
2070 STRCPY(tbuf
+ 3, tearpath
);
2071 STRCAT(tbuf
+ 3, "\r");
2073 STRCAT(tearpath
, ".");
2074 STRCAT(tearpath
, TEAR_STRING
);
2076 /* Priority of tear-off is always 1 */
2077 t
= pri_tab
[pri_idx
+ 1];
2078 pri_tab
[pri_idx
+ 1] = 1;
2081 menuarg
.iconfile
= NULL
;
2082 menuarg
.iconidx
= -1;
2083 menuarg
.icon_builtin
= FALSE
;
2085 menuarg
.noremap
[0] = REMAP_NONE
;
2086 menuarg
.silent
[0] = TRUE
;
2088 menuarg
.modes
= MENU_ALL_MODES
;
2089 add_menu_path(tearpath
, &menuarg
, pri_tab
, tbuf
, FALSE
);
2091 menuarg
.modes
= MENU_TIP_MODE
;
2092 add_menu_path(tearpath
, &menuarg
, pri_tab
,
2093 (char_u
*)_("Tear off this menu"), FALSE
);
2095 pri_tab
[pri_idx
+ 1] = t
;
2101 * Recursively destroy tearoff items
2104 gui_destroy_tearoffs_recurse(menu
)
2111 /* check if tearoff exists */
2112 if (STRCMP(menu
->children
->name
, TEAR_STRING
) == 0)
2114 /* Disconnect the item and free the memory */
2115 free_menu(&menu
->children
);
2117 if (menu
->children
!= NULL
) /* if not the last one */
2118 gui_destroy_tearoffs_recurse(menu
->children
);
2124 #endif /* FEAT_GUI_W32 && FEAT_TEAROFF */
2127 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2141 saved_name
= vim_strsave(eap
->arg
);
2142 if (saved_name
== NULL
)
2149 /* Find in the menu hierarchy */
2150 p
= menu_name_skip(name
);
2152 while (menu
!= NULL
)
2154 if (menu_name_equal(name
, menu
))
2156 if (*p
== NUL
&& menu
->children
!= NULL
)
2158 EMSG(_("E333: Menu path must lead to a menu item"));
2161 else if (*p
!= NUL
&& menu
->children
== NULL
)
2163 EMSG(_(e_notsubmenu
));
2170 if (menu
== NULL
|| *p
== NUL
)
2172 menu
= menu
->children
;
2175 vim_free(saved_name
);
2178 EMSG2(_("E334: Menu not found: %s"), eap
->arg
);
2182 /* Found the menu, so execute.
2183 * Use the Insert mode entry when returning to Insert mode. */
2190 mode
= (char_u
*)"Insert";
2191 idx
= MENU_INDEX_INSERT
;
2193 else if (eap
->addr_count
)
2197 mode
= (char_u
*)"Visual";
2198 idx
= MENU_INDEX_VISUAL
;
2200 /* GEDDES: This is not perfect - but it is a
2201 * quick way of detecting whether we are doing this from a
2202 * selection - see if the range matches up with the visual
2203 * select start and end. */
2204 if ((curbuf
->b_visual
.vi_start
.lnum
== eap
->line1
)
2205 && (curbuf
->b_visual
.vi_end
.lnum
) == eap
->line2
)
2207 /* Set it up for visual mode - equivalent to gv. */
2208 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
2209 tpos
= curbuf
->b_visual
.vi_end
;
2210 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
2211 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
2215 /* Set it up for line-wise visual mode */
2217 curwin
->w_cursor
.lnum
= eap
->line1
;
2218 curwin
->w_cursor
.col
= 1;
2219 tpos
.lnum
= eap
->line2
;
2221 #ifdef FEAT_VIRTUALEDIT
2226 /* Activate visual mode */
2227 VIsual_active
= TRUE
;
2228 VIsual_reselect
= TRUE
;
2230 VIsual
= curwin
->w_cursor
;
2231 curwin
->w_cursor
= tpos
;
2235 /* Adjust the cursor to make sure it is in the correct pos
2236 * for exclusive mode */
2237 if (*p_sel
== 'e' && gchar_cursor() != NUL
)
2238 ++curwin
->w_cursor
.col
;
2242 mode
= (char_u
*)"Normal";
2243 idx
= MENU_INDEX_NORMAL
;
2246 if (idx
!= MENU_INDEX_INVALID
&& menu
->strings
[idx
] != NULL
)
2248 /* When executing a script or function execute the commands right now.
2249 * Otherwise put them in the typeahead buffer. */
2251 if (current_SID
!= 0)
2252 exec_normal_cmd(menu
->strings
[idx
], menu
->noremap
[idx
],
2256 ins_typebuf(menu
->strings
[idx
], menu
->noremap
[idx
], 0,
2257 TRUE
, menu
->silent
[idx
]);
2260 EMSG2(_("E335: Menu not defined for %s mode"), mode
);
2263 #if defined(FEAT_GUI_MSWIN) \
2264 || (defined(FEAT_GUI_GTK) && defined(FEAT_MENU)) \
2265 || defined(FEAT_BEVAL_TIP) || defined(PROTO)
2267 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy.
2270 gui_find_menu(path_name
)
2273 vimmenu_T
*menu
= NULL
;
2280 saved_name
= vim_strsave(path_name
);
2281 if (saved_name
== NULL
)
2287 /* find the end of one dot-separated name and put a NUL at the dot */
2288 p
= menu_name_skip(name
);
2290 while (menu
!= NULL
)
2292 if (STRCMP(name
, menu
->name
) == 0 || STRCMP(name
, menu
->dname
) == 0)
2294 if (menu
->children
== NULL
)
2296 /* found a menu item instead of a sub-menu */
2298 EMSG(_("E336: Menu path must lead to a sub-menu"));
2300 EMSG(_(e_notsubmenu
));
2304 if (*p
== NUL
) /* found a full match */
2310 if (menu
== NULL
) /* didn't find it */
2313 /* Found a match, search the sub-menu. */
2314 menu
= menu
->children
;
2319 EMSG(_("E337: Menu not found - check menu names"));
2321 vim_free(saved_name
);
2326 #ifdef FEAT_MULTI_LANG
2328 * Translation of menu names. Just a simple lookup table.
2333 char_u
*from
; /* English name */
2334 char_u
*from_noamp
; /* same, without '&' */
2335 char_u
*to
; /* translated name */
2338 static garray_T menutrans_ga
= {0, 0, 0, 0, NULL
};
2343 * This function is also defined without the +multi_lang feature, in which
2344 * case the commands are ignored.
2348 ex_menutranslate(eap
)
2351 #ifdef FEAT_MULTI_LANG
2352 char_u
*arg
= eap
->arg
;
2355 char_u
*from
, *from_noamp
, *to
;
2357 if (menutrans_ga
.ga_itemsize
== 0)
2358 ga_init2(&menutrans_ga
, (int)sizeof(menutrans_T
), 5);
2361 * ":menutrans clear": clear all translations.
2363 if (STRNCMP(arg
, "clear", 5) == 0 && ends_excmd(*skipwhite(arg
+ 5)))
2365 tp
= (menutrans_T
*)menutrans_ga
.ga_data
;
2366 for (i
= 0; i
< menutrans_ga
.ga_len
; ++i
)
2368 vim_free(tp
[i
].from
);
2369 vim_free(tp
[i
].from_noamp
);
2372 ga_clear(&menutrans_ga
);
2374 /* Delete all "menutrans_" global variables. */
2375 del_menutrans_vars();
2380 /* ":menutrans from to": add translation */
2382 arg
= menu_skip_part(arg
);
2383 to
= skipwhite(arg
);
2385 arg
= menu_skip_part(to
);
2390 if (ga_grow(&menutrans_ga
, 1) == OK
)
2392 tp
= (menutrans_T
*)menutrans_ga
.ga_data
;
2393 from
= vim_strsave(from
);
2396 from_noamp
= menu_text(from
, NULL
, NULL
);
2397 to
= vim_strnsave(to
, (int)(arg
- to
));
2398 if (from_noamp
!= NULL
&& to
!= NULL
)
2400 tp
[menutrans_ga
.ga_len
].from
= from
;
2401 tp
[menutrans_ga
.ga_len
].from_noamp
= from_noamp
;
2402 tp
[menutrans_ga
.ga_len
].to
= to
;
2403 ++menutrans_ga
.ga_len
;
2408 vim_free(from_noamp
);
2418 #if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
2420 * Find the character just after one part of a menu name.
2426 while (*p
!= NUL
&& *p
!= '.' && !vim_iswhite(*p
))
2428 if ((*p
== '\\' || *p
== Ctrl_V
) && p
[1] != NUL
)
2436 #ifdef FEAT_MULTI_LANG
2438 * Lookup part of a menu name in the translations.
2439 * Return a pointer to the translation or NULL if not found.
2442 menutrans_lookup(name
, len
)
2446 menutrans_T
*tp
= (menutrans_T
*)menutrans_ga
.ga_data
;
2450 for (i
= 0; i
< menutrans_ga
.ga_len
; ++i
)
2451 if (STRNCMP(name
, tp
[i
].from
, len
) == 0 && tp
[i
].from
[len
] == NUL
)
2454 /* Now try again while ignoring '&' characters. */
2457 dname
= menu_text(name
, NULL
, NULL
);
2461 for (i
= 0; i
< menutrans_ga
.ga_len
; ++i
)
2462 if (STRCMP(dname
, tp
[i
].from_noamp
) == 0)
2472 #endif /* FEAT_MULTI_LANG */
2474 #endif /* FEAT_MENU */