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