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