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