2007-11-02 Drake Wilson <drake@begriffli.ch> (tiny change)
[bpt/emacs.git] / lisp / ediff-vers.el
CommitLineData
8122a6da
MK
1;;; ediff-vers.el --- version control interface to Ediff
2
e91081eb 3;; Copyright (C) 1995, 1996, 1997, 2001, 2002, 2003, 2004,
d7a0267c 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
8122a6da 5
50a07e18 6;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
8122a6da
MK
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
b4aa6026 12;; the Free Software Foundation; either version 3, or (at your option)
8122a6da
MK
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
8122a6da 24
3afbc435 25;;; Commentary:
8122a6da
MK
26
27;;; Code:
bbe6126c
MK
28
29;; Compiler pacifier
30(defvar rcs-default-co-switches)
31(defvar sc-mode)
32(defvar cvs-shell)
33(defvar cvs-program)
34(defvar cvs-cookie-handle)
ddc90f39 35(defvar ediff-temp-file-prefix)
bbe6126c 36
92c51e07
MK
37(and noninteractive
38 (eval-when-compile
50a07e18
MK
39 (let ((load-path (cons (expand-file-name ".") load-path)))
40 (load "pcl-cvs" 'noerror)
41 (load "rcs" 'noerror)
50a07e18
MK
42 ;; (load "vc" 'noerror) ; this sometimes causes compiler error
43 (or (featurep 'ediff-init)
44 (load "ediff-init.el" nil nil 'nosuffix))
45 )))
bbe6126c 46;; end pacifier
71296446 47
ec6aebe8
MK
48(defcustom ediff-keep-tmp-versions nil
49 "*If t, do not delete temporary previous versions for the files on which
50comparison or merge operations are being performed."
51 :type 'boolean
52 :group 'ediff-vers
53 )
54
33468a59
MK
55(defalias 'ediff-vc-revision-other-window
56 (if (fboundp 'vc-revision-other-window)
57 'vc-revision-other-window
58 'vc-version-other-window))
59
60(defalias 'ediff-vc-working-revision
61 (if (fboundp 'vc-working-revision)
62 'vc-working-revision
63 vc-workfile-version))
64
8122a6da 65;; VC.el support
0831d50c 66
33468a59
MK
67(eval-when-compile
68 (require 'vc-hooks)) ;; for vc-call macro
69
70
0831d50c
MK
71(defun ediff-vc-latest-version (file)
72 "Return the version level of the latest version of FILE in repository."
73 (if (fboundp 'vc-latest-version)
74 (vc-latest-version file)
75 (or (vc-file-getprop file 'vc-latest-version)
76 (cond ((vc-backend file)
77 (vc-call state file)
78 (vc-file-getprop file 'vc-latest-version))
79 (t (error "File %s is not under version control" file))))
80 ))
81
82
92c51e07 83(defun ediff-vc-internal (rev1 rev2 &optional startup-hooks)
0831d50c
MK
84 ;; Run Ediff on versions of the current buffer.
85 ;; If REV1 is "", use the latest version of the current buffer's file.
86 ;; If REV2 is "" then compare current buffer with REV1.
87 ;; If the current buffer is named `F', the version is named `F.~REV~'.
88 ;; If `F.~REV~' already exists, it is used instead of being re-created.
8122a6da 89 (let (file1 file2 rev1buf rev2buf)
71296446 90 (if (string= rev1 "")
0831d50c 91 (setq rev1 (ediff-vc-latest-version (buffer-file-name))))
657f9cb8
MK
92 (save-window-excursion
93 (save-excursion
33468a59 94 (ediff-vc-revision-other-window rev1)
657f9cb8
MK
95 (setq rev1buf (current-buffer)
96 file1 (buffer-file-name)))
97 (save-excursion
98 (or (string= rev2 "") ; use current buffer
33468a59 99 (ediff-vc-revision-other-window rev2))
657f9cb8
MK
100 (setq rev2buf (current-buffer)
101 file2 (buffer-file-name)))
102 (setq startup-hooks
103 (cons `(lambda ()
ec6aebe8
MK
104 (ediff-delete-version-file ,file1)
105 (or ,(string= rev2 "") (ediff-delete-version-file ,file2)))
657f9cb8 106 startup-hooks)))
8122a6da
MK
107 (ediff-buffers
108 rev1buf rev2buf
109 startup-hooks
110 'ediff-revision)))
71296446 111
8122a6da
MK
112;; RCS.el support
113(defun rcs-ediff-view-revision (&optional rev)
114;; View previous RCS revision of current file.
115;; With prefix argument, prompts for a revision name.
71296446 116 (interactive (list (if current-prefix-arg
8122a6da
MK
117 (read-string "Revision: "))))
118 (let* ((filename (buffer-file-name (current-buffer)))
119 (switches (append '("-p")
120 (if rev (list (concat "-r" rev)) nil)))
121 (buff (concat (file-name-nondirectory filename) ".~" rev "~")))
122 (message "Working ...")
123 (setq filename (expand-file-name filename))
124 (with-output-to-temp-buffer buff
7de0717c
DL
125 (ediff-with-current-buffer standard-output
126 (fundamental-mode))
8122a6da
MK
127 (let ((output-buffer (ediff-rcs-get-output-buffer filename buff)))
128 (delete-windows-on output-buffer)
129 (save-excursion
130 (set-buffer output-buffer)
131 (apply 'call-process "co" nil t nil
132 ;; -q: quiet (no diagnostics)
133 (append switches rcs-default-co-switches
71296446 134 (list "-q" filename)))))
8122a6da 135 (message "")
71296446
JB
136 buff)))
137
8122a6da
MK
138(defun ediff-rcs-get-output-buffer (file name)
139 ;; Get a buffer for RCS output for FILE, make it writable and clean it up.
140 ;; Optional NAME is name to use instead of `*RCS-output*'.
3af0304a 141 ;; This is a modified version from rcs.el v1.1. I use it here to make
8122a6da
MK
142 ;; Ediff immune to changes in rcs.el
143 (let* ((default-major-mode 'fundamental-mode) ; no frills!
144 (buf (get-buffer-create name)))
145 (save-excursion
146 (set-buffer buf)
147 (setq buffer-read-only nil
148 default-directory (file-name-directory (expand-file-name file)))
149 (erase-buffer))
150 buf))
151
92c51e07 152(defun ediff-rcs-internal (rev1 rev2 &optional startup-hooks)
8122a6da
MK
153;; Run Ediff on versions of the current buffer.
154;; If REV2 is "" then use current buffer.
657f9cb8
MK
155 (let (rev2buf rev1buf)
156 (save-window-excursion
157 (setq rev2buf (if (string= rev2 "")
158 (current-buffer)
159 (rcs-ediff-view-revision rev2))
160 rev1buf (rcs-ediff-view-revision rev1)))
71296446 161
8122a6da
MK
162 ;; rcs.el doesn't create temp version files, so we don't have to delete
163 ;; anything in startup hooks to ediff-buffers
164 (ediff-buffers rev1buf rev2buf startup-hooks 'ediff-revision)
165 ))
166
8122a6da
MK
167;;; Merge with Version Control
168
71296446 169(defun ediff-vc-merge-internal (rev1 rev2 ancestor-rev
328b4b70 170 &optional startup-hooks merge-buffer-file)
8122a6da
MK
171;; If ANCESTOR-REV non-nil, merge with ancestor
172 (let (buf1 buf2 ancestor-buf)
657f9cb8
MK
173 (save-window-excursion
174 (save-excursion
33468a59 175 (ediff-vc-revision-other-window rev1)
657f9cb8
MK
176 (setq buf1 (current-buffer)))
177 (save-excursion
178 (or (string= rev2 "")
33468a59 179 (ediff-vc-revision-other-window rev2))
657f9cb8
MK
180 (setq buf2 (current-buffer)))
181 (if ancestor-rev
182 (save-excursion
183 (if (string= ancestor-rev "")
33468a59
MK
184 (setq ancestor-rev (ediff-vc-working-revision buffer-file-name)))
185 (ediff-vc-revision-other-window ancestor-rev)
657f9cb8 186 (setq ancestor-buf (current-buffer))))
71296446
JB
187 (setq startup-hooks
188 (cons
189 `(lambda ()
ec6aebe8 190 (ediff-delete-version-file ,(buffer-file-name buf1))
657f9cb8 191 (or ,(string= rev2 "")
ec6aebe8 192 (ediff-delete-version-file ,(buffer-file-name buf2)))
657f9cb8
MK
193 (or ,(string= ancestor-rev "")
194 ,(not ancestor-rev)
ec6aebe8 195 (ediff-delete-version-file ,(buffer-file-name ancestor-buf)))
657f9cb8
MK
196 )
197 startup-hooks)))
8122a6da
MK
198 (if ancestor-rev
199 (ediff-merge-buffers-with-ancestor
200 buf1 buf2 ancestor-buf
328b4b70
MK
201 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
202 (ediff-merge-buffers
203 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))
8122a6da
MK
204 ))
205
92c51e07 206(defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
328b4b70
MK
207 &optional
208 startup-hooks merge-buffer-file)
8122a6da
MK
209 ;; If ANCESTOR-REV non-nil, merge with ancestor
210 (let (buf1 buf2 ancestor-buf)
657f9cb8
MK
211 (save-window-excursion
212 (setq buf1 (rcs-ediff-view-revision rev1)
213 buf2 (if (string= rev2 "")
214 (current-buffer)
215 (rcs-ediff-view-revision rev2))
216 ancestor-buf (if ancestor-rev
217 (if (string= ancestor-rev "")
218 (current-buffer)
219 (rcs-ediff-view-revision ancestor-rev)))))
8122a6da
MK
220 ;; rcs.el doesn't create temp version files, so we don't have to delete
221 ;; anything in startup hooks to ediff-buffers
222 (if ancestor-rev
223 (ediff-merge-buffers-with-ancestor
224 buf1 buf2 ancestor-buf
328b4b70
MK
225 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
226 (ediff-merge-buffers
227 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))))
8122a6da 228
ec6aebe8
MK
229
230;; delete version file on exit unless ediff-keep-tmp-versions is true
231(defun ediff-delete-version-file (file)
232 (or ediff-keep-tmp-versions (delete-file file)))
8122a6da 233
8ea74b0e
MK
234
235(provide 'ediff-vers)
236
b6178721 237
8122a6da
MK
238;;; Local Variables:
239;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
e756eb9f
MK
240;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
241;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
8122a6da
MK
242;;; End:
243
ab5796a9 244;;; arch-tag: bbb34f0c-2a90-426a-a77a-c75f479ebbbf
8122a6da 245;;; ediff-vers.el ends here