* doc/emacs/custom.texi (Custom Themes): Fix typo.
[emacs.git] / lisp / vc / ediff-vers.el
blobdc004fd1ff17406e7d63c853cb7cf34b17425b89
1 ;;; ediff-vers.el --- version control interface to Ediff
3 ;; Copyright (C) 1995-1997, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
6 ;; Package: ediff
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-revision)
73 (cond ((vc-backend file)
74 (vc-call state file)
75 (vc-file-getprop file 'vc-latest-revision))
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 (with-current-buffer output-buffer
127 (apply 'call-process "co" nil t nil
128 ;; -q: quiet (no diagnostics)
129 (append switches rcs-default-co-switches
130 (list "-q" filename)))))
131 (message "")
132 buff)))
134 (defun ediff-rcs-get-output-buffer (file name)
135 ;; Get a buffer for RCS output for FILE, make it writable and clean it up.
136 ;; Optional NAME is name to use instead of `*RCS-output*'.
137 ;; This is a modified version from rcs.el v1.1. I use it here to make
138 ;; Ediff immune to changes in rcs.el
139 (let ((buf (get-buffer-create name)))
140 (with-current-buffer buf
141 (setq buffer-read-only nil
142 default-directory (file-name-directory (expand-file-name file)))
143 (erase-buffer))
144 buf))
146 (defun ediff-rcs-internal (rev1 rev2 &optional startup-hooks)
147 ;; Run Ediff on versions of the current buffer.
148 ;; If REV2 is "" then use current buffer.
149 (let (rev2buf rev1buf)
150 (save-window-excursion
151 (setq rev2buf (if (string= rev2 "")
152 (current-buffer)
153 (rcs-ediff-view-revision rev2))
154 rev1buf (rcs-ediff-view-revision rev1)))
156 ;; rcs.el doesn't create temp version files, so we don't have to delete
157 ;; anything in startup hooks to ediff-buffers
158 (ediff-buffers rev1buf rev2buf startup-hooks 'ediff-revision)
161 ;;; Merge with Version Control
163 (defun ediff-vc-merge-internal (rev1 rev2 ancestor-rev
164 &optional startup-hooks merge-buffer-file)
165 ;; If ANCESTOR-REV non-nil, merge with ancestor
166 (let (buf1 buf2 ancestor-buf)
167 (save-window-excursion
168 (save-excursion
169 (ediff-vc-revision-other-window rev1)
170 (setq buf1 (current-buffer)))
171 (save-excursion
172 (or (string= rev2 "")
173 (ediff-vc-revision-other-window rev2))
174 (setq buf2 (current-buffer)))
175 (if ancestor-rev
176 (save-excursion
177 (if (string= ancestor-rev "")
178 (setq ancestor-rev (ediff-vc-working-revision buffer-file-name)))
179 (ediff-vc-revision-other-window ancestor-rev)
180 (setq ancestor-buf (current-buffer))))
181 (setq startup-hooks
182 (cons
183 `(lambda ()
184 (ediff-delete-version-file ,(buffer-file-name buf1))
185 (or ,(string= rev2 "")
186 (ediff-delete-version-file ,(buffer-file-name buf2)))
187 (or ,(string= ancestor-rev "")
188 ,(not ancestor-rev)
189 (ediff-delete-version-file ,(buffer-file-name ancestor-buf)))
191 startup-hooks)))
192 (if ancestor-rev
193 (ediff-merge-buffers-with-ancestor
194 buf1 buf2 ancestor-buf
195 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
196 (ediff-merge-buffers
197 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))
200 (defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
201 &optional
202 startup-hooks merge-buffer-file)
203 ;; If ANCESTOR-REV non-nil, merge with ancestor
204 (let (buf1 buf2 ancestor-buf)
205 (save-window-excursion
206 (setq buf1 (rcs-ediff-view-revision rev1)
207 buf2 (if (string= rev2 "")
208 (current-buffer)
209 (rcs-ediff-view-revision rev2))
210 ancestor-buf (if ancestor-rev
211 (if (string= ancestor-rev "")
212 (current-buffer)
213 (rcs-ediff-view-revision ancestor-rev)))))
214 ;; rcs.el doesn't create temp version files, so we don't have to delete
215 ;; anything in startup hooks to ediff-buffers
216 (if ancestor-rev
217 (ediff-merge-buffers-with-ancestor
218 buf1 buf2 ancestor-buf
219 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
220 (ediff-merge-buffers
221 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))))
224 ;; delete version file on exit unless ediff-keep-tmp-versions is true
225 (defun ediff-delete-version-file (file)
226 (or ediff-keep-tmp-versions (delete-file file)))
229 (provide 'ediff-vers)
232 ;; Local Variables:
233 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
234 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
235 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
236 ;; End:
238 ;;; ediff-vers.el ends here