* emulation/cua-base.el (cua--init-keymaps):
[bpt/emacs.git] / lisp / vc / 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, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7 ;; Package: ediff
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; Compiler pacifier
29 (defvar rcs-default-co-switches)
30
31 (and noninteractive
32 (eval-when-compile
33 (condition-case nil
34 ;; for compatibility with current stable version of xemacs
35 (progn
36 ;;(require 'pcvs nil 'noerror)
37 ;;(require 'rcs nil 'noerror)
38 (require 'pcvs)
39 (require 'rcs))
40 (error nil))
41 (require 'vc)
42 (require 'ediff-init)
43 ))
44 ;; end pacifier
45
46 (defcustom ediff-keep-tmp-versions nil
47 "If t, do not delete temporary previous versions for the files on which
48 comparison or merge operations are being performed."
49 :type 'boolean
50 :group 'ediff-vers
51 )
52
53 (defalias 'ediff-vc-revision-other-window
54 (if (fboundp 'vc-revision-other-window)
55 'vc-revision-other-window
56 'vc-version-other-window))
57
58 (defalias 'ediff-vc-working-revision
59 (if (fboundp 'vc-working-revision)
60 'vc-working-revision
61 'vc-workfile-version))
62
63 ;; VC.el support
64
65 (eval-when-compile
66 (require 'vc-hooks)) ;; for vc-call macro
67
68
69 (defun ediff-vc-latest-version (file)
70 "Return the version level of the latest version of FILE in repository."
71 (if (fboundp 'vc-latest-version)
72 (vc-latest-version file)
73 (or (vc-file-getprop file 'vc-latest-revision)
74 (cond ((vc-backend file)
75 (vc-call state file)
76 (vc-file-getprop file 'vc-latest-revision))
77 (t (error "File %s is not under version control" file))))
78 ))
79
80
81 (defun ediff-vc-internal (rev1 rev2 &optional startup-hooks)
82 ;; Run Ediff on versions of the current buffer.
83 ;; If REV1 is "", use the latest version of the current buffer's file.
84 ;; If REV2 is "" then compare current buffer with REV1.
85 ;; If the current buffer is named `F', the version is named `F.~REV~'.
86 ;; If `F.~REV~' already exists, it is used instead of being re-created.
87 (let (file1 file2 rev1buf rev2buf)
88 (if (string= rev1 "")
89 (setq rev1 (ediff-vc-latest-version (buffer-file-name))))
90 (save-window-excursion
91 (save-excursion
92 (ediff-vc-revision-other-window rev1)
93 (setq rev1buf (current-buffer)
94 file1 (buffer-file-name)))
95 (save-excursion
96 (or (string= rev2 "") ; use current buffer
97 (ediff-vc-revision-other-window rev2))
98 (setq rev2buf (current-buffer)
99 file2 (buffer-file-name)))
100 (setq startup-hooks
101 (cons `(lambda ()
102 (ediff-delete-version-file ,file1)
103 (or ,(string= rev2 "") (ediff-delete-version-file ,file2)))
104 startup-hooks)))
105 (ediff-buffers
106 rev1buf rev2buf
107 startup-hooks
108 'ediff-revision)))
109
110 ;; RCS.el support
111 (defun rcs-ediff-view-revision (&optional rev)
112 ;; View previous RCS revision of current file.
113 ;; With prefix argument, prompts for a revision name.
114 (interactive (list (if current-prefix-arg
115 (read-string "Revision: "))))
116 (let* ((filename (buffer-file-name (current-buffer)))
117 (switches (append '("-p")
118 (if rev (list (concat "-r" rev)) nil)))
119 (buff (concat (file-name-nondirectory filename) ".~" rev "~")))
120 (message "Working ...")
121 (setq filename (expand-file-name filename))
122 (with-output-to-temp-buffer buff
123 (ediff-with-current-buffer standard-output
124 (fundamental-mode))
125 (let ((output-buffer (ediff-rcs-get-output-buffer filename buff)))
126 (delete-windows-on output-buffer)
127 (with-current-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)))
134
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 ((buf (get-buffer-create name)))
141 (with-current-buffer buf
142 (setq buffer-read-only nil
143 default-directory (file-name-directory (expand-file-name file)))
144 (erase-buffer))
145 buf))
146
147 (defun ediff-rcs-internal (rev1 rev2 &optional startup-hooks)
148 ;; Run Ediff on versions of the current buffer.
149 ;; If REV2 is "" then use current buffer.
150 (let (rev2buf rev1buf)
151 (save-window-excursion
152 (setq rev2buf (if (string= rev2 "")
153 (current-buffer)
154 (rcs-ediff-view-revision rev2))
155 rev1buf (rcs-ediff-view-revision rev1)))
156
157 ;; rcs.el doesn't create temp version files, so we don't have to delete
158 ;; anything in startup hooks to ediff-buffers
159 (ediff-buffers rev1buf rev2buf startup-hooks 'ediff-revision)
160 ))
161
162 ;;; Merge with Version Control
163
164 (defun ediff-vc-merge-internal (rev1 rev2 ancestor-rev
165 &optional startup-hooks merge-buffer-file)
166 ;; If ANCESTOR-REV non-nil, merge with ancestor
167 (let (buf1 buf2 ancestor-buf)
168 (save-window-excursion
169 (save-excursion
170 (ediff-vc-revision-other-window rev1)
171 (setq buf1 (current-buffer)))
172 (save-excursion
173 (or (string= rev2 "")
174 (ediff-vc-revision-other-window rev2))
175 (setq buf2 (current-buffer)))
176 (if ancestor-rev
177 (save-excursion
178 (if (string= ancestor-rev "")
179 (setq ancestor-rev (ediff-vc-working-revision buffer-file-name)))
180 (ediff-vc-revision-other-window ancestor-rev)
181 (setq ancestor-buf (current-buffer))))
182 (setq startup-hooks
183 (cons
184 `(lambda ()
185 (ediff-delete-version-file ,(buffer-file-name buf1))
186 (or ,(string= rev2 "")
187 (ediff-delete-version-file ,(buffer-file-name buf2)))
188 (or ,(string= ancestor-rev "")
189 ,(not ancestor-rev)
190 (ediff-delete-version-file ,(buffer-file-name ancestor-buf)))
191 )
192 startup-hooks)))
193 (if ancestor-rev
194 (ediff-merge-buffers-with-ancestor
195 buf1 buf2 ancestor-buf
196 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
197 (ediff-merge-buffers
198 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))
199 ))
200
201 (defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
202 &optional
203 startup-hooks merge-buffer-file)
204 ;; If ANCESTOR-REV non-nil, merge with ancestor
205 (let (buf1 buf2 ancestor-buf)
206 (save-window-excursion
207 (setq buf1 (rcs-ediff-view-revision rev1)
208 buf2 (if (string= rev2 "")
209 (current-buffer)
210 (rcs-ediff-view-revision rev2))
211 ancestor-buf (if ancestor-rev
212 (if (string= ancestor-rev "")
213 (current-buffer)
214 (rcs-ediff-view-revision ancestor-rev)))))
215 ;; rcs.el doesn't create temp version files, so we don't have to delete
216 ;; anything in startup hooks to ediff-buffers
217 (if ancestor-rev
218 (ediff-merge-buffers-with-ancestor
219 buf1 buf2 ancestor-buf
220 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
221 (ediff-merge-buffers
222 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))))
223
224
225 ;; delete version file on exit unless ediff-keep-tmp-versions is true
226 (defun ediff-delete-version-file (file)
227 (or ediff-keep-tmp-versions (delete-file file)))
228
229
230 (provide 'ediff-vers)
231
232
233 ;; Local Variables:
234 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
235 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
236 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
237 ;; End:
238
239 ;; arch-tag: bbb34f0c-2a90-426a-a77a-c75f479ebbbf
240 ;;; ediff-vers.el ends here