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