+(defun vc-rcs-checkin (file rev comment)
+ "RCS-specific version of `vc-backend-checkin'."
+ (let ((switches (vc-switches 'RCS 'checkin)))
+ (let ((old-version (vc-workfile-version file)) new-version
+ (default-branch (vc-file-getprop file 'vc-rcs-default-branch)))
+ ;; Force branch creation if an appropriate
+ ;; default branch has been set.
+ (and (not rev)
+ default-branch
+ (string-match (concat "^" (regexp-quote old-version) "\\.")
+ default-branch)
+ (setq rev default-branch)
+ (setq switches (cons "-f" switches)))
+ (if (and (not rev) old-version)
+ (setq rev (vc-branch-part old-version)))
+ (apply 'vc-do-command nil 0 "ci" (vc-name file)
+ ;; if available, use the secure check-in option
+ (and (vc-rcs-release-p "5.6.4") "-j")
+ (concat (if vc-keep-workfiles "-u" "-r") rev)
+ (concat "-m" comment)
+ switches)
+ (vc-file-setprop file 'vc-workfile-version nil)
+
+ ;; determine the new workfile version
+ (set-buffer "*vc*")
+ (goto-char (point-min))
+ (when (or (re-search-forward
+ "new revision: \\([0-9.]+\\);" nil t)
+ (re-search-forward
+ "reverting to previous revision \\([0-9.]+\\)" nil t))
+ (setq new-version (match-string 1))
+ (vc-file-setprop file 'vc-workfile-version new-version))
+
+ ;; if we got to a different branch, adjust the default
+ ;; branch accordingly
+ (cond
+ ((and old-version new-version
+ (not (string= (vc-branch-part old-version)
+ (vc-branch-part new-version))))
+ (vc-rcs-set-default-branch file
+ (if (vc-trunk-p new-version) nil
+ (vc-branch-part new-version)))
+ ;; If this is an old RCS release, we might have
+ ;; to remove a remaining lock.
+ (if (not (vc-rcs-release-p "5.6.2"))
+ ;; exit status of 1 is also accepted.
+ ;; It means that the lock was removed before.
+ (vc-do-command nil 1 "rcs" (vc-name file)
+ (concat "-u" old-version))))))))
+
+(defun vc-rcs-find-version (file rev buffer)
+ (apply 'vc-do-command
+ buffer 0 "co" (vc-name file)
+ "-q" ;; suppress diagnostic output
+ (concat "-p" rev)
+ (vc-switches 'RCS 'checkout)))
+
+(defun vc-rcs-checkout (file &optional editable rev)
+ "Retrieve a copy of a saved version of FILE."
+ (let ((file-buffer (get-file-buffer file))
+ switches)
+ (message "Checking out %s..." file)
+ (save-excursion
+ ;; Change buffers to get local value of vc-checkout-switches.
+ (if file-buffer (set-buffer file-buffer))
+ (setq switches (vc-switches 'RCS 'checkout))
+ ;; Save this buffer's default-directory
+ ;; and use save-excursion to make sure it is restored
+ ;; in the same buffer it was saved in.
+ (let ((default-directory default-directory))
+ (save-excursion
+ ;; Adjust the default-directory so that the check-out creates
+ ;; the file in the right place.
+ (setq default-directory (file-name-directory file))
+ (let (new-version)
+ ;; if we should go to the head of the trunk,
+ ;; clear the default branch first
+ (and rev (string= rev "")
+ (vc-rcs-set-default-branch file nil))
+ ;; now do the checkout
+ (apply 'vc-do-command
+ nil 0 "co" (vc-name file)
+ ;; If locking is not strict, force to overwrite
+ ;; the writable workfile.
+ (if (eq (vc-checkout-model file) 'implicit) "-f")
+ (if editable "-l")
+ (if (stringp rev)
+ ;; a literal revision was specified
+ (concat "-r" rev)
+ (let ((workrev (vc-workfile-version file)))
+ (if workrev
+ (concat "-r"
+ (if (not rev)
+ ;; no revision specified:
+ ;; use current workfile version
+ workrev
+ ;; REV is t ...
+ (if (not (vc-trunk-p workrev))
+ ;; ... go to head of current branch
+ (vc-branch-part workrev)
+ ;; ... go to head of trunk
+ (vc-rcs-set-default-branch file
+ nil)
+ ""))))))
+ switches)
+ ;; determine the new workfile version
+ (with-current-buffer "*vc*"
+ (setq new-version
+ (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
+ (vc-file-setprop file 'vc-workfile-version new-version)
+ ;; if necessary, adjust the default branch
+ (and rev (not (string= rev ""))
+ (vc-rcs-set-default-branch
+ file
+ (if (vc-rcs-latest-on-branch-p file new-version)
+ (if (vc-trunk-p new-version) nil
+ (vc-branch-part new-version))
+ new-version)))))
+ (message "Checking out %s...done" file)))))
+
+(defun vc-rcs-revert (file &optional contents-done)
+ "Revert FILE to the version it was based on."
+ (vc-do-command nil 0 "co" (vc-name file) "-f"
+ (concat (if (eq (vc-state file) 'edited) "-u" "-r")
+ (vc-workfile-version file))))
+
+(defun vc-rcs-cancel-version (file editable)
+ "Undo the most recent checkin of FILE.
+EDITABLE non-nil means previous version should be locked."
+ (let* ((target (vc-workfile-version file))
+ (previous (if (vc-trunk-p target) "" (vc-branch-part target)))
+ (config (current-window-configuration))
+ (done nil))
+ (vc-do-command nil 0 "rcs" (vc-name file) (concat "-o" target))
+ ;; Check out the most recent remaining version. If it fails, because
+ ;; the whole branch got deleted, do a double-take and check out the
+ ;; version where the branch started.
+ (while (not done)
+ (condition-case err
+ (progn
+ (vc-do-command nil 0 "co" (vc-name file) "-f"
+ (concat (if editable "-l" "-u") previous))
+ (setq done t))
+ (error (set-buffer "*vc*")
+ (goto-char (point-min))
+ (if (search-forward "no side branches present for" nil t)
+ (progn (setq previous (vc-branch-part previous))
+ (vc-rcs-set-default-branch file previous)
+ ;; vc-do-command popped up a window with
+ ;; the error message. Get rid of it, by
+ ;; restoring the old window configuration.
+ (set-window-configuration config))
+ ;; No, it was some other error: re-signal it.
+ (signal (car err) (cdr err))))))))
+
+(defun vc-rcs-merge (file first-version &optional second-version)
+ "Merge changes into current working copy of FILE.
+The changes are between FIRST-VERSION and SECOND-VERSION."
+ (vc-do-command nil 1 "rcsmerge" (vc-name file)
+ "-kk" ; ignore keyword conflicts
+ (concat "-r" first-version)
+ (if second-version (concat "-r" second-version))))
+
+(defun vc-rcs-steal-lock (file &optional rev)
+ "Steal the lock on the current workfile for FILE and revision REV.
+Needs RCS 5.6.2 or later for -M."
+ (vc-do-command nil 0 "rcs" (vc-name file) "-M" (concat "-u" rev))
+ ;; Do a real checkout after stealing the lock, so that we see
+ ;; expanded headers.
+ (vc-do-command nil 0 "co" (vc-name file) "-f" (concat "-l" rev)))
+
+
+\f
+;;;
+;;; History functions
+;;;
+
+(defun vc-rcs-print-log (file &optional buffer)
+ "Get change log associated with FILE."
+ (vc-do-command buffer 0 "rlog" (vc-name file)))
+
+(defun vc-rcs-diff (file &optional oldvers newvers buffer)
+ "Get a difference report using RCS between two versions of FILE."
+ (if (not oldvers) (setq oldvers (vc-workfile-version file)))
+ (apply 'vc-do-command (or buffer "*vc-diff*") 1 "rcsdiff" file
+ (append (list "-q"
+ (concat "-r" oldvers)
+ (and newvers (concat "-r" newvers)))
+ (vc-switches 'RCS 'diff))))
+
+(defun vc-rcs-annotate-command (file buffer &optional revision)
+ "Annotate FILE, inserting the results in BUFFER.
+Optional arg REVISION is a revision to annotate from."
+ (vc-setup-buffer buffer)
+ ;; Aside from the "head revision on the trunk", the instructions for
+ ;; each revision on the trunk are an ordered list of kill and insert
+ ;; commands necessary to go from the chronologically-following
+ ;; revision to this one. That is, associated with revision N are
+ ;; edits that applied to revision N+1 would result in revision N.
+ ;;
+ ;; On a branch, however, (some) things are inverted: the commands
+ ;; listed are those necessary to go from the chronologically-preceding
+ ;; revision to this one. That is, associated with revision N are
+ ;; edits that applied to revision N-1 would result in revision N.
+ ;;
+ ;; So, to get per-line history info, we apply reverse-chronological
+ ;; edits, starting with the head revision on the trunk, all the way
+ ;; back through the initial revision (typically "1.1" or similar),
+ ;; then apply forward-chronological edits -- keeping track of which
+ ;; revision is associated with each inserted line -- until we reach
+ ;; the desired revision for display (which may be either on the trunk
+ ;; or on a branch).
+ (let* ((tree (with-temp-buffer
+ (insert-file-contents (vc-rcs-registered file))
+ (vc-rcs-parse)))
+ (revisions (cdr (assq 'revisions tree)))
+ ;; The revision N whose instructions we currently are processing.
+ (cur (cdr (assq 'head (cdr (assq 'headers tree)))))
+ ;; Alist from the parse tree for N.
+ (meta (cdr (assoc cur revisions)))
+ ;; Point and temporary string, respectively.
+ p s
+ ;; "Next-branch list". Nil means the desired revision to
+ ;; display lives on the trunk. Non-nil means it lives on a
+ ;; branch, in which case the value is a list of revision pairs
+ ;; (PARENT . CHILD), the first PARENT being on the trunk, that
+ ;; links each series of revisions in the path from the initial
+ ;; revision to the desired revision to display.
+ nbls
+ ;; "Path-accumulate-predicate plus revision/date/author".
+ ;; Until set, forward-chronological edits are not accumulated.
+ ;; Once set, its value (updated every revision) is used for
+ ;; the text property `:vc-rcs-r/d/a' for inserts during
+ ;; processing of forward-chronological instructions for N.
+ ;; See internal func `r/d/a'.
+ prda
+ ;; List of forward-chronological instructions, each of the
+ ;; form: (POS . ACTION), where POS is a buffer position. If
+ ;; ACTION is a string, it is inserted, otherwise it is taken as
+ ;; the number of characters to be deleted.
+ path
+ ;; N+1. When `cur' is "", this is the initial revision.
+ pre)
+ (unless revision
+ (setq revision cur))
+ (unless (assoc revision revisions)
+ (error "No such revision: %s" revision))
+ ;; Find which branches (if any) must be included in the edits.
+ (let ((par revision)
+ bpt kids)
+ (while (setq bpt (vc-branch-part par)
+ par (vc-branch-part bpt))
+ (setq kids (cdr (assq 'branches (cdr (assoc par revisions)))))
+ ;; A branchpoint may have multiple children. Find the right one.
+ (while (not (string= bpt (vc-branch-part (car kids))))
+ (setq kids (cdr kids)))
+ (push (cons par (car kids)) nbls)))
+ ;; Start with the full text.
+ (set-buffer buffer)
+ (insert (cdr (assq 'text meta)))
+ ;; Apply reverse-chronological edits on the trunk, computing and
+ ;; accumulating forward-chronological edits after some point, for
+ ;; later.
+ (flet ((r/d/a () (vector pre
+ (cdr (assq 'date meta))
+ (cdr (assq 'author meta)))))
+ (while (when (setq pre cur cur (cdr (assq 'next meta)))
+ (not (string= "" cur)))
+ (setq
+ ;; Start accumulating the forward-chronological edits when N+1
+ ;; on the trunk is either the desired revision to display, or
+ ;; the appropriate branchpoint for it. Do this before
+ ;; updating `meta' since `r/d/a' uses N+1's `meta' value.
+ prda (when (or prda (string= (if nbls (caar nbls) revision) pre))
+ (r/d/a))
+ meta (cdr (assoc cur revisions)))
+ ;; Edits in the parse tree specify a line number (in the buffer
+ ;; *BEFORE* editing occurs) to start from, but line numbers
+ ;; change as a result of edits. To DTRT, we apply edits in
+ ;; order of descending buffer position so that edits further
+ ;; down in the buffer occur first w/o corrupting specified
+ ;; buffer positions of edits occurring towards the beginning of
+ ;; the buffer. In this way we avoid using markers. A pleasant
+ ;; property of this approach is ability to push instructions
+ ;; onto `path' directly, w/o need to maintain rev boundaries.
+ (dolist (insn (cdr (assq :insn meta)))
+ (goto-line (pop insn))
+ (setq p (point))
+ (case (pop insn)
+ (k (setq s (buffer-substring-no-properties
+ p (progn (forward-line (car insn))
+ (point))))
+ (when prda
+ (push `(,p . ,(propertize s :vc-rcs-r/d/a prda)) path))
+ (delete-region p (point)))
+ (i (setq s (car insn))
+ (when prda
+ (push `(,p . ,(length s)) path))
+ (insert s)))))
+ ;; For the initial revision, setting `:vc-rcs-r/d/a' directly is
+ ;; equivalent to pushing an insert instruction (of the entire buffer
+ ;; contents) onto `path' then erasing the buffer, but less wasteful.
+ (put-text-property (point-min) (point-max) :vc-rcs-r/d/a (r/d/a))
+ ;; Now apply the forward-chronological edits for the trunk.
+ (dolist (insn path)
+ (goto-char (pop insn))
+ (if (stringp insn)
+ (insert insn)
+ (delete-char insn)))
+ ;; Now apply the forward-chronological edits (directly from the
+ ;; parse-tree) for the branch(es), if necessary. We re-use vars
+ ;; `pre' and `meta' for the sake of internal func `r/d/a'.
+ (while nbls
+ (setq pre (cdr (pop nbls)))
+ (while (progn
+ (setq meta (cdr (assoc pre revisions))
+ prda nil)
+ (dolist (insn (cdr (assq :insn meta)))
+ (goto-line (pop insn))
+ (case (pop insn)
+ (k (delete-region
+ (point) (progn (forward-line (car insn))
+ (point))))
+ (i (insert (propertize
+ (car insn)
+ :vc-rcs-r/d/a
+ (or prda (setq prda (r/d/a))))))))
+ (prog1 (not (string= (if nbls (caar nbls) revision) pre))
+ (setq pre (cdr (assq 'next meta)))))))))
+ ;; Lastly, for each line, insert at bol nicely-formatted history info.
+ ;; We do two passes to collect summary information used to minimize
+ ;; the annotation's usage of screen real-estate: (1) Consider rendered
+ ;; width of revision plus author together as a unit; and (2) Omit
+ ;; author entirely if all authors are the same as the user.
+ (let ((ht (make-hash-table :test 'eq))
+ (me (user-login-name))
+ (maxw 0)
+ (all-me t)
+ rda w a)
+ (goto-char (point-max))
+ (while (not (bobp))
+ (forward-line -1)
+ (setq rda (get-text-property (point) :vc-rcs-r/d/a))
+ (unless (gethash rda ht)
+ (setq a (aref rda 2)
+ all-me (and all-me (string= a me)))
+ (puthash rda (setq w (+ (length (aref rda 0))
+ (length a)))
+ ht)
+ (setq maxw (max w maxw))))
+ (let ((padding (make-string maxw 32)))
+ (flet ((pad (w) (substring-no-properties padding w))
+ (render (rda &rest ls)
+ (propertize
+ (apply 'concat
+ (format-time-string "%Y-%m-%d" (aref rda 1))
+ " "
+ (aref rda 0)
+ ls)
+ :vc-rcs-r/d/a rda)))
+ (maphash
+ (if all-me
+ (lambda (rda w)
+ (puthash rda (render rda (pad w) ": ") ht))
+ (lambda (rda w)
+ (puthash rda (render rda " " (pad w) " " (aref rda 2) ": ") ht)))
+ ht)))
+ (while (not (eobp))
+ (insert (gethash (get-text-property (point) :vc-rcs-r/d/a) ht))
+ (forward-line 1))))
+
+(defun vc-rcs-annotate-current-time ()
+ "Return the current time, based at midnight of the current day, and
+encoded as fractional days."
+ (vc-annotate-convert-time
+ (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
+
+(defun vc-rcs-annotate-time ()
+ "Return the time of the next annotation (as fraction of days)
+systime, or nil if there is none. Also, reposition point."
+ (unless (eobp)
+ (search-forward ": ")
+ (vc-annotate-convert-time
+ (aref (get-text-property (point) :vc-rcs-r/d/a) 1))))
+
+(defun vc-rcs-annotate-extract-revision-at-line ()
+ (aref (get-text-property (point) :vc-rcs-r/d/a) 0))
+
+\f
+;;;
+;;; Snapshot system
+;;;
+
+(defun vc-rcs-assign-name (file name)
+ "Assign to FILE's latest version a given NAME."
+ (vc-do-command nil 0 "rcs" (vc-name file) (concat "-n" name ":")))
+
+\f
+;;;
+;;; Miscellaneous
+;;;
+
+(defun vc-rcs-check-headers ()
+ "Check if the current file has any headers in it."
+ (save-excursion
+ (goto-char (point-min))
+ (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
+\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
+
+(defun vc-rcs-clear-headers ()
+ "Implementation of vc-clear-headers for RCS."
+ (let ((case-fold-search nil))
+ (goto-char (point-min))
+ (while (re-search-forward
+ (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
+ "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
+ nil t)
+ (replace-match "$\\1$"))))
+
+(defun vc-rcs-rename-file (old new)
+ ;; Just move the master file (using vc-rcs-master-templates).
+ (vc-rename-master (vc-name old) new vc-rcs-master-templates))
+
+\f
+;;;
+;;; Internal functions
+;;;
+
+(defun vc-rcs-workfile-is-newer (file)
+ "Return non-nil if FILE is newer than its RCS master.
+This likely means that FILE has been changed with respect
+to its master version."
+ (let ((file-time (nth 5 (file-attributes file)))
+ (master-time (nth 5 (file-attributes (vc-name file)))))
+ (or (> (nth 0 file-time) (nth 0 master-time))
+ (and (= (nth 0 file-time) (nth 0 master-time))
+ (> (nth 1 file-time) (nth 1 master-time))))))