blob: dbca89a9788a46e52dc28bc460079117cbded3b6 [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>>'),
47 ]),
48 ('help', [
49 ('_Help...', '<<help>>'),
50 None,
51 ('_About IDLE...', '<<about-idle>>'),
52 ]),
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000053]
54
Guido van Rossum6e0a4131998-11-27 03:17:49 +000055def prepstr(s):
56 # Helper to extract the underscore from a string,
57 # e.g. prepstr("Co_py") returns (2, "Copy").
58 i = string.find(s, '_')
59 if i >= 0:
60 s = s[:i] + s[i+1:]
61 return i, s
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000062
Guido van Rossum6e0a4131998-11-27 03:17:49 +000063keynames = {
64 'bracketleft': '[',
65 'bracketright': ']',
Guido van Rossum504b0bf1999-01-02 21:28:54 +000066 'slash': '/',
Guido van Rossum6e0a4131998-11-27 03:17:49 +000067}
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000068
Guido van Rossum504b0bf1999-01-02 21:28:54 +000069def get_accelerator(keydefs, event):
Guido van Rossum6e0a4131998-11-27 03:17:49 +000070 keylist = keydefs.get(event)
71 if not keylist:
72 return ""
73 s = keylist[0]
Guido van Rossum6e0a4131998-11-27 03:17:49 +000074 s = re.sub(r"-[a-z]\b", lambda m: string.upper(m.group()), s)
75 s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s)
76 s = re.sub("Key-", "", s)
77 s = re.sub("Control-", "Ctrl-", s)
78 s = re.sub("-", "+", s)
79 s = re.sub("><", " ", s)
80 s = re.sub("<", "", s)
81 s = re.sub(">", "", s)
82 return s
Guido van Rossumd8d676c1998-10-12 20:57:09 +000083
Guido van Rossum6e0a4131998-11-27 03:17:49 +000084if sys.platform == 'win32':
85 default_keydefs = windows_keydefs
86else:
Guido van Rossum504b0bf1999-01-02 21:28:54 +000087 default_keydefs = unix_keydefs
Guido van Rossumd8d676c1998-10-12 20:57:09 +000088
Guido van Rossum6e0a4131998-11-27 03:17:49 +000089def apply_bindings(text, keydefs=default_keydefs):
90 text.keydefs = keydefs
91 for event, keylist in keydefs.items():
92 if keylist:
93 apply(text.event_add, (event,) + tuple(keylist))
Guido van Rossumd8d676c1998-10-12 20:57:09 +000094
Guido van Rossum504b0bf1999-01-02 21:28:54 +000095def fill_menus(text, menudict, defs=menudefs, keydefs=default_keydefs):
Guido van Rossum6e0a4131998-11-27 03:17:49 +000096 # Fill the menus for the given text widget. The menudict argument is
Guido van Rossumd8d676c1998-10-12 20:57:09 +000097 # a dictionary containing the menus, keyed by their lowercased name.
Guido van Rossum219ffde1998-10-12 23:55:10 +000098 # Menus that are absent or None are ignored.
Guido van Rossum6e0a4131998-11-27 03:17:49 +000099 for mname, itemlist in defs:
100 menu = menudict.get(mname)
Guido van Rossumd8d676c1998-10-12 20:57:09 +0000101 if not menu:
102 continue
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000103 for item in itemlist:
104 if not item:
105 menu.add_separator()
106 else:
107 label, event = item
108 underline, label = prepstr(label)
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000109 accelerator = get_accelerator(keydefs, event)
Guido van Rossum6e0a4131998-11-27 03:17:49 +0000110 def command(text=text, event=event):
111 text.event_generate(event)
112 menu.add_command(label=label, underline=underline,
113 command=command, accelerator=accelerator)