blob: e365032c768ebcdc6f60ebcc27794244ec785d9d [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 Rossum504b0bf1999-01-02 21:28:54 +000011from keydefs import *
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000012
Guido van Rossum6e0a4131998-11-27 03:17:49 +000013menudefs = [
14 # underscore prefixes character to underscore
15 ('file', [
16 ('_New window', '<<open-new-window>>'),
17 ('_Open...', '<<open-window-from-file>>'),
18 ('Open _module...', '<<open-module>>'),
Guido van Rossum504b0bf1999-01-02 21:28:54 +000019 ('Class _browser', '<<open-class-browser>>'),
20 ('Python shell', '<<open-python-shell>>'),
Guido van Rossum6e0a4131998-11-27 03:17:49 +000021 None,
22 ('_Save', '<<save-window>>'),
23 ('Save _As...', '<<save-window-as-file>>'),
24 ('Save Co_py As...', '<<save-copy-of-window-as-file>>'),
25 None,
26 ('_Close', '<<close-window>>'),
27 ('E_xit', '<<close-all-windows>>'),
28 ]),
29 ('edit', [
30 ('_Undo', '<<undo>>'),
31 ('_Redo', '<<redo>>'),
32 None,
33 ('Cu_t', '<<Cut>>'),
34 ('_Copy', '<<Copy>>'),
35 ('_Paste', '<<Paste>>'),
Guido van Rossum504b0bf1999-01-02 21:28:54 +000036 ('Select _All', '<<select-all>>'),
37 ]),
38 ('script', [
39 ('Run module', '<<run-module>>'),
40 ('Run script', '<<run-script>>'),
41 ('New shell', '<<new-shell>>'),
Guido van Rossum6e0a4131998-11-27 03:17:49 +000042 ]),
43 ('debug', [
Guido van Rossum504b0bf1999-01-02 21:28:54 +000044 ('_Go to file/line', '<<goto-file-line>>'),
Guido van Rossum6e0a4131998-11-27 03:17:49 +000045 ('_Open stack viewer', '<<open-stack-viewer>>'),
46 ('_Debugger toggle', '<<toggle-debugger>>'),
Guido van Rossum38df3c31999-01-08 15:31:07 +000047 ('_JIT Stack viewer toggle', '<<toggle-jit-stack-viewer>>' ),
Guido van Rossum6e0a4131998-11-27 03:17:49 +000048 ]),
49 ('help', [
50 ('_Help...', '<<help>>'),
51 None,
52 ('_About IDLE...', '<<about-idle>>'),
53 ]),
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000054]
55
Guido van Rossum6e0a4131998-11-27 03:17:49 +000056def prepstr(s):
57 # Helper to extract the underscore from a string,
58 # e.g. prepstr("Co_py") returns (2, "Copy").
59 i = string.find(s, '_')
60 if i >= 0:
61 s = s[:i] + s[i+1:]
62 return i, s
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000063
Guido van Rossum6e0a4131998-11-27 03:17:49 +000064keynames = {
65 'bracketleft': '[',
66 'bracketright': ']',
Guido van Rossum504b0bf1999-01-02 21:28:54 +000067 'slash': '/',
Guido van Rossum6e0a4131998-11-27 03:17:49 +000068}
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000069
Guido van Rossum504b0bf1999-01-02 21:28:54 +000070def get_accelerator(keydefs, event):
Guido van Rossum6e0a4131998-11-27 03:17:49 +000071 keylist = keydefs.get(event)
72 if not keylist:
73 return ""
74 s = keylist[0]
Guido van Rossum6e0a4131998-11-27 03:17:49 +000075 s = re.sub(r"-[a-z]\b", lambda m: string.upper(m.group()), s)
76 s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s)
77 s = re.sub("Key-", "", s)
78 s = re.sub("Control-", "Ctrl-", s)
79 s = re.sub("-", "+", s)
80 s = re.sub("><", " ", s)
81 s = re.sub("<", "", s)
82 s = re.sub(">", "", s)
83 return s
Guido van Rossumd8d676c1998-10-12 20:57:09 +000084
Guido van Rossum6e0a4131998-11-27 03:17:49 +000085if sys.platform == 'win32':
86 default_keydefs = windows_keydefs
87else:
Guido van Rossum504b0bf1999-01-02 21:28:54 +000088 default_keydefs = unix_keydefs
Guido van Rossumd8d676c1998-10-12 20:57:09 +000089
Guido van Rossum6e0a4131998-11-27 03:17:49 +000090def apply_bindings(text, keydefs=default_keydefs):
91 text.keydefs = keydefs
92 for event, keylist in keydefs.items():
93 if keylist:
94 apply(text.event_add, (event,) + tuple(keylist))
Guido van Rossumd8d676c1998-10-12 20:57:09 +000095
Guido van Rossum504b0bf1999-01-02 21:28:54 +000096def fill_menus(text, menudict, defs=menudefs, keydefs=default_keydefs):
Guido van Rossum6e0a4131998-11-27 03:17:49 +000097 # Fill the menus for the given text widget. The menudict argument is
Guido van Rossumd8d676c1998-10-12 20:57:09 +000098 # a dictionary containing the menus, keyed by their lowercased name.
Guido van Rossum219ffde1998-10-12 23:55:10 +000099 # Menus that are absent or None are ignored.
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000100 for mname, itemlist in defs:
101 menu = menudict.get(mname)
Guido van Rossumd8d676c1998-10-12 20:57:09 +0000102 if not menu:
103 continue
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000104 for item in itemlist:
105 if not item:
106 menu.add_separator()
107 else:
108 label, event = item
109 underline, label = prepstr(label)
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000110 accelerator = get_accelerator(keydefs, event)
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000111 def command(text=text, event=event):
112 text.event_generate(event)
113 menu.add_command(label=label, underline=underline,
114 command=command, accelerator=accelerator)