remember.el: Make remember-buffer an alias of remember-finalize
[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
8122a6da 55;; VC.el support
0831d50c
MK
56
57(defun ediff-vc-latest-version (file)
58 "Return the version level of the latest version of FILE in repository."
59 (if (fboundp 'vc-latest-version)
60 (vc-latest-version file)
61 (or (vc-file-getprop file 'vc-latest-version)
62 (cond ((vc-backend file)
63 (vc-call state file)
64 (vc-file-getprop file 'vc-latest-version))
65 (t (error "File %s is not under version control" file))))
66 ))
67
68
92c51e07 69(defun ediff-vc-internal (rev1 rev2 &optional startup-hooks)
0831d50c
MK
70 ;; Run Ediff on versions of the current buffer.
71 ;; If REV1 is "", use the latest version of the current buffer's file.
72 ;; If REV2 is "" then compare current buffer with REV1.
73 ;; If the current buffer is named `F', the version is named `F.~REV~'.
74 ;; If `F.~REV~' already exists, it is used instead of being re-created.
8122a6da 75 (let (file1 file2 rev1buf rev2buf)
71296446 76 (if (string= rev1 "")
0831d50c 77 (setq rev1 (ediff-vc-latest-version (buffer-file-name))))
657f9cb8
MK
78 (save-window-excursion
79 (save-excursion
0a0ca7f1 80 (vc-revision-other-window rev1)
657f9cb8
MK
81 (setq rev1buf (current-buffer)
82 file1 (buffer-file-name)))
83 (save-excursion
84 (or (string= rev2 "") ; use current buffer
0a0ca7f1 85 (vc-revision-other-window rev2))
657f9cb8
MK
86 (setq rev2buf (current-buffer)
87 file2 (buffer-file-name)))
88 (setq startup-hooks
89 (cons `(lambda ()
ec6aebe8
MK
90 (ediff-delete-version-file ,file1)
91 (or ,(string= rev2 "") (ediff-delete-version-file ,file2)))
657f9cb8 92 startup-hooks)))
8122a6da
MK
93 (ediff-buffers
94 rev1buf rev2buf
95 startup-hooks
96 'ediff-revision)))
71296446 97
8122a6da
MK
98;; RCS.el support
99(defun rcs-ediff-view-revision (&optional rev)
100;; View previous RCS revision of current file.
101;; With prefix argument, prompts for a revision name.
71296446 102 (interactive (list (if current-prefix-arg
8122a6da
MK
103 (read-string "Revision: "))))
104 (let* ((filename (buffer-file-name (current-buffer)))
105 (switches (append '("-p")
106 (if rev (list (concat "-r" rev)) nil)))
107 (buff (concat (file-name-nondirectory filename) ".~" rev "~")))
108 (message "Working ...")
109 (setq filename (expand-file-name filename))
110 (with-output-to-temp-buffer buff
7de0717c
DL
111 (ediff-with-current-buffer standard-output
112 (fundamental-mode))
8122a6da
MK
113 (let ((output-buffer (ediff-rcs-get-output-buffer filename buff)))
114 (delete-windows-on output-buffer)
115 (save-excursion
116 (set-buffer output-buffer)
117 (apply 'call-process "co" nil t nil
118 ;; -q: quiet (no diagnostics)
119 (append switches rcs-default-co-switches
71296446 120 (list "-q" filename)))))
8122a6da 121 (message "")
71296446
JB
122 buff)))
123
8122a6da
MK
124(defun ediff-rcs-get-output-buffer (file name)
125 ;; Get a buffer for RCS output for FILE, make it writable and clean it up.
126 ;; Optional NAME is name to use instead of `*RCS-output*'.
3af0304a 127 ;; This is a modified version from rcs.el v1.1. I use it here to make
8122a6da
MK
128 ;; Ediff immune to changes in rcs.el
129 (let* ((default-major-mode 'fundamental-mode) ; no frills!
130 (buf (get-buffer-create name)))
131 (save-excursion
132 (set-buffer buf)
133 (setq buffer-read-only nil
134 default-directory (file-name-directory (expand-file-name file)))
135 (erase-buffer))
136 buf))
137
92c51e07 138(defun ediff-rcs-internal (rev1 rev2 &optional startup-hooks)
8122a6da
MK
139;; Run Ediff on versions of the current buffer.
140;; If REV2 is "" then use current buffer.
657f9cb8
MK
141 (let (rev2buf rev1buf)
142 (save-window-excursion
143 (setq rev2buf (if (string= rev2 "")
144 (current-buffer)
145 (rcs-ediff-view-revision rev2))
146 rev1buf (rcs-ediff-view-revision rev1)))
71296446 147
8122a6da
MK
148 ;; rcs.el doesn't create temp version files, so we don't have to delete
149 ;; anything in startup hooks to ediff-buffers
150 (ediff-buffers rev1buf rev2buf startup-hooks 'ediff-revision)
151 ))
152
8122a6da
MK
153;;; Merge with Version Control
154
71296446 155(defun ediff-vc-merge-internal (rev1 rev2 ancestor-rev
328b4b70 156 &optional startup-hooks merge-buffer-file)
8122a6da
MK
157;; If ANCESTOR-REV non-nil, merge with ancestor
158 (let (buf1 buf2 ancestor-buf)
657f9cb8
MK
159 (save-window-excursion
160 (save-excursion
0a0ca7f1 161 (vc-revision-other-window rev1)
657f9cb8
MK
162 (setq buf1 (current-buffer)))
163 (save-excursion
164 (or (string= rev2 "")
0a0ca7f1 165 (vc-revision-other-window rev2))
657f9cb8
MK
166 (setq buf2 (current-buffer)))
167 (if ancestor-rev
168 (save-excursion
169 (if (string= ancestor-rev "")
ac3f4c6f 170 (setq ancestor-rev (vc-working-revision buffer-file-name)))
0a0ca7f1 171 (vc-revision-other-window ancestor-rev)
657f9cb8 172 (setq ancestor-buf (current-buffer))))
71296446
JB
173 (setq startup-hooks
174 (cons
175 `(lambda ()
ec6aebe8 176 (ediff-delete-version-file ,(buffer-file-name buf1))
657f9cb8 177 (or ,(string= rev2 "")
ec6aebe8 178 (ediff-delete-version-file ,(buffer-file-name buf2)))
657f9cb8
MK
179 (or ,(string= ancestor-rev "")
180 ,(not ancestor-rev)
ec6aebe8 181 (ediff-delete-version-file ,(buffer-file-name ancestor-buf)))
657f9cb8
MK
182 )
183 startup-hooks)))
8122a6da
MK
184 (if ancestor-rev
185 (ediff-merge-buffers-with-ancestor
186 buf1 buf2 ancestor-buf
328b4b70
MK
187 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
188 (ediff-merge-buffers
189 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))
8122a6da
MK
190 ))
191
92c51e07 192(defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
328b4b70
MK
193 &optional
194 startup-hooks merge-buffer-file)
8122a6da
MK
195 ;; If ANCESTOR-REV non-nil, merge with ancestor
196 (let (buf1 buf2 ancestor-buf)
657f9cb8
MK
197 (save-window-excursion
198 (setq buf1 (rcs-ediff-view-revision rev1)
199 buf2 (if (string= rev2 "")
200 (current-buffer)
201 (rcs-ediff-view-revision rev2))
202 ancestor-buf (if ancestor-rev
203 (if (string= ancestor-rev "")
204 (current-buffer)
205 (rcs-ediff-view-revision ancestor-rev)))))
8122a6da
MK
206 ;; rcs.el doesn't create temp version files, so we don't have to delete
207 ;; anything in startup hooks to ediff-buffers
208 (if ancestor-rev
209 (ediff-merge-buffers-with-ancestor
210 buf1 buf2 ancestor-buf
328b4b70
MK
211 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
212 (ediff-merge-buffers
213 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))))
8122a6da 214
ec6aebe8
MK
215
216;; delete version file on exit unless ediff-keep-tmp-versions is true
217(defun ediff-delete-version-file (file)
218 (or ediff-keep-tmp-versions (delete-file file)))
8122a6da 219
8ea74b0e
MK
220
221(provide 'ediff-vers)
222
b6178721 223
8122a6da
MK
224;;; Local Variables:
225;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
e756eb9f
MK
226;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
227;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
8122a6da
MK
228;;; End:
229
ab5796a9 230;;; arch-tag: bbb34f0c-2a90-426a-a77a-c75f479ebbbf
8122a6da 231;;; ediff-vers.el ends here