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