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