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