(font-lock-keywords): Docstring improvement.
[bpt/emacs.git] / lisp / vc-sccs.el
CommitLineData
7a004b71
GM
1;;; vc-sccs.el --- support for SCCS version-control
2
891b8b69 3;; Copyright (C) 1992,93,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
7a004b71
GM
4
5;; Author: FSF (see vc.el for full credits)
6;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7
e556ef05 8;; $Id: vc-sccs.el,v 1.24 2003/09/01 15:45:17 miles Exp $
7a004b71
GM
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27;;; Commentary:
28
29;;; Code:
30
f1180544 31(eval-when-compile
a1e9f194
AS
32 (require 'vc))
33
f1180544 34;;;
8f98485f
AS
35;;; Customization options
36;;;
37
7a004b71 38(defcustom vc-sccs-register-switches nil
a7cafade
DL
39 "*Extra switches for registering a file in SCCS.
40A string or list of strings passed to the checkin program by
7a004b71
GM
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))
a7cafade 47 :version "21.1"
7a004b71
GM
48 :group 'vc)
49
a1e9f194
AS
50(defcustom vc-sccs-diff-switches nil
51 "*A string or list of strings specifying extra switches for `vcdiff',
52the 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)
60
7a004b71
GM
61(defcustom vc-sccs-header (or (cdr (assoc 'SCCS vc-header-alist)) '("%W%"))
62 "*Header keywords to be inserted by `vc-insert-headers'."
8b7c8991 63 :type '(repeat string)
7a004b71
GM
64 :group 'vc)
65
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.
70For 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)))
a7cafade 76 :version "21.1"
7a004b71
GM
77 :group 'vc)
78
8f98485f
AS
79\f
80;;;
81;;; Internal variables
82;;;
83
099bd78a
SM
84(defconst vc-sccs-name-assoc-file "VC-names")
85
8f98485f
AS
86\f
87;;;
88;;; State-querying functions
89;;;
90
9ef33737
AS
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.
95
96;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
af65391b 97(defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
7a004b71
GM
98
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)))
114
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))
8bfde4be
AS
133 ;; Strange permissions.
134 ;; Fall through to real state computation.
135 (vc-sccs-state file))))
136 (vc-sccs-state file)))
7a004b71
GM
137
138(defun vc-sccs-workfile-version (file)
139 "SCCS-specific version of `vc-workfile-version'."
140 (with-temp-buffer
8bfde4be
AS
141 ;; The workfile version is always the latest version number.
142 ;; To find this number, search the entire delta table,
143 ;; rather than just the first entry, because the
144 ;; first entry might be a deleted ("R") version.
145 (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
7a004b71
GM
146 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
147
a7cafade 148(defun vc-sccs-checkout-model (file)
7a004b71
GM
149 "SCCS-specific version of `vc-checkout-model'."
150 'locking)
151
152(defun vc-sccs-workfile-unchanged-p (file)
153 "SCCS-specific implementation of vc-workfile-unchanged-p."
a1e9f194
AS
154 (zerop (apply 'vc-do-command nil 1 "vcdiff" (vc-name file)
155 (list "--brief" "-q"
156 (concat "-r" (vc-workfile-version file))))))
7a004b71 157
7a004b71 158\f
8f98485f
AS
159;;;
160;;; State-changing functions
161;;;
7a004b71
GM
162
163(defun vc-sccs-register (file &optional rev comment)
164 "Register FILE into the SCCS version-control system.
165REV is the optional revision number for the file. COMMENT can be used
166to provide an initial description of FILE.
167
168`vc-register-switches' and `vc-sccs-register-switches' are passed to
169the SCCS command (in that order).
170
171Automatically retrieve a read-only version of the file with keywords
172expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
dc0f87c9 173 (let* ((dirname (or (file-name-directory file) ""))
7a004b71
GM
174 (basename (file-name-nondirectory file))
175 (project-file (vc-sccs-search-project-dir dirname basename)))
176 (let ((vc-name
177 (or project-file
dc0f87c9 178 (format (car vc-sccs-master-templates) dirname basename))))
0cccd8ff 179 (apply 'vc-do-command nil 0 "admin" vc-name
7a004b71
GM
180 (and rev (concat "-r" rev))
181 "-fb"
0cccd8ff 182 (concat "-i" (file-relative-name file))
7a004b71 183 (and comment (concat "-y" comment))
dc0f87c9 184 (vc-switches 'SCCS 'register)))
7a004b71
GM
185 (delete-file file)
186 (if vc-keep-workfiles
187 (vc-do-command nil 0 "get" (vc-name file)))))
188
8f98485f
AS
189(defun vc-sccs-responsible-p (file)
190 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
191 ;; TODO: check for all the patterns in vc-sccs-master-templates
192 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
193 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
194 (file-name-nondirectory file)))))
195
196(defun vc-sccs-checkin (file rev comment)
197 "SCCS-specific version of `vc-backend-checkin'."
dc0f87c9
SM
198 (apply 'vc-do-command nil 0 "delta" (vc-name file)
199 (if rev (concat "-r" rev))
200 (concat "-y" comment)
201 (vc-switches 'SCCS 'checkin))
202 (if vc-keep-workfiles
203 (vc-do-command nil 0 "get" (vc-name file))))
8f98485f 204
52876673
SM
205(defun vc-sccs-find-version (file rev buffer)
206 (apply 'vc-do-command
207 buffer 0 "get" (vc-name file)
208 "-s" ;; suppress diagnostic output
209 "-p"
210 (and rev
211 (concat "-r"
212 (vc-sccs-lookup-triple file rev)))
dc0f87c9 213 (vc-switches 'SCCS 'checkout)))
52876673
SM
214
215(defun vc-sccs-checkout (file &optional editable rev)
216 "Retrieve a copy of a saved version of SCCS controlled FILE.
1862f9ef 217EDITABLE non-nil means that the file should be writable and
52876673
SM
218locked. REV is the revision to check out."
219 (let ((file-buffer (get-file-buffer file))
7a004b71 220 switches)
52876673 221 (message "Checking out %s..." file)
7a004b71
GM
222 (save-excursion
223 ;; Change buffers to get local value of vc-checkout-switches.
224 (if file-buffer (set-buffer file-buffer))
dc0f87c9 225 (setq switches (vc-switches 'SCCS 'checkout))
7a004b71
GM
226 ;; Save this buffer's default-directory
227 ;; and use save-excursion to make sure it is restored
228 ;; in the same buffer it was saved in.
229 (let ((default-directory default-directory))
230 (save-excursion
231 ;; Adjust the default-directory so that the check-out creates
232 ;; the file in the right place.
52876673 233 (setq default-directory (file-name-directory file))
7a004b71 234
f1180544 235 (and rev (or (string= rev "")
bbd59337
AS
236 (not (stringp rev)))
237 (setq rev nil))
52876673
SM
238 (apply 'vc-do-command nil 0 "get" (vc-name file)
239 (if editable "-e")
240 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
241 switches))))
242 (message "Checking out %s...done" file)))
7a004b71 243
99739bbf 244(defun vc-sccs-revert (file &optional contents-done)
8f98485f
AS
245 "Revert FILE to the version it was based on."
246 (vc-do-command nil 0 "unget" (vc-name file))
247 (vc-do-command nil 0 "get" (vc-name file))
248 ;; Checking out explicit versions is not supported under SCCS, yet.
249 ;; We always "revert" to the latest version; therefore
250 ;; vc-workfile-version is cleared here so that it gets recomputed.
251 (vc-file-setprop file 'vc-workfile-version nil))
252
1862f9ef 253(defun vc-sccs-cancel-version (file editable)
8f98485f 254 "Undo the most recent checkin of FILE.
1862f9ef 255EDITABLE non-nil means previous version should be locked."
8f98485f
AS
256 (vc-do-command nil 0 "rmdel"
257 (vc-name file)
258 (concat "-r" (vc-workfile-version file)))
259 (vc-do-command nil 0 "get"
260 (vc-name file)
1862f9ef 261 (if editable "-e")))
8f98485f
AS
262
263(defun vc-sccs-steal-lock (file &optional rev)
264 "Steal the lock on the current workfile for FILE and revision REV."
265 (vc-do-command nil 0 "unget" (vc-name file) "-n" (if rev (concat "-r" rev)))
266 (vc-do-command nil 0 "get" (vc-name file) "-g" (if rev (concat "-r" rev))))
267
268\f
269;;;
270;;; History functions
271;;;
272
e556ef05 273(defun vc-sccs-print-log (file &optional buffer)
8f98485f 274 "Get change log associated with FILE."
e556ef05 275 (vc-do-command buffer 0 "prs" (vc-name file)))
8f98485f
AS
276
277(defun vc-sccs-logentry-check ()
278 "Check that the log entry in the current buffer is acceptable for SCCS."
279 (when (>= (buffer-size) 512)
280 (goto-char 512)
281 (error "Log must be less than 512 characters; point is now at pos 512")))
282
e556ef05 283(defun vc-sccs-diff (file &optional oldvers newvers buffer)
8f98485f
AS
284 "Get a difference report using SCCS between two versions of FILE."
285 (setq oldvers (vc-sccs-lookup-triple file oldvers))
286 (setq newvers (vc-sccs-lookup-triple file newvers))
e556ef05 287 (apply 'vc-do-command (or buffer "*vc-diff*") 1 "vcdiff" (vc-name file)
a1e9f194
AS
288 (append (list "-q"
289 (and oldvers (concat "-r" oldvers))
290 (and newvers (concat "-r" newvers)))
dc0f87c9 291 (vc-switches 'SCCS 'diff))))
8f98485f
AS
292
293\f
294;;;
295;;; Snapshot system
296;;;
297
298(defun vc-sccs-assign-name (file name)
299 "Assign to FILE's latest version a given NAME."
300 (vc-sccs-add-triple name file (vc-workfile-version file)))
301
302\f
303;;;
304;;; Miscellaneous
305;;;
306
307(defun vc-sccs-check-headers ()
308 "Check if the current file has any headers in it."
309 (save-excursion
310 (goto-char (point-min))
311 (re-search-forward "%[A-Z]%" nil t)))
312
313(defun vc-sccs-rename-file (old new)
314 ;; Move the master file (using vc-rcs-master-templates).
315 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
316 ;; Update the snapshot file.
317 (with-current-buffer
318 (find-file-noselect
319 (expand-file-name vc-sccs-name-assoc-file
320 (file-name-directory (vc-name old))))
321 (goto-char (point-min))
322 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
323 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
324 (replace-match (concat ":" new) nil nil))
325 (basic-save-buffer)
326 (kill-buffer (current-buffer))))
327
328\f
329;;;
330;;; Internal functions
331;;;
332
333;; This function is wrapped with `progn' so that the autoload cookie
334;; copies the whole function itself into loaddefs.el rather than just placing
335;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
336;; help us avoid loading vc-sccs.
337;;;###autoload
338(progn (defun vc-sccs-search-project-dir (dirname basename)
339 "Return the name of a master file in the SCCS project directory.
340Does not check whether the file exists but returns nil if it does not
341find any project directory."
342 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
343 (when project-dir
344 (if (file-name-absolute-p project-dir)
345 (setq dirs '("SCCS" ""))
346 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
347 (setq project-dir (expand-file-name (concat "~" project-dir))))
348 (while (and (not dir) dirs)
349 (setq dir (expand-file-name (car dirs) project-dir))
350 (unless (file-directory-p dir)
351 (setq dir nil)
352 (setq dirs (cdr dirs))))
353 (and dir (expand-file-name (concat "s." basename) dir))))))
354
355(defun vc-sccs-lock-file (file)
356 "Generate lock file name corresponding to FILE."
357 (let ((master (vc-name file)))
358 (and
359 master
360 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
361 (replace-match "p." t t master 2))))
362
363(defun vc-sccs-parse-locks ()
364 "Parse SCCS locks in current buffer.
365The result is a list of the form ((VERSION . USER) (VERSION . USER) ...)."
366 (let (master-locks)
367 (goto-char (point-min))
368 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
369 nil t)
370 (setq master-locks
371 (cons (cons (match-string 1) (match-string 2)) master-locks)))
372 ;; FIXME: is it really necessary to reverse ?
373 (nreverse master-locks)))
374
375(defun vc-sccs-add-triple (name file rev)
376 (with-current-buffer
377 (find-file-noselect
378 (expand-file-name vc-sccs-name-assoc-file
379 (file-name-directory (vc-name file))))
380 (goto-char (point-max))
381 (insert name "\t:\t" file "\t" rev "\n")
382 (basic-save-buffer)
383 (kill-buffer (current-buffer))))
384
385(defun vc-sccs-lookup-triple (file name)
386 "Return the numeric version corresponding to a named snapshot of FILE.
387If NAME is nil or a version number string it's just passed through."
388 (if (or (null name)
389 (let ((firstchar (aref name 0)))
390 (and (>= firstchar ?0) (<= firstchar ?9))))
391 name
392 (with-temp-buffer
393 (vc-insert-file
394 (expand-file-name vc-sccs-name-assoc-file
395 (file-name-directory (vc-name file))))
396 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
7a004b71
GM
397
398(provide 'vc-sccs)
399
ab5796a9 400;;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
7a004b71 401;;; vc-sccs.el ends here