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
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 (defcustom vc-stay-local 'only-file
174 "Non-nil means use local operations when possible for remote repositories.
175 This avoids slow queries over the network and instead uses heuristics
176 and past information to determine the current status of a file.
178 If value is the symbol `only-file', `vc-dir' will connect to the
179 server, but heuristics will be used to determine the status for
180 all other VC operations.
182 The value can also be a regular expression or list of regular
183 expressions to match against the host name of a repository; then VC
184 only stays local for hosts that match it. Alternatively, the value
185 can be a list of regular expressions where the first element is the
186 symbol `except'; then VC always stays local except for hosts matched
187 by these regular expressions."
189 (const :tag "Always stay local" t)
190 (const :tag "Only for file operations" only-file)
191 (const :tag "Don't stay local" nil)
192 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
193 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
194 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
195 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
199 (defun vc-stay-local-p (file &optional backend)
200 "Return non-nil if VC should stay local when handling FILE.
201 This uses the `repository-hostname' backend operation.
202 If FILE is a list of files, return non-nil if any of them
203 individually should stay local."
205 (delq nil (mapcar (lambda (arg) (vc-stay-local-p arg backend)) file))
206 (setq backend (or backend (vc-backend file)))
207 (let* ((sym (vc-make-backend-sym backend 'stay-local))
208 (stay-local (if (boundp sym) (symbol-value sym) vc-stay-local)))
209 (if (symbolp stay-local) stay-local
210 (let ((dirname (if (file-directory-p file)
211 (directory-file-name file)
212 (file-name-directory file))))
214 (or (vc-file-getprop dirname 'vc-stay-local-p)
216 dirname 'vc-stay-local-p
217 (let ((hostname (vc-call-backend
218 backend 'repository-hostname dirname)))
222 (if (eq (car-safe stay-local) 'except)
223 (setq default nil stay-local (cdr stay-local)))
224 (when (consp stay-local)
226 (mapconcat 'identity stay-local "\\|")))
227 (if (if (string-match stay-local hostname)
228 default (not default))
231 ;;; This is handled specially now.
232 ;; Tell Emacs about this new kind of minor mode
233 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
236 (put 'vc-mode 'risky-local-variable t)
237 (make-variable-buffer-local 'vc-mode)
238 (put 'vc-mode 'permanent-local t)
240 ;;; We signal this error when we try to do something a VC backend
241 ;;; doesn't support. Two arguments: the method that's not supported
243 (define-error 'vc-not-supported "VC method not implemented for backend")
245 (defun vc-mode (&optional _arg)
246 ;; Dummy function for C-h m
247 "Version Control minor mode.
248 This minor mode is automatically activated whenever you visit a file under
249 control of one of the revision control systems in `vc-handled-backends'.
250 VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
253 (defmacro vc-error-occurred (&rest body)
254 `(condition-case nil (progn ,@body nil) (error t)))
256 ;; We need a notion of per-file properties because the version
257 ;; control state of a file is expensive to derive --- we compute
258 ;; them when the file is initially found, keep them up to date
259 ;; during any subsequent VC operations, and forget them when
260 ;; the buffer is killed.
262 (defvar vc-file-prop-obarray (make-vector 17 0)
263 "Obarray for per-file properties.")
265 (defvar vc-touched-properties nil)
267 (defun vc-file-setprop (file property value)
268 "Set per-file VC PROPERTY for FILE to VALUE."
269 (if (and vc-touched-properties
270 (not (memq property vc-touched-properties)))
271 (setq vc-touched-properties (append (list property)
272 vc-touched-properties)))
273 (put (intern file vc-file-prop-obarray) property value))
275 (defun vc-file-getprop (file property)
276 "Get per-file VC PROPERTY for FILE."
277 (get (intern file vc-file-prop-obarray) property))
279 (defun vc-file-clearprops (file)
280 "Clear all VC properties of FILE."
281 (if (boundp 'vc-parent-buffer)
282 (kill-local-variable 'vc-parent-buffer))
283 (setplist (intern file vc-file-prop-obarray) nil))
286 ;; We keep properties on each symbol naming a backend as follows:
287 ;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
289 (defun vc-make-backend-sym (backend sym)
290 "Return BACKEND-specific version of VC symbol SYM."
291 (intern (concat "vc-" (downcase (symbol-name backend))
292 "-" (symbol-name sym))))
294 (defun vc-find-backend-function (backend fun)
295 "Return BACKEND-specific implementation of FUN.
296 If there is no such implementation, return the default implementation;
297 if that doesn't exist either, return nil."
298 (let ((f (vc-make-backend-sym backend fun)))
300 ;; Load vc-BACKEND.el if needed.
301 (require (intern (concat "vc-" (downcase (symbol-name backend)))))
303 (let ((def (vc-make-backend-sym 'default fun)))
304 (if (fboundp def) (cons def backend) nil))))))
306 (defun vc-call-backend (backend function-name &rest args)
307 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
310 (apply 'vc-BACKEND-FUN ARGS)
312 if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
315 (apply 'vc-default-FUN BACKEND ARGS)
317 It is usually called via the `vc-call' macro."
318 (let ((f (assoc function-name (get backend 'vc-functions))))
319 (if f (setq f (cdr f))
320 (setq f (vc-find-backend-function backend function-name))
321 (push (cons function-name f) (get backend 'vc-functions)))
324 (signal 'vc-not-supported (list function-name backend)))
325 ((consp f) (apply (car f) (cdr f) args))
326 (t (apply f args)))))
328 (defmacro vc-call (fun file &rest args)
329 "A convenience macro for calling VC backend functions.
330 Functions called by this macro must accept FILE as the first argument.
331 ARGS specifies any additional arguments. FUN should be unquoted.
332 BEWARE!! FILE is evaluated twice!!"
333 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
335 (defsubst vc-parse-buffer (pattern i)
336 "Find PATTERN in the current buffer and return its Ith submatch."
337 (goto-char (point-min))
338 (if (re-search-forward pattern nil t)
341 (defun vc-insert-file (file &optional limit blocksize)
342 "Insert the contents of FILE into the current buffer.
344 Optional argument LIMIT is a regexp. If present, the file is inserted
345 in chunks of size BLOCKSIZE (default 8 kByte), until the first
346 occurrence of LIMIT is found. Anything from the start of that occurrence
347 to the end of the buffer is then deleted. The function returns
348 non-nil if FILE exists and its contents were successfully inserted."
350 (when (file-exists-p file)
352 (insert-file-contents file)
353 (unless blocksize (setq blocksize 8192))
356 (and (< 0 (cadr (insert-file-contents
357 file nil filepos (cl-incf filepos blocksize))))
358 (progn (beginning-of-line)
359 (let ((pos (re-search-forward limit nil 'move)))
360 (when pos (delete-region (match-beginning 0)
363 (set-buffer-modified-p nil)
366 (defun vc-find-root (file witness)
367 "Find the root of a checked out project.
368 The function walks up the directory tree from FILE looking for WITNESS.
369 If WITNESS if not found, return nil, otherwise return the root."
370 (let ((locate-dominating-stop-dir-regexp
371 (or vc-ignore-dir-regexp locate-dominating-stop-dir-regexp)))
372 (locate-dominating-file file witness)))
374 ;; Access functions to file properties
375 ;; (Properties should be _set_ using vc-file-setprop, but
376 ;; _retrieved_ only through these functions, which decide
377 ;; if the property is already known or not. A property should
378 ;; only be retrieved by vc-file-getprop if there is no
381 ;; properties indicating the backend being used for FILE
383 (defun vc-registered (file)
384 "Return non-nil if FILE is registered in a version control system.
386 This function performs the check each time it is called. To rely
387 on the result of a previous call, use `vc-backend' instead. If the
388 file was previously registered under a certain backend, then that
389 backend is tried first."
392 ((and (file-name-directory file)
393 (string-match vc-ignore-dir-regexp (file-name-directory file)))
395 ((and (boundp 'file-name-handler-alist)
396 (setq handler (find-file-name-handler file 'vc-registered)))
397 ;; handler should set vc-backend and return t if registered
398 (funcall handler 'vc-registered file))
400 ;; There is no file name handler.
401 ;; Try vc-BACKEND-registered for each handled BACKEND.
403 (let ((backend (vc-file-getprop file 'vc-backend)))
406 (and (vc-call-backend b 'registered file)
407 (vc-file-setprop file 'vc-backend b)
409 (if (or (not backend) (eq backend 'none))
411 (cons backend vc-handled-backends))))
412 ;; File is not registered.
413 (vc-file-setprop file 'vc-backend 'none)
416 (defun vc-backend (file-or-list)
417 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
418 If the argument is a list, the files must all have the same back end."
419 ;; `file' can be nil in several places (typically due to the use of
420 ;; code like (vc-backend buffer-file-name)).
421 (cond ((stringp file-or-list)
422 (let ((property (vc-file-getprop file-or-list 'vc-backend)))
423 ;; Note that internally, Emacs remembers unregistered
424 ;; files by setting the property to `none'.
425 (cond ((eq property 'none) nil)
427 ;; vc-registered sets the vc-backend property
428 (t (if (vc-registered file-or-list)
429 (vc-file-getprop file-or-list 'vc-backend)
431 ((and file-or-list (listp file-or-list))
432 (vc-backend (car file-or-list)))
437 (defun vc-backend-subdirectory-name (file)
438 "Return where the repository for the current directory is kept."
439 (symbol-name (vc-backend file)))
441 (defun vc-checkout-model (backend files)
442 "Indicate how FILES are checked out.
444 If FILES are not registered, this function always returns nil.
445 For registered files, the possible values are:
447 'implicit FILES are always writable, and checked out `implicitly'
448 when the user saves the first changes to the file.
450 'locking FILES are read-only if up-to-date; user must type
451 \\[vc-next-action] before editing. Strict locking
454 'announce FILES are read-only if up-to-date; user must type
455 \\[vc-next-action] before editing. But other users
456 may be editing at the same time."
457 (vc-call-backend backend 'checkout-model files))
459 (defun vc-user-login-name (file)
460 "Return the name under which the user accesses the given FILE."
461 (or (and (eq (string-match tramp-file-name-regexp file) 0)
462 ;; tramp case: execute "whoami" via tramp
463 (let ((default-directory (file-name-directory file))
464 process-file-side-effects)
466 (if (not (zerop (process-file "whoami" nil t)))
467 ;; fall through if "whoami" didn't work
469 ;; remove trailing newline
470 (delete-region (1- (point-max)) (point-max))
474 ;; if user-login-name is nil, return the UID as a string
475 (number-to-string (user-uid))))
477 (defun vc-state (file &optional backend)
478 "Return the version control state of FILE.
480 A return of nil from this function means we have no information on the
481 status of this file. Otherwise, the value returned is one of:
483 'up-to-date The working file is unmodified with respect to the
484 latest version on the current branch, and not locked.
486 'edited The working file has been edited by the user. If
487 locking is used for the file, this state means that
488 the current version is locked by the calling user.
489 This status should *not* be reported for files
490 which have a changed mtime but the same content
493 USER The current version of the working file is locked by
494 some other USER (a string).
496 'needs-update The file has not been edited by the user, but there is
497 a more recent version on the current branch stored
500 'needs-merge The file has been edited by the user, and there is also
501 a more recent version on the current branch stored in
502 the repository. This state can only occur if locking
503 is not used for the file.
505 'unlocked-changes The working version of the file is not locked,
506 but the working file has been changed with respect
507 to that version. This state can only occur for files
508 with locking; it represents an erroneous condition that
509 should be resolved by the user (vc-next-action will
510 prompt the user to do it).
512 'added Scheduled to go into the repository on the next commit.
513 Often represented by vc-working-revision = \"0\" in VCSes
514 with monotonic IDs like Subversion and Mercurial.
516 'removed Scheduled to be deleted from the repository on next commit.
518 'conflict The file contains conflicts as the result of a merge.
519 For now the conflicts are text conflicts. In the
520 future this might be extended to deal with metadata
523 'missing The file is not present in the file system, but the VC
524 system still tracks it.
526 'ignored The file showed up in a dir-status listing with a flag
527 indicating the version-control system is ignoring it,
528 Note: This property is not set reliably (some VCSes
529 don't have useful directory-status commands) so assume
530 that any file with vc-state nil might be ignorable
531 without VC knowing it.
533 'unregistered The file is not under version control."
535 ;; Note: in Emacs 22 and older, return of nil meant the file was
536 ;; unregistered. This is potentially a source of
537 ;; backward-compatibility bugs.
539 ;; FIXME: New (sub)states needed (?):
540 ;; - `copied' and `moved' (might be handled by `removed' and `added')
541 (or (vc-file-getprop file 'vc-state)
542 (when (> (length file) 0) ;Why?? --Stef
543 (setq backend (or backend (vc-backend file)))
545 (vc-state-refresh file backend)))))
547 (defun vc-state-refresh (file backend)
548 "Quickly recompute the `state' of FILE."
551 (vc-call-backend backend 'state-heuristic file)))
553 (defsubst vc-up-to-date-p (file)
554 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
555 (eq (vc-state file) 'up-to-date))
557 (defun vc-default-state-heuristic (backend file)
558 "Default implementation of vc-BACKEND-state-heuristic.
559 It simply calls the real state computation function `vc-BACKEND-state'
560 and does not employ any heuristic at all."
561 (vc-call-backend backend 'state file))
563 (defun vc-working-revision (file &optional backend)
564 "Return the repository version from which FILE was checked out.
565 If FILE is not registered, this function always returns nil."
566 (or (vc-file-getprop file 'vc-working-revision)
568 (setq backend (or backend (vc-backend file)))
570 (vc-file-setprop file 'vc-working-revision
571 (vc-call-backend backend 'working-revision file))))))
573 ;; Backward compatibility.
574 (define-obsolete-function-alias
575 'vc-workfile-version 'vc-working-revision "23.1")
576 (defun vc-default-working-revision (backend file)
578 "`working-revision' not found: using the old `workfile-version' instead")
579 (vc-call-backend backend 'workfile-version file))
581 (defun vc-default-registered (backend file)
582 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
583 (let ((sym (vc-make-backend-sym backend 'master-templates)))
584 (unless (get backend 'vc-templates-grabbed)
585 (put backend 'vc-templates-grabbed t))
586 (let ((result (vc-check-master-templates file (symbol-value sym))))
588 (vc-file-setprop file 'vc-master-name result)
589 nil)))) ; Not registered
592 (defun vc-possible-master (s dirname basename)
594 ((stringp s) (format s dirname basename))
596 ;; The template is a function to invoke. If the
597 ;; function returns non-nil, that means it has found a
598 ;; master. For backward compatibility, we also handle
599 ;; the case that the function throws a 'found atom
600 ;; and a pair (cons MASTER-FILE BACKEND).
601 (let ((result (catch 'found (funcall s dirname basename))))
602 (if (consp result) (car result) result)))))
604 (defun vc-check-master-templates (file templates)
605 "Return non-nil if there is a master corresponding to FILE.
607 TEMPLATES is a list of strings or functions. If an element is a
608 string, it must be a control string as required by `format', with two
609 string placeholders, such as \"%sRCS/%s,v\". The directory part of
610 FILE is substituted for the first placeholder, the basename of FILE
611 for the second. If a file with the resulting name exists, it is taken
612 as the master of FILE, and returned.
614 If an element of TEMPLATES is a function, it is called with the
615 directory part and the basename of FILE as arguments. It should
616 return non-nil if it finds a master; that value is then returned by
618 (let ((dirname (or (file-name-directory file) ""))
619 (basename (file-name-nondirectory file)))
623 (let ((trial (vc-possible-master s dirname basename)))
624 (when (and trial (file-exists-p trial)
625 ;; Make sure the file we found with name
626 ;; TRIAL is not the source file itself.
627 ;; That can happen with RCS-style names if
628 ;; the file name is truncated (e.g. to 14
629 ;; chars). See if either directory or
630 ;; attributes differ.
631 (or (not (string= dirname
632 (file-name-directory trial)))
633 (not (equal (file-attributes file)
634 (file-attributes trial)))))
635 (throw 'found trial))))
639 ;; toggle-read-only is obsolete since 24.3, but since vc-t-r-o was made
640 ;; obsolete earlier, it is ok for the latter to be an alias to the former,
641 ;; since the latter will be removed first. We can't just make it
642 ;; an alias for read-only-mode, since that is not 100% the same.
643 (defalias 'vc-toggle-read-only 'toggle-read-only)
644 (make-obsolete 'vc-toggle-read-only
645 "use `read-only-mode' instead (or `toggle-read-only' in older versions of Emacs)."
648 (defun vc-default-make-version-backups-p (_backend _file)
649 "Return non-nil if unmodified versions should be backed up locally.
650 The default is to switch off this feature."
653 (defun vc-version-backup-file-name (file &optional rev manual regexp)
654 "Return a backup file name for REV or the current version of FILE.
655 If MANUAL is non-nil it means that a name for backups created by
656 the user should be returned; if REGEXP is non-nil that means to return
657 a regexp for matching all such backup files, regardless of the version."
659 (concat (regexp-quote (file-name-nondirectory file))
660 "\\.~.+" (unless manual "\\.") "~")
661 (expand-file-name (concat (file-name-nondirectory file)
662 ".~" (subst-char-in-string
663 ?/ ?_ (or rev (vc-working-revision file)))
664 (unless manual ".") "~")
665 (file-name-directory file))))
667 (defun vc-delete-automatic-version-backups (file)
668 "Delete all existing automatic version backups for FILE."
672 (directory-files (or (file-name-directory file) default-directory) t
673 (vc-version-backup-file-name file nil nil t)))
674 ;; Don't fail when the directory doesn't exist.
677 (defun vc-make-version-backup (file)
678 "Make a backup copy of FILE, which is assumed in sync with the repository.
679 Before doing that, check if there are any old backups and get rid of them."
680 (unless (and (fboundp 'msdos-long-file-names)
681 (not (with-no-warnings (msdos-long-file-names))))
682 (vc-delete-automatic-version-backups file)
684 (copy-file file (vc-version-backup-file-name file)
686 ;; It's ok if it doesn't work (e.g. directory not writable),
687 ;; since this is just for efficiency.
690 (concat "Warning: Cannot make version backup; "
691 "diff/revert therefore not local"))))))
693 (defun vc-before-save ()
694 "Function to be called by `basic-save-buffer' (in files.el)."
695 ;; If the file on disk is still in sync with the repository,
696 ;; and version backups should be made, copy the file to
697 ;; another name. This enables local diffs and local reverting.
698 (let ((file buffer-file-name)
700 (ignore-errors ;Be careful not to prevent saving the file.
701 (unless (file-exists-p file)
702 (vc-file-clearprops file))
703 (and (setq backend (vc-backend file))
704 (vc-up-to-date-p file)
705 (eq (vc-checkout-model backend (list file)) 'implicit)
706 (vc-call-backend backend 'make-version-backups-p file)
707 (vc-make-version-backup file)))))
709 (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
711 (defvar vc-dir-buffers nil "List of vc-dir buffers.")
713 (defun vc-after-save ()
714 "Function to be called by `basic-save-buffer' (in files.el)."
715 ;; If the file in the current buffer is under version control,
716 ;; up-to-date, and locking is not used for the file, set
717 ;; the state to 'edited and redisplay the mode line.
718 (let* ((file buffer-file-name)
719 (backend (vc-backend file)))
722 ((eq (vc-checkout-model backend (list file)) 'implicit)
723 ;; If the file was saved in the same second in which it was
724 ;; checked out, clear the checkout-time to avoid confusion.
725 (if (equal (vc-file-getprop file 'vc-checkout-time)
726 (nth 5 (file-attributes file)))
727 (vc-file-setprop file 'vc-checkout-time nil))
728 (if (vc-state-refresh file backend)
729 (vc-mode-line file backend)))
730 ;; If we saved an unlocked file on a locking based VCS, that
731 ;; file is not longer up-to-date.
732 ((eq (vc-file-getprop file 'vc-state) 'up-to-date)
733 (vc-file-setprop file 'vc-state nil)))
734 ;; Resynch *vc-dir* buffers, if any are present.
736 (vc-dir-resynch-file file))))
738 (defvar vc-menu-entry
739 `(menu-item ,(purecopy "Version Control") vc-menu-map
740 :filter vc-menu-map-filter))
742 (when (boundp 'menu-bar-tools-menu)
743 ;; We do not need to worry here about the placement of this entry
744 ;; because menu-bar.el has already created the proper spot for us
745 ;; and this will simply use it.
746 (define-key menu-bar-tools-menu [vc] vc-menu-entry))
748 (defconst vc-mode-line-map
749 (let ((map (make-sparse-keymap)))
750 (define-key map [mode-line down-mouse-1] vc-menu-entry)
753 (defun vc-mode-line (file &optional backend)
754 "Set `vc-mode' to display type of version control for FILE.
755 The value is set in the current buffer, which should be the buffer
757 If BACKEND is passed use it as the VC backend when computing the result."
758 (interactive (list buffer-file-name))
759 (setq backend (or backend (vc-backend file)))
762 (let* ((ml-string (vc-call-backend backend 'mode-line-string file))
763 (ml-echo (get-text-property 0 'help-echo ml-string)))
767 (if (null vc-display-status)
768 (symbol-name backend)
771 'mouse-face 'mode-line-highlight
774 (format "File under the %s version control system"
776 "\nmouse-1: Version Control menu")
777 'local-map vc-mode-line-map)))))
778 ;; If the user is root, and the file is not owner-writable,
779 ;; then pretend that we can't write it
780 ;; even though we can (because root can write anything).
781 ;; This way, even root cannot modify a file that isn't locked.
782 (and (equal file buffer-file-name)
783 (not buffer-read-only)
784 (zerop (user-real-uid))
785 (zerop (logand (file-modes buffer-file-name) 128))
786 (setq buffer-read-only t)))
787 (force-mode-line-update)
790 (defun vc-default-mode-line-string (backend file)
791 "Return a string for `vc-mode-line' to put in the mode line for FILE.
794 \"BACKEND-REV\" if the file is up-to-date
795 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
796 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
797 \"BACKEND@REV\" if the file was locally added
798 \"BACKEND!REV\" if the file contains conflicts or was removed
799 \"BACKEND?REV\" if the file is under VC, but is missing
801 This function assumes that the file is registered."
802 (let* ((backend-name (symbol-name backend))
803 (state (vc-state file backend))
806 (rev (vc-working-revision file backend)))
808 (cond ((or (eq state 'up-to-date)
809 (eq state 'needs-update))
810 (setq state-echo "Up to date file")
811 (setq face 'vc-up-to-date-state)
812 (concat backend-name "-" rev))
814 (setq state-echo (concat "File locked by" state))
815 (setq face 'vc-locked-state)
816 (concat backend-name ":" state ":" rev))
818 (setq state-echo "Locally added file")
819 (setq face 'vc-locally-added-state)
820 (concat backend-name "@" rev))
821 ((eq state 'conflict)
822 (setq state-echo "File contains conflicts after the last merge")
823 (setq face 'vc-conflict-state)
824 (concat backend-name "!" rev))
826 (setq state-echo "File removed from the VC system")
827 (setq face 'vc-removed-state)
828 (concat backend-name "!" rev))
830 (setq state-echo "File tracked by the VC system, but missing from the file system")
831 (setq face 'vc-missing-state)
832 (concat backend-name "?" rev))
834 ;; Not just for the 'edited state, but also a fallback
835 ;; for all other states. Think about different symbols
836 ;; for 'needs-update and 'needs-merge.
837 (setq state-echo "Locally modified file")
838 (setq face 'vc-edited-state)
839 (concat backend-name ":" rev)))
841 'help-echo (concat state-echo " under the " backend-name
842 " version control system"))))
844 (defun vc-follow-link ()
845 "If current buffer visits a symbolic link, visit the real file.
846 If the real file is already visited in another buffer, make that buffer
847 current, and kill the buffer that visits the link."
848 (let* ((true-buffer (find-buffer-visiting buffer-file-truename))
849 (this-buffer (current-buffer)))
850 (if (eq true-buffer this-buffer)
851 (let ((truename buffer-file-truename))
852 (kill-buffer this-buffer)
853 ;; In principle, we could do something like set-visited-file-name.
854 ;; However, it can't be exactly the same as set-visited-file-name.
855 ;; I'm not going to work out the details right now. -- rms.
856 (set-buffer (find-file-noselect truename)))
857 (set-buffer true-buffer)
858 (kill-buffer this-buffer))))
860 (defun vc-default-find-file-hook (_backend)
863 (defun vc-find-file-hook ()
864 "Function for `find-file-hook' activating VC mode if appropriate."
865 ;; Recompute whether file is version controlled,
866 ;; if user has killed the buffer and revisited.
869 (when buffer-file-name
870 (vc-file-clearprops buffer-file-name)
871 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
872 (add-hook 'vc-mode-line-hook 'vc-mode-line nil t)
875 ((setq backend (with-demoted-errors (vc-backend buffer-file-name)))
876 ;; Compute the state and put it in the mode line.
877 (vc-mode-line buffer-file-name backend)
878 (unless vc-make-backup-files
879 ;; Use this variable, not make-backup-files,
880 ;; because this is for things that depend on the file name.
881 (set (make-local-variable 'backup-inhibited) t))
882 ;; Let the backend setup any buffer-local things he needs.
883 (vc-call-backend backend 'find-file-hook))
884 ((let* ((truename (and buffer-file-truename
885 (expand-file-name buffer-file-truename)))
886 (link-type (and truename
887 (not (equal buffer-file-name truename))
888 (vc-backend truename))))
889 (cond ((not link-type) nil) ;Nothing to do.
890 ((eq vc-follow-symlinks nil)
892 "Warning: symbolic link to %s-controlled source file" link-type))
893 ((or (not (eq vc-follow-symlinks 'ask))
894 ;; Assume we cannot ask, default to yes.
896 ;; Copied from server-start. Seems like there should
897 ;; be a better way to ask "can we get user input?"...
899 (null (cdr (frame-list)))
900 (eq (selected-frame) terminal-frame))
901 ;; If we already visited this file by following
902 ;; the link, don't ask again if we try to visit
903 ;; it again. GUD does that, and repeated questions
906 (abbreviate-file-name
907 (file-chase-links buffer-file-name))))
910 (message "Followed link to %s" buffer-file-name)
913 (if (yes-or-no-p (format
914 "Symbolic link to %s-controlled source file; follow link? " link-type))
915 (progn (vc-follow-link)
916 (message "Followed link to %s" buffer-file-name)
919 "Warning: editing through the link bypasses version control")
922 (add-hook 'find-file-hook 'vc-find-file-hook)
924 (defun vc-kill-buffer-hook ()
925 "Discard VC info about a file when we kill its buffer."
926 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
928 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
930 ;; Now arrange for (autoloaded) bindings of the main package.
931 ;; Bindings for this have to go in the global map, as we'll often
932 ;; want to call them from random buffers.
934 ;; Autoloading works fine, but it prevents shortcuts from appearing
935 ;; in the menu because they don't exist yet when the menu is built.
936 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
937 (defvar vc-prefix-map
938 (let ((map (make-sparse-keymap)))
939 (define-key map "a" 'vc-update-change-log)
940 (define-key map "b" 'vc-switch-backend)
941 (define-key map "c" 'vc-rollback)
942 (define-key map "d" 'vc-dir)
943 (define-key map "g" 'vc-annotate)
944 (define-key map "G" 'vc-ignore)
945 (define-key map "h" 'vc-insert-headers)
946 (define-key map "i" 'vc-register)
947 (define-key map "l" 'vc-print-log)
948 (define-key map "L" 'vc-print-root-log)
949 (define-key map "I" 'vc-log-incoming)
950 (define-key map "O" 'vc-log-outgoing)
951 (define-key map "m" 'vc-merge)
952 (define-key map "r" 'vc-retrieve-tag)
953 (define-key map "s" 'vc-create-tag)
954 (define-key map "u" 'vc-revert)
955 (define-key map "v" 'vc-next-action)
956 (define-key map "+" 'vc-update)
957 (define-key map "=" 'vc-diff)
958 (define-key map "D" 'vc-root-diff)
959 (define-key map "~" 'vc-revision-other-window)
961 (fset 'vc-prefix-map vc-prefix-map)
962 (define-key ctl-x-map "v" 'vc-prefix-map)
965 (let ((map (make-sparse-keymap "Version Control")))
966 ;;(define-key map [show-files]
967 ;; '("Show Files under VC" . (vc-directory t)))
968 (bindings--define-key map [vc-retrieve-tag]
969 '(menu-item "Retrieve Tag" vc-retrieve-tag
970 :help "Retrieve tagged version or branch"))
971 (bindings--define-key map [vc-create-tag]
972 '(menu-item "Create Tag" vc-create-tag
973 :help "Create version tag"))
974 (bindings--define-key map [separator1] menu-bar-separator)
975 (bindings--define-key map [vc-annotate]
976 '(menu-item "Annotate" vc-annotate
977 :help "Display the edit history of the current file using colors"))
978 (bindings--define-key map [vc-rename-file]
979 '(menu-item "Rename File" vc-rename-file
980 :help "Rename file"))
981 (bindings--define-key map [vc-revision-other-window]
982 '(menu-item "Show Other Version" vc-revision-other-window
983 :help "Visit another version of the current file in another window"))
984 (bindings--define-key map [vc-diff]
985 '(menu-item "Compare with Base Version" vc-diff
986 :help "Compare file set with the base version"))
987 (bindings--define-key map [vc-root-diff]
988 '(menu-item "Compare Tree with Base Version" vc-root-diff
989 :help "Compare current tree with the base version"))
990 (bindings--define-key map [vc-update-change-log]
991 '(menu-item "Update ChangeLog" vc-update-change-log
992 :help "Find change log file and add entries from recent version control logs"))
993 (bindings--define-key map [vc-log-out]
994 '(menu-item "Show Outgoing Log" vc-log-outgoing
995 :help "Show a log of changes that will be sent with a push operation"))
996 (bindings--define-key map [vc-log-in]
997 '(menu-item "Show Incoming Log" vc-log-incoming
998 :help "Show a log of changes that will be received with a pull operation"))
999 (bindings--define-key map [vc-print-log]
1000 '(menu-item "Show History" vc-print-log
1001 :help "List the change log of the current file set in a window"))
1002 (bindings--define-key map [vc-print-root-log]
1003 '(menu-item "Show Top of the Tree History " vc-print-root-log
1004 :help "List the change log for the current tree in a window"))
1005 (bindings--define-key map [separator2] menu-bar-separator)
1006 (bindings--define-key map [vc-insert-header]
1007 '(menu-item "Insert Header" vc-insert-headers
1008 :help "Insert headers into a file for use with a version control system.
1010 (bindings--define-key map [undo]
1011 '(menu-item "Undo Last Check-In" vc-rollback
1012 :enable (let ((backend (if buffer-file-name
1013 (vc-backend buffer-file-name))))
1015 (vc-find-backend-function backend 'rollback)))
1016 :help "Remove the most recent changeset committed to the repository"))
1017 (bindings--define-key map [vc-revert]
1018 '(menu-item "Revert to Base Version" vc-revert
1019 :help "Revert working copies of the selected file set to their repository contents"))
1020 (bindings--define-key map [vc-update]
1021 '(menu-item "Update to Latest Version" vc-update
1022 :help "Update the current fileset's files to their tip revisions"))
1023 (bindings--define-key map [vc-next-action]
1024 '(menu-item "Check In/Out" vc-next-action
1025 :help "Do the next logical version control operation on the current fileset"))
1026 (bindings--define-key map [vc-register]
1027 '(menu-item "Register" vc-register
1028 :help "Register file set into a version control system"))
1029 (bindings--define-key map [vc-ignore]
1030 '(menu-item "Ignore File..." vc-ignore
1031 :help "Ignore a file under current version control system"))
1032 (bindings--define-key map [vc-dir]
1033 '(menu-item "VC Dir" vc-dir
1034 :help "Show the VC status of files in a directory"))
1037 (defalias 'vc-menu-map vc-menu-map)
1039 (declare-function vc-responsible-backend "vc" (file))
1041 (defun vc-menu-map-filter (orig-binding)
1042 (if (and (symbolp orig-binding) (fboundp orig-binding))
1043 (setq orig-binding (indirect-function orig-binding)))
1047 (if buffer-file-name
1048 (vc-backend buffer-file-name)
1049 (vc-responsible-backend default-directory))
1051 ;; Give the VC backend a chance to add menu entries
1052 ;; specific for that backend.
1053 (if (null ext-binding)
1055 (append orig-binding
1056 '((ext-menu-separator "--"))
1059 (defun vc-default-extra-menu (_backend)
1064 ;;; vc-hooks.el ends here