robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// Code for menus. Used for the GUI and 'wildmenu'. 2// GUI/Motif support by Robert Webb 3 4#include <assert.h> 5#include <stdbool.h> 6#include <stdint.h> 7#include <string.h> 8 9#include "nvim/ascii_defs.h" 10#include "nvim/autocmd.h" 11#include "nvim/autocmd_defs.h" 12#include "nvim/buffer_defs.h" 13#include "nvim/charset.h" 14#include "nvim/cmdexpand_defs.h" 15#include "nvim/cursor.h" 16#include "nvim/errors.h" 17#include "nvim/eval/typval.h" 18#include "nvim/eval/typval_defs.h" 19#include "nvim/eval/vars.h" 20#include "nvim/ex_cmds_defs.h" 21#include "nvim/ex_docmd.h" 22#include "nvim/garray.h" 23#include "nvim/garray_defs.h" 24#include "nvim/gettext_defs.h" 25#include "nvim/globals.h" 26#include "nvim/highlight_defs.h" 27#include "nvim/input.h" 28#include "nvim/input_defs.h" 29#include "nvim/keycodes.h" 30#include "nvim/macros_defs.h" 31#include "nvim/mbyte.h" 32#include "nvim/mbyte_defs.h" 33#include "nvim/memory.h" 34#include "nvim/menu.h" 35#include "nvim/menu_defs.h" 36#include "nvim/message.h" 37#include "nvim/option_vars.h" 38#include "nvim/popupmenu.h" 39#include "nvim/pos_defs.h" 40#include "nvim/state.h" 41#include "nvim/state_defs.h" 42#include "nvim/strings.h" 43#include "nvim/types_defs.h" 44#include "nvim/ui.h" 45#include "nvim/vim_defs.h" 46 47#define MENUDEPTH 10 // maximum depth of menus 48 49#include "menu.c.generated.h" 50 51/// When non-zero no menu must be added or cleared. Prevents the list of menus 52/// changing while listing them. 53static int menus_locked = 0 ; 54 55/// The character for each menu mode 56static char * menu_mode_chars []= {"n" ,"v" ,"s" ,"o" ,"i" ,"c" ,"tl" ,"t" }; 57 58static const char e_notsubmenu []= N_ ("E327: Part of menu-item path is not sub-menu" ); 59static const char e_nomenu []= N_ ("E329: No menu \"%s\"" ); 60 61// Return true if "name" is a window toolbar menu name. 62static bool menu_is_winbar (const char * const name ) 63FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 64{ 65return (strncmp (name ,"WinBar" ,6 )== 0 ); 66} 67 68static vimmenu_T ** get_root_menu (const char * const name ) 69FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 70{ 71return & root_menu ; 72} 73 74/// If "menus_locked" is set then give an error and return true. 75/// Otherwise return false. 76static int is_menus_locked (void ) 77{ 78if (menus_locked > 0 ) { 79emsg (_ (e_cannot_change_menus_while_listing )); 80return true; 81 } 82return false; 83} 84 85/// Do the :menu command and relatives. 86/// @param eap Ex command arguments 87void ex_menu (exarg_T * eap ) 88{ 89char * map_to ;// command mapped to the menu entry 90int noremap ; 91bool silent = false; 92bool unmenu ; 93char * map_buf ; 94char * p ; 95int i ; 96int pri_tab [MENUDEPTH + 1 ]; 97TriState enable = kNone ;// kTrue for "menu enable", 98// kFalse for "menu disable 99vimmenu_T menuarg ; 100 101int modes = get_menu_cmd_modes (eap -> cmd ,eap -> forceit ,& noremap ,& unmenu ); 102char * arg = eap -> arg ; 103 104while (true) { 105if (strncmp (arg ,"<script>" ,8 )== 0 ) { 106noremap = REMAP_SCRIPT ; 107arg = skipwhite (arg + 8 ); 108continue ; 109 } 110if (strncmp (arg ,"<silent>" ,8 )== 0 ) { 111silent = true; 112arg = skipwhite (arg + 8 ); 113continue ; 114 } 115if (strncmp (arg ,"<special>" ,9 )== 0 ) { 116// Ignore obsolete "<special>" modifier. 117arg = skipwhite (arg + 9 ); 118continue ; 119 } 120break ; 121 } 122 123// Locate an optional "icon=filename" argument 124// TODO(nvim): Currently this is only parsed. Should expose it to UIs. 125if (strncmp (arg ,"icon=" ,5 )== 0 ) { 126arg += 5 ; 127while (* arg != NUL && * arg != ' ' ) { 128if (* arg == '\\' ) { 129STRMOVE (arg ,arg + 1 ); 130 } 131MB_PTR_ADV (arg ); 132 } 133if (* arg != NUL ) { 134* arg ++ = NUL ; 135arg = skipwhite (arg ); 136 } 137 } 138 139// Fill in the priority table. 140for (p = arg ;* p ;p ++ ) { 141if (!ascii_isdigit (* p )&& * p != '.' ) { 142break ; 143 } 144 } 145if (ascii_iswhite (* p )) { 146for (i = 0 ;i < MENUDEPTH && !ascii_iswhite (* arg );i ++ ) { 147pri_tab [i ]= getdigits_int (& arg , false,0 ); 148if (pri_tab [i ]== 0 ) { 149pri_tab [i ]= 500 ; 150 } 151if (* arg == '.' ) { 152arg ++ ; 153 } 154 } 155arg = skipwhite (arg ); 156 }else if (eap -> addr_count && eap -> line2 != 0 ) { 157pri_tab [0 ]= eap -> line2 ; 158i = 1 ; 159 }else { 160i = 0 ; 161 } 162while (i < MENUDEPTH ) { 163pri_tab [i ++ ]= 500 ; 164 } 165pri_tab [MENUDEPTH ]= -1 ;// mark end of the table 166 167// Check for "disable" or "enable" argument. 168if (strncmp (arg ,"enable" ,6 )== 0 && ascii_iswhite (arg [6 ])) { 169enable = kTrue ; 170arg = skipwhite (arg + 6 ); 171 }else if (strncmp (arg ,"disable" ,7 )== 0 && ascii_iswhite (arg [7 ])) { 172enable = kFalse ; 173arg = skipwhite (arg + 7 ); 174 } 175 176// If there is no argument, display all menus. 177if (* arg == NUL ) { 178show_menus (arg ,modes ); 179return ; 180 } 181 182char * menu_path = arg ; 183if (* menu_path == '.' ) { 184semsg (_ (e_invarg2 ),menu_path ); 185 gototheend ; 186 } 187 188map_to = menu_translate_tab_and_shift (arg ); 189 190// If there is only a menu name, display menus with that name. 191if (* map_to == NUL && !unmenu && enable == kNone ) { 192show_menus (menu_path ,modes ); 193 gototheend ; 194 }else if (* map_to != NUL && (unmenu || enable != kNone )) { 195semsg (_ (e_trailing_arg ),map_to ); 196 gototheend ; 197 } 198 199vimmenu_T ** root_menu_ptr = get_root_menu (menu_path ); 200 201if (enable != kNone ) { 202// Change sensitivity of the menu. 203// For the PopUp menu, remove a menu for each mode separately. 204// Careful: menu_enable_recurse() changes menu_path. 205if (strcmp (menu_path ,"*" )== 0 ) {// meaning: do all menus 206menu_path = "" ; 207 } 208 209if (menu_is_popup (menu_path )) { 210for (i = 0 ;i < MENU_INDEX_TIP ;i ++ ) { 211if (modes & (1 <<i )) { 212p = popup_mode_name (menu_path ,i ); 213menu_enable_recurse (* root_menu_ptr ,p ,MENU_ALL_MODES ,enable ); 214xfree (p ); 215 } 216 } 217 } 218menu_enable_recurse (* root_menu_ptr ,menu_path ,modes ,enable ); 219 }else if (unmenu ) { 220if (is_menus_locked ()) { 221 gototheend ; 222 } 223 224// Delete menu(s). 225if (strcmp (menu_path ,"*" )== 0 ) {// meaning: remove all menus 226menu_path = "" ; 227 } 228 229// For the PopUp menu, remove a menu for each mode separately. 230if (menu_is_popup (menu_path )) { 231for (i = 0 ;i < MENU_INDEX_TIP ;i ++ ) { 232if (modes & (1 <<i )) { 233p = popup_mode_name (menu_path ,i ); 234remove_menu (root_menu_ptr ,p ,MENU_ALL_MODES , true); 235xfree (p ); 236 } 237 } 238 } 239 240// Careful: remove_menu() changes menu_path 241remove_menu (root_menu_ptr ,menu_path ,modes , false); 242 }else { 243if (is_menus_locked ()) { 244 gototheend ; 245 } 246 247// Add menu(s). 248// Replace special key codes. 249if (STRICMP (map_to ,"<nop>" )== 0 ) {// "<Nop>" means nothing 250map_to = "" ; 251map_buf = NULL ; 252 }else if (modes & MENU_TIP_MODE ) { 253map_buf = NULL ;// Menu tips are plain text. 254 }else { 255map_buf = NULL ; 256map_to = replace_termcodes (map_to ,strlen (map_to ),& map_buf ,0 , 257REPTERM_DO_LT ,NULL ,p_cpo ); 258 } 259menuarg .modes = modes ; 260menuarg .noremap [0 ]= noremap ; 261menuarg .silent [0 ]= silent ; 262add_menu_path (menu_path ,& menuarg ,pri_tab ,map_to ); 263 264// For the PopUp menu, add a menu for each mode separately. 265if (menu_is_popup (menu_path )) { 266for (i = 0 ;i < MENU_INDEX_TIP ;i ++ ) { 267if (modes & (1 <<i )) { 268p = popup_mode_name (menu_path ,i ); 269// Include all modes, to make ":amenu" work 270menuarg .modes = modes ; 271add_menu_path (p ,& menuarg ,pri_tab ,map_to ); 272xfree (p ); 273 } 274 } 275 } 276 277xfree (map_buf ); 278 } 279 280ui_call_update_menu (); 281 282theend : 283 ; 284} 285 286/// Add the menu with the given name to the menu hierarchy 287/// 288/// @param[out] menuarg menu entry 289/// @param[] pri_tab priority table 290/// @param[in] call_data Right hand side command 291static int add_menu_path (const char * const menu_path ,vimmenu_T * menuarg ,const int * const pri_tab , 292const char * const call_data ) 293{ 294int modes = menuarg -> modes ; 295vimmenu_T * menu = NULL ; 296vimmenu_T ** lower_pri ; 297char * dname ; 298int pri_idx = 0 ; 299int old_modes = 0 ; 300char * en_name ; 301 302// Make a copy so we can stuff around with it, since it could be const 303char * path_name = xstrdup (menu_path ); 304vimmenu_T ** root_menu_ptr = get_root_menu (menu_path ); 305vimmenu_T ** menup = root_menu_ptr ; 306vimmenu_T * parent = NULL ; 307char * name = path_name ; 308while (* name ) { 309// Get name of this element in the menu hierarchy, and the simplified 310// name (without mnemonic and accelerator text). 311char * next_name = menu_name_skip (name ); 312char * map_to = menutrans_lookup (name , (int )strlen (name )); 313if (map_to != NULL ) { 314en_name = name ; 315name = map_to ; 316 }else { 317en_name = NULL ; 318 } 319dname = menu_text (name ,NULL ,NULL ); 320if (* dname == NUL ) { 321// Only a mnemonic or accelerator is not valid. 322emsg (_ ("E792: Empty menu name" )); 323 gotoerret ; 324 } 325 326// See if it's already there 327lower_pri = menup ; 328menu = * menup ; 329while (menu != NULL ) { 330if (menu_name_equal (name ,menu )|| menu_name_equal (dname ,menu )) { 331if (* next_name == NUL && menu -> children != NULL ) { 332if (!sys_menu ) { 333emsg (_ ("E330: Menu path must not lead to a sub-menu" )); 334 } 335 gotoerret ; 336 } 337if (* next_name != NUL && menu -> children == NULL ) { 338if (!sys_menu ) { 339emsg (_ (e_notsubmenu )); 340 } 341 gotoerret ; 342 } 343break ; 344 } 345menup = & menu -> next ; 346 347// Count menus, to find where this one needs to be inserted. 348// Ignore menus that are not in the menubar (PopUp and Toolbar) 349if (parent != NULL || menu_is_menubar (menu -> name )) { 350if (menu -> priority <=pri_tab [pri_idx ]) { 351lower_pri = menup ; 352 } 353 } 354menu = menu -> next ; 355 } 356 357if (menu == NULL ) { 358if (* next_name == NUL && parent == NULL ) { 359emsg (_ ("E331: Must not add menu items directly to menu bar" )); 360 gotoerret ; 361 } 362 363if (menu_is_separator (dname )&& * next_name != NUL ) { 364emsg (_ ("E332: Separator cannot be part of a menu path" )); 365 gotoerret ; 366 } 367 368// Not already there, so let's add it 369menu = xcalloc (1 ,sizeof (vimmenu_T )); 370 371menu -> modes = modes ; 372menu -> enabled = MENU_ALL_MODES ; 373menu -> name = xstrdup (name ); 374// separate mnemonic and accelerator text from actual menu name 375menu -> dname = menu_text (name ,& menu -> mnemonic ,& menu -> actext ); 376if (en_name != NULL ) { 377menu -> en_name = xstrdup (en_name ); 378menu -> en_dname = menu_text (en_name ,NULL ,NULL ); 379 }else { 380menu -> en_name = NULL ; 381menu -> en_dname = NULL ; 382 } 383menu -> priority = pri_tab [pri_idx ]; 384menu -> parent = parent ; 385 386// Add after menu that has lower priority. 387menu -> next = * lower_pri ; 388* lower_pri = menu ; 389 390old_modes = 0 ; 391 }else { 392old_modes = menu -> modes ; 393 394// If this menu option was previously only available in other 395// modes, then make sure it's available for this one now 396// Also enable a menu when it's created or changed. 397 { 398menu -> modes |=modes ; 399menu -> enabled |=modes ; 400 } 401 } 402 403menup = & menu -> children ; 404parent = menu ; 405name = next_name ; 406XFREE_CLEAR (dname ); 407if (pri_tab [pri_idx + 1 ]!= -1 ) { 408pri_idx ++ ; 409 } 410 } 411xfree (path_name ); 412 413// Only add system menu items which have not been defined yet. 414// First check if this was an ":amenu". 415int amenu = ((modes & (MENU_NORMAL_MODE |MENU_INSERT_MODE ))== 416 (MENU_NORMAL_MODE |MENU_INSERT_MODE )); 417if (sys_menu ) { 418modes &= ~old_modes ; 419 } 420 421if (menu != NULL && modes ) { 422char * p = (call_data == NULL ) ?NULL :xstrdup (call_data ); 423 424// loop over all modes, may add more than one 425for (int i = 0 ;i < MENU_MODES ;i ++ ) { 426if (modes & (1 <<i )) { 427// free any old menu 428free_menu_string (menu ,i ); 429 430// For "amenu", may insert an extra character. 431// Don't do this for "<Nop>". 432char c = 0 ; 433char d = 0 ; 434if (amenu && call_data != NULL && * call_data != NUL ) { 435switch (1 <<i ) { 436case MENU_VISUAL_MODE : 437case MENU_SELECT_MODE : 438case MENU_OP_PENDING_MODE : 439case MENU_CMDLINE_MODE : 440c = Ctrl_C ; 441break ; 442case MENU_INSERT_MODE : 443c = Ctrl_BSL ; 444d = Ctrl_O ; 445break ; 446 } 447 } 448 449if (c != 0 ) { 450menu -> strings [i ]= xmalloc (strlen (call_data )+ 5 ); 451menu -> strings [i ][0 ]= c ; 452if (d == 0 ) { 453STRCPY (menu -> strings [i ]+ 1 ,call_data ); 454 }else { 455menu -> strings [i ][1 ]= d ; 456STRCPY (menu -> strings [i ]+ 2 ,call_data ); 457 } 458if (c == Ctrl_C ) { 459int len = (int )strlen (menu -> strings [i ]); 460 461menu -> strings [i ][len ]= Ctrl_BSL ; 462menu -> strings [i ][len + 1 ]= Ctrl_G ; 463menu -> strings [i ][len + 2 ]= NUL ; 464 } 465 }else { 466menu -> strings [i ]= p ; 467 } 468menu -> noremap [i ]= menuarg -> noremap [0 ]; 469menu -> silent [i ]= menuarg -> silent [0 ]; 470 } 471 } 472 } 473return OK ; 474 475erret : 476xfree (path_name ); 477xfree (dname ); 478 479// Delete any empty submenu we added before discovering the error. Repeat 480// for higher levels. 481while (parent != NULL && parent -> children == NULL ) { 482if (parent -> parent == NULL ) { 483menup = root_menu_ptr ; 484 }else { 485menup = & parent -> parent -> children ; 486 } 487for (;* menup != NULL && * menup != parent ;menup = & ((* menup )-> next )) {} 488if (* menup == NULL ) {// safety check 489break ; 490 } 491parent = parent -> parent ; 492free_menu (menup ); 493 } 494return FAIL ; 495} 496 497// Set the (sub)menu with the given name to enabled or disabled. 498// Called recursively. 499static int menu_enable_recurse (vimmenu_T * menu ,char * name ,int modes ,int enable ) 500{ 501if (menu == NULL ) { 502return OK ;// Got to bottom of hierarchy 503 } 504// Get name of this element in the menu hierarchy 505char * p = menu_name_skip (name ); 506 507// Find the menu 508while (menu != NULL ) { 509if (* name == NUL || * name == '*' || menu_name_equal (name ,menu )) { 510if (* p != NUL ) { 511if (menu -> children == NULL ) { 512emsg (_ (e_notsubmenu )); 513return FAIL ; 514 } 515if (menu_enable_recurse (menu -> children ,p ,modes ,enable )== FAIL ) { 516return FAIL ; 517 } 518 }else if (enable ) { 519menu -> enabled |=modes ; 520 }else { 521menu -> enabled &= ~modes ; 522 } 523 524// When name is empty, we are doing all menu items for the given 525// modes, so keep looping, otherwise we are just doing the named 526// menu item (which has been found) so break here. 527if (* name != NUL && * name != '*' ) { 528break ; 529 } 530 } 531menu = menu -> next ; 532 } 533if (* name != NUL && * name != '*' && menu == NULL ) { 534semsg (_ (e_nomenu ),name ); 535return FAIL ; 536 } 537 538return OK ; 539} 540 541/// Remove the (sub)menu with the given name from the menu hierarchy 542/// Called recursively. 543/// 544/// @param silent don't give error messages 545static int remove_menu (vimmenu_T ** menup ,char * name ,int modes ,bool silent ) 546{ 547vimmenu_T * menu ; 548 549if (* menup == NULL ) { 550return OK ;// Got to bottom of hierarchy 551 } 552// Get name of this element in the menu hierarchy 553char * p = menu_name_skip (name ); 554 555// Find the menu 556while ((menu = * menup )!= NULL ) { 557if (* name == NUL || menu_name_equal (name ,menu )) { 558if (* p != NUL && menu -> children == NULL ) { 559if (!silent ) { 560emsg (_ (e_notsubmenu )); 561 } 562return FAIL ; 563 } 564if ((menu -> modes & modes )!= 0x0 ) { 565if (remove_menu (& menu -> children ,p ,modes ,silent )== FAIL ) { 566return FAIL ; 567 } 568 }else if (* name != NUL ) { 569if (!silent ) { 570emsg (_ (e_menu_only_exists_in_another_mode )); 571 } 572return FAIL ; 573 } 574 575// When name is empty, we are removing all menu items for the given 576// modes, so keep looping, otherwise we are just removing the named 577// menu item (which has been found) so break here. 578if (* name != NUL ) { 579break ; 580 } 581 582// Remove the menu item for the given mode[s]. If the menu item 583// is no longer valid in ANY mode, delete it 584menu -> modes &= ~modes ; 585if (modes & MENU_TIP_MODE ) { 586free_menu_string (menu ,MENU_INDEX_TIP ); 587 } 588if ((menu -> modes & MENU_ALL_MODES )== 0 ) { 589free_menu (menup ); 590 }else { 591menup = & menu -> next ; 592 } 593 }else { 594menup = & menu -> next ; 595 } 596 } 597if (* name != NUL ) { 598if (menu == NULL ) { 599if (!silent ) { 600semsg (_ (e_nomenu ),name ); 601 } 602return FAIL ; 603 } 604 605// Recalculate modes for menu based on the new updated children 606menu -> modes &= ~modes ; 607vimmenu_T * child = menu -> children ; 608for (;child != NULL ;child = child -> next ) { 609menu -> modes |=child -> modes ; 610 } 611if (modes & MENU_TIP_MODE ) { 612free_menu_string (menu ,MENU_INDEX_TIP ); 613 } 614if ((menu -> modes & MENU_ALL_MODES )== 0 ) { 615// The menu item is no longer valid in ANY mode, so delete it 616* menup = menu ; 617free_menu (menup ); 618 } 619 } 620 621return OK ; 622} 623 624// Free the given menu structure and remove it from the linked list. 625static void free_menu (vimmenu_T ** menup ) 626{ 627vimmenu_T * menu = * menup ; 628 629* menup = menu -> next ; 630xfree (menu -> name ); 631xfree (menu -> dname ); 632xfree (menu -> en_name ); 633xfree (menu -> en_dname ); 634xfree (menu -> actext ); 635for (int i = 0 ;i < MENU_MODES ;i ++ ) { 636free_menu_string (menu ,i ); 637 } 638xfree (menu ); 639} 640 641// Free the menu->string with the given index. 642static void free_menu_string (vimmenu_T * menu ,int idx ) 643{ 644int count = 0 ; 645 646for (int i = 0 ;i < MENU_MODES ;i ++ ) { 647if (menu -> strings [i ]== menu -> strings [idx ]) { 648count ++ ; 649 } 650 } 651if (count == 1 ) { 652xfree (menu -> strings [idx ]); 653 } 654menu -> strings [idx ]= NULL ; 655} 656 657/// Export menus 658/// 659/// @param[in] menu if null, starts from root_menu 660/// @param modes, a choice of \ref MENU_MODES 661/// @return dict with name/commands 662/// @see show_menus_recursive 663/// @see menu_get 664static dict_T * menu_get_recursive (const vimmenu_T * menu ,int modes ) 665{ 666if (!menu || (menu -> modes & modes )== 0x0 ) { 667return NULL ; 668 } 669 670dict_T * dict = tv_dict_alloc (); 671tv_dict_add_str (dict ,S_LEN ("name" ),menu -> dname ); 672tv_dict_add_nr (dict ,S_LEN ("priority" ),menu -> priority ); 673tv_dict_add_nr (dict ,S_LEN ("hidden" ),menu_is_hidden (menu -> dname )); 674 675if (menu -> mnemonic ) { 676char buf [MB_MAXCHAR + 1 ]= {0 };// > max value of utf8_char2bytes 677int buflen = utf_char2bytes (menu -> mnemonic ,buf ); 678tv_dict_add_str_len (dict ,S_LEN ("shortcut" ),buf ,buflen ); 679 } 680 681if (menu -> actext ) { 682tv_dict_add_str (dict ,S_LEN ("actext" ),menu -> actext ); 683 } 684 685if (menu -> modes & MENU_TIP_MODE && menu -> strings [MENU_INDEX_TIP ]) { 686tv_dict_add_str (dict ,S_LEN ("tooltip" ), 687menu -> strings [MENU_INDEX_TIP ]); 688 } 689 690if (!menu -> children ) { 691// leaf menu 692dict_T * commands = tv_dict_alloc (); 693tv_dict_add_dict (dict ,S_LEN ("mappings" ),commands ); 694 695for (int bit = 0 ;bit < MENU_MODES ;bit ++ ) { 696if ((menu -> modes & modes & (1 <<bit ))!= 0 ) { 697dict_T * impl = tv_dict_alloc (); 698tv_dict_add_allocated_str (impl ,S_LEN ("rhs" ), 699str2special_save (menu -> strings [bit ], false, false)); 700tv_dict_add_nr (impl ,S_LEN ("silent" ),menu -> silent [bit ]); 701tv_dict_add_nr (impl ,S_LEN ("enabled" ), 702 (menu -> enabled & (1 <<bit )) ?1 :0 ); 703tv_dict_add_nr (impl ,S_LEN ("noremap" ), 704 (menu -> noremap [bit ]== REMAP_NONE ) ?1 :0 ); 705tv_dict_add_nr (impl ,S_LEN ("sid" ), 706 (menu -> noremap [bit ]== REMAP_SCRIPT ) ?1 :0 ); 707tv_dict_add_dict (commands ,menu_mode_chars [bit ],strlen (menu_mode_chars [bit ]),impl ); 708 } 709 } 710 }else { 711// visit recursively all children 712list_T * const children_list = tv_list_alloc (kListLenMayKnow ); 713for (menu = menu -> children ;menu != NULL ;menu = menu -> next ) { 714dict_T * d = menu_get_recursive (menu ,modes ); 715if (tv_dict_len (d )> 0 ) { 716tv_list_append_dict (children_list ,d ); 717 } 718 } 719tv_dict_add_list (dict ,S_LEN ("submenus" ),children_list ); 720 } 721return dict ; 722} 723 724/// Export menus matching path \p path_name 725/// 726/// @param path_name 727/// @param modes supported modes, see \ref MENU_MODES 728/// @param[in,out] list must be allocated 729/// @return false if could not find path_name 730bool menu_get (char * const path_name ,int modes ,list_T * list ) 731{ 732vimmenu_T * menu = * get_root_menu (path_name ); 733if (* path_name != NUL ) { 734menu = find_menu (menu ,path_name ,modes ); 735if (!menu ) { 736return false; 737 } 738 } 739for (;menu != NULL ;menu = menu -> next ) { 740dict_T * d = menu_get_recursive (menu ,modes ); 741if (d && tv_dict_len (d )> 0 ) { 742tv_list_append_dict (list ,d ); 743 } 744if (* path_name != NUL ) { 745// If a (non-empty) path query was given, only the first node in the 746// find_menu() result is relevant. Else we want all nodes. 747break ; 748 } 749 } 750return true; 751} 752 753/// Find menu matching `name` and `modes`. Does not handle empty `name`. 754/// 755/// @param menu top menu to start looking from 756/// @param path_name path towards the menu 757/// @return found menu or NULL 758static vimmenu_T * find_menu (vimmenu_T * menu ,const char * path_name ,int modes ) 759{ 760assert (* path_name ); 761char * const saved_name = xstrdup (path_name ); 762char * name = saved_name ; 763 764while (* name ) { 765// find the end of one dot-separated name and put a NUL at the dot 766char * p = menu_name_skip (name ); 767while (menu != NULL ) { 768if (menu_name_equal (name ,menu )) { 769// Found menu 770if (* p != NUL && menu -> children == NULL ) { 771emsg (_ (e_notsubmenu )); 772menu = NULL ; 773 gototheend ; 774 }else if ((menu -> modes & modes )== 0x0 ) { 775emsg (_ (e_menu_only_exists_in_another_mode )); 776menu = NULL ; 777 gototheend ; 778 }else if (* p == NUL ) {// found a full match 779 gototheend ; 780 } 781break ; 782 } 783menu = menu -> next ; 784 } 785 786if (menu == NULL ) { 787semsg (_ (e_nomenu ),name ); 788break ; 789 } 790// Found a match, search the sub-menu. 791name = p ; 792assert (* name ); 793menu = menu -> children ; 794 } 795 796theend : 797xfree (saved_name ); 798return menu ; 799} 800 801/// Show the mapping associated with a menu item or hierarchy in a sub-menu. 802static int show_menus (char * const path_name ,int modes ) 803{ 804vimmenu_T * menu = NULL ; 805if (* path_name != NUL ) { 806// First, find the (sub)menu with the given name 807menu = find_menu (* get_root_menu (path_name ),path_name ,modes ); 808if (menu == NULL ) { 809return FAIL ; 810 } 811 } 812 813// make sure the list of menus doesn't change while listing them 814menus_locked ++ ; 815 816// list the matching menu mappings 817msg_puts_title (_ ("\n--- Menus ---" )); 818show_menus_recursive (menu ,modes ,0 ); 819 820menus_locked -- ; 821return OK ; 822} 823 824/// Recursively show the mappings associated with the menus under the given one 825static void show_menus_recursive (vimmenu_T * menu ,int modes ,int depth ) 826{ 827if (menu != NULL && (menu -> modes & modes )== 0x0 ) { 828return ; 829 } 830 831if (menu != NULL ) { 832msg_putchar ('\n' ); 833if (got_int ) {// "q" hit for "--more--" 834return ; 835 } 836for (int i = 0 ;i < depth ;i ++ ) { 837msg_puts (" " ); 838 } 839if (menu -> priority ) { 840msg_outnum (menu -> priority ); 841msg_puts (" " ); 842 } 843// Same highlighting as for directories!? 844msg_outtrans (menu -> name ,HLF_D , false); 845 } 846 847if (menu != NULL && menu -> children == NULL ) { 848for (int bit = 0 ;bit < MENU_MODES ;bit ++ ) { 849if ((menu -> modes & modes & (1 <<bit ))!= 0 ) { 850msg_putchar ('\n' ); 851if (got_int ) {// "q" hit for "--more--" 852return ; 853 } 854for (int i = 0 ;i < depth + 2 ;i ++ ) { 855msg_puts (" " ); 856 } 857msg_puts (menu_mode_chars [bit ]); 858if (menu -> noremap [bit ]== REMAP_NONE ) { 859msg_putchar ('*' ); 860 }else if (menu -> noremap [bit ]== REMAP_SCRIPT ) { 861msg_putchar ('&' ); 862 }else { 863msg_putchar (' ' ); 864 } 865if (menu -> silent [bit ]) { 866msg_putchar ('s' ); 867 }else { 868msg_putchar (' ' ); 869 } 870if ((menu -> modes & menu -> enabled & (1 <<bit ))== 0 ) { 871msg_putchar ('-' ); 872 }else { 873msg_putchar (' ' ); 874 } 875msg_puts (" " ); 876if (* menu -> strings [bit ]== NUL ) { 877msg_puts_hl ("<Nop>" ,HLF_8 , false); 878 }else { 879msg_outtrans_special (menu -> strings [bit ], false,0 ); 880 } 881 } 882 } 883 }else { 884if (menu == NULL ) { 885menu = root_menu ; 886depth -- ; 887 }else { 888menu = menu -> children ; 889 } 890 891// recursively show all children. Skip PopUp[nvoci]. 892for (;menu != NULL && !got_int ;menu = menu -> next ) { 893if (!menu_is_hidden (menu -> dname )) { 894show_menus_recursive (menu ,modes ,depth + 1 ); 895 } 896 } 897 } 898} 899 900// Used when expanding menu names. 901static vimmenu_T * expand_menu = NULL ; 902static int expand_modes = 0x0 ; 903static int expand_emenu ;// true for ":emenu" command 904 905// Work out what to complete when doing command line completion of menu names. 906char * set_context_in_menu_cmd (expand_T * xp ,const char * cmd ,char * arg ,bool forceit ) 907FUNC_ATTR_NONNULL_ALL 908{ 909char * after_dot ; 910char * p ; 911char * path_name = NULL ; 912bool unmenu ; 913vimmenu_T * menu ; 914 915xp -> xp_context = EXPAND_UNSUCCESSFUL ; 916 917// Check for priority numbers, enable and disable 918for (p = arg ;* p ;p ++ ) { 919if (!ascii_isdigit (* p )&& * p != '.' ) { 920break ; 921 } 922 } 923 924if (!ascii_iswhite (* p )) { 925if (strncmp (arg ,"enable" ,6 )== 0 926&& (arg [6 ]== NUL || ascii_iswhite (arg [6 ]))) { 927p = arg + 6 ; 928 }else if (strncmp (arg ,"disable" ,7 )== 0 929&& (arg [7 ]== NUL || ascii_iswhite (arg [7 ]))) { 930p = arg + 7 ; 931 }else { 932p = arg ; 933 } 934 } 935 936while (* p != NUL && ascii_iswhite (* p )) { 937p ++ ; 938 } 939 940arg = after_dot = p ; 941 942for (;* p && !ascii_iswhite (* p );p ++ ) { 943if ((* p == '\\' || * p == Ctrl_V )&& p [1 ]!= NUL ) { 944p ++ ; 945 }else if (* p == '.' ) { 946after_dot = p + 1 ; 947 } 948 } 949 950// ":popup" only uses menus, not entries 951int expand_menus = !((* cmd == 't' && cmd [1 ]== 'e' )|| * cmd == 'p' ); 952expand_emenu = (* cmd == 'e' ); 953if (expand_menus && ascii_iswhite (* p )) { 954return NULL ;// TODO(vim): check for next command? 955 } 956if (* p == NUL ) {// Complete the menu name 957// With :unmenu, you only want to match menus for the appropriate mode. 958// With :menu though you might want to add a menu with the same name as 959// one in another mode, so match menus from other modes too. 960expand_modes = get_menu_cmd_modes (cmd ,forceit ,NULL ,& unmenu ); 961if (!unmenu ) { 962expand_modes = MENU_ALL_MODES ; 963 } 964 965menu = root_menu ; 966if (after_dot > arg ) { 967size_t path_len = (size_t )(after_dot - arg ); 968path_name = xmalloc (path_len ); 969xstrlcpy (path_name ,arg ,path_len ); 970 } 971char * name = path_name ; 972while (name != NULL && * name ) { 973p = menu_name_skip (name ); 974while (menu != NULL ) { 975if (menu_name_equal (name ,menu )) { 976// Found menu 977if ((* p != NUL && menu -> children == NULL ) 978|| ((menu -> modes & expand_modes )== 0x0 )) { 979// Menu path continues, but we have reached a leaf. 980// Or menu exists only in another mode. 981xfree (path_name ); 982return NULL ; 983 } 984break ; 985 } 986menu = menu -> next ; 987 } 988if (menu == NULL ) { 989// No menu found with the name we were looking for 990xfree (path_name ); 991return NULL ; 992 } 993name = p ; 994menu = menu -> children ; 995 } 996xfree (path_name ); 997 998xp -> xp_context = expand_menus ?EXPAND_MENUNAMES :EXPAND_MENUS ; 999xp -> xp_pattern = after_dot ; 1000expand_menu = menu ; 1001 }else {// We're in the mapping part 1002xp -> xp_context = EXPAND_NOTHING ; 1003 } 1004return NULL ; 1005} 1006 1007// Function given to ExpandGeneric() to obtain the list of (sub)menus (not 1008// entries). 1009char * get_menu_name (expand_T * xp ,int idx ) 1010{ 1011static vimmenu_T * menu = NULL ; 1012char * str ; 1013static bool should_advance = false; 1014 1015if (idx == 0 ) {// first call: start at first item 1016menu = expand_menu ; 1017should_advance = false; 1018 } 1019 1020// Skip PopUp[nvoci]. 1021while (menu != NULL && (menu_is_hidden (menu -> dname ) 1022|| menu_is_separator (menu -> dname ) 1023|| menu -> children == NULL )) { 1024menu = menu -> next ; 1025 } 1026 1027if (menu == NULL ) {// at end of linked list 1028return NULL ; 1029 } 1030 1031if (menu -> modes & expand_modes ) { 1032if (should_advance ) { 1033str = menu -> en_dname ; 1034 }else { 1035str = menu -> dname ; 1036if (menu -> en_dname == NULL ) { 1037should_advance = true; 1038 } 1039 } 1040 }else { 1041str = "" ; 1042 } 1043 1044if (should_advance ) { 1045// Advance to next menu entry. 1046menu = menu -> next ; 1047 } 1048 1049should_advance = !should_advance ; 1050 1051return str ; 1052} 1053 1054// Function given to ExpandGeneric() to obtain the list of menus and menu 1055// entries. 1056char * get_menu_names (expand_T * xp ,int idx ) 1057{ 1058static vimmenu_T * menu = NULL ; 1059#define TBUFFER_LEN 256 1060static char tbuffer [TBUFFER_LEN ];// hack 1061char * str ; 1062static bool should_advance = false; 1063 1064if (idx == 0 ) {// first call: start at first item 1065menu = expand_menu ; 1066should_advance = false; 1067 } 1068 1069// Skip Browse-style entries, popup menus and separators. 1070while (menu != NULL 1071&& (menu_is_hidden (menu -> dname ) 1072|| (expand_emenu && menu_is_separator (menu -> dname )) 1073|| menu -> dname [strlen (menu -> dname )- 1 ]== '.' )) { 1074menu = menu -> next ; 1075 } 1076 1077if (menu == NULL ) {// at end of linked list 1078return NULL ; 1079 } 1080 1081if (menu -> modes & expand_modes ) { 1082if (menu -> children != NULL ) { 1083if (should_advance ) { 1084xstrlcpy (tbuffer ,menu -> en_dname ,TBUFFER_LEN ); 1085 }else { 1086xstrlcpy (tbuffer ,menu -> dname ,TBUFFER_LEN ); 1087if (menu -> en_dname == NULL ) { 1088should_advance = true; 1089 } 1090 } 1091// hack on menu separators: use a 'magic' char for the separator 1092// so that '.' in names gets escaped properly 1093strcat (tbuffer ,"\001" ); 1094str = tbuffer ; 1095 }else { 1096if (should_advance ) { 1097str = menu -> en_dname ; 1098 }else { 1099str = menu -> dname ; 1100if (menu -> en_dname == NULL ) { 1101should_advance = true; 1102 } 1103 } 1104 } 1105 }else { 1106str = "" ; 1107 } 1108 1109if (should_advance ) { 1110// Advance to next menu entry. 1111menu = menu -> next ; 1112 } 1113 1114should_advance = !should_advance ; 1115 1116return str ; 1117} 1118 1119/// Skip over this element of the menu path and return the start of the next 1120/// element. Any \ and ^Vs are removed from the current element. 1121/// 1122/// @param name may be modified. 1123/// @return start of the next element 1124static char * menu_name_skip (char * const name ) 1125{ 1126char * p ; 1127 1128for (p = name ;* p && * p != '.' ;MB_PTR_ADV (p )) { 1129if (* p == '\\' || * p == Ctrl_V ) { 1130STRMOVE (p ,p + 1 ); 1131if (* p == NUL ) { 1132break ; 1133 } 1134 } 1135 } 1136if (* p ) { 1137* p ++ = NUL ; 1138 } 1139return p ; 1140} 1141 1142/// Return true when "name" matches with menu "menu". The name is compared in 1143/// two ways: raw menu name and menu name without '&'. ignore part after a TAB. 1144static bool menu_name_equal (const char * const name ,const vimmenu_T * const menu ) 1145{ 1146if (menu -> en_name != NULL 1147&& (menu_namecmp (name ,menu -> en_name ) 1148|| menu_namecmp (name ,menu -> en_dname ))) { 1149return true; 1150 } 1151return menu_namecmp (name ,menu -> name )|| menu_namecmp (name ,menu -> dname ); 1152} 1153 1154static bool menu_namecmp (const char * const name ,const char * const mname ) 1155{ 1156int i ; 1157 1158for (i = 0 ;name [i ]!= NUL && name [i ]!= TAB ;i ++ ) { 1159if (name [i ]!= mname [i ]) { 1160break ; 1161 } 1162 } 1163return (name [i ]== NUL || name [i ]== TAB ) 1164&& (mname [i ]== NUL || mname [i ]== TAB ); 1165} 1166 1167/// Returns the \ref MENU_MODES specified by menu command `cmd`. 1168/// (eg :menu! returns MENU_CMDLINE_MODE | MENU_INSERT_MODE) 1169/// 1170/// @param[in] cmd string like "nmenu", "vmenu", etc. 1171/// @param[in] forceit bang (!) was given after the command 1172/// @param[out] noremap If not NULL, the flag it points to is set according 1173/// to whether the command is a "nore" command. 1174/// @param[out] unmenu If not NULL, the flag it points to is set according 1175/// to whether the command is an "unmenu" command. 1176int get_menu_cmd_modes (const char * cmd ,bool forceit ,int * noremap ,bool * unmenu ) 1177{ 1178int modes ; 1179 1180switch (* cmd ++ ) { 1181case 'v' :// vmenu, vunmenu, vnoremenu 1182modes = MENU_VISUAL_MODE |MENU_SELECT_MODE ; 1183break ; 1184case 'x' :// xmenu, xunmenu, xnoremenu 1185modes = MENU_VISUAL_MODE ; 1186break ; 1187case 's' :// smenu, sunmenu, snoremenu 1188modes = MENU_SELECT_MODE ; 1189break ; 1190case 'o' :// omenu 1191modes = MENU_OP_PENDING_MODE ; 1192break ; 1193case 'i' :// imenu 1194modes = MENU_INSERT_MODE ; 1195break ; 1196case 't' : 1197if (* cmd == 'l' ) {// tlmenu, tlunmenu, tlnoremenu 1198modes = MENU_TERMINAL_MODE ; 1199cmd ++ ; 1200break ; 1201 } 1202modes = MENU_TIP_MODE ;// tmenu 1203break ; 1204case 'c' :// cmenu 1205modes = MENU_CMDLINE_MODE ; 1206break ; 1207case 'a' :// amenu 1208modes = MENU_INSERT_MODE |MENU_CMDLINE_MODE |MENU_NORMAL_MODE 1209 |MENU_VISUAL_MODE |MENU_SELECT_MODE 1210 |MENU_OP_PENDING_MODE ; 1211break ; 1212case 'n' : 1213if (* cmd != 'o' ) {// nmenu, not noremenu 1214modes = MENU_NORMAL_MODE ; 1215break ; 1216 } 1217FALLTHROUGH ; 1218default : 1219cmd -- ; 1220if (forceit ) { 1221// menu!! 1222modes = MENU_INSERT_MODE |MENU_CMDLINE_MODE ; 1223 }else { 1224// menu 1225modes = MENU_NORMAL_MODE |MENU_VISUAL_MODE |MENU_SELECT_MODE 1226 |MENU_OP_PENDING_MODE ; 1227 } 1228 } 1229 1230if (noremap != NULL ) { 1231* noremap = (* cmd == 'n' ?REMAP_NONE :REMAP_YES ); 1232 } 1233if (unmenu != NULL ) { 1234* unmenu = (* cmd == 'u' ); 1235 } 1236return modes ; 1237} 1238 1239/// Return the string representation of the menu modes. Does the opposite 1240/// of get_menu_cmd_modes(). 1241static char * get_menu_mode_str (int modes ) 1242{ 1243if ((modes & (MENU_INSERT_MODE |MENU_CMDLINE_MODE |MENU_NORMAL_MODE | 1244MENU_VISUAL_MODE |MENU_SELECT_MODE |MENU_OP_PENDING_MODE )) 1245== (MENU_INSERT_MODE |MENU_CMDLINE_MODE |MENU_NORMAL_MODE | 1246MENU_VISUAL_MODE |MENU_SELECT_MODE |MENU_OP_PENDING_MODE )) { 1247return "a" ; 1248 } 1249if ((modes & (MENU_NORMAL_MODE |MENU_VISUAL_MODE |MENU_SELECT_MODE | 1250MENU_OP_PENDING_MODE )) 1251== (MENU_NORMAL_MODE |MENU_VISUAL_MODE |MENU_SELECT_MODE | 1252MENU_OP_PENDING_MODE )) { 1253return " " ; 1254 } 1255if ((modes & (MENU_INSERT_MODE |MENU_CMDLINE_MODE )) 1256== (MENU_INSERT_MODE |MENU_CMDLINE_MODE )) { 1257return "!" ; 1258 } 1259if ((modes & (MENU_VISUAL_MODE |MENU_SELECT_MODE )) 1260== (MENU_VISUAL_MODE |MENU_SELECT_MODE )) { 1261return "v" ; 1262 } 1263if (modes & MENU_VISUAL_MODE ) { 1264return "x" ; 1265 } 1266if (modes & MENU_SELECT_MODE ) { 1267return "s" ; 1268 } 1269if (modes & MENU_OP_PENDING_MODE ) { 1270return "o" ; 1271 } 1272if (modes & MENU_INSERT_MODE ) { 1273return "i" ; 1274 } 1275if (modes & MENU_TERMINAL_MODE ) { 1276return "tl" ; 1277 } 1278if (modes & MENU_CMDLINE_MODE ) { 1279return "c" ; 1280 } 1281if (modes & MENU_NORMAL_MODE ) { 1282return "n" ; 1283 } 1284if (modes & MENU_TIP_MODE ) { 1285return "t" ; 1286 } 1287 1288return "" ; 1289} 1290 1291// Modify a menu name starting with "PopUp" to include the mode character. 1292// Returns the name in allocated memory. 1293static char * popup_mode_name (char * name ,int idx ) 1294{ 1295size_t len = strlen (name ); 1296assert (len >=4 ); 1297 1298char * mode_chars = menu_mode_chars [idx ]; 1299size_t mode_chars_len = strlen (mode_chars ); 1300char * p = xstrnsave (name ,len + mode_chars_len ); 1301memmove (p + 5 + mode_chars_len ,p + 5 ,len - 4 ); 1302for (size_t i = 0 ;i < mode_chars_len ;i ++ ) { 1303p [5 + i ]= menu_mode_chars [idx ][i ]; 1304 } 1305 1306return p ; 1307} 1308 1309/// Duplicate the menu item text and then process to see if a mnemonic key 1310/// and/or accelerator text has been identified. 1311/// 1312/// @param str The menu item text. 1313/// @param[out] mnemonic If non-NULL, *mnemonic is set to the character after 1314/// the first '&'. 1315/// @param[out] actext If non-NULL, *actext is set to the text after the first 1316/// TAB, but only if a TAB was found. Memory pointed to is newly 1317/// allocated. 1318/// 1319/// @return a pointer to allocated memory. 1320static char * menu_text (const char * str ,int * mnemonic ,char ** actext ) 1321FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT 1322FUNC_ATTR_NONNULL_ARG (1 ) 1323{ 1324char * text ; 1325 1326// Locate accelerator text, after the first TAB 1327char * p = vim_strchr (str ,TAB ); 1328if (p != NULL ) { 1329if (actext != NULL ) { 1330* actext = xstrdup (p + 1 ); 1331 } 1332assert (p >=str ); 1333text = xmemdupz (str , (size_t )(p - str )); 1334 }else { 1335text = xstrdup (str ); 1336 } 1337 1338// Find mnemonic characters "&a" and reduce "&&" to "&". 1339for (p = text ;p != NULL ;) { 1340p = vim_strchr (p ,'&' ); 1341if (p != NULL ) { 1342if (p [1 ]== NUL ) {// trailing "&" 1343break ; 1344 } 1345if (mnemonic != NULL && p [1 ]!= '&' ) { 1346* mnemonic = (uint8_t )p [1 ]; 1347 } 1348STRMOVE (p ,p + 1 ); 1349p = p + 1 ; 1350 } 1351 } 1352return text ; 1353} 1354 1355// Return true if "name" can be a menu in the MenuBar. 1356bool menu_is_menubar (const char * const name ) 1357FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 1358{ 1359return !menu_is_popup (name ) 1360&& !menu_is_toolbar (name ) 1361&& !menu_is_winbar (name ) 1362&& * name != MNU_HIDDEN_CHAR ; 1363} 1364 1365// Return true if "name" is a popup menu name. 1366bool menu_is_popup (const char * const name ) 1367FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 1368{ 1369return strncmp (name ,"PopUp" ,5 )== 0 ; 1370} 1371 1372// Return true if "name" is a toolbar menu name. 1373bool menu_is_toolbar (const char * const name ) 1374FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 1375{ 1376return strncmp (name ,"ToolBar" ,7 )== 0 ; 1377} 1378 1379/// @return true if the name is a menu separator identifier: Starts and ends 1380/// with '-' 1381bool menu_is_separator (char * name ) 1382{ 1383return name [0 ]== '-' && name [strlen (name )- 1 ]== '-' ; 1384} 1385 1386/// True if a popup menu or starts with \ref MNU_HIDDEN_CHAR 1387/// 1388/// @return true if the menu is hidden 1389static bool menu_is_hidden (char * name ) 1390{ 1391return (name [0 ]== MNU_HIDDEN_CHAR ) 1392|| (menu_is_popup (name )&& name [5 ]!= NUL ); 1393} 1394 1395static int get_menu_mode (void ) 1396{ 1397if (State & MODE_TERMINAL ) { 1398return MENU_INDEX_TERMINAL ; 1399 } 1400if (Visual .active ) { 1401if (Visual .select ) { 1402return MENU_INDEX_SELECT ; 1403 } 1404return MENU_INDEX_VISUAL ; 1405 } 1406if (State & MODE_INSERT ) { 1407return MENU_INDEX_INSERT ; 1408 } 1409if ((State & MODE_CMDLINE )|| State == MODE_ASKMORE || State == MODE_HITRETURN ) { 1410return MENU_INDEX_CMDLINE ; 1411 } 1412if (finish_op ) { 1413return MENU_INDEX_OP_PENDING ; 1414 } 1415if (State & MODE_NORMAL ) { 1416return MENU_INDEX_NORMAL ; 1417 } 1418if (State & MODE_LANGMAP ) {// must be a "r" command, like Insert mode 1419return MENU_INDEX_INSERT ; 1420 } 1421return MENU_INDEX_INVALID ; 1422} 1423 1424int get_menu_mode_flag (void ) 1425{ 1426int mode = get_menu_mode (); 1427 1428if (mode == MENU_INDEX_INVALID ) { 1429return 0 ; 1430 } 1431return 1 <<mode ; 1432} 1433 1434/// Display the Special "PopUp" menu as a pop-up at the current mouse 1435/// position. The "PopUpn" menu is for Normal mode, "PopUpi" for Insert mode, 1436/// etc. 1437void show_popupmenu (void ) 1438{ 1439int menu_mode = get_menu_mode (); 1440if (menu_mode == MENU_INDEX_INVALID ) { 1441return ; 1442 } 1443char * mode = menu_mode_chars [menu_mode ]; 1444size_t mode_len = strlen (mode ); 1445 1446apply_autocmds (EVENT_MENUPOPUP ,mode ,NULL , false,curbuf ); 1447 1448vimmenu_T * menu ; 1449 1450for (menu = root_menu ;menu != NULL ;menu = menu -> next ) { 1451if (strncmp ("PopUp" ,menu -> name ,5 )== 0 && strncmp (menu -> name + 5 ,mode ,mode_len )== 0 ) { 1452break ; 1453 } 1454 } 1455 1456// Only show a popup when it is defined and has entries 1457if (menu == NULL || menu -> children == NULL ) { 1458return ; 1459 } 1460 1461pum_show_popupmenu (menu ); 1462} 1463 1464/// Execute "menu". Use by ":emenu" and the window toolbar. 1465/// @param eap NULL for the window toolbar. 1466/// @param mode_idx specify a MENU_INDEX_ value, 1467/// use MENU_INDEX_INVALID to depend on the current state 1468void execute_menu (const exarg_T * eap ,vimmenu_T * menu ,int mode_idx ) 1469FUNC_ATTR_NONNULL_ARG (2 ) 1470{ 1471int idx = mode_idx ; 1472 1473if (idx < 0 ) { 1474if (State & MODE_TERMINAL ) { 1475idx = MENU_INDEX_TERMINAL ; 1476 }else if (State & MODE_CMDLINE ) { 1477idx = MENU_INDEX_CMDLINE ; 1478 }else if (get_real_state ()& MODE_VISUAL ) { 1479// Detect real visual mode -- if we are really in visual mode we 1480// don't need to do any guesswork to figure out what the selection 1481// is. Just execute the visual binding for the menu. 1482idx = MENU_INDEX_VISUAL ; 1483 }else if (((State & MODE_INSERT )|| restart_edit )&& current_sctx .sc_sid == 0 ) { 1484// Use the Insert mode entry when returning to Insert mode. 1485idx = MENU_INDEX_INSERT ; 1486 }else if (eap != NULL && eap -> addr_count ) { 1487pos_T tpos ; 1488 1489idx = MENU_INDEX_VISUAL ; 1490 1491// GEDDES: This is not perfect - but it is a 1492// quick way of detecting whether we are doing this from a 1493// selection - see if the range matches up with the visual 1494// select start and end. 1495if ((curbuf -> b_visual .vi_start .lnum == eap -> line1 ) 1496&& (curbuf -> b_visual .vi_end .lnum )== eap -> line2 ) { 1497// Set it up for visual mode - equivalent to gv. 1498Visual .mode = curbuf -> b_visual .vi_mode ; 1499tpos = curbuf -> b_visual .vi_end ; 1500curwin -> w_cursor = curbuf -> b_visual .vi_start ; 1501curwin -> w_curswant = curbuf -> b_visual .vi_curswant ; 1502 }else { 1503// Set it up for line-wise visual mode 1504Visual .mode = 'V' ; 1505curwin -> w_cursor .lnum = eap -> line1 ; 1506curwin -> w_cursor .col = 1 ; 1507tpos .lnum = eap -> line2 ; 1508tpos .col = MAXCOL ; 1509tpos .coladd = 0 ; 1510 } 1511 1512// Activate visual mode 1513Visual .active = true; 1514Visual .reselect = true; 1515check_cursor (curwin ); 1516Visual .start = curwin -> w_cursor ; 1517curwin -> w_cursor = tpos ; 1518 1519check_cursor (curwin ); 1520 1521// Adjust the cursor to make sure it is in the correct pos 1522// for exclusive mode 1523if (* p_sel == 'e' && gchar_cursor ()!= NUL ) { 1524curwin -> w_cursor .col ++ ; 1525 } 1526 } 1527 } 1528 1529if (idx == MENU_INDEX_INVALID || eap == NULL ) { 1530idx = MENU_INDEX_NORMAL ; 1531 } 1532 1533if (menu -> strings [idx ]!= NULL && (menu -> modes & (1 <<idx ))) { 1534// When executing a script or function execute the commands right now. 1535// Also for the window toolbar 1536// Otherwise put them in the typeahead buffer. 1537if (eap == NULL || current_sctx .sc_sid != 0 ) { 1538save_state_T save_state ; 1539 1540ex_normal_busy ++ ; 1541save_current_state (& save_state ); 1542exec_normal_cmd (menu -> strings [idx ],menu -> noremap [idx ],menu -> silent [idx ]); 1543restore_current_state (& save_state ); 1544ex_normal_busy -- ; 1545 }else { 1546ins_typebuf (menu -> strings [idx ],menu -> noremap [idx ],0 , true, 1547menu -> silent [idx ]); 1548 } 1549 }else if (eap != NULL ) { 1550char * mode ; 1551switch (idx ) { 1552case MENU_INDEX_VISUAL : 1553mode = "Visual" ; 1554break ; 1555case MENU_INDEX_SELECT : 1556mode = "Select" ; 1557break ; 1558case MENU_INDEX_OP_PENDING : 1559mode = "Op-pending" ; 1560break ; 1561case MENU_INDEX_TERMINAL : 1562mode = "Terminal" ; 1563break ; 1564case MENU_INDEX_INSERT : 1565mode = "Insert" ; 1566break ; 1567case MENU_INDEX_CMDLINE : 1568mode = "Cmdline" ; 1569break ; 1570// case MENU_INDEX_TIP: cannot happen 1571default : 1572mode = "Normal" ; 1573 } 1574semsg (_ ("E335: Menu not defined for %s mode" ),mode ); 1575 } 1576} 1577 1578/// Lookup a menu by the descriptor name e.g. "File.New" 1579/// Returns NULL if the menu is not found 1580static vimmenu_T * menu_getbyname (char * name_arg ) 1581FUNC_ATTR_NONNULL_ALL 1582{ 1583char * saved_name = xstrdup (name_arg ); 1584vimmenu_T * menu = * get_root_menu (saved_name ); 1585char * name = saved_name ; 1586bool gave_emsg = false; 1587while (* name ) { 1588// Find in the menu hierarchy 1589char * p = menu_name_skip (name ); 1590 1591while (menu != NULL ) { 1592if (menu_name_equal (name ,menu )) { 1593if (* p == NUL && menu -> children != NULL ) { 1594emsg (_ ("E333: Menu path must lead to a menu item" )); 1595gave_emsg = true; 1596menu = NULL ; 1597 }else if (* p != NUL && menu -> children == NULL ) { 1598emsg (_ (e_notsubmenu )); 1599menu = NULL ; 1600 } 1601break ; 1602 } 1603menu = menu -> next ; 1604 } 1605if (menu == NULL || * p == NUL ) { 1606break ; 1607 } 1608menu = menu -> children ; 1609name = p ; 1610 } 1611xfree (saved_name ); 1612if (menu == NULL ) { 1613if (!gave_emsg ) { 1614semsg (_ ("E334: Menu not found: %s" ),name_arg ); 1615 } 1616return NULL ; 1617 } 1618 1619return menu ; 1620} 1621 1622/// Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and 1623/// execute it. 1624void ex_emenu (exarg_T * eap ) 1625{ 1626char * arg = eap -> arg ; 1627int mode_idx = MENU_INDEX_INVALID ; 1628 1629if (arg [0 ]&& ascii_iswhite (arg [1 ])) { 1630switch (arg [0 ]) { 1631case 'n' : 1632mode_idx = MENU_INDEX_NORMAL ;break ; 1633case 'v' : 1634mode_idx = MENU_INDEX_VISUAL ;break ; 1635case 's' : 1636mode_idx = MENU_INDEX_SELECT ;break ; 1637case 'o' : 1638mode_idx = MENU_INDEX_OP_PENDING ;break ; 1639case 't' : 1640mode_idx = MENU_INDEX_TERMINAL ;break ; 1641case 'i' : 1642mode_idx = MENU_INDEX_INSERT ;break ; 1643case 'c' : 1644mode_idx = MENU_INDEX_CMDLINE ;break ; 1645default : 1646semsg (_ (e_invarg2 ),arg ); 1647return ; 1648 } 1649arg = skipwhite (arg + 2 ); 1650 } 1651 1652vimmenu_T * menu = menu_getbyname (arg ); 1653if (menu == NULL ) { 1654return ; 1655 } 1656 1657// Found the menu, so execute. 1658execute_menu (eap ,menu ,mode_idx ); 1659} 1660 1661/// Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy. 1662vimmenu_T * menu_find (const char * path_name ) 1663{ 1664vimmenu_T * menu = * get_root_menu (path_name ); 1665char * saved_name = xstrdup (path_name ); 1666char * name = saved_name ; 1667while (* name ) { 1668// find the end of one dot-separated name and put a NUL at the dot 1669char * p = menu_name_skip (name ); 1670 1671while (menu != NULL ) { 1672if (menu_name_equal (name ,menu )) { 1673if (menu -> children == NULL ) { 1674// found a menu item instead of a sub-menu 1675if (* p == NUL ) { 1676emsg (_ ("E336: Menu path must lead to a sub-menu" )); 1677 }else { 1678emsg (_ (e_notsubmenu )); 1679 } 1680menu = NULL ; 1681 gototheend ; 1682 } 1683if (* p == NUL ) {// found a full match 1684 gototheend ; 1685 } 1686break ; 1687 } 1688menu = menu -> next ; 1689 } 1690if (menu == NULL ) {// didn't find it 1691break ; 1692 } 1693 1694// Found a match, search the sub-menu. 1695menu = menu -> children ; 1696name = p ; 1697 } 1698 1699if (menu == NULL ) { 1700emsg (_ ("E337: Menu not found - check menu names" )); 1701 } 1702theend : 1703xfree (saved_name ); 1704return menu ; 1705} 1706 1707// Translation of menu names. Just a simple lookup table. 1708 1709typedef struct { 1710char * from ;// English name 1711char * from_noamp ;// same, without '&' 1712char * to ;// translated name 1713}menutrans_T ; 1714 1715static garray_T menutrans_ga = GA_EMPTY_INIT_VALUE ; 1716 1717#define FREE_MENUTRANS (mt ) \ 1718 menutrans_T *_mt = (mt); \ 1719 xfree(_mt->from); \ 1720 xfree(_mt->from_noamp); \ 1721 xfree(_mt->to) 1722 1723// ":menutrans". 1724// This function is also defined without the +multi_lang feature, in which 1725// case the commands are ignored. 1726void ex_menutranslate (exarg_T * eap ) 1727{ 1728char * arg = eap -> arg ; 1729 1730if (menutrans_ga .ga_itemsize == 0 ) { 1731ga_init (& menutrans_ga , (int )sizeof (menutrans_T ),5 ); 1732 } 1733 1734// ":menutrans clear": clear all translations. 1735if (strncmp (arg ,"clear" ,5 )== 0 && ends_excmd (* skipwhite (arg + 5 ))) { 1736GA_DEEP_CLEAR (& menutrans_ga ,menutrans_T ,FREE_MENUTRANS ); 1737 1738// Delete all "menutrans_" global variables. 1739del_menutrans_vars (); 1740 }else { 1741// ":menutrans from to": add translation 1742char * from = arg ; 1743arg = menu_skip_part (arg ); 1744char * to = skipwhite (arg ); 1745* arg = NUL ; 1746arg = menu_skip_part (to ); 1747if (arg == to ) { 1748emsg (_ (e_invarg )); 1749 }else { 1750from = xstrdup (from ); 1751char * from_noamp = menu_text (from ,NULL ,NULL ); 1752assert (arg >=to ); 1753to = xmemdupz (to , (size_t )(arg - to )); 1754menu_translate_tab_and_shift (from ); 1755menu_translate_tab_and_shift (to ); 1756menu_unescape_name (from ); 1757menu_unescape_name (to ); 1758menutrans_T * tp = GA_APPEND_VIA_PTR (menutrans_T ,& menutrans_ga ); 1759tp -> from = from ; 1760tp -> from_noamp = from_noamp ; 1761tp -> to = to ; 1762 } 1763 } 1764} 1765 1766// Find the character just after one part of a menu name. 1767static char * menu_skip_part (char * p ) 1768{ 1769while (* p != NUL && * p != '.' && !ascii_iswhite (* p )) { 1770if ((* p == '\\' || * p == Ctrl_V )&& p [1 ]!= NUL ) { 1771p ++ ; 1772 } 1773p ++ ; 1774 } 1775return p ; 1776} 1777 1778// Lookup part of a menu name in the translations. 1779// Return a pointer to the translation or NULL if not found. 1780static char * menutrans_lookup (char * name ,int len ) 1781{ 1782menutrans_T * tp = (menutrans_T * )menutrans_ga .ga_data ; 1783 1784for (int i = 0 ;i < menutrans_ga .ga_len ;i ++ ) { 1785if (STRNICMP (name ,tp [i ].from , (size_t )len )== 0 && tp [i ].from [len ]== NUL ) { 1786return tp [i ].to ; 1787 } 1788 } 1789 1790// Now try again while ignoring '&' characters. 1791char c = name [len ]; 1792name [len ]= NUL ; 1793char * dname = menu_text (name ,NULL ,NULL ); 1794name [len ]= c ; 1795for (int i = 0 ;i < menutrans_ga .ga_len ;i ++ ) { 1796if (STRICMP (dname ,tp [i ].from_noamp )== 0 ) { 1797xfree (dname ); 1798return tp [i ].to ; 1799 } 1800 } 1801xfree (dname ); 1802 1803return NULL ; 1804} 1805 1806// Unescape the name in the translate dictionary table. 1807static void menu_unescape_name (char * name ) 1808{ 1809for (char * p = name ;* p && * p != '.' ;MB_PTR_ADV (p )) { 1810if (* p == '\\' ) { 1811STRMOVE (p ,p + 1 ); 1812 } 1813 } 1814} 1815 1816// Isolate the menu name. 1817// Skip the menu name, and translate <Tab> into a real TAB. 1818static char * menu_translate_tab_and_shift (char * arg_start ) 1819{ 1820char * arg = arg_start ; 1821 1822while (* arg && !ascii_iswhite (* arg )) { 1823if ((* arg == '\\' || * arg == Ctrl_V )&& arg [1 ]!= NUL ) { 1824arg ++ ; 1825 }else if (STRNICMP (arg ,"<TAB>" ,5 )== 0 ) { 1826* arg = TAB ; 1827STRMOVE (arg + 1 ,arg + 5 ); 1828 } 1829arg ++ ; 1830 } 1831if (* arg != NUL ) { 1832* arg ++ = NUL ; 1833 } 1834arg = skipwhite (arg ); 1835 1836return arg ; 1837} 1838 1839/// Get the information about a menu item in mode 'which' 1840static void menuitem_getinfo (const char * menu_name ,const vimmenu_T * menu ,int modes ,dict_T * dict ) 1841FUNC_ATTR_NONNULL_ALL 1842{ 1843if (* menu_name == NUL ) { 1844// Return all the top-level menus 1845list_T * const l = tv_list_alloc (kListLenMayKnow ); 1846tv_dict_add_list (dict ,S_LEN ("submenus" ),l ); 1847// get all the children. Skip PopUp[nvoci]. 1848for (const vimmenu_T * topmenu = menu ;topmenu != NULL ;topmenu = topmenu -> next ) { 1849if (!menu_is_hidden (topmenu -> dname )) { 1850tv_list_append_string (l ,topmenu -> dname ,-1 ); 1851 } 1852 } 1853return ; 1854 } 1855 1856tv_dict_add_str (dict ,S_LEN ("name" ),menu -> name ); 1857tv_dict_add_str (dict ,S_LEN ("display" ),menu -> dname ); 1858if (menu -> actext != NULL ) { 1859tv_dict_add_str (dict ,S_LEN ("accel" ),menu -> actext ); 1860 } 1861tv_dict_add_nr (dict ,S_LEN ("priority" ),menu -> priority ); 1862tv_dict_add_str (dict ,S_LEN ("modes" ),get_menu_mode_str (menu -> modes )); 1863 1864char buf [NUMBUFLEN ]; 1865int buflen = utf_char2bytes (menu -> mnemonic ,buf ); 1866buf [buflen ]= NUL ; 1867tv_dict_add_str_len (dict ,S_LEN ("shortcut" ),buf ,buflen ); 1868 1869if (menu -> children == NULL ) {// leaf menu 1870int bit ; 1871 1872// Get the first mode in which the menu is available 1873for (bit = 0 ; (bit < MENU_MODES )&& !((1 <<bit )& modes );bit ++ ) {} 1874 1875if (bit < MENU_MODES ) {// just in case, avoid Coverity warning 1876if (menu -> strings [bit ]!= NULL ) { 1877tv_dict_add_allocated_str (dict ,S_LEN ("rhs" ), 1878* menu -> strings [bit ]== NUL 1879 ?xmemdupz (S_LEN ("<Nop>" )) 1880 :str2special_save (menu -> strings [bit ], false, false)); 1881 } 1882tv_dict_add_bool (dict ,S_LEN ("noremenu" ),menu -> noremap [bit ]== REMAP_NONE ); 1883tv_dict_add_bool (dict ,S_LEN ("script" ),menu -> noremap [bit ]== REMAP_SCRIPT ); 1884tv_dict_add_bool (dict ,S_LEN ("silent" ),menu -> silent [bit ]); 1885tv_dict_add_bool (dict ,S_LEN ("enabled" ), (menu -> enabled & (1 <<bit ))!= 0 ); 1886 } 1887 }else { 1888// If there are submenus, add all the submenu display names 1889list_T * const l = tv_list_alloc (kListLenMayKnow ); 1890tv_dict_add_list (dict ,S_LEN ("submenus" ),l ); 1891const vimmenu_T * child = menu -> children ; 1892while (child != NULL ) { 1893tv_list_append_string (l ,child -> dname ,-1 ); 1894child = child -> next ; 1895 } 1896 } 1897} 1898 1899/// "menu_info()" function 1900/// Return information about a menu (including all the child menus) 1901void f_menu_info (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1902{ 1903tv_dict_alloc_ret (rettv ); 1904dict_T * const retdict = rettv -> vval .v_dict ; 1905 1906const char * const menu_name = tv_get_string_chk (& argvars [0 ]); 1907if (menu_name == NULL ) { 1908return ; 1909 } 1910 1911// menu mode 1912const char * which ; 1913if (argvars [1 ].v_type != VAR_UNKNOWN ) { 1914which = tv_get_string_chk (& argvars [1 ]); 1915 }else { 1916which = "" ;// Default is modes for "menu" 1917 } 1918if (which == NULL ) { 1919return ; 1920 } 1921 1922const int modes = get_menu_cmd_modes (which ,* which == '!' ,NULL ,NULL ); 1923 1924// Locate the specified menu or menu item 1925const vimmenu_T * menu = * get_root_menu (menu_name ); 1926char * const saved_name = xstrdup (menu_name ); 1927if (* saved_name != NUL ) { 1928char * name = saved_name ; 1929while (* name ) { 1930// Find in the menu hierarchy 1931char * p = menu_name_skip (name ); 1932while (menu != NULL ) { 1933if (menu_name_equal (name ,menu )) { 1934break ; 1935 } 1936menu = menu -> next ; 1937 } 1938if (menu == NULL || * p == NUL ) { 1939break ; 1940 } 1941menu = menu -> children ; 1942name = p ; 1943 } 1944 } 1945xfree (saved_name ); 1946 1947if (menu == NULL ) {// specified menu not found 1948return ; 1949 } 1950 1951if (menu -> modes & modes ) { 1952menuitem_getinfo (menu_name ,menu ,modes ,retdict ); 1953 } 1954}