Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 1 | # 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 Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 8 | import sys |
| 9 | import string |
| 10 | import re |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 11 | from keydefs import * |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 13 | menudefs = [ |
| 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 Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 19 | ('Class _browser', '<<open-class-browser>>'), |
| 20 | ('Python shell', '<<open-python-shell>>'), |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 21 | 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 Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 36 | ('Select _All', '<<select-all>>'), |
| 37 | ]), |
| 38 | ('script', [ |
| 39 | ('Run module', '<<run-module>>'), |
| 40 | ('Run script', '<<run-script>>'), |
| 41 | ('New shell', '<<new-shell>>'), |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 42 | ]), |
| 43 | ('debug', [ |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 44 | ('_Go to file/line', '<<goto-file-line>>'), |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 45 | ('_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 Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 53 | ] |
| 54 | |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 55 | def 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 Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 63 | keynames = { |
| 64 | 'bracketleft': '[', |
| 65 | 'bracketright': ']', |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 66 | 'slash': '/', |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 67 | } |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 69 | def get_accelerator(keydefs, event): |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 70 | keylist = keydefs.get(event) |
| 71 | if not keylist: |
| 72 | return "" |
| 73 | s = keylist[0] |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 74 | 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 Rossum | d8d676c | 1998-10-12 20:57:09 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 84 | if sys.platform == 'win32': |
| 85 | default_keydefs = windows_keydefs |
| 86 | else: |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 87 | default_keydefs = unix_keydefs |
Guido van Rossum | d8d676c | 1998-10-12 20:57:09 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 89 | def 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 Rossum | d8d676c | 1998-10-12 20:57:09 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 95 | def fill_menus(text, menudict, defs=menudefs, keydefs=default_keydefs): |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 96 | # Fill the menus for the given text widget. The menudict argument is |
Guido van Rossum | d8d676c | 1998-10-12 20:57:09 +0000 | [diff] [blame] | 97 | # a dictionary containing the menus, keyed by their lowercased name. |
Guido van Rossum | 219ffde | 1998-10-12 23:55:10 +0000 | [diff] [blame] | 98 | # Menus that are absent or None are ignored. |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 99 | for mname, itemlist in defs: |
| 100 | menu = menudict.get(mname) |
Guido van Rossum | d8d676c | 1998-10-12 20:57:09 +0000 | [diff] [blame] | 101 | if not menu: |
| 102 | continue |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 103 | 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 Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 109 | accelerator = get_accelerator(keydefs, event) |
Guido van Rossum | 6e0a413 | 1998-11-27 03:17:49 +0000 | [diff] [blame] | 110 | def command(text=text, event=event): |
| 111 | text.event_generate(event) |
| 112 | menu.add_command(label=label, underline=underline, |
| 113 | command=command, accelerator=accelerator) |