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