Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / ediff-vers.el
1 ;;; ediff-vers.el --- version control interface to Ediff
2
3 ;; Copyright (C) 1995, 1996, 1997, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
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
12 ;; the Free Software Foundation; either version 3, or (at your option)
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
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
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)
35 (defvar ediff-temp-file-prefix)
36
37 (and noninteractive
38 (eval-when-compile
39 (condition-case nil
40 ;; for compatibility with current stable version of xemacs
41 (progn
42 ;;(require 'pcvs nil 'noerror)
43 ;;(require 'rcs nil 'noerror)
44 (require 'pcvs)
45 (require 'rcs))
46 (error nil))
47 (require 'vc)
48 (require 'ediff-init)
49 ))
50 ;; end pacifier
51
52 (defcustom ediff-keep-tmp-versions nil
53 "*If t, do not delete temporary previous versions for the files on which
54 comparison or merge operations are being performed."
55 :type 'boolean
56 :group 'ediff-vers
57 )
58
59 (defalias 'ediff-vc-revision-other-window
60 (if (fboundp 'vc-revision-other-window)
61 'vc-revision-other-window
62 'vc-version-other-window))
63
64 (defalias 'ediff-vc-working-revision
65 (if (fboundp 'vc-working-revision)
66 'vc-working-revision
67 'vc-workfile-version))
68
69 ;; VC.el support
70
71 (eval-when-compile
72 (require 'vc-hooks)) ;; for vc-call macro
73
74
75 (defun ediff-vc-latest-version (file)
76 "Return the version level of the latest version of FILE in repository."
77 (if (fboundp 'vc-latest-version)
78 (vc-latest-version file)
79 (or (vc-file-getprop file 'vc-latest-version)
80 (cond ((vc-backend file)
81 (vc-call state file)
82 (vc-file-getprop file 'vc-latest-version))
83 (t (error "File %s is not under version control" file))))
84 ))
85
86
87 (defun ediff-vc-internal (rev1 rev2 &optional startup-hooks)
88 ;; Run Ediff on versions of the current buffer.
89 ;; If REV1 is "", use the latest version of the current buffer's file.
90 ;; If REV2 is "" then compare current buffer with REV1.
91 ;; If the current buffer is named `F', the version is named `F.~REV~'.
92 ;; If `F.~REV~' already exists, it is used instead of being re-created.
93 (let (file1 file2 rev1buf rev2buf)
94 (if (string= rev1 "")
95 (setq rev1 (ediff-vc-latest-version (buffer-file-name))))
96 (save-window-excursion
97 (save-excursion
98 (ediff-vc-revision-other-window rev1)
99 (setq rev1buf (current-buffer)
100 file1 (buffer-file-name)))
101 (save-excursion
102 (or (string= rev2 "") ; use current buffer
103 (ediff-vc-revision-other-window rev2))
104 (setq rev2buf (current-buffer)
105 file2 (buffer-file-name)))
106 (setq startup-hooks
107 (cons `(lambda ()
108 (ediff-delete-version-file ,file1)
109 (or ,(string= rev2 "") (ediff-delete-version-file ,file2)))
110 startup-hooks)))
111 (ediff-buffers
112 rev1buf rev2buf
113 startup-hooks
114 'ediff-revision)))
115
116 ;; RCS.el support
117 (defun rcs-ediff-view-revision (&optional rev)
118 ;; View previous RCS revision of current file.
119 ;; With prefix argument, prompts for a revision name.
120 (interactive (list (if current-prefix-arg
121 (read-string "Revision: "))))
122 (let* ((filename (buffer-file-name (current-buffer)))
123 (switches (append '("-p")
124 (if rev (list (concat "-r" rev)) nil)))
125 (buff (concat (file-name-nondirectory filename) ".~" rev "~")))
126 (message "Working ...")
127 (setq filename (expand-file-name filename))
128 (with-output-to-temp-buffer buff
129 (ediff-with-current-buffer standard-output
130 (fundamental-mode))
131 (let ((output-buffer (ediff-rcs-get-output-buffer filename buff)))
132 (delete-windows-on output-buffer)
133 (save-excursion
134 (set-buffer output-buffer)
135 (apply 'call-process "co" nil t nil
136 ;; -q: quiet (no diagnostics)
137 (append switches rcs-default-co-switches
138 (list "-q" filename)))))
139 (message "")
140 buff)))
141
142 (defun ediff-rcs-get-output-buffer (file name)
143 ;; Get a buffer for RCS output for FILE, make it writable and clean it up.
144 ;; Optional NAME is name to use instead of `*RCS-output*'.
145 ;; This is a modified version from rcs.el v1.1. I use it here to make
146 ;; Ediff immune to changes in rcs.el
147 (let* ((default-major-mode 'fundamental-mode) ; no frills!
148 (buf (get-buffer-create name)))
149 (save-excursion
150 (set-buffer buf)
151 (setq buffer-read-only nil
152 default-directory (file-name-directory (expand-file-name file)))
153 (erase-buffer))
154 buf))
155
156 (defun ediff-rcs-internal (rev1 rev2 &optional startup-hooks)
157 ;; Run Ediff on versions of the current buffer.
158 ;; If REV2 is "" then use current buffer.
159 (let (rev2buf rev1buf)
160 (save-window-excursion
161 (setq rev2buf (if (string= rev2 "")
162 (current-buffer)
163 (rcs-ediff-view-revision rev2))
164 rev1buf (rcs-ediff-view-revision rev1)))
165
166 ;; rcs.el doesn't create temp version files, so we don't have to delete
167 ;; anything in startup hooks to ediff-buffers
168 (ediff-buffers rev1buf rev2buf startup-hooks 'ediff-revision)
169 ))
170
171 ;;; Merge with Version Control
172
173 (defun ediff-vc-merge-internal (rev1 rev2 ancestor-rev
174 &optional startup-hooks merge-buffer-file)
175 ;; If ANCESTOR-REV non-nil, merge with ancestor
176 (let (buf1 buf2 ancestor-buf)
177 (save-window-excursion
178 (save-excursion
179 (ediff-vc-revision-other-window rev1)
180 (setq buf1 (current-buffer)))
181 (save-excursion
182 (or (string= rev2 "")
183 (ediff-vc-revision-other-window rev2))
184 (setq buf2 (current-buffer)))
185 (if ancestor-rev
186 (save-excursion
187 (if (string= ancestor-rev "")
188 (setq ancestor-rev (ediff-vc-working-revision buffer-file-name)))
189 (ediff-vc-revision-other-window ancestor-rev)
190 (setq ancestor-buf (current-buffer))))
191 (setq startup-hooks
192 (cons
193 `(lambda ()
194 (ediff-delete-version-file ,(buffer-file-name buf1))
195 (or ,(string= rev2 "")
196 (ediff-delete-version-file ,(buffer-file-name buf2)))
197 (or ,(string= ancestor-rev "")
198 ,(not ancestor-rev)
199 (ediff-delete-version-file ,(buffer-file-name ancestor-buf)))
200 )
201 startup-hooks)))
202 (if ancestor-rev
203 (ediff-merge-buffers-with-ancestor
204 buf1 buf2 ancestor-buf
205 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
206 (ediff-merge-buffers
207 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))
208 ))
209
210 (defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
211 &optional
212 startup-hooks merge-buffer-file)
213 ;; If ANCESTOR-REV non-nil, merge with ancestor
214 (let (buf1 buf2 ancestor-buf)
215 (save-window-excursion
216 (setq buf1 (rcs-ediff-view-revision rev1)
217 buf2 (if (string= rev2 "")
218 (current-buffer)
219 (rcs-ediff-view-revision rev2))
220 ancestor-buf (if ancestor-rev
221 (if (string= ancestor-rev "")
222 (current-buffer)
223 (rcs-ediff-view-revision ancestor-rev)))))
224 ;; rcs.el doesn't create temp version files, so we don't have to delete
225 ;; anything in startup hooks to ediff-buffers
226 (if ancestor-rev
227 (ediff-merge-buffers-with-ancestor
228 buf1 buf2 ancestor-buf
229 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
230 (ediff-merge-buffers
231 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))))
232
233
234 ;; delete version file on exit unless ediff-keep-tmp-versions is true
235 (defun ediff-delete-version-file (file)
236 (or ediff-keep-tmp-versions (delete-file file)))
237
238
239 (provide 'ediff-vers)
240
241
242 ;;; Local Variables:
243 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
244 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
245 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
246 ;;; End:
247
248 ;; arch-tag: bbb34f0c-2a90-426a-a77a-c75f479ebbbf
249 ;;; ediff-vers.el ends here