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