-
Notifications
You must be signed in to change notification settings - Fork 2
/
undo-tree.el
4287 lines (3806 loc) · 164 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 -*- lexical-binding: t; -*-
;; Copyright (C) 2009-2012 Free Software Foundation, Inc
;; Author: Toby Cubitt <[email protected]>
;; Version: 0.6.3
;; Keywords: convenience, files, undo, redo, history, tree
;; URL: http://www.dr-qubit.org/emacs.php
;; Repository: http://www.dr-qubit.org/git/undo-tree.git
;; This file is 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 24 and CVS. It should
;; work in Emacs versions 22 and 23 too, but 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 to 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.
;;
;; C-<up> M-{ (`undo-tree-visualize-undo-to-x')
;; Undo changes up to last branch point.
;;
;; C-<down> M-} (`undo-tree-visualize-redo-to-x')
;; Redo changes down to next branch point.
;;
;; <down> n C-n (`undo-tree-visualize-redo')
;; Redo changes.
;;
;; <mouse-1> (`undo-tree-visualizer-mouse-set')
;; Set state to node at mouse click.
;;
;; t (`undo-tree-visualizer-toggle-timestamps')
;; Toggle display of time-stamps.
;;
;; d (`undo-tree-visualizer-toggle-diff')
;; Toggle diff display.
;;
;; s (`undo-tree-visualizer-selection-mode')
;; Toggle keyboard selection mode.
;;
;; q (`undo-tree-visualizer-quit')
;; Quit undo-tree-visualizer.
;;
;; C-q (`undo-tree-visualizer-abort')
;; Abort undo-tree-visualizer.
;;
;; , <
;; Scroll left.
;;
;; . >
;; Scroll right.
;;
;; <pgup> M-v
;; Scroll up.
;;
;; <pgdown> C-v
;; Scroll down.
;;
;;
;;
;; In visualizer selection mode:
;;
;; <up> p C-p (`undo-tree-visualizer-select-previous')
;; Select previous node.
;;
;; <down> n C-n (`undo-tree-visualizer-select-next')
;; Select next node.
;;
;; <left> b C-b (`undo-tree-visualizer-select-left')
;; Select left sibling node.
;;
;; <right> f C-f (`undo-tree-visualizer-select-right')
;; Select right sibling node.
;;
;; <pgup> M-v
;; Select node 10 above.
;;
;; <pgdown> C-v
;; Select node 10 below.
;;
;; <enter> (`undo-tree-visualizer-set')
;; Set state to selected node and exit selection mode.
;;
;; s (`undo-tree-visualizer-mode')
;; Exit selection mode.
;;
;; t (`undo-tree-visualizer-toggle-timestamps')
;; Toggle display of time-stamps.
;;
;; d (`undo-tree-visualizer-toggle-diff')
;; Toggle diff display.
;;
;; q (`undo-tree-visualizer-quit')
;; Quit undo-tree-visualizer.
;;
;; C-q (`undo-tree-visualizer-abort')
;; Abort undo-tree-visualizer.
;;
;; , <
;; Scroll left.
;;
;; . >
;; Scroll right.
;;
;;
;;
;; Persistent undo history:
;;
;; Note: Requires a recent development version of Emacs checked out out from
;; the Emacs bzr repository. All stable versions of Emacs currently
;; break this feature.
;;
;; `undo-tree-auto-save-history' (variable)
;; automatically save and restore undo-tree history along with buffer
;; (disabled by default)
;;
;; `undo-tree-save-history' (command)
;; manually save undo history to file
;;
;; `undo-tree-load-history' (command)
;; manually load undo history from file
;;
;;
;;
;; Compressing undo history:
;;
;; Undo history files cannot grow beyond the maximum undo tree size, which
;; is limited by `undo-limit', `undo-strong-limit' and
;; `undo-outer-limit'. Nevertheless, undo history files can grow quite
;; large. If you want to automatically compress undo history, add the
;; following advice to your .emacs file (replacing ".gz" with the filename
;; extension of your favourite compression algorithm):
;;
;; (defadvice undo-tree-make-history-save-file-name
;; (after undo-tree activate)
;; (setq concat ad-return-value ".gz"))
;;
;;
;;
;;
;; 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 you can toggle the display of time-stamps (by hitting "t") and
;; a diff of the undo changes (by hitting "d"). (There's one other tiny
;; difference: the visualizer puts the most recent branch on the left rather
;; than the right.)
;;
;; Bring up the undo tree visualizer whenever you want by hitting "C-x u".
;;
;; 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").
;;
;; 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.
;;
;; You can also select nodes directly using the keyboard, by hitting "s" to
;; toggle selection mode. The usual motion keys now allow you to move around
;; the tree without changing the parent buffer. Hitting <enter> will reset the
;; state of the parent buffer to the state represented by the currently
;; selected 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.)
;;
;; To get some idea of what changes are represented by a given node in the
;; tree, it can be useful to see a diff of the changes. Hit "d" in the
;; visualizer to toggle a diff display. This normally displays a diff between
;; the current state and the previous one, i.e. it shows you the changes that
;; will be applied if you undo (move up the tree). However, the diff display
;; really comes into its own in the visualizer's selection mode (see above),
;; where it instead shows a diff between the current state and the currently
;; selected state, i.e. it shows you the changes that will be applied if you
;; reset to the selected state.
;;
;; (Note that the diff is generated by the Emacs `diff' command, and is
;; displayed using `diff-mode'. See the corresponding customization groups if
;; you want to customize the diff display.)
;;
;; Finally, hitting "q" will quit the visualizer, leaving the parent buffer in
;; whatever state you ended at. Hitting "C-q" will abort the visualizer,
;; returning the parent buffer to whatever state it was originally in when the
;; visualizer was .
;;
;;
;;
;; 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 be 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.
;;; Code:
(eval-when-compile (require 'cl))
(require 'diff)
;;; =====================================================================
;;; Compatibility hacks for older Emacsen
;; `characterp' isn't defined in Emacs versions < 23
(unless (fboundp 'characterp)
(defalias 'characterp 'char-valid-p))
;; `region-active-p' isn't defined in Emacs versions < 23
(unless (fboundp 'region-active-p)
(defun region-active-p () (and transient-mark-mode mark-active)))
;; `registerv' defstruct isn't defined in Emacs versions < 24
(unless (fboundp 'registerv-make)
(defmacro registerv-make (data &rest _dummy) data))
(unless (fboundp 'registerv-data)
(defmacro registerv-data (data) data))
;; `diff-no-select' and `diff-file-local-copy' aren't defined in Emacs
;; versions < 24 (copied and adapted from Emacs 24)
(unless (fboundp 'diff-no-select)
(defun diff-no-select (old new &optional switches no-async buf)
;; Noninteractive helper for creating and reverting diff buffers
(unless (bufferp new) (setq new (expand-file-name new)))
(unless (bufferp old) (setq old (expand-file-name old)))
(or switches (setq switches diff-switches)) ; If not specified, use default.
(unless (listp switches) (setq switches (list switches)))
(or buf (setq buf (get-buffer-create "*Diff*")))
(let* ((old-alt (diff-file-local-copy old))
(new-alt (diff-file-local-copy new))
(command
(mapconcat 'identity
`(,diff-command
;; Use explicitly specified switches
,@switches
,@(mapcar #'shell-quote-argument
(nconc
(when (or old-alt new-alt)
(list "-L" (if (stringp old)
old (prin1-to-string old))
"-L" (if (stringp new)
new (prin1-to-string new))))
(list (or old-alt old)
(or new-alt new)))))
" "))
(thisdir default-directory))
(with-current-buffer buf
(setq buffer-read-only t)
(buffer-disable-undo (current-buffer))
(let ((inhibit-read-only t))
(erase-buffer))
(buffer-enable-undo (current-buffer))
(diff-mode)
(set (make-local-variable 'revert-buffer-function)
(lambda (_ignore-auto _noconfirm)
(diff-no-select old new switches no-async (current-buffer))))
(setq default-directory thisdir)
(let ((inhibit-read-only t))
(insert command "\n"))
(if (and (not no-async) (fboundp 'start-process))
(let ((proc (start-process "Diff" buf shell-file-name
shell-command-switch command)))
(set-process-filter proc 'diff-process-filter)
(set-process-sentinel
proc (lambda (proc _msg)
(with-current-buffer (process-buffer proc)
(diff-sentinel (process-exit-status proc))
(if old-alt (delete-file old-alt))
(if new-alt (delete-file new-alt))))))
;; Async processes aren't available.
(let ((inhibit-read-only t))
(diff-sentinel
(call-process shell-file-name nil buf nil
shell-command-switch command))
(if old-alt (delete-file old-alt))
(if new-alt (delete-file new-alt)))))
buf)))
(unless (fboundp 'diff-file-local-copy)
(defun diff-file-local-copy (file-or-buf)
(if (bufferp file-or-buf)
(with-current-buffer file-or-buf
(let ((tempfile (make-temp-file "buffer-content-")))
(write-region nil nil tempfile nil 'nomessage)
tempfile))
(file-local-copy file-or-buf))))
;;; =====================================================================
;;; Global variables and customization options
(defvar buffer-undo-tree nil
"Tree of undo entries in current buffer.")
(make-variable-buffer-local 'buffer-undo-tree)
(put 'buffer-undo-tree 'permanent-local t)
(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 '(term-mode)
"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-enable-undo-in-region t
"When non-nil, enable undo-in-region.
When undo-in-region is enabled, undoing or redoing when the
region is active (in `transient-mark-mode') or with a prefix
argument (not in `transient-mark-mode') only undoes changes
within the current region."
:group 'undo-tree
:type 'boolean)
(defcustom undo-tree-auto-save-history nil
"When non-nil, `undo-tree-mode' will save undo history to file
when a buffer is saved to file.
It will automatically load undo history when a buffer is loaded
from file, if an undo save file exists.
Undo-tree history is saved to a file called
\".<buffer-file-name>.~undo-tree\" in the same directory as the
file itself.
WARNING! `undo-tree-auto-save-history' will not work properly in
Emacs versions prior to 24.3, so it cannot be enabled via
the customization interface in versions earlier than that one. To
ignore this warning and enable it regardless, set
`undo-tree-auto-save-history' to a non-nil value outside of
customize."
:group 'undo-tree
:type (if (version-list-< (version-to-list emacs-version) '(24 3))
'(choice (const :tag "<disabled>" nil))
'boolean))
(defcustom undo-tree-history-directory-alist nil
"Alist of filename patterns and undo history directory names.
Each element looks like (REGEXP . DIRECTORY). Undo history for
files with names matching REGEXP will be saved in DIRECTORY.
DIRECTORY may be relative or absolute. If it is absolute, so
that all matching files are backed up into the same directory,
the file names in this directory will be the full name of the
file backed up with all directory separators changed to `!' to
prevent clashes. This will not work correctly if your filesystem
truncates the resulting name.
For the common case of all backups going into one directory, the
alist should contain a single element pairing \".\" with the
appropriate directory name.
If this variable is nil, or it fails to match a filename, the
backup is made in the original file's directory.
On MS-DOS filesystems without long names this variable is always
ignored."
:group 'undo-tree
:type '(repeat (cons (regexp :tag "Regexp matching filename")
(directory :tag "Undo history directory name"))))
(defcustom undo-tree-visualizer-relative-timestamps t
"When non-nil, display times relative to current time
when displaying time stamps in visualizer.
Otherwise, display absolute times."
:group 'undo-tree
:type 'boolean)
(defcustom undo-tree-visualizer-timestamps nil
"When non-nil, display time-stamps by default
in undo-tree visualizer.
\\<undo-tree-visualizer-map>You can always toggle time-stamps on and off \
using \\[undo-tree-visualizer-toggle-timestamps], regardless of the
setting of this variable."
:group 'undo-tree
:type 'boolean)
(defcustom undo-tree-visualizer-diff nil
"When non-nil, display diff by default in undo-tree visualizer.
\\<undo-tree-visualizer-map>You can always toggle the diff display \
using \\[undo-tree-visualizer-toggle-diff], regardless of the
setting of this variable."
:group 'undo-tree
:type 'boolean)
(defcustom undo-tree-visualizer-lazy-drawing 100
"When non-nil, use lazy undo-tree drawing in visualizer.
Setting this to a number causes the visualizer to switch to lazy
drawing when the number of nodes in the tree is larger than this
value.
Lazy drawing means that only the visible portion of the tree will
be drawn initially , and the tree will be extended later as
needed. For the most part, the only visible effect of this is to
significantly speed up displaying the visualizer for very large
trees.
There is one potential negative effect of lazy drawing. Other
branches of the tree will only be drawn once the node from which
they branch off becomes visible. So it can happen that certain
portions of the tree that would be shown with lazy drawing
disabled, will not be drawn immediately when it is
enabled. However, this effect is quite rare in practice."
:group 'undo-tree
:type '(choice (const :tag "never" nil)
(const :tag "always" t)
(integer :tag "> size")))
(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)
(defface undo-tree-visualizer-unmodified-face
'((((class color)) :foreground "cyan"))