(coptic-name): defvar rather than defconst.
[emacs.git] / lisp / vc-sccs.el
blob352218b6c4db014b7ddf7e16f7157f18da51d78a
1 ;;; vc-sccs.el --- support for SCCS version-control
3 ;; Copyright (C) 1992,93,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
8 ;; $Id: vc-sccs.el,v 1.21 2003/02/04 12:11:54 lektu Exp $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;;; Code:
31 (eval-when-compile
32 (require 'vc))
34 ;;;
35 ;;; Customization options
36 ;;;
38 (defcustom vc-sccs-register-switches nil
39 "*Extra switches for registering a file in SCCS.
40 A string or list of strings passed to the checkin program by
41 \\[vc-sccs-register]."
42 :type '(choice (const :tag "None" nil)
43 (string :tag "Argument String")
44 (repeat :tag "Argument List"
45 :value ("")
46 string))
47 :version "21.1"
48 :group 'vc)
50 (defcustom vc-sccs-diff-switches nil
51 "*A string or list of strings specifying extra switches for `vcdiff',
52 the diff utility used for SCCS under VC."
53 :type '(choice (const :tag "None" nil)
54 (string :tag "Argument String")
55 (repeat :tag "Argument List"
56 :value ("")
57 string))
58 :version "21.1"
59 :group 'vc)
61 (defcustom vc-sccs-header (or (cdr (assoc 'SCCS vc-header-alist)) '("%W%"))
62 "*Header keywords to be inserted by `vc-insert-headers'."
63 :type '(repeat string)
64 :group 'vc)
66 ;;;###autoload
67 (defcustom vc-sccs-master-templates
68 '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir)
69 "*Where to look for SCCS master files.
70 For a description of possible values, see `vc-check-master-templates'."
71 :type '(choice (const :tag "Use standard SCCS file names"
72 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
73 (repeat :tag "User-specified"
74 (choice string
75 function)))
76 :version "21.1"
77 :group 'vc)
80 ;;;
81 ;;; Internal variables
82 ;;;
84 (defconst vc-sccs-name-assoc-file "VC-names")
87 ;;;
88 ;;; State-querying functions
89 ;;;
91 ;;; The autoload cookie below places vc-sccs-registered directly into
92 ;;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
93 ;;; every file that is visited. The definition is repeated below
94 ;;; so that Help and etags can find it.
96 ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
97 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
99 (defun vc-sccs-state (file)
100 "SCCS-specific function to compute the version control state."
101 (with-temp-buffer
102 (if (vc-insert-file (vc-sccs-lock-file file))
103 (let* ((locks (vc-sccs-parse-locks))
104 (workfile-version (vc-workfile-version file))
105 (locking-user (cdr (assoc workfile-version locks))))
106 (if (not locking-user)
107 (if (vc-workfile-unchanged-p file)
108 'up-to-date
109 'unlocked-changes)
110 (if (string= locking-user (vc-user-login-name))
111 'edited
112 locking-user)))
113 'up-to-date)))
115 (defun vc-sccs-state-heuristic (file)
116 "SCCS-specific state heuristic."
117 (if (not (vc-mistrust-permissions file))
118 ;; This implementation assumes that any file which is under version
119 ;; control and has -rw-r--r-- is locked by its owner. This is true
120 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
121 ;; We have to be careful not to exclude files with execute bits on;
122 ;; scripts can be under version control too. Also, we must ignore the
123 ;; group-read and other-read bits, since paranoid users turn them off.
124 (let* ((attributes (file-attributes file))
125 (owner-uid (nth 2 attributes))
126 (permissions (nth 8 attributes)))
127 (if (string-match ".r-..-..-." permissions)
128 'up-to-date
129 (if (string-match ".rw..-..-." permissions)
130 (if (file-ownership-preserved-p file)
131 'edited
132 (vc-user-login-name owner-uid))
133 ;; Strange permissions.
134 ;; Fall through to real state computation.
135 (vc-sccs-state file)))
136 (vc-sccs-state file))))
138 (defun vc-sccs-workfile-version (file)
139 "SCCS-specific version of `vc-workfile-version'."
140 (with-temp-buffer
141 (vc-insert-file (vc-name file) "^\001e")
142 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
144 (defun vc-sccs-checkout-model (file)
145 "SCCS-specific version of `vc-checkout-model'."
146 'locking)
148 (defun vc-sccs-workfile-unchanged-p (file)
149 "SCCS-specific implementation of vc-workfile-unchanged-p."
150 (zerop (apply 'vc-do-command nil 1 "vcdiff" (vc-name file)
151 (list "--brief" "-q"
152 (concat "-r" (vc-workfile-version file))))))
156 ;;; State-changing functions
159 (defun vc-sccs-register (file &optional rev comment)
160 "Register FILE into the SCCS version-control system.
161 REV is the optional revision number for the file. COMMENT can be used
162 to provide an initial description of FILE.
164 `vc-register-switches' and `vc-sccs-register-switches' are passed to
165 the SCCS command (in that order).
167 Automatically retrieve a read-only version of the file with keywords
168 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
169 (let* ((dirname (or (file-name-directory file) ""))
170 (basename (file-name-nondirectory file))
171 (project-file (vc-sccs-search-project-dir dirname basename)))
172 (let ((vc-name
173 (or project-file
174 (format (car vc-sccs-master-templates) dirname basename))))
175 (apply 'vc-do-command nil 0 "admin" vc-name
176 (and rev (concat "-r" rev))
177 "-fb"
178 (concat "-i" (file-relative-name file))
179 (and comment (concat "-y" comment))
180 (vc-switches 'SCCS 'register)))
181 (delete-file file)
182 (if vc-keep-workfiles
183 (vc-do-command nil 0 "get" (vc-name file)))))
185 (defun vc-sccs-responsible-p (file)
186 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
187 ;; TODO: check for all the patterns in vc-sccs-master-templates
188 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
189 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
190 (file-name-nondirectory file)))))
192 (defun vc-sccs-checkin (file rev comment)
193 "SCCS-specific version of `vc-backend-checkin'."
194 (apply 'vc-do-command nil 0 "delta" (vc-name file)
195 (if rev (concat "-r" rev))
196 (concat "-y" comment)
197 (vc-switches 'SCCS 'checkin))
198 (if vc-keep-workfiles
199 (vc-do-command nil 0 "get" (vc-name file))))
201 (defun vc-sccs-find-version (file rev buffer)
202 (apply 'vc-do-command
203 buffer 0 "get" (vc-name file)
204 "-s" ;; suppress diagnostic output
205 "-p"
206 (and rev
207 (concat "-r"
208 (vc-sccs-lookup-triple file rev)))
209 (vc-switches 'SCCS 'checkout)))
211 (defun vc-sccs-checkout (file &optional editable rev)
212 "Retrieve a copy of a saved version of SCCS controlled FILE.
213 EDITABLE non-nil means that the file should be writable and
214 locked. REV is the revision to check out."
215 (let ((file-buffer (get-file-buffer file))
216 switches)
217 (message "Checking out %s..." file)
218 (save-excursion
219 ;; Change buffers to get local value of vc-checkout-switches.
220 (if file-buffer (set-buffer file-buffer))
221 (setq switches (vc-switches 'SCCS 'checkout))
222 ;; Save this buffer's default-directory
223 ;; and use save-excursion to make sure it is restored
224 ;; in the same buffer it was saved in.
225 (let ((default-directory default-directory))
226 (save-excursion
227 ;; Adjust the default-directory so that the check-out creates
228 ;; the file in the right place.
229 (setq default-directory (file-name-directory file))
231 (and rev (or (string= rev "")
232 (not (stringp rev)))
233 (setq rev nil))
234 (apply 'vc-do-command nil 0 "get" (vc-name file)
235 (if editable "-e")
236 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
237 switches))))
238 (message "Checking out %s...done" file)))
240 (defun vc-sccs-revert (file &optional contents-done)
241 "Revert FILE to the version it was based on."
242 (vc-do-command nil 0 "unget" (vc-name file))
243 (vc-do-command nil 0 "get" (vc-name file))
244 ;; Checking out explicit versions is not supported under SCCS, yet.
245 ;; We always "revert" to the latest version; therefore
246 ;; vc-workfile-version is cleared here so that it gets recomputed.
247 (vc-file-setprop file 'vc-workfile-version nil))
249 (defun vc-sccs-cancel-version (file editable)
250 "Undo the most recent checkin of FILE.
251 EDITABLE non-nil means previous version should be locked."
252 (vc-do-command nil 0 "rmdel"
253 (vc-name file)
254 (concat "-r" (vc-workfile-version file)))
255 (vc-do-command nil 0 "get"
256 (vc-name file)
257 (if editable "-e")))
259 (defun vc-sccs-steal-lock (file &optional rev)
260 "Steal the lock on the current workfile for FILE and revision REV."
261 (vc-do-command nil 0 "unget" (vc-name file) "-n" (if rev (concat "-r" rev)))
262 (vc-do-command nil 0 "get" (vc-name file) "-g" (if rev (concat "-r" rev))))
266 ;;; History functions
269 (defun vc-sccs-print-log (file)
270 "Get change log associated with FILE."
271 (vc-do-command nil 0 "prs" (vc-name file)))
273 (defun vc-sccs-logentry-check ()
274 "Check that the log entry in the current buffer is acceptable for SCCS."
275 (when (>= (buffer-size) 512)
276 (goto-char 512)
277 (error "Log must be less than 512 characters; point is now at pos 512")))
279 (defun vc-sccs-diff (file &optional oldvers newvers)
280 "Get a difference report using SCCS between two versions of FILE."
281 (setq oldvers (vc-sccs-lookup-triple file oldvers))
282 (setq newvers (vc-sccs-lookup-triple file newvers))
283 (apply 'vc-do-command "*vc-diff*" 1 "vcdiff" (vc-name file)
284 (append (list "-q"
285 (and oldvers (concat "-r" oldvers))
286 (and newvers (concat "-r" newvers)))
287 (vc-switches 'SCCS 'diff))))
291 ;;; Snapshot system
294 (defun vc-sccs-assign-name (file name)
295 "Assign to FILE's latest version a given NAME."
296 (vc-sccs-add-triple name file (vc-workfile-version file)))
300 ;;; Miscellaneous
303 (defun vc-sccs-check-headers ()
304 "Check if the current file has any headers in it."
305 (save-excursion
306 (goto-char (point-min))
307 (re-search-forward "%[A-Z]%" nil t)))
309 (defun vc-sccs-rename-file (old new)
310 ;; Move the master file (using vc-rcs-master-templates).
311 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
312 ;; Update the snapshot file.
313 (with-current-buffer
314 (find-file-noselect
315 (expand-file-name vc-sccs-name-assoc-file
316 (file-name-directory (vc-name old))))
317 (goto-char (point-min))
318 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
319 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
320 (replace-match (concat ":" new) nil nil))
321 (basic-save-buffer)
322 (kill-buffer (current-buffer))))
326 ;;; Internal functions
329 ;; This function is wrapped with `progn' so that the autoload cookie
330 ;; copies the whole function itself into loaddefs.el rather than just placing
331 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
332 ;; help us avoid loading vc-sccs.
333 ;;;###autoload
334 (progn (defun vc-sccs-search-project-dir (dirname basename)
335 "Return the name of a master file in the SCCS project directory.
336 Does not check whether the file exists but returns nil if it does not
337 find any project directory."
338 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
339 (when project-dir
340 (if (file-name-absolute-p project-dir)
341 (setq dirs '("SCCS" ""))
342 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
343 (setq project-dir (expand-file-name (concat "~" project-dir))))
344 (while (and (not dir) dirs)
345 (setq dir (expand-file-name (car dirs) project-dir))
346 (unless (file-directory-p dir)
347 (setq dir nil)
348 (setq dirs (cdr dirs))))
349 (and dir (expand-file-name (concat "s." basename) dir))))))
351 (defun vc-sccs-lock-file (file)
352 "Generate lock file name corresponding to FILE."
353 (let ((master (vc-name file)))
354 (and
355 master
356 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
357 (replace-match "p." t t master 2))))
359 (defun vc-sccs-parse-locks ()
360 "Parse SCCS locks in current buffer.
361 The result is a list of the form ((VERSION . USER) (VERSION . USER) ...)."
362 (let (master-locks)
363 (goto-char (point-min))
364 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
365 nil t)
366 (setq master-locks
367 (cons (cons (match-string 1) (match-string 2)) master-locks)))
368 ;; FIXME: is it really necessary to reverse ?
369 (nreverse master-locks)))
371 (defun vc-sccs-add-triple (name file rev)
372 (with-current-buffer
373 (find-file-noselect
374 (expand-file-name vc-sccs-name-assoc-file
375 (file-name-directory (vc-name file))))
376 (goto-char (point-max))
377 (insert name "\t:\t" file "\t" rev "\n")
378 (basic-save-buffer)
379 (kill-buffer (current-buffer))))
381 (defun vc-sccs-lookup-triple (file name)
382 "Return the numeric version corresponding to a named snapshot of FILE.
383 If NAME is nil or a version number string it's just passed through."
384 (if (or (null name)
385 (let ((firstchar (aref name 0)))
386 (and (>= firstchar ?0) (<= firstchar ?9))))
387 name
388 (with-temp-buffer
389 (vc-insert-file
390 (expand-file-name vc-sccs-name-assoc-file
391 (file-name-directory (vc-name file))))
392 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
394 (provide 'vc-sccs)
396 ;;; vc-sccs.el ends here