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