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