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