]> code.delx.au - gnu-emacs/blob - lisp/vc-hooks.el
(vc-fetch-master-properties): Pass an absolute
[gnu-emacs] / lisp / vc-hooks.el
1 ;;; vc-hooks.el --- resident support for version-control
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Modified by:
7 ;; Per Cederqvist <ceder@lysator.liu.se>
8 ;; Andre Spiegel <spiegel@berlin.informatik.uni-stuttgart.de>
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
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
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Commentary:
27
28 ;; This is the always-loaded portion of VC.
29 ;; It takes care VC-related activities that are done when you visit a file,
30 ;; so that vc.el itself is loaded only when you use a VC command.
31 ;; See the commentary of vc.el.
32
33 ;;; Code:
34
35 ;; Customization Variables (the rest is in vc.el)
36
37 (defvar vc-default-back-end nil
38 "*Back-end actually used by this interface; may be SCCS or RCS.
39 The value is only computed when needed to avoid an expensive search.")
40
41 (defvar vc-handle-cvs t
42 "*If non-nil, use VC for files managed with CVS.
43 If it is nil, don't use VC for those files.")
44
45 (defvar vc-path
46 (if (file-directory-p "/usr/sccs")
47 '("/usr/sccs")
48 nil)
49 "*List of extra directories to search for version control commands.")
50
51 (defvar vc-master-templates
52 '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
53 ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS)
54 vc-find-cvs-master)
55 "*Where to look for version-control master files.
56 The first pair corresponding to a given back end is used as a template
57 when creating new masters.")
58
59 (defvar vc-make-backup-files nil
60 "*If non-nil, backups of registered files are made as with other files.
61 If nil (the default), files covered by version control don't get backups.")
62
63 (defvar vc-display-status t
64 "*If non-nil, display revision number and lock status in modeline.
65 Otherwise, not displayed.")
66
67 (defvar vc-consult-headers t
68 "*Identify work files by searching for version headers.")
69
70 (defvar vc-mistrust-permissions nil
71 "*Don't assume that permissions and ownership track version-control status.")
72
73 (defvar vc-keep-workfiles t
74 "*If non-nil, don't delete working files after registering changes.
75 If the back-end is CVS, workfiles are always kept, regardless of the
76 value of this flag.")
77
78 ;; Tell Emacs about this new kind of minor mode
79 (if (not (assoc 'vc-mode minor-mode-alist))
80 (setq minor-mode-alist (cons '(vc-mode vc-mode)
81 minor-mode-alist)))
82
83 (make-variable-buffer-local 'vc-mode)
84 (put 'vc-mode 'permanent-local t)
85
86 ;; We need a notion of per-file properties because the version
87 ;; control state of a file is expensive to derive --- we compute
88 ;; them when the file is initially found, keep them up to date
89 ;; during any subsequent VC operations, and forget them when
90 ;; the buffer is killed.
91
92 (defmacro vc-error-occurred (&rest body)
93 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
94
95 (defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
96 "Obarray for per-file properties.")
97
98 (defvar vc-buffer-backend t)
99 (make-variable-buffer-local 'vc-buffer-backend)
100
101 (defun vc-file-setprop (file property value)
102 ;; set per-file property
103 (put (intern file vc-file-prop-obarray) property value))
104
105 (defun vc-file-getprop (file property)
106 ;; get per-file property
107 (get (intern file vc-file-prop-obarray) property))
108
109 (defun vc-file-clearprops (file)
110 ;; clear all properties of a given file
111 (setplist (intern file vc-file-prop-obarray) nil))
112
113 ;;; Functions that determine property values, by examining the
114 ;;; working file, the master file, or log program output
115
116 (defun vc-match-substring (bn)
117 (buffer-substring (match-beginning bn) (match-end bn)))
118
119 (defun vc-lock-file (file)
120 ;; Generate lock file name corresponding to FILE
121 (let ((master (vc-name file)))
122 (and
123 master
124 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
125 (concat
126 (substring master (match-beginning 1) (match-end 1))
127 "p."
128 (substring master (match-beginning 2) (match-end 2))))))
129
130 (defun vc-parse-buffer (patterns &optional file properties)
131 ;; Use PATTERNS to parse information out of the current buffer.
132 ;; Each element of PATTERNS is a list of 2 to 3 elements. The first element
133 ;; is the pattern to be matched, and the second (an integer) is the
134 ;; number of the subexpression that should be returned. If there's
135 ;; a third element (also the number of a subexpression), that
136 ;; subexpression is assumed to be a date field and we want the most
137 ;; recent entry matching the template.
138 ;; If FILE and PROPERTIES are given, the latter must be a list of
139 ;; properties of the same length as PATTERNS; each property is assigned
140 ;; the corresponding value.
141 (mapcar (function (lambda (p)
142 (goto-char (point-min))
143 (cond
144 ((eq (length p) 2) ;; search for first entry
145 (let ((value nil))
146 (if (re-search-forward (car p) nil t)
147 (setq value (vc-match-substring (elt p 1))))
148 (if file
149 (progn (vc-file-setprop file (car properties) value)
150 (setq properties (cdr properties))))
151 value))
152 ((eq (length p) 3) ;; search for latest entry
153 (let ((latest-date "") (latest-val))
154 (while (re-search-forward (car p) nil t)
155 (let ((date (vc-match-substring (elt p 2))))
156 (if (string< latest-date date)
157 (progn
158 (setq latest-date date)
159 (setq latest-val
160 (vc-match-substring (elt p 1)))))))
161 (if file
162 (progn (vc-file-setprop file (car properties) latest-val)
163 (setq properties (cdr properties))))
164 latest-val)))))
165 patterns)
166 )
167
168 (defun vc-insert-file (file &optional limit blocksize)
169 ;; Insert the contents of FILE into the current buffer.
170 ;; Optional argument LIMIT is a regexp. If present,
171 ;; the file is inserted in chunks of size BLOCKSIZE
172 ;; (default 8 kByte), until the first occurence of
173 ;; LIMIT is found. The function returns nil if FILE
174 ;; doesn't exist.
175 (cond ((file-exists-p file)
176 (cond (limit
177 (if (not blocksize) (setq blocksize 8192))
178 (let (found s)
179 (while (not found)
180 (setq s (buffer-size))
181 (goto-char (1+ s))
182 (setq found
183 (or (zerop (car (cdr
184 (insert-file-contents file nil s
185 (+ s blocksize)))))
186 (progn (beginning-of-line)
187 (re-search-forward limit nil t)))))))
188 (t (insert-file-contents file)))
189 (set-buffer-modified-p nil)
190 (auto-save-mode nil)
191 t)
192 (t nil)))
193
194 (defun vc-parse-locks (file locks)
195 ;; Parse RCS or SCCS locks.
196 ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
197 ;; which is returned and stored into the property `vc-master-locks'.
198 (if (not locks)
199 (vc-file-setprop file 'vc-master-locks 'none)
200 (let ((found t) (index 0) master-locks version user)
201 (cond ((eq (vc-backend file) 'SCCS)
202 (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
203 locks index)
204 (setq version (substring locks
205 (match-beginning 1) (match-end 1)))
206 (setq user (substring locks
207 (match-beginning 2) (match-end 2)))
208 (setq master-locks (append master-locks
209 (list (cons version user))))
210 (setq index (match-end 0))))
211 ((eq (vc-backend file) 'RCS)
212 (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
213 locks index)
214 (setq version (substring locks
215 (match-beginning 2) (match-end 2)))
216 (setq user (substring locks
217 (match-beginning 1) (match-end 1)))
218 (setq master-locks (append master-locks
219 (list (cons version user))))
220 (setq index (match-end 0)))))
221 (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
222
223 (defun vc-fetch-master-properties (file)
224 ;; Fetch those properties of FILE that are stored in the master file.
225 ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
226 ;; here because that is slow.
227 ;; That gets done if/when the functions vc-latest-version
228 ;; and vc-your-latest-version get called.
229 (save-excursion
230 (cond
231 ((eq (vc-backend file) 'SCCS)
232 (set-buffer (get-buffer-create "*vc-info*"))
233 (if (vc-insert-file (vc-lock-file file))
234 (progn (vc-parse-locks file (buffer-string))
235 (erase-buffer))
236 (vc-file-setprop file 'vc-master-locks 'none))
237 (vc-insert-file (vc-name file) "^\001e")
238 (vc-parse-buffer
239 (list '("^\001d D \\([^ ]+\\)" 1)
240 (list (concat "^\001d D \\([^ ]+\\) .* "
241 (regexp-quote (user-login-name)) " ") 1))
242 file
243 '(vc-latest-version vc-your-latest-version)))
244
245 ((eq (vc-backend file) 'RCS)
246 (set-buffer (get-buffer-create "*vc-info*"))
247 (vc-insert-file (vc-name file) "^locks")
248 (vc-parse-buffer
249 (list '("^head[ \t\n]+\\([^;]+\\);" 1)
250 '("^branch[ \t\n]+\\([^;]+\\);" 1)
251 '("^locks\\([^;]+\\);" 1))
252 file
253 '(vc-head-version
254 vc-default-branch
255 vc-master-locks))
256 ;; determine vc-top-version: it is either the head version,
257 ;; or the tip of the default branch
258 (let ((default-branch (vc-file-getprop file 'vc-default-branch)))
259 (cond
260 ;; no default branch
261 ((or (not default-branch) (string= "" default-branch))
262 (vc-file-setprop file 'vc-top-version
263 (vc-file-getprop file 'vc-head-version)))
264 ;; default branch is actually a revision
265 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
266 default-branch)
267 (vc-file-setprop file 'vc-top-version default-branch))
268 ;; else, search for the tip of the default branch
269 (t (erase-buffer)
270 (vc-insert-file (vc-name file) "^desc")
271 (vc-parse-buffer (list (list
272 (concat "^\\("
273 (regexp-quote default-branch)
274 "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
275 file '(vc-top-version)))))
276 ;; translate the locks
277 (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
278
279 ((eq (vc-backend file) 'CVS)
280 ;; don't switch to the *vc-info* buffer before running the
281 ;; command, because that would change its default directory
282 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
283 (erase-buffer))
284 (let ((exec-path (append vc-path exec-path))
285 ;; Add vc-path to PATH for the execution of this command.
286 (process-environment
287 (cons (concat "PATH=" (getenv "PATH")
288 path-separator
289 (mapconcat 'identity vc-path path-separator))
290 process-environment)))
291 (apply 'call-process "cvs" nil "*vc-info*" nil
292 (list "status" file)))
293 (set-buffer (get-buffer "*vc-info*"))
294 (set-buffer-modified-p nil)
295 (auto-save-mode nil)
296 (vc-parse-buffer
297 ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
298 ;; and CVS 1.4a1 says "Repository revision:".
299 '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
300 ("^File: [^ \t]+[ \t]+Status: \\(.*\\)" 1))
301 file
302 '(vc-latest-version vc-cvs-status))
303 ;; Translate those status values that are needed into symbols.
304 ;; Any other value is converted to nil.
305 (let ((status (vc-file-getprop file 'vc-cvs-status)))
306 (cond ((string-match "Up-to-date" status)
307 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
308 (vc-file-setprop file 'vc-checkout-time
309 (nth 5 (file-attributes file))))
310 ((string-match "Locally Modified" status)
311 (vc-file-setprop file 'vc-cvs-status 'locally-modified))
312 ((string-match "Needs Merge" status)
313 (vc-file-setprop file 'vc-cvs-status 'needs-merge))
314 (t (vc-file-setprop file 'vc-cvs-status nil))))))
315 (if (get-buffer "*vc-info*")
316 (kill-buffer (get-buffer "*vc-info*")))))
317
318 ;;; Functions that determine property values, by examining the
319 ;;; working file, the master file, or log program output
320
321 (defun vc-consult-rcs-headers (file)
322 ;; Search for RCS headers in FILE, and set properties
323 ;; accordingly. This function can be disabled by setting
324 ;; vc-consult-headers to nil.
325 ;; Returns: nil if no headers were found
326 ;; (or if the feature is disabled,
327 ;; or if there is currently no buffer
328 ;; visiting FILE)
329 ;; 'rev if a workfile revision was found
330 ;; 'rev-and-lock if revision and lock info was found
331 (cond
332 ((or (not vc-consult-headers)
333 (not (get-file-buffer file))) nil)
334 ((save-excursion
335 (set-buffer (get-file-buffer file))
336 (goto-char (point-min))
337 (cond
338 ;; search for $Id or $Header
339 ;; -------------------------
340 ((or (and (search-forward "$Id: " nil t)
341 (looking-at "[^ ]+ \\([0-9.]+\\) "))
342 (and (progn (goto-char (point-min))
343 (search-forward "$Header: " nil t))
344 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
345 (goto-char (match-end 0))
346 ;; if found, store the revision number ...
347 (let ((rev (buffer-substring (match-beginning 1)
348 (match-end 1))))
349 ;; ... and check for the locking state
350 (if (re-search-forward
351 (concat "\\=[0-9]+/[0-9]+/[0-9]+ " ; date
352 "[0-9]+:[0-9]+:[0-9]+ " ; time
353 "[^ ]+ [^ ]+ ") ; author & state
354 nil t)
355 (cond
356 ;; unlocked revision
357 ((looking-at "\\$")
358 (vc-file-setprop file 'vc-workfile-version rev)
359 (vc-file-setprop file 'vc-locking-user 'none)
360 'rev-and-lock)
361 ;; revision is locked by some user
362 ((looking-at "\\([^ ]+\\) \\$")
363 (vc-file-setprop file 'vc-workfile-version rev)
364 (vc-file-setprop file 'vc-locking-user
365 (buffer-substring (match-beginning 1)
366 (match-end 1)))
367 'rev-and-lock)
368 ;; everything else: false
369 (nil))
370 ;; unexpected information in
371 ;; keyword string --> quit
372 nil)))
373 ;; search for $Revision
374 ;; --------------------
375 ((re-search-forward (concat "\\$"
376 "Revision: \\([0-9.]+\\) \\$")
377 nil t)
378 ;; if found, store the revision number ...
379 (let ((rev (buffer-substring (match-beginning 1)
380 (match-end 1))))
381 ;; and see if there's any lock information
382 (goto-char (point-min))
383 (if (re-search-forward (concat "\\$" "Locker:") nil t)
384 (cond ((looking-at " \\([^ ]+\\) \\$")
385 (vc-file-setprop file 'vc-workfile-version rev)
386 (vc-file-setprop file 'vc-locking-user
387 (buffer-substring (match-beginning 1)
388 (match-end 1)))
389 'rev-and-lock)
390 ((looking-at " *\\$")
391 (vc-file-setprop file 'vc-workfile-version rev)
392 (vc-file-setprop file 'vc-locking-user 'none)
393 'rev-and-lock)
394 (t
395 (vc-file-setprop file 'vc-workfile-version rev)
396 (vc-file-setprop file 'vc-locking-user 'none)
397 'rev-and-lock))
398 (vc-file-setprop file 'vc-workfile-version rev)
399 'rev)))
400 ;; else: nothing found
401 ;; -------------------
402 (t nil))))))
403
404 ;;; Access functions to file properties
405 ;;; (Properties should be _set_ using vc-file-setprop, but
406 ;;; _retrieved_ only through these functions, which decide
407 ;;; if the property is already known or not. A property should
408 ;;; only be retrieved by vc-file-getprop if there is no
409 ;;; access function.)
410
411 ;;; properties indicating the backend
412 ;;; being used for FILE
413
414 (defun vc-backend-subdirectory-name (&optional file)
415 ;; Where the master and lock files for the current directory are kept
416 (symbol-name
417 (or
418 (and file (vc-backend file))
419 vc-default-back-end
420 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
421
422 (defun vc-name (file)
423 "Return the master name of a file, nil if it is not registered."
424 (or (vc-file-getprop file 'vc-name)
425 (let ((name-and-type (vc-registered file)))
426 (if name-and-type
427 (progn
428 (vc-file-setprop file 'vc-backend (cdr name-and-type))
429 (vc-file-setprop file 'vc-name (car name-and-type)))))))
430
431 (defun vc-backend (file)
432 "Return the version-control type of a file, nil if it is not registered."
433 (and file
434 (or (vc-file-getprop file 'vc-backend)
435 (let ((name-and-type (vc-registered file)))
436 (if name-and-type
437 (progn
438 (vc-file-setprop file 'vc-name (car name-and-type))
439 (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
440
441 ;;; properties indicating the locking state
442
443 (defun vc-cvs-status (file)
444 ;; Return the cvs status of FILE
445 ;; (Status field in output of "cvs status")
446 (cond ((vc-file-getprop file 'vc-cvs-status))
447 (t (vc-fetch-master-properties file)
448 (vc-file-getprop file 'vc-cvs-status))))
449
450 (defun vc-master-locks (file)
451 ;; Return the lock entries in the master of FILE.
452 ;; Return 'none if there are no such entries, and a list
453 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
454 (cond ((vc-file-getprop file 'vc-master-locks))
455 (t (vc-fetch-master-properties file)
456 (vc-file-getprop file 'vc-master-locks))))
457
458 (defun vc-master-locking-user (file)
459 ;; Return the master file's idea of who is locking
460 ;; the current workfile version of FILE.
461 ;; Return 'none if it is not locked.
462 (let ((master-locks (vc-master-locks file)) lock)
463 (if (eq master-locks 'none) 'none
464 ;; search for a lock on the current workfile version
465 (setq lock (assoc (vc-workfile-version file) master-locks))
466 (cond (lock (cdr lock))
467 ('none)))))
468
469 (defun vc-locking-user (file)
470 ;; Return the name of the person currently holding a lock on FILE.
471 ;; Return nil if there is no such person.
472 ;; Under CVS, a file is considered locked if it has been modified since
473 ;; it was checked out. Under CVS, this will sometimes return the uid of
474 ;; the owner of the file (as a number) instead of a string.
475 ;; The property is cached. It is only looked up if it is currently nil.
476 ;; Note that, for a file that is not locked, the actual property value
477 ;; is 'none, to distinguish it from an unknown locking state. That value
478 ;; is converted to nil by this function, and returned to the caller.
479 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
480 (if locking-user
481 ;; if we already know the property, return it
482 (if (eq locking-user 'none) nil locking-user)
483
484 ;; otherwise, infer the property...
485 (cond
486 ;; in the CVS case, check the status
487 ((eq (vc-backend file) 'CVS)
488 (if (eq (vc-cvs-status file) 'up-to-date)
489 (vc-file-setprop file 'vc-locking-user 'none)
490 ;; The expression below should return the username of the owner
491 ;; of the file. It doesn't. It returns the username if it is
492 ;; you, or otherwise the UID of the owner of the file. The
493 ;; return value from this function is only used by
494 ;; vc-dired-reformat-line, and it does the proper thing if a UID
495 ;; is returned.
496 ;;
497 ;; The *proper* way to fix this would be to implement a built-in
498 ;; function in Emacs, say, (username UID), that returns the
499 ;; username of a given UID.
500 ;;
501 ;; The result of this hack is that vc-directory will print the
502 ;; name of the owner of the file for any files that are
503 ;; modified.
504 (let ((uid (nth 2 (file-attributes file))))
505 (if (= uid (user-uid))
506 (vc-file-setprop file 'vc-locking-user (user-login-name))
507 (vc-file-setprop file 'vc-locking-user uid)))))
508
509 ;; RCS case: attempt a header search. If this feature is
510 ;; disabled, vc-consult-rcs-headers always returns nil.
511 ((and (eq (vc-backend file) 'RCS)
512 (eq (vc-consult-rcs-headers file) 'rev-and-lock)))
513
514 ;; if the file permissions are not trusted,
515 ;; use the information from the master file
516 ((or (not vc-keep-workfiles)
517 (eq vc-mistrust-permissions 't)
518 (and vc-mistrust-permissions
519 (funcall vc-mistrust-permissions
520 (vc-backend-subdirectory-name file))))
521 (vc-file-setprop file 'vc-locking-user (vc-master-locking-user file)))
522
523 ;; Otherwise: Use the file permissions. (But if it turns out that the
524 ;; file is not owned by the user, use the master file.)
525 ;; This implementation assumes that any file which is under version
526 ;; control and has -rw-r--r-- is locked by its owner. This is true
527 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
528 ;; We have to be careful not to exclude files with execute bits on;
529 ;; scripts can be under version control too. Also, we must ignore the
530 ;; group-read and other-read bits, since paranoid users turn them off.
531 ;; This hack wins because calls to the somewhat expensive
532 ;; `vc-fetch-master-properties' function only have to be made if
533 ;; (a) the file is locked by someone other than the current user,
534 ;; or (b) some untoward manipulation behind vc's back has changed
535 ;; the owner or the `group' or `other' write bits.
536 (t
537 (let ((attributes (file-attributes file)))
538 (cond ((string-match ".r-..-..-." (nth 8 attributes))
539 (vc-file-setprop file 'vc-locking-user 'none))
540 ((and (= (nth 2 attributes) (user-uid))
541 (string-match ".rw..-..-." (nth 8 attributes)))
542 (vc-file-setprop file 'vc-locking-user (user-login-name)))
543 (t
544 (vc-file-setprop file 'vc-locking-user
545 (vc-master-locking-user file))))
546 )))
547 ;; recursively call the function again,
548 ;; to convert a possible 'none value
549 (vc-locking-user file))))
550
551 ;;; properties to store current and recent version numbers
552
553 (defun vc-latest-version (file)
554 ;; Return version level of the latest version of FILE
555 (cond ((vc-file-getprop file 'vc-latest-version))
556 (t (vc-fetch-properties file)
557 (vc-file-getprop file 'vc-latest-version))))
558
559 (defun vc-your-latest-version (file)
560 ;; Return version level of the latest version of FILE checked in by you
561 (cond ((vc-file-getprop file 'vc-your-latest-version))
562 (t (vc-fetch-properties file)
563 (vc-file-getprop file 'vc-your-latest-version))))
564
565 (defun vc-top-version (file)
566 ;; Return version level of the highest revision on the default branch
567 ;; If there is no default branch, return the highest version number
568 ;; on the trunk.
569 ;; This property is defined for RCS only.
570 (cond ((vc-file-getprop file 'vc-top-version))
571 (t (vc-fetch-master-properties file)
572 (vc-file-getprop file 'vc-top-version))))
573
574 (defun vc-fetch-properties (file)
575 ;; Fetch vc-latest-version and vc-your-latest-version
576 ;; if that wasn't already done.
577 (cond
578 ((eq (vc-backend file) 'RCS)
579 (save-excursion
580 (set-buffer (get-buffer-create "*vc-info*"))
581 (vc-insert-file (vc-name file) "^desc")
582 (vc-parse-buffer
583 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
584 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
585 "date[ \t]+\\([0-9.]+\\);[ \t]+"
586 "author[ \t]+"
587 (regexp-quote (user-login-name)) ";") 1 2))
588 file
589 '(vc-latest-version vc-your-latest-version))))
590 (t (vc-fetch-master-properties file))
591 ))
592
593 (defun vc-workfile-version (file)
594 ;; Return version level of the current workfile FILE
595 ;; This is attempted by first looking at the RCS keywords.
596 ;; If there are no keywords in the working file,
597 ;; vc-top-version is taken.
598 ;; Note that this property is cached, that is, it is only
599 ;; looked up if it is nil.
600 ;; For SCCS, this property is equivalent to vc-latest-version.
601 (cond ((vc-file-getprop file 'vc-workfile-version))
602 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
603 ((eq (vc-backend file) 'RCS)
604 (if (vc-consult-rcs-headers file)
605 (vc-file-getprop file 'vc-workfile-version)
606 (let ((rev (cond ((vc-top-version file))
607 ((vc-latest-version file)))))
608 (vc-file-setprop file 'vc-workfile-version rev)
609 rev)))
610 ((eq (vc-backend file) 'CVS)
611 (if (vc-consult-rcs-headers file) ;; CVS
612 (vc-file-getprop file 'vc-workfile-version)
613 (vc-find-cvs-master (file-name-directory file)
614 (file-name-nondirectory file))
615 (vc-file-getprop file 'vc-workfile-version)))))
616
617 ;;; actual version-control code starts here
618
619 (defun vc-registered (file)
620 (let (handler handlers)
621 (if (boundp 'file-name-handler-alist)
622 (setq handler (find-file-name-handler file 'vc-registered)))
623 (if handler
624 (funcall handler 'vc-registered file)
625 ;; Search for a master corresponding to the given file
626 (let ((dirname (or (file-name-directory file) ""))
627 (basename (file-name-nondirectory file)))
628 (catch 'found
629 (mapcar
630 (function (lambda (s)
631 (if (atom s)
632 (funcall s dirname basename)
633 (let ((trial (format (car s) dirname basename)))
634 (if (and (file-exists-p trial)
635 ;; Make sure the file we found with name
636 ;; TRIAL is not the source file itself.
637 ;; That can happen with RCS-style names
638 ;; if the file name is truncated
639 ;; (e.g. to 14 chars). See if either
640 ;; directory or attributes differ.
641 (or (not (string= dirname
642 (file-name-directory trial)))
643 (not (equal
644 (file-attributes file)
645 (file-attributes trial)))))
646 (throw 'found (cons trial (cdr s))))))))
647 vc-master-templates)
648 nil)))))
649
650 (defun vc-find-cvs-master (dirname basename)
651 ;; Check if DIRNAME/BASENAME is handled by CVS.
652 ;; If it is, do a (throw 'found (cons MASTER 'CVS)).
653 ;; Note: If the file is ``cvs add''ed but not yet ``cvs commit''ed
654 ;; the MASTER will not actually exist yet. The other parts of VC
655 ;; checks for this condition. This function returns nil if
656 ;; DIRNAME/BASENAME is not handled by CVS.
657 (if (and vc-handle-cvs
658 (file-directory-p (concat dirname "CVS/"))
659 (file-readable-p (concat dirname "CVS/Entries"))
660 (file-readable-p (concat dirname "CVS/Repository")))
661 (let ((bufs nil) (fold case-fold-search))
662 (unwind-protect
663 (save-excursion
664 (setq bufs (list
665 (find-file-noselect (concat dirname "CVS/Entries"))))
666 (set-buffer (car bufs))
667 (goto-char (point-min))
668 ;; make sure the file name is searched
669 ;; case-sensitively
670 (setq case-fold-search nil)
671 (cond
672 ((re-search-forward
673 (concat "^/" (regexp-quote basename) "/\\([^/]*\\)/")
674 nil t)
675 (setq case-fold-search fold) ;; restore the old value
676 ;; We found it. Store away version number, now
677 ;; that we are anyhow so close to finding it.
678 (vc-file-setprop (concat dirname basename)
679 'vc-workfile-version
680 (buffer-substring (match-beginning 1)
681 (match-end 1)))
682 (setq bufs (cons (find-file-noselect
683 (concat dirname "CVS/Repository"))
684 bufs))
685 (set-buffer (car bufs))
686 (let ((master
687 (concat (file-name-as-directory
688 (buffer-substring (point-min)
689 (1- (point-max))))
690 basename
691 ",v")))
692 (throw 'found (cons master 'CVS))))
693 (t (setq case-fold-search fold) ;; restore the old value
694 nil)))
695 (mapcar (function kill-buffer) bufs)))))
696
697 (defun vc-buffer-backend ()
698 "Return the version-control type of the visited file, or nil if none."
699 (if (eq vc-buffer-backend t)
700 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
701 vc-buffer-backend))
702
703 (defun vc-toggle-read-only (&optional verbose)
704 "Change read-only status of current buffer, perhaps via version control.
705 If the buffer is visiting a file registered with version control,
706 then check the file in or out. Otherwise, just change the read-only flag
707 of the buffer. With prefix argument, ask for version number."
708 (interactive "P")
709 (if (vc-backend (buffer-file-name))
710 (vc-next-action verbose)
711 (toggle-read-only)))
712 (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
713
714 (defun vc-mode-line (file &optional label)
715 "Set `vc-mode' to display type of version control for FILE.
716 The value is set in the current buffer, which should be the buffer
717 visiting FILE. Second optional arg LABEL is put in place of version
718 control system name."
719 (interactive (list buffer-file-name nil))
720 (let ((vc-type (vc-backend file))
721 (vc-status-string (and vc-display-status (vc-status file))))
722 (setq vc-mode
723 (concat " " (or label (symbol-name vc-type)) vc-status-string))
724 ;; Make the buffer read-only if the file is not locked
725 ;; (or unchanged, in the CVS case).
726 ;; Determine this by looking at the mode string,
727 ;; so that no further external status query is necessary
728 (if vc-status-string
729 (if (eq (elt vc-status-string 0) ?-)
730 (setq buffer-read-only t))
731 (if (not (vc-locking-user file))
732 (setq buffer-read-only t)))
733 ;; Even root shouldn't modify a registered file without
734 ;; locking it first.
735 (and vc-type
736 (not buffer-read-only)
737 (zerop (user-uid))
738 (require 'vc)
739 (not (equal (user-login-name) (vc-locking-user file)))
740 (setq buffer-read-only t))
741 (and (null vc-type)
742 (file-symlink-p file)
743 (let ((link-type (vc-backend (file-symlink-p file))))
744 (if link-type
745 (message
746 "Warning: symbolic link to %s-controlled source file"
747 link-type))))
748 (force-mode-line-update)
749 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
750 vc-type))
751
752 (defun vc-status (file)
753 ;; Return string for placement in modeline by `vc-mode-line'.
754 ;; Format:
755 ;;
756 ;; "-REV" if the revision is not locked
757 ;; ":REV" if the revision is locked by the user
758 ;; ":LOCKER:REV" if the revision is locked by somebody else
759 ;; " @@" for a CVS file that is added, but not yet committed
760 ;;
761 ;; In the CVS case, a "locked" working file is a
762 ;; working file that is modified with respect to the master.
763 ;; The file is "locked" from the moment when the user makes
764 ;; the buffer writable.
765 ;;
766 ;; This function assumes that the file is registered.
767
768 (let ((locker (vc-locking-user file))
769 (rev (vc-workfile-version file)))
770 (cond ((string= "0" rev)
771 " @@")
772 ((not locker)
773 (concat "-" rev))
774 ((if (stringp locker)
775 (string= locker (user-login-name))
776 (= locker (user-uid)))
777 (concat ":" rev))
778 (t
779 (concat ":" locker ":" rev)))))
780
781 ;;; install a call to the above as a find-file hook
782 (defun vc-find-file-hook ()
783 ;; Recompute whether file is version controlled,
784 ;; if user has killed the buffer and revisited.
785 (cond
786 (buffer-file-name
787 (vc-file-clearprops buffer-file-name)
788 (cond
789 ((vc-backend buffer-file-name)
790 (vc-mode-line buffer-file-name)
791 (cond ((not vc-make-backup-files)
792 ;; Use this variable, not make-backup-files,
793 ;; because this is for things that depend on the file name.
794 (make-local-variable 'backup-inhibited)
795 (setq backup-inhibited t))))))))
796
797 (add-hook 'find-file-hooks 'vc-find-file-hook)
798
799 ;;; more hooks, this time for file-not-found
800 (defun vc-file-not-found-hook ()
801 "When file is not found, try to check it out from RCS or SCCS.
802 Returns t if checkout was successful, nil otherwise."
803 (if (vc-backend buffer-file-name)
804 (save-excursion
805 (require 'vc)
806 (setq default-directory (file-name-directory (buffer-file-name)))
807 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
808
809 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
810
811 ;; Discard info about a file when we kill its buffer.
812 (defun vc-kill-buffer-hook ()
813 (if (stringp (buffer-file-name))
814 (progn
815 (vc-file-clearprops (buffer-file-name))
816 (kill-local-variable 'vc-buffer-backend))))
817
818 ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
819
820 ;;; Now arrange for bindings and autoloading of the main package.
821 ;;; Bindings for this have to go in the global map, as we'll often
822 ;;; want to call them from random buffers.
823
824 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
825 (if (not (keymapp vc-prefix-map))
826 (progn
827 (setq vc-prefix-map (make-sparse-keymap))
828 (define-key global-map "\C-xv" vc-prefix-map)
829 (define-key vc-prefix-map "a" 'vc-update-change-log)
830 (define-key vc-prefix-map "c" 'vc-cancel-version)
831 (define-key vc-prefix-map "d" 'vc-directory)
832 (define-key vc-prefix-map "h" 'vc-insert-headers)
833 (define-key vc-prefix-map "i" 'vc-register)
834 (define-key vc-prefix-map "l" 'vc-print-log)
835 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
836 (define-key vc-prefix-map "s" 'vc-create-snapshot)
837 (define-key vc-prefix-map "u" 'vc-revert-buffer)
838 (define-key vc-prefix-map "v" 'vc-next-action)
839 (define-key vc-prefix-map "=" 'vc-diff)
840 (define-key vc-prefix-map "~" 'vc-version-other-window)))
841
842 (if (not (boundp 'vc-menu-map))
843 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
844 ;; vc-menu-map.
845 ()
846 ;;(define-key vc-menu-map [show-files]
847 ;; '("Show Files under VC" . (vc-directory t)))
848 (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
849 (define-key vc-menu-map [separator1] '("----"))
850 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
851 (define-key vc-menu-map [vc-version-other-window]
852 '("Show Other Version" . vc-version-other-window))
853 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
854 (define-key vc-menu-map [vc-update-change-log]
855 '("Update ChangeLog" . vc-update-change-log))
856 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
857 (define-key vc-menu-map [separator2] '("----"))
858 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
859 (define-key vc-menu-map [vc-revert-buffer]
860 '("Revert to Last Version" . vc-revert-buffer))
861 (define-key vc-menu-map [vc-insert-header]
862 '("Insert Header" . vc-insert-headers))
863 (define-key vc-menu-map [vc-menu-check-in] '("Check In" . vc-next-action))
864 (define-key vc-menu-map [vc-check-out] '("Check Out" . vc-toggle-read-only))
865 (define-key vc-menu-map [vc-register] '("Register" . vc-register))
866 (put 'vc-rename-file 'menu-enable 'vc-mode)
867 (put 'vc-version-other-window 'menu-enable 'vc-mode)
868 (put 'vc-diff 'menu-enable 'vc-mode)
869 (put 'vc-update-change-log 'menu-enable
870 '(eq (vc-buffer-backend) 'RCS))
871 (put 'vc-print-log 'menu-enable 'vc-mode)
872 (put 'vc-cancel-version 'menu-enable 'vc-mode)
873 (put 'vc-revert-buffer 'menu-enable 'vc-mode)
874 (put 'vc-insert-headers 'menu-enable 'vc-mode)
875 (put 'vc-next-action 'menu-enable '(and vc-mode (not buffer-read-only)))
876 (put 'vc-toggle-read-only 'menu-enable '(and vc-mode buffer-read-only))
877 (put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
878 )
879
880 (provide 'vc-hooks)
881
882 ;;; vc-hooks.el ends here