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