1 | n/a | import string |
---|
2 | n/a | |
---|
3 | n/a | from idlelib.delegator import Delegator |
---|
4 | n/a | |
---|
5 | n/a | # tkintter import not needed because module does not create widgets, |
---|
6 | n/a | # although many methods operate on text widget arguments. |
---|
7 | n/a | |
---|
8 | n/a | #$ event <<redo>> |
---|
9 | n/a | #$ win <Control-y> |
---|
10 | n/a | #$ unix <Alt-z> |
---|
11 | n/a | |
---|
12 | n/a | #$ event <<undo>> |
---|
13 | n/a | #$ win <Control-z> |
---|
14 | n/a | #$ unix <Control-z> |
---|
15 | n/a | |
---|
16 | n/a | #$ event <<dump-undo-state>> |
---|
17 | n/a | #$ win <Control-backslash> |
---|
18 | n/a | #$ unix <Control-backslash> |
---|
19 | n/a | |
---|
20 | n/a | |
---|
21 | n/a | class UndoDelegator(Delegator): |
---|
22 | n/a | |
---|
23 | n/a | max_undo = 1000 |
---|
24 | n/a | |
---|
25 | n/a | def __init__(self): |
---|
26 | n/a | Delegator.__init__(self) |
---|
27 | n/a | self.reset_undo() |
---|
28 | n/a | |
---|
29 | n/a | def setdelegate(self, delegate): |
---|
30 | n/a | if self.delegate is not None: |
---|
31 | n/a | self.unbind("<<undo>>") |
---|
32 | n/a | self.unbind("<<redo>>") |
---|
33 | n/a | self.unbind("<<dump-undo-state>>") |
---|
34 | n/a | Delegator.setdelegate(self, delegate) |
---|
35 | n/a | if delegate is not None: |
---|
36 | n/a | self.bind("<<undo>>", self.undo_event) |
---|
37 | n/a | self.bind("<<redo>>", self.redo_event) |
---|
38 | n/a | self.bind("<<dump-undo-state>>", self.dump_event) |
---|
39 | n/a | |
---|
40 | n/a | def dump_event(self, event): |
---|
41 | n/a | from pprint import pprint |
---|
42 | n/a | pprint(self.undolist[:self.pointer]) |
---|
43 | n/a | print("pointer:", self.pointer, end=' ') |
---|
44 | n/a | print("saved:", self.saved, end=' ') |
---|
45 | n/a | print("can_merge:", self.can_merge, end=' ') |
---|
46 | n/a | print("get_saved():", self.get_saved()) |
---|
47 | n/a | pprint(self.undolist[self.pointer:]) |
---|
48 | n/a | return "break" |
---|
49 | n/a | |
---|
50 | n/a | def reset_undo(self): |
---|
51 | n/a | self.was_saved = -1 |
---|
52 | n/a | self.pointer = 0 |
---|
53 | n/a | self.undolist = [] |
---|
54 | n/a | self.undoblock = 0 # or a CommandSequence instance |
---|
55 | n/a | self.set_saved(1) |
---|
56 | n/a | |
---|
57 | n/a | def set_saved(self, flag): |
---|
58 | n/a | if flag: |
---|
59 | n/a | self.saved = self.pointer |
---|
60 | n/a | else: |
---|
61 | n/a | self.saved = -1 |
---|
62 | n/a | self.can_merge = False |
---|
63 | n/a | self.check_saved() |
---|
64 | n/a | |
---|
65 | n/a | def get_saved(self): |
---|
66 | n/a | return self.saved == self.pointer |
---|
67 | n/a | |
---|
68 | n/a | saved_change_hook = None |
---|
69 | n/a | |
---|
70 | n/a | def set_saved_change_hook(self, hook): |
---|
71 | n/a | self.saved_change_hook = hook |
---|
72 | n/a | |
---|
73 | n/a | was_saved = -1 |
---|
74 | n/a | |
---|
75 | n/a | def check_saved(self): |
---|
76 | n/a | is_saved = self.get_saved() |
---|
77 | n/a | if is_saved != self.was_saved: |
---|
78 | n/a | self.was_saved = is_saved |
---|
79 | n/a | if self.saved_change_hook: |
---|
80 | n/a | self.saved_change_hook() |
---|
81 | n/a | |
---|
82 | n/a | def insert(self, index, chars, tags=None): |
---|
83 | n/a | self.addcmd(InsertCommand(index, chars, tags)) |
---|
84 | n/a | |
---|
85 | n/a | def delete(self, index1, index2=None): |
---|
86 | n/a | self.addcmd(DeleteCommand(index1, index2)) |
---|
87 | n/a | |
---|
88 | n/a | # Clients should call undo_block_start() and undo_block_stop() |
---|
89 | n/a | # around a sequence of editing cmds to be treated as a unit by |
---|
90 | n/a | # undo & redo. Nested matching calls are OK, and the inner calls |
---|
91 | n/a | # then act like nops. OK too if no editing cmds, or only one |
---|
92 | n/a | # editing cmd, is issued in between: if no cmds, the whole |
---|
93 | n/a | # sequence has no effect; and if only one cmd, that cmd is entered |
---|
94 | n/a | # directly into the undo list, as if undo_block_xxx hadn't been |
---|
95 | n/a | # called. The intent of all that is to make this scheme easy |
---|
96 | n/a | # to use: all the client has to worry about is making sure each |
---|
97 | n/a | # _start() call is matched by a _stop() call. |
---|
98 | n/a | |
---|
99 | n/a | def undo_block_start(self): |
---|
100 | n/a | if self.undoblock == 0: |
---|
101 | n/a | self.undoblock = CommandSequence() |
---|
102 | n/a | self.undoblock.bump_depth() |
---|
103 | n/a | |
---|
104 | n/a | def undo_block_stop(self): |
---|
105 | n/a | if self.undoblock.bump_depth(-1) == 0: |
---|
106 | n/a | cmd = self.undoblock |
---|
107 | n/a | self.undoblock = 0 |
---|
108 | n/a | if len(cmd) > 0: |
---|
109 | n/a | if len(cmd) == 1: |
---|
110 | n/a | # no need to wrap a single cmd |
---|
111 | n/a | cmd = cmd.getcmd(0) |
---|
112 | n/a | # this blk of cmds, or single cmd, has already |
---|
113 | n/a | # been done, so don't execute it again |
---|
114 | n/a | self.addcmd(cmd, 0) |
---|
115 | n/a | |
---|
116 | n/a | def addcmd(self, cmd, execute=True): |
---|
117 | n/a | if execute: |
---|
118 | n/a | cmd.do(self.delegate) |
---|
119 | n/a | if self.undoblock != 0: |
---|
120 | n/a | self.undoblock.append(cmd) |
---|
121 | n/a | return |
---|
122 | n/a | if self.can_merge and self.pointer > 0: |
---|
123 | n/a | lastcmd = self.undolist[self.pointer-1] |
---|
124 | n/a | if lastcmd.merge(cmd): |
---|
125 | n/a | return |
---|
126 | n/a | self.undolist[self.pointer:] = [cmd] |
---|
127 | n/a | if self.saved > self.pointer: |
---|
128 | n/a | self.saved = -1 |
---|
129 | n/a | self.pointer = self.pointer + 1 |
---|
130 | n/a | if len(self.undolist) > self.max_undo: |
---|
131 | n/a | ##print "truncating undo list" |
---|
132 | n/a | del self.undolist[0] |
---|
133 | n/a | self.pointer = self.pointer - 1 |
---|
134 | n/a | if self.saved >= 0: |
---|
135 | n/a | self.saved = self.saved - 1 |
---|
136 | n/a | self.can_merge = True |
---|
137 | n/a | self.check_saved() |
---|
138 | n/a | |
---|
139 | n/a | def undo_event(self, event): |
---|
140 | n/a | if self.pointer == 0: |
---|
141 | n/a | self.bell() |
---|
142 | n/a | return "break" |
---|
143 | n/a | cmd = self.undolist[self.pointer - 1] |
---|
144 | n/a | cmd.undo(self.delegate) |
---|
145 | n/a | self.pointer = self.pointer - 1 |
---|
146 | n/a | self.can_merge = False |
---|
147 | n/a | self.check_saved() |
---|
148 | n/a | return "break" |
---|
149 | n/a | |
---|
150 | n/a | def redo_event(self, event): |
---|
151 | n/a | if self.pointer >= len(self.undolist): |
---|
152 | n/a | self.bell() |
---|
153 | n/a | return "break" |
---|
154 | n/a | cmd = self.undolist[self.pointer] |
---|
155 | n/a | cmd.redo(self.delegate) |
---|
156 | n/a | self.pointer = self.pointer + 1 |
---|
157 | n/a | self.can_merge = False |
---|
158 | n/a | self.check_saved() |
---|
159 | n/a | return "break" |
---|
160 | n/a | |
---|
161 | n/a | |
---|
162 | n/a | class Command: |
---|
163 | n/a | # Base class for Undoable commands |
---|
164 | n/a | |
---|
165 | n/a | tags = None |
---|
166 | n/a | |
---|
167 | n/a | def __init__(self, index1, index2, chars, tags=None): |
---|
168 | n/a | self.marks_before = {} |
---|
169 | n/a | self.marks_after = {} |
---|
170 | n/a | self.index1 = index1 |
---|
171 | n/a | self.index2 = index2 |
---|
172 | n/a | self.chars = chars |
---|
173 | n/a | if tags: |
---|
174 | n/a | self.tags = tags |
---|
175 | n/a | |
---|
176 | n/a | def __repr__(self): |
---|
177 | n/a | s = self.__class__.__name__ |
---|
178 | n/a | t = (self.index1, self.index2, self.chars, self.tags) |
---|
179 | n/a | if self.tags is None: |
---|
180 | n/a | t = t[:-1] |
---|
181 | n/a | return s + repr(t) |
---|
182 | n/a | |
---|
183 | n/a | def do(self, text): |
---|
184 | n/a | pass |
---|
185 | n/a | |
---|
186 | n/a | def redo(self, text): |
---|
187 | n/a | pass |
---|
188 | n/a | |
---|
189 | n/a | def undo(self, text): |
---|
190 | n/a | pass |
---|
191 | n/a | |
---|
192 | n/a | def merge(self, cmd): |
---|
193 | n/a | return 0 |
---|
194 | n/a | |
---|
195 | n/a | def save_marks(self, text): |
---|
196 | n/a | marks = {} |
---|
197 | n/a | for name in text.mark_names(): |
---|
198 | n/a | if name != "insert" and name != "current": |
---|
199 | n/a | marks[name] = text.index(name) |
---|
200 | n/a | return marks |
---|
201 | n/a | |
---|
202 | n/a | def set_marks(self, text, marks): |
---|
203 | n/a | for name, index in marks.items(): |
---|
204 | n/a | text.mark_set(name, index) |
---|
205 | n/a | |
---|
206 | n/a | |
---|
207 | n/a | class InsertCommand(Command): |
---|
208 | n/a | # Undoable insert command |
---|
209 | n/a | |
---|
210 | n/a | def __init__(self, index1, chars, tags=None): |
---|
211 | n/a | Command.__init__(self, index1, None, chars, tags) |
---|
212 | n/a | |
---|
213 | n/a | def do(self, text): |
---|
214 | n/a | self.marks_before = self.save_marks(text) |
---|
215 | n/a | self.index1 = text.index(self.index1) |
---|
216 | n/a | if text.compare(self.index1, ">", "end-1c"): |
---|
217 | n/a | # Insert before the final newline |
---|
218 | n/a | self.index1 = text.index("end-1c") |
---|
219 | n/a | text.insert(self.index1, self.chars, self.tags) |
---|
220 | n/a | self.index2 = text.index("%s+%dc" % (self.index1, len(self.chars))) |
---|
221 | n/a | self.marks_after = self.save_marks(text) |
---|
222 | n/a | ##sys.__stderr__.write("do: %s\n" % self) |
---|
223 | n/a | |
---|
224 | n/a | def redo(self, text): |
---|
225 | n/a | text.mark_set('insert', self.index1) |
---|
226 | n/a | text.insert(self.index1, self.chars, self.tags) |
---|
227 | n/a | self.set_marks(text, self.marks_after) |
---|
228 | n/a | text.see('insert') |
---|
229 | n/a | ##sys.__stderr__.write("redo: %s\n" % self) |
---|
230 | n/a | |
---|
231 | n/a | def undo(self, text): |
---|
232 | n/a | text.mark_set('insert', self.index1) |
---|
233 | n/a | text.delete(self.index1, self.index2) |
---|
234 | n/a | self.set_marks(text, self.marks_before) |
---|
235 | n/a | text.see('insert') |
---|
236 | n/a | ##sys.__stderr__.write("undo: %s\n" % self) |
---|
237 | n/a | |
---|
238 | n/a | def merge(self, cmd): |
---|
239 | n/a | if self.__class__ is not cmd.__class__: |
---|
240 | n/a | return False |
---|
241 | n/a | if self.index2 != cmd.index1: |
---|
242 | n/a | return False |
---|
243 | n/a | if self.tags != cmd.tags: |
---|
244 | n/a | return False |
---|
245 | n/a | if len(cmd.chars) != 1: |
---|
246 | n/a | return False |
---|
247 | n/a | if self.chars and \ |
---|
248 | n/a | self.classify(self.chars[-1]) != self.classify(cmd.chars): |
---|
249 | n/a | return False |
---|
250 | n/a | self.index2 = cmd.index2 |
---|
251 | n/a | self.chars = self.chars + cmd.chars |
---|
252 | n/a | return True |
---|
253 | n/a | |
---|
254 | n/a | alphanumeric = string.ascii_letters + string.digits + "_" |
---|
255 | n/a | |
---|
256 | n/a | def classify(self, c): |
---|
257 | n/a | if c in self.alphanumeric: |
---|
258 | n/a | return "alphanumeric" |
---|
259 | n/a | if c == "\n": |
---|
260 | n/a | return "newline" |
---|
261 | n/a | return "punctuation" |
---|
262 | n/a | |
---|
263 | n/a | |
---|
264 | n/a | class DeleteCommand(Command): |
---|
265 | n/a | # Undoable delete command |
---|
266 | n/a | |
---|
267 | n/a | def __init__(self, index1, index2=None): |
---|
268 | n/a | Command.__init__(self, index1, index2, None, None) |
---|
269 | n/a | |
---|
270 | n/a | def do(self, text): |
---|
271 | n/a | self.marks_before = self.save_marks(text) |
---|
272 | n/a | self.index1 = text.index(self.index1) |
---|
273 | n/a | if self.index2: |
---|
274 | n/a | self.index2 = text.index(self.index2) |
---|
275 | n/a | else: |
---|
276 | n/a | self.index2 = text.index(self.index1 + " +1c") |
---|
277 | n/a | if text.compare(self.index2, ">", "end-1c"): |
---|
278 | n/a | # Don't delete the final newline |
---|
279 | n/a | self.index2 = text.index("end-1c") |
---|
280 | n/a | self.chars = text.get(self.index1, self.index2) |
---|
281 | n/a | text.delete(self.index1, self.index2) |
---|
282 | n/a | self.marks_after = self.save_marks(text) |
---|
283 | n/a | ##sys.__stderr__.write("do: %s\n" % self) |
---|
284 | n/a | |
---|
285 | n/a | def redo(self, text): |
---|
286 | n/a | text.mark_set('insert', self.index1) |
---|
287 | n/a | text.delete(self.index1, self.index2) |
---|
288 | n/a | self.set_marks(text, self.marks_after) |
---|
289 | n/a | text.see('insert') |
---|
290 | n/a | ##sys.__stderr__.write("redo: %s\n" % self) |
---|
291 | n/a | |
---|
292 | n/a | def undo(self, text): |
---|
293 | n/a | text.mark_set('insert', self.index1) |
---|
294 | n/a | text.insert(self.index1, self.chars) |
---|
295 | n/a | self.set_marks(text, self.marks_before) |
---|
296 | n/a | text.see('insert') |
---|
297 | n/a | ##sys.__stderr__.write("undo: %s\n" % self) |
---|
298 | n/a | |
---|
299 | n/a | |
---|
300 | n/a | class CommandSequence(Command): |
---|
301 | n/a | # Wrapper for a sequence of undoable cmds to be undone/redone |
---|
302 | n/a | # as a unit |
---|
303 | n/a | |
---|
304 | n/a | def __init__(self): |
---|
305 | n/a | self.cmds = [] |
---|
306 | n/a | self.depth = 0 |
---|
307 | n/a | |
---|
308 | n/a | def __repr__(self): |
---|
309 | n/a | s = self.__class__.__name__ |
---|
310 | n/a | strs = [] |
---|
311 | n/a | for cmd in self.cmds: |
---|
312 | n/a | strs.append(" %r" % (cmd,)) |
---|
313 | n/a | return s + "(\n" + ",\n".join(strs) + "\n)" |
---|
314 | n/a | |
---|
315 | n/a | def __len__(self): |
---|
316 | n/a | return len(self.cmds) |
---|
317 | n/a | |
---|
318 | n/a | def append(self, cmd): |
---|
319 | n/a | self.cmds.append(cmd) |
---|
320 | n/a | |
---|
321 | n/a | def getcmd(self, i): |
---|
322 | n/a | return self.cmds[i] |
---|
323 | n/a | |
---|
324 | n/a | def redo(self, text): |
---|
325 | n/a | for cmd in self.cmds: |
---|
326 | n/a | cmd.redo(text) |
---|
327 | n/a | |
---|
328 | n/a | def undo(self, text): |
---|
329 | n/a | cmds = self.cmds[:] |
---|
330 | n/a | cmds.reverse() |
---|
331 | n/a | for cmd in cmds: |
---|
332 | n/a | cmd.undo(text) |
---|
333 | n/a | |
---|
334 | n/a | def bump_depth(self, incr=1): |
---|
335 | n/a | self.depth = self.depth + incr |
---|
336 | n/a | return self.depth |
---|
337 | n/a | |
---|
338 | n/a | |
---|
339 | n/a | def _undo_delegator(parent): # htest # |
---|
340 | n/a | from tkinter import Toplevel, Text, Button |
---|
341 | n/a | from idlelib.percolator import Percolator |
---|
342 | n/a | undowin = Toplevel(parent) |
---|
343 | n/a | undowin.title("Test UndoDelegator") |
---|
344 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
---|
345 | n/a | undowin.geometry("+%d+%d" % (x, y + 175)) |
---|
346 | n/a | |
---|
347 | n/a | text = Text(undowin, height=10) |
---|
348 | n/a | text.pack() |
---|
349 | n/a | text.focus_set() |
---|
350 | n/a | p = Percolator(text) |
---|
351 | n/a | d = UndoDelegator() |
---|
352 | n/a | p.insertfilter(d) |
---|
353 | n/a | |
---|
354 | n/a | undo = Button(undowin, text="Undo", command=lambda:d.undo_event(None)) |
---|
355 | n/a | undo.pack(side='left') |
---|
356 | n/a | redo = Button(undowin, text="Redo", command=lambda:d.redo_event(None)) |
---|
357 | n/a | redo.pack(side='left') |
---|
358 | n/a | dump = Button(undowin, text="Dump", command=lambda:d.dump_event(None)) |
---|
359 | n/a | dump.pack(side='left') |
---|
360 | n/a | |
---|
361 | n/a | if __name__ == "__main__": |
---|
362 | n/a | import unittest |
---|
363 | n/a | unittest.main('idlelib.idle_test.test_undo', verbosity=2, exit=False) |
---|
364 | n/a | |
---|
365 | n/a | from idlelib.idle_test.htest import run |
---|
366 | n/a | run(_undo_delegator) |
---|