(number, original-date, add-to-diary-list)
[emacs.git] / lisp / ediff-vers.el
blobfa9b4505bf58075dd90faa054394d445004707f0
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, or (at your option)
13 ;; 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; 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.
25 ;;; Commentary:
27 ;;; Code:
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)
37 (and noninteractive
38 (eval-when-compile
39 (let ((load-path (cons (expand-file-name ".") load-path)))
40 (load "pcl-cvs" 'noerror)
41 (load "rcs" 'noerror)
42 ;; (load "vc" 'noerror) ; this sometimes causes compiler error
43 (or (featurep 'ediff-init)
44 (load "ediff-init.el" nil t 'nosuffix))
45 )))
46 ;; end pacifier
48 (defcustom ediff-keep-tmp-versions nil
49 "*If t, do not delete temporary previous versions for the files on which
50 comparison or merge operations are being performed."
51 :type 'boolean
52 :group 'ediff-vers
55 (defalias 'ediff-vc-revision-other-window
56 (if (fboundp 'vc-revision-other-window)
57 'vc-revision-other-window
58 'vc-version-other-window))
60 (defalias 'ediff-vc-working-revision
61 (if (fboundp 'vc-working-revision)
62 'vc-working-revision
63 'vc-workfile-version))
65 ;; VC.el support
67 (eval-when-compile
68 (require 'vc-hooks)) ;; for vc-call macro
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))))
83 (defun ediff-vc-internal (rev1 rev2 &optional startup-hooks)
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.
89 (let (file1 file2 rev1buf rev2buf)
90 (if (string= rev1 "")
91 (setq rev1 (ediff-vc-latest-version (buffer-file-name))))
92 (save-window-excursion
93 (save-excursion
94 (ediff-vc-revision-other-window rev1)
95 (setq rev1buf (current-buffer)
96 file1 (buffer-file-name)))
97 (save-excursion
98 (or (string= rev2 "") ; use current buffer
99 (ediff-vc-revision-other-window rev2))
100 (setq rev2buf (current-buffer)
101 file2 (buffer-file-name)))
102 (setq startup-hooks
103 (cons `(lambda ()
104 (ediff-delete-version-file ,file1)
105 (or ,(string= rev2 "") (ediff-delete-version-file ,file2)))
106 startup-hooks)))
107 (ediff-buffers
108 rev1buf rev2buf
109 startup-hooks
110 'ediff-revision)))
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.
116 (interactive (list (if current-prefix-arg
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
125 (ediff-with-current-buffer standard-output
126 (fundamental-mode))
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
134 (list "-q" filename)))))
135 (message "")
136 buff)))
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*'.
141 ;; This is a modified version from rcs.el v1.1. I use it here to make
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))
152 (defun ediff-rcs-internal (rev1 rev2 &optional startup-hooks)
153 ;; Run Ediff on versions of the current buffer.
154 ;; If REV2 is "" then use current buffer.
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)))
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)
167 ;;; Merge with Version Control
169 (defun ediff-vc-merge-internal (rev1 rev2 ancestor-rev
170 &optional startup-hooks merge-buffer-file)
171 ;; If ANCESTOR-REV non-nil, merge with ancestor
172 (let (buf1 buf2 ancestor-buf)
173 (save-window-excursion
174 (save-excursion
175 (ediff-vc-revision-other-window rev1)
176 (setq buf1 (current-buffer)))
177 (save-excursion
178 (or (string= rev2 "")
179 (ediff-vc-revision-other-window rev2))
180 (setq buf2 (current-buffer)))
181 (if ancestor-rev
182 (save-excursion
183 (if (string= ancestor-rev "")
184 (setq ancestor-rev (ediff-vc-working-revision buffer-file-name)))
185 (ediff-vc-revision-other-window ancestor-rev)
186 (setq ancestor-buf (current-buffer))))
187 (setq startup-hooks
188 (cons
189 `(lambda ()
190 (ediff-delete-version-file ,(buffer-file-name buf1))
191 (or ,(string= rev2 "")
192 (ediff-delete-version-file ,(buffer-file-name buf2)))
193 (or ,(string= ancestor-rev "")
194 ,(not ancestor-rev)
195 (ediff-delete-version-file ,(buffer-file-name ancestor-buf)))
197 startup-hooks)))
198 (if ancestor-rev
199 (ediff-merge-buffers-with-ancestor
200 buf1 buf2 ancestor-buf
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))
206 (defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
207 &optional
208 startup-hooks merge-buffer-file)
209 ;; If ANCESTOR-REV non-nil, merge with ancestor
210 (let (buf1 buf2 ancestor-buf)
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)))))
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
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))))
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)))
235 (provide 'ediff-vers)
238 ;;; Local Variables:
239 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
240 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
241 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
242 ;;; End:
244 ;;; arch-tag: bbb34f0c-2a90-426a-a77a-c75f479ebbbf
245 ;;; ediff-vers.el ends here