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