7 class Buffer(g
.TextBuffer
):
8 "A buffer that deals with undo/redo."
11 g
.TextBuffer
.__init
__(self
, None)
13 self
.redo_buffer
= [] # Will be None during undo or redo
15 def start_undo_history(self
):
16 self
.connect('delete-range', self
.delete_range
)
17 self
.connect('insert-text', self
.insert_text
)
19 def delete_range(self
, buffer, start
, end
):
20 self
.undo_buffer
.append((INSERT
,
22 buffer.get_slice(start
, end
, g
.TRUE
)))
26 def insert_text(self
, buffer, where
, text
, len):
27 text
= text
[:len] # PyGtk bug?
28 start
= where
.get_offset()
29 self
.undo_buffer
.append((DELETE
, start
, start
+ len, text
))
33 def do_action(self
, op
):
35 start
= self
.get_iter_at_offset(op
[1])
36 end
= self
.get_iter_at_offset(op
[2])
37 self
.delete(start
, end
)
39 start
= self
.get_iter_at_offset(op
[1])
41 self
.insert(start
, op
[2])
43 self
.insert(start
, op
[2], -1)
45 rox
.alert('Unknown entry in undo buffer!\n' + `op`
)
48 if not self
.undo_buffer
:
51 op
= self
.undo_buffer
.pop()
53 old_undo
= self
.undo_buffer
54 self
.undo_buffer
= self
.redo_buffer
# Put undos in redo buffer
55 self
.redo_buffer
= None # Don't clear redo buffer
60 self
.redo_buffer
= self
.undo_buffer
61 self
.undo_buffer
= old_undo
64 if not self
.redo_buffer
:
67 op
= self
.redo_buffer
.pop()
69 old_redo
= self
.redo_buffer
70 self
.redo_buffer
= None # Don't clear redo buffer
75 self
.redo_buffer
= old_redo