1 ;;; vc-hooks.el --- resident support for version-control
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: FSF (see vc.el for full credits)
7 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
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)
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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
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.
40 ;; Customization Variables (the rest is in vc.el)
42 (defvar vc-ignore-vc-files nil)
43 (make-obsolete-variable 'vc-ignore-vc-files
44 "set `vc-handled-backends' to nil to disable VC.")
46 (defvar vc-master-templates ())
47 (make-obsolete-variable 'vc-master-templates
48 "to define master templates for a given BACKEND, use
49 vc-BACKEND-master-templates. To enable or disable VC for a given
50 BACKEND, use `vc-handled-backends'.")
52 (defvar vc-header-alist ())
53 (make-obsolete-variable 'vc-header-alist 'vc-BACKEND-header)
55 (defcustom vc-ignore-dir-regexp
56 ;; Stop SMB, automounter, AFS, and DFS host lookups.
57 "\\`\\(?:[\\/][\\/]\\|/\\(?:net\\|afs\\|\\.\\\.\\.\\)/\\)\\'"
58 "Regexp matching directory names that are not under VC's control.
59 The default regexp prevents fruitless and time-consuming attempts
60 to determine the VC status in directories in which filenames are
61 interpreted as hostnames."
65 (defcustom vc-handled-backends '(RCS CVS SVN SCCS BZR HG Arch MCVS)
66 ;; BZR, HG, Arch and MCVS come last because they are per-tree rather
68 "List of version control backends for which VC will be used.
69 Entries in this list will be tried in order to determine whether a
70 file is under that sort of version control.
71 Removing an entry from the list prevents VC from being activated
72 when visiting a file managed by that backend.
73 An empty list disables VC altogether."
74 :type '(repeat symbol)
79 (if (file-directory-p "/usr/sccs")
82 "List of extra directories to search for version control commands."
83 :type '(repeat directory)
86 (defcustom vc-make-backup-files nil
87 "If non-nil, backups of registered files are made as with other files.
88 If nil (the default), files covered by version control don't get backups."
93 (defcustom vc-follow-symlinks 'ask
94 "What to do if visiting a symbolic link to a file under version control.
95 Editing such a file through the link bypasses the version control system,
96 which is dangerous and probably not what you want.
98 If this variable is t, VC follows the link and visits the real file,
99 telling you about it in the echo area. If it is `ask', VC asks for
100 confirmation whether it should follow the link. If nil, the link is
101 visited and a warning displayed."
102 :type '(choice (const :tag "Ask for confirmation" ask)
103 (const :tag "Visit link and warn" nil)
104 (const :tag "Follow link" t))
107 (defcustom vc-display-status t
108 "If non-nil, display revision number and lock status in modeline.
109 Otherwise, not displayed."
114 (defcustom vc-consult-headers t
115 "If non-nil, identify work files by searching for version headers."
119 (defcustom vc-keep-workfiles t
120 "If non-nil, don't delete working files after registering changes.
121 If the back-end is CVS, workfiles are always kept, regardless of the
126 (defcustom vc-mistrust-permissions nil
127 "If non-nil, don't assume permissions/ownership track version-control status.
128 If nil, do rely on the permissions.
129 See also variable `vc-consult-headers'."
133 (defun vc-mistrust-permissions (file)
134 "Internal access function to variable `vc-mistrust-permissions' for FILE."
135 (or (eq vc-mistrust-permissions 't)
136 (and vc-mistrust-permissions
137 (funcall vc-mistrust-permissions
138 (vc-backend-subdirectory-name file)))))
140 (defcustom vc-stay-local t
141 "Non-nil means use local operations when possible for remote repositories.
142 This avoids slow queries over the network and instead uses heuristics
143 and past information to determine the current status of a file.
145 The value can also be a regular expression or list of regular
146 expressions to match against the host name of a repository; then VC
147 only stays local for hosts that match it. Alternatively, the value
148 can be a list of regular expressions where the first element is the
149 symbol `except'; then VC always stays local except for hosts matched
150 by these regular expressions."
151 :type '(choice (const :tag "Always stay local" t)
152 (const :tag "Don't stay local" nil)
153 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
154 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
155 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
156 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
160 (defun vc-stay-local-p (file)
161 "Return non-nil if VC should stay local when handling FILE.
162 This uses the `repository-hostname' backend operation.
163 If FILE is a list of files, return non-nil if any of them
164 individually should stay local."
166 (delq nil (mapcar 'vc-stay-local-p file))
167 (let* ((backend (vc-backend file))
168 (sym (vc-make-backend-sym backend 'stay-local))
169 (stay-local (if (boundp sym) (symbol-value sym) t)))
170 (if (eq stay-local t) (setq stay-local vc-stay-local))
171 (if (symbolp stay-local) stay-local
172 (let ((dirname (if (file-directory-p file)
173 (directory-file-name file)
174 (file-name-directory file))))
176 (or (vc-file-getprop dirname 'vc-stay-local-p)
178 dirname 'vc-stay-local-p
179 (let ((hostname (vc-call-backend
180 backend 'repository-hostname dirname)))
184 (if (eq (car-safe stay-local) 'except)
185 (setq default nil stay-local (cdr stay-local)))
186 (when (consp stay-local)
188 (mapconcat 'identity stay-local "\\|")))
189 (if (if (string-match stay-local hostname)
190 default (not default))
193 ;;; This is handled specially now.
194 ;; Tell Emacs about this new kind of minor mode
195 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
197 (make-variable-buffer-local 'vc-mode)
198 (put 'vc-mode 'permanent-local t)
200 (defun vc-mode (&optional arg)
201 ;; Dummy function for C-h m
202 "Version Control minor mode.
203 This minor mode is automatically activated whenever you visit a file under
204 control of one of the revision control systems in `vc-handled-backends'.
205 VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
208 (defmacro vc-error-occurred (&rest body)
209 `(condition-case nil (progn ,@body nil) (error t)))
211 ;; We need a notion of per-file properties because the version
212 ;; control state of a file is expensive to derive --- we compute
213 ;; them when the file is initially found, keep them up to date
214 ;; during any subsequent VC operations, and forget them when
215 ;; the buffer is killed.
217 (defvar vc-file-prop-obarray (make-vector 17 0)
218 "Obarray for per-file properties.")
220 (defvar vc-touched-properties nil)
222 (defun vc-file-setprop (file property value)
223 "Set per-file VC PROPERTY for FILE to VALUE."
224 (if (and vc-touched-properties
225 (not (memq property vc-touched-properties)))
226 (setq vc-touched-properties (append (list property)
227 vc-touched-properties)))
228 (put (intern file vc-file-prop-obarray) property value))
230 (defun vc-file-getprop (file property)
231 "Get per-file VC PROPERTY for FILE."
232 (get (intern file vc-file-prop-obarray) property))
234 (defun vc-file-clearprops (file)
235 "Clear all VC properties of FILE."
236 (setplist (intern file vc-file-prop-obarray) nil))
239 ;; We keep properties on each symbol naming a backend as follows:
240 ;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
242 (defun vc-make-backend-sym (backend sym)
243 "Return BACKEND-specific version of VC symbol SYM."
244 (intern (concat "vc-" (downcase (symbol-name backend))
245 "-" (symbol-name sym))))
247 (defun vc-find-backend-function (backend fun)
248 "Return BACKEND-specific implementation of FUN.
249 If there is no such implementation, return the default implementation;
250 if that doesn't exist either, return nil."
251 (let ((f (vc-make-backend-sym backend fun)))
253 ;; Load vc-BACKEND.el if needed.
254 (require (intern (concat "vc-" (downcase (symbol-name backend)))))
256 (let ((def (vc-make-backend-sym 'default fun)))
257 (if (fboundp def) (cons def backend) nil))))))
259 (defun vc-call-backend (backend function-name &rest args)
260 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
263 (apply 'vc-BACKEND-FUN ARGS)
265 if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
268 (apply 'vc-default-FUN BACKEND ARGS)
270 It is usually called via the `vc-call' macro."
271 (let ((f (assoc function-name (get backend 'vc-functions))))
272 (if f (setq f (cdr f))
273 (setq f (vc-find-backend-function backend function-name))
274 (push (cons function-name f) (get backend 'vc-functions)))
277 (error "Sorry, %s is not implemented for %s" function-name backend))
278 ((consp f) (apply (car f) (cdr f) args))
279 (t (apply f args)))))
281 (defmacro vc-call (fun file &rest args)
282 ;; BEWARE!! `file' is evaluated twice!!
283 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
285 (defsubst vc-parse-buffer (pattern i)
286 "Find PATTERN in the current buffer and return its Ith submatch."
287 (goto-char (point-min))
288 (if (re-search-forward pattern nil t)
291 (defun vc-insert-file (file &optional limit blocksize)
292 "Insert the contents of FILE into the current buffer.
294 Optional argument LIMIT is a regexp. If present, the file is inserted
295 in chunks of size BLOCKSIZE (default 8 kByte), until the first
296 occurrence of LIMIT is found. Anything from the start of that occurrence
297 to the end of the buffer is then deleted. The function returns
298 non-nil if FILE exists and its contents were successfully inserted."
300 (when (file-exists-p file)
302 (insert-file-contents file)
303 (if (not blocksize) (setq blocksize 8192))
306 (and (< 0 (cadr (insert-file-contents
307 file nil filepos (incf filepos blocksize))))
308 (progn (beginning-of-line)
309 (let ((pos (re-search-forward limit nil 'move)))
310 (if pos (delete-region (match-beginning 0)
313 (set-buffer-modified-p nil)
316 (defun vc-find-root (file witness)
317 "Find the root of a checked out project.
318 The function walks up the directory tree from FILE looking for WITNESS.
319 If WITNESS if not found, return nil, otherwise return the root."
320 ;; Represent /home/luser/foo as ~/foo so that we don't try to look for
321 ;; witnesses in /home or in /.
322 (while (not (file-directory-p file))
323 (setq file (file-name-directory (directory-file-name file))))
324 (setq file (abbreviate-file-name file))
326 (user (nth 2 (file-attributes file))))
329 ;; As a heuristic, we stop looking up the hierarchy of
330 ;; directories as soon as we find a directory belonging
331 ;; to another user. This should save us from looking in
332 ;; things like /net and /afs. This assumes that all the
333 ;; files inside a project belong to the same user.
334 (not (equal user (nth 2 (file-attributes file))))
335 (string-match vc-ignore-dir-regexp file)))
336 (if (file-exists-p (expand-file-name witness file))
339 (setq file (file-name-directory (directory-file-name file))))
343 ;; Access functions to file properties
344 ;; (Properties should be _set_ using vc-file-setprop, but
345 ;; _retrieved_ only through these functions, which decide
346 ;; if the property is already known or not. A property should
347 ;; only be retrieved by vc-file-getprop if there is no
350 ;; properties indicating the backend being used for FILE
352 (defun vc-registered (file)
353 "Return non-nil if FILE is registered in a version control system.
355 This function performs the check each time it is called. To rely
356 on the result of a previous call, use `vc-backend' instead. If the
357 file was previously registered under a certain backend, then that
358 backend is tried first."
361 ((string-match vc-ignore-dir-regexp (file-name-directory file)) nil)
362 ((and (boundp 'file-name-handler-alist)
363 (setq handler (find-file-name-handler file 'vc-registered)))
364 ;; handler should set vc-backend and return t if registered
365 (funcall handler 'vc-registered file))
367 ;; There is no file name handler.
368 ;; Try vc-BACKEND-registered for each handled BACKEND.
370 (let ((backend (vc-file-getprop file 'vc-backend)))
373 (and (vc-call-backend b 'registered file)
374 (vc-file-setprop file 'vc-backend b)
376 (if (or (not backend) (eq backend 'none))
378 (cons backend vc-handled-backends))))
379 ;; File is not registered.
380 (vc-file-setprop file 'vc-backend 'none)
383 (defun vc-backend (file-or-list)
384 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
385 If the argument is a list, the files must all have the same back end."
386 ;; `file' can be nil in several places (typically due to the use of
387 ;; code like (vc-backend buffer-file-name)).
388 (cond ((stringp file-or-list)
389 (let ((property (vc-file-getprop file-or-list 'vc-backend)))
390 ;; Note that internally, Emacs remembers unregistered
391 ;; files by setting the property to `none'.
392 (cond ((eq property 'none) nil)
394 ;; vc-registered sets the vc-backend property
395 (t (if (vc-registered file-or-list)
396 (vc-file-getprop file-or-list 'vc-backend)
398 ((and file-or-list (listp file-or-list))
399 (vc-backend (car file-or-list)))
404 (defun vc-backend-subdirectory-name (file)
405 "Return where the master and lock FILEs for the current directory are kept."
406 (symbol-name (vc-backend file)))
408 (defun vc-name (file)
409 "Return the master name of FILE.
410 If the file is not registered, or the master name is not known, return nil."
411 ;; TODO: This should ultimately become obsolete, at least up here
413 (or (vc-file-getprop file 'vc-name)
414 ;; force computation of the property by calling
415 ;; vc-BACKEND-registered explicitly
416 (if (and (vc-backend file)
417 (vc-call-backend (vc-backend file) 'registered file))
418 (vc-file-getprop file 'vc-name))))
420 (defun vc-checkout-model (file)
421 "Indicate how FILE is checked out.
423 If FILE is not registered, this function always returns nil.
424 For registered files, the possible values are:
426 'implicit FILE is always writeable, and checked out `implicitly'
427 when the user saves the first changes to the file.
429 'locking FILE is read-only if up-to-date; user must type
430 \\[vc-next-action] before editing. Strict locking
433 'announce FILE is read-only if up-to-date; user must type
434 \\[vc-next-action] before editing. But other users
435 may be editing at the same time."
436 (or (vc-file-getprop file 'vc-checkout-model)
437 (if (vc-backend file)
438 (vc-file-setprop file 'vc-checkout-model
439 (vc-call checkout-model file)))))
441 (defun vc-user-login-name (file)
442 "Return the name under which the user accesses the given FILE."
443 (or (and (eq (string-match tramp-file-name-regexp file) 0)
444 ;; tramp case: execute "whoami" via tramp
445 (let ((default-directory (file-name-directory file)))
447 (if (not (zerop (process-file "whoami" nil t)))
448 ;; fall through if "whoami" didn't work
450 ;; remove trailing newline
451 (delete-region (1- (point-max)) (point-max))
455 ;; if user-login-name is nil, return the UID as a string
456 (number-to-string (user-uid))))
458 (defun vc-state (file)
459 "Return the version control state of FILE.
461 If FILE is not registered, this function always returns nil.
462 For registered files, the value returned is one of:
464 'up-to-date The working file is unmodified with respect to the
465 latest version on the current branch, and not locked.
467 'edited The working file has been edited by the user. If
468 locking is used for the file, this state means that
469 the current version is locked by the calling user.
471 USER The current version of the working file is locked by
472 some other USER (a string).
474 'needs-patch The file has not been edited by the user, but there is
475 a more recent version on the current branch stored
478 'needs-merge The file has been edited by the user, and there is also
479 a more recent version on the current branch stored in
480 the master file. This state can only occur if locking
481 is not used for the file.
483 'unlocked-changes The current version of the working file is not locked,
484 but the working file has been changed with respect
485 to that version. This state can only occur for files
486 with locking; it represents an erroneous condition that
487 should be resolved by the user (vc-next-action will
488 prompt the user to do it)."
489 ;; FIXME: New (sub)states needed (?):
490 ;; - `added' (i.e. `edited' but with no base version yet,
491 ;; typically represented by vc-workfile-version = "0")
492 ;; - `conflict' (i.e. `edited' with conflict markers)
494 ;; - `copied' and `moved' (might be handled by `removed' and `added')
495 (or (vc-file-getprop file 'vc-state)
496 (if (and (> (length file) 0) (vc-backend file))
497 (vc-file-setprop file 'vc-state
498 (vc-call state-heuristic file)))))
500 (defun vc-recompute-state (file)
501 "Recompute the version control state of FILE, and return it.
502 This calls the possibly expensive function vc-BACKEND-state,
503 rather than the heuristic."
504 (vc-file-setprop file 'vc-state (vc-call state file)))
506 (defsubst vc-up-to-date-p (file)
507 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
508 (eq (vc-state file) 'up-to-date))
510 (defun vc-default-state-heuristic (backend file)
511 "Default implementation of vc-state-heuristic.
512 It simply calls the real state computation function `vc-BACKEND-state'
513 and does not employ any heuristic at all."
514 (vc-call-backend backend 'state file))
516 (defun vc-workfile-unchanged-p (file)
517 "Return non-nil if FILE has not changed since the last checkout."
518 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
519 (lastmod (nth 5 (file-attributes file))))
520 (if (and checkout-time
521 ;; Tramp and Ange-FTP return this when they don't know the time.
522 (not (equal lastmod '(0 0))))
523 (equal checkout-time lastmod)
524 (let ((unchanged (vc-call workfile-unchanged-p file)))
525 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
528 (defun vc-default-workfile-unchanged-p (backend file)
529 "Check if FILE is unchanged by diffing against the master version.
530 Return non-nil if FILE is unchanged."
531 (zerop (condition-case err
532 ;; If the implementation supports it, let the output
533 ;; go to *vc*, not *vc-diff*, since this is an internal call.
534 (vc-call diff (list file) nil nil "*vc*")
535 (wrong-number-of-arguments
536 ;; If this error came from the above call to vc-BACKEND-diff,
537 ;; try again without the optional buffer argument (for
538 ;; backward compatibility). Otherwise, resignal.
539 (if (or (not (eq (cadr err)
541 (vc-find-backend-function (vc-backend file)
543 (not (eq (caddr err) 4)))
544 (signal (car err) (cdr err))
545 (vc-call diff (list file)))))))
547 (defun vc-workfile-version (file)
548 "Return the repository version from which FILE was checked out.
549 If FILE is not registered, this function always returns nil."
550 (or (vc-file-getprop file 'vc-workfile-version)
551 (if (vc-backend file)
552 (vc-file-setprop file 'vc-workfile-version
553 (vc-call workfile-version file)))))
555 (defun vc-default-registered (backend file)
556 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
557 (let ((sym (vc-make-backend-sym backend 'master-templates)))
558 (unless (get backend 'vc-templates-grabbed)
559 (put backend 'vc-templates-grabbed t)
560 (set sym (append (delq nil
563 (and (consp template)
564 (eq (cdr template) backend)
567 vc-master-templates)))
568 (symbol-value sym))))
569 (let ((result (vc-check-master-templates file (symbol-value sym))))
571 (vc-file-setprop file 'vc-name result)
572 nil)))) ; Not registered
574 (defun vc-possible-master (s dirname basename)
576 ((stringp s) (format s dirname basename))
578 ;; The template is a function to invoke. If the
579 ;; function returns non-nil, that means it has found a
580 ;; master. For backward compatibility, we also handle
581 ;; the case that the function throws a 'found atom
582 ;; and a pair (cons MASTER-FILE BACKEND).
583 (let ((result (catch 'found (funcall s dirname basename))))
584 (if (consp result) (car result) result)))))
586 (defun vc-check-master-templates (file templates)
587 "Return non-nil if there is a master corresponding to FILE.
589 TEMPLATES is a list of strings or functions. If an element is a
590 string, it must be a control string as required by `format', with two
591 string placeholders, such as \"%sRCS/%s,v\". The directory part of
592 FILE is substituted for the first placeholder, the basename of FILE
593 for the second. If a file with the resulting name exists, it is taken
594 as the master of FILE, and returned.
596 If an element of TEMPLATES is a function, it is called with the
597 directory part and the basename of FILE as arguments. It should
598 return non-nil if it finds a master; that value is then returned by
600 (let ((dirname (or (file-name-directory file) ""))
601 (basename (file-name-nondirectory file)))
605 (let ((trial (vc-possible-master s dirname basename)))
606 (if (and trial (file-exists-p trial)
607 ;; Make sure the file we found with name
608 ;; TRIAL is not the source file itself.
609 ;; That can happen with RCS-style names if
610 ;; the file name is truncated (e.g. to 14
611 ;; chars). See if either directory or
612 ;; attributes differ.
613 (or (not (string= dirname
614 (file-name-directory trial)))
615 (not (equal (file-attributes file)
616 (file-attributes trial)))))
617 (throw 'found trial))))
620 (defun vc-toggle-read-only (&optional verbose)
621 "Change read-only status of current buffer, perhaps via version control.
623 If the buffer is visiting a file registered with version control,
624 then check the file in or out. Otherwise, just change the read-only flag
626 With prefix argument, ask for version number to check in or check out.
627 Check-out of a specified version number does not lock the file;
628 to do that, use this command a second time with no argument.
630 If you bind this function to \\[toggle-read-only], then Emacs checks files
631 in or out whenever you toggle the read-only flag."
633 (if (or (and (boundp 'vc-dired-mode) vc-dired-mode)
634 ;; use boundp because vc.el might not be loaded
635 (vc-backend buffer-file-name))
636 (vc-next-action verbose)
639 (defun vc-default-make-version-backups-p (backend file)
640 "Return non-nil if unmodified versions should be backed up locally.
641 The default is to switch off this feature."
644 (defun vc-version-backup-file-name (file &optional rev manual regexp)
645 "Return a backup file name for REV or the current version of FILE.
646 If MANUAL is non-nil it means that a name for backups created by
647 the user should be returned; if REGEXP is non-nil that means to return
648 a regexp for matching all such backup files, regardless of the version."
650 (concat (regexp-quote (file-name-nondirectory file))
651 "\\.~.+" (unless manual "\\.") "~")
652 (expand-file-name (concat (file-name-nondirectory file)
653 ".~" (subst-char-in-string
654 ?/ ?_ (or rev (vc-workfile-version file)))
655 (unless manual ".") "~")
656 (file-name-directory file))))
658 (defun vc-delete-automatic-version-backups (file)
659 "Delete all existing automatic version backups for FILE."
663 (directory-files (or (file-name-directory file) default-directory) t
664 (vc-version-backup-file-name file nil nil t)))
665 ;; Don't fail when the directory doesn't exist.
668 (defun vc-make-version-backup (file)
669 "Make a backup copy of FILE, which is assumed in sync with the repository.
670 Before doing that, check if there are any old backups and get rid of them."
671 (unless (and (fboundp 'msdos-long-file-names)
672 (not (with-no-warnings (msdos-long-file-names))))
673 (vc-delete-automatic-version-backups file)
675 (copy-file file (vc-version-backup-file-name file)
677 ;; It's ok if it doesn't work (e.g. directory not writable),
678 ;; since this is just for efficiency.
681 (concat "Warning: Cannot make version backup; "
682 "diff/revert therefore not local"))))))
684 (defun vc-before-save ()
685 "Function to be called by `basic-save-buffer' (in files.el)."
686 ;; If the file on disk is still in sync with the repository,
687 ;; and version backups should be made, copy the file to
688 ;; another name. This enables local diffs and local reverting.
689 (let ((file buffer-file-name))
690 (and (vc-backend file)
691 (vc-up-to-date-p file)
692 (eq (vc-checkout-model file) 'implicit)
693 (vc-call make-version-backups-p file)
694 (vc-make-version-backup file))))
696 (defun vc-after-save ()
697 "Function to be called by `basic-save-buffer' (in files.el)."
698 ;; If the file in the current buffer is under version control,
699 ;; up-to-date, and locking is not used for the file, set
700 ;; the state to 'edited and redisplay the mode line.
701 (let ((file buffer-file-name))
702 (and (vc-backend file)
703 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
704 (nth 5 (file-attributes file)))
705 ;; File has been saved in the same second in which
706 ;; it was checked out. Clear the checkout-time
707 ;; to avoid confusion.
708 (vc-file-setprop file 'vc-checkout-time nil))
710 (vc-up-to-date-p file)
711 (eq (vc-checkout-model file) 'implicit)
712 (vc-file-setprop file 'vc-state 'edited)
715 ;; If VC is not loaded, then there can't be
716 ;; any VC Dired buffer to synchronize.
717 (vc-dired-resynch-file file)))))
719 (defconst vc-mode-line-map
720 (let ((map (make-sparse-keymap)))
721 (define-key map [mode-line down-mouse-1] 'vc-menu-map)
724 (defun vc-mode-line (file)
725 "Set `vc-mode' to display type of version control for FILE.
726 The value is set in the current buffer, which should be the buffer
728 (interactive (list buffer-file-name))
729 (let ((backend (vc-backend file)))
732 (let* ((ml-string (vc-call mode-line-string file))
733 (ml-echo (get-text-property 0 'help-echo ml-string)))
737 (if (null vc-display-status)
738 (symbol-name backend)
741 'mouse-face 'mode-line-highlight
744 (format "File under the %s version control system"
746 "\nmouse-1: Version Control menu")
747 'local-map vc-mode-line-map)))))
748 ;; If the file is locked by some other user, make
749 ;; the buffer read-only. Like this, even root
750 ;; cannot modify a file that someone else has locked.
751 (and (equal file buffer-file-name)
752 (stringp (vc-state file))
753 (setq buffer-read-only t))
754 ;; If the user is root, and the file is not owner-writable,
755 ;; then pretend that we can't write it
756 ;; even though we can (because root can write anything).
757 ;; This way, even root cannot modify a file that isn't locked.
758 (and (equal file buffer-file-name)
759 (not buffer-read-only)
760 (zerop (user-real-uid))
761 (zerop (logand (file-modes buffer-file-name) 128))
762 (setq buffer-read-only t)))
763 (force-mode-line-update)
766 (defun vc-default-mode-line-string (backend file)
767 "Return string for placement in modeline by `vc-mode-line' for FILE.
770 \"BACKEND-REV\" if the file is up-to-date
771 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
772 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
774 This function assumes that the file is registered."
775 (setq backend (symbol-name backend))
776 (let ((state (vc-state file))
778 (rev (vc-workfile-version file)))
780 (cond ((or (eq state 'up-to-date)
781 (eq state 'needs-patch))
782 (setq state-echo "Up to date file")
783 (concat backend "-" rev))
785 (setq state-echo (concat "File locked by" state))
786 (concat backend ":" state ":" rev))
788 ;; Not just for the 'edited state, but also a fallback
789 ;; for all other states. Think about different symbols
790 ;; for 'needs-patch and 'needs-merge.
791 (setq state-echo "Locally modified file")
792 (concat backend ":" rev)))
793 'help-echo (concat state-echo " under the " backend
794 " version control system"))))
796 (defun vc-follow-link ()
797 "If current buffer visits a symbolic link, visit the real file.
798 If the real file is already visited in another buffer, make that buffer
799 current, and kill the buffer that visits the link."
800 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
801 (true-buffer (find-buffer-visiting truename))
802 (this-buffer (current-buffer)))
803 (if (eq true-buffer this-buffer)
805 (kill-buffer this-buffer)
806 ;; In principle, we could do something like set-visited-file-name.
807 ;; However, it can't be exactly the same as set-visited-file-name.
808 ;; I'm not going to work out the details right now. -- rms.
809 (set-buffer (find-file-noselect truename)))
810 (set-buffer true-buffer)
811 (kill-buffer this-buffer))))
813 (defun vc-default-find-file-hook (backend)
816 (defun vc-find-file-hook ()
817 "Function for `find-file-hook' activating VC mode if appropriate."
818 ;; Recompute whether file is version controlled,
819 ;; if user has killed the buffer and revisited.
822 (when buffer-file-name
823 (vc-file-clearprops buffer-file-name)
825 ((with-demoted-errors (vc-backend buffer-file-name))
826 ;; Compute the state and put it in the modeline.
827 (vc-mode-line buffer-file-name)
828 (unless vc-make-backup-files
829 ;; Use this variable, not make-backup-files,
830 ;; because this is for things that depend on the file name.
831 (set (make-local-variable 'backup-inhibited) t))
832 ;; Let the backend setup any buffer-local things he needs.
833 (vc-call-backend (vc-backend buffer-file-name) 'find-file-hook))
834 ((let ((link-type (and (file-symlink-p buffer-file-name)
835 (vc-backend (file-chase-links buffer-file-name)))))
836 (cond ((not link-type) nil) ;Nothing to do.
837 ((eq vc-follow-symlinks nil)
839 "Warning: symbolic link to %s-controlled source file" link-type))
840 ((or (not (eq vc-follow-symlinks 'ask))
841 ;; If we already visited this file by following
842 ;; the link, don't ask again if we try to visit
843 ;; it again. GUD does that, and repeated questions
846 (abbreviate-file-name
847 (file-chase-links buffer-file-name))))
850 (message "Followed link to %s" buffer-file-name)
853 (if (yes-or-no-p (format
854 "Symbolic link to %s-controlled source file; follow link? " link-type))
855 (progn (vc-follow-link)
856 (message "Followed link to %s" buffer-file-name)
859 "Warning: editing through the link bypasses version control")
862 (add-hook 'find-file-hook 'vc-find-file-hook)
864 ;; more hooks, this time for file-not-found
865 (defun vc-file-not-found-hook ()
866 "When file is not found, try to check it out from version control.
867 Returns t if checkout was successful, nil otherwise.
868 Used in `find-file-not-found-functions'."
869 ;; When a file does not exist, ignore cached info about it
870 ;; from a previous visit.
871 (vc-file-clearprops buffer-file-name)
872 (let ((backend (vc-backend buffer-file-name)))
873 (if backend (vc-call-backend backend 'find-file-not-found-hook))))
875 (defun vc-default-find-file-not-found-hook (backend)
877 (format "File %s was lost; check out from version control? "
878 (file-name-nondirectory buffer-file-name)))
881 (setq default-directory (file-name-directory buffer-file-name))
882 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
884 (add-hook 'find-file-not-found-functions 'vc-file-not-found-hook)
886 (defun vc-kill-buffer-hook ()
887 "Discard VC info about a file when we kill its buffer."
889 (vc-file-clearprops buffer-file-name)))
891 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
893 ;; Now arrange for (autoloaded) bindings of the main package.
894 ;; Bindings for this have to go in the global map, as we'll often
895 ;; want to call them from random buffers.
897 ;; Autoloading works fine, but it prevents shortcuts from appearing
898 ;; in the menu because they don't exist yet when the menu is built.
899 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
900 (defvar vc-prefix-map
901 (let ((map (make-sparse-keymap)))
902 (define-key map "a" 'vc-update-change-log)
903 (define-key map "b" 'vc-switch-backend)
904 (define-key map "c" 'vc-rollback)
905 (define-key map "d" 'vc-directory)
906 (define-key map "g" 'vc-annotate)
907 (define-key map "h" 'vc-insert-headers)
908 (define-key map "i" 'vc-register)
909 (define-key map "l" 'vc-print-log)
910 (define-key map "m" 'vc-merge)
911 (define-key map "r" 'vc-retrieve-snapshot)
912 (define-key map "s" 'vc-create-snapshot)
913 (define-key map "u" 'vc-revert)
914 (define-key map "v" 'vc-next-action)
915 (define-key map "+" 'vc-update)
916 (define-key map "=" 'vc-diff)
917 (define-key map "~" 'vc-version-other-window)
919 (fset 'vc-prefix-map vc-prefix-map)
920 (define-key global-map "\C-xv" 'vc-prefix-map)
922 (if (not (boundp 'vc-menu-map))
923 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
926 ;;(define-key vc-menu-map [show-files]
927 ;; '("Show Files under VC" . (vc-directory t)))
928 (define-key vc-menu-map [vc-retrieve-snapshot]
929 '("Retrieve Snapshot" . vc-retrieve-snapshot))
930 (define-key vc-menu-map [vc-create-snapshot]
931 '("Create Snapshot" . vc-create-snapshot))
932 (define-key vc-menu-map [vc-directory] '("VC Directory Listing" . vc-directory))
933 (define-key vc-menu-map [separator1] '("----"))
934 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
935 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
936 (define-key vc-menu-map [vc-version-other-window]
937 '("Show Other Version" . vc-version-other-window))
938 (define-key vc-menu-map [vc-diff] '("Compare with Base Version" . vc-diff))
939 (define-key vc-menu-map [vc-update-change-log]
940 '("Update ChangeLog" . vc-update-change-log))
941 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
942 (define-key vc-menu-map [separator2] '("----"))
943 (define-key vc-menu-map [vc-insert-header]
944 '("Insert Header" . vc-insert-headers))
945 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-rollback))
946 (define-key vc-menu-map [vc-revert]
947 '("Revert to Base Version" . vc-revert))
948 (define-key vc-menu-map [vc-update]
949 '("Update to Latest Version" . vc-update))
950 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
951 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
953 ;; These are not correct and it's not currently clear how doing it
954 ;; better (with more complicated expressions) might slow things down
957 ;;(put 'vc-rename-file 'menu-enable 'vc-mode)
958 ;;(put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
959 ;;(put 'vc-version-other-window 'menu-enable 'vc-mode)
960 ;;(put 'vc-diff 'menu-enable 'vc-mode)
961 ;;(put 'vc-update-change-log 'menu-enable
962 ;; '(member (vc-buffer-backend) '(RCS CVS)))
963 ;;(put 'vc-print-log 'menu-enable 'vc-mode)
964 ;;(put 'vc-rollback 'menu-enable 'vc-mode)
965 ;;(put 'vc-revert 'menu-enable 'vc-mode)
966 ;;(put 'vc-insert-headers 'menu-enable 'vc-mode)
967 ;;(put 'vc-next-action 'menu-enable 'vc-mode)
968 ;;(put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
972 ;; arch-tag: 2e5a6fa7-1d30-48e2-8bd0-e3d335f04f32
973 ;;; vc-hooks.el ends here