Added ignore file.
[rox-edit.git] / buffer.py
blobf974b19867ee020cbf1c4b3578439c3095c3723a
1 import rox
2 from rox import g
4 DELETE = 1
5 INSERT = 2
7 class Buffer(g.TextBuffer):
8 "A buffer that deals with undo/redo."
10 def __init__(self):
11 g.TextBuffer.__init__(self, None)
12 self.undo_buffer = []
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,
21 start.get_offset(),
22 buffer.get_slice(start, end, g.TRUE)))
23 if self.redo_buffer:
24 self.redo_buffer = []
26 def insert_text(self, buffer, where, text, len):
27 start = where.get_offset()
28 self.undo_buffer.append((DELETE, start, start + len, text))
29 if self.redo_buffer:
30 self.redo_buffer = []
32 def do_action(self, op):
33 if op[0] == DELETE:
34 start = self.get_iter_at_offset(op[1])
35 end = self.get_iter_at_offset(op[2])
36 self.delete(start, end)
37 elif op[0] == INSERT:
38 start = self.get_iter_at_offset(op[1])
39 self.insert(start, op[2], -1)
40 else:
41 rox.alert('Unknown entry in undo buffer!\n' + `op`)
43 def undo(self):
44 if not self.undo_buffer:
45 g.gdk.beep()
46 return
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
53 try:
54 self.do_action(op)
55 finally:
56 self.redo_buffer = self.undo_buffer
57 self.undo_buffer = old_undo
59 def redo(self):
60 if not self.redo_buffer:
61 g.gdk.beep()
62 return
63 op = self.redo_buffer.pop()
65 old_redo = self.redo_buffer
66 self.redo_buffer = None # Don't clear redo buffer
68 try:
69 self.do_action(op)
70 finally:
71 self.redo_buffer = old_redo