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