1. Fail gracefully if the file fails to decode when loaded.

2. If the load fails, close the half-built edit window.
3. Don't reopen the file to check the shebang.
4. Clarify that we're setting tabs in Tk.

M    EditorWindow.py
M    FileList.py
This commit is contained in:
Kurt B. Kaiser 2007-09-06 04:03:04 +00:00
parent 3da4c4bcab
commit 105f60ee62
2 changed files with 31 additions and 44 deletions

View File

@ -224,42 +224,32 @@ class EditorWindow(object):
# conceivable file). # conceivable file).
# Making the initial values larger slows things down more often. # Making the initial values larger slows things down more often.
self.num_context_lines = 50, 500, 5000000 self.num_context_lines = 50, 500, 5000000
self.per = per = self.Percolator(text) self.per = per = self.Percolator(text)
if self.ispythonsource(filename): self.color = None
self.color = color = self.ColorDelegator()
per.insertfilter(color)
else:
self.color = None
self.undo = undo = self.UndoDelegator() self.undo = undo = self.UndoDelegator()
per.insertfilter(undo) per.insertfilter(undo)
text.undo_block_start = undo.undo_block_start text.undo_block_start = undo.undo_block_start
text.undo_block_stop = undo.undo_block_stop text.undo_block_stop = undo.undo_block_stop
undo.set_saved_change_hook(self.saved_change_hook) undo.set_saved_change_hook(self.saved_change_hook)
# IOBinding implements file I/O and printing functionality # IOBinding implements file I/O and printing functionality
self.io = io = self.IOBinding(self) self.io = io = self.IOBinding(self)
io.set_filename_change_hook(self.filename_change_hook) io.set_filename_change_hook(self.filename_change_hook)
self.good_load = False
# Create the recent files submenu self.set_indentation_params(False)
self.recent_files_menu = Menu(self.menubar)
self.menudict['file'].insert_cascade(3, label='Recent Files',
underline=0,
menu=self.recent_files_menu)
self.update_recent_files_list()
if filename: if filename:
if os.path.exists(filename) and not os.path.isdir(filename): if os.path.exists(filename) and not os.path.isdir(filename):
io.loadfile(filename) if io.loadfile(filename):
self.good_load = True
is_py_src = self.ispythonsource(filename)
self.set_indentation_params(is_py_src)
if is_py_src:
self.color = color = self.ColorDelegator()
per.insertfilter(color)
else: else:
io.set_filename(filename) io.set_filename(filename)
self.saved_change_hook() self.saved_change_hook()
self.update_recent_files_list()
self.set_indentation_params(self.ispythonsource(filename))
self.load_extensions() self.load_extensions()
menu = self.menudict.get('windows') menu = self.menudict.get('windows')
if menu: if menu:
end = menu.index("end") end = menu.index("end")
@ -337,13 +327,15 @@ class EditorWindow(object):
underline, label = prepstr(label) underline, label = prepstr(label)
menudict[name] = menu = Menu(mbar, name=name) menudict[name] = menu = Menu(mbar, name=name)
mbar.add_cascade(label=label, menu=menu, underline=underline) mbar.add_cascade(label=label, menu=menu, underline=underline)
if sys.platform == 'darwin' and '.framework' in sys.executable: if sys.platform == 'darwin' and '.framework' in sys.executable:
# Insert the application menu # Insert the application menu
menudict['application'] = menu = Menu(mbar, name='apple') menudict['application'] = menu = Menu(mbar, name='apple')
mbar.add_cascade(label='IDLE', menu=menu) mbar.add_cascade(label='IDLE', menu=menu)
self.fill_menus() self.fill_menus()
self.recent_files_menu = Menu(self.menubar)
self.menudict['file'].insert_cascade(3, label='Recent Files',
underline=0,
menu=self.recent_files_menu)
self.base_helpmenu_length = self.menudict['help'].index(END) self.base_helpmenu_length = self.menudict['help'].index(END)
self.reset_help_menu_entries() self.reset_help_menu_entries()
@ -489,7 +481,7 @@ class EditorWindow(object):
text.see("insert") text.see("insert")
def open_module(self, event=None): def open_module(self, event=None):
# XXX Shouldn't this be in IOBinding or in FileList? # XXX Shouldn't this be in IOBinding?
try: try:
name = self.text.get("sel.first", "sel.last") name = self.text.get("sel.first", "sel.last")
except TclError: except TclError:
@ -552,13 +544,8 @@ class EditorWindow(object):
base, ext = os.path.splitext(os.path.basename(filename)) base, ext = os.path.splitext(os.path.basename(filename))
if os.path.normcase(ext) in (".py", ".pyw"): if os.path.normcase(ext) in (".py", ".pyw"):
return True return True
try: line = self.text.get('1.0', '1.0 lineend')
f = open(filename) return line.startswith('#!') and 'python' in line
line = f.readline()
f.close()
except IOError:
return False
return line.startswith('#!') and line.find('python') >= 0
def close_hook(self): def close_hook(self):
if self.flist: if self.flist:
@ -1003,15 +990,16 @@ class EditorWindow(object):
# Return the text widget's current view of what a tab stop means # Return the text widget's current view of what a tab stop means
# (equivalent width in spaces). # (equivalent width in spaces).
def get_tabwidth(self): def get_tk_tabwidth(self):
current = self.text['tabs'] or TK_TABWIDTH_DEFAULT current = self.text['tabs'] or TK_TABWIDTH_DEFAULT
return int(current) return int(current)
# Set the text widget's current view of what a tab stop means. # Set the text widget's current view of what a tab stop means.
def set_tabwidth(self, newtabwidth): def set_tk_tabwidth(self, newtabwidth):
text = self.text text = self.text
if self.get_tabwidth() != newtabwidth: if self.get_tk_tabwidth() != newtabwidth:
# Set text widget tab width
pixels = text.tk.call("font", "measure", text["font"], pixels = text.tk.call("font", "measure", text["font"],
"-displayof", text.master, "-displayof", text.master,
"n" * newtabwidth) "n" * newtabwidth)
@ -1019,20 +1007,14 @@ class EditorWindow(object):
### begin autoindent code ### (configuration was moved to beginning of class) ### begin autoindent code ### (configuration was moved to beginning of class)
# If ispythonsource and guess are true, guess a good value for def set_indentation_params(self, is_py_src, guess=True):
# indentwidth based on file content (if possible), and if if is_py_src and guess:
# indentwidth != tabwidth set usetabs false.
# In any case, adjust the Text widget's view of what a tab
# character means.
def set_indentation_params(self, ispythonsource, guess=True):
if guess and ispythonsource:
i = self.guess_indent() i = self.guess_indent()
if 2 <= i <= 8: if 2 <= i <= 8:
self.indentwidth = i self.indentwidth = i
if self.indentwidth != self.tabwidth: if self.indentwidth != self.tabwidth:
self.usetabs = False self.usetabs = False
self.set_tabwidth(self.tabwidth) self.set_tk_tabwidth(self.tabwidth)
def smart_backspace_event(self, event): def smart_backspace_event(self, event):
text = self.text text = self.text

View File

@ -33,7 +33,12 @@ class FileList:
# Don't create window, perform 'action', e.g. open in same window # Don't create window, perform 'action', e.g. open in same window
return action(filename) return action(filename)
else: else:
return self.EditorWindow(self, filename, key) edit = self.EditorWindow(self, filename, key)
if edit.good_load:
return edit
else:
edit._close()
return None
def gotofileline(self, filename, lineno=None): def gotofileline(self, filename, lineno=None):
edit = self.open(filename) edit = self.open(filename)