summaryrefslogtreecommitdiffstats
path: root/Tools/idle/EditorWindow.py
blob: fc8a8f1e4f19888c3106615099a0ec90513e0a7c (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import sys
import os
import string
import imp
from Tkinter import *
import tkSimpleDialog
import tkMessageBox

about_title = "About IDLE"
about_text = """\
IDLE 0.1

A not totally unintegrated development environment for Python

by Guido van Rossum
"""

class EditorWindow:

    from Percolator import Percolator
    from ColorDelegator import ColorDelegator
    from UndoDelegator import UndoDelegator
    from IOBinding import IOBinding
    from SearchBinding import SearchBinding
    from AutoIndent import AutoIndent
    from AutoExpand import AutoExpand
    import Bindings
    
    about_title = about_title
    about_text = about_text    

    def __init__(self, root, filename=None):
        self.root = root
        self.menubar = Menu(root)
        self.top = top = Toplevel(root, menu=self.menubar)
        self.vbar = vbar = Scrollbar(top, name='vbar')
        self.text = text = Text(top, name='text')

        self.createmenubar()
        self.Bindings.apply_bindings(text)

        self.top.protocol("WM_DELETE_WINDOW", self.close)
        self.top.bind("<<close-window>>", self.close_event)
        self.text.bind("<<center-insert>>", self.center_insert_event)
        self.text.bind("<<help>>", self.help_dialog)
        self.text.bind("<<about-idle>>", self.about_dialog)
        self.text.bind("<<open-module>>", self.open_module)
        self.text.bind("<<do-nothing>>", lambda event: "break")

        vbar['command'] = text.yview
        vbar.pack(side=RIGHT, fill=Y)

        text['yscrollcommand'] = vbar.set
        text['background'] = 'white'
        if sys.platform[:3] == 'win':
            text['font'] = ("lucida console", 8)
        text.pack(side=LEFT, fill=BOTH, expand=1)
        text.focus_set()

        self.auto = auto = self.AutoIndent(text)
        self.autoex = self.AutoExpand(text)
        self.per = per = self.Percolator(text)
        if self.ispythonsource(filename):
            self.color = color = self.ColorDelegator(); per.insertfilter(color)
            ##print "Initial colorizer"
        else:
            ##print "No initial colorizer"
            self.color = None
        self.undo = undo = self.UndoDelegator(); per.insertfilter(undo)
        self.search = search = self.SearchBinding(undo)
        self.io = io = self.IOBinding(undo)

        undo.set_saved_change_hook(self.saved_change_hook)
        io.set_filename_change_hook(self.filename_change_hook)

        if filename:
            if os.path.exists(filename):
                io.loadfile(filename)
            else:
                io.set_filename(filename)

        self.saved_change_hook()

    menu_specs = [
        ("file", "File"),
        ("edit", "Edit"),
        ("help", "Help"),
    ]

    def createmenubar(self):
        mbar = self.menubar
        self.menudict = mdict = {}
        for name, label in self.menu_specs:
            mdict[name] = menu = Menu(mbar, name=name)
            mbar.add_cascade(label=label, menu=menu)
        self.Bindings.fill_menus(self.text, mdict)

    def about_dialog(self, event=None):
        tkMessageBox.showinfo(self.about_title, self.about_text,
                              master=self.text)

    def help_dialog(self, event=None):
        from HelpWindow import HelpWindow
        HelpWindow(root=self.root)
    
    def open_module(self, event=None):
        try:
            name = self.text.get("sel.first", "sel.last")
        except TclError:
            name = ""
        else:
            name = string.strip(name)
        if not name:
            name = tkSimpleDialog.askstring("Module",
                     "Enter the name of a Python module\n"
                     "to search on sys.path and open:",
                     parent=self.text)
            if name:
                name = string.strip(name)
            if not name:
                return
        try:
            (f, file, (suffix, mode, type)) = imp.find_module(name)
        except ImportError, msg:
            tkMessageBox.showerror("Import error", str(msg), parent=self.text)
            return
        if type != imp.PY_SOURCE:
            tkMessageBox.showerror("Unsupported type",
                "%s is not a source module" % name, parent=self.text)
            return
        if f:
            f.close()
        self.flist.open(file, self)

    def gotoline(self, lineno):
        if lineno is not None and lineno > 0:
            self.text.mark_set("insert", "%d.0" % lineno)
            self.text.tag_remove("sel", "1.0", "end")
            self.text.tag_add("sel", "insert", "insert +1l")
            self.center()

    def ispythonsource(self, filename):
        if not filename:
            return 1
        if os.path.normcase(filename[-3:]) == ".py":
            return 1
        try:
            f = open(filename)
            line = f.readline()
            f.close()
        except IOError:
            return 0
        return line[:2] == '#!' and string.find(line, 'python') >= 0

    close_hook = None

    def set_close_hook(self, close_hook):
        self.close_hook = close_hook

    def filename_change_hook(self):
        self.saved_change_hook()
        if self.ispythonsource(self.io.filename):
            self.addcolorizer()
        else:
            self.rmcolorizer()

    def addcolorizer(self):
        if self.color:
            return
        ##print "Add colorizer"
        self.per.removefilter(self.undo)
        self.color = self.ColorDelegator()
        self.per.insertfilter(self.color)
        self.per.insertfilter(self.undo)

    def rmcolorizer(self):
        if not self.color:
            return
        ##print "Remove colorizer"
        self.per.removefilter(self.undo)
        self.per.removefilter(self.color)
        self.color = None
        self.per.insertfilter(self.undo)

    def saved_change_hook(self):
        if self.io.filename:
            title = self.io.filename
        else:
            title = "(Untitled)"
        if not self.undo.get_saved():
            title = title + " *"
        self.top.wm_title(title)

    def center_insert_event(self, event):
        self.center()

    def center(self, mark="insert"):
        insert = float(self.text.index(mark + " linestart"))
        end = float(self.text.index("end"))
        if insert > end-insert:
            self.text.see("1.0")
        else:
            self.text.see("end")
        self.text.see(mark)

    def close_event(self, event):
        self.close()

    def close(self):
        self.top.wm_deiconify()
        self.top.tkraise()
        reply = self.io.maybesave()
        if reply != "cancel":
            if self.color and self.color.colorizing:
                self.color.close()
                self.top.bell()
                return "cancel"
            if self.close_hook:
                self.close_hook()
            if self.color:
                self.color.close()          # Cancel colorization
            self.top.destroy()
        return reply


def fixwordbreaks(root):
    tk = root.tk
    tk.call('tcl_wordBreakAfter', 'a b', 0) # make sure word.tcl is loaded
    tk.call('set', 'tcl_wordchars', '[a-zA-Z0-9_]')
    tk.call('set', 'tcl_nonwordchars', '[^a-zA-Z0-9_]')


def test():
    root = Tk()
    fixwordbreaks(root)
    root.withdraw()
    if sys.argv[1:]:
        filename = sys.argv[1]
    else:
        filename = None
    edit = EditorWindow(root, filename)
    edit.set_close_hook(root.quit)
    root.mainloop()
    root.destroy()

if __name__ == '__main__':
    test()