* mail/rmail.el (rmail-buffer-swapped): Fix last change.
[bpt/emacs.git] / lisp / vc-hooks.el
CommitLineData
aae56ea7 1;;; vc-hooks.el --- resident support for version-control
594722a8 2
c90f2757 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
ae940284 4;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
594722a8 5
0e0d9831
GM
6;; Author: FSF (see vc.el for full credits)
7;; Maintainer: Andre Spiegel <spiegel@gnu.org>
594722a8
ER
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
594722a8 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
594722a8
ER
15
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
7d56ad0d 19;; GNU General Public License for more details.
594722a8
ER
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
594722a8
ER
23
24;;; Commentary:
25
0e0d9831
GM
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.
594722a8
ER
30
31;;; Code:
32
c3ce5e29
AS
33(eval-when-compile
34 (require 'cl))
099bd78a 35
e1c0c2d1
KH
36;; Customization Variables (the rest is in vc.el)
37
e18cf2ee 38(defvar vc-ignore-vc-files nil)
5499f9dc 39(make-obsolete-variable 'vc-ignore-vc-files
329a656e
JB
40 "set `vc-handled-backends' to nil to disable VC."
41 "21.1")
5499f9dc 42
e18cf2ee 43(defvar vc-master-templates ())
0d2ce4ef
JB
44(make-obsolete-variable 'vc-master-templates
45 "to define master templates for a given BACKEND, use
5499f9dc 46vc-BACKEND-master-templates. To enable or disable VC for a given
329a656e
JB
47BACKEND, use `vc-handled-backends'."
48 "21.1")
5499f9dc 49
e18cf2ee 50(defvar vc-header-alist ())
329a656e 51(make-obsolete-variable 'vc-header-alist 'vc-BACKEND-header "21.1")
0e0d9831 52
456e749f
SM
53(defcustom vc-ignore-dir-regexp
54 ;; Stop SMB, automounter, AFS, and DFS host lookups.
8cd56959 55 locate-dominating-stop-dir-regexp
43621386 56 "Regexp matching directory names that are not under VC's control.
ce9f8ffb
SM
57The default regexp prevents fruitless and time-consuming attempts
58to determine the VC status in directories in which filenames are
43621386
SM
59interpreted as hostnames."
60 :type 'regexp
61 :group 'vc)
ce9f8ffb 62
db86064f 63(defcustom vc-handled-backends '(RCS CVS SVN SCCS Bzr Git Hg Mtn Arch)
b1dc6d44
SM
64 ;; RCS, CVS, SVN and SCCS come first because they are per-dir
65 ;; rather than per-tree. RCS comes first because of the multibackend
66 ;; support intended to use RCS for local commits (with a remote CVS server).
e5162bc1 67 "List of version control backends for which VC will be used.
0e0d9831
GM
68Entries in this list will be tried in order to determine whether a
69file is under that sort of version control.
70Removing an entry from the list prevents VC from being activated
71when visiting a file managed by that backend.
72An empty list disables VC altogether."
73 :type '(repeat symbol)
dbd3d787 74 :version "23.1"
50bec091 75 :group 'vc)
31888047 76
1f0bee0a 77;; Note: we don't actually have a darcs back end yet.
db86064f 78;; Also, Meta-CVS (corresponsding to MCVS) is unsupported.
527b313d
SS
79(defcustom vc-directory-exclusion-list '("SCCS" "RCS" "CVS" "MCVS"
80 ".svn" ".git" ".hg" ".bzr"
1f0bee0a 81 "_MTN" "_darcs" "{arch}")
9c4b89d5
ER
82 "List of directory names to be ignored when walking directory trees."
83 :type '(repeat string)
84 :group 'vc)
85
14b11401 86(defcustom vc-path nil
e5162bc1 87 "List of extra directories to search for version control commands."
50bec091
KH
88 :type '(repeat directory)
89 :group 'vc)
e1c0c2d1 90
50bec091 91(defcustom vc-make-backup-files nil
e5162bc1 92 "If non-nil, backups of registered files are made as with other files.
50bec091
KH
93If nil (the default), files covered by version control don't get backups."
94 :type 'boolean
8f383484
DL
95 :group 'vc
96 :group 'backup)
594722a8 97
50bec091 98(defcustom vc-follow-symlinks 'ask
e5162bc1 99 "What to do if visiting a symbolic link to a file under version control.
0e0d9831
GM
100Editing such a file through the link bypasses the version control system,
101which is dangerous and probably not what you want.
102
103If this variable is t, VC follows the link and visits the real file,
b8063212
AS
104telling you about it in the echo area. If it is `ask', VC asks for
105confirmation whether it should follow the link. If nil, the link is
50bec091 106visited and a warning displayed."
0e0d9831
GM
107 :type '(choice (const :tag "Ask for confirmation" ask)
108 (const :tag "Visit link and warn" nil)
109 (const :tag "Follow link" t))
50bec091 110 :group 'vc)
b8063212 111
50bec091 112(defcustom vc-display-status t
e5162bc1 113 "If non-nil, display revision number and lock status in modeline.
50bec091
KH
114Otherwise, not displayed."
115 :type 'boolean
116 :group 'vc)
117
198d5c00 118
50bec091 119(defcustom vc-consult-headers t
e5162bc1 120 "If non-nil, identify work files by searching for version headers."
50bec091
KH
121 :type 'boolean
122 :group 'vc)
e1c0c2d1 123
50bec091 124(defcustom vc-keep-workfiles t
e5162bc1 125 "If non-nil, don't delete working files after registering changes.
e1c0c2d1 126If the back-end is CVS, workfiles are always kept, regardless of the
50bec091
KH
127value of this flag."
128 :type 'boolean
129 :group 'vc)
e1c0c2d1 130
50bec091 131(defcustom vc-mistrust-permissions nil
e5162bc1 132 "If non-nil, don't assume permissions/ownership track version-control status.
0e0d9831 133If nil, do rely on the permissions.
50bec091
KH
134See also variable `vc-consult-headers'."
135 :type 'boolean
136 :group 'vc)
e66eac08
AS
137
138(defun vc-mistrust-permissions (file)
0e0d9831 139 "Internal access function to variable `vc-mistrust-permissions' for FILE."
e66eac08
AS
140 (or (eq vc-mistrust-permissions 't)
141 (and vc-mistrust-permissions
0e0d9831 142 (funcall vc-mistrust-permissions
e66eac08
AS
143 (vc-backend-subdirectory-name file)))))
144
5870cb76 145(defcustom vc-stay-local 'only-file
e5162bc1 146 "Non-nil means use local operations when possible for remote repositories.
113414a9
SM
147This avoids slow queries over the network and instead uses heuristics
148and past information to determine the current status of a file.
149
5870cb76
DN
150If value is the symbol `only-file' `vc-dir' will connect to the
151server, but heuristics will be used to determine the status for
152all other VC operations.
153
113414a9
SM
154The value can also be a regular expression or list of regular
155expressions to match against the host name of a repository; then VC
156only stays local for hosts that match it. Alternatively, the value
157can be a list of regular expressions where the first element is the
158symbol `except'; then VC always stays local except for hosts matched
159by these regular expressions."
5870cb76
DN
160 :type '(choice
161 (const :tag "Always stay local" t)
37e14a62 162 (const :tag "Only for file operations" only-file)
113414a9
SM
163 (const :tag "Don't stay local" nil)
164 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
165 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
166 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
167 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
5870cb76 168 :version "23.1"
113414a9
SM
169 :group 'vc)
170
171(defun vc-stay-local-p (file)
172 "Return non-nil if VC should stay local when handling FILE.
33e0847d
TTN
173This uses the `repository-hostname' backend operation.
174If FILE is a list of files, return non-nil if any of them
175individually should stay local."
e4d26892 176 (if (listp file)
33e0847d 177 (delq nil (mapcar 'vc-stay-local-p file))
e4d26892
ER
178 (let* ((backend (vc-backend file))
179 (sym (vc-make-backend-sym backend 'stay-local))
180 (stay-local (if (boundp sym) (symbol-value sym) t)))
181 (if (eq stay-local t) (setq stay-local vc-stay-local))
182 (if (symbolp stay-local) stay-local
183 (let ((dirname (if (file-directory-p file)
184 (directory-file-name file)
185 (file-name-directory file))))
186 (eq 'yes
187 (or (vc-file-getprop dirname 'vc-stay-local-p)
188 (vc-file-setprop
189 dirname 'vc-stay-local-p
190 (let ((hostname (vc-call-backend
191 backend 'repository-hostname dirname)))
192 (if (not hostname)
193 'no
194 (let ((default t))
195 (if (eq (car-safe stay-local) 'except)
196 (setq default nil stay-local (cdr stay-local)))
197 (when (consp stay-local)
198 (setq stay-local
199 (mapconcat 'identity stay-local "\\|")))
200 (if (if (string-match stay-local hostname)
201 default (not default))
202 'yes 'no))))))))))))
113414a9 203
9bd06e95 204;;; This is handled specially now.
594722a8 205;; Tell Emacs about this new kind of minor mode
9bd06e95 206;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
594722a8 207
7bc2b98b 208(make-variable-buffer-local 'vc-mode)
c43e436c 209(put 'vc-mode 'permanent-local t)
594722a8 210
14fef9de
SM
211(defun vc-mode (&optional arg)
212 ;; Dummy function for C-h m
213 "Version Control minor mode.
214This minor mode is automatically activated whenever you visit a file under
215control of one of the revision control systems in `vc-handled-backends'.
216VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
217\\{vc-prefix-map}")
218
099bd78a 219(defmacro vc-error-occurred (&rest body)
becd6193 220 `(condition-case nil (progn ,@body nil) (error t)))
099bd78a 221
594722a8 222;; We need a notion of per-file properties because the version
f2ee4191 223;; control state of a file is expensive to derive --- we compute
0e0d9831 224;; them when the file is initially found, keep them up to date
f2ee4191
RS
225;; during any subsequent VC operations, and forget them when
226;; the buffer is killed.
594722a8 227
becd6193 228(defvar vc-file-prop-obarray (make-vector 17 0)
594722a8
ER
229 "Obarray for per-file properties.")
230
099bd78a
SM
231(defvar vc-touched-properties nil)
232
594722a8 233(defun vc-file-setprop (file property value)
0e0d9831 234 "Set per-file VC PROPERTY for FILE to VALUE."
099bd78a
SM
235 (if (and vc-touched-properties
236 (not (memq property vc-touched-properties)))
237 (setq vc-touched-properties (append (list property)
238 vc-touched-properties)))
594722a8
ER
239 (put (intern file vc-file-prop-obarray) property value))
240
241(defun vc-file-getprop (file property)
099bd78a 242 "Get per-file VC PROPERTY for FILE."
594722a8
ER
243 (get (intern file vc-file-prop-obarray) property))
244
e1c0c2d1 245(defun vc-file-clearprops (file)
0e0d9831 246 "Clear all VC properties of FILE."
f98c9a23 247 (setplist (intern file vc-file-prop-obarray) nil))
f2ee4191 248
0e0d9831
GM
249\f
250;; We keep properties on each symbol naming a backend as follows:
251;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
252
253(defun vc-make-backend-sym (backend sym)
254 "Return BACKEND-specific version of VC symbol SYM."
255 (intern (concat "vc-" (downcase (symbol-name backend))
256 "-" (symbol-name sym))))
257
258(defun vc-find-backend-function (backend fun)
259 "Return BACKEND-specific implementation of FUN.
f8b72742 260If there is no such implementation, return the default implementation;
0e0d9831
GM
261if that doesn't exist either, return nil."
262 (let ((f (vc-make-backend-sym backend fun)))
263 (if (fboundp f) f
264 ;; Load vc-BACKEND.el if needed.
265 (require (intern (concat "vc-" (downcase (symbol-name backend)))))
266 (if (fboundp f) f
267 (let ((def (vc-make-backend-sym 'default fun)))
268 (if (fboundp def) (cons def backend) nil))))))
269
270(defun vc-call-backend (backend function-name &rest args)
271 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
272Calls
273
274 (apply 'vc-BACKEND-FUN ARGS)
275
276if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
277and else calls
278
279 (apply 'vc-default-FUN BACKEND ARGS)
280
281It is usually called via the `vc-call' macro."
0bfb74a9
SM
282 (let ((f (assoc function-name (get backend 'vc-functions))))
283 (if f (setq f (cdr f))
0e0d9831 284 (setq f (vc-find-backend-function backend function-name))
0bfb74a9
SM
285 (push (cons function-name f) (get backend 'vc-functions)))
286 (cond
287 ((null f)
288 (error "Sorry, %s is not implemented for %s" function-name backend))
289 ((consp f) (apply (car f) (cdr f) args))
290 (t (apply f args)))))
0e0d9831
GM
291
292(defmacro vc-call (fun file &rest args)
daffc81a
JR
293 "A convenience macro for calling VC backend functions.
294Functions called by this macro must accept FILE as the first argument.
d4e772c4
JB
295ARGS specifies any additional arguments. FUN should be unquoted.
296BEWARE!! FILE is evaluated twice!!"
0e0d9831 297 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
0e0d9831
GM
298\f
299(defsubst vc-parse-buffer (pattern i)
300 "Find PATTERN in the current buffer and return its Ith submatch."
301 (goto-char (point-min))
302 (if (re-search-forward pattern nil t)
303 (match-string i)))
e1c0c2d1 304
02d383eb 305(defun vc-insert-file (file &optional limit blocksize)
0e0d9831
GM
306 "Insert the contents of FILE into the current buffer.
307
308Optional argument LIMIT is a regexp. If present, the file is inserted
309in chunks of size BLOCKSIZE (default 8 kByte), until the first
f8b72742 310occurrence of LIMIT is found. Anything from the start of that occurrence
e30140ce
AS
311to the end of the buffer is then deleted. The function returns
312non-nil if FILE exists and its contents were successfully inserted."
07de4c3d 313 (erase-buffer)
ff40374a
AS
314 (when (file-exists-p file)
315 (if (not limit)
316 (insert-file-contents file)
b5446276 317 (unless blocksize (setq blocksize 8192))
4d2806e2
SM
318 (let ((filepos 0))
319 (while
320 (and (< 0 (cadr (insert-file-contents
321 file nil filepos (incf filepos blocksize))))
322 (progn (beginning-of-line)
e30140ce 323 (let ((pos (re-search-forward limit nil 'move)))
b5446276
DN
324 (when pos (delete-region (match-beginning 0)
325 (point-max)))
e30140ce 326 (not pos)))))))
ff40374a
AS
327 (set-buffer-modified-p nil)
328 t))
02d383eb 329
3dd2acc9 330(defun vc-find-root (file witness)
ce9f8ffb
SM
331 "Find the root of a checked out project.
332The function walks up the directory tree from FILE looking for WITNESS.
3dd2acc9 333If WITNESS if not found, return nil, otherwise return the root."
8cd56959
SM
334 (let ((locate-dominating-stop-dir-regexp
335 (or vc-ignore-dir-regexp locate-dominating-stop-dir-regexp)))
336 (locate-dominating-file file witness)))
37e14a62 337
14fef9de
SM
338;; Access functions to file properties
339;; (Properties should be _set_ using vc-file-setprop, but
340;; _retrieved_ only through these functions, which decide
d4e772c4 341;; if the property is already known or not. A property should
14fef9de
SM
342;; only be retrieved by vc-file-getprop if there is no
343;; access function.)
02d383eb 344
14fef9de 345;; properties indicating the backend being used for FILE
e1c0c2d1 346
0e0d9831
GM
347(defun vc-registered (file)
348 "Return non-nil if FILE is registered in a version control system.
349
1f22ad24
AS
350This function performs the check each time it is called. To rely
351on the result of a previous call, use `vc-backend' instead. If the
352file was previously registered under a certain backend, then that
353backend is tried first."
0e0d9831 354 (let (handler)
ce9f8ffb 355 (cond
8cd56959
SM
356 ((and (file-name-directory file)
357 (string-match vc-ignore-dir-regexp (file-name-directory file)))
44cd688b 358 nil)
ce9f8ffb
SM
359 ((and (boundp 'file-name-handler-alist)
360 (setq handler (find-file-name-handler file 'vc-registered)))
361 ;; handler should set vc-backend and return t if registered
362 (funcall handler 'vc-registered file))
363 (t
0e0d9831
GM
364 ;; There is no file name handler.
365 ;; Try vc-BACKEND-registered for each handled BACKEND.
366 (catch 'found
1f22ad24 367 (let ((backend (vc-file-getprop file 'vc-backend)))
a549ce70 368 (mapc
1f22ad24
AS
369 (lambda (b)
370 (and (vc-call-backend b 'registered file)
371 (vc-file-setprop file 'vc-backend b)
372 (throw 'found t)))
f8b72742 373 (if (or (not backend) (eq backend 'none))
1f22ad24
AS
374 vc-handled-backends
375 (cons backend vc-handled-backends))))
0e0d9831
GM
376 ;; File is not registered.
377 (vc-file-setprop file 'vc-backend 'none)
ce9f8ffb 378 nil)))))
0e0d9831 379
e4d26892
ER
380(defun vc-backend (file-or-list)
381 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
382If the argument is a list, the files must all have the same back end."
0e0d9831 383 ;; `file' can be nil in several places (typically due to the use of
5232a436 384 ;; code like (vc-backend buffer-file-name)).
e4d26892
ER
385 (cond ((stringp file-or-list)
386 (let ((property (vc-file-getprop file-or-list 'vc-backend)))
387 ;; Note that internally, Emacs remembers unregistered
388 ;; files by setting the property to `none'.
389 (cond ((eq property 'none) nil)
390 (property)
391 ;; vc-registered sets the vc-backend property
392 (t (if (vc-registered file-or-list)
393 (vc-file-getprop file-or-list 'vc-backend)
394 nil)))))
395 ((and file-or-list (listp file-or-list))
396 (vc-backend (car file-or-list)))
397 (t
398 nil)))
399
0e0d9831
GM
400
401(defun vc-backend-subdirectory-name (file)
402 "Return where the master and lock FILEs for the current directory are kept."
403 (symbol-name (vc-backend file)))
e1c0c2d1 404
02d383eb 405(defun vc-name (file)
5eb2b516
DL
406 "Return the master name of FILE.
407If the file is not registered, or the master name is not known, return nil."
408 ;; TODO: This should ultimately become obsolete, at least up here
0e0d9831 409 ;; in vc-hooks.
02d383eb 410 (or (vc-file-getprop file 'vc-name)
64341022
AS
411 ;; force computation of the property by calling
412 ;; vc-BACKEND-registered explicitly
413 (if (and (vc-backend file)
414 (vc-call-backend (vc-backend file) 'registered file))
8aa81ea8 415 (vc-file-getprop file 'vc-name))))
e1c0c2d1 416
e0607aaa
SM
417(defun vc-checkout-model (backend files)
418 "Indicate how FILES are checked out.
0e0d9831 419
e0607aaa 420If FILES are not registered, this function always returns nil.
cb5af02a 421For registered files, the possible values are:
0e0d9831 422
fe3e2715 423 'implicit FILES are always writable, and checked out `implicitly'
0e0d9831
GM
424 when the user saves the first changes to the file.
425
e0607aaa 426 'locking FILES are read-only if up-to-date; user must type
db8afaee 427 \\[vc-next-action] before editing. Strict locking
0e0d9831
GM
428 is assumed.
429
e0607aaa 430 'announce FILES are read-only if up-to-date; user must type
db8afaee 431 \\[vc-next-action] before editing. But other users
0e0d9831 432 may be editing at the same time."
e0607aaa 433 (vc-call-backend backend 'checkout-model files))
7064821c 434
8d2b9c1a
AS
435(defun vc-user-login-name (file)
436 "Return the name under which the user accesses the given FILE."
437 (or (and (eq (string-match tramp-file-name-regexp file) 0)
438 ;; tramp case: execute "whoami" via tramp
439 (let ((default-directory (file-name-directory file)))
440 (with-temp-buffer
441 (if (not (zerop (process-file "whoami" nil t)))
442 ;; fall through if "whoami" didn't work
443 nil
444 ;; remove trailing newline
445 (delete-region (1- (point-max)) (point-max))
446 (buffer-string)))))
447 ;; normal case
448 (user-login-name)
449 ;; if user-login-name is nil, return the UID as a string
450 (number-to-string (user-uid))))
0e0d9831
GM
451
452(defun vc-state (file)
453 "Return the version control state of FILE.
454
cb5af02a
AS
455If FILE is not registered, this function always returns nil.
456For registered files, the value returned is one of:
0e0d9831
GM
457
458 'up-to-date The working file is unmodified with respect to the
459 latest version on the current branch, and not locked.
460
461 'edited The working file has been edited by the user. If
462 locking is used for the file, this state means that
463 the current version is locked by the calling user.
464
465 USER The current version of the working file is locked by
466 some other USER (a string).
f8b72742 467
3702367b 468 'needs-update The file has not been edited by the user, but there is
0e0d9831
GM
469 a more recent version on the current branch stored
470 in the master file.
471
472 'needs-merge The file has been edited by the user, and there is also
473 a more recent version on the current branch stored in
474 the master file. This state can only occur if locking
475 is not used for the file.
476
fae00181 477 'unlocked-changes The working version of the file is not locked,
0e0d9831
GM
478 but the working file has been changed with respect
479 to that version. This state can only occur for files
480 with locking; it represents an erroneous condition that
481 should be resolved by the user (vc-next-action will
fae00181
ER
482 prompt the user to do it).
483
484 'added Scheduled to go into the repository on the next commit.
14f26054 485 Often represented by vc-working-revision = \"0\" in VCSes
722f037f
ER
486 with monotonic IDs like Subversion and Mercurial.
487
484c1b1f 488 'removed Scheduled to be deleted from the repository on next commit.
527b313d 489
7fbb4797 490 'conflict The file contains conflicts as the result of a merge.
329a656e
JB
491 For now the conflicts are text conflicts. In the
492 future this might be extended to deal with metadata
7fbb4797
DN
493 conflicts too.
494
329a656e 495 'missing The file is not present in the file system, but the VC
dd0d723c
DN
496 system still tracks it.
497
4903369a 498 'ignored The file showed up in a dir-status listing with a flag
722f037f 499 indicating the version-control system is ignoring it,
527b313d
SS
500 Note: This property is not set reliably (some VCSes
501 don't have useful directory-status commands) so assume
722f037f 502 that any file with vc-state nil might be ignorable
527b313d 503 without VC knowing it.
722f037f 504
3702367b 505 'unregistered The file is not under version control.
722f037f 506
527b313d 507A return of nil from this function means we have no information on the
d4e772c4 508status of this file."
a970a27e
ER
509 ;; Note: in Emacs 22 and older, return of nil meant the file was
510 ;; unregistered. This is potentially a source of
511 ;; backward-compatibility bugs.
fae00181 512
2a3897f5 513 ;; FIXME: New (sub)states needed (?):
2a3897f5 514 ;; - `copied' and `moved' (might be handled by `removed' and `added')
0e0d9831 515 (or (vc-file-getprop file 'vc-state)
a749e19d
SM
516 (when (> (length file) 0)
517 (let ((backend (vc-backend file)))
518 (when backend
519 (vc-file-setprop
520 file 'vc-state
521 (vc-call-backend backend 'state-heuristic file)))))))
0e0d9831
GM
522
523(defsubst vc-up-to-date-p (file)
524 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
525 (eq (vc-state file) 'up-to-date))
526
527(defun vc-default-state-heuristic (backend file)
d4e772c4 528 "Default implementation of vc-BACKEND-state-heuristic.
5eb2b516
DL
529It simply calls the real state computation function `vc-BACKEND-state'
530and does not employ any heuristic at all."
0e0d9831 531 (vc-call-backend backend 'state file))
1efcbf46 532
e767004f
AS
533(defun vc-workfile-unchanged-p (file)
534 "Return non-nil if FILE has not changed since the last checkout."
535 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
536 (lastmod (nth 5 (file-attributes file))))
70c8a390
CY
537 ;; This is a shortcut for determining when the workfile is
538 ;; unchanged. It can fail under some circumstances; see the
539 ;; discussion in bug#694.
540 (if (and checkout-time
541 ;; Tramp and Ange-FTP return this when they don't know the time.
542 (not (equal lastmod '(0 0))))
543 (equal checkout-time lastmod)
544 (let ((unchanged (vc-call workfile-unchanged-p file)))
545 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
546 unchanged))))
e767004f
AS
547
548(defun vc-default-workfile-unchanged-p (backend file)
549 "Check if FILE is unchanged by diffing against the master version.
550Return non-nil if FILE is unchanged."
f8836381 551 (zerop (condition-case err
4392edab
AS
552 ;; If the implementation supports it, let the output
553 ;; go to *vc*, not *vc-diff*, since this is an internal call.
a749e19d 554 (vc-call-backend backend 'diff (list file) nil nil "*vc*")
f8836381
AS
555 (wrong-number-of-arguments
556 ;; If this error came from the above call to vc-BACKEND-diff,
557 ;; try again without the optional buffer argument (for
558 ;; backward compatibility). Otherwise, resignal.
559 (if (or (not (eq (cadr err)
560 (indirect-function
a749e19d 561 (vc-find-backend-function backend 'diff))))
0d2ce4ef
JB
562 (not (eq (caddr err) 4)))
563 (signal (car err) (cdr err))
a749e19d 564 (vc-call-backend backend 'diff (list file)))))))
e767004f 565
ac3f4c6f 566(defun vc-working-revision (file)
e4d26892 567 "Return the repository version from which FILE was checked out.
cb5af02a 568If FILE is not registered, this function always returns nil."
ac3f4c6f 569 (or (vc-file-getprop file 'vc-working-revision)
a749e19d
SM
570 (let ((backend (vc-backend file)))
571 (when backend
572 (vc-file-setprop file 'vc-working-revision
573 (vc-call-backend backend 'working-revision file))))))
b5446276 574
6e5d0e9e
SM
575;; Backward compatibility.
576(define-obsolete-function-alias
577 'vc-workfile-version 'vc-working-revision "23.1")
578(defun vc-default-working-revision (backend file)
579 (message
580 "`working-revision' not found: using the old `workfile-version' instead")
581 (vc-call-backend backend 'workfile-version file))
f2ee4191 582
0e0d9831
GM
583(defun vc-default-registered (backend file)
584 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
585 (let ((sym (vc-make-backend-sym backend 'master-templates)))
586 (unless (get backend 'vc-templates-grabbed)
587 (put backend 'vc-templates-grabbed t)
588 (set sym (append (delq nil
589 (mapcar
590 (lambda (template)
591 (and (consp template)
592 (eq (cdr template) backend)
593 (car template)))
3756d481
AS
594 (with-no-warnings
595 vc-master-templates)))
0e0d9831
GM
596 (symbol-value sym))))
597 (let ((result (vc-check-master-templates file (symbol-value sym))))
598 (if (stringp result)
599 (vc-file-setprop file 'vc-name result)
600 nil)))) ; Not registered
601
602(defun vc-possible-master (s dirname basename)
603 (cond
604 ((stringp s) (format s dirname basename))
605 ((functionp s)
606 ;; The template is a function to invoke. If the
607 ;; function returns non-nil, that means it has found a
608 ;; master. For backward compatibility, we also handle
609 ;; the case that the function throws a 'found atom
610 ;; and a pair (cons MASTER-FILE BACKEND).
611 (let ((result (catch 'found (funcall s dirname basename))))
612 (if (consp result) (car result) result)))))
613
614(defun vc-check-master-templates (file templates)
cb5af02a 615 "Return non-nil if there is a master corresponding to FILE.
0e0d9831
GM
616
617TEMPLATES is a list of strings or functions. If an element is a
618string, it must be a control string as required by `format', with two
619string placeholders, such as \"%sRCS/%s,v\". The directory part of
620FILE is substituted for the first placeholder, the basename of FILE
621for the second. If a file with the resulting name exists, it is taken
622as the master of FILE, and returned.
623
624If an element of TEMPLATES is a function, it is called with the
625directory part and the basename of FILE as arguments. It should
626return non-nil if it finds a master; that value is then returned by
627this function."
628 (let ((dirname (or (file-name-directory file) ""))
629 (basename (file-name-nondirectory file)))
630 (catch 'found
5eb2b516 631 (mapcar
0e0d9831
GM
632 (lambda (s)
633 (let ((trial (vc-possible-master s dirname basename)))
b5446276
DN
634 (when (and trial (file-exists-p trial)
635 ;; Make sure the file we found with name
636 ;; TRIAL is not the source file itself.
637 ;; That can happen with RCS-style names if
638 ;; the file name is truncated (e.g. to 14
639 ;; chars). See if either directory or
640 ;; attributes differ.
641 (or (not (string= dirname
642 (file-name-directory trial)))
643 (not (equal (file-attributes file)
644 (file-attributes trial)))))
0e0d9831
GM
645 (throw 'found trial))))
646 templates))))
f2ee4191 647
c844616c 648(defun vc-toggle-read-only (&optional verbose)
c43e436c 649 "Change read-only status of current buffer, perhaps via version control.
db8afaee 650
c43e436c 651If the buffer is visiting a file registered with version control,
d4e772c4
JB
652throw an error, because this is not a safe or really meaningful operation
653on any version-control system newer than RCS.
25e94278
ER
654
655Otherwise, just change the read-only flag of the buffer.
656
657If you bind this function to \\[toggle-read-only], then Emacs
658will properly intercept all attempts to toggle the read-only flag
659on version-controlled buffer."
c844616c 660 (interactive "P")
25e94278
ER
661 (if (vc-backend buffer-file-name)
662 (error "Toggling the readability of a version controlled file is likely to wreak havoc.")
594722a8
ER
663 (toggle-read-only)))
664
e896a9e1 665(defun vc-default-make-version-backups-p (backend file)
cb5af02a 666 "Return non-nil if unmodified versions should be backed up locally.
9aa10a43 667The default is to switch off this feature."
d445a975
AS
668 nil)
669
e896a9e1
AS
670(defun vc-version-backup-file-name (file &optional rev manual regexp)
671 "Return a backup file name for REV or the current version of FILE.
672If MANUAL is non-nil it means that a name for backups created by
673the user should be returned; if REGEXP is non-nil that means to return
674a regexp for matching all such backup files, regardless of the version."
e3f955b6
AS
675 (if regexp
676 (concat (regexp-quote (file-name-nondirectory file))
d4c813e9 677 "\\.~.+" (unless manual "\\.") "~")
f8b72742 678 (expand-file-name (concat (file-name-nondirectory file)
d4c813e9 679 ".~" (subst-char-in-string
ac3f4c6f 680 ?/ ?_ (or rev (vc-working-revision file)))
e3f955b6
AS
681 (unless manual ".") "~")
682 (file-name-directory file))))
e896a9e1
AS
683
684(defun vc-delete-automatic-version-backups (file)
685 "Delete all existing automatic version backups for FILE."
d455f4f7 686 (condition-case nil
a549ce70 687 (mapc
d455f4f7 688 'delete-file
79e954d0 689 (directory-files (or (file-name-directory file) default-directory) t
d455f4f7
SM
690 (vc-version-backup-file-name file nil nil t)))
691 ;; Don't fail when the directory doesn't exist.
692 (file-error nil)))
e896a9e1
AS
693
694(defun vc-make-version-backup (file)
695 "Make a backup copy of FILE, which is assumed in sync with the repository.
696Before doing that, check if there are any old backups and get rid of them."
48b15d3f 697 (unless (and (fboundp 'msdos-long-file-names)
ee9be3de 698 (not (with-no-warnings (msdos-long-file-names))))
48b15d3f 699 (vc-delete-automatic-version-backups file)
27707243
AS
700 (condition-case nil
701 (copy-file file (vc-version-backup-file-name file)
702 nil 'keep-date)
703 ;; It's ok if it doesn't work (e.g. directory not writable),
704 ;; since this is just for efficiency.
bf247b6e 705 (file-error
27707243
AS
706 (message
707 (concat "Warning: Cannot make version backup; "
708 "diff/revert therefore not local"))))))
d445a975
AS
709
710(defun vc-before-save ()
711 "Function to be called by `basic-save-buffer' (in files.el)."
712 ;; If the file on disk is still in sync with the repository,
713 ;; and version backups should be made, copy the file to
714 ;; another name. This enables local diffs and local reverting.
e0607aaa
SM
715 (let ((file buffer-file-name)
716 backend)
f42af255 717 (ignore-errors ;Be careful not to prevent saving the file.
e0607aaa 718 (and (setq backend (vc-backend file))
f42af255 719 (vc-up-to-date-p file)
70e2f6c7 720 (eq (vc-checkout-model backend (list file)) 'implicit)
a749e19d 721 (vc-call-backend backend 'make-version-backups-p file)
f42af255 722 (vc-make-version-backup file)))))
d445a975 723
74d0991f 724(declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
004a00f4 725
e66eac08 726(defun vc-after-save ()
0e0d9831 727 "Function to be called by `basic-save-buffer' (in files.el)."
5eb2b516 728 ;; If the file in the current buffer is under version control,
0e0d9831
GM
729 ;; up-to-date, and locking is not used for the file, set
730 ;; the state to 'edited and redisplay the mode line.
e0607aaa
SM
731 (let* ((file buffer-file-name)
732 (backend (vc-backend file)))
733 (and backend
b23a2306
AS
734 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
735 (nth 5 (file-attributes file)))
736 ;; File has been saved in the same second in which
737 ;; it was checked out. Clear the checkout-time
738 ;; to avoid confusion.
739 (vc-file-setprop file 'vc-checkout-time nil))
740 t)
0e0d9831 741 (vc-up-to-date-p file)
70e2f6c7 742 (eq (vc-checkout-model backend (list file)) 'implicit)
0e0d9831
GM
743 (vc-file-setprop file 'vc-state 'edited)
744 (vc-mode-line file)
e02d8ae7
DN
745 ;; Try to avoid unnecessary work, a *vc-dir* buffer is only
746 ;; present if this is true.
747 (when (memq 'vc-dir-resynch-file after-save-hook)
748 (vc-dir-resynch-file file)))))
04446ed0 749
e2247dc8
SM
750(defvar vc-menu-entry
751 '(menu-item "Version Control" vc-menu-map
752 :filter vc-menu-map-filter))
753
754(when (boundp 'menu-bar-tools-menu)
755 ;; We do not need to worry here about the placement of this entry
756 ;; because menu-bar.el has already created the proper spot for us
757 ;; and this will simply use it.
758 (define-key menu-bar-tools-menu [vc] vc-menu-entry))
759
5719a098
SM
760(defconst vc-mode-line-map
761 (let ((map (make-sparse-keymap)))
e2247dc8 762 (define-key map [mode-line down-mouse-1] vc-menu-entry)
5719a098
SM
763 map))
764
0e0d9831 765(defun vc-mode-line (file)
7bc2b98b 766 "Set `vc-mode' to display type of version control for FILE.
594722a8 767The value is set in the current buffer, which should be the buffer
0e0d9831 768visiting FILE."
67c6f446 769 (interactive (list buffer-file-name))
5719a098 770 (let ((backend (vc-backend file)))
5232a436
SM
771 (if (not backend)
772 (setq vc-mode nil)
a749e19d 773 (let* ((ml-string (vc-call-backend backend 'mode-line-string file))
5719a098
SM
774 (ml-echo (get-text-property 0 'help-echo ml-string)))
775 (setq vc-mode
776 (concat
777 " "
778 (if (null vc-display-status)
779 (symbol-name backend)
780 (propertize
781 ml-string
782 'mouse-face 'mode-line-highlight
527b313d 783 'help-echo
5719a098
SM
784 (concat (or ml-echo
785 (format "File under the %s version control system"
786 backend))
787 "\nmouse-1: Version Control menu")
788 'local-map vc-mode-line-map)))))
5232a436
SM
789 ;; If the file is locked by some other user, make
790 ;; the buffer read-only. Like this, even root
791 ;; cannot modify a file that someone else has locked.
792 (and (equal file buffer-file-name)
793 (stringp (vc-state file))
794 (setq buffer-read-only t))
795 ;; If the user is root, and the file is not owner-writable,
796 ;; then pretend that we can't write it
797 ;; even though we can (because root can write anything).
e02d8ae7
DN
798 ;; This way, even root cannot modify a file that isn't locked.
799 (and (equal file buffer-file-name)
800 (not buffer-read-only)
5232a436
SM
801 (zerop (user-real-uid))
802 (zerop (logand (file-modes buffer-file-name) 128))
803 (setq buffer-read-only t)))
804 (force-mode-line-update)
805 backend))
0e0d9831
GM
806
807(defun vc-default-mode-line-string (backend file)
808 "Return string for placement in modeline by `vc-mode-line' for FILE.
809Format:
810
811 \"BACKEND-REV\" if the file is up-to-date
812 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
813 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
0e0d9831
GM
814
815This function assumes that the file is registered."
816 (setq backend (symbol-name backend))
817 (let ((state (vc-state file))
82c4728d 818 (state-echo nil)
ac3f4c6f 819 (rev (vc-working-revision file)))
82c4728d
DN
820 (propertize
821 (cond ((or (eq state 'up-to-date)
3702367b 822 (eq state 'needs-update))
82c4728d
DN
823 (setq state-echo "Up to date file")
824 (concat backend "-" rev))
825 ((stringp state)
826 (setq state-echo (concat "File locked by" state))
827 (concat backend ":" state ":" rev))
45b24b4d
SM
828 ((eq state 'added)
829 (setq state-echo "Locally added file")
830 (concat backend "@" rev))
7fbb4797
DN
831 ((eq state 'conflict)
832 (setq state-echo "File contains conflicts after the last merge")
833 (concat backend "!" rev))
a58b57e2
DN
834 ((eq state 'removed)
835 (setq state-echo "File removed from the VC system")
836 (concat backend "!" rev))
837 ((eq state 'missing)
838 (setq state-echo "File tracked by the VC system, but missing from the file system")
5bad3140 839 (concat backend "?" rev))
82c4728d
DN
840 (t
841 ;; Not just for the 'edited state, but also a fallback
842 ;; for all other states. Think about different symbols
3702367b 843 ;; for 'needs-update and 'needs-merge.
3a12f9f8 844 (setq state-echo "Locally modified file")
82c4728d 845 (concat backend ":" rev)))
33e0847d 846 'help-echo (concat state-echo " under the " backend
3a12f9f8 847 " version control system"))))
f2ee4191 848
a3a39848 849(defun vc-follow-link ()
0e0d9831
GM
850 "If current buffer visits a symbolic link, visit the real file.
851If the real file is already visited in another buffer, make that buffer
852current, and kill the buffer that visits the link."
33f95a82 853 (let* ((true-buffer (find-buffer-visiting buffer-file-truename))
e7f5ddc2
RS
854 (this-buffer (current-buffer)))
855 (if (eq true-buffer this-buffer)
066b7259 856 (let ((truename buffer-file-truename))
d8221951 857 (kill-buffer this-buffer)
e7f5ddc2
RS
858 ;; In principle, we could do something like set-visited-file-name.
859 ;; However, it can't be exactly the same as set-visited-file-name.
860 ;; I'm not going to work out the details right now. -- rms.
d8221951 861 (set-buffer (find-file-noselect truename)))
e7f5ddc2
RS
862 (set-buffer true-buffer)
863 (kill-buffer this-buffer))))
a3a39848 864
b8d1db77
SM
865(defun vc-default-find-file-hook (backend)
866 nil)
867
594722a8 868(defun vc-find-file-hook ()
be4d6a6f 869 "Function for `find-file-hook' activating VC mode if appropriate."
18c8a18e
PE
870 ;; Recompute whether file is version controlled,
871 ;; if user has killed the buffer and revisited.
060a1456
AS
872 (if vc-mode
873 (setq vc-mode nil))
0e0d9831 874 (when buffer-file-name
f2ee4191 875 (vc-file-clearprops buffer-file-name)
c8d4f1a0 876 (add-hook 'mode-line-hook 'vc-mode-line nil t)
f2ee4191 877 (cond
fc8b8d0f 878 ((with-demoted-errors (vc-backend buffer-file-name))
0bfb74a9 879 ;; Compute the state and put it in the modeline.
f2ee4191 880 (vc-mode-line buffer-file-name)
0bfb74a9
SM
881 (unless vc-make-backup-files
882 ;; Use this variable, not make-backup-files,
883 ;; because this is for things that depend on the file name.
b8d1db77
SM
884 (set (make-local-variable 'backup-inhibited) t))
885 ;; Let the backend setup any buffer-local things he needs.
886 (vc-call-backend (vc-backend buffer-file-name) 'find-file-hook))
33f95a82
SM
887 ((let ((link-type (and (not (equal buffer-file-name buffer-file-truename))
888 (vc-backend buffer-file-truename))))
0bfb74a9
SM
889 (cond ((not link-type) nil) ;Nothing to do.
890 ((eq vc-follow-symlinks nil)
891 (message
b8063212 892 "Warning: symbolic link to %s-controlled source file" link-type))
0bfb74a9
SM
893 ((or (not (eq vc-follow-symlinks 'ask))
894 ;; If we already visited this file by following
895 ;; the link, don't ask again if we try to visit
896 ;; it again. GUD does that, and repeated questions
897 ;; are painful.
898 (get-file-buffer
899 (abbreviate-file-name
900 (file-chase-links buffer-file-name))))
901
902 (vc-follow-link)
903 (message "Followed link to %s" buffer-file-name)
904 (vc-find-file-hook))
905 (t
906 (if (yes-or-no-p (format
b8063212 907 "Symbolic link to %s-controlled source file; follow link? " link-type))
0bfb74a9
SM
908 (progn (vc-follow-link)
909 (message "Followed link to %s" buffer-file-name)
910 (vc-find-file-hook))
911 (message
b8063212 912 "Warning: editing through the link bypasses version control")
0bfb74a9 913 ))))))))
594722a8 914
be4d6a6f 915(add-hook 'find-file-hook 'vc-find-file-hook)
594722a8 916
14fef9de 917;; more hooks, this time for file-not-found
594722a8 918(defun vc-file-not-found-hook ()
0e0d9831
GM
919 "When file is not found, try to check it out from version control.
920Returns t if checkout was successful, nil otherwise.
d3d558a9 921Used in `find-file-not-found-functions'."
5f42a1d4
RS
922 ;; When a file does not exist, ignore cached info about it
923 ;; from a previous visit.
f98c9a23
MA
924 ;; We check that `buffer-file-name' is non-nil. It should be always
925 ;; the case, but in conjunction with Tramp, it might be nil. M. Albinus.
926 (when buffer-file-name
927 (vc-file-clearprops buffer-file-name)
928 (let ((backend (vc-backend buffer-file-name)))
929 (when backend (vc-call-backend backend 'find-file-not-found-hook)))))
4a330ab7
SM
930
931(defun vc-default-find-file-not-found-hook (backend)
a3773730
SM
932 ;; This used to do what vc-rcs-find-file-not-found-hook does, but it only
933 ;; really makes sense for RCS. For other backends, better not do anything.
934 nil)
594722a8 935
3756d481 936(add-hook 'find-file-not-found-functions 'vc-file-not-found-hook)
594722a8 937
f2ee4191 938(defun vc-kill-buffer-hook ()
0e0d9831 939 "Discard VC info about a file when we kill its buffer."
b5446276 940 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
f2ee4191 941
cd32d5d1 942(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
f2ee4191 943
14fef9de
SM
944;; Now arrange for (autoloaded) bindings of the main package.
945;; Bindings for this have to go in the global map, as we'll often
946;; want to call them from random buffers.
947
948;; Autoloading works fine, but it prevents shortcuts from appearing
949;; in the menu because they don't exist yet when the menu is built.
950;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
951(defvar vc-prefix-map
952 (let ((map (make-sparse-keymap)))
953 (define-key map "a" 'vc-update-change-log)
954 (define-key map "b" 'vc-switch-backend)
e4d26892 955 (define-key map "c" 'vc-rollback)
e1aec6fb 956 (define-key map "d" 'vc-dir)
14fef9de
SM
957 (define-key map "g" 'vc-annotate)
958 (define-key map "h" 'vc-insert-headers)
959 (define-key map "i" 'vc-register)
960 (define-key map "l" 'vc-print-log)
961 (define-key map "m" 'vc-merge)
370fded4
ER
962 (define-key map "r" 'vc-retrieve-tag)
963 (define-key map "s" 'vc-create-tag)
e4d26892 964 (define-key map "u" 'vc-revert)
14fef9de 965 (define-key map "v" 'vc-next-action)
e4d26892 966 (define-key map "+" 'vc-update)
14fef9de 967 (define-key map "=" 'vc-diff)
0a0ca7f1 968 (define-key map "~" 'vc-revision-other-window)
14fef9de
SM
969 map))
970(fset 'vc-prefix-map vc-prefix-map)
c1fdf758 971(define-key global-map "\C-xv" 'vc-prefix-map)
624c0e9d 972
e2247dc8
SM
973(defvar vc-menu-map
974 (let ((map (make-sparse-keymap "Version Control")))
975 ;;(define-key map [show-files]
976 ;; '("Show Files under VC" . (vc-directory t)))
370fded4
ER
977 (define-key map [vc-retrieve-tag]
978 '(menu-item "Retrieve Tag" vc-retrieve-tag
979 :help "Retrieve tagged version or branch"))
980 (define-key map [vc-create-tag]
981 '(menu-item "Create Tag" vc-create-tag
982 :help "Create version tag"))
e2247dc8 983 (define-key map [separator1] '("----"))
6dbb5c76 984 (define-key map [vc-annotate]
40fabc71
DN
985 '(menu-item "Annotate" vc-annotate
986 :help "Display the edit history of the current file using colors"))
6dbb5c76 987 (define-key map [vc-rename-file]
40fabc71
DN
988 '(menu-item "Rename File" vc-rename-file
989 :help "Rename file"))
0a0ca7f1 990 (define-key map [vc-revision-other-window]
40fabc71
DN
991 '(menu-item "Show Other Version" vc-revision-other-window
992 :help "Visit another version of the current file in another window"))
6dbb5c76 993 (define-key map [vc-diff]
40fabc71
DN
994 '(menu-item "Compare with Base Version" vc-diff
995 :help "Compare file set with the base version"))
e2247dc8 996 (define-key map [vc-update-change-log]
40fabc71
DN
997 '(menu-item "Update ChangeLog" vc-update-change-log
998 :help "Find change log file and add entries from recent version control logs"))
6dbb5c76 999 (define-key map [vc-print-log]
40fabc71
DN
1000 '(menu-item "Show History" vc-print-log
1001 :help "List the change log of the current file set in a window"))
e2247dc8
SM
1002 (define-key map [separator2] '("----"))
1003 (define-key map [vc-insert-header]
40fabc71
DN
1004 '(menu-item "Insert Header" vc-insert-headers
1005 :help "Insert headers into a file for use with a version control system.
1006"))
6dbb5c76 1007 (define-key map [undo]
40fabc71
DN
1008 '(menu-item "Undo Last Check-In" vc-rollback
1009 :help "Remove the most recent changeset committed to the repository"))
e2247dc8 1010 (define-key map [vc-revert]
40fabc71
DN
1011 '(menu-item "Revert to Base Version" vc-revert
1012 :help "Revert working copies of the selected file set to their repository contents"))
e2247dc8 1013 (define-key map [vc-update]
40fabc71
DN
1014 '(menu-item "Update to Latest Version" vc-update
1015 :help "Update the current fileset's files to their tip revisions"))
6dbb5c76 1016 (define-key map [vc-next-action]
40fabc71
DN
1017 '(menu-item "Check In/Out" vc-next-action
1018 :help "Do the next logical version control operation on the current fileset"))
6dbb5c76 1019 (define-key map [vc-register]
40fabc71
DN
1020 '(menu-item "Register" vc-register
1021 :help "Register file set into a version control system"))
e1aec6fb
SM
1022 (define-key map [vc-dir]
1023 '(menu-item "VC Dir" vc-dir
7fbb4797 1024 :help "Show the VC status of files in a directory"))
e2247dc8
SM
1025 map))
1026
1027(defalias 'vc-menu-map vc-menu-map)
1028
aafb0703
GM
1029(declare-function vc-responsible-backend "vc" (file &optional register))
1030
e2247dc8
SM
1031(defun vc-menu-map-filter (orig-binding)
1032 (if (and (symbolp orig-binding) (fboundp orig-binding))
1033 (setq orig-binding (indirect-function orig-binding)))
1034 (let ((ext-binding
f3d57a2c 1035 (when vc-mode
aafb0703 1036 (vc-call-backend
f3d57a2c
DN
1037 (if buffer-file-name
1038 (vc-backend buffer-file-name)
1039 (vc-responsible-backend default-directory))
1040 'extra-menu))))
e2247dc8
SM
1041 ;; Give the VC backend a chance to add menu entries
1042 ;; specific for that backend.
1043 (if (null ext-binding)
98481bad 1044 orig-binding
e2247dc8
SM
1045 (append orig-binding
1046 '((ext-menu-separator "---"))
98481bad 1047 ext-binding))))
738efc8e 1048
cb223bba
DN
1049(defun vc-default-extra-menu (backend)
1050 nil)
1051
594722a8
ER
1052(provide 'vc-hooks)
1053
ce9f8ffb 1054;; arch-tag: 2e5a6fa7-1d30-48e2-8bd0-e3d335f04f32
594722a8 1055;;; vc-hooks.el ends here