summaryrefslogtreecommitdiffstats
path: root/Tools/idle/FileList.py
blob: bebf74271ebf111bb2cdec110ac4711340726543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
from Tkinter import *
import tkMessageBox

from EditorWindow import EditorWindow, fixwordbreaks
from IOBinding import IOBinding


class MultiIOBinding(IOBinding):

    def open(self, event):
        filename = self.askopenfile()
        if filename:
            self.flist.open(filename, self.edit)
        return "break"


class MultiEditorWindow(EditorWindow):

    IOBinding = MultiIOBinding
    
    # Override menu bar specs
    menu_specs = EditorWindow.menu_specs[:]
    menu_specs.insert(len(menu_specs)-1, ("windows", "_Windows"))

    def __init__(self, flist, filename, key):
        self.flist = flist
        flist.inversedict[self] = key
        if key:
            flist.dict[key] = self
        EditorWindow.__init__(self, flist.root, filename)
        self.io.flist = flist
        self.io.edit = self
        self.text.bind("<<open-new-window>>", self.flist.new_callback)
        self.text.bind("<<close-all-windows>>", self.flist.close_all_callback)
        self.text.bind("<<open-class-browser>>", self.open_class_browser)

    def close_hook(self):
        self.flist.close_edit(self)

    def filename_change_hook(self):
        self.flist.filename_changed_edit(self)
        EditorWindow.filename_change_hook(self)
    
    def wakeup(self):
        self.top.tkraise()
        self.top.wm_deiconify()
        self.text.focus_set()

    def createmenubar(self):
        EditorWindow.createmenubar(self)
        self.menudict['windows'].configure(postcommand=self.postwindowsmenu)
    
    def postwindowsmenu(self):
        wmenu = self.menudict['windows']
        wmenu.delete(0, 'end')
        self.fixedwindowsmenu(wmenu)
        files = self.flist.dict.keys()
        files.sort()
        for file in files:
            def openit(self=self, file=file):
                self.flist.open(file)
            wmenu.add_command(label=file, command=openit)
    
    def open_class_browser(self, event=None):
        filename = self.io.filename
        if not filename:
            tkMessageBox.showerror(
                "No filename",
                "This buffer has no associated filename",
                master=self.text)
            return None
        head, tail = os.path.split(filename)
        base, ext = os.path.splitext(tail)
        import pyclbr
        if pyclbr._modules.has_key(base):
            del pyclbr._modules[base]
        import ClassBrowser
        ClassBrowser.ClassBrowser(self.flist, base)


class FileList:
    
    EditorWindow = MultiEditorWindow
    
    def __init__(self, root):
        self.root = root
        self.dict = {}
        self.inversedict = {}

    def new(self):
        return self.open(None)

    def open(self, filename, edit=None):
        if filename:
            filename = self.canonize(filename)
            if os.path.isdir(filename):
                tkMessageBox.showerror(
                    "Is A Directory",
                    "The path %s is a directory." % `filename`,
                    master=self.root)
                return None
            key = os.path.normcase(filename)
            if self.dict.has_key(key):
                edit = self.dict[key]
                edit.wakeup()
                return edit
            if not os.path.exists(filename):
                tkMessageBox.showinfo(
                    "New File",
                    "Opening non-existent file %s" % `filename`,
                    master=self.root)
            if edit and not edit.io.filename and edit.undo.get_saved():
                # Reuse existing Untitled window for new file
                edit.io.loadfile(filename)
                self.dict[key] = edit
                self.inversedict[edit] = key
                edit.wakeup()
                return edit
        else:
            key = None
        edit = self.EditorWindow(self, filename, key)
        return edit

    def new_callback(self, event):
        self.new()
        return "break"

    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:
            print "Don't know this EditorWindow object.  (close)"
            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:
            print "Don't know this EditorWindow object.  (rename)"
            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
        if self.dict.has_key(newkey):
            conflict = self.dict[newkey]
            self.inversedict[conflict] = None
            tkMessageBox.showerror(
                "Name Conflict",
                "You now have multiple edit windows open for %s" % `filename`,
                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)


def test():
    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__':
    test()