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