summaryrefslogtreecommitdiff
path: root/lisp/vc/ediff.el
blob: 837940c776374f3ac528d4610707fc3457c9b5d6 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
;;; ediff.el --- a comprehensive visual interface to diff & patch

;; Copyright (C) 1994-2015 Free Software Foundation, Inc.

;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
;; Created: February 2, 1994
;; Keywords: comparing, merging, patching, vc, tools, unix
;; Version: 2.81.4

;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
;; file on 20/3/2008, and the maintainer agreed that when a bug is
;; filed in the Emacs bug reporting system against this file, a copy
;; of the bug report be sent to the maintainer's email address.

(defconst ediff-version "2.81.5" "The current version of Ediff")
(defconst ediff-date "July 4, 2013" "Date of last update")


;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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:

;; Never read that diff output again!
;; Apply patch interactively!
;; Merge with ease!

;; This package provides a convenient way of simultaneous browsing through
;; the differences between a pair (or a triple) of files or buffers.  The
;; files being compared, file-A, file-B, and file-C (if applicable) are
;; shown in separate windows (side by side, one above the another, or in
;; separate frames), and the differences are highlighted as you step
;; through them.  You can also copy difference regions from one buffer to
;; another (and recover old differences if you change your mind).

;; Ediff also supports merging operations on files and buffers, including
;; merging using ancestor versions.  Both comparison and merging operations can
;; be performed on directories, i.e., by pairwise comparison of files in those
;; directories.

;; In addition, Ediff can apply a patch to a file and then let you step
;; though both files, the patched and the original one, simultaneously,
;; difference-by-difference.  You can even apply a patch right out of a
;; mail buffer, i.e., patches received by mail don't even have to be saved.
;; Since Ediff lets you copy differences between buffers, you can, in
;; effect, apply patches selectively (i.e., you can copy a difference
;; region from file_orig to file, thereby undoing any particular patch that
;; you don't like).

;; Ediff is aware of version control, which lets the user compare
;; files with their older versions.  Ediff can also work with remote and
;; compressed files.  Details are given below.

;; Finally, Ediff supports directory-level comparison, merging and patching.
;; See the Ediff manual for details.

;; This package builds upon the ideas borrowed from emerge.el and several
;; Ediff's functions are adaptations from emerge.el.  Much of the functionality
;; Ediff provides is also influenced by emerge.el.

;; The present version of Ediff supersedes Emerge.  It provides a superior user
;; interface and has numerous major features not found in Emerge.  In
;; particular, it can do patching, and 2-way and 3-way file comparison,
;; merging, and directory operations.



;;; Bugs:

;;  1. The undo command doesn't restore deleted regions well.  That is, if
;;  you delete all characters in a difference region and then invoke
;;  `undo', the reinstated text will most likely be inserted outside of
;;  what Ediff thinks is the current difference region. (This problem
;;  doesn't seem to exist with XEmacs.)
;;
;;  If at any point you feel that difference regions are no longer correct,
;;  you can hit '!' to recompute the differences.

;;  2. On a monochrome display, the repertoire of faces with which to
;;  highlight fine differences is limited.  By default, Ediff is using
;;  underlining.  However, if the region is already underlined by some other
;;  overlays, there is no simple way to temporarily remove that residual
;;  underlining.  This problem occurs when a buffer is highlighted with
;;  hilit19.el or font-lock.el packages.  If this residual highlighting gets
;;  in the way, you can do the following.  Both font-lock.el and hilit19.el
;;  provide commands for unhighlighting buffers.  You can either place these
;;  commands in `ediff-prepare-buffer-hook' (which will unhighlight every
;;  buffer used by Ediff) or you can execute them interactively, at any time
;;  and on any buffer.


;;; Acknowledgments:

;; Ediff was inspired by Dale R. Worley's <drw@math.mit.edu> emerge.el.
;; Ediff would not have been possible without the help and encouragement of
;; its many users.  See Ediff on-line Info for the full list of those who
;; helped.  Improved defaults in Ediff file-name reading commands.

;;; Code:

(provide 'ediff)

;; Compiler pacifier
(eval-and-compile
  (unless (fboundp 'declare-function) (defmacro declare-function (&rest  _r))))

(require 'ediff-util)
;; end pacifier

(require 'ediff-init)
(require 'ediff-mult)  ; required because of the registry stuff

(defgroup ediff nil
  "Comprehensive visual interface to `diff' and `patch'."
  :tag "Ediff"
  :group 'tools)


(defcustom ediff-use-last-dir nil
  "If t, Ediff will use previous directory as default when reading file name."
  :type 'boolean
  :group 'ediff)

;; Last directory used by an Ediff command for file-A.
(defvar ediff-last-dir-A nil)
;; Last directory used by an Ediff command for file-B.
(defvar ediff-last-dir-B nil)
;; Last directory used by an Ediff command for file-C.
(defvar ediff-last-dir-C nil)
;; Last directory used by an Ediff command for the ancestor file.
(defvar ediff-last-dir-ancestor nil)
;; Last directory used by an Ediff command as the output directory for merge.
(defvar ediff-last-merge-autostore-dir nil)


;; Used as a startup hook to set `_orig' patch file read-only.
(defun ediff-set-read-only-in-buf-A ()
  (ediff-with-current-buffer ediff-buffer-A
    (setq buffer-read-only t)))

(declare-function dired-get-filename "dired"
                  (&optional localp no-error-if-not-filep))
(declare-function dired-get-marked-files "dired"
                  (&optional localp arg filter distinguish-one-marked))

;; Return a plausible default for ediff's first file:
;; In dired, return the file number FILENO (or 0) in the list
;; (all-selected-files, filename under the cursor), where directories are
;; ignored. Otherwise, return DEFAULT file name, if non-nil. Else,
;; if the buffer is visiting a file, return that file name.
(defun ediff-get-default-file-name (&optional default fileno)
  (cond ((eq major-mode 'dired-mode)
	 (let ((current (dired-get-filename nil 'no-error))
	       (marked (condition-case nil
			   (dired-get-marked-files 'no-dir)
			 (error nil)))
	       aux-list choices result)
	   (or (integerp fileno) (setq fileno 0))
	   (if (stringp default)
	       (setq aux-list (cons default aux-list)))
	   (if (and (stringp current) (not (file-directory-p current)))
	       (setq aux-list (cons current aux-list)))
	   (setq choices (nconc  marked aux-list))
	   (setq result (elt choices fileno))
	   (or result
	       default)))
	((stringp default) default)
	((buffer-file-name (current-buffer))
	 (file-name-nondirectory (buffer-file-name (current-buffer))))
	))

;;; Compare files/buffers

;;;###autoload
(defun ediff-files (file-A file-B &optional startup-hooks)
  "Run Ediff on a pair of files, FILE-A and FILE-B."
  (interactive
   (let ((dir-A (if ediff-use-last-dir
		    ediff-last-dir-A
		  default-directory))
	 dir-B f)
     (list (setq f (ediff-read-file-name
		    "File A to compare"
		    dir-A
		    (ediff-get-default-file-name)
		    'no-dirs))
	   (ediff-read-file-name "File B to compare"
				 (setq dir-B
				       (if ediff-use-last-dir
					   ediff-last-dir-B
					 (file-name-directory f)))
				 (progn
				   (ediff-add-to-history
				    'file-name-history
				    (ediff-abbreviate-file-name
				     (expand-file-name
				      (file-name-nondirectory f)
				      dir-B)))
				   (ediff-get-default-file-name f 1)))
	   )))
  (ediff-files-internal file-A
			(if (file-directory-p file-B)
			    (expand-file-name
			     (file-name-nondirectory file-A) file-B)
			  file-B)
			nil ; file-C
			startup-hooks
			'ediff-files))

;;;###autoload
(defun ediff-files3 (file-A file-B file-C &optional startup-hooks)
  "Run Ediff on three files, FILE-A, FILE-B, and FILE-C."
  (interactive
   (let ((dir-A (if ediff-use-last-dir
		    ediff-last-dir-A
		  default-directory))
	 dir-B dir-C f ff)
     (list (setq f (ediff-read-file-name
		    "File A to compare"
		    dir-A
		    (ediff-get-default-file-name)
		    'no-dirs))
	   (setq ff (ediff-read-file-name "File B to compare"
					  (setq dir-B
						(if ediff-use-last-dir
						    ediff-last-dir-B
						  (file-name-directory f)))
					  (progn
					    (ediff-add-to-history
					     'file-name-history
					     (ediff-abbreviate-file-name
					      (expand-file-name
					       (file-name-nondirectory f)
					       dir-B)))
					    (ediff-get-default-file-name f 1))))
	   (ediff-read-file-name "File C to compare"
				 (setq dir-C (if ediff-use-last-dir
						 ediff-last-dir-C
					       (file-name-directory ff)))
				 (progn
				   (ediff-add-to-history
				    'file-name-history
				    (ediff-abbreviate-file-name
				     (expand-file-name
				      (file-name-nondirectory ff)
				      dir-C)))
				   (ediff-get-default-file-name ff 2)))
	   )))
  (ediff-files-internal file-A
			(if (file-directory-p file-B)
			    (expand-file-name
			     (file-name-nondirectory file-A) file-B)
			  file-B)
			(if (file-directory-p file-C)
			    (expand-file-name
			     (file-name-nondirectory file-A) file-C)
			  file-C)
			startup-hooks
			'ediff-files3))

;;;###autoload
(defalias 'ediff3 'ediff-files3)


(defun ediff-find-file (file-var buffer-name &optional last-dir hooks-var)
  "Visit FILE and arrange its buffer to Ediff's liking.
FILE-VAR is actually a variable symbol whose value must contain a true
file name.
BUFFER-NAME is a variable symbol, which will get the buffer object into
which FILE is read.
LAST-DIR is the directory variable symbol where FILE's
directory name should be returned.  HOOKS-VAR is a variable symbol that will
be assigned the hook to be executed after `ediff-startup' is finished.
`ediff-find-file' arranges that the temp files it might create will be
deleted."
  (let* ((file (symbol-value file-var))
	 (file-magic (ediff-filename-magic-p file))
	 (temp-file-name-prefix (file-name-nondirectory file)))
    (cond ((not (file-readable-p file))
	   (error "File `%s' does not exist or is not readable" file))
	  ((file-directory-p file)
	   (error "File `%s' is a directory" file)))

    ;; some of the commands, below, require full file name
    (setq file (expand-file-name file))

    ;; Record the directory of the file
    (if last-dir
	(set last-dir (expand-file-name (file-name-directory file))))

    ;; Setup the buffer
    (set buffer-name (find-file-noselect file))

    (ediff-with-current-buffer (symbol-value buffer-name)
      (widen) ; Make sure the entire file is seen
      (cond (file-magic  ;   file has a handler, such as jka-compr-handler or
	     		 ;;; ange-ftp-hook-function--arrange for temp file
	     (ediff-verify-file-buffer 'magic)
	     (setq file
		   (ediff-make-temp-file
		    (current-buffer) temp-file-name-prefix))
	     (set hooks-var (cons `(lambda () (delete-file ,file))
				  (symbol-value hooks-var))))
	    ;; file processed via auto-mode-alist, a la uncompress.el
	    ((not (equal (file-truename file)
			 (file-truename (buffer-file-name))))
	     (setq file
		   (ediff-make-temp-file
		    (current-buffer) temp-file-name-prefix))
	     (set hooks-var (cons `(lambda () (delete-file ,file))
				  (symbol-value hooks-var))))
	    (t ;; plain file---just check that the file matches the buffer
	     (ediff-verify-file-buffer))))
    (set file-var file)))

;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
(defun ediff-files-internal (file-A file-B file-C startup-hooks job-name
				    &optional merge-buffer-file)
  (let (buf-A buf-B buf-C)
    (if (string= file-A file-B)
	(error "Files A and B are the same"))
    (if (stringp file-C)
	(or (and (string= file-A file-C) (error "Files A and C are the same"))
	    (and (string= file-B file-C) (error "Files B and C are the same"))))
    (message "Reading file %s ... " file-A)
    ;;(sit-for 0)
    (ediff-find-file 'file-A 'buf-A 'ediff-last-dir-A 'startup-hooks)
    (message "Reading file %s ... " file-B)
    ;;(sit-for 0)
    (ediff-find-file 'file-B 'buf-B 'ediff-last-dir-B 'startup-hooks)
    (if (stringp file-C)
	(progn
	  (message "Reading file %s ... " file-C)
	  ;;(sit-for 0)
	  (ediff-find-file
	   'file-C 'buf-C
	   (if (eq job-name 'ediff-merge-files-with-ancestor)
	       'ediff-last-dir-ancestor 'ediff-last-dir-C)
	   'startup-hooks)))
    (ediff-setup buf-A file-A
		 buf-B file-B
		 buf-C file-C
		 startup-hooks
		 (list (cons 'ediff-job-name job-name))
		 merge-buffer-file)))

(declare-function diff-latest-backup-file "diff" (fn))

;;;###autoload
(defalias 'ediff 'ediff-files)

;;;###autoload
(defun ediff-current-file ()
  "Start ediff between current buffer and its file on disk.
This command can be used instead of `revert-buffer'.  If there is
nothing to revert then this command fails."
  (interactive)
  ;; This duplicates code from menu-bar.el.
  (unless (or (not (eq revert-buffer-function 'revert-buffer--default))
              (not (eq revert-buffer-insert-file-contents-function
		       'revert-buffer-insert-file-contents--default-function))
              (and buffer-file-number
                   (or (buffer-modified-p)
                       (not (verify-visited-file-modtime
                             (current-buffer))))))
    (error "Nothing to revert"))
  (let* ((auto-save-p (and (recent-auto-save-p)
                           buffer-auto-save-file-name
                           (file-readable-p buffer-auto-save-file-name)
                           (y-or-n-p
                            "Buffer has been auto-saved recently.  Compare with auto-save file? ")))
         (file-name (if auto-save-p
                        buffer-auto-save-file-name
                      buffer-file-name))
         (revert-buf-name (concat "FILE=" file-name))
         (revert-buf (get-buffer revert-buf-name))
         (current-major major-mode))
    (unless file-name
      (error "Buffer does not seem to be associated with any file"))
    (when revert-buf
      (kill-buffer revert-buf)
      (setq revert-buf nil))
    (setq revert-buf (get-buffer-create revert-buf-name))
    (with-current-buffer revert-buf
      (insert-file-contents file-name)
      ;; Assume same modes:
      (funcall current-major))
    (ediff-buffers revert-buf (current-buffer))))


;;;###autoload
(defun ediff-backup (file)
  "Run Ediff on FILE and its backup file.
Uses the latest backup, if there are several numerical backups.
If this file is a backup, `ediff' it with its original."
  (interactive (list (read-file-name "Ediff (file with backup): ")))
  ;; The code is taken from `diff-backup'.
  (require 'diff)
  (let (bak ori)
    (if (backup-file-name-p file)
	(setq bak file
	      ori (file-name-sans-versions file))
      (setq bak (or (diff-latest-backup-file file)
		    (error "No backup found for %s" file))
	    ori file))
    (ediff-files bak ori)))

;;;###autoload
(defun ediff-buffers (buffer-A buffer-B &optional startup-hooks job-name)
  "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B."
  (interactive
   (let (bf)
     (list (setq bf (read-buffer "Buffer A to compare: "
				 (ediff-other-buffer "") t))
	   (read-buffer "Buffer B to compare: "
			(progn
			  ;; realign buffers so that two visible bufs will be
			  ;; at the top
			  (save-window-excursion (other-window 1))
			  (ediff-other-buffer bf))
			t))))
  (or job-name (setq job-name 'ediff-buffers))
  (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name))

;;;###autoload
(defalias 'ebuffers 'ediff-buffers)


;;;###autoload
(defun ediff-buffers3 (buffer-A buffer-B buffer-C
				 &optional startup-hooks job-name)
  "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C."
  (interactive
   (let (bf bff)
     (list (setq bf (read-buffer "Buffer A to compare: "
				 (ediff-other-buffer "") t))
	   (setq bff (read-buffer "Buffer B to compare: "
				  (progn
				    ;; realign buffers so that two visible
				    ;; bufs will be at the top
				    (save-window-excursion (other-window 1))
				    (ediff-other-buffer bf))
				  t))
	   (read-buffer "Buffer C to compare: "
				  (progn
				    ;; realign buffers so that three visible
				    ;; bufs will be at the top
				    (save-window-excursion (other-window 1))
				    (ediff-other-buffer (list bf bff)))
				  t)
	   )))
  (or job-name (setq job-name 'ediff-buffers3))
  (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name))

;;;###autoload
(defalias 'ebuffers3 'ediff-buffers3)



;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
(defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
				     &optional merge-buffer-file)
  (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A)))
	 (buf-B-file-name (buffer-file-name (get-buffer buf-B)))
	 (buf-C-is-alive (ediff-buffer-live-p buf-C))
	 (buf-C-file-name (if buf-C-is-alive
			      (buffer-file-name (get-buffer buf-B))))
	 file-A file-B file-C)
    (unwind-protect
	(progn
	  (if (not (ediff-buffer-live-p buf-A))
	      (error "Buffer %S doesn't exist" buf-A))
	  (if (not (ediff-buffer-live-p buf-B))
	      (error "Buffer %S doesn't exist" buf-B))
	  (let ((ediff-job-name job-name))
	    (if (and ediff-3way-comparison-job
		     (not buf-C-is-alive))
		(error "Buffer %S doesn't exist" buf-C)))
	  (if (stringp buf-A-file-name)
	      (setq buf-A-file-name (file-name-nondirectory buf-A-file-name)))
	  (if (stringp buf-B-file-name)
	      (setq buf-B-file-name (file-name-nondirectory buf-B-file-name)))
	  (if (stringp buf-C-file-name)
	      (setq buf-C-file-name (file-name-nondirectory buf-C-file-name)))

	  (setq file-A (ediff-make-temp-file buf-A buf-A-file-name)
		file-B (ediff-make-temp-file buf-B buf-B-file-name))
	  (if buf-C-is-alive
	      (setq file-C (ediff-make-temp-file buf-C buf-C-file-name)))

	  (ediff-setup (get-buffer buf-A) file-A
		       (get-buffer buf-B) file-B
		       (if buf-C-is-alive (get-buffer buf-C))
		       file-C
		       (cons `(lambda ()
				(delete-file ,file-A)
				(delete-file ,file-B)
				(if (stringp ,file-C) (delete-file ,file-C)))
			     startup-hooks)
		       (list (cons 'ediff-job-name job-name))
		       merge-buffer-file))
      (if (and (stringp file-A) (file-exists-p file-A))
	  (delete-file file-A))
      (if (and (stringp file-B) (file-exists-p file-B))
	  (delete-file file-B))
      (if (and (stringp file-C) (file-exists-p file-C))
	  (delete-file file-C)))))


;;; Directory and file group operations

;; Get appropriate default name for directory:
;; If ediff-use-last-dir, use ediff-last-dir-A.
;; In dired mode, use the directory that is under the point (if any);
;; otherwise, use default-directory
(defun ediff-get-default-directory-name ()
  (cond (ediff-use-last-dir ediff-last-dir-A)
	((eq major-mode 'dired-mode)
	 (let ((f (dired-get-filename nil 'noerror)))
	   (if (and (stringp f) (file-directory-p f))
	       f
	     default-directory)))
	(t default-directory)))


;;;###autoload
(defun ediff-directories (dir1 dir2 regexp)
  "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
the same name in both.  The third argument, REGEXP, is nil or a regular
expression; only file names that match the regexp are considered."
  (interactive
   (let ((dir-A (ediff-get-default-directory-name))
	 (default-regexp (eval ediff-default-filtering-regexp))
	 f)
     (list (setq f (read-directory-name
		    "Directory A to compare: " dir-A nil 'must-match))
	   (read-directory-name "Directory B to compare: "
			   (if ediff-use-last-dir
			       ediff-last-dir-B
			     (ediff-strip-last-dir f))
			   nil 'must-match)
	   (read-string
	    (if (stringp default-regexp)
		(format "Filter through regular expression (default %s): "
			 default-regexp)
	      "Filter through regular expression: ")
	    nil
	    'ediff-filtering-regexp-history
	    (eval ediff-default-filtering-regexp))
	   )))
  (ediff-directories-internal
   dir1 dir2 nil regexp 'ediff-files 'ediff-directories
   ))

;;;###autoload
(defalias 'edirs 'ediff-directories)


;;;###autoload
(defun ediff-directory-revisions (dir1 regexp)
  "Run Ediff on a directory, DIR1, comparing its files with their revisions.
The second argument, REGEXP, is a regular expression that filters the file
names.  Only the files that are under revision control are taken into account."
  (interactive
   (let ((dir-A (ediff-get-default-directory-name))
	 (default-regexp (eval ediff-default-filtering-regexp))
	 )
     (list (read-directory-name
	    "Directory to compare with revision:" dir-A nil 'must-match)
	   (read-string
	    (if (stringp default-regexp)
		(format "Filter through regular expression (default %s): "
			 default-regexp)
	      "Filter through regular expression: ")
	    nil
	    'ediff-filtering-regexp-history
	    (eval ediff-default-filtering-regexp))
	   )))
  (ediff-directory-revisions-internal
   dir1 regexp 'ediff-revision 'ediff-directory-revisions
   ))

;;;###autoload
(defalias 'edir-revisions 'ediff-directory-revisions)


;;;###autoload
(defun ediff-directories3 (dir1 dir2 dir3 regexp)
  "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
have the same name in all three.  The last argument, REGEXP, is nil or a
regular expression; only file names that match the regexp are considered."

  (interactive
   (let ((dir-A (ediff-get-default-directory-name))
	 (default-regexp (eval ediff-default-filtering-regexp))
	 f)
     (list (setq f (read-directory-name "Directory A to compare:" dir-A nil))
	   (setq f (read-directory-name "Directory B to compare:"
				   (if ediff-use-last-dir
				       ediff-last-dir-B
				     (ediff-strip-last-dir f))
				   nil 'must-match))
	   (read-directory-name "Directory C to compare:"
			   (if ediff-use-last-dir
			       ediff-last-dir-C
			     (ediff-strip-last-dir f))
			   nil 'must-match)
	   (read-string
	    (if (stringp default-regexp)
		(format "Filter through regular expression (default %s): "
			 default-regexp)
	      "Filter through regular expression: ")
	    nil
	    'ediff-filtering-regexp-history
	    (eval ediff-default-filtering-regexp))
	   )))
  (ediff-directories-internal
   dir1 dir2 dir3 regexp 'ediff-files3 'ediff-directories3
   ))

;;;###autoload
(defalias 'edirs3 'ediff-directories3)

;;;###autoload
(defun ediff-merge-directories (dir1 dir2 regexp &optional merge-autostore-dir)
  "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
the same name in both.  The third argument, REGEXP, is nil or a regular
expression; only file names that match the regexp are considered."
  (interactive
   (let ((dir-A (ediff-get-default-directory-name))
	 (default-regexp (eval ediff-default-filtering-regexp))
	 f)
     (list (setq f (read-directory-name "Directory A to merge:"
					dir-A nil 'must-match))
	   (read-directory-name "Directory B to merge:"
			   (if ediff-use-last-dir
			       ediff-last-dir-B
			     (ediff-strip-last-dir f))
			   nil 'must-match)
	   (read-string
	    (if (stringp default-regexp)
		(format "Filter through regular expression (default %s): "
			 default-regexp)
	      "Filter through regular expression: ")
	    nil
	    'ediff-filtering-regexp-history
	    (eval ediff-default-filtering-regexp))
	   )))
  (ediff-directories-internal
   dir1 dir2 nil regexp 'ediff-merge-files 'ediff-merge-directories
   nil merge-autostore-dir
   ))

;;;###autoload
(defalias 'edirs-merge 'ediff-merge-directories)

;;;###autoload
(defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
						   &optional
						   merge-autostore-dir)
  "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
Ediff merges files that have identical names in DIR1, DIR2.  If a pair of files
in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
without ancestor.  The fourth argument, REGEXP, is nil or a regular expression;
only file names that match the regexp are considered."
  (interactive
   (let ((dir-A (ediff-get-default-directory-name))
	 (default-regexp (eval ediff-default-filtering-regexp))
	 f)
     (list (setq f (read-directory-name "Directory A to merge:" dir-A nil))
	   (setq f (read-directory-name "Directory B to merge:"
				 (if ediff-use-last-dir
				     ediff-last-dir-B
				   (ediff-strip-last-dir f))
				 nil 'must-match))
	   (read-directory-name "Ancestor directory:"
				 (if ediff-use-last-dir
				     ediff-last-dir-C
				   (ediff-strip-last-dir f))
				 nil 'must-match)
	   (read-string
	    (if (stringp default-regexp)
		(format "Filter through regular expression (default %s): "
			 default-regexp)
	      "Filter through regular expression: ")
	    nil
	    'ediff-filtering-regexp-history
	    (eval ediff-default-filtering-regexp))
	   )))
  (ediff-directories-internal
   dir1 dir2 ancestor-dir regexp
   'ediff-merge-files-with-ancestor 'ediff-merge-directories-with-ancestor
   nil merge-autostore-dir
   ))

;;;###autoload
(defun ediff-merge-directory-revisions (dir1 regexp
					     &optional merge-autostore-dir)
  "Run Ediff on a directory, DIR1, merging its files with their revisions.
The second argument, REGEXP, is a regular expression that filters the file
names.  Only the files that are under revision control are taken into account."
  (interactive
   (let ((dir-A (ediff-get-default-directory-name))
	 (default-regexp (eval ediff-default-filtering-regexp))
	 )
     (list (read-directory-name
	    "Directory to merge with revisions:" dir-A nil 'must-match)
	   (read-string
	    (if (stringp default-regexp)
		(format "Filter through regular expression (default %s): "
			 default-regexp)
	      "Filter through regular expression: ")
	    nil
	    'ediff-filtering-regexp-history
	    (eval ediff-default-filtering-regexp))
	   )))
  (ediff-directory-revisions-internal
   dir1 regexp 'ediff-merge-revisions 'ediff-merge-directory-revisions
   nil merge-autostore-dir
   ))

;;;###autoload
(defalias 'edir-merge-revisions 'ediff-merge-directory-revisions)

;;;###autoload
(defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
							   &optional
							   merge-autostore-dir)
  "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
The second argument, REGEXP, is a regular expression that filters the file
names.  Only the files that are under revision control are taken into account."
  (interactive
   (let ((dir-A (ediff-get-default-directory-name))
	 (default-regexp (eval ediff-default-filtering-regexp))
	 )
     (list (read-directory-name
	    "Directory to merge with revisions and ancestors:"
	    dir-A nil 'must-match)
	   (read-string
	    (if (stringp default-regexp)
		(format "Filter through regular expression (default %s): "
			 default-regexp)
	      "Filter through regular expression: ")
	    nil
	    'ediff-filtering-regexp-history
	    (eval ediff-default-filtering-regexp))
	   )))
  (ediff-directory-revisions-internal
   dir1 regexp 'ediff-merge-revisions-with-ancestor
   'ediff-merge-directory-revisions-with-ancestor
   nil merge-autostore-dir
   ))

;;;###autoload
(defalias
  'edir-merge-revisions-with-ancestor
  'ediff-merge-directory-revisions-with-ancestor)

;;;###autoload
(defalias 'edirs-merge-with-ancestor 'ediff-merge-directories-with-ancestor)

;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
;; on a pair of directories (three directories, in case of ancestor).
;; The third argument, REGEXP, is nil or a regular expression;
;; only file names that match the regexp are considered.
;; JOBNAME is the symbol indicating the meta-job to be performed.
;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
(defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
					&optional startup-hooks
					merge-autostore-dir)
  (if (stringp dir3)
      (setq dir3 (if (file-directory-p dir3) dir3 (file-name-directory dir3))))

  (cond ((string= dir1 dir2)
	 (error "Directories A and B are the same: %s" dir1))
	((and (eq jobname 'ediff-directories3)
	      (string= dir1 dir3))
	 (error "Directories A and C are the same: %s" dir1))
	((and (eq jobname 'ediff-directories3)
	      (string= dir2 dir3))
	 (error "Directories B and C are the same: %s" dir1)))

  (if merge-autostore-dir
      (or (stringp merge-autostore-dir)
	  (error "%s: Directory for storing merged files must be a string"
		 jobname)))
  (let (;; dir-diff-struct is of the form (common-list diff-list)
	;; It is a structure where ediff-intersect-directories returns
	;; commonalities and differences among directories
	dir-diff-struct
	meta-buf)
    (if (and ediff-autostore-merges
	     (ediff-merge-metajob jobname)
	     (not merge-autostore-dir))
	(setq merge-autostore-dir
	      (read-directory-name "Save merged files in directory: "
			      (if ediff-use-last-dir
					ediff-last-merge-autostore-dir
				      (ediff-strip-last-dir dir1))
			      nil
			      'must-match)))
    ;; verify we are not merging into an orig directory
    (if merge-autostore-dir
	(cond ((and (stringp dir1) (string= merge-autostore-dir dir1))
	       (or (y-or-n-p
		    "Directory for saving merged files = Directory A.  Sure? ")
		   (error "Directory merge aborted")))
	      ((and (stringp dir2) (string= merge-autostore-dir dir2))
	       (or (y-or-n-p
		    "Directory for saving merged files = Directory B.  Sure? ")
		   (error "Directory merge aborted")))
	      ((and (stringp dir3) (string= merge-autostore-dir dir3))
	       (or (y-or-n-p
		    "Directory for saving merged files = Ancestor Directory.  Sure? ")
		   (error "Directory merge aborted")))))

    (setq dir-diff-struct (ediff-intersect-directories
			   jobname
			   regexp dir1 dir2 dir3 merge-autostore-dir))
    (setq startup-hooks
	  ;; this sets various vars in the meta buffer inside
	  ;; ediff-prepare-meta-buffer
	  (cons `(lambda ()
		   ;; tell what to do if the user clicks on a session record
		   (setq ediff-session-action-function (quote ,action))
		   ;; set ediff-dir-difference-list
		   (setq ediff-dir-difference-list
			 (cdr (quote ,dir-diff-struct))))
		startup-hooks))
    (setq meta-buf (ediff-prepare-meta-buffer
		    'ediff-filegroup-action
		    (car dir-diff-struct)
		    "*Ediff Session Group Panel"
		    'ediff-redraw-directory-group-buffer
		    jobname
		    startup-hooks))
    (ediff-show-meta-buffer meta-buf)
    ))

;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
;; files
(defun ediff-directory-revisions-internal (dir1 regexp action jobname
						&optional startup-hooks
						merge-autostore-dir)
  (setq dir1 (if (file-directory-p dir1) dir1 (file-name-directory dir1)))

  (if merge-autostore-dir
      (or (stringp merge-autostore-dir)
	  (error "%S: Directory for storing merged files must be a string"
		 jobname)))
  (let (file-list meta-buf)
    (if (and ediff-autostore-merges
	     (ediff-merge-metajob jobname)
	     (not merge-autostore-dir))
	(setq merge-autostore-dir
	      (read-directory-name "Save merged files in directory: "
			      (if ediff-use-last-dir
				  ediff-last-merge-autostore-dir
				(ediff-strip-last-dir dir1))
			      nil
			      'must-match)))
    ;; verify merge-autostore-dir != dir1
    (if (and merge-autostore-dir
	     (stringp dir1)
	     (string= merge-autostore-dir dir1))
	(or (y-or-n-p
	     "Directory for saving merged file = directory A.  Sure? ")
	    (error "Merge of directory revisions aborted")))

    (setq file-list
	  (ediff-get-directory-files-under-revision
	   jobname regexp dir1 merge-autostore-dir))
    (setq startup-hooks
	  ;; this sets various vars in the meta buffer inside
	  ;; ediff-prepare-meta-buffer
	  (cons `(lambda ()
		   ;; tell what to do if the user clicks on a session record
		   (setq ediff-session-action-function (quote ,action)))
		startup-hooks))
    (setq meta-buf (ediff-prepare-meta-buffer
		    'ediff-filegroup-action
		    file-list
		    "*Ediff Session Group Panel"
		    'ediff-redraw-directory-group-buffer
		    jobname
		    startup-hooks))
    (ediff-show-meta-buffer meta-buf)
    ))


;;; Compare regions and windows

;;;###autoload
(defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks)
  "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
With prefix argument, DUMB-MODE, or on a non-windowing display, works as
follows:
If WIND-A is nil, use selected window.
If WIND-B is nil, use window next to WIND-A."
  (interactive "P")
  (ediff-windows dumb-mode wind-A wind-B
		 startup-hooks 'ediff-windows-wordwise 'word-mode))

;;;###autoload
(defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks)
  "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
With prefix argument, DUMB-MODE, or on a non-windowing display, works as
follows:
If WIND-A is nil, use selected window.
If WIND-B is nil, use window next to WIND-A."
  (interactive "P")
  (ediff-windows dumb-mode wind-A wind-B
		 startup-hooks 'ediff-windows-linewise nil))

;; Compare WIND-A and WIND-B, which are selected by clicking.
;; With prefix argument, DUMB-MODE, or on a non-windowing display,
;; works as follows:
;; If WIND-A is nil, use selected window.
;; If WIND-B is nil, use window next to WIND-A.
(defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode)
  (if (or dumb-mode (not (ediff-window-display-p)))
      (setq wind-A (ediff-get-next-window wind-A nil)
	    wind-B (ediff-get-next-window wind-B wind-A))
    (setq wind-A (ediff-get-window-by-clicking wind-A nil 1)
	  wind-B (ediff-get-window-by-clicking wind-B wind-A 2)))

  (let ((buffer-A (window-buffer wind-A))
	(buffer-B (window-buffer wind-B))
	beg-A end-A beg-B end-B)

    (save-excursion
      (save-window-excursion
	(sit-for 0) ; sync before using window-start/end -- a precaution
	(select-window wind-A)
	(setq beg-A (window-start)
	      end-A (window-end))
	(select-window wind-B)
	(setq beg-B (window-start)
	      end-B (window-end))))
    (setq buffer-A
	  (ediff-clone-buffer-for-window-comparison
	   buffer-A wind-A "-Window.A-")
	  buffer-B
	  (ediff-clone-buffer-for-window-comparison
	   buffer-B wind-B "-Window.B-"))
    (ediff-regions-internal
     buffer-A beg-A end-A buffer-B beg-B end-B
     startup-hooks job-name word-mode nil)))


;;;###autoload
(defun ediff-regions-wordwise (buffer-A buffer-B &optional startup-hooks)
  "Run Ediff on a pair of regions in specified buffers.
Regions \(i.e., point and mark\) can be set in advance or marked interactively.
This function is effective only for relatively small regions, up to 200
lines.  For large regions, use `ediff-regions-linewise'."
  (interactive
   (let (bf)
     (list (setq bf (read-buffer "Region's A buffer: "
				 (ediff-other-buffer "") t))
	   (read-buffer "Region's B buffer: "
			(progn
			  ;; realign buffers so that two visible bufs will be
			  ;; at the top
			  (save-window-excursion (other-window 1))
			  (ediff-other-buffer bf))
			t))))
  (if (not (ediff-buffer-live-p buffer-A))
      (error "Buffer %S doesn't exist" buffer-A))
  (if (not (ediff-buffer-live-p buffer-B))
      (error "Buffer %S doesn't exist" buffer-B))


  (let ((buffer-A
         (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
	(buffer-B
         (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
        reg-A-beg reg-A-end reg-B-beg reg-B-end)
    (with-current-buffer buffer-A
      (setq reg-A-beg (region-beginning)
	    reg-A-end (region-end))
      (set-buffer buffer-B)
      (setq reg-B-beg (region-beginning)
	    reg-B-end (region-end)))

    (ediff-regions-internal
     (get-buffer buffer-A) reg-A-beg reg-A-end
     (get-buffer buffer-B) reg-B-beg reg-B-end
     startup-hooks 'ediff-regions-wordwise 'word-mode nil)))

;;;###autoload
(defun ediff-regions-linewise (buffer-A buffer-B &optional startup-hooks)
  "Run Ediff on a pair of regions in specified buffers.
Regions \(i.e., point and mark\) can be set in advance or marked interactively.
Each region is enlarged to contain full lines.
This function is effective for large regions, over 100-200
lines.  For small regions, use `ediff-regions-wordwise'."
  (interactive
   (let (bf)
     (list (setq bf (read-buffer "Region A's buffer: "
				 (ediff-other-buffer "") t))
	   (read-buffer "Region B's buffer: "
			(progn
			  ;; realign buffers so that two visible bufs will be
			  ;; at the top
			  (save-window-excursion (other-window 1))
			  (ediff-other-buffer bf))
			t))))
  (if (not (ediff-buffer-live-p buffer-A))
      (error "Buffer %S doesn't exist" buffer-A))
  (if (not (ediff-buffer-live-p buffer-B))
      (error "Buffer %S doesn't exist" buffer-B))

  (let ((buffer-A
         (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
	(buffer-B
         (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
        reg-A-beg reg-A-end reg-B-beg reg-B-end)
    (with-current-buffer buffer-A
      (setq reg-A-beg (region-beginning)
	    reg-A-end (region-end))
      ;; enlarge the region to hold full lines
      (goto-char reg-A-beg)
      (beginning-of-line)
      (setq reg-A-beg (point))
      (goto-char reg-A-end)
      (end-of-line)
      (or (eobp) (forward-char)) ; include the newline char
      (setq reg-A-end (point))

      (set-buffer buffer-B)
      (setq reg-B-beg (region-beginning)
	    reg-B-end (region-end))
      ;; enlarge the region to hold full lines
      (goto-char reg-B-beg)
      (beginning-of-line)
      (setq reg-B-beg (point))
      (goto-char reg-B-end)
      (end-of-line)
      (or (eobp) (forward-char)) ; include the newline char
      (setq reg-B-end (point))
      ) ; save excursion

    (ediff-regions-internal
     (get-buffer buffer-A) reg-A-beg reg-A-end
     (get-buffer buffer-B) reg-B-beg reg-B-end
     startup-hooks 'ediff-regions-linewise nil nil))) ; no word mode

;; compare region beg-A to end-A of buffer-A
;; to regions beg-B -- end-B in buffer-B.
(defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
					startup-hooks job-name word-mode
					setup-parameters)
  (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
	overl-A overl-B
	file-A file-B)
    (unwind-protect
	(progn
	  ;; in case beg/end-A/B aren't markers--make them into markers
	  (ediff-with-current-buffer buffer-A
	    (setq beg-A (move-marker (make-marker) beg-A)
		  end-A (move-marker (make-marker) end-A)))
	  (ediff-with-current-buffer buffer-B
	    (setq beg-B (move-marker (make-marker) beg-B)
		  end-B (move-marker (make-marker) end-B)))

	  ;; make file-A
	  (if word-mode
	      (ediff-wordify beg-A end-A buffer-A tmp-buffer)
	    (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer))
	  (setq file-A (ediff-make-temp-file tmp-buffer "regA"))

	  ;; make file-B
	  (if word-mode
	      (ediff-wordify beg-B end-B buffer-B tmp-buffer)
	    (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer))
	  (setq file-B (ediff-make-temp-file tmp-buffer "regB"))

	  (setq overl-A (ediff-make-bullet-proof-overlay beg-A end-A buffer-A))
	  (setq overl-B (ediff-make-bullet-proof-overlay beg-B end-B buffer-B))
	  (ediff-setup buffer-A file-A
		       buffer-B file-B
		       nil nil	    ; buffer & file C
		       (cons `(lambda ()
				(delete-file ,file-A)
				(delete-file ,file-B))
			     startup-hooks)
		       (append
			(list (cons 'ediff-word-mode  word-mode)
			      (cons 'ediff-narrow-bounds (list overl-A overl-B))
			      (cons 'ediff-job-name job-name))
			setup-parameters)))
      (if (and (stringp file-A) (file-exists-p file-A))
	  (delete-file file-A))
      (if (and (stringp file-B) (file-exists-p file-B))
	  (delete-file file-B)))
    ))


;;; Merge files and buffers

;;;###autoload
(defalias 'ediff-merge 'ediff-merge-files)

(defsubst ediff-merge-on-startup ()
  (ediff-do-merge 0)
  ;; Can't remember why this is here, but it may cause the automatically merged
  ;; buffer to be lost. So, keep the buffer modified.
  ;;(ediff-with-current-buffer ediff-buffer-C
  ;;  (set-buffer-modified-p nil))
  )

;;;###autoload
(defun ediff-merge-files (file-A file-B
				 ;; MERGE-BUFFER-FILE is the file to be
				 ;; associated with the merge buffer
				 &optional startup-hooks merge-buffer-file)
  "Merge two files without ancestor."
  (interactive
   (let ((dir-A (if ediff-use-last-dir
		    ediff-last-dir-A
		  default-directory))
	 dir-B f)
     (list (setq f (ediff-read-file-name
		    "File A to merge"
		    dir-A
		    (ediff-get-default-file-name)
		    'no-dirs))
	   (ediff-read-file-name "File B to merge"
				 (setq dir-B
				       (if ediff-use-last-dir
					   ediff-last-dir-B
					 (file-name-directory f)))
				 (progn
				   (ediff-add-to-history
				    'file-name-history
				    (ediff-abbreviate-file-name
				     (expand-file-name
				      (file-name-nondirectory f)
				      dir-B)))
				   (ediff-get-default-file-name f 1)))
	   )))
  (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
  (ediff-files-internal file-A
			(if (file-directory-p file-B)
			    (expand-file-name
			     (file-name-nondirectory file-A) file-B)
			  file-B)
			  nil ; file-C
			  startup-hooks
			  'ediff-merge-files
			  merge-buffer-file))

;;;###autoload
(defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
					       &optional
					       startup-hooks
					       ;; MERGE-BUFFER-FILE is the file
					       ;; to be associated with the
					       ;; merge buffer
					       merge-buffer-file)
  "Merge two files with ancestor."
  (interactive
   (let ((dir-A (if ediff-use-last-dir
		    ediff-last-dir-A
		  default-directory))
	 dir-B dir-ancestor f ff)
     (list (setq f (ediff-read-file-name
		    "File A to merge"
		    dir-A
		    (ediff-get-default-file-name)
		    'no-dirs))
	   (setq ff (ediff-read-file-name "File B to merge"
					  (setq dir-B
						(if ediff-use-last-dir
						    ediff-last-dir-B
						  (file-name-directory f)))
					  (progn
					    (ediff-add-to-history
					     'file-name-history
					     (ediff-abbreviate-file-name
					      (expand-file-name
					       (file-name-nondirectory f)
					       dir-B)))
					    (ediff-get-default-file-name f 1))))
	   (ediff-read-file-name "Ancestor file"
				 (setq dir-ancestor
				       (if ediff-use-last-dir
					   ediff-last-dir-ancestor
					 (file-name-directory ff)))
				 (progn
				   (ediff-add-to-history
				    'file-name-history
				    (ediff-abbreviate-file-name
				     (expand-file-name
				      (file-name-nondirectory ff)
				      dir-ancestor)))
				   (ediff-get-default-file-name ff 2)))
	   )))
  (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
  (ediff-files-internal file-A
			(if (file-directory-p file-B)
			    (expand-file-name
			     (file-name-nondirectory file-A) file-B)
			  file-B)
			  file-ancestor
			  startup-hooks
			  'ediff-merge-files-with-ancestor
			  merge-buffer-file))

;;;###autoload
(defalias 'ediff-merge-with-ancestor 'ediff-merge-files-with-ancestor)

;;;###autoload
(defun ediff-merge-buffers (buffer-A buffer-B
				     &optional
				     ;; MERGE-BUFFER-FILE is the file to be
				     ;; associated with the merge buffer
				     startup-hooks job-name merge-buffer-file)
  "Merge buffers without ancestor."
  (interactive
   (let (bf)
     (list (setq bf (read-buffer "Buffer A to merge: "
				 (ediff-other-buffer "") t))
	   (read-buffer "Buffer B to merge: "
			(progn
			  ;; realign buffers so that two visible bufs will be
			  ;; at the top
			  (save-window-excursion (other-window 1))
			  (ediff-other-buffer bf))
			t))))

  (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
  (or job-name (setq job-name 'ediff-merge-buffers))
  (ediff-buffers-internal
   buffer-A buffer-B nil startup-hooks job-name merge-buffer-file))

;;;###autoload
(defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
						   &optional
						   startup-hooks
						   job-name
						   ;; MERGE-BUFFER-FILE is the
						   ;; file to be associated
						   ;; with the merge buffer
						   merge-buffer-file)
  "Merge buffers with ancestor."
  (interactive
   (let (bf bff)
     (list (setq bf (read-buffer "Buffer A to merge: "
				 (ediff-other-buffer "") t))
	   (setq bff (read-buffer "Buffer B to merge: "
				  (progn
				    ;; realign buffers so that two visible
				    ;; bufs will be at the top
				    (save-window-excursion (other-window 1))
				    (ediff-other-buffer bf))
				  t))
	   (read-buffer "Ancestor buffer: "
				  (progn
				    ;; realign buffers so that three visible
				    ;; bufs will be at the top
				    (save-window-excursion (other-window 1))
				    (ediff-other-buffer (list bf bff)))
				  t)
	   )))

  (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
  (or job-name (setq job-name 'ediff-merge-buffers-with-ancestor))
  (ediff-buffers-internal
   buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file))


;;;###autoload
(defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file)
  ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
  "Run Ediff by merging two revisions of a file.
The file is the optional FILE argument or the file visited by the current
buffer."
  (interactive)
  (if (stringp file) (find-file file))
  (let (rev1 rev2)
    (setq rev1
	  (read-string
	   (format
	    "Version 1 to merge (default %s's working version): "
	    (if (stringp file)
		(file-name-nondirectory file) "current buffer")))
	  rev2
	  (read-string
	   (format
	    "Version 2 to merge (default %s): "
	    (if (stringp file)
		(file-name-nondirectory file) "current buffer"))))
    (ediff-load-version-control)
    ;; ancestor-revision=nil
    (funcall
     (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
     rev1 rev2 nil startup-hooks merge-buffer-file)))


;;;###autoload
(defun ediff-merge-revisions-with-ancestor (&optional
					    file startup-hooks
					    ;; MERGE-BUFFER-FILE is the file to
					    ;; be associated with the merge
					    ;; buffer
					    merge-buffer-file)
  "Run Ediff by merging two revisions of a file with a common ancestor.
The file is the optional FILE argument or the file visited by the current
buffer."
  (interactive)
  (if (stringp file) (find-file file))
  (let (rev1 rev2 ancestor-rev)
    (setq rev1
	  (read-string
	   (format
	    "Version 1 to merge (default %s's working version): "
	    (if (stringp file)
		(file-name-nondirectory file) "current buffer")))
	  rev2
	  (read-string
	   (format
	    "Version 2 to merge (default %s): "
	    (if (stringp file)
		(file-name-nondirectory file) "current buffer")))
	  ancestor-rev
	  (read-string
	   (format
	    "Ancestor version (default %s's base revision): "
	    (if (stringp file)
		(file-name-nondirectory file) "current buffer"))))
    (ediff-load-version-control)
    (funcall
     (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
     rev1 rev2 ancestor-rev startup-hooks merge-buffer-file)))

;;; Apply patch
(defvar ediff-last-dir-patch)
(defvar ediff-patch-default-directory)
(declare-function ediff-get-patch-buffer "ediff-ptch"
                  (&optional arg patch-buf))
(declare-function ediff-dispatch-file-patching-job "ediff-ptch"
                  (patch-buf filename &optional startup-hooks))

;;;###autoload
(defun ediff-patch-file (&optional arg patch-buf)
  "Query for a file name, and then run Ediff by patching that file.
If optional PATCH-BUF is given, use the patch in that buffer
and don't ask the user.
If prefix argument, then: if even argument, assume that the patch is in a
buffer. If odd -- assume it is in a file."
  (interactive "P")
  (let (source-dir source-file)
    (require 'ediff-ptch)
    (setq patch-buf
	  (ediff-get-patch-buffer
	   (if arg (prefix-numeric-value arg)) patch-buf))
    (setq source-dir (cond (ediff-use-last-dir ediff-last-dir-patch)
			   ((and (not ediff-patch-default-directory)
				 (buffer-file-name patch-buf))
			    (file-name-directory
			     (expand-file-name
			      (buffer-file-name patch-buf))))
			   (t default-directory)))
    (setq source-file
	  (read-file-name
	   "File to patch (directory, if multifile patch): "
	   ;; use an explicit initial file
	   source-dir nil nil (ediff-get-default-file-name)))
    (ediff-dispatch-file-patching-job patch-buf source-file)))

(declare-function ediff-patch-buffer-internal "ediff-ptch"
                  (patch-buf buf-to-patch-name &optional startup-hooks))

;;;###autoload
(defun ediff-patch-buffer (&optional arg patch-buf)
  "Run Ediff by patching the buffer specified at prompt.
Without the optional prefix ARG, asks if the patch is in some buffer and
prompts for the buffer or a file, depending on the answer.
With ARG=1, assumes the patch is in a file and prompts for the file.
With ARG=2, assumes the patch is in a buffer and prompts for the buffer.
PATCH-BUF is an optional argument, which specifies the buffer that contains the
patch. If not given, the user is prompted according to the prefix argument."
  (interactive "P")
  (require 'ediff-ptch)
  (setq patch-buf
	(ediff-get-patch-buffer
	 (if arg (prefix-numeric-value arg)) patch-buf))
  (ediff-patch-buffer-internal
   patch-buf
   (read-buffer
    "Which buffer to patch? "
    (ediff-other-buffer patch-buf))))


;;;###autoload
(defalias 'epatch 'ediff-patch-file)
;;;###autoload
(defalias 'epatch-buffer 'ediff-patch-buffer)




;;; Versions Control functions

;;;###autoload
(defun ediff-revision (&optional file startup-hooks)
  "Run Ediff by comparing versions of a file.
The file is an optional FILE argument or the file entered at the prompt.
Default: the file visited by the current buffer.
Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
  ;; if buffer is non-nil, use that buffer instead of the current buffer
  (interactive "P")
  (if (not (stringp file))
    (setq file
	  (ediff-read-file-name "Compare revisions for file"
				(if ediff-use-last-dir
				    ediff-last-dir-A
				  default-directory)
				(ediff-get-default-file-name)
				'no-dirs)))
  (find-file file)
  (if (and (buffer-modified-p)
	   (y-or-n-p (format "Buffer %s is modified. Save buffer? "
                             (buffer-name))))
      (save-buffer (current-buffer)))
  (let (rev1 rev2)
    (setq rev1
	  (read-string
	   (format "Revision 1 to compare (default %s's latest revision): "
		   (file-name-nondirectory file)))
	  rev2
	  (read-string
	   (format "Revision 2 to compare (default %s's current state): "
		   (file-name-nondirectory file))))
    (ediff-load-version-control)
    (funcall
     (intern (format "ediff-%S-internal" ediff-version-control-package))
     rev1 rev2 startup-hooks)
    ))


;;;###autoload
(defalias 'erevision 'ediff-revision)


;; Test if version control package is loaded and load if not
;; Is SILENT is non-nil, don't report error if package is not found.
(defun ediff-load-version-control (&optional silent)
  (require 'ediff-vers)
  (or (featurep ediff-version-control-package)
      (if (locate-library (symbol-name ediff-version-control-package))
	  (progn
	    (message "") ; kill the message from `locate-library'
	    (require ediff-version-control-package))
	(or silent
	    (error "Version control package %S.el not found.  Use vc.el instead"
		   ediff-version-control-package)))))


;;;###autoload
(defun ediff-version ()
  "Return string describing the version of Ediff.
When called interactively, displays the version."
  (interactive)
  (if (if (featurep 'xemacs)
          (interactive-p)
        (called-interactively-p 'interactive))
      (message "%s" (ediff-version))
    (format "Ediff %s of %s" ediff-version ediff-date)))

;; info is run first, and will autoload info.el.
(declare-function Info-goto-node "info" (nodename &optional fork strict-case))

;;;###autoload
(defun ediff-documentation (&optional node)
  "Display Ediff's manual.
With optional NODE, goes to that node."
  (interactive)
  (let ((ctl-window ediff-control-window)
	(ctl-buf ediff-control-buffer))

    (ediff-skip-unsuitable-frames)
    (condition-case nil
	(progn
	  (pop-to-buffer (get-buffer-create "*info*"))
	  (info (if (featurep 'xemacs) "ediff.info" "ediff"))
	  (if node
	      (Info-goto-node node)
	    (message "Type `i' to search for a specific topic"))
	  (raise-frame))
      (error (beep 1)
	     (with-output-to-temp-buffer ediff-msg-buffer
	       (ediff-with-current-buffer standard-output
		 (fundamental-mode))
	       (princ ediff-BAD-INFO))
	     (if (window-live-p ctl-window)
		 (progn
		   (select-window ctl-window)
		   (set-window-buffer ctl-window ctl-buf)))))))


(dolist (mess '("^Errors in diff output. Diff output is in "
                "^Hmm... I don't see an Ediff command around here...$"
                "^Undocumented command! Type `G' in Ediff Control Panel to drop a note to the Ediff maintainer$"
                ": This command runs in Ediff Control Buffer only!$"
                ": Invalid op in ediff-check-version$"
                "^ediff-shrink-window-C can be used only for merging jobs$"
                "^Lost difference info on these directories$"
                "^This command is inapplicable in the present context$"
                "^This session group has no parent$"
                "^Can't hide active session, $"
                "^Ediff: something wrong--no multiple diffs buffer$"
                "^Can't make context diff for Session $"
                "^The patch buffer wasn't found$"
                "^Aborted$"
                "^This Ediff session is not part of a session group$"
                "^No active Ediff sessions or corrupted session registry$"
                "^No session info in this line$"
                "^`.*' is not an ordinary file$"
                "^Patch appears to have failed$"
                "^Recomputation of differences cancelled$"
                "^No fine differences in this mode$"
                "^Lost connection to ancestor buffer...sorry$"
                "^Not merging with ancestor$"
                "^Don't know how to toggle read-only in buffer "
                "Emacs is not running as a window application$"
                "^This command makes sense only when merging with an ancestor$"
                "^At end of the difference list$"
                "^At beginning of the difference list$"
                "^Nothing saved for diff .* in buffer "
                "^Buffer is out of sync for file "
                "^Buffer out of sync for file "
                "^Output from `diff' not found$"
                "^You forgot to specify a region in buffer "
                "^All right. Make up your mind and come back...$"
                "^Current buffer is not visiting any file$"
                "^Failed to retrieve revision: $"
                "^Can't determine display width.$"
                "^File `.*' does not exist or is not readable$"
                "^File `.*' is a directory$"
                "^Buffer .* doesn't exist$"
                "^Directories . and . are the same: "
                "^Directory merge aborted$"
                "^Merge of directory revisions aborted$"
                "^Buffer .* doesn't exist$"
                "^There is no file to merge$"
                "^Version control package .*.el not found. Use vc.el instead$"))
  (add-to-list 'debug-ignored-errors mess))



;;; Command line interface

;;;###autoload
(defun ediff-files-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left)))
    (setq command-line-args-left (nthcdr 2 command-line-args-left))
    (ediff file-a file-b)))

;;;###autoload
(defun ediff3-files-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left))
	(file-c (nth 2 command-line-args-left)))
    (setq command-line-args-left (nthcdr 3 command-line-args-left))
    (ediff3 file-a file-b file-c)))

;;;###autoload
(defun ediff-merge-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left)))
    (setq command-line-args-left (nthcdr 2 command-line-args-left))
    (ediff-merge-files file-a file-b)))

;;;###autoload
(defun ediff-merge-with-ancestor-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left))
	(ancestor (nth 2 command-line-args-left)))
    (setq command-line-args-left (nthcdr 3 command-line-args-left))
    (ediff-merge-files-with-ancestor file-a file-b ancestor)))

;;;###autoload
(defun ediff-directories-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left))
	(regexp (nth 2 command-line-args-left)))
    (setq command-line-args-left (nthcdr 3 command-line-args-left))
    (ediff-directories file-a file-b regexp)))

;;;###autoload
(defun ediff-directories3-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left))
	(file-c (nth 2 command-line-args-left))
	(regexp (nth 3 command-line-args-left)))
    (setq command-line-args-left (nthcdr 4 command-line-args-left))
    (ediff-directories3 file-a file-b file-c regexp)))

;;;###autoload
(defun ediff-merge-directories-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left))
	(regexp (nth 2 command-line-args-left)))
    (setq command-line-args-left (nthcdr 3 command-line-args-left))
    (ediff-merge-directories file-a file-b regexp)))

;;;###autoload
(defun ediff-merge-directories-with-ancestor-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left))
	(ancestor (nth 2 command-line-args-left))
	(regexp (nth 3 command-line-args-left)))
    (setq command-line-args-left (nthcdr 4 command-line-args-left))
    (ediff-merge-directories-with-ancestor file-a file-b ancestor regexp)))



(require 'ediff-util)

(run-hooks 'ediff-load-hook)


;; Local Variables:
;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
;; End:

;;; ediff.el ends here