Kaydet (Commit) 6e0a4136 authored tarafından Guido van Rossum's avatar Guido van Rossum

New approach, separate tables for menus (platform-independent) and key

definitions (platform-specific), and generating accelerator strings
automatically from the key definitions.
üst 4471f20e
# The first three items of each tupel pertain to the menu bar. All # This file defines the menu contents and key bindings. Note that
# three are None if the item is not to appeat in a menu. Otherwise, # there is additional configuration information in the EditorWindow
# the first item is the menu name (in lowercase), the second item is # class (and subclasses): the menus are created there based on the
# the menu item label; the third item is the keyboard shortcut to be # menu_specs (class) variable, and menus not created are silently
# listed in the menu, if any. Menu items are added in the order of # skipped by the code here. This makes it possible to define the
# their occurrence in this table. An item of the form # Debug menu here, which is only present in the PythonShell window.
# ("menu", None, None) can be used to insert a separator in the menu.
# import sys
# The fourth item, if present is the virtual event; each of the import string
# remaining items is an actual key binding for the event. (Thus, import re
# items[3:] conveniently forms an argument list for event_add().)
menudefs = [
win_bindings = [ # underscore prefixes character to underscore
(None, None, None, "<<beginning-of-line>>", "<Control-a>", "<Home>"), ('file', [
('_New window', '<<open-new-window>>'),
(None, None, None, "<<expand-word>>", "<Meta-slash>", "<Alt-slash>"), ('_Open...', '<<open-window-from-file>>'),
('Open _module...', '<<open-module>>'),
(None, None, None, "<<newline-and-indent>>", "<Key-Return>", "<KP_Enter>"), ('Class _browser...', '<<open-class-browser>>'),
(None, None, None, "<<plain-newline-and-indent>>", "<Control-j>"), None,
('_Save', '<<save-window>>'),
(None, None, None, "<<interrupt-execution>>", "<Control-c>"), ('Save _As...', '<<save-window-as-file>>'),
(None, None, None, "<<end-of-file>>", "<Control-d>"), ('Save Co_py As...', '<<save-copy-of-window-as-file>>'),
None,
(None, None, None, "<<dedent-region>>", "<Control-bracketleft>"), ('_Close', '<<close-window>>'),
(None, None, None, "<<indent-region>>", "<Control-bracketright>"), ('E_xit', '<<close-all-windows>>'),
]),
(None, None, None, "<<comment-region>>", "<Meta-Key-3>", "<Alt-Key-3>"), ('edit', [
(None, None, None, "<<uncomment-region>>", "<Meta-Key-4>", "<Alt-Key-4>"), ('_Undo', '<<undo>>'),
('_Redo', '<<redo>>'),
(None, None, None, "<<history-previous>>", "<Meta-p>", "<Alt-p>"), None,
(None, None, None, "<<history-next>>", "<Meta-n>", "<Alt-n>"), ('Cu_t', '<<Cut>>'),
('_Copy', '<<Copy>>'),
(None, None, None, "<<toggle-auto-coloring>>", "<Control-slash>"), ('_Paste', '<<Paste>>'),
None,
(None, None, None, "<<close-all-windows>>", "<Control-q>"), ('_Find...', '<<find>>'),
(None, None, None, "<<open-new-window>>", "<Control-n>"), ('Find _next', '<<find-next>>'),
(None, None, None, "<<open-window-from-file>>", "<Control-o>"), ('Find _same', '<<find-same>>'),
(None, None, None, "<<save-window>>", "<Control-s>"), ('_Go to line', '<<goto-line>>'),
(None, None, None, "<<save-window-as-file>>", "<Control-w>"), None,
(None, None, None, "<<save-copy-of-window-as-file>>", "<Meta-w>"), ('_Dedent region', '<<dedent-region>>'),
('_Indent region', '<<indent-region>>'),
(None, None, None, "<<find>>", "<Control-f>"), ('Comment _out region', '<<comment-region>>'),
(None, None, None, "<<find-next>>", "<F3>"), ('U_ncomment region', '<<uncomment-region>>'),
(None, None, None, "<<find-same>>", "<Control-F3>"), ]),
(None, None, None, "<<goto-line>>", "<Alt-g>", "<Meta-g>"), ('debug', [
('_Go to line from traceback', '<<goto-traceback-line>>'),
(None, None, None, "<<undo>>", "<Control-z>"), ('_Open stack viewer', '<<open-stack-viewer>>'),
(None, None, None, "<<redo>>", "<Control-y>"), ('_Debugger toggle', '<<toggle-debugger>>'),
(None, None, None, "<<dump-undo-state>>", "<Control-backslash>"), ]),
('help', [
('_Help...', '<<help>>'),
None,
('_About IDLE...', '<<about-idle>>'),
]),
] ]
emacs_bindings = [ windows_keydefs = {
'<<beginning-of-line>>': ['<Control-a>', '<Home>'],
# File menu '<<close-all-windows>>': ['<Control-q>'],
'<<comment-region>>': ['<Meta-Key-3>', '<Alt-Key-3>'],
("file", "New window", "C-x C-n", '<<dedent-region>>': ['<Control-bracketleft>'],
"<<open-new-window>>", "<Control-x><Control-n>"), '<<dump-undo-state>>': ['<Control-backslash>'],
("file", "Open...", "C-x C-f", '<<end-of-file>>': ['<Control-d>'],
"<<open-window-from-file>>", "<Control-x><Control-f>"), '<<expand-word>>': ['<Meta-slash>', '<Alt-slash>'],
("file", "Open module...", "C-x C-m", '<<find-next>>': ['<F3>', '<Control-g>'],
"<<open-module>>", "<Control-x><Control-m>"), '<<find-same>>': ['<Control-F3>'],
("file", "Class browser...", "C-x C-b", '<<find>>': ['<Control-f>'],
"<<open-class-browser>>", "<Control-x><Control-b>"), '<<goto-line>>': ['<Alt-g>', '<Meta-g>'],
("file", None, None), '<<history-next>>': ['<Meta-n>', '<Alt-n>'],
'<<history-previous>>': ['<Meta-p>', '<Alt-p>'],
("file", "Save", "C-x C-s", '<<indent-region>>': ['<Control-bracketright>'],
"<<save-window>>", "<Control-x><Control-s>"), '<<interrupt-execution>>': ['<Control-c>'],
("file", "Save As...", "C-x C-w", '<<newline-and-indent>>': ['<Key-Return>', '<KP_Enter>'],
"<<save-window-as-file>>", "<Control-x><Control-w>"), '<<open-new-window>>': ['<Control-n>'],
("file", "Save Copy As...", "C-x w", '<<open-window-from-file>>': ['<Control-o>'],
"<<save-copy-of-window-as-file>>", "<Control-x><w>"), '<<plain-newline-and-indent>>': ['<Control-j>'],
("file", None, None), '<<redo>>': ['<Control-y>'],
'<<save-copy-of-window-as-file>>': ['<Meta-w>'],
("file", "Close", "C-x C-0", '<<save-window-as-file>>': ['<Control-w>'],
"<<close-window>>", "<Control-x><Control-0>"), '<<save-window>>': ['<Control-s>'],
("file", "Exit", "C-x C-c", '<<toggle-auto-coloring>>': ['<Control-slash>'],
"<<close-all-windows>>", "<Control-x><Control-c>"), '<<uncomment-region>>': ['<Meta-Key-4>', '<Alt-Key-4>'],
'<<undo>>': ['<Control-z>'],
# Edit menu }
("edit", "Undo", "C-z", "<<undo>>", "<Control-z>"), emacs_keydefs = {
("edit", "Redo", "Alt-z", "<<redo>>", "<Alt-z>", "<Meta-z>"), '<<Copy>>': ['<Alt-w>'],
("edit", None, None), '<<Cut>>': ['<Control-w>'],
'<<Paste>>': ['<Control-y>'],
("edit", "Cut", None, "<<Cut>>", "<Control-w>"), '<<about-idle>>': [],
("edit", "Copy", None, "<<Copy>>", "<Alt-w>"), '<<beginning-of-line>>': ['<Control-a>', '<Home>'],
("edit", "Paste", None, "<<Paste>>", "<Control-y>"), '<<center-insert>>': ['<Control-l>'],
("edit", None, None), '<<close-all-windows>>': ['<Control-x><Control-c>'],
'<<close-window>>': ['<Control-x><Control-0>'],
("edit", "Find...", "C-s", '<<comment-region>>': ['<Meta-Key-3>', '<Alt-Key-3>'],
"<<find>>", "<Control-u><Control-u><Control-s>"), '<<dedent-region>>': ['<Meta-bracketleft>',
("edit", "Find next", "C-u C-s", '<Alt-bracketleft>',
"<<find-next>>", "<Control-u><Control-s>"), '<Control-bracketleft>'],
("edit", "Find same", "C-s", "<<find-same>>", "<Control-s>"), '<<do-nothing>>': ['<Control-x>'],
("edit", "Go to line", "Alt-g", "<<goto-line>>", "<Alt-g>", "<Meta-g>"), '<<dump-undo-state>>': ['<Control-backslash>'],
("edit", None, None), '<<end-of-file>>': ['<Control-d>'],
'<<expand-word>>': ['<Meta-slash>', '<Alt-slash>'],
("edit", "Dedent region", "Ctrl-[", "<<dedent-region>>", '<<find-next>>': ['<Control-u><Control-s>'],
"<Meta-bracketleft>", "<Alt-bracketleft>", "<Control-bracketleft>"), '<<find-same>>': ['<Control-s>'],
("edit", "Indent region", "Ctrl-]", "<<indent-region>>", '<<find>>': ['<Control-u><Control-u><Control-s>'],
"<Meta-bracketright>", "<Alt-bracketright>", "<Control-bracketright>"), '<<goto-line>>': ['<Alt-g>', '<Meta-g>'],
'<<goto-traceback-line>>': [],
("edit", "Comment out region", "Alt-3", '<<help>>': [],
"<<comment-region>>", "<Meta-Key-3>", "<Alt-Key-3>"), '<<history-next>>': ['<Meta-n>', '<Alt-n>'],
("edit", "Uncomment region", "Alt-4", '<<history-previous>>': ['<Meta-p>', '<Alt-p>'],
"<<uncomment-region>>", "<Meta-Key-4>", "<Alt-Key-4>"), '<<indent-region>>': ['<Meta-bracketright>',
'<Alt-bracketright>',
# Debug menu '<Control-bracketright>'],
'<<interrupt-execution>>': ['<Control-c>'],
("debug", "Go to line from traceback", None, "<<goto-traceback-line>>"), '<<newline-and-indent>>': ['<Key-Return>', '<KP_Enter>'],
("debug", "Open stack viewer", None, "<<open-stack-viewer>>"), '<<open-class-browser>>': ['<Control-x><Control-b>'],
("debug", "Toggle debugger", None, "<<toggle-debugger>>"), '<<open-module>>': ['<Control-x><Control-m>'],
'<<open-new-window>>': ['<Control-x><Control-n>'],
# Help menu '<<open-stack-viewer>>': [],
'<<open-window-from-file>>': ['<Control-x><Control-f>'],
("help", "Help...", None, "<<help>>"), '<<plain-newline-and-indent>>': ['<Control-j>'],
("help", None, None), '<<redo>>': ['<Alt-z>', '<Meta-z>'],
("help", "About IDLE...", None, "<<about-idle>>"), '<<save-copy-of-window-as-file>>': ['<Control-x><w>'],
'<<save-window-as-file>>': ['<Control-x><Control-w>'],
# Not in any menu '<<save-window>>': ['<Control-x><Control-s>'],
'<<toggle-auto-coloring>>': ['<Control-slash>'],
(None, None, None, "<<beginning-of-line>>", "<Control-a>", "<Home>"), '<<toggle-debugger>>': [],
(None, None, None, "<<center-insert>>", "<Control-l>"), '<<uncomment-region>>': ['<Meta-Key-4>', '<Alt-Key-4>'],
'<<undo>>': ['<Control-z>'],
(None, None, None, "<<expand-word>>", "<Meta-slash>", "<Alt-slash>"), }
(None, None, None, "<<newline-and-indent>>", "<Key-Return>", "<KP_Enter>"), def prepstr(s):
(None, None, None, "<<plain-newline-and-indent>>", "<Control-j>"), # Helper to extract the underscore from a string,
# e.g. prepstr("Co_py") returns (2, "Copy").
(None, None, None, "<<interrupt-execution>>", "<Control-c>"), i = string.find(s, '_')
(None, None, None, "<<end-of-file>>", "<Control-d>"), if i >= 0:
s = s[:i] + s[i+1:]
(None, None, None, "<<history-previous>>", "<Meta-p>", "<Alt-p>"), return i, s
(None, None, None, "<<history-next>>", "<Meta-n>", "<Alt-n>"),
keynames = {
(None, None, None, "<<toggle-auto-coloring>>", "<Control-slash>"), 'bracketleft': '[',
'bracketright': ']',
(None, None, None, "<<dump-undo-state>>", "<Control-backslash>"), }
(None, None, None, "<<do-nothing>>", "<Control-x>"), def getaccelerator(keydefs, event):
] keylist = keydefs.get(event)
if not keylist:
default_bindings = emacs_bindings return ""
s = keylist[0]
def apply_bindings(text, bindings=default_bindings): if s[:6] == "<Meta-":
event_add = text.event_add # Prefer Alt over Meta -- they should be the same thing anyway
for args in bindings: alts = "<Alt-" + s[6:]
args = args[3:] if alts in keylist:
if args[1:]: s = alts
apply(event_add, args) s = re.sub(r"-[a-z]\b", lambda m: string.upper(m.group()), s)
s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s)
def fill_menus(text, dict, bindings=default_bindings): s = re.sub("Key-", "", s)
# Fill the menus for the given text widget. The dict argument is s = re.sub("Control-", "Ctrl-", s)
s = re.sub("-", "+", s)
s = re.sub("><", " ", s)
s = re.sub("<", "", s)
s = re.sub(">", "", s)
return s
if sys.platform == 'win32':
default_keydefs = windows_keydefs
else:
default_keydefs = emacs_keydefs
def apply_bindings(text, keydefs=default_keydefs):
text.keydefs = keydefs
for event, keylist in keydefs.items():
if keylist:
apply(text.event_add, (event,) + tuple(keylist))
def fill_menus(text, menudict, defs=menudefs):
# Fill the menus for the given text widget. The menudict argument is
# a dictionary containing the menus, keyed by their lowercased name. # a dictionary containing the menus, keyed by their lowercased name.
# Menus that are absent or None are ignored. # Menus that are absent or None are ignored.
for args in bindings: if hasattr(text, "keydefs"):
menu, label, accelerator = args[:3] keydefs = text.keydefs
if not menu: else:
continue keydefs = default_keydefs
menu = dict.get(menu) for mname, itemlist in defs:
menu = menudict.get(mname)
if not menu: if not menu:
continue continue
if accelerator is None: for item in itemlist:
accelerator = "" if not item:
args = args[3:] menu.add_separator()
if args: else:
def command(text=text, event=args[0]): label, event = item
text.event_generate(event) underline, label = prepstr(label)
menu.add_command(label=label, accelerator=accelerator, accelerator = getaccelerator(keydefs, event)
command=command) def command(text=text, event=event):
elif label or accelerator: text.event_generate(event)
menu.add_command(label=label, accelerator=accelerator) menu.add_command(label=label, underline=underline,
else: command=command, accelerator=accelerator)
menu.add_separator()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment