blob: dac4754cda7a77cf03a4610387c856188b601fe2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
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 */
10
11/*
12 * Code for menus. Used for the GUI and 'wildmenu'.
13 */
14
15#include "vim.h"
16
17#if defined(FEAT_MENU) || defined(PROTO)
18
19#define MENUDEPTH 10 /* maximum depth of menus */
20
21#ifdef FEAT_GUI_W32
22static int add_menu_path __ARGS((char_u *, vimmenu_T *, int *, char_u *, int));
23#else
24static int add_menu_path __ARGS((char_u *, vimmenu_T *, int *, char_u *));
25#endif
26static int menu_nable_recurse __ARGS((vimmenu_T *menu, char_u *name, int modes, int enable));
27static int remove_menu __ARGS((vimmenu_T **, char_u *, int, int silent));
28static void free_menu __ARGS((vimmenu_T **menup));
29static void free_menu_string __ARGS((vimmenu_T *, int));
30static int show_menus __ARGS((char_u *, int));
31static void show_menus_recursive __ARGS((vimmenu_T *, int, int));
32static int menu_name_equal __ARGS((char_u *name, vimmenu_T *menu));
33static int menu_namecmp __ARGS((char_u *name, char_u *mname));
34static int get_menu_cmd_modes __ARGS((char_u *, int, int *, int *));
35static char_u *popup_mode_name __ARGS((char_u *name, int idx));
36static char_u *menu_text __ARGS((char_u *text, int *mnemonic, char_u **actext));
37#ifdef FEAT_GUI
38static int get_menu_mode __ARGS((void));
39static void gui_update_menus_recurse __ARGS((vimmenu_T *, int));
40#endif
41
42#if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
43static void gui_create_tearoffs_recurse __ARGS((vimmenu_T *menu, const char_u *pname, int *pri_tab, int pri_idx));
44static void gui_add_tearoff __ARGS((char_u *tearpath, int *pri_tab, int pri_idx));
45static void gui_destroy_tearoffs_recurse __ARGS((vimmenu_T *menu));
46static int s_tearoffs = FALSE;
47#endif
48
49static int menu_is_hidden __ARGS((char_u *name));
50#if defined(FEAT_CMDL_COMPL) || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
51static int menu_is_tearoff __ARGS((char_u *name));
52#endif
53
54#if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
55static char_u *menu_skip_part __ARGS((char_u *p));
56#endif
57#ifdef FEAT_MULTI_LANG
58static char_u *menutrans_lookup __ARGS((char_u *name, int len));
59#endif
60
61/* The character for each menu mode */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000062static char_u menu_mode_chars[] = {'n', 'v', 's', 'o', 'i', 'c', 't'};
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64static char_u e_notsubmenu[] = N_("E327: Part of menu-item path is not sub-menu");
65static char_u e_othermode[] = N_("E328: Menu only exists in another mode");
Bram Moolenaar342337a2005-07-21 21:11:17 +000066static char_u e_nomenu[] = N_("E329: No menu \"%s\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68#ifdef FEAT_TOOLBAR
69static const char *toolbar_names[] =
70{
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"
78};
79# define TOOLBAR_NAME_COUNT (sizeof(toolbar_names) / sizeof(char *))
80#endif
81
82/*
83 * Do the :menu command and relatives.
84 */
85 void
86ex_menu(eap)
87 exarg_T *eap; /* Ex command arguments */
88{
89 char_u *menu_path;
90 int modes;
91 char_u *map_to;
92 int noremap;
93 int silent = FALSE;
94 int unmenu;
95 char_u *map_buf;
96 char_u *arg;
97 char_u *p;
98 int i;
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000099#if defined(FEAT_GUI) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 int old_menu_height;
101# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
102 int old_toolbar_height;
103# endif
104#endif
105 int pri_tab[MENUDEPTH + 1];
106 int enable = MAYBE; /* TRUE for "menu enable", FALSE for "menu
107 * disable */
108#ifdef FEAT_MULTI_LANG
109 char_u *tofree = NULL;
110 char_u *new_cmd;
111#endif
112#ifdef FEAT_TOOLBAR
113 char_u *icon = NULL;
114#endif
115 vimmenu_T menuarg;
116
117 modes = get_menu_cmd_modes(eap->cmd, eap->forceit, &noremap, &unmenu);
118 arg = eap->arg;
119
120 for (;;)
121 {
122 if (STRNCMP(arg, "<script>", 8) == 0)
123 {
124 noremap = REMAP_SCRIPT;
125 arg = skipwhite(arg + 8);
126 continue;
127 }
128 if (STRNCMP(arg, "<silent>", 8) == 0)
129 {
130 silent = TRUE;
131 arg = skipwhite(arg + 8);
132 continue;
133 }
134 break;
135 }
136
137
138 /* Locate an optional "icon=filename" argument. */
139 if (STRNCMP(arg, "icon=", 5) == 0)
140 {
141 arg += 5;
142#ifdef FEAT_TOOLBAR
143 icon = arg;
144#endif
145 while (*arg != NUL && *arg != ' ')
146 {
147 if (*arg == '\\')
148 mch_memmove(arg, arg + 1, STRLEN(arg));
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000149 mb_ptr_adv(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 }
151 if (*arg != NUL)
152 {
153 *arg++ = NUL;
154 arg = skipwhite(arg);
155 }
156 }
157
158 /*
159 * Fill in the priority table.
160 */
161 for (p = arg; *p; ++p)
162 if (!VIM_ISDIGIT(*p) && *p != '.')
163 break;
164 if (vim_iswhite(*p))
165 {
166 for (i = 0; i < MENUDEPTH && !vim_iswhite(*arg); ++i)
167 {
168 pri_tab[i] = getdigits(&arg);
169 if (pri_tab[i] == 0)
170 pri_tab[i] = 500;
171 if (*arg == '.')
172 ++arg;
173 }
174 arg = skipwhite(arg);
175 }
176 else if (eap->addr_count && eap->line2 != 0)
177 {
178 pri_tab[0] = eap->line2;
179 i = 1;
180 }
181 else
182 i = 0;
183 while (i < MENUDEPTH)
184 pri_tab[i++] = 500;
185 pri_tab[MENUDEPTH] = -1; /* mark end of the table */
186
187 /*
188 * Check for "disable" or "enable" argument.
189 */
190 if (STRNCMP(arg, "enable", 6) == 0 && vim_iswhite(arg[6]))
191 {
192 enable = TRUE;
193 arg = skipwhite(arg + 6);
194 }
195 else if (STRNCMP(arg, "disable", 7) == 0 && vim_iswhite(arg[7]))
196 {
197 enable = FALSE;
198 arg = skipwhite(arg + 7);
199 }
200
201 /*
202 * If there is no argument, display all menus.
203 */
204 if (*arg == NUL)
205 {
206 show_menus(arg, modes);
207 return;
208 }
209
210#ifdef FEAT_TOOLBAR
211 /*
212 * Need to get the toolbar icon index before doing the translation.
213 */
214 menuarg.iconidx = -1;
215 menuarg.icon_builtin = FALSE;
216 if (menu_is_toolbar(arg))
217 {
218 menu_path = menu_skip_part(arg);
219 if (*menu_path == '.')
220 {
221 p = menu_skip_part(++menu_path);
222 if (STRNCMP(menu_path, "BuiltIn", 7) == 0)
223 {
224 if (skipdigits(menu_path + 7) == p)
225 {
226 menuarg.iconidx = atoi((char *)menu_path + 7);
227 if (menuarg.iconidx >= TOOLBAR_NAME_COUNT)
228 menuarg.iconidx = -1;
229 else
230 menuarg.icon_builtin = TRUE;
231 }
232 }
233 else
234 {
235 for (i = 0; i < TOOLBAR_NAME_COUNT; ++i)
236 if (STRNCMP(toolbar_names[i], menu_path, p - menu_path)
237 == 0)
238 {
239 menuarg.iconidx = i;
240 break;
241 }
242 }
243 }
244 }
245#endif
246
247#ifdef FEAT_MULTI_LANG
248 /*
249 * Translate menu names as specified with ":menutrans" commands.
250 */
251 menu_path = arg;
252 while (*menu_path)
253 {
254 /* find the end of one part and check if it should be translated */
255 p = menu_skip_part(menu_path);
256 map_to = menutrans_lookup(menu_path, (int)(p - menu_path));
257 if (map_to != NULL)
258 {
259 /* found a match: replace with the translated part */
260 i = (int)STRLEN(map_to);
261 new_cmd = alloc((unsigned)STRLEN(arg) + i + 1);
262 if (new_cmd == NULL)
263 break;
264 mch_memmove(new_cmd, arg, menu_path - arg);
265 mch_memmove(new_cmd + (menu_path - arg), map_to, (size_t)i);
266 STRCPY(new_cmd + (menu_path - arg) + i, p);
267 p = new_cmd + (menu_path - arg) + i;
268 vim_free(tofree);
269 tofree = new_cmd;
270 arg = new_cmd;
271 }
272 if (*p != '.')
273 break;
274 menu_path = p + 1;
275 }
276#endif
277
278 /*
279 * Isolate the menu name.
280 * Skip the menu name, and translate <Tab> into a real TAB.
281 */
282 menu_path = arg;
283 if (*menu_path == '.')
284 {
285 EMSG2(_(e_invarg2), menu_path);
286 goto theend;
287 }
288
289 while (*arg && !vim_iswhite(*arg))
290 {
291 if ((*arg == '\\' || *arg == Ctrl_V) && arg[1] != NUL)
292 arg++;
293 else if (STRNICMP(arg, "<TAB>", 5) == 0)
294 {
295 *arg = TAB;
296 mch_memmove(arg + 1, arg + 5, STRLEN(arg + 4));
297 }
298 arg++;
299 }
300 if (*arg != NUL)
301 *arg++ = NUL;
302 arg = skipwhite(arg);
303 map_to = arg;
304
305 /*
306 * If there is only a menu name, display menus with that name.
307 */
308 if (*map_to == NUL && !unmenu && enable == MAYBE)
309 {
310 show_menus(menu_path, modes);
311 goto theend;
312 }
313 else if (*map_to != NUL && (unmenu || enable != MAYBE))
314 {
315 EMSG(_(e_trailing));
316 goto theend;
317 }
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000318#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 old_menu_height = gui.menu_height;
320# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
321 old_toolbar_height = gui.toolbar_height;
322# endif
323#endif
324
325 if (enable != MAYBE)
326 {
327 /*
328 * Change sensitivity of the menu.
329 * For the PopUp menu, remove a menu for each mode separately.
330 * Careful: menu_nable_recurse() changes menu_path.
331 */
332 if (STRCMP(menu_path, "*") == 0) /* meaning: do all menus */
333 menu_path = (char_u *)"";
334
335 if (menu_is_popup(menu_path))
336 {
337 for (i = 0; i < MENU_INDEX_TIP; ++i)
338 if (modes & (1 << i))
339 {
340 p = popup_mode_name(menu_path, i);
341 if (p != NULL)
342 {
343 menu_nable_recurse(root_menu, p, MENU_ALL_MODES,
344 enable);
345 vim_free(p);
346 }
347 }
348 }
349 menu_nable_recurse(root_menu, menu_path, modes, enable);
350 }
351 else if (unmenu)
352 {
353 /*
354 * Delete menu(s).
355 */
356 if (STRCMP(menu_path, "*") == 0) /* meaning: remove all menus */
357 menu_path = (char_u *)"";
358
359 /*
360 * For the PopUp menu, remove a menu for each mode separately.
361 */
362 if (menu_is_popup(menu_path))
363 {
364 for (i = 0; i < MENU_INDEX_TIP; ++i)
365 if (modes & (1 << i))
366 {
367 p = popup_mode_name(menu_path, i);
368 if (p != NULL)
369 {
370 remove_menu(&root_menu, p, MENU_ALL_MODES, TRUE);
371 vim_free(p);
372 }
373 }
374 }
375
376 /* Careful: remove_menu() changes menu_path */
377 remove_menu(&root_menu, menu_path, modes, FALSE);
378 }
379 else
380 {
381 /*
382 * Add menu(s).
383 * Replace special key codes.
384 */
385 if (STRICMP(map_to, "<nop>") == 0) /* "<Nop>" means nothing */
386 {
387 map_to = (char_u *)"";
388 map_buf = NULL;
389 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000390 else if (modes & MENU_TIP_MODE)
391 map_buf = NULL; /* Menu tips are plain text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 else
393 map_to = replace_termcodes(map_to, &map_buf, FALSE, TRUE);
394 menuarg.modes = modes;
395#ifdef FEAT_TOOLBAR
396 menuarg.iconfile = icon;
397#endif
398 menuarg.noremap[0] = noremap;
399 menuarg.silent[0] = silent;
400 add_menu_path(menu_path, &menuarg, pri_tab, map_to
401#ifdef FEAT_GUI_W32
402 , TRUE
403#endif
404 );
405
406 /*
407 * For the PopUp menu, add a menu for each mode separately.
408 */
409 if (menu_is_popup(menu_path))
410 {
411 for (i = 0; i < MENU_INDEX_TIP; ++i)
412 if (modes & (1 << i))
413 {
414 p = popup_mode_name(menu_path, i);
415 if (p != NULL)
416 {
417 /* Include all modes, to make ":amenu" work */
418 menuarg.modes = modes;
419#ifdef FEAT_TOOLBAR
420 menuarg.iconfile = NULL;
421 menuarg.iconidx = -1;
422 menuarg.icon_builtin = FALSE;
423#endif
424 add_menu_path(p, &menuarg, pri_tab, map_to
425#ifdef FEAT_GUI_W32
426 , TRUE
427#endif
428 );
429 vim_free(p);
430 }
431 }
432 }
433
434 vim_free(map_buf);
435 }
436
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000437#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /* If the menubar height changed, resize the window */
439 if (gui.in_use
440 && (gui.menu_height != old_menu_height
441# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
442 || gui.toolbar_height != old_toolbar_height
443# endif
444 ))
Bram Moolenaar04a9d452006-03-27 21:03:26 +0000445 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446#endif
447
448theend:
449#ifdef FEAT_MULTI_LANG
450 vim_free(tofree);
451#else
452 ;
453#endif
454}
455
456/*
457 * Add the menu with the given name to the menu hierarchy
458 */
459 static int
460add_menu_path(menu_path, menuarg, pri_tab, call_data
461#ifdef FEAT_GUI_W32
462 , addtearoff
463#endif
464 )
465 char_u *menu_path;
466 vimmenu_T *menuarg; /* passes modes, iconfile, iconidx,
467 icon_builtin, silent[0], noremap[0] */
468 int *pri_tab;
469 char_u *call_data;
470#ifdef FEAT_GUI_W32
471 int addtearoff; /* may add tearoff item */
472#endif
473{
474 char_u *path_name;
475 int modes = menuarg->modes;
476 vimmenu_T **menup;
477 vimmenu_T *menu = NULL;
478 vimmenu_T *parent;
479 vimmenu_T **lower_pri;
480 char_u *p;
481 char_u *name;
482 char_u *dname;
483 char_u *next_name;
484 int i;
485 int c;
486#ifdef FEAT_GUI
487 int idx;
488 int new_idx;
489#endif
490 int pri_idx = 0;
491 int old_modes = 0;
492 int amenu;
493
494 /* Make a copy so we can stuff around with it, since it could be const */
495 path_name = vim_strsave(menu_path);
496 if (path_name == NULL)
497 return FAIL;
498 menup = &root_menu;
499 parent = NULL;
500 name = path_name;
501 while (*name)
502 {
503 /* Get name of this element in the menu hierarchy, and the simplified
504 * name (without mnemonic and accelerator text). */
505 next_name = menu_name_skip(name);
506 dname = menu_text(name, NULL, NULL);
507
508 /* See if it's already there */
509 lower_pri = menup;
510#ifdef FEAT_GUI
511 idx = 0;
512 new_idx = 0;
513#endif
514 menu = *menup;
515 while (menu != NULL)
516 {
517 if (menu_name_equal(name, menu) || menu_name_equal(dname, menu))
518 {
519 if (*next_name == NUL && menu->children != NULL)
520 {
521 if (!sys_menu)
522 EMSG(_("E330: Menu path must not lead to a sub-menu"));
523 goto erret;
524 }
525 if (*next_name != NUL && menu->children == NULL
526#ifdef FEAT_GUI_W32
527 && addtearoff
528#endif
529 )
530 {
531 if (!sys_menu)
532 EMSG(_(e_notsubmenu));
533 goto erret;
534 }
535 break;
536 }
537 menup = &menu->next;
538
539 /* Count menus, to find where this one needs to be inserted.
540 * Ignore menus that are not in the menubar (PopUp and Toolbar) */
541 if (parent != NULL || menu_is_menubar(menu->name))
542 {
543#ifdef FEAT_GUI
544 ++idx;
545#endif
546 if (menu->priority <= pri_tab[pri_idx])
547 {
548 lower_pri = menup;
549#ifdef FEAT_GUI
550 new_idx = idx;
551#endif
552 }
553 }
554 menu = menu->next;
555 }
556
557 if (menu == NULL)
558 {
559 if (*next_name == NUL && parent == NULL)
560 {
561 EMSG(_("E331: Must not add menu items directly to menu bar"));
562 goto erret;
563 }
564
565 if (menu_is_separator(dname) && *next_name != NUL)
566 {
567 EMSG(_("E332: Separator cannot be part of a menu path"));
568 goto erret;
569 }
570
571 /* Not already there, so lets add it */
572 menu = (vimmenu_T *)alloc_clear((unsigned)sizeof(vimmenu_T));
573 if (menu == NULL)
574 goto erret;
575
576 menu->modes = modes;
577 menu->enabled = MENU_ALL_MODES;
578 menu->name = vim_strsave(name);
579 /* separate mnemonic and accelerator text from actual menu name */
580 menu->dname = menu_text(name, &menu->mnemonic, &menu->actext);
581 menu->priority = pri_tab[pri_idx];
582 menu->parent = parent;
583#ifdef FEAT_GUI_MOTIF
584 menu->sensitive = TRUE; /* the default */
585#endif
586#ifdef FEAT_BEVAL_TIP
587 menu->tip = NULL;
588#endif
589#ifdef FEAT_GUI_ATHENA
590 menu->image = None; /* X-Windows definition for NULL*/
591#endif
592
593 /*
594 * Add after menu that has lower priority.
595 */
596 menu->next = *lower_pri;
597 *lower_pri = menu;
598
599 old_modes = 0;
600
601#ifdef FEAT_TOOLBAR
602 menu->iconidx = menuarg->iconidx;
603 menu->icon_builtin = menuarg->icon_builtin;
604 if (*next_name == NUL && menuarg->iconfile != NULL)
605 menu->iconfile = vim_strsave(menuarg->iconfile);
606#endif
607#if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
608 /* the tearoff item must be present in the modes of each item. */
609 if (parent != NULL && menu_is_tearoff(parent->children->dname))
610 parent->children->modes |= modes;
611#endif
612 }
613 else
614 {
615 old_modes = menu->modes;
616
617 /*
618 * If this menu option was previously only available in other
619 * modes, then make sure it's available for this one now
620 * Also enable a menu when it's created or changed.
621 */
622#ifdef FEAT_GUI_W32
623 /* If adding a tearbar (addtearoff == FALSE) don't update modes */
624 if (addtearoff)
625#endif
626 {
627 menu->modes |= modes;
628 menu->enabled |= modes;
629 }
630 }
631
632#ifdef FEAT_GUI
633 /*
634 * Add the menu item when it's used in one of the modes, but not when
635 * only a tooltip is defined.
636 */
637 if ((old_modes & MENU_ALL_MODES) == 0
638 && (menu->modes & MENU_ALL_MODES) != 0)
639 {
640 if (gui.in_use) /* Otherwise it will be added when GUI starts */
641 {
642 if (*next_name == NUL)
643 {
644 /* Real menu item, not sub-menu */
645 gui_mch_add_menu_item(menu, new_idx);
646
647 /* Want to update menus now even if mode not changed */
648 force_menu_update = TRUE;
649 }
650 else
651 {
652 /* Sub-menu (not at end of path yet) */
653 gui_mch_add_menu(menu, new_idx);
654 }
655 }
656
657# if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
658 /* When adding a new submenu, may add a tearoff item */
659 if ( addtearoff
660 && *next_name
661 && vim_strchr(p_go, GO_TEAROFF) != NULL
662 && menu_is_menubar(name))
663 {
664 char_u *tearpath;
665
666 /*
667 * The pointers next_name & path_name refer to a string with
668 * \'s and ^V's stripped out. But menu_path is a "raw"
669 * string, so we must correct for special characters.
670 */
671 tearpath = alloc((unsigned int)STRLEN(menu_path) + TEAR_LEN + 2);
672 if (tearpath != NULL)
673 {
674 char_u *s;
675 int idx;
676
677 STRCPY(tearpath, menu_path);
678 idx = (int)(next_name - path_name - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000679 for (s = tearpath; *s && s < tearpath + idx; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
681 if ((*s == '\\' || *s == Ctrl_V) && s[1])
682 {
683 ++idx;
684 ++s;
685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 }
687 tearpath[idx] = NUL;
688 gui_add_tearoff(tearpath, pri_tab, pri_idx);
689 vim_free(tearpath);
690 }
691 }
692# endif
693 }
694#endif /* FEAT_GUI */
695
696 menup = &menu->children;
697 parent = menu;
698 name = next_name;
699 vim_free(dname);
700 if (pri_tab[pri_idx + 1] != -1)
701 ++pri_idx;
702 }
703 vim_free(path_name);
704
705 /*
706 * Only add system menu items which have not been defined yet.
707 * First check if this was an ":amenu".
708 */
709 amenu = ((modes & (MENU_NORMAL_MODE | MENU_INSERT_MODE)) ==
710 (MENU_NORMAL_MODE | MENU_INSERT_MODE));
711 if (sys_menu)
712 modes &= ~old_modes;
713
714 if (menu != NULL && modes)
715 {
716#ifdef FEAT_GUI
717 menu->cb = gui_menu_cb;
718#endif
719 p = (call_data == NULL) ? NULL : vim_strsave(call_data);
720
721 /* loop over all modes, may add more than one */
722 for (i = 0; i < MENU_MODES; ++i)
723 {
724 if (modes & (1 << i))
725 {
726 /* free any old menu */
727 free_menu_string(menu, i);
728
729 /* For "amenu", may insert an extra character.
730 * Don't do this if adding a tearbar (addtearoff == FALSE).
731 * Don't do this for "<Nop>". */
732 c = 0;
733 if (amenu && call_data != NULL && *call_data != NUL
734#ifdef FEAT_GUI_W32
735 && addtearoff
736#endif
737 )
738 {
739 switch (1 << i)
740 {
741 case MENU_VISUAL_MODE:
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000742 case MENU_SELECT_MODE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 case MENU_OP_PENDING_MODE:
744 case MENU_CMDLINE_MODE:
745 c = Ctrl_C;
746 break;
747 case MENU_INSERT_MODE:
748 c = Ctrl_O;
749 break;
750 }
751 }
752
753 if (c)
754 {
755 menu->strings[i] = alloc((unsigned)(STRLEN(call_data) + 4));
756 if (menu->strings[i] != NULL)
757 {
758 menu->strings[i][0] = c;
759 STRCPY(menu->strings[i] + 1, call_data);
760 if (c == Ctrl_C)
761 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000762 int len = (int)STRLEN(menu->strings[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
764 /* Append CTRL-\ CTRL-G to obey 'insertmode'. */
765 menu->strings[i][len] = Ctrl_BSL;
766 menu->strings[i][len + 1] = Ctrl_G;
767 menu->strings[i][len + 2] = NUL;
768 }
769 }
770 }
771 else
772 menu->strings[i] = p;
773 menu->noremap[i] = menuarg->noremap[0];
774 menu->silent[i] = menuarg->silent[0];
775 }
776 }
777#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
778 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
779 /* Need to update the menu tip. */
780 if (modes & MENU_TIP_MODE)
781 gui_mch_menu_set_tip(menu);
782#endif
783 }
784 return OK;
785
786erret:
787 vim_free(path_name);
788 vim_free(dname);
789 return FAIL;
790}
791
792/*
793 * Set the (sub)menu with the given name to enabled or disabled.
794 * Called recursively.
795 */
796 static int
797menu_nable_recurse(menu, name, modes, enable)
798 vimmenu_T *menu;
799 char_u *name;
800 int modes;
801 int enable;
802{
803 char_u *p;
804
805 if (menu == NULL)
806 return OK; /* Got to bottom of hierarchy */
807
808 /* Get name of this element in the menu hierarchy */
809 p = menu_name_skip(name);
810
811 /* Find the menu */
812 while (menu != NULL)
813 {
814 if (*name == NUL || *name == '*' || menu_name_equal(name, menu))
815 {
816 if (*p != NUL)
817 {
818 if (menu->children == NULL)
819 {
820 EMSG(_(e_notsubmenu));
821 return FAIL;
822 }
823 if (menu_nable_recurse(menu->children, p, modes, enable)
824 == FAIL)
825 return FAIL;
826 }
827 else
828 if (enable)
829 menu->enabled |= modes;
830 else
831 menu->enabled &= ~modes;
832
833 /*
834 * When name is empty, we are doing all menu items for the given
835 * modes, so keep looping, otherwise we are just doing the named
836 * menu item (which has been found) so break here.
837 */
838 if (*name != NUL && *name != '*')
839 break;
840 }
841 menu = menu->next;
842 }
843 if (*name != NUL && *name != '*' && menu == NULL)
844 {
Bram Moolenaar342337a2005-07-21 21:11:17 +0000845 EMSG2(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 return FAIL;
847 }
848
849#ifdef FEAT_GUI
850 /* Want to update menus now even if mode not changed */
851 force_menu_update = TRUE;
852#endif
853
854 return OK;
855}
856
857/*
858 * Remove the (sub)menu with the given name from the menu hierarchy
859 * Called recursively.
860 */
861 static int
862remove_menu(menup, name, modes, silent)
863 vimmenu_T **menup;
864 char_u *name;
865 int modes;
866 int silent; /* don't give error messages */
867{
868 vimmenu_T *menu;
869 vimmenu_T *child;
870 char_u *p;
871
872 if (*menup == NULL)
873 return OK; /* Got to bottom of hierarchy */
874
875 /* Get name of this element in the menu hierarchy */
876 p = menu_name_skip(name);
877
878 /* Find the menu */
879 while ((menu = *menup) != NULL)
880 {
881 if (*name == NUL || menu_name_equal(name, menu))
882 {
883 if (*p != NUL && menu->children == NULL)
884 {
885 if (!silent)
886 EMSG(_(e_notsubmenu));
887 return FAIL;
888 }
889 if ((menu->modes & modes) != 0x0)
890 {
891#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
892 /*
893 * If we are removing all entries for this menu,MENU_ALL_MODES,
894 * Then kill any tearoff before we start
895 */
896 if (*p == NUL && modes == MENU_ALL_MODES)
897 {
898 if (IsWindow(menu->tearoff_handle))
899 DestroyWindow(menu->tearoff_handle);
900 }
901#endif
902 if (remove_menu(&menu->children, p, modes, silent) == FAIL)
903 return FAIL;
904 }
905 else if (*name != NUL)
906 {
907 if (!silent)
908 EMSG(_(e_othermode));
909 return FAIL;
910 }
911
912 /*
913 * When name is empty, we are removing all menu items for the given
914 * modes, so keep looping, otherwise we are just removing the named
915 * menu item (which has been found) so break here.
916 */
917 if (*name != NUL)
918 break;
919
920 /* Remove the menu item for the given mode[s]. If the menu item
921 * is no longer valid in ANY mode, delete it */
922 menu->modes &= ~modes;
923 if (modes & MENU_TIP_MODE)
924 free_menu_string(menu, MENU_INDEX_TIP);
925 if ((menu->modes & MENU_ALL_MODES) == 0)
926 free_menu(menup);
927 else
928 menup = &menu->next;
929 }
930 else
931 menup = &menu->next;
932 }
933 if (*name != NUL)
934 {
935 if (menu == NULL)
936 {
937 if (!silent)
Bram Moolenaar342337a2005-07-21 21:11:17 +0000938 EMSG2(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 return FAIL;
940 }
941
942
943 /* Recalculate modes for menu based on the new updated children */
944 menu->modes &= ~modes;
945#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
946 if ((s_tearoffs) && (menu->children != NULL)) /* there's a tear bar.. */
947 child = menu->children->next; /* don't count tearoff bar */
948 else
949#endif
950 child = menu->children;
951 for ( ; child != NULL; child = child->next)
952 menu->modes |= child->modes;
953 if (modes & MENU_TIP_MODE)
954 {
955 free_menu_string(menu, MENU_INDEX_TIP);
956#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
957 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
958 /* Need to update the menu tip. */
959 if (gui.in_use)
960 gui_mch_menu_set_tip(menu);
961#endif
962 }
963 if ((menu->modes & MENU_ALL_MODES) == 0)
964 {
965 /* The menu item is no longer valid in ANY mode, so delete it */
966#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
967 if (s_tearoffs && menu->children != NULL) /* there's a tear bar.. */
968 free_menu(&menu->children);
969#endif
970 *menup = menu;
971 free_menu(menup);
972 }
973 }
974
975 return OK;
976}
977
978/*
979 * Free the given menu structure and remove it from the linked list.
980 */
981 static void
982free_menu(menup)
983 vimmenu_T **menup;
984{
985 int i;
986 vimmenu_T *menu;
987
988 menu = *menup;
989
990#ifdef FEAT_GUI
991 /* Free machine specific menu structures (only when already created) */
992 /* Also may rebuild a tearoff'ed menu */
993 if (gui.in_use)
994 gui_mch_destroy_menu(menu);
995#endif
996
997 /* Don't change *menup until after calling gui_mch_destroy_menu(). The
998 * MacOS code needs the original structure to properly delete the menu. */
999 *menup = menu->next;
1000 vim_free(menu->name);
1001 vim_free(menu->dname);
1002 vim_free(menu->actext);
1003#ifdef FEAT_TOOLBAR
1004 vim_free(menu->iconfile);
Bram Moolenaarbee0c5b2005-02-07 22:03:36 +00001005# ifdef FEAT_GUI_MOTIF
1006 vim_free(menu->xpm_fname);
1007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#endif
1009 for (i = 0; i < MENU_MODES; i++)
1010 free_menu_string(menu, i);
1011 vim_free(menu);
1012
1013#ifdef FEAT_GUI
1014 /* Want to update menus now even if mode not changed */
1015 force_menu_update = TRUE;
1016#endif
1017}
1018
1019/*
1020 * Free the menu->string with the given index.
1021 */
1022 static void
1023free_menu_string(menu, idx)
1024 vimmenu_T *menu;
1025 int idx;
1026{
1027 int count = 0;
1028 int i;
1029
1030 for (i = 0; i < MENU_MODES; i++)
1031 if (menu->strings[i] == menu->strings[idx])
1032 count++;
1033 if (count == 1)
1034 vim_free(menu->strings[idx]);
1035 menu->strings[idx] = NULL;
1036}
1037
1038/*
1039 * Show the mapping associated with a menu item or hierarchy in a sub-menu.
1040 */
1041 static int
1042show_menus(path_name, modes)
1043 char_u *path_name;
1044 int modes;
1045{
1046 char_u *p;
1047 char_u *name;
1048 vimmenu_T *menu;
1049 vimmenu_T *parent = NULL;
1050
1051 menu = root_menu;
1052 name = path_name = vim_strsave(path_name);
1053 if (path_name == NULL)
1054 return FAIL;
1055
1056 /* First, find the (sub)menu with the given name */
1057 while (*name)
1058 {
1059 p = menu_name_skip(name);
1060 while (menu != NULL)
1061 {
1062 if (menu_name_equal(name, menu))
1063 {
1064 /* Found menu */
1065 if (*p != NUL && menu->children == NULL)
1066 {
1067 EMSG(_(e_notsubmenu));
1068 vim_free(path_name);
1069 return FAIL;
1070 }
1071 else if ((menu->modes & modes) == 0x0)
1072 {
1073 EMSG(_(e_othermode));
1074 vim_free(path_name);
1075 return FAIL;
1076 }
1077 break;
1078 }
1079 menu = menu->next;
1080 }
1081 if (menu == NULL)
1082 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00001083 EMSG2(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 vim_free(path_name);
1085 return FAIL;
1086 }
1087 name = p;
1088 parent = menu;
1089 menu = menu->children;
1090 }
1091
1092 /* Now we have found the matching menu, and we list the mappings */
1093 /* Highlight title */
1094 MSG_PUTS_TITLE(_("\n--- Menus ---"));
1095
1096 show_menus_recursive(parent, modes, 0);
1097 return OK;
1098}
1099
1100/*
1101 * Recursively show the mappings associated with the menus under the given one
1102 */
1103 static void
1104show_menus_recursive(menu, modes, depth)
1105 vimmenu_T *menu;
1106 int modes;
1107 int depth;
1108{
1109 int i;
1110 int bit;
1111
1112 if (menu != NULL && (menu->modes & modes) == 0x0)
1113 return;
1114
1115 if (menu != NULL)
1116 {
1117 msg_putchar('\n');
1118 if (got_int) /* "q" hit for "--more--" */
1119 return;
1120 for (i = 0; i < depth; i++)
1121 MSG_PUTS(" ");
1122 if (menu->priority)
1123 {
1124 msg_outnum((long)menu->priority);
1125 MSG_PUTS(" ");
1126 }
1127 /* Same highlighting as for directories!? */
1128 msg_outtrans_attr(menu->name, hl_attr(HLF_D));
1129 }
1130
1131 if (menu != NULL && menu->children == NULL)
1132 {
1133 for (bit = 0; bit < MENU_MODES; bit++)
1134 if ((menu->modes & modes & (1 << bit)) != 0)
1135 {
1136 msg_putchar('\n');
1137 if (got_int) /* "q" hit for "--more--" */
1138 return;
1139 for (i = 0; i < depth + 2; i++)
1140 MSG_PUTS(" ");
1141 msg_putchar(menu_mode_chars[bit]);
1142 if (menu->noremap[bit] == REMAP_NONE)
1143 msg_putchar('*');
1144 else if (menu->noremap[bit] == REMAP_SCRIPT)
1145 msg_putchar('&');
1146 else
1147 msg_putchar(' ');
1148 if (menu->silent[bit])
1149 msg_putchar('s');
1150 else
1151 msg_putchar(' ');
1152 if ((menu->modes & menu->enabled & (1 << bit)) == 0)
1153 msg_putchar('-');
1154 else
1155 msg_putchar(' ');
1156 MSG_PUTS(" ");
1157 if (*menu->strings[bit] == NUL)
1158 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
1159 else
1160 msg_outtrans_special(menu->strings[bit], FALSE);
1161 }
1162 }
1163 else
1164 {
1165 if (menu == NULL)
1166 {
1167 menu = root_menu;
1168 depth--;
1169 }
1170 else
1171 menu = menu->children;
1172
1173 /* recursively show all children. Skip PopUp[nvoci]. */
1174 for (; menu != NULL && !got_int; menu = menu->next)
1175 if (!menu_is_hidden(menu->dname))
1176 show_menus_recursive(menu, modes, depth + 1);
1177 }
1178}
1179
1180#ifdef FEAT_CMDL_COMPL
1181
1182/*
1183 * Used when expanding menu names.
1184 */
1185static vimmenu_T *expand_menu = NULL;
1186static int expand_modes = 0x0;
1187static int expand_emenu; /* TRUE for ":emenu" command */
1188
1189/*
1190 * Work out what to complete when doing command line completion of menu names.
1191 */
1192 char_u *
1193set_context_in_menu_cmd(xp, cmd, arg, forceit)
1194 expand_T *xp;
1195 char_u *cmd;
1196 char_u *arg;
1197 int forceit;
1198{
1199 char_u *after_dot;
1200 char_u *p;
1201 char_u *path_name = NULL;
1202 char_u *name;
1203 int unmenu;
1204 vimmenu_T *menu;
1205 int expand_menus;
1206
1207 xp->xp_context = EXPAND_UNSUCCESSFUL;
1208
1209
1210 /* Check for priority numbers, enable and disable */
1211 for (p = arg; *p; ++p)
1212 if (!VIM_ISDIGIT(*p) && *p != '.')
1213 break;
1214
1215 if (!vim_iswhite(*p))
1216 {
1217 if (STRNCMP(arg, "enable", 6) == 0
1218 && (arg[6] == NUL || vim_iswhite(arg[6])))
1219 p = arg + 6;
1220 else if (STRNCMP(arg, "disable", 7) == 0
1221 && (arg[7] == NUL || vim_iswhite(arg[7])))
1222 p = arg + 7;
1223 else
1224 p = arg;
1225 }
1226
1227 while (*p != NUL && vim_iswhite(*p))
1228 ++p;
1229
1230 arg = after_dot = p;
1231
1232 for (; *p && !vim_iswhite(*p); ++p)
1233 {
1234 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
1235 p++;
1236 else if (*p == '.')
1237 after_dot = p + 1;
1238 }
1239
1240 /* ":tearoff" and ":popup" only use menus, not entries */
1241 expand_menus = !((*cmd == 't' && cmd[1] == 'e') || *cmd == 'p');
1242 expand_emenu = (*cmd == 'e');
1243 if (expand_menus && vim_iswhite(*p))
1244 return NULL; /* TODO: check for next command? */
1245 if (*p == NUL) /* Complete the menu name */
1246 {
1247 /*
1248 * With :unmenu, you only want to match menus for the appropriate mode.
1249 * With :menu though you might want to add a menu with the same name as
1250 * one in another mode, so match menus from other modes too.
1251 */
1252 expand_modes = get_menu_cmd_modes(cmd, forceit, NULL, &unmenu);
1253 if (!unmenu)
1254 expand_modes = MENU_ALL_MODES;
1255
1256 menu = root_menu;
1257 if (after_dot != arg)
1258 {
1259 path_name = alloc((unsigned)(after_dot - arg));
1260 if (path_name == NULL)
1261 return NULL;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001262 vim_strncpy(path_name, arg, after_dot - arg - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264 name = path_name;
1265 while (name != NULL && *name)
1266 {
1267 p = menu_name_skip(name);
1268 while (menu != NULL)
1269 {
1270 if (menu_name_equal(name, menu))
1271 {
1272 /* Found menu */
1273 if ((*p != NUL && menu->children == NULL)
1274 || ((menu->modes & expand_modes) == 0x0))
1275 {
1276 /*
1277 * Menu path continues, but we have reached a leaf.
1278 * Or menu exists only in another mode.
1279 */
1280 vim_free(path_name);
1281 return NULL;
1282 }
1283 break;
1284 }
1285 menu = menu->next;
1286 }
1287 if (menu == NULL)
1288 {
1289 /* No menu found with the name we were looking for */
1290 vim_free(path_name);
1291 return NULL;
1292 }
1293 name = p;
1294 menu = menu->children;
1295 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001296 vim_free(path_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298 xp->xp_context = expand_menus ? EXPAND_MENUNAMES : EXPAND_MENUS;
1299 xp->xp_pattern = after_dot;
1300 expand_menu = menu;
1301 }
1302 else /* We're in the mapping part */
1303 xp->xp_context = EXPAND_NOTHING;
1304 return NULL;
1305}
1306
1307/*
1308 * Function given to ExpandGeneric() to obtain the list of (sub)menus (not
1309 * entries).
1310 */
1311/*ARGSUSED*/
1312 char_u *
1313get_menu_name(xp, idx)
1314 expand_T *xp;
1315 int idx;
1316{
1317 static vimmenu_T *menu = NULL;
1318 char_u *str;
1319
1320 if (idx == 0) /* first call: start at first item */
1321 menu = expand_menu;
1322
1323 /* Skip PopUp[nvoci]. */
1324 while (menu != NULL && (menu_is_hidden(menu->dname)
1325 || menu_is_separator(menu->dname)
1326 || menu_is_tearoff(menu->dname)
1327 || menu->children == NULL))
1328 menu = menu->next;
1329
1330 if (menu == NULL) /* at end of linked list */
1331 return NULL;
1332
1333 if (menu->modes & expand_modes)
1334 str = menu->dname;
1335 else
1336 str = (char_u *)"";
1337
1338 /* Advance to next menu entry. */
1339 menu = menu->next;
1340
1341 return str;
1342}
1343
1344/*
1345 * Function given to ExpandGeneric() to obtain the list of menus and menu
1346 * entries.
1347 */
1348/*ARGSUSED*/
1349 char_u *
1350get_menu_names(xp, idx)
1351 expand_T *xp;
1352 int idx;
1353{
1354 static vimmenu_T *menu = NULL;
1355 static char_u tbuffer[256]; /*hack*/
1356 char_u *str;
1357
1358 if (idx == 0) /* first call: start at first item */
1359 menu = expand_menu;
1360
1361 /* Skip Browse-style entries, popup menus and separators. */
1362 while (menu != NULL
1363 && ( menu_is_hidden(menu->dname)
1364 || (expand_emenu && menu_is_separator(menu->dname))
1365 || menu_is_tearoff(menu->dname)
1366#ifndef FEAT_BROWSE
1367 || menu->dname[STRLEN(menu->dname) - 1] == '.'
1368#endif
1369 ))
1370 menu = menu->next;
1371
1372 if (menu == NULL) /* at end of linked list */
1373 return NULL;
1374
1375 if (menu->modes & expand_modes)
1376 {
1377 if (menu->children != NULL)
1378 {
1379 STRCPY(tbuffer, menu->dname);
1380 /* hack on menu separators: use a 'magic' char for the separator
1381 * so that '.' in names gets escaped properly */
1382 STRCAT(tbuffer, "\001");
1383 str = tbuffer;
1384 }
1385 else
1386 str = menu->dname;
1387 }
1388 else
1389 str = (char_u *)"";
1390
1391 /* Advance to next menu entry. */
1392 menu = menu->next;
1393
1394 return str;
1395}
1396#endif /* FEAT_CMDL_COMPL */
1397
1398/*
1399 * Skip over this element of the menu path and return the start of the next
1400 * element. Any \ and ^Vs are removed from the current element.
Bram Moolenaar342337a2005-07-21 21:11:17 +00001401 * "name" may be modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
1404menu_name_skip(name)
1405 char_u *name;
1406{
1407 char_u *p;
1408
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001409 for (p = name; *p && *p != '.'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
1411 if (*p == '\\' || *p == Ctrl_V)
1412 {
1413 mch_memmove(p, p + 1, STRLEN(p));
1414 if (*p == NUL)
1415 break;
1416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 if (*p)
1419 *p++ = NUL;
1420 return p;
1421}
1422
1423/*
1424 * Return TRUE when "name" matches with menu "menu". The name is compared in
1425 * two ways: raw menu name and menu name without '&'. ignore part after a TAB.
1426 */
1427 static int
1428menu_name_equal(name, menu)
1429 char_u *name;
1430 vimmenu_T *menu;
1431{
1432 return (menu_namecmp(name, menu->name) || menu_namecmp(name, menu->dname));
1433}
1434
1435 static int
1436menu_namecmp(name, mname)
1437 char_u *name;
1438 char_u *mname;
1439{
1440 int i;
1441
1442 for (i = 0; name[i] != NUL && name[i] != TAB; ++i)
1443 if (name[i] != mname[i])
1444 break;
1445 return ((name[i] == NUL || name[i] == TAB)
1446 && (mname[i] == NUL || mname[i] == TAB));
1447}
1448
1449/*
1450 * Return the modes specified by the given menu command (eg :menu! returns
1451 * MENU_CMDLINE_MODE | MENU_INSERT_MODE).
1452 * If "noremap" is not NULL, then the flag it points to is set according to
1453 * whether the command is a "nore" command.
1454 * If "unmenu" is not NULL, then the flag it points to is set according to
1455 * whether the command is an "unmenu" command.
1456 */
1457 static int
1458get_menu_cmd_modes(cmd, forceit, noremap, unmenu)
1459 char_u *cmd;
1460 int forceit; /* Was there a "!" after the command? */
1461 int *noremap;
1462 int *unmenu;
1463{
1464 int modes;
1465
1466 switch (*cmd++)
1467 {
1468 case 'v': /* vmenu, vunmenu, vnoremenu */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001469 modes = MENU_VISUAL_MODE | MENU_SELECT_MODE;
1470 break;
1471 case 'x': /* xmenu, xunmenu, xnoremenu */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 modes = MENU_VISUAL_MODE;
1473 break;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001474 case 's': /* smenu, sunmenu, snoremenu */
1475 modes = MENU_SELECT_MODE;
1476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 case 'o': /* omenu */
1478 modes = MENU_OP_PENDING_MODE;
1479 break;
1480 case 'i': /* imenu */
1481 modes = MENU_INSERT_MODE;
1482 break;
1483 case 't':
1484 modes = MENU_TIP_MODE; /* tmenu */
1485 break;
1486 case 'c': /* cmenu */
1487 modes = MENU_CMDLINE_MODE;
1488 break;
1489 case 'a': /* amenu */
1490 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE | MENU_NORMAL_MODE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001491 | MENU_VISUAL_MODE | MENU_SELECT_MODE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001492 | MENU_OP_PENDING_MODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 break;
1494 case 'n':
1495 if (*cmd != 'o') /* nmenu, not noremenu */
1496 {
1497 modes = MENU_NORMAL_MODE;
1498 break;
1499 }
1500 /* FALLTHROUGH */
1501 default:
1502 --cmd;
1503 if (forceit) /* menu!! */
1504 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE;
1505 else /* menu */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001506 modes = MENU_NORMAL_MODE | MENU_VISUAL_MODE | MENU_SELECT_MODE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 | MENU_OP_PENDING_MODE;
1508 }
1509
1510 if (noremap != NULL)
1511 *noremap = (*cmd == 'n' ? REMAP_NONE : REMAP_YES);
1512 if (unmenu != NULL)
1513 *unmenu = (*cmd == 'u');
1514 return modes;
1515}
1516
1517/*
1518 * Modify a menu name starting with "PopUp" to include the mode character.
1519 * Returns the name in allocated memory (NULL for failure).
1520 */
1521 static char_u *
1522popup_mode_name(name, idx)
1523 char_u *name;
1524 int idx;
1525{
1526 char_u *p;
1527 int len = (int)STRLEN(name);
1528
1529 p = vim_strnsave(name, len + 1);
1530 if (p != NULL)
1531 {
1532 mch_memmove(p + 6, p + 5, (size_t)(len - 4));
1533 p[5] = menu_mode_chars[idx];
1534 }
1535 return p;
1536}
1537
1538#if defined(FEAT_GUI) || defined(PROTO)
1539/*
1540 * Return the index into the menu->strings or menu->noremap arrays for the
1541 * current state. Returns MENU_INDEX_INVALID if there is no mapping for the
1542 * given menu in the current mode.
1543 */
1544 int
1545get_menu_index(menu, state)
1546 vimmenu_T *menu;
1547 int state;
1548{
1549 int idx;
1550
1551 if ((state & INSERT))
1552 idx = MENU_INDEX_INSERT;
1553 else if (state & CMDLINE)
1554 idx = MENU_INDEX_CMDLINE;
1555#ifdef FEAT_VISUAL
1556 else if (VIsual_active)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001557 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001558 if (VIsual_select)
1559 idx = MENU_INDEX_SELECT;
1560 else
1561 idx = MENU_INDEX_VISUAL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563#endif
1564 else if (state == HITRETURN || state == ASKMORE)
1565 idx = MENU_INDEX_CMDLINE;
1566 else if (finish_op)
1567 idx = MENU_INDEX_OP_PENDING;
1568 else if ((state & NORMAL))
1569 idx = MENU_INDEX_NORMAL;
1570 else
1571 idx = MENU_INDEX_INVALID;
1572
1573 if (idx != MENU_INDEX_INVALID && menu->strings[idx] == NULL)
1574 idx = MENU_INDEX_INVALID;
1575 return idx;
1576}
1577#endif
1578
1579/*
1580 * Duplicate the menu item text and then process to see if a mnemonic key
1581 * and/or accelerator text has been identified.
1582 * Returns a pointer to allocated memory, or NULL for failure.
1583 * If mnemonic != NULL, *mnemonic is set to the character after the first '&'.
1584 * If actext != NULL, *actext is set to the text after the first TAB.
1585 */
1586 static char_u *
1587menu_text(str, mnemonic, actext)
1588 char_u *str;
1589 int *mnemonic;
1590 char_u **actext;
1591{
1592 char_u *p;
1593 char_u *text;
1594
1595 /* Locate accelerator text, after the first TAB */
1596 p = vim_strchr(str, TAB);
1597 if (p != NULL)
1598 {
1599 if (actext != NULL)
1600 *actext = vim_strsave(p + 1);
1601 text = vim_strnsave(str, (int)(p - str));
1602 }
1603 else
1604 text = vim_strsave(str);
1605
1606 /* Find mnemonic characters "&a" and reduce "&&" to "&". */
1607 for (p = text; p != NULL; )
1608 {
1609 p = vim_strchr(p, '&');
1610 if (p != NULL)
1611 {
1612 if (p[1] == NUL) /* trailing "&" */
1613 break;
1614 if (mnemonic != NULL && p[1] != '&')
1615#if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED)
1616 *mnemonic = p[1];
1617#else
1618 {
1619 /*
1620 * Well there is a bug in the Motif libraries on OS390 Unix.
1621 * The mnemonic keys needs to be converted to ASCII values
1622 * first.
1623 * This behavior has been seen in 2.8 and 2.9.
1624 */
1625 char c = p[1];
1626 __etoa_l(&c, 1);
1627 *mnemonic = c;
1628 }
1629#endif
1630 mch_memmove(p, p + 1, STRLEN(p));
1631 p = p + 1;
1632 }
1633 }
1634 return text;
1635}
1636
1637/*
1638 * Return TRUE if "name" can be a menu in the MenuBar.
1639 */
1640 int
1641menu_is_menubar(name)
1642 char_u *name;
1643{
1644 return (!menu_is_popup(name)
1645 && !menu_is_toolbar(name)
1646 && *name != MNU_HIDDEN_CHAR);
1647}
1648
1649/*
1650 * Return TRUE if "name" is a popup menu name.
1651 */
1652 int
1653menu_is_popup(name)
1654 char_u *name;
1655{
1656 return (STRNCMP(name, "PopUp", 5) == 0);
1657}
1658
1659#if (defined(FEAT_GUI_MOTIF) && (XmVersion <= 1002)) || defined(PROTO)
1660/*
1661 * Return TRUE if "name" is part of a popup menu.
1662 */
1663 int
1664menu_is_child_of_popup(menu)
1665 vimmenu_T *menu;
1666{
1667 while (menu->parent != NULL)
1668 menu = menu->parent;
1669 return menu_is_popup(menu->name);
1670}
1671#endif
1672
1673/*
1674 * Return TRUE if "name" is a toolbar menu name.
1675 */
1676 int
1677menu_is_toolbar(name)
1678 char_u *name;
1679{
1680 return (STRNCMP(name, "ToolBar", 7) == 0);
1681}
1682
1683/*
1684 * Return TRUE if the name is a menu separator identifier: Starts and ends
1685 * with '-'
1686 */
1687 int
1688menu_is_separator(name)
1689 char_u *name;
1690{
1691 return (name[0] == '-' && name[STRLEN(name) - 1] == '-');
1692}
1693
1694/*
1695 * Return TRUE if the menu is hidden: Starts with ']'
1696 */
1697 static int
1698menu_is_hidden(name)
1699 char_u *name;
1700{
1701 return (name[0] == ']') || (menu_is_popup(name) && name[5] != NUL);
1702}
1703
1704#if defined(FEAT_CMDL_COMPL) \
1705 || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
1706/*
1707 * Return TRUE if the menu is the tearoff menu.
1708 */
1709/*ARGSUSED*/
1710 static int
1711menu_is_tearoff(name)
1712 char_u *name;
1713{
1714#ifdef FEAT_GUI
1715 return (STRCMP(name, TEAR_STRING) == 0);
1716#else
1717 return FALSE;
1718#endif
1719}
1720#endif
1721
1722#ifdef FEAT_GUI
1723
1724 static int
1725get_menu_mode()
1726{
1727#ifdef FEAT_VISUAL
1728 if (VIsual_active)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001729 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001730 if (VIsual_select)
1731 return MENU_INDEX_SELECT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 return MENU_INDEX_VISUAL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#endif
1735 if (State & INSERT)
1736 return MENU_INDEX_INSERT;
1737 if ((State & CMDLINE) || State == ASKMORE || State == HITRETURN)
1738 return MENU_INDEX_CMDLINE;
1739 if (finish_op)
1740 return MENU_INDEX_OP_PENDING;
1741 if (State & NORMAL)
1742 return MENU_INDEX_NORMAL;
1743 if (State & LANGMAP) /* must be a "r" command, like Insert mode */
1744 return MENU_INDEX_INSERT;
1745 return MENU_INDEX_INVALID;
1746}
1747
1748/*
1749 * After we have started the GUI, then we can create any menus that have been
1750 * defined. This is done once here. add_menu_path() may have already been
1751 * called to define these menus, and may be called again. This function calls
1752 * itself recursively. Should be called at the top level with:
1753 * gui_create_initial_menus(root_menu, NULL);
1754 */
1755 void
1756gui_create_initial_menus(menu)
1757 vimmenu_T *menu;
1758{
1759 int idx = 0;
1760
1761 while (menu != NULL)
1762 {
1763 /* Don't add a menu when only a tip was defined. */
1764 if (menu->modes & MENU_ALL_MODES)
1765 {
1766 if (menu->children != NULL)
1767 {
1768 gui_mch_add_menu(menu, idx);
1769 gui_create_initial_menus(menu->children);
1770 }
1771 else
1772 gui_mch_add_menu_item(menu, idx);
1773 }
1774 menu = menu->next;
1775 ++idx;
1776 }
1777}
1778
1779/*
1780 * Used recursively by gui_update_menus (see below)
1781 */
1782 static void
1783gui_update_menus_recurse(menu, mode)
1784 vimmenu_T *menu;
1785 int mode;
1786{
1787 int grey;
1788
1789 while (menu)
1790 {
1791 if ((menu->modes & menu->enabled & mode)
1792#if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
1793 || menu_is_tearoff(menu->dname)
1794#endif
1795 )
1796 grey = FALSE;
1797 else
1798 grey = TRUE;
1799#ifdef FEAT_GUI_ATHENA
1800 /* Hiding menus doesn't work for Athena, it can cause a crash. */
1801 gui_mch_menu_grey(menu, grey);
1802#else
1803 /* Never hide a toplevel menu, it may make the menubar resize or
1804 * disappear. Same problem for ToolBar items. */
1805 if (vim_strchr(p_go, GO_GREY) != NULL || menu->parent == NULL
1806# ifdef FEAT_TOOLBAR
1807 || menu_is_toolbar(menu->parent->name)
1808# endif
1809 )
1810 gui_mch_menu_grey(menu, grey);
1811 else
1812 gui_mch_menu_hidden(menu, grey);
1813#endif
1814 gui_update_menus_recurse(menu->children, mode);
1815 menu = menu->next;
1816 }
1817}
1818
1819/*
1820 * Make sure only the valid menu items appear for this mode. If
1821 * force_menu_update is not TRUE, then we only do this if the mode has changed
1822 * since last time. If "modes" is not 0, then we use these modes instead.
1823 */
1824 void
1825gui_update_menus(modes)
1826 int modes;
1827{
1828 static int prev_mode = -1;
1829 int mode = 0;
1830
1831 if (modes != 0x0)
1832 mode = modes;
1833 else
1834 {
1835 mode = get_menu_mode();
1836 if (mode == MENU_INDEX_INVALID)
1837 mode = 0;
1838 else
1839 mode = (1 << mode);
1840 }
1841
1842 if (force_menu_update || mode != prev_mode)
1843 {
1844 gui_update_menus_recurse(root_menu, mode);
1845 gui_mch_draw_menubar();
1846 prev_mode = mode;
1847 force_menu_update = FALSE;
1848#ifdef FEAT_GUI_W32
1849 /* This can leave a tearoff as active window - make sure we
1850 * have the focus <negri>*/
1851 gui_mch_activate_window();
1852#endif
1853 }
1854}
1855
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001856#if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MOTIF) \
1857 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858/*
1859 * Check if a key is used as a mnemonic for a toplevel menu.
1860 * Case of the key is ignored.
1861 */
1862 int
1863gui_is_menu_shortcut(key)
1864 int key;
1865{
1866 vimmenu_T *menu;
1867
1868 if (key < 256)
1869 key = TOLOWER_LOC(key);
1870 for (menu = root_menu; menu != NULL; menu = menu->next)
1871 if (menu->mnemonic == key
1872 || (menu->mnemonic < 256 && TOLOWER_LOC(menu->mnemonic) == key))
1873 return TRUE;
1874 return FALSE;
1875}
1876#endif
1877
1878/*
1879 * Display the Special "PopUp" menu as a pop-up at the current mouse
1880 * position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode,
1881 * etc.
1882 */
1883 void
1884gui_show_popupmenu()
1885{
1886 vimmenu_T *menu;
1887 int mode;
1888
1889 mode = get_menu_mode();
1890 if (mode == MENU_INDEX_INVALID)
1891 return;
1892 mode = menu_mode_chars[mode];
1893
Bram Moolenaar342337a2005-07-21 21:11:17 +00001894#ifdef FEAT_AUTOCMD
1895 {
1896 char_u ename[2];
1897
1898 ename[0] = mode;
1899 ename[1] = NUL;
1900 apply_autocmds(EVENT_MENUPOPUP, ename, NULL, FALSE, curbuf);
1901 }
1902#endif
1903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 for (menu = root_menu; menu != NULL; menu = menu->next)
1905 if (STRNCMP("PopUp", menu->name, 5) == 0 && menu->name[5] == mode)
1906 break;
1907
1908 /* Only show a popup when it is defined and has entries */
1909 if (menu != NULL && menu->children != NULL)
1910 gui_mch_show_popupmenu(menu);
1911}
1912#endif /* FEAT_GUI */
1913
1914#if (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)) || defined(PROTO)
1915
1916/*
1917 * Deal with tearoff items that are added like a menu item.
1918 * Currently only for Win32 GUI. Others may follow later.
1919 */
1920
1921 void
1922gui_mch_toggle_tearoffs(int enable)
1923{
1924 int pri_tab[MENUDEPTH + 1];
1925 int i;
1926
1927 if (enable)
1928 {
1929 for (i = 0; i < MENUDEPTH; ++i)
1930 pri_tab[i] = 500;
1931 pri_tab[MENUDEPTH] = -1;
1932 gui_create_tearoffs_recurse(root_menu, (char_u *)"", pri_tab, 0);
1933 }
1934 else
1935 gui_destroy_tearoffs_recurse(root_menu);
1936 s_tearoffs = enable;
1937}
1938
1939/*
1940 * Recursively add tearoff items
1941 */
1942 static void
1943gui_create_tearoffs_recurse(menu, pname, pri_tab, pri_idx)
1944 vimmenu_T *menu;
1945 const char_u *pname;
1946 int *pri_tab;
1947 int pri_idx;
1948{
1949 char_u *newpname = NULL;
1950 int len;
1951 char_u *s;
1952 char_u *d;
1953
1954 if (pri_tab[pri_idx + 1] != -1)
1955 ++pri_idx;
1956 while (menu != NULL)
1957 {
1958 if (menu->children != NULL && menu_is_menubar(menu->name))
1959 {
1960 /* Add the menu name to the menu path. Insert a backslash before
1961 * dots (it's used to separate menu names). */
1962 len = (int)STRLEN(pname) + (int)STRLEN(menu->name);
1963 for (s = menu->name; *s; ++s)
1964 if (*s == '.' || *s == '\\')
1965 ++len;
1966 newpname = alloc(len + TEAR_LEN + 2);
1967 if (newpname != NULL)
1968 {
1969 STRCPY(newpname, pname);
1970 d = newpname + STRLEN(newpname);
1971 for (s = menu->name; *s; ++s)
1972 {
1973 if (*s == '.' || *s == '\\')
1974 *d++ = '\\';
1975 *d++ = *s;
1976 }
1977 *d = NUL;
1978
1979 /* check if tearoff already exists */
1980 if (STRCMP(menu->children->name, TEAR_STRING) != 0)
1981 {
1982 gui_add_tearoff(newpname, pri_tab, pri_idx - 1);
1983 *d = NUL; /* remove TEAR_STRING */
1984 }
1985
1986 STRCAT(newpname, ".");
1987 gui_create_tearoffs_recurse(menu->children, newpname,
1988 pri_tab, pri_idx);
1989 vim_free(newpname);
1990 }
1991 }
1992 menu = menu->next;
1993 }
1994}
1995
1996/*
1997 * Add tear-off menu item for a submenu.
1998 * "tearpath" is the menu path, and must have room to add TEAR_STRING.
1999 */
2000 static void
2001gui_add_tearoff(tearpath, pri_tab, pri_idx)
2002 char_u *tearpath;
2003 int *pri_tab;
2004 int pri_idx;
2005{
2006 char_u *tbuf;
2007 int t;
2008 vimmenu_T menuarg;
2009
2010 tbuf = alloc(5 + (unsigned int)STRLEN(tearpath));
2011 if (tbuf != NULL)
2012 {
2013 tbuf[0] = K_SPECIAL;
2014 tbuf[1] = K_SECOND(K_TEAROFF);
2015 tbuf[2] = K_THIRD(K_TEAROFF);
2016 STRCPY(tbuf + 3, tearpath);
2017 STRCAT(tbuf + 3, "\r");
2018
2019 STRCAT(tearpath, ".");
2020 STRCAT(tearpath, TEAR_STRING);
2021
2022 /* Priority of tear-off is always 1 */
2023 t = pri_tab[pri_idx + 1];
2024 pri_tab[pri_idx + 1] = 1;
2025
2026#ifdef FEAT_TOOLBAR
2027 menuarg.iconfile = NULL;
2028 menuarg.iconidx = -1;
2029 menuarg.icon_builtin = FALSE;
2030#endif
2031 menuarg.noremap[0] = REMAP_NONE;
2032 menuarg.silent[0] = TRUE;
2033
2034 menuarg.modes = MENU_ALL_MODES;
2035 add_menu_path(tearpath, &menuarg, pri_tab, tbuf, FALSE);
2036
2037 menuarg.modes = MENU_TIP_MODE;
2038 add_menu_path(tearpath, &menuarg, pri_tab,
2039 (char_u *)_("Tear off this menu"), FALSE);
2040
2041 pri_tab[pri_idx + 1] = t;
2042 vim_free(tbuf);
2043 }
2044}
2045
2046/*
2047 * Recursively destroy tearoff items
2048 */
2049 static void
2050gui_destroy_tearoffs_recurse(menu)
2051 vimmenu_T *menu;
2052{
2053 while (menu)
2054 {
2055 if (menu->children)
2056 {
2057 /* check if tearoff exists */
2058 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
2059 {
2060 /* Disconnect the item and free the memory */
2061 free_menu(&menu->children);
2062 }
2063 if (menu->children != NULL) /* if not the last one */
2064 gui_destroy_tearoffs_recurse(menu->children);
2065 }
2066 menu = menu->next;
2067 }
2068}
2069
2070#endif /* FEAT_GUI_W32 && FEAT_TEAROFF */
2071
2072/*
2073 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2074 * execute it.
2075 */
2076 void
2077ex_emenu(eap)
2078 exarg_T *eap;
2079{
2080 vimmenu_T *menu;
2081 char_u *name;
2082 char_u *saved_name;
2083 char_u *p;
2084 int idx;
2085 char_u *mode;
2086
2087 saved_name = vim_strsave(eap->arg);
2088 if (saved_name == NULL)
2089 return;
2090
2091 menu = root_menu;
2092 name = saved_name;
2093 while (*name)
2094 {
2095 /* Find in the menu hierarchy */
2096 p = menu_name_skip(name);
2097
2098 while (menu != NULL)
2099 {
2100 if (menu_name_equal(name, menu))
2101 {
2102 if (*p == NUL && menu->children != NULL)
2103 {
2104 EMSG(_("E333: Menu path must lead to a menu item"));
2105 menu = NULL;
2106 }
2107 else if (*p != NUL && menu->children == NULL)
2108 {
2109 EMSG(_(e_notsubmenu));
2110 menu = NULL;
2111 }
2112 break;
2113 }
2114 menu = menu->next;
2115 }
2116 if (menu == NULL || *p == NUL)
2117 break;
2118 menu = menu->children;
2119 name = p;
2120 }
2121 vim_free(saved_name);
2122 if (menu == NULL)
2123 {
2124 EMSG2(_("E334: Menu not found: %s"), eap->arg);
2125 return;
2126 }
2127
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002128 /* Found the menu, so execute.
2129 * Use the Insert mode entry when returning to Insert mode. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002130 if (restart_edit
2131#ifdef FEAT_EVAL
2132 && !current_SID
2133#endif
2134 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 mode = (char_u *)"Insert";
2137 idx = MENU_INDEX_INSERT;
2138 }
2139 else if (eap->addr_count)
2140 {
2141 pos_T tpos;
2142
2143 mode = (char_u *)"Visual";
2144 idx = MENU_INDEX_VISUAL;
2145
2146 /* GEDDES: This is not perfect - but it is a
2147 * quick way of detecting whether we are doing this from a
2148 * selection - see if the range matches up with the visual
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002149 * select start and end. */
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002150 if ((curbuf->b_visual.vi_start.lnum == eap->line1)
2151 && (curbuf->b_visual.vi_end.lnum) == eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 /* Set it up for visual mode - equivalent to gv. */
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002154 VIsual_mode = curbuf->b_visual.vi_mode;
2155 tpos = curbuf->b_visual.vi_end;
2156 curwin->w_cursor = curbuf->b_visual.vi_start;
2157 curwin->w_curswant = curbuf->b_visual.vi_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159 else
2160 {
2161 /* Set it up for line-wise visual mode */
2162 VIsual_mode = 'V';
2163 curwin->w_cursor.lnum = eap->line1;
2164 curwin->w_cursor.col = 1;
2165 tpos.lnum = eap->line2;
2166 tpos.col = MAXCOL;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00002167#ifdef FEAT_VIRTUALEDIT
2168 tpos.coladd = 0;
2169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002172 /* Activate visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 VIsual_active = TRUE;
2174 VIsual_reselect = TRUE;
2175 check_cursor();
2176 VIsual = curwin->w_cursor;
2177 curwin->w_cursor = tpos;
2178
2179 check_cursor();
2180
2181 /* Adjust the cursor to make sure it is in the correct pos
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002182 * for exclusive mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (*p_sel == 'e' && gchar_cursor() != NUL)
2184 ++curwin->w_cursor.col;
2185 }
2186 else
2187 {
2188 mode = (char_u *)"Normal";
2189 idx = MENU_INDEX_NORMAL;
2190 }
2191
2192 if (idx != MENU_INDEX_INVALID && menu->strings[idx] != NULL)
2193 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002194 /* When executing a script or function execute the commands right now.
2195 * Otherwise put them in the typeahead buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002196#ifdef FEAT_En
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002197 if (current_SID != 0)
2198 exec_normal_cmd(menu->strings[idx], menu->noremap[idx],
2199 menu->silent[idx]);
2200 else
Bram Moolenaar4463f292005-09-25 22:20:24 +00002201#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002202 ins_typebuf(menu->strings[idx], menu->noremap[idx], 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 TRUE, menu->silent[idx]);
2204 }
2205 else
2206 EMSG2(_("E335: Menu not defined for %s mode"), mode);
2207}
2208
2209#if defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 || (defined(FEAT_GUI_GTK) && defined(FEAT_MENU)) \
2211 || defined(FEAT_BEVAL_TIP) || defined(PROTO)
2212/*
2213 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy.
2214 */
2215 vimmenu_T *
2216gui_find_menu(path_name)
2217 char_u *path_name;
2218{
2219 vimmenu_T *menu = NULL;
2220 char_u *name;
2221 char_u *saved_name;
2222 char_u *p;
2223
2224 menu = root_menu;
2225
2226 saved_name = vim_strsave(path_name);
2227 if (saved_name == NULL)
2228 return NULL;
2229
2230 name = saved_name;
2231 while (*name)
2232 {
2233 /* find the end of one dot-separated name and put a NUL at the dot */
2234 p = menu_name_skip(name);
2235
2236 while (menu != NULL)
2237 {
2238 if (STRCMP(name, menu->name) == 0 || STRCMP(name, menu->dname) == 0)
2239 {
2240 if (menu->children == NULL)
2241 {
2242 /* found a menu item instead of a sub-menu */
2243 if (*p == NUL)
2244 EMSG(_("E336: Menu path must lead to a sub-menu"));
2245 else
2246 EMSG(_(e_notsubmenu));
2247 menu = NULL;
2248 goto theend;
2249 }
2250 if (*p == NUL) /* found a full match */
2251 goto theend;
2252 break;
2253 }
2254 menu = menu->next;
2255 }
2256 if (menu == NULL) /* didn't find it */
2257 break;
2258
2259 /* Found a match, search the sub-menu. */
2260 menu = menu->children;
2261 name = p;
2262 }
2263
2264 if (menu == NULL)
2265 EMSG(_("E337: Menu not found - check menu names"));
2266theend:
2267 vim_free(saved_name);
2268 return menu;
2269}
2270#endif
2271
2272#ifdef FEAT_MULTI_LANG
2273/*
2274 * Translation of menu names. Just a simple lookup table.
2275 */
2276
2277typedef struct
2278{
2279 char_u *from; /* English name */
2280 char_u *from_noamp; /* same, without '&' */
2281 char_u *to; /* translated name */
2282} menutrans_T;
2283
2284static garray_T menutrans_ga = {0, 0, 0, 0, NULL};
2285#endif
2286
2287/*
2288 * ":menutrans".
2289 * This function is also defined without the +multi_lang feature, in which
2290 * case the commands are ignored.
2291 */
2292/*ARGSUSED*/
2293 void
2294ex_menutranslate(eap)
2295 exarg_T *eap;
2296{
2297#ifdef FEAT_MULTI_LANG
2298 char_u *arg = eap->arg;
2299 menutrans_T *tp;
2300 int i;
2301 char_u *from, *from_noamp, *to;
2302
2303 if (menutrans_ga.ga_itemsize == 0)
2304 ga_init2(&menutrans_ga, (int)sizeof(menutrans_T), 5);
2305
2306 /*
2307 * ":menutrans clear": clear all translations.
2308 */
2309 if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5)))
2310 {
2311 tp = (menutrans_T *)menutrans_ga.ga_data;
2312 for (i = 0; i < menutrans_ga.ga_len; ++i)
2313 {
2314 vim_free(tp[i].from);
2315 vim_free(tp[i].from_noamp);
2316 vim_free(tp[i].to);
2317 }
2318 ga_clear(&menutrans_ga);
2319# ifdef FEAT_EVAL
2320 /* Delete all "menutrans_" global variables. */
2321 del_menutrans_vars();
2322# endif
2323 }
2324 else
2325 {
2326 /* ":menutrans from to": add translation */
2327 from = arg;
2328 arg = menu_skip_part(arg);
2329 to = skipwhite(arg);
2330 *arg = NUL;
2331 arg = menu_skip_part(to);
2332 if (arg == to)
2333 EMSG(_(e_invarg));
2334 else
2335 {
2336 if (ga_grow(&menutrans_ga, 1) == OK)
2337 {
2338 tp = (menutrans_T *)menutrans_ga.ga_data;
2339 from = vim_strsave(from);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002340 if (from != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002342 from_noamp = menu_text(from, NULL, NULL);
2343 to = vim_strnsave(to, (int)(arg - to));
2344 if (from_noamp != NULL && to != NULL)
2345 {
2346 tp[menutrans_ga.ga_len].from = from;
2347 tp[menutrans_ga.ga_len].from_noamp = from_noamp;
2348 tp[menutrans_ga.ga_len].to = to;
2349 ++menutrans_ga.ga_len;
2350 }
2351 else
2352 {
2353 vim_free(from);
2354 vim_free(from_noamp);
2355 vim_free(to);
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358 }
2359 }
2360 }
2361#endif
2362}
2363
2364#if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
2365/*
2366 * Find the character just after one part of a menu name.
2367 */
2368 static char_u *
2369menu_skip_part(p)
2370 char_u *p;
2371{
2372 while (*p != NUL && *p != '.' && !vim_iswhite(*p))
2373 {
2374 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
2375 ++p;
2376 ++p;
2377 }
2378 return p;
2379}
2380#endif
2381
2382#ifdef FEAT_MULTI_LANG
2383/*
2384 * Lookup part of a menu name in the translations.
2385 * Return a pointer to the translation or NULL if not found.
2386 */
2387 static char_u *
2388menutrans_lookup(name, len)
2389 char_u *name;
2390 int len;
2391{
2392 menutrans_T *tp = (menutrans_T *)menutrans_ga.ga_data;
2393 int i;
2394 char_u *dname;
2395
2396 for (i = 0; i < menutrans_ga.ga_len; ++i)
2397 if (STRNCMP(name, tp[i].from, len) == 0 && tp[i].from[len] == NUL)
2398 return tp[i].to;
2399
2400 /* Now try again while ignoring '&' characters. */
2401 i = name[len];
2402 name[len] = NUL;
2403 dname = menu_text(name, NULL, NULL);
2404 name[len] = i;
2405 if (dname != NULL)
2406 {
2407 for (i = 0; i < menutrans_ga.ga_len; ++i)
2408 if (STRCMP(dname, tp[i].from_noamp) == 0)
2409 {
2410 vim_free(dname);
2411 return tp[i].to;
2412 }
2413 vim_free(dname);
2414 }
2415
2416 return NULL;
2417}
2418#endif /* FEAT_MULTI_LANG */
2419
2420#endif /* FEAT_MENU */