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