(vc-start-entry): Prevent lossage when doing a mass checkin from
[bpt/emacs.git] / 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 (erase-buffer)
176 (cond ((file-exists-p file)
177 (cond (limit
178 (if (not blocksize) (setq blocksize 8192))
179 (let (found s)
180 (while (not found)
181 (setq s (buffer-size))
182 (goto-char (1+ s))
183 (setq found
184 (or (zerop (car (cdr
185 (insert-file-contents file nil s
186 (+ s blocksize)))))
187 (progn (beginning-of-line)
188 (re-search-forward limit nil t)))))))
189 (t (insert-file-contents file)))
190 (set-buffer-modified-p nil)
191 (auto-save-mode nil)
192 t)
193 (t nil)))
194
195 (defun vc-parse-locks (file locks)
196 ;; Parse RCS or SCCS locks.
197 ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
198 ;; which is returned and stored into the property `vc-master-locks'.
199 (if (not locks)
200 (vc-file-setprop file 'vc-master-locks 'none)
201 (let ((found t) (index 0) master-locks version user)
202 (cond ((eq (vc-backend file) 'SCCS)
203 (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
204 locks index)
205 (setq version (substring locks
206 (match-beginning 1) (match-end 1)))
207 (setq user (substring locks
208 (match-beginning 2) (match-end 2)))
209 (setq master-locks (append master-locks
210 (list (cons version user))))
211 (setq index (match-end 0))))
212 ((eq (vc-backend file) 'RCS)
213 (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
214 locks index)
215 (setq version (substring locks
216 (match-beginning 2) (match-end 2)))
217 (setq user (substring locks
218 (match-beginning 1) (match-end 1)))
219 (setq master-locks (append master-locks
220 (list (cons version user))))
221 (setq index (match-end 0)))))
222 (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
223
224 (defun vc-fetch-master-properties (file)
225 ;; Fetch those properties of FILE that are stored in the master file.
226 ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
227 ;; here because that is slow.
228 ;; That gets done if/when the functions vc-latest-version
229 ;; and vc-your-latest-version get called.
230 (save-excursion
231 (cond
232 ((eq (vc-backend file) 'SCCS)
233 (set-buffer (get-buffer-create "*vc-info*"))
234 (if (vc-insert-file (vc-lock-file file))
235 (vc-parse-locks file (buffer-string))
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 (vc-insert-file (vc-name file) "^desc")
270 (vc-parse-buffer (list (list
271 (concat "^\\("
272 (regexp-quote default-branch)
273 "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
274 file '(vc-top-version)))))
275 ;; translate the locks
276 (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
277
278 ((eq (vc-backend file) 'CVS)
279 ;; don't switch to the *vc-info* buffer before running the
280 ;; command, because that would change its default directory
281 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
282 (erase-buffer))
283 (let ((exec-path (append vc-path exec-path))
284 ;; Add vc-path to PATH for the execution of this command.
285 (process-environment
286 (cons (concat "PATH=" (getenv "PATH")
287 path-separator
288 (mapconcat 'identity vc-path path-separator))
289 process-environment)))
290 (apply 'call-process "cvs" nil "*vc-info*" nil
291 (list "status" file)))
292 (set-buffer (get-buffer "*vc-info*"))
293 (set-buffer-modified-p nil)
294 (auto-save-mode nil)
295 (vc-parse-buffer
296 ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
297 ;; and CVS 1.4a1 says "Repository revision:".
298 '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
299 ("^File: [^ \t]+[ \t]+Status: \\(.*\\)" 1))
300 file
301 '(vc-latest-version vc-cvs-status))
302 ;; Translate those status values that are needed into symbols.
303 ;; Any other value is converted to nil.
304 (let ((status (vc-file-getprop file 'vc-cvs-status)))
305 (cond ((string-match "Up-to-date" status)
306 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
307 (vc-file-setprop file 'vc-checkout-time
308 (nth 5 (file-attributes file))))
309 ((string-match "Locally Modified" status)
310 (vc-file-setprop file 'vc-cvs-status 'locally-modified))
311 ((string-match "Needs Merge" status)
312 (vc-file-setprop file 'vc-cvs-status 'needs-merge))
313 (t (vc-file-setprop file 'vc-cvs-status nil))))))
314 (if (get-buffer "*vc-info*")
315 (kill-buffer (get-buffer "*vc-info*")))))
316
317 ;;; Functions that determine property values, by examining the
318 ;;; working file, the master file, or log program output
319
320 (defun vc-consult-rcs-headers (file)
321 ;; Search for RCS headers in FILE, and set properties
322 ;; accordingly. This function can be disabled by setting
323 ;; vc-consult-headers to nil.
324 ;; Returns: nil if no headers were found
325 ;; (or if the feature is disabled,
326 ;; or if there is currently no buffer
327 ;; visiting FILE)
328 ;; 'rev if a workfile revision was found
329 ;; 'rev-and-lock if revision and lock info was found
330 (cond
331 ((or (not vc-consult-headers)
332 (not (get-file-buffer file))) nil)
333 ((save-excursion
334 (set-buffer (get-file-buffer file))
335 (goto-char (point-min))
336 (cond
337 ;; search for $Id or $Header
338 ;; -------------------------
339 ((or (and (search-forward "$Id: " nil t)
340 (looking-at "[^ ]+ \\([0-9.]+\\) "))
341 (and (progn (goto-char (point-min))
342 (search-forward "$Header: " nil t))
343 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
344 (goto-char (match-end 0))
345 ;; if found, store the revision number ...
346 (let ((rev (buffer-substring (match-beginning 1)
347 (match-end 1))))
348 ;; ... and check for the locking state
349 (if (re-search-forward
350 (concat "\\=[0-9]+[/-][0-9]+[/-][0-9]+ " ; date
351 "[0-9]+:[0-9]+:[0-9]+\\([+-][0-9:]+\\)? " ; time
352 "[^ ]+ [^ ]+ ") ; author & state
353 nil t)
354 (cond
355 ;; unlocked revision
356 ((looking-at "\\$")
357 (vc-file-setprop file 'vc-workfile-version rev)
358 (vc-file-setprop file 'vc-locking-user 'none)
359 'rev-and-lock)
360 ;; revision is locked by some user
361 ((looking-at "\\([^ ]+\\) \\$")
362 (vc-file-setprop file 'vc-workfile-version rev)
363 (vc-file-setprop file 'vc-locking-user
364 (buffer-substring (match-beginning 1)
365 (match-end 1)))
366 'rev-and-lock)
367 ;; everything else: false
368 (nil))
369 ;; unexpected information in
370 ;; keyword string --> quit
371 nil)))
372 ;; search for $Revision
373 ;; --------------------
374 ((re-search-forward (concat "\\$"
375 "Revision: \\([0-9.]+\\) \\$")
376 nil t)
377 ;; if found, store the revision number ...
378 (let ((rev (buffer-substring (match-beginning 1)
379 (match-end 1))))
380 ;; and see if there's any lock information
381 (goto-char (point-min))
382 (if (re-search-forward (concat "\\$" "Locker:") nil t)
383 (cond ((looking-at " \\([^ ]+\\) \\$")
384 (vc-file-setprop file 'vc-workfile-version rev)
385 (vc-file-setprop file 'vc-locking-user
386 (buffer-substring (match-beginning 1)
387 (match-end 1)))
388 'rev-and-lock)
389 ((looking-at " *\\$")
390 (vc-file-setprop file 'vc-workfile-version rev)
391 (vc-file-setprop file 'vc-locking-user 'none)
392 'rev-and-lock)
393 (t
394 (vc-file-setprop file 'vc-workfile-version rev)
395 (vc-file-setprop file 'vc-locking-user 'none)
396 'rev-and-lock))
397 (vc-file-setprop file 'vc-workfile-version rev)
398 'rev)))
399 ;; else: nothing found
400 ;; -------------------
401 (t nil))))))
402
403 ;;; Access functions to file properties
404 ;;; (Properties should be _set_ using vc-file-setprop, but
405 ;;; _retrieved_ only through these functions, which decide
406 ;;; if the property is already known or not. A property should
407 ;;; only be retrieved by vc-file-getprop if there is no
408 ;;; access function.)
409
410 ;;; properties indicating the backend
411 ;;; being used for FILE
412
413 (defun vc-backend-subdirectory-name (&optional file)
414 ;; Where the master and lock files for the current directory are kept
415 (symbol-name
416 (or
417 (and file (vc-backend file))
418 vc-default-back-end
419 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
420
421 (defun vc-name (file)
422 "Return the master name of a file, nil if it is not registered."
423 (or (vc-file-getprop file 'vc-name)
424 (let ((name-and-type (vc-registered file)))
425 (if name-and-type
426 (progn
427 (vc-file-setprop file 'vc-backend (cdr name-and-type))
428 (vc-file-setprop file 'vc-name (car name-and-type)))))))
429
430 (defun vc-backend (file)
431 "Return the version-control type of a file, nil if it is not registered."
432 (and file
433 (or (vc-file-getprop file 'vc-backend)
434 (let ((name-and-type (vc-registered file)))
435 (if name-and-type
436 (progn
437 (vc-file-setprop file 'vc-name (car name-and-type))
438 (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
439
440 ;;; properties indicating the locking state
441
442 (defun vc-cvs-status (file)
443 ;; Return the cvs status of FILE
444 ;; (Status field in output of "cvs status")
445 (cond ((vc-file-getprop file 'vc-cvs-status))
446 (t (vc-fetch-master-properties file)
447 (vc-file-getprop file 'vc-cvs-status))))
448
449 (defun vc-master-locks (file)
450 ;; Return the lock entries in the master of FILE.
451 ;; Return 'none if there are no such entries, and a list
452 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
453 (cond ((vc-file-getprop file 'vc-master-locks))
454 (t (vc-fetch-master-properties file)
455 (vc-file-getprop file 'vc-master-locks))))
456
457 (defun vc-master-locking-user (file)
458 ;; Return the master file's idea of who is locking
459 ;; the current workfile version of FILE.
460 ;; Return 'none if it is not locked.
461 (let ((master-locks (vc-master-locks file)) lock)
462 (if (eq master-locks 'none) 'none
463 ;; search for a lock on the current workfile version
464 (setq lock (assoc (vc-workfile-version file) master-locks))
465 (cond (lock (cdr lock))
466 ('none)))))
467
468 (defun vc-locking-user (file)
469 ;; Return the name of the person currently holding a lock on FILE.
470 ;; Return nil if there is no such person.
471 ;; Under CVS, a file is considered locked if it has been modified since
472 ;; it was checked out. Under CVS, this will sometimes return the uid of
473 ;; the owner of the file (as a number) instead of a string.
474 ;; The property is cached. It is only looked up if it is currently nil.
475 ;; Note that, for a file that is not locked, the actual property value
476 ;; is 'none, to distinguish it from an unknown locking state. That value
477 ;; is converted to nil by this function, and returned to the caller.
478 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
479 (if locking-user
480 ;; if we already know the property, return it
481 (if (eq locking-user 'none) nil locking-user)
482
483 ;; otherwise, infer the property...
484 (cond
485 ;; in the CVS case, check the status
486 ((eq (vc-backend file) 'CVS)
487 (if (eq (vc-cvs-status file) 'up-to-date)
488 (vc-file-setprop file 'vc-locking-user 'none)
489 ;; The expression below should return the username of the owner
490 ;; of the file. It doesn't. It returns the username if it is
491 ;; you, or otherwise the UID of the owner of the file. The
492 ;; return value from this function is only used by
493 ;; vc-dired-reformat-line, and it does the proper thing if a UID
494 ;; is returned.
495 ;;
496 ;; The *proper* way to fix this would be to implement a built-in
497 ;; function in Emacs, say, (username UID), that returns the
498 ;; username of a given UID.
499 ;;
500 ;; The result of this hack is that vc-directory will print the
501 ;; name of the owner of the file for any files that are
502 ;; modified.
503 (let ((uid (nth 2 (file-attributes file))))
504 (if (= uid (user-uid))
505 (vc-file-setprop file 'vc-locking-user (user-login-name))
506 (vc-file-setprop file 'vc-locking-user uid)))))
507
508 ;; RCS case: attempt a header search. If this feature is
509 ;; disabled, vc-consult-rcs-headers always returns nil.
510 ((and (eq (vc-backend file) 'RCS)
511 (eq (vc-consult-rcs-headers file) 'rev-and-lock)))
512
513 ;; if the file permissions are not trusted,
514 ;; use the information from the master file
515 ((or (not vc-keep-workfiles)
516 (eq vc-mistrust-permissions 't)
517 (and vc-mistrust-permissions
518 (funcall vc-mistrust-permissions
519 (vc-backend-subdirectory-name file))))
520 (vc-file-setprop file 'vc-locking-user (vc-master-locking-user file)))
521
522 ;; Otherwise: Use the file permissions. (But if it turns out that the
523 ;; file is not owned by the user, use the master file.)
524 ;; This implementation assumes that any file which is under version
525 ;; control and has -rw-r--r-- is locked by its owner. This is true
526 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
527 ;; We have to be careful not to exclude files with execute bits on;
528 ;; scripts can be under version control too. Also, we must ignore the
529 ;; group-read and other-read bits, since paranoid users turn them off.
530 ;; This hack wins because calls to the somewhat expensive
531 ;; `vc-fetch-master-properties' function only have to be made if
532 ;; (a) the file is locked by someone other than the current user,
533 ;; or (b) some untoward manipulation behind vc's back has changed
534 ;; the owner or the `group' or `other' write bits.
535 (t
536 (let ((attributes (file-attributes file)))
537 (cond ((string-match ".r-..-..-." (nth 8 attributes))
538 (vc-file-setprop file 'vc-locking-user 'none))
539 ((and (= (nth 2 attributes) (user-uid))
540 (string-match ".rw..-..-." (nth 8 attributes)))
541 (vc-file-setprop file 'vc-locking-user (user-login-name)))
542 (t
543 (vc-file-setprop file 'vc-locking-user
544 (vc-master-locking-user file))))
545 )))
546 ;; recursively call the function again,
547 ;; to convert a possible 'none value
548 (vc-locking-user file))))
549
550 ;;; properties to store current and recent version numbers
551
552 (defun vc-latest-version (file)
553 ;; Return version level of the latest version of FILE
554 (cond ((vc-file-getprop file 'vc-latest-version))
555 (t (vc-fetch-properties file)
556 (vc-file-getprop file 'vc-latest-version))))
557
558 (defun vc-your-latest-version (file)
559 ;; Return version level of the latest version of FILE checked in by you
560 (cond ((vc-file-getprop file 'vc-your-latest-version))
561 (t (vc-fetch-properties file)
562 (vc-file-getprop file 'vc-your-latest-version))))
563
564 (defun vc-top-version (file)
565 ;; Return version level of the highest revision on the default branch
566 ;; If there is no default branch, return the highest version number
567 ;; on the trunk.
568 ;; This property is defined for RCS only.
569 (cond ((vc-file-getprop file 'vc-top-version))
570 (t (vc-fetch-master-properties file)
571 (vc-file-getprop file 'vc-top-version))))
572
573 (defun vc-fetch-properties (file)
574 ;; Fetch vc-latest-version and vc-your-latest-version
575 ;; if that wasn't already done.
576 (cond
577 ((eq (vc-backend file) 'RCS)
578 (save-excursion
579 (set-buffer (get-buffer-create "*vc-info*"))
580 (vc-insert-file (vc-name file) "^desc")
581 (vc-parse-buffer
582 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
583 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
584 "date[ \t]+\\([0-9.]+\\);[ \t]+"
585 "author[ \t]+"
586 (regexp-quote (user-login-name)) ";") 1 2))
587 file
588 '(vc-latest-version vc-your-latest-version))
589 (if (get-buffer "*vc-info*")
590 (kill-buffer (get-buffer "*vc-info*")))))
591 (t (vc-fetch-master-properties file))
592 ))
593
594 (defun vc-workfile-version (file)
595 ;; Return version level of the current workfile FILE
596 ;; This is attempted by first looking at the RCS keywords.
597 ;; If there are no keywords in the working file,
598 ;; vc-top-version is taken.
599 ;; Note that this property is cached, that is, it is only
600 ;; looked up if it is nil.
601 ;; For SCCS, this property is equivalent to vc-latest-version.
602 (cond ((vc-file-getprop file 'vc-workfile-version))
603 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
604 ((eq (vc-backend file) 'RCS)
605 (if (vc-consult-rcs-headers file)
606 (vc-file-getprop file 'vc-workfile-version)
607 (let ((rev (cond ((vc-top-version file))
608 ((vc-latest-version file)))))
609 (vc-file-setprop file 'vc-workfile-version rev)
610 rev)))
611 ((eq (vc-backend file) 'CVS)
612 (if (vc-consult-rcs-headers file) ;; CVS
613 (vc-file-getprop file 'vc-workfile-version)
614 (vc-find-cvs-master (file-name-directory file)
615 (file-name-nondirectory file))
616 (vc-file-getprop file 'vc-workfile-version)))))
617
618 ;;; actual version-control code starts here
619
620 (defun vc-registered (file)
621 (let (handler handlers)
622 (if (boundp 'file-name-handler-alist)
623 (setq handler (find-file-name-handler file 'vc-registered)))
624 (if handler
625 (funcall handler 'vc-registered file)
626 ;; Search for a master corresponding to the given file
627 (let ((dirname (or (file-name-directory file) ""))
628 (basename (file-name-nondirectory file)))
629 (catch 'found
630 (mapcar
631 (function (lambda (s)
632 (if (atom s)
633 (funcall s dirname basename)
634 (let ((trial (format (car s) dirname basename)))
635 (if (and (file-exists-p trial)
636 ;; Make sure the file we found with name
637 ;; TRIAL is not the source file itself.
638 ;; That can happen with RCS-style names
639 ;; if the file name is truncated
640 ;; (e.g. to 14 chars). See if either
641 ;; directory or attributes differ.
642 (or (not (string= dirname
643 (file-name-directory trial)))
644 (not (equal
645 (file-attributes file)
646 (file-attributes trial)))))
647 (throw 'found (cons trial (cdr s))))))))
648 vc-master-templates)
649 nil)))))
650
651 (defun vc-find-cvs-master (dirname basename)
652 ;; Check if DIRNAME/BASENAME is handled by CVS.
653 ;; If it is, do a (throw 'found (cons MASTER 'CVS)).
654 ;; Note: If the file is ``cvs add''ed but not yet ``cvs commit''ed
655 ;; the MASTER will not actually exist yet. The other parts of VC
656 ;; checks for this condition. This function returns nil if
657 ;; DIRNAME/BASENAME is not handled by CVS.
658 (if (and vc-handle-cvs
659 (file-directory-p (concat dirname "CVS/"))
660 (file-readable-p (concat dirname "CVS/Entries"))
661 (file-readable-p (concat dirname "CVS/Repository")))
662 (let ((bufs nil) (fold case-fold-search))
663 (unwind-protect
664 (save-excursion
665 (setq bufs (list
666 (find-file-noselect (concat dirname "CVS/Entries"))))
667 (set-buffer (car bufs))
668 (goto-char (point-min))
669 ;; make sure the file name is searched
670 ;; case-sensitively
671 (setq case-fold-search nil)
672 (cond
673 ((re-search-forward
674 (concat "^/" (regexp-quote basename) "/\\([^/]*\\)/")
675 nil t)
676 (setq case-fold-search fold) ;; restore the old value
677 ;; We found it. Store away version number, now
678 ;; that we are anyhow so close to finding it.
679 (vc-file-setprop (concat dirname basename)
680 'vc-workfile-version
681 (buffer-substring (match-beginning 1)
682 (match-end 1)))
683 (setq bufs (cons (find-file-noselect
684 (concat dirname "CVS/Repository"))
685 bufs))
686 (set-buffer (car bufs))
687 (let ((master
688 (concat (file-name-as-directory
689 (buffer-substring (point-min)
690 (1- (point-max))))
691 basename
692 ",v")))
693 (throw 'found (cons master 'CVS))))
694 (t (setq case-fold-search fold) ;; restore the old value
695 nil)))
696 (mapcar (function kill-buffer) bufs)))))
697
698 (defun vc-buffer-backend ()
699 "Return the version-control type of the visited file, or nil if none."
700 (if (eq vc-buffer-backend t)
701 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
702 vc-buffer-backend))
703
704 (defun vc-toggle-read-only (&optional verbose)
705 "Change read-only status of current buffer, perhaps via version control.
706 If the buffer is visiting a file registered with version control,
707 then check the file in or out. Otherwise, just change the read-only flag
708 of the buffer. With prefix argument, ask for version number."
709 (interactive "P")
710 (if (vc-backend (buffer-file-name))
711 (vc-next-action verbose)
712 (toggle-read-only)))
713 (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
714
715 (defun vc-mode-line (file &optional label)
716 "Set `vc-mode' to display type of version control for FILE.
717 The value is set in the current buffer, which should be the buffer
718 visiting FILE. Second optional arg LABEL is put in place of version
719 control system name."
720 (interactive (list buffer-file-name nil))
721 (let ((vc-type (vc-backend file))
722 (vc-status-string (and vc-display-status (vc-status file))))
723 (setq vc-mode
724 (concat " " (or label (symbol-name vc-type)) vc-status-string))
725 ;; Make the buffer read-only if the file is not locked
726 ;; (or unchanged, in the CVS case).
727 ;; Determine this by looking at the mode string,
728 ;; so that no further external status query is necessary
729 (if vc-status-string
730 (if (eq (elt vc-status-string 0) ?-)
731 (setq buffer-read-only t))
732 (if (not (vc-locking-user file))
733 (setq buffer-read-only t)))
734 ;; Even root shouldn't modify a registered file without
735 ;; locking it first.
736 (and vc-type
737 (not buffer-read-only)
738 (zerop (user-uid))
739 (require 'vc)
740 (not (equal (user-login-name) (vc-locking-user file)))
741 (setq buffer-read-only t))
742 (and (null vc-type)
743 (file-symlink-p file)
744 (let ((link-type (vc-backend (file-symlink-p file))))
745 (if link-type
746 (message
747 "Warning: symbolic link to %s-controlled source file"
748 link-type))))
749 (force-mode-line-update)
750 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
751 vc-type))
752
753 (defun vc-status (file)
754 ;; Return string for placement in modeline by `vc-mode-line'.
755 ;; Format:
756 ;;
757 ;; "-REV" if the revision is not locked
758 ;; ":REV" if the revision is locked by the user
759 ;; ":LOCKER:REV" if the revision is locked by somebody else
760 ;; " @@" for a CVS file that is added, but not yet committed
761 ;;
762 ;; In the CVS case, a "locked" working file is a
763 ;; working file that is modified with respect to the master.
764 ;; The file is "locked" from the moment when the user makes
765 ;; the buffer writable.
766 ;;
767 ;; This function assumes that the file is registered.
768
769 (let ((locker (vc-locking-user file))
770 (rev (vc-workfile-version file)))
771 (cond ((string= "0" rev)
772 " @@")
773 ((not locker)
774 (concat "-" rev))
775 ((if (stringp locker)
776 (string= locker (user-login-name))
777 (= locker (user-uid)))
778 (concat ":" rev))
779 (t
780 (concat ":" locker ":" rev)))))
781
782 ;;; install a call to the above as a find-file hook
783 (defun vc-find-file-hook ()
784 ;; Recompute whether file is version controlled,
785 ;; if user has killed the buffer and revisited.
786 (cond
787 (buffer-file-name
788 (vc-file-clearprops buffer-file-name)
789 (cond
790 ((vc-backend buffer-file-name)
791 (vc-mode-line buffer-file-name)
792 (cond ((not vc-make-backup-files)
793 ;; Use this variable, not make-backup-files,
794 ;; because this is for things that depend on the file name.
795 (make-local-variable 'backup-inhibited)
796 (setq backup-inhibited t))))))))
797
798 (add-hook 'find-file-hooks 'vc-find-file-hook)
799
800 ;;; more hooks, this time for file-not-found
801 (defun vc-file-not-found-hook ()
802 "When file is not found, try to check it out from RCS or SCCS.
803 Returns t if checkout was successful, nil otherwise."
804 (if (vc-backend buffer-file-name)
805 (save-excursion
806 (require 'vc)
807 (setq default-directory (file-name-directory (buffer-file-name)))
808 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
809
810 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
811
812 ;; Discard info about a file when we kill its buffer.
813 (defun vc-kill-buffer-hook ()
814 (if (stringp (buffer-file-name))
815 (progn
816 (vc-file-clearprops (buffer-file-name))
817 (kill-local-variable 'vc-buffer-backend))))
818
819 ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
820
821 ;;; Now arrange for bindings and autoloading of the main package.
822 ;;; Bindings for this have to go in the global map, as we'll often
823 ;;; want to call them from random buffers.
824
825 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
826 (if (not (keymapp vc-prefix-map))
827 (progn
828 (setq vc-prefix-map (make-sparse-keymap))
829 (define-key global-map "\C-xv" vc-prefix-map)
830 (define-key vc-prefix-map "a" 'vc-update-change-log)
831 (define-key vc-prefix-map "c" 'vc-cancel-version)
832 (define-key vc-prefix-map "d" 'vc-directory)
833 (define-key vc-prefix-map "h" 'vc-insert-headers)
834 (define-key vc-prefix-map "i" 'vc-register)
835 (define-key vc-prefix-map "l" 'vc-print-log)
836 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
837 (define-key vc-prefix-map "s" 'vc-create-snapshot)
838 (define-key vc-prefix-map "u" 'vc-revert-buffer)
839 (define-key vc-prefix-map "v" 'vc-next-action)
840 (define-key vc-prefix-map "=" 'vc-diff)
841 (define-key vc-prefix-map "~" 'vc-version-other-window)))
842
843 (if (not (boundp 'vc-menu-map))
844 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
845 ;; vc-menu-map.
846 ()
847 ;;(define-key vc-menu-map [show-files]
848 ;; '("Show Files under VC" . (vc-directory t)))
849 (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
850 (define-key vc-menu-map [separator1] '("----"))
851 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
852 (define-key vc-menu-map [vc-version-other-window]
853 '("Show Other Version" . vc-version-other-window))
854 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
855 (define-key vc-menu-map [vc-update-change-log]
856 '("Update ChangeLog" . vc-update-change-log))
857 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
858 (define-key vc-menu-map [separator2] '("----"))
859 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
860 (define-key vc-menu-map [vc-revert-buffer]
861 '("Revert to Last Version" . vc-revert-buffer))
862 (define-key vc-menu-map [vc-insert-header]
863 '("Insert Header" . vc-insert-headers))
864 (define-key vc-menu-map [vc-menu-check-in] '("Check In" . vc-next-action))
865 (define-key vc-menu-map [vc-check-out] '("Check Out" . vc-toggle-read-only))
866 (define-key vc-menu-map [vc-register] '("Register" . vc-register))
867 (put 'vc-rename-file 'menu-enable 'vc-mode)
868 (put 'vc-version-other-window 'menu-enable 'vc-mode)
869 (put 'vc-diff 'menu-enable 'vc-mode)
870 (put 'vc-update-change-log 'menu-enable
871 '(eq (vc-buffer-backend) 'RCS))
872 (put 'vc-print-log 'menu-enable 'vc-mode)
873 (put 'vc-cancel-version 'menu-enable 'vc-mode)
874 (put 'vc-revert-buffer 'menu-enable 'vc-mode)
875 (put 'vc-insert-headers 'menu-enable 'vc-mode)
876 (put 'vc-next-action 'menu-enable '(and vc-mode (not buffer-read-only)))
877 (put 'vc-toggle-read-only 'menu-enable '(and vc-mode buffer-read-only))
878 (put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
879 )
880
881 (provide 'vc-hooks)
882
883 ;;; vc-hooks.el ends here