17db96d56Sopenharmony_ciWriting an IDLE extension 27db96d56Sopenharmony_ci========================= 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ciAn IDLE extension can define new key bindings and menu entries for IDLE 57db96d56Sopenharmony_ciedit windows. There is a simple mechanism to load extensions when IDLE 67db96d56Sopenharmony_cistarts up and to attach them to each edit window. (It is also possible 77db96d56Sopenharmony_cito make other changes to IDLE, but this must be done by editing the IDLE 87db96d56Sopenharmony_cisource code.) 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ciThe list of extensions loaded at startup time is configured by editing 117db96d56Sopenharmony_cithe file config-extensions.def. See below for details. 127db96d56Sopenharmony_ci 137db96d56Sopenharmony_ciAn IDLE extension is defined by a class. Methods of the class define 147db96d56Sopenharmony_ciactions that are invoked by event bindings or menu entries. Class (or 157db96d56Sopenharmony_ciinstance) variables define the bindings and menu additions; these are 167db96d56Sopenharmony_ciautomatically applied by IDLE when the extension is linked to an edit 177db96d56Sopenharmony_ciwindow. 187db96d56Sopenharmony_ci 197db96d56Sopenharmony_ciAn IDLE extension class is instantiated with a single argument, 207db96d56Sopenharmony_ci`editwin', an EditorWindow instance. The extension cannot assume much 217db96d56Sopenharmony_ciabout this argument, but it is guaranteed to have the following instance 227db96d56Sopenharmony_civariables: 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci text a Text instance (a widget) 257db96d56Sopenharmony_ci io an IOBinding instance (more about this later) 267db96d56Sopenharmony_ci flist the FileList instance (shared by all edit windows) 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ci(There are a few more, but they are rarely useful.) 297db96d56Sopenharmony_ci 307db96d56Sopenharmony_ciThe extension class must not directly bind Window Manager (e.g. X) events. 317db96d56Sopenharmony_ciRather, it must define one or more virtual events, e.g. <<z-in>>, and 327db96d56Sopenharmony_cicorresponding methods, e.g. z_in_event(). The virtual events will be 337db96d56Sopenharmony_cibound to the corresponding methods, and Window Manager events can then be bound 347db96d56Sopenharmony_cito the virtual events. (This indirection is done so that the key bindings can 357db96d56Sopenharmony_cieasily be changed, and so that other sources of virtual events can exist, such 367db96d56Sopenharmony_cias menu entries.) 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ciAn extension can define menu entries. This is done with a class or instance 397db96d56Sopenharmony_civariable named menudefs; it should be a list of pairs, where each pair is a 407db96d56Sopenharmony_cimenu name (lowercase) and a list of menu entries. Each menu entry is either 417db96d56Sopenharmony_ciNone (to insert a separator entry) or a pair of strings (menu_label, 427db96d56Sopenharmony_civirtual_event). Here, menu_label is the label of the menu entry, and 437db96d56Sopenharmony_civirtual_event is the virtual event to be generated when the entry is selected. 447db96d56Sopenharmony_ciAn underscore in the menu label is removed; the character following the 457db96d56Sopenharmony_ciunderscore is displayed underlined, to indicate the shortcut character (for 467db96d56Sopenharmony_ciWindows). 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ciAt the moment, extensions cannot define whole new menus; they must define 497db96d56Sopenharmony_cientries in existing menus. Some menus are not present on some windows; such 507db96d56Sopenharmony_cientry definitions are then ignored, but key bindings are still applied. (This 517db96d56Sopenharmony_cishould probably be refined in the future.) 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ciExtensions are not required to define menu entries for all the events they 547db96d56Sopenharmony_ciimplement. (They are also not required to create keybindings, but in that 557db96d56Sopenharmony_cicase there must be empty bindings in cofig-extensions.def) 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ciHere is a partial example from zzdummy.py: 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ciclass ZzDummy: 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ci menudefs = [ 627db96d56Sopenharmony_ci ('format', [ 637db96d56Sopenharmony_ci ('Z in', '<<z-in>>'), 647db96d56Sopenharmony_ci ('Z out', '<<z-out>>'), 657db96d56Sopenharmony_ci ] ) 667db96d56Sopenharmony_ci ] 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci def __init__(self, editwin): 697db96d56Sopenharmony_ci self.editwin = editwin 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci def z_in_event(self, event=None): 727db96d56Sopenharmony_ci "...Do what you want here..." 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ciThe final piece of the puzzle is the file "config-extensions.def", which is 757db96d56Sopenharmony_ciused to configure the loading of extensions and to establish key (or, more 767db96d56Sopenharmony_cigenerally, event) bindings to the virtual events defined in the extensions. 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ciSee the comments at the top of config-extensions.def for information. It's 797db96d56Sopenharmony_cicurrently necessary to manually modify that file to change IDLE's extension 807db96d56Sopenharmony_ciloading or extension key bindings. 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ciFor further information on binding refer to the Tkinter Resources web page at 837db96d56Sopenharmony_cipython.org and to the Tk Command "bind" man page. 84