1;;; vc-rcs.el --- support for RCS version-control
2
3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4;;   2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6;; Author:     FSF (see vc.el for full credits)
7;; Maintainer: Andre Spiegel <spiegel@gnu.org>
8
9;; $Id$
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING.  If not, write to the
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
27
28;;; Commentary:
29
30;; See vc.el
31
32;;; Code:
33
34;;;
35;;; Customization options
36;;;
37
38(eval-when-compile
39  (require 'cl)
40  (require 'vc))
41
42(defcustom vc-rcs-release nil
43  "*The release number of your RCS installation, as a string.
44If nil, VC itself computes this value when it is first needed."
45  :type '(choice (const :tag "Auto" nil)
46		 (string :tag "Specified")
47		 (const :tag "Unknown" unknown))
48  :group 'vc)
49
50(defcustom vc-rcs-register-switches nil
51  "*Extra switches for registering a file in RCS.
52A string or list of strings.  These are passed to the checkin program
53by \\[vc-rcs-register]."
54  :type '(choice (const :tag "None" nil)
55		 (string :tag "Argument String")
56		 (repeat :tag "Argument List"
57			 :value ("")
58			 string))
59  :version "21.1"
60  :group 'vc)
61
62(defcustom vc-rcs-diff-switches nil
63  "*A string or list of strings specifying extra switches for rcsdiff under VC."
64  :type '(choice (const :tag "None" nil)
65		 (string :tag "Argument String")
66		 (repeat :tag "Argument List"
67			 :value ("")
68			 string))
69  :version "21.1"
70  :group 'vc)
71
72(defcustom vc-rcs-header (or (cdr (assoc 'RCS vc-header-alist)) '("\$Id\$"))
73  "*Header keywords to be inserted by `vc-insert-headers'."
74  :type '(repeat string)
75  :version "21.1"
76  :group 'vc)
77
78(defcustom vc-rcsdiff-knows-brief nil
79  "*Indicates whether rcsdiff understands the --brief option.
80The value is either `yes', `no', or nil.  If it is nil, VC tries
81to use --brief and sets this variable to remember whether it worked."
82  :type '(choice (const :tag "Work out" nil) (const yes) (const no))
83  :group 'vc)
84
85;;;###autoload
86(defcustom vc-rcs-master-templates
87  '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s")
88  "*Where to look for RCS master files.
89For a description of possible values, see `vc-check-master-templates'."
90  :type '(choice (const :tag "Use standard RCS file names"
91			'("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
92		 (repeat :tag "User-specified"
93			 (choice string
94				 function)))
95  :version "21.1"
96  :group 'vc)
97
98
99;;;
100;;; State-querying functions
101;;;
102
103;;; The autoload cookie below places vc-rcs-registered directly into
104;;; loaddefs.el, so that vc-rcs.el does not need to be loaded for
105;;; every file that is visited.  The definition is repeated below
106;;; so that Help and etags can find it.
107
108;;;###autoload (defun vc-rcs-registered (f) (vc-default-registered 'RCS f))
109(defun vc-rcs-registered (f) (vc-default-registered 'RCS f))
110
111(defun vc-rcs-state (file)
112  "Implementation of `vc-state' for RCS."
113  (or (boundp 'vc-rcs-headers-result)
114      (and vc-consult-headers
115           (vc-rcs-consult-headers file)))
116  (let ((state
117         ;; vc-workfile-version might not be known; in that case the
118         ;; property is nil.  vc-rcs-fetch-master-state knows how to
119         ;; handle that.
120         (vc-rcs-fetch-master-state file
121                                    (vc-file-getprop file
122                                                     'vc-workfile-version))))
123    (if (not (eq state 'up-to-date))
124        state
125      (if (vc-workfile-unchanged-p file)
126          'up-to-date
127        (if (eq (vc-checkout-model file) 'locking)
128            'unlocked-changes
129          'edited)))))
130
131(defun vc-rcs-state-heuristic (file)
132  "State heuristic for RCS."
133  (let (vc-rcs-headers-result)
134    (if (and vc-consult-headers
135             (setq vc-rcs-headers-result
136                   (vc-rcs-consult-headers file))
137             (eq vc-rcs-headers-result 'rev-and-lock))
138        (let ((state (vc-file-getprop file 'vc-state)))
139          ;; If the headers say that the file is not locked, the
140          ;; permissions can tell us whether locking is used for
141          ;; the file or not.
142          (if (and (eq state 'up-to-date)
143                   (not (vc-mistrust-permissions file)))
144              (cond
145               ((string-match ".rw..-..-." (nth 8 (file-attributes file)))
146                (vc-file-setprop file 'vc-checkout-model 'implicit)
147		(setq state
148		      (if (vc-rcs-workfile-is-newer file)
149			  'edited
150			'up-to-date)))
151               ((string-match ".r-..-..-." (nth 8 (file-attributes file)))
152                (vc-file-setprop file 'vc-checkout-model 'locking))))
153          state)
154      (if (not (vc-mistrust-permissions file))
155          (let* ((attributes  (file-attributes file 'string))
156                 (owner-name  (nth 2 attributes))
157                 (permissions (nth 8 attributes)))
158            (cond ((string-match ".r-..-..-." permissions)
159                   (vc-file-setprop file 'vc-checkout-model 'locking)
160                   'up-to-date)
161                  ((string-match ".rw..-..-." permissions)
162		   (if (eq (vc-checkout-model file) 'locking)
163		       (if (file-ownership-preserved-p file)
164			   'edited
165			 owner-name)
166		     (if (vc-rcs-workfile-is-newer file)
167			 'edited
168		       'up-to-date)))
169                  (t
170                   ;; Strange permissions.  Fall through to
171                   ;; expensive state computation.
172                   (vc-rcs-state file))))
173        (vc-rcs-state file)))))
174
175(defun vc-rcs-workfile-version (file)
176  "RCS-specific version of `vc-workfile-version'."
177  (or (and vc-consult-headers
178           (vc-rcs-consult-headers file)
179           (vc-file-getprop file 'vc-workfile-version))
180      (progn
181        (vc-rcs-fetch-master-state file)
182        (vc-file-getprop file 'vc-workfile-version))))
183
184(defun vc-rcs-latest-on-branch-p (file &optional version)
185  "Return non-nil if workfile version of FILE is the latest on its branch.
186When VERSION is given, perform check for that version."
187  (unless version (setq version (vc-workfile-version file)))
188  (with-temp-buffer
189    (string= version
190	     (if (vc-trunk-p version)
191		 (progn
192		   ;; Compare VERSION to the head version number.
193		   (vc-insert-file (vc-name file) "^[0-9]")
194		   (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
195	       ;; If we are not on the trunk, we need to examine the
196	       ;; whole current branch.
197	       (vc-insert-file (vc-name file) "^desc")
198	       (vc-rcs-find-most-recent-rev (vc-branch-part version))))))
199
200(defun vc-rcs-checkout-model (file)
201  "RCS-specific version of `vc-checkout-model'."
202  (let (result)
203    (when vc-consult-headers
204      (vc-file-setprop file 'vc-checkout-model nil)
205      (vc-rcs-consult-headers file)
206      (setq result (vc-file-getprop file 'vc-checkout-model)))
207    (or result
208        (progn (vc-rcs-fetch-master-state file)
209               (vc-file-getprop file 'vc-checkout-model)))))
210
211(defun vc-rcs-workfile-unchanged-p (file)
212  "RCS-specific implementation of `vc-workfile-unchanged-p'."
213  ;; Try to use rcsdiff --brief.  If rcsdiff does not understand that,
214  ;; do a double take and remember the fact for the future
215  (let* ((version (concat "-r" (vc-workfile-version file)))
216         (status (if (eq vc-rcsdiff-knows-brief 'no)
217                     (vc-do-command nil 1 "rcsdiff" file version)
218                   (vc-do-command nil 2 "rcsdiff" file "--brief" version))))
219    (if (eq status 2)
220        (if (not vc-rcsdiff-knows-brief)
221            (setq vc-rcsdiff-knows-brief 'no
222                  status (vc-do-command nil 1 "rcsdiff" file version))
223          (error "rcsdiff failed"))
224      (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
225    ;; The workfile is unchanged if rcsdiff found no differences.
226    (zerop status)))
227
228
229;;;
230;;; State-changing functions
231;;;
232
233(defun vc-rcs-register (file &optional rev comment)
234  "Register FILE into the RCS version-control system.
235REV is the optional revision number for the file.  COMMENT can be used
236to provide an initial description of FILE.
237
238`vc-register-switches' and `vc-rcs-register-switches' are passed to
239the RCS command (in that order).
240
241Automatically retrieve a read-only version of the file with keywords
242expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
243    (let ((subdir (expand-file-name "RCS" (file-name-directory file))))
244      (and (not (file-exists-p subdir))
245	   (not (directory-files (file-name-directory file)
246				 nil ".*,v$" t))
247	   (yes-or-no-p "Create RCS subdirectory? ")
248	   (make-directory subdir))
249      (apply 'vc-do-command nil 0 "ci" file
250	     ;; if available, use the secure registering option
251	     (and (vc-rcs-release-p "5.6.4") "-i")
252	     (concat (if vc-keep-workfiles "-u" "-r") rev)
253	     (and comment (concat "-t-" comment))
254	     (vc-switches 'RCS 'register))
255      ;; parse output to find master file name and workfile version
256      (with-current-buffer "*vc*"
257        (goto-char (point-min))
258        (let ((name (if (looking-at (concat "^\\(.*\\)  <--  "
259                                            (file-name-nondirectory file)))
260                        (match-string 1))))
261          (if (not name)
262              ;; if we couldn't find the master name,
263              ;; run vc-rcs-registered to get it
264              ;; (will be stored into the vc-name property)
265              (vc-rcs-registered file)
266            (vc-file-setprop file 'vc-name
267                             (if (file-name-absolute-p name)
268                                 name
269                               (expand-file-name
270                                name
271                                (file-name-directory file))))))
272        (vc-file-setprop file 'vc-workfile-version
273                         (if (re-search-forward
274                              "^initial revision: \\([0-9.]+\\).*\n"
275                              nil t)
276                             (match-string 1))))))
277
278(defun vc-rcs-responsible-p (file)
279  "Return non-nil if RCS thinks it would be responsible for registering FILE."
280  ;; TODO: check for all the patterns in vc-rcs-master-templates
281  (file-directory-p (expand-file-name "RCS" (file-name-directory file))))
282
283(defun vc-rcs-receive-file (file rev)
284  "Implementation of receive-file for RCS."
285  (let ((checkout-model (vc-checkout-model file)))
286    (vc-rcs-register file rev "")
287    (when (eq checkout-model 'implicit)
288      (vc-rcs-set-non-strict-locking file))
289    (vc-rcs-set-default-branch file (concat rev ".1"))))
290
291(defun vc-rcs-unregister (file)
292  "Unregister FILE from RCS.
293If this leaves the RCS subdirectory empty, ask the user
294whether to remove it."
295  (let* ((master (vc-name file))
296	 (dir (file-name-directory master))
297	 (backup-info (find-backup-file-name master)))
298    (if (not backup-info)
299	(delete-file master)
300      (rename-file master (car backup-info) 'ok-if-already-exists)
301      (dolist (f (cdr backup-info)) (ignore-errors (delete-file f))))
302    (and (string= (file-name-nondirectory (directory-file-name dir)) "RCS")
303	 ;; check whether RCS dir is empty, i.e. it does not
304	 ;; contain any files except "." and ".."
305	 (not (directory-files dir nil
306			       "^\\([^.]\\|\\.[^.]\\|\\.\\.[^.]\\).*"))
307	 (yes-or-no-p (format "Directory %s is empty; remove it? " dir))
308	 (delete-directory dir))))
309
310(defun vc-rcs-checkin (file rev comment)
311  "RCS-specific version of `vc-backend-checkin'."
312  (let ((switches (vc-switches 'RCS 'checkin)))
313    (let ((old-version (vc-workfile-version file)) new-version
314	  (default-branch (vc-file-getprop file 'vc-rcs-default-branch)))
315      ;; Force branch creation if an appropriate
316      ;; default branch has been set.
317      (and (not rev)
318	   default-branch
319	   (string-match (concat "^" (regexp-quote old-version) "\\.")
320			 default-branch)
321	   (setq rev default-branch)
322	   (setq switches (cons "-f" switches)))
323      (if (and (not rev) old-version)
324          (setq rev (vc-branch-part old-version)))
325      (apply 'vc-do-command nil 0 "ci" (vc-name file)
326	     ;; if available, use the secure check-in option
327	     (and (vc-rcs-release-p "5.6.4") "-j")
328	     (concat (if vc-keep-workfiles "-u" "-r") rev)
329	     (concat "-m" comment)
330	     switches)
331      (vc-file-setprop file 'vc-workfile-version nil)
332
333      ;; determine the new workfile version
334      (set-buffer "*vc*")
335      (goto-char (point-min))
336      (when (or (re-search-forward
337		 "new revision: \\([0-9.]+\\);" nil t)
338		(re-search-forward
339		 "reverting to previous revision \\([0-9.]+\\)" nil t))
340	(setq new-version (match-string 1))
341	(vc-file-setprop file 'vc-workfile-version new-version))
342
343      ;; if we got to a different branch, adjust the default
344      ;; branch accordingly
345      (cond
346       ((and old-version new-version
347	     (not (string= (vc-branch-part old-version)
348			   (vc-branch-part new-version))))
349	(vc-rcs-set-default-branch file
350				   (if (vc-trunk-p new-version) nil
351				     (vc-branch-part new-version)))
352	;; If this is an old RCS release, we might have
353	;; to remove a remaining lock.
354	(if (not (vc-rcs-release-p "5.6.2"))
355	    ;; exit status of 1 is also accepted.
356	    ;; It means that the lock was removed before.
357	    (vc-do-command nil 1 "rcs" (vc-name file)
358			   (concat "-u" old-version))))))))
359
360(defun vc-rcs-find-version (file rev buffer)
361  (apply 'vc-do-command
362	 buffer 0 "co" (vc-name file)
363	 "-q" ;; suppress diagnostic output
364	 (concat "-p" rev)
365	 (vc-switches 'RCS 'checkout)))
366
367(defun vc-rcs-checkout (file &optional editable rev)
368  "Retrieve a copy of a saved version of FILE."
369  (let ((file-buffer (get-file-buffer file))
370	switches)
371    (message "Checking out %s..." file)
372    (save-excursion
373      ;; Change buffers to get local value of vc-checkout-switches.
374      (if file-buffer (set-buffer file-buffer))
375      (setq switches (vc-switches 'RCS 'checkout))
376      ;; Save this buffer's default-directory
377      ;; and use save-excursion to make sure it is restored
378      ;; in the same buffer it was saved in.
379      (let ((default-directory default-directory))
380	(save-excursion
381	  ;; Adjust the default-directory so that the check-out creates
382	  ;; the file in the right place.
383	  (setq default-directory (file-name-directory file))
384	  (let (new-version)
385	    ;; if we should go to the head of the trunk,
386	    ;; clear the default branch first
387	    (and rev (string= rev "")
388		 (vc-rcs-set-default-branch file nil))
389	    ;; now do the checkout
390	    (apply 'vc-do-command
391		   nil 0 "co" (vc-name file)
392		   ;; If locking is not strict, force to overwrite
393		   ;; the writable workfile.
394		   (if (eq (vc-checkout-model file) 'implicit) "-f")
395		   (if editable "-l")
396                   (if (stringp rev)
397                       ;; a literal revision was specified
398                       (concat "-r" rev)
399                     (let ((workrev (vc-workfile-version file)))
400                       (if workrev
401                           (concat "-r"
402                                   (if (not rev)
403                                       ;; no revision specified:
404                                       ;; use current workfile version
405                                       workrev
406                                     ;; REV is t ...
407                                     (if (not (vc-trunk-p workrev))
408                                         ;; ... go to head of current branch
409                                         (vc-branch-part workrev)
410                                       ;; ... go to head of trunk
411                                       (vc-rcs-set-default-branch file
412                                                                  nil)
413                                       ""))))))
414		   switches)
415	    ;; determine the new workfile version
416	    (with-current-buffer "*vc*"
417	      (setq new-version
418		    (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
419	    (vc-file-setprop file 'vc-workfile-version new-version)
420	    ;; if necessary, adjust the default branch
421	    (and rev (not (string= rev ""))
422		 (vc-rcs-set-default-branch
423		  file
424		  (if (vc-rcs-latest-on-branch-p file new-version)
425		      (if (vc-trunk-p new-version) nil
426			(vc-branch-part new-version))
427		    new-version)))))
428	(message "Checking out %s...done" file)))))
429
430(defun vc-rcs-revert (file &optional contents-done)
431  "Revert FILE to the version it was based on."
432  (vc-do-command nil 0 "co" (vc-name file) "-f"
433                 (concat (if (eq (vc-state file) 'edited) "-u" "-r")
434                         (vc-workfile-version file))))
435
436(defun vc-rcs-cancel-version (file editable)
437  "Undo the most recent checkin of FILE.
438EDITABLE non-nil means previous version should be locked."
439  (let* ((target (vc-workfile-version file))
440	 (previous (if (vc-trunk-p target) "" (vc-branch-part target)))
441	 (config (current-window-configuration))
442	 (done nil))
443    (vc-do-command nil 0 "rcs" (vc-name file) (concat "-o" target))
444    ;; Check out the most recent remaining version.  If it fails, because
445    ;; the whole branch got deleted, do a double-take and check out the
446    ;; version where the branch started.
447    (while (not done)
448      (condition-case err
449	  (progn
450	    (vc-do-command nil 0 "co" (vc-name file) "-f"
451			   (concat (if editable "-l" "-u") previous))
452	    (setq done t))
453	(error (set-buffer "*vc*")
454	       (goto-char (point-min))
455	       (if (search-forward "no side branches present for" nil t)
456		   (progn (setq previous (vc-branch-part previous))
457			  (vc-rcs-set-default-branch file previous)
458			  ;; vc-do-command popped up a window with
459			  ;; the error message.  Get rid of it, by
460			  ;; restoring the old window configuration.
461			  (set-window-configuration config))
462		 ;; No, it was some other error: re-signal it.
463		 (signal (car err) (cdr err))))))))
464
465(defun vc-rcs-merge (file first-version &optional second-version)
466  "Merge changes into current working copy of FILE.
467The changes are between FIRST-VERSION and SECOND-VERSION."
468  (vc-do-command nil 1 "rcsmerge" (vc-name file)
469		 "-kk"			; ignore keyword conflicts
470		 (concat "-r" first-version)
471		 (if second-version (concat "-r" second-version))))
472
473(defun vc-rcs-steal-lock (file &optional rev)
474  "Steal the lock on the current workfile for FILE and revision REV.
475Needs RCS 5.6.2 or later for -M."
476  (vc-do-command nil 0 "rcs" (vc-name file) "-M" (concat "-u" rev))
477  ;; Do a real checkout after stealing the lock, so that we see
478  ;; expanded headers.
479  (vc-do-command nil 0 "co" (vc-name file) "-f" (concat "-l" rev)))
480
481
482
483;;;
484;;; History functions
485;;;
486
487(defun vc-rcs-print-log (file &optional buffer)
488  "Get change log associated with FILE."
489  (vc-do-command buffer 0 "rlog" (vc-name file)))
490
491(defun vc-rcs-diff (file &optional oldvers newvers buffer)
492  "Get a difference report using RCS between two versions of FILE."
493  (if (not oldvers) (setq oldvers (vc-workfile-version file)))
494  (apply 'vc-do-command (or buffer "*vc-diff*") 1 "rcsdiff" file
495         (append (list "-q"
496                       (concat "-r" oldvers)
497                       (and newvers (concat "-r" newvers)))
498                 (vc-switches 'RCS 'diff))))
499
500(defun vc-rcs-annotate-command (file buffer &optional revision)
501  "Annotate FILE, inserting the results in BUFFER.
502Optional arg REVISION is a revision to annotate from."
503  (vc-setup-buffer buffer)
504  ;; Aside from the "head revision on the trunk", the instructions for
505  ;; each revision on the trunk are an ordered list of kill and insert
506  ;; commands necessary to go from the chronologically-following
507  ;; revision to this one.  That is, associated with revision N are
508  ;; edits that applied to revision N+1 would result in revision N.
509  ;;
510  ;; On a branch, however, (some) things are inverted: the commands
511  ;; listed are those necessary to go from the chronologically-preceding
512  ;; revision to this one.  That is, associated with revision N are
513  ;; edits that applied to revision N-1 would result in revision N.
514  ;;
515  ;; So, to get per-line history info, we apply reverse-chronological
516  ;; edits, starting with the head revision on the trunk, all the way
517  ;; back through the initial revision (typically "1.1" or similar),
518  ;; then apply forward-chronological edits -- keeping track of which
519  ;; revision is associated with each inserted line -- until we reach
520  ;; the desired revision for display (which may be either on the trunk
521  ;; or on a branch).
522  (let* ((tree (with-temp-buffer
523                 (insert-file-contents (vc-rcs-registered file))
524                 (vc-rcs-parse)))
525         (revisions (cdr (assq 'revisions tree)))
526         ;; The revision N whose instructions we currently are processing.
527         (cur (cdr (assq 'head (cdr (assq 'headers tree)))))
528         ;; Alist from the parse tree for N.
529         (meta (cdr (assoc cur revisions)))
530         ;; Point and temporary string, respectively.
531         p s
532         ;; "Next-branch list".  Nil means the desired revision to
533         ;; display lives on the trunk.  Non-nil means it lives on a
534         ;; branch, in which case the value is a list of revision pairs
535         ;; (PARENT . CHILD), the first PARENT being on the trunk, that
536         ;; links each series of revisions in the path from the initial
537         ;; revision to the desired revision to display.
538         nbls
539         ;; "Path-accumulate-predicate plus revision/date/author".
540         ;; Until set, forward-chronological edits are not accumulated.
541         ;; Once set, its value (updated every revision) is used for
542         ;; the text property `:vc-rcs-r/d/a' for inserts during
543         ;; processing of forward-chronological instructions for N.
544         ;; See internal func `r/d/a'.
545         prda
546         ;; List of forward-chronological instructions, each of the
547         ;; form: (POS . ACTION), where POS is a buffer position.  If
548         ;; ACTION is a string, it is inserted, otherwise it is taken as
549         ;; the number of characters to be deleted.
550         path
551         ;; N+1.  When `cur' is "", this is the initial revision.
552         pre)
553    (unless revision
554      (setq revision cur))
555    (unless (assoc revision revisions)
556      (error "No such revision: %s" revision))
557    ;; Find which branches (if any) must be included in the edits.
558    (let ((par revision)
559          bpt kids)
560      (while (setq bpt (vc-branch-part par)
561                   par (vc-branch-part bpt))
562        (setq kids (cdr (assq 'branches (cdr (assoc par revisions)))))
563        ;; A branchpoint may have multiple children.  Find the right one.
564        (while (not (string= bpt (vc-branch-part (car kids))))
565          (setq kids (cdr kids)))
566        (push (cons par (car kids)) nbls)))
567    ;; Start with the full text.
568    (set-buffer buffer)
569    (insert (cdr (assq 'text meta)))
570    ;; Apply reverse-chronological edits on the trunk, computing and
571    ;; accumulating forward-chronological edits after some point, for
572    ;; later.
573    (flet ((r/d/a () (vector pre
574                             (cdr (assq 'date meta))
575                             (cdr (assq 'author meta)))))
576      (while (when (setq pre cur cur (cdr (assq 'next meta)))
577               (not (string= "" cur)))
578        (setq
579         ;; Start accumulating the forward-chronological edits when N+1
580         ;; on the trunk is either the desired revision to display, or
581         ;; the appropriate branchpoint for it.  Do this before
582         ;; updating `meta' since `r/d/a' uses N+1's `meta' value.
583         prda (when (or prda (string= (if nbls (caar nbls) revision) pre))
584                (r/d/a))
585         meta (cdr (assoc cur revisions)))
586        ;; Edits in the parse tree specify a line number (in the buffer
587        ;; *BEFORE* editing occurs) to start from, but line numbers
588        ;; change as a result of edits.  To DTRT, we apply edits in
589        ;; order of descending buffer position so that edits further
590        ;; down in the buffer occur first w/o corrupting specified
591        ;; buffer positions of edits occurring towards the beginning of
592        ;; the buffer.  In this way we avoid using markers.  A pleasant
593        ;; property of this approach is ability to push instructions
594        ;; onto `path' directly, w/o need to maintain rev boundaries.
595        (dolist (insn (cdr (assq :insn meta)))
596          (goto-line (pop insn))
597          (setq p (point))
598          (case (pop insn)
599            (k (setq s (buffer-substring-no-properties
600                        p (progn (forward-line (car insn))
601                                 (point))))
602               (when prda
603                 (push `(,p . ,(propertize s :vc-rcs-r/d/a prda)) path))
604               (delete-region p (point)))
605            (i (setq s (car insn))
606               (when prda
607                 (push `(,p . ,(length s)) path))
608               (insert s)))))
609      ;; For the initial revision, setting `:vc-rcs-r/d/a' directly is
610      ;; equivalent to pushing an insert instruction (of the entire buffer
611      ;; contents) onto `path' then erasing the buffer, but less wasteful.
612      (put-text-property (point-min) (point-max) :vc-rcs-r/d/a (r/d/a))
613      ;; Now apply the forward-chronological edits for the trunk.
614      (dolist (insn path)
615        (goto-char (pop insn))
616        (if (stringp insn)
617            (insert insn)
618          (delete-char insn)))
619      ;; Now apply the forward-chronological edits (directly from the
620      ;; parse-tree) for the branch(es), if necessary.  We re-use vars
621      ;; `pre' and `meta' for the sake of internal func `r/d/a'.
622      (while nbls
623        (setq pre (cdr (pop nbls)))
624        (while (progn
625                 (setq meta (cdr (assoc pre revisions))
626                       prda nil)
627                 (dolist (insn (cdr (assq :insn meta)))
628                   (goto-line (pop insn))
629                   (case (pop insn)
630                     (k (delete-region
631                         (point) (progn (forward-line (car insn))
632                                        (point))))
633                     (i (insert (propertize
634                                 (car insn)
635                                 :vc-rcs-r/d/a
636                                 (or prda (setq prda (r/d/a))))))))
637                 (prog1 (not (string= (if nbls (caar nbls) revision) pre))
638                   (setq pre (cdr (assq 'next meta)))))))))
639  ;; Lastly, for each line, insert at bol nicely-formatted history info.
640  ;; We do two passes to collect summary information used to minimize
641  ;; the annotation's usage of screen real-estate: (1) Consider rendered
642  ;; width of revision plus author together as a unit; and (2) Omit
643  ;; author entirely if all authors are the same as the user.
644  (let ((ht (make-hash-table :test 'eq))
645        (me (user-login-name))
646        (maxw 0)
647        (all-me t)
648        rda w a)
649    (goto-char (point-max))
650    (while (not (bobp))
651      (forward-line -1)
652      (setq rda (get-text-property (point) :vc-rcs-r/d/a))
653      (unless (gethash rda ht)
654        (setq a (aref rda 2)
655              all-me (and all-me (string= a me)))
656        (puthash rda (setq w (+ (length (aref rda 0))
657                                (length a)))
658                 ht)
659        (setq maxw (max w maxw))))
660    (let ((padding (make-string maxw 32)))
661      (flet ((pad (w) (substring-no-properties padding w))
662             (render (rda &rest ls)
663                     (propertize
664                      (apply 'concat
665                             (format-time-string "%Y-%m-%d" (aref rda 1))
666                             "  "
667                             (aref rda 0)
668                             ls)
669                      :vc-annotate-prefix t
670                      :vc-rcs-r/d/a rda)))
671        (maphash
672         (if all-me
673             (lambda (rda w)
674               (puthash rda (render rda (pad w) ": ") ht))
675           (lambda (rda w)
676             (puthash rda (render rda " " (pad w) " " (aref rda 2) ": ") ht)))
677         ht)))
678    (while (not (eobp))
679      (insert (gethash (get-text-property (point) :vc-rcs-r/d/a) ht))
680      (forward-line 1))))
681
682(defun vc-rcs-annotate-current-time ()
683  "Return the current time, based at midnight of the current day, and
684encoded as fractional days."
685  (vc-annotate-convert-time
686   (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
687
688(defun vc-rcs-annotate-time ()
689  "Return the time of the next annotation (as fraction of days)
690systime, or nil if there is none.  Also, reposition point."
691  (unless (eobp)
692    (prog1 (vc-annotate-convert-time
693            (aref (get-text-property (point) :vc-rcs-r/d/a) 1))
694      (goto-char (next-single-property-change (point) :vc-annotate-prefix)))))
695
696(defun vc-rcs-annotate-extract-revision-at-line ()
697  (aref (get-text-property (point) :vc-rcs-r/d/a) 0))
698
699
700;;;
701;;; Snapshot system
702;;;
703
704(defun vc-rcs-assign-name (file name)
705  "Assign to FILE's latest version a given NAME."
706  (vc-do-command nil 0 "rcs" (vc-name file) (concat "-n" name ":")))
707
708
709;;;
710;;; Miscellaneous
711;;;
712
713(defun vc-rcs-check-headers ()
714  "Check if the current file has any headers in it."
715  (save-excursion
716    (goto-char (point-min))
717         (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
718\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
719
720(defun vc-rcs-clear-headers ()
721  "Implementation of vc-clear-headers for RCS."
722  (let ((case-fold-search nil))
723    (goto-char (point-min))
724    (while (re-search-forward
725            (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
726                    "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
727            nil t)
728      (replace-match "$\\1$"))))
729
730(defun vc-rcs-rename-file (old new)
731  ;; Just move the master file (using vc-rcs-master-templates).
732  (vc-rename-master (vc-name old) new vc-rcs-master-templates))
733
734
735;;;
736;;; Internal functions
737;;;
738
739(defun vc-rcs-workfile-is-newer (file)
740  "Return non-nil if FILE is newer than its RCS master.
741This likely means that FILE has been changed with respect
742to its master version."
743  (let ((file-time (nth 5 (file-attributes file)))
744	(master-time (nth 5 (file-attributes (vc-name file)))))
745    (or (> (nth 0 file-time) (nth 0 master-time))
746	(and (= (nth 0 file-time) (nth 0 master-time))
747	     (> (nth 1 file-time) (nth 1 master-time))))))
748
749(defun vc-rcs-find-most-recent-rev (branch)
750  "Find most recent revision on BRANCH."
751  (goto-char (point-min))
752  (let ((latest-rev -1) value)
753    (while (re-search-forward (concat "^\\(" (regexp-quote branch)
754				      "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
755			      nil t)
756      (let ((rev (string-to-number (match-string 2))))
757	(when (< latest-rev rev)
758	  (setq latest-rev rev)
759	  (setq value (match-string 1)))))
760    (or value
761	(vc-branch-part branch))))
762
763(defun vc-rcs-fetch-master-state (file &optional workfile-version)
764  "Compute the master file's idea of the state of FILE.
765If a WORKFILE-VERSION is given, compute the state of that version,
766otherwise determine the workfile version based on the master file.
767This function sets the properties `vc-workfile-version' and
768`vc-checkout-model' to their correct values, based on the master
769file."
770  (with-temp-buffer
771    (if (or (not (vc-insert-file (vc-name file) "^[0-9]"))
772            (progn (goto-char (point-min))
773                   (not (looking-at "^head[ \t\n]+[^;]+;$"))))
774        (error "File %s is not an RCS master file" (vc-name file)))
775    (let ((workfile-is-latest nil)
776	  (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
777      (vc-file-setprop file 'vc-rcs-default-branch default-branch)
778      (unless workfile-version
779	;; Workfile version not known yet.  Determine that first.  It
780	;; is either the head of the trunk, the head of the default
781	;; branch, or the "default branch" itself, if that is a full
782	;; revision number.
783	(cond
784	 ;; no default branch
785	 ((or (not default-branch) (string= "" default-branch))
786	  (setq workfile-version
787		(vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
788	  (setq workfile-is-latest t))
789	 ;; default branch is actually a revision
790	 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
791			default-branch)
792	  (setq workfile-version default-branch))
793	 ;; else, search for the head of the default branch
794	 (t (vc-insert-file (vc-name file) "^desc")
795	    (setq workfile-version
796		  (vc-rcs-find-most-recent-rev default-branch))
797	    (setq workfile-is-latest t)))
798	(vc-file-setprop file 'vc-workfile-version workfile-version))
799      ;; Check strict locking
800      (goto-char (point-min))
801      (vc-file-setprop file 'vc-checkout-model
802		       (if (re-search-forward ";[ \t\n]*strict;" nil t)
803			   'locking 'implicit))
804      ;; Compute state of workfile version
805      (goto-char (point-min))
806      (let ((locking-user
807	     (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
808				      (regexp-quote workfile-version)
809				      "[^0-9.]")
810			      1)))
811	(cond
812	 ;; not locked
813	 ((not locking-user)
814          (if (or workfile-is-latest
815                  (vc-rcs-latest-on-branch-p file workfile-version))
816              ;; workfile version is latest on branch
817              'up-to-date
818            ;; workfile version is not latest on branch
819            'needs-patch))
820	 ;; locked by the calling user
821	 ((and (stringp locking-user)
822	       (string= locking-user (vc-user-login-name file)))
823	  (if (or (eq (vc-checkout-model file) 'locking)
824		  workfile-is-latest
825		  (vc-rcs-latest-on-branch-p file workfile-version))
826	      'edited
827	    ;; Locking is not used for the file, but the owner does
828	    ;; have a lock, and there is a higher version on the current
829	    ;; branch.  Not sure if this can occur, and if it is right
830	    ;; to use `needs-merge' in this case.
831	    'needs-merge))
832	 ;; locked by somebody else
833	 ((stringp locking-user)
834	  locking-user)
835	 (t
836	  (error "Error getting state of RCS file")))))))
837
838(defun vc-rcs-consult-headers (file)
839  "Search for RCS headers in FILE, and set properties accordingly.
840
841Returns: nil            if no headers were found
842         'rev           if a workfile revision was found
843         'rev-and-lock  if revision and lock info was found"
844  (cond
845   ((not (get-file-buffer file)) nil)
846   ((let (status version locking-user)
847     (save-excursion
848      (set-buffer (get-file-buffer file))
849      (goto-char (point-min))
850      (cond
851       ;; search for $Id or $Header
852       ;; -------------------------
853       ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
854       ((or (and (search-forward "$Id\ : " nil t)
855		 (looking-at "[^ ]+ \\([0-9.]+\\) "))
856	    (and (progn (goto-char (point-min))
857			(search-forward "$Header\ : " nil t))
858		 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
859	(goto-char (match-end 0))
860	;; if found, store the revision number ...
861	(setq version (match-string-no-properties 1))
862	;; ... and check for the locking state
863	(cond
864	 ((looking-at
865	   (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] "             ; date
866	    "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
867	           "[^ ]+ [^ ]+ "))                       ; author & state
868	  (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
869	  (cond
870	   ;; unlocked revision
871	   ((looking-at "\\$")
872	    (setq locking-user 'none)
873	    (setq status 'rev-and-lock))
874	   ;; revision is locked by some user
875	   ((looking-at "\\([^ ]+\\) \\$")
876	    (setq locking-user (match-string-no-properties 1))
877	    (setq status 'rev-and-lock))
878	   ;; everything else: false
879	   (nil)))
880	 ;; unexpected information in
881	 ;; keyword string --> quit
882	 (nil)))
883       ;; search for $Revision
884       ;; --------------------
885       ((re-search-forward (concat "\\$"
886				   "Revision: \\([0-9.]+\\) \\$")
887			   nil t)
888	;; if found, store the revision number ...
889	(setq version (match-string-no-properties 1))
890	;; and see if there's any lock information
891	(goto-char (point-min))
892	(if (re-search-forward (concat "\\$" "Locker:") nil t)
893	    (cond ((looking-at " \\([^ ]+\\) \\$")
894		   (setq locking-user (match-string-no-properties 1))
895		   (setq status 'rev-and-lock))
896		  ((looking-at " *\\$")
897		   (setq locking-user 'none)
898		   (setq status 'rev-and-lock))
899		  (t
900		   (setq locking-user 'none)
901		   (setq status 'rev-and-lock)))
902	  (setq status 'rev)))
903       ;; else: nothing found
904       ;; -------------------
905       (t nil)))
906     (if status (vc-file-setprop file 'vc-workfile-version version))
907     (and (eq status 'rev-and-lock)
908	  (vc-file-setprop file 'vc-state
909			   (cond
910			    ((eq locking-user 'none) 'up-to-date)
911			    ((string= locking-user (vc-user-login-name file))
912                             'edited)
913			    (t locking-user)))
914	  ;; If the file has headers, we don't want to query the
915	  ;; master file, because that would eliminate all the
916	  ;; performance gain the headers brought us.  We therefore
917	  ;; use a heuristic now to find out whether locking is used
918	  ;; for this file.  If we trust the file permissions, and the
919	  ;; file is not locked, then if the file is read-only we
920          ;; assume that locking is used for the file, otherwise
921          ;; locking is not used.
922	  (not (vc-mistrust-permissions file))
923	  (vc-up-to-date-p file)
924	  (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
925	      (vc-file-setprop file 'vc-checkout-model 'locking)
926	    (vc-file-setprop file 'vc-checkout-model 'implicit)))
927     status))))
928
929(defun vc-release-greater-or-equal (r1 r2)
930  "Compare release numbers, represented as strings.
931Release components are assumed cardinal numbers, not decimal fractions
932\(5.10 is a higher release than 5.9\).  Omitted fields are considered
933lower \(5.6.7 is earlier than 5.6.7.1\).  Comparison runs till the end
934of the string is found, or a non-numeric component shows up \(5.6.7 is
935earlier than \"5.6.7 beta\", which is probably not what you want in
936some cases\).  This code is suitable for existing RCS release numbers.
937CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
938  (let (v1 v2 i1 i2)
939    (catch 'done
940      (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
941	       (setq i1 (match-end 0))
942	       (setq v1 (string-to-number (match-string 1 r1)))
943	       (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
944			(setq i2 (match-end 0))
945			(setq v2 (string-to-number (match-string 1 r2)))
946			(if (> v1 v2) (throw 'done t)
947			  (if (< v1 v2) (throw 'done nil)
948			    (throw 'done
949				   (vc-release-greater-or-equal
950				    (substring r1 i1)
951				    (substring r2 i2)))))))
952		   (throw 'done t)))
953	  (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
954		   (throw 'done nil))
955	      (throw 'done t)))))
956
957(defun vc-rcs-release-p (release)
958  "Return t if we have RELEASE or better."
959  (let ((installation (vc-rcs-system-release)))
960    (if (and installation
961	     (not (eq installation 'unknown)))
962	(vc-release-greater-or-equal installation release))))
963
964(defun vc-rcs-system-release ()
965  "Return the RCS release installed on this system, as a string.
966Return symbol UNKNOWN if the release cannot be deducted.  The user can
967override this using variable `vc-rcs-release'.
968
969If the user has not set variable `vc-rcs-release' and it is nil,
970variable `vc-rcs-release' is set to the returned value."
971  (or vc-rcs-release
972      (setq vc-rcs-release
973	    (or (and (zerop (vc-do-command nil nil "rcs" nil "-V"))
974		     (with-current-buffer (get-buffer "*vc*")
975		       (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
976		'unknown))))
977
978(defun vc-rcs-set-non-strict-locking (file)
979  (vc-do-command nil 0 "rcs" file "-U")
980  (vc-file-setprop file 'vc-checkout-model 'implicit)
981  (set-file-modes file (logior (file-modes file) 128)))
982
983(defun vc-rcs-set-default-branch (file branch)
984  (vc-do-command nil 0 "rcs" (vc-name file) (concat "-b" branch))
985  (vc-file-setprop file 'vc-rcs-default-branch branch))
986
987(defun vc-rcs-parse (&optional buffer)
988  "Parse current buffer, presumed to be in RCS-style masterfile format.
989Optional arg BUFFER specifies another buffer to parse.  Return an alist
990of two elements, w/ keys `headers' and `revisions' and values in turn
991sub-alists.  For `headers', the values unless otherwise specified are
992strings and the keys are:
993
994  desc     -- description
995  head     -- latest revision
996  branch   -- the branch the \"head revision\" lies on;
997              absent if the head revision lies on the trunk
998  access   -- ???
999  symbols  -- sub-alist of (SYMBOL . REVISION) elements
1000  locks    -- if file is checked out, something like \"ttn:1.7\"
1001  strict   -- t if \"strict locking\" is in effect, otherwise nil
1002  comment  -- may be absent; typically something like \"# \" or \"; \"
1003  expand   -- may be absent; ???
1004
1005For `revisions', the car is REVISION (string), the cdr a sub-alist,
1006with string values (unless otherwise specified) and keys:
1007
1008  date     -- a time value (like that returned by `encode-time'); as a
1009              special case, a year value less than 100 is augmented by 1900
1010  author   -- username
1011  state    -- typically \"Exp\" or \"Rel\"
1012  branches -- list of revisions that begin branches from this revision
1013  next     -- on the trunk: the chronologically-preceding revision, or \"\";
1014              on a branch: the chronologically-following revision, or \"\"
1015  log      -- change log entry
1016  text     -- for the head revision on the trunk, the body of the file;
1017              other revisions have `:insn' instead
1018  :insn    -- for non-head revisions, a list of parsed instructions
1019              in one of two forms, in both cases START meaning \"first
1020              go to line START\":
1021               - `(START k COUNT)' -- kill COUNT lines
1022               - `(START i TEXT)'  -- insert TEXT (a string)
1023              The list is in descending order by START.
1024
1025The `:insn' key is a keyword to distinguish it as a vc-rcs.el extension."
1026  (setq buffer (get-buffer (or buffer (current-buffer))))
1027  (set-buffer buffer)
1028  ;; An RCS masterfile can be viewed as containing four regular (for the
1029  ;; most part) sections: (a) the "headers", (b) the "rev headers", (c)
1030  ;; the "description" and (d) the "rev bodies", in that order.  In the
1031  ;; returned alist (see docstring), elements from (b) and (d) are
1032  ;; combined pairwise to form the "revisions", while those from (a) and
1033  ;; (c) are simply combined to form the "headers".
1034  ;;
1035  ;; Loosely speaking, each section contains a series of alternating
1036  ;; "tags" and "printed representations".  In the (b) and (d), many
1037  ;; such series can appear, and a revision number on a line by itself
1038  ;; precedes the series of tags and printed representations associated
1039  ;; with it.
1040  ;;
1041  ;; In (a) and (b), the printed representations (with the exception of
1042  ;; the `comment' tag in the headers) terminate with a semicolon, which
1043  ;; is NOT part of the "value" finally associated with the tag.  All
1044  ;; other printed representations are in "@@-format"; there is an "@",
1045  ;; the middle part (to be translated into the value), another "@" and
1046  ;; a newline.  Each "@@" in the middle part indicates the position of
1047  ;; a single "@" (and consequently the requirement of an additional
1048  ;; initial step when translating to the value).
1049  ;;
1050  ;; Parser state includes vars that collect parts of the return value...
1051  (let ((desc nil) (headers nil) (revs nil)
1052        ;; ... as well as vars that support a single-pass, tag-assisted,
1053        ;; minimal-data-copying scan.  Basically -- skirting around the
1054        ;; grouping by revision required in (b) and (d) -- we repeatedly
1055        ;; and context-sensitively read a tag (that MUST be present),
1056        ;; determine the bounds of the printed representation, translate
1057        ;; it into a value, and push the tag plus value onto one of the
1058        ;; collection vars.  Finally, we return the parse tree
1059        ;; incorporating the values of the collection vars (see "rv").
1060        ;;
1061        ;; A symbol or string to keep track of context (for error messages).
1062        context
1063        ;; A symbol, the current tag.
1064        tok
1065        ;; Region (begin and end buffer positions) of the printed
1066        ;; representation for the current tag.
1067        b e
1068        ;; A list of buffer positions where "@@" can be found within the
1069        ;; printed representation region.  For each location, we push two
1070        ;; elements onto the list, 1+ and 2+ the location, respectively,
1071        ;; with the 2+ appearing at the head.  In this way, the expression
1072        ;;   `(,e ,@@-holes ,b)
1073        ;; describes regions that can be concatenated (in reverse order)
1074        ;; to "de-@@-format" the printed representation as the first step
1075        ;; to translating it into some value.  See internal func `gather'.
1076        @-holes)
1077    (flet ((sw () (skip-chars-forward " \t\n")) ; i.e., `[:space:]'
1078           (at (tag) (save-excursion (eq tag (read buffer))))
1079           (to-eol () (buffer-substring-no-properties
1080                       (point) (progn (forward-line 1)
1081                                      (1- (point)))))
1082           (to-semi () (setq b (point)
1083                             e (progn (search-forward ";")
1084                                      (1- (point)))))
1085           (to-one@ () (setq @-holes nil
1086                             b (progn (search-forward "@") (point))
1087                             e (progn (while (and (search-forward "@")
1088                                                  (= ?@ (char-after))
1089                                                  (progn
1090                                                    (push (point) @-holes)
1091                                                    (forward-char 1)
1092                                                    (push (point) @-holes))))
1093                                      (1- (point)))))
1094           (tok+val (set-b+e name &optional proc)
1095                    (unless (eq name (setq tok (read buffer)))
1096                      (error "Missing `%s' while parsing %s" name context))
1097                    (sw)
1098                    (funcall set-b+e)
1099                    (cons tok (if proc
1100                                  (funcall proc)
1101                                (buffer-substring-no-properties b e))))
1102           (k-semi (name &optional proc) (tok+val 'to-semi name proc))
1103           (gather () (let ((pairs `(,e ,@@-holes ,b))
1104                            acc)
1105                        (while pairs
1106                          (push (buffer-substring-no-properties
1107                                 (cadr pairs) (car pairs))
1108                                acc)
1109                          (setq pairs (cddr pairs)))
1110                        (apply 'concat acc)))
1111           (k-one@ (name &optional later) (tok+val 'to-one@ name
1112                                                   (if later
1113                                                       (lambda () t)
1114                                                     'gather))))
1115      (save-excursion
1116        (goto-char (point-min))
1117        ;; headers
1118        (setq context 'headers)
1119        (flet ((hpush (name &optional proc)
1120                      (push (k-semi name proc) headers)))
1121          (hpush 'head)
1122          (when (at 'branch)
1123            (hpush 'branch))
1124          (hpush 'access)
1125          (hpush 'symbols
1126                 (lambda ()
1127                   (mapcar (lambda (together)
1128                             (let ((two (split-string together ":")))
1129                               (setcar two (intern (car two)))
1130                               (setcdr two (cadr two))
1131                               two))
1132                           (split-string
1133                            (buffer-substring-no-properties b e)))))
1134          (hpush 'locks))
1135        (push `(strict . ,(when (at 'strict)
1136                            (search-forward ";")
1137                            t))
1138              headers)
1139        (when (at 'comment)
1140          (push (k-one@ 'comment) headers)
1141          (search-forward ";"))
1142        (when (at 'expand)
1143          (push (k-one@ 'expand) headers)
1144          (search-forward ";"))
1145        (setq headers (nreverse headers))
1146        ;; rev headers
1147        (sw) (setq context 'rev-headers)
1148        (while (looking-at "[0-9]")
1149          (push `(,(to-eol)
1150                  ,(k-semi 'date
1151                           (lambda ()
1152                             (let ((ls (mapcar 'string-to-number
1153                                               (split-string
1154                                                (buffer-substring-no-properties
1155                                                 b e)
1156                                                "\\."))))
1157                               ;; Hack the year -- verified to be the
1158                               ;; same algorithm used in RCS 5.7.
1159                               (when (< (car ls) 100)
1160                                 (setcar ls (+ 1900 (car ls))))
1161                               (apply 'encode-time (nreverse ls)))))
1162                  ,@(mapcar 'k-semi '(author state))
1163                  ,(k-semi 'branches
1164                           (lambda ()
1165                             (split-string
1166                              (buffer-substring-no-properties b e))))
1167                  ,(k-semi 'next))
1168                revs)
1169          (sw))
1170        (setq revs (nreverse revs))
1171        ;; desc
1172        (sw) (setq context 'desc
1173                   desc (k-one@ 'desc))
1174        ;; rev bodies
1175        (let (acc
1176              ;; Element of `revs' that initially holds only header info.
1177              ;; "Pairwise combination" occurs when we add body info.
1178              rev
1179              ;; Components of the editing commands (aside from the actual
1180              ;; text) that comprise the `text' printed representations
1181              ;; (not including the "head" revision).
1182              cmd start act
1183              ;; Ascending (reversed) `@-holes' which the internal func
1184              ;; `incg' pops to effect incremental gathering.
1185              asc
1186              ;; Function to extract text (for the `a' command), either
1187              ;; `incg' or `buffer-substring-no-properties'.  (This is
1188              ;; for speed; strictly speaking, it is sufficient to use
1189              ;; only the former since it behaves identically to the
1190              ;; latter in the absense of "@@".)
1191              sub)
1192          (flet ((incg (beg end) (let ((b beg) (e end) @-holes)
1193                                   (while (and asc (< (car asc) e))
1194                                     (push (pop asc) @-holes))
1195                                   ;; Self-deprecate when work is done.
1196                                   ;; Folding many dimensions into one.
1197                                   ;; Thanks B.Mandelbrot, for complex sum.
1198                                   ;; O beauteous math! --the Unvexed Bum
1199                                   (unless asc
1200                                     (setq sub 'buffer-substring-no-properties))
1201                                   (gather))))
1202            (while (and (sw)
1203                        (not (eobp))
1204                        (setq context (to-eol)
1205                              rev (or (assoc context revs)
1206                                      (error "Rev `%s' has body but no head"
1207                                             context))))
1208              (push (k-one@ 'log) (cdr rev))
1209              ;; For rev body `text' tags, delay translation slightly...
1210              (push (k-one@ 'text t) (cdr rev))
1211              ;; ... until we decide which tag and value is appropriate to
1212              ;; collect.  For the "head" revision, compute the value of the
1213              ;; `text' printed representation by simple `gather'.  For all
1214              ;; other revisions, replace the `text' tag+value with `:insn'
1215              ;; plus value, always scanning in-place.
1216              (if (string= context (cdr (assq 'head headers)))
1217                  (setcdr (cadr rev) (gather))
1218                (if @-holes
1219                    (setq asc (nreverse @-holes)
1220                          sub 'incg)
1221                  (setq sub 'buffer-substring-no-properties))
1222                (goto-char b)
1223                (setq acc nil)
1224                (while (< (point) e)
1225                  (forward-char 1)
1226                  (setq cmd (char-before)
1227                        start (read (current-buffer))
1228                        act (read (current-buffer)))
1229                  (forward-char 1)
1230                  (push (case cmd
1231                          (?d
1232                           ;; `d' means "delete lines".
1233                           ;; For Emacs spirit, we use `k' for "kill".
1234                           `(,start k ,act))
1235                          (?a
1236                           ;; `a' means "append after this line" but
1237                           ;; internally we normalize it so that START
1238                           ;; specifies the actual line for insert, thus
1239                           ;; requiring less hair in the realization algs.
1240                           ;; For Emacs spirit, we use `i' for "insert".
1241                           `(,(1+ start) i
1242                             ,(funcall sub (point) (progn (forward-line act)
1243                                                          (point)))))
1244                          (t (error "Bad command `%c' in `text' for rev `%s'"
1245                                    cmd context)))
1246                        acc))
1247                (goto-char (1+ e))
1248                (setcar (cdr rev) (cons :insn acc)))))))
1249      ;; rv
1250      `((headers ,desc ,@headers)
1251        (revisions ,@revs)))))
1252
1253(provide 'vc-rcs)
1254
1255;; arch-tag: 759b4916-5b0d-431d-b647-b185b8c652cf
1256;;; vc-rcs.el ends here
1257