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 start
= where
.get_offset()
28 self
.undo_buffer
.append((DELETE
, start
, start
+ len, text
))
32 def do_action(self
, op
):
34 start
= self
.get_iter_at_offset(op
[1])
35 end
= self
.get_iter_at_offset(op
[2])
36 self
.delete(start
, end
)
38 start
= self
.get_iter_at_offset(op
[1])
39 self
.insert(start
, op
[2], -1)
41 rox
.alert('Unknown entry in undo buffer!\n' + `op`
)
44 if not self
.undo_buffer
:
47 op
= self
.undo_buffer
.pop()
49 old_undo
= self
.undo_buffer
50 self
.undo_buffer
= self
.redo_buffer
# Put undos in redo buffer
51 self
.redo_buffer
= None # Don't clear redo buffer
56 self
.redo_buffer
= self
.undo_buffer
57 self
.undo_buffer
= old_undo
60 if not self
.redo_buffer
:
63 op
= self
.redo_buffer
.pop()
65 old_redo
= self
.redo_buffer
66 self
.redo_buffer
= None # Don't clear redo buffer
71 self
.redo_buffer
= old_redo