Entry that should have been made last month.
[emacs.git] / lisp / vc-hooks.el
blob510dca6b12b638203d06c4de99f6c274300f0d5b
1 ;;; vc-hooks.el --- resident support for version-control
3 ;; Copyright (C) 1992,93,94,95,96,98,99,2000,2003
4 ;; Free Software Foundation, Inc.
6 ;; Author: FSF (see vc.el for full credits)
7 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; $Id: vc-hooks.el,v 1.151 2003/05/31 17:43:28 monnier Exp $
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
30 ;; This is the always-loaded portion of VC. It takes care of
31 ;; VC-related activities that are done when you visit a file, so that
32 ;; vc.el itself is loaded only when you use a VC command. See the
33 ;; commentary of vc.el.
35 ;;; Code:
37 (eval-when-compile
38 (require 'cl))
40 ;; Customization Variables (the rest is in vc.el)
42 (defvar vc-ignore-vc-files nil)
43 (make-obsolete-variable 'vc-ignore-vc-files 'vc-handled-backends)
44 (defvar vc-master-templates ())
45 (make-obsolete-variable 'vc-master-templates 'vc-BACKEND-master-templates)
46 (defvar vc-header-alist ())
47 (make-obsolete-variable 'vc-header-alist 'vc-BACKEND-header)
49 (defcustom vc-handled-backends '(RCS CVS SVN MCVS SCCS)
50 "*List of version control backends for which VC will be used.
51 Entries in this list will be tried in order to determine whether a
52 file is under that sort of version control.
53 Removing an entry from the list prevents VC from being activated
54 when visiting a file managed by that backend.
55 An empty list disables VC altogether."
56 :type '(repeat symbol)
57 :version "21.1"
58 :group 'vc)
60 (defcustom vc-path
61 (if (file-directory-p "/usr/sccs")
62 '("/usr/sccs")
63 nil)
64 "*List of extra directories to search for version control commands."
65 :type '(repeat directory)
66 :group 'vc)
68 (defcustom vc-make-backup-files nil
69 "*If non-nil, backups of registered files are made as with other files.
70 If nil (the default), files covered by version control don't get backups."
71 :type 'boolean
72 :group 'vc
73 :group 'backup)
75 (defcustom vc-follow-symlinks 'ask
76 "*What to do if visiting a symbolic link to a file under version control.
77 Editing such a file through the link bypasses the version control system,
78 which is dangerous and probably not what you want.
80 If this variable is t, VC follows the link and visits the real file,
81 telling you about it in the echo area. If it is `ask', VC asks for
82 confirmation whether it should follow the link. If nil, the link is
83 visited and a warning displayed."
84 :type '(choice (const :tag "Ask for confirmation" ask)
85 (const :tag "Visit link and warn" nil)
86 (const :tag "Follow link" t))
87 :group 'vc)
89 (defcustom vc-display-status t
90 "*If non-nil, display revision number and lock status in modeline.
91 Otherwise, not displayed."
92 :type 'boolean
93 :group 'vc)
96 (defcustom vc-consult-headers t
97 "*If non-nil, identify work files by searching for version headers."
98 :type 'boolean
99 :group 'vc)
101 (defcustom vc-keep-workfiles t
102 "*If non-nil, don't delete working files after registering changes.
103 If the back-end is CVS, workfiles are always kept, regardless of the
104 value of this flag."
105 :type 'boolean
106 :group 'vc)
108 (defcustom vc-mistrust-permissions nil
109 "*If non-nil, don't assume permissions/ownership track version-control status.
110 If nil, do rely on the permissions.
111 See also variable `vc-consult-headers'."
112 :type 'boolean
113 :group 'vc)
115 (defun vc-mistrust-permissions (file)
116 "Internal access function to variable `vc-mistrust-permissions' for FILE."
117 (or (eq vc-mistrust-permissions 't)
118 (and vc-mistrust-permissions
119 (funcall vc-mistrust-permissions
120 (vc-backend-subdirectory-name file)))))
122 ;;; This is handled specially now.
123 ;; Tell Emacs about this new kind of minor mode
124 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
126 (make-variable-buffer-local 'vc-mode)
127 (put 'vc-mode 'permanent-local t)
129 (defun vc-mode (&optional arg)
130 ;; Dummy function for C-h m
131 "Version Control minor mode.
132 This minor mode is automatically activated whenever you visit a file under
133 control of one of the revision control systems in `vc-handled-backends'.
134 VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
135 \\{vc-prefix-map}")
137 (defmacro vc-error-occurred (&rest body)
138 `(condition-case nil (progn ,@body nil) (error t)))
140 ;; We need a notion of per-file properties because the version
141 ;; control state of a file is expensive to derive --- we compute
142 ;; them when the file is initially found, keep them up to date
143 ;; during any subsequent VC operations, and forget them when
144 ;; the buffer is killed.
146 (defvar vc-file-prop-obarray (make-vector 17 0)
147 "Obarray for per-file properties.")
149 (defvar vc-touched-properties nil)
151 (defun vc-file-setprop (file property value)
152 "Set per-file VC PROPERTY for FILE to VALUE."
153 (if (and vc-touched-properties
154 (not (memq property vc-touched-properties)))
155 (setq vc-touched-properties (append (list property)
156 vc-touched-properties)))
157 (put (intern file vc-file-prop-obarray) property value))
159 (defun vc-file-getprop (file property)
160 "Get per-file VC PROPERTY for FILE."
161 (get (intern file vc-file-prop-obarray) property))
163 (defun vc-file-clearprops (file)
164 "Clear all VC properties of FILE."
165 (setplist (intern file vc-file-prop-obarray) nil))
168 ;; We keep properties on each symbol naming a backend as follows:
169 ;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
171 (defun vc-make-backend-sym (backend sym)
172 "Return BACKEND-specific version of VC symbol SYM."
173 (intern (concat "vc-" (downcase (symbol-name backend))
174 "-" (symbol-name sym))))
176 (defun vc-find-backend-function (backend fun)
177 "Return BACKEND-specific implementation of FUN.
178 If there is no such implementation, return the default implementation;
179 if that doesn't exist either, return nil."
180 (let ((f (vc-make-backend-sym backend fun)))
181 (if (fboundp f) f
182 ;; Load vc-BACKEND.el if needed.
183 (require (intern (concat "vc-" (downcase (symbol-name backend)))))
184 (if (fboundp f) f
185 (let ((def (vc-make-backend-sym 'default fun)))
186 (if (fboundp def) (cons def backend) nil))))))
188 (defun vc-call-backend (backend function-name &rest args)
189 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
190 Calls
192 (apply 'vc-BACKEND-FUN ARGS)
194 if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
195 and else calls
197 (apply 'vc-default-FUN BACKEND ARGS)
199 It is usually called via the `vc-call' macro."
200 (let ((f (assoc function-name (get backend 'vc-functions))))
201 (if f (setq f (cdr f))
202 (setq f (vc-find-backend-function backend function-name))
203 (push (cons function-name f) (get backend 'vc-functions)))
204 (cond
205 ((null f)
206 (error "Sorry, %s is not implemented for %s" function-name backend))
207 ((consp f) (apply (car f) (cdr f) args))
208 (t (apply f args)))))
210 (defmacro vc-call (fun file &rest args)
211 ;; BEWARE!! `file' is evaluated twice!!
212 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
215 (defsubst vc-parse-buffer (pattern i)
216 "Find PATTERN in the current buffer and return its Ith submatch."
217 (goto-char (point-min))
218 (if (re-search-forward pattern nil t)
219 (match-string i)))
221 (defun vc-insert-file (file &optional limit blocksize)
222 "Insert the contents of FILE into the current buffer.
224 Optional argument LIMIT is a regexp. If present, the file is inserted
225 in chunks of size BLOCKSIZE (default 8 kByte), until the first
226 occurrence of LIMIT is found. Anything from the start of that occurrence
227 to the end of the buffer is then deleted. The function returns
228 non-nil if FILE exists and its contents were successfully inserted."
229 (erase-buffer)
230 (when (file-exists-p file)
231 (if (not limit)
232 (insert-file-contents file)
233 (if (not blocksize) (setq blocksize 8192))
234 (let ((filepos 0))
235 (while
236 (and (< 0 (cadr (insert-file-contents
237 file nil filepos (incf filepos blocksize))))
238 (progn (beginning-of-line)
239 (let ((pos (re-search-forward limit nil 'move)))
240 (if pos (delete-region (match-beginning 0)
241 (point-max)))
242 (not pos)))))))
243 (set-buffer-modified-p nil)
246 ;; Access functions to file properties
247 ;; (Properties should be _set_ using vc-file-setprop, but
248 ;; _retrieved_ only through these functions, which decide
249 ;; if the property is already known or not. A property should
250 ;; only be retrieved by vc-file-getprop if there is no
251 ;; access function.)
253 ;; properties indicating the backend being used for FILE
255 (defun vc-registered (file)
256 "Return non-nil if FILE is registered in a version control system.
258 This function performs the check each time it is called. To rely
259 on the result of a previous call, use `vc-backend' instead. If the
260 file was previously registered under a certain backend, then that
261 backend is tried first."
262 (let (handler)
263 (if (boundp 'file-name-handler-alist)
264 (setq handler (find-file-name-handler file 'vc-registered)))
265 (if handler
266 ;; handler should set vc-backend and return t if registered
267 (funcall handler 'vc-registered file)
268 ;; There is no file name handler.
269 ;; Try vc-BACKEND-registered for each handled BACKEND.
270 (catch 'found
271 (let ((backend (vc-file-getprop file 'vc-backend)))
272 (mapcar
273 (lambda (b)
274 (and (vc-call-backend b 'registered file)
275 (vc-file-setprop file 'vc-backend b)
276 (throw 'found t)))
277 (if (or (not backend) (eq backend 'none))
278 vc-handled-backends
279 (cons backend vc-handled-backends))))
280 ;; File is not registered.
281 (vc-file-setprop file 'vc-backend 'none)
282 nil))))
284 (defun vc-backend (file)
285 "Return the version control type of FILE, nil if it is not registered."
286 ;; `file' can be nil in several places (typically due to the use of
287 ;; code like (vc-backend buffer-file-name)).
288 (when (stringp file)
289 (let ((property (vc-file-getprop file 'vc-backend)))
290 ;; Note that internally, Emacs remembers unregistered
291 ;; files by setting the property to `none'.
292 (cond ((eq property 'none) nil)
293 (property)
294 ;; vc-registered sets the vc-backend property
295 (t (if (vc-registered file)
296 (vc-file-getprop file 'vc-backend)
297 nil))))))
299 (defun vc-backend-subdirectory-name (file)
300 "Return where the master and lock FILEs for the current directory are kept."
301 (symbol-name (vc-backend file)))
303 (defun vc-name (file)
304 "Return the master name of FILE.
305 If the file is not registered, or the master name is not known, return nil."
306 ;; TODO: This should ultimately become obsolete, at least up here
307 ;; in vc-hooks.
308 (or (vc-file-getprop file 'vc-name)
309 ;; force computation of the property by calling
310 ;; vc-BACKEND-registered explicitly
311 (if (and (vc-backend file)
312 (vc-call-backend (vc-backend file) 'registered file))
313 (vc-file-getprop file 'vc-name))))
315 (defun vc-checkout-model (file)
316 "Indicate how FILE is checked out.
318 If FILE is not registered, this function always returns nil.
319 For registered files, the possible values are:
321 'implicit FILE is always writeable, and checked out `implicitly'
322 when the user saves the first changes to the file.
324 'locking FILE is read-only if up-to-date; user must type
325 \\[vc-next-action] before editing. Strict locking
326 is assumed.
328 'announce FILE is read-only if up-to-date; user must type
329 \\[vc-next-action] before editing. But other users
330 may be editing at the same time."
331 (or (vc-file-getprop file 'vc-checkout-model)
332 (if (vc-backend file)
333 (vc-file-setprop file 'vc-checkout-model
334 (vc-call checkout-model file)))))
336 (defun vc-user-login-name (&optional uid)
337 "Return the name under which the user is logged in, as a string.
338 \(With optional argument UID, return the name of that user.)
339 This function does the same as function `user-login-name', but unlike
340 that, it never returns nil. If a UID cannot be resolved, that
341 UID is returned as a string."
342 (or (user-login-name uid)
343 (number-to-string (or uid (user-uid)))))
345 (defun vc-state (file)
346 "Return the version control state of FILE.
348 If FILE is not registered, this function always returns nil.
349 For registered files, the value returned is one of:
351 'up-to-date The working file is unmodified with respect to the
352 latest version on the current branch, and not locked.
354 'edited The working file has been edited by the user. If
355 locking is used for the file, this state means that
356 the current version is locked by the calling user.
358 USER The current version of the working file is locked by
359 some other USER (a string).
361 'needs-patch The file has not been edited by the user, but there is
362 a more recent version on the current branch stored
363 in the master file.
365 'needs-merge The file has been edited by the user, and there is also
366 a more recent version on the current branch stored in
367 the master file. This state can only occur if locking
368 is not used for the file.
370 'unlocked-changes The current version of the working file is not locked,
371 but the working file has been changed with respect
372 to that version. This state can only occur for files
373 with locking; it represents an erroneous condition that
374 should be resolved by the user (vc-next-action will
375 prompt the user to do it)."
376 ;; FIXME: New (sub)states needed (?):
377 ;; - `added' (i.e. `edited' but with no base version yet,
378 ;; typically represented by vc-workfile-version = "0")
379 ;; - `conflict' (i.e. `edited' with conflict markers)
380 ;; - `removed'
381 ;; - `copied' and `moved' (might be handled by `removed' and `added')
382 (or (vc-file-getprop file 'vc-state)
383 (if (vc-backend file)
384 (vc-file-setprop file 'vc-state
385 (vc-call state-heuristic file)))))
387 (defsubst vc-up-to-date-p (file)
388 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
389 (eq (vc-state file) 'up-to-date))
391 (defun vc-default-state-heuristic (backend file)
392 "Default implementation of vc-state-heuristic.
393 It simply calls the real state computation function `vc-BACKEND-state'
394 and does not employ any heuristic at all."
395 (vc-call-backend backend 'state file))
397 (defun vc-workfile-unchanged-p (file)
398 "Return non-nil if FILE has not changed since the last checkout."
399 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
400 (lastmod (nth 5 (file-attributes file))))
401 (if checkout-time
402 (equal checkout-time lastmod)
403 (let ((unchanged (vc-call workfile-unchanged-p file)))
404 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
405 unchanged))))
407 (defun vc-default-workfile-unchanged-p (backend file)
408 "Check if FILE is unchanged by diffing against the master version.
409 Return non-nil if FILE is unchanged."
410 ;; If rev1 is nil, `diff' uses the current workfile version.
411 (zerop (vc-call diff file)))
413 (defun vc-workfile-version (file)
414 "Return the version level of the current workfile FILE.
415 If FILE is not registered, this function always returns nil."
416 (or (vc-file-getprop file 'vc-workfile-version)
417 (if (vc-backend file)
418 (vc-file-setprop file 'vc-workfile-version
419 (vc-call workfile-version file)))))
421 (defun vc-default-registered (backend file)
422 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
423 (let ((sym (vc-make-backend-sym backend 'master-templates)))
424 (unless (get backend 'vc-templates-grabbed)
425 (put backend 'vc-templates-grabbed t)
426 (set sym (append (delq nil
427 (mapcar
428 (lambda (template)
429 (and (consp template)
430 (eq (cdr template) backend)
431 (car template)))
432 vc-master-templates))
433 (symbol-value sym))))
434 (let ((result (vc-check-master-templates file (symbol-value sym))))
435 (if (stringp result)
436 (vc-file-setprop file 'vc-name result)
437 nil)))) ; Not registered
439 (defun vc-possible-master (s dirname basename)
440 (cond
441 ((stringp s) (format s dirname basename))
442 ((functionp s)
443 ;; The template is a function to invoke. If the
444 ;; function returns non-nil, that means it has found a
445 ;; master. For backward compatibility, we also handle
446 ;; the case that the function throws a 'found atom
447 ;; and a pair (cons MASTER-FILE BACKEND).
448 (let ((result (catch 'found (funcall s dirname basename))))
449 (if (consp result) (car result) result)))))
451 (defun vc-check-master-templates (file templates)
452 "Return non-nil if there is a master corresponding to FILE.
454 TEMPLATES is a list of strings or functions. If an element is a
455 string, it must be a control string as required by `format', with two
456 string placeholders, such as \"%sRCS/%s,v\". The directory part of
457 FILE is substituted for the first placeholder, the basename of FILE
458 for the second. If a file with the resulting name exists, it is taken
459 as the master of FILE, and returned.
461 If an element of TEMPLATES is a function, it is called with the
462 directory part and the basename of FILE as arguments. It should
463 return non-nil if it finds a master; that value is then returned by
464 this function."
465 (let ((dirname (or (file-name-directory file) ""))
466 (basename (file-name-nondirectory file)))
467 (catch 'found
468 (mapcar
469 (lambda (s)
470 (let ((trial (vc-possible-master s dirname basename)))
471 (if (and trial (file-exists-p trial)
472 ;; Make sure the file we found with name
473 ;; TRIAL is not the source file itself.
474 ;; That can happen with RCS-style names if
475 ;; the file name is truncated (e.g. to 14
476 ;; chars). See if either directory or
477 ;; attributes differ.
478 (or (not (string= dirname
479 (file-name-directory trial)))
480 (not (equal (file-attributes file)
481 (file-attributes trial)))))
482 (throw 'found trial))))
483 templates))))
485 (defun vc-toggle-read-only (&optional verbose)
486 "Change read-only status of current buffer, perhaps via version control.
488 If the buffer is visiting a file registered with version control,
489 then check the file in or out. Otherwise, just change the read-only flag
490 of the buffer.
491 With prefix argument, ask for version number to check in or check out.
492 Check-out of a specified version number does not lock the file;
493 to do that, use this command a second time with no argument.
495 If you bind this function to \\[toggle-read-only], then Emacs checks files
496 in or out whenever you toggle the read-only flag."
497 (interactive "P")
498 (if (or (and (boundp 'vc-dired-mode) vc-dired-mode)
499 ;; use boundp because vc.el might not be loaded
500 (vc-backend buffer-file-name))
501 (vc-next-action verbose)
502 (toggle-read-only)))
504 (defun vc-default-make-version-backups-p (backend file)
505 "Return non-nil if unmodified versions should be backed up locally.
506 The default is to switch off this feature."
507 nil)
509 (defun vc-version-backup-file-name (file &optional rev manual regexp)
510 "Return a backup file name for REV or the current version of FILE.
511 If MANUAL is non-nil it means that a name for backups created by
512 the user should be returned; if REGEXP is non-nil that means to return
513 a regexp for matching all such backup files, regardless of the version."
514 (if regexp
515 (concat (regexp-quote (file-name-nondirectory file))
516 "\\.~[0-9.]+" (unless manual "\\.") "~")
517 (expand-file-name (concat (file-name-nondirectory file)
518 ".~" (or rev (vc-workfile-version file))
519 (unless manual ".") "~")
520 (file-name-directory file))))
522 (defun vc-delete-automatic-version-backups (file)
523 "Delete all existing automatic version backups for FILE."
524 (condition-case nil
525 (mapcar
526 'delete-file
527 (directory-files (or (file-name-directory file) default-directory) t
528 (vc-version-backup-file-name file nil nil t)))
529 ;; Don't fail when the directory doesn't exist.
530 (file-error nil)))
532 (defun vc-make-version-backup (file)
533 "Make a backup copy of FILE, which is assumed in sync with the repository.
534 Before doing that, check if there are any old backups and get rid of them."
535 (unless (and (fboundp 'msdos-long-file-names)
536 (not (msdos-long-file-names)))
537 (vc-delete-automatic-version-backups file)
538 (copy-file file (vc-version-backup-file-name file)
539 nil 'keep-date)))
541 (defun vc-before-save ()
542 "Function to be called by `basic-save-buffer' (in files.el)."
543 ;; If the file on disk is still in sync with the repository,
544 ;; and version backups should be made, copy the file to
545 ;; another name. This enables local diffs and local reverting.
546 (let ((file buffer-file-name))
547 (and (vc-backend file)
548 (vc-up-to-date-p file)
549 (eq (vc-checkout-model file) 'implicit)
550 (vc-call make-version-backups-p file)
551 (vc-make-version-backup file))))
553 (defun vc-after-save ()
554 "Function to be called by `basic-save-buffer' (in files.el)."
555 ;; If the file in the current buffer is under version control,
556 ;; up-to-date, and locking is not used for the file, set
557 ;; the state to 'edited and redisplay the mode line.
558 (let ((file buffer-file-name))
559 (and (vc-backend file)
560 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
561 (nth 5 (file-attributes file)))
562 ;; File has been saved in the same second in which
563 ;; it was checked out. Clear the checkout-time
564 ;; to avoid confusion.
565 (vc-file-setprop file 'vc-checkout-time nil))
567 (vc-up-to-date-p file)
568 (eq (vc-checkout-model file) 'implicit)
569 (vc-file-setprop file 'vc-state 'edited)
570 (vc-mode-line file)
571 (if (featurep 'vc)
572 ;; If VC is not loaded, then there can't be
573 ;; any VC Dired buffer to synchronize.
574 (vc-dired-resynch-file file)))))
576 (defun vc-mode-line (file)
577 "Set `vc-mode' to display type of version control for FILE.
578 The value is set in the current buffer, which should be the buffer
579 visiting FILE."
580 (interactive (list buffer-file-name))
581 (let ((backend (vc-backend file)))
582 (if (not backend)
583 (setq vc-mode nil)
584 (setq vc-mode (concat " " (if vc-display-status
585 (vc-call mode-line-string file)
586 (symbol-name backend))))
587 ;; If the file is locked by some other user, make
588 ;; the buffer read-only. Like this, even root
589 ;; cannot modify a file that someone else has locked.
590 (and (equal file buffer-file-name)
591 (stringp (vc-state file))
592 (setq buffer-read-only t))
593 ;; If the user is root, and the file is not owner-writable,
594 ;; then pretend that we can't write it
595 ;; even though we can (because root can write anything).
596 ;; This way, even root cannot modify a file that isn't locked.
597 (and (equal file buffer-file-name)
598 (not buffer-read-only)
599 (zerop (user-real-uid))
600 (zerop (logand (file-modes buffer-file-name) 128))
601 (setq buffer-read-only t)))
602 (force-mode-line-update)
603 backend))
605 (defun vc-default-mode-line-string (backend file)
606 "Return string for placement in modeline by `vc-mode-line' for FILE.
607 Format:
609 \"BACKEND-REV\" if the file is up-to-date
610 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
611 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
613 This function assumes that the file is registered."
614 (setq backend (symbol-name backend))
615 (let ((state (vc-state file))
616 (rev (vc-workfile-version file)))
617 (cond ((or (eq state 'up-to-date)
618 (eq state 'needs-patch))
619 (concat backend "-" rev))
620 ((stringp state)
621 (concat backend ":" state ":" rev))
623 ;; Not just for the 'edited state, but also a fallback
624 ;; for all other states. Think about different symbols
625 ;; for 'needs-patch and 'needs-merge.
626 (concat backend ":" rev)))))
628 (defun vc-follow-link ()
629 "If current buffer visits a symbolic link, visit the real file.
630 If the real file is already visited in another buffer, make that buffer
631 current, and kill the buffer that visits the link."
632 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
633 (true-buffer (find-buffer-visiting truename))
634 (this-buffer (current-buffer)))
635 (if (eq true-buffer this-buffer)
636 (progn
637 (kill-buffer this-buffer)
638 ;; In principle, we could do something like set-visited-file-name.
639 ;; However, it can't be exactly the same as set-visited-file-name.
640 ;; I'm not going to work out the details right now. -- rms.
641 (set-buffer (find-file-noselect truename)))
642 (set-buffer true-buffer)
643 (kill-buffer this-buffer))))
645 (defun vc-find-file-hook ()
646 "Function for `find-file-hook' activating VC mode if appropriate."
647 ;; Recompute whether file is version controlled,
648 ;; if user has killed the buffer and revisited.
649 (if vc-mode
650 (setq vc-mode nil))
651 (when buffer-file-name
652 (vc-file-clearprops buffer-file-name)
653 (cond
654 ((vc-backend buffer-file-name)
655 ;; Compute the state and put it in the modeline.
656 (vc-mode-line buffer-file-name)
657 (unless vc-make-backup-files
658 ;; Use this variable, not make-backup-files,
659 ;; because this is for things that depend on the file name.
660 (set (make-local-variable 'backup-inhibited) t)))
661 ((let* ((link (file-symlink-p buffer-file-name))
662 (link-type (and link (vc-backend (file-chase-links link)))))
663 (cond ((not link-type) nil) ;Nothing to do.
664 ((eq vc-follow-symlinks nil)
665 (message
666 "Warning: symbolic link to %s-controlled source file" link-type))
667 ((or (not (eq vc-follow-symlinks 'ask))
668 ;; If we already visited this file by following
669 ;; the link, don't ask again if we try to visit
670 ;; it again. GUD does that, and repeated questions
671 ;; are painful.
672 (get-file-buffer
673 (abbreviate-file-name
674 (file-chase-links buffer-file-name))))
676 (vc-follow-link)
677 (message "Followed link to %s" buffer-file-name)
678 (vc-find-file-hook))
680 (if (yes-or-no-p (format
681 "Symbolic link to %s-controlled source file; follow link? " link-type))
682 (progn (vc-follow-link)
683 (message "Followed link to %s" buffer-file-name)
684 (vc-find-file-hook))
685 (message
686 "Warning: editing through the link bypasses version control")
687 ))))))))
689 (add-hook 'find-file-hook 'vc-find-file-hook)
691 ;; more hooks, this time for file-not-found
692 (defun vc-file-not-found-hook ()
693 "When file is not found, try to check it out from version control.
694 Returns t if checkout was successful, nil otherwise.
695 Used in `find-file-not-found-hook'."
696 ;; When a file does not exist, ignore cached info about it
697 ;; from a previous visit.
698 (vc-file-clearprops buffer-file-name)
699 (if (and (vc-backend buffer-file-name)
700 (yes-or-no-p
701 (format "File %s was lost; check out from version control? "
702 (file-name-nondirectory buffer-file-name))))
703 (save-excursion
704 (require 'vc)
705 (setq default-directory (file-name-directory buffer-file-name))
706 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
708 (add-hook 'find-file-not-found-hook 'vc-file-not-found-hook)
710 (defun vc-kill-buffer-hook ()
711 "Discard VC info about a file when we kill its buffer."
712 (if buffer-file-name
713 (vc-file-clearprops buffer-file-name)))
715 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
717 ;; Now arrange for (autoloaded) bindings of the main package.
718 ;; Bindings for this have to go in the global map, as we'll often
719 ;; want to call them from random buffers.
721 ;; Autoloading works fine, but it prevents shortcuts from appearing
722 ;; in the menu because they don't exist yet when the menu is built.
723 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
724 (defvar vc-prefix-map
725 (let ((map (make-sparse-keymap)))
726 (define-key map "a" 'vc-update-change-log)
727 (define-key map "b" 'vc-switch-backend)
728 (define-key map "c" 'vc-cancel-version)
729 (define-key map "d" 'vc-directory)
730 (define-key map "g" 'vc-annotate)
731 (define-key map "h" 'vc-insert-headers)
732 (define-key map "i" 'vc-register)
733 (define-key map "l" 'vc-print-log)
734 (define-key map "m" 'vc-merge)
735 (define-key map "r" 'vc-retrieve-snapshot)
736 (define-key map "s" 'vc-create-snapshot)
737 (define-key map "u" 'vc-revert-buffer)
738 (define-key map "v" 'vc-next-action)
739 (define-key map "=" 'vc-diff)
740 (define-key map "~" 'vc-version-other-window)
741 map))
742 (fset 'vc-prefix-map vc-prefix-map)
743 (define-key global-map "\C-xv" 'vc-prefix-map)
745 (if (not (boundp 'vc-menu-map))
746 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
747 ;; vc-menu-map.
749 ;;(define-key vc-menu-map [show-files]
750 ;; '("Show Files under VC" . (vc-directory t)))
751 (define-key vc-menu-map [vc-retrieve-snapshot]
752 '("Retrieve Snapshot" . vc-retrieve-snapshot))
753 (define-key vc-menu-map [vc-create-snapshot]
754 '("Create Snapshot" . vc-create-snapshot))
755 (define-key vc-menu-map [vc-directory] '("VC Directory Listing" . vc-directory))
756 (define-key vc-menu-map [separator1] '("----"))
757 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
758 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
759 (define-key vc-menu-map [vc-version-other-window]
760 '("Show Other Version" . vc-version-other-window))
761 (define-key vc-menu-map [vc-diff] '("Compare with Base Version" . vc-diff))
762 (define-key vc-menu-map [vc-update-change-log]
763 '("Update ChangeLog" . vc-update-change-log))
764 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
765 (define-key vc-menu-map [separator2] '("----"))
766 (define-key vc-menu-map [vc-insert-header]
767 '("Insert Header" . vc-insert-headers))
768 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
769 (define-key vc-menu-map [vc-revert-buffer]
770 '("Revert to Base Version" . vc-revert-buffer))
771 (define-key vc-menu-map [vc-update]
772 '("Update to Latest Version" . vc-update))
773 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
774 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
776 ;; These are not correct and it's not currently clear how doing it
777 ;; better (with more complicated expressions) might slow things down
778 ;; on older systems.
780 ;;(put 'vc-rename-file 'menu-enable 'vc-mode)
781 ;;(put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
782 ;;(put 'vc-version-other-window 'menu-enable 'vc-mode)
783 ;;(put 'vc-diff 'menu-enable 'vc-mode)
784 ;;(put 'vc-update-change-log 'menu-enable
785 ;; '(member (vc-buffer-backend) '(RCS CVS)))
786 ;;(put 'vc-print-log 'menu-enable 'vc-mode)
787 ;;(put 'vc-cancel-version 'menu-enable 'vc-mode)
788 ;;(put 'vc-revert-buffer 'menu-enable 'vc-mode)
789 ;;(put 'vc-insert-headers 'menu-enable 'vc-mode)
790 ;;(put 'vc-next-action 'menu-enable 'vc-mode)
791 ;;(put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
793 (provide 'vc-hooks)
795 ;;; vc-hooks.el ends here