(smtpmail-via-smtp): Speciy process coding system.
[bpt/emacs.git] / lisp / vc-hooks.el
1 ;;; vc-hooks.el --- resident support for version-control
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: Andre Spiegel <spiegel@inf.fu-berlin.de>
7
8 ;; $Id: vc-hooks.el,v 1.108 1998/05/06 13:36:45 spiegel Exp rms $
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 the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This is the always-loaded portion of VC.
30 ;; It takes care VC-related activities that are done when you visit a file,
31 ;; so that vc.el itself is loaded only when you use a VC command.
32 ;; See the commentary of vc.el.
33
34 ;;; Code:
35
36 ;; Customization Variables (the rest is in vc.el)
37
38 (defcustom vc-default-back-end nil
39 "*Back-end actually used by this interface; may be SCCS or RCS.
40 The value is only computed when needed to avoid an expensive search."
41 :type '(choice (const nil) (const RCS) (const SCCS))
42 :group 'vc)
43
44 (defcustom vc-handle-cvs t
45 "*If non-nil, use VC for files managed with CVS.
46 If it is nil, don't use VC for those files."
47 :type 'boolean
48 :group 'vc)
49
50 (defcustom vc-rcsdiff-knows-brief nil
51 "*Indicates whether rcsdiff understands the --brief option.
52 The value is either `yes', `no', or nil. If it is nil, VC tries
53 to use --brief and sets this variable to remember whether it worked."
54 :type '(choice (const nil) (const yes) (const no))
55 :group 'vc)
56
57 (defcustom vc-path
58 (if (file-directory-p "/usr/sccs")
59 '("/usr/sccs")
60 nil)
61 "*List of extra directories to search for version control commands."
62 :type '(repeat directory)
63 :group 'vc)
64
65 (defcustom vc-master-templates
66 '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
67 ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS)
68 vc-find-cvs-master
69 vc-search-sccs-project-dir)
70 "*Where to look for version-control master files.
71 The first pair corresponding to a given back end is used as a template
72 when creating new masters.
73 Setting this variable to nil turns off use of VC entirely."
74 :type '(repeat sexp)
75 :group 'vc)
76
77 (defcustom vc-make-backup-files nil
78 "*If non-nil, backups of registered files are made as with other files.
79 If nil (the default), files covered by version control don't get backups."
80 :type 'boolean
81 :group 'vc)
82
83 (defcustom vc-follow-symlinks 'ask
84 "*Indicates what to do if you visit a symbolic link to a file
85 that is under version control. Editing such a file through the
86 link bypasses the version control system, which is dangerous and
87 probably not what you want.
88 If this variable is t, VC follows the link and visits the real file,
89 telling you about it in the echo area. If it is `ask', VC asks for
90 confirmation whether it should follow the link. If nil, the link is
91 visited and a warning displayed."
92 :type '(choice (const ask) (const nil) (const t))
93 :group 'vc)
94
95 (defcustom vc-display-status t
96 "*If non-nil, display revision number and lock status in modeline.
97 Otherwise, not displayed."
98 :type 'boolean
99 :group 'vc)
100
101
102 (defcustom vc-consult-headers t
103 "*If non-nil, identify work files by searching for version headers."
104 :type 'boolean
105 :group 'vc)
106
107 (defcustom vc-keep-workfiles t
108 "*If non-nil, don't delete working files after registering changes.
109 If the back-end is CVS, workfiles are always kept, regardless of the
110 value of this flag."
111 :type 'boolean
112 :group 'vc)
113
114 (defcustom vc-mistrust-permissions nil
115 "*If non-nil, don't assume that permissions and ownership track
116 version-control status. If nil, do rely on the permissions.
117 See also variable `vc-consult-headers'."
118 :type 'boolean
119 :group 'vc)
120
121 (defcustom vc-ignore-vc-files nil
122 "*If non-nil don't look for version control information when finding files.
123
124 It may be useful to set this if (say) you edit files in a directory
125 containing corresponding RCS files but don't have RCS available;
126 similarly for other version control systems."
127 :type 'boolean
128 :group 'vc
129 :version "20.3")
130
131 (defun vc-mistrust-permissions (file)
132 ;; Access function to the above.
133 (or (eq vc-mistrust-permissions 't)
134 (and vc-mistrust-permissions
135 (funcall vc-mistrust-permissions
136 (vc-backend-subdirectory-name file)))))
137
138 ;; Tell Emacs about this new kind of minor mode
139 (if (not (assoc 'vc-mode minor-mode-alist))
140 (setq minor-mode-alist (cons '(vc-mode vc-mode)
141 minor-mode-alist)))
142
143 (make-variable-buffer-local 'vc-mode)
144 (put 'vc-mode 'permanent-local t)
145
146 ;; We need a notion of per-file properties because the version
147 ;; control state of a file is expensive to derive --- we compute
148 ;; them when the file is initially found, keep them up to date
149 ;; during any subsequent VC operations, and forget them when
150 ;; the buffer is killed.
151
152 (defmacro vc-error-occurred (&rest body)
153 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
154
155 (defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
156 "Obarray for per-file properties.")
157
158 (defvar vc-buffer-backend t)
159 (make-variable-buffer-local 'vc-buffer-backend)
160
161 (defun vc-file-setprop (file property value)
162 ;; set per-file property
163 (put (intern file vc-file-prop-obarray) property value))
164
165 (defun vc-file-getprop (file property)
166 ;; get per-file property
167 (get (intern file vc-file-prop-obarray) property))
168
169 (defun vc-file-clearprops (file)
170 ;; clear all properties of a given file
171 (setplist (intern file vc-file-prop-obarray) nil))
172
173 ;;; Functions that determine property values, by examining the
174 ;;; working file, the master file, or log program output
175
176 (defun vc-match-substring (bn)
177 (buffer-substring (match-beginning bn) (match-end bn)))
178
179 (defun vc-lock-file (file)
180 ;; Generate lock file name corresponding to FILE
181 (let ((master (vc-name file)))
182 (and
183 master
184 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
185 (concat
186 (substring master (match-beginning 1) (match-end 1))
187 "p."
188 (substring master (match-beginning 2) (match-end 2))))))
189
190 (defun vc-parse-buffer (patterns &optional file properties)
191 ;; Use PATTERNS to parse information out of the current buffer.
192 ;; Each element of PATTERNS is a list of 2 to 3 elements. The first element
193 ;; is the pattern to be matched, and the second (an integer) is the
194 ;; number of the subexpression that should be returned. If there's
195 ;; a third element (also the number of a subexpression), that
196 ;; subexpression is assumed to be a date field and we want the most
197 ;; recent entry matching the template.
198 ;; If FILE and PROPERTIES are given, the latter must be a list of
199 ;; properties of the same length as PATTERNS; each property is assigned
200 ;; the corresponding value.
201 (mapcar (function (lambda (p)
202 (goto-char (point-min))
203 (cond
204 ((eq (length p) 2) ;; search for first entry
205 (let ((value nil))
206 (if (re-search-forward (car p) nil t)
207 (setq value (vc-match-substring (elt p 1))))
208 (if file
209 (progn (vc-file-setprop file (car properties) value)
210 (setq properties (cdr properties))))
211 value))
212 ((eq (length p) 3) ;; search for latest entry
213 (let ((latest-date "") (latest-val))
214 (while (re-search-forward (car p) nil t)
215 (let ((date (vc-match-substring (elt p 2))))
216 (if (string< latest-date date)
217 (progn
218 (setq latest-date date)
219 (setq latest-val
220 (vc-match-substring (elt p 1)))))))
221 (if file
222 (progn (vc-file-setprop file (car properties) latest-val)
223 (setq properties (cdr properties))))
224 latest-val)))))
225 patterns)
226 )
227
228 (defun vc-insert-file (file &optional limit blocksize)
229 ;; Insert the contents of FILE into the current buffer.
230 ;; Optional argument LIMIT is a regexp. If present,
231 ;; the file is inserted in chunks of size BLOCKSIZE
232 ;; (default 8 kByte), until the first occurrence of
233 ;; LIMIT is found. The function returns nil if FILE
234 ;; doesn't exist.
235 (erase-buffer)
236 (cond ((file-exists-p file)
237 (cond (limit
238 (if (not blocksize) (setq blocksize 8192))
239 (let (found s)
240 (while (not found)
241 (setq s (buffer-size))
242 (goto-char (1+ s))
243 (setq found
244 (or (zerop (car (cdr
245 (insert-file-contents file nil s
246 (+ s blocksize)))))
247 (progn (beginning-of-line)
248 (re-search-forward limit nil t)))))))
249 (t (insert-file-contents file)))
250 (set-buffer-modified-p nil)
251 (auto-save-mode nil)
252 t)
253 (t nil)))
254
255 (defun vc-parse-locks (file locks)
256 ;; Parse RCS or SCCS locks.
257 ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
258 ;; which is returned and stored into the property `vc-master-locks'.
259 (if (not locks)
260 (vc-file-setprop file 'vc-master-locks 'none)
261 (let ((found t) (index 0) master-locks version user)
262 (cond ((eq (vc-backend file) 'SCCS)
263 (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
264 locks index)
265 (setq version (substring locks
266 (match-beginning 1) (match-end 1)))
267 (setq user (substring locks
268 (match-beginning 2) (match-end 2)))
269 (setq master-locks (append master-locks
270 (list (cons version user))))
271 (setq index (match-end 0))))
272 ((eq (vc-backend file) 'RCS)
273 (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
274 locks index)
275 (setq version (substring locks
276 (match-beginning 2) (match-end 2)))
277 (setq user (substring locks
278 (match-beginning 1) (match-end 1)))
279 (setq master-locks (append master-locks
280 (list (cons version user))))
281 (setq index (match-end 0)))
282 (if (string-match ";[ \t\n]+strict;" locks index)
283 (vc-file-setprop file 'vc-checkout-model 'manual)
284 (vc-file-setprop file 'vc-checkout-model 'implicit))))
285 (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
286
287 (defun vc-simple-command (okstatus command file &rest args)
288 ;; Simple version of vc-do-command, for use in vc-hooks only.
289 ;; Don't switch to the *vc-info* buffer before running the
290 ;; command, because that would change its default directory
291 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
292 (erase-buffer))
293 (let ((exec-path (append vc-path exec-path)) exec-status
294 ;; Add vc-path to PATH for the execution of this command.
295 (process-environment
296 (cons (concat "PATH=" (getenv "PATH")
297 path-separator
298 (mapconcat 'identity vc-path path-separator))
299 process-environment)))
300 (setq exec-status
301 (apply 'call-process command nil "*vc-info*" nil
302 (append args (list file))))
303 (cond ((> exec-status okstatus)
304 (switch-to-buffer (get-file-buffer file))
305 (shrink-window-if-larger-than-buffer
306 (display-buffer "*vc-info*"))
307 (error "Couldn't find version control information")))
308 exec-status))
309
310 (defun vc-parse-cvs-status (&optional full)
311 ;; Parse output of "cvs status" command in the current buffer and
312 ;; set file properties accordingly. Unless FULL is t, parse only
313 ;; essential information.
314 (let (file status)
315 (goto-char (point-min))
316 (if (re-search-forward "^File: " nil t)
317 (cond
318 ((looking-at "no file") nil)
319 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
320 (setq file (concat default-directory (match-string 1)))
321 (vc-file-setprop file 'vc-backend 'CVS)
322 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
323 (setq status "Unknown")
324 (setq status (match-string 1)))
325 (if (and full
326 (re-search-forward
327 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)"
328 nil t))
329 (vc-file-setprop file 'vc-latest-version (match-string 2)))
330 (cond
331 ((string-match "Up-to-date" status)
332 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
333 (vc-file-setprop file 'vc-checkout-time
334 (nth 5 (file-attributes file))))
335 ((vc-file-setprop file 'vc-cvs-status
336 (cond
337 ((string-match "Locally Modified" status) 'locally-modified)
338 ((string-match "Needs Merge" status) 'needs-merge)
339 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
340 'needs-checkout)
341 ((string-match "Unresolved Conflict" status) 'unresolved-conflict)
342 ((string-match "Locally Added" status) 'locally-added)
343 ((string-match "New file!" status) 'locally-added)
344 (t 'unknown))))))))))
345
346 (defun vc-fetch-master-properties (file)
347 ;; Fetch those properties of FILE that are stored in the master file.
348 ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
349 ;; here because that is slow.
350 ;; That gets done if/when the functions vc-latest-version
351 ;; and vc-your-latest-version get called.
352 (save-excursion
353 (cond
354 ((eq (vc-backend file) 'SCCS)
355 (set-buffer (get-buffer-create "*vc-info*"))
356 (if (vc-insert-file (vc-lock-file file))
357 (vc-parse-locks file (buffer-string))
358 (vc-file-setprop file 'vc-master-locks 'none))
359 (vc-insert-file (vc-name file) "^\001e")
360 (vc-parse-buffer
361 (list '("^\001d D \\([^ ]+\\)" 1)
362 (list (concat "^\001d D \\([^ ]+\\) .* "
363 (regexp-quote (vc-user-login-name)) " ") 1))
364 file
365 '(vc-latest-version vc-your-latest-version)))
366
367 ((eq (vc-backend file) 'RCS)
368 (set-buffer (get-buffer-create "*vc-info*"))
369 (vc-insert-file (vc-name file) "^[0-9]")
370 (vc-parse-buffer
371 (list '("^head[ \t\n]+\\([^;]+\\);" 1)
372 '("^branch[ \t\n]+\\([^;]+\\);" 1)
373 '("^locks[ \t\n]*\\([^;]*;\\([ \t\n]*strict;\\)?\\)" 1))
374 file
375 '(vc-head-version
376 vc-default-branch
377 vc-master-locks))
378 ;; determine vc-master-workfile-version: it is either the head
379 ;; of the trunk, the head of the default branch, or the
380 ;; "default branch" itself, if that is a full revision number.
381 (let ((default-branch (vc-file-getprop file 'vc-default-branch)))
382 (cond
383 ;; no default branch
384 ((or (not default-branch) (string= "" default-branch))
385 (vc-file-setprop file 'vc-master-workfile-version
386 (vc-file-getprop file 'vc-head-version)))
387 ;; default branch is actually a revision
388 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
389 default-branch)
390 (vc-file-setprop file 'vc-master-workfile-version default-branch))
391 ;; else, search for the head of the default branch
392 (t (vc-insert-file (vc-name file) "^desc")
393 (vc-parse-buffer (list (list
394 (concat "^\\("
395 (regexp-quote default-branch)
396 "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
397 file '(vc-master-workfile-version)))))
398 ;; translate the locks
399 (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
400
401 ((eq (vc-backend file) 'CVS)
402 (save-excursion
403 ;; Call "cvs status" in the right directory, passing only the
404 ;; nondirectory part of the file name -- otherwise CVS might
405 ;; silently give a wrong result.
406 (let ((default-directory (file-name-directory file)))
407 (vc-simple-command 0 "cvs" (file-name-nondirectory file) "status"))
408 (set-buffer (get-buffer "*vc-info*"))
409 (vc-parse-cvs-status t))))
410 (if (get-buffer "*vc-info*")
411 (kill-buffer (get-buffer "*vc-info*")))))
412
413 ;;; Functions that determine property values, by examining the
414 ;;; working file, the master file, or log program output
415
416 (defun vc-consult-rcs-headers (file)
417 ;; Search for RCS headers in FILE, and set properties
418 ;; accordingly. This function can be disabled by setting
419 ;; vc-consult-headers to nil.
420 ;; Returns: nil if no headers were found
421 ;; (or if the feature is disabled,
422 ;; or if there is currently no buffer
423 ;; visiting FILE)
424 ;; 'rev if a workfile revision was found
425 ;; 'rev-and-lock if revision and lock info was found
426 (cond
427 ((or (not vc-consult-headers)
428 (not (get-file-buffer file))) nil)
429 ((let (status version locking-user)
430 (save-excursion
431 (set-buffer (get-file-buffer file))
432 (goto-char (point-min))
433 (cond
434 ;; search for $Id or $Header
435 ;; -------------------------
436 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
437 ((or (and (search-forward "$Id\ : " nil t)
438 (looking-at "[^ ]+ \\([0-9.]+\\) "))
439 (and (progn (goto-char (point-min))
440 (search-forward "$Header\ : " nil t))
441 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
442 (goto-char (match-end 0))
443 ;; if found, store the revision number ...
444 (setq version (buffer-substring-no-properties (match-beginning 1)
445 (match-end 1)))
446 ;; ... and check for the locking state
447 (cond
448 ((looking-at
449 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
450 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
451 "[^ ]+ [^ ]+ ")) ; author & state
452 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
453 (cond
454 ;; unlocked revision
455 ((looking-at "\\$")
456 (setq locking-user 'none)
457 (setq status 'rev-and-lock))
458 ;; revision is locked by some user
459 ((looking-at "\\([^ ]+\\) \\$")
460 (setq locking-user
461 (buffer-substring-no-properties (match-beginning 1)
462 (match-end 1)))
463 (setq status 'rev-and-lock))
464 ;; everything else: false
465 (nil)))
466 ;; unexpected information in
467 ;; keyword string --> quit
468 (nil)))
469 ;; search for $Revision
470 ;; --------------------
471 ((re-search-forward (concat "\\$"
472 "Revision: \\([0-9.]+\\) \\$")
473 nil t)
474 ;; if found, store the revision number ...
475 (setq version (buffer-substring-no-properties (match-beginning 1)
476 (match-end 1)))
477 ;; and see if there's any lock information
478 (goto-char (point-min))
479 (if (re-search-forward (concat "\\$" "Locker:") nil t)
480 (cond ((looking-at " \\([^ ]+\\) \\$")
481 (setq locking-user (buffer-substring-no-properties
482 (match-beginning 1)
483 (match-end 1)))
484 (setq status 'rev-and-lock))
485 ((looking-at " *\\$")
486 (setq locking-user 'none)
487 (setq status 'rev-and-lock))
488 (t
489 (setq locking-user 'none)
490 (setq status 'rev-and-lock)))
491 (setq status 'rev)))
492 ;; else: nothing found
493 ;; -------------------
494 (t nil)))
495 (if status (vc-file-setprop file 'vc-workfile-version version))
496 (and (eq status 'rev-and-lock)
497 (eq (vc-backend file) 'RCS)
498 (vc-file-setprop file 'vc-locking-user locking-user)
499 ;; If the file has headers, we don't want to query the master file,
500 ;; because that would eliminate all the performance gain the headers
501 ;; brought us. We therefore use a heuristic for the checkout model
502 ;; now: If we trust the file permissions, and the file is not
503 ;; locked, then if the file is read-only the checkout model is
504 ;; `manual', otherwise `implicit'.
505 (not (vc-mistrust-permissions file))
506 (not (vc-locking-user file))
507 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
508 (vc-file-setprop file 'vc-checkout-model 'manual)
509 (vc-file-setprop file 'vc-checkout-model 'implicit)))
510 status))))
511
512 ;;; Access functions to file properties
513 ;;; (Properties should be _set_ using vc-file-setprop, but
514 ;;; _retrieved_ only through these functions, which decide
515 ;;; if the property is already known or not. A property should
516 ;;; only be retrieved by vc-file-getprop if there is no
517 ;;; access function.)
518
519 ;;; properties indicating the backend
520 ;;; being used for FILE
521
522 (defun vc-backend-subdirectory-name (&optional file)
523 ;; Where the master and lock files for the current directory are kept
524 (symbol-name
525 (or
526 (and file (vc-backend file))
527 vc-default-back-end
528 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
529
530 (defun vc-name (file)
531 "Return the master name of a file, nil if it is not registered.
532 For CVS, the full name of CVS/Entries is returned."
533 (or (vc-file-getprop file 'vc-name)
534 ;; Use the caching mechanism of vc-backend, below.
535 (if (vc-backend file)
536 (vc-file-getprop file 'vc-name))))
537
538 (defun vc-backend (file)
539 "Return the version-control type of a file, nil if it is not registered."
540 ;; Note that internally, Emacs remembers unregistered
541 ;; files by setting the property to `none'.
542 (if file
543 (let ((property (vc-file-getprop file 'vc-backend))
544 (name-and-type))
545 (cond ((eq property 'none) nil)
546 (property)
547 (t (setq name-and-type (vc-registered file))
548 (if name-and-type
549 (progn
550 (vc-file-setprop file 'vc-name (car name-and-type))
551 (vc-file-setprop file 'vc-backend (cdr name-and-type)))
552 (vc-file-setprop file 'vc-backend 'none)
553 nil))))))
554
555 (defun vc-checkout-model (file)
556 ;; Return `manual' if the user has to type C-x C-q to check out FILE.
557 ;; Return `implicit' if the file can be modified without locking it first.
558 (or
559 (vc-file-getprop file 'vc-checkout-model)
560 (cond
561 ((eq (vc-backend file) 'SCCS)
562 (vc-file-setprop file 'vc-checkout-model 'manual))
563 ((eq (vc-backend file) 'RCS)
564 (vc-consult-rcs-headers file)
565 (or (vc-file-getprop file 'vc-checkout-model)
566 (progn (vc-fetch-master-properties file)
567 (vc-file-getprop file 'vc-checkout-model))))
568 ((eq (vc-backend file) 'CVS)
569 (vc-file-setprop file 'vc-checkout-model
570 (cond
571 ((getenv "CVSREAD") 'manual)
572 ;; If the file is not writeable, this is probably because the
573 ;; file is being "watched" by other developers. Use "manual"
574 ;; checkout in this case. (If vc-mistrust-permissions was t,
575 ;; we actually shouldn't trust this, but there is no other way
576 ;; to learn this from CVS at the moment (version 1.9).)
577 ((string-match "r-..-..-." (nth 8 (file-attributes file)))
578 'manual)
579 (t 'implicit)))))))
580
581 ;;; properties indicating the locking state
582
583 (defun vc-cvs-status (file)
584 ;; Return the cvs status of FILE
585 ;; (Status field in output of "cvs status")
586 (cond ((vc-file-getprop file 'vc-cvs-status))
587 (t (vc-fetch-master-properties file)
588 (vc-file-getprop file 'vc-cvs-status))))
589
590 (defun vc-master-locks (file)
591 ;; Return the lock entries in the master of FILE.
592 ;; Return 'none if there are no such entries, and a list
593 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
594 (cond ((vc-file-getprop file 'vc-master-locks))
595 (t (vc-fetch-master-properties file)
596 (vc-file-getprop file 'vc-master-locks))))
597
598 (defun vc-master-locking-user (file)
599 ;; Return the master file's idea of who is locking
600 ;; the current workfile version of FILE.
601 ;; Return 'none if it is not locked.
602 (let ((master-locks (vc-master-locks file)) lock)
603 (if (eq master-locks 'none) 'none
604 ;; search for a lock on the current workfile version
605 (setq lock (assoc (vc-workfile-version file) master-locks))
606 (cond (lock (cdr lock))
607 ('none)))))
608
609 (defun vc-lock-from-permissions (file)
610 ;; If the permissions can be trusted for this file, determine the
611 ;; locking state from them. Returns (user-login-name), `none', or nil.
612 ;; This implementation assumes that any file which is under version
613 ;; control and has -rw-r--r-- is locked by its owner. This is true
614 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
615 ;; We have to be careful not to exclude files with execute bits on;
616 ;; scripts can be under version control too. Also, we must ignore the
617 ;; group-read and other-read bits, since paranoid users turn them off.
618 ;; This hack wins because calls to the somewhat expensive
619 ;; `vc-fetch-master-properties' function only have to be made if
620 ;; (a) the file is locked by someone other than the current user,
621 ;; or (b) some untoward manipulation behind vc's back has changed
622 ;; the owner or the `group' or `other' write bits.
623 (let ((attributes (file-attributes file)))
624 (if (not (vc-mistrust-permissions file))
625 (cond ((string-match ".r-..-..-." (nth 8 attributes))
626 (vc-file-setprop file 'vc-locking-user 'none))
627 ((and (= (nth 2 attributes) (user-uid))
628 (string-match ".rw..-..-." (nth 8 attributes)))
629 (vc-file-setprop file 'vc-locking-user (vc-user-login-name)))
630 (nil)))))
631
632 (defun vc-user-login-name (&optional uid)
633 ;; Return the name under which the user is logged in, as a string.
634 ;; (With optional argument UID, return the name of that user.)
635 ;; This function does the same as `user-login-name', but unlike
636 ;; that, it never returns nil. If a UID cannot be resolved, that
637 ;; UID is returned as a string.
638 (or (user-login-name uid)
639 (and uid (number-to-string uid))
640 (number-to-string (user-uid))))
641
642 (defun vc-file-owner (file)
643 ;; Return who owns FILE (user name, as a string).
644 (vc-user-login-name (nth 2 (file-attributes file))))
645
646 (defun vc-rcs-lock-from-diff (file)
647 ;; Diff the file against the master version. If differences are found,
648 ;; mark the file locked. This is only used for RCS with non-strict
649 ;; locking. (If "rcsdiff" doesn't understand --brief, we do a double-take
650 ;; and remember the fact for the future.)
651 (let* ((version (concat "-r" (vc-workfile-version file)))
652 (status (if (eq vc-rcsdiff-knows-brief 'no)
653 (vc-simple-command 1 "rcsdiff" file version)
654 (vc-simple-command 2 "rcsdiff" file "--brief" version))))
655 (if (eq status 2)
656 (if (not vc-rcsdiff-knows-brief)
657 (setq vc-rcsdiff-knows-brief 'no
658 status (vc-simple-command 1 "rcsdiff" file version))
659 (error "rcsdiff failed."))
660 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
661 (if (zerop status)
662 (vc-file-setprop file 'vc-locking-user 'none)
663 (vc-file-setprop file 'vc-locking-user (vc-file-owner file)))))
664
665 (defun vc-locking-user (file)
666 ;; Return the name of the person currently holding a lock on FILE.
667 ;; Return nil if there is no such person.
668 ;; Under CVS, a file is considered locked if it has been modified since
669 ;; it was checked out.
670 ;; The property is cached. It is only looked up if it is currently nil.
671 ;; Note that, for a file that is not locked, the actual property value
672 ;; is `none', to distinguish it from an unknown locking state. That value
673 ;; is converted to nil by this function, and returned to the caller.
674 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
675 (if locking-user
676 ;; if we already know the property, return it
677 (if (eq locking-user 'none) nil locking-user)
678
679 ;; otherwise, infer the property...
680 (cond
681 ((eq (vc-backend file) 'CVS)
682 (or (and (eq (vc-checkout-model file) 'manual)
683 (vc-lock-from-permissions file))
684 (and (equal (vc-file-getprop file 'vc-checkout-time)
685 (nth 5 (file-attributes file)))
686 (vc-file-setprop file 'vc-locking-user 'none))
687 (vc-file-setprop file 'vc-locking-user (vc-file-owner file))))
688
689 ((eq (vc-backend file) 'RCS)
690 (let (p-lock)
691
692 ;; Check for RCS headers first
693 (or (eq (vc-consult-rcs-headers file) 'rev-and-lock)
694
695 ;; If there are no headers, try to learn it
696 ;; from the permissions.
697 (and (setq p-lock (vc-lock-from-permissions file))
698 (if (eq p-lock 'none)
699
700 ;; If the permissions say "not locked", we know
701 ;; that the checkout model must be `manual'.
702 (vc-file-setprop file 'vc-checkout-model 'manual)
703
704 ;; If the permissions say "locked", we can only trust
705 ;; this *if* the checkout model is `manual'.
706 (eq (vc-checkout-model file) 'manual)))
707
708 ;; Otherwise, use lock information from the master file.
709 (vc-file-setprop file 'vc-locking-user
710 (vc-master-locking-user file)))
711
712 ;; Finally, if the file is not explicitly locked
713 ;; it might still be locked implicitly.
714 (and (eq (vc-file-getprop file 'vc-locking-user) 'none)
715 (eq (vc-checkout-model file) 'implicit)
716 (vc-rcs-lock-from-diff file))))
717
718 ((eq (vc-backend file) 'SCCS)
719 (or (vc-lock-from-permissions file)
720 (vc-file-setprop file 'vc-locking-user
721 (vc-master-locking-user file)))))
722
723 ;; convert a possible 'none value
724 (setq locking-user (vc-file-getprop file 'vc-locking-user))
725 (if (eq locking-user 'none) nil locking-user))))
726
727 ;;; properties to store current and recent version numbers
728
729 (defun vc-latest-version (file)
730 ;; Return version level of the latest version of FILE
731 (cond ((vc-file-getprop file 'vc-latest-version))
732 (t (vc-fetch-properties file)
733 (vc-file-getprop file 'vc-latest-version))))
734
735 (defun vc-your-latest-version (file)
736 ;; Return version level of the latest version of FILE checked in by you
737 (cond ((vc-file-getprop file 'vc-your-latest-version))
738 (t (vc-fetch-properties file)
739 (vc-file-getprop file 'vc-your-latest-version))))
740
741 (defun vc-master-workfile-version (file)
742 ;; Return the master file's idea of what is the current workfile version.
743 ;; This property is defined for RCS only.
744 (cond ((vc-file-getprop file 'vc-master-workfile-version))
745 (t (vc-fetch-master-properties file)
746 (vc-file-getprop file 'vc-master-workfile-version))))
747
748 (defun vc-fetch-properties (file)
749 ;; Fetch vc-latest-version and vc-your-latest-version
750 ;; if that wasn't already done.
751 (cond
752 ((eq (vc-backend file) 'RCS)
753 (save-excursion
754 (set-buffer (get-buffer-create "*vc-info*"))
755 (vc-insert-file (vc-name file) "^desc")
756 (vc-parse-buffer
757 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
758 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
759 "date[ \t]+\\([0-9.]+\\);[ \t]+"
760 "author[ \t]+"
761 (regexp-quote (vc-user-login-name)) ";") 1 2))
762 file
763 '(vc-latest-version vc-your-latest-version))
764 (if (get-buffer "*vc-info*")
765 (kill-buffer (get-buffer "*vc-info*")))))
766 (t (vc-fetch-master-properties file))
767 ))
768
769 (defun vc-workfile-version (file)
770 ;; Return version level of the current workfile FILE
771 ;; This is attempted by first looking at the RCS keywords.
772 ;; If there are no keywords in the working file,
773 ;; vc-master-workfile-version is taken.
774 ;; Note that this property is cached, that is, it is only
775 ;; looked up if it is nil.
776 ;; For SCCS, this property is equivalent to vc-latest-version.
777 (cond ((vc-file-getprop file 'vc-workfile-version))
778 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
779 ((eq (vc-backend file) 'RCS)
780 (if (vc-consult-rcs-headers file)
781 (vc-file-getprop file 'vc-workfile-version)
782 (let ((rev (cond ((vc-master-workfile-version file))
783 ((vc-latest-version file)))))
784 (vc-file-setprop file 'vc-workfile-version rev)
785 rev)))
786 ((eq (vc-backend file) 'CVS)
787 (if (vc-consult-rcs-headers file) ;; CVS
788 (vc-file-getprop file 'vc-workfile-version)
789 (catch 'found
790 (vc-find-cvs-master (file-name-directory file)
791 (file-name-nondirectory file)))
792 (vc-file-getprop file 'vc-workfile-version)))))
793
794 ;;; actual version-control code starts here
795
796 (defun vc-registered (file)
797 (let (handler handlers)
798 (if (boundp 'file-name-handler-alist)
799 (setq handler (find-file-name-handler file 'vc-registered)))
800 (if handler
801 (funcall handler 'vc-registered file)
802 ;; Search for a master corresponding to the given file
803 (let ((dirname (or (file-name-directory file) ""))
804 (basename (file-name-nondirectory file)))
805 (catch 'found
806 (mapcar
807 (function (lambda (s)
808 (if (atom s)
809 (funcall s dirname basename)
810 (let ((trial (format (car s) dirname basename)))
811 (if (and (file-exists-p trial)
812 ;; Make sure the file we found with name
813 ;; TRIAL is not the source file itself.
814 ;; That can happen with RCS-style names
815 ;; if the file name is truncated
816 ;; (e.g. to 14 chars). See if either
817 ;; directory or attributes differ.
818 (or (not (string= dirname
819 (file-name-directory trial)))
820 (not (equal
821 (file-attributes file)
822 (file-attributes trial)))))
823 (throw 'found (cons trial (cdr s))))))))
824 vc-master-templates)
825 nil)))))
826
827 (defun vc-sccs-project-dir ()
828 ;; Return the full pathname of the SCCS PROJECTDIR, if it exists,
829 ;; otherwise nil. The PROJECTDIR is indicated by the environment
830 ;; variable of the same name. If its value starts with a slash,
831 ;; it must be an absolute path name that points to the
832 ;; directory where SCCS history files reside. If it does not
833 ;; begin with a slash, it is taken as the name of a user,
834 ;; and history files reside in an "src" or "source" subdirectory
835 ;; of that user's home directory.
836 (let ((project-dir (getenv "PROJECTDIR")))
837 (and project-dir
838 (if (eq (elt project-dir 0) ?/)
839 (if (file-exists-p (concat project-dir "/SCCS"))
840 (concat project-dir "/SCCS/")
841 (if (file-exists-p project-dir)
842 project-dir))
843 (setq project-dir (expand-file-name (concat "~" project-dir)))
844 (let (trial)
845 (setq trial (concat project-dir "/src/SCCS"))
846 (if (file-exists-p trial)
847 (concat trial "/")
848 (setq trial (concat project-dir "/src"))
849 (if (file-exists-p trial)
850 (concat trial "/")
851 (setq trial (concat project-dir "/source/SCCS"))
852 (if (file-exists-p trial)
853 (concat trial "/")
854 (setq trial (concat project-dir "/source/"))
855 (if (file-exists-p trial)
856 (concat trial "/"))))))))))
857
858 (defun vc-search-sccs-project-dir (dirname basename)
859 ;; Check if there is a master file for BASENAME in the
860 ;; SCCS project directory. If yes, throw `found' as
861 ;; expected by vc-registered. If not, return nil.
862 (let* ((project-dir (vc-sccs-project-dir))
863 (master-file (and project-dir (concat project-dir "s." basename))))
864 (and master-file
865 (file-exists-p master-file)
866 (throw 'found (cons master-file 'SCCS)))))
867
868 (defun vc-find-cvs-master (dirname basename)
869 ;; Check if DIRNAME/BASENAME is handled by CVS.
870 ;; If it is, do a (throw 'found (cons MASTER-FILE 'CVS)).
871 ;; Note: This function throws the name of CVS/Entries
872 ;; NOT that of the RCS master file (because we wouldn't be able
873 ;; to access it under remote CVS).
874 ;; The function returns nil if DIRNAME/BASENAME is not handled by CVS.
875 (if (and vc-handle-cvs
876 (file-directory-p (concat dirname "CVS/"))
877 (file-readable-p (concat dirname "CVS/Entries")))
878 (let ((file (concat dirname basename))
879 ;; make sure that the file name is searched
880 ;; case-sensitively
881 (case-fold-search nil)
882 buffer)
883 (unwind-protect
884 (save-excursion
885 (setq buffer (set-buffer (get-buffer-create "*vc-info*")))
886 (vc-insert-file (concat dirname "CVS/Entries"))
887 (goto-char (point-min))
888 (cond
889 ;; entry for a "locally added" file (not yet committed)
890 ((re-search-forward
891 (concat "^/" (regexp-quote basename) "/0/") nil t)
892 (vc-file-setprop file 'vc-checkout-time 0)
893 (vc-file-setprop file 'vc-workfile-version "0")
894 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
895 ;; normal entry
896 ((re-search-forward
897 (concat "^/" (regexp-quote basename)
898 ;; revision
899 "/\\([^/]*\\)"
900 ;; timestamp
901 "/[A-Z][a-z][a-z]" ;; week day (irrelevant)
902 " \\([A-Z][a-z][a-z]\\)" ;; month name
903 " *\\([0-9]*\\)" ;; day of month
904 " \\([0-9]*\\):\\([0-9]*\\):\\([0-9]*\\)" ;; hms
905 " \\([0-9]*\\)" ;; year
906 ;; optional conflict field
907 "\\(+[^/]*\\)?/")
908 nil t)
909 ;; We found it. Store away version number now that we
910 ;; are anyhow so close to finding it.
911 (vc-file-setprop file
912 'vc-workfile-version
913 (match-string 1))
914 ;; If the file hasn't been modified since checkout,
915 ;; store the checkout-time.
916 (let ((mtime (nth 5 (file-attributes file)))
917 (second (string-to-number (match-string 6)))
918 (minute (string-to-number (match-string 5)))
919 (hour (string-to-number (match-string 4)))
920 (day (string-to-number (match-string 3)))
921 (year (string-to-number (match-string 7))))
922 (if (equal mtime
923 (encode-time
924 second minute hour day
925 (/ (string-match
926 (match-string 2)
927 "xxxJanFebMarAprMayJunJulAugSepOctNovDec")
928 3)
929 year 0))
930 (vc-file-setprop file 'vc-checkout-time mtime)
931 (vc-file-setprop file 'vc-checkout-time 0)))
932 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
933 ;; entry with arbitrary text as timestamp
934 ;; (this means we should consider it modified)
935 ((re-search-forward
936 (concat "^/" (regexp-quote basename)
937 ;; revision
938 "/\\([^/]*\\)"
939 ;; timestamp (arbitrary text)
940 "/[^/]*"
941 ;; optional conflict field
942 "\\(+[^/]*\\)?/")
943 nil t)
944 ;; We found it. Store away version number now that we
945 ;; are anyhow so close to finding it.
946 (vc-file-setprop file 'vc-workfile-version (match-string 1))
947 (vc-file-setprop file 'vc-checkout-time 0)
948 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
949 (t nil)))
950 (kill-buffer buffer)))))
951
952 (defun vc-buffer-backend ()
953 "Return the version-control type of the visited file, or nil if none."
954 (if (eq vc-buffer-backend t)
955 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
956 vc-buffer-backend))
957
958 (defun vc-toggle-read-only (&optional verbose)
959 "Change read-only status of current buffer, perhaps via version control.
960 If the buffer is visiting a file registered with version control,
961 then check the file in or out. Otherwise, just change the read-only flag
962 of the buffer. With prefix argument, ask for version number."
963 (interactive "P")
964 (if (or (and (boundp 'vc-dired-mode) vc-dired-mode)
965 ;; use boundp because vc.el might not be loaded
966 (vc-backend (buffer-file-name)))
967 (vc-next-action verbose)
968 (toggle-read-only)))
969 (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
970
971 (defun vc-after-save ()
972 ;; Function to be called by basic-save-buffer (in files.el).
973 ;; If the file in the current buffer is under version control,
974 ;; not locked, and the checkout model for it is `implicit',
975 ;; mark it "locked" and redisplay the mode line.
976 (let ((file (buffer-file-name)))
977 (and (vc-backend file)
978 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
979 (nth 5 (file-attributes file)))
980 ;; File has been saved in the same second in which
981 ;; it was checked out. Clear the checkout-time
982 ;; to avoid confusion.
983 (vc-file-setprop file 'vc-checkout-time nil))
984 t)
985 (not (vc-locking-user file))
986 (eq (vc-checkout-model file) 'implicit)
987 (vc-file-setprop file 'vc-locking-user (vc-user-login-name))
988 (or (and (eq (vc-backend file) 'CVS)
989 (vc-file-setprop file 'vc-cvs-status nil))
990 t)
991 (vc-mode-line file))))
992
993 (defun vc-mode-line (file &optional label)
994 "Set `vc-mode' to display type of version control for FILE.
995 The value is set in the current buffer, which should be the buffer
996 visiting FILE. Second optional arg LABEL is put in place of version
997 control system name."
998 (interactive (list buffer-file-name nil))
999 (let ((vc-type (vc-backend file)))
1000 (setq vc-mode
1001 (and vc-type
1002 (concat " " (or label (symbol-name vc-type))
1003 (and vc-display-status (vc-status file)))))
1004 ;; If the file is locked by some other user, make
1005 ;; the buffer read-only. Like this, even root
1006 ;; cannot modify a file that someone else has locked.
1007 (and vc-type
1008 (equal file (buffer-file-name))
1009 (vc-locking-user file)
1010 (not (string= (vc-user-login-name) (vc-locking-user file)))
1011 (setq buffer-read-only t))
1012 ;; If the user is root, and the file is not owner-writable,
1013 ;; then pretend that we can't write it
1014 ;; even though we can (because root can write anything).
1015 ;; This way, even root cannot modify a file that isn't locked.
1016 (and vc-type
1017 (equal file (buffer-file-name))
1018 (not buffer-read-only)
1019 (zerop (user-real-uid))
1020 (zerop (logand (file-modes (buffer-file-name)) 128))
1021 (setq buffer-read-only t))
1022 (force-mode-line-update)
1023 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
1024 vc-type))
1025
1026 (defun vc-status (file)
1027 ;; Return string for placement in modeline by `vc-mode-line'.
1028 ;; Format:
1029 ;;
1030 ;; "-REV" if the revision is not locked
1031 ;; ":REV" if the revision is locked by the user
1032 ;; ":LOCKER:REV" if the revision is locked by somebody else
1033 ;; " @@" for a CVS file that is added, but not yet committed
1034 ;;
1035 ;; In the CVS case, a "locked" working file is a
1036 ;; working file that is modified with respect to the master.
1037 ;; The file is "locked" from the moment when the user saves
1038 ;; the modified buffer.
1039 ;;
1040 ;; This function assumes that the file is registered.
1041
1042 (let ((locker (vc-locking-user file))
1043 (rev (vc-workfile-version file)))
1044 (cond ((string= "0" rev)
1045 " @@")
1046 ((not locker)
1047 (concat "-" rev))
1048 ((string= locker (vc-user-login-name))
1049 (concat ":" rev))
1050 (t
1051 (concat ":" locker ":" rev)))))
1052
1053 (defun vc-follow-link ()
1054 ;; If the current buffer visits a symbolic link, this function makes it
1055 ;; visit the real file instead. If the real file is already visited in
1056 ;; another buffer, make that buffer current, and kill the buffer
1057 ;; that visits the link.
1058 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
1059 (true-buffer (find-buffer-visiting truename))
1060 (this-buffer (current-buffer)))
1061 (if (eq true-buffer this-buffer)
1062 (progn
1063 (kill-buffer this-buffer)
1064 ;; In principle, we could do something like set-visited-file-name.
1065 ;; However, it can't be exactly the same as set-visited-file-name.
1066 ;; I'm not going to work out the details right now. -- rms.
1067 (set-buffer (find-file-noselect truename)))
1068 (set-buffer true-buffer)
1069 (kill-buffer this-buffer))))
1070
1071 ;;; install a call to the above as a find-file hook
1072 (defun vc-find-file-hook ()
1073 ;; Recompute whether file is version controlled,
1074 ;; if user has killed the buffer and revisited.
1075 (cond
1076 ((and (not vc-ignore-vc-files) buffer-file-name)
1077 (vc-file-clearprops buffer-file-name)
1078 (cond
1079 ((vc-backend buffer-file-name)
1080 (vc-mode-line buffer-file-name)
1081 (cond ((not vc-make-backup-files)
1082 ;; Use this variable, not make-backup-files,
1083 ;; because this is for things that depend on the file name.
1084 (make-local-variable 'backup-inhibited)
1085 (setq backup-inhibited t))))
1086 ((let* ((link (file-symlink-p buffer-file-name))
1087 (link-type (and link (vc-backend (file-chase-links link)))))
1088 (if link-type
1089 (cond ((eq vc-follow-symlinks nil)
1090 (message
1091 "Warning: symbolic link to %s-controlled source file" link-type))
1092 ((or (not (eq vc-follow-symlinks 'ask))
1093 ;; If we already visited this file by following
1094 ;; the link, don't ask again if we try to visit
1095 ;; it again. GUD does that, and repeated questions
1096 ;; are painful.
1097 (get-file-buffer
1098 (abbreviate-file-name (file-chase-links buffer-file-name))))
1099
1100 (vc-follow-link)
1101 (message "Followed link to %s" buffer-file-name)
1102 (vc-find-file-hook))
1103 (t
1104 (if (yes-or-no-p (format
1105 "Symbolic link to %s-controlled source file; follow link? " link-type))
1106 (progn (vc-follow-link)
1107 (message "Followed link to %s" buffer-file-name)
1108 (vc-find-file-hook))
1109 (message
1110 "Warning: editing through the link bypasses version control")
1111 ))))))))))
1112
1113 (add-hook 'find-file-hooks 'vc-find-file-hook)
1114
1115 ;;; more hooks, this time for file-not-found
1116 (defun vc-file-not-found-hook ()
1117 "When file is not found, try to check it out from RCS or SCCS.
1118 Returns t if checkout was successful, nil otherwise."
1119 (if (and (not vc-ignore-vc-files)
1120 (vc-backend buffer-file-name))
1121 (save-excursion
1122 (require 'vc)
1123 (setq default-directory (file-name-directory (buffer-file-name)))
1124 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
1125
1126 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
1127
1128 ;; Discard info about a file when we kill its buffer.
1129 (defun vc-kill-buffer-hook ()
1130 (if (stringp (buffer-file-name))
1131 (progn
1132 (vc-file-clearprops (buffer-file-name))
1133 (kill-local-variable 'vc-buffer-backend))))
1134
1135 ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
1136
1137 ;;; Now arrange for bindings and autoloading of the main package.
1138 ;;; Bindings for this have to go in the global map, as we'll often
1139 ;;; want to call them from random buffers.
1140
1141 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
1142 (if (not (keymapp vc-prefix-map))
1143 (progn
1144 (setq vc-prefix-map (make-sparse-keymap))
1145 (define-key global-map "\C-xv" vc-prefix-map)
1146 (define-key vc-prefix-map "a" 'vc-update-change-log)
1147 (define-key vc-prefix-map "c" 'vc-cancel-version)
1148 (define-key vc-prefix-map "d" 'vc-directory)
1149 (define-key vc-prefix-map "g" 'vc-annotate)
1150 (define-key vc-prefix-map "h" 'vc-insert-headers)
1151 (define-key vc-prefix-map "i" 'vc-register)
1152 (define-key vc-prefix-map "l" 'vc-print-log)
1153 (define-key vc-prefix-map "m" 'vc-merge)
1154 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
1155 (define-key vc-prefix-map "s" 'vc-create-snapshot)
1156 (define-key vc-prefix-map "u" 'vc-revert-buffer)
1157 (define-key vc-prefix-map "v" 'vc-next-action)
1158 (define-key vc-prefix-map "=" 'vc-diff)
1159 (define-key vc-prefix-map "~" 'vc-version-other-window)))
1160
1161 (if (not (boundp 'vc-menu-map))
1162 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
1163 ;; vc-menu-map.
1164 ()
1165 ;;(define-key vc-menu-map [show-files]
1166 ;; '("Show Files under VC" . (vc-directory t)))
1167 (define-key vc-menu-map [vc-retrieve-snapshot]
1168 '("Retrieve Snapshot" . vc-retrieve-snapshot))
1169 (define-key vc-menu-map [vc-create-snapshot]
1170 '("Create Snapshot" . vc-create-snapshot))
1171 (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
1172 (define-key vc-menu-map [separator1] '("----"))
1173 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
1174 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
1175 (define-key vc-menu-map [vc-version-other-window]
1176 '("Show Other Version" . vc-version-other-window))
1177 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
1178 (define-key vc-menu-map [vc-update-change-log]
1179 '("Update ChangeLog" . vc-update-change-log))
1180 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
1181 (define-key vc-menu-map [separator2] '("----"))
1182 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
1183 (define-key vc-menu-map [vc-revert-buffer]
1184 '("Revert to Last Version" . vc-revert-buffer))
1185 (define-key vc-menu-map [vc-insert-header]
1186 '("Insert Header" . vc-insert-headers))
1187 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
1188 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
1189
1190 (put 'vc-rename-file 'menu-enable 'vc-mode)
1191 (put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
1192 (put 'vc-version-other-window 'menu-enable 'vc-mode)
1193 (put 'vc-diff 'menu-enable 'vc-mode)
1194 (put 'vc-update-change-log 'menu-enable
1195 '(eq (vc-buffer-backend) 'RCS))
1196 (put 'vc-print-log 'menu-enable 'vc-mode)
1197 (put 'vc-cancel-version 'menu-enable 'vc-mode)
1198 (put 'vc-revert-buffer 'menu-enable 'vc-mode)
1199 (put 'vc-insert-headers 'menu-enable 'vc-mode)
1200 (put 'vc-next-action 'menu-enable 'vc-mode)
1201 (put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
1202
1203 (provide 'vc-hooks)
1204
1205 ;;; vc-hooks.el ends here