Kiln » TortoiseHg » TortoiseHg
Clone URL:  
textview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# textview.py - TextView/TextBuffer with undo/redo functionality # # Copyright 2009 Florian Heinle # Copyright 2010 Yuki KODAMA <endflow.net@gmail.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import gtk import gobject from tortoisehg.hgtk import gtklib class UndoableInsert(object): """something that has been inserted into our textbuffer""" def __init__(self, text_iter, text, length): self.time = gobject.get_current_time() self.offset = text_iter.get_offset() self.text = text self.length = length if self.length > 1 or self.text in ('\r', '\n', ' '): self.mergeable = False else: self.mergeable = True class UndoableDelete(object): """something that has ben deleted from our textbuffer""" def __init__(self, text_buffer, start_iter, end_iter): self.time = gobject.get_current_time() self.text = text_buffer.get_text(start_iter, end_iter) self.start = start_iter.get_offset() self.end = end_iter.get_offset() # need to find out if backspace or delete key has been used # so we don't mess up during redo insert_iter = text_buffer.get_iter_at_mark(text_buffer.get_insert()) if insert_iter.get_offset() <= self.start: self.delete_key_used = True else: self.delete_key_used = False if self.end - self.start > 1 or self.text in ('\r', '\n', ' '): self.mergeable = False else: self.mergeable = True class UndoableReplace(object): def __init__(self, first, second): self.first = first self.second = second class UndoableTextBuffer(gtk.TextBuffer): """text buffer with added undo capabilities designed as a drop-in replacement for gtksourceview, at least as far as undo is concerned""" def __init__(self): """ we'll need empty stacks for undo and some state keeping """ gtk.TextBuffer.__init__(self) self.undo_stack = [] self.redo_stack = [] self.not_undoable_action = False self.undo_in_progress = False self.connect('insert-text', self.on_insert_text) self.connect('delete-range', self.on_delete_range) @property def can_undo(self): return bool(self.undo_stack) @property def can_redo(self): return bool(self.redo_stack) def on_insert_text(self, textbuffer, text_iter, text, length): def can_be_merged(prev, cur): """see if we can merge multiple inserts here will try to merge words or whitespace can't merge if prev is UndoableDelete can't merge if prev and cur are not mergeable in the first place can't merge when user set the input bar somewhere else can't merge across word boundaries""" WHITESPACE = (' ', '\t') if isinstance(prev, UndoableReplace): prev = prev.second if isinstance(prev, UndoableDelete): return False elif not cur.mergeable or not prev.mergeable: return False elif cur.offset != (prev.offset + prev.length): return False elif cur.text in WHITESPACE and not prev.text in WHITESPACE: return False elif prev.text in WHITESPACE and not cur.text in WHITESPACE: return False return True def can_be_replaced(prev, cur): return isinstance(prev, UndoableDelete) and prev.time == cur.time if not self.undo_in_progress: self.redo_stack = [] if self.not_undoable_action: return undo_action = UndoableInsert(text_iter, text, length) try: prev_action = self.undo_stack.pop() except IndexError: self.undo_stack.append(undo_action) return if can_be_replaced(prev_action, undo_action): undo_action = UndoableReplace(prev_action, undo_action) elif can_be_merged(prev_action, undo_action): if isinstance(prev_action, UndoableReplace): merge_action = prev_action.second else: merge_action = prev_action merge_action.length += undo_action.length merge_action.text += undo_action.text undo_action = prev_action else: self.undo_stack.append(prev_action) self.undo_stack.append(undo_action) def on_delete_range(self, text_buffer, start_iter, end_iter): def can_be_merged(prev, cur): """see if we can merge multiple deletions here will try to merge words or whitespace can't merge if prev is UndoableInsert can't merge if prev and cur are not mergeable in the first place can't merge if delete and backspace key were both used can't merge across word boundaries""" WHITESPACE = (' ', '\t') if isinstance(prev, UndoableReplace): prev = prev.second if isinstance(prev, UndoableInsert): return False elif not cur.mergeable or not prev.mergeable: return False elif prev.delete_key_used != cur.delete_key_used: return False elif prev.start != cur.start and prev.start != cur.end: return False elif cur.text not in WHITESPACE and \ prev.text in WHITESPACE: return False elif cur.text in WHITESPACE and \ prev.text not in WHITESPACE: return False return True def can_be_replaced(prev, cur): return isinstance(prev, UndoableInsert) and prev.time == cur.time if not self.undo_in_progress: self.redo_stack = [] if self.not_undoable_action: return undo_action = UndoableDelete(text_buffer, start_iter, end_iter) try: prev_action = self.undo_stack.pop() except IndexError: self.undo_stack.append(undo_action) return if can_be_replaced(prev_action, undo_action): undo_action = UndoableReplace(prev_action, undo_action) elif can_be_merged(prev_action, undo_action): if isinstance(prev_action, UndoableReplace): merge_action = prev_action.second else: merge_action = prev_action if merge_action.start == undo_action.start: # delete key used merge_action.text += undo_action.text merge_action.end += (undo_action.end - undo_action.start) else: # Backspace used merge_action.text = '%s%s' % (undo_action.text, merge_action.text) merge_action.start = undo_action.start undo_action = prev_action else: self.undo_stack.append(prev_action) self.undo_stack.append(undo_action) def begin_not_undoable_action(self): """don't record the next actions toggles self.not_undoable_action""" self.not_undoable_action = True def end_not_undoable_action(self): """record next actions toggles self.not_undoable_action""" self.not_undoable_action = False def undo(self): """undo inserts or deletions undone actions are being moved to redo stack""" if not self.undo_stack: return self.begin_not_undoable_action() self.undo_in_progress = True undo_action = self.undo_stack.pop() self.redo_stack.append(undo_action) def do_insert(action): start = self.get_iter_at_offset(action.offset) stop = self.get_iter_at_offset( action.offset + action.length ) self.delete(start, stop) self.place_cursor(start) def do_delete(action): start = self.get_iter_at_offset(action.start) self.insert(start, action.text) stop = self.get_iter_at_offset(action.end) if action.delete_key_used: self.place_cursor(start) else: self.place_cursor(stop) def do(action): if isinstance(action, UndoableInsert): do_insert(action) elif isinstance(action, UndoableDelete): do_delete(action) if isinstance(undo_action, UndoableReplace): do(undo_action.second) do(undo_action.first) else: do(undo_action) self.end_not_undoable_action() self.undo_in_progress = False def redo(self): """redo inserts or deletions redone actions are moved to undo stack""" if not self.redo_stack: return self.begin_not_undoable_action() self.undo_in_progress = True redo_action = self.redo_stack.pop() self.undo_stack.append(redo_action) def do_insert(action): start = self.get_iter_at_offset(action.offset) self.insert(start, action.text) new_cursor_pos = self.get_iter_at_offset( action.offset + action.length ) self.place_cursor(new_cursor_pos) def do_delete(action): start = self.get_iter_at_offset(action.start) stop = self.get_iter_at_offset(action.end) self.delete(start, stop) self.place_cursor(start) def do(action): if isinstance(action, UndoableInsert): do_insert(action) elif isinstance(action, UndoableDelete): do_delete(action) if isinstance(redo_action, UndoableReplace): do(redo_action.first) do(redo_action.second) else: do(redo_action) self.end_not_undoable_action() self.undo_in_progress = False class UndoableTextView(gtk.TextView): def __init__(self, buffer=None, accelgroup=None): if buffer is None: buffer = UndoableTextBuffer() gtk.TextView.__init__(self, buffer) if accelgroup: mod = gtklib.get_thg_modifier() key, modifier = gtk.accelerator_parse(mod+'z') self.add_accelerator('thg-undo', accelgroup, key, modifier, gtk.ACCEL_VISIBLE) def do_undo(view): buffer = self.get_buffer() if hasattr(buffer, 'undo'): buffer.undo() self.connect('thg-undo', do_undo) key, modifier = gtk.accelerator_parse(mod+'y') self.add_accelerator('thg-redo', accelgroup, key, modifier, gtk.ACCEL_VISIBLE) def do_redo(view): buffer = self.get_buffer() if hasattr(buffer, 'redo'): buffer.redo() self.connect('thg-redo', do_redo)