Document reserved keys
[emacs.git] / lisp / vc / vc-rcs.el
blob9fa52bf5dcefeb87d3579a7b2b0a91420c0e0c71
1 ;;; vc-rcs.el --- support for RCS version-control -*- lexical-binding:t -*-
3 ;; Copyright (C) 1992-2018 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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; See vc.el
28 ;; Some features will not work with ancient RCS versions. Where
29 ;; appropriate, VC finds out which version you have, and allows or
30 ;; disallows those features.
32 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
34 ;;; Code:
36 ;;;
37 ;;; Customization options
38 ;;;
40 (eval-when-compile
41 (require 'cl-lib)
42 (require 'vc))
44 (declare-function vc-branch-p "vc" (rev))
45 (declare-function vc-read-revision "vc"
46 (prompt &optional files backend default initial-input))
47 (declare-function vc-buffer-context "vc-dispatcher" ())
48 (declare-function vc-restore-buffer-context "vc-dispatcher" (context))
49 (declare-function vc-setup-buffer "vc-dispatcher" (buf))
51 (defgroup vc-rcs nil
52 "VC RCS backend."
53 :version "24.1"
54 :group 'vc)
56 (defcustom vc-rcs-release nil
57 "The release number of your RCS installation, as a string.
58 If nil, VC itself computes this value when it is first needed."
59 :type '(choice (const :tag "Auto" nil)
60 (string :tag "Specified")
61 (const :tag "Unknown" unknown))
62 :group 'vc-rcs)
64 (defcustom vc-rcs-register-switches nil
65 "Switches for registering a file in RCS.
66 A string or list of strings passed to the checkin program by
67 \\[vc-register]. If nil, use the value of `vc-register-switches'.
68 If t, use no switches."
69 :type '(choice (const :tag "Unspecified" nil)
70 (const :tag "None" t)
71 (string :tag "Argument String")
72 (repeat :tag "Argument List" :value ("") string))
73 :version "21.1"
74 :group 'vc-rcs)
76 (defcustom vc-rcs-diff-switches nil
77 "String or list of strings specifying switches for RCS diff under VC.
78 If nil, use the value of `vc-diff-switches'. If t, use no switches."
79 :type '(choice (const :tag "Unspecified" nil)
80 (const :tag "None" t)
81 (string :tag "Argument String")
82 (repeat :tag "Argument List" :value ("") string))
83 :version "21.1"
84 :group 'vc-rcs)
86 (defcustom vc-rcs-header '("$Id\ $")
87 "Header keywords to be inserted by `vc-insert-headers'."
88 :type '(repeat string)
89 :version "24.1" ; no longer consult the obsolete vc-header-alist
90 :group 'vc-rcs)
92 (defcustom vc-rcsdiff-knows-brief nil
93 "Indicates whether rcsdiff understands the --brief option.
94 The value is either `yes', `no', or nil. If it is nil, VC tries
95 to use --brief and sets this variable to remember whether it worked."
96 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
97 :group 'vc-rcs)
99 ;; This needs to be autoloaded because vc-rcs-registered uses it (via
100 ;; vc-default-registered), and vc-hooks needs to be able to check
101 ;; for a registered backend without loading every backend.
102 ;;;###autoload
103 (defcustom vc-rcs-master-templates
104 (purecopy '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
105 "Where to look for RCS master files.
106 For a description of possible values, see `vc-check-master-templates'."
107 :type '(choice (const :tag "Use standard RCS file names"
108 '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
109 (repeat :tag "User-specified"
110 (choice string
111 function)))
112 :version "21.1"
113 :group 'vc-rcs)
116 ;;; Properties of the backend
118 (defun vc-rcs-revision-granularity () 'file)
120 (defun vc-rcs-checkout-model (files)
121 "RCS-specific version of `vc-checkout-model'."
122 (let ((file (if (consp files) (car files) files))
123 result)
124 (when vc-consult-headers
125 (vc-file-setprop file 'vc-checkout-model nil)
126 (vc-rcs-consult-headers file)
127 (setq result (vc-file-getprop file 'vc-checkout-model)))
128 (or result
129 (progn (vc-rcs-fetch-master-state file)
130 (vc-file-getprop file 'vc-checkout-model))
131 ;; For non-existing files we assume strict locking.
132 'locking)))
135 ;;; State-querying functions
138 ;; The autoload cookie below places vc-rcs-registered directly into
139 ;; loaddefs.el, so that vc-rcs.el does not need to be loaded for
140 ;; every file that is visited.
141 ;;;###autoload
142 (progn
143 (defun vc-rcs-registered (f) (vc-default-registered 'RCS f)))
145 (defun vc-rcs-state (file)
146 "Implementation of `vc-state' for RCS."
147 (if (not (vc-rcs-registered file))
148 'unregistered
149 (or (boundp 'vc-rcs-headers-result)
150 (and vc-consult-headers
151 (vc-rcs-consult-headers file)))
152 (let ((state
153 ;; vc-working-revision might not be known; in that case the
154 ;; property is nil. vc-rcs-fetch-master-state knows how to
155 ;; handle that.
156 (vc-rcs-fetch-master-state file
157 (vc-file-getprop file
158 'vc-working-revision))))
159 (if (not (eq state 'up-to-date))
160 state
161 (if (vc-rcs-workfile-unchanged-p file)
162 'up-to-date
163 (if (eq (vc-rcs-checkout-model (list file)) 'locking)
164 'unlocked-changes
165 'edited))))))
167 (autoload 'vc-expand-dirs "vc")
169 (defun vc-rcs-dir-status-files (dir files update-function)
170 (if (not files) (setq files (vc-expand-dirs (list dir) 'RCS)))
171 (let ((result nil))
172 (dolist (file files)
173 (let ((state (vc-state file))
174 (frel (file-relative-name file)))
175 (when (and (eq (vc-backend file) 'RCS)
176 (not (eq state 'up-to-date)))
177 (push (list frel state) result))))
178 (funcall update-function result)))
180 (defun vc-rcs-dir-extra-headers (&rest _ignore))
182 (defun vc-rcs-working-revision (file)
183 "RCS-specific version of `vc-working-revision'."
184 (or (and vc-consult-headers
185 (vc-rcs-consult-headers file)
186 (vc-file-getprop file 'vc-working-revision))
187 (progn
188 (vc-rcs-fetch-master-state file)
189 (vc-file-getprop file 'vc-working-revision))))
191 (autoload 'vc-master-name "vc-filewise")
193 (defun vc-rcs-latest-on-branch-p (file &optional version)
194 "Return non-nil if workfile version of FILE is the latest on its branch.
195 When VERSION is given, perform check for that version."
196 (unless version (setq version (vc-working-revision file)))
197 (with-temp-buffer
198 (string= version
199 (if (vc-rcs-trunk-p version)
200 (progn
201 ;; Compare VERSION to the head version number.
202 (vc-insert-file (vc-master-name file) "^[0-9]")
203 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
204 ;; If we are not on the trunk, we need to examine the
205 ;; whole current branch.
206 (vc-insert-file (vc-master-name file) "^desc")
207 (vc-rcs-find-most-recent-rev (vc-branch-part version))))))
209 (defun vc-rcs-workfile-unchanged-p (file)
210 "Has FILE remained unchanged since last checkout?"
211 ;; Try to use rcsdiff --brief. If rcsdiff does not understand that,
212 ;; do a double take and remember the fact for the future
213 (let* ((version (concat "-r" (vc-working-revision file)))
214 (status (if (eq vc-rcsdiff-knows-brief 'no)
215 (vc-do-command "*vc*" 1 "rcsdiff" file version)
216 (vc-do-command "*vc*" 2 "rcsdiff" file "--brief" version))))
217 (if (eq status 2)
218 (if (not vc-rcsdiff-knows-brief)
219 (setq vc-rcsdiff-knows-brief 'no
220 status (vc-do-command "*vc*" 1 "rcsdiff" file version))
221 (error "rcsdiff failed"))
222 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
223 ;; The workfile is unchanged if rcsdiff found no differences.
224 (zerop status)))
228 ;;; State-changing functions
231 (defun vc-rcs-create-repo ()
232 "Create a new RCS repository."
233 ;; RCS is totally file-oriented, so all we have to do is make the directory.
234 (make-directory "RCS"))
236 (autoload 'vc-switches "vc")
238 (defun vc-rcs-register (files &optional comment)
239 "Register FILES into the RCS version-control system.
240 Automatically retrieve a read-only version of the file with keywords expanded.
241 COMMENT can be used to provide an initial description for each FILES.
242 Passes either `vc-rcs-register-switches' or `vc-register-switches'
243 to the RCS command."
244 (let (subdir name)
245 (dolist (file files)
246 (and (not (file-exists-p
247 (setq subdir (expand-file-name "RCS"
248 (file-name-directory file)))))
249 (not (directory-files (file-name-directory file)
250 nil ".*,v$" t))
251 (yes-or-no-p "Create RCS subdirectory? ")
252 (make-directory subdir))
253 (apply #'vc-do-command "*vc*" 0 "ci" file
254 ;; if available, use the secure registering option
255 (and (vc-rcs-release-p "5.6.4") "-i")
256 "-u"
257 ;; Some old MS-Windows ports of RCS crash when "ci -i" is
258 ;; invoked without -t; indulge them.
259 (concat "-t-" (or comment ""))
260 (vc-switches 'RCS 'register))
261 ;; parse output to find master file name and workfile version
262 (with-current-buffer "*vc*"
263 (goto-char (point-min))
264 (if (not (setq name
265 (if (looking-at (concat "^\\(.*\\) <-- "
266 (file-name-nondirectory file)))
267 (match-string 1))))
268 ;; if we couldn't find the master name,
269 ;; run vc-rcs-registered to get it
270 ;; (will be stored into the vc-master-name property)
271 (vc-rcs-registered file)
272 (vc-file-setprop file 'vc-master-name
273 (if (file-name-absolute-p name)
274 name
275 (expand-file-name
276 name
277 (file-name-directory file))))))
278 (vc-file-setprop file 'vc-working-revision
279 (if (re-search-forward
280 "^initial revision: \\([0-9.]+\\).*\n"
281 nil t)
282 (match-string 1))))))
284 (defun vc-rcs-responsible-p (file)
285 "Return non-nil if RCS thinks it would be responsible for registering FILE."
286 ;; TODO: check for all the patterns in vc-rcs-master-templates
287 (file-directory-p (expand-file-name "RCS"
288 (if (file-directory-p file)
289 file
290 (file-name-directory file)))))
292 (defun vc-rcs-receive-file (file rev)
293 "Implementation of receive-file for RCS."
294 (let ((checkout-model (vc-rcs-checkout-model (list file))))
295 (vc-rcs-register file "")
296 (when (eq checkout-model 'implicit)
297 (vc-rcs-set-non-strict-locking file))
298 (vc-rcs-set-default-branch file (concat rev ".1"))))
300 (defun vc-rcs-unregister (file)
301 "Unregister FILE from RCS.
302 If this leaves the RCS subdirectory empty, ask the user
303 whether to remove it."
304 (unless (memq (vc-state file) '(nil unregistered))
305 (let* ((master (vc-master-name file))
306 (dir (file-name-directory master))
307 (backup-info (find-backup-file-name master)))
308 (if (not backup-info)
309 (delete-file master)
310 (rename-file master (car backup-info) 'ok-if-already-exists)
311 (dolist (f (cdr backup-info)) (ignore-errors (delete-file f))))
312 (and (string= (file-name-nondirectory (directory-file-name dir)) "RCS")
313 ;; check whether RCS dir is empty, i.e. it does not
314 ;; contain any files except "." and ".."
315 (not (directory-files dir nil
316 "^\\([^.]\\|\\.[^.]\\|\\.\\.[^.]\\).*"))
317 (yes-or-no-p (format "Directory %s is empty; remove it? " dir))
318 (delete-directory dir)))))
320 (defun vc-rcs-checkin (files comment &optional rev)
321 "RCS-specific version of `vc-backend-checkin'."
322 (let ((switches (vc-switches 'RCS 'checkin)))
323 ;; Now operate on the files
324 (dolist (file (vc-expand-dirs files 'RCS))
325 (let ((old-version (vc-working-revision file)) new-version
326 (default-branch (vc-file-getprop file 'vc-rcs-default-branch)))
327 ;; Force branch creation if an appropriate
328 ;; default branch has been set.
329 (and (not rev)
330 default-branch
331 (string-match (concat "^" (regexp-quote old-version) "\\.")
332 default-branch)
333 (setq rev default-branch)
334 (setq switches (cons "-f" switches)))
335 (if (and (not rev) old-version)
336 (setq rev (vc-branch-part old-version)))
337 (apply #'vc-do-command "*vc*" 0 "ci" (vc-master-name file)
338 ;; if available, use the secure check-in option
339 (and (vc-rcs-release-p "5.6.4") "-j")
340 (concat "-u" rev)
341 (concat "-m" comment)
342 switches)
343 (vc-file-setprop file 'vc-working-revision nil)
345 ;; determine the new workfile version
346 (set-buffer "*vc*")
347 (goto-char (point-min))
348 (when (or (re-search-forward
349 "new revision: \\([0-9.]+\\);" nil t)
350 (re-search-forward
351 "reverting to previous revision \\([0-9.]+\\)" nil t))
352 (setq new-version (match-string 1))
353 (vc-file-setprop file 'vc-working-revision new-version))
355 ;; if we got to a different branch, adjust the default
356 ;; branch accordingly
357 (cond
358 ((and old-version new-version
359 (not (string= (vc-branch-part old-version)
360 (vc-branch-part new-version))))
361 (vc-rcs-set-default-branch file
362 (if (vc-rcs-trunk-p new-version) nil
363 (vc-branch-part new-version)))
364 ;; If this is an old (pre-1992!) RCS release, we might have
365 ;; to remove a remaining lock.
366 (if (not (vc-rcs-release-p "5.6.2"))
367 ;; exit status of 1 is also accepted.
368 ;; It means that the lock was removed before.
369 (vc-do-command "*vc*" 1 "rcs" (vc-master-name file)
370 (concat "-u" old-version)))))))))
372 (defun vc-rcs-find-revision (file rev buffer)
373 (apply #'vc-do-command
374 (or buffer "*vc*") 0 "co" (vc-master-name file)
375 "-q" ;; suppress diagnostic output
376 (concat "-p" rev)
377 (vc-switches 'RCS 'checkout)))
379 (defun vc-rcs-checkout (file &optional rev)
380 "Retrieve a copy of a saved version of FILE. If FILE is a directory,
381 attempt the checkout for all registered files beneath it."
382 (if (file-directory-p file)
383 (mapc 'vc-rcs-checkout (vc-expand-dirs (list file) 'RCS))
384 (let ((file-buffer (get-file-buffer file))
385 switches)
386 (message "Checking out %s..." file)
387 (save-excursion
388 ;; Change buffers to get local value of vc-checkout-switches.
389 (if file-buffer (set-buffer file-buffer))
390 (setq switches (vc-switches 'RCS 'checkout))
391 ;; Save this buffer's default-directory
392 ;; and use save-excursion to make sure it is restored
393 ;; in the same buffer it was saved in.
394 (let ((default-directory default-directory))
395 (save-excursion
396 ;; Adjust the default-directory so that the check-out creates
397 ;; the file in the right place.
398 (setq default-directory (file-name-directory file))
399 (let (new-version)
400 ;; if we should go to the head of the trunk,
401 ;; clear the default branch first
402 (and rev (string= rev "")
403 (vc-rcs-set-default-branch file nil))
404 ;; now do the checkout
405 (apply #'vc-do-command
406 "*vc*" 0 "co" (vc-master-name file)
407 ;; If locking is not strict, force to overwrite
408 ;; the writable workfile.
409 (if (eq (vc-rcs-checkout-model (list file)) 'implicit) "-f")
410 "-l"
411 (if (stringp rev)
412 ;; a literal revision was specified
413 (concat "-r" rev)
414 (let ((workrev (vc-working-revision file)))
415 (if workrev
416 (concat "-r"
417 (if (not rev)
418 ;; no revision specified:
419 ;; use current workfile version
420 workrev
421 ;; REV is t ...
422 (if (not (vc-rcs-trunk-p workrev))
423 ;; ... go to head of current branch
424 (vc-branch-part workrev)
425 ;; ... go to head of trunk
426 (vc-rcs-set-default-branch file
427 nil)
428 ""))))))
429 switches)
430 ;; determine the new workfile version
431 (with-current-buffer "*vc*"
432 (setq new-version
433 (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
434 (vc-file-setprop file 'vc-working-revision new-version)
435 ;; if necessary, adjust the default branch
436 (and rev (not (string= rev ""))
437 (vc-rcs-set-default-branch
438 file
439 (if (vc-rcs-latest-on-branch-p file new-version)
440 (if (vc-rcs-trunk-p new-version) nil
441 (vc-branch-part new-version))
442 new-version)))))
443 (message "Checking out %s...done" file))))))
445 (defun vc-rcs-revert (file &optional _contents-done)
446 "Revert FILE to the version it was based on. If FILE is a directory,
447 revert all registered files beneath it."
448 (if (file-directory-p file)
449 (mapc 'vc-rcs-revert (vc-expand-dirs (list file) 'RCS))
450 (vc-do-command "*vc*" 0 "co" (vc-master-name file) "-f"
451 (concat (if (eq (vc-state file) 'edited) "-u" "-r")
452 (vc-working-revision file)))))
454 (defun vc-rcs-merge-file (file)
455 "Accept a file merge request, prompting for revisions."
456 (let* ((first-revision
457 (vc-read-revision
458 (concat "Merge " file " from branch or revision: ")
459 (list file)
460 'RCS))
461 second-revision)
462 (cond
463 ((string= first-revision "")
464 (error "A starting RCS revision is required"))
466 (if (not (vc-branch-p first-revision))
467 (setq second-revision
468 (vc-read-revision
469 "Second RCS revision: "
470 (list file) 'RCS nil
471 (concat (vc-branch-part first-revision) ".")))
472 ;; We want to merge an entire branch. Set revisions
473 ;; accordingly, so that vc-rcs-merge understands us.
474 (setq second-revision first-revision)
475 ;; first-revision must be the starting point of the branch
476 (setq first-revision (vc-branch-part first-revision)))))
477 (vc-rcs-merge file first-revision second-revision)))
479 (defun vc-rcs-merge (file first-version &optional second-version)
480 "Merge changes into current working copy of FILE.
481 The changes are between FIRST-VERSION and SECOND-VERSION."
482 (vc-do-command "*vc*" 1 "rcsmerge" (vc-master-name file)
483 "-kk" ; ignore keyword conflicts
484 (concat "-r" first-version)
485 (if second-version (concat "-r" second-version))))
487 (defun vc-rcs-steal-lock (file &optional rev)
488 "Steal the lock on the current workfile for FILE and revision REV.
489 If FILE is a directory, steal the lock on all registered files beneath it.
490 Needs RCS 5.6.2 or later for -M."
491 (if (file-directory-p file)
492 (mapc 'vc-rcs-steal-lock (vc-expand-dirs (list file) 'RCS))
493 (vc-do-command "*vc*" 0 "rcs" (vc-master-name file) "-M" (concat "-u" rev))
494 ;; Do a real checkout after stealing the lock, so that we see
495 ;; expanded headers.
496 (vc-do-command "*vc*" 0 "co" (vc-master-name file) "-f" (concat "-l" rev))
497 ;; Must clear any headers here because they wouldn't
498 ;; show that the file is locked now.
499 (let* ((filename (or file buffer-file-name))
500 (visited (find-buffer-visiting filename)))
501 (if visited
502 (let ((context (vc-buffer-context)))
503 ;; save-excursion may be able to relocate point and mark
504 ;; properly. If it fails, vc-restore-buffer-context
505 ;; will give it a second try.
506 (save-excursion
507 (vc-rcs-clear-headers))
508 (vc-restore-buffer-context context))
509 (set-buffer (find-file-noselect filename))
510 (vc-rcs-clear-headers)
511 (kill-buffer filename)))))
513 (defun vc-rcs-modify-change-comment (files rev comment)
514 "Modify the change comments change on FILES on a specified REV. If FILE is a
515 directory the operation is applied to all registered files beneath it."
516 (dolist (file (vc-expand-dirs files 'RCS))
517 (vc-do-command "*vc*" 0 "rcs" (vc-master-name file)
518 (concat "-m" rev ":" comment))))
522 ;;; History functions
525 (defun vc-rcs-print-log-cleanup ()
526 (let ((inhibit-read-only t))
527 (goto-char (point-max))
528 (forward-line -1)
529 (while (looking-at "=*\n")
530 (delete-char (- (match-end 0) (match-beginning 0)))
531 (forward-line -1))
532 (goto-char (point-min))
533 (when (looking-at "[\b\t\n\v\f\r ]+")
534 (delete-char (- (match-end 0) (match-beginning 0))))))
536 (defun vc-rcs-print-log (files buffer &optional _shortlog
537 _start-revision-ignored limit)
538 "Print commit log associated with FILES into specified BUFFER.
539 Remaining arguments are ignored.
540 If FILE is a directory the operation is applied to all registered
541 files beneath it."
542 (vc-do-command (or buffer "*vc*") 0 "rlog"
543 (mapcar 'vc-master-name (vc-expand-dirs files 'RCS)))
544 (with-current-buffer (or buffer "*vc*")
545 (vc-rcs-print-log-cleanup))
546 (when limit 'limit-unsupported))
548 (defun vc-rcs-diff (files &optional oldvers newvers buffer _async)
549 "Get a difference report using RCS between two sets of files."
550 (apply #'vc-do-command (or buffer "*vc-diff*")
551 ;; The repo is local, so this is fast anyway.
552 1 ; bug#21969
553 "rcsdiff" (vc-expand-dirs files 'RCS)
554 (append (list "-q"
555 (and oldvers (concat "-r" oldvers))
556 (and newvers (concat "-r" newvers)))
557 (vc-switches 'RCS 'diff))))
559 (defun vc-rcs-find-admin-dir (file)
560 "Return the administrative directory of FILE."
561 (vc-find-root file "RCS"))
563 (defun vc-rcs-comment-history (file)
564 "Return a string with all log entries stored in BACKEND for FILE."
565 (with-current-buffer "*vc*"
566 ;; Has to be written this way, this function is used by the CVS backend too
567 (vc-call-backend (vc-backend file) 'print-log (list file))
568 ;; Remove cruft
569 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
570 "\\(branches: .*;\n\\)?"
571 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
572 (goto-char (point-max)) (forward-line -1)
573 (while (looking-at "=*\n")
574 (delete-char (- (match-end 0) (match-beginning 0)))
575 (forward-line -1))
576 (goto-char (point-min))
577 (if (looking-at "[\b\t\n\v\f\r ]+")
578 (delete-char (- (match-end 0) (match-beginning 0))))
579 (goto-char (point-min))
580 (re-search-forward separator nil t)
581 (delete-region (point-min) (point))
582 (while (re-search-forward separator nil t)
583 (delete-region (match-beginning 0) (match-end 0))))
584 ;; Return the de-crufted comment list
585 (buffer-string)))
587 (defun vc-rcs-annotate-command (file buffer &optional revision)
588 "Annotate FILE, inserting the results in BUFFER.
589 Optional arg REVISION is a revision to annotate from."
590 (vc-setup-buffer buffer)
591 ;; Aside from the "head revision on the trunk", the instructions for
592 ;; each revision on the trunk are an ordered list of kill and insert
593 ;; commands necessary to go from the chronologically-following
594 ;; revision to this one. That is, associated with revision N are
595 ;; edits that applied to revision N+1 would result in revision N.
597 ;; On a branch, however, (some) things are inverted: the commands
598 ;; listed are those necessary to go from the chronologically-preceding
599 ;; revision to this one. That is, associated with revision N are
600 ;; edits that applied to revision N-1 would result in revision N.
602 ;; So, to get per-line history info, we apply reverse-chronological
603 ;; edits, starting with the head revision on the trunk, all the way
604 ;; back through the initial revision (typically "1.1" or similar),
605 ;; then apply forward-chronological edits -- keeping track of which
606 ;; revision is associated with each inserted line -- until we reach
607 ;; the desired revision for display (which may be either on the trunk
608 ;; or on a branch).
609 (let* ((tree (with-temp-buffer
610 (insert-file-contents (vc-rcs-registered file))
611 (vc-rcs-parse)))
612 (revisions (cdr (assq 'revisions tree)))
613 ;; The revision N whose instructions we currently are processing.
614 (cur (cdr (assq 'head (cdr (assq 'headers tree)))))
615 ;; Alist from the parse tree for N.
616 (meta (cdr (assoc cur revisions)))
617 ;; Point and temporary string, respectively.
619 ;; "Next-branch list". Nil means the desired revision to
620 ;; display lives on the trunk. Non-nil means it lives on a
621 ;; branch, in which case the value is a list of revision pairs
622 ;; (PARENT . CHILD), the first PARENT being on the trunk, that
623 ;; links each series of revisions in the path from the initial
624 ;; revision to the desired revision to display.
625 nbls
626 ;; "Path-accumulate-predicate plus revision/date/author".
627 ;; Until set, forward-chronological edits are not accumulated.
628 ;; Once set, its value (updated every revision) is used for
629 ;; the text property `:vc-rcs-r/d/a' for inserts during
630 ;; processing of forward-chronological instructions for N.
631 ;; See internal func `r/d/a'.
632 prda
633 ;; List of forward-chronological instructions, each of the
634 ;; form: (POS . ACTION), where POS is a buffer position. If
635 ;; ACTION is a string, it is inserted, otherwise it is taken as
636 ;; the number of characters to be deleted.
637 path
638 ;; N+1. When `cur' is "", this is the initial revision.
639 pre)
640 (unless revision
641 (setq revision cur))
642 (unless (assoc revision revisions)
643 (error "No such revision: %s" revision))
644 ;; Find which branches (if any) must be included in the edits.
645 (let ((par revision)
646 bpt kids)
647 (while (setq bpt (vc-branch-part par)
648 par (vc-branch-part bpt))
649 (setq kids (cdr (assq 'branches (cdr (assoc par revisions)))))
650 ;; A branchpoint may have multiple children. Find the right one.
651 (while (not (string= bpt (vc-branch-part (car kids))))
652 (setq kids (cdr kids)))
653 (push (cons par (car kids)) nbls)))
654 ;; Start with the full text.
655 (set-buffer buffer)
656 (insert (cdr (assq 'text meta)))
657 ;; Apply reverse-chronological edits on the trunk, computing and
658 ;; accumulating forward-chronological edits after some point, for
659 ;; later.
660 (cl-flet ((r/d/a () (vector pre
661 (cdr (assq 'date meta))
662 (cdr (assq 'author meta)))))
663 (while (when (setq pre cur cur (cdr (assq 'next meta)))
664 (not (string= "" cur)))
665 (setq
666 ;; Start accumulating the forward-chronological edits when N+1
667 ;; on the trunk is either the desired revision to display, or
668 ;; the appropriate branchpoint for it. Do this before
669 ;; updating `meta' since `r/d/a' uses N+1's `meta' value.
670 prda (when (or prda (string= (if nbls (caar nbls) revision) pre))
671 (r/d/a))
672 meta (cdr (assoc cur revisions)))
673 ;; Edits in the parse tree specify a line number (in the buffer
674 ;; *BEFORE* editing occurs) to start from, but line numbers
675 ;; change as a result of edits. To DTRT, we apply edits in
676 ;; order of descending buffer position so that edits further
677 ;; down in the buffer occur first w/o corrupting specified
678 ;; buffer positions of edits occurring towards the beginning of
679 ;; the buffer. In this way we avoid using markers. A pleasant
680 ;; property of this approach is ability to push instructions
681 ;; onto `path' directly, w/o need to maintain rev boundaries.
682 (dolist (insn (cdr (assq :insn meta)))
683 (goto-char (point-min))
684 (forward-line (1- (pop insn)))
685 (setq p (point))
686 (pcase (pop insn)
687 (`k (setq s (buffer-substring-no-properties
688 p (progn (forward-line (car insn))
689 (point))))
690 (when prda
691 (push `(,p . ,(propertize s :vc-rcs-r/d/a prda)) path))
692 (delete-region p (point)))
693 (`i (setq s (car insn))
694 (when prda
695 (push `(,p . ,(length s)) path))
696 (insert s)))))
697 ;; For the initial revision, setting `:vc-rcs-r/d/a' directly is
698 ;; equivalent to pushing an insert instruction (of the entire buffer
699 ;; contents) onto `path' then erasing the buffer, but less wasteful.
700 (put-text-property (point-min) (point-max) :vc-rcs-r/d/a (r/d/a))
701 ;; Now apply the forward-chronological edits for the trunk.
702 (dolist (insn path)
703 (goto-char (pop insn))
704 (if (stringp insn)
705 (insert insn)
706 (delete-char insn)))
707 ;; Now apply the forward-chronological edits (directly from the
708 ;; parse-tree) for the branch(es), if necessary. We re-use vars
709 ;; `pre' and `meta' for the sake of internal func `r/d/a'.
710 (while nbls
711 (setq pre (cdr (pop nbls)))
712 (while (progn
713 (setq meta (cdr (assoc pre revisions))
714 prda nil)
715 (dolist (insn (cdr (assq :insn meta)))
716 (goto-char (point-min))
717 (forward-line (1- (pop insn)))
718 (pcase (pop insn)
719 (`k (delete-region
720 (point) (progn (forward-line (car insn))
721 (point))))
722 (`i (insert (propertize
723 (car insn)
724 :vc-rcs-r/d/a
725 (or prda (setq prda (r/d/a))))))))
726 (prog1 (not (string= (if nbls (caar nbls) revision) pre))
727 (setq pre (cdr (assq 'next meta)))))))))
728 ;; Lastly, for each line, insert at bol nicely-formatted history info.
729 ;; We do two passes to collect summary information used to minimize
730 ;; the annotation's usage of screen real-estate: (1) Consider rendered
731 ;; width of revision plus author together as a unit; and (2) Omit
732 ;; author entirely if all authors are the same as the user.
733 (let ((ht (make-hash-table :test 'eq))
734 (me (user-login-name))
735 (maxw 0)
736 (all-me t)
737 rda w a)
738 (goto-char (point-max))
739 (while (not (bobp))
740 (forward-line -1)
741 (setq rda (get-text-property (point) :vc-rcs-r/d/a))
742 (unless (gethash rda ht)
743 (setq a (aref rda 2)
744 all-me (and all-me (string= a me)))
745 (puthash rda (setq w (+ (length (aref rda 0))
746 (length a)))
748 (setq maxw (max w maxw))))
749 (let ((padding (make-string maxw 32)))
750 (cl-flet ((pad (w) (substring-no-properties padding w))
751 (render (rda &rest ls)
752 (propertize
753 (apply #'concat
754 (format-time-string "%Y-%m-%d" (aref rda 1))
756 (aref rda 0)
758 :vc-annotate-prefix t
759 :vc-rcs-r/d/a rda)))
760 (maphash
761 (if all-me
762 (lambda (rda w)
763 (puthash rda (render rda (pad w) ": ") ht))
764 (lambda (rda w)
765 (puthash rda (render rda " " (pad w) " " (aref rda 2) ": ") ht)))
766 ht)))
767 (while (not (eobp))
768 (insert (gethash (get-text-property (point) :vc-rcs-r/d/a) ht))
769 (forward-line 1))))
771 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
773 (defun vc-rcs-annotate-current-time ()
774 "Return the current time, based at midnight of the current day, and
775 encoded as fractional days."
776 (vc-annotate-convert-time
777 (apply #'encode-time 0 0 0 (nthcdr 3 (decode-time)))))
779 (defun vc-rcs-annotate-time ()
780 "Return the time of the next annotation (as fraction of days)
781 systime, or nil if there is none. Also, reposition point."
782 (unless (eobp)
783 (prog1 (vc-annotate-convert-time
784 (aref (get-text-property (point) :vc-rcs-r/d/a) 1))
785 (goto-char (next-single-property-change (point) :vc-annotate-prefix)))))
787 (defun vc-rcs-annotate-extract-revision-at-line ()
788 (aref (get-text-property (point) :vc-rcs-r/d/a) 0))
792 ;;; Tag system
795 (autoload 'vc-tag-precondition "vc")
796 (declare-function vc-file-tree-walk "vc" (dirname func &rest args))
798 (defun vc-rcs-create-tag (dir name branchp)
799 (when branchp
800 (error "RCS backend does not support module branches"))
801 (let ((result (vc-tag-precondition dir)))
802 (if (stringp result)
803 (error "File %s is not up-to-date" result)
804 (vc-file-tree-walk
806 (lambda (f)
807 (vc-do-command "*vc*" 0 "rcs" (vc-master-name f) (concat "-n" name ":")))))))
811 ;;; Miscellaneous
814 (defun vc-rcs-trunk-p (rev)
815 "Return t if REV is a revision on the trunk."
816 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
818 (defun vc-rcs-minor-part (rev)
819 "Return the minor revision number of a revision number REV."
820 (string-match "[0-9]+\\'" rev)
821 (substring rev (match-beginning 0) (match-end 0)))
823 (defun vc-rcs-previous-revision (_file rev)
824 "Return the revision number immediately preceding REV for FILE,
825 or nil if there is no previous revision. This default
826 implementation works for MAJOR.MINOR-style revision numbers as
827 used by RCS and CVS."
828 (let ((branch (vc-branch-part rev))
829 (minor-num (string-to-number (vc-rcs-minor-part rev))))
830 (when branch
831 (if (> minor-num 1)
832 ;; revision does probably not start a branch or release
833 (concat branch "." (number-to-string (1- minor-num)))
834 (if (vc-rcs-trunk-p rev)
835 ;; we are at the beginning of the trunk --
836 ;; don't know anything to return here
838 ;; we are at the beginning of a branch --
839 ;; return revision of starting point
840 (vc-branch-part branch))))))
842 (defun vc-rcs-next-revision (file rev)
843 "Return the revision number immediately following REV for FILE,
844 or nil if there is no next revision. This default implementation
845 works for MAJOR.MINOR-style revision numbers as used by RCS
846 and CVS."
847 (when (not (string= rev (vc-working-revision file)))
848 (let ((branch (vc-branch-part rev))
849 (minor-num (string-to-number (vc-rcs-minor-part rev))))
850 (concat branch "." (number-to-string (1+ minor-num))))))
852 ;; You might think that this should be distributed with RCS, but
853 ;; apparently not. CVS sometimes provides a version of it.
854 ;; https://lists.gnu.org/r/emacs-devel/2014-05/msg00288.html
855 (defvar vc-rcs-rcs2log-program
856 (let (exe)
857 (cond ((file-executable-p
858 (setq exe (expand-file-name "rcs2log" exec-directory)))
859 exe)
860 ;; In the unlikely event that someone is running an
861 ;; uninstalled Emacs and wants to do something RCS-related.
862 ((file-executable-p
863 (setq exe (expand-file-name "lib-src/rcs2log" source-directory)))
864 exe)
865 (t "rcs2log")))
866 "Path to the `rcs2log' program (normally in `exec-directory').")
868 (autoload 'vc-buffer-sync "vc-dispatcher")
870 (defun vc-rcs-update-changelog (files)
871 "Default implementation of update-changelog.
872 Uses `rcs2log' which only works for RCS and CVS."
873 ;; FIXME: We (c|sh)ould add support for cvs2cl
874 (let ((odefault default-directory)
875 (changelog (find-change-log))
876 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
877 (tempfile (make-temp-file
878 (expand-file-name "vc"
879 (or small-temporary-file-directory
880 temporary-file-directory))))
881 (login-name (or user-login-name
882 (format "uid%d" (number-to-string (user-uid)))))
883 (full-name (or add-log-full-name
884 (user-full-name)
885 (user-login-name)
886 (format "uid%d" (number-to-string (user-uid)))))
887 (mailing-address (or add-log-mailing-address
888 user-mail-address)))
889 (find-file-other-window changelog)
890 (barf-if-buffer-read-only)
891 (vc-buffer-sync)
892 (undo-boundary)
893 (goto-char (point-min))
894 (push-mark)
895 (message "Computing change log entries...")
896 (message "Computing change log entries... %s"
897 (unwind-protect
898 (progn
899 (setq default-directory odefault)
900 (if (eq 0 (apply #'call-process vc-rcs-rcs2log-program
901 nil (list t tempfile) nil
902 "-c" changelog
903 "-u" (concat login-name
904 "\t" full-name
905 "\t" mailing-address)
906 (mapcar
907 (lambda (f)
908 (file-relative-name
909 (expand-file-name f odefault)))
910 files)))
911 "done"
912 (pop-to-buffer (get-buffer-create "*vc*"))
913 (erase-buffer)
914 (insert-file-contents tempfile)
915 "failed"))
916 (setq default-directory (file-name-directory changelog))
917 (delete-file tempfile)))))
919 (defun vc-rcs-check-headers ()
920 "Check if the current file has any headers in it."
921 (save-excursion
922 (goto-char (point-min))
923 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
924 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
926 (defun vc-rcs-clear-headers ()
927 "Clear RCS header value parts."
928 (let ((case-fold-search nil))
929 (goto-char (point-min))
930 (while (re-search-forward
931 (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
932 "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
933 nil t)
934 (replace-match "$\\1$"))))
936 (autoload 'vc-rename-master "vc-filewise")
938 (defun vc-rcs-rename-file (old new)
939 ;; Just move the master file (using vc-rcs-master-templates).
940 (vc-rename-master (vc-master-name old) new vc-rcs-master-templates))
942 (defun vc-rcs-find-file-hook ()
943 ;; If the file is locked by some other user, make
944 ;; the buffer read-only. Like this, even root
945 ;; cannot modify a file that someone else has locked.
946 (and (stringp (vc-state buffer-file-name 'RCS))
947 (setq buffer-read-only t)))
951 ;;; Internal functions
954 (defun vc-rcs-workfile-is-newer (file)
955 "Return non-nil if FILE is newer than its RCS master.
956 This likely means that FILE has been changed with respect
957 to its master version."
958 (let ((file-time (nth 5 (file-attributes file)))
959 (master-time (nth 5 (file-attributes (vc-master-name file)))))
960 (or (> (nth 0 file-time) (nth 0 master-time))
961 (and (= (nth 0 file-time) (nth 0 master-time))
962 (> (nth 1 file-time) (nth 1 master-time))))))
964 (defun vc-rcs-find-most-recent-rev (branch)
965 "Find most recent revision on BRANCH."
966 (goto-char (point-min))
967 (let ((latest-rev -1) value)
968 (while (re-search-forward (concat "^\\(" (regexp-quote branch)
969 "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
970 nil t)
971 (let ((rev (string-to-number (match-string 2))))
972 (when (< latest-rev rev)
973 (setq latest-rev rev)
974 (setq value (match-string 1)))))
975 (or value
976 (vc-branch-part branch))))
978 (defun vc-rcs-fetch-master-state (file &optional working-revision)
979 "Compute the master file's idea of the state of FILE.
980 If a WORKING-REVISION is given, compute the state of that version,
981 otherwise determine the workfile version based on the master file.
982 This function sets the properties `vc-working-revision' and
983 `vc-checkout-model' to their correct values, based on the master
984 file."
985 (when (and (file-regular-p file) (vc-master-name file))
986 (with-temp-buffer
987 (if (or (not (vc-insert-file (vc-master-name file) "^[0-9]"))
988 (progn (goto-char (point-min))
989 (not (looking-at "^head[ \t\n]+[^;]+;$"))))
990 (error "File %s is not an RCS master file" (vc-master-name file)))
991 (let ((workfile-is-latest nil)
992 (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
993 (vc-file-setprop file 'vc-rcs-default-branch default-branch)
994 (unless working-revision
995 ;; Workfile version not known yet. Determine that first. It
996 ;; is either the head of the trunk, the head of the default
997 ;; branch, or the "default branch" itself, if that is a full
998 ;; revision number.
999 (cond
1000 ;; no default branch
1001 ((or (not default-branch) (string= "" default-branch))
1002 (setq working-revision
1003 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
1004 (setq workfile-is-latest t))
1005 ;; default branch is actually a revision
1006 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
1007 default-branch)
1008 (setq working-revision default-branch))
1009 ;; else, search for the head of the default branch
1010 (t (vc-insert-file (vc-master-name file) "^desc")
1011 (setq working-revision
1012 (vc-rcs-find-most-recent-rev default-branch))
1013 (setq workfile-is-latest t)))
1014 (vc-file-setprop file 'vc-working-revision working-revision))
1015 ;; Check strict locking
1016 (goto-char (point-min))
1017 (vc-file-setprop file 'vc-checkout-model
1018 (if (re-search-forward ";[ \t\n]*strict;" nil t)
1019 'locking 'implicit))
1020 ;; Compute state of workfile version
1021 (goto-char (point-min))
1022 (let ((locking-user
1023 (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
1024 (regexp-quote working-revision)
1025 "[^0-9.]")
1026 1)))
1027 (cond
1028 ;; not locked
1029 ((not locking-user)
1030 (if (or workfile-is-latest
1031 (vc-rcs-latest-on-branch-p file working-revision))
1032 ;; workfile version is latest on branch
1033 'up-to-date
1034 ;; workfile version is not latest on branch
1035 'needs-update))
1036 ;; locked by the calling user
1037 ((and (stringp locking-user)
1038 (string= locking-user (vc-user-login-name file)))
1039 ;; Don't call `vc-rcs-checkout-model' to avoid inf-looping.
1040 (if (or (eq (vc-file-getprop file 'vc-checkout-model) 'locking)
1041 workfile-is-latest
1042 (vc-rcs-latest-on-branch-p file working-revision))
1043 'edited
1044 ;; Locking is not used for the file, but the owner does
1045 ;; have a lock, and there is a higher version on the current
1046 ;; branch. Not sure if this can occur, and if it is right
1047 ;; to use `needs-merge' in this case.
1048 'needs-merge))
1049 ;; locked by somebody else
1050 ((stringp locking-user)
1051 locking-user)
1053 (error "Error getting state of RCS file"))))))))
1055 (defun vc-rcs-consult-headers (file)
1056 "Search for RCS headers in FILE, and set properties accordingly.
1058 Returns: nil if no headers were found
1059 'rev if a workfile revision was found
1060 'rev-and-lock if revision and lock info was found"
1061 (cond
1062 ((not (get-file-buffer file)) nil)
1063 ((let (status version)
1064 (with-current-buffer (get-file-buffer file)
1065 (save-excursion
1066 (goto-char (point-min))
1067 (cond
1068 ;; search for $Id or $Header
1069 ;; -------------------------
1070 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
1071 ((or (and (search-forward "$Id\ : " nil t)
1072 (looking-at "[^ ]+ \\([0-9.]+\\) "))
1073 (and (progn (goto-char (point-min))
1074 (search-forward "$Header\ : " nil t))
1075 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
1076 (goto-char (match-end 0))
1077 ;; if found, store the revision number ...
1078 (setq version (match-string-no-properties 1))
1079 ;; ... and check for the locking state
1080 (cond
1081 ((looking-at
1082 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
1083 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
1084 "[^ ]+ [^ ]+ ")) ; author & state
1085 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
1086 (cond
1087 ;; unlocked revision
1088 ((looking-at "\\$")
1089 ;;(setq locking-user 'none)
1090 (setq status 'rev-and-lock))
1091 ;; revision is locked by some user
1092 ((looking-at "\\([^ ]+\\) \\$")
1093 ;;(setq locking-user (match-string-no-properties 1))
1094 (setq status 'rev-and-lock))
1095 ;; everything else: false
1096 (nil)))
1097 ;; unexpected information in
1098 ;; keyword string --> quit
1099 (nil)))
1100 ;; search for $Revision
1101 ;; --------------------
1102 ((re-search-forward (concat "\\$"
1103 "Revision: \\([0-9.]+\\) \\$")
1104 nil t)
1105 ;; if found, store the revision number ...
1106 (setq version (match-string-no-properties 1))
1107 ;; and see if there's any lock information
1108 (goto-char (point-min))
1109 (if (re-search-forward (concat "\\$" "Locker:") nil t)
1110 (cond ((looking-at " \\([^ ]+\\) \\$")
1111 ;;(setq locking-user (match-string-no-properties 1))
1112 (setq status 'rev-and-lock))
1113 ((looking-at " *\\$")
1114 ;;(setq locking-user 'none)
1115 (setq status 'rev-and-lock))
1117 ;;(setq locking-user 'none)
1118 (setq status 'rev-and-lock)))
1119 (setq status 'rev)))
1120 ;; else: nothing found
1121 ;; -------------------
1122 (t nil))))
1123 (if status (vc-file-setprop file 'vc-working-revision version))
1124 status))))
1126 (defun vc-release-greater-or-equal (r1 r2)
1127 "Compare release numbers, represented as strings.
1128 Release components are assumed cardinal numbers, not decimal fractions
1129 \(5.10 is a higher release than 5.9). Omitted fields are considered
1130 lower \(5.6.7 is earlier than 5.6.7.1). Comparison runs till the end
1131 of the string is found, or a non-numeric component shows up \(5.6.7 is
1132 earlier than \"5.6.7 beta\", which is probably not what you want in
1133 some cases). This code is suitable for existing RCS release numbers.
1134 CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5)."
1135 (let (v1 v2 i1 i2)
1136 (catch 'done
1137 (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
1138 (setq i1 (match-end 0))
1139 (setq v1 (string-to-number (match-string 1 r1)))
1140 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
1141 (setq i2 (match-end 0))
1142 (setq v2 (string-to-number (match-string 1 r2)))
1143 (if (> v1 v2) (throw 'done t)
1144 (if (< v1 v2) (throw 'done nil)
1145 (throw 'done
1146 (vc-release-greater-or-equal
1147 (substring r1 i1)
1148 (substring r2 i2)))))))
1149 (throw 'done t)))
1150 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
1151 (throw 'done nil))
1152 (throw 'done t)))))
1154 (defun vc-rcs-release-p (release)
1155 "Return t if we have RELEASE or better."
1156 (let ((installation (vc-rcs-system-release)))
1157 (if (and installation
1158 (not (eq installation 'unknown)))
1159 (vc-release-greater-or-equal installation release))))
1161 (defun vc-rcs-system-release ()
1162 "Return the RCS release installed on this system, as a string.
1163 Return symbol `unknown' if the release cannot be deducted. The user can
1164 override this using variable `vc-rcs-release'.
1166 If the user has not set variable `vc-rcs-release' and it is nil,
1167 variable `vc-rcs-release' is set to the returned value."
1168 (or vc-rcs-release
1169 (setq vc-rcs-release
1170 (or (and (zerop (vc-do-command "*vc*" nil "rcs" nil "-V"))
1171 (with-current-buffer (get-buffer "*vc*")
1172 (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
1173 'unknown))))
1175 (defun vc-rcs-set-non-strict-locking (file)
1176 (vc-do-command "*vc*" 0 "rcs" file "-U")
1177 (vc-file-setprop file 'vc-checkout-model 'implicit)
1178 (set-file-modes file (logior (file-modes file) 128)))
1180 (defun vc-rcs-set-default-branch (file branch)
1181 (vc-do-command "*vc*" 0 "rcs" (vc-master-name file) (concat "-b" branch))
1182 (vc-file-setprop file 'vc-rcs-default-branch branch))
1184 (defun vc-rcs-parse (&optional buffer)
1185 "Parse current buffer, presumed to be in RCS-style masterfile format.
1186 Optional arg BUFFER specifies another buffer to parse. Return an alist
1187 of two elements, w/ keys `headers' and `revisions' and values in turn
1188 sub-alists. For `headers', the values unless otherwise specified are
1189 strings and the keys are:
1191 desc -- description
1192 head -- latest revision
1193 branch -- the branch the \"head revision\" lies on;
1194 absent if the head revision lies on the trunk
1195 access -- ???
1196 symbols -- sub-alist of (SYMBOL . REVISION) elements
1197 locks -- if file is checked out, something like \"ttn:1.7\"
1198 strict -- t if \"strict locking\" is in effect, otherwise nil
1199 comment -- may be absent; typically something like \"# \" or \"; \"
1200 expand -- may be absent; ???
1202 For `revisions', the car is REVISION (string), the cdr a sub-alist,
1203 with string values (unless otherwise specified) and keys:
1205 date -- a time value (like that returned by `encode-time'); as a
1206 special case, a year value less than 100 is augmented by 1900
1207 author -- username
1208 state -- typically \"Exp\" or \"Rel\"
1209 branches -- list of revisions that begin branches from this revision
1210 next -- on the trunk: the chronologically-preceding revision, or \"\";
1211 on a branch: the chronologically-following revision, or \"\"
1212 log -- change log entry
1213 text -- for the head revision on the trunk, the body of the file;
1214 other revisions have `:insn' instead
1215 :insn -- for non-head revisions, a list of parsed instructions
1216 in one of two forms, in both cases START meaning \"first
1217 go to line START\":
1218 - `(START k COUNT)' -- kill COUNT lines
1219 - `(START i TEXT)' -- insert TEXT (a string)
1220 The list is in descending order by START.
1222 The `:insn' key is a keyword to distinguish it as a vc-rcs.el extension."
1223 (setq buffer (get-buffer (or buffer (current-buffer))))
1224 (set-buffer buffer)
1225 ;; An RCS masterfile can be viewed as containing four regular (for the
1226 ;; most part) sections: (a) the "headers", (b) the "rev headers", (c)
1227 ;; the "description" and (d) the "rev bodies", in that order. In the
1228 ;; returned alist (see docstring), elements from (b) and (d) are
1229 ;; combined pairwise to form the "revisions", while those from (a) and
1230 ;; (c) are simply combined to form the "headers".
1232 ;; Loosely speaking, each section contains a series of alternating
1233 ;; "tags" and "printed representations". In the (b) and (d), many
1234 ;; such series can appear, and a revision number on a line by itself
1235 ;; precedes the series of tags and printed representations associated
1236 ;; with it.
1238 ;; In (a) and (b), the printed representations (with the exception of
1239 ;; the `comment' tag in the headers) terminate with a semicolon, which
1240 ;; is NOT part of the "value" finally associated with the tag. All
1241 ;; other printed representations are in "@@-format"; there is an "@",
1242 ;; the middle part (to be translated into the value), another "@" and
1243 ;; a newline. Each "@@" in the middle part indicates the position of
1244 ;; a single "@" (and consequently the requirement of an additional
1245 ;; initial step when translating to the value).
1247 ;; Parser state includes vars that collect parts of the return value...
1248 (let ((desc nil) (headers nil) (revs nil)
1249 ;; ... as well as vars that support a single-pass, tag-assisted,
1250 ;; minimal-data-copying scan. Basically -- skirting around the
1251 ;; grouping by revision required in (b) and (d) -- we repeatedly
1252 ;; and context-sensitively read a tag (that MUST be present),
1253 ;; determine the bounds of the printed representation, translate
1254 ;; it into a value, and push the tag plus value onto one of the
1255 ;; collection vars. Finally, we return the parse tree
1256 ;; incorporating the values of the collection vars (see "rv").
1258 ;; A symbol or string to keep track of context (for error messages).
1259 context
1260 ;; A symbol, the current tag.
1262 ;; Region (begin and end buffer positions) of the printed
1263 ;; representation for the current tag.
1265 ;; A list of buffer positions where "@@" can be found within the
1266 ;; printed representation region. For each location, we push two
1267 ;; elements onto the list, 1+ and 2+ the location, respectively,
1268 ;; with the 2+ appearing at the head. In this way, the expression
1269 ;; `(,e ,@@-holes ,b)
1270 ;; describes regions that can be concatenated (in reverse order)
1271 ;; to "de-@@-format" the printed representation as the first step
1272 ;; to translating it into some value. See internal func `gather'.
1273 @-holes)
1274 (cl-flet*
1275 ((sw () (skip-chars-forward " \t\n")) ; i.e., `[:space:]'
1276 (at (tag) (save-excursion (eq tag (read buffer))))
1277 (to-eol () (buffer-substring-no-properties
1278 (point) (progn (forward-line 1)
1279 (1- (point)))))
1280 (to-semi () (setq b (point)
1281 e (progn (search-forward ";")
1282 (1- (point)))))
1283 (to-one@ () (setq @-holes nil
1284 b (progn (search-forward "@") (point))
1285 e (progn (while (and (search-forward "@")
1286 (= ?@ (char-after)))
1287 (push (point) @-holes)
1288 (forward-char 1)
1289 (push (point) @-holes))
1290 (1- (point)))))
1291 (tok+val (set-b+e name &optional proc)
1292 (unless (eq name (setq tok (read buffer)))
1293 (error "Missing `%s' while parsing %s" name context))
1294 (sw)
1295 (funcall set-b+e)
1296 (cons tok (if proc
1297 (funcall proc)
1298 (buffer-substring-no-properties b e))))
1299 (k-semi (name &optional proc) (tok+val #'to-semi name proc))
1300 (gather (b e @-holes)
1301 (let ((pairs `(,e ,@@-holes ,b))
1302 acc)
1303 (while pairs
1304 (push (buffer-substring-no-properties
1305 (cadr pairs) (car pairs))
1306 acc)
1307 (setq pairs (cddr pairs)))
1308 (apply #'concat acc)))
1309 (gather1 () (gather b e @-holes))
1310 (k-one@ (name &optional later)
1311 (tok+val #'to-one@ name (if later (lambda () t) #'gather1))))
1312 (save-excursion
1313 (goto-char (point-min))
1314 ;; headers
1315 (setq context 'headers)
1316 (cl-flet ((hpush (name &optional proc)
1317 (push (k-semi name proc) headers)))
1318 (hpush 'head)
1319 (when (at 'branch)
1320 (hpush 'branch))
1321 (hpush 'access)
1322 (hpush 'symbols
1323 (lambda ()
1324 (mapcar (lambda (together)
1325 (let ((two (split-string together ":")))
1326 (setcar two (intern (car two)))
1327 (setcdr two (cadr two))
1328 two))
1329 (split-string
1330 (buffer-substring-no-properties b e)))))
1331 (hpush 'locks))
1332 (push `(strict . ,(when (at 'strict)
1333 (search-forward ";")
1335 headers)
1336 (when (at 'comment)
1337 (push (k-one@ 'comment) headers)
1338 (search-forward ";"))
1339 (when (at 'expand)
1340 (push (k-one@ 'expand) headers)
1341 (search-forward ";"))
1342 (setq headers (nreverse headers))
1343 ;; rev headers
1344 (sw) (setq context 'rev-headers)
1345 (while (looking-at "[0-9]")
1346 (push `(,(to-eol)
1347 ,(k-semi 'date
1348 (lambda ()
1349 (let ((ls (mapcar 'string-to-number
1350 (split-string
1351 (buffer-substring-no-properties
1352 b e)
1353 "\\."))))
1354 ;; Hack the year -- verified to be the
1355 ;; same algorithm used in RCS 5.7.
1356 (when (< (car ls) 100)
1357 (setcar ls (+ 1900 (car ls))))
1358 (apply #'encode-time (nreverse ls)))))
1359 ,@(mapcar #'k-semi '(author state))
1360 ,(k-semi 'branches
1361 (lambda ()
1362 (split-string
1363 (buffer-substring-no-properties b e))))
1364 ,(k-semi 'next))
1365 revs)
1366 (sw))
1367 (setq revs (nreverse revs))
1368 ;; desc
1369 (sw) (setq context 'desc
1370 desc (k-one@ 'desc))
1371 ;; rev bodies
1372 (let (acc
1373 ;; Element of `revs' that initially holds only header info.
1374 ;; "Pairwise combination" occurs when we add body info.
1376 ;; Components of the editing commands (aside from the actual
1377 ;; text) that comprise the `text' printed representations
1378 ;; (not including the "head" revision).
1379 cmd start act
1380 ;; Ascending (reversed) `@-holes' which the internal func
1381 ;; `incg' pops to effect incremental gathering.
1383 ;; Function to extract text (for the `a' command), either
1384 ;; `incg' or `buffer-substring-no-properties'. (This is
1385 ;; for speed; strictly speaking, it is sufficient to use
1386 ;; only the former since it behaves identically to the
1387 ;; latter in the absence of "@@".)
1388 sub)
1389 (cl-flet ((incg (beg end)
1390 (let ((b beg) (e end) @-holes)
1391 (while (and asc (< (car asc) e))
1392 (push (pop asc) @-holes)
1393 (push (pop asc) @-holes))
1394 ;; Self-deprecate when work is done.
1395 ;; Folding many dimensions into one.
1396 ;; Thanks B.Mandelbrot, for complex sum.
1397 ;; O beauteous math! --the Unvexed Bum
1398 (unless asc
1399 (setq sub #'buffer-substring-no-properties))
1400 (gather b e @-holes))))
1401 (while (and (sw)
1402 (not (eobp))
1403 (setq context (to-eol)
1404 rev (or (assoc context revs)
1405 (error "Rev `%s' has body but no head"
1406 context))))
1407 (push (k-one@ 'log) (cdr rev))
1408 ;; For rev body `text' tags, delay translation slightly...
1409 (push (k-one@ 'text t) (cdr rev))
1410 ;; ... until we decide which tag and value is appropriate to
1411 ;; collect. For the "head" revision, compute the value of the
1412 ;; `text' printed representation by simple `gather'. For all
1413 ;; other revisions, replace the `text' tag+value with `:insn'
1414 ;; plus value, always scanning in-place.
1415 (if (string= context (cdr (assq 'head headers)))
1416 (setcdr (cadr rev) (gather b e @-holes))
1417 (if @-holes
1418 (setq asc (nreverse @-holes)
1419 sub #'incg)
1420 (setq sub #'buffer-substring-no-properties))
1421 (goto-char b)
1422 (setq acc nil)
1423 (while (< (point) e)
1424 (forward-char 1)
1425 (setq cmd (char-before)
1426 start (read (current-buffer))
1427 act (read (current-buffer)))
1428 (forward-char 1)
1429 (push (pcase cmd
1431 ;; `d' means "delete lines".
1432 ;; For Emacs spirit, we use `k' for "kill".
1433 `(,start k ,act))
1435 ;; `a' means "append after this line" but
1436 ;; internally we normalize it so that START
1437 ;; specifies the actual line for insert, thus
1438 ;; requiring less hair in the realization algs.
1439 ;; For Emacs spirit, we use `i' for "insert".
1440 `(,(1+ start) i
1441 ,(funcall sub (point) (progn (forward-line act)
1442 (point)))))
1443 (_ (error "Bad command `%c' in `text' for rev `%s'"
1444 cmd context)))
1445 acc))
1446 (goto-char (1+ e))
1447 (setcar (cdr rev) (cons :insn acc)))))))
1448 ;; rv
1449 `((headers ,desc ,@headers)
1450 (revisions ,@revs)))))
1452 (provide 'vc-rcs)
1454 ;;; vc-rcs.el ends here