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