Avoid losing Ctrl-C keystrokes in compilation mode on MS-Windows
[emacs.git] / lisp / vc / vc-sccs.el
blobfd27db381d312d273cef6702b564a68d9a298a0e
1 ;;; vc-sccs.el --- support for SCCS version-control -*- lexical-binding:t -*-
3 ;; Copyright (C) 1992-2017 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Package: vc
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile
29 (require 'vc))
31 ;;;
32 ;;; Customization options
33 ;;;
35 ;; ;; Maybe a better solution is to not use "get" but "sccs get".
36 ;; ;; Note for GNU CSSC, you can parse sccs -V to get the libexec path.
37 ;; (defcustom vc-sccs-path
38 ;; (prune-directory-list '("/usr/ccs/bin" "/usr/sccs" "/usr/lib/sccs"
39 ;; "/usr/libexec/sccs"))
40 ;; "List of extra directories to search for SCCS commands."
41 ;; :type '(repeat directory)
42 ;; :group 'vc)
44 (defgroup vc-sccs nil
45 "VC SCCS backend."
46 :version "24.1"
47 :group 'vc)
49 (defcustom vc-sccs-register-switches nil
50 "Switches for registering a file in SCCS.
51 A string or list of strings passed to the checkin program by
52 \\[vc-register]. If nil, use the value of `vc-register-switches'.
53 If t, use no switches."
54 :type '(choice (const :tag "Unspecified" nil)
55 (const :tag "None" t)
56 (string :tag "Argument String")
57 (repeat :tag "Argument List" :value ("") string))
58 :version "21.1"
59 :group 'vc-sccs)
61 (defcustom vc-sccs-diff-switches nil
62 "String or list of strings specifying switches for SCCS diff under VC.
63 If nil, use the value of `vc-diff-switches'. If t, use no switches."
64 :type '(choice (const :tag "Unspecified" nil)
65 (const :tag "None" t)
66 (string :tag "Argument String")
67 (repeat :tag "Argument List" :value ("") string))
68 :version "21.1"
69 :group 'vc-sccs)
71 (defcustom vc-sccs-header '("%W%")
72 "Header keywords to be inserted by `vc-insert-headers'."
73 :type '(repeat string)
74 :version "24.1" ; no longer consult the obsolete vc-header-alist
75 :group 'vc-sccs)
77 ;; This needs to be autoloaded because vc-sccs-registered uses it (via
78 ;; vc-default-registered), and vc-hooks needs to be able to check
79 ;; for a registered backend without loading every backend.
80 ;;;###autoload
81 (defcustom vc-sccs-master-templates
82 (purecopy '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
83 "Where to look for SCCS master files.
84 For a description of possible values, see `vc-check-master-templates'."
85 :type '(choice (const :tag "Use standard SCCS file names"
86 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
87 (repeat :tag "User-specified"
88 (choice string
89 function)))
90 :version "21.1"
91 :group 'vc-sccs)
94 ;;;
95 ;;; Internal variables
96 ;;;
98 (defconst vc-sccs-name-assoc-file "VC-names")
101 ;;; Properties of the backend
103 (defun vc-sccs-revision-granularity () 'file)
104 (defun vc-sccs-checkout-model (_files) 'locking)
107 ;;; State-querying functions
110 ;; The autoload cookie below places vc-sccs-registered directly into
111 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
112 ;; every file that is visited.
113 ;;;###autoload
114 (progn
115 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f)))
117 (defun vc-sccs-state (file)
118 "SCCS-specific function to compute the version control state."
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-sccs-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))))
135 (autoload 'vc-expand-dirs "vc")
137 (defun vc-sccs-dir-status-files (dir files update-function)
138 (if (not files) (setq files (vc-expand-dirs (list dir) 'RCS)))
139 (let ((result nil))
140 (dolist (file files)
141 (let ((state (vc-state file))
142 (frel (file-relative-name file)))
143 (when (and (eq (vc-backend file) 'SCCS)
144 (not (eq state 'up-to-date)))
145 (push (list frel state) result))))
146 (funcall update-function result)))
148 (autoload 'vc-master-name "vc-filewise")
150 (defun vc-sccs-working-revision (file)
151 "SCCS-specific version of `vc-working-revision'."
152 (when (and (file-regular-p file) (vc-master-name file))
153 (with-temp-buffer
154 ;; The working revision is always the latest revision number.
155 ;; To find this number, search the entire delta table,
156 ;; rather than just the first entry, because the
157 ;; first entry might be a deleted ("R") revision.
158 (vc-insert-file (vc-master-name file) "^\001e\n\001[^s]")
159 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1))))
161 ;; Cf vc-sccs-find-revision.
162 (defun vc-sccs-write-revision (file outfile &optional rev)
163 "Write the SCCS version of input file FILE to output file OUTFILE.
164 Optional string REV is a revision."
165 (with-temp-buffer
166 (apply 'vc-sccs-do-command t 0 "get" (vc-master-name file)
167 (append '("-s" "-p" "-k") ; -k: no keyword expansion
168 (if rev (list (concat "-r" rev)))))
169 (write-region nil nil outfile nil 'silent)))
171 (defun vc-sccs-workfile-unchanged-p (file)
172 "Has FILE remained unchanged since last checkout?"
173 (let ((tempfile (make-temp-file "vc-sccs")))
174 (unwind-protect
175 (progn
176 (vc-sccs-write-revision file tempfile (vc-working-revision file))
177 (zerop (vc-do-command "*vc*" 1 "cmp" file tempfile)))
178 (delete-file tempfile))))
182 ;;; State-changing functions
185 (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
186 ;; (let ((load-path (append vc-sccs-path load-path)))
187 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
188 (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
190 (defun vc-sccs-create-repo ()
191 "Create a new SCCS repository."
192 ;; SCCS is totally file-oriented, so all we have to do is make the directory
193 (make-directory "SCCS"))
195 (autoload 'vc-switches "vc")
197 (defun vc-sccs-register (files &optional comment)
198 "Register FILES into the SCCS version-control system.
199 Automatically retrieve a read-only version of the files with keywords expanded.
200 COMMENT can be used to provide an initial description of FILES.
201 Passes either `vc-sccs-register-switches' or `vc-register-switches'
202 to the SCCS command."
203 (dolist (file files)
204 (let* ((dirname (or (file-name-directory file) ""))
205 (basename (file-name-nondirectory file))
206 (project-file (vc-sccs-search-project-dir dirname basename)))
207 (let ((vc-master-name
208 (or project-file
209 (format (car vc-sccs-master-templates) dirname basename))))
210 (apply 'vc-sccs-do-command nil 0 "admin" vc-master-name
211 "-fb"
212 (concat "-i" (file-relative-name file))
213 (and comment (concat "-y" comment))
214 (vc-switches 'SCCS 'register)))
215 (delete-file file)
216 (vc-sccs-do-command nil 0 "get" (vc-master-name file)))))
218 (defun vc-sccs-responsible-p (file)
219 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
220 ;; TODO: check for all the patterns in vc-sccs-master-templates
221 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
222 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
223 (file-name-nondirectory file)))))
225 (defun vc-sccs-checkin (files comment &optional rev)
226 "SCCS-specific version of `vc-backend-checkin'."
227 (dolist (file (vc-expand-dirs files 'SCCS))
228 (apply 'vc-sccs-do-command nil 0 "delta" (vc-master-name file)
229 (if rev (concat "-r" rev))
230 (concat "-y" comment)
231 (vc-switches 'SCCS 'checkin))
232 (vc-sccs-do-command nil 0 "get" (vc-master-name file))))
234 (defun vc-sccs-find-revision (file rev buffer)
235 (apply 'vc-sccs-do-command
236 buffer 0 "get" (vc-master-name file)
237 "-s" ;; suppress diagnostic output
238 "-p"
239 (and rev
240 (concat "-r"
241 (vc-sccs-lookup-triple file rev)))
242 (vc-switches 'SCCS 'checkout)))
244 (defun vc-sccs-checkout (file &optional rev)
245 "Retrieve a copy of a saved revision of SCCS controlled FILE.
246 If FILE is a directory, all version-controlled files beneath are checked out.
247 EDITABLE non-nil means that the file should be writable and
248 locked. REV is the revision to check out."
249 (if (file-directory-p file)
250 (mapc 'vc-sccs-checkout (vc-expand-dirs (list file) 'SCCS))
251 (let ((file-buffer (get-file-buffer file))
252 switches)
253 (message "Checking out %s..." file)
254 (save-excursion
255 ;; Change buffers to get local value of vc-checkout-switches.
256 (if file-buffer (set-buffer file-buffer))
257 (setq switches (vc-switches 'SCCS 'checkout))
258 ;; Save this buffer's default-directory
259 ;; and use save-excursion to make sure it is restored
260 ;; in the same buffer it was saved in.
261 (let ((default-directory default-directory))
262 (save-excursion
263 ;; Adjust the default-directory so that the check-out creates
264 ;; the file in the right place.
265 (setq default-directory (file-name-directory file))
267 (and rev (or (string= rev "")
268 (not (stringp rev)))
269 (setq rev nil))
270 (apply 'vc-sccs-do-command nil 0 "get" (vc-master-name file)
271 "-e"
272 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
273 switches))))
274 (message "Checking out %s...done" file))))
276 (defun vc-sccs-revert (file &optional _contents-done)
277 "Revert FILE to the version it was based on. If FILE is a directory,
278 revert all subfiles."
279 (if (file-directory-p file)
280 (mapc 'vc-sccs-revert (vc-expand-dirs (list file) 'SCCS))
281 (vc-sccs-do-command nil 0 "unget" (vc-master-name file))
282 (vc-sccs-do-command nil 0 "get" (vc-master-name file))
283 ;; Checking out explicit revisions is not supported under SCCS, yet.
284 ;; We always "revert" to the latest revision; therefore
285 ;; vc-working-revision is cleared here so that it gets recomputed.
286 (vc-file-setprop file 'vc-working-revision nil)))
288 (defun vc-sccs-steal-lock (file &optional rev)
289 "Steal the lock on the current workfile for FILE and revision REV."
290 (if (file-directory-p file)
291 (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file) 'SCCS))
292 (vc-sccs-do-command nil 0 "unget"
293 (vc-master-name file) "-n" (if rev (concat "-r" rev)))
294 (vc-sccs-do-command nil 0 "get"
295 (vc-master-name file) "-g" (if rev (concat "-r" rev)))))
297 (defun vc-sccs-modify-change-comment (files rev comment)
298 "Modify (actually, append to) the change comments for FILES on a specified REV."
299 (dolist (file (vc-expand-dirs files 'SCCS))
300 (vc-sccs-do-command nil 0 "cdc" (vc-master-name file)
301 (concat "-y" comment) (concat "-r" rev))))
305 ;;; History functions
308 (defun vc-sccs-print-log (files buffer &optional _shortlog _start-revision-ignored limit)
309 "Print commit log associated with FILES into specified BUFFER.
310 Remaining arguments are ignored."
311 (setq files (vc-expand-dirs files 'SCCS))
312 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-master-name files))
313 (when limit 'limit-unsupported))
315 (autoload 'vc-setup-buffer "vc-dispatcher")
316 (autoload 'vc-delistify "vc-dispatcher")
318 (defvar w32-quote-process-args)
320 ;; FIXME use sccsdiff if present?
321 (defun vc-sccs-diff (files &optional oldvers newvers buffer _async)
322 "Get a difference report using SCCS between two filesets."
323 (setq files (vc-expand-dirs files 'SCCS))
324 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
325 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
326 (or buffer (setq buffer "*vc-diff*"))
327 ;; We have to reimplement pieces of vc-do-command, because
328 ;; we want to run multiple external commands, and only do the setup
329 ;; and exit pieces once.
330 (save-current-buffer
331 (unless (or (eq buffer t)
332 (and (stringp buffer) (string= (buffer-name) buffer))
333 (eq buffer (current-buffer)))
334 (vc-setup-buffer buffer))
335 (let* ((fake-flags (append (vc-switches 'SCCS 'diff)
336 (if oldvers (list (concat " -r" oldvers)))
337 (if newvers (list (concat " -r" newvers)))))
338 (fake-command
339 (format "diff%s %s"
340 (if fake-flags
341 (concat " " (mapconcat 'identity fake-flags " "))
343 (vc-delistify files)))
344 (status 0)
345 (oldproc (get-buffer-process (current-buffer))))
346 (when vc-command-messages
347 (message "Running %s in foreground..." fake-command))
348 (if oldproc (delete-process oldproc))
349 (dolist (file files)
350 (let ((oldfile (make-temp-file "vc-sccs"))
351 newfile)
352 (unwind-protect
353 (progn
354 (vc-sccs-write-revision file oldfile oldvers)
355 (if newvers
356 (vc-sccs-write-revision file (setq newfile
357 (make-temp-file "vc-sccs"))
358 newvers))
359 (let* ((inhibit-read-only t)
360 (buffer-undo-list t)
361 (process-environment
362 (cons "LC_MESSAGES=C" process-environment))
363 (w32-quote-process-args t)
364 (this-status
365 (apply 'process-file "diff" nil t nil
366 (append (vc-switches 'SCCS 'diff)
367 (list oldfile
368 (or newfile
369 (file-relative-name file)))))))
370 (or (integerp this-status) (setq status 'error))
371 (and (integerp status)
372 (> this-status status)
373 (setq status this-status))))
374 (delete-file oldfile)
375 (if newfile (delete-file newfile)))))
376 (when (or (not (integerp status)) (> status 1))
377 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
378 (pop-to-buffer (current-buffer))
379 (goto-char (point-min))
380 (shrink-window-if-larger-than-buffer))
381 (error "Running %s...FAILED (%s)" fake-command
382 (if (integerp status) (format "status %d" status) status)))
383 (when vc-command-messages
384 (message "Running %s...OK = %d" fake-command status))
385 ;; Should we pretend we ran sccsdiff instead?
386 ;; This might not actually be a valid diff command.
387 (run-hook-with-args 'vc-post-command-functions "diff" files fake-flags)
388 status)))
392 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
393 ;;; our own set of name-to-revision mappings.
396 (autoload 'vc-tag-precondition "vc")
397 (declare-function vc-file-tree-walk "vc" (dirname func &rest args))
399 (defun vc-sccs-create-tag (dir name branchp)
400 (when branchp
401 (error "SCCS backend does not support module branches"))
402 (let ((result (vc-tag-precondition dir)))
403 (if (stringp result)
404 (error "File %s is not up-to-date" result)
405 (vc-file-tree-walk
407 (lambda (f)
408 (vc-sccs-add-triple name f (vc-working-revision f)))))))
412 ;;; Miscellaneous
415 (defun vc-sccs-previous-revision (file rev)
416 (vc-call-backend 'RCS 'previous-revision file rev))
418 (defun vc-sccs-next-revision (file rev)
419 (vc-call-backend 'RCS 'next-revision file rev))
421 (defun vc-sccs-check-headers ()
422 "Check if the current file has any headers in it."
423 (save-excursion
424 (goto-char (point-min))
425 (re-search-forward "%[A-Z]%" nil t)))
427 (autoload 'vc-rename-master "vc-filewise")
429 (defun vc-sccs-rename-file (old new)
430 ;; Move the master file (using vc-rcs-master-templates).
431 (vc-rename-master (vc-master-name old) new vc-sccs-master-templates)
432 ;; Update the tag file.
433 (with-current-buffer
434 (find-file-noselect
435 (expand-file-name vc-sccs-name-assoc-file
436 (file-name-directory (vc-master-name old))))
437 (goto-char (point-min))
438 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
439 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
440 (replace-match (concat ":" new) nil nil))
441 (basic-save-buffer)
442 (kill-buffer (current-buffer))))
444 (defun vc-sccs-find-file-hook ()
445 ;; If the file is locked by some other user, make
446 ;; the buffer read-only. Like this, even root
447 ;; cannot modify a file that someone else has locked.
448 (and (stringp (vc-state buffer-file-name 'SCCS))
449 (setq buffer-read-only t)))
453 ;;; Internal functions
456 ;; This function is wrapped with `progn' so that the autoload cookie
457 ;; copies the whole function itself into loaddefs.el rather than just placing
458 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
459 ;; help us avoid loading vc-sccs.
460 ;;;###autoload
461 (progn (defun vc-sccs-search-project-dir (_dirname basename)
462 "Return the name of a master file in the SCCS project directory.
463 Does not check whether the file exists but returns nil if it does not
464 find any project directory."
465 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
466 (when project-dir
467 (if (file-name-absolute-p project-dir)
468 (setq dirs '("SCCS" ""))
469 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
470 (setq project-dir (expand-file-name (concat "~" project-dir))))
471 (while (and (not dir) dirs)
472 (setq dir (expand-file-name (car dirs) project-dir))
473 (unless (file-directory-p dir)
474 (setq dir nil)
475 (setq dirs (cdr dirs))))
476 (and dir (expand-file-name (concat "s." basename) dir))))))
478 (defun vc-sccs-lock-file (file)
479 "Generate lock file name corresponding to FILE."
480 (let ((master (vc-master-name file)))
481 (and
482 master
483 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
484 (replace-match "p." t t master 2))))
486 (defun vc-sccs-parse-locks ()
487 "Parse SCCS locks in current buffer.
488 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
489 (let (master-locks)
490 (goto-char (point-min))
491 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
492 nil t)
493 (setq master-locks
494 (cons (cons (match-string 1) (match-string 2)) master-locks)))
495 ;; FIXME: is it really necessary to reverse ?
496 (nreverse master-locks)))
498 (defun vc-sccs-add-triple (name file rev)
499 (with-current-buffer
500 (find-file-noselect
501 (expand-file-name vc-sccs-name-assoc-file
502 (file-name-directory (vc-master-name file))))
503 (goto-char (point-max))
504 (insert name "\t:\t" file "\t" rev "\n")
505 (basic-save-buffer)
506 (kill-buffer (current-buffer))))
508 (defun vc-sccs-lookup-triple (file name)
509 "Return the numeric revision corresponding to a named tag of FILE.
510 If NAME is nil or a revision number string it's just passed through."
511 (if (or (null name)
512 (let ((firstchar (aref name 0)))
513 (and (>= firstchar ?0) (<= firstchar ?9))))
514 name
515 (with-temp-buffer
516 (vc-insert-file
517 (expand-file-name vc-sccs-name-assoc-file
518 (file-name-directory (vc-master-name file))))
519 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
521 (provide 'vc-sccs)
523 ;;; vc-sccs.el ends here