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