(vip-add-hook,vip-remove-hook): new functions.
[emacs.git] / lisp / vc-hooks.el
blob88ef2dbe1f5b40c7b130b2c7ca1cf435c8bb1cb0
1 ;;; vc-hooks.el --- resident support for version-control
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
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>
10 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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.
33 ;;; Code:
35 ;; Customization Variables (the rest is in vc.el)
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.")
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.")
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.")
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.")
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.")
63 (defvar vc-display-status t
64 "*If non-nil, display revision number and lock status in modeline.
65 Otherwise, not displayed.")
67 (defvar vc-consult-headers t
68 "*Identify work files by searching for version headers.")
70 (defvar vc-mistrust-permissions nil
71 "*Don't assume that permissions and ownership track version-control status.")
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.")
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)))
83 (make-variable-buffer-local 'vc-mode)
84 (put 'vc-mode 'permanent-local t)
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.
92 (defmacro vc-error-occurred (&rest body)
93 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
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.")
98 (defvar vc-buffer-backend t)
99 (make-variable-buffer-local 'vc-buffer-backend)
101 (defun vc-file-setprop (file property value)
102 ;; set per-file property
103 (put (intern file vc-file-prop-obarray) property value))
105 (defun vc-file-getprop (file property)
106 ;; get per-file property
107 (get (intern file vc-file-prop-obarray) property))
109 (defun vc-file-clearprops (file)
110 ;; clear all properties of a given file
111 (setplist (intern file vc-file-prop-obarray) nil))
113 ;;; Functions that determine property values, by examining the
114 ;;; working file, the master file, or log program output
116 (defun vc-match-substring (bn)
117 (buffer-substring (match-beginning bn) (match-end bn)))
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))))))
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)
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)
193 (t nil)))
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)))))
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)))
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)))
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)) exec-status
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 (setq exec-status
291 (apply 'call-process "cvs" nil "*vc-info*" nil
292 (list "status" file)))
293 (cond ((> exec-status 0)
294 (switch-to-buffer (get-file-buffer file))
295 (shrink-window-if-larger-than-buffer
296 (display-buffer "*vc-info*"))
297 (error "Couldn't find version control information"))))
298 (set-buffer (get-buffer "*vc-info*"))
299 (set-buffer-modified-p nil)
300 (auto-save-mode nil)
301 (vc-parse-buffer
302 ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
303 ;; and CVS 1.4a1 says "Repository revision:".
304 '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
305 ("^File: [^ \t]+[ \t]+Status: \\(.*\\)" 1))
306 file
307 '(vc-latest-version vc-cvs-status))
308 ;; Translate those status values that are needed into symbols.
309 ;; Any other value is converted to nil.
310 (let ((status (vc-file-getprop file 'vc-cvs-status)))
311 (cond ((string-match "Up-to-date" status)
312 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
313 (vc-file-setprop file 'vc-checkout-time
314 (nth 5 (file-attributes file))))
315 ((string-match "Locally Modified" status)
316 (vc-file-setprop file 'vc-cvs-status 'locally-modified))
317 ((string-match "Needs Merge" status)
318 (vc-file-setprop file 'vc-cvs-status 'needs-merge))
319 ((string-match "Needs Checkout" status)
320 (vc-file-setprop file 'vc-cvs-status 'needs-checkout))
321 (t (vc-file-setprop file 'vc-cvs-status nil))))))
322 (if (get-buffer "*vc-info*")
323 (kill-buffer (get-buffer "*vc-info*")))))
325 ;;; Functions that determine property values, by examining the
326 ;;; working file, the master file, or log program output
328 (defun vc-consult-rcs-headers (file)
329 ;; Search for RCS headers in FILE, and set properties
330 ;; accordingly. This function can be disabled by setting
331 ;; vc-consult-headers to nil.
332 ;; Returns: nil if no headers were found
333 ;; (or if the feature is disabled,
334 ;; or if there is currently no buffer
335 ;; visiting FILE)
336 ;; 'rev if a workfile revision was found
337 ;; 'rev-and-lock if revision and lock info was found
338 (cond
339 ((or (not vc-consult-headers)
340 (not (get-file-buffer file))) nil)
341 ((save-excursion
342 (set-buffer (get-file-buffer file))
343 (goto-char (point-min))
344 (cond
345 ;; search for $Id or $Header
346 ;; -------------------------
347 ((or (and (search-forward "$Id: " nil t)
348 (looking-at "[^ ]+ \\([0-9.]+\\) "))
349 (and (progn (goto-char (point-min))
350 (search-forward "$Header: " nil t))
351 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
352 (goto-char (match-end 0))
353 ;; if found, store the revision number ...
354 (let ((rev (buffer-substring (match-beginning 1)
355 (match-end 1))))
356 ;; ... and check for the locking state
357 (cond
358 ((looking-at
359 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
360 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
361 "[^ ]+ [^ ]+ ")) ; author & state
362 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
363 (cond
364 ;; unlocked revision
365 ((looking-at "\\$")
366 (vc-file-setprop file 'vc-workfile-version rev)
367 (vc-file-setprop file 'vc-locking-user 'none)
368 'rev-and-lock)
369 ;; revision is locked by some user
370 ((looking-at "\\([^ ]+\\) \\$")
371 (vc-file-setprop file 'vc-workfile-version rev)
372 (vc-file-setprop file 'vc-locking-user
373 (buffer-substring (match-beginning 1)
374 (match-end 1)))
375 'rev-and-lock)
376 ;; everything else: false
377 (nil)))
378 ;; unexpected information in
379 ;; keyword string --> quit
380 (nil))))
381 ;; search for $Revision
382 ;; --------------------
383 ((re-search-forward (concat "\\$"
384 "Revision: \\([0-9.]+\\) \\$")
385 nil t)
386 ;; if found, store the revision number ...
387 (let ((rev (buffer-substring (match-beginning 1)
388 (match-end 1))))
389 ;; and see if there's any lock information
390 (goto-char (point-min))
391 (if (re-search-forward (concat "\\$" "Locker:") nil t)
392 (cond ((looking-at " \\([^ ]+\\) \\$")
393 (vc-file-setprop file 'vc-workfile-version rev)
394 (vc-file-setprop file 'vc-locking-user
395 (buffer-substring (match-beginning 1)
396 (match-end 1)))
397 'rev-and-lock)
398 ((looking-at " *\\$")
399 (vc-file-setprop file 'vc-workfile-version rev)
400 (vc-file-setprop file 'vc-locking-user 'none)
401 'rev-and-lock)
403 (vc-file-setprop file 'vc-workfile-version rev)
404 (vc-file-setprop file 'vc-locking-user 'none)
405 'rev-and-lock))
406 (vc-file-setprop file 'vc-workfile-version rev)
407 'rev)))
408 ;; else: nothing found
409 ;; -------------------
410 (t nil))))))
412 ;;; Access functions to file properties
413 ;;; (Properties should be _set_ using vc-file-setprop, but
414 ;;; _retrieved_ only through these functions, which decide
415 ;;; if the property is already known or not. A property should
416 ;;; only be retrieved by vc-file-getprop if there is no
417 ;;; access function.)
419 ;;; properties indicating the backend
420 ;;; being used for FILE
422 (defun vc-backend-subdirectory-name (&optional file)
423 ;; Where the master and lock files for the current directory are kept
424 (symbol-name
426 (and file (vc-backend file))
427 vc-default-back-end
428 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
430 (defun vc-name (file)
431 "Return the master name of a file, nil if it is not registered."
432 (or (vc-file-getprop file 'vc-name)
433 (let ((name-and-type (vc-registered file)))
434 (if name-and-type
435 (progn
436 (vc-file-setprop file 'vc-backend (cdr name-and-type))
437 (vc-file-setprop file 'vc-name (car name-and-type)))))))
439 (defun vc-backend (file)
440 "Return the version-control type of a file, nil if it is not registered."
441 (and file
442 (or (vc-file-getprop file 'vc-backend)
443 (let ((name-and-type (vc-registered file)))
444 (if name-and-type
445 (progn
446 (vc-file-setprop file 'vc-name (car name-and-type))
447 (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
449 ;;; properties indicating the locking state
451 (defun vc-cvs-status (file)
452 ;; Return the cvs status of FILE
453 ;; (Status field in output of "cvs status")
454 (cond ((vc-file-getprop file 'vc-cvs-status))
455 (t (vc-fetch-master-properties file)
456 (vc-file-getprop file 'vc-cvs-status))))
458 (defun vc-master-locks (file)
459 ;; Return the lock entries in the master of FILE.
460 ;; Return 'none if there are no such entries, and a list
461 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
462 (cond ((vc-file-getprop file 'vc-master-locks))
463 (t (vc-fetch-master-properties file)
464 (vc-file-getprop file 'vc-master-locks))))
466 (defun vc-master-locking-user (file)
467 ;; Return the master file's idea of who is locking
468 ;; the current workfile version of FILE.
469 ;; Return 'none if it is not locked.
470 (let ((master-locks (vc-master-locks file)) lock)
471 (if (eq master-locks 'none) 'none
472 ;; search for a lock on the current workfile version
473 (setq lock (assoc (vc-workfile-version file) master-locks))
474 (cond (lock (cdr lock))
475 ('none)))))
477 (defun vc-locking-user (file)
478 ;; Return the name of the person currently holding a lock on FILE.
479 ;; Return nil if there is no such person.
480 ;; Under CVS, a file is considered locked if it has been modified since
481 ;; it was checked out. Under CVS, this will sometimes return the uid of
482 ;; the owner of the file (as a number) instead of a string.
483 ;; The property is cached. It is only looked up if it is currently nil.
484 ;; Note that, for a file that is not locked, the actual property value
485 ;; is 'none, to distinguish it from an unknown locking state. That value
486 ;; is converted to nil by this function, and returned to the caller.
487 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
488 (if locking-user
489 ;; if we already know the property, return it
490 (if (eq locking-user 'none) nil locking-user)
492 ;; otherwise, infer the property...
493 (cond
494 ;; in the CVS case, check the status
495 ((eq (vc-backend file) 'CVS)
496 (if (and (not (eq (vc-cvs-status file) 'locally-modified))
497 (not (eq (vc-cvs-status file) 'needs-merge)))
498 (vc-file-setprop file 'vc-locking-user 'none)
499 ;; The expression below should return the username of the owner
500 ;; of the file. It doesn't. It returns the username if it is
501 ;; you, or otherwise the UID of the owner of the file. The
502 ;; return value from this function is only used by
503 ;; vc-dired-reformat-line, and it does the proper thing if a UID
504 ;; is returned.
506 ;; The *proper* way to fix this would be to implement a built-in
507 ;; function in Emacs, say, (username UID), that returns the
508 ;; username of a given UID.
510 ;; The result of this hack is that vc-directory will print the
511 ;; name of the owner of the file for any files that are
512 ;; modified.
513 (let ((uid (nth 2 (file-attributes file))))
514 (if (= uid (user-uid))
515 (vc-file-setprop file 'vc-locking-user (user-login-name))
516 (vc-file-setprop file 'vc-locking-user uid)))))
518 ;; RCS case: attempt a header search. If this feature is
519 ;; disabled, vc-consult-rcs-headers always returns nil.
520 ((and (eq (vc-backend file) 'RCS)
521 (eq (vc-consult-rcs-headers file) 'rev-and-lock)))
523 ;; if the file permissions are not trusted,
524 ;; use the information from the master file
525 ((or (not vc-keep-workfiles)
526 (eq vc-mistrust-permissions 't)
527 (and vc-mistrust-permissions
528 (funcall vc-mistrust-permissions
529 (vc-backend-subdirectory-name file))))
530 (vc-file-setprop file 'vc-locking-user (vc-master-locking-user file)))
532 ;; Otherwise: Use the file permissions. (But if it turns out that the
533 ;; file is not owned by the user, use the master file.)
534 ;; This implementation assumes that any file which is under version
535 ;; control and has -rw-r--r-- is locked by its owner. This is true
536 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
537 ;; We have to be careful not to exclude files with execute bits on;
538 ;; scripts can be under version control too. Also, we must ignore the
539 ;; group-read and other-read bits, since paranoid users turn them off.
540 ;; This hack wins because calls to the somewhat expensive
541 ;; `vc-fetch-master-properties' function only have to be made if
542 ;; (a) the file is locked by someone other than the current user,
543 ;; or (b) some untoward manipulation behind vc's back has changed
544 ;; the owner or the `group' or `other' write bits.
546 (let ((attributes (file-attributes file)))
547 (cond ((string-match ".r-..-..-." (nth 8 attributes))
548 (vc-file-setprop file 'vc-locking-user 'none))
549 ((and (= (nth 2 attributes) (user-uid))
550 (string-match ".rw..-..-." (nth 8 attributes)))
551 (vc-file-setprop file 'vc-locking-user (user-login-name)))
553 (vc-file-setprop file 'vc-locking-user
554 (vc-master-locking-user file))))
556 ;; recursively call the function again,
557 ;; to convert a possible 'none value
558 (vc-locking-user file))))
560 ;;; properties to store current and recent version numbers
562 (defun vc-latest-version (file)
563 ;; Return version level of the latest version of FILE
564 (cond ((vc-file-getprop file 'vc-latest-version))
565 (t (vc-fetch-properties file)
566 (vc-file-getprop file 'vc-latest-version))))
568 (defun vc-your-latest-version (file)
569 ;; Return version level of the latest version of FILE checked in by you
570 (cond ((vc-file-getprop file 'vc-your-latest-version))
571 (t (vc-fetch-properties file)
572 (vc-file-getprop file 'vc-your-latest-version))))
574 (defun vc-top-version (file)
575 ;; Return version level of the highest revision on the default branch
576 ;; If there is no default branch, return the highest version number
577 ;; on the trunk.
578 ;; This property is defined for RCS only.
579 (cond ((vc-file-getprop file 'vc-top-version))
580 (t (vc-fetch-master-properties file)
581 (vc-file-getprop file 'vc-top-version))))
583 (defun vc-fetch-properties (file)
584 ;; Fetch vc-latest-version and vc-your-latest-version
585 ;; if that wasn't already done.
586 (cond
587 ((eq (vc-backend file) 'RCS)
588 (save-excursion
589 (set-buffer (get-buffer-create "*vc-info*"))
590 (vc-insert-file (vc-name file) "^desc")
591 (vc-parse-buffer
592 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
593 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
594 "date[ \t]+\\([0-9.]+\\);[ \t]+"
595 "author[ \t]+"
596 (regexp-quote (user-login-name)) ";") 1 2))
597 file
598 '(vc-latest-version vc-your-latest-version))
599 (if (get-buffer "*vc-info*")
600 (kill-buffer (get-buffer "*vc-info*")))))
601 (t (vc-fetch-master-properties file))
604 (defun vc-workfile-version (file)
605 ;; Return version level of the current workfile FILE
606 ;; This is attempted by first looking at the RCS keywords.
607 ;; If there are no keywords in the working file,
608 ;; vc-top-version is taken.
609 ;; Note that this property is cached, that is, it is only
610 ;; looked up if it is nil.
611 ;; For SCCS, this property is equivalent to vc-latest-version.
612 (cond ((vc-file-getprop file 'vc-workfile-version))
613 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
614 ((eq (vc-backend file) 'RCS)
615 (if (vc-consult-rcs-headers file)
616 (vc-file-getprop file 'vc-workfile-version)
617 (let ((rev (cond ((vc-top-version file))
618 ((vc-latest-version file)))))
619 (vc-file-setprop file 'vc-workfile-version rev)
620 rev)))
621 ((eq (vc-backend file) 'CVS)
622 (if (vc-consult-rcs-headers file) ;; CVS
623 (vc-file-getprop file 'vc-workfile-version)
624 (catch 'found
625 (vc-find-cvs-master (file-name-directory file)
626 (file-name-nondirectory file)))
627 (vc-file-getprop file 'vc-workfile-version)))))
629 ;;; actual version-control code starts here
631 (defun vc-registered (file)
632 (let (handler handlers)
633 (if (boundp 'file-name-handler-alist)
634 (setq handler (find-file-name-handler file 'vc-registered)))
635 (if handler
636 (funcall handler 'vc-registered file)
637 ;; Search for a master corresponding to the given file
638 (let ((dirname (or (file-name-directory file) ""))
639 (basename (file-name-nondirectory file)))
640 (catch 'found
641 (mapcar
642 (function (lambda (s)
643 (if (atom s)
644 (funcall s dirname basename)
645 (let ((trial (format (car s) dirname basename)))
646 (if (and (file-exists-p trial)
647 ;; Make sure the file we found with name
648 ;; TRIAL is not the source file itself.
649 ;; That can happen with RCS-style names
650 ;; if the file name is truncated
651 ;; (e.g. to 14 chars). See if either
652 ;; directory or attributes differ.
653 (or (not (string= dirname
654 (file-name-directory trial)))
655 (not (equal
656 (file-attributes file)
657 (file-attributes trial)))))
658 (throw 'found (cons trial (cdr s))))))))
659 vc-master-templates)
660 nil)))))
662 (defun vc-find-cvs-master (dirname basename)
663 ;; Check if DIRNAME/BASENAME is handled by CVS.
664 ;; If it is, do a (throw 'found (cons MASTER 'CVS)).
665 ;; Note: If the file is ``cvs add''ed but not yet ``cvs commit''ed
666 ;; the MASTER will not actually exist yet. The other parts of VC
667 ;; checks for this condition. This function returns nil if
668 ;; DIRNAME/BASENAME is not handled by CVS.
669 (if (and vc-handle-cvs
670 (file-directory-p (concat dirname "CVS/"))
671 (file-readable-p (concat dirname "CVS/Entries"))
672 (file-readable-p (concat dirname "CVS/Repository")))
673 (let ((bufs nil) (fold case-fold-search))
674 (unwind-protect
675 (save-excursion
676 (setq bufs (list
677 (find-file-noselect (concat dirname "CVS/Entries"))))
678 (set-buffer (car bufs))
679 (goto-char (point-min))
680 ;; make sure the file name is searched
681 ;; case-sensitively
682 (setq case-fold-search nil)
683 (cond
684 ((re-search-forward
685 (concat "^/" (regexp-quote basename) "/\\([^/]*\\)/")
686 nil t)
687 (setq case-fold-search fold) ;; restore the old value
688 ;; We found it. Store away version number, now
689 ;; that we are anyhow so close to finding it.
690 (vc-file-setprop (concat dirname basename)
691 'vc-workfile-version
692 (buffer-substring (match-beginning 1)
693 (match-end 1)))
694 (setq bufs (cons (find-file-noselect
695 (concat dirname "CVS/Repository"))
696 bufs))
697 (set-buffer (car bufs))
698 (let ((master
699 (concat (file-name-as-directory
700 (buffer-substring (point-min)
701 (1- (point-max))))
702 basename
703 ",v")))
704 (throw 'found (cons master 'CVS))))
705 (t (setq case-fold-search fold) ;; restore the old value
706 nil)))
707 (mapcar (function kill-buffer) bufs)))))
709 (defun vc-buffer-backend ()
710 "Return the version-control type of the visited file, or nil if none."
711 (if (eq vc-buffer-backend t)
712 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
713 vc-buffer-backend))
715 (defun vc-toggle-read-only (&optional verbose)
716 "Change read-only status of current buffer, perhaps via version control.
717 If the buffer is visiting a file registered with version control,
718 then check the file in or out. Otherwise, just change the read-only flag
719 of the buffer. With prefix argument, ask for version number."
720 (interactive "P")
721 (if (vc-backend (buffer-file-name))
722 (vc-next-action verbose)
723 (toggle-read-only)))
724 (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
726 (defun vc-mode-line (file &optional label)
727 "Set `vc-mode' to display type of version control for FILE.
728 The value is set in the current buffer, which should be the buffer
729 visiting FILE. Second optional arg LABEL is put in place of version
730 control system name."
731 (interactive (list buffer-file-name nil))
732 (let ((vc-type (vc-backend file)))
733 (setq vc-mode
734 (and vc-type
735 (concat " " (or label (symbol-name vc-type))
736 (and vc-display-status (vc-status file)))))
737 (and vc-type
738 (equal file (buffer-file-name))
739 ;; Make the buffer read-only if the file is not locked
740 ;; (or unchanged, in the CVS case).
741 (if (not (vc-locking-user file))
742 (setq buffer-read-only t))
743 ;; Even root shouldn't modify a registered file without
744 ;; locking it first.
745 (not buffer-read-only)
746 (zerop (user-uid))
747 (not (equal (user-login-name) (vc-locking-user file)))
748 (setq buffer-read-only t))
749 (force-mode-line-update)
750 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
751 vc-type))
753 (defun vc-status (file)
754 ;; Return string for placement in modeline by `vc-mode-line'.
755 ;; Format:
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
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.
767 ;; This function assumes that the file is registered.
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))
780 (concat ":" locker ":" rev)))))
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 ((let* ((link (file-symlink-p buffer-file-name))
798 (link-type (and link (vc-backend link))))
799 (if link-type
800 (message
801 "Warning: symbolic link to %s-controlled source file"
802 link-type))))))))
804 (add-hook 'find-file-hooks 'vc-find-file-hook)
806 ;;; more hooks, this time for file-not-found
807 (defun vc-file-not-found-hook ()
808 "When file is not found, try to check it out from RCS or SCCS.
809 Returns t if checkout was successful, nil otherwise."
810 (if (vc-backend buffer-file-name)
811 (save-excursion
812 (require 'vc)
813 (setq default-directory (file-name-directory (buffer-file-name)))
814 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
816 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
818 ;; Discard info about a file when we kill its buffer.
819 (defun vc-kill-buffer-hook ()
820 (if (stringp (buffer-file-name))
821 (progn
822 (vc-file-clearprops (buffer-file-name))
823 (kill-local-variable 'vc-buffer-backend))))
825 ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
827 ;;; Now arrange for bindings and autoloading of the main package.
828 ;;; Bindings for this have to go in the global map, as we'll often
829 ;;; want to call them from random buffers.
831 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
832 (if (not (keymapp vc-prefix-map))
833 (progn
834 (setq vc-prefix-map (make-sparse-keymap))
835 (define-key global-map "\C-xv" vc-prefix-map)
836 (define-key vc-prefix-map "a" 'vc-update-change-log)
837 (define-key vc-prefix-map "c" 'vc-cancel-version)
838 (define-key vc-prefix-map "d" 'vc-directory)
839 (define-key vc-prefix-map "h" 'vc-insert-headers)
840 (define-key vc-prefix-map "i" 'vc-register)
841 (define-key vc-prefix-map "l" 'vc-print-log)
842 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
843 (define-key vc-prefix-map "s" 'vc-create-snapshot)
844 (define-key vc-prefix-map "u" 'vc-revert-buffer)
845 (define-key vc-prefix-map "v" 'vc-next-action)
846 (define-key vc-prefix-map "=" 'vc-diff)
847 (define-key vc-prefix-map "~" 'vc-version-other-window)))
849 (if (not (boundp 'vc-menu-map))
850 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
851 ;; vc-menu-map.
853 ;;(define-key vc-menu-map [show-files]
854 ;; '("Show Files under VC" . (vc-directory t)))
855 (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
856 (define-key vc-menu-map [separator1] '("----"))
857 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
858 (define-key vc-menu-map [vc-version-other-window]
859 '("Show Other Version" . vc-version-other-window))
860 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
861 (define-key vc-menu-map [vc-update-change-log]
862 '("Update ChangeLog" . vc-update-change-log))
863 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
864 (define-key vc-menu-map [separator2] '("----"))
865 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
866 (define-key vc-menu-map [vc-revert-buffer]
867 '("Revert to Last Version" . vc-revert-buffer))
868 (define-key vc-menu-map [vc-insert-header]
869 '("Insert Header" . vc-insert-headers))
870 (define-key vc-menu-map [vc-menu-check-in] '("Check In" . vc-next-action))
871 (define-key vc-menu-map [vc-check-out] '("Check Out" . vc-toggle-read-only))
872 (define-key vc-menu-map [vc-register] '("Register" . vc-register))
873 (put 'vc-rename-file 'menu-enable 'vc-mode)
874 (put 'vc-version-other-window 'menu-enable 'vc-mode)
875 (put 'vc-diff 'menu-enable 'vc-mode)
876 (put 'vc-update-change-log 'menu-enable
877 '(eq (vc-buffer-backend) 'RCS))
878 (put 'vc-print-log 'menu-enable 'vc-mode)
879 (put 'vc-cancel-version 'menu-enable 'vc-mode)
880 (put 'vc-revert-buffer 'menu-enable 'vc-mode)
881 (put 'vc-insert-headers 'menu-enable 'vc-mode)
882 (put 'vc-next-action 'menu-enable '(and vc-mode (not buffer-read-only)))
883 (put 'vc-toggle-read-only 'menu-enable '(and vc-mode buffer-read-only))
884 (put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
887 (provide 'vc-hooks)
889 ;;; vc-hooks.el ends here