blob: 5a13d22e92a755d25e4d0d22db75717338da76a3 [file] [log] [blame]
Guido van Rossum6e0a4131998-11-27 03:17:49 +00001# This file defines the menu contents and key bindings. Note that
2# there is additional configuration information in the EditorWindow
3# class (and subclasses): the menus are created there based on the
4# menu_specs (class) variable, and menus not created are silently
5# skipped by the code here. This makes it possible to define the
6# Debug menu here, which is only present in the PythonShell window.
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +00007
Guido van Rossum6e0a4131998-11-27 03:17:49 +00008import sys
9import string
10import re
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000011
Guido van Rossum6e0a4131998-11-27 03:17:49 +000012menudefs = [
13 # underscore prefixes character to underscore
14 ('file', [
15 ('_New window', '<<open-new-window>>'),
16 ('_Open...', '<<open-window-from-file>>'),
17 ('Open _module...', '<<open-module>>'),
18 ('Class _browser...', '<<open-class-browser>>'),
19 None,
20 ('_Save', '<<save-window>>'),
21 ('Save _As...', '<<save-window-as-file>>'),
22 ('Save Co_py As...', '<<save-copy-of-window-as-file>>'),
23 None,
24 ('_Close', '<<close-window>>'),
25 ('E_xit', '<<close-all-windows>>'),
26 ]),
27 ('edit', [
28 ('_Undo', '<<undo>>'),
29 ('_Redo', '<<redo>>'),
30 None,
31 ('Cu_t', '<<Cut>>'),
32 ('_Copy', '<<Copy>>'),
33 ('_Paste', '<<Paste>>'),
34 None,
35 ('_Find...', '<<find>>'),
36 ('Find _next', '<<find-next>>'),
37 ('Find _same', '<<find-same>>'),
38 ('_Go to line', '<<goto-line>>'),
39 None,
40 ('_Dedent region', '<<dedent-region>>'),
41 ('_Indent region', '<<indent-region>>'),
42 ('Comment _out region', '<<comment-region>>'),
43 ('U_ncomment region', '<<uncomment-region>>'),
44 ]),
45 ('debug', [
46 ('_Go to line from traceback', '<<goto-traceback-line>>'),
47 ('_Open stack viewer', '<<open-stack-viewer>>'),
48 ('_Debugger toggle', '<<toggle-debugger>>'),
49 ]),
50 ('help', [
51 ('_Help...', '<<help>>'),
52 None,
53 ('_About IDLE...', '<<about-idle>>'),
54 ]),
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000055]
56
Guido van Rossum6e0a4131998-11-27 03:17:49 +000057windows_keydefs = {
58 '<<beginning-of-line>>': ['<Control-a>', '<Home>'],
59 '<<close-all-windows>>': ['<Control-q>'],
60 '<<comment-region>>': ['<Meta-Key-3>', '<Alt-Key-3>'],
61 '<<dedent-region>>': ['<Control-bracketleft>'],
62 '<<dump-undo-state>>': ['<Control-backslash>'],
63 '<<end-of-file>>': ['<Control-d>'],
64 '<<expand-word>>': ['<Meta-slash>', '<Alt-slash>'],
65 '<<find-next>>': ['<F3>', '<Control-g>'],
66 '<<find-same>>': ['<Control-F3>'],
67 '<<find>>': ['<Control-f>'],
68 '<<goto-line>>': ['<Alt-g>', '<Meta-g>'],
69 '<<history-next>>': ['<Meta-n>', '<Alt-n>'],
70 '<<history-previous>>': ['<Meta-p>', '<Alt-p>'],
71 '<<indent-region>>': ['<Control-bracketright>'],
72 '<<interrupt-execution>>': ['<Control-c>'],
73 '<<newline-and-indent>>': ['<Key-Return>', '<KP_Enter>'],
74 '<<open-new-window>>': ['<Control-n>'],
75 '<<open-window-from-file>>': ['<Control-o>'],
76 '<<plain-newline-and-indent>>': ['<Control-j>'],
77 '<<redo>>': ['<Control-y>'],
78 '<<save-copy-of-window-as-file>>': ['<Meta-w>'],
79 '<<save-window-as-file>>': ['<Control-w>'],
80 '<<save-window>>': ['<Control-s>'],
81 '<<toggle-auto-coloring>>': ['<Control-slash>'],
82 '<<uncomment-region>>': ['<Meta-Key-4>', '<Alt-Key-4>'],
83 '<<undo>>': ['<Control-z>'],
84}
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000085
Guido van Rossum6e0a4131998-11-27 03:17:49 +000086emacs_keydefs = {
87 '<<Copy>>': ['<Alt-w>'],
88 '<<Cut>>': ['<Control-w>'],
89 '<<Paste>>': ['<Control-y>'],
90 '<<about-idle>>': [],
91 '<<beginning-of-line>>': ['<Control-a>', '<Home>'],
92 '<<center-insert>>': ['<Control-l>'],
93 '<<close-all-windows>>': ['<Control-x><Control-c>'],
94 '<<close-window>>': ['<Control-x><Control-0>'],
95 '<<comment-region>>': ['<Meta-Key-3>', '<Alt-Key-3>'],
96 '<<dedent-region>>': ['<Meta-bracketleft>',
97 '<Alt-bracketleft>',
98 '<Control-bracketleft>'],
99 '<<do-nothing>>': ['<Control-x>'],
100 '<<dump-undo-state>>': ['<Control-backslash>'],
101 '<<end-of-file>>': ['<Control-d>'],
102 '<<expand-word>>': ['<Meta-slash>', '<Alt-slash>'],
103 '<<find-next>>': ['<Control-u><Control-s>'],
104 '<<find-same>>': ['<Control-s>'],
105 '<<find>>': ['<Control-u><Control-u><Control-s>'],
106 '<<goto-line>>': ['<Alt-g>', '<Meta-g>'],
107 '<<goto-traceback-line>>': [],
108 '<<help>>': [],
109 '<<history-next>>': ['<Meta-n>', '<Alt-n>'],
110 '<<history-previous>>': ['<Meta-p>', '<Alt-p>'],
111 '<<indent-region>>': ['<Meta-bracketright>',
112 '<Alt-bracketright>',
113 '<Control-bracketright>'],
114 '<<interrupt-execution>>': ['<Control-c>'],
115 '<<newline-and-indent>>': ['<Key-Return>', '<KP_Enter>'],
116 '<<open-class-browser>>': ['<Control-x><Control-b>'],
117 '<<open-module>>': ['<Control-x><Control-m>'],
118 '<<open-new-window>>': ['<Control-x><Control-n>'],
119 '<<open-stack-viewer>>': [],
120 '<<open-window-from-file>>': ['<Control-x><Control-f>'],
121 '<<plain-newline-and-indent>>': ['<Control-j>'],
122 '<<redo>>': ['<Alt-z>', '<Meta-z>'],
123 '<<save-copy-of-window-as-file>>': ['<Control-x><w>'],
124 '<<save-window-as-file>>': ['<Control-x><Control-w>'],
125 '<<save-window>>': ['<Control-x><Control-s>'],
126 '<<toggle-auto-coloring>>': ['<Control-slash>'],
127 '<<toggle-debugger>>': [],
128 '<<uncomment-region>>': ['<Meta-Key-4>', '<Alt-Key-4>'],
129 '<<undo>>': ['<Control-z>'],
130}
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000131
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000132def prepstr(s):
133 # Helper to extract the underscore from a string,
134 # e.g. prepstr("Co_py") returns (2, "Copy").
135 i = string.find(s, '_')
136 if i >= 0:
137 s = s[:i] + s[i+1:]
138 return i, s
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000139
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000140keynames = {
141 'bracketleft': '[',
142 'bracketright': ']',
143}
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000144
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000145def getaccelerator(keydefs, event):
146 keylist = keydefs.get(event)
147 if not keylist:
148 return ""
149 s = keylist[0]
150 if s[:6] == "<Meta-":
151 # Prefer Alt over Meta -- they should be the same thing anyway
152 alts = "<Alt-" + s[6:]
153 if alts in keylist:
154 s = alts
155 s = re.sub(r"-[a-z]\b", lambda m: string.upper(m.group()), s)
156 s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s)
157 s = re.sub("Key-", "", s)
158 s = re.sub("Control-", "Ctrl-", s)
159 s = re.sub("-", "+", s)
160 s = re.sub("><", " ", s)
161 s = re.sub("<", "", s)
162 s = re.sub(">", "", s)
163 return s
Guido van Rossumd8d676c1998-10-12 20:57:09 +0000164
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000165if sys.platform == 'win32':
166 default_keydefs = windows_keydefs
167else:
168 default_keydefs = emacs_keydefs
Guido van Rossumd8d676c1998-10-12 20:57:09 +0000169
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000170def apply_bindings(text, keydefs=default_keydefs):
171 text.keydefs = keydefs
172 for event, keylist in keydefs.items():
173 if keylist:
174 apply(text.event_add, (event,) + tuple(keylist))
Guido van Rossumd8d676c1998-10-12 20:57:09 +0000175
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000176def fill_menus(text, menudict, defs=menudefs):
177 # Fill the menus for the given text widget. The menudict argument is
Guido van Rossumd8d676c1998-10-12 20:57:09 +0000178 # a dictionary containing the menus, keyed by their lowercased name.
Guido van Rossum219ffde1998-10-12 23:55:10 +0000179 # Menus that are absent or None are ignored.
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000180 if hasattr(text, "keydefs"):
181 keydefs = text.keydefs
182 else:
183 keydefs = default_keydefs
184 for mname, itemlist in defs:
185 menu = menudict.get(mname)
Guido van Rossumd8d676c1998-10-12 20:57:09 +0000186 if not menu:
187 continue
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000188 for item in itemlist:
189 if not item:
190 menu.add_separator()
191 else:
192 label, event = item
193 underline, label = prepstr(label)
194 accelerator = getaccelerator(keydefs, event)
195 def command(text=text, event=event):
196 text.event_generate(event)
197 menu.add_command(label=label, underline=underline,
198 command=command, accelerator=accelerator)