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