1 | n/a | import os |
---|
2 | n/a | |
---|
3 | n/a | from tkinter import * |
---|
4 | n/a | import tkinter.messagebox as tkMessageBox |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class FileList: |
---|
8 | n/a | |
---|
9 | n/a | # N.B. this import overridden in PyShellFileList. |
---|
10 | n/a | from idlelib.editor import EditorWindow |
---|
11 | n/a | |
---|
12 | n/a | def __init__(self, root): |
---|
13 | n/a | self.root = root |
---|
14 | n/a | self.dict = {} |
---|
15 | n/a | self.inversedict = {} |
---|
16 | n/a | self.vars = {} # For EditorWindow.getrawvar (shared Tcl variables) |
---|
17 | n/a | |
---|
18 | n/a | def open(self, filename, action=None): |
---|
19 | n/a | assert filename |
---|
20 | n/a | filename = self.canonize(filename) |
---|
21 | n/a | if os.path.isdir(filename): |
---|
22 | n/a | # This can happen when bad filename is passed on command line: |
---|
23 | n/a | tkMessageBox.showerror( |
---|
24 | n/a | "File Error", |
---|
25 | n/a | "%r is a directory." % (filename,), |
---|
26 | n/a | master=self.root) |
---|
27 | n/a | return None |
---|
28 | n/a | key = os.path.normcase(filename) |
---|
29 | n/a | if key in self.dict: |
---|
30 | n/a | edit = self.dict[key] |
---|
31 | n/a | edit.top.wakeup() |
---|
32 | n/a | return edit |
---|
33 | n/a | if action: |
---|
34 | n/a | # Don't create window, perform 'action', e.g. open in same window |
---|
35 | n/a | return action(filename) |
---|
36 | n/a | else: |
---|
37 | n/a | edit = self.EditorWindow(self, filename, key) |
---|
38 | n/a | if edit.good_load: |
---|
39 | n/a | return edit |
---|
40 | n/a | else: |
---|
41 | n/a | edit._close() |
---|
42 | n/a | return None |
---|
43 | n/a | |
---|
44 | n/a | def gotofileline(self, filename, lineno=None): |
---|
45 | n/a | edit = self.open(filename) |
---|
46 | n/a | if edit is not None and lineno is not None: |
---|
47 | n/a | edit.gotoline(lineno) |
---|
48 | n/a | |
---|
49 | n/a | def new(self, filename=None): |
---|
50 | n/a | return self.EditorWindow(self, filename) |
---|
51 | n/a | |
---|
52 | n/a | def close_all_callback(self, *args, **kwds): |
---|
53 | n/a | for edit in list(self.inversedict): |
---|
54 | n/a | reply = edit.close() |
---|
55 | n/a | if reply == "cancel": |
---|
56 | n/a | break |
---|
57 | n/a | return "break" |
---|
58 | n/a | |
---|
59 | n/a | def unregister_maybe_terminate(self, edit): |
---|
60 | n/a | try: |
---|
61 | n/a | key = self.inversedict[edit] |
---|
62 | n/a | except KeyError: |
---|
63 | n/a | print("Don't know this EditorWindow object. (close)") |
---|
64 | n/a | return |
---|
65 | n/a | if key: |
---|
66 | n/a | del self.dict[key] |
---|
67 | n/a | del self.inversedict[edit] |
---|
68 | n/a | if not self.inversedict: |
---|
69 | n/a | self.root.quit() |
---|
70 | n/a | |
---|
71 | n/a | def filename_changed_edit(self, edit): |
---|
72 | n/a | edit.saved_change_hook() |
---|
73 | n/a | try: |
---|
74 | n/a | key = self.inversedict[edit] |
---|
75 | n/a | except KeyError: |
---|
76 | n/a | print("Don't know this EditorWindow object. (rename)") |
---|
77 | n/a | return |
---|
78 | n/a | filename = edit.io.filename |
---|
79 | n/a | if not filename: |
---|
80 | n/a | if key: |
---|
81 | n/a | del self.dict[key] |
---|
82 | n/a | self.inversedict[edit] = None |
---|
83 | n/a | return |
---|
84 | n/a | filename = self.canonize(filename) |
---|
85 | n/a | newkey = os.path.normcase(filename) |
---|
86 | n/a | if newkey == key: |
---|
87 | n/a | return |
---|
88 | n/a | if newkey in self.dict: |
---|
89 | n/a | conflict = self.dict[newkey] |
---|
90 | n/a | self.inversedict[conflict] = None |
---|
91 | n/a | tkMessageBox.showerror( |
---|
92 | n/a | "Name Conflict", |
---|
93 | n/a | "You now have multiple edit windows open for %r" % (filename,), |
---|
94 | n/a | master=self.root) |
---|
95 | n/a | self.dict[newkey] = edit |
---|
96 | n/a | self.inversedict[edit] = newkey |
---|
97 | n/a | if key: |
---|
98 | n/a | try: |
---|
99 | n/a | del self.dict[key] |
---|
100 | n/a | except KeyError: |
---|
101 | n/a | pass |
---|
102 | n/a | |
---|
103 | n/a | def canonize(self, filename): |
---|
104 | n/a | if not os.path.isabs(filename): |
---|
105 | n/a | try: |
---|
106 | n/a | pwd = os.getcwd() |
---|
107 | n/a | except OSError: |
---|
108 | n/a | pass |
---|
109 | n/a | else: |
---|
110 | n/a | filename = os.path.join(pwd, filename) |
---|
111 | n/a | return os.path.normpath(filename) |
---|
112 | n/a | |
---|
113 | n/a | |
---|
114 | n/a | def _test(): |
---|
115 | n/a | from idlelib.editor import fixwordbreaks |
---|
116 | n/a | import sys |
---|
117 | n/a | root = Tk() |
---|
118 | n/a | fixwordbreaks(root) |
---|
119 | n/a | root.withdraw() |
---|
120 | n/a | flist = FileList(root) |
---|
121 | n/a | if sys.argv[1:]: |
---|
122 | n/a | for filename in sys.argv[1:]: |
---|
123 | n/a | flist.open(filename) |
---|
124 | n/a | else: |
---|
125 | n/a | flist.new() |
---|
126 | n/a | if flist.inversedict: |
---|
127 | n/a | root.mainloop() |
---|
128 | n/a | |
---|
129 | n/a | if __name__ == '__main__': |
---|
130 | n/a | _test() |
---|