blob: 6787056942a80d98b1e820bd0dccbdcdd5a2f816 [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 */
62static char_u menu_mode_chars[] = {'n', 'v', 'o', 'i', 'c', 't'};
63
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");
66static char_u e_nomenu[] = N_("E329: No menu of that name");
67
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 Moolenaar843ee412004-06-30 16:16:41 +000099#if defined(FEAT_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_KDE)
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 Moolenaar843ee412004-06-30 16:16:41 +0000318#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE) || 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 Moolenaar843ee412004-06-30 16:16:41 +0000437#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE))
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 ))
445 gui_set_shellsize(FALSE, FALSE);
446#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:
742 case MENU_OP_PENDING_MODE:
743 case MENU_CMDLINE_MODE:
744 c = Ctrl_C;
745 break;
746 case MENU_INSERT_MODE:
747 c = Ctrl_O;
748 break;
749 }
750 }
751
752 if (c)
753 {
754 menu->strings[i] = alloc((unsigned)(STRLEN(call_data) + 4));
755 if (menu->strings[i] != NULL)
756 {
757 menu->strings[i][0] = c;
758 STRCPY(menu->strings[i] + 1, call_data);
759 if (c == Ctrl_C)
760 {
761 int len = STRLEN(menu->strings[i]);
762
763 /* Append CTRL-\ CTRL-G to obey 'insertmode'. */
764 menu->strings[i][len] = Ctrl_BSL;
765 menu->strings[i][len + 1] = Ctrl_G;
766 menu->strings[i][len + 2] = NUL;
767 }
768 }
769 }
770 else
771 menu->strings[i] = p;
772 menu->noremap[i] = menuarg->noremap[0];
773 menu->silent[i] = menuarg->silent[0];
774 }
775 }
776#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
777 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
778 /* Need to update the menu tip. */
779 if (modes & MENU_TIP_MODE)
780 gui_mch_menu_set_tip(menu);
781#endif
782 }
783 return OK;
784
785erret:
786 vim_free(path_name);
787 vim_free(dname);
788 return FAIL;
789}
790
791/*
792 * Set the (sub)menu with the given name to enabled or disabled.
793 * Called recursively.
794 */
795 static int
796menu_nable_recurse(menu, name, modes, enable)
797 vimmenu_T *menu;
798 char_u *name;
799 int modes;
800 int enable;
801{
802 char_u *p;
803
804 if (menu == NULL)
805 return OK; /* Got to bottom of hierarchy */
806
807 /* Get name of this element in the menu hierarchy */
808 p = menu_name_skip(name);
809
810 /* Find the menu */
811 while (menu != NULL)
812 {
813 if (*name == NUL || *name == '*' || menu_name_equal(name, menu))
814 {
815 if (*p != NUL)
816 {
817 if (menu->children == NULL)
818 {
819 EMSG(_(e_notsubmenu));
820 return FAIL;
821 }
822 if (menu_nable_recurse(menu->children, p, modes, enable)
823 == FAIL)
824 return FAIL;
825 }
826 else
827 if (enable)
828 menu->enabled |= modes;
829 else
830 menu->enabled &= ~modes;
831
832 /*
833 * When name is empty, we are doing all menu items for the given
834 * modes, so keep looping, otherwise we are just doing the named
835 * menu item (which has been found) so break here.
836 */
837 if (*name != NUL && *name != '*')
838 break;
839 }
840 menu = menu->next;
841 }
842 if (*name != NUL && *name != '*' && menu == NULL)
843 {
844 EMSG(_(e_nomenu));
845 return FAIL;
846 }
847
848#ifdef FEAT_GUI
849 /* Want to update menus now even if mode not changed */
850 force_menu_update = TRUE;
851#endif
852
853 return OK;
854}
855
856/*
857 * Remove the (sub)menu with the given name from the menu hierarchy
858 * Called recursively.
859 */
860 static int
861remove_menu(menup, name, modes, silent)
862 vimmenu_T **menup;
863 char_u *name;
864 int modes;
865 int silent; /* don't give error messages */
866{
867 vimmenu_T *menu;
868 vimmenu_T *child;
869 char_u *p;
870
871 if (*menup == NULL)
872 return OK; /* Got to bottom of hierarchy */
873
874 /* Get name of this element in the menu hierarchy */
875 p = menu_name_skip(name);
876
877 /* Find the menu */
878 while ((menu = *menup) != NULL)
879 {
880 if (*name == NUL || menu_name_equal(name, menu))
881 {
882 if (*p != NUL && menu->children == NULL)
883 {
884 if (!silent)
885 EMSG(_(e_notsubmenu));
886 return FAIL;
887 }
888 if ((menu->modes & modes) != 0x0)
889 {
890#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
891 /*
892 * If we are removing all entries for this menu,MENU_ALL_MODES,
893 * Then kill any tearoff before we start
894 */
895 if (*p == NUL && modes == MENU_ALL_MODES)
896 {
897 if (IsWindow(menu->tearoff_handle))
898 DestroyWindow(menu->tearoff_handle);
899 }
900#endif
901 if (remove_menu(&menu->children, p, modes, silent) == FAIL)
902 return FAIL;
903 }
904 else if (*name != NUL)
905 {
906 if (!silent)
907 EMSG(_(e_othermode));
908 return FAIL;
909 }
910
911 /*
912 * When name is empty, we are removing all menu items for the given
913 * modes, so keep looping, otherwise we are just removing the named
914 * menu item (which has been found) so break here.
915 */
916 if (*name != NUL)
917 break;
918
919 /* Remove the menu item for the given mode[s]. If the menu item
920 * is no longer valid in ANY mode, delete it */
921 menu->modes &= ~modes;
922 if (modes & MENU_TIP_MODE)
923 free_menu_string(menu, MENU_INDEX_TIP);
924 if ((menu->modes & MENU_ALL_MODES) == 0)
925 free_menu(menup);
926 else
927 menup = &menu->next;
928 }
929 else
930 menup = &menu->next;
931 }
932 if (*name != NUL)
933 {
934 if (menu == NULL)
935 {
936 if (!silent)
937 EMSG(_(e_nomenu));
938 return FAIL;
939 }
940
941
942 /* Recalculate modes for menu based on the new updated children */
943 menu->modes &= ~modes;
944#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
945 if ((s_tearoffs) && (menu->children != NULL)) /* there's a tear bar.. */
946 child = menu->children->next; /* don't count tearoff bar */
947 else
948#endif
949 child = menu->children;
950 for ( ; child != NULL; child = child->next)
951 menu->modes |= child->modes;
952 if (modes & MENU_TIP_MODE)
953 {
954 free_menu_string(menu, MENU_INDEX_TIP);
955#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
956 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
957 /* Need to update the menu tip. */
958 if (gui.in_use)
959 gui_mch_menu_set_tip(menu);
960#endif
961 }
962 if ((menu->modes & MENU_ALL_MODES) == 0)
963 {
964 /* The menu item is no longer valid in ANY mode, so delete it */
965#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
966 if (s_tearoffs && menu->children != NULL) /* there's a tear bar.. */
967 free_menu(&menu->children);
968#endif
969 *menup = menu;
970 free_menu(menup);
971 }
972 }
973
974 return OK;
975}
976
977/*
978 * Free the given menu structure and remove it from the linked list.
979 */
980 static void
981free_menu(menup)
982 vimmenu_T **menup;
983{
984 int i;
985 vimmenu_T *menu;
986
987 menu = *menup;
988
989#ifdef FEAT_GUI
990 /* Free machine specific menu structures (only when already created) */
991 /* Also may rebuild a tearoff'ed menu */
992 if (gui.in_use)
993 gui_mch_destroy_menu(menu);
994#endif
995
996 /* Don't change *menup until after calling gui_mch_destroy_menu(). The
997 * MacOS code needs the original structure to properly delete the menu. */
998 *menup = menu->next;
999 vim_free(menu->name);
1000 vim_free(menu->dname);
1001 vim_free(menu->actext);
1002#ifdef FEAT_TOOLBAR
1003 vim_free(menu->iconfile);
Bram Moolenaarbee0c5b2005-02-07 22:03:36 +00001004# ifdef FEAT_GUI_MOTIF
1005 vim_free(menu->xpm_fname);
1006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#endif
1008 for (i = 0; i < MENU_MODES; i++)
1009 free_menu_string(menu, i);
1010 vim_free(menu);
1011
1012#ifdef FEAT_GUI
1013 /* Want to update menus now even if mode not changed */
1014 force_menu_update = TRUE;
1015#endif
1016}
1017
1018/*
1019 * Free the menu->string with the given index.
1020 */
1021 static void
1022free_menu_string(menu, idx)
1023 vimmenu_T *menu;
1024 int idx;
1025{
1026 int count = 0;
1027 int i;
1028
1029 for (i = 0; i < MENU_MODES; i++)
1030 if (menu->strings[i] == menu->strings[idx])
1031 count++;
1032 if (count == 1)
1033 vim_free(menu->strings[idx]);
1034 menu->strings[idx] = NULL;
1035}
1036
1037/*
1038 * Show the mapping associated with a menu item or hierarchy in a sub-menu.
1039 */
1040 static int
1041show_menus(path_name, modes)
1042 char_u *path_name;
1043 int modes;
1044{
1045 char_u *p;
1046 char_u *name;
1047 vimmenu_T *menu;
1048 vimmenu_T *parent = NULL;
1049
1050 menu = root_menu;
1051 name = path_name = vim_strsave(path_name);
1052 if (path_name == NULL)
1053 return FAIL;
1054
1055 /* First, find the (sub)menu with the given name */
1056 while (*name)
1057 {
1058 p = menu_name_skip(name);
1059 while (menu != NULL)
1060 {
1061 if (menu_name_equal(name, menu))
1062 {
1063 /* Found menu */
1064 if (*p != NUL && menu->children == NULL)
1065 {
1066 EMSG(_(e_notsubmenu));
1067 vim_free(path_name);
1068 return FAIL;
1069 }
1070 else if ((menu->modes & modes) == 0x0)
1071 {
1072 EMSG(_(e_othermode));
1073 vim_free(path_name);
1074 return FAIL;
1075 }
1076 break;
1077 }
1078 menu = menu->next;
1079 }
1080 if (menu == NULL)
1081 {
1082 EMSG(_(e_nomenu));
1083 vim_free(path_name);
1084 return FAIL;
1085 }
1086 name = p;
1087 parent = menu;
1088 menu = menu->children;
1089 }
1090
1091 /* Now we have found the matching menu, and we list the mappings */
1092 /* Highlight title */
1093 MSG_PUTS_TITLE(_("\n--- Menus ---"));
1094
1095 show_menus_recursive(parent, modes, 0);
1096 return OK;
1097}
1098
1099/*
1100 * Recursively show the mappings associated with the menus under the given one
1101 */
1102 static void
1103show_menus_recursive(menu, modes, depth)
1104 vimmenu_T *menu;
1105 int modes;
1106 int depth;
1107{
1108 int i;
1109 int bit;
1110
1111 if (menu != NULL && (menu->modes & modes) == 0x0)
1112 return;
1113
1114 if (menu != NULL)
1115 {
1116 msg_putchar('\n');
1117 if (got_int) /* "q" hit for "--more--" */
1118 return;
1119 for (i = 0; i < depth; i++)
1120 MSG_PUTS(" ");
1121 if (menu->priority)
1122 {
1123 msg_outnum((long)menu->priority);
1124 MSG_PUTS(" ");
1125 }
1126 /* Same highlighting as for directories!? */
1127 msg_outtrans_attr(menu->name, hl_attr(HLF_D));
1128 }
1129
1130 if (menu != NULL && menu->children == NULL)
1131 {
1132 for (bit = 0; bit < MENU_MODES; bit++)
1133 if ((menu->modes & modes & (1 << bit)) != 0)
1134 {
1135 msg_putchar('\n');
1136 if (got_int) /* "q" hit for "--more--" */
1137 return;
1138 for (i = 0; i < depth + 2; i++)
1139 MSG_PUTS(" ");
1140 msg_putchar(menu_mode_chars[bit]);
1141 if (menu->noremap[bit] == REMAP_NONE)
1142 msg_putchar('*');
1143 else if (menu->noremap[bit] == REMAP_SCRIPT)
1144 msg_putchar('&');
1145 else
1146 msg_putchar(' ');
1147 if (menu->silent[bit])
1148 msg_putchar('s');
1149 else
1150 msg_putchar(' ');
1151 if ((menu->modes & menu->enabled & (1 << bit)) == 0)
1152 msg_putchar('-');
1153 else
1154 msg_putchar(' ');
1155 MSG_PUTS(" ");
1156 if (*menu->strings[bit] == NUL)
1157 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
1158 else
1159 msg_outtrans_special(menu->strings[bit], FALSE);
1160 }
1161 }
1162 else
1163 {
1164 if (menu == NULL)
1165 {
1166 menu = root_menu;
1167 depth--;
1168 }
1169 else
1170 menu = menu->children;
1171
1172 /* recursively show all children. Skip PopUp[nvoci]. */
1173 for (; menu != NULL && !got_int; menu = menu->next)
1174 if (!menu_is_hidden(menu->dname))
1175 show_menus_recursive(menu, modes, depth + 1);
1176 }
1177}
1178
1179#ifdef FEAT_CMDL_COMPL
1180
1181/*
1182 * Used when expanding menu names.
1183 */
1184static vimmenu_T *expand_menu = NULL;
1185static int expand_modes = 0x0;
1186static int expand_emenu; /* TRUE for ":emenu" command */
1187
1188/*
1189 * Work out what to complete when doing command line completion of menu names.
1190 */
1191 char_u *
1192set_context_in_menu_cmd(xp, cmd, arg, forceit)
1193 expand_T *xp;
1194 char_u *cmd;
1195 char_u *arg;
1196 int forceit;
1197{
1198 char_u *after_dot;
1199 char_u *p;
1200 char_u *path_name = NULL;
1201 char_u *name;
1202 int unmenu;
1203 vimmenu_T *menu;
1204 int expand_menus;
1205
1206 xp->xp_context = EXPAND_UNSUCCESSFUL;
1207
1208
1209 /* Check for priority numbers, enable and disable */
1210 for (p = arg; *p; ++p)
1211 if (!VIM_ISDIGIT(*p) && *p != '.')
1212 break;
1213
1214 if (!vim_iswhite(*p))
1215 {
1216 if (STRNCMP(arg, "enable", 6) == 0
1217 && (arg[6] == NUL || vim_iswhite(arg[6])))
1218 p = arg + 6;
1219 else if (STRNCMP(arg, "disable", 7) == 0
1220 && (arg[7] == NUL || vim_iswhite(arg[7])))
1221 p = arg + 7;
1222 else
1223 p = arg;
1224 }
1225
1226 while (*p != NUL && vim_iswhite(*p))
1227 ++p;
1228
1229 arg = after_dot = p;
1230
1231 for (; *p && !vim_iswhite(*p); ++p)
1232 {
1233 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
1234 p++;
1235 else if (*p == '.')
1236 after_dot = p + 1;
1237 }
1238
1239 /* ":tearoff" and ":popup" only use menus, not entries */
1240 expand_menus = !((*cmd == 't' && cmd[1] == 'e') || *cmd == 'p');
1241 expand_emenu = (*cmd == 'e');
1242 if (expand_menus && vim_iswhite(*p))
1243 return NULL; /* TODO: check for next command? */
1244 if (*p == NUL) /* Complete the menu name */
1245 {
1246 /*
1247 * With :unmenu, you only want to match menus for the appropriate mode.
1248 * With :menu though you might want to add a menu with the same name as
1249 * one in another mode, so match menus from other modes too.
1250 */
1251 expand_modes = get_menu_cmd_modes(cmd, forceit, NULL, &unmenu);
1252 if (!unmenu)
1253 expand_modes = MENU_ALL_MODES;
1254
1255 menu = root_menu;
1256 if (after_dot != arg)
1257 {
1258 path_name = alloc((unsigned)(after_dot - arg));
1259 if (path_name == NULL)
1260 return NULL;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001261 vim_strncpy(path_name, arg, after_dot - arg - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263 name = path_name;
1264 while (name != NULL && *name)
1265 {
1266 p = menu_name_skip(name);
1267 while (menu != NULL)
1268 {
1269 if (menu_name_equal(name, menu))
1270 {
1271 /* Found menu */
1272 if ((*p != NUL && menu->children == NULL)
1273 || ((menu->modes & expand_modes) == 0x0))
1274 {
1275 /*
1276 * Menu path continues, but we have reached a leaf.
1277 * Or menu exists only in another mode.
1278 */
1279 vim_free(path_name);
1280 return NULL;
1281 }
1282 break;
1283 }
1284 menu = menu->next;
1285 }
1286 if (menu == NULL)
1287 {
1288 /* No menu found with the name we were looking for */
1289 vim_free(path_name);
1290 return NULL;
1291 }
1292 name = p;
1293 menu = menu->children;
1294 }
1295
1296 xp->xp_context = expand_menus ? EXPAND_MENUNAMES : EXPAND_MENUS;
1297 xp->xp_pattern = after_dot;
1298 expand_menu = menu;
1299 }
1300 else /* We're in the mapping part */
1301 xp->xp_context = EXPAND_NOTHING;
1302 return NULL;
1303}
1304
1305/*
1306 * Function given to ExpandGeneric() to obtain the list of (sub)menus (not
1307 * entries).
1308 */
1309/*ARGSUSED*/
1310 char_u *
1311get_menu_name(xp, idx)
1312 expand_T *xp;
1313 int idx;
1314{
1315 static vimmenu_T *menu = NULL;
1316 char_u *str;
1317
1318 if (idx == 0) /* first call: start at first item */
1319 menu = expand_menu;
1320
1321 /* Skip PopUp[nvoci]. */
1322 while (menu != NULL && (menu_is_hidden(menu->dname)
1323 || menu_is_separator(menu->dname)
1324 || menu_is_tearoff(menu->dname)
1325 || menu->children == NULL))
1326 menu = menu->next;
1327
1328 if (menu == NULL) /* at end of linked list */
1329 return NULL;
1330
1331 if (menu->modes & expand_modes)
1332 str = menu->dname;
1333 else
1334 str = (char_u *)"";
1335
1336 /* Advance to next menu entry. */
1337 menu = menu->next;
1338
1339 return str;
1340}
1341
1342/*
1343 * Function given to ExpandGeneric() to obtain the list of menus and menu
1344 * entries.
1345 */
1346/*ARGSUSED*/
1347 char_u *
1348get_menu_names(xp, idx)
1349 expand_T *xp;
1350 int idx;
1351{
1352 static vimmenu_T *menu = NULL;
1353 static char_u tbuffer[256]; /*hack*/
1354 char_u *str;
1355
1356 if (idx == 0) /* first call: start at first item */
1357 menu = expand_menu;
1358
1359 /* Skip Browse-style entries, popup menus and separators. */
1360 while (menu != NULL
1361 && ( menu_is_hidden(menu->dname)
1362 || (expand_emenu && menu_is_separator(menu->dname))
1363 || menu_is_tearoff(menu->dname)
1364#ifndef FEAT_BROWSE
1365 || menu->dname[STRLEN(menu->dname) - 1] == '.'
1366#endif
1367 ))
1368 menu = menu->next;
1369
1370 if (menu == NULL) /* at end of linked list */
1371 return NULL;
1372
1373 if (menu->modes & expand_modes)
1374 {
1375 if (menu->children != NULL)
1376 {
1377 STRCPY(tbuffer, menu->dname);
1378 /* hack on menu separators: use a 'magic' char for the separator
1379 * so that '.' in names gets escaped properly */
1380 STRCAT(tbuffer, "\001");
1381 str = tbuffer;
1382 }
1383 else
1384 str = menu->dname;
1385 }
1386 else
1387 str = (char_u *)"";
1388
1389 /* Advance to next menu entry. */
1390 menu = menu->next;
1391
1392 return str;
1393}
1394#endif /* FEAT_CMDL_COMPL */
1395
1396/*
1397 * Skip over this element of the menu path and return the start of the next
1398 * element. Any \ and ^Vs are removed from the current element.
1399 */
1400 char_u *
1401menu_name_skip(name)
1402 char_u *name;
1403{
1404 char_u *p;
1405
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001406 for (p = name; *p && *p != '.'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
1408 if (*p == '\\' || *p == Ctrl_V)
1409 {
1410 mch_memmove(p, p + 1, STRLEN(p));
1411 if (*p == NUL)
1412 break;
1413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 if (*p)
1416 *p++ = NUL;
1417 return p;
1418}
1419
1420/*
1421 * Return TRUE when "name" matches with menu "menu". The name is compared in
1422 * two ways: raw menu name and menu name without '&'. ignore part after a TAB.
1423 */
1424 static int
1425menu_name_equal(name, menu)
1426 char_u *name;
1427 vimmenu_T *menu;
1428{
1429 return (menu_namecmp(name, menu->name) || menu_namecmp(name, menu->dname));
1430}
1431
1432 static int
1433menu_namecmp(name, mname)
1434 char_u *name;
1435 char_u *mname;
1436{
1437 int i;
1438
1439 for (i = 0; name[i] != NUL && name[i] != TAB; ++i)
1440 if (name[i] != mname[i])
1441 break;
1442 return ((name[i] == NUL || name[i] == TAB)
1443 && (mname[i] == NUL || mname[i] == TAB));
1444}
1445
1446/*
1447 * Return the modes specified by the given menu command (eg :menu! returns
1448 * MENU_CMDLINE_MODE | MENU_INSERT_MODE).
1449 * If "noremap" is not NULL, then the flag it points to is set according to
1450 * whether the command is a "nore" command.
1451 * If "unmenu" is not NULL, then the flag it points to is set according to
1452 * whether the command is an "unmenu" command.
1453 */
1454 static int
1455get_menu_cmd_modes(cmd, forceit, noremap, unmenu)
1456 char_u *cmd;
1457 int forceit; /* Was there a "!" after the command? */
1458 int *noremap;
1459 int *unmenu;
1460{
1461 int modes;
1462
1463 switch (*cmd++)
1464 {
1465 case 'v': /* vmenu, vunmenu, vnoremenu */
1466 modes = MENU_VISUAL_MODE;
1467 break;
1468 case 'o': /* omenu */
1469 modes = MENU_OP_PENDING_MODE;
1470 break;
1471 case 'i': /* imenu */
1472 modes = MENU_INSERT_MODE;
1473 break;
1474 case 't':
1475 modes = MENU_TIP_MODE; /* tmenu */
1476 break;
1477 case 'c': /* cmenu */
1478 modes = MENU_CMDLINE_MODE;
1479 break;
1480 case 'a': /* amenu */
1481 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE | MENU_NORMAL_MODE
1482 | MENU_VISUAL_MODE | MENU_OP_PENDING_MODE;
1483 break;
1484 case 'n':
1485 if (*cmd != 'o') /* nmenu, not noremenu */
1486 {
1487 modes = MENU_NORMAL_MODE;
1488 break;
1489 }
1490 /* FALLTHROUGH */
1491 default:
1492 --cmd;
1493 if (forceit) /* menu!! */
1494 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE;
1495 else /* menu */
1496 modes = MENU_NORMAL_MODE | MENU_VISUAL_MODE
1497 | MENU_OP_PENDING_MODE;
1498 }
1499
1500 if (noremap != NULL)
1501 *noremap = (*cmd == 'n' ? REMAP_NONE : REMAP_YES);
1502 if (unmenu != NULL)
1503 *unmenu = (*cmd == 'u');
1504 return modes;
1505}
1506
1507/*
1508 * Modify a menu name starting with "PopUp" to include the mode character.
1509 * Returns the name in allocated memory (NULL for failure).
1510 */
1511 static char_u *
1512popup_mode_name(name, idx)
1513 char_u *name;
1514 int idx;
1515{
1516 char_u *p;
1517 int len = (int)STRLEN(name);
1518
1519 p = vim_strnsave(name, len + 1);
1520 if (p != NULL)
1521 {
1522 mch_memmove(p + 6, p + 5, (size_t)(len - 4));
1523 p[5] = menu_mode_chars[idx];
1524 }
1525 return p;
1526}
1527
1528#if defined(FEAT_GUI) || defined(PROTO)
1529/*
1530 * Return the index into the menu->strings or menu->noremap arrays for the
1531 * current state. Returns MENU_INDEX_INVALID if there is no mapping for the
1532 * given menu in the current mode.
1533 */
1534 int
1535get_menu_index(menu, state)
1536 vimmenu_T *menu;
1537 int state;
1538{
1539 int idx;
1540
1541 if ((state & INSERT))
1542 idx = MENU_INDEX_INSERT;
1543 else if (state & CMDLINE)
1544 idx = MENU_INDEX_CMDLINE;
1545#ifdef FEAT_VISUAL
1546 else if (VIsual_active)
1547 idx = MENU_INDEX_VISUAL;
1548#endif
1549 else if (state == HITRETURN || state == ASKMORE)
1550 idx = MENU_INDEX_CMDLINE;
1551 else if (finish_op)
1552 idx = MENU_INDEX_OP_PENDING;
1553 else if ((state & NORMAL))
1554 idx = MENU_INDEX_NORMAL;
1555 else
1556 idx = MENU_INDEX_INVALID;
1557
1558 if (idx != MENU_INDEX_INVALID && menu->strings[idx] == NULL)
1559 idx = MENU_INDEX_INVALID;
1560 return idx;
1561}
1562#endif
1563
1564/*
1565 * Duplicate the menu item text and then process to see if a mnemonic key
1566 * and/or accelerator text has been identified.
1567 * Returns a pointer to allocated memory, or NULL for failure.
1568 * If mnemonic != NULL, *mnemonic is set to the character after the first '&'.
1569 * If actext != NULL, *actext is set to the text after the first TAB.
1570 */
1571 static char_u *
1572menu_text(str, mnemonic, actext)
1573 char_u *str;
1574 int *mnemonic;
1575 char_u **actext;
1576{
1577 char_u *p;
1578 char_u *text;
1579
1580 /* Locate accelerator text, after the first TAB */
1581 p = vim_strchr(str, TAB);
1582 if (p != NULL)
1583 {
1584 if (actext != NULL)
1585 *actext = vim_strsave(p + 1);
1586 text = vim_strnsave(str, (int)(p - str));
1587 }
1588 else
1589 text = vim_strsave(str);
1590
1591 /* Find mnemonic characters "&a" and reduce "&&" to "&". */
1592 for (p = text; p != NULL; )
1593 {
1594 p = vim_strchr(p, '&');
1595 if (p != NULL)
1596 {
1597 if (p[1] == NUL) /* trailing "&" */
1598 break;
1599 if (mnemonic != NULL && p[1] != '&')
1600#if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED)
1601 *mnemonic = p[1];
1602#else
1603 {
1604 /*
1605 * Well there is a bug in the Motif libraries on OS390 Unix.
1606 * The mnemonic keys needs to be converted to ASCII values
1607 * first.
1608 * This behavior has been seen in 2.8 and 2.9.
1609 */
1610 char c = p[1];
1611 __etoa_l(&c, 1);
1612 *mnemonic = c;
1613 }
1614#endif
1615 mch_memmove(p, p + 1, STRLEN(p));
1616 p = p + 1;
1617 }
1618 }
1619 return text;
1620}
1621
1622/*
1623 * Return TRUE if "name" can be a menu in the MenuBar.
1624 */
1625 int
1626menu_is_menubar(name)
1627 char_u *name;
1628{
1629 return (!menu_is_popup(name)
1630 && !menu_is_toolbar(name)
1631 && *name != MNU_HIDDEN_CHAR);
1632}
1633
1634/*
1635 * Return TRUE if "name" is a popup menu name.
1636 */
1637 int
1638menu_is_popup(name)
1639 char_u *name;
1640{
1641 return (STRNCMP(name, "PopUp", 5) == 0);
1642}
1643
1644#if (defined(FEAT_GUI_MOTIF) && (XmVersion <= 1002)) || defined(PROTO)
1645/*
1646 * Return TRUE if "name" is part of a popup menu.
1647 */
1648 int
1649menu_is_child_of_popup(menu)
1650 vimmenu_T *menu;
1651{
1652 while (menu->parent != NULL)
1653 menu = menu->parent;
1654 return menu_is_popup(menu->name);
1655}
1656#endif
1657
1658/*
1659 * Return TRUE if "name" is a toolbar menu name.
1660 */
1661 int
1662menu_is_toolbar(name)
1663 char_u *name;
1664{
1665 return (STRNCMP(name, "ToolBar", 7) == 0);
1666}
1667
1668/*
1669 * Return TRUE if the name is a menu separator identifier: Starts and ends
1670 * with '-'
1671 */
1672 int
1673menu_is_separator(name)
1674 char_u *name;
1675{
1676 return (name[0] == '-' && name[STRLEN(name) - 1] == '-');
1677}
1678
1679/*
1680 * Return TRUE if the menu is hidden: Starts with ']'
1681 */
1682 static int
1683menu_is_hidden(name)
1684 char_u *name;
1685{
1686 return (name[0] == ']') || (menu_is_popup(name) && name[5] != NUL);
1687}
1688
1689#if defined(FEAT_CMDL_COMPL) \
1690 || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
1691/*
1692 * Return TRUE if the menu is the tearoff menu.
1693 */
1694/*ARGSUSED*/
1695 static int
1696menu_is_tearoff(name)
1697 char_u *name;
1698{
1699#ifdef FEAT_GUI
1700 return (STRCMP(name, TEAR_STRING) == 0);
1701#else
1702 return FALSE;
1703#endif
1704}
1705#endif
1706
1707#ifdef FEAT_GUI
1708
1709 static int
1710get_menu_mode()
1711{
1712#ifdef FEAT_VISUAL
1713 if (VIsual_active)
1714 return MENU_INDEX_VISUAL;
1715#endif
1716 if (State & INSERT)
1717 return MENU_INDEX_INSERT;
1718 if ((State & CMDLINE) || State == ASKMORE || State == HITRETURN)
1719 return MENU_INDEX_CMDLINE;
1720 if (finish_op)
1721 return MENU_INDEX_OP_PENDING;
1722 if (State & NORMAL)
1723 return MENU_INDEX_NORMAL;
1724 if (State & LANGMAP) /* must be a "r" command, like Insert mode */
1725 return MENU_INDEX_INSERT;
1726 return MENU_INDEX_INVALID;
1727}
1728
1729/*
1730 * After we have started the GUI, then we can create any menus that have been
1731 * defined. This is done once here. add_menu_path() may have already been
1732 * called to define these menus, and may be called again. This function calls
1733 * itself recursively. Should be called at the top level with:
1734 * gui_create_initial_menus(root_menu, NULL);
1735 */
1736 void
1737gui_create_initial_menus(menu)
1738 vimmenu_T *menu;
1739{
1740 int idx = 0;
1741
1742 while (menu != NULL)
1743 {
1744 /* Don't add a menu when only a tip was defined. */
1745 if (menu->modes & MENU_ALL_MODES)
1746 {
1747 if (menu->children != NULL)
1748 {
1749 gui_mch_add_menu(menu, idx);
1750 gui_create_initial_menus(menu->children);
1751 }
1752 else
1753 gui_mch_add_menu_item(menu, idx);
1754 }
1755 menu = menu->next;
1756 ++idx;
1757 }
1758}
1759
1760/*
1761 * Used recursively by gui_update_menus (see below)
1762 */
1763 static void
1764gui_update_menus_recurse(menu, mode)
1765 vimmenu_T *menu;
1766 int mode;
1767{
1768 int grey;
1769
1770 while (menu)
1771 {
1772 if ((menu->modes & menu->enabled & mode)
1773#if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
1774 || menu_is_tearoff(menu->dname)
1775#endif
1776 )
1777 grey = FALSE;
1778 else
1779 grey = TRUE;
1780#ifdef FEAT_GUI_ATHENA
1781 /* Hiding menus doesn't work for Athena, it can cause a crash. */
1782 gui_mch_menu_grey(menu, grey);
1783#else
1784 /* Never hide a toplevel menu, it may make the menubar resize or
1785 * disappear. Same problem for ToolBar items. */
1786 if (vim_strchr(p_go, GO_GREY) != NULL || menu->parent == NULL
1787# ifdef FEAT_TOOLBAR
1788 || menu_is_toolbar(menu->parent->name)
1789# endif
1790 )
1791 gui_mch_menu_grey(menu, grey);
1792 else
1793 gui_mch_menu_hidden(menu, grey);
1794#endif
1795 gui_update_menus_recurse(menu->children, mode);
1796 menu = menu->next;
1797 }
1798}
1799
1800/*
1801 * Make sure only the valid menu items appear for this mode. If
1802 * force_menu_update is not TRUE, then we only do this if the mode has changed
1803 * since last time. If "modes" is not 0, then we use these modes instead.
1804 */
1805 void
1806gui_update_menus(modes)
1807 int modes;
1808{
1809 static int prev_mode = -1;
1810 int mode = 0;
1811
1812 if (modes != 0x0)
1813 mode = modes;
1814 else
1815 {
1816 mode = get_menu_mode();
1817 if (mode == MENU_INDEX_INVALID)
1818 mode = 0;
1819 else
1820 mode = (1 << mode);
1821 }
1822
1823 if (force_menu_update || mode != prev_mode)
1824 {
1825 gui_update_menus_recurse(root_menu, mode);
1826 gui_mch_draw_menubar();
1827 prev_mode = mode;
1828 force_menu_update = FALSE;
1829#ifdef FEAT_GUI_W32
1830 /* This can leave a tearoff as active window - make sure we
1831 * have the focus <negri>*/
1832 gui_mch_activate_window();
1833#endif
1834 }
1835}
1836
Bram Moolenaar843ee412004-06-30 16:16:41 +00001837#if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
1839/*
1840 * Check if a key is used as a mnemonic for a toplevel menu.
1841 * Case of the key is ignored.
1842 */
1843 int
1844gui_is_menu_shortcut(key)
1845 int key;
1846{
1847 vimmenu_T *menu;
1848
1849 if (key < 256)
1850 key = TOLOWER_LOC(key);
1851 for (menu = root_menu; menu != NULL; menu = menu->next)
1852 if (menu->mnemonic == key
1853 || (menu->mnemonic < 256 && TOLOWER_LOC(menu->mnemonic) == key))
1854 return TRUE;
1855 return FALSE;
1856}
1857#endif
1858
1859/*
1860 * Display the Special "PopUp" menu as a pop-up at the current mouse
1861 * position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode,
1862 * etc.
1863 */
1864 void
1865gui_show_popupmenu()
1866{
1867 vimmenu_T *menu;
1868 int mode;
1869
1870 mode = get_menu_mode();
1871 if (mode == MENU_INDEX_INVALID)
1872 return;
1873 mode = menu_mode_chars[mode];
1874
1875 for (menu = root_menu; menu != NULL; menu = menu->next)
1876 if (STRNCMP("PopUp", menu->name, 5) == 0 && menu->name[5] == mode)
1877 break;
1878
1879 /* Only show a popup when it is defined and has entries */
1880 if (menu != NULL && menu->children != NULL)
1881 gui_mch_show_popupmenu(menu);
1882}
1883#endif /* FEAT_GUI */
1884
1885#if (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)) || defined(PROTO)
1886
1887/*
1888 * Deal with tearoff items that are added like a menu item.
1889 * Currently only for Win32 GUI. Others may follow later.
1890 */
1891
1892 void
1893gui_mch_toggle_tearoffs(int enable)
1894{
1895 int pri_tab[MENUDEPTH + 1];
1896 int i;
1897
1898 if (enable)
1899 {
1900 for (i = 0; i < MENUDEPTH; ++i)
1901 pri_tab[i] = 500;
1902 pri_tab[MENUDEPTH] = -1;
1903 gui_create_tearoffs_recurse(root_menu, (char_u *)"", pri_tab, 0);
1904 }
1905 else
1906 gui_destroy_tearoffs_recurse(root_menu);
1907 s_tearoffs = enable;
1908}
1909
1910/*
1911 * Recursively add tearoff items
1912 */
1913 static void
1914gui_create_tearoffs_recurse(menu, pname, pri_tab, pri_idx)
1915 vimmenu_T *menu;
1916 const char_u *pname;
1917 int *pri_tab;
1918 int pri_idx;
1919{
1920 char_u *newpname = NULL;
1921 int len;
1922 char_u *s;
1923 char_u *d;
1924
1925 if (pri_tab[pri_idx + 1] != -1)
1926 ++pri_idx;
1927 while (menu != NULL)
1928 {
1929 if (menu->children != NULL && menu_is_menubar(menu->name))
1930 {
1931 /* Add the menu name to the menu path. Insert a backslash before
1932 * dots (it's used to separate menu names). */
1933 len = (int)STRLEN(pname) + (int)STRLEN(menu->name);
1934 for (s = menu->name; *s; ++s)
1935 if (*s == '.' || *s == '\\')
1936 ++len;
1937 newpname = alloc(len + TEAR_LEN + 2);
1938 if (newpname != NULL)
1939 {
1940 STRCPY(newpname, pname);
1941 d = newpname + STRLEN(newpname);
1942 for (s = menu->name; *s; ++s)
1943 {
1944 if (*s == '.' || *s == '\\')
1945 *d++ = '\\';
1946 *d++ = *s;
1947 }
1948 *d = NUL;
1949
1950 /* check if tearoff already exists */
1951 if (STRCMP(menu->children->name, TEAR_STRING) != 0)
1952 {
1953 gui_add_tearoff(newpname, pri_tab, pri_idx - 1);
1954 *d = NUL; /* remove TEAR_STRING */
1955 }
1956
1957 STRCAT(newpname, ".");
1958 gui_create_tearoffs_recurse(menu->children, newpname,
1959 pri_tab, pri_idx);
1960 vim_free(newpname);
1961 }
1962 }
1963 menu = menu->next;
1964 }
1965}
1966
1967/*
1968 * Add tear-off menu item for a submenu.
1969 * "tearpath" is the menu path, and must have room to add TEAR_STRING.
1970 */
1971 static void
1972gui_add_tearoff(tearpath, pri_tab, pri_idx)
1973 char_u *tearpath;
1974 int *pri_tab;
1975 int pri_idx;
1976{
1977 char_u *tbuf;
1978 int t;
1979 vimmenu_T menuarg;
1980
1981 tbuf = alloc(5 + (unsigned int)STRLEN(tearpath));
1982 if (tbuf != NULL)
1983 {
1984 tbuf[0] = K_SPECIAL;
1985 tbuf[1] = K_SECOND(K_TEAROFF);
1986 tbuf[2] = K_THIRD(K_TEAROFF);
1987 STRCPY(tbuf + 3, tearpath);
1988 STRCAT(tbuf + 3, "\r");
1989
1990 STRCAT(tearpath, ".");
1991 STRCAT(tearpath, TEAR_STRING);
1992
1993 /* Priority of tear-off is always 1 */
1994 t = pri_tab[pri_idx + 1];
1995 pri_tab[pri_idx + 1] = 1;
1996
1997#ifdef FEAT_TOOLBAR
1998 menuarg.iconfile = NULL;
1999 menuarg.iconidx = -1;
2000 menuarg.icon_builtin = FALSE;
2001#endif
2002 menuarg.noremap[0] = REMAP_NONE;
2003 menuarg.silent[0] = TRUE;
2004
2005 menuarg.modes = MENU_ALL_MODES;
2006 add_menu_path(tearpath, &menuarg, pri_tab, tbuf, FALSE);
2007
2008 menuarg.modes = MENU_TIP_MODE;
2009 add_menu_path(tearpath, &menuarg, pri_tab,
2010 (char_u *)_("Tear off this menu"), FALSE);
2011
2012 pri_tab[pri_idx + 1] = t;
2013 vim_free(tbuf);
2014 }
2015}
2016
2017/*
2018 * Recursively destroy tearoff items
2019 */
2020 static void
2021gui_destroy_tearoffs_recurse(menu)
2022 vimmenu_T *menu;
2023{
2024 while (menu)
2025 {
2026 if (menu->children)
2027 {
2028 /* check if tearoff exists */
2029 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
2030 {
2031 /* Disconnect the item and free the memory */
2032 free_menu(&menu->children);
2033 }
2034 if (menu->children != NULL) /* if not the last one */
2035 gui_destroy_tearoffs_recurse(menu->children);
2036 }
2037 menu = menu->next;
2038 }
2039}
2040
2041#endif /* FEAT_GUI_W32 && FEAT_TEAROFF */
2042
2043/*
2044 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2045 * execute it.
2046 */
2047 void
2048ex_emenu(eap)
2049 exarg_T *eap;
2050{
2051 vimmenu_T *menu;
2052 char_u *name;
2053 char_u *saved_name;
2054 char_u *p;
2055 int idx;
2056 char_u *mode;
2057
2058 saved_name = vim_strsave(eap->arg);
2059 if (saved_name == NULL)
2060 return;
2061
2062 menu = root_menu;
2063 name = saved_name;
2064 while (*name)
2065 {
2066 /* Find in the menu hierarchy */
2067 p = menu_name_skip(name);
2068
2069 while (menu != NULL)
2070 {
2071 if (menu_name_equal(name, menu))
2072 {
2073 if (*p == NUL && menu->children != NULL)
2074 {
2075 EMSG(_("E333: Menu path must lead to a menu item"));
2076 menu = NULL;
2077 }
2078 else if (*p != NUL && menu->children == NULL)
2079 {
2080 EMSG(_(e_notsubmenu));
2081 menu = NULL;
2082 }
2083 break;
2084 }
2085 menu = menu->next;
2086 }
2087 if (menu == NULL || *p == NUL)
2088 break;
2089 menu = menu->children;
2090 name = p;
2091 }
2092 vim_free(saved_name);
2093 if (menu == NULL)
2094 {
2095 EMSG2(_("E334: Menu not found: %s"), eap->arg);
2096 return;
2097 }
2098
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002099 /* Found the menu, so execute.
2100 * Use the Insert mode entry when returning to Insert mode. */
2101 if (restart_edit && !current_SID)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 mode = (char_u *)"Insert";
2104 idx = MENU_INDEX_INSERT;
2105 }
2106 else if (eap->addr_count)
2107 {
2108 pos_T tpos;
2109
2110 mode = (char_u *)"Visual";
2111 idx = MENU_INDEX_VISUAL;
2112
2113 /* GEDDES: This is not perfect - but it is a
2114 * quick way of detecting whether we are doing this from a
2115 * selection - see if the range matches up with the visual
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002116 * select start and end. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if ((curbuf->b_visual_start.lnum == eap->line1)
2118 && (curbuf->b_visual_end.lnum) == eap->line2)
2119 {
2120 /* Set it up for visual mode - equivalent to gv. */
2121 VIsual_mode = curbuf->b_visual_mode;
2122 tpos = curbuf->b_visual_end;
2123 curwin->w_cursor = curbuf->b_visual_start;
2124 curwin->w_curswant = curbuf->b_visual_curswant;
2125 }
2126 else
2127 {
2128 /* Set it up for line-wise visual mode */
2129 VIsual_mode = 'V';
2130 curwin->w_cursor.lnum = eap->line1;
2131 curwin->w_cursor.col = 1;
2132 tpos.lnum = eap->line2;
2133 tpos.col = MAXCOL;
2134 }
2135
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002136 /* Activate visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 VIsual_active = TRUE;
2138 VIsual_reselect = TRUE;
2139 check_cursor();
2140 VIsual = curwin->w_cursor;
2141 curwin->w_cursor = tpos;
2142
2143 check_cursor();
2144
2145 /* Adjust the cursor to make sure it is in the correct pos
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002146 * for exclusive mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (*p_sel == 'e' && gchar_cursor() != NUL)
2148 ++curwin->w_cursor.col;
2149 }
2150 else
2151 {
2152 mode = (char_u *)"Normal";
2153 idx = MENU_INDEX_NORMAL;
2154 }
2155
2156 if (idx != MENU_INDEX_INVALID && menu->strings[idx] != NULL)
2157 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002158 /* When executing a script or function execute the commands right now.
2159 * Otherwise put them in the typeahead buffer. */
2160 if (current_SID != 0)
2161 exec_normal_cmd(menu->strings[idx], menu->noremap[idx],
2162 menu->silent[idx]);
2163 else
2164 ins_typebuf(menu->strings[idx], menu->noremap[idx], 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 TRUE, menu->silent[idx]);
2166 }
2167 else
2168 EMSG2(_("E335: Menu not defined for %s mode"), mode);
2169}
2170
2171#if defined(FEAT_GUI_MSWIN) \
Bram Moolenaar843ee412004-06-30 16:16:41 +00002172 || (defined(FEAT_GUI_KDE) && defined(FEAT_MENU)) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 || (defined(FEAT_GUI_GTK) && defined(FEAT_MENU)) \
2174 || defined(FEAT_BEVAL_TIP) || defined(PROTO)
2175/*
2176 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy.
2177 */
2178 vimmenu_T *
2179gui_find_menu(path_name)
2180 char_u *path_name;
2181{
2182 vimmenu_T *menu = NULL;
2183 char_u *name;
2184 char_u *saved_name;
2185 char_u *p;
2186
2187 menu = root_menu;
2188
2189 saved_name = vim_strsave(path_name);
2190 if (saved_name == NULL)
2191 return NULL;
2192
2193 name = saved_name;
2194 while (*name)
2195 {
2196 /* find the end of one dot-separated name and put a NUL at the dot */
2197 p = menu_name_skip(name);
2198
2199 while (menu != NULL)
2200 {
2201 if (STRCMP(name, menu->name) == 0 || STRCMP(name, menu->dname) == 0)
2202 {
2203 if (menu->children == NULL)
2204 {
2205 /* found a menu item instead of a sub-menu */
2206 if (*p == NUL)
2207 EMSG(_("E336: Menu path must lead to a sub-menu"));
2208 else
2209 EMSG(_(e_notsubmenu));
2210 menu = NULL;
2211 goto theend;
2212 }
2213 if (*p == NUL) /* found a full match */
2214 goto theend;
2215 break;
2216 }
2217 menu = menu->next;
2218 }
2219 if (menu == NULL) /* didn't find it */
2220 break;
2221
2222 /* Found a match, search the sub-menu. */
2223 menu = menu->children;
2224 name = p;
2225 }
2226
2227 if (menu == NULL)
2228 EMSG(_("E337: Menu not found - check menu names"));
2229theend:
2230 vim_free(saved_name);
2231 return menu;
2232}
2233#endif
2234
2235#ifdef FEAT_MULTI_LANG
2236/*
2237 * Translation of menu names. Just a simple lookup table.
2238 */
2239
2240typedef struct
2241{
2242 char_u *from; /* English name */
2243 char_u *from_noamp; /* same, without '&' */
2244 char_u *to; /* translated name */
2245} menutrans_T;
2246
2247static garray_T menutrans_ga = {0, 0, 0, 0, NULL};
2248#endif
2249
2250/*
2251 * ":menutrans".
2252 * This function is also defined without the +multi_lang feature, in which
2253 * case the commands are ignored.
2254 */
2255/*ARGSUSED*/
2256 void
2257ex_menutranslate(eap)
2258 exarg_T *eap;
2259{
2260#ifdef FEAT_MULTI_LANG
2261 char_u *arg = eap->arg;
2262 menutrans_T *tp;
2263 int i;
2264 char_u *from, *from_noamp, *to;
2265
2266 if (menutrans_ga.ga_itemsize == 0)
2267 ga_init2(&menutrans_ga, (int)sizeof(menutrans_T), 5);
2268
2269 /*
2270 * ":menutrans clear": clear all translations.
2271 */
2272 if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5)))
2273 {
2274 tp = (menutrans_T *)menutrans_ga.ga_data;
2275 for (i = 0; i < menutrans_ga.ga_len; ++i)
2276 {
2277 vim_free(tp[i].from);
2278 vim_free(tp[i].from_noamp);
2279 vim_free(tp[i].to);
2280 }
2281 ga_clear(&menutrans_ga);
2282# ifdef FEAT_EVAL
2283 /* Delete all "menutrans_" global variables. */
2284 del_menutrans_vars();
2285# endif
2286 }
2287 else
2288 {
2289 /* ":menutrans from to": add translation */
2290 from = arg;
2291 arg = menu_skip_part(arg);
2292 to = skipwhite(arg);
2293 *arg = NUL;
2294 arg = menu_skip_part(to);
2295 if (arg == to)
2296 EMSG(_(e_invarg));
2297 else
2298 {
2299 if (ga_grow(&menutrans_ga, 1) == OK)
2300 {
2301 tp = (menutrans_T *)menutrans_ga.ga_data;
2302 from = vim_strsave(from);
2303 from_noamp = menu_text(from, NULL, NULL);
2304 to = vim_strnsave(to, (int)(arg - to));
2305 if (from != NULL && from_noamp != NULL && to != NULL)
2306 {
2307 tp[menutrans_ga.ga_len].from = from;
2308 tp[menutrans_ga.ga_len].from_noamp = from_noamp;
2309 tp[menutrans_ga.ga_len].to = to;
2310 ++menutrans_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312 else
2313 {
2314 vim_free(from);
2315 vim_free(from_noamp);
2316 vim_free(to);
2317 }
2318 }
2319 }
2320 }
2321#endif
2322}
2323
2324#if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
2325/*
2326 * Find the character just after one part of a menu name.
2327 */
2328 static char_u *
2329menu_skip_part(p)
2330 char_u *p;
2331{
2332 while (*p != NUL && *p != '.' && !vim_iswhite(*p))
2333 {
2334 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
2335 ++p;
2336 ++p;
2337 }
2338 return p;
2339}
2340#endif
2341
2342#ifdef FEAT_MULTI_LANG
2343/*
2344 * Lookup part of a menu name in the translations.
2345 * Return a pointer to the translation or NULL if not found.
2346 */
2347 static char_u *
2348menutrans_lookup(name, len)
2349 char_u *name;
2350 int len;
2351{
2352 menutrans_T *tp = (menutrans_T *)menutrans_ga.ga_data;
2353 int i;
2354 char_u *dname;
2355
2356 for (i = 0; i < menutrans_ga.ga_len; ++i)
2357 if (STRNCMP(name, tp[i].from, len) == 0 && tp[i].from[len] == NUL)
2358 return tp[i].to;
2359
2360 /* Now try again while ignoring '&' characters. */
2361 i = name[len];
2362 name[len] = NUL;
2363 dname = menu_text(name, NULL, NULL);
2364 name[len] = i;
2365 if (dname != NULL)
2366 {
2367 for (i = 0; i < menutrans_ga.ga_len; ++i)
2368 if (STRCMP(dname, tp[i].from_noamp) == 0)
2369 {
2370 vim_free(dname);
2371 return tp[i].to;
2372 }
2373 vim_free(dname);
2374 }
2375
2376 return NULL;
2377}
2378#endif /* FEAT_MULTI_LANG */
2379
2380#endif /* FEAT_MENU */