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