blob: 29f58c7c98551c35ad0ff38bc1a33f82708c8e80 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar4f974752019-02-17 17:44:42 +010021#ifdef FEAT_GUI_MSWIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int add_menu_path(char_u *, vimmenu_T *, int *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static int add_menu_path(char_u *, vimmenu_T *, int *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static int menu_nable_recurse(vimmenu_T *menu, char_u *name, int modes, int enable);
27static int remove_menu(vimmenu_T **, char_u *, int, int silent);
28static void free_menu(vimmenu_T **menup);
29static void free_menu_string(vimmenu_T *, int);
30static int show_menus(char_u *, int);
31static void show_menus_recursive(vimmenu_T *, int, int);
32static int menu_name_equal(char_u *name, vimmenu_T *menu);
33static int menu_namecmp(char_u *name, char_u *mname);
34static int get_menu_cmd_modes(char_u *, int, int *, int *);
35static char_u *popup_mode_name(char_u *name, int idx);
36static char_u *menu_text(char_u *text, int *mnemonic, char_u **actext);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar4f974752019-02-17 17:44:42 +010038#if defined(FEAT_GUI_MSWIN) && defined(FEAT_TEAROFF)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void gui_create_tearoffs_recurse(vimmenu_T *menu, const char_u *pname, int *pri_tab, int pri_idx);
40static void gui_add_tearoff(char_u *tearpath, int *pri_tab, int pri_idx);
41static void gui_destroy_tearoffs_recurse(vimmenu_T *menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static int s_tearoffs = FALSE;
43#endif
44
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static int menu_is_hidden(char_u *name);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static int menu_is_tearoff(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
48#if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static char_u *menu_skip_part(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#endif
51#ifdef FEAT_MULTI_LANG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static char_u *menutrans_lookup(char_u *name, int len);
53static void menu_unescape_name(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#endif
55
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static char_u *menu_translate_tab_and_shift(char_u *arg_start);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +020057
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/* The character for each menu mode */
Bram Moolenaar4c5d8152018-10-19 22:36:53 +020059static char *menu_mode_chars[] = {"n", "v", "s", "o", "i", "c", "tl", "t"};
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
61static char_u e_notsubmenu[] = N_("E327: Part of menu-item path is not sub-menu");
Bram Moolenaar342337a2005-07-21 21:11:17 +000062static char_u e_nomenu[] = N_("E329: No menu \"%s\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64#ifdef FEAT_TOOLBAR
65static const char *toolbar_names[] =
66{
67 /* 0 */ "New", "Open", "Save", "Undo", "Redo",
68 /* 5 */ "Cut", "Copy", "Paste", "Print", "Help",
69 /* 10 */ "Find", "SaveAll", "SaveSesn", "NewSesn", "LoadSesn",
70 /* 15 */ "RunScript", "Replace", "WinClose", "WinMax", "WinMin",
71 /* 20 */ "WinSplit", "Shell", "FindPrev", "FindNext", "FindHelp",
72 /* 25 */ "Make", "TagJump", "RunCtags", "WinVSplit", "WinMaxWidth",
73 /* 30 */ "WinMinWidth", "Exit"
74};
75# define TOOLBAR_NAME_COUNT (sizeof(toolbar_names) / sizeof(char *))
76#endif
77
78/*
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020079 * Return TRUE if "name" is a window toolbar menu name.
80 */
81 static int
82menu_is_winbar(char_u *name)
83{
Bram Moolenaar378daf82017-09-23 23:58:28 +020084 return (STRNCMP(name, "WinBar", 6) == 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020085}
86
87 int
88winbar_height(win_T *wp)
89{
90 if (wp->w_winbar != NULL && wp->w_winbar->children != NULL)
91 return 1;
92 return 0;
93}
94
95 static vimmenu_T **
96get_root_menu(char_u *name)
97{
98 if (menu_is_winbar(name))
99 return &curwin->w_winbar;
100 return &root_menu;
101}
102
103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 * Do the :menu command and relatives.
105 */
106 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100107ex_menu(
108 exarg_T *eap) /* Ex command arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109{
110 char_u *menu_path;
111 int modes;
112 char_u *map_to;
113 int noremap;
114 int silent = FALSE;
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +0000115 int special = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 int unmenu;
117 char_u *map_buf;
118 char_u *arg;
119 char_u *p;
120 int i;
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000121#if defined(FEAT_GUI) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 int old_menu_height;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100123# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 int old_toolbar_height;
125# endif
126#endif
127 int pri_tab[MENUDEPTH + 1];
128 int enable = MAYBE; /* TRUE for "menu enable", FALSE for "menu
129 * disable */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifdef FEAT_TOOLBAR
131 char_u *icon = NULL;
132#endif
133 vimmenu_T menuarg;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200134 vimmenu_T **root_menu_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136 modes = get_menu_cmd_modes(eap->cmd, eap->forceit, &noremap, &unmenu);
137 arg = eap->arg;
138
139 for (;;)
140 {
141 if (STRNCMP(arg, "<script>", 8) == 0)
142 {
143 noremap = REMAP_SCRIPT;
144 arg = skipwhite(arg + 8);
145 continue;
146 }
147 if (STRNCMP(arg, "<silent>", 8) == 0)
148 {
149 silent = TRUE;
150 arg = skipwhite(arg + 8);
151 continue;
152 }
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +0000153 if (STRNCMP(arg, "<special>", 9) == 0)
154 {
155 special = TRUE;
156 arg = skipwhite(arg + 9);
157 continue;
158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 break;
160 }
161
162
163 /* Locate an optional "icon=filename" argument. */
164 if (STRNCMP(arg, "icon=", 5) == 0)
165 {
166 arg += 5;
167#ifdef FEAT_TOOLBAR
168 icon = arg;
169#endif
170 while (*arg != NUL && *arg != ' ')
171 {
172 if (*arg == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000173 STRMOVE(arg, arg + 1);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100174 MB_PTR_ADV(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 }
176 if (*arg != NUL)
177 {
178 *arg++ = NUL;
179 arg = skipwhite(arg);
180 }
181 }
182
183 /*
184 * Fill in the priority table.
185 */
186 for (p = arg; *p; ++p)
187 if (!VIM_ISDIGIT(*p) && *p != '.')
188 break;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100189 if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100191 for (i = 0; i < MENUDEPTH && !VIM_ISWHITE(*arg); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
193 pri_tab[i] = getdigits(&arg);
194 if (pri_tab[i] == 0)
195 pri_tab[i] = 500;
196 if (*arg == '.')
197 ++arg;
198 }
199 arg = skipwhite(arg);
200 }
201 else if (eap->addr_count && eap->line2 != 0)
202 {
203 pri_tab[0] = eap->line2;
204 i = 1;
205 }
206 else
207 i = 0;
208 while (i < MENUDEPTH)
209 pri_tab[i++] = 500;
210 pri_tab[MENUDEPTH] = -1; /* mark end of the table */
211
212 /*
213 * Check for "disable" or "enable" argument.
214 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100215 if (STRNCMP(arg, "enable", 6) == 0 && VIM_ISWHITE(arg[6]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {
217 enable = TRUE;
218 arg = skipwhite(arg + 6);
219 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100220 else if (STRNCMP(arg, "disable", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 {
222 enable = FALSE;
223 arg = skipwhite(arg + 7);
224 }
225
226 /*
227 * If there is no argument, display all menus.
228 */
229 if (*arg == NUL)
230 {
231 show_menus(arg, modes);
232 return;
233 }
234
235#ifdef FEAT_TOOLBAR
236 /*
237 * Need to get the toolbar icon index before doing the translation.
238 */
239 menuarg.iconidx = -1;
240 menuarg.icon_builtin = FALSE;
241 if (menu_is_toolbar(arg))
242 {
243 menu_path = menu_skip_part(arg);
244 if (*menu_path == '.')
245 {
246 p = menu_skip_part(++menu_path);
247 if (STRNCMP(menu_path, "BuiltIn", 7) == 0)
248 {
249 if (skipdigits(menu_path + 7) == p)
250 {
251 menuarg.iconidx = atoi((char *)menu_path + 7);
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000252 if (menuarg.iconidx >= (int)TOOLBAR_NAME_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 menuarg.iconidx = -1;
254 else
255 menuarg.icon_builtin = TRUE;
256 }
257 }
258 else
259 {
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000260 for (i = 0; i < (int)TOOLBAR_NAME_COUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 if (STRNCMP(toolbar_names[i], menu_path, p - menu_path)
262 == 0)
263 {
264 menuarg.iconidx = i;
265 break;
266 }
267 }
268 }
269 }
270#endif
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 menu_path = arg;
273 if (*menu_path == '.')
274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100275 semsg(_(e_invarg2), menu_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 goto theend;
277 }
278
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200279 map_to = menu_translate_tab_and_shift(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281 /*
282 * If there is only a menu name, display menus with that name.
283 */
284 if (*map_to == NUL && !unmenu && enable == MAYBE)
285 {
286 show_menus(menu_path, modes);
287 goto theend;
288 }
289 else if (*map_to != NUL && (unmenu || enable != MAYBE))
290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100291 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 goto theend;
293 }
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000294#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 old_menu_height = gui.menu_height;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100296# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 old_toolbar_height = gui.toolbar_height;
298# endif
299#endif
300
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200301 root_menu_ptr = get_root_menu(menu_path);
302 if (root_menu_ptr == &curwin->w_winbar)
303 /* Assume the window toolbar menu will change. */
304 redraw_later(NOT_VALID);
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 if (enable != MAYBE)
307 {
308 /*
309 * Change sensitivity of the menu.
310 * For the PopUp menu, remove a menu for each mode separately.
311 * Careful: menu_nable_recurse() changes menu_path.
312 */
313 if (STRCMP(menu_path, "*") == 0) /* meaning: do all menus */
314 menu_path = (char_u *)"";
315
316 if (menu_is_popup(menu_path))
317 {
318 for (i = 0; i < MENU_INDEX_TIP; ++i)
319 if (modes & (1 << i))
320 {
321 p = popup_mode_name(menu_path, i);
322 if (p != NULL)
323 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200324 menu_nable_recurse(*root_menu_ptr, p, MENU_ALL_MODES,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 enable);
326 vim_free(p);
327 }
328 }
329 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200330 menu_nable_recurse(*root_menu_ptr, menu_path, modes, enable);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 }
332 else if (unmenu)
333 {
334 /*
335 * Delete menu(s).
336 */
337 if (STRCMP(menu_path, "*") == 0) /* meaning: remove all menus */
338 menu_path = (char_u *)"";
339
340 /*
341 * For the PopUp menu, remove a menu for each mode separately.
342 */
343 if (menu_is_popup(menu_path))
344 {
345 for (i = 0; i < MENU_INDEX_TIP; ++i)
346 if (modes & (1 << i))
347 {
348 p = popup_mode_name(menu_path, i);
349 if (p != NULL)
350 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200351 remove_menu(root_menu_ptr, p, MENU_ALL_MODES, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 vim_free(p);
353 }
354 }
355 }
356
357 /* Careful: remove_menu() changes menu_path */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200358 remove_menu(root_menu_ptr, menu_path, modes, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360 else
361 {
362 /*
363 * Add menu(s).
364 * Replace special key codes.
365 */
366 if (STRICMP(map_to, "<nop>") == 0) /* "<Nop>" means nothing */
367 {
368 map_to = (char_u *)"";
369 map_buf = NULL;
370 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000371 else if (modes & MENU_TIP_MODE)
372 map_buf = NULL; /* Menu tips are plain text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 else
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +0000374 map_to = replace_termcodes(map_to, &map_buf, FALSE, TRUE, special);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 menuarg.modes = modes;
376#ifdef FEAT_TOOLBAR
377 menuarg.iconfile = icon;
378#endif
379 menuarg.noremap[0] = noremap;
380 menuarg.silent[0] = silent;
381 add_menu_path(menu_path, &menuarg, pri_tab, map_to
Bram Moolenaar4f974752019-02-17 17:44:42 +0100382#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 , TRUE
384#endif
385 );
386
387 /*
388 * For the PopUp menu, add a menu for each mode separately.
389 */
390 if (menu_is_popup(menu_path))
391 {
392 for (i = 0; i < MENU_INDEX_TIP; ++i)
393 if (modes & (1 << i))
394 {
395 p = popup_mode_name(menu_path, i);
396 if (p != NULL)
397 {
398 /* Include all modes, to make ":amenu" work */
399 menuarg.modes = modes;
400#ifdef FEAT_TOOLBAR
401 menuarg.iconfile = NULL;
402 menuarg.iconidx = -1;
403 menuarg.icon_builtin = FALSE;
404#endif
405 add_menu_path(p, &menuarg, pri_tab, map_to
Bram Moolenaar4f974752019-02-17 17:44:42 +0100406#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 , TRUE
408#endif
409 );
410 vim_free(p);
411 }
412 }
413 }
414
415 vim_free(map_buf);
416 }
417
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000418#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 /* If the menubar height changed, resize the window */
420 if (gui.in_use
421 && (gui.menu_height != old_menu_height
Bram Moolenaar4f974752019-02-17 17:44:42 +0100422# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 || gui.toolbar_height != old_toolbar_height
424# endif
425 ))
Bram Moolenaar04a9d452006-03-27 21:03:26 +0000426 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200428 if (root_menu_ptr == &curwin->w_winbar)
429 {
430 int h = winbar_height(curwin);
431
432 if (h != curwin->w_winbar_height)
433 {
434 if (h == 0)
435 ++curwin->w_height;
436 else if (curwin->w_height > 0)
437 --curwin->w_height;
438 curwin->w_winbar_height = h;
439 }
440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444}
445
446/*
447 * Add the menu with the given name to the menu hierarchy
448 */
449 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100450add_menu_path(
451 char_u *menu_path,
452 vimmenu_T *menuarg, /* passes modes, iconfile, iconidx,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 icon_builtin, silent[0], noremap[0] */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100454 int *pri_tab,
455 char_u *call_data
Bram Moolenaar4f974752019-02-17 17:44:42 +0100456#ifdef FEAT_GUI_MSWIN
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100457 , int addtearoff /* may add tearoff item */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458#endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100459 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460{
461 char_u *path_name;
462 int modes = menuarg->modes;
463 vimmenu_T **menup;
464 vimmenu_T *menu = NULL;
465 vimmenu_T *parent;
466 vimmenu_T **lower_pri;
467 char_u *p;
468 char_u *name;
469 char_u *dname;
470 char_u *next_name;
471 int i;
472 int c;
Bram Moolenaar7871a502010-05-14 21:19:23 +0200473 int d;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474#ifdef FEAT_GUI
475 int idx;
476 int new_idx;
477#endif
478 int pri_idx = 0;
479 int old_modes = 0;
480 int amenu;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200481#ifdef FEAT_MULTI_LANG
482 char_u *en_name;
483 char_u *map_to = NULL;
484#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200485 vimmenu_T **root_menu_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
487 /* Make a copy so we can stuff around with it, since it could be const */
488 path_name = vim_strsave(menu_path);
489 if (path_name == NULL)
490 return FAIL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200491 root_menu_ptr = get_root_menu(menu_path);
492 menup = root_menu_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 parent = NULL;
494 name = path_name;
495 while (*name)
496 {
497 /* Get name of this element in the menu hierarchy, and the simplified
498 * name (without mnemonic and accelerator text). */
499 next_name = menu_name_skip(name);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200500#ifdef FEAT_MULTI_LANG
Bram Moolenaar442b4222010-05-24 21:34:22 +0200501 map_to = menutrans_lookup(name, (int)STRLEN(name));
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200502 if (map_to != NULL)
503 {
504 en_name = name;
505 name = map_to;
506 }
507 else
508 en_name = NULL;
509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 dname = menu_text(name, NULL, NULL);
Bram Moolenaar18a0b122006-08-16 13:55:16 +0000511 if (dname == NULL)
512 goto erret;
513 if (*dname == NUL)
514 {
515 /* Only a mnemonic or accelerator is not valid. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100516 emsg(_("E792: Empty menu name"));
Bram Moolenaar18a0b122006-08-16 13:55:16 +0000517 goto erret;
518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520 /* See if it's already there */
521 lower_pri = menup;
522#ifdef FEAT_GUI
523 idx = 0;
524 new_idx = 0;
525#endif
526 menu = *menup;
527 while (menu != NULL)
528 {
529 if (menu_name_equal(name, menu) || menu_name_equal(dname, menu))
530 {
531 if (*next_name == NUL && menu->children != NULL)
532 {
533 if (!sys_menu)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100534 emsg(_("E330: Menu path must not lead to a sub-menu"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 goto erret;
536 }
537 if (*next_name != NUL && menu->children == NULL
Bram Moolenaar4f974752019-02-17 17:44:42 +0100538#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 && addtearoff
540#endif
541 )
542 {
543 if (!sys_menu)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100544 emsg(_(e_notsubmenu));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 goto erret;
546 }
547 break;
548 }
549 menup = &menu->next;
550
551 /* Count menus, to find where this one needs to be inserted.
552 * Ignore menus that are not in the menubar (PopUp and Toolbar) */
553 if (parent != NULL || menu_is_menubar(menu->name))
554 {
555#ifdef FEAT_GUI
556 ++idx;
557#endif
558 if (menu->priority <= pri_tab[pri_idx])
559 {
560 lower_pri = menup;
561#ifdef FEAT_GUI
562 new_idx = idx;
563#endif
564 }
565 }
566 menu = menu->next;
567 }
568
569 if (menu == NULL)
570 {
571 if (*next_name == NUL && parent == NULL)
572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100573 emsg(_("E331: Must not add menu items directly to menu bar"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 goto erret;
575 }
576
577 if (menu_is_separator(dname) && *next_name != NUL)
578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100579 emsg(_("E332: Separator cannot be part of a menu path"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 goto erret;
581 }
582
583 /* Not already there, so lets add it */
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200584 menu = ALLOC_CLEAR_ONE(vimmenu_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (menu == NULL)
586 goto erret;
587
588 menu->modes = modes;
589 menu->enabled = MENU_ALL_MODES;
590 menu->name = vim_strsave(name);
591 /* separate mnemonic and accelerator text from actual menu name */
592 menu->dname = menu_text(name, &menu->mnemonic, &menu->actext);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +0200593#ifdef FEAT_MULTI_LANG
594 if (en_name != NULL)
595 {
596 menu->en_name = vim_strsave(en_name);
597 menu->en_dname = menu_text(en_name, NULL, NULL);
598 }
599 else
600 {
601 menu->en_name = NULL;
602 menu->en_dname = NULL;
603 }
604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 menu->priority = pri_tab[pri_idx];
606 menu->parent = parent;
607#ifdef FEAT_GUI_MOTIF
608 menu->sensitive = TRUE; /* the default */
609#endif
610#ifdef FEAT_BEVAL_TIP
611 menu->tip = NULL;
612#endif
613#ifdef FEAT_GUI_ATHENA
614 menu->image = None; /* X-Windows definition for NULL*/
615#endif
616
617 /*
618 * Add after menu that has lower priority.
619 */
620 menu->next = *lower_pri;
621 *lower_pri = menu;
622
623 old_modes = 0;
624
625#ifdef FEAT_TOOLBAR
626 menu->iconidx = menuarg->iconidx;
627 menu->icon_builtin = menuarg->icon_builtin;
628 if (*next_name == NUL && menuarg->iconfile != NULL)
629 menu->iconfile = vim_strsave(menuarg->iconfile);
630#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100631#if defined(FEAT_GUI_MSWIN) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 /* the tearoff item must be present in the modes of each item. */
633 if (parent != NULL && menu_is_tearoff(parent->children->dname))
634 parent->children->modes |= modes;
635#endif
636 }
637 else
638 {
639 old_modes = menu->modes;
640
641 /*
642 * If this menu option was previously only available in other
643 * modes, then make sure it's available for this one now
644 * Also enable a menu when it's created or changed.
645 */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100646#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 /* If adding a tearbar (addtearoff == FALSE) don't update modes */
648 if (addtearoff)
649#endif
650 {
651 menu->modes |= modes;
652 menu->enabled |= modes;
653 }
654 }
655
656#ifdef FEAT_GUI
657 /*
658 * Add the menu item when it's used in one of the modes, but not when
659 * only a tooltip is defined.
660 */
661 if ((old_modes & MENU_ALL_MODES) == 0
662 && (menu->modes & MENU_ALL_MODES) != 0)
663 {
664 if (gui.in_use) /* Otherwise it will be added when GUI starts */
665 {
666 if (*next_name == NUL)
667 {
668 /* Real menu item, not sub-menu */
669 gui_mch_add_menu_item(menu, new_idx);
670
671 /* Want to update menus now even if mode not changed */
672 force_menu_update = TRUE;
673 }
674 else
675 {
676 /* Sub-menu (not at end of path yet) */
677 gui_mch_add_menu(menu, new_idx);
678 }
679 }
680
Bram Moolenaar4f974752019-02-17 17:44:42 +0100681# if defined(FEAT_GUI_MSWIN) & defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 /* When adding a new submenu, may add a tearoff item */
683 if ( addtearoff
684 && *next_name
685 && vim_strchr(p_go, GO_TEAROFF) != NULL
686 && menu_is_menubar(name))
687 {
688 char_u *tearpath;
689
690 /*
691 * The pointers next_name & path_name refer to a string with
692 * \'s and ^V's stripped out. But menu_path is a "raw"
693 * string, so we must correct for special characters.
694 */
Bram Moolenaar964b3742019-05-24 18:54:09 +0200695 tearpath = alloc(STRLEN(menu_path) + TEAR_LEN + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (tearpath != NULL)
697 {
698 char_u *s;
699 int idx;
700
701 STRCPY(tearpath, menu_path);
702 idx = (int)(next_name - path_name - 1);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100703 for (s = tearpath; *s && s < tearpath + idx; MB_PTR_ADV(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 if ((*s == '\\' || *s == Ctrl_V) && s[1])
706 {
707 ++idx;
708 ++s;
709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711 tearpath[idx] = NUL;
712 gui_add_tearoff(tearpath, pri_tab, pri_idx);
713 vim_free(tearpath);
714 }
715 }
716# endif
717 }
718#endif /* FEAT_GUI */
719
720 menup = &menu->children;
721 parent = menu;
722 name = next_name;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100723 VIM_CLEAR(dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 if (pri_tab[pri_idx + 1] != -1)
725 ++pri_idx;
726 }
727 vim_free(path_name);
728
729 /*
730 * Only add system menu items which have not been defined yet.
731 * First check if this was an ":amenu".
732 */
733 amenu = ((modes & (MENU_NORMAL_MODE | MENU_INSERT_MODE)) ==
734 (MENU_NORMAL_MODE | MENU_INSERT_MODE));
735 if (sys_menu)
736 modes &= ~old_modes;
737
738 if (menu != NULL && modes)
739 {
740#ifdef FEAT_GUI
741 menu->cb = gui_menu_cb;
742#endif
743 p = (call_data == NULL) ? NULL : vim_strsave(call_data);
744
745 /* loop over all modes, may add more than one */
746 for (i = 0; i < MENU_MODES; ++i)
747 {
748 if (modes & (1 << i))
749 {
750 /* free any old menu */
751 free_menu_string(menu, i);
752
753 /* For "amenu", may insert an extra character.
754 * Don't do this if adding a tearbar (addtearoff == FALSE).
755 * Don't do this for "<Nop>". */
756 c = 0;
Bram Moolenaar7871a502010-05-14 21:19:23 +0200757 d = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (amenu && call_data != NULL && *call_data != NUL
Bram Moolenaar4f974752019-02-17 17:44:42 +0100759#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 && addtearoff
761#endif
762 )
763 {
764 switch (1 << i)
765 {
766 case MENU_VISUAL_MODE:
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000767 case MENU_SELECT_MODE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 case MENU_OP_PENDING_MODE:
769 case MENU_CMDLINE_MODE:
770 c = Ctrl_C;
771 break;
772 case MENU_INSERT_MODE:
Bram Moolenaar7871a502010-05-14 21:19:23 +0200773 c = Ctrl_BSL;
774 d = Ctrl_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 break;
776 }
777 }
778
Bram Moolenaar7871a502010-05-14 21:19:23 +0200779 if (c != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200781 menu->strings[i] = alloc(STRLEN(call_data) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (menu->strings[i] != NULL)
783 {
784 menu->strings[i][0] = c;
Bram Moolenaar7871a502010-05-14 21:19:23 +0200785 if (d == 0)
786 STRCPY(menu->strings[i] + 1, call_data);
787 else
788 {
789 menu->strings[i][1] = d;
790 STRCPY(menu->strings[i] + 2, call_data);
791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (c == Ctrl_C)
793 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000794 int len = (int)STRLEN(menu->strings[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /* Append CTRL-\ CTRL-G to obey 'insertmode'. */
797 menu->strings[i][len] = Ctrl_BSL;
798 menu->strings[i][len + 1] = Ctrl_G;
799 menu->strings[i][len + 2] = NUL;
800 }
801 }
802 }
803 else
804 menu->strings[i] = p;
805 menu->noremap[i] = menuarg->noremap[0];
806 menu->silent[i] = menuarg->silent[0];
807 }
808 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100809#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN) \
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100810 && (defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_GTK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 /* Need to update the menu tip. */
812 if (modes & MENU_TIP_MODE)
813 gui_mch_menu_set_tip(menu);
814#endif
815 }
816 return OK;
817
818erret:
819 vim_free(path_name);
820 vim_free(dname);
Bram Moolenaar18a0b122006-08-16 13:55:16 +0000821
822 /* Delete any empty submenu we added before discovering the error. Repeat
823 * for higher levels. */
824 while (parent != NULL && parent->children == NULL)
825 {
826 if (parent->parent == NULL)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200827 menup = root_menu_ptr;
Bram Moolenaar18a0b122006-08-16 13:55:16 +0000828 else
829 menup = &parent->parent->children;
830 for ( ; *menup != NULL && *menup != parent; menup = &((*menup)->next))
831 ;
832 if (*menup == NULL) /* safety check */
833 break;
834 parent = parent->parent;
835 free_menu(menup);
836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 return FAIL;
838}
839
840/*
841 * Set the (sub)menu with the given name to enabled or disabled.
842 * Called recursively.
843 */
844 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100845menu_nable_recurse(
846 vimmenu_T *menu,
847 char_u *name,
848 int modes,
849 int enable)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 char_u *p;
852
853 if (menu == NULL)
854 return OK; /* Got to bottom of hierarchy */
855
856 /* Get name of this element in the menu hierarchy */
857 p = menu_name_skip(name);
858
859 /* Find the menu */
860 while (menu != NULL)
861 {
862 if (*name == NUL || *name == '*' || menu_name_equal(name, menu))
863 {
864 if (*p != NUL)
865 {
866 if (menu->children == NULL)
867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100868 emsg(_(e_notsubmenu));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 return FAIL;
870 }
871 if (menu_nable_recurse(menu->children, p, modes, enable)
872 == FAIL)
873 return FAIL;
874 }
875 else
876 if (enable)
877 menu->enabled |= modes;
878 else
879 menu->enabled &= ~modes;
880
881 /*
882 * When name is empty, we are doing all menu items for the given
883 * modes, so keep looping, otherwise we are just doing the named
884 * menu item (which has been found) so break here.
885 */
886 if (*name != NUL && *name != '*')
887 break;
888 }
889 menu = menu->next;
890 }
891 if (*name != NUL && *name != '*' && menu == NULL)
892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100893 semsg(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 return FAIL;
895 }
896
897#ifdef FEAT_GUI
898 /* Want to update menus now even if mode not changed */
899 force_menu_update = TRUE;
900#endif
901
902 return OK;
903}
904
905/*
906 * Remove the (sub)menu with the given name from the menu hierarchy
907 * Called recursively.
908 */
909 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100910remove_menu(
911 vimmenu_T **menup,
912 char_u *name,
913 int modes,
914 int silent) /* don't give error messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 vimmenu_T *menu;
917 vimmenu_T *child;
918 char_u *p;
919
920 if (*menup == NULL)
921 return OK; /* Got to bottom of hierarchy */
922
923 /* Get name of this element in the menu hierarchy */
924 p = menu_name_skip(name);
925
926 /* Find the menu */
927 while ((menu = *menup) != NULL)
928 {
929 if (*name == NUL || menu_name_equal(name, menu))
930 {
931 if (*p != NUL && menu->children == NULL)
932 {
933 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100934 emsg(_(e_notsubmenu));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 return FAIL;
936 }
937 if ((menu->modes & modes) != 0x0)
938 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100939#if defined(FEAT_GUI_MSWIN) & defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 /*
941 * If we are removing all entries for this menu,MENU_ALL_MODES,
942 * Then kill any tearoff before we start
943 */
944 if (*p == NUL && modes == MENU_ALL_MODES)
945 {
946 if (IsWindow(menu->tearoff_handle))
947 DestroyWindow(menu->tearoff_handle);
948 }
949#endif
950 if (remove_menu(&menu->children, p, modes, silent) == FAIL)
951 return FAIL;
952 }
953 else if (*name != NUL)
954 {
955 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100956 emsg(_(e_menuothermode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 return FAIL;
958 }
959
960 /*
961 * When name is empty, we are removing all menu items for the given
962 * modes, so keep looping, otherwise we are just removing the named
963 * menu item (which has been found) so break here.
964 */
965 if (*name != NUL)
966 break;
967
968 /* Remove the menu item for the given mode[s]. If the menu item
969 * is no longer valid in ANY mode, delete it */
970 menu->modes &= ~modes;
971 if (modes & MENU_TIP_MODE)
972 free_menu_string(menu, MENU_INDEX_TIP);
973 if ((menu->modes & MENU_ALL_MODES) == 0)
974 free_menu(menup);
975 else
976 menup = &menu->next;
977 }
978 else
979 menup = &menu->next;
980 }
981 if (*name != NUL)
982 {
983 if (menu == NULL)
984 {
985 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 semsg(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 return FAIL;
988 }
989
990
991 /* Recalculate modes for menu based on the new updated children */
992 menu->modes &= ~modes;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100993#if defined(FEAT_GUI_MSWIN) & defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if ((s_tearoffs) && (menu->children != NULL)) /* there's a tear bar.. */
995 child = menu->children->next; /* don't count tearoff bar */
996 else
997#endif
998 child = menu->children;
999 for ( ; child != NULL; child = child->next)
1000 menu->modes |= child->modes;
1001 if (modes & MENU_TIP_MODE)
1002 {
1003 free_menu_string(menu, MENU_INDEX_TIP);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001004#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN) \
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001005 && (defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_GTK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 /* Need to update the menu tip. */
1007 if (gui.in_use)
1008 gui_mch_menu_set_tip(menu);
1009#endif
1010 }
1011 if ((menu->modes & MENU_ALL_MODES) == 0)
1012 {
1013 /* The menu item is no longer valid in ANY mode, so delete it */
Bram Moolenaar4f974752019-02-17 17:44:42 +01001014#if defined(FEAT_GUI_MSWIN) & defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (s_tearoffs && menu->children != NULL) /* there's a tear bar.. */
1016 free_menu(&menu->children);
1017#endif
1018 *menup = menu;
1019 free_menu(menup);
1020 }
1021 }
1022
1023 return OK;
1024}
1025
1026/*
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001027 * Remove the WinBar menu from window "wp".
1028 */
1029 void
1030remove_winbar(win_T *wp)
1031{
1032 remove_menu(&wp->w_winbar, (char_u *)"", MENU_ALL_MODES, TRUE);
1033 vim_free(wp->w_winbar_items);
1034}
1035
1036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 * Free the given menu structure and remove it from the linked list.
1038 */
1039 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001040free_menu(vimmenu_T **menup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 int i;
1043 vimmenu_T *menu;
1044
1045 menu = *menup;
1046
1047#ifdef FEAT_GUI
1048 /* Free machine specific menu structures (only when already created) */
1049 /* Also may rebuild a tearoff'ed menu */
1050 if (gui.in_use)
1051 gui_mch_destroy_menu(menu);
1052#endif
1053
1054 /* Don't change *menup until after calling gui_mch_destroy_menu(). The
1055 * MacOS code needs the original structure to properly delete the menu. */
1056 *menup = menu->next;
1057 vim_free(menu->name);
1058 vim_free(menu->dname);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001059#ifdef FEAT_MULTI_LANG
1060 vim_free(menu->en_name);
1061 vim_free(menu->en_dname);
1062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 vim_free(menu->actext);
1064#ifdef FEAT_TOOLBAR
1065 vim_free(menu->iconfile);
Bram Moolenaarbee0c5b2005-02-07 22:03:36 +00001066# ifdef FEAT_GUI_MOTIF
1067 vim_free(menu->xpm_fname);
1068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069#endif
1070 for (i = 0; i < MENU_MODES; i++)
1071 free_menu_string(menu, i);
1072 vim_free(menu);
1073
1074#ifdef FEAT_GUI
1075 /* Want to update menus now even if mode not changed */
1076 force_menu_update = TRUE;
1077#endif
1078}
1079
1080/*
1081 * Free the menu->string with the given index.
1082 */
1083 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001084free_menu_string(vimmenu_T *menu, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 int count = 0;
1087 int i;
1088
1089 for (i = 0; i < MENU_MODES; i++)
1090 if (menu->strings[i] == menu->strings[idx])
1091 count++;
1092 if (count == 1)
1093 vim_free(menu->strings[idx]);
1094 menu->strings[idx] = NULL;
1095}
1096
1097/*
1098 * Show the mapping associated with a menu item or hierarchy in a sub-menu.
1099 */
1100 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001101show_menus(char_u *path_name, int modes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 char_u *p;
1104 char_u *name;
1105 vimmenu_T *menu;
1106 vimmenu_T *parent = NULL;
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 name = path_name = vim_strsave(path_name);
1109 if (path_name == NULL)
1110 return FAIL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001111 menu = *get_root_menu(path_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
1113 /* First, find the (sub)menu with the given name */
1114 while (*name)
1115 {
1116 p = menu_name_skip(name);
1117 while (menu != NULL)
1118 {
1119 if (menu_name_equal(name, menu))
1120 {
1121 /* Found menu */
1122 if (*p != NUL && menu->children == NULL)
1123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001124 emsg(_(e_notsubmenu));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 vim_free(path_name);
1126 return FAIL;
1127 }
1128 else if ((menu->modes & modes) == 0x0)
1129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001130 emsg(_(e_menuothermode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 vim_free(path_name);
1132 return FAIL;
1133 }
1134 break;
1135 }
1136 menu = menu->next;
1137 }
1138 if (menu == NULL)
1139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001140 semsg(_(e_nomenu), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 vim_free(path_name);
1142 return FAIL;
1143 }
1144 name = p;
1145 parent = menu;
1146 menu = menu->children;
1147 }
Bram Moolenaaracbd4422008-08-17 21:44:45 +00001148 vim_free(path_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 /* Now we have found the matching menu, and we list the mappings */
1151 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001152 msg_puts_title(_("\n--- Menus ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
1154 show_menus_recursive(parent, modes, 0);
1155 return OK;
1156}
1157
1158/*
1159 * Recursively show the mappings associated with the menus under the given one
1160 */
1161 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001162show_menus_recursive(vimmenu_T *menu, int modes, int depth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163{
1164 int i;
1165 int bit;
1166
1167 if (menu != NULL && (menu->modes & modes) == 0x0)
1168 return;
1169
1170 if (menu != NULL)
1171 {
1172 msg_putchar('\n');
1173 if (got_int) /* "q" hit for "--more--" */
1174 return;
1175 for (i = 0; i < depth; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001176 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (menu->priority)
1178 {
1179 msg_outnum((long)menu->priority);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001180 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
1182 /* Same highlighting as for directories!? */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001183 msg_outtrans_attr(menu->name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185
1186 if (menu != NULL && menu->children == NULL)
1187 {
1188 for (bit = 0; bit < MENU_MODES; bit++)
1189 if ((menu->modes & modes & (1 << bit)) != 0)
1190 {
1191 msg_putchar('\n');
1192 if (got_int) /* "q" hit for "--more--" */
1193 return;
1194 for (i = 0; i < depth + 2; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001195 msg_puts(" ");
1196 msg_puts(menu_mode_chars[bit]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if (menu->noremap[bit] == REMAP_NONE)
1198 msg_putchar('*');
1199 else if (menu->noremap[bit] == REMAP_SCRIPT)
1200 msg_putchar('&');
1201 else
1202 msg_putchar(' ');
1203 if (menu->silent[bit])
1204 msg_putchar('s');
1205 else
1206 msg_putchar(' ');
1207 if ((menu->modes & menu->enabled & (1 << bit)) == 0)
1208 msg_putchar('-');
1209 else
1210 msg_putchar(' ');
Bram Moolenaar32526b32019-01-19 17:43:09 +01001211 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (*menu->strings[bit] == NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001213 msg_puts_attr("<Nop>", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 else
Bram Moolenaar725310d2019-04-24 23:08:23 +02001215 msg_outtrans_special(menu->strings[bit], FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217 }
1218 else
1219 {
1220 if (menu == NULL)
1221 {
1222 menu = root_menu;
1223 depth--;
1224 }
1225 else
1226 menu = menu->children;
1227
1228 /* recursively show all children. Skip PopUp[nvoci]. */
1229 for (; menu != NULL && !got_int; menu = menu->next)
1230 if (!menu_is_hidden(menu->dname))
1231 show_menus_recursive(menu, modes, depth + 1);
1232 }
1233}
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
1236 * Used when expanding menu names.
1237 */
1238static vimmenu_T *expand_menu = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001239static vimmenu_T *expand_menu_alt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240static int expand_modes = 0x0;
1241static int expand_emenu; /* TRUE for ":emenu" command */
1242
1243/*
1244 * Work out what to complete when doing command line completion of menu names.
1245 */
1246 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001247set_context_in_menu_cmd(
1248 expand_T *xp,
1249 char_u *cmd,
1250 char_u *arg,
1251 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 char_u *after_dot;
1254 char_u *p;
1255 char_u *path_name = NULL;
1256 char_u *name;
1257 int unmenu;
1258 vimmenu_T *menu;
1259 int expand_menus;
1260
1261 xp->xp_context = EXPAND_UNSUCCESSFUL;
1262
1263
1264 /* Check for priority numbers, enable and disable */
1265 for (p = arg; *p; ++p)
1266 if (!VIM_ISDIGIT(*p) && *p != '.')
1267 break;
1268
Bram Moolenaar1c465442017-03-12 20:10:05 +01001269 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
1271 if (STRNCMP(arg, "enable", 6) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01001272 && (arg[6] == NUL || VIM_ISWHITE(arg[6])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 p = arg + 6;
1274 else if (STRNCMP(arg, "disable", 7) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01001275 && (arg[7] == NUL || VIM_ISWHITE(arg[7])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 p = arg + 7;
1277 else
1278 p = arg;
1279 }
1280
Bram Moolenaar1c465442017-03-12 20:10:05 +01001281 while (*p != NUL && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 ++p;
1283
1284 arg = after_dot = p;
1285
Bram Moolenaar1c465442017-03-12 20:10:05 +01001286 for (; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 {
1288 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
1289 p++;
1290 else if (*p == '.')
1291 after_dot = p + 1;
1292 }
1293
1294 /* ":tearoff" and ":popup" only use menus, not entries */
1295 expand_menus = !((*cmd == 't' && cmd[1] == 'e') || *cmd == 'p');
1296 expand_emenu = (*cmd == 'e');
Bram Moolenaar1c465442017-03-12 20:10:05 +01001297 if (expand_menus && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 return NULL; /* TODO: check for next command? */
1299 if (*p == NUL) /* Complete the menu name */
1300 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001301 int try_alt_menu = TRUE;
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 /*
1304 * With :unmenu, you only want to match menus for the appropriate mode.
1305 * With :menu though you might want to add a menu with the same name as
1306 * one in another mode, so match menus from other modes too.
1307 */
1308 expand_modes = get_menu_cmd_modes(cmd, forceit, NULL, &unmenu);
1309 if (!unmenu)
1310 expand_modes = MENU_ALL_MODES;
1311
1312 menu = root_menu;
1313 if (after_dot != arg)
1314 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001315 path_name = alloc(after_dot - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (path_name == NULL)
1317 return NULL;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001318 vim_strncpy(path_name, arg, after_dot - arg - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
1320 name = path_name;
1321 while (name != NULL && *name)
1322 {
1323 p = menu_name_skip(name);
1324 while (menu != NULL)
1325 {
1326 if (menu_name_equal(name, menu))
1327 {
1328 /* Found menu */
1329 if ((*p != NUL && menu->children == NULL)
1330 || ((menu->modes & expand_modes) == 0x0))
1331 {
1332 /*
1333 * Menu path continues, but we have reached a leaf.
1334 * Or menu exists only in another mode.
1335 */
1336 vim_free(path_name);
1337 return NULL;
1338 }
1339 break;
1340 }
1341 menu = menu->next;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001342 if (menu == NULL && try_alt_menu)
1343 {
1344 menu = curwin->w_winbar;
1345 try_alt_menu = FALSE;
1346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 if (menu == NULL)
1349 {
1350 /* No menu found with the name we were looking for */
1351 vim_free(path_name);
1352 return NULL;
1353 }
1354 name = p;
1355 menu = menu->children;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001356 try_alt_menu = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001358 vim_free(path_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 xp->xp_context = expand_menus ? EXPAND_MENUNAMES : EXPAND_MENUS;
1361 xp->xp_pattern = after_dot;
1362 expand_menu = menu;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001363 if (expand_menu == root_menu)
1364 expand_menu_alt = curwin->w_winbar;
1365 else
1366 expand_menu_alt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368 else /* We're in the mapping part */
1369 xp->xp_context = EXPAND_NOTHING;
1370 return NULL;
1371}
1372
1373/*
1374 * Function given to ExpandGeneric() to obtain the list of (sub)menus (not
1375 * entries).
1376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001378get_menu_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380 static vimmenu_T *menu = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001381 static int did_alt_menu = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 char_u *str;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001383#ifdef FEAT_MULTI_LANG
1384 static int should_advance = FALSE;
1385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 if (idx == 0) /* first call: start at first item */
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 menu = expand_menu;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001390 did_alt_menu = FALSE;
Bram Moolenaar41375642010-05-16 12:49:27 +02001391#ifdef FEAT_MULTI_LANG
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001392 should_advance = FALSE;
Bram Moolenaar41375642010-05-16 12:49:27 +02001393#endif
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /* Skip PopUp[nvoci]. */
1397 while (menu != NULL && (menu_is_hidden(menu->dname)
1398 || menu_is_separator(menu->dname)
1399 || menu_is_tearoff(menu->dname)
1400 || menu->children == NULL))
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 menu = menu->next;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001403 if (menu == NULL && !did_alt_menu)
1404 {
1405 menu = expand_menu_alt;
1406 did_alt_menu = TRUE;
1407 }
1408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 if (menu == NULL) /* at end of linked list */
1411 return NULL;
1412
1413 if (menu->modes & expand_modes)
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001414#ifdef FEAT_MULTI_LANG
1415 if (should_advance)
1416 str = menu->en_dname;
1417 else
1418 {
1419#endif
1420 str = menu->dname;
1421#ifdef FEAT_MULTI_LANG
1422 if (menu->en_dname == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001423 should_advance = TRUE;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001424 }
1425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 else
1427 str = (char_u *)"";
1428
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001429#ifdef FEAT_MULTI_LANG
1430 if (should_advance)
1431#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001432 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001433 /* Advance to next menu entry. */
1434 menu = menu->next;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001435 if (menu == NULL && !did_alt_menu)
1436 {
1437 menu = expand_menu_alt;
1438 did_alt_menu = TRUE;
1439 }
1440 }
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001441
1442#ifdef FEAT_MULTI_LANG
1443 should_advance = !should_advance;
1444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 return str;
1447}
1448
1449/*
1450 * Function given to ExpandGeneric() to obtain the list of menus and menu
1451 * entries.
1452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001454get_menu_names(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
1456 static vimmenu_T *menu = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001457 static int did_alt_menu = FALSE;
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001458#define TBUFFER_LEN 256
1459 static char_u tbuffer[TBUFFER_LEN]; /*hack*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 char_u *str;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001461#ifdef FEAT_MULTI_LANG
1462 static int should_advance = FALSE;
1463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
1465 if (idx == 0) /* first call: start at first item */
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 menu = expand_menu;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001468 did_alt_menu = FALSE;
Bram Moolenaar41375642010-05-16 12:49:27 +02001469#ifdef FEAT_MULTI_LANG
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001470 should_advance = FALSE;
Bram Moolenaar41375642010-05-16 12:49:27 +02001471#endif
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
1474 /* Skip Browse-style entries, popup menus and separators. */
1475 while (menu != NULL
1476 && ( menu_is_hidden(menu->dname)
1477 || (expand_emenu && menu_is_separator(menu->dname))
1478 || menu_is_tearoff(menu->dname)
1479#ifndef FEAT_BROWSE
1480 || menu->dname[STRLEN(menu->dname) - 1] == '.'
1481#endif
1482 ))
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 menu = menu->next;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001485 if (menu == NULL && !did_alt_menu)
1486 {
1487 menu = expand_menu_alt;
1488 did_alt_menu = TRUE;
1489 }
1490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 if (menu == NULL) /* at end of linked list */
1493 return NULL;
1494
1495 if (menu->modes & expand_modes)
1496 {
1497 if (menu->children != NULL)
1498 {
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001499#ifdef FEAT_MULTI_LANG
1500 if (should_advance)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001501 vim_strncpy(tbuffer, menu->en_dname, TBUFFER_LEN - 2);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001502 else
1503 {
1504#endif
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001505 vim_strncpy(tbuffer, menu->dname, TBUFFER_LEN - 2);
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001506#ifdef FEAT_MULTI_LANG
1507 if (menu->en_dname == NULL)
1508 should_advance = TRUE;
1509 }
1510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 /* hack on menu separators: use a 'magic' char for the separator
1512 * so that '.' in names gets escaped properly */
1513 STRCAT(tbuffer, "\001");
1514 str = tbuffer;
1515 }
1516 else
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001517#ifdef FEAT_MULTI_LANG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001518 {
1519 if (should_advance)
1520 str = menu->en_dname;
1521 else
1522 {
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001523#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001524 str = menu->dname;
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001525#ifdef FEAT_MULTI_LANG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001526 if (menu->en_dname == NULL)
1527 should_advance = TRUE;
1528 }
1529 }
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 else
1533 str = (char_u *)"";
1534
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001535#ifdef FEAT_MULTI_LANG
1536 if (should_advance)
1537#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001538 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001539 /* Advance to next menu entry. */
1540 menu = menu->next;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001541 if (menu == NULL && !did_alt_menu)
1542 {
1543 menu = expand_menu_alt;
1544 did_alt_menu = TRUE;
1545 }
1546 }
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001547
1548#ifdef FEAT_MULTI_LANG
1549 should_advance = !should_advance;
1550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552 return str;
1553}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555/*
1556 * Skip over this element of the menu path and return the start of the next
1557 * element. Any \ and ^Vs are removed from the current element.
Bram Moolenaar342337a2005-07-21 21:11:17 +00001558 * "name" may be modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
1560 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001561menu_name_skip(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563 char_u *p;
1564
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001565 for (p = name; *p && *p != '.'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
1567 if (*p == '\\' || *p == Ctrl_V)
1568 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (*p == NUL)
1571 break;
1572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
1574 if (*p)
1575 *p++ = NUL;
1576 return p;
1577}
1578
1579/*
1580 * Return TRUE when "name" matches with menu "menu". The name is compared in
1581 * two ways: raw menu name and menu name without '&'. ignore part after a TAB.
1582 */
1583 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001584menu_name_equal(char_u *name, vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
Bram Moolenaar41375642010-05-16 12:49:27 +02001586#ifdef FEAT_MULTI_LANG
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001587 if (menu->en_name != NULL
Bram Moolenaard91f7042011-01-04 17:49:32 +01001588 && (menu_namecmp(name, menu->en_name)
1589 || menu_namecmp(name, menu->en_dname)))
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001590 return TRUE;
Bram Moolenaar41375642010-05-16 12:49:27 +02001591#endif
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02001592 return menu_namecmp(name, menu->name) || menu_namecmp(name, menu->dname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593}
1594
1595 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001596menu_namecmp(char_u *name, char_u *mname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 int i;
1599
1600 for (i = 0; name[i] != NUL && name[i] != TAB; ++i)
1601 if (name[i] != mname[i])
1602 break;
1603 return ((name[i] == NUL || name[i] == TAB)
1604 && (mname[i] == NUL || mname[i] == TAB));
1605}
1606
1607/*
1608 * Return the modes specified by the given menu command (eg :menu! returns
1609 * MENU_CMDLINE_MODE | MENU_INSERT_MODE).
1610 * If "noremap" is not NULL, then the flag it points to is set according to
1611 * whether the command is a "nore" command.
1612 * If "unmenu" is not NULL, then the flag it points to is set according to
1613 * whether the command is an "unmenu" command.
1614 */
1615 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001616get_menu_cmd_modes(
1617 char_u *cmd,
1618 int forceit, /* Was there a "!" after the command? */
1619 int *noremap,
1620 int *unmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 int modes;
1623
1624 switch (*cmd++)
1625 {
1626 case 'v': /* vmenu, vunmenu, vnoremenu */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001627 modes = MENU_VISUAL_MODE | MENU_SELECT_MODE;
1628 break;
1629 case 'x': /* xmenu, xunmenu, xnoremenu */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 modes = MENU_VISUAL_MODE;
1631 break;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001632 case 's': /* smenu, sunmenu, snoremenu */
1633 modes = MENU_SELECT_MODE;
1634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 case 'o': /* omenu */
1636 modes = MENU_OP_PENDING_MODE;
1637 break;
1638 case 'i': /* imenu */
1639 modes = MENU_INSERT_MODE;
1640 break;
1641 case 't':
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001642 if (*cmd == 'l') /* tlmenu, tlunmenu, tlnoremenu */
1643 {
1644 modes = MENU_TERMINAL_MODE;
1645 ++cmd;
1646 break;
1647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 modes = MENU_TIP_MODE; /* tmenu */
1649 break;
1650 case 'c': /* cmenu */
1651 modes = MENU_CMDLINE_MODE;
1652 break;
1653 case 'a': /* amenu */
1654 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE | MENU_NORMAL_MODE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001655 | MENU_VISUAL_MODE | MENU_SELECT_MODE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001656 | MENU_OP_PENDING_MODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 break;
1658 case 'n':
1659 if (*cmd != 'o') /* nmenu, not noremenu */
1660 {
1661 modes = MENU_NORMAL_MODE;
1662 break;
1663 }
1664 /* FALLTHROUGH */
1665 default:
1666 --cmd;
1667 if (forceit) /* menu!! */
1668 modes = MENU_INSERT_MODE | MENU_CMDLINE_MODE;
1669 else /* menu */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001670 modes = MENU_NORMAL_MODE | MENU_VISUAL_MODE | MENU_SELECT_MODE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 | MENU_OP_PENDING_MODE;
1672 }
1673
1674 if (noremap != NULL)
1675 *noremap = (*cmd == 'n' ? REMAP_NONE : REMAP_YES);
1676 if (unmenu != NULL)
1677 *unmenu = (*cmd == 'u');
1678 return modes;
1679}
1680
1681/*
1682 * Modify a menu name starting with "PopUp" to include the mode character.
1683 * Returns the name in allocated memory (NULL for failure).
1684 */
1685 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001686popup_mode_name(char_u *name, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
1688 char_u *p;
1689 int len = (int)STRLEN(name);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001690 char *mode_chars = menu_mode_chars[idx];
1691 int mode_chars_len = (int)strlen(mode_chars);
1692 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001694 p = vim_strnsave(name, len + mode_chars_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (p != NULL)
1696 {
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001697 mch_memmove(p + 5 + mode_chars_len, p + 5, (size_t)(len - 4));
1698 for (i = 0; i < mode_chars_len; ++i)
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001699 p[5 + i] = menu_mode_chars[idx][i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 }
1701 return p;
1702}
1703
1704#if defined(FEAT_GUI) || defined(PROTO)
1705/*
1706 * Return the index into the menu->strings or menu->noremap arrays for the
1707 * current state. Returns MENU_INDEX_INVALID if there is no mapping for the
1708 * given menu in the current mode.
1709 */
1710 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001711get_menu_index(vimmenu_T *menu, int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 int idx;
1714
1715 if ((state & INSERT))
1716 idx = MENU_INDEX_INSERT;
1717 else if (state & CMDLINE)
1718 idx = MENU_INDEX_CMDLINE;
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001719#ifdef FEAT_TERMINAL
1720 else if (term_use_loop())
1721 idx = MENU_INDEX_TERMINAL;
1722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 else if (VIsual_active)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001724 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001725 if (VIsual_select)
1726 idx = MENU_INDEX_SELECT;
1727 else
1728 idx = MENU_INDEX_VISUAL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 else if (state == HITRETURN || state == ASKMORE)
1731 idx = MENU_INDEX_CMDLINE;
1732 else if (finish_op)
1733 idx = MENU_INDEX_OP_PENDING;
1734 else if ((state & NORMAL))
1735 idx = MENU_INDEX_NORMAL;
1736 else
1737 idx = MENU_INDEX_INVALID;
1738
1739 if (idx != MENU_INDEX_INVALID && menu->strings[idx] == NULL)
1740 idx = MENU_INDEX_INVALID;
1741 return idx;
1742}
1743#endif
1744
1745/*
1746 * Duplicate the menu item text and then process to see if a mnemonic key
1747 * and/or accelerator text has been identified.
1748 * Returns a pointer to allocated memory, or NULL for failure.
1749 * If mnemonic != NULL, *mnemonic is set to the character after the first '&'.
1750 * If actext != NULL, *actext is set to the text after the first TAB.
1751 */
1752 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001753menu_text(char_u *str, int *mnemonic, char_u **actext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 char_u *p;
1756 char_u *text;
1757
1758 /* Locate accelerator text, after the first TAB */
1759 p = vim_strchr(str, TAB);
1760 if (p != NULL)
1761 {
1762 if (actext != NULL)
1763 *actext = vim_strsave(p + 1);
1764 text = vim_strnsave(str, (int)(p - str));
1765 }
1766 else
1767 text = vim_strsave(str);
1768
1769 /* Find mnemonic characters "&a" and reduce "&&" to "&". */
1770 for (p = text; p != NULL; )
1771 {
1772 p = vim_strchr(p, '&');
1773 if (p != NULL)
1774 {
1775 if (p[1] == NUL) /* trailing "&" */
1776 break;
1777 if (mnemonic != NULL && p[1] != '&')
1778#if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED)
1779 *mnemonic = p[1];
1780#else
1781 {
1782 /*
1783 * Well there is a bug in the Motif libraries on OS390 Unix.
1784 * The mnemonic keys needs to be converted to ASCII values
1785 * first.
1786 * This behavior has been seen in 2.8 and 2.9.
1787 */
1788 char c = p[1];
1789 __etoa_l(&c, 1);
1790 *mnemonic = c;
1791 }
1792#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001793 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 p = p + 1;
1795 }
1796 }
1797 return text;
1798}
1799
1800/*
1801 * Return TRUE if "name" can be a menu in the MenuBar.
1802 */
1803 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001804menu_is_menubar(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805{
1806 return (!menu_is_popup(name)
1807 && !menu_is_toolbar(name)
Bram Moolenaar378daf82017-09-23 23:58:28 +02001808 && !menu_is_winbar(name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 && *name != MNU_HIDDEN_CHAR);
1810}
1811
1812/*
1813 * Return TRUE if "name" is a popup menu name.
1814 */
1815 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001816menu_is_popup(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 return (STRNCMP(name, "PopUp", 5) == 0);
1819}
1820
1821#if (defined(FEAT_GUI_MOTIF) && (XmVersion <= 1002)) || defined(PROTO)
1822/*
1823 * Return TRUE if "name" is part of a popup menu.
1824 */
1825 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001826menu_is_child_of_popup(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 while (menu->parent != NULL)
1829 menu = menu->parent;
1830 return menu_is_popup(menu->name);
1831}
1832#endif
1833
1834/*
1835 * Return TRUE if "name" is a toolbar menu name.
1836 */
1837 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001838menu_is_toolbar(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 return (STRNCMP(name, "ToolBar", 7) == 0);
1841}
1842
1843/*
1844 * Return TRUE if the name is a menu separator identifier: Starts and ends
1845 * with '-'
1846 */
1847 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001848menu_is_separator(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 return (name[0] == '-' && name[STRLEN(name) - 1] == '-');
1851}
1852
1853/*
1854 * Return TRUE if the menu is hidden: Starts with ']'
1855 */
1856 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001857menu_is_hidden(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 return (name[0] == ']') || (menu_is_popup(name) && name[5] != NUL);
1860}
1861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862/*
1863 * Return TRUE if the menu is the tearoff menu.
1864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001866menu_is_tearoff(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
1868#ifdef FEAT_GUI
1869 return (STRCMP(name, TEAR_STRING) == 0);
1870#else
1871 return FALSE;
1872#endif
1873}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001875#if defined(FEAT_GUI) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001878get_menu_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001880#ifdef FEAT_TERMINAL
1881 if (term_use_loop())
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001882 return MENU_INDEX_TERMINAL;
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (VIsual_active)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001885 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001886 if (VIsual_select)
1887 return MENU_INDEX_SELECT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 return MENU_INDEX_VISUAL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (State & INSERT)
1891 return MENU_INDEX_INSERT;
1892 if ((State & CMDLINE) || State == ASKMORE || State == HITRETURN)
1893 return MENU_INDEX_CMDLINE;
1894 if (finish_op)
1895 return MENU_INDEX_OP_PENDING;
1896 if (State & NORMAL)
1897 return MENU_INDEX_NORMAL;
1898 if (State & LANGMAP) /* must be a "r" command, like Insert mode */
1899 return MENU_INDEX_INSERT;
1900 return MENU_INDEX_INVALID;
1901}
1902
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001903 int
1904get_menu_mode_flag(void)
1905{
1906 int mode = get_menu_mode();
1907
1908 if (mode == MENU_INDEX_INVALID)
1909 return 0;
1910 return 1 << mode;
1911}
1912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913/*
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001914 * Display the Special "PopUp" menu as a pop-up at the current mouse
1915 * position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode,
1916 * etc.
1917 */
1918 void
1919show_popupmenu(void)
1920{
1921 vimmenu_T *menu;
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001922 int menu_mode;
1923 char* mode;
1924 int mode_len;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001925
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001926 menu_mode = get_menu_mode();
1927 if (menu_mode == MENU_INDEX_INVALID)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001928 return;
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001929 mode = menu_mode_chars[menu_mode];
1930 mode_len = (int)strlen(mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001931
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001932 apply_autocmds(EVENT_MENUPOPUP, (char_u*)mode, NULL, FALSE, curbuf);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001933
1934 for (menu = root_menu; menu != NULL; menu = menu->next)
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001935 if (STRNCMP("PopUp", menu->name, 5) == 0 && STRNCMP(menu->name + 5, mode, mode_len) == 0)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001936 break;
1937
1938 /* Only show a popup when it is defined and has entries */
1939 if (menu != NULL && menu->children != NULL)
1940 {
1941# if defined(FEAT_GUI)
1942 if (gui.in_use)
1943 {
1944 /* Update the menus now, in case the MenuPopup autocommand did
1945 * anything. */
1946 gui_update_menus(0);
1947 gui_mch_show_popupmenu(menu);
1948 }
1949# endif
1950# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
1951 else
1952# endif
1953# if defined(FEAT_TERM_POPUP_MENU)
1954 pum_show_popupmenu(menu);
1955# endif
1956 }
1957}
1958#endif
1959
1960#if defined(FEAT_GUI) || defined(PROTO)
1961
1962/*
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00001963 * Check that a pointer appears in the menu tree. Used to protect from using
1964 * a menu that was deleted after it was selected but before the event was
1965 * handled.
1966 * Return OK or FAIL. Used recursively.
1967 */
1968 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001969check_menu_pointer(vimmenu_T *root, vimmenu_T *menu_to_check)
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00001970{
1971 vimmenu_T *p;
1972
1973 for (p = root; p != NULL; p = p->next)
1974 if (p == menu_to_check
1975 || (p->children != NULL
1976 && check_menu_pointer(p->children, menu_to_check) == OK))
1977 return OK;
1978 return FAIL;
1979}
1980
1981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 * After we have started the GUI, then we can create any menus that have been
1983 * defined. This is done once here. add_menu_path() may have already been
1984 * called to define these menus, and may be called again. This function calls
1985 * itself recursively. Should be called at the top level with:
Bram Moolenaara06ecab2016-07-16 14:47:36 +02001986 * gui_create_initial_menus(root_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
1988 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001989gui_create_initial_menus(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 int idx = 0;
1992
1993 while (menu != NULL)
1994 {
1995 /* Don't add a menu when only a tip was defined. */
1996 if (menu->modes & MENU_ALL_MODES)
1997 {
1998 if (menu->children != NULL)
1999 {
2000 gui_mch_add_menu(menu, idx);
2001 gui_create_initial_menus(menu->children);
2002 }
2003 else
2004 gui_mch_add_menu_item(menu, idx);
2005 }
2006 menu = menu->next;
2007 ++idx;
2008 }
2009}
2010
2011/*
2012 * Used recursively by gui_update_menus (see below)
2013 */
2014 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002015gui_update_menus_recurse(vimmenu_T *menu, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 int grey;
2018
2019 while (menu)
2020 {
2021 if ((menu->modes & menu->enabled & mode)
Bram Moolenaar4f974752019-02-17 17:44:42 +01002022# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 || menu_is_tearoff(menu->dname)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 )
2026 grey = FALSE;
2027 else
2028 grey = TRUE;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002029# ifdef FEAT_GUI_ATHENA
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 /* Hiding menus doesn't work for Athena, it can cause a crash. */
2031 gui_mch_menu_grey(menu, grey);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002032# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 /* Never hide a toplevel menu, it may make the menubar resize or
2034 * disappear. Same problem for ToolBar items. */
2035 if (vim_strchr(p_go, GO_GREY) != NULL || menu->parent == NULL
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002036# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 || menu_is_toolbar(menu->parent->name)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 )
2040 gui_mch_menu_grey(menu, grey);
2041 else
2042 gui_mch_menu_hidden(menu, grey);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 gui_update_menus_recurse(menu->children, mode);
2045 menu = menu->next;
2046 }
2047}
2048
2049/*
2050 * Make sure only the valid menu items appear for this mode. If
2051 * force_menu_update is not TRUE, then we only do this if the mode has changed
2052 * since last time. If "modes" is not 0, then we use these modes instead.
2053 */
2054 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002055gui_update_menus(int modes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
2057 static int prev_mode = -1;
2058 int mode = 0;
2059
2060 if (modes != 0x0)
2061 mode = modes;
2062 else
Bram Moolenaar29a2c082018-03-05 21:06:23 +01002063 mode = get_menu_mode_flag();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 if (force_menu_update || mode != prev_mode)
2066 {
2067 gui_update_menus_recurse(root_menu, mode);
2068 gui_mch_draw_menubar();
2069 prev_mode = mode;
2070 force_menu_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072}
2073
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002074# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MOTIF) \
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00002075 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076/*
2077 * Check if a key is used as a mnemonic for a toplevel menu.
2078 * Case of the key is ignored.
2079 */
2080 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002081gui_is_menu_shortcut(int key)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 vimmenu_T *menu;
2084
2085 if (key < 256)
2086 key = TOLOWER_LOC(key);
2087 for (menu = root_menu; menu != NULL; menu = menu->next)
2088 if (menu->mnemonic == key
2089 || (menu->mnemonic < 256 && TOLOWER_LOC(menu->mnemonic) == key))
2090 return TRUE;
2091 return FALSE;
2092}
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094#endif /* FEAT_GUI */
2095
Bram Moolenaar4f974752019-02-17 17:44:42 +01002096#if (defined(FEAT_GUI_MSWIN) && defined(FEAT_TEAROFF)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
2098/*
2099 * Deal with tearoff items that are added like a menu item.
2100 * Currently only for Win32 GUI. Others may follow later.
2101 */
2102
2103 void
2104gui_mch_toggle_tearoffs(int enable)
2105{
2106 int pri_tab[MENUDEPTH + 1];
2107 int i;
2108
2109 if (enable)
2110 {
2111 for (i = 0; i < MENUDEPTH; ++i)
2112 pri_tab[i] = 500;
2113 pri_tab[MENUDEPTH] = -1;
2114 gui_create_tearoffs_recurse(root_menu, (char_u *)"", pri_tab, 0);
2115 }
2116 else
2117 gui_destroy_tearoffs_recurse(root_menu);
2118 s_tearoffs = enable;
2119}
2120
2121/*
2122 * Recursively add tearoff items
2123 */
2124 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002125gui_create_tearoffs_recurse(
2126 vimmenu_T *menu,
2127 const char_u *pname,
2128 int *pri_tab,
2129 int pri_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
2131 char_u *newpname = NULL;
2132 int len;
2133 char_u *s;
2134 char_u *d;
2135
2136 if (pri_tab[pri_idx + 1] != -1)
2137 ++pri_idx;
2138 while (menu != NULL)
2139 {
2140 if (menu->children != NULL && menu_is_menubar(menu->name))
2141 {
2142 /* Add the menu name to the menu path. Insert a backslash before
2143 * dots (it's used to separate menu names). */
2144 len = (int)STRLEN(pname) + (int)STRLEN(menu->name);
2145 for (s = menu->name; *s; ++s)
2146 if (*s == '.' || *s == '\\')
2147 ++len;
2148 newpname = alloc(len + TEAR_LEN + 2);
2149 if (newpname != NULL)
2150 {
2151 STRCPY(newpname, pname);
2152 d = newpname + STRLEN(newpname);
2153 for (s = menu->name; *s; ++s)
2154 {
2155 if (*s == '.' || *s == '\\')
2156 *d++ = '\\';
2157 *d++ = *s;
2158 }
2159 *d = NUL;
2160
2161 /* check if tearoff already exists */
2162 if (STRCMP(menu->children->name, TEAR_STRING) != 0)
2163 {
2164 gui_add_tearoff(newpname, pri_tab, pri_idx - 1);
2165 *d = NUL; /* remove TEAR_STRING */
2166 }
2167
2168 STRCAT(newpname, ".");
2169 gui_create_tearoffs_recurse(menu->children, newpname,
2170 pri_tab, pri_idx);
2171 vim_free(newpname);
2172 }
2173 }
2174 menu = menu->next;
2175 }
2176}
2177
2178/*
2179 * Add tear-off menu item for a submenu.
2180 * "tearpath" is the menu path, and must have room to add TEAR_STRING.
2181 */
2182 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002183gui_add_tearoff(char_u *tearpath, int *pri_tab, int pri_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
2185 char_u *tbuf;
2186 int t;
2187 vimmenu_T menuarg;
2188
2189 tbuf = alloc(5 + (unsigned int)STRLEN(tearpath));
2190 if (tbuf != NULL)
2191 {
2192 tbuf[0] = K_SPECIAL;
2193 tbuf[1] = K_SECOND(K_TEAROFF);
2194 tbuf[2] = K_THIRD(K_TEAROFF);
2195 STRCPY(tbuf + 3, tearpath);
2196 STRCAT(tbuf + 3, "\r");
2197
2198 STRCAT(tearpath, ".");
2199 STRCAT(tearpath, TEAR_STRING);
2200
2201 /* Priority of tear-off is always 1 */
2202 t = pri_tab[pri_idx + 1];
2203 pri_tab[pri_idx + 1] = 1;
2204
2205#ifdef FEAT_TOOLBAR
2206 menuarg.iconfile = NULL;
2207 menuarg.iconidx = -1;
2208 menuarg.icon_builtin = FALSE;
2209#endif
2210 menuarg.noremap[0] = REMAP_NONE;
2211 menuarg.silent[0] = TRUE;
2212
2213 menuarg.modes = MENU_ALL_MODES;
2214 add_menu_path(tearpath, &menuarg, pri_tab, tbuf, FALSE);
2215
2216 menuarg.modes = MENU_TIP_MODE;
2217 add_menu_path(tearpath, &menuarg, pri_tab,
2218 (char_u *)_("Tear off this menu"), FALSE);
2219
2220 pri_tab[pri_idx + 1] = t;
2221 vim_free(tbuf);
2222 }
2223}
2224
2225/*
2226 * Recursively destroy tearoff items
2227 */
2228 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002229gui_destroy_tearoffs_recurse(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 while (menu)
2232 {
2233 if (menu->children)
2234 {
2235 /* check if tearoff exists */
2236 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
2237 {
2238 /* Disconnect the item and free the memory */
2239 free_menu(&menu->children);
2240 }
2241 if (menu->children != NULL) /* if not the last one */
2242 gui_destroy_tearoffs_recurse(menu->children);
2243 }
2244 menu = menu->next;
2245 }
2246}
2247
Bram Moolenaar4f974752019-02-17 17:44:42 +01002248#endif /* FEAT_GUI_MSWIN && FEAT_TEAROFF */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250/*
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002251 * Execute "menu". Use by ":emenu" and the window toolbar.
2252 * "eap" is NULL for the window toolbar.
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002253 * "mode_idx" specifies a MENU_INDEX_ value, use -1 to depend on the current
2254 * state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 */
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002256 void
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002257execute_menu(exarg_T *eap, vimmenu_T *menu, int mode_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258{
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002259 int idx = mode_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002261 if (idx < 0)
2262 {
2263 /* Use the Insert mode entry when returning to Insert mode. */
2264 if (restart_edit
Bram Moolenaar4463f292005-09-25 22:20:24 +00002265#ifdef FEAT_EVAL
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002266 && !current_sctx.sc_sid
Bram Moolenaar4463f292005-09-25 22:20:24 +00002267#endif
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002268 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002270 idx = MENU_INDEX_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002272#ifdef FEAT_TERMINAL
2273 else if (term_use_loop())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002275 idx = MENU_INDEX_TERMINAL;
2276 }
2277#endif
2278 else if (VIsual_active)
2279 {
2280 idx = MENU_INDEX_VISUAL;
2281 }
2282 else if (eap != NULL && eap->addr_count)
2283 {
2284 pos_T tpos;
2285
2286 idx = MENU_INDEX_VISUAL;
2287
2288 /* GEDDES: This is not perfect - but it is a
2289 * quick way of detecting whether we are doing this from a
2290 * selection - see if the range matches up with the visual
2291 * select start and end. */
2292 if ((curbuf->b_visual.vi_start.lnum == eap->line1)
2293 && (curbuf->b_visual.vi_end.lnum) == eap->line2)
2294 {
2295 /* Set it up for visual mode - equivalent to gv. */
2296 VIsual_mode = curbuf->b_visual.vi_mode;
2297 tpos = curbuf->b_visual.vi_end;
2298 curwin->w_cursor = curbuf->b_visual.vi_start;
2299 curwin->w_curswant = curbuf->b_visual.vi_curswant;
2300 }
2301 else
2302 {
2303 /* Set it up for line-wise visual mode */
2304 VIsual_mode = 'V';
2305 curwin->w_cursor.lnum = eap->line1;
2306 curwin->w_cursor.col = 1;
2307 tpos.lnum = eap->line2;
2308 tpos.col = MAXCOL;
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002309 tpos.coladd = 0;
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002310 }
2311
2312 /* Activate visual mode */
2313 VIsual_active = TRUE;
2314 VIsual_reselect = TRUE;
2315 check_cursor();
2316 VIsual = curwin->w_cursor;
2317 curwin->w_cursor = tpos;
2318
2319 check_cursor();
2320
2321 /* Adjust the cursor to make sure it is in the correct pos
2322 * for exclusive mode */
2323 if (*p_sel == 'e' && gchar_cursor() != NUL)
2324 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
Bram Moolenaara21a6a92017-09-23 16:33:50 +02002327
2328 /* For the WinBar menu always use the Normal mode menu. */
2329 if (idx == -1 || eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 idx = MENU_INDEX_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
Bram Moolenaarce793532019-05-05 14:19:20 +02002332 if (idx != MENU_INDEX_INVALID && menu->strings[idx] != NULL
2333 && (menu->modes & (1 << idx)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002335 /* When executing a script or function execute the commands right now.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002336 * Also for the window toolbar.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002337 * Otherwise put them in the typeahead buffer. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002338 if (eap == NULL
Bram Moolenaar9c4b4ab2006-12-05 20:29:56 +00002339#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002340 || current_sctx.sc_sid != 0
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002341#endif
2342 )
Bram Moolenaara21a6a92017-09-23 16:33:50 +02002343 {
2344 save_state_T save_state;
2345
2346 ++ex_normal_busy;
2347 if (save_current_state(&save_state))
2348 exec_normal_cmd(menu->strings[idx], menu->noremap[idx],
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002349 menu->silent[idx]);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02002350 restore_current_state(&save_state);
2351 --ex_normal_busy;
2352 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002353 else
2354 ins_typebuf(menu->strings[idx], menu->noremap[idx], 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 TRUE, menu->silent[idx]);
2356 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002357 else if (eap != NULL)
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002358 {
2359 char_u *mode;
2360
2361 switch (idx)
2362 {
2363 case MENU_INDEX_VISUAL:
2364 mode = (char_u *)"Visual";
2365 break;
2366 case MENU_INDEX_SELECT:
2367 mode = (char_u *)"Select";
2368 break;
2369 case MENU_INDEX_OP_PENDING:
2370 mode = (char_u *)"Op-pending";
2371 break;
2372 case MENU_INDEX_TERMINAL:
2373 mode = (char_u *)"Terminal";
2374 break;
2375 case MENU_INDEX_INSERT:
2376 mode = (char_u *)"Insert";
2377 break;
2378 case MENU_INDEX_CMDLINE:
2379 mode = (char_u *)"Cmdline";
2380 break;
2381 // case MENU_INDEX_TIP: cannot happen
2382 default:
2383 mode = (char_u *)"Normal";
2384 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002385 semsg(_("E335: Menu not defined for %s mode"), mode);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387}
2388
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002389/*
2390 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2391 * execute it.
2392 */
2393 void
2394ex_emenu(exarg_T *eap)
2395{
2396 vimmenu_T *menu;
2397 char_u *name;
2398 char_u *saved_name;
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002399 char_u *arg = eap->arg;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002400 char_u *p;
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002401 int gave_emsg = FALSE;
2402 int mode_idx = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002403
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002404 if (arg[0] && VIM_ISWHITE(arg[1]))
2405 {
2406 switch (arg[0])
2407 {
2408 case 'n': mode_idx = MENU_INDEX_NORMAL; break;
2409 case 'v': mode_idx = MENU_INDEX_VISUAL; break;
2410 case 's': mode_idx = MENU_INDEX_SELECT; break;
2411 case 'o': mode_idx = MENU_INDEX_OP_PENDING; break;
2412 case 't': mode_idx = MENU_INDEX_TERMINAL; break;
2413 case 'i': mode_idx = MENU_INDEX_INSERT; break;
2414 case 'c': mode_idx = MENU_INDEX_CMDLINE; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002415 default: semsg(_(e_invarg2), arg);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002416 return;
2417 }
2418 arg = skipwhite(arg + 2);
2419 }
2420
2421 saved_name = vim_strsave(arg);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002422 if (saved_name == NULL)
2423 return;
2424
2425 menu = *get_root_menu(saved_name);
2426 name = saved_name;
2427 while (*name)
2428 {
2429 /* Find in the menu hierarchy */
2430 p = menu_name_skip(name);
2431
2432 while (menu != NULL)
2433 {
2434 if (menu_name_equal(name, menu))
2435 {
2436 if (*p == NUL && menu->children != NULL)
2437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 emsg(_("E333: Menu path must lead to a menu item"));
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002439 gave_emsg = TRUE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002440 menu = NULL;
2441 }
2442 else if (*p != NUL && menu->children == NULL)
2443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002444 emsg(_(e_notsubmenu));
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002445 menu = NULL;
2446 }
2447 break;
2448 }
2449 menu = menu->next;
2450 }
2451 if (menu == NULL || *p == NUL)
2452 break;
2453 menu = menu->children;
2454 name = p;
2455 }
2456 vim_free(saved_name);
2457 if (menu == NULL)
2458 {
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002459 if (!gave_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002460 semsg(_("E334: Menu not found: %s"), arg);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002461 return;
2462 }
2463
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002464 // Found the menu, so execute.
2465 execute_menu(eap, menu, mode_idx);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002466}
2467
2468/*
2469 * Handle a click in the window toolbar of "wp" at column "col".
2470 */
2471 void
2472winbar_click(win_T *wp, int col)
2473{
2474 int idx;
2475
2476 if (wp->w_winbar_items == NULL)
2477 return;
2478 for (idx = 0; wp->w_winbar_items[idx].wb_menu != NULL; ++idx)
2479 {
2480 winbar_item_T *item = &wp->w_winbar_items[idx];
2481
2482 if (col >= item->wb_startcol && col <= item->wb_endcol)
2483 {
Bram Moolenaard2fad672019-05-04 16:55:25 +02002484 win_T *save_curwin = NULL;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02002485 pos_T save_visual = VIsual;
2486 int save_visual_active = VIsual_active;
2487 int save_visual_select = VIsual_select;
2488 int save_visual_reselect = VIsual_reselect;
2489 int save_visual_mode = VIsual_mode;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002490
2491 if (wp != curwin)
2492 {
2493 /* Clicking in the window toolbar of a not-current window.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02002494 * Make that window the current one and save Visual mode. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002495 save_curwin = curwin;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02002496 VIsual_active = FALSE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002497 curwin = wp;
2498 curbuf = curwin->w_buffer;
2499 check_cursor();
2500 }
2501
Bram Moolenaard2fad672019-05-04 16:55:25 +02002502 // Note: the command might close the current window.
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02002503 execute_menu(NULL, item->wb_menu, -1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002504
Bram Moolenaard2fad672019-05-04 16:55:25 +02002505 if (save_curwin != NULL && win_valid(save_curwin))
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002506 {
2507 curwin = save_curwin;
2508 curbuf = curwin->w_buffer;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02002509 VIsual = save_visual;
2510 VIsual_active = save_visual_active;
2511 VIsual_select = save_visual_select;
2512 VIsual_reselect = save_visual_reselect;
2513 VIsual_mode = save_visual_mode;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002514 }
Bram Moolenaard2fad672019-05-04 16:55:25 +02002515 if (!win_valid(wp))
2516 break;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002517 }
2518 }
2519}
2520
2521#if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
Bram Moolenaar40d77b02018-03-05 21:32:27 +01002522 || defined(FEAT_TERM_POPUP_MENU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 || defined(FEAT_BEVAL_TIP) || defined(PROTO)
2524/*
2525 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy.
2526 */
2527 vimmenu_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002528gui_find_menu(char_u *path_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530 vimmenu_T *menu = NULL;
2531 char_u *name;
2532 char_u *saved_name;
2533 char_u *p;
2534
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002535 menu = *get_root_menu(path_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
2537 saved_name = vim_strsave(path_name);
2538 if (saved_name == NULL)
2539 return NULL;
2540
2541 name = saved_name;
2542 while (*name)
2543 {
2544 /* find the end of one dot-separated name and put a NUL at the dot */
2545 p = menu_name_skip(name);
2546
2547 while (menu != NULL)
2548 {
Bram Moolenaard91f7042011-01-04 17:49:32 +01002549 if (menu_name_equal(name, menu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
2551 if (menu->children == NULL)
2552 {
2553 /* found a menu item instead of a sub-menu */
2554 if (*p == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002555 emsg(_("E336: Menu path must lead to a sub-menu"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002557 emsg(_(e_notsubmenu));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 menu = NULL;
2559 goto theend;
2560 }
2561 if (*p == NUL) /* found a full match */
2562 goto theend;
2563 break;
2564 }
2565 menu = menu->next;
2566 }
2567 if (menu == NULL) /* didn't find it */
2568 break;
2569
2570 /* Found a match, search the sub-menu. */
2571 menu = menu->children;
2572 name = p;
2573 }
2574
2575 if (menu == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002576 emsg(_("E337: Menu not found - check menu names"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577theend:
2578 vim_free(saved_name);
2579 return menu;
2580}
2581#endif
2582
2583#ifdef FEAT_MULTI_LANG
2584/*
2585 * Translation of menu names. Just a simple lookup table.
2586 */
2587
2588typedef struct
2589{
2590 char_u *from; /* English name */
2591 char_u *from_noamp; /* same, without '&' */
2592 char_u *to; /* translated name */
2593} menutrans_T;
2594
2595static garray_T menutrans_ga = {0, 0, 0, 0, NULL};
2596#endif
2597
2598/*
2599 * ":menutrans".
2600 * This function is also defined without the +multi_lang feature, in which
2601 * case the commands are ignored.
2602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002604ex_menutranslate(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606#ifdef FEAT_MULTI_LANG
2607 char_u *arg = eap->arg;
2608 menutrans_T *tp;
2609 int i;
2610 char_u *from, *from_noamp, *to;
2611
2612 if (menutrans_ga.ga_itemsize == 0)
2613 ga_init2(&menutrans_ga, (int)sizeof(menutrans_T), 5);
2614
2615 /*
2616 * ":menutrans clear": clear all translations.
2617 */
2618 if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5)))
2619 {
2620 tp = (menutrans_T *)menutrans_ga.ga_data;
2621 for (i = 0; i < menutrans_ga.ga_len; ++i)
2622 {
2623 vim_free(tp[i].from);
2624 vim_free(tp[i].from_noamp);
2625 vim_free(tp[i].to);
2626 }
2627 ga_clear(&menutrans_ga);
2628# ifdef FEAT_EVAL
2629 /* Delete all "menutrans_" global variables. */
2630 del_menutrans_vars();
2631# endif
2632 }
2633 else
2634 {
2635 /* ":menutrans from to": add translation */
2636 from = arg;
2637 arg = menu_skip_part(arg);
2638 to = skipwhite(arg);
2639 *arg = NUL;
2640 arg = menu_skip_part(to);
2641 if (arg == to)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002642 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 else
2644 {
2645 if (ga_grow(&menutrans_ga, 1) == OK)
2646 {
2647 tp = (menutrans_T *)menutrans_ga.ga_data;
2648 from = vim_strsave(from);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002649 if (from != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002651 from_noamp = menu_text(from, NULL, NULL);
2652 to = vim_strnsave(to, (int)(arg - to));
2653 if (from_noamp != NULL && to != NULL)
2654 {
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002655 menu_translate_tab_and_shift(from);
2656 menu_translate_tab_and_shift(to);
2657 menu_unescape_name(from);
2658 menu_unescape_name(to);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002659 tp[menutrans_ga.ga_len].from = from;
2660 tp[menutrans_ga.ga_len].from_noamp = from_noamp;
2661 tp[menutrans_ga.ga_len].to = to;
2662 ++menutrans_ga.ga_len;
2663 }
2664 else
2665 {
2666 vim_free(from);
2667 vim_free(from_noamp);
2668 vim_free(to);
2669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671 }
2672 }
2673 }
2674#endif
2675}
2676
2677#if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
2678/*
2679 * Find the character just after one part of a menu name.
2680 */
2681 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002682menu_skip_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
Bram Moolenaar1c465442017-03-12 20:10:05 +01002684 while (*p != NUL && *p != '.' && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 {
2686 if ((*p == '\\' || *p == Ctrl_V) && p[1] != NUL)
2687 ++p;
2688 ++p;
2689 }
2690 return p;
2691}
2692#endif
2693
2694#ifdef FEAT_MULTI_LANG
2695/*
2696 * Lookup part of a menu name in the translations.
2697 * Return a pointer to the translation or NULL if not found.
2698 */
2699 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002700menutrans_lookup(char_u *name, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 menutrans_T *tp = (menutrans_T *)menutrans_ga.ga_data;
2703 int i;
2704 char_u *dname;
2705
2706 for (i = 0; i < menutrans_ga.ga_len; ++i)
Bram Moolenaar11dd8c12017-03-04 20:41:34 +01002707 if (STRNICMP(name, tp[i].from, len) == 0 && tp[i].from[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 return tp[i].to;
2709
2710 /* Now try again while ignoring '&' characters. */
2711 i = name[len];
2712 name[len] = NUL;
2713 dname = menu_text(name, NULL, NULL);
2714 name[len] = i;
2715 if (dname != NULL)
2716 {
2717 for (i = 0; i < menutrans_ga.ga_len; ++i)
Bram Moolenaar11dd8c12017-03-04 20:41:34 +01002718 if (STRICMP(dname, tp[i].from_noamp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 vim_free(dname);
2721 return tp[i].to;
2722 }
2723 vim_free(dname);
2724 }
2725
2726 return NULL;
2727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002729/*
2730 * Unescape the name in the translate dictionary table.
2731 */
2732 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002733menu_unescape_name(char_u *name)
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002734{
2735 char_u *p;
2736
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002737 for (p = name; *p && *p != '.'; MB_PTR_ADV(p))
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002738 if (*p == '\\')
2739 STRMOVE(p, p + 1);
2740}
Bram Moolenaar56be9502010-06-06 14:20:26 +02002741#endif /* FEAT_MULTI_LANG */
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002742
2743/*
2744 * Isolate the menu name.
2745 * Skip the menu name, and translate <Tab> into a real TAB.
2746 */
2747 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002748menu_translate_tab_and_shift(char_u *arg_start)
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002749{
2750 char_u *arg = arg_start;
2751
Bram Moolenaar1c465442017-03-12 20:10:05 +01002752 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar70b11cd2010-05-14 22:24:40 +02002753 {
2754 if ((*arg == '\\' || *arg == Ctrl_V) && arg[1] != NUL)
2755 arg++;
2756 else if (STRNICMP(arg, "<TAB>", 5) == 0)
2757 {
2758 *arg = TAB;
2759 STRMOVE(arg + 1, arg + 5);
2760 }
2761 arg++;
2762 }
2763 if (*arg != NUL)
2764 *arg++ = NUL;
2765 arg = skipwhite(arg);
2766
2767 return arg;
2768}
2769
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770#endif /* FEAT_MENU */