Fix typos.
[bpt/emacs.git] / lisp / vc-sccs.el
CommitLineData
7a004b71
GM
1;;; vc-sccs.el --- support for SCCS version-control
2
0d30b337 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
ae940284 4;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
59465d84 5;; Free Software Foundation, Inc.
7a004b71
GM
6
7;; Author: FSF (see vc.el for full credits)
8;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9
7a004b71
GM
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
7a004b71 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
7a004b71
GM
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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
7a004b71
GM
24
25;;; Commentary:
26
27;;; Code:
28
f1180544 29(eval-when-compile
a1e9f194
AS
30 (require 'vc))
31
f1180544 32;;;
8f98485f
AS
33;;; Customization options
34;;;
35
14b11401
SM
36;; ;; Maybe a better solution is to not use "get" but "sccs get".
37;; (defcustom vc-sccs-path
38;; (let ((path ()))
39;; (dolist (dir '("/usr/sccs" "/usr/lib/sccs" "/usr/libexec/sccs"))
40;; (if (file-directory-p dir)
41;; (push dir path)))
42;; path)
43;; "List of extra directories to search for SCCS commands."
44;; :type '(repeat directory)
45;; :group 'vc)
46
7a004b71 47(defcustom vc-sccs-register-switches nil
12fcd25a 48 "Switches for registering a file in SCCS.
a7cafade 49A string or list of strings passed to the checkin program by
12fcd25a
GM
50\\[vc-register]. If nil, use the value of `vc-register-switches'.
51If t, use no switches."
52 :type '(choice (const :tag "Unspecified" nil)
53 (const :tag "None" t)
7a004b71 54 (string :tag "Argument String")
12fcd25a 55 (repeat :tag "Argument List" :value ("") string))
a7cafade 56 :version "21.1"
7a004b71
GM
57 :group 'vc)
58
a1e9f194 59(defcustom vc-sccs-diff-switches nil
3edf7a64
GM
60 "String or list of strings specifying switches for SCCS diff under VC.
61If nil, use the value of `vc-diff-switches'. If t, use no switches."
62 :type '(choice (const :tag "Unspecified" nil)
63 (const :tag "None" t)
a1e9f194 64 (string :tag "Argument String")
3edf7a64 65 (repeat :tag "Argument List" :value ("") string))
a1e9f194
AS
66 :version "21.1"
67 :group 'vc)
68
7a004b71 69(defcustom vc-sccs-header (or (cdr (assoc 'SCCS vc-header-alist)) '("%W%"))
9201cc28 70 "Header keywords to be inserted by `vc-insert-headers'."
8b7c8991 71 :type '(repeat string)
7a004b71
GM
72 :group 'vc)
73
74;;;###autoload
75(defcustom vc-sccs-master-templates
76 '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir)
9201cc28 77 "Where to look for SCCS master files.
7a004b71
GM
78For a description of possible values, see `vc-check-master-templates'."
79 :type '(choice (const :tag "Use standard SCCS file names"
80 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
81 (repeat :tag "User-specified"
82 (choice string
83 function)))
a7cafade 84 :version "21.1"
7a004b71
GM
85 :group 'vc)
86
8f98485f
AS
87\f
88;;;
89;;; Internal variables
90;;;
91
099bd78a
SM
92(defconst vc-sccs-name-assoc-file "VC-names")
93
8f98485f 94\f
8cdd17b4
ER
95;;; Properties of the backend
96
14b11401 97(defun vc-sccs-revision-granularity () 'file)
70e2f6c7 98(defun vc-sccs-checkout-model (files) 'locking)
8cdd17b4 99
8f98485f
AS
100;;;
101;;; State-querying functions
102;;;
103
14b11401
SM
104;; The autoload cookie below places vc-sccs-registered directly into
105;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
106;; every file that is visited. The definition is repeated below
107;; so that Help and etags can find it.
9ef33737
AS
108
109;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
af65391b 110(defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
7a004b71
GM
111
112(defun vc-sccs-state (file)
113 "SCCS-specific function to compute the version control state."
3702367b
ER
114 (if (not (vc-sccs-registered file))
115 'unregistered
116 (with-temp-buffer
117 (if (vc-insert-file (vc-sccs-lock-file file))
118 (let* ((locks (vc-sccs-parse-locks))
119 (working-revision (vc-working-revision file))
120 (locking-user (cdr (assoc working-revision locks))))
121 (if (not locking-user)
122 (if (vc-workfile-unchanged-p file)
123 'up-to-date
124 'unlocked-changes)
125 (if (string= locking-user (vc-user-login-name file))
126 'edited
127 locking-user)))
128 'up-to-date))))
7a004b71
GM
129
130(defun vc-sccs-state-heuristic (file)
131 "SCCS-specific state heuristic."
132 (if (not (vc-mistrust-permissions file))
133 ;; This implementation assumes that any file which is under version
134 ;; control and has -rw-r--r-- is locked by its owner. This is true
135 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
136 ;; We have to be careful not to exclude files with execute bits on;
137 ;; scripts can be under version control too. Also, we must ignore the
138 ;; group-read and other-read bits, since paranoid users turn them off.
bbd88348
AS
139 (let* ((attributes (file-attributes file 'string))
140 (owner-name (nth 2 attributes))
7a004b71
GM
141 (permissions (nth 8 attributes)))
142 (if (string-match ".r-..-..-." permissions)
143 'up-to-date
144 (if (string-match ".rw..-..-." permissions)
145 (if (file-ownership-preserved-p file)
146 'edited
bbd88348 147 owner-name)
8bfde4be
AS
148 ;; Strange permissions.
149 ;; Fall through to real state computation.
150 (vc-sccs-state file))))
151 (vc-sccs-state file)))
7a004b71 152
c1b51374 153(defun vc-sccs-dir-status (dir update-function)
4b1a01b3
DN
154 ;; FIXME: this function should be rewritten, using `vc-expand-dirs'
155 ;; is not TRTD because it returns files from multiple backends.
156 ;; It should also return 'unregistered files.
157
158 ;; Doing lots of individual VC-state calls is painful, but
5a9de6d0 159 ;; there is no better option in SCCS-land.
90e9ca17
DN
160 (let ((flist (vc-expand-dirs (list dir)))
161 (result nil))
162 (dolist (file flist)
163 (let ((state (vc-state file))
164 (frel (file-relative-name file)))
4b1a01b3
DN
165 (when (and (eq (vc-backend file) 'SCCS)
166 (not (eq state 'up-to-date)))
167 (push (list frel state) result))))
c1b51374 168 (funcall update-function result)))
90e9ca17 169
ac3f4c6f
ER
170(defun vc-sccs-working-revision (file)
171 "SCCS-specific version of `vc-working-revision'."
7a004b71 172 (with-temp-buffer
5b5afd50 173 ;; The working revision is always the latest revision number.
8bfde4be
AS
174 ;; To find this number, search the entire delta table,
175 ;; rather than just the first entry, because the
5b5afd50 176 ;; first entry might be a deleted ("R") revision.
8bfde4be 177 (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
7a004b71
GM
178 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
179
7a004b71 180(defun vc-sccs-workfile-unchanged-p (file)
fa63cb6d 181 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
2888a97e 182 (zerop (apply 'vc-do-command "*vc*" 1 "vcdiff" (vc-name file)
a1e9f194 183 (list "--brief" "-q"
ac3f4c6f 184 (concat "-r" (vc-working-revision file))))))
7a004b71 185
7a004b71 186\f
8f98485f
AS
187;;;
188;;; State-changing functions
189;;;
7a004b71 190
14b11401
SM
191(defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
192 ;; (let ((load-path (append vc-sccs-path load-path)))
193 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
2888a97e 194 (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
14b11401 195
8cdd17b4
ER
196(defun vc-sccs-create-repo ()
197 "Create a new SCCS repository."
198 ;; SCCS is totally file-oriented, so all we have to do is make the directory
199 (make-directory "SCCS"))
200
201(defun vc-sccs-register (files &optional rev comment)
202 "Register FILES into the SCCS version-control system.
7a004b71 203REV is the optional revision number for the file. COMMENT can be used
8cdd17b4 204to provide an initial description of FILES.
12fcd25a
GM
205Passes either `vc-sccs-register-switches' or `vc-register-switches'
206to the SCCS command.
7a004b71 207
8cdd17b4 208Automatically retrieve a read-only version of the files with keywords
7a004b71 209expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
8cdd17b4 210 (dolist (file files)
dc0f87c9 211 (let* ((dirname (or (file-name-directory file) ""))
7a004b71
GM
212 (basename (file-name-nondirectory file))
213 (project-file (vc-sccs-search-project-dir dirname basename)))
214 (let ((vc-name
215 (or project-file
dc0f87c9 216 (format (car vc-sccs-master-templates) dirname basename))))
14b11401 217 (apply 'vc-sccs-do-command nil 0 "admin" vc-name
8cdd17b4 218 (and rev (not (string= rev "")) (concat "-r" rev))
7a004b71 219 "-fb"
0cccd8ff 220 (concat "-i" (file-relative-name file))
7a004b71 221 (and comment (concat "-y" comment))
dc0f87c9 222 (vc-switches 'SCCS 'register)))
7a004b71
GM
223 (delete-file file)
224 (if vc-keep-workfiles
14b11401 225 (vc-sccs-do-command nil 0 "get" (vc-name file))))))
7a004b71 226
8f98485f
AS
227(defun vc-sccs-responsible-p (file)
228 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
229 ;; TODO: check for all the patterns in vc-sccs-master-templates
230 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
231 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
232 (file-name-nondirectory file)))))
233
8cdd17b4 234(defun vc-sccs-checkin (files rev comment)
8f98485f 235 "SCCS-specific version of `vc-backend-checkin'."
e7290559 236 (dolist (file (vc-expand-dirs files))
14b11401 237 (apply 'vc-sccs-do-command nil 0 "delta" (vc-name file)
8cdd17b4
ER
238 (if rev (concat "-r" rev))
239 (concat "-y" comment)
240 (vc-switches 'SCCS 'checkin))
241 (if vc-keep-workfiles
14b11401 242 (vc-sccs-do-command nil 0 "get" (vc-name file)))))
8f98485f 243
ac3f4c6f 244(defun vc-sccs-find-revision (file rev buffer)
14b11401 245 (apply 'vc-sccs-do-command
52876673
SM
246 buffer 0 "get" (vc-name file)
247 "-s" ;; suppress diagnostic output
248 "-p"
249 (and rev
250 (concat "-r"
251 (vc-sccs-lookup-triple file rev)))
dc0f87c9 252 (vc-switches 'SCCS 'checkout)))
52876673
SM
253
254(defun vc-sccs-checkout (file &optional editable rev)
5b5afd50 255 "Retrieve a copy of a saved revision of SCCS controlled FILE.
e7290559 256If FILE is a directory, all version-controlled files beneath are checked out.
1862f9ef 257EDITABLE non-nil means that the file should be writable and
52876673 258locked. REV is the revision to check out."
a2a413ad 259 (if (file-directory-p file)
e7290559
ER
260 (mapc 'vc-sccs-checkout (vc-expand-dirs (list file)))
261 (let ((file-buffer (get-file-buffer file))
262 switches)
263 (message "Checking out %s..." file)
264 (save-excursion
265 ;; Change buffers to get local value of vc-checkout-switches.
266 (if file-buffer (set-buffer file-buffer))
267 (setq switches (vc-switches 'SCCS 'checkout))
268 ;; Save this buffer's default-directory
269 ;; and use save-excursion to make sure it is restored
270 ;; in the same buffer it was saved in.
271 (let ((default-directory default-directory))
272 (save-excursion
273 ;; Adjust the default-directory so that the check-out creates
274 ;; the file in the right place.
275 (setq default-directory (file-name-directory file))
276
277 (and rev (or (string= rev "")
278 (not (stringp rev)))
279 (setq rev nil))
280 (apply 'vc-sccs-do-command nil 0 "get" (vc-name file)
281 (if editable "-e")
282 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
283 switches))))
284 (message "Checking out %s...done" file))))
7a004b71 285
5b5afd50 286(defun vc-sccs-rollback (files)
e7290559
ER
287 "Roll back, undoing the most recent checkins of FILES. Directories
288are expanded to all version-controlled subfiles."
289 (setq files (vc-expand-dirs files))
8cdd17b4 290 (if (not files)
5a0c3f56 291 (error "SCCS backend doesn't support directory-level rollback"))
8cdd17b4 292 (dolist (file files)
ac3f4c6f 293 (let ((discard (vc-working-revision file)))
a2a413ad 294 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
8cdd17b4
ER
295 discard file)))
296 (error "Aborted"))
297 (message "Removing revision %s from %s..." discard file)
14b11401
SM
298 (vc-sccs-do-command nil 0 "rmdel"
299 (vc-name file) (concat "-r" discard))
300 (vc-sccs-do-command nil 0 "get" (vc-name file) nil))))
8cdd17b4 301
99739bbf 302(defun vc-sccs-revert (file &optional contents-done)
e7290559
ER
303 "Revert FILE to the version it was based on. If FILE is a directory,
304revert all subfiles."
a2a413ad 305 (if (file-directory-p file)
e7290559
ER
306 (mapc 'vc-sccs-revert (vc-expand-dirs (list file)))
307 (vc-sccs-do-command nil 0 "unget" (vc-name file))
308 (vc-sccs-do-command nil 0 "get" (vc-name file))
309 ;; Checking out explicit revisions is not supported under SCCS, yet.
310 ;; We always "revert" to the latest revision; therefore
311 ;; vc-working-revision is cleared here so that it gets recomputed.
312 (vc-file-setprop file 'vc-working-revision nil)))
8f98485f 313
8f98485f
AS
314(defun vc-sccs-steal-lock (file &optional rev)
315 "Steal the lock on the current workfile for FILE and revision REV."
a2a413ad 316 (if (file-directory-p file)
e7290559
ER
317 (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file)))
318 (vc-sccs-do-command nil 0 "unget"
319 (vc-name file) "-n" (if rev (concat "-r" rev)))
320 (vc-sccs-do-command nil 0 "get"
321 (vc-name file) "-g" (if rev (concat "-r" rev)))))
8f98485f 322
9b64a7f0
ER
323(defun vc-sccs-modify-change-comment (files rev comment)
324 "Modify (actually, append to) the change comments for FILES on a specified REV."
e7290559 325 (dolist (file (vc-expand-dirs files))
a2a413ad 326 (vc-sccs-do-command nil 0 "cdc" (vc-name file)
14b11401 327 (concat "-y" comment) (concat "-r" rev))))
9b64a7f0 328
8f98485f
AS
329\f
330;;;
331;;; History functions
332;;;
333
32ba3abc 334(defun vc-sccs-print-log (files &optional buffer shortlog)
8cdd17b4 335 "Get change log associated with FILES."
e7290559 336 (setq files (vc-expand-dirs files))
14b11401 337 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-name files)))
8cdd17b4 338
8cdd17b4
ER
339(defun vc-sccs-diff (files &optional oldvers newvers buffer)
340 "Get a difference report using SCCS between two filesets."
e7290559 341 (setq files (vc-expand-dirs files))
e46e905a
GM
342 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
343 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
59465d84 344 (apply 'vc-do-command (or buffer "*vc-diff*")
8cdd17b4 345 1 "vcdiff" (mapcar 'vc-name (vc-expand-dirs files))
a1e9f194
AS
346 (append (list "-q"
347 (and oldvers (concat "-r" oldvers))
348 (and newvers (concat "-r" newvers)))
dc0f87c9 349 (vc-switches 'SCCS 'diff))))
8f98485f
AS
350
351\f
352;;;
370fded4
ER
353;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
354;;; our own set of name-to-revision mappings.
8f98485f
AS
355;;;
356
370fded4
ER
357(defun vc-sccs-create-tag (backend dir name branchp)
358 (when branchp
a2a413ad 359 (error "SCCS backend %s does not support module branches" backend))
370fded4
ER
360 (let ((result (vc-tag-precondition dir)))
361 (if (stringp result)
362 (error "File %s is not up-to-date" result)
363 (vc-file-tree-walk
364 dir
365 (lambda (f)
366 (vc-sccs-add-triple name f (vc-working-revision f)))))))
8f98485f
AS
367
368\f
369;;;
370;;; Miscellaneous
371;;;
372
3b64d86b
DN
373(defun vc-sccs-previous-revision (file rev)
374 (vc-call-backend 'RCS 'previous-revision file rev))
375
376(defun vc-sccs-next-revision (file rev)
377 (vc-call-backend 'RCS 'next-revision file rev))
378
8f98485f
AS
379(defun vc-sccs-check-headers ()
380 "Check if the current file has any headers in it."
381 (save-excursion
382 (goto-char (point-min))
383 (re-search-forward "%[A-Z]%" nil t)))
384
385(defun vc-sccs-rename-file (old new)
386 ;; Move the master file (using vc-rcs-master-templates).
387 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
370fded4 388 ;; Update the tag file.
8f98485f
AS
389 (with-current-buffer
390 (find-file-noselect
391 (expand-file-name vc-sccs-name-assoc-file
392 (file-name-directory (vc-name old))))
393 (goto-char (point-min))
394 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
395 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
396 (replace-match (concat ":" new) nil nil))
397 (basic-save-buffer)
398 (kill-buffer (current-buffer))))
399
77bf3f54
DN
400(defun vc-sccs-find-file-hook ()
401 ;; If the file is locked by some other user, make
402 ;; the buffer read-only. Like this, even root
403 ;; cannot modify a file that someone else has locked.
d56fdcd2
DN
404 (and (stringp (vc-state buffer-file-name 'SCCS))
405 (setq buffer-read-only t)))
77bf3f54 406
8f98485f
AS
407\f
408;;;
409;;; Internal functions
410;;;
411
412;; This function is wrapped with `progn' so that the autoload cookie
413;; copies the whole function itself into loaddefs.el rather than just placing
414;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
415;; help us avoid loading vc-sccs.
416;;;###autoload
417(progn (defun vc-sccs-search-project-dir (dirname basename)
418 "Return the name of a master file in the SCCS project directory.
419Does not check whether the file exists but returns nil if it does not
420find any project directory."
421 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
422 (when project-dir
423 (if (file-name-absolute-p project-dir)
424 (setq dirs '("SCCS" ""))
425 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
426 (setq project-dir (expand-file-name (concat "~" project-dir))))
427 (while (and (not dir) dirs)
428 (setq dir (expand-file-name (car dirs) project-dir))
429 (unless (file-directory-p dir)
430 (setq dir nil)
431 (setq dirs (cdr dirs))))
432 (and dir (expand-file-name (concat "s." basename) dir))))))
433
434(defun vc-sccs-lock-file (file)
435 "Generate lock file name corresponding to FILE."
436 (let ((master (vc-name file)))
437 (and
438 master
439 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
440 (replace-match "p." t t master 2))))
441
442(defun vc-sccs-parse-locks ()
443 "Parse SCCS locks in current buffer.
5b5afd50 444The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
8f98485f
AS
445 (let (master-locks)
446 (goto-char (point-min))
447 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
448 nil t)
449 (setq master-locks
450 (cons (cons (match-string 1) (match-string 2)) master-locks)))
451 ;; FIXME: is it really necessary to reverse ?
452 (nreverse master-locks)))
453
454(defun vc-sccs-add-triple (name file rev)
455 (with-current-buffer
456 (find-file-noselect
457 (expand-file-name vc-sccs-name-assoc-file
458 (file-name-directory (vc-name file))))
459 (goto-char (point-max))
460 (insert name "\t:\t" file "\t" rev "\n")
461 (basic-save-buffer)
462 (kill-buffer (current-buffer))))
463
464(defun vc-sccs-lookup-triple (file name)
370fded4 465 "Return the numeric revision corresponding to a named tag of FILE.
5b5afd50 466If NAME is nil or a revision number string it's just passed through."
8f98485f
AS
467 (if (or (null name)
468 (let ((firstchar (aref name 0)))
469 (and (>= firstchar ?0) (<= firstchar ?9))))
470 name
471 (with-temp-buffer
472 (vc-insert-file
473 (expand-file-name vc-sccs-name-assoc-file
474 (file-name-directory (vc-name file))))
475 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
7a004b71
GM
476
477(provide 'vc-sccs)
478
fa63cb6d 479;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
7a004b71 480;;; vc-sccs.el ends here