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, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see vc.el for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This is the always-loaded portion of VC. It takes care of
28 ;; VC-related activities that are done when you visit a file, so that
29 ;; vc.el itself is loaded only when you use a VC command. See the
30 ;; commentary of vc.el.
37 ;; Customization Variables (the rest is in vc.el)
39 (defvar vc-ignore-vc-files nil
)
40 (make-obsolete-variable 'vc-ignore-vc-files
41 "set `vc-handled-backends' to nil to disable VC."
44 (defvar vc-master-templates
())
45 (make-obsolete-variable 'vc-master-templates
46 "to define master templates for a given BACKEND, use
47 vc-BACKEND-master-templates. To enable or disable VC for a given
48 BACKEND, use `vc-handled-backends'."
51 (defvar vc-header-alist
())
52 (make-obsolete-variable 'vc-header-alist
'vc-BACKEND-header
"21.1")
54 (defcustom vc-ignore-dir-regexp
55 ;; Stop SMB, automounter, AFS, and DFS host lookups.
56 locate-dominating-stop-dir-regexp
57 "Regexp matching directory names that are not under VC's control.
58 The default regexp prevents fruitless and time-consuming attempts
59 to determine the VC status in directories in which filenames are
60 interpreted as hostnames."
64 (defcustom vc-handled-backends
'(RCS CVS SVN SCCS Bzr Git Hg Mtn Arch
)
65 ;; RCS, CVS, SVN and SCCS come first because they are per-dir
66 ;; rather than per-tree. RCS comes first because of the multibackend
67 ;; support intended to use RCS for local commits (with a remote CVS server).
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
)
78 ;; Note: we don't actually have a darcs back end yet.
79 ;; Also, Meta-CVS (corresponsding to MCVS) is unsupported.
80 (defcustom vc-directory-exclusion-list
(purecopy '("SCCS" "RCS" "CVS" "MCVS"
81 ".svn" ".git" ".hg" ".bzr"
82 "_MTN" "_darcs" "{arch}"))
83 "List of directory names to be ignored when walking directory trees."
84 :type
'(repeat string
)
87 (defcustom vc-path nil
88 "List of extra directories to search for version control commands."
89 :type
'(repeat directory
)
92 (make-obsolete-variable 'vc-path
"should not be necessary anymore." "23.2")
94 (defcustom vc-make-backup-files nil
95 "If non-nil, backups of registered files are made as with other files.
96 If nil (the default), files covered by version control don't get backups."
101 (defcustom vc-follow-symlinks
'ask
102 "What to do if visiting a symbolic link to a file under version control.
103 Editing such a file through the link bypasses the version control system,
104 which is dangerous and probably not what you want.
106 If this variable is t, VC follows the link and visits the real file,
107 telling you about it in the echo area. If it is `ask', VC asks for
108 confirmation whether it should follow the link. If nil, the link is
109 visited and a warning displayed."
110 :type
'(choice (const :tag
"Ask for confirmation" ask
)
111 (const :tag
"Visit link and warn" nil
)
112 (const :tag
"Follow link" t
))
115 (defcustom vc-display-status t
116 "If non-nil, display revision number and lock status in modeline.
117 Otherwise, not displayed."
122 (defcustom vc-consult-headers t
123 "If non-nil, identify work files by searching for version headers."
127 (defcustom vc-keep-workfiles t
128 "If non-nil, don't delete working files after registering changes.
129 If the back-end is CVS, workfiles are always kept, regardless of the
134 (defcustom vc-mistrust-permissions nil
135 "If non-nil, don't assume permissions/ownership track version-control status.
136 If nil, do rely on the permissions.
137 See also variable `vc-consult-headers'."
141 (defun vc-mistrust-permissions (file)
142 "Internal access function to variable `vc-mistrust-permissions' for FILE."
143 (or (eq vc-mistrust-permissions
't
)
144 (and vc-mistrust-permissions
145 (funcall vc-mistrust-permissions
146 (vc-backend-subdirectory-name file
)))))
148 (defcustom vc-stay-local
'only-file
149 "Non-nil means use local operations when possible for remote repositories.
150 This avoids slow queries over the network and instead uses heuristics
151 and past information to determine the current status of a file.
153 If value is the symbol `only-file' `vc-dir' will connect to the
154 server, but heuristics will be used to determine the status for
155 all other VC operations.
157 The value can also be a regular expression or list of regular
158 expressions to match against the host name of a repository; then VC
159 only stays local for hosts that match it. Alternatively, the value
160 can be a list of regular expressions where the first element is the
161 symbol `except'; then VC always stays local except for hosts matched
162 by these regular expressions."
164 (const :tag
"Always stay local" t
)
165 (const :tag
"Only for file operations" only-file
)
166 (const :tag
"Don't stay local" nil
)
167 (list :format
"\nExamine hostname and %v" :tag
"Examine hostname ..."
168 (set :format
"%v" :inline t
(const :format
"%t" :tag
"don't" except
))
169 (regexp :format
" stay local,\n%t: %v" :tag
"if it matches")
170 (repeat :format
"%v%i\n" :inline t
(regexp :tag
"or"))))
174 (defun vc-stay-local-p (file &optional backend
)
175 "Return non-nil if VC should stay local when handling FILE.
176 This uses the `repository-hostname' backend operation.
177 If FILE is a list of files, return non-nil if any of them
178 individually should stay local."
180 (delq nil
(mapcar (lambda (arg) (vc-stay-local-p arg backend
)) file
))
181 (setq backend
(or backend
(vc-backend file
)))
182 (let* ((sym (vc-make-backend-sym backend
'stay-local
))
183 (stay-local (if (boundp sym
) (symbol-value sym
) vc-stay-local
)))
184 (if (symbolp stay-local
) stay-local
185 (let ((dirname (if (file-directory-p file
)
186 (directory-file-name file
)
187 (file-name-directory file
))))
189 (or (vc-file-getprop dirname
'vc-stay-local-p
)
191 dirname
'vc-stay-local-p
192 (let ((hostname (vc-call-backend
193 backend
'repository-hostname dirname
)))
197 (if (eq (car-safe stay-local
) 'except
)
198 (setq default nil stay-local
(cdr stay-local
)))
199 (when (consp stay-local
)
201 (mapconcat 'identity stay-local
"\\|")))
202 (if (if (string-match stay-local hostname
)
203 default
(not default
))
206 ;;; This is handled specially now.
207 ;; Tell Emacs about this new kind of minor mode
208 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
211 (put 'vc-mode
'risky-local-variable t
)
212 (make-variable-buffer-local 'vc-mode
)
213 (put 'vc-mode
'permanent-local t
)
215 (defun vc-mode (&optional arg
)
216 ;; Dummy function for C-h m
217 "Version Control minor mode.
218 This minor mode is automatically activated whenever you visit a file under
219 control of one of the revision control systems in `vc-handled-backends'.
220 VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
223 (defmacro vc-error-occurred
(&rest body
)
224 `(condition-case nil
(progn ,@body nil
) (error t
)))
226 ;; We need a notion of per-file properties because the version
227 ;; control state of a file is expensive to derive --- we compute
228 ;; them when the file is initially found, keep them up to date
229 ;; during any subsequent VC operations, and forget them when
230 ;; the buffer is killed.
232 (defvar vc-file-prop-obarray
(make-vector 17 0)
233 "Obarray for per-file properties.")
235 (defvar vc-touched-properties nil
)
237 (defun vc-file-setprop (file property value
)
238 "Set per-file VC PROPERTY for FILE to VALUE."
239 (if (and vc-touched-properties
240 (not (memq property vc-touched-properties
)))
241 (setq vc-touched-properties
(append (list property
)
242 vc-touched-properties
)))
243 (put (intern file vc-file-prop-obarray
) property value
))
245 (defun vc-file-getprop (file property
)
246 "Get per-file VC PROPERTY for FILE."
247 (get (intern file vc-file-prop-obarray
) property
))
249 (defun vc-file-clearprops (file)
250 "Clear all VC properties of FILE."
251 (setplist (intern file vc-file-prop-obarray
) nil
))
254 ;; We keep properties on each symbol naming a backend as follows:
255 ;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
257 (defun vc-make-backend-sym (backend sym
)
258 "Return BACKEND-specific version of VC symbol SYM."
259 (intern (concat "vc-" (downcase (symbol-name backend
))
260 "-" (symbol-name sym
))))
262 (defun vc-find-backend-function (backend fun
)
263 "Return BACKEND-specific implementation of FUN.
264 If there is no such implementation, return the default implementation;
265 if that doesn't exist either, return nil."
266 (let ((f (vc-make-backend-sym backend fun
)))
268 ;; Load vc-BACKEND.el if needed.
269 (require (intern (concat "vc-" (downcase (symbol-name backend
)))))
271 (let ((def (vc-make-backend-sym 'default fun
)))
272 (if (fboundp def
) (cons def backend
) nil
))))))
274 (defun vc-call-backend (backend function-name
&rest args
)
275 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
278 (apply 'vc-BACKEND-FUN ARGS)
280 if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
283 (apply 'vc-default-FUN BACKEND ARGS)
285 It is usually called via the `vc-call' macro."
286 (let ((f (assoc function-name
(get backend
'vc-functions
))))
287 (if f
(setq f
(cdr f
))
288 (setq f
(vc-find-backend-function backend function-name
))
289 (push (cons function-name f
) (get backend
'vc-functions
)))
292 (error "Sorry, %s is not implemented for %s" function-name backend
))
293 ((consp f
) (apply (car f
) (cdr f
) args
))
294 (t (apply f args
)))))
296 (defmacro vc-call
(fun file
&rest args
)
297 "A convenience macro for calling VC backend functions.
298 Functions called by this macro must accept FILE as the first argument.
299 ARGS specifies any additional arguments. FUN should be unquoted.
300 BEWARE!! FILE is evaluated twice!!"
301 `(vc-call-backend (vc-backend ,file
) ',fun
,file
,@args
))
303 (defsubst vc-parse-buffer
(pattern i
)
304 "Find PATTERN in the current buffer and return its Ith submatch."
305 (goto-char (point-min))
306 (if (re-search-forward pattern nil t
)
309 (defun vc-insert-file (file &optional limit blocksize
)
310 "Insert the contents of FILE into the current buffer.
312 Optional argument LIMIT is a regexp. If present, the file is inserted
313 in chunks of size BLOCKSIZE (default 8 kByte), until the first
314 occurrence of LIMIT is found. Anything from the start of that occurrence
315 to the end of the buffer is then deleted. The function returns
316 non-nil if FILE exists and its contents were successfully inserted."
318 (when (file-exists-p file
)
320 (insert-file-contents file
)
321 (unless blocksize
(setq blocksize
8192))
324 (and (< 0 (cadr (insert-file-contents
325 file nil filepos
(incf filepos blocksize
))))
326 (progn (beginning-of-line)
327 (let ((pos (re-search-forward limit nil
'move
)))
328 (when pos
(delete-region (match-beginning 0)
331 (set-buffer-modified-p nil
)
334 (defun vc-find-root (file witness
)
335 "Find the root of a checked out project.
336 The function walks up the directory tree from FILE looking for WITNESS.
337 If WITNESS if not found, return nil, otherwise return the root."
338 (let ((locate-dominating-stop-dir-regexp
339 (or vc-ignore-dir-regexp locate-dominating-stop-dir-regexp
)))
340 (locate-dominating-file file witness
)))
342 ;; Access functions to file properties
343 ;; (Properties should be _set_ using vc-file-setprop, but
344 ;; _retrieved_ only through these functions, which decide
345 ;; if the property is already known or not. A property should
346 ;; only be retrieved by vc-file-getprop if there is no
349 ;; properties indicating the backend being used for FILE
351 (defun vc-registered (file)
352 "Return non-nil if FILE is registered in a version control system.
354 This function performs the check each time it is called. To rely
355 on the result of a previous call, use `vc-backend' instead. If the
356 file was previously registered under a certain backend, then that
357 backend is tried first."
360 ((and (file-name-directory file
)
361 (string-match vc-ignore-dir-regexp
(file-name-directory file
)))
363 ((and (boundp 'file-name-handler-alist
)
364 (setq handler
(find-file-name-handler file
'vc-registered
)))
365 ;; handler should set vc-backend and return t if registered
366 (funcall handler
'vc-registered file
))
368 ;; There is no file name handler.
369 ;; Try vc-BACKEND-registered for each handled BACKEND.
371 (let ((backend (vc-file-getprop file
'vc-backend
)))
374 (and (vc-call-backend b
'registered file
)
375 (vc-file-setprop file
'vc-backend b
)
377 (if (or (not backend
) (eq backend
'none
))
379 (cons backend vc-handled-backends
))))
380 ;; File is not registered.
381 (vc-file-setprop file
'vc-backend
'none
)
384 (defun vc-backend (file-or-list)
385 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
386 If the argument is a list, the files must all have the same back end."
387 ;; `file' can be nil in several places (typically due to the use of
388 ;; code like (vc-backend buffer-file-name)).
389 (cond ((stringp file-or-list
)
390 (let ((property (vc-file-getprop file-or-list
'vc-backend
)))
391 ;; Note that internally, Emacs remembers unregistered
392 ;; files by setting the property to `none'.
393 (cond ((eq property
'none
) nil
)
395 ;; vc-registered sets the vc-backend property
396 (t (if (vc-registered file-or-list
)
397 (vc-file-getprop file-or-list
'vc-backend
)
399 ((and file-or-list
(listp file-or-list
))
400 (vc-backend (car file-or-list
)))
405 (defun vc-backend-subdirectory-name (file)
406 "Return where the repository for the current directory is kept."
407 (symbol-name (vc-backend file
)))
409 (defun vc-name (file)
410 "Return the master name of FILE.
411 If the file is not registered, or the master name is not known, return nil."
412 ;; TODO: This should ultimately become obsolete, at least up here
414 (or (vc-file-getprop file
'vc-name
)
415 ;; force computation of the property by calling
416 ;; vc-BACKEND-registered explicitly
417 (let ((backend (vc-backend file
)))
419 (vc-call-backend backend
'registered file
))
420 (vc-file-getprop file
'vc-name
)))))
422 (defun vc-checkout-model (backend files
)
423 "Indicate how FILES are checked out.
425 If FILES are not registered, this function always returns nil.
426 For registered files, the possible values are:
428 'implicit FILES are always writable, and checked out `implicitly'
429 when the user saves the first changes to the file.
431 'locking FILES are read-only if up-to-date; user must type
432 \\[vc-next-action] before editing. Strict locking
435 'announce FILES are read-only if up-to-date; user must type
436 \\[vc-next-action] before editing. But other users
437 may be editing at the same time."
438 (vc-call-backend backend
'checkout-model files
))
440 (defun vc-user-login-name (file)
441 "Return the name under which the user accesses the given FILE."
442 (or (and (eq (string-match tramp-file-name-regexp file
) 0)
443 ;; tramp case: execute "whoami" via tramp
444 (let ((default-directory (file-name-directory file
))
445 process-file-side-effects
)
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 &optional backend
)
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-update 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 repository. This state can only occur if locking
481 is not used for the file.
483 'unlocked-changes The working version of the 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).
490 'added Scheduled to go into the repository on the next commit.
491 Often represented by vc-working-revision = \"0\" in VCSes
492 with monotonic IDs like Subversion and Mercurial.
494 'removed Scheduled to be deleted from the repository on next commit.
496 'conflict The file contains conflicts as the result of a merge.
497 For now the conflicts are text conflicts. In the
498 future this might be extended to deal with metadata
501 'missing The file is not present in the file system, but the VC
502 system still tracks it.
504 'ignored The file showed up in a dir-status listing with a flag
505 indicating the version-control system is ignoring it,
506 Note: This property is not set reliably (some VCSes
507 don't have useful directory-status commands) so assume
508 that any file with vc-state nil might be ignorable
509 without VC knowing it.
511 'unregistered The file is not under version control.
513 A return of nil from this function means we have no information on the
514 status of this file."
515 ;; Note: in Emacs 22 and older, return of nil meant the file was
516 ;; unregistered. This is potentially a source of
517 ;; backward-compatibility bugs.
519 ;; FIXME: New (sub)states needed (?):
520 ;; - `copied' and `moved' (might be handled by `removed' and `added')
521 (or (vc-file-getprop file
'vc-state
)
522 (when (> (length file
) 0) ;Why?? --Stef
523 (setq backend
(or backend
(vc-backend file
)))
525 (vc-state-refresh file backend
)))))
527 (defun vc-state-refresh (file backend
)
528 "Quickly recompute the `state' of FILE."
531 (vc-call-backend backend
'state-heuristic file
)))
533 (defsubst vc-up-to-date-p
(file)
534 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
535 (eq (vc-state file
) 'up-to-date
))
537 (defun vc-default-state-heuristic (backend file
)
538 "Default implementation of vc-BACKEND-state-heuristic.
539 It simply calls the real state computation function `vc-BACKEND-state'
540 and does not employ any heuristic at all."
541 (vc-call-backend backend
'state file
))
543 (defun vc-workfile-unchanged-p (file)
544 "Return non-nil if FILE has not changed since the last checkout."
545 (let ((checkout-time (vc-file-getprop file
'vc-checkout-time
))
546 (lastmod (nth 5 (file-attributes file
))))
547 ;; This is a shortcut for determining when the workfile is
548 ;; unchanged. It can fail under some circumstances; see the
549 ;; discussion in bug#694.
550 (if (and checkout-time
551 ;; Tramp and Ange-FTP return this when they don't know the time.
552 (not (equal lastmod
'(0 0))))
553 (equal checkout-time lastmod
)
554 (let ((unchanged (vc-call workfile-unchanged-p file
)))
555 (vc-file-setprop file
'vc-checkout-time
(if unchanged lastmod
0))
558 (defun vc-default-workfile-unchanged-p (backend file
)
559 "Check if FILE is unchanged by diffing against the repository version.
560 Return non-nil if FILE is unchanged."
561 (zerop (condition-case err
562 ;; If the implementation supports it, let the output
563 ;; go to *vc*, not *vc-diff*, since this is an internal call.
564 (vc-call-backend backend
'diff
(list file
) nil nil
"*vc*")
565 (wrong-number-of-arguments
566 ;; If this error came from the above call to vc-BACKEND-diff,
567 ;; try again without the optional buffer argument (for
568 ;; backward compatibility). Otherwise, resignal.
569 (if (or (not (eq (cadr err
)
571 (vc-find-backend-function backend
'diff
))))
572 (not (eq (caddr err
) 4)))
573 (signal (car err
) (cdr err
))
574 (vc-call-backend backend
'diff
(list file
)))))))
576 (defun vc-working-revision (file &optional backend
)
577 "Return the repository version from which FILE was checked out.
578 If FILE is not registered, this function always returns nil."
579 (or (vc-file-getprop file
'vc-working-revision
)
581 (setq backend
(or backend
(vc-backend file
)))
583 (vc-file-setprop file
'vc-working-revision
584 (vc-call-backend backend
'working-revision file
))))))
586 ;; Backward compatibility.
587 (define-obsolete-function-alias
588 'vc-workfile-version
'vc-working-revision
"23.1")
589 (defun vc-default-working-revision (backend file
)
591 "`working-revision' not found: using the old `workfile-version' instead")
592 (vc-call-backend backend
'workfile-version file
))
594 (defun vc-default-registered (backend file
)
595 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
596 (let ((sym (vc-make-backend-sym backend
'master-templates
)))
597 (unless (get backend
'vc-templates-grabbed
)
598 (put backend
'vc-templates-grabbed t
)
599 (set sym
(append (delq nil
602 (and (consp template
)
603 (eq (cdr template
) backend
)
606 vc-master-templates
)))
607 (symbol-value sym
))))
608 (let ((result (vc-check-master-templates file
(symbol-value sym
))))
610 (vc-file-setprop file
'vc-name result
)
611 nil
)))) ; Not registered
613 (defun vc-possible-master (s dirname basename
)
615 ((stringp s
) (format s dirname basename
))
617 ;; The template is a function to invoke. If the
618 ;; function returns non-nil, that means it has found a
619 ;; master. For backward compatibility, we also handle
620 ;; the case that the function throws a 'found atom
621 ;; and a pair (cons MASTER-FILE BACKEND).
622 (let ((result (catch 'found
(funcall s dirname basename
))))
623 (if (consp result
) (car result
) result
)))))
625 (defun vc-check-master-templates (file templates
)
626 "Return non-nil if there is a master corresponding to FILE.
628 TEMPLATES is a list of strings or functions. If an element is a
629 string, it must be a control string as required by `format', with two
630 string placeholders, such as \"%sRCS/%s,v\". The directory part of
631 FILE is substituted for the first placeholder, the basename of FILE
632 for the second. If a file with the resulting name exists, it is taken
633 as the master of FILE, and returned.
635 If an element of TEMPLATES is a function, it is called with the
636 directory part and the basename of FILE as arguments. It should
637 return non-nil if it finds a master; that value is then returned by
639 (let ((dirname (or (file-name-directory file
) ""))
640 (basename (file-name-nondirectory file
)))
644 (let ((trial (vc-possible-master s dirname basename
)))
645 (when (and trial
(file-exists-p trial
)
646 ;; Make sure the file we found with name
647 ;; TRIAL is not the source file itself.
648 ;; That can happen with RCS-style names if
649 ;; the file name is truncated (e.g. to 14
650 ;; chars). See if either directory or
651 ;; attributes differ.
652 (or (not (string= dirname
653 (file-name-directory trial
)))
654 (not (equal (file-attributes file
)
655 (file-attributes trial
)))))
656 (throw 'found trial
))))
659 (defun vc-toggle-read-only (&optional verbose
)
660 "Change read-only status of current buffer, perhaps via version control.
662 If the buffer is visiting a file registered with version control,
663 throw an error, because this is not a safe or really meaningful operation
664 on any version-control system newer than RCS.
666 Otherwise, just change the read-only flag of the buffer.
668 If you bind this function to \\[toggle-read-only], then Emacs
669 will properly intercept all attempts to toggle the read-only flag
670 on version-controlled buffer."
672 (if (vc-backend buffer-file-name
)
673 (error "Toggling the readability of a version controlled file is likely to wreak havoc")
676 (defun vc-default-make-version-backups-p (backend file
)
677 "Return non-nil if unmodified versions should be backed up locally.
678 The default is to switch off this feature."
681 (defun vc-version-backup-file-name (file &optional rev manual regexp
)
682 "Return a backup file name for REV or the current version of FILE.
683 If MANUAL is non-nil it means that a name for backups created by
684 the user should be returned; if REGEXP is non-nil that means to return
685 a regexp for matching all such backup files, regardless of the version."
687 (concat (regexp-quote (file-name-nondirectory file
))
688 "\\.~.+" (unless manual
"\\.") "~")
689 (expand-file-name (concat (file-name-nondirectory file
)
690 ".~" (subst-char-in-string
691 ?
/ ?_
(or rev
(vc-working-revision file
)))
692 (unless manual
".") "~")
693 (file-name-directory file
))))
695 (defun vc-delete-automatic-version-backups (file)
696 "Delete all existing automatic version backups for FILE."
700 (directory-files (or (file-name-directory file
) default-directory
) t
701 (vc-version-backup-file-name file nil nil t
)))
702 ;; Don't fail when the directory doesn't exist.
705 (defun vc-make-version-backup (file)
706 "Make a backup copy of FILE, which is assumed in sync with the repository.
707 Before doing that, check if there are any old backups and get rid of them."
708 (unless (and (fboundp 'msdos-long-file-names
)
709 (not (with-no-warnings (msdos-long-file-names))))
710 (vc-delete-automatic-version-backups file
)
712 (copy-file file
(vc-version-backup-file-name file
)
714 ;; It's ok if it doesn't work (e.g. directory not writable),
715 ;; since this is just for efficiency.
718 (concat "Warning: Cannot make version backup; "
719 "diff/revert therefore not local"))))))
721 (defun vc-before-save ()
722 "Function to be called by `basic-save-buffer' (in files.el)."
723 ;; If the file on disk is still in sync with the repository,
724 ;; and version backups should be made, copy the file to
725 ;; another name. This enables local diffs and local reverting.
726 (let ((file buffer-file-name
)
728 (ignore-errors ;Be careful not to prevent saving the file.
729 (and (setq backend
(vc-backend file
))
730 (vc-up-to-date-p file
)
731 (eq (vc-checkout-model backend
(list file
)) 'implicit
)
732 (vc-call-backend backend
'make-version-backups-p file
)
733 (vc-make-version-backup file
)))))
735 (declare-function vc-dir-resynch-file
"vc-dir" (&optional fname
))
737 (defvar vc-dir-buffers nil
"List of vc-dir buffers.")
739 (defun vc-after-save ()
740 "Function to be called by `basic-save-buffer' (in files.el)."
741 ;; If the file in the current buffer is under version control,
742 ;; up-to-date, and locking is not used for the file, set
743 ;; the state to 'edited and redisplay the mode line.
744 (let* ((file buffer-file-name
)
745 (backend (vc-backend file
)))
747 (or (and (equal (vc-file-getprop file
'vc-checkout-time
)
748 (nth 5 (file-attributes file
)))
749 ;; File has been saved in the same second in which
750 ;; it was checked out. Clear the checkout-time
751 ;; to avoid confusion.
752 (vc-file-setprop file
'vc-checkout-time nil
))
754 (eq (vc-checkout-model backend
(list file
)) 'implicit
)
755 (vc-state-refresh file backend
)
756 (vc-mode-line file backend
))
757 ;; Try to avoid unnecessary work, a *vc-dir* buffer is
758 ;; present if this is true.
760 (vc-dir-resynch-file file
))))
762 (defvar vc-menu-entry
763 `(menu-item ,(purecopy "Version Control") vc-menu-map
764 :filter vc-menu-map-filter
))
766 (when (boundp 'menu-bar-tools-menu
)
767 ;; We do not need to worry here about the placement of this entry
768 ;; because menu-bar.el has already created the proper spot for us
769 ;; and this will simply use it.
770 (define-key menu-bar-tools-menu
[vc] vc-menu-entry))
772 (defconst vc-mode-line-map
773 (let ((map (make-sparse-keymap)))
774 (define-key map [mode-line down-mouse-1] vc-menu-entry)
777 (defun vc-mode-line (file &optional backend)
778 "Set `vc-mode' to display type of version control for FILE.
779 The value is set in the current buffer, which should be the buffer
781 If BACKEND is passed use it as the VC backend when computing the result."
782 (interactive (list buffer-file-name))
783 (setq backend (or backend (vc-backend file)))
786 (let* ((ml-string (vc-call-backend backend 'mode-line-string file))
787 (ml-echo (get-text-property 0 'help-echo ml-string)))
791 (if (null vc-display-status)
792 (symbol-name backend)
795 'mouse-face 'mode-line-highlight
798 (format "File under the %s version control system"
800 "\nmouse-1: Version Control menu")
801 'local-map vc-mode-line-map)))))
802 ;; If the user is root, and the file is not owner-writable,
803 ;; then pretend that we can't write it
804 ;; even though we can (because root can write anything).
805 ;; This way, even root cannot modify a file that isn't locked.
806 (and (equal file buffer-file-name)
807 (not buffer-read-only)
808 (zerop (user-real-uid))
809 (zerop (logand (file-modes buffer-file-name) 128))
810 (setq buffer-read-only t)))
811 (force-mode-line-update)
814 (defun vc-default-mode-line-string (backend file)
815 "Return string for placement in modeline by `vc-mode-line' for FILE.
818 \"BACKEND-REV\" if the file is up-to-date
819 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
820 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
821 \"BACKEND@REV\" if the file was locally added
822 \"BACKEND!REV\" if the file contains conflicts or was removed
823 \"BACKEND?REV\" if the file is under VC, but is missing
825 This function assumes that the file is registered."
826 (let* ((backend-name (symbol-name backend))
827 (state (vc-state file backend))
829 (rev (vc-working-revision file backend)))
831 (cond ((or (eq state 'up-to-date)
832 (eq state 'needs-update))
833 (setq state-echo "Up to date file")
834 (concat backend-name "-" rev))
836 (setq state-echo (concat "File locked by" state))
837 (concat backend-name ":" state ":" rev))
839 (setq state-echo "Locally added file")
840 (concat backend-name "@" rev))
841 ((eq state 'conflict)
842 (setq state-echo "File contains conflicts after the last merge")
843 (concat backend-name "!" rev))
845 (setq state-echo "File removed from the VC system")
846 (concat backend-name "!" rev))
848 (setq state-echo "File tracked by the VC system, but missing from the file system")
849 (concat backend-name "?" rev))
851 ;; Not just for the 'edited state, but also a fallback
852 ;; for all other states. Think about different symbols
853 ;; for 'needs-update and 'needs-merge.
854 (setq state-echo "Locally modified file")
855 (concat backend-name ":" rev)))
856 'help-echo (concat state-echo " under the " backend-name
857 " version control system"))))
859 (defun vc-follow-link ()
860 "If current buffer visits a symbolic link, visit the real file.
861 If the real file is already visited in another buffer, make that buffer
862 current, and kill the buffer that visits the link."
863 (let* ((true-buffer (find-buffer-visiting buffer-file-truename))
864 (this-buffer (current-buffer)))
865 (if (eq true-buffer this-buffer)
866 (let ((truename buffer-file-truename))
867 (kill-buffer this-buffer)
868 ;; In principle, we could do something like set-visited-file-name.
869 ;; However, it can't be exactly the same as set-visited-file-name.
870 ;; I'm not going to work out the details right now. -- rms.
871 (set-buffer (find-file-noselect truename)))
872 (set-buffer true-buffer)
873 (kill-buffer this-buffer))))
875 (defun vc-default-find-file-hook (backend)
878 (defun vc-find-file-hook ()
879 "Function for `find-file-hook' activating VC mode if appropriate."
880 ;; Recompute whether file is version controlled,
881 ;; if user has killed the buffer and revisited.
884 (when buffer-file-name
885 (vc-file-clearprops buffer-file-name)
886 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
887 (add-hook 'vc-mode-line-hook 'vc-mode-line nil t)
890 ((setq backend (with-demoted-errors (vc-backend buffer-file-name)))
891 ;; Compute the state and put it in the modeline.
892 (vc-mode-line buffer-file-name backend)
893 (unless vc-make-backup-files
894 ;; Use this variable, not make-backup-files,
895 ;; because this is for things that depend on the file name.
896 (set (make-local-variable 'backup-inhibited) t))
897 ;; Let the backend setup any buffer-local things he needs.
898 (vc-call-backend backend 'find-file-hook))
899 ((let ((link-type (and (not (equal buffer-file-name buffer-file-truename))
900 (vc-backend buffer-file-truename))))
901 (cond ((not link-type) nil) ;Nothing to do.
902 ((eq vc-follow-symlinks nil)
904 "Warning: symbolic link to %s-controlled source file" link-type))
905 ((or (not (eq vc-follow-symlinks 'ask))
906 ;; If we already visited this file by following
907 ;; the link, don't ask again if we try to visit
908 ;; it again. GUD does that, and repeated questions
911 (abbreviate-file-name
912 (file-chase-links buffer-file-name))))
915 (message "Followed link to %s" buffer-file-name)
918 (if (yes-or-no-p (format
919 "Symbolic link to %s-controlled source file; follow link? " link-type))
920 (progn (vc-follow-link)
921 (message "Followed link to %s" buffer-file-name)
924 "Warning: editing through the link bypasses version control")
927 (add-hook 'find-file-hook 'vc-find-file-hook)
929 (defun vc-kill-buffer-hook ()
930 "Discard VC info about a file when we kill its buffer."
931 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
933 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
935 ;; Now arrange for (autoloaded) bindings of the main package.
936 ;; Bindings for this have to go in the global map, as we'll often
937 ;; want to call them from random buffers.
939 ;; Autoloading works fine, but it prevents shortcuts from appearing
940 ;; in the menu because they don't exist yet when the menu is built.
941 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
942 (defvar vc-prefix-map
943 (let ((map (make-sparse-keymap)))
944 (define-key map "a" 'vc-update-change-log)
945 (define-key map "b" 'vc-switch-backend)
946 (define-key map "c" 'vc-rollback)
947 (define-key map "d" 'vc-dir)
948 (define-key map "g" 'vc-annotate)
949 (define-key map "h" 'vc-insert-headers)
950 (define-key map "i" 'vc-register)
951 (define-key map "l" 'vc-print-log)
952 (define-key map "L" 'vc-print-root-log)
953 (define-key map "I" 'vc-log-incoming)
954 (define-key map "O" 'vc-log-outgoing)
955 (define-key map "m" 'vc-merge)
956 (define-key map "r" 'vc-retrieve-tag)
957 (define-key map "s" 'vc-create-tag)
958 (define-key map "u" 'vc-revert)
959 (define-key map "v" 'vc-next-action)
960 (define-key map "+" 'vc-update)
961 (define-key map "=" 'vc-diff)
962 (define-key map "D" 'vc-root-diff)
963 (define-key map "~" 'vc-revision-other-window)
965 (fset 'vc-prefix-map vc-prefix-map)
966 (define-key global-map "\C-xv" 'vc-prefix-map)
969 (let ((map (make-sparse-keymap "Version Control")))
970 ;;(define-key map [show-files]
971 ;; '("Show Files under VC" . (vc-directory t)))
972 (define-key map [vc-retrieve-tag]
973 `(menu-item ,(purecopy "Retrieve Tag") vc-retrieve-tag
974 :help ,(purecopy "Retrieve tagged version or branch")))
975 (define-key map [vc-create-tag]
976 `(menu-item ,(purecopy "Create Tag") vc-create-tag
977 :help ,(purecopy "Create version tag")))
978 (define-key map [separator1] menu-bar-separator)
979 (define-key map [vc-annotate]
980 `(menu-item ,(purecopy "Annotate") vc-annotate
981 :help ,(purecopy "Display the edit history of the current file using colors")))
982 (define-key map [vc-rename-file]
983 `(menu-item ,(purecopy "Rename File") vc-rename-file
984 :help ,(purecopy "Rename file")))
985 (define-key map [vc-revision-other-window]
986 `(menu-item ,(purecopy "Show Other Version") vc-revision-other-window
987 :help ,(purecopy "Visit another version of the current file in another window")))
988 (define-key map [vc-diff]
989 `(menu-item ,(purecopy "Compare with Base Version") vc-diff
990 :help ,(purecopy "Compare file set with the base version")))
991 (define-key map [vc-root-diff]
992 `(menu-item ,(purecopy "Compare Tree with Base Version") vc-root-diff
993 :help ,(purecopy "Compare current tree with the base version")))
994 (define-key map [vc-update-change-log]
995 `(menu-item ,(purecopy "Update ChangeLog") vc-update-change-log
996 :help ,(purecopy "Find change log file and add entries from recent version control logs")))
997 (define-key map [vc-log-out]
998 `(menu-item ,(purecopy "Show Outgoing Log") vc-log-outgoing
999 :help ,(purecopy "Show a log of changes that will be sent with a push operation")))
1000 (define-key map [vc-log-in]
1001 `(menu-item ,(purecopy "Show Incoming Log") vc-log-incoming
1002 :help ,(purecopy "Show a log of changes that will be received with a pull operation")))
1003 (define-key map [vc-print-log]
1004 `(menu-item ,(purecopy "Show History") vc-print-log
1005 :help ,(purecopy "List the change log of the current file set in a window")))
1006 (define-key map [vc-print-root-log]
1007 `(menu-item ,(purecopy "Show Top of the Tree History ") vc-print-root-log
1008 :help ,(purecopy "List the change log for the current tree in a window")))
1009 (define-key map [separator2] menu-bar-separator)
1010 (define-key map [vc-insert-header]
1011 `(menu-item ,(purecopy "Insert Header") vc-insert-headers
1012 :help ,(purecopy "Insert headers into a file for use with a version control system.
1014 (define-key map [undo]
1015 `(menu-item ,(purecopy "Undo Last Check-In") vc-rollback
1016 :help ,(purecopy "Remove the most recent changeset committed to the repository")))
1017 (define-key map [vc-revert]
1018 `(menu-item ,(purecopy "Revert to Base Version") vc-revert
1019 :help ,(purecopy "Revert working copies of the selected file set to their repository contents")))
1020 (define-key map [vc-update]
1021 `(menu-item ,(purecopy "Update to Latest Version") vc-update
1022 :help ,(purecopy "Update the current fileset's files to their tip revisions")))
1023 (define-key map [vc-next-action]
1024 `(menu-item ,(purecopy "Check In/Out") vc-next-action
1025 :help ,(purecopy "Do the next logical version control operation on the current fileset")))
1026 (define-key map [vc-register]
1027 `(menu-item ,(purecopy "Register") vc-register
1028 :help ,(purecopy "Register file set into a version control system")))
1029 (define-key map [vc-dir]
1030 `(menu-item ,(purecopy "VC Dir") vc-dir
1031 :help ,(purecopy "Show the VC status of files in a directory")))
1034 (defalias 'vc-menu-map vc-menu-map)
1036 (declare-function vc-responsible-backend "vc" (file))
1038 (defun vc-menu-map-filter (orig-binding)
1039 (if (and (symbolp orig-binding) (fboundp orig-binding))
1040 (setq orig-binding (indirect-function orig-binding)))
1044 (if buffer-file-name
1045 (vc-backend buffer-file-name)
1046 (vc-responsible-backend default-directory))
1048 ;; Give the VC backend a chance to add menu entries
1049 ;; specific for that backend.
1050 (if (null ext-binding)
1052 (append orig-binding
1053 '((ext-menu-separator "--"))
1056 (defun vc-default-extra-menu (backend)
1061 ;; arch-tag: 2e5a6fa7-1d30-48e2-8bd0-e3d335f04f32
1062 ;;; vc-hooks.el ends here