-
Notifications
You must be signed in to change notification settings - Fork 17
/
vdiff.el
2421 lines (2213 loc) · 91.8 KB
/
vdiff.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
;;; vdiff.el --- A diff tool similar to vimdiff -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2018 Free Software Foundation, Inc.
;; Author: Justin Burkett <[email protected]>
;; Maintainer: Justin Burkett <[email protected]>
;; URL: https://github.com/justbur/emacs-vdiff
;; Version: 0.2.4
;; Keywords: diff
;; Package-Requires: ((emacs "24.4") (hydra "0.13.0"))
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; News:
;; Version 0.2
;; * First ELPA Release
;;; Commentary:
;; A tool like vimdiff for Emacs
;; vdiff compares two or three buffers on the basis of the output from the diff
;; tool. The buffers are kept synchronized so that as you move through one of
;; the buffers the top of the active buffer aligns with the corresponding top of
;; the other buffer(s). This is similar to how ediff works, but in ediff you use
;; a third "control buffer" to move through the diffed buffers. The key
;; difference is that in vdiff you are meant to actively edit one of the buffers
;; and the display will update automatically for the other buffer. Similar to
;; ediff, vdiff provides commands to "send" and "receive" hunks from one buffer
;; to the other as well as commands to traverse the diff hunks, which are useful
;; if you are trying to merge changes. In contrast to ediff, vdiff also provides
;; folding capabilities to fold sections of the buffers that don't contain
;; changes. This folding occurs automatically. Finally, you are encouraged to
;; bind a key to `vdiff-hydra/body', which will use hydra.el (in ELPA) to create
;; a convenient transient keymap containing most of the useful vdiff commands.
;; This functionality is all inspired by (but not equivalent to) the vimdiff
;; tool from vim.
;; See https://github.com/justbur/emacs-vdiff for more information
;;; Code:
(require 'cl-lib)
(eval-when-compile (require 'subr-x))
(require 'diff-mode)
(require 'hydra)
(require 'smerge-mode)
(defvar vdiff-mode)
(defvar vdiff-3way-mode)
(defvar vdiff-debug nil)
(defgroup vdiff nil
"Diff tool that is like vimdiff"
:tag "Vdiff"
:group 'tools)
(defcustom vdiff-lock-scrolling t
"Whether to lock scrolling by default when starting
`vdiff-mode'."
:type 'boolean)
(defcustom vdiff-truncate-lines t
"If non-nil, use `toggle-truncate-lines' in vdiff buffers."
:type 'boolean)
(defcustom vdiff-diff-algorithms
'((diff . "diff -u")
(diff-minimal . "diff -u --minimal")
(git-diff . "git --no-pager diff --no-index --no-color")
(git-diff-myers . "git --no-pager diff --myers --no-index --no-color")
(git-diff-minimal . "git --no-pager diff --minimal --no-index --no-color")
(git-diff-patience . "git --no-pager diff --patience --no-index --no-color")
(git-diff-histogram . "git --no-pager diff --histogram --no-index --no-color")
(custom . "diff -u"))
"An alist containing choices of diff algorithms to be selected
by setting `vdiff-diff-algorithm'. If you want to use a custom
command, set `vidff-diff-algorithm' to `custom' and customize the
`custom' key in this alist."
:type '(alist :key-type symbol :value-type string))
(defcustom vdiff-diff-algorithm 'diff
"Choice of algorithm for generating diffs. The choices are
`diff', `diff-minimal', `git-diff',`git-diff-myers',
`git-diff-minimal', `git-diff-patience', `git-diff-histogram' and
`custom'. See `vdiff-diff-algorithms' for the associated
commands."
:type '(choice (const :tag "diff -u" diff-u)
(const :tag "git diff" git-diff)
(const :tag "git diff --myers" git-diff-myers)
(const :tag "git diff --minimal" git-diff-minimal)
(const :tag "git diff --patience" git-diff-patience)
(const :tag "git diff --histogram" git-diff-histogram)
(const :tag "custom" custom)))
(defcustom vdiff-diff3-command '("diff3")
"diff3 command to use. Specify as a list where the car is the command to use
and the remaining elements are the arguments to the command."
:type '(repeat string))
(make-obsolete-variable 'vdiff-diff-program 'vdiff-diff-algorithm "2018-04-17")
(make-obsolete-variable 'vdiff-diff3-program 'vdiff-diff3-command "2018-04-17")
(make-obsolete-variable 'vdiff-diff-extra-args "See `vdiff-diff-algorithms'." "2018-04-17")
(make-obsolete-variable 'vdiff-diff3-extra-args 'vdiff-diff3-command "2018-04-17")
(defcustom vdiff-disable-folding nil
"If non-nil, disable folding in vdiff buffers."
:type 'boolean)
(defcustom vdiff-fold-padding 6
"Unchanged lines to leave unfolded around a fold"
:type 'integer)
(defcustom vdiff-min-fold-size 4
"Minimum number of lines to fold"
:type 'integer)
(defcustom vdiff-may-close-fold-on-point t
"If non-nil, allow closing new folds around point after updates."
:type 'boolean)
(defcustom vdiff-fold-string-function #'vdiff-fold-string-default
"Function that returns the string printed for a closed
fold. The arguments passed are the number of lines folded, the
text on the first line, and the width of the buffer."
:type 'function)
(defcustom vdiff-default-refinement-syntax-code "w"
"Default syntax table class code to use for identifying
\"words\" in \`vdiff-refine-this-hunk'. Some useful options are
\"w\" (default) words
\"w_\" symbols \(words plus symbol constituents\)
For more information see
https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Class-Table.html"
:type 'string)
(defcustom vdiff-auto-refine nil
"If non-nil, automatically refine all hunks."
:type 'boolean)
(defcustom vdiff-only-highlight-refinements nil
"If non-nil, apply faces to refined words but not hunks."
:type 'boolean)
(defcustom vdiff-subtraction-style 'full
"How to represent subtractions (i.e., deleted lines). The
default is full which means add the same number of (fake) lines
as those that were removed. The choice single means add only one
fake line. The choice fringe means don't add lines but do
indicate the subtraction location in the fringe."
:type '(radio (const :tag "Add same number of fake lines" full)
(const :tag "Add single line" single)
(const :tag "Add no lines but use fringe" fringe)))
(defcustom vdiff-subtraction-fill-char ?-
"Character to use for filling subtraction lines. See also
`vdiff-subtraction-style'."
:type 'integer)
(defcustom vdiff-use-ancestor-as-merge-buffer nil
"When in a merge conflict file and text from the ancestor file
is included, `vdiff-merge-conflict' will use the ancestor file as
the merge buffer (or target buffer) that will be saved when the
merge is finished. The default is to show the original file with
conflicts as the merge buffer."
:type 'boolean)
(defface vdiff-addition-face
'((t :inherit diff-added))
"Face for additions")
(defface vdiff-change-face
'((t :inherit diff-changed))
"Face for changes")
(defface vdiff-closed-fold-face
'((t :inherit region))
"Face for closed folds")
(defface vdiff-open-fold-face
'((t))
"Face for open folds")
(defface vdiff-subtraction-face
'((t :inherit diff-removed))
"Face for subtractions")
(defface vdiff-subtraction-fringe-face
'((t :inherit vdiff-subtraction-face))
"Face for subtraction fringe indicators")
(defface vdiff-refine-changed
'((t :inherit diff-refine-changed))
"Face for word changes within a change hunk")
(defface vdiff-refine-added
'((t :inherit diff-refine-added))
"Face for word changes within an addition hunk")
(defface vdiff-target-face
'((t :inverse-video t :inherit warning))
"Face for selecting hunk targets.")
(defvar vdiff--force-sync-commands '(next-line
previous-line
beginning-of-buffer
end-of-buffer)
"Commands that trigger sync in other buffer. There should not
be a need to include commands that scroll the buffer here,
because those are handled differently.")
(defvar vdiff--diff-code-regexp
"^\\([0-9]+\\),?\\([0-9]+\\)?\\([adc]\\)\\([0-9]+\\),?\\([0-9]+\\)?")
(defvar vdiff--diff3-code-regexp
"^\\([1-3]\\):\\([0-9]+\\),?\\([0-9]+\\)?\\([adc]\\)")
(defvar vdiff--inhibit-window-switch nil)
(defvar vdiff--inhibit-diff-update nil)
(defvar vdiff--in-scroll-hook nil)
(defvar vdiff--cleanup-hook nil)
;; (defvar vdiff--in-post-command-hook nil)
(defvar vdiff--setting-vscroll nil)
(defvar vdiff--after-change-timer nil)
(defvar vdiff--after-change-refresh-delay 1)
(defvar vdiff--new-command nil)
(defvar vdiff--last-command nil)
(defvar vdiff--case-options
'(("Don't ignore case" . "")
("Ignore case (-i)" . "-i")))
(defvar vdiff--whitespace-options
'(("Don't ignore whitespace" . "")
("Ignore all whitespace (-w)" . "-w")
("Ignore space changes (-b)" . "-b")
("Ignore blank lines (-B)" . "-B")))
(defvar vdiff--testing-mode nil
"Configure for testing in batch mode.")
;; Sessions
(defvar vdiff--temp-session nil
"Temporarily stores new vdiff session globally.")
(defvar-local vdiff--session nil
"Holds reference to local vdiff session in each vdiff buffer.")
(put 'vdiff--session 'permanent-local t)
(cl-defstruct vdiff-session
;; buffers
buffers
process-buffer
word-diff-output-buffer
;; data
diff-data
line-maps
folds
all-folds-open
diff-stale
;; other
window-config
case-args
whitespace-args
;; Quit hooks
on-quit
prior-window-config
kill-buffers-on-quit)
;; * Utilities
(defsubst vdiff-diff-command ()
(let ((cmd-cons (assoc vdiff-diff-algorithm vdiff-diff-algorithms)))
(if (stringp (cdr-safe cmd-cons))
(split-string (cdr cmd-cons) " ")
'("diff" "-u"))))
(defun vdiff--maybe-int (str)
"Return an int>=0 from STR."
(let ((num (or (and (numberp str) str)
(and str (string-to-number str)))))
(when (and (numberp num)
(>= num 0))
num)))
(defun vdiff--non-nil-list (&rest args)
"Make ARGS into list and remove nils."
(delq nil (apply #'list args)))
(defun vdiff--maybe-list (str)
"Return a list with STR as the sole element, or an empty list if STR is empty."
(if (string= str "") '() (list str)))
(defun vdiff--buffer-a-p ()
(when (and
vdiff--session
(eq (current-buffer)
(car (vdiff-session-buffers vdiff--session))))
(current-buffer)))
(defun vdiff--buffer-b-p ()
(when (and
vdiff--session
(eq (current-buffer)
(cadr (vdiff-session-buffers vdiff--session))))
(current-buffer)))
(defun vdiff--buffer-c-p ()
(when (and
vdiff--session
(eq (current-buffer)
(nth 2 (vdiff-session-buffers vdiff--session))))
(current-buffer)))
(defun vdiff--buffer-p ()
"Non-nil if in any vdiff buffer"
(cond ((vdiff--buffer-a-p) 'a)
((vdiff--buffer-b-p) 'b)
((vdiff--buffer-c-p) 'c)))
(defun vdiff--unselected-buffers ()
(cl-remove-if
(lambda (buf) (or (eq buf (current-buffer))
(not (buffer-live-p buf))))
(vdiff-session-buffers vdiff--session)))
(defun vdiff--unselected-windows ()
(mapcar (lambda (buf) (get-buffer-window buf 0))
(vdiff--unselected-buffers)))
(defun vdiff--all-windows ()
(remq nil
(mapcar (lambda (buf) (get-buffer-window buf 0))
(vdiff-session-buffers vdiff--session))))
(defun vdiff--all-overlays (ovr)
(overlay-get ovr 'vdiff-hunk-overlays))
(defun vdiff--other-overlays (ovr)
(remq ovr (vdiff--all-overlays ovr)))
(defun vdiff--overlay-marker (ovr)
(let ((current (eq (current-buffer) (overlay-buffer ovr))))
(propertize
(format "%s%s\n"
(1+
(cl-position
(overlay-buffer ovr)
(vdiff-session-buffers vdiff--session)))
(if current " (to all) " ""))
'face 'vdiff-target-face)))
(defun vdiff--add-overlay-marker (ovr)
(overlay-put ovr 'before-string
(concat (vdiff--overlay-marker ovr)
(overlay-get ovr 'before-string))))
(defun vdiff--remove-overlay-marker (ovr)
(overlay-put ovr 'before-string
(substring
(overlay-get ovr 'before-string)
(length (vdiff--overlay-marker ovr)))))
(defun vdiff--read-3way-target (ovr &optional just-one)
"Read a target overlay when sending or receiving a hunk from
one buffer to another. Only applies in 3-way diffs."
(when vdiff-3way-mode
(let* ((all-ovrs (vdiff--all-overlays ovr))
(other-ovrs (remq ovr all-ovrs))
(this-idx (cl-position ovr all-ovrs))
(marked-ovrs (if just-one other-ovrs all-ovrs))
target)
(unwind-protect
(progn
(mapc #'vdiff--add-overlay-marker marked-ovrs)
(setq target (1- (string-to-number
(char-to-string
(read-char "Select target: ")))))
(cond ((or (not (member target (list 0 1 2)))
(and just-one (= target this-idx)))
(user-error "Invalid target"))
((= target this-idx)
(message "all others %s %s" target this-idx)
other-ovrs)
(t
(message "just %s" (nth target all-ovrs))
(list (nth target all-ovrs)))))
(mapc #'vdiff--remove-overlay-marker marked-ovrs)))))
(defun vdiff--target-overlays (this-ovr &optional just-one)
(when (and (overlayp this-ovr)
(overlay-get this-ovr 'vdiff))
(let ((3way-target (vdiff--read-3way-target this-ovr just-one))
(other-ovrs (vdiff--other-overlays this-ovr)))
(cond ((and vdiff-3way-mode
3way-target)
(cl-remove-if
(lambda (ovr)
(or (eq ovr this-ovr)
(null (member ovr 3way-target))))
other-ovrs))
(vdiff-3way-mode
(user-error "vdiff: No target overlay"))
(t
(remq this-ovr other-ovrs))))))
(defun vdiff--move-to-line (n)
(goto-char (point-min))
(forward-line (1- n)))
(defun vdiff--overlay-at-pos (&optional pos noerror)
"Return first vdiff overlay found at POS which defaults to
point.
If NOERROR is non-nil, don't signal an error when no overlay is
found."
(let ((pos (or pos (point)))
ovr)
(setq ovr
(catch 'yes
(dolist (ovr (overlays-at pos))
(when (overlay-get ovr 'vdiff-type)
(throw 'yes ovr)))))
(if (or ovr noerror)
ovr
(user-error "No vdiff overlay found here."))))
(defun vdiff--hunk-at-point-p ()
"Non-nil if point is in hunk overlay.
Returns overlay."
(let ((ovr (vdiff--overlay-at-pos nil t)))
(and (overlayp ovr)
(overlay-get ovr 'vdiff-type)
(not (eq (overlay-get ovr 'vdiff-type) 'fold))
ovr)))
(defun vdiff--fold-at-point-p ()
"Non-nil if point is in fold overlay.
Returns overlay."
(let ((ovr (vdiff--overlay-at-pos nil t)))
(and (overlayp ovr)
(overlay-get ovr 'vdiff-type)
(eq (overlay-get ovr 'vdiff-type) 'fold)
ovr)))
(defun vdiff--overlays-in-region (beg end)
"Return any vdiff overlays found within BEG and END."
(let (ovrs)
(dolist (ovr (overlays-in beg end))
(when (overlay-get ovr 'vdiff-type)
(push ovr ovrs)))
(nreverse ovrs)))
(defun vdiff--maybe-exit-overlay (&optional up no-fold)
"Move point out of any vdiff overlays. Move down unless UP is
non-nil. Ignore folds if NO-FOLD is non-nil."
(let* ((ovr (vdiff--overlay-at-pos nil t))
(type (when ovr (overlay-get ovr 'vdiff-type))))
(when (and type
(or (not no-fold)
(not (eq type 'fold))))
(goto-char
(if up
(1- (overlay-start ovr))
(1+ (overlay-end ovr)))))))
(defmacro vdiff--with-all-buffers (&rest body)
"Execute BODY in all vdiff buffers."
`(dolist (buf (vdiff-session-buffers vdiff--session))
(when (buffer-live-p buf)
(with-current-buffer buf
,@body))))
;; * Toggles
(defun vdiff-toggle-case (command-line-arg)
"Toggle ignoring of case in diff command."
(interactive
(list (cdr-safe
(assoc-string
(completing-read "Case options: "
vdiff--case-options)
vdiff--case-options))))
(setf (vdiff-session-case-args vdiff--session)
(vdiff--maybe-list command-line-arg))
(when vdiff-mode
(vdiff-refresh)))
(defun vdiff-toggle-whitespace (command-line-arg)
"Toggle ignoring of whitespace in diff command."
(interactive
(list (cdr-safe
(assoc-string
(completing-read "Whitespace options: "
vdiff--whitespace-options)
vdiff--whitespace-options))))
(setf (vdiff-session-whitespace-args vdiff--session)
(vdiff--maybe-list command-line-arg))
(when vdiff-mode
(vdiff-refresh)))
;; * Main overlay refresh routine
(defun vdiff-refresh (&optional post-refresh-function)
"Refresh diff information.
POST-REFRESH-FUNCTION is called when the process finishes."
(interactive)
(when (vdiff--buffer-p)
(let* ((tmp-a (make-temp-file "vdiff-a-"))
(tmp-b (make-temp-file "vdiff-b-"))
(tmp-c (when vdiff-3way-mode
(make-temp-file "vdiff-c-")))
(base-cmd (if vdiff-3way-mode
vdiff-diff3-command
(vdiff-diff-command)))
(ses vdiff--session)
(cmd (append
base-cmd
(vdiff-session-whitespace-args ses)
(unless (string= (car base-cmd) "git")
(vdiff-session-case-args ses))
(list "--" tmp-a tmp-b)
(when tmp-c
(list tmp-c))))
(buffers (vdiff-session-buffers ses))
(proc-buf (vdiff-session-process-buffer ses))
(proc (get-buffer-process proc-buf)))
(setq vdiff--last-command cmd)
(with-current-buffer (car buffers)
(write-region nil nil tmp-a nil 'quietly)
;; ensure tmp file ends in newline
(when (or (= (point-min) (point-max))
(/= (char-before (point-max)) ?\n))
(message "vdiff: Warning %s does not end in a newline."
(if buffer-file-name buffer-file-name (buffer-name)))
(write-region "\n" nil tmp-a t 'quietly)))
(with-current-buffer (cadr buffers)
(write-region nil nil tmp-b nil 'quietly)
;; ensure tmp file ends in newline
(when (or (= (point-min) (point-max))
(/= (char-before (point-max)) ?\n))
(message "vdiff: Warning %s does not end in a newline."
(if buffer-file-name buffer-file-name (buffer-name)))
(write-region "\n" nil tmp-b t 'quietly)))
(when vdiff-3way-mode
(with-current-buffer (nth 2 buffers)
(write-region nil nil tmp-c nil 'quietly)
;; ensure tmp file ends in newline
(when (or (= (point-min) (point-max))
(/= (char-before (point-max)) ?\n))
(message "vdiff: Warning %s does not end in a newline."
(if buffer-file-name buffer-file-name (buffer-name)))
(write-region "\n" nil tmp-c t 'quietly))))
(when proc
(kill-process proc))
(with-current-buffer (get-buffer-create proc-buf)
(erase-buffer))
(if vdiff--testing-mode
(progn
(apply #'call-process (car cmd) nil (list proc-buf) nil (cdr cmd))
(vdiff--diff-refresh-sync-sentinel
proc-buf ses vdiff-3way-mode tmp-a tmp-b
tmp-c post-refresh-function))
(setq proc
(make-process
:name "*vdiff*"
:buffer proc-buf
:command cmd))
(when vdiff-3way-mode
(process-put proc 'vdiff-3way t))
(process-put proc 'vdiff-session ses)
(process-put proc 'vdiff-tmp-a tmp-a)
(process-put proc 'vdiff-tmp-b tmp-b)
(process-put proc 'vdiff-post-refresh-function post-refresh-function)
(when tmp-c
(process-put proc 'vdiff-tmp-c tmp-c))
(set-process-sentinel proc #'vdiff--diff-refresh-async-sentinel)))))
(defun vdiff--encode-range (insert beg &optional end)
"Normalize BEG and END of range. INSERT indicates that this is
an addition when compared to other vdiff buffers."
(let* ((beg (vdiff--maybe-int beg))
(end (vdiff--maybe-int end)))
(cond ((and end insert)
(error "vdiff: multi-line range for a or d code"))
(insert
(cons (1+ beg) nil))
(t
(cons beg (or end beg))))))
(defun vdiff--parse-diff (buf)
"Parse diff output in BUF and return list of hunks."
(let (res)
(with-current-buffer buf
(goto-char (point-min))
(while (re-search-forward vdiff--diff-code-regexp nil t)
(let* ((code (match-string 3)))
(push
(cl-case (string-to-char code)
(?a (list (vdiff--encode-range
t (match-string 1))
(vdiff--encode-range
nil (match-string 4) (match-string 5))))
(?d (list (vdiff--encode-range
nil (match-string 1) (match-string 2))
(vdiff--encode-range
t (match-string 4))))
(?c (list (vdiff--encode-range
nil (match-string 1) (match-string 2))
(vdiff--encode-range
nil (match-string 4) (match-string 5))))
(t (error "vdiff: Unexpected code in parse-diff")))
res))))
(nreverse res)))
(defsubst vdiff--inc-lines (lines)
(forward-line)
(let ((a (car lines))
(b (cdr lines)))
(cond ((or (looking-at-p " ") (eobp)) (cons (1+ a) (1+ b)))
((looking-at-p "+") (cons a (1+ b)))
((looking-at-p "-") (cons (1+ a) b)))))
(defun vdiff--parse-diff-u (buf)
"Parse diff -u output in BUF and return list of hunks."
(let ((header-regexp "^@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@")
res)
(with-current-buffer buf
(goto-char (point-min))
(while (re-search-forward header-regexp nil t)
(forward-line)
(let* ((start-line-a (string-to-number (match-string 1)))
(start-line-b (string-to-number (match-string 2)))
(lines (cons start-line-a start-line-b)))
;; Adjust starting line in case it's not actually a line of one of the
;; files
(when (looking-at-p "+")
(setcar lines (1- (car lines))))
(when (looking-at-p "-")
(setcdr lines (1- (cdr lines))))
(while (and (not (looking-at-p "@"))
(not (eobp)))
(cond ((looking-at-p "+")
;; addition
(let ((beg-b (cdr lines)))
(while (looking-at-p "+")
(setq lines (vdiff--inc-lines lines)))
(when vdiff-debug
(cl-assert (or (looking-at-p " ") (eobp))))
(push
(list (cons (car lines) nil)
(cons beg-b (1- (cdr lines))))
res)))
((looking-at-p "-")
;; subtraction or change
(let ((beg-a (car lines)))
(while (looking-at-p "-")
(setq lines (vdiff--inc-lines lines)))
(if (or (looking-at-p " ") (eobp))
;; subtraction
(push
(list (cons beg-a (1- (car lines)))
(cons (cdr lines) nil))
res)
(when vdiff-debug
(cl-assert (or (looking-at-p "+") (eobp))))
(let ((beg-b (cdr lines)))
(while (looking-at-p "+")
(setq lines (vdiff--inc-lines lines)))
(when vdiff-debug
(cl-assert (or (looking-at-p " ") (eobp))))
(push
(list (cons beg-a (1- (car lines)))
(cons beg-b (1- (cdr lines))))
res)))))
(t
(setq lines (vdiff--inc-lines lines))))))))
(nreverse res)))
(defun vdiff--parse-diff3 (buf)
"Parse diff3 output in BUF and return list of hunks."
(catch 'final-res
(let (res)
(with-current-buffer buf
(goto-char (point-min))
(let (a-el b-el c-el)
(while t
(cond ((looking-at vdiff--diff3-code-regexp)
(let* ((file (string-to-number
(match-string-no-properties 1)))
(code (match-string-no-properties 4))
(range (vdiff--encode-range
(string= code "a")
(match-string-no-properties 2)
(match-string-no-properties 3))))
(cl-case file
(1 (setq a-el range))
(2 (setq b-el range))
(3 (setq c-el range)))))
((and a-el
(looking-at-p "^===="))
(push (list a-el b-el c-el) res)
(setq a-el nil)
(setq b-el nil)
(setq c-el nil))
((eobp)
(when (or a-el b-el)
(push (list a-el b-el c-el) res))
(throw 'final-res (nreverse res))))
(forward-line 1)))))))
(defun vdiff--diff-refresh-finish
(session tmp-a tmp-b &optional tmp-c post-function)
"Final step in diff refresh."
(vdiff--refresh-overlays session)
(vdiff--refresh-line-maps session)
(let ((vdiff--session session))
(when vdiff-auto-refine
(vdiff-refine-all-hunks))
(when post-function
(funcall post-function)))
(delete-file tmp-a)
(delete-file tmp-b)
(when tmp-c
(delete-file tmp-c))
(setf (vdiff-session-diff-stale session) nil))
(defun vdiff--diff-refresh-sync-sentinel
(buffer session vdiff-3way tmp-a tmp-b &optional tmp-c post-function)
"This is the sentinel for `vdiff-refresh' when
`vdiff--testing-mode' is non-nil."
(unless vdiff--inhibit-diff-update
(setf (vdiff-session-diff-data session)
(funcall (if vdiff-3way
#'vdiff--parse-diff3
#'vdiff--parse-diff-u) buffer))
(vdiff--diff-refresh-finish
session tmp-a tmp-b tmp-c post-function)))
(defun vdiff--diff-refresh-async-sentinel (proc event)
"This is the sentinel for `vdiff-refresh'. It does the job of
parsing the diff output and triggering the overlay updates."
(unless vdiff--inhibit-diff-update
(let ((parse-func (if (process-get proc 'vdiff-3way)
#'vdiff--parse-diff3
#'vdiff--parse-diff-u))
(session (process-get proc 'vdiff-session))
finished)
(cond
;; Was getting different exit code conventions depending on the
;; version of diff used
((or (string= "finished\n" event)
(string= "exited abnormally with code 1\n" event))
(setf (vdiff-session-diff-data session)
(funcall parse-func (process-buffer proc)))
(setq finished t))
((string-match-p "exited abnormally with code" event)
(setf (vdiff-session-diff-data session) nil)
(setq finished t)
(message "vdiff process error: %s" event)))
(when finished
(vdiff--diff-refresh-finish
session
(process-get proc 'vdiff-tmp-a)
(process-get proc 'vdiff-tmp-b)
(process-get proc 'vdiff-tmp-c)
(process-get proc 'vdiff-post-refresh-function))))))
(defun vdiff--remove-all-overlays ()
"Remove all vdiff overlays in both vdiff buffers."
(when (vdiff--buffer-p)
(vdiff--with-all-buffers
(remove-overlays (point-min) (point-max) 'vdiff t))))
(defun vdiff-save-buffers ()
"Save all vdiff buffers."
(interactive)
(vdiff--with-all-buffers (save-buffer)))
;; * Word diffs
(defun vdiff--overlay-to-words (&optional ovr syntax-code)
"Convert OVR to string of \"words\", one per line."
(let* ((ovr (or ovr (vdiff--overlay-at-pos)))
(word-syn (or syntax-code
vdiff-default-refinement-syntax-code))
(not-word-syn (concat "^" word-syn))
last-word-end buf-syntax ovr-text)
(with-current-buffer (overlay-buffer ovr)
(setq buf-syntax (syntax-table))
(setq ovr-text (buffer-substring-no-properties
(overlay-start ovr)
(overlay-end ovr))))
(with-temp-buffer
(set-syntax-table buf-syntax)
(insert ovr-text)
(goto-char (point-min))
(skip-syntax-forward not-word-syn)
(delete-region (point-min) (point))
(while (not (eobp))
(skip-syntax-forward word-syn)
(insert "\n")
(setq last-word-end (point))
(skip-syntax-forward not-word-syn)
(delete-region last-word-end (point)))
(buffer-string))))
(defun vdiff--diff-words (this-ovr other-ovr &optional syntax-code)
"Diff \"words\" between THIS-OVR and OTHER-OVR"
(when (and (eq (overlay-get this-ovr 'vdiff-type) 'change)
(overlayp other-ovr))
(let* ((a-words (vdiff--overlay-to-words this-ovr syntax-code))
(b-words (vdiff--overlay-to-words other-ovr syntax-code))
(tmp-file-a (make-temp-file "vdiff-word-a-"))
(tmp-file-b (make-temp-file "vdiff-word-b-"))
(out-buffer (get-buffer-create
(vdiff-session-word-diff-output-buffer
vdiff--session)))
(a-result '())
(b-result '()))
(write-region a-words nil tmp-file-a nil 'quietly)
(write-region b-words nil tmp-file-b nil 'quietly)
(with-current-buffer out-buffer (erase-buffer))
(let ((exit-code (apply #'call-process
(car (vdiff-diff-command))
nil out-buffer nil tmp-file-a tmp-file-b
(cdr (vdiff-diff-command)))))
(delete-file tmp-file-a)
(delete-file tmp-file-b)
(when (= exit-code 1)
(with-current-buffer out-buffer
(goto-char (point-min))
(while (re-search-forward vdiff--diff-code-regexp nil t)
(let ((a-change (list (string-to-number (match-string 1))))
(b-change (list (string-to-number (match-string 4)))))
(forward-line 1)
(while (and (not (eobp))
(not (looking-at-p vdiff--diff-code-regexp)))
(cond ((looking-at-p "^<")
(push (buffer-substring-no-properties
(+ 2 (point)) (line-end-position))
a-change))
((looking-at-p "^>")
(push (buffer-substring-no-properties
(+ 2 (point)) (line-end-position))
b-change)))
(forward-line 1))
(when (cdr a-change)
(push (nreverse a-change) a-result))
(when (cdr b-change)
(push (nreverse b-change) b-result))))
(cons (nreverse a-result) (nreverse b-result))))))))
(defun vdiff-refine-this-hunk (&optional syntax-code ovr)
"Highlight word differences in current hunk.
This uses `vdiff-default-refinement-syntax-code' for the
definition of a \"word\", unless one is provided using
SYNTAX-CODE."
(interactive (list vdiff-default-refinement-syntax-code
(vdiff--overlay-at-pos)))
(let* ((ovr (or ovr (vdiff--overlay-at-pos)))
(target-ovr (car (vdiff--target-overlays ovr)))
(word-syn (or syntax-code
vdiff-default-refinement-syntax-code))
(not-word-syn (concat "^" word-syn))
(type (overlay-get ovr 'vdiff-type))
(face (if (eq type 'addition)
'vdiff-refine-added
'vdiff-refine-changed))
instructions ovr-ins)
(if (fboundp 'smerge-refine-regions)
(when (and ovr target-ovr)
(smerge-refine-regions
(with-current-buffer (overlay-buffer ovr)
(copy-marker (overlay-start ovr)))
(overlay-end ovr)
(with-current-buffer (overlay-buffer target-ovr)
(copy-marker (overlay-start target-ovr)))
(overlay-end target-ovr)
`((face . ,face)
(vdiff . t)
(vdiff-refinement . t))))
(when (and ovr
target-ovr
(consp (setq instructions
(vdiff--diff-words ovr target-ovr))))
(dolist (curr-ovr (vdiff--all-overlays ovr))
(setq ovr-ins (if (eq curr-ovr ovr)
(car instructions)
(cdr instructions)))
(with-current-buffer (overlay-buffer curr-ovr)
(save-excursion
(let ((current-word-n 1))
(goto-char (overlay-start curr-ovr))
(skip-syntax-forward not-word-syn)
(dolist (ins ovr-ins)
(dotimes (_ (- (car ins) current-word-n))
(skip-syntax-forward word-syn)
(skip-syntax-forward not-word-syn))
(setq current-word-n (car ins))
(let* ((words (cdr ins))
(word-ovr
(make-overlay
(point)
(progn
(dotimes (_ (length words))
(skip-syntax-forward not-word-syn)
(skip-syntax-forward word-syn))
(point)))))
(cl-incf current-word-n (length words))
(overlay-put word-ovr 'vdiff t)
(overlay-put word-ovr 'face face)
(overlay-put word-ovr 'vdiff-refinement t)
(skip-syntax-forward not-word-syn)))))))))
(when vdiff-only-highlight-refinements
(when ovr
(overlay-put ovr 'face nil))
(when target-ovr
(overlay-put target-ovr 'face nil)))))
;; Not working yet
;; (defun vdiff-refine-this-hunk-whitespace (ovr)
;; "Highlight whitespace differences in current hunk."
;; (interactive (list (vdiff--overlay-at-pos)))
;; (vdiff-refine-this-hunk "-" ovr))
(defun vdiff-refine-this-hunk-symbol (ovr)
"Highlight symbol differences in current hunk."
(interactive (list (vdiff--overlay-at-pos)))
(vdiff-refine-this-hunk "w_" ovr))
(defun vdiff-refine-this-hunk-word (ovr)
"Highlight word differences in current hunk."
(interactive (list (vdiff--overlay-at-pos)))
(vdiff-refine-this-hunk "w" ovr))
(defun vdiff-remove-refinements-in-hunk (ovr)
"Remove any refinement overlays in the hunk overlay OVR."
(interactive (list (vdiff--overlay-at-pos)))
(dolist (chg-ovr (vdiff--all-overlays ovr))
(with-current-buffer (overlay-buffer chg-ovr)
(dolist (sub-ovr (overlays-in
(overlay-start chg-ovr)
(overlay-end chg-ovr)))
(when (overlay-get sub-ovr 'vdiff-refinement)
(delete-overlay sub-ovr))))
(when vdiff-only-highlight-refinements
(cl-case (overlay-get chg-ovr 'vdiff-type)
(addition (overlay-put chg-ovr 'face 'vdiff-addition-face))
(change (overlay-put chg-ovr 'face 'vdiff-change-face))))))
(defun vdiff-refine-all-hunks (&optional syntax-code)
"Highlight word differences in all hunks.
This uses `vdiff-default-refinement-syntax-code' for the
definition of a \"word\", unless one is provided using
SYNTAX-CODE.
See `vdiff-default-refinement-syntax-code' to change the definition
of a \"word\"."
(interactive)
;; Doesn't work for diff3 yet
(when (vdiff--buffer-p)
(dolist (ovr (overlays-in (point-min) (point-max)))
(vdiff-refine-this-hunk syntax-code ovr))))
;; Not working yet
;; (defun vdiff-refine-all-hunks-whitespace ()
;; "Highlight whitespace differences in all hunks."
;; (interactive)
;; (vdiff-refine-all-hunks "-"))
(defun vdiff-refine-all-hunks-symbol ()
"Highlight symbol differences in all hunks."
(interactive)
(vdiff-refine-all-hunks "w_"))
(defun vdiff-refine-all-hunks-word ()
"Highlight word differences in all hunks."
(interactive)
(vdiff-refine-all-hunks "w"))
;; * Bitmaps
;; emacs-nox users don't have this function. There's probably a better solution
;; here, but this seems to work.
(unless (fboundp 'define-fringe-bitmap)
(defun define-fringe-bitmap (&rest _)