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