1 ;;; vc-hooks.el --- resident support for version-control
3 ;; Copyright (C) 1992-1996, 1998-2014 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This is the always-loaded portion of VC. It takes care of
27 ;; VC-related activities that are done when you visit a file, so that
28 ;; vc.el itself is loaded only when you use a VC command. See the
29 ;; commentary of vc.el.
33 (eval-when-compile (require 'cl-lib))
37 (defgroup vc-state-faces nil
38 "Faces used in the mode line by the VC state indicator."
43 (defface vc-state-base-face
44 '((default :inherit mode-line))
45 "Base face for VC state indicator."
50 (defface vc-up-to-date-state
51 '((default :inherit vc-state-base-face))
52 "Face for VC modeline state when the file is up to date."
56 (defface vc-needs-update-state
57 '((default :inherit vc-state-base-face))
58 "Face for VC modeline state when the file needs update."
62 (defface vc-locked-state
63 '((default :inherit vc-state-base-face))
64 "Face for VC modeline state when the file locked."
68 (defface vc-locally-added-state
69 '((default :inherit vc-state-base-face))
70 "Face for VC modeline state when the file is locally added."
74 (defface vc-conflict-state
75 '((default :inherit vc-state-base-face))
76 "Face for VC modeline state when the file contains merge conflicts."
80 (defface vc-removed-state
81 '((default :inherit vc-state-base-face))
82 "Face for VC modeline state when the file was removed from the VC system."
86 (defface vc-missing-state
87 '((default :inherit vc-state-base-face))
88 "Face for VC modeline state when the file is missing from the file system."
92 (defface vc-edited-state
93 '((default :inherit vc-state-base-face))
94 "Face for VC modeline state when the file is up to date."
98 ;; Customization Variables (the rest is in vc.el)
100 (defcustom vc-ignore-dir-regexp
101 ;; Stop SMB, automounter, AFS, and DFS host lookups.
102 locate-dominating-stop-dir-regexp
103 "Regexp matching directory names that are not under VC's control.
104 The default regexp prevents fruitless and time-consuming attempts
105 to determine the VC status in directories in which filenames are
106 interpreted as hostnames."
110 (defcustom vc-handled-backends '(RCS CVS SVN SCCS SRC Bzr Git Hg Mtn Arch)
111 ;; RCS, CVS, SVN, SCCS, and SRC come first because they are per-dir
112 ;; rather than per-tree. RCS comes first because of the multibackend
113 ;; support intended to use RCS for local commits (with a remote CVS server).
114 "List of version control backends for which VC will be used.
115 Entries in this list will be tried in order to determine whether a
116 file is under that sort of version control.
117 Removing an entry from the list prevents VC from being activated
118 when visiting a file managed by that backend.
119 An empty list disables VC altogether."
120 :type '(repeat symbol)
124 ;; Note: we don't actually have a darcs back end yet.
125 ;; Also, Meta-CVS (corresponding to MCVS) is unsupported.
126 (defcustom vc-directory-exclusion-list (purecopy '("SCCS" "RCS" "CVS" "MCVS"
127 ".src", ".svn" ".git" ".hg" ".bzr"
128 "_MTN" "_darcs" "{arch}"))
129 "List of directory names to be ignored when walking directory trees."
130 :type '(repeat string)
133 (defcustom vc-make-backup-files nil
134 "If non-nil, backups of registered files are made as with other files.
135 If nil (the default), files covered by version control don't get backups."
140 (defcustom vc-follow-symlinks 'ask
141 "What to do if visiting a symbolic link to a file under version control.
142 Editing such a file through the link bypasses the version control system,
143 which is dangerous and probably not what you want.
145 If this variable is t, VC follows the link and visits the real file,
146 telling you about it in the echo area. If it is `ask', VC asks for
147 confirmation whether it should follow the link. If nil, the link is
148 visited and a warning displayed."
149 :type '(choice (const :tag "Ask for confirmation" ask)
150 (const :tag "Visit link and warn" nil)
151 (const :tag "Follow link" t))
154 (defcustom vc-display-status t
155 "If non-nil, display revision number and lock status in mode line.
156 Otherwise, not displayed."
161 (defcustom vc-consult-headers t
162 "If non-nil, identify work files by searching for version headers."
166 (defcustom vc-keep-workfiles t
167 "Whether to keep work files on disk after commits, on a locking VCS.
168 This variable has no effect on modern merging-based version
173 ;; If you fix bug#11490, probably you can set this back to nil.
174 (defcustom vc-mistrust-permissions t
175 "If non-nil, don't assume permissions/ownership track version-control status.
176 If nil, do rely on the permissions.
177 See also variable `vc-consult-headers'."
178 :version "24.3" ; nil->t, bug#11490
182 (defun vc-mistrust-permissions (file)
183 "Internal access function to variable `vc-mistrust-permissions' for FILE."
184 (or (eq vc-mistrust-permissions 't)
185 (and vc-mistrust-permissions
186 (funcall vc-mistrust-permissions
187 (vc-backend-subdirectory-name file)))))
189 (defcustom vc-stay-local 'only-file
190 "Non-nil means use local operations when possible for remote repositories.
191 This avoids slow queries over the network and instead uses heuristics
192 and past information to determine the current status of a file.
194 If value is the symbol `only-file', `vc-dir' will connect to the
195 server, but heuristics will be used to determine the status for
196 all other VC operations.
198 The value can also be a regular expression or list of regular
199 expressions to match against the host name of a repository; then VC
200 only stays local for hosts that match it. Alternatively, the value
201 can be a list of regular expressions where the first element is the
202 symbol `except'; then VC always stays local except for hosts matched
203 by these regular expressions."
205 (const :tag "Always stay local" t)
206 (const :tag "Only for file operations" only-file)
207 (const :tag "Don't stay local" nil)
208 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
209 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
210 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
211 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
215 (defun vc-stay-local-p (file &optional backend)
216 "Return non-nil if VC should stay local when handling FILE.
217 This uses the `repository-hostname' backend operation.
218 If FILE is a list of files, return non-nil if any of them
219 individually should stay local."
221 (delq nil (mapcar (lambda (arg) (vc-stay-local-p arg backend)) file))
222 (setq backend (or backend (vc-backend file)))
223 (let* ((sym (vc-make-backend-sym backend 'stay-local))
224 (stay-local (if (boundp sym) (symbol-value sym) vc-stay-local)))
225 (if (symbolp stay-local) stay-local
226 (let ((dirname (if (file-directory-p file)
227 (directory-file-name file)
228 (file-name-directory file))))
230 (or (vc-file-getprop dirname 'vc-stay-local-p)
232 dirname 'vc-stay-local-p
233 (let ((hostname (vc-call-backend
234 backend 'repository-hostname dirname)))
238 (if (eq (car-safe stay-local) 'except)
239 (setq default nil stay-local (cdr stay-local)))
240 (when (consp stay-local)
242 (mapconcat 'identity stay-local "\\|")))
243 (if (if (string-match stay-local hostname)
244 default (not default))
247 ;;; This is handled specially now.
248 ;; Tell Emacs about this new kind of minor mode
249 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
252 (put 'vc-mode 'risky-local-variable t)
253 (make-variable-buffer-local 'vc-mode)
254 (put 'vc-mode 'permanent-local t)
256 ;;; We signal this error when we try to do something a VC backend
257 ;;; doesn't support. Two arguments: the method that's not supported
259 (define-error 'vc-not-supported "VC method not implemented for backend")
261 (defun vc-mode (&optional _arg)
262 ;; Dummy function for C-h m
263 "Version Control minor mode.
264 This minor mode is automatically activated whenever you visit a file under
265 control of one of the revision control systems in `vc-handled-backends'.
266 VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
269 (defmacro vc-error-occurred (&rest body)
270 `(condition-case nil (progn ,@body nil) (error t)))
272 ;; We need a notion of per-file properties because the version
273 ;; control state of a file is expensive to derive --- we compute
274 ;; them when the file is initially found, keep them up to date
275 ;; during any subsequent VC operations, and forget them when
276 ;; the buffer is killed.
278 (defvar vc-file-prop-obarray (make-vector 17 0)
279 "Obarray for per-file properties.")
281 (defvar vc-touched-properties nil)
283 (defun vc-file-setprop (file property value)
284 "Set per-file VC PROPERTY for FILE to VALUE."
285 (if (and vc-touched-properties
286 (not (memq property vc-touched-properties)))
287 (setq vc-touched-properties (append (list property)
288 vc-touched-properties)))
289 (put (intern file vc-file-prop-obarray) property value))
291 (defun vc-file-getprop (file property)
292 "Get per-file VC PROPERTY for FILE."
293 (get (intern file vc-file-prop-obarray) property))
295 (defun vc-file-clearprops (file)
296 "Clear all VC properties of FILE."
297 (if (boundp 'vc-parent-buffer)
298 (kill-local-variable 'vc-parent-buffer))
299 (setplist (intern file vc-file-prop-obarray) nil))
302 ;; We keep properties on each symbol naming a backend as follows:
303 ;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
305 (defun vc-make-backend-sym (backend sym)
306 "Return BACKEND-specific version of VC symbol SYM."
307 (intern (concat "vc-" (downcase (symbol-name backend))
308 "-" (symbol-name sym))))
310 (defun vc-find-backend-function (backend fun)
311 "Return BACKEND-specific implementation of FUN.
312 If there is no such implementation, return the default implementation;
313 if that doesn't exist either, return nil."
314 (let ((f (vc-make-backend-sym backend fun)))
316 ;; Load vc-BACKEND.el if needed.
317 (require (intern (concat "vc-" (downcase (symbol-name backend)))))
319 (let ((def (vc-make-backend-sym 'default fun)))
320 (if (fboundp def) (cons def backend) nil))))))
322 (defun vc-call-backend (backend function-name &rest args)
323 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
326 (apply 'vc-BACKEND-FUN ARGS)
328 if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
331 (apply 'vc-default-FUN BACKEND ARGS)
333 It is usually called via the `vc-call' macro."
334 (let ((f (assoc function-name (get backend 'vc-functions))))
335 (if f (setq f (cdr f))
336 (setq f (vc-find-backend-function backend function-name))
337 (push (cons function-name f) (get backend 'vc-functions)))
340 (signal 'vc-not-supported (list function-name backend)))
341 ((consp f) (apply (car f) (cdr f) args))
342 (t (apply f args)))))
344 (defmacro vc-call (fun file &rest args)
345 "A convenience macro for calling VC backend functions.
346 Functions called by this macro must accept FILE as the first argument.
347 ARGS specifies any additional arguments. FUN should be unquoted.
348 BEWARE!! FILE is evaluated twice!!"
349 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
351 (defsubst vc-parse-buffer (pattern i)
352 "Find PATTERN in the current buffer and return its Ith submatch."
353 (goto-char (point-min))
354 (if (re-search-forward pattern nil t)
357 (defun vc-insert-file (file &optional limit blocksize)
358 "Insert the contents of FILE into the current buffer.
360 Optional argument LIMIT is a regexp. If present, the file is inserted
361 in chunks of size BLOCKSIZE (default 8 kByte), until the first
362 occurrence of LIMIT is found. Anything from the start of that occurrence
363 to the end of the buffer is then deleted. The function returns
364 non-nil if FILE exists and its contents were successfully inserted."
366 (when (file-exists-p file)
368 (insert-file-contents file)
369 (unless blocksize (setq blocksize 8192))
372 (and (< 0 (cadr (insert-file-contents
373 file nil filepos (cl-incf filepos blocksize))))
374 (progn (beginning-of-line)
375 (let ((pos (re-search-forward limit nil 'move)))
376 (when pos (delete-region (match-beginning 0)
379 (set-buffer-modified-p nil)
382 (defun vc-find-root (file witness)
383 "Find the root of a checked out project.
384 The function walks up the directory tree from FILE looking for WITNESS.
385 If WITNESS if not found, return nil, otherwise return the root."
386 (let ((locate-dominating-stop-dir-regexp
387 (or vc-ignore-dir-regexp locate-dominating-stop-dir-regexp)))
388 (locate-dominating-file file witness)))
390 ;; Access functions to file properties
391 ;; (Properties should be _set_ using vc-file-setprop, but
392 ;; _retrieved_ only through these functions, which decide
393 ;; if the property is already known or not. A property should
394 ;; only be retrieved by vc-file-getprop if there is no
397 ;; properties indicating the backend being used for FILE
399 (defun vc-registered (file)
400 "Return non-nil if FILE is registered in a version control system.
402 This function performs the check each time it is called. To rely
403 on the result of a previous call, use `vc-backend' instead. If the
404 file was previously registered under a certain backend, then that
405 backend is tried first."
408 ((and (file-name-directory file)
409 (string-match vc-ignore-dir-regexp (file-name-directory file)))
411 ((and (boundp 'file-name-handler-alist)
412 (setq handler (find-file-name-handler file 'vc-registered)))
413 ;; handler should set vc-backend and return t if registered
414 (funcall handler 'vc-registered file))
416 ;; There is no file name handler.
417 ;; Try vc-BACKEND-registered for each handled BACKEND.
419 (let ((backend (vc-file-getprop file 'vc-backend)))
422 (and (vc-call-backend b 'registered file)
423 (vc-file-setprop file 'vc-backend b)
425 (if (or (not backend) (eq backend 'none))
427 (cons backend vc-handled-backends))))
428 ;; File is not registered.
429 (vc-file-setprop file 'vc-backend 'none)
432 (defun vc-backend (file-or-list)
433 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
434 If the argument is a list, the files must all have the same back end."
435 ;; `file' can be nil in several places (typically due to the use of
436 ;; code like (vc-backend buffer-file-name)).
437 (cond ((stringp file-or-list)
438 (let ((property (vc-file-getprop file-or-list 'vc-backend)))
439 ;; Note that internally, Emacs remembers unregistered
440 ;; files by setting the property to `none'.
441 (cond ((eq property 'none) nil)
443 ;; vc-registered sets the vc-backend property
444 (t (if (vc-registered file-or-list)
445 (vc-file-getprop file-or-list 'vc-backend)
447 ((and file-or-list (listp file-or-list))
448 (vc-backend (car file-or-list)))
453 (defun vc-backend-subdirectory-name (file)
454 "Return where the repository for the current directory is kept."
455 (symbol-name (vc-backend file)))
457 (defun vc-checkout-model (backend files)
458 "Indicate how FILES are checked out.
460 If FILES are not registered, this function always returns nil.
461 For registered files, the possible values are:
463 'implicit FILES are always writable, and checked out `implicitly'
464 when the user saves the first changes to the file.
466 'locking FILES are read-only if up-to-date; user must type
467 \\[vc-next-action] before editing. Strict locking
470 'announce FILES are read-only if up-to-date; user must type
471 \\[vc-next-action] before editing. But other users
472 may be editing at the same time."
473 (vc-call-backend backend 'checkout-model files))
475 (defun vc-user-login-name (file)
476 "Return the name under which the user accesses the given FILE."
477 (or (and (eq (string-match tramp-file-name-regexp file) 0)
478 ;; tramp case: execute "whoami" via tramp
479 (let ((default-directory (file-name-directory file))
480 process-file-side-effects)
482 (if (not (zerop (process-file "whoami" nil t)))
483 ;; fall through if "whoami" didn't work
485 ;; remove trailing newline
486 (delete-region (1- (point-max)) (point-max))
490 ;; if user-login-name is nil, return the UID as a string
491 (number-to-string (user-uid))))
493 (defun vc-state (file &optional backend)
494 "Return the version control state of FILE.
496 A return of nil from this function means we have no information on the
497 status of this file. Otherwise, the value returned is one of:
499 'up-to-date The working file is unmodified with respect to the
500 latest version on the current branch, and not locked.
502 'edited The working file has been edited by the user. If
503 locking is used for the file, this state means that
504 the current version is locked by the calling user.
505 This status should *not* be reported for files
506 which have a changed mtime but the same content
509 USER The current version of the working file is locked by
510 some other USER (a string).
512 'needs-update The file has not been edited by the user, but there is
513 a more recent version on the current branch stored
516 'needs-merge The file has been edited by the user, and there is also
517 a more recent version on the current branch stored in
518 the repository. This state can only occur if locking
519 is not used for the file.
521 'unlocked-changes The working version of the file is not locked,
522 but the working file has been changed with respect
523 to that version. This state can only occur for files
524 with locking; it represents an erroneous condition that
525 should be resolved by the user (vc-next-action will
526 prompt the user to do it).
528 'added Scheduled to go into the repository on the next commit.
529 Often represented by vc-working-revision = \"0\" in VCSes
530 with monotonic IDs like Subversion and Mercurial.
532 'removed Scheduled to be deleted from the repository on next commit.
534 'conflict The file contains conflicts as the result of a merge.
535 For now the conflicts are text conflicts. In the
536 future this might be extended to deal with metadata
539 'missing The file is not present in the file system, but the VC
540 system still tracks it.
542 'ignored The file showed up in a dir-status listing with a flag
543 indicating the version-control system is ignoring it,
544 Note: This property is not set reliably (some VCSes
545 don't have useful directory-status commands) so assume
546 that any file with vc-state nil might be ignorable
547 without VC knowing it.
549 'unregistered The file is not under version control."
551 ;; Note: in Emacs 22 and older, return of nil meant the file was
552 ;; unregistered. This is potentially a source of
553 ;; backward-compatibility bugs.
555 ;; FIXME: New (sub)states needed (?):
556 ;; - `copied' and `moved' (might be handled by `removed' and `added')
557 (or (vc-file-getprop file 'vc-state)
558 (when (> (length file) 0) ;Why?? --Stef
559 (setq backend (or backend (vc-backend file)))
561 (vc-state-refresh file backend)))))
563 (defun vc-state-refresh (file backend)
564 "Quickly recompute the `state' of FILE."
567 (vc-call-backend backend 'state-heuristic file)))
569 (defsubst vc-up-to-date-p (file)
570 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
571 (eq (vc-state file) 'up-to-date))
573 (defun vc-default-state-heuristic (backend file)
574 "Default implementation of vc-BACKEND-state-heuristic.
575 It simply calls the real state computation function `vc-BACKEND-state'
576 and does not employ any heuristic at all."
577 (vc-call-backend backend 'state file))
579 (defun vc-workfile-unchanged-p (file)
580 "Return non-nil if FILE has not changed since the last checkout."
581 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
582 (lastmod (nth 5 (file-attributes file))))
583 ;; This is a shortcut for determining when the workfile is
584 ;; unchanged. It can fail under some circumstances; see the
585 ;; discussion in bug#694.
586 (if (and checkout-time
587 ;; Tramp and Ange-FTP return this when they don't know the time.
588 (not (equal lastmod '(0 0))))
589 (equal checkout-time lastmod)
590 (let ((unchanged (vc-call workfile-unchanged-p file)))
591 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
594 (defun vc-default-workfile-unchanged-p (backend file)
595 "Check if FILE is unchanged by diffing against the repository version.
596 Return non-nil if FILE is unchanged."
597 (zerop (condition-case err
598 ;; If the implementation supports it, let the output
599 ;; go to *vc*, not *vc-diff*, since this is an internal call.
600 (vc-call-backend backend 'diff (list file) nil nil "*vc*")
601 (wrong-number-of-arguments
602 ;; If this error came from the above call to vc-BACKEND-diff,
603 ;; try again without the optional buffer argument (for
604 ;; backward compatibility). Otherwise, resignal.
605 (if (or (not (eq (cadr err)
607 (vc-find-backend-function backend 'diff))))
608 (not (eq (cl-caddr err) 4)))
609 (signal (car err) (cdr err))
610 (vc-call-backend backend 'diff (list file)))))))
612 (defun vc-working-revision (file &optional backend)
613 "Return the repository version from which FILE was checked out.
614 If FILE is not registered, this function always returns nil."
615 (or (vc-file-getprop file 'vc-working-revision)
617 (setq backend (or backend (vc-backend file)))
619 (vc-file-setprop file 'vc-working-revision
620 (vc-call-backend backend 'working-revision file))))))
622 ;; Backward compatibility.
623 (define-obsolete-function-alias
624 'vc-workfile-version 'vc-working-revision "23.1")
625 (defun vc-default-working-revision (backend file)
627 "`working-revision' not found: using the old `workfile-version' instead")
628 (vc-call-backend backend 'workfile-version file))
630 (defun vc-default-registered (backend file)
631 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
632 (let ((sym (vc-make-backend-sym backend 'master-templates)))
633 (unless (get backend 'vc-templates-grabbed)
634 (put backend 'vc-templates-grabbed t))
635 (let ((result (vc-check-master-templates file (symbol-value sym))))
637 (vc-file-setprop file 'vc-master-name result)
638 nil)))) ; Not registered
641 (defun vc-possible-master (s dirname basename)
643 ((stringp s) (format s dirname basename))
645 ;; The template is a function to invoke. If the
646 ;; function returns non-nil, that means it has found a
647 ;; master. For backward compatibility, we also handle
648 ;; the case that the function throws a 'found atom
649 ;; and a pair (cons MASTER-FILE BACKEND).
650 (let ((result (catch 'found (funcall s dirname basename))))
651 (if (consp result) (car result) result)))))
653 (defun vc-check-master-templates (file templates)
654 "Return non-nil if there is a master corresponding to FILE.
656 TEMPLATES is a list of strings or functions. If an element is a
657 string, it must be a control string as required by `format', with two
658 string placeholders, such as \"%sRCS/%s,v\". The directory part of
659 FILE is substituted for the first placeholder, the basename of FILE
660 for the second. If a file with the resulting name exists, it is taken
661 as the master of FILE, and returned.
663 If an element of TEMPLATES is a function, it is called with the
664 directory part and the basename of FILE as arguments. It should
665 return non-nil if it finds a master; that value is then returned by
667 (let ((dirname (or (file-name-directory file) ""))
668 (basename (file-name-nondirectory file)))
672 (let ((trial (vc-possible-master s dirname basename)))
673 (when (and trial (file-exists-p trial)
674 ;; Make sure the file we found with name
675 ;; TRIAL is not the source file itself.
676 ;; That can happen with RCS-style names if
677 ;; the file name is truncated (e.g. to 14
678 ;; chars). See if either directory or
679 ;; attributes differ.
680 (or (not (string= dirname
681 (file-name-directory trial)))
682 (not (equal (file-attributes file)
683 (file-attributes trial)))))
684 (throw 'found trial))))
688 ;; toggle-read-only is obsolete since 24.3, but since vc-t-r-o was made
689 ;; obsolete earlier, it is ok for the latter to be an alias to the former,
690 ;; since the latter will be removed first. We can't just make it
691 ;; an alias for read-only-mode, since that is not 100% the same.
692 (defalias 'vc-toggle-read-only 'toggle-read-only)
693 (make-obsolete 'vc-toggle-read-only
694 "use `read-only-mode' instead (or `toggle-read-only' in older versions of Emacs)."
697 (defun vc-default-make-version-backups-p (_backend _file)
698 "Return non-nil if unmodified versions should be backed up locally.
699 The default is to switch off this feature."
702 (defun vc-version-backup-file-name (file &optional rev manual regexp)
703 "Return a backup file name for REV or the current version of FILE.
704 If MANUAL is non-nil it means that a name for backups created by
705 the user should be returned; if REGEXP is non-nil that means to return
706 a regexp for matching all such backup files, regardless of the version."
708 (concat (regexp-quote (file-name-nondirectory file))
709 "\\.~.+" (unless manual "\\.") "~")
710 (expand-file-name (concat (file-name-nondirectory file)
711 ".~" (subst-char-in-string
712 ?/ ?_ (or rev (vc-working-revision file)))
713 (unless manual ".") "~")
714 (file-name-directory file))))
716 (defun vc-delete-automatic-version-backups (file)
717 "Delete all existing automatic version backups for FILE."
721 (directory-files (or (file-name-directory file) default-directory) t
722 (vc-version-backup-file-name file nil nil t)))
723 ;; Don't fail when the directory doesn't exist.
726 (defun vc-make-version-backup (file)
727 "Make a backup copy of FILE, which is assumed in sync with the repository.
728 Before doing that, check if there are any old backups and get rid of them."
729 (unless (and (fboundp 'msdos-long-file-names)
730 (not (with-no-warnings (msdos-long-file-names))))
731 (vc-delete-automatic-version-backups file)
733 (copy-file file (vc-version-backup-file-name file)
735 ;; It's ok if it doesn't work (e.g. directory not writable),
736 ;; since this is just for efficiency.
739 (concat "Warning: Cannot make version backup; "
740 "diff/revert therefore not local"))))))
742 (defun vc-before-save ()
743 "Function to be called by `basic-save-buffer' (in files.el)."
744 ;; If the file on disk is still in sync with the repository,
745 ;; and version backups should be made, copy the file to
746 ;; another name. This enables local diffs and local reverting.
747 (let ((file buffer-file-name)
749 (ignore-errors ;Be careful not to prevent saving the file.
750 (unless (file-exists-p file)
751 (vc-file-clearprops file))
752 (and (setq backend (vc-backend file))
753 (vc-up-to-date-p file)
754 (eq (vc-checkout-model backend (list file)) 'implicit)
755 (vc-call-backend backend 'make-version-backups-p file)
756 (vc-make-version-backup file)))))
758 (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
760 (defvar vc-dir-buffers nil "List of vc-dir buffers.")
762 (defun vc-after-save ()
763 "Function to be called by `basic-save-buffer' (in files.el)."
764 ;; If the file in the current buffer is under version control,
765 ;; up-to-date, and locking is not used for the file, set
766 ;; the state to 'edited and redisplay the mode line.
767 (let* ((file buffer-file-name)
768 (backend (vc-backend file)))
771 ((eq (vc-checkout-model backend (list file)) 'implicit)
772 ;; If the file was saved in the same second in which it was
773 ;; checked out, clear the checkout-time to avoid confusion.
774 (if (equal (vc-file-getprop file 'vc-checkout-time)
775 (nth 5 (file-attributes file)))
776 (vc-file-setprop file 'vc-checkout-time nil))
777 (if (vc-state-refresh file backend)
778 (vc-mode-line file backend)))
779 ;; If we saved an unlocked file on a locking based VCS, that
780 ;; file is not longer up-to-date.
781 ((eq (vc-file-getprop file 'vc-state) 'up-to-date)
782 (vc-file-setprop file 'vc-state nil)))
783 ;; Resynch *vc-dir* buffers, if any are present.
785 (vc-dir-resynch-file file))))
787 (defvar vc-menu-entry
788 `(menu-item ,(purecopy "Version Control") vc-menu-map
789 :filter vc-menu-map-filter))
791 (when (boundp 'menu-bar-tools-menu)
792 ;; We do not need to worry here about the placement of this entry
793 ;; because menu-bar.el has already created the proper spot for us
794 ;; and this will simply use it.
795 (define-key menu-bar-tools-menu [vc] vc-menu-entry))
797 (defconst vc-mode-line-map
798 (let ((map (make-sparse-keymap)))
799 (define-key map [mode-line down-mouse-1] vc-menu-entry)
802 (defun vc-mode-line (file &optional backend)
803 "Set `vc-mode' to display type of version control for FILE.
804 The value is set in the current buffer, which should be the buffer
806 If BACKEND is passed use it as the VC backend when computing the result."
807 (interactive (list buffer-file-name))
808 (setq backend (or backend (vc-backend file)))
811 (let* ((ml-string (vc-call-backend backend 'mode-line-string file))
812 (ml-echo (get-text-property 0 'help-echo ml-string)))
816 (if (null vc-display-status)
817 (symbol-name backend)
820 'mouse-face 'mode-line-highlight
823 (format "File under the %s version control system"
825 "\nmouse-1: Version Control menu")
826 'local-map vc-mode-line-map)))))
827 ;; If the user is root, and the file is not owner-writable,
828 ;; then pretend that we can't write it
829 ;; even though we can (because root can write anything).
830 ;; This way, even root cannot modify a file that isn't locked.
831 (and (equal file buffer-file-name)
832 (not buffer-read-only)
833 (zerop (user-real-uid))
834 (zerop (logand (file-modes buffer-file-name) 128))
835 (setq buffer-read-only t)))
836 (force-mode-line-update)
839 (defun vc-default-mode-line-string (backend file)
840 "Return a string for `vc-mode-line' to put in the mode line for FILE.
843 \"BACKEND-REV\" if the file is up-to-date
844 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
845 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
846 \"BACKEND@REV\" if the file was locally added
847 \"BACKEND!REV\" if the file contains conflicts or was removed
848 \"BACKEND?REV\" if the file is under VC, but is missing
850 This function assumes that the file is registered."
851 (let* ((backend-name (symbol-name backend))
852 (state (vc-state file backend))
855 (rev (vc-working-revision file backend)))
857 (cond ((or (eq state 'up-to-date)
858 (eq state 'needs-update))
859 (setq state-echo "Up to date file")
860 (setq face 'vc-up-to-date-state)
861 (concat backend-name "-" rev))
863 (setq state-echo (concat "File locked by" state))
864 (setq face 'vc-locked-state)
865 (concat backend-name ":" state ":" rev))
867 (setq state-echo "Locally added file")
868 (setq face 'vc-locally-added-state)
869 (concat backend-name "@" rev))
870 ((eq state 'conflict)
871 (setq state-echo "File contains conflicts after the last merge")
872 (setq face 'vc-conflict-state)
873 (concat backend-name "!" rev))
875 (setq state-echo "File removed from the VC system")
876 (setq face 'vc-removed-state)
877 (concat backend-name "!" rev))
879 (setq state-echo "File tracked by the VC system, but missing from the file system")
880 (setq face 'vc-missing-state)
881 (concat backend-name "?" rev))
883 ;; Not just for the 'edited state, but also a fallback
884 ;; for all other states. Think about different symbols
885 ;; for 'needs-update and 'needs-merge.
886 (setq state-echo "Locally modified file")
887 (setq face 'vc-edited-state)
888 (concat backend-name ":" rev)))
890 'help-echo (concat state-echo " under the " backend-name
891 " version control system"))))
893 (defun vc-follow-link ()
894 "If current buffer visits a symbolic link, visit the real file.
895 If the real file is already visited in another buffer, make that buffer
896 current, and kill the buffer that visits the link."
897 (let* ((true-buffer (find-buffer-visiting buffer-file-truename))
898 (this-buffer (current-buffer)))
899 (if (eq true-buffer this-buffer)
900 (let ((truename buffer-file-truename))
901 (kill-buffer this-buffer)
902 ;; In principle, we could do something like set-visited-file-name.
903 ;; However, it can't be exactly the same as set-visited-file-name.
904 ;; I'm not going to work out the details right now. -- rms.
905 (set-buffer (find-file-noselect truename)))
906 (set-buffer true-buffer)
907 (kill-buffer this-buffer))))
909 (defun vc-default-find-file-hook (_backend)
912 (defun vc-find-file-hook ()
913 "Function for `find-file-hook' activating VC mode if appropriate."
914 ;; Recompute whether file is version controlled,
915 ;; if user has killed the buffer and revisited.
918 (when buffer-file-name
919 (vc-file-clearprops buffer-file-name)
920 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
921 (add-hook 'vc-mode-line-hook 'vc-mode-line nil t)
924 ((setq backend (with-demoted-errors (vc-backend buffer-file-name)))
925 ;; Compute the state and put it in the mode line.
926 (vc-mode-line buffer-file-name backend)
927 (unless vc-make-backup-files
928 ;; Use this variable, not make-backup-files,
929 ;; because this is for things that depend on the file name.
930 (set (make-local-variable 'backup-inhibited) t))
931 ;; Let the backend setup any buffer-local things he needs.
932 (vc-call-backend backend 'find-file-hook))
933 ((let* ((truename (and buffer-file-truename
934 (expand-file-name buffer-file-truename)))
935 (link-type (and truename
936 (not (equal buffer-file-name truename))
937 (vc-backend truename))))
938 (cond ((not link-type) nil) ;Nothing to do.
939 ((eq vc-follow-symlinks nil)
941 "Warning: symbolic link to %s-controlled source file" link-type))
942 ((or (not (eq vc-follow-symlinks 'ask))
943 ;; Assume we cannot ask, default to yes.
945 ;; Copied from server-start. Seems like there should
946 ;; be a better way to ask "can we get user input?"...
948 (null (cdr (frame-list)))
949 (eq (selected-frame) terminal-frame))
950 ;; If we already visited this file by following
951 ;; the link, don't ask again if we try to visit
952 ;; it again. GUD does that, and repeated questions
955 (abbreviate-file-name
956 (file-chase-links buffer-file-name))))
959 (message "Followed link to %s" buffer-file-name)
962 (if (yes-or-no-p (format
963 "Symbolic link to %s-controlled source file; follow link? " link-type))
964 (progn (vc-follow-link)
965 (message "Followed link to %s" buffer-file-name)
968 "Warning: editing through the link bypasses version control")
971 (add-hook 'find-file-hook 'vc-find-file-hook)
973 (defun vc-kill-buffer-hook ()
974 "Discard VC info about a file when we kill its buffer."
975 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
977 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
979 ;; Now arrange for (autoloaded) bindings of the main package.
980 ;; Bindings for this have to go in the global map, as we'll often
981 ;; want to call them from random buffers.
983 ;; Autoloading works fine, but it prevents shortcuts from appearing
984 ;; in the menu because they don't exist yet when the menu is built.
985 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
986 (defvar vc-prefix-map
987 (let ((map (make-sparse-keymap)))
988 (define-key map "a" 'vc-update-change-log)
989 (define-key map "b" 'vc-switch-backend)
990 (define-key map "c" 'vc-rollback)
991 (define-key map "d" 'vc-dir)
992 (define-key map "g" 'vc-annotate)
993 (define-key map "G" 'vc-ignore)
994 (define-key map "h" 'vc-insert-headers)
995 (define-key map "i" 'vc-register)
996 (define-key map "l" 'vc-print-log)
997 (define-key map "L" 'vc-print-root-log)
998 (define-key map "I" 'vc-log-incoming)
999 (define-key map "O" 'vc-log-outgoing)
1000 (define-key map "m" 'vc-merge)
1001 (define-key map "r" 'vc-retrieve-tag)
1002 (define-key map "s" 'vc-create-tag)
1003 (define-key map "u" 'vc-revert)
1004 (define-key map "v" 'vc-next-action)
1005 (define-key map "+" 'vc-update)
1006 (define-key map "=" 'vc-diff)
1007 (define-key map "D" 'vc-root-diff)
1008 (define-key map "~" 'vc-revision-other-window)
1010 (fset 'vc-prefix-map vc-prefix-map)
1011 (define-key ctl-x-map "v" 'vc-prefix-map)
1014 (let ((map (make-sparse-keymap "Version Control")))
1015 ;;(define-key map [show-files]
1016 ;; '("Show Files under VC" . (vc-directory t)))
1017 (bindings--define-key map [vc-retrieve-tag]
1018 '(menu-item "Retrieve Tag" vc-retrieve-tag
1019 :help "Retrieve tagged version or branch"))
1020 (bindings--define-key map [vc-create-tag]
1021 '(menu-item "Create Tag" vc-create-tag
1022 :help "Create version tag"))
1023 (bindings--define-key map [separator1] menu-bar-separator)
1024 (bindings--define-key map [vc-annotate]
1025 '(menu-item "Annotate" vc-annotate
1026 :help "Display the edit history of the current file using colors"))
1027 (bindings--define-key map [vc-rename-file]
1028 '(menu-item "Rename File" vc-rename-file
1029 :help "Rename file"))
1030 (bindings--define-key map [vc-revision-other-window]
1031 '(menu-item "Show Other Version" vc-revision-other-window
1032 :help "Visit another version of the current file in another window"))
1033 (bindings--define-key map [vc-diff]
1034 '(menu-item "Compare with Base Version" vc-diff
1035 :help "Compare file set with the base version"))
1036 (bindings--define-key map [vc-root-diff]
1037 '(menu-item "Compare Tree with Base Version" vc-root-diff
1038 :help "Compare current tree with the base version"))
1039 (bindings--define-key map [vc-update-change-log]
1040 '(menu-item "Update ChangeLog" vc-update-change-log
1041 :help "Find change log file and add entries from recent version control logs"))
1042 (bindings--define-key map [vc-log-out]
1043 '(menu-item "Show Outgoing Log" vc-log-outgoing
1044 :help "Show a log of changes that will be sent with a push operation"))
1045 (bindings--define-key map [vc-log-in]
1046 '(menu-item "Show Incoming Log" vc-log-incoming
1047 :help "Show a log of changes that will be received with a pull operation"))
1048 (bindings--define-key map [vc-print-log]
1049 '(menu-item "Show History" vc-print-log
1050 :help "List the change log of the current file set in a window"))
1051 (bindings--define-key map [vc-print-root-log]
1052 '(menu-item "Show Top of the Tree History " vc-print-root-log
1053 :help "List the change log for the current tree in a window"))
1054 (bindings--define-key map [separator2] menu-bar-separator)
1055 (bindings--define-key map [vc-insert-header]
1056 '(menu-item "Insert Header" vc-insert-headers
1057 :help "Insert headers into a file for use with a version control system.
1059 (bindings--define-key map [undo]
1060 '(menu-item "Undo Last Check-In" vc-rollback
1061 :enable (let ((backend (if buffer-file-name
1062 (vc-backend buffer-file-name))))
1064 (vc-find-backend-function backend 'rollback)))
1065 :help "Remove the most recent changeset committed to the repository"))
1066 (bindings--define-key map [vc-revert]
1067 '(menu-item "Revert to Base Version" vc-revert
1068 :help "Revert working copies of the selected file set to their repository contents"))
1069 (bindings--define-key map [vc-update]
1070 '(menu-item "Update to Latest Version" vc-update
1071 :help "Update the current fileset's files to their tip revisions"))
1072 (bindings--define-key map [vc-next-action]
1073 '(menu-item "Check In/Out" vc-next-action
1074 :help "Do the next logical version control operation on the current fileset"))
1075 (bindings--define-key map [vc-register]
1076 '(menu-item "Register" vc-register
1077 :help "Register file set into a version control system"))
1078 (bindings--define-key map [vc-ignore]
1079 '(menu-item "Ignore File..." vc-ignore
1080 :help "Ignore a file under current version control system"))
1081 (bindings--define-key map [vc-dir]
1082 '(menu-item "VC Dir" vc-dir
1083 :help "Show the VC status of files in a directory"))
1086 (defalias 'vc-menu-map vc-menu-map)
1088 (declare-function vc-responsible-backend "vc" (file))
1090 (defun vc-menu-map-filter (orig-binding)
1091 (if (and (symbolp orig-binding) (fboundp orig-binding))
1092 (setq orig-binding (indirect-function orig-binding)))
1096 (if buffer-file-name
1097 (vc-backend buffer-file-name)
1098 (vc-responsible-backend default-directory))
1100 ;; Give the VC backend a chance to add menu entries
1101 ;; specific for that backend.
1102 (if (null ext-binding)
1104 (append orig-binding
1105 '((ext-menu-separator "--"))
1108 (defun vc-default-extra-menu (_backend)
1113 ;;; vc-hooks.el ends here