-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundo-tree.el
3159 lines (2794 loc) · 122 KB
/
undo-tree.el
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; undo-tree.el --- Treat undo history as a tree
;; Copyright (C) 2009-2011 Toby Cubitt
;; Author: Toby Cubitt <[email protected]>
;; Version: 0.3.1
;; Keywords: undo, redo, history, tree
;; URL: http://www.dr-qubit.org/emacs.php
;; Git Repository: http://www.dr-qubit.org/git/undo-tree.git
;; This file is NOT part of Emacs.
;;
;; This file is free software: you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free
;; Software Foundation, either version 3 of the License, or (at your option)
;; any later version.
;;
;; This program 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 General Public License for
;; more details.
;;
;; You should have received a copy of the GNU General Public License along
;; with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Emacs has a powerful undo system. Unlike the standard undo/redo system in
;; most software, it allows you to recover *any* past state of a buffer
;; (whereas the standard undo/redo system can lose past states as soon as you
;; redo). However, this power comes at a price: many people find Emacs' undo
;; system confusing and difficult to use, spawning a number of packages that
;; replace it with the less powerful but more intuitive undo/redo system.
;;
;; Both the loss of data with standard undo/redo, and the confusion of Emacs'
;; undo, stem from trying to treat undo history as a linear sequence of
;; changes. It's not. The `undo-tree-mode' provided by this package replaces
;; Emacs' undo system with a system that treats undo history as what it is: a
;; branching tree of changes. This simple idea allows the more intuitive
;; behaviour of the standard undo/redo system to be combined with the power of
;; never losing any history. An added side bonus is that undo history can in
;; some cases be stored more efficiently, allowing more changes to accumulate
;; before Emacs starts discarding history.
;;
;; The only downside to this more advanced yet simpler undo system is that it
;; was inspired by Vim. But, after all, most successful religions steal the
;; best ideas from their competitors!
;;
;;
;; Installation
;; ============
;;
;; This package has only been tested with Emacs versions 22, 23 and CVS. It
;; will not work without modifications in earlier versions of Emacs.
;;
;; To install `undo-tree-mode', make sure this file is saved in a directory in
;; your `load-path', and add the line:
;;
;; (require 'undo-tree)
;;
;; to your .emacs file. Byte-compiling undo-tree.el is recommended (e.g. using
;; "M-x byte-compile-file" from within emacs).
;;
;; If you want to replace the standard Emacs' undo system with the
;; `undo-tree-mode' system in all buffers, you can enable it globally by
;; adding:
;;
;; (global-undo-tree-mode)
;;
;; to your .emacs file.
;;
;;
;; Quick-Start
;; ===========
;;
;; If you're the kind of person who likes jump in the car and drive, without
;; bothering to first figure out whether the button on the left dips the
;; headlights or operates the ejector seat (after all, you'll soon figure it
;; out when you push it), then here's the minimum you need to know:
;;
;; `undo-tree-mode' and `global-undo-tree-mode'
;; Enable undo-tree mode (either in the current buffer or globally).
;;
;; C-_ C-/ (`undo-tree-undo')
;; Undo changes.
;;
;; M-_ C-? (`undo-tree-redo')
;; Redo changes.
;;
;; `undo-tree-switch-branch'
;; Switch undo-tree branch.
;; (What does this mean? Better press the button and see!)
;;
;; C-x u (`undo-tree-visualize')
;; Visualize the undo tree.
;; (Better try pressing this button too!)
;;
;; C-x r u (`undo-tree-save-state-to-register')
;; Save current buffer state to register.
;;
;; C-x r U (`undo-tree-restore-state-from-register')
;; Restore buffer state from register.
;;
;;
;; In the undo-tree visualizer:
;;
;; <up> p C-p (`undo-tree-visualize-undo')
;; Undo changes.
;;
;; <down> n C-n (`undo-tree-visualize-redo')
;; Redo changes.
;;
;; <left> b C-b (`undo-tree-visualize-switch-branch-left')
;; Switch to previous undo-tree branch.
;;
;; <right> f C-f (`undo-tree-visualize-switch-branch-right')
;; Switch to next undo-tree branch.
;;
;; t (`undo-tree-visualizer-toggle-timestamps')
;; Toggle display of time-stamps.
;;
;; q C-q (`undo-tree-visualizer-quit')
;; Quit undo-tree-visualizer.
;;
;; , <
;; Scroll left.
;;
;; . >
;; Scroll right.
;;
;; <pgup>
;; Scroll up.
;;
;; <pgdown>
;; Scroll down.
;;
;;
;;
;; Undo Systems
;; ============
;;
;; To understand the different undo systems, it's easiest to consider an
;; example. Imagine you make a few edits in a buffer. As you edit, you
;; accumulate a history of changes, which we might visualize as a string of
;; past buffer states, growing downwards:
;;
;; o (initial buffer state)
;; |
;; |
;; o (first edit)
;; |
;; |
;; o (second edit)
;; |
;; |
;; x (current buffer state)
;;
;;
;; Now imagine that you undo the last two changes. We can visualize this as
;; rewinding the current state back two steps:
;;
;; o (initial buffer state)
;; |
;; |
;; x (current buffer state)
;; |
;; |
;; o
;; |
;; |
;; o
;;
;;
;; However, this isn't a good representation of what Emacs' undo system
;; does. Instead, it treats the undos as *new* changes to the buffer, and adds
;; them to the history:
;;
;; o (initial buffer state)
;; |
;; |
;; o (first edit)
;; |
;; |
;; o (second edit)
;; |
;; |
;; x (buffer state before undo)
;; |
;; |
;; o (first undo)
;; |
;; |
;; x (second undo)
;;
;;
;; Actually, since the buffer returns to a previous state after an undo,
;; perhaps a better way to visualize it is to imagine the string of changes
;; turning back on itself:
;;
;; (initial buffer state) o
;; |
;; |
;; (first edit) o x (second undo)
;; | |
;; | |
;; (second edit) o o (first undo)
;; | /
;; |/
;; o (buffer state before undo)
;;
;; Treating undos as new changes might seem a strange thing to do. But the
;; advantage becomes clear as soon as we imagine what happens when you edit
;; the buffer again. Since you've undone a couple of changes, new edits will
;; branch off from the buffer state that you've rewound to. Conceptually, it
;; looks like this:
;;
;; o (initial buffer state)
;; |
;; |
;; o
;; |\
;; | \
;; o x (new edit)
;; |
;; |
;; o
;;
;; The standard undo/redo system only lets you go backwards and forwards
;; linearly. So as soon as you make that new edit, it discards the old
;; branch. Emacs' undo just keeps adding changes to the end of the string. So
;; the undo history in the two systems now looks like this:
;;
;; Undo/Redo: Emacs' undo
;;
;; o o
;; | |
;; | |
;; o o o
;; .\ | |\
;; . \ | | \
;; . x (new edit) o o |
;; (discarded . | / |
;; branch) . |/ |
;; . o |
;; |
;; |
;; x (new edit)
;;
;; Now, what if you change your mind about those undos, and decide you did
;; like those other changes you'd made after all? With the standard undo/redo
;; system, you're lost. There's no way to recover them, because that branch
;; was discarded when you made the new edit.
;;
;; However, in Emacs' undo system, those old buffer states are still there in
;; the undo history. You just have to rewind back through the new edit, and
;; back through the changes made by the undos, until you reach them. Of
;; course, since Emacs treats undos (even undos of undos!) as new changes,
;; you're really weaving backwards and forwards through the history, all the
;; time adding new changes to the end of the string as you go:
;;
;; o
;; |
;; |
;; o o o (undo new edit)
;; | |\ |\
;; | | \ | \
;; o o | | o (undo the undo)
;; | / | | |
;; |/ | | |
;; (trying to get o | | x (undo the undo)
;; to this state) | /
;; |/
;; o
;;
;; So far, this is still reasonably intuitive to use. It doesn't behave so
;; differently to standard undo/redo, except that by going back far enough you
;; can access changes that would be lost in standard undo/redo.
;;
;; However, imagine that after undoing as just described, you decide you
;; actually want to rewind right back to the initial state. If you're lucky,
;; and haven't invoked any command since the last undo, you can just keep on
;; undoing until you get back to the start:
;;
;; (trying to get o x (got there!)
;; to this state) | |
;; | |
;; o o o o (keep undoing)
;; | |\ |\ |
;; | | \ | \ |
;; o o | | o o (keep undoing)
;; | / | | | /
;; |/ | | |/
;; (already undid o | | o (got this far)
;; to this state) | /
;; |/
;; o
;;
;; But if you're unlucky, and you happen to have moved the point (say) after
;; getting to the state labelled "got this far", then you've "broken the undo
;; chain". Hold on to something solid, because things are about to get
;; hairy. If you try to undo now, Emacs thinks you're trying to undo the
;; undos! So to get back to the initial state you now have to rewind through
;; *all* the changes, including the undos you just did:
;;
;; (trying to get o x (finally got there!)
;; to this state) | |
;; | |
;; o o o o o o
;; | |\ |\ |\ |\ |
;; | | \ | \ | \ | \ |
;; o o | | o o o | o o
;; | / | | | / | | | /
;; |/ | | |/ | | |/
;; (already undid o | | o<. | | o
;; to this state) | / : | /
;; |/ : |/
;; o : o
;; :
;; (got this far, but
;; broke the undo chain)
;;
;; Confused?
;;
;; In practice you can just hold down the undo key until you reach the buffer
;; state that you want. But whatever you do, don't move around in the buffer
;; to *check* that you've got back to where you want! Because you'll break the
;; undo chain, and then you'll have to traverse the entire string of undos
;; again, just to get back to the point at which you broke the
;; chain. Undo-in-region and commands such as `undo-only' help to make using
;; Emacs' undo a little easier, but nonetheless it remains confusing for many
;; people.
;;
;;
;; So what does `undo-tree-mode' do? Remember the diagram we drew to represent
;; the history we've been discussing (make a few edits, undo a couple of them,
;; and edit again)? The diagram that conceptually represented our undo
;; history, before we started discussing specific undo systems? It looked like
;; this:
;;
;; o (initial buffer state)
;; |
;; |
;; o
;; |\
;; | \
;; o x (current state)
;; |
;; |
;; o
;;
;; Well, that's *exactly* what the undo history looks like to
;; `undo-tree-mode'. It doesn't discard the old branch (as standard undo/redo
;; does), nor does it treat undos as new changes to be added to the end of a
;; linear string of buffer states (as Emacs' undo does). It just keeps track
;; of the tree of branching changes that make up the entire undo history.
;;
;; If you undo from this point, you'll rewind back up the tree to the previous
;; state:
;;
;; o
;; |
;; |
;; x (undo)
;; |\
;; | \
;; o o
;; |
;; |
;; o
;;
;; If you were to undo again, you'd rewind back to the initial state. If on
;; the other hand you redo the change, you'll end up back at the bottom of the
;; most recent branch:
;;
;; o (undo takes you here)
;; |
;; |
;; o (start here)
;; |\
;; | \
;; o x (redo takes you here)
;; |
;; |
;; o
;;
;; So far, this is just like the standard undo/redo system. But what if you
;; want to return to a buffer state located on a previous branch of the
;; history? Since `undo-tree-mode' keeps the entire history, you simply need
;; to tell it to switch to a different branch, and then redo the changes you
;; want:
;;
;; o
;; |
;; |
;; o (start here, but switch
;; |\ to the other branch)
;; | \
;; (redo) o o
;; |
;; |
;; (redo) x
;;
;; Now you're on the other branch, if you undo and redo changes you'll stay on
;; that branch, moving up and down through the buffer states located on that
;; branch. Until you decide to switch branches again, of course.
;;
;; Real undo trees might have multiple branches and sub-branches:
;;
;; o
;; ____|______
;; / \
;; o o
;; ____|__ __|
;; / | \ / \
;; o o o o x
;; | |
;; / \ / \
;; o o o o
;;
;; Trying to imagine what Emacs' undo would do as you move about such a tree
;; will likely frazzle your brain circuits! But in `undo-tree-mode', you're
;; just moving around this undo history tree. Most of the time, you'll
;; probably only need to stay on the most recent branch, in which case it
;; behaves like standard undo/redo, and is just as simple to understand. But
;; if you ever need to recover a buffer state on a different branch, the
;; possibility of switching between branches and accessing the full undo
;; history is still there.
;;
;;
;;
;; The Undo-Tree Visualizer
;; ========================
;;
;; Actually, it gets better. You don't have to imagine all these tree
;; diagrams, because `undo-tree-mode' includes an undo-tree visualizer which
;; draws them for you! In fact, it draws even better diagrams: it highlights
;; the node representing the current buffer state, it highlights the current
;; branch, and (by hitting "t") you can toggle the display of
;; time-stamps. (There's one other tiny difference: the visualizer puts the
;; most recent branch on the left rather than the right.)
;;
;; In the visualizer, the usual keys for moving up and down a buffer instead
;; move up and down the undo history tree (e.g. the up and down arrow keys, or
;; "C-n" and "C-p"). The state of the "parent" buffer (the buffer whose undo
;; history you are visualizing) is updated as you move around the undo tree in
;; the visualizer. If you reach a branch point in the visualizer, the usual
;; keys for moving forward and backward in a buffer instead switch branch
;; (e.g. the left and right arrow keys, or "C-f" and "C-b"). And clicking with
;; the mouse on any node in the visualizer will take you directly to that
;; node, resetting the state of the parent buffer to the state represented by
;; that node.
;;
;; It can be useful to see how long ago the parent buffer was in the state
;; represented by a particular node in the visualizer. Hitting "t" in the
;; visualizer toggles the display of time-stamps for all the nodes. (Note
;; that, because of the way `undo-tree-mode' works, these time-stamps may be
;; somewhat later than the true times, especially if it's been a long time
;; since you last undid any changes.)
;;
;; Finally, hitting "q" will quit the visualizer, leaving the parent buffer in
;; whatever state you ended at.
;;
;;
;;
;; Undo-in-Region
;; ==============
;;
;; Emacs allows a very useful and powerful method of undoing only selected
;; changes: when a region is active, only changes that affect the text within
;; that region will are undone. With the standard Emacs undo system, changes
;; produced by undoing-in-region naturally get added onto the end of the
;; linear undo history:
;;
;; o
;; |
;; | x (second undo-in-region)
;; o |
;; | |
;; | o (first undo-in-region)
;; o |
;; | /
;; |/
;; o
;;
;; You can of course redo these undos-in-region as usual, by undoing the
;; undos:
;;
;; o
;; |
;; | o_
;; o | \
;; | | |
;; | o o (undo the undo-in-region)
;; o | |
;; | / |
;; |/ |
;; o x (undo the undo-in-region)
;;
;;
;; In `undo-tree-mode', undo-in-region works similarly: when there's an active
;; region, undoing only undoes changes that affect that region. However, the
;; way these undos-in-region are recorded in the undo history is quite
;; different. In `undo-tree-mode', undo-in-region creates a new branch in the
;; undo history. The new branch consists of an undo step that undoes some of
;; the changes that affect the current region, and another step that undoes
;; the remaining changes needed to rejoin the previous undo history.
;;
;; Previous undo history Undo-in-region
;;
;; o o
;; | |
;; | |
;; o o
;; | |\
;; | | \
;; o o x (undo-in-region)
;; | | |
;; | | |
;; x o o
;;
;; As long as you don't change the active region after undoing-in-region,
;; continuing to undo-in-region extends the new branch, pulling more changes
;; that affect the current region into an undo step immediately above your
;; current location in the undo tree, and pushing the point at which the new
;; branch is attached further up the tree:
;;
;; First undo-in-region Second undo-in-region
;;
;; o o
;; | |\
;; | | \
;; o o x (undo-in-region)
;; |\ | |
;; | \ | |
;; o x o o
;; | | | |
;; | | | |
;; o o o o
;;
;; Redoing takes you back down the undo tree, as usual (as long as you haven't
;; changed the active region after undoing-in-region, it doesn't matter if it
;; is still active):
;;
;; o
;; |\
;; | \
;; o o
;; | |
;; | |
;; o o (redo)
;; | |
;; | |
;; o x (redo)
;;
;;
;; What about redo-in-region? Obviously, this only makes sense if you have
;; already undone some changes, so that there are some changes to redo!
;; Redoing-in-region splits off a new branch of the undo history below your
;; current location in the undo tree. This time, the new branch consists of a
;; redo step that redoes some of the redo changes that affect the current
;; region, followed by all the remaining redo changes.
;;
;; Previous undo history Redo-in-region
;;
;; o o
;; | |
;; | |
;; x o
;; | |\
;; | | \
;; o o x (redo-in-region)
;; | | |
;; | | |
;; o o o
;;
;; As long as you don't change the active region after redoing-in-region,
;; continuing to redo-in-region extends the new branch, pulling more redo
;; changes into a redo step immediately below your current location in the
;; undo tree.
;;
;; First redo-in-region Second redo-in-region
;;
;; o o
;; | |
;; | |
;; o o
;; |\ |\
;; | \ | \
;; o x (redo-in-region) o o
;; | | | |
;; | | | |
;; o o o x (redo-in-region)
;; |
;; |
;; o
;;
;; Note that undo-in-region and redo-in-region only ever add new changes to
;; the undo tree, they *never* modify existing undo history. So you can always
;; return to previous buffer states by switching to a previous branch of the
;; tree.
;;; Change Log:
;;
;; Version 0.3.1
;; * use `get-buffer-create' when creating the visualizer buffer in
;; `undo-tree-visualize', to fix bug caused by `global-undo-tree-mode' being
;; enabled in the visualizer when `default-major-mode' is set to something
;; other than `fundamental-mode' (thanks to Michael Heerdegen for suggesting
;; this fix)
;; * modified `turn-on-undo-tree-mode' to avoid turning on `undo-tree-mode' if
;; the buffer's `major-mode' implements its own undo system, by checking
;; whether `undo' is remapped, the default "C-/" or "C-_" bindings have been
;; overridden, or the `major-mode' is listed in
;; `undo-tree-incompatible-major-modes'
;; * discard position entries from `buffer-undo-list' changesets created by
;; undoing or redoing, to ensure point is always moved to where the change
;; is (standard Emacs `undo' also does this)
;; * fixed `undo-tree-draw-node' to use correct faces and indicate registers
;; when displaying timestamps in visualizer
;;
;; Version 0.3
;; * implemented undo-in-region
;; * fixed bugs in `undo-list-transfer-to-tree' and
;; `undo-list-rebuild-from-tree' which caused errors when undo history was
;; empty or disabled
;; * defun `region-active-p' if not already defined, for compatibility with
;; older Emacsen
;;
;; Version 0.2.1
;; * modified `undo-tree-node' defstruct and macros to allow arbitrary
;; meta-data to be stored in a plist associated with a node, and
;; reimplemented storage of visualizer data on top of this
;; * display registers storing undo-tree state in visualizer
;; * implemented keyboard selection in visualizer
;; * rebuild `buffer-undo-list' from tree when disabling `undo-tree-mode'
;;
;; Version 0.2
;; * added support for marker undo entries
;;
;; Version 0.1.7
;; * pass null argument to `kill-buffer' call in `undo-tree-visualizer-quit',
;; since the argument's not optional in earlier Emacs versions
;; * added match for "No further redo information" to
;; `debug-ignored-errors' to prevent debugger being called on this error
;; * made `undo-tree-visualizer-quit' select the window displaying the
;; visualizer's parent buffer, or switch to the parent buffer if no window
;; is displaying it
;; * fixed bug in `undo-tree-switch-branch'
;; * general code tidying and reorganisation
;; * fixed bugs in history-discarding logic
;; * fixed bug in `undo-tree-insert' triggered by `undo-tree-visualizer-set'
;; by ensuring mark is deactivated
;;
;; Version 0.1.6
;; * added `undo-tree-mode-lighter' customization option to allow the
;; mode-line lighter to be changed
;; * bug-fix in `undo-tree-discard-node'
;; * added `undo-tree-save-state-to-register' and
;; `undo-tree-restore-state-from-register' commands and keybindings for
;; saving/restoring undo-tree states using registers
;;
;; Version 0.1.5
;; * modified `undo-tree-visualize' to mark the visualizer window as
;; soft-dedicated, and changed `undo-tree-visualizer-quit' to use
;; `kill-buffer', so that the visualizer window is deleted along with its
;; buffer if the visualizer buffer was displayed in a new window, but not if
;; it was displayed in an existing window.
;;
;; Version 0.1.4
;; * modified `undo-tree-undo' and `undo-tree-redo' to always replace
;; redo/undo entries with new ones generated by `primitive-undo', as the new
;; changesets will restore the point more reliably
;;
;; Version 0.1.3
;; * fixed `undo-tree-visualizer-quit' to remove `after-change-functions'
;; hook there, rather than in `undo-tree-kill-visualizer'
;;
;; Version 0.1.2
;; * fixed keybindings
;; * renamed `undo-tree-visualizer-switch-previous-branch' and
;; `undo-tree-visualizer-switch-next-branch' to
;; `undo-tree-visualizer-switch-branch-left' and
;; `undo-tree-visualizer-switch-branch-right'
;;
;; Version 0.1.1
;; * prevented `undo-tree-kill-visualizer' from killing visualizer when
;; undoing/redoing from the visualizer, which completely broke the
;; visualizer!
;; * changed one redo binding, so that at least one set of undo/redo bindings
;; works in a terminal
;; * bound vertical scrolling commands in `undo-tree-visualizer-map', in case
;; they aren't bound globally
;; * added missing :group argument to `defface's
;;
;; Version 0.1
;; * initial release
;;; Code:
(eval-when-compile (require 'cl))
;; `characterp' isn't defined in Emacs versions <= 22
(unless (fboundp 'characterp)
(defalias 'characterp 'char-valid-p))
;; `region-active-p' isn't defined in Emacs versions <= 22
(unless (fboundp 'region-active-p)
(defun region-active-p () (and transient-mark-mode mark-active)))
;;; =====================================================================
;;; Global variables and customization options
(defvar buffer-undo-tree nil
"Tree of undo entries in current buffer.")
(make-variable-buffer-local 'buffer-undo-tree)
(defgroup undo-tree nil
"Tree undo/redo."
:group 'undo)
(defcustom undo-tree-mode-lighter " Undo-Tree"
"Lighter displayed in mode line
when `undo-tree-mode' is enabled."
:group 'undo-tree
:type 'string)
(defcustom undo-tree-incompatible-major-modes nil
"List of major-modes in which `undo-tree-mode' should not be enabled.
\(See `turn-on-undo-tree-mode'.\)"
:group 'undo-tree
:type '(repeat symbol))
(defcustom undo-tree-visualizer-spacing 3
"Horizontal spacing in undo-tree visualization.
Must be a postivie odd integer."
:group 'undo-tree
:type '(integer
:match (lambda (w n) (and (integerp n) (> n 0) (= (mod n 2) 1)))))
(make-variable-buffer-local 'undo-tree-visualizer-spacing)
(defvar undo-tree-map nil
"Keymap used in undo-tree-mode.")
(defface undo-tree-visualizer-default-face
'((((class color)) :foreground "gray"))
"*Face used to draw undo-tree in visualizer."
:group 'undo-tree)
(defface undo-tree-visualizer-current-face
'((((class color)) :foreground "red"))
"*Face used to highlight current undo-tree node in visualizer."
:group 'undo-tree)
(defface undo-tree-visualizer-active-branch-face
'((((class color) (background dark))
(:foreground "white" :weight bold))
(((class color) (background light))
(:foreground "black" :weight bold)))
"*Face used to highlight active undo-tree branch
in visualizer."
:group 'undo-tree)
(defface undo-tree-visualizer-register-face
'((((class color)) :foreground "yellow"))
"*Face used to highlight undo-tree nodes saved to a register
in visualizer."
:group 'undo-tree)
(defvar undo-tree-visualizer-map nil
"Keymap used in undo-tree visualizer.")
(defvar undo-tree-visualizer-selection-map nil
"Keymap used in undo-tree visualizer selection mode.")
(defvar undo-tree-visualizer-parent-buffer nil
"Parent buffer in visualizer.")
(make-variable-buffer-local 'undo-tree-visualizer-parent-buffer)
(defvar undo-tree-visualizer-timestamps nil
"Non-nil when visualizer is displaying time-stamps.")
(make-variable-buffer-local 'undo-tree-visualizer-timestamps)
(defconst undo-tree-visualizer-buffer-name " *undo-tree*")
;; prevent debugger being called on "No further redo information"
(add-to-list 'debug-ignored-errors "^No further redo information")
;;; =================================================================
;;; Setup default keymaps
(unless undo-tree-map
(setq undo-tree-map (make-sparse-keymap))
;; remap `undo' and `undo-only' to `undo-tree-undo'
(define-key undo-tree-map [remap undo] 'undo-tree-undo)
(define-key undo-tree-map [remap undo-only] 'undo-tree-undo)
;; bind standard undo bindings (since these match redo counterparts)
(define-key undo-tree-map (kbd "C-/") 'undo-tree-undo)
(define-key undo-tree-map "\C-_" 'undo-tree-undo)
;; redo doesn't exist normally, so define our own keybindings
(define-key undo-tree-map (kbd "C-?") 'undo-tree-redo)
(define-key undo-tree-map (kbd "M-_") 'undo-tree-redo)
;; just in case something has defined `redo'...
(define-key undo-tree-map [remap redo] 'undo-tree-redo)
;; we use "C-x u" for the undo-tree visualizer
(define-key undo-tree-map (kbd "\C-x u") 'undo-tree-visualize)
;; bind register commands
(define-key undo-tree-map (kbd "C-x r u")
'undo-tree-save-state-to-register)
(define-key undo-tree-map (kbd "C-x r U")
'undo-tree-restore-state-from-register))
(unless undo-tree-visualizer-map
(setq undo-tree-visualizer-map (make-keymap))
;; vertical motion keys undo/redo
(define-key undo-tree-visualizer-map [remap previous-line]
'undo-tree-visualize-undo)
(define-key undo-tree-visualizer-map [remap next-line]
'undo-tree-visualize-redo)
(define-key undo-tree-visualizer-map [up]
'undo-tree-visualize-undo)
(define-key undo-tree-visualizer-map "p"
'undo-tree-visualize-undo)
(define-key undo-tree-visualizer-map "\C-p"
'undo-tree-visualize-undo)
(define-key undo-tree-visualizer-map [down]
'undo-tree-visualize-redo)
(define-key undo-tree-visualizer-map "n"
'undo-tree-visualize-redo)
(define-key undo-tree-visualizer-map "\C-n"
'undo-tree-visualize-redo)
;; horizontal motion keys switch branch
(define-key undo-tree-visualizer-map [remap forward-char]
'undo-tree-visualize-switch-branch-right)
(define-key undo-tree-visualizer-map [remap backward-char]
'undo-tree-visualize-switch-branch-left)
(define-key undo-tree-visualizer-map [right]
'undo-tree-visualize-switch-branch-right)
(define-key undo-tree-visualizer-map "f"
'undo-tree-visualize-switch-branch-right)
(define-key undo-tree-visualizer-map "\C-f"
'undo-tree-visualize-switch-branch-right)
(define-key undo-tree-visualizer-map [left]
'undo-tree-visualize-switch-branch-left)
(define-key undo-tree-visualizer-map "b"
'undo-tree-visualize-switch-branch-left)
(define-key undo-tree-visualizer-map "\C-b"
'undo-tree-visualize-switch-branch-left)
;; mouse sets buffer state to node at click
(define-key undo-tree-visualizer-map [mouse-1]
'undo-tree-visualizer-mouse-set)
;; toggle timestamps
(define-key undo-tree-visualizer-map "t"
'undo-tree-visualizer-toggle-timestamps)
;; selection mode
(define-key undo-tree-visualizer-map "s"
'undo-tree-visualizer-selection-mode)
;; horizontal scrolling may be needed if the tree is very wide
(define-key undo-tree-visualizer-map ","
'undo-tree-visualizer-scroll-left)
(define-key undo-tree-visualizer-map "."
'undo-tree-visualizer-scroll-right)
(define-key undo-tree-visualizer-map "<"
'undo-tree-visualizer-scroll-left)
(define-key undo-tree-visualizer-map ">"
'undo-tree-visualizer-scroll-right)
;; vertical scrolling may be needed if the tree is very tall
(define-key undo-tree-visualizer-map [next] 'scroll-up)
(define-key undo-tree-visualizer-map [prior] 'scroll-down)
;; quit visualizer
(define-key undo-tree-visualizer-map "q"
'undo-tree-visualizer-quit)
(define-key undo-tree-visualizer-map "\C-q"
'undo-tree-visualizer-quit))
(unless undo-tree-visualizer-selection-map
(setq undo-tree-visualizer-selection-map (make-keymap))
;; vertical motion keys move up and down tree
(define-key undo-tree-visualizer-selection-map [remap previous-line]
'undo-tree-visualizer-select-previous)
(define-key undo-tree-visualizer-selection-map [remap next-line]
'undo-tree-visualizer-select-next)
(define-key undo-tree-visualizer-selection-map [up]
'undo-tree-visualizer-select-previous)
(define-key undo-tree-visualizer-selection-map "p"
'undo-tree-visualizer-select-previous)
(define-key undo-tree-visualizer-selection-map "\C-p"
'undo-tree-visualizer-select-previous)
(define-key undo-tree-visualizer-selection-map [down]
'undo-tree-visualizer-select-next)
(define-key undo-tree-visualizer-selection-map "n"
'undo-tree-visualizer-select-next)
(define-key undo-tree-visualizer-selection-map "\C-n"
'undo-tree-visualizer-select-next)
;; vertical scroll keys move up and down quickly
(define-key undo-tree-visualizer-selection-map [next]
(lambda () (interactive) (undo-tree-visualizer-select-next 10)))
(define-key undo-tree-visualizer-selection-map [prior]
(lambda () (interactive) (undo-tree-visualizer-select-previous 10)))
;; horizontal motion keys move to left and right siblings
(define-key undo-tree-visualizer-selection-map [remap forward-char]
'undo-tree-visualizer-select-right)
(define-key undo-tree-visualizer-selection-map [remap backward-char]
'undo-tree-visualizer-select-left)
(define-key undo-tree-visualizer-selection-map [right]
'undo-tree-visualizer-select-right)
(define-key undo-tree-visualizer-selection-map "f"
'undo-tree-visualizer-select-right)
(define-key undo-tree-visualizer-selection-map "\C-f"
'undo-tree-visualizer-select-right)
(define-key undo-tree-visualizer-selection-map [left]
'undo-tree-visualizer-select-left)
(define-key undo-tree-visualizer-selection-map "b"
'undo-tree-visualizer-select-left)
(define-key undo-tree-visualizer-selection-map "\C-b"
'undo-tree-visualizer-select-left)
;; horizontal scroll keys move left or right quickly
(define-key undo-tree-visualizer-selection-map ","
(lambda () (interactive) (undo-tree-visualizer-select-left 10)))
(define-key undo-tree-visualizer-selection-map "."
(lambda () (interactive) (undo-tree-visualizer-select-right 10)))
(define-key undo-tree-visualizer-selection-map "<"
(lambda () (interactive) (undo-tree-visualizer-select-left 10)))
(define-key undo-tree-visualizer-selection-map ">"
(lambda () (interactive) (undo-tree-visualizer-select-right 10)))
;; mouse or <enter> sets buffer state to node at point/click
(define-key undo-tree-visualizer-selection-map "\r"
'undo-tree-visualizer-set)
(define-key undo-tree-visualizer-selection-map [mouse-1]
'undo-tree-visualizer-mouse-set)
;; toggle timestamps
(define-key undo-tree-visualizer-selection-map "t"
'undo-tree-visualizer-toggle-timestamps)
;; quit visualizer selection mode
(define-key undo-tree-visualizer-selection-map "s"
'undo-tree-visualizer-mode)
;; quit visualizer
(define-key undo-tree-visualizer-selection-map "q"
'undo-tree-visualizer-quit)
(define-key undo-tree-visualizer-selection-map "\C-q"
'undo-tree-visualizer-quit))
;;; =====================================================================
;;; Undo-tree data structure
(defstruct
(undo-tree
:named
(:constructor nil)
(:constructor make-undo-tree
(&aux
(root (make-undo-tree-node nil nil))
(current root)
(size 0)
(object-pool (make-hash-table :test 'eq :weakness 'value))))
(:copier nil))
root current size object-pool)
(defstruct
(undo-tree-node
(:type vector) ; create unnamed struct
(:constructor nil)
(:constructor make-undo-tree-node
(previous undo
&optional redo
&aux
(timestamp (current-time))
(branch 0)))
(:constructor make-undo-tree-node-backwards
(next-node undo
&optional redo
&aux
(next (list next-node))
(timestamp (current-time))
(branch 0)))
(:copier nil))
previous next undo redo timestamp branch meta-data)