blob: d2ae1b05e4de1d9c851879d3515afd536624911a [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));
Bram Moolenaar56be9502010-06-06 14:20:26 +020059static void menu_unescape_name __ARGS((char_u *p));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar70b11cd2010-05-14 22:24:40 +020062static char_u *menu_translate_tab_and_shift __ARGS((char_u *arg_start));
Bram Moolenaar70b11cd2010-05-14 22:24:40 +020063
Bram Moolenaar071d4272004-06-13 20:20:40 +000064/* The character for each menu mode */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000065static char_u menu_mode_chars[] = {'n', 'v', 's', 'o', 'i', 'c', 't'};
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static char_u e_notsubmenu[] = N_("E327: Part of menu-item path is not sub-menu");
68static char_u e_othermode[] = N_("E328: Menu only exists in another mode");
Bram Moolenaar342337a2005-07-21 21:11:17 +000069static char_u e_nomenu[] = N_("E329: No menu \"%s\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71#ifdef FEAT_TOOLBAR
72static const char *toolbar_names[] =
73{
74 /* 0 */ "New", "Open", "Save", "Undo", "Redo",
75 /* 5 */ "Cut", "Copy", "Paste", "Print", "Help",
76 /* 10 */ "Find", "SaveAll", "SaveSesn", "NewSesn", "LoadSesn",
77 /* 15 */ "RunScript", "Replace", "WinClose", "WinMax", "WinMin",
78 /* 20 */ "WinSplit", "Shell", "FindPrev", "FindNext", "FindHelp",
79 /* 25 */ "Make", "TagJump", "RunCtags", "WinVSplit", "WinMaxWidth",
80 /* 30 */ "WinMinWidth", "Exit"
81};
82# define TOOLBAR_NAME_COUNT (sizeof(toolbar_names) / sizeof(char *))
83#endif
84
85/*
86 * Do the :menu command and relatives.
87 */
88 void
89ex_menu(eap)
90 exarg_T *eap; /* Ex command arguments */
91{
92 char_u *menu_path;
93 int modes;
94 char_u *map_to;
95 int noremap;
96 int silent = FALSE;
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +000097 int special = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 int unmenu;
99 char_u *map_buf;
100 char_u *arg;
101 char_u *p;
102 int i;
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000103#if defined(FEAT_GUI) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 int old_menu_height;
105# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
106 int old_toolbar_height;
107# endif
108#endif
109 int pri_tab[MENUDEPTH + 1];
110 int enable = MAYBE; /* TRUE for "menu enable", FALSE for "menu
111 * disable */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#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 }
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +0000134 if (STRNCMP(arg, "<special>", 9) == 0)
135 {
136 special = TRUE;
137 arg = skipwhite(arg + 9);
138 continue;
139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 break;
141 }
142
143
144 /* Locate an optional "icon=filename" argument. */
145 if (STRNCMP(arg, "icon=", 5) == 0)
146 {
147 arg += 5;
148#ifdef FEAT_TOOLBAR
149 icon = arg;
150#endif
151 while (*arg != NUL && *arg != ' ')
152 {
153 if (*arg == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000154 STRMOVE(arg, arg + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000155 mb_ptr_adv(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 }
157 if (*arg != NUL)
158 {
159 *arg++ = NUL;
160 arg = skipwhite(arg);
161 }
162 }
163
164 /*
165 * Fill in the priority table.
166 */
167 for (p = arg; *p; ++p)
168 if (!VIM_ISDIGIT(*p) && *p != '.')
169 break;
170 if (vim_iswhite(*p))
171 {
172 for (i = 0; i < MENUDEPTH && !vim_iswhite(*arg); ++i)
173 {
174 pri_tab[i] = getdigits(&arg);
175 if (pri_tab[i] == 0)
176 pri_tab[i] = 500;
177 if (*arg == '.')
178 ++arg;
179 }
180 arg = skipwhite(arg);
181 }
182 else if (eap->addr_count && eap->line2 != 0)
183 {
184 pri_tab[0] = eap->line2;
185 i = 1;
186 }
187 else
188 i = 0;
189 while (i < MENUDEPTH)
190 pri_tab[i++] = 500;
191 pri_tab[MENUDEPTH] = -1; /* mark end of the table */
192
193 /*
194 * Check for "disable" or "enable" argument.
195 */
196 if (STRNCMP(arg, "enable", 6) == 0 && vim_iswhite(arg[6]))
197 {
198 enable = TRUE;
199 arg = skipwhite(arg + 6);
200 }
201 else if (STRNCMP(arg, "disable", 7) == 0 && vim_iswhite(arg[7]))
202 {
203 enable = FALSE;
204 arg = skipwhite(arg + 7);
205 }
206
207 /*
208 * If there is no argument, display all menus.
209 */
210 if (*arg == NUL)
211 {
212 show_menus(arg, modes);
213 return;
214 }
215
216#ifdef FEAT_TOOLBAR
217 /*
218 * Need to get the toolbar icon index before doing the translation.
219 */
220 menuarg.iconidx = -1;
221 menuarg.icon_builtin = FALSE;
222 if (menu_is_toolbar(arg))
223 {
224 menu_path = menu_skip_part(arg);
225 if (*menu_path == '.')
226 {
227 p = menu_skip_part(++menu_path);
228 if (STRNCMP(menu_path, "BuiltIn", 7) == 0)
229 {
230 if (skipdigits(menu_path + 7) == p)
231 {
232 menuarg.iconidx = atoi((char *)menu_path + 7);
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000233 if (menuarg.iconidx >= (int)TOOLBAR_NAME_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 menuarg.iconidx = -1;
235 else
236 menuarg.icon_builtin = TRUE;
237 }
238 }
239 else
240 {
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000241 for (i = 0; i < (int)TOOLBAR_NAME_COUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 if (STRNCMP(toolbar_names[i], menu_path, p - menu_path)
243 == 0)
244 {
245 menuarg.iconidx = i;
246 break;
247 }
248 }
249 }
250 }
251#endif
252
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 menu_path = arg;
254 if (*menu_path == '.')
255 {
256 EMSG2(_(e_invarg2), menu_path);
257 goto theend;
258 }
259
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200260 map_to = menu_translate_tab_and_shift(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262 /*
263 * If there is only a menu name, display menus with that name.
264 */
265 if (*map_to == NUL && !unmenu && enable == MAYBE)
266 {
267 show_menus(menu_path, modes);
268 goto theend;
269 }
270 else if (*map_to != NUL && (unmenu || enable != MAYBE))
271 {
272 EMSG(_(e_trailing));
273 goto theend;
274 }
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000275#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 old_menu_height = gui.menu_height;
277# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
278 old_toolbar_height = gui.toolbar_height;
279# endif
280#endif
281
282 if (enable != MAYBE)
283 {
284 /*
285 * Change sensitivity of the menu.
286 * For the PopUp menu, remove a menu for each mode separately.
287 * Careful: menu_nable_recurse() changes menu_path.
288 */
289 if (STRCMP(menu_path, "*") == 0) /* meaning: do all menus */
290 menu_path = (char_u *)"";
291
292 if (menu_is_popup(menu_path))
293 {
294 for (i = 0; i < MENU_INDEX_TIP; ++i)
295 if (modes & (1 << i))
296 {
297 p = popup_mode_name(menu_path, i);
298 if (p != NULL)
299 {
300 menu_nable_recurse(root_menu, p, MENU_ALL_MODES,
301 enable);
302 vim_free(p);
303 }
304 }
305 }
306 menu_nable_recurse(root_menu, menu_path, modes, enable);
307 }
308 else if (unmenu)
309 {
310 /*
311 * Delete menu(s).
312 */
313 if (STRCMP(menu_path, "*") == 0) /* meaning: remove all menus */
314 menu_path = (char_u *)"";
315
316 /*
317 * For the PopUp menu, remove a menu for each mode separately.
318 */
319 if (menu_is_popup(menu_path))
320 {
321 for (i = 0; i < MENU_INDEX_TIP; ++i)
322 if (modes & (1 << i))
323 {
324 p = popup_mode_name(menu_path, i);
325 if (p != NULL)
326 {
327 remove_menu(&root_menu, p, MENU_ALL_MODES, TRUE);
328 vim_free(p);
329 }
330 }
331 }
332
333 /* Careful: remove_menu() changes menu_path */
334 remove_menu(&root_menu, menu_path, modes, FALSE);
335 }
336 else
337 {
338 /*
339 * Add menu(s).
340 * Replace special key codes.
341 */
342 if (STRICMP(map_to, "<nop>") == 0) /* "<Nop>" means nothing */
343 {
344 map_to = (char_u *)"";
345 map_buf = NULL;
346 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000347 else if (modes & MENU_TIP_MODE)
348 map_buf = NULL; /* Menu tips are plain text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 else
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +0000350 map_to = replace_termcodes(map_to, &map_buf, FALSE, TRUE, special);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 menuarg.modes = modes;
352#ifdef FEAT_TOOLBAR
353 menuarg.iconfile = icon;
354#endif
355 menuarg.noremap[0] = noremap;
356 menuarg.silent[0] = silent;
357 add_menu_path(menu_path, &menuarg, pri_tab, map_to
358#ifdef FEAT_GUI_W32
359 , TRUE
360#endif
361 );
362
363 /*
364 * For the PopUp menu, add a menu for each mode separately.
365 */
366 if (menu_is_popup(menu_path))
367 {
368 for (i = 0; i < MENU_INDEX_TIP; ++i)
369 if (modes & (1 << i))
370 {
371 p = popup_mode_name(menu_path, i);
372 if (p != NULL)
373 {
374 /* Include all modes, to make ":amenu" work */
375 menuarg.modes = modes;
376#ifdef FEAT_TOOLBAR
377 menuarg.iconfile = NULL;
378 menuarg.iconidx = -1;
379 menuarg.icon_builtin = FALSE;
380#endif
381 add_menu_path(p, &menuarg, pri_tab, map_to
382#ifdef FEAT_GUI_W32
383 , TRUE
384#endif
385 );
386 vim_free(p);
387 }
388 }
389 }
390
391 vim_free(map_buf);
392 }
393
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000394#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 /* If the menubar height changed, resize the window */
396 if (gui.in_use
397 && (gui.menu_height != old_menu_height
398# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
399 || gui.toolbar_height != old_toolbar_height
400# endif
401 ))
Bram Moolenaar04a9d452006-03-27 21:03:26 +0000402 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
404
405theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407}
408
409/*
410 * Add the menu with the given name to the menu hierarchy
411 */
412 static int
413add_menu_path(menu_path, menuarg, pri_tab, call_data
414#ifdef FEAT_GUI_W32
415 , addtearoff
416#endif
417 )
418 char_u *menu_path;
419 vimmenu_T *menuarg; /* passes modes, iconfile, iconidx,
420 icon_builtin, silent[0], noremap[0] */
421 int *pri_tab;
422 char_u *call_data;
423#ifdef FEAT_GUI_W32
424 int addtearoff; /* may add tearoff item */
425#endif
426{
427 char_u *path_name;
428 int modes = menuarg->modes;
429 vimmenu_T **menup;
430 vimmenu_T *menu = NULL;
431 vimmenu_T *parent;
432 vimmenu_T **lower_pri;
433 char_u *p;
434 char_u *name;
435 char_u *dname;
436 char_u *next_name;
437 int i;
438 int c;
Bram Moolenaar7871a502010-05-14 21:19:23 +0200439 int d;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440#ifdef FEAT_GUI
441 int idx;
442 int new_idx;
443#endif
444 int pri_idx = 0;
445 int old_modes = 0;
446 int amenu;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200447#ifdef FEAT_MULTI_LANG
448 char_u *en_name;
449 char_u *map_to = NULL;
450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452 /* Make a copy so we can stuff around with it, since it could be const */
453 path_name = vim_strsave(menu_path);
454 if (path_name == NULL)
455 return FAIL;
456 menup = &root_menu;
457 parent = NULL;
458 name = path_name;
459 while (*name)
460 {
461 /* Get name of this element in the menu hierarchy, and the simplified
462 * name (without mnemonic and accelerator text). */
463 next_name = menu_name_skip(name);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200464#ifdef FEAT_MULTI_LANG
Bram Moolenaar442b4222010-05-24 21:34:22 +0200465 map_to = menutrans_lookup(name, (int)STRLEN(name));
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200466 if (map_to != NULL)
467 {
468 en_name = name;
469 name = map_to;
470 }
471 else
472 en_name = NULL;
473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 dname = menu_text(name, NULL, NULL);
Bram Moolenaar18a0b122006-08-16 13:55:16 +0000475 if (dname == NULL)
476 goto erret;
477 if (*dname == NUL)
478 {
479 /* Only a mnemonic or accelerator is not valid. */
480 EMSG(_("E792: Empty menu name"));
481 goto erret;
482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
484 /* See if it's already there */
485 lower_pri = menup;
486#ifdef FEAT_GUI
487 idx = 0;
488 new_idx = 0;
489#endif
490 menu = *menup;
491 while (menu != NULL)
492 {
493 if (menu_name_equal(name, menu) || menu_name_equal(dname, menu))
494 {
495 if (*next_name == NUL && menu->children != NULL)
496 {
497 if (!sys_menu)
498 EMSG(_("E330: Menu path must not lead to a sub-menu"));
499 goto erret;
500 }
501 if (*next_name != NUL && menu->children == NULL
502#ifdef FEAT_GUI_W32
503 && addtearoff
504#endif
505 )
506 {
507 if (!sys_menu)
508 EMSG(_(e_notsubmenu));
509 goto erret;
510 }
511 break;
512 }
513 menup = &menu->next;
514
515 /* Count menus, to find where this one needs to be inserted.
516 * Ignore menus that are not in the menubar (PopUp and Toolbar) */
517 if (parent != NULL || menu_is_menubar(menu->name))
518 {
519#ifdef FEAT_GUI
520 ++idx;
521#endif
522 if (menu->priority <= pri_tab[pri_idx])
523 {
524 lower_pri = menup;
525#ifdef FEAT_GUI
526 new_idx = idx;
527#endif
528 }
529 }
530 menu = menu->next;
531 }
532
533 if (menu == NULL)
534 {
535 if (*next_name == NUL && parent == NULL)
536 {
537 EMSG(_("E331: Must not add menu items directly to menu bar"));
538 goto erret;
539 }
540
541 if (menu_is_separator(dname) && *next_name != NUL)
542 {
543 EMSG(_("E332: Separator cannot be part of a menu path"));
544 goto erret;
545 }
546
547 /* Not already there, so lets add it */
548 menu = (vimmenu_T *)alloc_clear((unsigned)sizeof(vimmenu_T));
549 if (menu == NULL)
550 goto erret;
551
552 menu->modes = modes;
553 menu->enabled = MENU_ALL_MODES;
554 menu->name = vim_strsave(name);
555 /* separate mnemonic and accelerator text from actual menu name */
556 menu->dname = menu_text(name, &menu->mnemonic, &menu->actext);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200557#ifdef FEAT_MULTI_LANG
558 if (en_name != NULL)
559 {
560 menu->en_name = vim_strsave(en_name);
561 menu->en_dname = menu_text(en_name, NULL, NULL);
562 }
563 else
564 {
565 menu->en_name = NULL;
566 menu->en_dname = NULL;
567 }
568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 menu->priority = pri_tab[pri_idx];
570 menu->parent = parent;
571#ifdef FEAT_GUI_MOTIF
572 menu->sensitive = TRUE; /* the default */
573#endif
574#ifdef FEAT_BEVAL_TIP
575 menu->tip = NULL;
576#endif
577#ifdef FEAT_GUI_ATHENA
578 menu->image = None; /* X-Windows definition for NULL*/
579#endif
580
581 /*
582 * Add after menu that has lower priority.
583 */
584 menu->next = *lower_pri;
585 *lower_pri = menu;
586
587 old_modes = 0;
588
589#ifdef FEAT_TOOLBAR
590 menu->iconidx = menuarg->iconidx;
591 menu->icon_builtin = menuarg->icon_builtin;
592 if (*next_name == NUL && menuarg->iconfile != NULL)
593 menu->iconfile = vim_strsave(menuarg->iconfile);
594#endif
595#if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
596 /* the tearoff item must be present in the modes of each item. */
597 if (parent != NULL && menu_is_tearoff(parent->children->dname))
598 parent->children->modes |= modes;
599#endif
600 }
601 else
602 {
603 old_modes = menu->modes;
604
605 /*
606 * If this menu option was previously only available in other
607 * modes, then make sure it's available for this one now
608 * Also enable a menu when it's created or changed.
609 */
610#ifdef FEAT_GUI_W32
611 /* If adding a tearbar (addtearoff == FALSE) don't update modes */
612 if (addtearoff)
613#endif
614 {
615 menu->modes |= modes;
616 menu->enabled |= modes;
617 }
618 }
619
620#ifdef FEAT_GUI
621 /*
622 * Add the menu item when it's used in one of the modes, but not when
623 * only a tooltip is defined.
624 */
625 if ((old_modes & MENU_ALL_MODES) == 0
626 && (menu->modes & MENU_ALL_MODES) != 0)
627 {
628 if (gui.in_use) /* Otherwise it will be added when GUI starts */
629 {
630 if (*next_name == NUL)
631 {
632 /* Real menu item, not sub-menu */
633 gui_mch_add_menu_item(menu, new_idx);
634
635 /* Want to update menus now even if mode not changed */
636 force_menu_update = TRUE;
637 }
638 else
639 {
640 /* Sub-menu (not at end of path yet) */
641 gui_mch_add_menu(menu, new_idx);
642 }
643 }
644
645# if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
646 /* When adding a new submenu, may add a tearoff item */
647 if ( addtearoff
648 && *next_name
649 && vim_strchr(p_go, GO_TEAROFF) != NULL
650 && menu_is_menubar(name))
651 {
652 char_u *tearpath;
653
654 /*
655 * The pointers next_name & path_name refer to a string with
656 * \'s and ^V's stripped out. But menu_path is a "raw"
657 * string, so we must correct for special characters.
658 */
659 tearpath = alloc((unsigned int)STRLEN(menu_path) + TEAR_LEN + 2);
660 if (tearpath != NULL)
661 {
662 char_u *s;
663 int idx;
664
665 STRCPY(tearpath, menu_path);
666 idx = (int)(next_name - path_name - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000667 for (s = tearpath; *s && s < tearpath + idx; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {
669 if ((*s == '\\' || *s == Ctrl_V) && s[1])
670 {
671 ++idx;
672 ++s;
673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
675 tearpath[idx] = NUL;
676 gui_add_tearoff(tearpath, pri_tab, pri_idx);
677 vim_free(tearpath);
678 }
679 }
680# endif
681 }
682#endif /* FEAT_GUI */
683
684 menup = &menu->children;
685 parent = menu;
686 name = next_name;
687 vim_free(dname);
Bram Moolenaar18a0b122006-08-16 13:55:16 +0000688 dname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (pri_tab[pri_idx + 1] != -1)
690 ++pri_idx;
691 }
692 vim_free(path_name);
693
694 /*
695 * Only add system menu items which have not been defined yet.
696 * First check if this was an ":amenu".
697 */
698 amenu = ((modes & (MENU_NORMAL_MODE | MENU_INSERT_MODE)) ==
699 (MENU_NORMAL_MODE | MENU_INSERT_MODE));
700 if (sys_menu)
701 modes &= ~old_modes;
702
703 if (menu != NULL && modes)
704 {
705#ifdef FEAT_GUI
706 menu->cb = gui_menu_cb;
707#endif
708 p = (call_data == NULL) ? NULL : vim_strsave(call_data);
709
710 /* loop over all modes, may add more than one */
711 for (i = 0; i < MENU_MODES; ++i)
712 {
713 if (modes & (1 << i))
714 {
715 /* free any old menu */
716 free_menu_string(menu, i);
717
718 /* For "amenu", may insert an extra character.
719 * Don't do this if adding a tearbar (addtearoff == FALSE).
720 * Don't do this for "<Nop>". */
721 c = 0;
Bram Moolenaar7871a502010-05-14 21:19:23 +0200722 d = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 if (amenu && call_data != NULL && *call_data != NUL
724#ifdef FEAT_GUI_W32
725 && addtearoff
726#endif
727 )
728 {
729 switch (1 << i)
730 {
731 case MENU_VISUAL_MODE:
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000732 case MENU_SELECT_MODE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 case MENU_OP_PENDING_MODE:
734 case MENU_CMDLINE_MODE:
735 c = Ctrl_C;
736 break;
737 case MENU_INSERT_MODE:
Bram Moolenaar7871a502010-05-14 21:19:23 +0200738 c = Ctrl_BSL;
739 d = Ctrl_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 break;
741 }
742 }
743
Bram Moolenaar7871a502010-05-14 21:19:23 +0200744 if (c != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
Bram Moolenaar7871a502010-05-14 21:19:23 +0200746 menu->strings[i] = alloc((unsigned)(STRLEN(call_data) + 5 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 if (menu->strings[i] != NULL)
748 {
749 menu->strings[i][0] = c;
Bram Moolenaar7871a502010-05-14 21:19:23 +0200750 if (d == 0)
751 STRCPY(menu->strings[i] + 1, call_data);
752 else
753 {
754 menu->strings[i][1] = d;
755 STRCPY(menu->strings[i] + 2, call_data);
756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (c == Ctrl_C)
758 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000759 int len = (int)STRLEN(menu->strings[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
761 /* Append CTRL-\ CTRL-G to obey 'insertmode'. */
762 menu->strings[i][len] = Ctrl_BSL;
763 menu->strings[i][len + 1] = Ctrl_G;
764 menu->strings[i][len + 2] = NUL;
765 }
766 }
767 }
768 else
769 menu->strings[i] = p;
770 menu->noremap[i] = menuarg->noremap[0];
771 menu->silent[i] = menuarg->silent[0];
772 }
773 }
774#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
775 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
776 /* Need to update the menu tip. */
777 if (modes & MENU_TIP_MODE)
778 gui_mch_menu_set_tip(menu);
779#endif
780 }
781 return OK;
782
783erret:
784 vim_free(path_name);
785 vim_free(dname);
Bram Moolenaar18a0b122006-08-16 13:55:16 +0000786
787 /* Delete any empty submenu we added before discovering the error. Repeat
788 * for higher levels. */
789 while (parent != NULL && parent->children == NULL)
790 {
791 if (parent->parent == NULL)
792 menup = &root_menu;
793 else
794 menup = &parent->parent->children;
795 for ( ; *menup != NULL && *menup != parent; menup = &((*menup)->next))
796 ;
797 if (*menup == NULL) /* safety check */
798 break;
799 parent = parent->parent;
800 free_menu(menup);
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 return FAIL;
803}
804
805/*
806 * Set the (sub)menu with the given name to enabled or disabled.
807 * Called recursively.
808 */
809 static int
810menu_nable_recurse(menu, name, modes, enable)
811 vimmenu_T *menu;
812 char_u *name;
813 int modes;
814 int enable;
815{
816 char_u *p;
817
818 if (menu == NULL)
819 return OK; /* Got to bottom of hierarchy */
820
821 /* Get name of this element in the menu hierarchy */
822 p = menu_name_skip(name);
823
824 /* Find the menu */
825 while (menu != NULL)
826 {
827 if (*name == NUL || *name == '*' || menu_name_equal(name, menu))
828 {
829 if (*p != NUL)
830 {
831 if (menu->children == NULL)
832 {
833 EMSG(_(e_notsubmenu));
834 return FAIL;
835 }
836 if (menu_nable_recurse(menu->children, p, modes, enable)
837 == FAIL)
838 return FAIL;
839 }
840 else
841 if (enable)
842 menu->enabled |= modes;
843 else
844 menu->enabled &= ~modes;
845
846 /*
847 * When name is empty, we are doing all menu items for the given
848 * modes, so keep looping, otherwise we are just doing the named
849 * menu item (which has been found) so break here.
850 */
851 if (*name != NUL && *name != '*')
852 break;
853 }
854 menu = menu->next;
855 }
856 if (*name != NUL && *name != '*' && menu == NULL)
857 {
Bram Moolenaar342337a2005-07-21 21:11:17 +0000858 EMSG2(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 return FAIL;
860 }
861
862#ifdef FEAT_GUI
863 /* Want to update menus now even if mode not changed */
864 force_menu_update = TRUE;
865#endif
866
867 return OK;
868}
869
870/*
871 * Remove the (sub)menu with the given name from the menu hierarchy
872 * Called recursively.
873 */
874 static int
875remove_menu(menup, name, modes, silent)
876 vimmenu_T **menup;
877 char_u *name;
878 int modes;
879 int silent; /* don't give error messages */
880{
881 vimmenu_T *menu;
882 vimmenu_T *child;
883 char_u *p;
884
885 if (*menup == NULL)
886 return OK; /* Got to bottom of hierarchy */
887
888 /* Get name of this element in the menu hierarchy */
889 p = menu_name_skip(name);
890
891 /* Find the menu */
892 while ((menu = *menup) != NULL)
893 {
894 if (*name == NUL || menu_name_equal(name, menu))
895 {
896 if (*p != NUL && menu->children == NULL)
897 {
898 if (!silent)
899 EMSG(_(e_notsubmenu));
900 return FAIL;
901 }
902 if ((menu->modes & modes) != 0x0)
903 {
904#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
905 /*
906 * If we are removing all entries for this menu,MENU_ALL_MODES,
907 * Then kill any tearoff before we start
908 */
909 if (*p == NUL && modes == MENU_ALL_MODES)
910 {
911 if (IsWindow(menu->tearoff_handle))
912 DestroyWindow(menu->tearoff_handle);
913 }
914#endif
915 if (remove_menu(&menu->children, p, modes, silent) == FAIL)
916 return FAIL;
917 }
918 else if (*name != NUL)
919 {
920 if (!silent)
921 EMSG(_(e_othermode));
922 return FAIL;
923 }
924
925 /*
926 * When name is empty, we are removing all menu items for the given
927 * modes, so keep looping, otherwise we are just removing the named
928 * menu item (which has been found) so break here.
929 */
930 if (*name != NUL)
931 break;
932
933 /* Remove the menu item for the given mode[s]. If the menu item
934 * is no longer valid in ANY mode, delete it */
935 menu->modes &= ~modes;
936 if (modes & MENU_TIP_MODE)
937 free_menu_string(menu, MENU_INDEX_TIP);
938 if ((menu->modes & MENU_ALL_MODES) == 0)
939 free_menu(menup);
940 else
941 menup = &menu->next;
942 }
943 else
944 menup = &menu->next;
945 }
946 if (*name != NUL)
947 {
948 if (menu == NULL)
949 {
950 if (!silent)
Bram Moolenaar342337a2005-07-21 21:11:17 +0000951 EMSG2(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 return FAIL;
953 }
954
955
956 /* Recalculate modes for menu based on the new updated children */
957 menu->modes &= ~modes;
958#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
959 if ((s_tearoffs) && (menu->children != NULL)) /* there's a tear bar.. */
960 child = menu->children->next; /* don't count tearoff bar */
961 else
962#endif
963 child = menu->children;
964 for ( ; child != NULL; child = child->next)
965 menu->modes |= child->modes;
966 if (modes & MENU_TIP_MODE)
967 {
968 free_menu_string(menu, MENU_INDEX_TIP);
969#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
970 && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
971 /* Need to update the menu tip. */
972 if (gui.in_use)
973 gui_mch_menu_set_tip(menu);
974#endif
975 }
976 if ((menu->modes & MENU_ALL_MODES) == 0)
977 {
978 /* The menu item is no longer valid in ANY mode, so delete it */
979#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
980 if (s_tearoffs && menu->children != NULL) /* there's a tear bar.. */
981 free_menu(&menu->children);
982#endif
983 *menup = menu;
984 free_menu(menup);
985 }
986 }
987
988 return OK;
989}
990
991/*
992 * Free the given menu structure and remove it from the linked list.
993 */
994 static void
995free_menu(menup)
996 vimmenu_T **menup;
997{
998 int i;
999 vimmenu_T *menu;
1000
1001 menu = *menup;
1002
1003#ifdef FEAT_GUI
1004 /* Free machine specific menu structures (only when already created) */
1005 /* Also may rebuild a tearoff'ed menu */
1006 if (gui.in_use)
1007 gui_mch_destroy_menu(menu);
1008#endif
1009
1010 /* Don't change *menup until after calling gui_mch_destroy_menu(). The
1011 * MacOS code needs the original structure to properly delete the menu. */
1012 *menup = menu->next;
1013 vim_free(menu->name);
1014 vim_free(menu->dname);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001015#ifdef FEAT_MULTI_LANG
1016 vim_free(menu->en_name);
1017 vim_free(menu->en_dname);
1018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 vim_free(menu->actext);
1020#ifdef FEAT_TOOLBAR
1021 vim_free(menu->iconfile);
Bram Moolenaarbee0c5b2005-02-07 22:03:36 +00001022# ifdef FEAT_GUI_MOTIF
1023 vim_free(menu->xpm_fname);
1024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#endif
1026 for (i = 0; i < MENU_MODES; i++)
1027 free_menu_string(menu, i);
1028 vim_free(menu);
1029
1030#ifdef FEAT_GUI
1031 /* Want to update menus now even if mode not changed */
1032 force_menu_update = TRUE;
1033#endif
1034}
1035
1036/*
1037 * Free the menu->string with the given index.
1038 */
1039 static void
1040free_menu_string(menu, idx)
1041 vimmenu_T *menu;
1042 int idx;
1043{
1044 int count = 0;
1045 int i;
1046
1047 for (i = 0; i < MENU_MODES; i++)
1048 if (menu->strings[i] == menu->strings[idx])
1049 count++;
1050 if (count == 1)
1051 vim_free(menu->strings[idx]);
1052 menu->strings[idx] = NULL;
1053}
1054
1055/*
1056 * Show the mapping associated with a menu item or hierarchy in a sub-menu.
1057 */
1058 static int
1059show_menus(path_name, modes)
1060 char_u *path_name;
1061 int modes;
1062{
1063 char_u *p;
1064 char_u *name;
1065 vimmenu_T *menu;
1066 vimmenu_T *parent = NULL;
1067
1068 menu = root_menu;
1069 name = path_name = vim_strsave(path_name);
1070 if (path_name == NULL)
1071 return FAIL;
1072
1073 /* First, find the (sub)menu with the given name */
1074 while (*name)
1075 {
1076 p = menu_name_skip(name);
1077 while (menu != NULL)
1078 {
1079 if (menu_name_equal(name, menu))
1080 {
1081 /* Found menu */
1082 if (*p != NUL && menu->children == NULL)
1083 {
1084 EMSG(_(e_notsubmenu));
1085 vim_free(path_name);
1086 return FAIL;
1087 }
1088 else if ((menu->modes & modes) == 0x0)
1089 {
1090 EMSG(_(e_othermode));
1091 vim_free(path_name);
1092 return FAIL;
1093 }
1094 break;
1095 }
1096 menu = menu->next;
1097 }
1098 if (menu == NULL)
1099 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00001100 EMSG2(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 vim_free(path_name);
1102 return FAIL;
1103 }
1104 name = p;
1105 parent = menu;
1106 menu = menu->children;
1107 }
Bram Moolenaaracbd4422008-08-17 21:44:45 +00001108 vim_free(path_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
1110 /* Now we have found the matching menu, and we list the mappings */
1111 /* Highlight title */
1112 MSG_PUTS_TITLE(_("\n--- Menus ---"));
1113
1114 show_menus_recursive(parent, modes, 0);
1115 return OK;
1116}
1117
1118/*
1119 * Recursively show the mappings associated with the menus under the given one
1120 */
1121 static void
1122show_menus_recursive(menu, modes, depth)
1123 vimmenu_T *menu;
1124 int modes;
1125 int depth;
1126{
1127 int i;
1128 int bit;
1129
1130 if (menu != NULL && (menu->modes & modes) == 0x0)
1131 return;
1132
1133 if (menu != NULL)
1134 {
1135 msg_putchar('\n');
1136 if (got_int) /* "q" hit for "--more--" */
1137 return;
1138 for (i = 0; i < depth; i++)
1139 MSG_PUTS(" ");
1140 if (menu->priority)
1141 {
1142 msg_outnum((long)menu->priority);
1143 MSG_PUTS(" ");
1144 }
1145 /* Same highlighting as for directories!? */
1146 msg_outtrans_attr(menu->name, hl_attr(HLF_D));
1147 }
1148
1149 if (menu != NULL && menu->children == NULL)
1150 {
1151 for (bit = 0; bit < MENU_MODES; bit++)
1152 if ((menu->modes & modes & (1 << bit)) != 0)
1153 {
1154 msg_putchar('\n');
1155 if (got_int) /* "q" hit for "--more--" */
1156 return;
1157 for (i = 0; i < depth + 2; i++)
1158 MSG_PUTS(" ");
1159 msg_putchar(menu_mode_chars[bit]);
1160 if (menu->noremap[bit] == REMAP_NONE)
1161 msg_putchar('*');
1162 else if (menu->noremap[bit] == REMAP_SCRIPT)
1163 msg_putchar('&');
1164 else
1165 msg_putchar(' ');
1166 if (menu->silent[bit])
1167 msg_putchar('s');
1168 else
1169 msg_putchar(' ');
1170 if ((menu->modes & menu->enabled & (1 << bit)) == 0)
1171 msg_putchar('-');
1172 else
1173 msg_putchar(' ');
1174 MSG_PUTS(" ");
1175 if (*menu->strings[bit] == NUL)
1176 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
1177 else
1178 msg_outtrans_special(menu->strings[bit], FALSE);
1179 }
1180 }
1181 else
1182 {
1183 if (menu == NULL)
1184 {
1185 menu = root_menu;
1186 depth--;
1187 }
1188 else
1189 menu = menu->children;
1190
1191 /* recursively show all children. Skip PopUp[nvoci]. */
1192 for (; menu != NULL && !got_int; menu = menu->next)
1193 if (!menu_is_hidden(menu->dname))
1194 show_menus_recursive(menu, modes, depth + 1);
1195 }
1196}
1197
1198#ifdef FEAT_CMDL_COMPL
1199
1200/*
1201 * Used when expanding menu names.
1202 */
1203static vimmenu_T *expand_menu = NULL;
1204static int expand_modes = 0x0;
1205static int expand_emenu; /* TRUE for ":emenu" command */
1206
1207/*
1208 * Work out what to complete when doing command line completion of menu names.
1209 */
1210 char_u *
1211set_context_in_menu_cmd(xp, cmd, arg, forceit)
1212 expand_T *xp;
1213 char_u *cmd;
1214 char_u *arg;
1215 int forceit;
1216{
1217 char_u *after_dot;
1218 char_u *p;
1219 char_u *path_name = NULL;
1220 char_u *name;
1221 int unmenu;
1222 vimmenu_T *menu;
1223 int expand_menus;
1224
1225 xp->xp_context = EXPAND_UNSUCCESSFUL;
1226
1227
1228 /* Check for priority numbers, enable and disable */
1229 for (p = arg; *p; ++p)
1230 if (!VIM_ISDIGIT(*p) && *p != '.')
1231 break;
1232
1233 if (!vim_iswhite(*p))
1234 {
1235 if (STRNCMP(arg, "enable", 6) == 0
1236 && (arg[6] == NUL || vim_iswhite(arg[6])))
1237 p = arg + 6;
1238 else if (STRNCMP(arg, "disable", 7) == 0
1239 && (arg[7] == NUL || vim_iswhite(arg[7])))
1240 p = arg + 7;
1241 else
1242 p = arg;
1243 }
1244
1245 while (*p != NUL && vim_iswhite(*p))
1246 ++p;
1247
1248 arg = after_dot = p;
1249
1250 for (; *p && !vim_iswhite(*p); ++p)
1251 {
1252 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
1253 p++;
1254 else if (*p == '.')
1255 after_dot = p + 1;
1256 }
1257
1258 /* ":tearoff" and ":popup" only use menus, not entries */
1259 expand_menus = !((*cmd == 't' && cmd[1] == 'e') || *cmd == 'p');
1260 expand_emenu = (*cmd == 'e');
1261 if (expand_menus && vim_iswhite(*p))
1262 return NULL; /* TODO: check for next command? */
1263 if (*p == NUL) /* Complete the menu name */
1264 {
1265 /*
1266 * With :unmenu, you only want to match menus for the appropriate mode.
1267 * With :menu though you might want to add a menu with the same name as
1268 * one in another mode, so match menus from other modes too.
1269 */
1270 expand_modes = get_menu_cmd_modes(cmd, forceit, NULL, &unmenu);
1271 if (!unmenu)
1272 expand_modes = MENU_ALL_MODES;
1273
1274 menu = root_menu;
1275 if (after_dot != arg)
1276 {
1277 path_name = alloc((unsigned)(after_dot - arg));
1278 if (path_name == NULL)
1279 return NULL;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001280 vim_strncpy(path_name, arg, after_dot - arg - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 name = path_name;
1283 while (name != NULL && *name)
1284 {
1285 p = menu_name_skip(name);
1286 while (menu != NULL)
1287 {
1288 if (menu_name_equal(name, menu))
1289 {
1290 /* Found menu */
1291 if ((*p != NUL && menu->children == NULL)
1292 || ((menu->modes & expand_modes) == 0x0))
1293 {
1294 /*
1295 * Menu path continues, but we have reached a leaf.
1296 * Or menu exists only in another mode.
1297 */
1298 vim_free(path_name);
1299 return NULL;
1300 }
1301 break;
1302 }
1303 menu = menu->next;
1304 }
1305 if (menu == NULL)
1306 {
1307 /* No menu found with the name we were looking for */
1308 vim_free(path_name);
1309 return NULL;
1310 }
1311 name = p;
1312 menu = menu->children;
1313 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001314 vim_free(path_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
1316 xp->xp_context = expand_menus ? EXPAND_MENUNAMES : EXPAND_MENUS;
1317 xp->xp_pattern = after_dot;
1318 expand_menu = menu;
1319 }
1320 else /* We're in the mapping part */
1321 xp->xp_context = EXPAND_NOTHING;
1322 return NULL;
1323}
1324
1325/*
1326 * Function given to ExpandGeneric() to obtain the list of (sub)menus (not
1327 * entries).
1328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 char_u *
1330get_menu_name(xp, idx)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001331 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 int idx;
1333{
1334 static vimmenu_T *menu = NULL;
1335 char_u *str;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001336#ifdef FEAT_MULTI_LANG
1337 static int should_advance = FALSE;
1338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 if (idx == 0) /* first call: start at first item */
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 menu = expand_menu;
Bram Moolenaar41375642010-05-16 12:49:27 +02001343#ifdef FEAT_MULTI_LANG
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001344 should_advance = FALSE;
Bram Moolenaar41375642010-05-16 12:49:27 +02001345#endif
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 /* Skip PopUp[nvoci]. */
1349 while (menu != NULL && (menu_is_hidden(menu->dname)
1350 || menu_is_separator(menu->dname)
1351 || menu_is_tearoff(menu->dname)
1352 || menu->children == NULL))
1353 menu = menu->next;
1354
1355 if (menu == NULL) /* at end of linked list */
1356 return NULL;
1357
1358 if (menu->modes & expand_modes)
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001359#ifdef FEAT_MULTI_LANG
1360 if (should_advance)
1361 str = menu->en_dname;
1362 else
1363 {
1364#endif
1365 str = menu->dname;
1366#ifdef FEAT_MULTI_LANG
1367 if (menu->en_dname == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001368 should_advance = TRUE;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001369 }
1370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 else
1372 str = (char_u *)"";
1373
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001374#ifdef FEAT_MULTI_LANG
1375 if (should_advance)
1376#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001377 /* Advance to next menu entry. */
1378 menu = menu->next;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001379
1380#ifdef FEAT_MULTI_LANG
1381 should_advance = !should_advance;
1382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
1384 return str;
1385}
1386
1387/*
1388 * Function given to ExpandGeneric() to obtain the list of menus and menu
1389 * entries.
1390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 char_u *
1392get_menu_names(xp, idx)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001393 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 int idx;
1395{
1396 static vimmenu_T *menu = NULL;
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001397#define TBUFFER_LEN 256
1398 static char_u tbuffer[TBUFFER_LEN]; /*hack*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 char_u *str;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001400#ifdef FEAT_MULTI_LANG
1401 static int should_advance = FALSE;
1402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404 if (idx == 0) /* first call: start at first item */
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 menu = expand_menu;
Bram Moolenaar41375642010-05-16 12:49:27 +02001407#ifdef FEAT_MULTI_LANG
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001408 should_advance = FALSE;
Bram Moolenaar41375642010-05-16 12:49:27 +02001409#endif
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412 /* Skip Browse-style entries, popup menus and separators. */
1413 while (menu != NULL
1414 && ( menu_is_hidden(menu->dname)
1415 || (expand_emenu && menu_is_separator(menu->dname))
1416 || menu_is_tearoff(menu->dname)
1417#ifndef FEAT_BROWSE
1418 || menu->dname[STRLEN(menu->dname) - 1] == '.'
1419#endif
1420 ))
1421 menu = menu->next;
1422
1423 if (menu == NULL) /* at end of linked list */
1424 return NULL;
1425
1426 if (menu->modes & expand_modes)
1427 {
1428 if (menu->children != NULL)
1429 {
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001430#ifdef FEAT_MULTI_LANG
1431 if (should_advance)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001432 vim_strncpy(tbuffer, menu->en_dname, TBUFFER_LEN - 2);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001433 else
1434 {
1435#endif
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001436 vim_strncpy(tbuffer, menu->dname, TBUFFER_LEN - 2);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001437#ifdef FEAT_MULTI_LANG
1438 if (menu->en_dname == NULL)
1439 should_advance = TRUE;
1440 }
1441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 /* hack on menu separators: use a 'magic' char for the separator
1443 * so that '.' in names gets escaped properly */
1444 STRCAT(tbuffer, "\001");
1445 str = tbuffer;
1446 }
1447 else
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001448#ifdef FEAT_MULTI_LANG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001449 {
1450 if (should_advance)
1451 str = menu->en_dname;
1452 else
1453 {
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001454#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001455 str = menu->dname;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001456#ifdef FEAT_MULTI_LANG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001457 if (menu->en_dname == NULL)
1458 should_advance = TRUE;
1459 }
1460 }
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 else
1464 str = (char_u *)"";
1465
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001466#ifdef FEAT_MULTI_LANG
1467 if (should_advance)
1468#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001469 /* Advance to next menu entry. */
1470 menu = menu->next;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001471
1472#ifdef FEAT_MULTI_LANG
1473 should_advance = !should_advance;
1474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476 return str;
1477}
1478#endif /* FEAT_CMDL_COMPL */
1479
1480/*
1481 * Skip over this element of the menu path and return the start of the next
1482 * element. Any \ and ^Vs are removed from the current element.
Bram Moolenaar342337a2005-07-21 21:11:17 +00001483 * "name" may be modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 */
1485 char_u *
1486menu_name_skip(name)
1487 char_u *name;
1488{
1489 char_u *p;
1490
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001491 for (p = name; *p && *p != '.'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 {
1493 if (*p == '\\' || *p == Ctrl_V)
1494 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001495 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (*p == NUL)
1497 break;
1498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 if (*p)
1501 *p++ = NUL;
1502 return p;
1503}
1504
1505/*
1506 * Return TRUE when "name" matches with menu "menu". The name is compared in
1507 * two ways: raw menu name and menu name without '&'. ignore part after a TAB.
1508 */
1509 static int
1510menu_name_equal(name, menu)
1511 char_u *name;
1512 vimmenu_T *menu;
1513{
Bram Moolenaar41375642010-05-16 12:49:27 +02001514#ifdef FEAT_MULTI_LANG
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001515 if (menu->en_name != NULL
Bram Moolenaard91f7042011-01-04 17:49:32 +01001516 && (menu_namecmp(name, menu->en_name)
1517 || menu_namecmp(name, menu->en_dname)))
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001518 return TRUE;
Bram Moolenaar41375642010-05-16 12:49:27 +02001519#endif
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001520 return menu_namecmp(name, menu->name) || menu_namecmp(name, menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521}
1522
1523 static int
1524menu_namecmp(name, mname)
1525 char_u *name;
1526 char_u *mname;
1527{
1528 int i;
1529
1530 for (i = 0; name[i] != NUL && name[i] != TAB; ++i)
1531 if (name[i] != mname[i])
1532 break;
1533 return ((name[i] == NUL || name[i] == TAB)
1534 && (mname[i] == NUL || mname[i] == TAB));
1535}
1536
1537/*
1538 * Return the modes specified by the given menu command (eg :menu! returns
1539 * MENU_CMDLINE_MODE | MENU_INSERT_MODE).
1540 * If "noremap" is not NULL, then the flag it points to is set according to
1541 * whether the command is a "nore" command.
1542 * If "unmenu" is not NULL, then the flag it points to is set according to
1543 * whether the command is an "unmenu" command.
1544 */
1545 static int
1546get_menu_cmd_modes(cmd, forceit, noremap, unmenu)
1547 char_u *cmd;
1548 int forceit; /* Was there a "!" after the command? */
1549 int *noremap;
1550 int *unmenu;
1551{
1552 int modes;
1553
1554 switch (*cmd++)
1555 {
1556 case 'v': /* vmenu, vunmenu, vnoremenu */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001557 modes = MENU_VISUAL_MODE | MENU_SELECT_MODE;
1558 break;
1559 case 'x': /* xmenu, xunmenu, xnoremenu */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 modes = MENU_VISUAL_MODE;
1561 break;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001562 case 's': /* smenu, sunmenu, snoremenu */
1563 modes = MENU_SELECT_MODE;
1564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 case 'o': /* omenu */
1566 modes = MENU_OP_PENDING_MODE;
1567 break;
1568 case 'i': /* imenu */
1569 modes = MENU_INSERT_MODE;
1570 break;
1571 case 't':
1572 modes = MENU_TIP_MODE; /* tmenu */
1573 break;
1574 case 'c': /* cmenu */
1575 modes = MENU_CMDLINE_MODE;
1576 break;
1577 case 'a': /* amenu */
1578 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE | MENU_NORMAL_MODE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001579 | MENU_VISUAL_MODE | MENU_SELECT_MODE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001580 | MENU_OP_PENDING_MODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 break;
1582 case 'n':
1583 if (*cmd != 'o') /* nmenu, not noremenu */
1584 {
1585 modes = MENU_NORMAL_MODE;
1586 break;
1587 }
1588 /* FALLTHROUGH */
1589 default:
1590 --cmd;
1591 if (forceit) /* menu!! */
1592 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE;
1593 else /* menu */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001594 modes = MENU_NORMAL_MODE | MENU_VISUAL_MODE | MENU_SELECT_MODE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 | MENU_OP_PENDING_MODE;
1596 }
1597
1598 if (noremap != NULL)
1599 *noremap = (*cmd == 'n' ? REMAP_NONE : REMAP_YES);
1600 if (unmenu != NULL)
1601 *unmenu = (*cmd == 'u');
1602 return modes;
1603}
1604
1605/*
1606 * Modify a menu name starting with "PopUp" to include the mode character.
1607 * Returns the name in allocated memory (NULL for failure).
1608 */
1609 static char_u *
1610popup_mode_name(name, idx)
1611 char_u *name;
1612 int idx;
1613{
1614 char_u *p;
1615 int len = (int)STRLEN(name);
1616
1617 p = vim_strnsave(name, len + 1);
1618 if (p != NULL)
1619 {
1620 mch_memmove(p + 6, p + 5, (size_t)(len - 4));
1621 p[5] = menu_mode_chars[idx];
1622 }
1623 return p;
1624}
1625
1626#if defined(FEAT_GUI) || defined(PROTO)
1627/*
1628 * Return the index into the menu->strings or menu->noremap arrays for the
1629 * current state. Returns MENU_INDEX_INVALID if there is no mapping for the
1630 * given menu in the current mode.
1631 */
1632 int
1633get_menu_index(menu, state)
1634 vimmenu_T *menu;
1635 int state;
1636{
1637 int idx;
1638
1639 if ((state & INSERT))
1640 idx = MENU_INDEX_INSERT;
1641 else if (state & CMDLINE)
1642 idx = MENU_INDEX_CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 else if (VIsual_active)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001644 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001645 if (VIsual_select)
1646 idx = MENU_INDEX_SELECT;
1647 else
1648 idx = MENU_INDEX_VISUAL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 else if (state == HITRETURN || state == ASKMORE)
1651 idx = MENU_INDEX_CMDLINE;
1652 else if (finish_op)
1653 idx = MENU_INDEX_OP_PENDING;
1654 else if ((state & NORMAL))
1655 idx = MENU_INDEX_NORMAL;
1656 else
1657 idx = MENU_INDEX_INVALID;
1658
1659 if (idx != MENU_INDEX_INVALID && menu->strings[idx] == NULL)
1660 idx = MENU_INDEX_INVALID;
1661 return idx;
1662}
1663#endif
1664
1665/*
1666 * Duplicate the menu item text and then process to see if a mnemonic key
1667 * and/or accelerator text has been identified.
1668 * Returns a pointer to allocated memory, or NULL for failure.
1669 * If mnemonic != NULL, *mnemonic is set to the character after the first '&'.
1670 * If actext != NULL, *actext is set to the text after the first TAB.
1671 */
1672 static char_u *
1673menu_text(str, mnemonic, actext)
1674 char_u *str;
1675 int *mnemonic;
1676 char_u **actext;
1677{
1678 char_u *p;
1679 char_u *text;
1680
1681 /* Locate accelerator text, after the first TAB */
1682 p = vim_strchr(str, TAB);
1683 if (p != NULL)
1684 {
1685 if (actext != NULL)
1686 *actext = vim_strsave(p + 1);
1687 text = vim_strnsave(str, (int)(p - str));
1688 }
1689 else
1690 text = vim_strsave(str);
1691
1692 /* Find mnemonic characters "&a" and reduce "&&" to "&". */
1693 for (p = text; p != NULL; )
1694 {
1695 p = vim_strchr(p, '&');
1696 if (p != NULL)
1697 {
1698 if (p[1] == NUL) /* trailing "&" */
1699 break;
1700 if (mnemonic != NULL && p[1] != '&')
1701#if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED)
1702 *mnemonic = p[1];
1703#else
1704 {
1705 /*
1706 * Well there is a bug in the Motif libraries on OS390 Unix.
1707 * The mnemonic keys needs to be converted to ASCII values
1708 * first.
1709 * This behavior has been seen in 2.8 and 2.9.
1710 */
1711 char c = p[1];
1712 __etoa_l(&c, 1);
1713 *mnemonic = c;
1714 }
1715#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001716 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 p = p + 1;
1718 }
1719 }
1720 return text;
1721}
1722
1723/*
1724 * Return TRUE if "name" can be a menu in the MenuBar.
1725 */
1726 int
1727menu_is_menubar(name)
1728 char_u *name;
1729{
1730 return (!menu_is_popup(name)
1731 && !menu_is_toolbar(name)
1732 && *name != MNU_HIDDEN_CHAR);
1733}
1734
1735/*
1736 * Return TRUE if "name" is a popup menu name.
1737 */
1738 int
1739menu_is_popup(name)
1740 char_u *name;
1741{
1742 return (STRNCMP(name, "PopUp", 5) == 0);
1743}
1744
1745#if (defined(FEAT_GUI_MOTIF) && (XmVersion <= 1002)) || defined(PROTO)
1746/*
1747 * Return TRUE if "name" is part of a popup menu.
1748 */
1749 int
1750menu_is_child_of_popup(menu)
1751 vimmenu_T *menu;
1752{
1753 while (menu->parent != NULL)
1754 menu = menu->parent;
1755 return menu_is_popup(menu->name);
1756}
1757#endif
1758
1759/*
1760 * Return TRUE if "name" is a toolbar menu name.
1761 */
1762 int
1763menu_is_toolbar(name)
1764 char_u *name;
1765{
1766 return (STRNCMP(name, "ToolBar", 7) == 0);
1767}
1768
1769/*
1770 * Return TRUE if the name is a menu separator identifier: Starts and ends
1771 * with '-'
1772 */
1773 int
1774menu_is_separator(name)
1775 char_u *name;
1776{
1777 return (name[0] == '-' && name[STRLEN(name) - 1] == '-');
1778}
1779
1780/*
1781 * Return TRUE if the menu is hidden: Starts with ']'
1782 */
1783 static int
1784menu_is_hidden(name)
1785 char_u *name;
1786{
1787 return (name[0] == ']') || (menu_is_popup(name) && name[5] != NUL);
1788}
1789
1790#if defined(FEAT_CMDL_COMPL) \
1791 || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
1792/*
1793 * Return TRUE if the menu is the tearoff menu.
1794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 static int
1796menu_is_tearoff(name)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001797 char_u *name UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799#ifdef FEAT_GUI
1800 return (STRCMP(name, TEAR_STRING) == 0);
1801#else
1802 return FALSE;
1803#endif
1804}
1805#endif
1806
1807#ifdef FEAT_GUI
1808
1809 static int
1810get_menu_mode()
1811{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 if (VIsual_active)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001813 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001814 if (VIsual_select)
1815 return MENU_INDEX_SELECT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 return MENU_INDEX_VISUAL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (State & INSERT)
1819 return MENU_INDEX_INSERT;
1820 if ((State & CMDLINE) || State == ASKMORE || State == HITRETURN)
1821 return MENU_INDEX_CMDLINE;
1822 if (finish_op)
1823 return MENU_INDEX_OP_PENDING;
1824 if (State & NORMAL)
1825 return MENU_INDEX_NORMAL;
1826 if (State & LANGMAP) /* must be a "r" command, like Insert mode */
1827 return MENU_INDEX_INSERT;
1828 return MENU_INDEX_INVALID;
1829}
1830
1831/*
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00001832 * Check that a pointer appears in the menu tree. Used to protect from using
1833 * a menu that was deleted after it was selected but before the event was
1834 * handled.
1835 * Return OK or FAIL. Used recursively.
1836 */
1837 int
1838check_menu_pointer(root, menu_to_check)
1839 vimmenu_T *root;
1840 vimmenu_T *menu_to_check;
1841{
1842 vimmenu_T *p;
1843
1844 for (p = root; p != NULL; p = p->next)
1845 if (p == menu_to_check
1846 || (p->children != NULL
1847 && check_menu_pointer(p->children, menu_to_check) == OK))
1848 return OK;
1849 return FAIL;
1850}
1851
1852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 * After we have started the GUI, then we can create any menus that have been
1854 * defined. This is done once here. add_menu_path() may have already been
1855 * called to define these menus, and may be called again. This function calls
1856 * itself recursively. Should be called at the top level with:
1857 * gui_create_initial_menus(root_menu, NULL);
1858 */
1859 void
1860gui_create_initial_menus(menu)
1861 vimmenu_T *menu;
1862{
1863 int idx = 0;
1864
1865 while (menu != NULL)
1866 {
1867 /* Don't add a menu when only a tip was defined. */
1868 if (menu->modes & MENU_ALL_MODES)
1869 {
1870 if (menu->children != NULL)
1871 {
1872 gui_mch_add_menu(menu, idx);
1873 gui_create_initial_menus(menu->children);
1874 }
1875 else
1876 gui_mch_add_menu_item(menu, idx);
1877 }
1878 menu = menu->next;
1879 ++idx;
1880 }
1881}
1882
1883/*
1884 * Used recursively by gui_update_menus (see below)
1885 */
1886 static void
1887gui_update_menus_recurse(menu, mode)
1888 vimmenu_T *menu;
1889 int mode;
1890{
1891 int grey;
1892
1893 while (menu)
1894 {
1895 if ((menu->modes & menu->enabled & mode)
1896#if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
1897 || menu_is_tearoff(menu->dname)
1898#endif
1899 )
1900 grey = FALSE;
1901 else
1902 grey = TRUE;
1903#ifdef FEAT_GUI_ATHENA
1904 /* Hiding menus doesn't work for Athena, it can cause a crash. */
1905 gui_mch_menu_grey(menu, grey);
1906#else
1907 /* Never hide a toplevel menu, it may make the menubar resize or
1908 * disappear. Same problem for ToolBar items. */
1909 if (vim_strchr(p_go, GO_GREY) != NULL || menu->parent == NULL
1910# ifdef FEAT_TOOLBAR
1911 || menu_is_toolbar(menu->parent->name)
1912# endif
1913 )
1914 gui_mch_menu_grey(menu, grey);
1915 else
1916 gui_mch_menu_hidden(menu, grey);
1917#endif
1918 gui_update_menus_recurse(menu->children, mode);
1919 menu = menu->next;
1920 }
1921}
1922
1923/*
1924 * Make sure only the valid menu items appear for this mode. If
1925 * force_menu_update is not TRUE, then we only do this if the mode has changed
1926 * since last time. If "modes" is not 0, then we use these modes instead.
1927 */
1928 void
1929gui_update_menus(modes)
1930 int modes;
1931{
1932 static int prev_mode = -1;
1933 int mode = 0;
1934
1935 if (modes != 0x0)
1936 mode = modes;
1937 else
1938 {
1939 mode = get_menu_mode();
1940 if (mode == MENU_INDEX_INVALID)
1941 mode = 0;
1942 else
1943 mode = (1 << mode);
1944 }
1945
1946 if (force_menu_update || mode != prev_mode)
1947 {
1948 gui_update_menus_recurse(root_menu, mode);
1949 gui_mch_draw_menubar();
1950 prev_mode = mode;
1951 force_menu_update = FALSE;
1952#ifdef FEAT_GUI_W32
1953 /* This can leave a tearoff as active window - make sure we
1954 * have the focus <negri>*/
1955 gui_mch_activate_window();
1956#endif
1957 }
1958}
1959
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001960#if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MOTIF) \
1961 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962/*
1963 * Check if a key is used as a mnemonic for a toplevel menu.
1964 * Case of the key is ignored.
1965 */
1966 int
1967gui_is_menu_shortcut(key)
1968 int key;
1969{
1970 vimmenu_T *menu;
1971
1972 if (key < 256)
1973 key = TOLOWER_LOC(key);
1974 for (menu = root_menu; menu != NULL; menu = menu->next)
1975 if (menu->mnemonic == key
1976 || (menu->mnemonic < 256 && TOLOWER_LOC(menu->mnemonic) == key))
1977 return TRUE;
1978 return FALSE;
1979}
1980#endif
1981
1982/*
1983 * Display the Special "PopUp" menu as a pop-up at the current mouse
1984 * position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode,
1985 * etc.
1986 */
1987 void
1988gui_show_popupmenu()
1989{
1990 vimmenu_T *menu;
1991 int mode;
1992
1993 mode = get_menu_mode();
1994 if (mode == MENU_INDEX_INVALID)
1995 return;
1996 mode = menu_mode_chars[mode];
1997
Bram Moolenaar342337a2005-07-21 21:11:17 +00001998#ifdef FEAT_AUTOCMD
1999 {
2000 char_u ename[2];
2001
2002 ename[0] = mode;
2003 ename[1] = NUL;
2004 apply_autocmds(EVENT_MENUPOPUP, ename, NULL, FALSE, curbuf);
2005 }
2006#endif
2007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 for (menu = root_menu; menu != NULL; menu = menu->next)
2009 if (STRNCMP("PopUp", menu->name, 5) == 0 && menu->name[5] == mode)
2010 break;
2011
2012 /* Only show a popup when it is defined and has entries */
2013 if (menu != NULL && menu->children != NULL)
2014 gui_mch_show_popupmenu(menu);
2015}
2016#endif /* FEAT_GUI */
2017
2018#if (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)) || defined(PROTO)
2019
2020/*
2021 * Deal with tearoff items that are added like a menu item.
2022 * Currently only for Win32 GUI. Others may follow later.
2023 */
2024
2025 void
2026gui_mch_toggle_tearoffs(int enable)
2027{
2028 int pri_tab[MENUDEPTH + 1];
2029 int i;
2030
2031 if (enable)
2032 {
2033 for (i = 0; i < MENUDEPTH; ++i)
2034 pri_tab[i] = 500;
2035 pri_tab[MENUDEPTH] = -1;
2036 gui_create_tearoffs_recurse(root_menu, (char_u *)"", pri_tab, 0);
2037 }
2038 else
2039 gui_destroy_tearoffs_recurse(root_menu);
2040 s_tearoffs = enable;
2041}
2042
2043/*
2044 * Recursively add tearoff items
2045 */
2046 static void
2047gui_create_tearoffs_recurse(menu, pname, pri_tab, pri_idx)
2048 vimmenu_T *menu;
2049 const char_u *pname;
2050 int *pri_tab;
2051 int pri_idx;
2052{
2053 char_u *newpname = NULL;
2054 int len;
2055 char_u *s;
2056 char_u *d;
2057
2058 if (pri_tab[pri_idx + 1] != -1)
2059 ++pri_idx;
2060 while (menu != NULL)
2061 {
2062 if (menu->children != NULL && menu_is_menubar(menu->name))
2063 {
2064 /* Add the menu name to the menu path. Insert a backslash before
2065 * dots (it's used to separate menu names). */
2066 len = (int)STRLEN(pname) + (int)STRLEN(menu->name);
2067 for (s = menu->name; *s; ++s)
2068 if (*s == '.' || *s == '\\')
2069 ++len;
2070 newpname = alloc(len + TEAR_LEN + 2);
2071 if (newpname != NULL)
2072 {
2073 STRCPY(newpname, pname);
2074 d = newpname + STRLEN(newpname);
2075 for (s = menu->name; *s; ++s)
2076 {
2077 if (*s == '.' || *s == '\\')
2078 *d++ = '\\';
2079 *d++ = *s;
2080 }
2081 *d = NUL;
2082
2083 /* check if tearoff already exists */
2084 if (STRCMP(menu->children->name, TEAR_STRING) != 0)
2085 {
2086 gui_add_tearoff(newpname, pri_tab, pri_idx - 1);
2087 *d = NUL; /* remove TEAR_STRING */
2088 }
2089
2090 STRCAT(newpname, ".");
2091 gui_create_tearoffs_recurse(menu->children, newpname,
2092 pri_tab, pri_idx);
2093 vim_free(newpname);
2094 }
2095 }
2096 menu = menu->next;
2097 }
2098}
2099
2100/*
2101 * Add tear-off menu item for a submenu.
2102 * "tearpath" is the menu path, and must have room to add TEAR_STRING.
2103 */
2104 static void
2105gui_add_tearoff(tearpath, pri_tab, pri_idx)
2106 char_u *tearpath;
2107 int *pri_tab;
2108 int pri_idx;
2109{
2110 char_u *tbuf;
2111 int t;
2112 vimmenu_T menuarg;
2113
2114 tbuf = alloc(5 + (unsigned int)STRLEN(tearpath));
2115 if (tbuf != NULL)
2116 {
2117 tbuf[0] = K_SPECIAL;
2118 tbuf[1] = K_SECOND(K_TEAROFF);
2119 tbuf[2] = K_THIRD(K_TEAROFF);
2120 STRCPY(tbuf + 3, tearpath);
2121 STRCAT(tbuf + 3, "\r");
2122
2123 STRCAT(tearpath, ".");
2124 STRCAT(tearpath, TEAR_STRING);
2125
2126 /* Priority of tear-off is always 1 */
2127 t = pri_tab[pri_idx + 1];
2128 pri_tab[pri_idx + 1] = 1;
2129
2130#ifdef FEAT_TOOLBAR
2131 menuarg.iconfile = NULL;
2132 menuarg.iconidx = -1;
2133 menuarg.icon_builtin = FALSE;
2134#endif
2135 menuarg.noremap[0] = REMAP_NONE;
2136 menuarg.silent[0] = TRUE;
2137
2138 menuarg.modes = MENU_ALL_MODES;
2139 add_menu_path(tearpath, &menuarg, pri_tab, tbuf, FALSE);
2140
2141 menuarg.modes = MENU_TIP_MODE;
2142 add_menu_path(tearpath, &menuarg, pri_tab,
2143 (char_u *)_("Tear off this menu"), FALSE);
2144
2145 pri_tab[pri_idx + 1] = t;
2146 vim_free(tbuf);
2147 }
2148}
2149
2150/*
2151 * Recursively destroy tearoff items
2152 */
2153 static void
2154gui_destroy_tearoffs_recurse(menu)
2155 vimmenu_T *menu;
2156{
2157 while (menu)
2158 {
2159 if (menu->children)
2160 {
2161 /* check if tearoff exists */
2162 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
2163 {
2164 /* Disconnect the item and free the memory */
2165 free_menu(&menu->children);
2166 }
2167 if (menu->children != NULL) /* if not the last one */
2168 gui_destroy_tearoffs_recurse(menu->children);
2169 }
2170 menu = menu->next;
2171 }
2172}
2173
2174#endif /* FEAT_GUI_W32 && FEAT_TEAROFF */
2175
2176/*
2177 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2178 * execute it.
2179 */
2180 void
2181ex_emenu(eap)
2182 exarg_T *eap;
2183{
2184 vimmenu_T *menu;
2185 char_u *name;
2186 char_u *saved_name;
2187 char_u *p;
2188 int idx;
2189 char_u *mode;
2190
2191 saved_name = vim_strsave(eap->arg);
2192 if (saved_name == NULL)
2193 return;
2194
2195 menu = root_menu;
2196 name = saved_name;
2197 while (*name)
2198 {
2199 /* Find in the menu hierarchy */
2200 p = menu_name_skip(name);
2201
2202 while (menu != NULL)
2203 {
2204 if (menu_name_equal(name, menu))
2205 {
2206 if (*p == NUL && menu->children != NULL)
2207 {
2208 EMSG(_("E333: Menu path must lead to a menu item"));
2209 menu = NULL;
2210 }
2211 else if (*p != NUL && menu->children == NULL)
2212 {
2213 EMSG(_(e_notsubmenu));
2214 menu = NULL;
2215 }
2216 break;
2217 }
2218 menu = menu->next;
2219 }
2220 if (menu == NULL || *p == NUL)
2221 break;
2222 menu = menu->children;
2223 name = p;
2224 }
2225 vim_free(saved_name);
2226 if (menu == NULL)
2227 {
2228 EMSG2(_("E334: Menu not found: %s"), eap->arg);
2229 return;
2230 }
2231
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002232 /* Found the menu, so execute.
2233 * Use the Insert mode entry when returning to Insert mode. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002234 if (restart_edit
2235#ifdef FEAT_EVAL
2236 && !current_SID
2237#endif
2238 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
2240 mode = (char_u *)"Insert";
2241 idx = MENU_INDEX_INSERT;
2242 }
2243 else if (eap->addr_count)
2244 {
2245 pos_T tpos;
2246
2247 mode = (char_u *)"Visual";
2248 idx = MENU_INDEX_VISUAL;
2249
2250 /* GEDDES: This is not perfect - but it is a
2251 * quick way of detecting whether we are doing this from a
2252 * selection - see if the range matches up with the visual
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002253 * select start and end. */
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002254 if ((curbuf->b_visual.vi_start.lnum == eap->line1)
2255 && (curbuf->b_visual.vi_end.lnum) == eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
2257 /* Set it up for visual mode - equivalent to gv. */
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002258 VIsual_mode = curbuf->b_visual.vi_mode;
2259 tpos = curbuf->b_visual.vi_end;
2260 curwin->w_cursor = curbuf->b_visual.vi_start;
2261 curwin->w_curswant = curbuf->b_visual.vi_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263 else
2264 {
2265 /* Set it up for line-wise visual mode */
2266 VIsual_mode = 'V';
2267 curwin->w_cursor.lnum = eap->line1;
2268 curwin->w_cursor.col = 1;
2269 tpos.lnum = eap->line2;
2270 tpos.col = MAXCOL;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00002271#ifdef FEAT_VIRTUALEDIT
2272 tpos.coladd = 0;
2273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002276 /* Activate visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 VIsual_active = TRUE;
2278 VIsual_reselect = TRUE;
2279 check_cursor();
2280 VIsual = curwin->w_cursor;
2281 curwin->w_cursor = tpos;
2282
2283 check_cursor();
2284
2285 /* Adjust the cursor to make sure it is in the correct pos
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002286 * for exclusive mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (*p_sel == 'e' && gchar_cursor() != NUL)
2288 ++curwin->w_cursor.col;
2289 }
2290 else
2291 {
2292 mode = (char_u *)"Normal";
2293 idx = MENU_INDEX_NORMAL;
2294 }
2295
2296 if (idx != MENU_INDEX_INVALID && menu->strings[idx] != NULL)
2297 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002298 /* When executing a script or function execute the commands right now.
2299 * Otherwise put them in the typeahead buffer. */
Bram Moolenaar9c4b4ab2006-12-05 20:29:56 +00002300#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002301 if (current_SID != 0)
2302 exec_normal_cmd(menu->strings[idx], menu->noremap[idx],
2303 menu->silent[idx]);
2304 else
Bram Moolenaar4463f292005-09-25 22:20:24 +00002305#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002306 ins_typebuf(menu->strings[idx], menu->noremap[idx], 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 TRUE, menu->silent[idx]);
2308 }
2309 else
2310 EMSG2(_("E335: Menu not defined for %s mode"), mode);
2311}
2312
2313#if defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 || (defined(FEAT_GUI_GTK) && defined(FEAT_MENU)) \
2315 || defined(FEAT_BEVAL_TIP) || defined(PROTO)
2316/*
2317 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy.
2318 */
2319 vimmenu_T *
2320gui_find_menu(path_name)
2321 char_u *path_name;
2322{
2323 vimmenu_T *menu = NULL;
2324 char_u *name;
2325 char_u *saved_name;
2326 char_u *p;
2327
2328 menu = root_menu;
2329
2330 saved_name = vim_strsave(path_name);
2331 if (saved_name == NULL)
2332 return NULL;
2333
2334 name = saved_name;
2335 while (*name)
2336 {
2337 /* find the end of one dot-separated name and put a NUL at the dot */
2338 p = menu_name_skip(name);
2339
2340 while (menu != NULL)
2341 {
Bram Moolenaard91f7042011-01-04 17:49:32 +01002342 if (menu_name_equal(name, menu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 if (menu->children == NULL)
2345 {
2346 /* found a menu item instead of a sub-menu */
2347 if (*p == NUL)
2348 EMSG(_("E336: Menu path must lead to a sub-menu"));
2349 else
2350 EMSG(_(e_notsubmenu));
2351 menu = NULL;
2352 goto theend;
2353 }
2354 if (*p == NUL) /* found a full match */
2355 goto theend;
2356 break;
2357 }
2358 menu = menu->next;
2359 }
2360 if (menu == NULL) /* didn't find it */
2361 break;
2362
2363 /* Found a match, search the sub-menu. */
2364 menu = menu->children;
2365 name = p;
2366 }
2367
2368 if (menu == NULL)
2369 EMSG(_("E337: Menu not found - check menu names"));
2370theend:
2371 vim_free(saved_name);
2372 return menu;
2373}
2374#endif
2375
2376#ifdef FEAT_MULTI_LANG
2377/*
2378 * Translation of menu names. Just a simple lookup table.
2379 */
2380
2381typedef struct
2382{
2383 char_u *from; /* English name */
2384 char_u *from_noamp; /* same, without '&' */
2385 char_u *to; /* translated name */
2386} menutrans_T;
2387
2388static garray_T menutrans_ga = {0, 0, 0, 0, NULL};
2389#endif
2390
2391/*
2392 * ":menutrans".
2393 * This function is also defined without the +multi_lang feature, in which
2394 * case the commands are ignored.
2395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 void
2397ex_menutranslate(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002398 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400#ifdef FEAT_MULTI_LANG
2401 char_u *arg = eap->arg;
2402 menutrans_T *tp;
2403 int i;
2404 char_u *from, *from_noamp, *to;
2405
2406 if (menutrans_ga.ga_itemsize == 0)
2407 ga_init2(&menutrans_ga, (int)sizeof(menutrans_T), 5);
2408
2409 /*
2410 * ":menutrans clear": clear all translations.
2411 */
2412 if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5)))
2413 {
2414 tp = (menutrans_T *)menutrans_ga.ga_data;
2415 for (i = 0; i < menutrans_ga.ga_len; ++i)
2416 {
2417 vim_free(tp[i].from);
2418 vim_free(tp[i].from_noamp);
2419 vim_free(tp[i].to);
2420 }
2421 ga_clear(&menutrans_ga);
2422# ifdef FEAT_EVAL
2423 /* Delete all "menutrans_" global variables. */
2424 del_menutrans_vars();
2425# endif
2426 }
2427 else
2428 {
2429 /* ":menutrans from to": add translation */
2430 from = arg;
2431 arg = menu_skip_part(arg);
2432 to = skipwhite(arg);
2433 *arg = NUL;
2434 arg = menu_skip_part(to);
2435 if (arg == to)
2436 EMSG(_(e_invarg));
2437 else
2438 {
2439 if (ga_grow(&menutrans_ga, 1) == OK)
2440 {
2441 tp = (menutrans_T *)menutrans_ga.ga_data;
2442 from = vim_strsave(from);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002443 if (from != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002445 from_noamp = menu_text(from, NULL, NULL);
2446 to = vim_strnsave(to, (int)(arg - to));
2447 if (from_noamp != NULL && to != NULL)
2448 {
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002449 menu_translate_tab_and_shift(from);
2450 menu_translate_tab_and_shift(to);
2451 menu_unescape_name(from);
2452 menu_unescape_name(to);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002453 tp[menutrans_ga.ga_len].from = from;
2454 tp[menutrans_ga.ga_len].from_noamp = from_noamp;
2455 tp[menutrans_ga.ga_len].to = to;
2456 ++menutrans_ga.ga_len;
2457 }
2458 else
2459 {
2460 vim_free(from);
2461 vim_free(from_noamp);
2462 vim_free(to);
2463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465 }
2466 }
2467 }
2468#endif
2469}
2470
2471#if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
2472/*
2473 * Find the character just after one part of a menu name.
2474 */
2475 static char_u *
2476menu_skip_part(p)
2477 char_u *p;
2478{
2479 while (*p != NUL && *p != '.' && !vim_iswhite(*p))
2480 {
2481 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
2482 ++p;
2483 ++p;
2484 }
2485 return p;
2486}
2487#endif
2488
2489#ifdef FEAT_MULTI_LANG
2490/*
2491 * Lookup part of a menu name in the translations.
2492 * Return a pointer to the translation or NULL if not found.
2493 */
2494 static char_u *
2495menutrans_lookup(name, len)
2496 char_u *name;
2497 int len;
2498{
2499 menutrans_T *tp = (menutrans_T *)menutrans_ga.ga_data;
2500 int i;
2501 char_u *dname;
2502
2503 for (i = 0; i < menutrans_ga.ga_len; ++i)
2504 if (STRNCMP(name, tp[i].from, len) == 0 && tp[i].from[len] == NUL)
2505 return tp[i].to;
2506
2507 /* Now try again while ignoring '&' characters. */
2508 i = name[len];
2509 name[len] = NUL;
2510 dname = menu_text(name, NULL, NULL);
2511 name[len] = i;
2512 if (dname != NULL)
2513 {
2514 for (i = 0; i < menutrans_ga.ga_len; ++i)
2515 if (STRCMP(dname, tp[i].from_noamp) == 0)
2516 {
2517 vim_free(dname);
2518 return tp[i].to;
2519 }
2520 vim_free(dname);
2521 }
2522
2523 return NULL;
2524}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002526/*
2527 * Unescape the name in the translate dictionary table.
2528 */
2529 static void
2530menu_unescape_name(name)
2531 char_u *name;
2532{
2533 char_u *p;
2534
2535 for (p = name; *p && *p != '.'; mb_ptr_adv(p))
2536 if (*p == '\\')
2537 STRMOVE(p, p + 1);
2538}
Bram Moolenaar56be9502010-06-06 14:20:26 +02002539#endif /* FEAT_MULTI_LANG */
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002540
2541/*
2542 * Isolate the menu name.
2543 * Skip the menu name, and translate <Tab> into a real TAB.
2544 */
2545 static char_u *
2546menu_translate_tab_and_shift(arg_start)
2547 char_u *arg_start;
2548{
2549 char_u *arg = arg_start;
2550
2551 while (*arg && !vim_iswhite(*arg))
2552 {
2553 if ((*arg == '\\' || *arg == Ctrl_V) && arg[1] != NUL)
2554 arg++;
2555 else if (STRNICMP(arg, "<TAB>", 5) == 0)
2556 {
2557 *arg = TAB;
2558 STRMOVE(arg + 1, arg + 5);
2559 }
2560 arg++;
2561 }
2562 if (*arg != NUL)
2563 *arg++ = NUL;
2564 arg = skipwhite(arg);
2565
2566 return arg;
2567}
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#endif /* FEAT_MENU */