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 3, 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 Git Hg Arch MCVS
)
66 ;; Bzr, Git, Hg, Arch and MCVS come last because they are per-tree
67 ;; rather than per-dir.
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 "A convenience macro for calling VC backend functions.
283 Functions called by this macro must accept FILE as the first argument.
284 ARGS specifies any additional arguments. FUN should be unquoted.
285 BEWARE!! `file' is evaluated twice!!"
286 `(vc-call-backend (vc-backend ,file
) ',fun
,file
,@args
))
288 (defsubst vc-parse-buffer
(pattern i
)
289 "Find PATTERN in the current buffer and return its Ith submatch."
290 (goto-char (point-min))
291 (if (re-search-forward pattern nil t
)
294 (defun vc-insert-file (file &optional limit blocksize
)
295 "Insert the contents of FILE into the current buffer.
297 Optional argument LIMIT is a regexp. If present, the file is inserted
298 in chunks of size BLOCKSIZE (default 8 kByte), until the first
299 occurrence of LIMIT is found. Anything from the start of that occurrence
300 to the end of the buffer is then deleted. The function returns
301 non-nil if FILE exists and its contents were successfully inserted."
303 (when (file-exists-p file
)
305 (insert-file-contents file
)
306 (if (not blocksize
) (setq blocksize
8192))
309 (and (< 0 (cadr (insert-file-contents
310 file nil filepos
(incf filepos blocksize
))))
311 (progn (beginning-of-line)
312 (let ((pos (re-search-forward limit nil
'move
)))
313 (if pos
(delete-region (match-beginning 0)
316 (set-buffer-modified-p nil
)
319 (defun vc-find-root (file witness
)
320 "Find the root of a checked out project.
321 The function walks up the directory tree from FILE looking for WITNESS.
322 If WITNESS if not found, return nil, otherwise return the root."
323 ;; Represent /home/luser/foo as ~/foo so that we don't try to look for
324 ;; witnesses in /home or in /.
325 (while (not (file-directory-p file
))
326 (setq file
(file-name-directory (directory-file-name file
))))
327 (setq file
(abbreviate-file-name file
))
329 (user (nth 2 (file-attributes file
))))
332 ;; As a heuristic, we stop looking up the hierarchy of
333 ;; directories as soon as we find a directory belonging
334 ;; to another user. This should save us from looking in
335 ;; things like /net and /afs. This assumes that all the
336 ;; files inside a project belong to the same user.
337 (not (equal user
(nth 2 (file-attributes file
))))
338 (string-match vc-ignore-dir-regexp file
)))
339 (if (file-exists-p (expand-file-name witness file
))
342 (setq file
(file-name-directory (directory-file-name file
))))
346 ;; Access functions to file properties
347 ;; (Properties should be _set_ using vc-file-setprop, but
348 ;; _retrieved_ only through these functions, which decide
349 ;; if the property is already known or not. A property should
350 ;; only be retrieved by vc-file-getprop if there is no
353 ;; properties indicating the backend being used for FILE
355 (defun vc-registered (file)
356 "Return non-nil if FILE is registered in a version control system.
358 This function performs the check each time it is called. To rely
359 on the result of a previous call, use `vc-backend' instead. If the
360 file was previously registered under a certain backend, then that
361 backend is tried first."
364 ((string-match vc-ignore-dir-regexp
(file-name-directory file
)) nil
)
365 ((and (boundp 'file-name-handler-alist
)
366 (setq handler
(find-file-name-handler file
'vc-registered
)))
367 ;; handler should set vc-backend and return t if registered
368 (funcall handler
'vc-registered file
))
370 ;; There is no file name handler.
371 ;; Try vc-BACKEND-registered for each handled BACKEND.
373 (let ((backend (vc-file-getprop file
'vc-backend
)))
376 (and (vc-call-backend b
'registered file
)
377 (vc-file-setprop file
'vc-backend b
)
379 (if (or (not backend
) (eq backend
'none
))
381 (cons backend vc-handled-backends
))))
382 ;; File is not registered.
383 (vc-file-setprop file
'vc-backend
'none
)
386 (defun vc-backend (file-or-list)
387 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
388 If the argument is a list, the files must all have the same back end."
389 ;; `file' can be nil in several places (typically due to the use of
390 ;; code like (vc-backend buffer-file-name)).
391 (cond ((stringp file-or-list
)
392 (let ((property (vc-file-getprop file-or-list
'vc-backend
)))
393 ;; Note that internally, Emacs remembers unregistered
394 ;; files by setting the property to `none'.
395 (cond ((eq property
'none
) nil
)
397 ;; vc-registered sets the vc-backend property
398 (t (if (vc-registered file-or-list
)
399 (vc-file-getprop file-or-list
'vc-backend
)
401 ((and file-or-list
(listp file-or-list
))
402 (vc-backend (car file-or-list
)))
407 (defun vc-backend-subdirectory-name (file)
408 "Return where the master and lock FILEs for the current directory are kept."
409 (symbol-name (vc-backend file
)))
411 (defun vc-name (file)
412 "Return the master name of FILE.
413 If the file is not registered, or the master name is not known, return nil."
414 ;; TODO: This should ultimately become obsolete, at least up here
416 (or (vc-file-getprop file
'vc-name
)
417 ;; force computation of the property by calling
418 ;; vc-BACKEND-registered explicitly
419 (if (and (vc-backend file
)
420 (vc-call-backend (vc-backend file
) 'registered file
))
421 (vc-file-getprop file
'vc-name
))))
423 (defun vc-checkout-model (file)
424 "Indicate how FILE is checked out.
426 If FILE is not registered, this function always returns nil.
427 For registered files, the possible values are:
429 'implicit FILE is always writeable, and checked out `implicitly'
430 when the user saves the first changes to the file.
432 'locking FILE is read-only if up-to-date; user must type
433 \\[vc-next-action] before editing. Strict locking
436 'announce FILE is read-only if up-to-date; user must type
437 \\[vc-next-action] before editing. But other users
438 may be editing at the same time."
439 (or (vc-file-getprop file
'vc-checkout-model
)
440 (if (vc-backend file
)
441 (vc-file-setprop file
'vc-checkout-model
442 (vc-call checkout-model file
)))))
444 (defun vc-user-login-name (file)
445 "Return the name under which the user accesses the given FILE."
446 (or (and (eq (string-match tramp-file-name-regexp file
) 0)
447 ;; tramp case: execute "whoami" via tramp
448 (let ((default-directory (file-name-directory file
)))
450 (if (not (zerop (process-file "whoami" nil t
)))
451 ;; fall through if "whoami" didn't work
453 ;; remove trailing newline
454 (delete-region (1- (point-max)) (point-max))
458 ;; if user-login-name is nil, return the UID as a string
459 (number-to-string (user-uid))))
461 (defun vc-state (file)
462 "Return the version control state of FILE.
464 If FILE is not registered, this function always returns nil.
465 For registered files, the value returned is one of:
467 'up-to-date The working file is unmodified with respect to the
468 latest version on the current branch, and not locked.
470 'edited The working file has been edited by the user. If
471 locking is used for the file, this state means that
472 the current version is locked by the calling user.
474 USER The current version of the working file is locked by
475 some other USER (a string).
477 'needs-patch The file has not been edited by the user, but there is
478 a more recent version on the current branch stored
481 'needs-merge The file has been edited by the user, and there is also
482 a more recent version on the current branch stored in
483 the master file. This state can only occur if locking
484 is not used for the file.
486 'unlocked-changes The current version of the working file is not locked,
487 but the working file has been changed with respect
488 to that version. This state can only occur for files
489 with locking; it represents an erroneous condition that
490 should be resolved by the user (vc-next-action will
491 prompt the user to do it)."
492 ;; FIXME: New (sub)states needed (?):
493 ;; - `added' (i.e. `edited' but with no base version yet,
494 ;; typically represented by vc-workfile-version = "0")
495 ;; - `conflict' (i.e. `edited' with conflict markers)
497 ;; - `copied' and `moved' (might be handled by `removed' and `added')
498 (or (vc-file-getprop file
'vc-state
)
499 (if (and (> (length file
) 0) (vc-backend file
))
500 (vc-file-setprop file
'vc-state
501 (vc-call state-heuristic file
)))))
503 (defun vc-recompute-state (file)
504 "Recompute the version control state of FILE, and return it.
505 This calls the possibly expensive function vc-BACKEND-state,
506 rather than the heuristic."
507 (vc-file-setprop file
'vc-state
(vc-call state file
)))
509 (defsubst vc-up-to-date-p
(file)
510 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
511 (eq (vc-state file
) 'up-to-date
))
513 (defun vc-default-state-heuristic (backend file
)
514 "Default implementation of vc-state-heuristic.
515 It simply calls the real state computation function `vc-BACKEND-state'
516 and does not employ any heuristic at all."
517 (vc-call-backend backend
'state file
))
519 (defun vc-workfile-unchanged-p (file)
520 "Return non-nil if FILE has not changed since the last checkout."
521 (let ((checkout-time (vc-file-getprop file
'vc-checkout-time
))
522 (lastmod (nth 5 (file-attributes file
))))
523 (if (and checkout-time
524 ;; Tramp and Ange-FTP return this when they don't know the time.
525 (not (equal lastmod
'(0 0))))
526 (equal checkout-time lastmod
)
527 (let ((unchanged (vc-call workfile-unchanged-p file
)))
528 (vc-file-setprop file
'vc-checkout-time
(if unchanged lastmod
0))
531 (defun vc-default-workfile-unchanged-p (backend file
)
532 "Check if FILE is unchanged by diffing against the master version.
533 Return non-nil if FILE is unchanged."
534 (zerop (condition-case err
535 ;; If the implementation supports it, let the output
536 ;; go to *vc*, not *vc-diff*, since this is an internal call.
537 (vc-call diff
(list file
) nil nil
"*vc*")
538 (wrong-number-of-arguments
539 ;; If this error came from the above call to vc-BACKEND-diff,
540 ;; try again without the optional buffer argument (for
541 ;; backward compatibility). Otherwise, resignal.
542 (if (or (not (eq (cadr err
)
544 (vc-find-backend-function (vc-backend file
)
546 (not (eq (caddr err
) 4)))
547 (signal (car err
) (cdr err
))
548 (vc-call diff
(list file
)))))))
550 (defun vc-workfile-version (file)
551 "Return the repository version from which FILE was checked out.
552 If FILE is not registered, this function always returns nil."
553 (or (vc-file-getprop file
'vc-workfile-version
)
554 (if (vc-backend file
)
555 (vc-file-setprop file
'vc-workfile-version
556 (vc-call workfile-version file
)))))
558 (defun vc-default-registered (backend file
)
559 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
560 (let ((sym (vc-make-backend-sym backend
'master-templates
)))
561 (unless (get backend
'vc-templates-grabbed
)
562 (put backend
'vc-templates-grabbed t
)
563 (set sym
(append (delq nil
566 (and (consp template
)
567 (eq (cdr template
) backend
)
570 vc-master-templates
)))
571 (symbol-value sym
))))
572 (let ((result (vc-check-master-templates file
(symbol-value sym
))))
574 (vc-file-setprop file
'vc-name result
)
575 nil
)))) ; Not registered
577 (defun vc-possible-master (s dirname basename
)
579 ((stringp s
) (format s dirname basename
))
581 ;; The template is a function to invoke. If the
582 ;; function returns non-nil, that means it has found a
583 ;; master. For backward compatibility, we also handle
584 ;; the case that the function throws a 'found atom
585 ;; and a pair (cons MASTER-FILE BACKEND).
586 (let ((result (catch 'found
(funcall s dirname basename
))))
587 (if (consp result
) (car result
) result
)))))
589 (defun vc-check-master-templates (file templates
)
590 "Return non-nil if there is a master corresponding to FILE.
592 TEMPLATES is a list of strings or functions. If an element is a
593 string, it must be a control string as required by `format', with two
594 string placeholders, such as \"%sRCS/%s,v\". The directory part of
595 FILE is substituted for the first placeholder, the basename of FILE
596 for the second. If a file with the resulting name exists, it is taken
597 as the master of FILE, and returned.
599 If an element of TEMPLATES is a function, it is called with the
600 directory part and the basename of FILE as arguments. It should
601 return non-nil if it finds a master; that value is then returned by
603 (let ((dirname (or (file-name-directory file
) ""))
604 (basename (file-name-nondirectory file
)))
608 (let ((trial (vc-possible-master s dirname basename
)))
609 (if (and trial
(file-exists-p trial
)
610 ;; Make sure the file we found with name
611 ;; TRIAL is not the source file itself.
612 ;; That can happen with RCS-style names if
613 ;; the file name is truncated (e.g. to 14
614 ;; chars). See if either directory or
615 ;; attributes differ.
616 (or (not (string= dirname
617 (file-name-directory trial
)))
618 (not (equal (file-attributes file
)
619 (file-attributes trial
)))))
620 (throw 'found trial
))))
623 (defun vc-toggle-read-only (&optional verbose
)
624 "Change read-only status of current buffer, perhaps via version control.
626 If the buffer is visiting a file registered with version control,
627 then check the file in or out. Otherwise, just change the read-only flag
629 With prefix argument, ask for version number to check in or check out.
630 Check-out of a specified version number does not lock the file;
631 to do that, use this command a second time with no argument.
633 If you bind this function to \\[toggle-read-only], then Emacs checks files
634 in or out whenever you toggle the read-only flag."
636 (if (or (and (boundp 'vc-dired-mode
) vc-dired-mode
)
637 ;; use boundp because vc.el might not be loaded
638 (vc-backend buffer-file-name
))
639 (vc-next-action verbose
)
642 (defun vc-default-make-version-backups-p (backend file
)
643 "Return non-nil if unmodified versions should be backed up locally.
644 The default is to switch off this feature."
647 (defun vc-version-backup-file-name (file &optional rev manual regexp
)
648 "Return a backup file name for REV or the current version of FILE.
649 If MANUAL is non-nil it means that a name for backups created by
650 the user should be returned; if REGEXP is non-nil that means to return
651 a regexp for matching all such backup files, regardless of the version."
653 (concat (regexp-quote (file-name-nondirectory file
))
654 "\\.~.+" (unless manual
"\\.") "~")
655 (expand-file-name (concat (file-name-nondirectory file
)
656 ".~" (subst-char-in-string
657 ?
/ ?_
(or rev
(vc-workfile-version file
)))
658 (unless manual
".") "~")
659 (file-name-directory file
))))
661 (defun vc-delete-automatic-version-backups (file)
662 "Delete all existing automatic version backups for FILE."
666 (directory-files (or (file-name-directory file
) default-directory
) t
667 (vc-version-backup-file-name file nil nil t
)))
668 ;; Don't fail when the directory doesn't exist.
671 (defun vc-make-version-backup (file)
672 "Make a backup copy of FILE, which is assumed in sync with the repository.
673 Before doing that, check if there are any old backups and get rid of them."
674 (unless (and (fboundp 'msdos-long-file-names
)
675 (not (with-no-warnings (msdos-long-file-names))))
676 (vc-delete-automatic-version-backups file
)
678 (copy-file file
(vc-version-backup-file-name file
)
680 ;; It's ok if it doesn't work (e.g. directory not writable),
681 ;; since this is just for efficiency.
684 (concat "Warning: Cannot make version backup; "
685 "diff/revert therefore not local"))))))
687 (defun vc-before-save ()
688 "Function to be called by `basic-save-buffer' (in files.el)."
689 ;; If the file on disk is still in sync with the repository,
690 ;; and version backups should be made, copy the file to
691 ;; another name. This enables local diffs and local reverting.
692 (let ((file buffer-file-name
))
693 (and (vc-backend file
)
694 (vc-up-to-date-p file
)
695 (eq (vc-checkout-model file
) 'implicit
)
696 (vc-call make-version-backups-p file
)
697 (vc-make-version-backup file
))))
699 (defun vc-after-save ()
700 "Function to be called by `basic-save-buffer' (in files.el)."
701 ;; If the file in the current buffer is under version control,
702 ;; up-to-date, and locking is not used for the file, set
703 ;; the state to 'edited and redisplay the mode line.
704 (let ((file buffer-file-name
))
705 (and (vc-backend file
)
706 (or (and (equal (vc-file-getprop file
'vc-checkout-time
)
707 (nth 5 (file-attributes file
)))
708 ;; File has been saved in the same second in which
709 ;; it was checked out. Clear the checkout-time
710 ;; to avoid confusion.
711 (vc-file-setprop file
'vc-checkout-time nil
))
713 (vc-up-to-date-p file
)
714 (eq (vc-checkout-model file
) 'implicit
)
715 (vc-file-setprop file
'vc-state
'edited
)
718 ;; If VC is not loaded, then there can't be
719 ;; any VC Dired buffer to synchronize.
720 (vc-dired-resynch-file file
)))))
722 (defconst vc-mode-line-map
723 (let ((map (make-sparse-keymap)))
724 (define-key map
[mode-line down-mouse-1
] 'vc-menu-map
)
727 (defun vc-mode-line (file)
728 "Set `vc-mode' to display type of version control for FILE.
729 The value is set in the current buffer, which should be the buffer
731 (interactive (list buffer-file-name
))
732 (let ((backend (vc-backend file
)))
735 (let* ((ml-string (vc-call mode-line-string file
))
736 (ml-echo (get-text-property 0 'help-echo ml-string
)))
740 (if (null vc-display-status
)
741 (symbol-name backend
)
744 'mouse-face
'mode-line-highlight
747 (format "File under the %s version control system"
749 "\nmouse-1: Version Control menu")
750 'local-map vc-mode-line-map
)))))
751 ;; If the file is locked by some other user, make
752 ;; the buffer read-only. Like this, even root
753 ;; cannot modify a file that someone else has locked.
754 (and (equal file buffer-file-name
)
755 (stringp (vc-state file
))
756 (setq buffer-read-only t
))
757 ;; If the user is root, and the file is not owner-writable,
758 ;; then pretend that we can't write it
759 ;; even though we can (because root can write anything).
760 ;; This way, even root cannot modify a file that isn't locked.
761 (and (equal file buffer-file-name
)
762 (not buffer-read-only
)
763 (zerop (user-real-uid))
764 (zerop (logand (file-modes buffer-file-name
) 128))
765 (setq buffer-read-only t
)))
766 (force-mode-line-update)
769 (defun vc-default-mode-line-string (backend file
)
770 "Return string for placement in modeline by `vc-mode-line' for FILE.
773 \"BACKEND-REV\" if the file is up-to-date
774 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
775 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
777 This function assumes that the file is registered."
778 (setq backend
(symbol-name backend
))
779 (let ((state (vc-state file
))
781 (rev (vc-workfile-version file
)))
783 (cond ((or (eq state
'up-to-date
)
784 (eq state
'needs-patch
))
785 (setq state-echo
"Up to date file")
786 (concat backend
"-" rev
))
788 (setq state-echo
(concat "File locked by" state
))
789 (concat backend
":" state
":" rev
))
791 ;; Not just for the 'edited state, but also a fallback
792 ;; for all other states. Think about different symbols
793 ;; for 'needs-patch and 'needs-merge.
794 (setq state-echo
"Locally modified file")
795 (concat backend
":" rev
)))
796 'help-echo
(concat state-echo
" under the " backend
797 " version control system"))))
799 (defun vc-follow-link ()
800 "If current buffer visits a symbolic link, visit the real file.
801 If the real file is already visited in another buffer, make that buffer
802 current, and kill the buffer that visits the link."
803 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name
)))
804 (true-buffer (find-buffer-visiting truename
))
805 (this-buffer (current-buffer)))
806 (if (eq true-buffer this-buffer
)
808 (kill-buffer this-buffer
)
809 ;; In principle, we could do something like set-visited-file-name.
810 ;; However, it can't be exactly the same as set-visited-file-name.
811 ;; I'm not going to work out the details right now. -- rms.
812 (set-buffer (find-file-noselect truename
)))
813 (set-buffer true-buffer
)
814 (kill-buffer this-buffer
))))
816 (defun vc-default-find-file-hook (backend)
819 (defun vc-find-file-hook ()
820 "Function for `find-file-hook' activating VC mode if appropriate."
821 ;; Recompute whether file is version controlled,
822 ;; if user has killed the buffer and revisited.
825 (when buffer-file-name
826 (vc-file-clearprops buffer-file-name
)
828 ((with-demoted-errors (vc-backend buffer-file-name
))
829 ;; Compute the state and put it in the modeline.
830 (vc-mode-line buffer-file-name
)
831 (unless vc-make-backup-files
832 ;; Use this variable, not make-backup-files,
833 ;; because this is for things that depend on the file name.
834 (set (make-local-variable 'backup-inhibited
) t
))
835 ;; Let the backend setup any buffer-local things he needs.
836 (vc-call-backend (vc-backend buffer-file-name
) 'find-file-hook
))
837 ((let ((link-type (and (file-symlink-p buffer-file-name
)
838 (vc-backend (file-chase-links buffer-file-name
)))))
839 (cond ((not link-type
) nil
) ;Nothing to do.
840 ((eq vc-follow-symlinks nil
)
842 "Warning: symbolic link to %s-controlled source file" link-type
))
843 ((or (not (eq vc-follow-symlinks
'ask
))
844 ;; If we already visited this file by following
845 ;; the link, don't ask again if we try to visit
846 ;; it again. GUD does that, and repeated questions
849 (abbreviate-file-name
850 (file-chase-links buffer-file-name
))))
853 (message "Followed link to %s" buffer-file-name
)
856 (if (yes-or-no-p (format
857 "Symbolic link to %s-controlled source file; follow link? " link-type
))
858 (progn (vc-follow-link)
859 (message "Followed link to %s" buffer-file-name
)
862 "Warning: editing through the link bypasses version control")
865 (add-hook 'find-file-hook
'vc-find-file-hook
)
867 ;; more hooks, this time for file-not-found
868 (defun vc-file-not-found-hook ()
869 "When file is not found, try to check it out from version control.
870 Returns t if checkout was successful, nil otherwise.
871 Used in `find-file-not-found-functions'."
872 ;; When a file does not exist, ignore cached info about it
873 ;; from a previous visit.
874 (vc-file-clearprops buffer-file-name
)
875 (let ((backend (vc-backend buffer-file-name
)))
876 (if backend
(vc-call-backend backend
'find-file-not-found-hook
))))
878 (defun vc-default-find-file-not-found-hook (backend)
879 ;; This used to do what vc-rcs-find-file-not-found-hook does, but it only
880 ;; really makes sense for RCS. For other backends, better not do anything.
883 (add-hook 'find-file-not-found-functions
'vc-file-not-found-hook
)
885 (defun vc-kill-buffer-hook ()
886 "Discard VC info about a file when we kill its buffer."
888 (vc-file-clearprops buffer-file-name
)))
890 (add-hook 'kill-buffer-hook
'vc-kill-buffer-hook
)
892 ;; Now arrange for (autoloaded) bindings of the main package.
893 ;; Bindings for this have to go in the global map, as we'll often
894 ;; want to call them from random buffers.
896 ;; Autoloading works fine, but it prevents shortcuts from appearing
897 ;; in the menu because they don't exist yet when the menu is built.
898 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
899 (defvar vc-prefix-map
900 (let ((map (make-sparse-keymap)))
901 (define-key map
"a" 'vc-update-change-log
)
902 (define-key map
"b" 'vc-switch-backend
)
903 (define-key map
"c" 'vc-rollback
)
904 (define-key map
"d" 'vc-directory
)
905 (define-key map
"g" 'vc-annotate
)
906 (define-key map
"h" 'vc-insert-headers
)
907 (define-key map
"i" 'vc-register
)
908 (define-key map
"l" 'vc-print-log
)
909 (define-key map
"m" 'vc-merge
)
910 (define-key map
"r" 'vc-retrieve-snapshot
)
911 (define-key map
"s" 'vc-create-snapshot
)
912 (define-key map
"u" 'vc-revert
)
913 (define-key map
"v" 'vc-next-action
)
914 (define-key map
"+" 'vc-update
)
915 (define-key map
"=" 'vc-diff
)
916 (define-key map
"~" 'vc-version-other-window
)
918 (fset 'vc-prefix-map vc-prefix-map
)
919 (define-key global-map
"\C-xv" 'vc-prefix-map
)
921 (if (not (boundp 'vc-menu-map
))
922 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
925 ;;(define-key vc-menu-map [show-files]
926 ;; '("Show Files under VC" . (vc-directory t)))
927 (define-key vc-menu-map
[vc-retrieve-snapshot
]
928 '("Retrieve Snapshot" . vc-retrieve-snapshot
))
929 (define-key vc-menu-map
[vc-create-snapshot
]
930 '("Create Snapshot" . vc-create-snapshot
))
931 (define-key vc-menu-map
[vc-directory
] '("VC Directory Listing" . vc-directory
))
932 (define-key vc-menu-map
[separator1] '("----"))
933 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
934 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
935 (define-key vc-menu-map [vc-version-other-window]
936 '("Show Other Version" . vc-version-other-window))
937 (define-key vc-menu-map [vc-diff] '("Compare with Base Version" . vc-diff))
938 (define-key vc-menu-map [vc-update-change-log]
939 '("Update ChangeLog" . vc-update-change-log))
940 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
941 (define-key vc-menu-map [separator2] '("----"))
942 (define-key vc-menu-map [vc-insert-header]
943 '("Insert Header" . vc-insert-headers))
944 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-rollback))
945 (define-key vc-menu-map [vc-revert]
946 '("Revert to Base Version" . vc-revert))
947 (define-key vc-menu-map [vc-update]
948 '("Update to Latest Version" . vc-update))
949 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
950 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
952 (defun vc-default-extra-menu (backend)
955 ;; These are not correct and it's not currently clear how doing it
956 ;; better (with more complicated expressions) might slow things down
959 ;;(put 'vc-rename-file 'menu-enable 'vc-mode)
960 ;;(put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
961 ;;(put 'vc-version-other-window 'menu-enable 'vc-mode)
962 ;;(put 'vc-diff 'menu-enable 'vc-mode)
963 ;;(put 'vc-update-change-log 'menu-enable
964 ;; '(member (vc-buffer-backend) '(RCS CVS)))
965 ;;(put 'vc-print-log 'menu-enable 'vc-mode)
966 ;;(put 'vc-rollback 'menu-enable 'vc-mode)
967 ;;(put 'vc-revert 'menu-enable 'vc-mode)
968 ;;(put 'vc-insert-headers 'menu-enable 'vc-mode)
969 ;;(put 'vc-next-action 'menu-enable 'vc-mode)
970 ;;(put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
974 ;; arch-tag: 2e5a6fa7-1d30-48e2-8bd0-e3d335f04f32
975 ;;; vc-hooks.el ends here