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