-
Notifications
You must be signed in to change notification settings - Fork 0
/
undostack.py
628 lines (531 loc) · 21.5 KB
/
undostack.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# undostack.py - undo/redo provide
# Copyright (C) Kuleshov Alexander 2010 <kuleshovmail@gmail.com>
#
# Incedit is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# main.py is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import gtk
import sys, os.path, pango
import locale
import cairo
import gtk.gdk
import re
import gobject
class Feature(object):
def __init__(self, buffer):
assert buffer is not None
self.buffer = buffer
def _print_range(self, start, end):
print "RANGE:", repr(self.buffer.get_text(start, end))
def _print_char(self, iter):
end = iter.copy()
end.forward_char()
print "CHAR:", repr(self.buffer.get_text(iter, end))
bullet_point_re = re.compile(r'^(?:\d+\.|[\*\-])$')
class ListIndent(Feature):
def __init__(self, buffer):
Feature.__init__(self, buffer)
self.bullet_point = u'.'
self.lock_signals = None
self.start_tag = buffer.create_tag('list-start',
left_margin = 30,
pixels_above_lines = 12)
self.bullet_tag = buffer.create_tag('list-bullet',
left_margin = 30)
self.list_tag = buffer.create_tag('list',
left_margin = 30,
pixels_above_lines = 3)
buffer.connect_after('insert-text', self._on_buffer_insert_text_after)
buffer.connect('delete-range', self._on_buffer_delete_range)
buffer.connect('mark-set', self._on_buffer_mark_set)
def _on_buffer_mark_set(self, buffer, iter, mark):
if mark.get_name() != 'insert':
return
if not iter.has_tag(self.bullet_tag):
return
if self.lock_signals:
return
self.lock_signals = True
next = iter.copy()
next.forward_char()
if next.has_tag(self.bullet_tag):
iter.forward_to_tag_toggle(self.bullet_tag)
else:
iter.backward_to_tag_toggle(self.bullet_tag)
iter.backward_char()
buffer.place_cursor(iter)
self.lock_signals = False
def _to_list_start(self, iter):
self._to_list_item_start(iter)
while not iter.is_start():
next = iter.copy()
next.backward_line()
if not self._at_bullet_point(next):
break
iter = next
def _to_list_end(self, iter):
while True:
self._to_list_item_end(iter)
iter.forward_char()
if iter.is_end():
return
if not self._at_bullet_point(iter):
iter.backward_char()
return
def _to_list_item_start(self, iter):
iter.set_line(iter.get_line())
def _to_list_item_end(self, iter):
iter.forward_line()
if iter.is_end():
return
iter.backward_char()
def _at_bullet_point(self, iter):
end = iter.copy()
end.forward_find_char(lambda x, d: x == ' ')
if end.is_end() or end.get_line() != iter.get_line():
return False
text = self.buffer.get_text(iter, end)
if text == self.bullet_point or bullet_point_re.match(text):
return True
return False
def _insert_inside_list(self, buffer, start, end):
insert_start = buffer.get_iter_at_offset(start)
insert_end = buffer.get_iter_at_offset(end)
text = buffer.get_text(insert_start, insert_end)
item_start = insert_start.copy()
self._to_list_item_start(item_start)
list_end = insert_end.copy()
self._to_list_end(list_end)
bullet_end = item_start.copy()
bullet_end.forward_to_tag_toggle(self.bullet_tag)
bullet_point = buffer.get_text(item_start, bullet_end)
buffer.apply_tag(self.list_tag, item_start, list_end)
if text in ('\r', '\n'):
next_char = list_end.copy()
next_char.forward_char()
buffer.remove_tag(self.list_tag, list_end, next_char)
buffer.insert_with_tags(insert_end,
bullet_point,
self.bullet_tag, self.list_tag)
def _insert_outside_list(self, buffer, insert_start_off, insert_end_off):
iter = buffer.get_iter_at_offset(insert_start_off)
iter.set_line(iter.get_line())
if not self._at_bullet_point(iter):
return
while iter.get_offset() < insert_end_off:
if not self._at_bullet_point(iter):
iter.forward_line()
continue
start_off = iter.get_offset()
start = iter.copy()
next_char = iter.copy()
next_char.forward_find_char(lambda x, d: x == ' ')
buffer.delete(start, next_char)
buffer.insert(start, self.bullet_point)
start = buffer.get_iter_at_offset(start_off)
end_off = start_off + len(self.bullet_point) + 1
end = buffer.get_iter_at_offset(end_off)
buffer.apply_tag(self.bullet_tag, start, end)
iter = buffer.get_iter_at_offset(start_off)
iter.forward_line()
start = buffer.get_iter_at_offset(insert_start_off)
self._to_list_start(start)
end = start.copy()
end.forward_to_tag_toggle(self.bullet_tag)
buffer.apply_tag(self.start_tag, start, end)
buffer.apply_tag(self.bullet_tag, start, end)
self._to_list_end(end)
buffer.apply_tag(self.list_tag, start, end)
def _on_buffer_insert_text_after(self, buffer, iter, text, length):
if self.lock_signals:
return
self.lock_signals = True
end = iter.get_offset()
start = end - len(unicode(text))
previous = buffer.get_iter_at_offset(start - 1)
if previous.has_tag(self.list_tag):
self._insert_inside_list(buffer, start, end)
else:
self._insert_outside_list(buffer, start, end)
self.lock_signals = False
def _delete_inside_list(self, start, end):
if not start.has_tag(self.list_tag) and not self._at_bullet_point(end):
next_item_start = end.copy()
self._to_list_item_end(next_item_start)
next_item_start.forward_line()
self.buffer.remove_tag(self.list_tag, end, next_item_start)
if self._at_bullet_point(next_item_start):
bullet_end = next_item_start.copy()
bullet_end.forward_to_tag_toggle(self.bullet_tag)
self.buffer.apply_tag(self.start_tag,
next_item_start,
bullet_end)
return
if start.has_tag(self.start_tag):
start.backward_to_tag_toggle(self.start_tag)
item_end = end.copy()
self._to_list_item_end(item_end)
item_end.forward_char()
self.buffer.remove_tag(self.list_tag, start, item_end)
if not item_end.has_tag(self.bullet_tag):
return
bullet_end = item_end.copy()
bullet_end.forward_to_tag_toggle(self.bullet_tag)
self.buffer.apply_tag(self.start_tag, item_end, bullet_end)
return
if start.has_tag(self.bullet_tag):
start.backward_to_tag_toggle(self.bullet_tag)
prev_char = start.copy()
prev_char.backward_char()
next_item = end.copy()
self._to_list_item_end(next_item)
next_item.forward_char()
self.buffer.remove_tag(self.list_tag, prev_char, next_item)
if next_item.has_tag(self.bullet_tag):
bullet_end = next_item.copy()
bullet_end.forward_to_tag_toggle(self.bullet_tag)
self.buffer.apply_tag(self.start_tag, next_item, bullet_end)
if end.has_tag(self.bullet_tag):
end.forward_to_tag_toggle(self.bullet_tag)
end_line = end.copy()
self._to_list_item_start(end_line)
if end_line.has_tag(self.bullet_tag):
return
next = end.copy()
self._to_list_item_end(next)
if not next.is_end():
next.backward_char()
self.buffer.apply_tag(self.list_tag, end, next)
def _delete_outside_list(self, start, end):
next = start.copy()
next.forward_char()
text = self.buffer.get_text(start, next)
if text not in ('\r', '\n'):
return
previous = start.copy()
previous.backward_char()
if not previous.has_tag(self.list_tag):
return
item_end = end.copy()
self._to_list_item_end(item_end)
self.buffer.apply_tag(self.list_tag, end, item_end)
def _on_buffer_delete_range(self, buffer, start, end):
if self.lock_signals:
return
self.lock_signals = True
if start.has_tag(self.list_tag) or end.has_tag(self.list_tag):
self._delete_inside_list(start, end)
else:
self._delete_outside_list(start, end)
self.lock_signals = False
class LayoutBox(object):
annotation = None
pull = 0
y = 0
height = 0
def __init__(self, parent, annotation):
self.parent = parent
self.annotation = annotation
self.annotation.connect('size-allocate', self._on_annotation_allocate)
def _on_annotation_allocate(self, annotation, rect):
self.height = rect.height
self.parent.widget._update_annotations()
class Layout(object):
def __init__(self, widget, *args, **kwargs):
self.widget = widget
self.annotations = []
self.boxes = {}
self.padding = kwargs.get('padding', 0)
def add(self, annotation):
self.annotations.append(annotation)
self.boxes[annotation] = LayoutBox(self, annotation)
def remove(self, annotation):
self.annotations.remove(annotation)
del self.boxes[annotation]
def pull(self, annotation, position):
self.boxes[annotation].pull = position
def sort(self, sort_func):
self.annotations.sort(sort_func)
def get_children(self):
return self.annotations
def get_annotation_position(self, annotation):
box = self.boxes[annotation]
return self.padding, box.y
def _try_move_up(self, number, delta):
if delta == 0:
return
annotation = self.annotations[number]
box = self.boxes[annotation]
if number == 0:
box.y = max(0, box.y - delta)
return
previous_annotation = self.annotations[number - 1]
previous_box = self.boxes[previous_annotation]
previous_box_end = previous_box.y + previous_box.height
possible_delta = max(0, box.y - previous_box_end)
if possible_delta >= delta:
box.y -= delta
return
box.y -= possible_delta
if possible_delta != delta:
self._try_move_up(number - 1, (delta - possible_delta) / 2)
box.y = previous_box.y + previous_box.height
def _dbg(self, action, number):
return
annotation = self.annotations[number]
box = self.boxes[annotation]
values = action, number, box.height, box.pull, box.y
print "%s: Box %d (h%d) with pull %d is at %d" % values
def update(self, width, height):
for n, annotation in enumerate(self.annotations):
box = self.boxes[annotation]
box.y = max(0, box.pull - box.height / 2)
self._dbg('default', n)
if n == 0:
continue
previous_annotation = self.annotations[n - 1]
previous_box = self.boxes[previous_annotation]
previous_box_end = previous_box.y + previous_box.height
if previous_box_end >= box.y:
self._dbg('moving', n - 1)
self._try_move_up(n - 1, (previous_box_end - box.y) / 2)
self._dbg('moved', n - 1)
box.y = previous_box.y + previous_box.height
self._dbg('moved', n)
for n, annotation in enumerate(self.annotations):
box = self.boxes[annotation]
self._dbg('showing', n)
self.widget.move_child(annotation, self.padding, box.y)
class Undoable(object):
def __init__(self, buffer, startiter = None):
self.buffer = buffer
self.start = None
if startiter is not None:
self.start = startiter.get_offset()
def undo(self):
raise Exception("Not implemented")
def redo(self):
raise Exception("Not implemented")
class UndoApplyTag(Undoable):
def __init__(self, buffer, startiter, enditer, tag):
Undoable.__init__(self, buffer, startiter)
self.end = enditer.get_offset()
self.old_tags = buffer.get_tags_at_offset(self.start, self.end)
self.tag = tag
def undo(self):
self.buffer.remove_tag_at_offset(self.tag, self.start, self.end)
self.buffer.apply_tags_at_offset(self.old_tags, self.start, self.end)
def redo(self):
self.buffer.apply_tag_at_offset(self.tag, self.start, self.end)
class UndoCollection(Undoable):
def __init__(self, buffer):
Undoable.__init__(self, buffer, None)
self.children = []
def add(self, child):
self.children.append(child)
def undo(self):
for child in reversed(self.children):
child.undo()
def redo(self):
for child in self.children:
child.redo()
class UndoDeleteText(Undoable):
def __init__(self, buffer, startiter, enditer):
Undoable.__init__(self, buffer, startiter)
self.end = enditer.get_offset()
self.tags = buffer.get_tags_at_offset(self.start, self.end)
self.text = buffer.get_text(startiter, enditer)
def undo(self):
self.buffer.insert_at_offset(self.start, self.text)
self.buffer.apply_tags_at_offset(self.tags, self.start, self.end)
def redo(self):
self.buffer.delete_range_at_offset(self.start, self.end)
class UndoInsertText(Undoable):
def __init__(self, buffer, startiter, text):
Undoable.__init__(self, buffer, startiter)
self.end = self.start + len(unicode(text))
self.text = text
def undo(self):
self.buffer.delete_range_at_offset(self.start, self.end)
def redo(self):
self.buffer.insert_at_offset(self.start, self.text)
class UndoRemoveTag(Undoable):
def __init__(self, buffer, startiter, enditer, tag):
Undoable.__init__(self, buffer, startiter)
self.end = enditer.get_offset()
self.old_tags = buffer.get_tags_at_offset(self.start, self.end)
self.tag = tag
def undo(self):
self.buffer.apply_tags_at_offset(self.old_tags, self.start, self.end)
def redo(self):
self.buffer.remove_tag_at_offset(self.tag, self.start, self.end)
class TextBuffer(gtk.TextBuffer):
def __init__(self, *args, **kwargs):
gtk.TextBuffer.__init__(self, *args)
self.undo_stack = []
self.redo_stack = []
self.current_undo = UndoCollection(self)
self.lock_undo = False
self.max_undo = 250
self.max_redo = 250
self.undo_freq = 300
self.undo_timeout_id = None
self.user_action = 0
self.active_features = []
self.connect('insert-text', self._on_insert_text)
self.connect('delete-range', self._on_delete_range)
self.connect('apply-tag', self._on_apply_tag)
self.connect('remove-tag', self._on_remove_tag)
self.connect('begin-user-action', self._on_begin_user_action)
self.connect('end-user-action', self._on_end_user_action)
features = (
('list-indent', True, ListIndent, ()),
)
for name, default, feature, feature_args in features:
active = kwargs.get(name, default)
if active:
self.activate_feature(feature, *feature_args)
self._update_timestamp()
def activate_feature(self, feature, *args):
self.active_features.append(feature(self, *args))
def _cancel_undo_timeout(self):
if self.undo_timeout_id is None:
return
gobject.source_remove(self.undo_timeout_id)
self.undo_timeout_id = None
self.end_user_action()
def _update_timestamp(self):
if self.undo_timeout_id is not None:
gobject.source_remove(self.undo_timeout_id)
else:
self.begin_user_action()
self.undo_timeout_id = gobject.timeout_add(self.undo_freq,
self._cancel_undo_timeout)
def _on_insert_text(self, buffer, start, text, length):
if self.lock_undo:
return
self._update_timestamp()
item = UndoInsertText(self, start, text)
self.current_undo.add(item)
self.emit('undo-stack-changed')
def _on_delete_range(self, buffer, start, end):
if self.lock_undo:
return
self._update_timestamp()
item = UndoDeleteText(self, start, end)
self.current_undo.add(item)
def _on_apply_tag(self, buffer, tag, start, end):
if self.lock_undo:
return
self._update_timestamp()
item = UndoApplyTag(self, start, end, tag)
self.current_undo.add(item)
def _on_remove_tag(self, buffer, tag, start, end):
if self.lock_undo:
return
self._update_timestamp()
item = UndoRemoveTag(self, start, end, tag)
self.current_undo.add(item)
def _on_begin_user_action(self, buffer):
self.user_action += 1
def _on_end_user_action(self, buffer):
self.user_action -= 1
if self.user_action != 0:
return
if self.current_undo is None:
return
if len(self.current_undo.children) == 0:
return
self._undo_add(self.current_undo)
self.redo_stack = []
self.current_undo = UndoCollection(self)
def _undo_add(self, item):
self.undo_stack.append(item)
while len(self.undo_stack) >= self.max_undo:
self.undo_stack.pop(0)
self.emit('undo-stack-changed')
def _redo_add(self, item):
self.redo_stack.append(item)
while len(self.redo_stack) >= self.max_redo:
self.redo_stack.pop(0)
self.emit('undo-stack-changed')
def insert_at_offset(self, offset, text):
iter = self.get_iter_at_offset(offset)
self.insert(iter, text)
def delete_range_at_offset(self, start, end):
start = self.get_iter_at_offset(start)
end = self.get_iter_at_offset(end)
self.delete(start, end)
def apply_tag_at_offset(self, tag, start, end):
start = self.get_iter_at_offset(start)
end = self.get_iter_at_offset(end)
self.apply_tag(tag, start, end)
def remove_tag_at_offset(self, tag, start, end):
start = self.get_iter_at_offset(start)
end = self.get_iter_at_offset(end)
self.remove_tag(tag, start, end)
def get_tags_at_offset(self, start, end):
taglist = []
iter = self.get_iter_at_offset(start)
while True:
taglist.append(iter.get_tags())
iter.forward_char()
if iter.get_offset() >= end:
break
return taglist
def apply_tags_at_offset(self, taglist, start, end):
end = self.get_iter_at_offset(start + 1)
start = self.get_iter_at_offset(start)
for tags in taglist:
for tag in tags:
self.apply_tag(tag, start, end)
start.forward_char()
end.forward_char()
def can_undo(self):
if self.current_undo is not None \
and len(self.current_undo.children) > 0:
return True
return len(self.undo_stack) > 0
def undo(self):
self._cancel_undo_timeout()
if len(self.undo_stack) == 0:
return
self.lock_undo = True
item = self.undo_stack.pop()
item.undo()
self._redo_add(item)
self.lock_undo = False
def can_redo(self):
return len(self.redo_stack) > 0
def redo(self):
self._cancel_undo_timeout()
if len(self.redo_stack) == 0:
return
self.lock_undo = True
item = self.redo_stack.pop()
item.redo()
self._undo_add(item)
self.lock_undo = False
gobject.signal_new('undo-stack-changed',
TextBuffer,
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
())
gobject.type_register(TextBuffer)
class TextEditor(gtk.TextView):
def __init__(self, textbuffer = None, *args, **kwargs):
if textbuffer is None:
textbuffer = TextBuffer()
gtk.TextView.__init__(self, textbuffer, *args, **kwargs)