* vc-hooks.el (vc-stay-local-p, vc-state, vc-working-revision):
[emacs.git] / lisp / vc-rcs.el
blob0a5ebe42eecd2e6cd97cfd220451f3c72d5beb8a
1 ;;; vc-rcs.el --- support for RCS version-control
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see vc.el for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;; See vc.el
29 ;;; Code:
31 ;;;
32 ;;; Customization options
33 ;;;
35 (eval-when-compile
36 (require 'cl)
37 (require 'vc))
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)
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)
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)
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)
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)
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)
96 ;;; Properties of the backend
98 (defun vc-rcs-revision-granularity () 'file)
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)))))
113 ;;; State-querying functions
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)))
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))))))
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)))
185 ;; Strange permissions. Fall through to
186 ;; expensive state computation.
187 (vc-rcs-state file))))
188 (vc-rcs-state file)))))
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.
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)))
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))))
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))))))
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)))
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)))))))
260 ;;; State-changing functions
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"))
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.
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))))))
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))))
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"))))
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))))
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)
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))
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)))))))))
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)))
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))))))
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)))))))))
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)))))
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))))
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))))
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))))
546 ;;; History functions
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))))
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))))
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)))
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.
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.
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.
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)))
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))
755 (aref rda 0)
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))))
770 (declare-function vc-annotate-convert-time "vc-annotate" (time))
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))))))
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)))))
786 (defun vc-rcs-annotate-extract-revision-at-line ()
787 (aref (get-text-property (point) :vc-rcs-r/d/a) 0))
791 ;;; Tag system
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
802 (lambda (f)
803 (vc-do-command "*vc*" 0 "rcs" (vc-name f) (concat "-n" name ":")))))))
807 ;;; Miscellaneous
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)))
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$"))))
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))
831 (defun vc-rcs-find-file-hook ()
832 ;; If the file is locked by some other user, make
833 ;; the buffer read-only. Like this, even root
834 ;; cannot modify a file that someone else has locked.
835 (stringp (vc-state buffer-file-name 'RCS))
836 (setq buffer-read-only t))
840 ;;; Internal functions
843 (defun vc-rcs-workfile-is-newer (file)
844 "Return non-nil if FILE is newer than its RCS master.
845 This likely means that FILE has been changed with respect
846 to its master version."
847 (let ((file-time (nth 5 (file-attributes file)))
848 (master-time (nth 5 (file-attributes (vc-name file)))))
849 (or (> (nth 0 file-time) (nth 0 master-time))
850 (and (= (nth 0 file-time) (nth 0 master-time))
851 (> (nth 1 file-time) (nth 1 master-time))))))
853 (defun vc-rcs-find-most-recent-rev (branch)
854 "Find most recent revision on BRANCH."
855 (goto-char (point-min))
856 (let ((latest-rev -1) value)
857 (while (re-search-forward (concat "^\\(" (regexp-quote branch)
858 "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
859 nil t)
860 (let ((rev (string-to-number (match-string 2))))
861 (when (< latest-rev rev)
862 (setq latest-rev rev)
863 (setq value (match-string 1)))))
864 (or value
865 (vc-branch-part branch))))
867 (defun vc-rcs-fetch-master-state (file &optional working-revision)
868 "Compute the master file's idea of the state of FILE.
869 If a WORKING-REVISION is given, compute the state of that version,
870 otherwise determine the workfile version based on the master file.
871 This function sets the properties `vc-working-revision' and
872 `vc-checkout-model' to their correct values, based on the master
873 file."
874 (with-temp-buffer
875 (if (or (not (vc-insert-file (vc-name file) "^[0-9]"))
876 (progn (goto-char (point-min))
877 (not (looking-at "^head[ \t\n]+[^;]+;$"))))
878 (error "File %s is not an RCS master file" (vc-name file)))
879 (let ((workfile-is-latest nil)
880 (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
881 (vc-file-setprop file 'vc-rcs-default-branch default-branch)
882 (unless working-revision
883 ;; Workfile version not known yet. Determine that first. It
884 ;; is either the head of the trunk, the head of the default
885 ;; branch, or the "default branch" itself, if that is a full
886 ;; revision number.
887 (cond
888 ;; no default branch
889 ((or (not default-branch) (string= "" default-branch))
890 (setq working-revision
891 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
892 (setq workfile-is-latest t))
893 ;; default branch is actually a revision
894 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
895 default-branch)
896 (setq working-revision default-branch))
897 ;; else, search for the head of the default branch
898 (t (vc-insert-file (vc-name file) "^desc")
899 (setq working-revision
900 (vc-rcs-find-most-recent-rev default-branch))
901 (setq workfile-is-latest t)))
902 (vc-file-setprop file 'vc-working-revision working-revision))
903 ;; Check strict locking
904 (goto-char (point-min))
905 (vc-file-setprop file 'vc-checkout-model
906 (if (re-search-forward ";[ \t\n]*strict;" nil t)
907 'locking 'implicit))
908 ;; Compute state of workfile version
909 (goto-char (point-min))
910 (let ((locking-user
911 (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
912 (regexp-quote working-revision)
913 "[^0-9.]")
914 1)))
915 (cond
916 ;; not locked
917 ((not locking-user)
918 (if (or workfile-is-latest
919 (vc-rcs-latest-on-branch-p file working-revision))
920 ;; workfile version is latest on branch
921 'up-to-date
922 ;; workfile version is not latest on branch
923 'needs-update))
924 ;; locked by the calling user
925 ((and (stringp locking-user)
926 (string= locking-user (vc-user-login-name file)))
927 ;; Don't call `vc-rcs-checkout-model' to avoid inf-looping.
928 (if (or (eq (vc-file-getprop file 'vc-checkout-model) 'locking)
929 workfile-is-latest
930 (vc-rcs-latest-on-branch-p file working-revision))
931 'edited
932 ;; Locking is not used for the file, but the owner does
933 ;; have a lock, and there is a higher version on the current
934 ;; branch. Not sure if this can occur, and if it is right
935 ;; to use `needs-merge' in this case.
936 'needs-merge))
937 ;; locked by somebody else
938 ((stringp locking-user)
939 locking-user)
941 (error "Error getting state of RCS file")))))))
943 (defun vc-rcs-consult-headers (file)
944 "Search for RCS headers in FILE, and set properties accordingly.
946 Returns: nil if no headers were found
947 'rev if a workfile revision was found
948 'rev-and-lock if revision and lock info was found"
949 (cond
950 ((not (get-file-buffer file)) nil)
951 ((let (status version locking-user)
952 (save-excursion
953 (set-buffer (get-file-buffer file))
954 (goto-char (point-min))
955 (cond
956 ;; search for $Id or $Header
957 ;; -------------------------
958 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
959 ((or (and (search-forward "$Id\ : " nil t)
960 (looking-at "[^ ]+ \\([0-9.]+\\) "))
961 (and (progn (goto-char (point-min))
962 (search-forward "$Header\ : " nil t))
963 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
964 (goto-char (match-end 0))
965 ;; if found, store the revision number ...
966 (setq version (match-string-no-properties 1))
967 ;; ... and check for the locking state
968 (cond
969 ((looking-at
970 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
971 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
972 "[^ ]+ [^ ]+ ")) ; author & state
973 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
974 (cond
975 ;; unlocked revision
976 ((looking-at "\\$")
977 (setq locking-user 'none)
978 (setq status 'rev-and-lock))
979 ;; revision is locked by some user
980 ((looking-at "\\([^ ]+\\) \\$")
981 (setq locking-user (match-string-no-properties 1))
982 (setq status 'rev-and-lock))
983 ;; everything else: false
984 (nil)))
985 ;; unexpected information in
986 ;; keyword string --> quit
987 (nil)))
988 ;; search for $Revision
989 ;; --------------------
990 ((re-search-forward (concat "\\$"
991 "Revision: \\([0-9.]+\\) \\$")
992 nil t)
993 ;; if found, store the revision number ...
994 (setq version (match-string-no-properties 1))
995 ;; and see if there's any lock information
996 (goto-char (point-min))
997 (if (re-search-forward (concat "\\$" "Locker:") nil t)
998 (cond ((looking-at " \\([^ ]+\\) \\$")
999 (setq locking-user (match-string-no-properties 1))
1000 (setq status 'rev-and-lock))
1001 ((looking-at " *\\$")
1002 (setq locking-user 'none)
1003 (setq status 'rev-and-lock))
1005 (setq locking-user 'none)
1006 (setq status 'rev-and-lock)))
1007 (setq status 'rev)))
1008 ;; else: nothing found
1009 ;; -------------------
1010 (t nil)))
1011 (if status (vc-file-setprop file 'vc-working-revision version))
1012 (and (eq status 'rev-and-lock)
1013 (vc-file-setprop file 'vc-state
1014 (cond
1015 ((eq locking-user 'none) 'up-to-date)
1016 ((string= locking-user (vc-user-login-name file))
1017 'edited)
1018 (t locking-user)))
1019 ;; If the file has headers, we don't want to query the
1020 ;; master file, because that would eliminate all the
1021 ;; performance gain the headers brought us. We therefore
1022 ;; use a heuristic now to find out whether locking is used
1023 ;; for this file. If we trust the file permissions, and the
1024 ;; file is not locked, then if the file is read-only we
1025 ;; assume that locking is used for the file, otherwise
1026 ;; locking is not used.
1027 (not (vc-mistrust-permissions file))
1028 (vc-up-to-date-p file)
1029 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
1030 (vc-file-setprop file 'vc-checkout-model 'locking)
1031 (vc-file-setprop file 'vc-checkout-model 'implicit)))
1032 status))))
1034 (defun vc-release-greater-or-equal (r1 r2)
1035 "Compare release numbers, represented as strings.
1036 Release components are assumed cardinal numbers, not decimal fractions
1037 \(5.10 is a higher release than 5.9\). Omitted fields are considered
1038 lower \(5.6.7 is earlier than 5.6.7.1\). Comparison runs till the end
1039 of the string is found, or a non-numeric component shows up \(5.6.7 is
1040 earlier than \"5.6.7 beta\", which is probably not what you want in
1041 some cases\). This code is suitable for existing RCS release numbers.
1042 CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
1043 (let (v1 v2 i1 i2)
1044 (catch 'done
1045 (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
1046 (setq i1 (match-end 0))
1047 (setq v1 (string-to-number (match-string 1 r1)))
1048 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
1049 (setq i2 (match-end 0))
1050 (setq v2 (string-to-number (match-string 1 r2)))
1051 (if (> v1 v2) (throw 'done t)
1052 (if (< v1 v2) (throw 'done nil)
1053 (throw 'done
1054 (vc-release-greater-or-equal
1055 (substring r1 i1)
1056 (substring r2 i2)))))))
1057 (throw 'done t)))
1058 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
1059 (throw 'done nil))
1060 (throw 'done t)))))
1062 (defun vc-rcs-release-p (release)
1063 "Return t if we have RELEASE or better."
1064 (let ((installation (vc-rcs-system-release)))
1065 (if (and installation
1066 (not (eq installation 'unknown)))
1067 (vc-release-greater-or-equal installation release))))
1069 (defun vc-rcs-system-release ()
1070 "Return the RCS release installed on this system, as a string.
1071 Return symbol `unknown' if the release cannot be deducted. The user can
1072 override this using variable `vc-rcs-release'.
1074 If the user has not set variable `vc-rcs-release' and it is nil,
1075 variable `vc-rcs-release' is set to the returned value."
1076 (or vc-rcs-release
1077 (setq vc-rcs-release
1078 (or (and (zerop (vc-do-command "*vc*" nil "rcs" nil "-V"))
1079 (with-current-buffer (get-buffer "*vc*")
1080 (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
1081 'unknown))))
1083 (defun vc-rcs-set-non-strict-locking (file)
1084 (vc-do-command "*vc*" 0 "rcs" file "-U")
1085 (vc-file-setprop file 'vc-checkout-model 'implicit)
1086 (set-file-modes file (logior (file-modes file) 128)))
1088 (defun vc-rcs-set-default-branch (file branch)
1089 (vc-do-command "*vc*" 0 "rcs" (vc-name file) (concat "-b" branch))
1090 (vc-file-setprop file 'vc-rcs-default-branch branch))
1092 (defun vc-rcs-parse (&optional buffer)
1093 "Parse current buffer, presumed to be in RCS-style masterfile format.
1094 Optional arg BUFFER specifies another buffer to parse. Return an alist
1095 of two elements, w/ keys `headers' and `revisions' and values in turn
1096 sub-alists. For `headers', the values unless otherwise specified are
1097 strings and the keys are:
1099 desc -- description
1100 head -- latest revision
1101 branch -- the branch the \"head revision\" lies on;
1102 absent if the head revision lies on the trunk
1103 access -- ???
1104 symbols -- sub-alist of (SYMBOL . REVISION) elements
1105 locks -- if file is checked out, something like \"ttn:1.7\"
1106 strict -- t if \"strict locking\" is in effect, otherwise nil
1107 comment -- may be absent; typically something like \"# \" or \"; \"
1108 expand -- may be absent; ???
1110 For `revisions', the car is REVISION (string), the cdr a sub-alist,
1111 with string values (unless otherwise specified) and keys:
1113 date -- a time value (like that returned by `encode-time'); as a
1114 special case, a year value less than 100 is augmented by 1900
1115 author -- username
1116 state -- typically \"Exp\" or \"Rel\"
1117 branches -- list of revisions that begin branches from this revision
1118 next -- on the trunk: the chronologically-preceding revision, or \"\";
1119 on a branch: the chronologically-following revision, or \"\"
1120 log -- change log entry
1121 text -- for the head revision on the trunk, the body of the file;
1122 other revisions have `:insn' instead
1123 :insn -- for non-head revisions, a list of parsed instructions
1124 in one of two forms, in both cases START meaning \"first
1125 go to line START\":
1126 - `(START k COUNT)' -- kill COUNT lines
1127 - `(START i TEXT)' -- insert TEXT (a string)
1128 The list is in descending order by START.
1130 The `:insn' key is a keyword to distinguish it as a vc-rcs.el extension."
1131 (setq buffer (get-buffer (or buffer (current-buffer))))
1132 (set-buffer buffer)
1133 ;; An RCS masterfile can be viewed as containing four regular (for the
1134 ;; most part) sections: (a) the "headers", (b) the "rev headers", (c)
1135 ;; the "description" and (d) the "rev bodies", in that order. In the
1136 ;; returned alist (see docstring), elements from (b) and (d) are
1137 ;; combined pairwise to form the "revisions", while those from (a) and
1138 ;; (c) are simply combined to form the "headers".
1140 ;; Loosely speaking, each section contains a series of alternating
1141 ;; "tags" and "printed representations". In the (b) and (d), many
1142 ;; such series can appear, and a revision number on a line by itself
1143 ;; precedes the series of tags and printed representations associated
1144 ;; with it.
1146 ;; In (a) and (b), the printed representations (with the exception of
1147 ;; the `comment' tag in the headers) terminate with a semicolon, which
1148 ;; is NOT part of the "value" finally associated with the tag. All
1149 ;; other printed representations are in "@@-format"; there is an "@",
1150 ;; the middle part (to be translated into the value), another "@" and
1151 ;; a newline. Each "@@" in the middle part indicates the position of
1152 ;; a single "@" (and consequently the requirement of an additional
1153 ;; initial step when translating to the value).
1155 ;; Parser state includes vars that collect parts of the return value...
1156 (let ((desc nil) (headers nil) (revs nil)
1157 ;; ... as well as vars that support a single-pass, tag-assisted,
1158 ;; minimal-data-copying scan. Basically -- skirting around the
1159 ;; grouping by revision required in (b) and (d) -- we repeatedly
1160 ;; and context-sensitively read a tag (that MUST be present),
1161 ;; determine the bounds of the printed representation, translate
1162 ;; it into a value, and push the tag plus value onto one of the
1163 ;; collection vars. Finally, we return the parse tree
1164 ;; incorporating the values of the collection vars (see "rv").
1166 ;; A symbol or string to keep track of context (for error messages).
1167 context
1168 ;; A symbol, the current tag.
1170 ;; Region (begin and end buffer positions) of the printed
1171 ;; representation for the current tag.
1173 ;; A list of buffer positions where "@@" can be found within the
1174 ;; printed representation region. For each location, we push two
1175 ;; elements onto the list, 1+ and 2+ the location, respectively,
1176 ;; with the 2+ appearing at the head. In this way, the expression
1177 ;; `(,e ,@@-holes ,b)
1178 ;; describes regions that can be concatenated (in reverse order)
1179 ;; to "de-@@-format" the printed representation as the first step
1180 ;; to translating it into some value. See internal func `gather'.
1181 @-holes)
1182 (flet ((sw () (skip-chars-forward " \t\n")) ; i.e., `[:space:]'
1183 (at (tag) (save-excursion (eq tag (read buffer))))
1184 (to-eol () (buffer-substring-no-properties
1185 (point) (progn (forward-line 1)
1186 (1- (point)))))
1187 (to-semi () (setq b (point)
1188 e (progn (search-forward ";")
1189 (1- (point)))))
1190 (to-one@ () (setq @-holes nil
1191 b (progn (search-forward "@") (point))
1192 e (progn (while (and (search-forward "@")
1193 (= ?@ (char-after))
1194 (progn
1195 (push (point) @-holes)
1196 (forward-char 1)
1197 (push (point) @-holes))))
1198 (1- (point)))))
1199 (tok+val (set-b+e name &optional proc)
1200 (unless (eq name (setq tok (read buffer)))
1201 (error "Missing `%s' while parsing %s" name context))
1202 (sw)
1203 (funcall set-b+e)
1204 (cons tok (if proc
1205 (funcall proc)
1206 (buffer-substring-no-properties b e))))
1207 (k-semi (name &optional proc) (tok+val 'to-semi name proc))
1208 (gather () (let ((pairs `(,e ,@@-holes ,b))
1209 acc)
1210 (while pairs
1211 (push (buffer-substring-no-properties
1212 (cadr pairs) (car pairs))
1213 acc)
1214 (setq pairs (cddr pairs)))
1215 (apply 'concat acc)))
1216 (k-one@ (name &optional later) (tok+val 'to-one@ name
1217 (if later
1218 (lambda () t)
1219 'gather))))
1220 (save-excursion
1221 (goto-char (point-min))
1222 ;; headers
1223 (setq context 'headers)
1224 (flet ((hpush (name &optional proc)
1225 (push (k-semi name proc) headers)))
1226 (hpush 'head)
1227 (when (at 'branch)
1228 (hpush 'branch))
1229 (hpush 'access)
1230 (hpush 'symbols
1231 (lambda ()
1232 (mapcar (lambda (together)
1233 (let ((two (split-string together ":")))
1234 (setcar two (intern (car two)))
1235 (setcdr two (cadr two))
1236 two))
1237 (split-string
1238 (buffer-substring-no-properties b e)))))
1239 (hpush 'locks))
1240 (push `(strict . ,(when (at 'strict)
1241 (search-forward ";")
1243 headers)
1244 (when (at 'comment)
1245 (push (k-one@ 'comment) headers)
1246 (search-forward ";"))
1247 (when (at 'expand)
1248 (push (k-one@ 'expand) headers)
1249 (search-forward ";"))
1250 (setq headers (nreverse headers))
1251 ;; rev headers
1252 (sw) (setq context 'rev-headers)
1253 (while (looking-at "[0-9]")
1254 (push `(,(to-eol)
1255 ,(k-semi 'date
1256 (lambda ()
1257 (let ((ls (mapcar 'string-to-number
1258 (split-string
1259 (buffer-substring-no-properties
1260 b e)
1261 "\\."))))
1262 ;; Hack the year -- verified to be the
1263 ;; same algorithm used in RCS 5.7.
1264 (when (< (car ls) 100)
1265 (setcar ls (+ 1900 (car ls))))
1266 (apply 'encode-time (nreverse ls)))))
1267 ,@(mapcar 'k-semi '(author state))
1268 ,(k-semi 'branches
1269 (lambda ()
1270 (split-string
1271 (buffer-substring-no-properties b e))))
1272 ,(k-semi 'next))
1273 revs)
1274 (sw))
1275 (setq revs (nreverse revs))
1276 ;; desc
1277 (sw) (setq context 'desc
1278 desc (k-one@ 'desc))
1279 ;; rev bodies
1280 (let (acc
1281 ;; Element of `revs' that initially holds only header info.
1282 ;; "Pairwise combination" occurs when we add body info.
1284 ;; Components of the editing commands (aside from the actual
1285 ;; text) that comprise the `text' printed representations
1286 ;; (not including the "head" revision).
1287 cmd start act
1288 ;; Ascending (reversed) `@-holes' which the internal func
1289 ;; `incg' pops to effect incremental gathering.
1291 ;; Function to extract text (for the `a' command), either
1292 ;; `incg' or `buffer-substring-no-properties'. (This is
1293 ;; for speed; strictly speaking, it is sufficient to use
1294 ;; only the former since it behaves identically to the
1295 ;; latter in the absense of "@@".)
1296 sub)
1297 (flet ((incg (beg end) (let ((b beg) (e end) @-holes)
1298 (while (and asc (< (car asc) e))
1299 (push (pop asc) @-holes))
1300 ;; Self-deprecate when work is done.
1301 ;; Folding many dimensions into one.
1302 ;; Thanks B.Mandelbrot, for complex sum.
1303 ;; O beauteous math! --the Unvexed Bum
1304 (unless asc
1305 (setq sub 'buffer-substring-no-properties))
1306 (gather))))
1307 (while (and (sw)
1308 (not (eobp))
1309 (setq context (to-eol)
1310 rev (or (assoc context revs)
1311 (error "Rev `%s' has body but no head"
1312 context))))
1313 (push (k-one@ 'log) (cdr rev))
1314 ;; For rev body `text' tags, delay translation slightly...
1315 (push (k-one@ 'text t) (cdr rev))
1316 ;; ... until we decide which tag and value is appropriate to
1317 ;; collect. For the "head" revision, compute the value of the
1318 ;; `text' printed representation by simple `gather'. For all
1319 ;; other revisions, replace the `text' tag+value with `:insn'
1320 ;; plus value, always scanning in-place.
1321 (if (string= context (cdr (assq 'head headers)))
1322 (setcdr (cadr rev) (gather))
1323 (if @-holes
1324 (setq asc (nreverse @-holes)
1325 sub 'incg)
1326 (setq sub 'buffer-substring-no-properties))
1327 (goto-char b)
1328 (setq acc nil)
1329 (while (< (point) e)
1330 (forward-char 1)
1331 (setq cmd (char-before)
1332 start (read (current-buffer))
1333 act (read (current-buffer)))
1334 (forward-char 1)
1335 (push (case cmd
1337 ;; `d' means "delete lines".
1338 ;; For Emacs spirit, we use `k' for "kill".
1339 `(,start k ,act))
1341 ;; `a' means "append after this line" but
1342 ;; internally we normalize it so that START
1343 ;; specifies the actual line for insert, thus
1344 ;; requiring less hair in the realization algs.
1345 ;; For Emacs spirit, we use `i' for "insert".
1346 `(,(1+ start) i
1347 ,(funcall sub (point) (progn (forward-line act)
1348 (point)))))
1349 (t (error "Bad command `%c' in `text' for rev `%s'"
1350 cmd context)))
1351 acc))
1352 (goto-char (1+ e))
1353 (setcar (cdr rev) (cons :insn acc)))))))
1354 ;; rv
1355 `((headers ,desc ,@headers)
1356 (revisions ,@revs)))))
1358 (provide 'vc-rcs)
1360 ;; arch-tag: 759b4916-5b0d-431d-b647-b185b8c652cf
1361 ;;; vc-rcs.el ends here