mirror of
https://github.com/python/cpython.git
synced 2024-11-29 04:44:13 +08:00
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:
parent
3da4c4bcab
commit
105f60ee62
@ -224,42 +224,32 @@ class EditorWindow(object):
|
||||
# conceivable file).
|
||||
# Making the initial values larger slows things down more often.
|
||||
self.num_context_lines = 50, 500, 5000000
|
||||
|
||||
self.per = per = self.Percolator(text)
|
||||
if self.ispythonsource(filename):
|
||||
self.color = color = self.ColorDelegator()
|
||||
per.insertfilter(color)
|
||||
else:
|
||||
self.color = None
|
||||
|
||||
self.color = None
|
||||
self.undo = undo = self.UndoDelegator()
|
||||
per.insertfilter(undo)
|
||||
text.undo_block_start = undo.undo_block_start
|
||||
text.undo_block_stop = undo.undo_block_stop
|
||||
undo.set_saved_change_hook(self.saved_change_hook)
|
||||
|
||||
# IOBinding implements file I/O and printing functionality
|
||||
self.io = io = self.IOBinding(self)
|
||||
io.set_filename_change_hook(self.filename_change_hook)
|
||||
|
||||
# Create the recent files submenu
|
||||
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()
|
||||
|
||||
self.good_load = False
|
||||
self.set_indentation_params(False)
|
||||
if 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:
|
||||
io.set_filename(filename)
|
||||
self.saved_change_hook()
|
||||
|
||||
self.set_indentation_params(self.ispythonsource(filename))
|
||||
|
||||
self.update_recent_files_list()
|
||||
self.load_extensions()
|
||||
|
||||
menu = self.menudict.get('windows')
|
||||
if menu:
|
||||
end = menu.index("end")
|
||||
@ -337,13 +327,15 @@ class EditorWindow(object):
|
||||
underline, label = prepstr(label)
|
||||
menudict[name] = menu = Menu(mbar, name=name)
|
||||
mbar.add_cascade(label=label, menu=menu, underline=underline)
|
||||
|
||||
if sys.platform == 'darwin' and '.framework' in sys.executable:
|
||||
# Insert the application menu
|
||||
menudict['application'] = menu = Menu(mbar, name='apple')
|
||||
mbar.add_cascade(label='IDLE', menu=menu)
|
||||
|
||||
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.reset_help_menu_entries()
|
||||
|
||||
@ -489,7 +481,7 @@ class EditorWindow(object):
|
||||
text.see("insert")
|
||||
|
||||
def open_module(self, event=None):
|
||||
# XXX Shouldn't this be in IOBinding or in FileList?
|
||||
# XXX Shouldn't this be in IOBinding?
|
||||
try:
|
||||
name = self.text.get("sel.first", "sel.last")
|
||||
except TclError:
|
||||
@ -552,13 +544,8 @@ class EditorWindow(object):
|
||||
base, ext = os.path.splitext(os.path.basename(filename))
|
||||
if os.path.normcase(ext) in (".py", ".pyw"):
|
||||
return True
|
||||
try:
|
||||
f = open(filename)
|
||||
line = f.readline()
|
||||
f.close()
|
||||
except IOError:
|
||||
return False
|
||||
return line.startswith('#!') and line.find('python') >= 0
|
||||
line = self.text.get('1.0', '1.0 lineend')
|
||||
return line.startswith('#!') and 'python' in line
|
||||
|
||||
def close_hook(self):
|
||||
if self.flist:
|
||||
@ -1003,15 +990,16 @@ class EditorWindow(object):
|
||||
# Return the text widget's current view of what a tab stop means
|
||||
# (equivalent width in spaces).
|
||||
|
||||
def get_tabwidth(self):
|
||||
def get_tk_tabwidth(self):
|
||||
current = self.text['tabs'] or TK_TABWIDTH_DEFAULT
|
||||
return int(current)
|
||||
|
||||
# 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
|
||||
if self.get_tabwidth() != newtabwidth:
|
||||
if self.get_tk_tabwidth() != newtabwidth:
|
||||
# Set text widget tab width
|
||||
pixels = text.tk.call("font", "measure", text["font"],
|
||||
"-displayof", text.master,
|
||||
"n" * newtabwidth)
|
||||
@ -1019,20 +1007,14 @@ class EditorWindow(object):
|
||||
|
||||
### begin autoindent code ### (configuration was moved to beginning of class)
|
||||
|
||||
# If ispythonsource and guess are true, guess a good value for
|
||||
# indentwidth based on file content (if possible), and if
|
||||
# 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:
|
||||
def set_indentation_params(self, is_py_src, guess=True):
|
||||
if is_py_src and guess:
|
||||
i = self.guess_indent()
|
||||
if 2 <= i <= 8:
|
||||
self.indentwidth = i
|
||||
if self.indentwidth != self.tabwidth:
|
||||
self.usetabs = False
|
||||
self.set_tabwidth(self.tabwidth)
|
||||
self.set_tk_tabwidth(self.tabwidth)
|
||||
|
||||
def smart_backspace_event(self, event):
|
||||
text = self.text
|
||||
|
@ -33,7 +33,12 @@ class FileList:
|
||||
# Don't create window, perform 'action', e.g. open in same window
|
||||
return action(filename)
|
||||
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):
|
||||
edit = self.open(filename)
|
||||
|
Loading…
Reference in New Issue
Block a user