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