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