Brett Cannon | 50793b4 | 2013-06-07 13:17:48 -0400 | [diff] [blame] | 1 | import importlib.abc |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 2 | import importlib.util |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 3 | import os |
Terry Jan Reedy | 0726ddf | 2014-08-14 21:54:43 -0400 | [diff] [blame] | 4 | import platform |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 5 | import re |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 6 | import string |
Brett Cannon | aef82d3 | 2012-04-14 20:44:23 -0400 | [diff] [blame] | 7 | import sys |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 8 | import tokenize |
| 9 | import traceback |
| 10 | import webbrowser |
| 11 | |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 12 | from tkinter import * |
Terry Jan Reedy | 01e3575 | 2016-06-10 18:19:21 -0400 | [diff] [blame] | 13 | from tkinter.ttk import Scrollbar |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 14 | import tkinter.simpledialog as tkSimpleDialog |
| 15 | import tkinter.messagebox as tkMessageBox |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 16 | |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 17 | from idlelib.config import idleConf |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 18 | from idlelib import configdialog |
| 19 | from idlelib import grep |
Terry Jan Reedy | 5d46ab1 | 2015-09-20 19:57:13 -0400 | [diff] [blame] | 20 | from idlelib import help |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 21 | from idlelib import help_about |
| 22 | from idlelib import macosx |
| 23 | from idlelib.multicall import MultiCallCreator |
| 24 | from idlelib import pyparse |
| 25 | from idlelib import query |
| 26 | from idlelib import replace |
| 27 | from idlelib import search |
Terry Jan Reedy | a361e89 | 2018-06-20 21:25:59 -0400 | [diff] [blame^] | 28 | from idlelib import window |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 29 | |
| 30 | # The default tab setting for a Text widget, in average-width characters. |
| 31 | TK_TABWIDTH_DEFAULT = 8 |
Terry Jan Reedy | 0726ddf | 2014-08-14 21:54:43 -0400 | [diff] [blame] | 32 | _py_version = ' (%s)' % platform.python_version() |
Cheryl Sabella | d49dbd9 | 2018-06-04 11:48:21 -0400 | [diff] [blame] | 33 | darwin = sys.platform == 'darwin' |
Terry Jan Reedy | 0726ddf | 2014-08-14 21:54:43 -0400 | [diff] [blame] | 34 | |
Kurt B. Kaiser | c34ed8e | 2009-04-26 01:33:55 +0000 | [diff] [blame] | 35 | def _sphinx_version(): |
| 36 | "Format sys.version_info to produce the Sphinx version string used to install the chm docs" |
| 37 | major, minor, micro, level, serial = sys.version_info |
| 38 | release = '%s%s' % (major, minor) |
Martin v. Löwis | 7f9d181 | 2012-05-01 16:31:18 +0200 | [diff] [blame] | 39 | release += '%s' % (micro,) |
Benjamin Peterson | b48f634 | 2009-06-22 19:36:31 +0000 | [diff] [blame] | 40 | if level == 'candidate': |
| 41 | release += 'rc%s' % (serial,) |
| 42 | elif level != 'final': |
Kurt B. Kaiser | c34ed8e | 2009-04-26 01:33:55 +0000 | [diff] [blame] | 43 | release += '%s%s' % (level[0], serial) |
| 44 | return release |
| 45 | |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 46 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 47 | class EditorWindow(object): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 48 | from idlelib.percolator import Percolator |
Terry Jan Reedy | 2bac3b7 | 2016-05-29 01:40:22 -0400 | [diff] [blame] | 49 | from idlelib.colorizer import ColorDelegator, color_config |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 50 | from idlelib.undo import UndoDelegator |
Terry Jan Reedy | 7c15341 | 2016-06-26 17:48:02 -0400 | [diff] [blame] | 51 | from idlelib.iomenu import IOBinding, encoding |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 52 | from idlelib import mainmenu |
Cheryl Sabella | d49dbd9 | 2018-06-04 11:48:21 -0400 | [diff] [blame] | 53 | from tkinter import Toplevel, EventType |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 54 | from idlelib.statusbar import MultiStatusBar |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 55 | from idlelib.autocomplete import AutoComplete |
| 56 | from idlelib.autoexpand import AutoExpand |
Terry Jan Reedy | 06e2029 | 2018-06-19 23:00:35 -0400 | [diff] [blame] | 57 | from idlelib.calltip import Calltip |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 58 | from idlelib.codecontext import CodeContext |
| 59 | from idlelib.paragraph import FormatParagraph |
| 60 | from idlelib.parenmatch import ParenMatch |
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) | 9bb9223 | 2018-06-20 13:12:13 +0530 | [diff] [blame] | 61 | from idlelib.rstrip import Rstrip |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 62 | from idlelib.zoomheight import ZoomHeight |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 63 | |
Terry Jan Reedy | 7c15341 | 2016-06-26 17:48:02 -0400 | [diff] [blame] | 64 | filesystemencoding = sys.getfilesystemencoding() # for file names |
Kurt B. Kaiser | 114713d | 2003-01-10 05:07:24 +0000 | [diff] [blame] | 65 | help_url = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 66 | |
| 67 | def __init__(self, flist=None, filename=None, key=None, root=None): |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 68 | # Delay import: runscript imports pyshell imports EditorWindow. |
| 69 | from idlelib.runscript import ScriptBinding |
| 70 | |
Kurt B. Kaiser | 114713d | 2003-01-10 05:07:24 +0000 | [diff] [blame] | 71 | if EditorWindow.help_url is None: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 72 | dochome = os.path.join(sys.base_prefix, 'Doc', 'index.html') |
Kurt B. Kaiser | 114713d | 2003-01-10 05:07:24 +0000 | [diff] [blame] | 73 | if sys.platform.count('linux'): |
| 74 | # look for html docs in a couple of standard places |
| 75 | pyver = 'python-docs-' + '%s.%s.%s' % sys.version_info[:3] |
| 76 | if os.path.isdir('/var/www/html/python/'): # "python2" rpm |
| 77 | dochome = '/var/www/html/python/index.html' |
| 78 | else: |
| 79 | basepath = '/usr/share/doc/' # standard location |
| 80 | dochome = os.path.join(basepath, pyver, |
| 81 | 'Doc', 'index.html') |
Kurt B. Kaiser | 8aa2392 | 2004-07-15 04:54:57 +0000 | [diff] [blame] | 82 | elif sys.platform[:3] == 'win': |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 83 | chmfile = os.path.join(sys.base_prefix, 'Doc', |
Kurt B. Kaiser | c34ed8e | 2009-04-26 01:33:55 +0000 | [diff] [blame] | 84 | 'Python%s.chm' % _sphinx_version()) |
Thomas Heller | 84ef153 | 2003-09-23 20:53:10 +0000 | [diff] [blame] | 85 | if os.path.isfile(chmfile): |
| 86 | dochome = chmfile |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 87 | elif sys.platform == 'darwin': |
| 88 | # documentation may be stored inside a python framework |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 89 | dochome = os.path.join(sys.base_prefix, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 90 | 'Resources/English.lproj/Documentation/index.html') |
Kurt B. Kaiser | 114713d | 2003-01-10 05:07:24 +0000 | [diff] [blame] | 91 | dochome = os.path.normpath(dochome) |
| 92 | if os.path.isfile(dochome): |
| 93 | EditorWindow.help_url = dochome |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 94 | if sys.platform == 'darwin': |
| 95 | # Safari requires real file:-URLs |
| 96 | EditorWindow.help_url = 'file://' + EditorWindow.help_url |
Kurt B. Kaiser | 114713d | 2003-01-10 05:07:24 +0000 | [diff] [blame] | 97 | else: |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 98 | EditorWindow.help_url = ("https://docs.python.org/%d.%d/" |
| 99 | % sys.version_info[:2]) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 100 | self.flist = flist |
| 101 | root = root or flist.root |
| 102 | self.root = root |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 103 | self.menubar = Menu(root) |
Terry Jan Reedy | a361e89 | 2018-06-20 21:25:59 -0400 | [diff] [blame^] | 104 | self.top = top = window.ListedToplevel(root, menu=self.menubar) |
Steven M. Gava | 0c5bc8c | 2002-03-27 02:25:44 +0000 | [diff] [blame] | 105 | if flist: |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 106 | self.tkinter_vars = flist.vars |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 107 | #self.top.instance_dict makes flist.inversedict available to |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 108 | #configdialog.py so it can access all EditorWindow instances |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 109 | self.top.instance_dict = flist.inversedict |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 110 | else: |
| 111 | self.tkinter_vars = {} # keys: Tkinter event names |
| 112 | # values: Tkinter variable instances |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 113 | self.top.instance_dict = {} |
terryjreedy | 223c7e7 | 2017-07-07 22:28:06 -0400 | [diff] [blame] | 114 | self.recent_files_path = os.path.join( |
| 115 | idleConf.userdir, 'recent-files.lst') |
Terry Jan Reedy | e86172d | 2017-10-27 20:26:12 -0400 | [diff] [blame] | 116 | |
| 117 | self.prompt_last_line = '' # Override in PyShell |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 118 | self.text_frame = text_frame = Frame(top) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | self.vbar = vbar = Scrollbar(text_frame, name='vbar') |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 120 | self.width = idleConf.GetOption('main', 'EditorWindow', |
| 121 | 'width', type='int') |
Kurt B. Kaiser | 113f0e8 | 2009-04-04 20:38:52 +0000 | [diff] [blame] | 122 | text_options = { |
| 123 | 'name': 'text', |
| 124 | 'padx': 5, |
| 125 | 'wrap': 'none', |
Terry Jan Reedy | d36d817 | 2015-11-16 07:32:26 -0500 | [diff] [blame] | 126 | 'highlightthickness': 0, |
Kurt B. Kaiser | 113f0e8 | 2009-04-04 20:38:52 +0000 | [diff] [blame] | 127 | 'width': self.width, |
Terry Jan Reedy | 1080d13 | 2016-06-09 21:09:15 -0400 | [diff] [blame] | 128 | 'tabstyle': 'wordprocessor', # new in 8.5 |
| 129 | 'height': idleConf.GetOption( |
| 130 | 'main', 'EditorWindow', 'height', type='int'), |
| 131 | } |
Kurt B. Kaiser | 113f0e8 | 2009-04-04 20:38:52 +0000 | [diff] [blame] | 132 | self.text = text = MultiCallCreator(Text)(text_frame, **text_options) |
Kurt B. Kaiser | 183403a | 2004-08-22 05:14:32 +0000 | [diff] [blame] | 133 | self.top.focused_widget = self.text |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 134 | |
| 135 | self.createmenubar() |
| 136 | self.apply_bindings() |
| 137 | |
| 138 | self.top.protocol("WM_DELETE_WINDOW", self.close) |
| 139 | self.top.bind("<<close-window>>", self.close_event) |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 140 | if macosx.isAquaTk(): |
Terry Jan Reedy | a361e89 | 2018-06-20 21:25:59 -0400 | [diff] [blame^] | 141 | # Command-W on editor windows doesn't work without this. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 142 | text.bind('<<close-window>>', self.close_event) |
Terry Jan Reedy | 3c7eccd | 2015-09-22 21:10:27 -0400 | [diff] [blame] | 143 | # Some OS X systems have only one mouse button, so use |
| 144 | # control-click for popup context menus there. For two |
| 145 | # buttons, AquaTk defines <2> as the right button, not <3>. |
R. David Murray | b68a7bc | 2010-12-18 17:19:10 +0000 | [diff] [blame] | 146 | text.bind("<Control-Button-1>",self.right_menu_event) |
Terry Jan Reedy | 3c7eccd | 2015-09-22 21:10:27 -0400 | [diff] [blame] | 147 | text.bind("<2>", self.right_menu_event) |
R. David Murray | b68a7bc | 2010-12-18 17:19:10 +0000 | [diff] [blame] | 148 | else: |
Terry Jan Reedy | 3c7eccd | 2015-09-22 21:10:27 -0400 | [diff] [blame] | 149 | # Elsewhere, use right-click for popup menus. |
R. David Murray | b68a7bc | 2010-12-18 17:19:10 +0000 | [diff] [blame] | 150 | text.bind("<3>",self.right_menu_event) |
Cheryl Sabella | d49dbd9 | 2018-06-04 11:48:21 -0400 | [diff] [blame] | 151 | text.bind('<MouseWheel>', self.mousescroll) |
| 152 | text.bind('<Button-4>', self.mousescroll) |
| 153 | text.bind('<Button-5>', self.mousescroll) |
Kurt B. Kaiser | 84f4803 | 2002-09-26 22:13:22 +0000 | [diff] [blame] | 154 | text.bind("<<cut>>", self.cut) |
| 155 | text.bind("<<copy>>", self.copy) |
| 156 | text.bind("<<paste>>", self.paste) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 157 | text.bind("<<center-insert>>", self.center_insert_event) |
| 158 | text.bind("<<help>>", self.help_dialog) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 159 | text.bind("<<python-docs>>", self.python_docs) |
| 160 | text.bind("<<about-idle>>", self.about_dialog) |
Steven M. Gava | 3b55a89 | 2001-11-21 05:56:26 +0000 | [diff] [blame] | 161 | text.bind("<<open-config-dialog>>", self.config_dialog) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 162 | text.bind("<<open-module>>", self.open_module_event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 163 | text.bind("<<do-nothing>>", lambda event: "break") |
| 164 | text.bind("<<select-all>>", self.select_all) |
| 165 | text.bind("<<remove-selection>>", self.remove_selection) |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 166 | text.bind("<<find>>", self.find_event) |
| 167 | text.bind("<<find-again>>", self.find_again_event) |
| 168 | text.bind("<<find-in-files>>", self.find_in_files_event) |
| 169 | text.bind("<<find-selection>>", self.find_selection_event) |
| 170 | text.bind("<<replace>>", self.replace_event) |
| 171 | text.bind("<<goto-line>>", self.goto_line_event) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 172 | text.bind("<<smart-backspace>>",self.smart_backspace_event) |
| 173 | text.bind("<<newline-and-indent>>",self.newline_and_indent_event) |
| 174 | text.bind("<<smart-indent>>",self.smart_indent_event) |
| 175 | text.bind("<<indent-region>>",self.indent_region_event) |
| 176 | text.bind("<<dedent-region>>",self.dedent_region_event) |
| 177 | text.bind("<<comment-region>>",self.comment_region_event) |
| 178 | text.bind("<<uncomment-region>>",self.uncomment_region_event) |
| 179 | text.bind("<<tabify-region>>",self.tabify_region_event) |
| 180 | text.bind("<<untabify-region>>",self.untabify_region_event) |
| 181 | text.bind("<<toggle-tabs>>",self.toggle_tabs_event) |
| 182 | text.bind("<<change-indentwidth>>",self.change_indentwidth_event) |
Kurt B. Kaiser | 5ec186b | 2003-01-17 04:04:06 +0000 | [diff] [blame] | 183 | text.bind("<Left>", self.move_at_edge_if_selection(0)) |
| 184 | text.bind("<Right>", self.move_at_edge_if_selection(1)) |
Kurt B. Kaiser | 3069dbb | 2005-01-28 00:16:16 +0000 | [diff] [blame] | 185 | text.bind("<<del-word-left>>", self.del_word_left) |
| 186 | text.bind("<<del-word-right>>", self.del_word_right) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 187 | text.bind("<<beginning-of-line>>", self.home_callback) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 188 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 189 | if flist: |
| 190 | flist.inversedict[self] = key |
| 191 | if key: |
| 192 | flist.dict[key] = self |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 193 | text.bind("<<open-new-window>>", self.new_callback) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 194 | text.bind("<<close-all-windows>>", self.flist.close_all_callback) |
Cheryl Sabella | cd99e79 | 2017-09-23 16:46:01 -0400 | [diff] [blame] | 195 | text.bind("<<open-class-browser>>", self.open_module_browser) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 196 | text.bind("<<open-path-browser>>", self.open_path_browser) |
Terry Jan Reedy | 7e55db2 | 2014-07-28 22:23:59 -0400 | [diff] [blame] | 197 | text.bind("<<open-turtle-demo>>", self.open_turtle_demo) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 198 | |
Steven M. Gava | 898a365 | 2001-10-07 11:10:44 +0000 | [diff] [blame] | 199 | self.set_status_bar() |
Cheryl Sabella | d49dbd9 | 2018-06-04 11:48:21 -0400 | [diff] [blame] | 200 | vbar['command'] = self.handle_yview |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 201 | vbar.pack(side=RIGHT, fill=Y) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 202 | text['yscrollcommand'] = vbar.set |
Terry Jan Reedy | d87d168 | 2015-08-01 18:57:33 -0400 | [diff] [blame] | 203 | text['font'] = idleConf.GetFont(self.root, 'main', 'EditorWindow') |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 204 | text_frame.pack(side=LEFT, fill=BOTH, expand=1) |
| 205 | text.pack(side=TOP, fill=BOTH, expand=1) |
| 206 | text.focus_set() |
| 207 | |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 208 | # usetabs true -> literal tab characters are used by indent and |
| 209 | # dedent cmds, possibly mixed with spaces if |
| 210 | # indentwidth is not a multiple of tabwidth, |
| 211 | # which will cause Tabnanny to nag! |
| 212 | # false -> tab characters are converted to spaces by indent |
| 213 | # and dedent cmds, and ditto TAB keystrokes |
Kurt B. Kaiser | acdef85 | 2005-01-31 03:34:26 +0000 | [diff] [blame] | 214 | # Although use-spaces=0 can be configured manually in config-main.def, |
| 215 | # configuration of tabs v. spaces is not supported in the configuration |
| 216 | # dialog. IDLE promotes the preferred Python indentation: use spaces! |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 217 | usespaces = idleConf.GetOption('main', 'Indent', |
| 218 | 'use-spaces', type='bool') |
Kurt B. Kaiser | acdef85 | 2005-01-31 03:34:26 +0000 | [diff] [blame] | 219 | self.usetabs = not usespaces |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 220 | |
| 221 | # tabwidth is the display width of a literal tab character. |
| 222 | # CAUTION: telling Tk to use anything other than its default |
| 223 | # tab setting causes it to use an entirely different tabbing algorithm, |
| 224 | # treating tab stops as fixed distances from the left margin. |
| 225 | # Nobody expects this, so for now tabwidth should never be changed. |
Kurt B. Kaiser | acdef85 | 2005-01-31 03:34:26 +0000 | [diff] [blame] | 226 | self.tabwidth = 8 # must remain 8 until Tk is fixed. |
| 227 | |
| 228 | # indentwidth is the number of screen characters per indent level. |
| 229 | # The recommended Python indentation is four spaces. |
| 230 | self.indentwidth = self.tabwidth |
| 231 | self.set_notabs_indentwidth() |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 232 | |
| 233 | # If context_use_ps1 is true, parsing searches back for a ps1 line; |
| 234 | # else searches for a popular (if, def, ...) Python stmt. |
| 235 | self.context_use_ps1 = False |
| 236 | |
| 237 | # When searching backwards for a reliable place to begin parsing, |
| 238 | # first start num_context_lines[0] lines back, then |
| 239 | # num_context_lines[1] lines back if that didn't work, and so on. |
| 240 | # The last value should be huge (larger than the # of lines in a |
| 241 | # conceivable file). |
| 242 | # Making the initial values larger slows things down more often. |
| 243 | self.num_context_lines = 50, 500, 5000000 |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 244 | self.per = per = self.Percolator(text) |
Kurt B. Kaiser | dc1e709 | 2002-07-11 04:33:41 +0000 | [diff] [blame] | 245 | self.undo = undo = self.UndoDelegator() |
| 246 | per.insertfilter(undo) |
| 247 | text.undo_block_start = undo.undo_block_start |
| 248 | text.undo_block_stop = undo.undo_block_stop |
| 249 | undo.set_saved_change_hook(self.saved_change_hook) |
Kurt B. Kaiser | dc1e709 | 2002-07-11 04:33:41 +0000 | [diff] [blame] | 250 | # IOBinding implements file I/O and printing functionality |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 251 | self.io = io = self.IOBinding(self) |
Kurt B. Kaiser | dc1e709 | 2002-07-11 04:33:41 +0000 | [diff] [blame] | 252 | io.set_filename_change_hook(self.filename_change_hook) |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 253 | self.good_load = False |
| 254 | self.set_indentation_params(False) |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 255 | self.color = None # initialized below in self.ResetColorizer |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 256 | if filename: |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 257 | if os.path.exists(filename) and not os.path.isdir(filename): |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 258 | if io.loadfile(filename): |
| 259 | self.good_load = True |
| 260 | is_py_src = self.ispythonsource(filename) |
| 261 | self.set_indentation_params(is_py_src) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 262 | else: |
| 263 | io.set_filename(filename) |
Roger Serwy | 5b1ab24 | 2013-05-05 11:34:21 -0500 | [diff] [blame] | 264 | self.good_load = True |
| 265 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 266 | self.ResetColorizer() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 267 | self.saved_change_hook() |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 268 | self.update_recent_files_list() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 269 | self.load_extensions() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 270 | menu = self.menudict.get('windows') |
| 271 | if menu: |
| 272 | end = menu.index("end") |
| 273 | if end is None: |
| 274 | end = -1 |
| 275 | if end >= 0: |
| 276 | menu.add_separator() |
| 277 | end = end + 1 |
| 278 | self.wmenu_end = end |
Terry Jan Reedy | a361e89 | 2018-06-20 21:25:59 -0400 | [diff] [blame^] | 279 | window.register_callback(self.postwindowsmenu) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 280 | |
| 281 | # Some abstractions so IDLE extensions are cross-IDE |
| 282 | self.askyesno = tkMessageBox.askyesno |
| 283 | self.askinteger = tkSimpleDialog.askinteger |
| 284 | self.showerror = tkMessageBox.showerror |
| 285 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 286 | # Add pseudoevents for former extension fixed keys. |
| 287 | # (This probably needs to be done once in the process.) |
| 288 | text.event_add('<<autocomplete>>', '<Key-Tab>') |
| 289 | text.event_add('<<try-open-completions>>', '<KeyRelease-period>', |
| 290 | '<KeyRelease-slash>', '<KeyRelease-backslash>') |
| 291 | text.event_add('<<try-open-calltip>>', '<KeyRelease-parenleft>') |
| 292 | text.event_add('<<refresh-calltip>>', '<KeyRelease-parenright>') |
| 293 | text.event_add('<<paren-closed>>', '<KeyRelease-parenright>', |
| 294 | '<KeyRelease-bracketright>', '<KeyRelease-braceright>') |
| 295 | |
| 296 | # Former extension bindings depends on frame.text being packed |
| 297 | # (called from self.ResetColorizer()). |
| 298 | autocomplete = self.AutoComplete(self) |
| 299 | text.bind("<<autocomplete>>", autocomplete.autocomplete_event) |
| 300 | text.bind("<<try-open-completions>>", |
| 301 | autocomplete.try_open_completions_event) |
| 302 | text.bind("<<force-open-completions>>", |
| 303 | autocomplete.force_open_completions_event) |
| 304 | text.bind("<<expand-word>>", self.AutoExpand(self).expand_word_event) |
| 305 | text.bind("<<format-paragraph>>", |
| 306 | self.FormatParagraph(self).format_paragraph_event) |
| 307 | parenmatch = self.ParenMatch(self) |
| 308 | text.bind("<<flash-paren>>", parenmatch.flash_paren_event) |
| 309 | text.bind("<<paren-closed>>", parenmatch.paren_closed_event) |
| 310 | scriptbinding = ScriptBinding(self) |
| 311 | text.bind("<<check-module>>", scriptbinding.check_module_event) |
| 312 | text.bind("<<run-module>>", scriptbinding.run_module_event) |
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) | 9bb9223 | 2018-06-20 13:12:13 +0530 | [diff] [blame] | 313 | text.bind("<<do-rstrip>>", self.Rstrip(self).do_rstrip) |
Terry Jan Reedy | 06e2029 | 2018-06-19 23:00:35 -0400 | [diff] [blame] | 314 | ctip = self.Calltip(self) |
| 315 | text.bind("<<try-open-calltip>>", ctip.try_open_calltip_event) |
| 316 | #refresh-calltip must come after paren-closed to work right |
| 317 | text.bind("<<refresh-calltip>>", ctip.refresh_calltip_event) |
| 318 | text.bind("<<force-open-calltip>>", ctip.force_open_calltip_event) |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 319 | text.bind("<<zoom-height>>", self.ZoomHeight(self).zoom_height_event) |
| 320 | text.bind("<<toggle-code-context>>", |
| 321 | self.CodeContext(self).toggle_code_context_event) |
| 322 | |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 323 | def _filename_to_unicode(self, filename): |
Terry Jan Reedy | 5c28e9f | 2015-08-06 00:54:07 -0400 | [diff] [blame] | 324 | """Return filename as BMP unicode so diplayable in Tk.""" |
| 325 | # Decode bytes to unicode. |
| 326 | if isinstance(filename, bytes): |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 327 | try: |
Terry Jan Reedy | 5c28e9f | 2015-08-06 00:54:07 -0400 | [diff] [blame] | 328 | filename = filename.decode(self.filesystemencoding) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 329 | except UnicodeDecodeError: |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 330 | try: |
Terry Jan Reedy | 5c28e9f | 2015-08-06 00:54:07 -0400 | [diff] [blame] | 331 | filename = filename.decode(self.encoding) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 332 | except UnicodeDecodeError: |
| 333 | # byte-to-byte conversion |
Terry Jan Reedy | 5c28e9f | 2015-08-06 00:54:07 -0400 | [diff] [blame] | 334 | filename = filename.decode('iso8859-1') |
| 335 | # Replace non-BMP char with diamond questionmark. |
| 336 | return re.sub('[\U00010000-\U0010FFFF]', '\ufffd', filename) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 337 | |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 338 | def new_callback(self, event): |
| 339 | dirname, basename = self.io.defaultfilename() |
| 340 | self.flist.new(dirname) |
| 341 | return "break" |
| 342 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 343 | def home_callback(self, event): |
Kurt B. Kaiser | 75fc566 | 2011-03-21 02:13:42 -0400 | [diff] [blame] | 344 | if (event.state & 4) != 0 and event.keysym == "Home": |
| 345 | # state&4==Control. If <Control-Home>, use the Tk binding. |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 346 | return None |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 347 | if self.text.index("iomark") and \ |
| 348 | self.text.compare("iomark", "<=", "insert lineend") and \ |
| 349 | self.text.compare("insert linestart", "<=", "iomark"): |
Kurt B. Kaiser | 946f172 | 2011-03-25 20:29:13 -0400 | [diff] [blame] | 350 | # In Shell on input line, go to just after prompt |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 351 | insertpt = int(self.text.index("iomark").split(".")[1]) |
| 352 | else: |
| 353 | line = self.text.get("insert linestart", "insert lineend") |
Georg Brandl | 7d4f39a | 2008-07-19 13:53:58 +0000 | [diff] [blame] | 354 | for insertpt in range(len(line)): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 355 | if line[insertpt] not in (' ','\t'): |
| 356 | break |
| 357 | else: |
| 358 | insertpt=len(line) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 359 | lineat = int(self.text.index("insert").split('.')[1]) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 360 | if insertpt == lineat: |
| 361 | insertpt = 0 |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 362 | dest = "insert linestart+"+str(insertpt)+"c" |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 363 | if (event.state&1) == 0: |
Kurt B. Kaiser | 946f172 | 2011-03-25 20:29:13 -0400 | [diff] [blame] | 364 | # shift was not pressed |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 365 | self.text.tag_remove("sel", "1.0", "end") |
| 366 | else: |
| 367 | if not self.text.index("sel.first"): |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 368 | # there was no previous selection |
| 369 | self.text.mark_set("my_anchor", "insert") |
Kurt B. Kaiser | 946f172 | 2011-03-25 20:29:13 -0400 | [diff] [blame] | 370 | else: |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 371 | if self.text.compare(self.text.index("sel.first"), "<", |
| 372 | self.text.index("insert")): |
Kurt B. Kaiser | 946f172 | 2011-03-25 20:29:13 -0400 | [diff] [blame] | 373 | self.text.mark_set("my_anchor", "sel.first") # extend back |
| 374 | else: |
| 375 | self.text.mark_set("my_anchor", "sel.last") # extend forward |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 376 | first = self.text.index(dest) |
Kurt B. Kaiser | 946f172 | 2011-03-25 20:29:13 -0400 | [diff] [blame] | 377 | last = self.text.index("my_anchor") |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 378 | if self.text.compare(first,">",last): |
| 379 | first,last = last,first |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 380 | self.text.tag_remove("sel", "1.0", "end") |
| 381 | self.text.tag_add("sel", first, last) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 382 | self.text.mark_set("insert", dest) |
| 383 | self.text.see("insert") |
| 384 | return "break" |
| 385 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 386 | def set_status_bar(self): |
Steven M. Gava | 898a365 | 2001-10-07 11:10:44 +0000 | [diff] [blame] | 387 | self.status_bar = self.MultiStatusBar(self.top) |
Terry Jan Reedy | d36d817 | 2015-11-16 07:32:26 -0500 | [diff] [blame] | 388 | sep = Frame(self.top, height=1, borderwidth=1, background='grey75') |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 389 | if sys.platform == "darwin": |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 390 | # Insert some padding to avoid obscuring some of the statusbar |
| 391 | # by the resize widget. |
| 392 | self.status_bar.set_label('_padding1', ' ', side=RIGHT) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 393 | self.status_bar.set_label('column', 'Col: ?', side=RIGHT) |
| 394 | self.status_bar.set_label('line', 'Ln: ?', side=RIGHT) |
| 395 | self.status_bar.pack(side=BOTTOM, fill=X) |
Terry Jan Reedy | d36d817 | 2015-11-16 07:32:26 -0500 | [diff] [blame] | 396 | sep.pack(side=BOTTOM, fill=X) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 397 | self.text.bind("<<set-line-and-column>>", self.set_line_and_column) |
| 398 | self.text.event_add("<<set-line-and-column>>", |
| 399 | "<KeyRelease>", "<ButtonRelease>") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 400 | self.text.after_idle(self.set_line_and_column) |
| 401 | |
| 402 | def set_line_and_column(self, event=None): |
Kurt B. Kaiser | 220ecbc | 2002-09-16 02:13:15 +0000 | [diff] [blame] | 403 | line, column = self.text.index(INSERT).split('.') |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 404 | self.status_bar.set_label('column', 'Col: %s' % column) |
| 405 | self.status_bar.set_label('line', 'Ln: %s' % line) |
| 406 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 407 | menu_specs = [ |
| 408 | ("file", "_File"), |
| 409 | ("edit", "_Edit"), |
| 410 | ("format", "F_ormat"), |
| 411 | ("run", "_Run"), |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 412 | ("options", "_Options"), |
Ned Deily | ccb416f | 2015-01-17 21:06:27 -0800 | [diff] [blame] | 413 | ("windows", "_Window"), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 414 | ("help", "_Help"), |
| 415 | ] |
| 416 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 417 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 418 | def createmenubar(self): |
| 419 | mbar = self.menubar |
| 420 | self.menudict = menudict = {} |
| 421 | for name, label in self.menu_specs: |
| 422 | underline, label = prepstr(label) |
Terry Jan Reedy | 30f1f67 | 2015-07-30 16:44:22 -0400 | [diff] [blame] | 423 | menudict[name] = menu = Menu(mbar, name=name, tearoff=0) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 424 | mbar.add_cascade(label=label, menu=menu, underline=underline) |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 425 | if macosx.isCarbonTk(): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 426 | # Insert the application menu |
Terry Jan Reedy | 30f1f67 | 2015-07-30 16:44:22 -0400 | [diff] [blame] | 427 | menudict['application'] = menu = Menu(mbar, name='apple', |
| 428 | tearoff=0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 429 | mbar.add_cascade(label='IDLE', menu=menu) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 430 | self.fill_menus() |
Terry Jan Reedy | 30f1f67 | 2015-07-30 16:44:22 -0400 | [diff] [blame] | 431 | self.recent_files_menu = Menu(self.menubar, tearoff=0) |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 432 | self.menudict['file'].insert_cascade(3, label='Recent Files', |
| 433 | underline=0, |
| 434 | menu=self.recent_files_menu) |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 435 | self.base_helpmenu_length = self.menudict['help'].index(END) |
| 436 | self.reset_help_menu_entries() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 437 | |
| 438 | def postwindowsmenu(self): |
Terry Jan Reedy | a361e89 | 2018-06-20 21:25:59 -0400 | [diff] [blame^] | 439 | # Only called when Window menu exists |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 440 | menu = self.menudict['windows'] |
| 441 | end = menu.index("end") |
| 442 | if end is None: |
| 443 | end = -1 |
| 444 | if end > self.wmenu_end: |
| 445 | menu.delete(self.wmenu_end+1, end) |
Terry Jan Reedy | a361e89 | 2018-06-20 21:25:59 -0400 | [diff] [blame^] | 446 | window.add_windows_to_menu(menu) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 447 | |
Cheryl Sabella | d49dbd9 | 2018-06-04 11:48:21 -0400 | [diff] [blame] | 448 | def handle_yview(self, event, *args): |
| 449 | "Handle scrollbar." |
| 450 | if event == 'moveto': |
| 451 | fraction = float(args[0]) |
| 452 | lines = (round(self.getlineno('end') * fraction) - |
| 453 | self.getlineno('@0,0')) |
| 454 | event = 'scroll' |
| 455 | args = (lines, 'units') |
| 456 | self.text.yview(event, *args) |
| 457 | return 'break' |
| 458 | |
| 459 | def mousescroll(self, event): |
| 460 | "Handle scroll wheel." |
| 461 | up = {EventType.MouseWheel: event.delta >= 0 == darwin, |
| 462 | EventType.Button: event.num == 4} |
| 463 | lines = 5 |
| 464 | if up[event.type]: |
| 465 | lines = -lines |
| 466 | self.text.yview_scroll(lines, 'units') |
| 467 | return 'break' |
| 468 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 469 | rmenu = None |
| 470 | |
| 471 | def right_menu_event(self, event): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 472 | self.text.mark_set("insert", "@%d,%d" % (event.x, event.y)) |
| 473 | if not self.rmenu: |
| 474 | self.make_rmenu() |
| 475 | rmenu = self.rmenu |
| 476 | self.event = event |
| 477 | iswin = sys.platform[:3] == 'win' |
| 478 | if iswin: |
| 479 | self.text.config(cursor="arrow") |
Andrew Svetlov | d183767 | 2012-11-01 22:41:19 +0200 | [diff] [blame] | 480 | |
Roger Serwy | 6b2918a | 2013-04-07 12:15:52 -0500 | [diff] [blame] | 481 | for item in self.rmenu_specs: |
| 482 | try: |
| 483 | label, eventname, verify_state = item |
| 484 | except ValueError: # see issue1207589 |
| 485 | continue |
| 486 | |
Andrew Svetlov | d183767 | 2012-11-01 22:41:19 +0200 | [diff] [blame] | 487 | if verify_state is None: |
| 488 | continue |
| 489 | state = getattr(self, verify_state)() |
| 490 | rmenu.entryconfigure(label, state=state) |
| 491 | |
| 492 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 493 | rmenu.tk_popup(event.x_root, event.y_root) |
| 494 | if iswin: |
| 495 | self.text.config(cursor="ibeam") |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 496 | return "break" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 497 | |
| 498 | rmenu_specs = [ |
Andrew Svetlov | d183767 | 2012-11-01 22:41:19 +0200 | [diff] [blame] | 499 | # ("Label", "<<virtual-event>>", "statefuncname"), ... |
| 500 | ("Close", "<<close-window>>", None), # Example |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 501 | ] |
| 502 | |
| 503 | def make_rmenu(self): |
| 504 | rmenu = Menu(self.text, tearoff=0) |
Roger Serwy | 6b2918a | 2013-04-07 12:15:52 -0500 | [diff] [blame] | 505 | for item in self.rmenu_specs: |
| 506 | label, eventname = item[0], item[1] |
Andrew Svetlov | d183767 | 2012-11-01 22:41:19 +0200 | [diff] [blame] | 507 | if label is not None: |
| 508 | def command(text=self.text, eventname=eventname): |
| 509 | text.event_generate(eventname) |
| 510 | rmenu.add_command(label=label, command=command) |
| 511 | else: |
| 512 | rmenu.add_separator() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 513 | self.rmenu = rmenu |
| 514 | |
Andrew Svetlov | d183767 | 2012-11-01 22:41:19 +0200 | [diff] [blame] | 515 | def rmenu_check_cut(self): |
| 516 | return self.rmenu_check_copy() |
| 517 | |
| 518 | def rmenu_check_copy(self): |
| 519 | try: |
| 520 | indx = self.text.index('sel.first') |
| 521 | except TclError: |
| 522 | return 'disabled' |
| 523 | else: |
| 524 | return 'normal' if indx else 'disabled' |
| 525 | |
| 526 | def rmenu_check_paste(self): |
| 527 | try: |
| 528 | self.text.tk.call('tk::GetSelection', self.text, 'CLIPBOARD') |
| 529 | except TclError: |
| 530 | return 'disabled' |
| 531 | else: |
| 532 | return 'normal' |
| 533 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 534 | def about_dialog(self, event=None): |
Terry Jan Reedy | b50c637 | 2015-09-20 22:55:39 -0400 | [diff] [blame] | 535 | "Handle Help 'About IDLE' event." |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 536 | # Synchronize with macosx.overrideRootMenu.about_dialog. |
csabella | 18ede06 | 2017-06-23 20:00:58 -0400 | [diff] [blame] | 537 | help_about.AboutDialog(self.top) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 538 | return "break" |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 539 | |
Steven M. Gava | 3b55a89 | 2001-11-21 05:56:26 +0000 | [diff] [blame] | 540 | def config_dialog(self, event=None): |
Terry Jan Reedy | b50c637 | 2015-09-20 22:55:39 -0400 | [diff] [blame] | 541 | "Handle Options 'Configure IDLE' event." |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 542 | # Synchronize with macosx.overrideRootMenu.config_dialog. |
| 543 | configdialog.ConfigDialog(self.top,'Settings') |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 544 | return "break" |
Terry Jan Reedy | b50c637 | 2015-09-20 22:55:39 -0400 | [diff] [blame] | 545 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 546 | def help_dialog(self, event=None): |
Terry Jan Reedy | b50c637 | 2015-09-20 22:55:39 -0400 | [diff] [blame] | 547 | "Handle Help 'IDLE Help' event." |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 548 | # Synchronize with macosx.overrideRootMenu.help_dialog. |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 549 | if self.root: |
| 550 | parent = self.root |
| 551 | else: |
| 552 | parent = self.top |
Terry Jan Reedy | 5d46ab1 | 2015-09-20 19:57:13 -0400 | [diff] [blame] | 553 | help.show_idlehelp(parent) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 554 | return "break" |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 555 | |
Kurt B. Kaiser | 114713d | 2003-01-10 05:07:24 +0000 | [diff] [blame] | 556 | def python_docs(self, event=None): |
Kurt B. Kaiser | 8aa2392 | 2004-07-15 04:54:57 +0000 | [diff] [blame] | 557 | if sys.platform[:3] == 'win': |
Terry Reedy | 6739cc0 | 2011-01-01 02:25:36 +0000 | [diff] [blame] | 558 | try: |
| 559 | os.startfile(self.help_url) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 560 | except OSError as why: |
Terry Reedy | 6739cc0 | 2011-01-01 02:25:36 +0000 | [diff] [blame] | 561 | tkMessageBox.showerror(title='Document Start Failure', |
| 562 | message=str(why), parent=self.text) |
Kurt B. Kaiser | 114713d | 2003-01-10 05:07:24 +0000 | [diff] [blame] | 563 | else: |
| 564 | webbrowser.open(self.help_url) |
Kurt B. Kaiser | 8aa2392 | 2004-07-15 04:54:57 +0000 | [diff] [blame] | 565 | return "break" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 566 | |
Kurt B. Kaiser | 84f4803 | 2002-09-26 22:13:22 +0000 | [diff] [blame] | 567 | def cut(self,event): |
| 568 | self.text.event_generate("<<Cut>>") |
| 569 | return "break" |
| 570 | |
| 571 | def copy(self,event): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 572 | if not self.text.tag_ranges("sel"): |
| 573 | # There is no selection, so do nothing and maybe interrupt. |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 574 | return None |
Kurt B. Kaiser | 84f4803 | 2002-09-26 22:13:22 +0000 | [diff] [blame] | 575 | self.text.event_generate("<<Copy>>") |
| 576 | return "break" |
| 577 | |
| 578 | def paste(self,event): |
| 579 | self.text.event_generate("<<Paste>>") |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 580 | self.text.see("insert") |
Kurt B. Kaiser | 84f4803 | 2002-09-26 22:13:22 +0000 | [diff] [blame] | 581 | return "break" |
| 582 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 583 | def select_all(self, event=None): |
| 584 | self.text.tag_add("sel", "1.0", "end-1c") |
| 585 | self.text.mark_set("insert", "1.0") |
| 586 | self.text.see("insert") |
| 587 | return "break" |
| 588 | |
| 589 | def remove_selection(self, event=None): |
| 590 | self.text.tag_remove("sel", "1.0", "end") |
| 591 | self.text.see("insert") |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 592 | return "break" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 593 | |
Kurt B. Kaiser | 5ec186b | 2003-01-17 04:04:06 +0000 | [diff] [blame] | 594 | def move_at_edge_if_selection(self, edge_index): |
| 595 | """Cursor move begins at start or end of selection |
| 596 | |
| 597 | When a left/right cursor key is pressed create and return to Tkinter a |
| 598 | function which causes a cursor move from the associated edge of the |
| 599 | selection. |
| 600 | |
| 601 | """ |
| 602 | self_text_index = self.text.index |
| 603 | self_text_mark_set = self.text.mark_set |
| 604 | edges_table = ("sel.first+1c", "sel.last-1c") |
| 605 | def move_at_edge(event): |
| 606 | if (event.state & 5) == 0: # no shift(==1) or control(==4) pressed |
| 607 | try: |
| 608 | self_text_index("sel.first") |
| 609 | self_text_mark_set("insert", edges_table[edge_index]) |
| 610 | except TclError: |
| 611 | pass |
| 612 | return move_at_edge |
| 613 | |
Kurt B. Kaiser | 3069dbb | 2005-01-28 00:16:16 +0000 | [diff] [blame] | 614 | def del_word_left(self, event): |
| 615 | self.text.event_generate('<Meta-Delete>') |
| 616 | return "break" |
| 617 | |
| 618 | def del_word_right(self, event): |
| 619 | self.text.event_generate('<Meta-d>') |
| 620 | return "break" |
| 621 | |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 622 | def find_event(self, event): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 623 | search.find(self.text) |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 624 | return "break" |
| 625 | |
| 626 | def find_again_event(self, event): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 627 | search.find_again(self.text) |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 628 | return "break" |
| 629 | |
| 630 | def find_selection_event(self, event): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 631 | search.find_selection(self.text) |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 632 | return "break" |
| 633 | |
| 634 | def find_in_files_event(self, event): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 635 | grep.grep(self.text, self.io, self.flist) |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 636 | return "break" |
| 637 | |
| 638 | def replace_event(self, event): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 639 | replace.replace(self.text) |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 640 | return "break" |
| 641 | |
| 642 | def goto_line_event(self, event): |
| 643 | text = self.text |
| 644 | lineno = tkSimpleDialog.askinteger("Goto", |
| 645 | "Go to line number:",parent=text) |
| 646 | if lineno is None: |
| 647 | return "break" |
| 648 | if lineno <= 0: |
| 649 | text.bell() |
| 650 | return "break" |
| 651 | text.mark_set("insert", "%d.0" % lineno) |
| 652 | text.see("insert") |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 653 | return "break" |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 654 | |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 655 | def open_module(self): |
Terry Jan Reedy | 0cd6b97 | 2016-07-03 19:11:13 -0400 | [diff] [blame] | 656 | """Get module name from user and open it. |
| 657 | |
Cheryl Sabella | cd99e79 | 2017-09-23 16:46:01 -0400 | [diff] [blame] | 658 | Return module path or None for calls by open_module_browser |
Terry Jan Reedy | 0cd6b97 | 2016-07-03 19:11:13 -0400 | [diff] [blame] | 659 | when latter is not invoked in named editor window. |
| 660 | """ |
Cheryl Sabella | cd99e79 | 2017-09-23 16:46:01 -0400 | [diff] [blame] | 661 | # XXX This, open_module_browser, and open_path_browser |
Terry Jan Reedy | 0cd6b97 | 2016-07-03 19:11:13 -0400 | [diff] [blame] | 662 | # would fit better in iomenu.IOBinding. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 663 | try: |
Terry Jan Reedy | 0cd6b97 | 2016-07-03 19:11:13 -0400 | [diff] [blame] | 664 | name = self.text.get("sel.first", "sel.last").strip() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 665 | except TclError: |
Terry Jan Reedy | 0cd6b97 | 2016-07-03 19:11:13 -0400 | [diff] [blame] | 666 | name = '' |
| 667 | file_path = query.ModuleName( |
| 668 | self.text, "Open Module", |
| 669 | "Enter the name of a Python module\n" |
| 670 | "to search on sys.path and open:", |
| 671 | name).result |
| 672 | if file_path is not None: |
| 673 | if self.flist: |
| 674 | self.flist.open(file_path) |
| 675 | else: |
| 676 | self.io.loadfile(file_path) |
Terry Jan Reedy | 380ec63 | 2014-10-15 22:01:31 -0400 | [diff] [blame] | 677 | return file_path |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 678 | |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 679 | def open_module_event(self, event): |
| 680 | self.open_module() |
| 681 | return "break" |
| 682 | |
Cheryl Sabella | cd99e79 | 2017-09-23 16:46:01 -0400 | [diff] [blame] | 683 | def open_module_browser(self, event=None): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 684 | filename = self.io.filename |
Terry Jan Reedy | 380ec63 | 2014-10-15 22:01:31 -0400 | [diff] [blame] | 685 | if not (self.__class__.__name__ == 'PyShellEditorWindow' |
| 686 | and filename): |
| 687 | filename = self.open_module() |
| 688 | if filename is None: |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 689 | return "break" |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 690 | from idlelib import browser |
Terry Jan Reedy | d6bb65f | 2017-09-30 19:54:28 -0400 | [diff] [blame] | 691 | browser.ModuleBrowser(self.root, filename) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 692 | return "break" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 693 | |
| 694 | def open_path_browser(self, event=None): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 695 | from idlelib import pathbrowser |
Cheryl Sabella | 20d48a4 | 2017-11-22 19:05:25 -0500 | [diff] [blame] | 696 | pathbrowser.PathBrowser(self.root) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 697 | return "break" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 698 | |
Terry Jan Reedy | 7e55db2 | 2014-07-28 22:23:59 -0400 | [diff] [blame] | 699 | def open_turtle_demo(self, event = None): |
| 700 | import subprocess |
| 701 | |
| 702 | cmd = [sys.executable, |
| 703 | '-c', |
| 704 | 'from turtledemo.__main__ import main; main()'] |
Terry Jan Reedy | 038c16b | 2015-05-15 23:03:17 -0400 | [diff] [blame] | 705 | subprocess.Popen(cmd, shell=False) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 706 | return "break" |
Terry Jan Reedy | 7e55db2 | 2014-07-28 22:23:59 -0400 | [diff] [blame] | 707 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 708 | def gotoline(self, lineno): |
| 709 | if lineno is not None and lineno > 0: |
| 710 | self.text.mark_set("insert", "%d.0" % lineno) |
| 711 | self.text.tag_remove("sel", "1.0", "end") |
| 712 | self.text.tag_add("sel", "insert", "insert +1l") |
| 713 | self.center() |
| 714 | |
| 715 | def ispythonsource(self, filename): |
Kurt B. Kaiser | df506ea | 2005-06-12 04:33:30 +0000 | [diff] [blame] | 716 | if not filename or os.path.isdir(filename): |
Kurt B. Kaiser | 220ecbc | 2002-09-16 02:13:15 +0000 | [diff] [blame] | 717 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 718 | base, ext = os.path.splitext(os.path.basename(filename)) |
| 719 | if os.path.normcase(ext) in (".py", ".pyw"): |
Kurt B. Kaiser | 220ecbc | 2002-09-16 02:13:15 +0000 | [diff] [blame] | 720 | return True |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 721 | line = self.text.get('1.0', '1.0 lineend') |
| 722 | return line.startswith('#!') and 'python' in line |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 723 | |
| 724 | def close_hook(self): |
| 725 | if self.flist: |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 726 | self.flist.unregister_maybe_terminate(self) |
| 727 | self.flist = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 728 | |
| 729 | def set_close_hook(self, close_hook): |
| 730 | self.close_hook = close_hook |
| 731 | |
| 732 | def filename_change_hook(self): |
| 733 | if self.flist: |
| 734 | self.flist.filename_changed_edit(self) |
| 735 | self.saved_change_hook() |
Kurt B. Kaiser | 260cb90 | 2003-06-06 21:58:38 +0000 | [diff] [blame] | 736 | self.top.update_windowlist_registry(self) |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 737 | self.ResetColorizer() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 738 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 739 | def _addcolorizer(self): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 740 | if self.color: |
| 741 | return |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 742 | if self.ispythonsource(self.io.filename): |
| 743 | self.color = self.ColorDelegator() |
| 744 | # can add more colorizers here... |
| 745 | if self.color: |
| 746 | self.per.removefilter(self.undo) |
| 747 | self.per.insertfilter(self.color) |
| 748 | self.per.insertfilter(self.undo) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 749 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 750 | def _rmcolorizer(self): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 751 | if not self.color: |
| 752 | return |
Kurt B. Kaiser | df506ea | 2005-06-12 04:33:30 +0000 | [diff] [blame] | 753 | self.color.removecolors() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 754 | self.per.removefilter(self.color) |
| 755 | self.color = None |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 756 | |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 757 | def ResetColorizer(self): |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 758 | "Update the color theme" |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 759 | # Called from self.filename_change_hook and from configdialog.py |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 760 | self._rmcolorizer() |
| 761 | self._addcolorizer() |
Terry Jan Reedy | 2bac3b7 | 2016-05-29 01:40:22 -0400 | [diff] [blame] | 762 | EditorWindow.color_config(self.text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 763 | |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 764 | IDENTCHARS = string.ascii_letters + string.digits + "_" |
| 765 | |
| 766 | def colorize_syntax_error(self, text, pos): |
| 767 | text.tag_add("ERROR", pos) |
| 768 | char = text.get(pos) |
| 769 | if char and char in self.IDENTCHARS: |
| 770 | text.tag_add("ERROR", pos + " wordstart", pos) |
| 771 | if '\n' == text.get(pos): # error at line end |
| 772 | text.mark_set("insert", pos) |
| 773 | else: |
| 774 | text.mark_set("insert", pos + "+1c") |
| 775 | text.see(pos) |
| 776 | |
Steven M. Gava | b158541 | 2002-03-12 00:21:56 +0000 | [diff] [blame] | 777 | def ResetFont(self): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 778 | "Update the text widgets' font if it is changed" |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 779 | # Called from configdialog.py |
Terry Jan Reedy | d87d168 | 2015-08-01 18:57:33 -0400 | [diff] [blame] | 780 | |
| 781 | self.text['font'] = idleConf.GetFont(self.root, 'main','EditorWindow') |
Steven M. Gava | b158541 | 2002-03-12 00:21:56 +0000 | [diff] [blame] | 782 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 783 | def RemoveKeybindings(self): |
| 784 | "Remove the keybindings before they are changed." |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 785 | # Called from configdialog.py |
| 786 | self.mainmenu.default_keydefs = keydefs = idleConf.GetCurrentKeySet() |
Steven M. Gava | dbfe92c | 2002-03-18 02:38:44 +0000 | [diff] [blame] | 787 | for event, keylist in keydefs.items(): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 788 | self.text.event_delete(event, *keylist) |
| 789 | for extensionName in self.get_standard_extension_names(): |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 790 | xkeydefs = idleConf.GetExtensionBindings(extensionName) |
| 791 | if xkeydefs: |
| 792 | for event, keylist in xkeydefs.items(): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 793 | self.text.event_delete(event, *keylist) |
| 794 | |
| 795 | def ApplyKeybindings(self): |
| 796 | "Update the keybindings after they are changed" |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 797 | # Called from configdialog.py |
| 798 | self.mainmenu.default_keydefs = keydefs = idleConf.GetCurrentKeySet() |
Steven M. Gava | dbfe92c | 2002-03-18 02:38:44 +0000 | [diff] [blame] | 799 | self.apply_bindings() |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 800 | for extensionName in self.get_standard_extension_names(): |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 801 | xkeydefs = idleConf.GetExtensionBindings(extensionName) |
| 802 | if xkeydefs: |
| 803 | self.apply_bindings(xkeydefs) |
Steven M. Gava | dbfe92c | 2002-03-18 02:38:44 +0000 | [diff] [blame] | 804 | #update menu accelerators |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 805 | menuEventDict = {} |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 806 | for menu in self.mainmenu.menudefs: |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 807 | menuEventDict[menu[0]] = {} |
Steven M. Gava | dbfe92c | 2002-03-18 02:38:44 +0000 | [diff] [blame] | 808 | for item in menu[1]: |
| 809 | if item: |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 810 | menuEventDict[menu[0]][prepstr(item[0])[1]] = item[1] |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 811 | for menubarItem in self.menudict: |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 812 | menu = self.menudict[menubarItem] |
Ned Deily | 8e8b9ba | 2013-07-20 15:06:26 -0700 | [diff] [blame] | 813 | end = menu.index(END) |
| 814 | if end is None: |
| 815 | # Skip empty menus |
| 816 | continue |
| 817 | end += 1 |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 818 | for index in range(0, end): |
| 819 | if menu.type(index) == 'command': |
| 820 | accel = menu.entrycget(index, 'accelerator') |
Steven M. Gava | dbfe92c | 2002-03-18 02:38:44 +0000 | [diff] [blame] | 821 | if accel: |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 822 | itemName = menu.entrycget(index, 'label') |
| 823 | event = '' |
Guido van Rossum | 811c4e0 | 2006-08-22 15:45:46 +0000 | [diff] [blame] | 824 | if menubarItem in menuEventDict: |
| 825 | if itemName in menuEventDict[menubarItem]: |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 826 | event = menuEventDict[menubarItem][itemName] |
Steven M. Gava | dbfe92c | 2002-03-18 02:38:44 +0000 | [diff] [blame] | 827 | if event: |
Kurt B. Kaiser | 5a67f9b | 2005-11-22 01:47:14 +0000 | [diff] [blame] | 828 | accel = get_accelerator(keydefs, event) |
| 829 | menu.entryconfig(index, accelerator=accel) |
Steven M. Gava | dbfe92c | 2002-03-18 02:38:44 +0000 | [diff] [blame] | 830 | |
Kurt B. Kaiser | acdef85 | 2005-01-31 03:34:26 +0000 | [diff] [blame] | 831 | def set_notabs_indentwidth(self): |
| 832 | "Update the indentwidth if changed and not using tabs in this window" |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 833 | # Called from configdialog.py |
Kurt B. Kaiser | acdef85 | 2005-01-31 03:34:26 +0000 | [diff] [blame] | 834 | if not self.usetabs: |
| 835 | self.indentwidth = idleConf.GetOption('main', 'Indent','num-spaces', |
| 836 | type='int') |
| 837 | |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 838 | def reset_help_menu_entries(self): |
| 839 | "Update the additional help entries on the Help menu" |
| 840 | help_list = idleConf.GetAllExtraHelpSourcesList() |
| 841 | helpmenu = self.menudict['help'] |
| 842 | # first delete the extra help entries, if any |
| 843 | helpmenu_length = helpmenu.index(END) |
| 844 | if helpmenu_length > self.base_helpmenu_length: |
| 845 | helpmenu.delete((self.base_helpmenu_length + 1), helpmenu_length) |
| 846 | # then rebuild them |
| 847 | if help_list: |
| 848 | helpmenu.add_separator() |
| 849 | for entry in help_list: |
| 850 | cmd = self.__extra_help_callback(entry[1]) |
| 851 | helpmenu.add_command(label=entry[0], command=cmd) |
| 852 | # and update the menu dictionary |
| 853 | self.menudict['help'] = helpmenu |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 854 | |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 855 | def __extra_help_callback(self, helpfile): |
| 856 | "Create a callback with the helpfile value frozen at definition time" |
| 857 | def display_extra_help(helpfile=helpfile): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 858 | if not helpfile.startswith(('www', 'http')): |
Terry Reedy | 6739cc0 | 2011-01-01 02:25:36 +0000 | [diff] [blame] | 859 | helpfile = os.path.normpath(helpfile) |
Kurt B. Kaiser | 8aa2392 | 2004-07-15 04:54:57 +0000 | [diff] [blame] | 860 | if sys.platform[:3] == 'win': |
Terry Reedy | 6739cc0 | 2011-01-01 02:25:36 +0000 | [diff] [blame] | 861 | try: |
| 862 | os.startfile(helpfile) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 863 | except OSError as why: |
Terry Reedy | 6739cc0 | 2011-01-01 02:25:36 +0000 | [diff] [blame] | 864 | tkMessageBox.showerror(title='Document Start Failure', |
| 865 | message=str(why), parent=self.text) |
Kurt B. Kaiser | 8aa2392 | 2004-07-15 04:54:57 +0000 | [diff] [blame] | 866 | else: |
| 867 | webbrowser.open(helpfile) |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 868 | return display_extra_help |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 869 | |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 870 | def update_recent_files_list(self, new_file=None): |
| 871 | "Load and update the recent files list and menus" |
| 872 | rf_list = [] |
| 873 | if os.path.exists(self.recent_files_path): |
Terry Jan Reedy | 95f34ab | 2013-08-04 15:39:03 -0400 | [diff] [blame] | 874 | with open(self.recent_files_path, 'r', |
| 875 | encoding='utf_8', errors='replace') as rf_list_file: |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 876 | rf_list = rf_list_file.readlines() |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 877 | if new_file: |
| 878 | new_file = os.path.abspath(new_file) + '\n' |
| 879 | if new_file in rf_list: |
| 880 | rf_list.remove(new_file) # move to top |
| 881 | rf_list.insert(0, new_file) |
| 882 | # clean and save the recent files list |
| 883 | bad_paths = [] |
| 884 | for path in rf_list: |
| 885 | if '\0' in path or not os.path.exists(path[0:-1]): |
| 886 | bad_paths.append(path) |
| 887 | rf_list = [path for path in rf_list if path not in bad_paths] |
| 888 | ulchars = "1234567890ABCDEFGHIJK" |
| 889 | rf_list = rf_list[0:len(ulchars)] |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 890 | try: |
Ned Deily | f505b74 | 2011-12-14 14:58:24 -0800 | [diff] [blame] | 891 | with open(self.recent_files_path, 'w', |
| 892 | encoding='utf_8', errors='replace') as rf_file: |
| 893 | rf_file.writelines(rf_list) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 894 | except OSError as err: |
Ned Deily | f505b74 | 2011-12-14 14:58:24 -0800 | [diff] [blame] | 895 | if not getattr(self.root, "recentfilelist_error_displayed", False): |
| 896 | self.root.recentfilelist_error_displayed = True |
Terry Jan Reedy | 4c973e9 | 2015-10-27 03:38:02 -0400 | [diff] [blame] | 897 | tkMessageBox.showwarning(title='IDLE Warning', |
| 898 | message="Cannot update File menu Recent Files list. " |
| 899 | "Your operating system says:\n%s\n" |
| 900 | "Select OK and IDLE will continue without updating." |
| 901 | % self._filename_to_unicode(str(err)), |
Ned Deily | f505b74 | 2011-12-14 14:58:24 -0800 | [diff] [blame] | 902 | parent=self.text) |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 903 | # for each edit window instance, construct the recent files menu |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 904 | for instance in self.top.instance_dict: |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 905 | menu = instance.recent_files_menu |
Ned Deily | 3aee941 | 2012-05-29 10:43:36 -0700 | [diff] [blame] | 906 | menu.delete(0, END) # clear, and rebuild: |
Guilherme Polo | 1fff008 | 2009-08-14 15:05:30 +0000 | [diff] [blame] | 907 | for i, file_name in enumerate(rf_list): |
| 908 | file_name = file_name.rstrip() # zap \n |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 909 | # make unicode string to display non-ASCII chars correctly |
| 910 | ufile_name = self._filename_to_unicode(file_name) |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 911 | callback = instance.__recent_file_callback(file_name) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 912 | menu.add_command(label=ulchars[i] + " " + ufile_name, |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 913 | command=callback, |
| 914 | underline=0) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 915 | |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 916 | def __recent_file_callback(self, file_name): |
| 917 | def open_recent_file(fn_closure=file_name): |
| 918 | self.io.open(editFile=fn_closure) |
| 919 | return open_recent_file |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 920 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 921 | def saved_change_hook(self): |
| 922 | short = self.short_title() |
| 923 | long = self.long_title() |
| 924 | if short and long: |
Terry Jan Reedy | 0726ddf | 2014-08-14 21:54:43 -0400 | [diff] [blame] | 925 | title = short + " - " + long + _py_version |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 926 | elif short: |
| 927 | title = short |
| 928 | elif long: |
| 929 | title = long |
| 930 | else: |
| 931 | title = "Untitled" |
| 932 | icon = short or long or title |
| 933 | if not self.get_saved(): |
| 934 | title = "*%s*" % title |
| 935 | icon = "*%s" % icon |
| 936 | self.top.wm_title(title) |
| 937 | self.top.wm_iconname(icon) |
| 938 | |
| 939 | def get_saved(self): |
| 940 | return self.undo.get_saved() |
| 941 | |
| 942 | def set_saved(self, flag): |
| 943 | self.undo.set_saved(flag) |
| 944 | |
| 945 | def reset_undo(self): |
| 946 | self.undo.reset_undo() |
| 947 | |
| 948 | def short_title(self): |
| 949 | filename = self.io.filename |
| 950 | if filename: |
| 951 | filename = os.path.basename(filename) |
Terry Jan Reedy | 94338de | 2014-01-23 00:36:46 -0500 | [diff] [blame] | 952 | else: |
| 953 | filename = "Untitled" |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 954 | # return unicode string to display non-ASCII chars correctly |
Terry Jan Reedy | 0726ddf | 2014-08-14 21:54:43 -0400 | [diff] [blame] | 955 | return self._filename_to_unicode(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 956 | |
| 957 | def long_title(self): |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 958 | # return unicode string to display non-ASCII chars correctly |
| 959 | return self._filename_to_unicode(self.io.filename or "") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 960 | |
| 961 | def center_insert_event(self, event): |
| 962 | self.center() |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 963 | return "break" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 964 | |
| 965 | def center(self, mark="insert"): |
| 966 | text = self.text |
| 967 | top, bot = self.getwindowlines() |
| 968 | lineno = self.getlineno(mark) |
| 969 | height = bot - top |
Kurt B. Kaiser | 220ecbc | 2002-09-16 02:13:15 +0000 | [diff] [blame] | 970 | newtop = max(1, lineno - height//2) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 971 | text.yview(float(newtop)) |
| 972 | |
| 973 | def getwindowlines(self): |
| 974 | text = self.text |
| 975 | top = self.getlineno("@0,0") |
| 976 | bot = self.getlineno("@0,65535") |
| 977 | if top == bot and text.winfo_height() == 1: |
| 978 | # Geometry manager hasn't run yet |
| 979 | height = int(text['height']) |
| 980 | bot = top + height - 1 |
| 981 | return top, bot |
| 982 | |
| 983 | def getlineno(self, mark="insert"): |
| 984 | text = self.text |
| 985 | return int(float(text.index(mark))) |
| 986 | |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 987 | def get_geometry(self): |
| 988 | "Return (width, height, x, y)" |
| 989 | geom = self.top.wm_geometry() |
| 990 | m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", geom) |
Kurt B. Kaiser | 66aaf74 | 2007-08-09 18:00:23 +0000 | [diff] [blame] | 991 | return list(map(int, m.groups())) |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 992 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 993 | def close_event(self, event): |
| 994 | self.close() |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 995 | return "break" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 996 | |
| 997 | def maybesave(self): |
| 998 | if self.io: |
Steven M. Gava | 67716b5 | 2002-02-26 02:31:03 +0000 | [diff] [blame] | 999 | if not self.get_saved(): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 1000 | if self.top.state()!='normal': |
Steven M. Gava | 67716b5 | 2002-02-26 02:31:03 +0000 | [diff] [blame] | 1001 | self.top.deiconify() |
| 1002 | self.top.lower() |
| 1003 | self.top.lift() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1004 | return self.io.maybesave() |
| 1005 | |
| 1006 | def close(self): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1007 | reply = self.maybesave() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1008 | if str(reply) != "cancel": |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1009 | self._close() |
| 1010 | return reply |
| 1011 | |
| 1012 | def _close(self): |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 1013 | if self.io.filename: |
Kurt B. Kaiser | cf6f1b6 | 2004-04-11 03:16:07 +0000 | [diff] [blame] | 1014 | self.update_recent_files_list(new_file=self.io.filename) |
Terry Jan Reedy | a361e89 | 2018-06-20 21:25:59 -0400 | [diff] [blame^] | 1015 | window.unregister_callback(self.postwindowsmenu) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1016 | self.unload_extensions() |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1017 | self.io.close() |
| 1018 | self.io = None |
| 1019 | self.undo = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1020 | if self.color: |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1021 | self.color.close(False) |
| 1022 | self.color = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1023 | self.text = None |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1024 | self.tkinter_vars = None |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1025 | self.per.close() |
| 1026 | self.per = None |
| 1027 | self.top.destroy() |
| 1028 | if self.close_hook: |
| 1029 | # unless override: unregister from flist, terminate if last window |
| 1030 | self.close_hook() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1031 | |
| 1032 | def load_extensions(self): |
| 1033 | self.extensions = {} |
| 1034 | self.load_standard_extensions() |
| 1035 | |
| 1036 | def unload_extensions(self): |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 1037 | for ins in list(self.extensions.values()): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1038 | if hasattr(ins, "close"): |
| 1039 | ins.close() |
| 1040 | self.extensions = {} |
| 1041 | |
| 1042 | def load_standard_extensions(self): |
| 1043 | for name in self.get_standard_extension_names(): |
| 1044 | try: |
| 1045 | self.load_extension(name) |
| 1046 | except: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1047 | print("Failed to load extension", repr(name)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1048 | traceback.print_exc() |
| 1049 | |
| 1050 | def get_standard_extension_names(self): |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 1051 | return idleConf.GetExtensions(editor_only=True) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1052 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 1053 | extfiles = { # Map built-in config-extension section names to file names. |
| 1054 | 'ZzDummy': 'zzdummy', |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1055 | } |
| 1056 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1057 | def load_extension(self, name): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1058 | fname = self.extfiles.get(name, name) |
Kurt B. Kaiser | b00e89f | 2005-01-18 00:54:58 +0000 | [diff] [blame] | 1059 | try: |
Brett Cannon | aef82d3 | 2012-04-14 20:44:23 -0400 | [diff] [blame] | 1060 | try: |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1061 | mod = importlib.import_module('.' + fname, package=__package__) |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 1062 | except (ImportError, TypeError): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1063 | mod = importlib.import_module(fname) |
Kurt B. Kaiser | b00e89f | 2005-01-18 00:54:58 +0000 | [diff] [blame] | 1064 | except ImportError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1065 | print("\nFailed to import extension: ", name) |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 1066 | raise |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1067 | cls = getattr(mod, name) |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 1068 | keydefs = idleConf.GetExtensionBindings(name) |
| 1069 | if hasattr(cls, "menudefs"): |
| 1070 | self.fill_menus(cls.menudefs, keydefs) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1071 | ins = cls(self) |
| 1072 | self.extensions[name] = ins |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1073 | if keydefs: |
| 1074 | self.apply_bindings(keydefs) |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 1075 | for vevent in keydefs: |
Kurt B. Kaiser | 220ecbc | 2002-09-16 02:13:15 +0000 | [diff] [blame] | 1076 | methodname = vevent.replace("-", "_") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1077 | while methodname[:1] == '<': |
| 1078 | methodname = methodname[1:] |
| 1079 | while methodname[-1:] == '>': |
| 1080 | methodname = methodname[:-1] |
| 1081 | methodname = methodname + "_event" |
| 1082 | if hasattr(ins, methodname): |
| 1083 | self.text.bind(vevent, getattr(ins, methodname)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1084 | |
| 1085 | def apply_bindings(self, keydefs=None): |
| 1086 | if keydefs is None: |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1087 | keydefs = self.mainmenu.default_keydefs |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1088 | text = self.text |
| 1089 | text.keydefs = keydefs |
| 1090 | for event, keylist in keydefs.items(): |
| 1091 | if keylist: |
Raymond Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 1092 | text.event_add(event, *keylist) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1093 | |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1094 | def fill_menus(self, menudefs=None, keydefs=None): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 1095 | """Add appropriate entries to the menus and submenus |
| 1096 | |
| 1097 | Menus that are absent or None in self.menudict are ignored. |
| 1098 | """ |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1099 | if menudefs is None: |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1100 | menudefs = self.mainmenu.menudefs |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1101 | if keydefs is None: |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1102 | keydefs = self.mainmenu.default_keydefs |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1103 | menudict = self.menudict |
| 1104 | text = self.text |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1105 | for mname, entrylist in menudefs: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1106 | menu = menudict.get(mname) |
| 1107 | if not menu: |
| 1108 | continue |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1109 | for entry in entrylist: |
| 1110 | if not entry: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1111 | menu.add_separator() |
| 1112 | else: |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1113 | label, eventname = entry |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1114 | checkbutton = (label[:1] == '!') |
| 1115 | if checkbutton: |
| 1116 | label = label[1:] |
| 1117 | underline, label = prepstr(label) |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1118 | accelerator = get_accelerator(keydefs, eventname) |
| 1119 | def command(text=text, eventname=eventname): |
| 1120 | text.event_generate(eventname) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1121 | if checkbutton: |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1122 | var = self.get_var_obj(eventname, BooleanVar) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1123 | menu.add_checkbutton(label=label, underline=underline, |
| 1124 | command=command, accelerator=accelerator, |
| 1125 | variable=var) |
| 1126 | else: |
| 1127 | menu.add_command(label=label, underline=underline, |
Kurt B. Kaiser | 84f4803 | 2002-09-26 22:13:22 +0000 | [diff] [blame] | 1128 | command=command, |
| 1129 | accelerator=accelerator) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1130 | |
| 1131 | def getvar(self, name): |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1132 | var = self.get_var_obj(name) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1133 | if var: |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1134 | value = var.get() |
| 1135 | return value |
| 1136 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 1137 | raise NameError(name) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1138 | |
| 1139 | def setvar(self, name, value, vartype=None): |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1140 | var = self.get_var_obj(name, vartype) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1141 | if var: |
| 1142 | var.set(value) |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1143 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 1144 | raise NameError(name) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1145 | |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1146 | def get_var_obj(self, name, vartype=None): |
| 1147 | var = self.tkinter_vars.get(name) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1148 | if not var and vartype: |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1149 | # create a Tkinter variable object with self.text as master: |
| 1150 | self.tkinter_vars[name] = var = vartype(self.text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1151 | return var |
| 1152 | |
| 1153 | # Tk implementations of "virtual text methods" -- each platform |
| 1154 | # reusing IDLE's support code needs to define these for its GUI's |
| 1155 | # flavor of widget. |
| 1156 | |
| 1157 | # Is character at text_index in a Python string? Return 0 for |
| 1158 | # "guaranteed no", true for anything else. This info is expensive |
| 1159 | # to compute ab initio, but is probably already known by the |
| 1160 | # platform's colorizer. |
| 1161 | |
| 1162 | def is_char_in_string(self, text_index): |
| 1163 | if self.color: |
| 1164 | # Return true iff colorizer hasn't (re)gotten this far |
| 1165 | # yet, or the character is tagged as being in a string |
| 1166 | return self.text.tag_prevrange("TODO", text_index) or \ |
| 1167 | "STRING" in self.text.tag_names(text_index) |
| 1168 | else: |
| 1169 | # The colorizer is missing: assume the worst |
| 1170 | return 1 |
| 1171 | |
| 1172 | # If a selection is defined in the text widget, return (start, |
| 1173 | # end) as Tkinter text indices, otherwise return (None, None) |
| 1174 | def get_selection_indices(self): |
| 1175 | try: |
| 1176 | first = self.text.index("sel.first") |
| 1177 | last = self.text.index("sel.last") |
| 1178 | return first, last |
| 1179 | except TclError: |
| 1180 | return None, None |
| 1181 | |
| 1182 | # Return the text widget's current view of what a tab stop means |
| 1183 | # (equivalent width in spaces). |
| 1184 | |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 1185 | def get_tk_tabwidth(self): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1186 | current = self.text['tabs'] or TK_TABWIDTH_DEFAULT |
| 1187 | return int(current) |
| 1188 | |
| 1189 | # Set the text widget's current view of what a tab stop means. |
| 1190 | |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 1191 | def set_tk_tabwidth(self, newtabwidth): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1192 | text = self.text |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 1193 | if self.get_tk_tabwidth() != newtabwidth: |
| 1194 | # Set text widget tab width |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1195 | pixels = text.tk.call("font", "measure", text["font"], |
| 1196 | "-displayof", text.master, |
Kurt B. Kaiser | afdf71b | 2001-07-13 03:35:32 +0000 | [diff] [blame] | 1197 | "n" * newtabwidth) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1198 | text.configure(tabs=pixels) |
| 1199 | |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 1200 | ### begin autoindent code ### (configuration was moved to beginning of class) |
| 1201 | |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 1202 | def set_indentation_params(self, is_py_src, guess=True): |
| 1203 | if is_py_src and guess: |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1204 | i = self.guess_indent() |
| 1205 | if 2 <= i <= 8: |
| 1206 | self.indentwidth = i |
| 1207 | if self.indentwidth != self.tabwidth: |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 1208 | self.usetabs = False |
Kurt B. Kaiser | 105f60e | 2007-09-06 04:03:04 +0000 | [diff] [blame] | 1209 | self.set_tk_tabwidth(self.tabwidth) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1210 | |
| 1211 | def smart_backspace_event(self, event): |
| 1212 | text = self.text |
| 1213 | first, last = self.get_selection_indices() |
| 1214 | if first and last: |
| 1215 | text.delete(first, last) |
| 1216 | text.mark_set("insert", first) |
| 1217 | return "break" |
| 1218 | # Delete whitespace left, until hitting a real char or closest |
| 1219 | # preceding virtual tab stop. |
| 1220 | chars = text.get("insert linestart", "insert") |
| 1221 | if chars == '': |
| 1222 | if text.compare("insert", ">", "1.0"): |
| 1223 | # easy: delete preceding newline |
| 1224 | text.delete("insert-1c") |
| 1225 | else: |
| 1226 | text.bell() # at start of buffer |
| 1227 | return "break" |
| 1228 | if chars[-1] not in " \t": |
| 1229 | # easy: delete preceding real char |
| 1230 | text.delete("insert-1c") |
| 1231 | return "break" |
| 1232 | # Ick. It may require *inserting* spaces if we back up over a |
| 1233 | # tab character! This is written to be clear, not fast. |
Kurt B. Kaiser | 1b3c269 | 2002-09-15 21:31:30 +0000 | [diff] [blame] | 1234 | tabwidth = self.tabwidth |
| 1235 | have = len(chars.expandtabs(tabwidth)) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1236 | assert have > 0 |
| 1237 | want = ((have - 1) // self.indentwidth) * self.indentwidth |
Kurt B. Kaiser | 4ada7ad | 2002-12-29 22:03:38 +0000 | [diff] [blame] | 1238 | # Debug prompt is multilined.... |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1239 | ncharsdeleted = 0 |
| 1240 | while 1: |
Terry Jan Reedy | e86172d | 2017-10-27 20:26:12 -0400 | [diff] [blame] | 1241 | if chars == self.prompt_last_line: # '' unless PyShell |
Kurt B. Kaiser | 1bdca5e | 2002-12-16 22:25:10 +0000 | [diff] [blame] | 1242 | break |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1243 | chars = chars[:-1] |
| 1244 | ncharsdeleted = ncharsdeleted + 1 |
Kurt B. Kaiser | 1b3c269 | 2002-09-15 21:31:30 +0000 | [diff] [blame] | 1245 | have = len(chars.expandtabs(tabwidth)) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1246 | if have <= want or chars[-1] not in " \t": |
| 1247 | break |
| 1248 | text.undo_block_start() |
| 1249 | text.delete("insert-%dc" % ncharsdeleted, "insert") |
| 1250 | if have < want: |
| 1251 | text.insert("insert", ' ' * (want - have)) |
| 1252 | text.undo_block_stop() |
| 1253 | return "break" |
| 1254 | |
| 1255 | def smart_indent_event(self, event): |
| 1256 | # if intraline selection: |
| 1257 | # delete it |
| 1258 | # elif multiline selection: |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 1259 | # do indent-region |
| 1260 | # else: |
| 1261 | # indent one level |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1262 | text = self.text |
| 1263 | first, last = self.get_selection_indices() |
| 1264 | text.undo_block_start() |
| 1265 | try: |
| 1266 | if first and last: |
| 1267 | if index2line(first) != index2line(last): |
| 1268 | return self.indent_region_event(event) |
| 1269 | text.delete(first, last) |
| 1270 | text.mark_set("insert", first) |
| 1271 | prefix = text.get("insert linestart", "insert") |
| 1272 | raw, effective = classifyws(prefix, self.tabwidth) |
| 1273 | if raw == len(prefix): |
| 1274 | # only whitespace to the left |
| 1275 | self.reindent_to(effective + self.indentwidth) |
| 1276 | else: |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 1277 | # tab to the next 'stop' within or to right of line's text: |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1278 | if self.usetabs: |
| 1279 | pad = '\t' |
| 1280 | else: |
Kurt B. Kaiser | 1b3c269 | 2002-09-15 21:31:30 +0000 | [diff] [blame] | 1281 | effective = len(prefix.expandtabs(self.tabwidth)) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1282 | n = self.indentwidth |
| 1283 | pad = ' ' * (n - effective % n) |
| 1284 | text.insert("insert", pad) |
| 1285 | text.see("insert") |
| 1286 | return "break" |
| 1287 | finally: |
| 1288 | text.undo_block_stop() |
| 1289 | |
| 1290 | def newline_and_indent_event(self, event): |
| 1291 | text = self.text |
| 1292 | first, last = self.get_selection_indices() |
| 1293 | text.undo_block_start() |
| 1294 | try: |
| 1295 | if first and last: |
| 1296 | text.delete(first, last) |
| 1297 | text.mark_set("insert", first) |
| 1298 | line = text.get("insert linestart", "insert") |
| 1299 | i, n = 0, len(line) |
| 1300 | while i < n and line[i] in " \t": |
| 1301 | i = i+1 |
| 1302 | if i == n: |
Kurt B. Kaiser | 4ada7ad | 2002-12-29 22:03:38 +0000 | [diff] [blame] | 1303 | # the cursor is in or at leading indentation in a continuation |
| 1304 | # line; just inject an empty line at the start |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1305 | text.insert("insert linestart", '\n') |
| 1306 | return "break" |
| 1307 | indent = line[:i] |
Kurt B. Kaiser | 4ada7ad | 2002-12-29 22:03:38 +0000 | [diff] [blame] | 1308 | # strip whitespace before insert point unless it's in the prompt |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1309 | i = 0 |
Terry Jan Reedy | e86172d | 2017-10-27 20:26:12 -0400 | [diff] [blame] | 1310 | while line and line[-1] in " \t" and line != self.prompt_last_line: |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1311 | line = line[:-1] |
| 1312 | i = i+1 |
| 1313 | if i: |
| 1314 | text.delete("insert - %d chars" % i, "insert") |
| 1315 | # strip whitespace after insert point |
| 1316 | while text.get("insert") in " \t": |
| 1317 | text.delete("insert") |
| 1318 | # start new line |
| 1319 | text.insert("insert", '\n') |
| 1320 | |
| 1321 | # adjust indentation for continuations and block |
| 1322 | # open/close first need to find the last stmt |
| 1323 | lno = index2line(text.index('insert')) |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1324 | y = pyparse.Parser(self.indentwidth, self.tabwidth) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 1325 | if not self.context_use_ps1: |
| 1326 | for context in self.num_context_lines: |
| 1327 | startat = max(lno - context, 1) |
Brett Cannon | 0b70cca | 2006-08-25 02:59:59 +0000 | [diff] [blame] | 1328 | startatindex = repr(startat) + ".0" |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 1329 | rawtext = text.get(startatindex, "insert") |
Cheryl Sabella | c29c03a | 2018-02-23 21:35:27 -0500 | [diff] [blame] | 1330 | y.set_code(rawtext) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 1331 | bod = y.find_good_parse_start( |
| 1332 | self.context_use_ps1, |
| 1333 | self._build_char_in_string_func(startatindex)) |
| 1334 | if bod is not None or startat == 1: |
| 1335 | break |
| 1336 | y.set_lo(bod or 0) |
| 1337 | else: |
| 1338 | r = text.tag_prevrange("console", "insert") |
| 1339 | if r: |
| 1340 | startatindex = r[1] |
| 1341 | else: |
| 1342 | startatindex = "1.0" |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1343 | rawtext = text.get(startatindex, "insert") |
Cheryl Sabella | c29c03a | 2018-02-23 21:35:27 -0500 | [diff] [blame] | 1344 | y.set_code(rawtext) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 1345 | y.set_lo(0) |
| 1346 | |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1347 | c = y.get_continuation_type() |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1348 | if c != pyparse.C_NONE: |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1349 | # The current stmt hasn't ended yet. |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1350 | if c == pyparse.C_STRING_FIRST_LINE: |
Kurt B. Kaiser | b61602c | 2005-11-15 07:20:06 +0000 | [diff] [blame] | 1351 | # after the first line of a string; do not indent at all |
| 1352 | pass |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1353 | elif c == pyparse.C_STRING_NEXT_LINES: |
Kurt B. Kaiser | b61602c | 2005-11-15 07:20:06 +0000 | [diff] [blame] | 1354 | # inside a string which started before this line; |
| 1355 | # just mimic the current indent |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1356 | text.insert("insert", indent) |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1357 | elif c == pyparse.C_BRACKET: |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1358 | # line up with the first (if any) element of the |
| 1359 | # last open bracket structure; else indent one |
| 1360 | # level beyond the indent of the line with the |
| 1361 | # last open bracket |
| 1362 | self.reindent_to(y.compute_bracket_indent()) |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1363 | elif c == pyparse.C_BACKSLASH: |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1364 | # if more than one line in this stmt already, just |
| 1365 | # mimic the current indent; else if initial line |
| 1366 | # has a start on an assignment stmt, indent to |
| 1367 | # beyond leftmost =; else to beyond first chunk of |
| 1368 | # non-whitespace on initial line |
| 1369 | if y.get_num_lines_in_stmt() > 1: |
| 1370 | text.insert("insert", indent) |
| 1371 | else: |
| 1372 | self.reindent_to(y.compute_backslash_indent()) |
| 1373 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1374 | assert 0, "bogus continuation type %r" % (c,) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1375 | return "break" |
| 1376 | |
| 1377 | # This line starts a brand new stmt; indent relative to |
| 1378 | # indentation of initial line of closest preceding |
| 1379 | # interesting stmt. |
| 1380 | indent = y.get_base_indent_string() |
| 1381 | text.insert("insert", indent) |
| 1382 | if y.is_block_opener(): |
| 1383 | self.smart_indent_event(event) |
| 1384 | elif indent and y.is_block_closer(): |
| 1385 | self.smart_backspace_event(event) |
| 1386 | return "break" |
| 1387 | finally: |
| 1388 | text.see("insert") |
| 1389 | text.undo_block_stop() |
| 1390 | |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 1391 | # Our editwin provides an is_char_in_string function that works |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1392 | # with a Tk text index, but PyParse only knows about offsets into |
| 1393 | # a string. This builds a function for PyParse that accepts an |
| 1394 | # offset. |
| 1395 | |
| 1396 | def _build_char_in_string_func(self, startindex): |
| 1397 | def inner(offset, _startindex=startindex, |
| 1398 | _icis=self.is_char_in_string): |
| 1399 | return _icis(_startindex + "+%dc" % offset) |
| 1400 | return inner |
| 1401 | |
| 1402 | def indent_region_event(self, event): |
| 1403 | head, tail, chars, lines = self.get_region() |
| 1404 | for pos in range(len(lines)): |
| 1405 | line = lines[pos] |
| 1406 | if line: |
| 1407 | raw, effective = classifyws(line, self.tabwidth) |
| 1408 | effective = effective + self.indentwidth |
| 1409 | lines[pos] = self._make_blanks(effective) + line[raw:] |
| 1410 | self.set_region(head, tail, chars, lines) |
| 1411 | return "break" |
| 1412 | |
| 1413 | def dedent_region_event(self, event): |
| 1414 | head, tail, chars, lines = self.get_region() |
| 1415 | for pos in range(len(lines)): |
| 1416 | line = lines[pos] |
| 1417 | if line: |
| 1418 | raw, effective = classifyws(line, self.tabwidth) |
| 1419 | effective = max(effective - self.indentwidth, 0) |
| 1420 | lines[pos] = self._make_blanks(effective) + line[raw:] |
| 1421 | self.set_region(head, tail, chars, lines) |
| 1422 | return "break" |
| 1423 | |
| 1424 | def comment_region_event(self, event): |
| 1425 | head, tail, chars, lines = self.get_region() |
| 1426 | for pos in range(len(lines) - 1): |
| 1427 | line = lines[pos] |
| 1428 | lines[pos] = '##' + line |
| 1429 | self.set_region(head, tail, chars, lines) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 1430 | return "break" |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1431 | |
| 1432 | def uncomment_region_event(self, event): |
| 1433 | head, tail, chars, lines = self.get_region() |
| 1434 | for pos in range(len(lines)): |
| 1435 | line = lines[pos] |
| 1436 | if not line: |
| 1437 | continue |
| 1438 | if line[:2] == '##': |
| 1439 | line = line[2:] |
| 1440 | elif line[:1] == '#': |
| 1441 | line = line[1:] |
| 1442 | lines[pos] = line |
| 1443 | self.set_region(head, tail, chars, lines) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 1444 | return "break" |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1445 | |
| 1446 | def tabify_region_event(self, event): |
| 1447 | head, tail, chars, lines = self.get_region() |
| 1448 | tabwidth = self._asktabwidth() |
Roger Serwy | 0ef392c | 2013-04-06 20:26:53 -0500 | [diff] [blame] | 1449 | if tabwidth is None: return |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1450 | for pos in range(len(lines)): |
| 1451 | line = lines[pos] |
| 1452 | if line: |
| 1453 | raw, effective = classifyws(line, tabwidth) |
| 1454 | ntabs, nspaces = divmod(effective, tabwidth) |
| 1455 | lines[pos] = '\t' * ntabs + ' ' * nspaces + line[raw:] |
| 1456 | self.set_region(head, tail, chars, lines) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 1457 | return "break" |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1458 | |
| 1459 | def untabify_region_event(self, event): |
| 1460 | head, tail, chars, lines = self.get_region() |
| 1461 | tabwidth = self._asktabwidth() |
Roger Serwy | 0ef392c | 2013-04-06 20:26:53 -0500 | [diff] [blame] | 1462 | if tabwidth is None: return |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1463 | for pos in range(len(lines)): |
Kurt B. Kaiser | 1b3c269 | 2002-09-15 21:31:30 +0000 | [diff] [blame] | 1464 | lines[pos] = lines[pos].expandtabs(tabwidth) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1465 | self.set_region(head, tail, chars, lines) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 1466 | return "break" |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1467 | |
| 1468 | def toggle_tabs_event(self, event): |
| 1469 | if self.askyesno( |
| 1470 | "Toggle tabs", |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 1471 | "Turn tabs " + ("on", "off")[self.usetabs] + |
| 1472 | "?\nIndent width " + |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1473 | ("will be", "remains at")[self.usetabs] + " 8." + |
| 1474 | "\n Note: a tab is always 8 columns", |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1475 | parent=self.text): |
| 1476 | self.usetabs = not self.usetabs |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1477 | # Try to prevent inconsistent indentation. |
| 1478 | # User must change indent width manually after using tabs. |
| 1479 | self.indentwidth = 8 |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1480 | return "break" |
| 1481 | |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 1482 | # XXX this isn't bound to anything -- see tabwidth comments |
| 1483 | ## def change_tabwidth_event(self, event): |
| 1484 | ## new = self._asktabwidth() |
| 1485 | ## if new != self.tabwidth: |
| 1486 | ## self.tabwidth = new |
| 1487 | ## self.set_indentation_params(0, guess=0) |
| 1488 | ## return "break" |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1489 | |
| 1490 | def change_indentwidth_event(self, event): |
| 1491 | new = self.askinteger( |
| 1492 | "Indent width", |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 1493 | "New indent width (2-16)\n(Always use 8 when using tabs)", |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1494 | parent=self.text, |
| 1495 | initialvalue=self.indentwidth, |
| 1496 | minvalue=2, |
| 1497 | maxvalue=16) |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 1498 | if new and new != self.indentwidth and not self.usetabs: |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1499 | self.indentwidth = new |
| 1500 | return "break" |
| 1501 | |
| 1502 | def get_region(self): |
| 1503 | text = self.text |
| 1504 | first, last = self.get_selection_indices() |
| 1505 | if first and last: |
| 1506 | head = text.index(first + " linestart") |
| 1507 | tail = text.index(last + "-1c lineend +1c") |
| 1508 | else: |
| 1509 | head = text.index("insert linestart") |
| 1510 | tail = text.index("insert lineend +1c") |
| 1511 | chars = text.get(head, tail) |
Kurt B. Kaiser | 1b3c269 | 2002-09-15 21:31:30 +0000 | [diff] [blame] | 1512 | lines = chars.split("\n") |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1513 | return head, tail, chars, lines |
| 1514 | |
| 1515 | def set_region(self, head, tail, chars, lines): |
| 1516 | text = self.text |
Kurt B. Kaiser | 1b3c269 | 2002-09-15 21:31:30 +0000 | [diff] [blame] | 1517 | newchars = "\n".join(lines) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1518 | if newchars == chars: |
| 1519 | text.bell() |
| 1520 | return |
| 1521 | text.tag_remove("sel", "1.0", "end") |
| 1522 | text.mark_set("insert", head) |
| 1523 | text.undo_block_start() |
| 1524 | text.delete(head, tail) |
| 1525 | text.insert(head, newchars) |
| 1526 | text.undo_block_stop() |
| 1527 | text.tag_add("sel", head, "insert") |
| 1528 | |
| 1529 | # Make string that displays as n leading blanks. |
| 1530 | |
| 1531 | def _make_blanks(self, n): |
| 1532 | if self.usetabs: |
| 1533 | ntabs, nspaces = divmod(n, self.tabwidth) |
| 1534 | return '\t' * ntabs + ' ' * nspaces |
| 1535 | else: |
| 1536 | return ' ' * n |
| 1537 | |
| 1538 | # Delete from beginning of line to insert point, then reinsert |
| 1539 | # column logical (meaning use tabs if appropriate) spaces. |
| 1540 | |
| 1541 | def reindent_to(self, column): |
| 1542 | text = self.text |
| 1543 | text.undo_block_start() |
| 1544 | if text.compare("insert linestart", "!=", "insert"): |
| 1545 | text.delete("insert linestart", "insert") |
| 1546 | if column: |
| 1547 | text.insert("insert", self._make_blanks(column)) |
| 1548 | text.undo_block_stop() |
| 1549 | |
| 1550 | def _asktabwidth(self): |
| 1551 | return self.askinteger( |
| 1552 | "Tab width", |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 1553 | "Columns per tab? (2-16)", |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1554 | parent=self.text, |
| 1555 | initialvalue=self.indentwidth, |
| 1556 | minvalue=2, |
Roger Serwy | 0ef392c | 2013-04-06 20:26:53 -0500 | [diff] [blame] | 1557 | maxvalue=16) |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1558 | |
| 1559 | # Guess indentwidth from text content. |
| 1560 | # Return guessed indentwidth. This should not be believed unless |
| 1561 | # it's in a reasonable range (e.g., it will be 0 if no indented |
| 1562 | # blocks are found). |
| 1563 | |
| 1564 | def guess_indent(self): |
| 1565 | opener, indented = IndentSearcher(self.text, self.tabwidth).run() |
| 1566 | if opener and indented: |
| 1567 | raw, indentsmall = classifyws(opener, self.tabwidth) |
| 1568 | raw, indentlarge = classifyws(indented, self.tabwidth) |
| 1569 | else: |
| 1570 | indentsmall = indentlarge = 0 |
| 1571 | return indentlarge - indentsmall |
| 1572 | |
| 1573 | # "line.col" -> line, as an int |
| 1574 | def index2line(index): |
| 1575 | return int(float(index)) |
| 1576 | |
| 1577 | # Look at the leading whitespace in s. |
| 1578 | # Return pair (# of leading ws characters, |
| 1579 | # effective # of leading blanks after expanding |
| 1580 | # tabs to width tabwidth) |
| 1581 | |
| 1582 | def classifyws(s, tabwidth): |
| 1583 | raw = effective = 0 |
| 1584 | for ch in s: |
| 1585 | if ch == ' ': |
| 1586 | raw = raw + 1 |
| 1587 | effective = effective + 1 |
| 1588 | elif ch == '\t': |
| 1589 | raw = raw + 1 |
| 1590 | effective = (effective // tabwidth + 1) * tabwidth |
| 1591 | else: |
| 1592 | break |
| 1593 | return raw, effective |
| 1594 | |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1595 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 1596 | class IndentSearcher(object): |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1597 | |
| 1598 | # .run() chews over the Text widget, looking for a block opener |
| 1599 | # and the stmt following it. Returns a pair, |
| 1600 | # (line containing block opener, line containing stmt) |
| 1601 | # Either or both may be None. |
| 1602 | |
| 1603 | def __init__(self, text, tabwidth): |
| 1604 | self.text = text |
| 1605 | self.tabwidth = tabwidth |
| 1606 | self.i = self.finished = 0 |
| 1607 | self.blkopenline = self.indentedline = None |
| 1608 | |
| 1609 | def readline(self): |
| 1610 | if self.finished: |
| 1611 | return "" |
| 1612 | i = self.i = self.i + 1 |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1613 | mark = repr(i) + ".0" |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1614 | if self.text.compare(mark, ">=", "end"): |
| 1615 | return "" |
| 1616 | return self.text.get(mark, mark + " lineend+1c") |
| 1617 | |
| 1618 | def tokeneater(self, type, token, start, end, line, |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 1619 | INDENT=tokenize.INDENT, |
| 1620 | NAME=tokenize.NAME, |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1621 | OPENERS=('class', 'def', 'for', 'if', 'try', 'while')): |
| 1622 | if self.finished: |
| 1623 | pass |
| 1624 | elif type == NAME and token in OPENERS: |
| 1625 | self.blkopenline = line |
| 1626 | elif type == INDENT and self.blkopenline: |
| 1627 | self.indentedline = line |
| 1628 | self.finished = 1 |
| 1629 | |
| 1630 | def run(self): |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 1631 | save_tabsize = tokenize.tabsize |
| 1632 | tokenize.tabsize = self.tabwidth |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1633 | try: |
| 1634 | try: |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 1635 | tokens = tokenize.generate_tokens(self.readline) |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 1636 | for token in tokens: |
| 1637 | self.tokeneater(*token) |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 1638 | except (tokenize.TokenError, SyntaxError): |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1639 | # since we cut off the tokenizer early, we can trigger |
| 1640 | # spurious errors |
| 1641 | pass |
| 1642 | finally: |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 1643 | tokenize.tabsize = save_tabsize |
Kurt B. Kaiser | cb7a383 | 2002-09-14 02:34:23 +0000 | [diff] [blame] | 1644 | return self.blkopenline, self.indentedline |
| 1645 | |
| 1646 | ### end autoindent code ### |
| 1647 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1648 | def prepstr(s): |
| 1649 | # Helper to extract the underscore from a string, e.g. |
| 1650 | # prepstr("Co_py") returns (2, "Copy"). |
Kurt B. Kaiser | 220ecbc | 2002-09-16 02:13:15 +0000 | [diff] [blame] | 1651 | i = s.find('_') |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1652 | if i >= 0: |
| 1653 | s = s[:i] + s[i+1:] |
| 1654 | return i, s |
| 1655 | |
| 1656 | |
| 1657 | keynames = { |
| 1658 | 'bracketleft': '[', |
| 1659 | 'bracketright': ']', |
| 1660 | 'slash': '/', |
| 1661 | } |
| 1662 | |
Kurt B. Kaiser | 610c7e0 | 2004-04-24 03:01:48 +0000 | [diff] [blame] | 1663 | def get_accelerator(keydefs, eventname): |
| 1664 | keylist = keydefs.get(eventname) |
Ned Deily | 7006393 | 2011-01-29 18:29:01 +0000 | [diff] [blame] | 1665 | # issue10940: temporary workaround to prevent hang with OS X Cocoa Tk 8.5 |
| 1666 | # if not keylist: |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1667 | if (not keylist) or (macosx.isCocoaTk() and eventname in { |
Ned Deily | 7006393 | 2011-01-29 18:29:01 +0000 | [diff] [blame] | 1668 | "<<open-module>>", |
| 1669 | "<<goto-line>>", |
| 1670 | "<<change-indentwidth>>"}): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1671 | return "" |
| 1672 | s = keylist[0] |
Kurt B. Kaiser | 220ecbc | 2002-09-16 02:13:15 +0000 | [diff] [blame] | 1673 | s = re.sub(r"-[a-z]\b", lambda m: m.group().upper(), s) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1674 | s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s) |
| 1675 | s = re.sub("Key-", "", s) |
| 1676 | s = re.sub("Cancel","Ctrl-Break",s) # dscherer@cmu.edu |
| 1677 | s = re.sub("Control-", "Ctrl-", s) |
| 1678 | s = re.sub("-", "+", s) |
| 1679 | s = re.sub("><", " ", s) |
| 1680 | s = re.sub("<", "", s) |
| 1681 | s = re.sub(">", "", s) |
| 1682 | return s |
| 1683 | |
| 1684 | |
| 1685 | def fixwordbreaks(root): |
Terry Jan Reedy | 5ff3a16 | 2018-04-30 03:08:01 -0400 | [diff] [blame] | 1686 | # On Windows, tcl/tk breaks 'words' only on spaces, as in Command Prompt. |
| 1687 | # We want Motif style everywhere. See #21474, msg218992 and followup. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1688 | tk = root.tk |
| 1689 | tk.call('tcl_wordBreakAfter', 'a b', 0) # make sure word.tcl is loaded |
Terry Jan Reedy | 5ff3a16 | 2018-04-30 03:08:01 -0400 | [diff] [blame] | 1690 | tk.call('set', 'tcl_wordchars', r'\w') |
| 1691 | tk.call('set', 'tcl_nonwordchars', r'\W') |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1692 | |
| 1693 | |
Terry Jan Reedy | cd56736 | 2014-10-17 01:31:35 -0400 | [diff] [blame] | 1694 | def _editor_window(parent): # htest # |
| 1695 | # error if close master window first - timer event, after script |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 1696 | root = parent |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1697 | fixwordbreaks(root) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1698 | if sys.argv[1:]: |
| 1699 | filename = sys.argv[1] |
| 1700 | else: |
| 1701 | filename = None |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1702 | macosx.setupApp(root, None) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1703 | edit = EditorWindow(root=root, filename=filename) |
Terry Jan Reedy | a2fc99e | 2014-05-25 18:44:05 -0400 | [diff] [blame] | 1704 | edit.text.bind("<<close-all-windows>>", edit.close_event) |
Terry Jan Reedy | cd56736 | 2014-10-17 01:31:35 -0400 | [diff] [blame] | 1705 | # Does not stop error, neither does following |
| 1706 | # edit.text.bind("<<close-window>>", edit.close_event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1707 | |
| 1708 | if __name__ == '__main__': |
Terry Jan Reedy | ee5ef30 | 2018-06-15 18:20:55 -0400 | [diff] [blame] | 1709 | from unittest import main |
| 1710 | main('idlelib.idle_test.test_editor', verbosity=2, exit=False) |
Terry Jan Reedy | 7c15341 | 2016-06-26 17:48:02 -0400 | [diff] [blame] | 1711 | |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 1712 | from idlelib.idle_test.htest import run |
Terry Jan Reedy | 5d46ab1 | 2015-09-20 19:57:13 -0400 | [diff] [blame] | 1713 | run(_editor_window) |