2000-08-14 22:13:23 -03:00
|
|
|
import os
|
|
|
|
from Tkinter import *
|
|
|
|
import tkMessageBox
|
|
|
|
|
|
|
|
|
|
|
|
class FileList:
|
|
|
|
|
2004-08-22 02:14:32 -03:00
|
|
|
from EditorWindow import EditorWindow # class variable, may be overridden
|
|
|
|
# e.g. by PyShellFileList
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def __init__(self, root):
|
|
|
|
self.root = root
|
|
|
|
self.dict = {}
|
|
|
|
self.inversedict = {}
|
|
|
|
self.vars = {} # For EditorWindow.getrawvar (shared Tcl variables)
|
|
|
|
|
|
|
|
def open(self, filename, action=None):
|
|
|
|
assert filename
|
|
|
|
filename = self.canonize(filename)
|
|
|
|
if os.path.isdir(filename):
|
2004-08-22 02:14:32 -03:00
|
|
|
# This can happen when bad filename is passed on command line:
|
2000-08-14 22:13:23 -03:00
|
|
|
tkMessageBox.showerror(
|
2004-08-22 02:14:32 -03:00
|
|
|
"File Error",
|
|
|
|
"%r is a directory." % (filename,),
|
2000-08-14 22:13:23 -03:00
|
|
|
master=self.root)
|
|
|
|
return None
|
|
|
|
key = os.path.normcase(filename)
|
2006-08-22 12:45:46 -03:00
|
|
|
if key in self.dict:
|
2000-08-14 22:13:23 -03:00
|
|
|
edit = self.dict[key]
|
2004-08-22 02:14:32 -03:00
|
|
|
edit.top.wakeup()
|
2000-08-14 22:13:23 -03:00
|
|
|
return edit
|
2004-08-22 02:14:32 -03:00
|
|
|
if action:
|
|
|
|
# Don't create window, perform 'action', e.g. open in same window
|
2000-08-14 22:13:23 -03:00
|
|
|
return action(filename)
|
2004-08-22 02:14:32 -03:00
|
|
|
else:
|
|
|
|
return self.EditorWindow(self, filename, key)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2001-07-14 01:45:32 -03:00
|
|
|
def gotofileline(self, filename, lineno=None):
|
|
|
|
edit = self.open(filename)
|
|
|
|
if edit is not None and lineno is not None:
|
|
|
|
edit.gotoline(lineno)
|
|
|
|
|
2003-06-04 23:34:04 -03:00
|
|
|
def new(self, filename=None):
|
|
|
|
return self.EditorWindow(self, filename)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def close_all_callback(self, event):
|
|
|
|
for edit in self.inversedict.keys():
|
|
|
|
reply = edit.close()
|
|
|
|
if reply == "cancel":
|
|
|
|
break
|
|
|
|
return "break"
|
|
|
|
|
|
|
|
def close_edit(self, edit):
|
|
|
|
try:
|
|
|
|
key = self.inversedict[edit]
|
|
|
|
except KeyError:
|
2007-02-09 01:37:30 -04:00
|
|
|
print("Don't know this EditorWindow object. (close)")
|
2000-08-14 22:13:23 -03:00
|
|
|
return
|
|
|
|
if key:
|
|
|
|
del self.dict[key]
|
|
|
|
del self.inversedict[edit]
|
|
|
|
if not self.inversedict:
|
|
|
|
self.root.quit()
|
|
|
|
|
|
|
|
def filename_changed_edit(self, edit):
|
|
|
|
edit.saved_change_hook()
|
|
|
|
try:
|
|
|
|
key = self.inversedict[edit]
|
|
|
|
except KeyError:
|
2007-02-09 01:37:30 -04:00
|
|
|
print("Don't know this EditorWindow object. (rename)")
|
2000-08-14 22:13:23 -03:00
|
|
|
return
|
|
|
|
filename = edit.io.filename
|
|
|
|
if not filename:
|
|
|
|
if key:
|
|
|
|
del self.dict[key]
|
|
|
|
self.inversedict[edit] = None
|
|
|
|
return
|
|
|
|
filename = self.canonize(filename)
|
|
|
|
newkey = os.path.normcase(filename)
|
|
|
|
if newkey == key:
|
|
|
|
return
|
2006-08-22 12:45:46 -03:00
|
|
|
if newkey in self.dict:
|
2000-08-14 22:13:23 -03:00
|
|
|
conflict = self.dict[newkey]
|
|
|
|
self.inversedict[conflict] = None
|
|
|
|
tkMessageBox.showerror(
|
|
|
|
"Name Conflict",
|
2004-02-12 13:35:32 -04:00
|
|
|
"You now have multiple edit windows open for %r" % (filename,),
|
2000-08-14 22:13:23 -03:00
|
|
|
master=self.root)
|
|
|
|
self.dict[newkey] = edit
|
|
|
|
self.inversedict[edit] = newkey
|
|
|
|
if key:
|
|
|
|
try:
|
|
|
|
del self.dict[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def canonize(self, filename):
|
|
|
|
if not os.path.isabs(filename):
|
|
|
|
try:
|
|
|
|
pwd = os.getcwd()
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
filename = os.path.join(pwd, filename)
|
|
|
|
return os.path.normpath(filename)
|
|
|
|
|
|
|
|
|
2001-07-14 01:45:32 -03:00
|
|
|
def _test():
|
2000-08-14 22:13:23 -03:00
|
|
|
from EditorWindow import fixwordbreaks
|
|
|
|
import sys
|
|
|
|
root = Tk()
|
|
|
|
fixwordbreaks(root)
|
|
|
|
root.withdraw()
|
|
|
|
flist = FileList(root)
|
|
|
|
if sys.argv[1:]:
|
|
|
|
for filename in sys.argv[1:]:
|
|
|
|
flist.open(filename)
|
|
|
|
else:
|
|
|
|
flist.new()
|
|
|
|
if flist.inversedict:
|
|
|
|
root.mainloop()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2001-07-14 01:45:32 -03:00
|
|
|
_test()
|