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])
40 self
.insert(start
, op
[2], -1)
42 rox
.alert('Unknown entry in undo buffer!\n' + `op`
)
45 if not self
.undo_buffer
:
48 op
= self
.undo_buffer
.pop()
50 old_undo
= self
.undo_buffer
51 self
.undo_buffer
= self
.redo_buffer
# Put undos in redo buffer
52 self
.redo_buffer
= None # Don't clear redo buffer
57 self
.redo_buffer
= self
.undo_buffer
58 self
.undo_buffer
= old_undo
61 if not self
.redo_buffer
:
64 op
= self
.redo_buffer
.pop()
66 old_redo
= self
.redo_buffer
67 self
.redo_buffer
= None # Don't clear redo buffer
72 self
.redo_buffer
= old_redo