blob: aaf13816febd0e1a9cb3995b3215d6dc28eb8a73 [file] [log] [blame]
Guido van Rossum504b0bf1999-01-02 21:28:54 +00001Writing an IDLE extension
2
Guido van Rossumf9038981999-01-04 13:05:58 +00003An IDLE extension can define new key bindings and menu entries for IDLE
4edit windows. There is a simple mechanism to load extensions when IDLE
5starts up and to attach them to each edit window. (It is also possible
6to make other changes to IDLE, but this must be done by editing the IDLE
7source code.)
Guido van Rossum504b0bf1999-01-02 21:28:54 +00008
9The list of extensions loaded at startup time is configured by editing
10the file extend.py; see below for details.
11
12An IDLE extension is defined by a class. Methods of the class define
Guido van Rossumf9038981999-01-04 13:05:58 +000013actions that are invoked by those bindings or menu entries. Class (or
14instance) variables define the bindings and menu additions; these are
15automatically applied by IDLE when the extension is linked to an edit
16window.
Guido van Rossum504b0bf1999-01-02 21:28:54 +000017
18An IDLE extension class is instantiated with a single argument,
Guido van Rossumf9038981999-01-04 13:05:58 +000019`editwin', an EditorWindow instance. The extension cannot assume much
20about this argument, but it is guarateed to have the following instance
21variables:
Guido van Rossum504b0bf1999-01-02 21:28:54 +000022
23 text a Text instance (a widget)
24 io an IOBinding instance (more about this later)
25 flist the FileList instance (shared by all edit windows)
26
27(There are a few more, but they are rarely useful.)
28
29The extension class must not bind key events. Rather, it must define
30one or more virtual events, e.g. <<zoom-height>>, and corresponding
31methods, e.g. zoom_height(), and have one or more class (or instance)
32variables that define mappings between virtual events and key sequences,
33e.g. <Alt-F2>. When the extension is loaded, these key sequences will
34be bound to the corresponding virtual events, and the virtual events
35will be bound to the corresponding methods. (This indirection is done
Guido van Rossumf9038981999-01-04 13:05:58 +000036so that the key bindings can easily be changed, and so that other
37sources of virtual events can exist, such as menu entries.)
Guido van Rossum504b0bf1999-01-02 21:28:54 +000038
39The following class or instance variables are used to define key
40bindings for virtual events:
41
42 keydefs for all platforms
43 mac_keydefs for Macintosh
44 windows_keydefs for Windows
45 unix_keydefs for Unix (and other platforms)
46
47Each of these variables, if it exists, must be a dictionary whose
48keys are virtual events, and whose values are lists of key sequences.
49
50An extension can define menu entries in a similar fashion. This is done
51with a class or instance variable named menudefs; it should be a list of
Guido van Rossumf9038981999-01-04 13:05:58 +000052pair, where each pair is a menu name (lowercase) and a list of menu
53entries. Each menu entry is either None (to insert a separator entry) or
54a pair of strings (menu_label, virtual_event). Here, menu_label is the
55label of the menu entry, and virtual_event is the virtual event to be
56generated when the entry is selected. An underscore in the menu label
57is removed; the character following the underscore is displayed
58underlined, to indicate the shortcut character (for Windows).
Guido van Rossum504b0bf1999-01-02 21:28:54 +000059
Guido van Rossumf9038981999-01-04 13:05:58 +000060At the moment, extensions cannot define whole new menus; they must
61define entries in existing menus. Some menus are not present on some
62windows; such entry definitions are then ignored, but the key bindings
63are still applied. (This should probably be refined in the future.)
Guido van Rossum504b0bf1999-01-02 21:28:54 +000064
65Here is a complete example example:
66
67class ZoomHeight:
68
69 menudefs = [
70 ('edit', [
71 None, # Separator
72 ('_Zoom Height', '<<zoom-height>>'),
73 ])
74 ]
75
76 windows_keydefs = {
77 '<<zoom-height>>': ['<Alt-F2>'],
78 }
79 unix_keydefs = {
80 '<<zoom-height>>': ['<Control-z><Control-z>'],
81 }
82
83 def __init__(self, editwin):
84 self.editwin = editwin
85
86 def zoom_height(self, event):
87 "...Do what you want here..."
88
89The final piece of the puzzle is the file "extend.py", which contains a
Guido van Rossumf9038981999-01-04 13:05:58 +000090simple table used to configure the loading of extensions. This file
91currently contains a single list variable named "standard", which is a
92list of extension names that are to be loaded. (In the future, other
93configuration variables may be added to this module.)
Guido van Rossum504b0bf1999-01-02 21:28:54 +000094
Guido van Rossumf9038981999-01-04 13:05:58 +000095Extensions can define key bindings and menu entries that reference
96events they don't implement (including standard events); however this is
97not recommended (and may be forbidden in the future).
Guido van Rossum504b0bf1999-01-02 21:28:54 +000098
Guido van Rossumf9038981999-01-04 13:05:58 +000099Extensions are not required to define menu entries for all events they
100implement.
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000101
102Note: in order to change key bindings, you must currently edit the file
103keydefs. It contains two dictionaries named and formatted like the
Guido van Rossumf9038981999-01-04 13:05:58 +0000104keydefs dictionaries described above, one for the Unix bindings and one
105for the Windows bindings. In the future, a better mechanism will be
106provided.