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