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