]> code.delx.au - gnu-emacs/blob - lisp/vc-hooks.el
Merge from emacs--rel--22
[gnu-emacs] / 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 2, 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 HG Arch MCVS)
66 ;; BZR, HG, Arch and MCVS come last because they are per-tree rather
67 ;; 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 ;; BEWARE!! `file' is evaluated twice!!
283 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
284 \f
285 (defsubst vc-parse-buffer (pattern i)
286 "Find PATTERN in the current buffer and return its Ith submatch."
287 (goto-char (point-min))
288 (if (re-search-forward pattern nil t)
289 (match-string i)))
290
291 (defun vc-insert-file (file &optional limit blocksize)
292 "Insert the contents of FILE into the current buffer.
293
294 Optional argument LIMIT is a regexp. If present, the file is inserted
295 in chunks of size BLOCKSIZE (default 8 kByte), until the first
296 occurrence of LIMIT is found. Anything from the start of that occurrence
297 to the end of the buffer is then deleted. The function returns
298 non-nil if FILE exists and its contents were successfully inserted."
299 (erase-buffer)
300 (when (file-exists-p file)
301 (if (not limit)
302 (insert-file-contents file)
303 (if (not blocksize) (setq blocksize 8192))
304 (let ((filepos 0))
305 (while
306 (and (< 0 (cadr (insert-file-contents
307 file nil filepos (incf filepos blocksize))))
308 (progn (beginning-of-line)
309 (let ((pos (re-search-forward limit nil 'move)))
310 (if pos (delete-region (match-beginning 0)
311 (point-max)))
312 (not pos)))))))
313 (set-buffer-modified-p nil)
314 t))
315
316 (defun vc-find-root (file witness)
317 "Find the root of a checked out project.
318 The function walks up the directory tree from FILE looking for WITNESS.
319 If WITNESS if not found, return nil, otherwise return the root."
320 ;; Represent /home/luser/foo as ~/foo so that we don't try to look for
321 ;; witnesses in /home or in /.
322 (while (not (file-directory-p file))
323 (setq file (file-name-directory (directory-file-name file))))
324 (setq file (abbreviate-file-name file))
325 (let ((root nil)
326 (user (nth 2 (file-attributes file))))
327 (while (not (or root
328 (null file)
329 ;; As a heuristic, we stop looking up the hierarchy of
330 ;; directories as soon as we find a directory belonging
331 ;; to another user. This should save us from looking in
332 ;; things like /net and /afs. This assumes that all the
333 ;; files inside a project belong to the same user.
334 (not (equal user (nth 2 (file-attributes file))))
335 (string-match vc-ignore-dir-regexp file)))
336 (if (file-exists-p (expand-file-name witness file))
337 (setq root file)
338 (if (equal file
339 (setq file (file-name-directory (directory-file-name file))))
340 (setq file nil))))
341 root))
342
343 ;; Access functions to file properties
344 ;; (Properties should be _set_ using vc-file-setprop, but
345 ;; _retrieved_ only through these functions, which decide
346 ;; if the property is already known or not. A property should
347 ;; only be retrieved by vc-file-getprop if there is no
348 ;; access function.)
349
350 ;; properties indicating the backend being used for FILE
351
352 (defun vc-registered (file)
353 "Return non-nil if FILE is registered in a version control system.
354
355 This function performs the check each time it is called. To rely
356 on the result of a previous call, use `vc-backend' instead. If the
357 file was previously registered under a certain backend, then that
358 backend is tried first."
359 (let (handler)
360 (cond
361 ((string-match vc-ignore-dir-regexp (file-name-directory file)) nil)
362 ((and (boundp 'file-name-handler-alist)
363 (setq handler (find-file-name-handler file 'vc-registered)))
364 ;; handler should set vc-backend and return t if registered
365 (funcall handler 'vc-registered file))
366 (t
367 ;; There is no file name handler.
368 ;; Try vc-BACKEND-registered for each handled BACKEND.
369 (catch 'found
370 (let ((backend (vc-file-getprop file 'vc-backend)))
371 (mapcar
372 (lambda (b)
373 (and (vc-call-backend b 'registered file)
374 (vc-file-setprop file 'vc-backend b)
375 (throw 'found t)))
376 (if (or (not backend) (eq backend 'none))
377 vc-handled-backends
378 (cons backend vc-handled-backends))))
379 ;; File is not registered.
380 (vc-file-setprop file 'vc-backend 'none)
381 nil)))))
382
383 (defun vc-backend (file-or-list)
384 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
385 If the argument is a list, the files must all have the same back end."
386 ;; `file' can be nil in several places (typically due to the use of
387 ;; code like (vc-backend buffer-file-name)).
388 (cond ((stringp file-or-list)
389 (let ((property (vc-file-getprop file-or-list 'vc-backend)))
390 ;; Note that internally, Emacs remembers unregistered
391 ;; files by setting the property to `none'.
392 (cond ((eq property 'none) nil)
393 (property)
394 ;; vc-registered sets the vc-backend property
395 (t (if (vc-registered file-or-list)
396 (vc-file-getprop file-or-list 'vc-backend)
397 nil)))))
398 ((and file-or-list (listp file-or-list))
399 (vc-backend (car file-or-list)))
400 (t
401 nil)))
402
403
404 (defun vc-backend-subdirectory-name (file)
405 "Return where the master and lock FILEs for the current directory are kept."
406 (symbol-name (vc-backend file)))
407
408 (defun vc-name (file)
409 "Return the master name of FILE.
410 If the file is not registered, or the master name is not known, return nil."
411 ;; TODO: This should ultimately become obsolete, at least up here
412 ;; in vc-hooks.
413 (or (vc-file-getprop file 'vc-name)
414 ;; force computation of the property by calling
415 ;; vc-BACKEND-registered explicitly
416 (if (and (vc-backend file)
417 (vc-call-backend (vc-backend file) 'registered file))
418 (vc-file-getprop file 'vc-name))))
419
420 (defun vc-checkout-model (file)
421 "Indicate how FILE is checked out.
422
423 If FILE is not registered, this function always returns nil.
424 For registered files, the possible values are:
425
426 'implicit FILE is always writeable, and checked out `implicitly'
427 when the user saves the first changes to the file.
428
429 'locking FILE is read-only if up-to-date; user must type
430 \\[vc-next-action] before editing. Strict locking
431 is assumed.
432
433 'announce FILE is read-only if up-to-date; user must type
434 \\[vc-next-action] before editing. But other users
435 may be editing at the same time."
436 (or (vc-file-getprop file 'vc-checkout-model)
437 (if (vc-backend file)
438 (vc-file-setprop file 'vc-checkout-model
439 (vc-call checkout-model file)))))
440
441 (defun vc-user-login-name (file)
442 "Return the name under which the user accesses the given FILE."
443 (or (and (eq (string-match tramp-file-name-regexp file) 0)
444 ;; tramp case: execute "whoami" via tramp
445 (let ((default-directory (file-name-directory file)))
446 (with-temp-buffer
447 (if (not (zerop (process-file "whoami" nil t)))
448 ;; fall through if "whoami" didn't work
449 nil
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))))
457
458 (defun vc-state (file)
459 "Return the version control state of FILE.
460
461 If FILE is not registered, this function always returns nil.
462 For registered files, the value returned is one of:
463
464 'up-to-date The working file is unmodified with respect to the
465 latest version on the current branch, and not locked.
466
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.
470
471 USER The current version of the working file is locked by
472 some other USER (a string).
473
474 'needs-patch 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.
477
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.
482
483 'unlocked-changes The current version of the working 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)."
489 ;; FIXME: New (sub)states needed (?):
490 ;; - `added' (i.e. `edited' but with no base version yet,
491 ;; typically represented by vc-workfile-version = "0")
492 ;; - `conflict' (i.e. `edited' with conflict markers)
493 ;; - `removed'
494 ;; - `copied' and `moved' (might be handled by `removed' and `added')
495 (or (vc-file-getprop file 'vc-state)
496 (if (and (> (length file) 0) (vc-backend file))
497 (vc-file-setprop file 'vc-state
498 (vc-call state-heuristic file)))))
499
500 (defun vc-recompute-state (file)
501 "Recompute the version control state of FILE, and return it.
502 This calls the possibly expensive function vc-BACKEND-state,
503 rather than the heuristic."
504 (vc-file-setprop file 'vc-state (vc-call state file)))
505
506 (defsubst vc-up-to-date-p (file)
507 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
508 (eq (vc-state file) 'up-to-date))
509
510 (defun vc-default-state-heuristic (backend file)
511 "Default implementation of vc-state-heuristic.
512 It simply calls the real state computation function `vc-BACKEND-state'
513 and does not employ any heuristic at all."
514 (vc-call-backend backend 'state file))
515
516 (defun vc-workfile-unchanged-p (file)
517 "Return non-nil if FILE has not changed since the last checkout."
518 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
519 (lastmod (nth 5 (file-attributes file))))
520 (if (and checkout-time
521 ;; Tramp and Ange-FTP return this when they don't know the time.
522 (not (equal lastmod '(0 0))))
523 (equal checkout-time lastmod)
524 (let ((unchanged (vc-call workfile-unchanged-p file)))
525 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
526 unchanged))))
527
528 (defun vc-default-workfile-unchanged-p (backend file)
529 "Check if FILE is unchanged by diffing against the master version.
530 Return non-nil if FILE is unchanged."
531 (zerop (condition-case err
532 ;; If the implementation supports it, let the output
533 ;; go to *vc*, not *vc-diff*, since this is an internal call.
534 (vc-call diff (list file) nil nil "*vc*")
535 (wrong-number-of-arguments
536 ;; If this error came from the above call to vc-BACKEND-diff,
537 ;; try again without the optional buffer argument (for
538 ;; backward compatibility). Otherwise, resignal.
539 (if (or (not (eq (cadr err)
540 (indirect-function
541 (vc-find-backend-function (vc-backend file)
542 'diff))))
543 (not (eq (caddr err) 4)))
544 (signal (car err) (cdr err))
545 (vc-call diff (list file)))))))
546
547 (defun vc-workfile-version (file)
548 "Return the repository version from which FILE was checked out.
549 If FILE is not registered, this function always returns nil."
550 (or (vc-file-getprop file 'vc-workfile-version)
551 (if (vc-backend file)
552 (vc-file-setprop file 'vc-workfile-version
553 (vc-call workfile-version file)))))
554
555 (defun vc-default-registered (backend file)
556 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
557 (let ((sym (vc-make-backend-sym backend 'master-templates)))
558 (unless (get backend 'vc-templates-grabbed)
559 (put backend 'vc-templates-grabbed t)
560 (set sym (append (delq nil
561 (mapcar
562 (lambda (template)
563 (and (consp template)
564 (eq (cdr template) backend)
565 (car template)))
566 (with-no-warnings
567 vc-master-templates)))
568 (symbol-value sym))))
569 (let ((result (vc-check-master-templates file (symbol-value sym))))
570 (if (stringp result)
571 (vc-file-setprop file 'vc-name result)
572 nil)))) ; Not registered
573
574 (defun vc-possible-master (s dirname basename)
575 (cond
576 ((stringp s) (format s dirname basename))
577 ((functionp s)
578 ;; The template is a function to invoke. If the
579 ;; function returns non-nil, that means it has found a
580 ;; master. For backward compatibility, we also handle
581 ;; the case that the function throws a 'found atom
582 ;; and a pair (cons MASTER-FILE BACKEND).
583 (let ((result (catch 'found (funcall s dirname basename))))
584 (if (consp result) (car result) result)))))
585
586 (defun vc-check-master-templates (file templates)
587 "Return non-nil if there is a master corresponding to FILE.
588
589 TEMPLATES is a list of strings or functions. If an element is a
590 string, it must be a control string as required by `format', with two
591 string placeholders, such as \"%sRCS/%s,v\". The directory part of
592 FILE is substituted for the first placeholder, the basename of FILE
593 for the second. If a file with the resulting name exists, it is taken
594 as the master of FILE, and returned.
595
596 If an element of TEMPLATES is a function, it is called with the
597 directory part and the basename of FILE as arguments. It should
598 return non-nil if it finds a master; that value is then returned by
599 this function."
600 (let ((dirname (or (file-name-directory file) ""))
601 (basename (file-name-nondirectory file)))
602 (catch 'found
603 (mapcar
604 (lambda (s)
605 (let ((trial (vc-possible-master s dirname basename)))
606 (if (and trial (file-exists-p trial)
607 ;; Make sure the file we found with name
608 ;; TRIAL is not the source file itself.
609 ;; That can happen with RCS-style names if
610 ;; the file name is truncated (e.g. to 14
611 ;; chars). See if either directory or
612 ;; attributes differ.
613 (or (not (string= dirname
614 (file-name-directory trial)))
615 (not (equal (file-attributes file)
616 (file-attributes trial)))))
617 (throw 'found trial))))
618 templates))))
619
620 (defun vc-toggle-read-only (&optional verbose)
621 "Change read-only status of current buffer, perhaps via version control.
622
623 If the buffer is visiting a file registered with version control,
624 then check the file in or out. Otherwise, just change the read-only flag
625 of the buffer.
626 With prefix argument, ask for version number to check in or check out.
627 Check-out of a specified version number does not lock the file;
628 to do that, use this command a second time with no argument.
629
630 If you bind this function to \\[toggle-read-only], then Emacs checks files
631 in or out whenever you toggle the read-only flag."
632 (interactive "P")
633 (if (or (and (boundp 'vc-dired-mode) vc-dired-mode)
634 ;; use boundp because vc.el might not be loaded
635 (vc-backend buffer-file-name))
636 (vc-next-action verbose)
637 (toggle-read-only)))
638
639 (defun vc-default-make-version-backups-p (backend file)
640 "Return non-nil if unmodified versions should be backed up locally.
641 The default is to switch off this feature."
642 nil)
643
644 (defun vc-version-backup-file-name (file &optional rev manual regexp)
645 "Return a backup file name for REV or the current version of FILE.
646 If MANUAL is non-nil it means that a name for backups created by
647 the user should be returned; if REGEXP is non-nil that means to return
648 a regexp for matching all such backup files, regardless of the version."
649 (if regexp
650 (concat (regexp-quote (file-name-nondirectory file))
651 "\\.~.+" (unless manual "\\.") "~")
652 (expand-file-name (concat (file-name-nondirectory file)
653 ".~" (subst-char-in-string
654 ?/ ?_ (or rev (vc-workfile-version file)))
655 (unless manual ".") "~")
656 (file-name-directory file))))
657
658 (defun vc-delete-automatic-version-backups (file)
659 "Delete all existing automatic version backups for FILE."
660 (condition-case nil
661 (mapcar
662 'delete-file
663 (directory-files (or (file-name-directory file) default-directory) t
664 (vc-version-backup-file-name file nil nil t)))
665 ;; Don't fail when the directory doesn't exist.
666 (file-error nil)))
667
668 (defun vc-make-version-backup (file)
669 "Make a backup copy of FILE, which is assumed in sync with the repository.
670 Before doing that, check if there are any old backups and get rid of them."
671 (unless (and (fboundp 'msdos-long-file-names)
672 (not (with-no-warnings (msdos-long-file-names))))
673 (vc-delete-automatic-version-backups file)
674 (condition-case nil
675 (copy-file file (vc-version-backup-file-name file)
676 nil 'keep-date)
677 ;; It's ok if it doesn't work (e.g. directory not writable),
678 ;; since this is just for efficiency.
679 (file-error
680 (message
681 (concat "Warning: Cannot make version backup; "
682 "diff/revert therefore not local"))))))
683
684 (defun vc-before-save ()
685 "Function to be called by `basic-save-buffer' (in files.el)."
686 ;; If the file on disk is still in sync with the repository,
687 ;; and version backups should be made, copy the file to
688 ;; another name. This enables local diffs and local reverting.
689 (let ((file buffer-file-name))
690 (and (vc-backend file)
691 (vc-up-to-date-p file)
692 (eq (vc-checkout-model file) 'implicit)
693 (vc-call make-version-backups-p file)
694 (vc-make-version-backup file))))
695
696 (defun vc-after-save ()
697 "Function to be called by `basic-save-buffer' (in files.el)."
698 ;; If the file in the current buffer is under version control,
699 ;; up-to-date, and locking is not used for the file, set
700 ;; the state to 'edited and redisplay the mode line.
701 (let ((file buffer-file-name))
702 (and (vc-backend file)
703 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
704 (nth 5 (file-attributes file)))
705 ;; File has been saved in the same second in which
706 ;; it was checked out. Clear the checkout-time
707 ;; to avoid confusion.
708 (vc-file-setprop file 'vc-checkout-time nil))
709 t)
710 (vc-up-to-date-p file)
711 (eq (vc-checkout-model file) 'implicit)
712 (vc-file-setprop file 'vc-state 'edited)
713 (vc-mode-line file)
714 (if (featurep 'vc)
715 ;; If VC is not loaded, then there can't be
716 ;; any VC Dired buffer to synchronize.
717 (vc-dired-resynch-file file)))))
718
719 (defconst vc-mode-line-map
720 (let ((map (make-sparse-keymap)))
721 (define-key map [mode-line down-mouse-1] 'vc-menu-map)
722 map))
723
724 (defun vc-mode-line (file)
725 "Set `vc-mode' to display type of version control for FILE.
726 The value is set in the current buffer, which should be the buffer
727 visiting FILE."
728 (interactive (list buffer-file-name))
729 (let ((backend (vc-backend file)))
730 (if (not backend)
731 (setq vc-mode nil)
732 (let* ((ml-string (vc-call mode-line-string file))
733 (ml-echo (get-text-property 0 'help-echo ml-string)))
734 (setq vc-mode
735 (concat
736 " "
737 (if (null vc-display-status)
738 (symbol-name backend)
739 (propertize
740 ml-string
741 'mouse-face 'mode-line-highlight
742 'help-echo
743 (concat (or ml-echo
744 (format "File under the %s version control system"
745 backend))
746 "\nmouse-1: Version Control menu")
747 'local-map vc-mode-line-map)))))
748 ;; If the file is locked by some other user, make
749 ;; the buffer read-only. Like this, even root
750 ;; cannot modify a file that someone else has locked.
751 (and (equal file buffer-file-name)
752 (stringp (vc-state file))
753 (setq buffer-read-only t))
754 ;; If the user is root, and the file is not owner-writable,
755 ;; then pretend that we can't write it
756 ;; even though we can (because root can write anything).
757 ;; This way, even root cannot modify a file that isn't locked.
758 (and (equal file buffer-file-name)
759 (not buffer-read-only)
760 (zerop (user-real-uid))
761 (zerop (logand (file-modes buffer-file-name) 128))
762 (setq buffer-read-only t)))
763 (force-mode-line-update)
764 backend))
765
766 (defun vc-default-mode-line-string (backend file)
767 "Return string for placement in modeline by `vc-mode-line' for FILE.
768 Format:
769
770 \"BACKEND-REV\" if the file is up-to-date
771 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
772 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
773
774 This function assumes that the file is registered."
775 (setq backend (symbol-name backend))
776 (let ((state (vc-state file))
777 (state-echo nil)
778 (rev (vc-workfile-version file)))
779 (propertize
780 (cond ((or (eq state 'up-to-date)
781 (eq state 'needs-patch))
782 (setq state-echo "Up to date file")
783 (concat backend "-" rev))
784 ((stringp state)
785 (setq state-echo (concat "File locked by" state))
786 (concat backend ":" state ":" rev))
787 (t
788 ;; Not just for the 'edited state, but also a fallback
789 ;; for all other states. Think about different symbols
790 ;; for 'needs-patch and 'needs-merge.
791 (setq state-echo "Locally modified file")
792 (concat backend ":" rev)))
793 'help-echo (concat state-echo " under the " backend
794 " version control system"))))
795
796 (defun vc-follow-link ()
797 "If current buffer visits a symbolic link, visit the real file.
798 If the real file is already visited in another buffer, make that buffer
799 current, and kill the buffer that visits the link."
800 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
801 (true-buffer (find-buffer-visiting truename))
802 (this-buffer (current-buffer)))
803 (if (eq true-buffer this-buffer)
804 (progn
805 (kill-buffer this-buffer)
806 ;; In principle, we could do something like set-visited-file-name.
807 ;; However, it can't be exactly the same as set-visited-file-name.
808 ;; I'm not going to work out the details right now. -- rms.
809 (set-buffer (find-file-noselect truename)))
810 (set-buffer true-buffer)
811 (kill-buffer this-buffer))))
812
813 (defun vc-default-find-file-hook (backend)
814 nil)
815
816 (defun vc-find-file-hook ()
817 "Function for `find-file-hook' activating VC mode if appropriate."
818 ;; Recompute whether file is version controlled,
819 ;; if user has killed the buffer and revisited.
820 (if vc-mode
821 (setq vc-mode nil))
822 (when buffer-file-name
823 (vc-file-clearprops buffer-file-name)
824 (cond
825 ((with-demoted-errors (vc-backend buffer-file-name))
826 ;; Compute the state and put it in the modeline.
827 (vc-mode-line buffer-file-name)
828 (unless vc-make-backup-files
829 ;; Use this variable, not make-backup-files,
830 ;; because this is for things that depend on the file name.
831 (set (make-local-variable 'backup-inhibited) t))
832 ;; Let the backend setup any buffer-local things he needs.
833 (vc-call-backend (vc-backend buffer-file-name) 'find-file-hook))
834 ((let ((link-type (and (file-symlink-p buffer-file-name)
835 (vc-backend (file-chase-links buffer-file-name)))))
836 (cond ((not link-type) nil) ;Nothing to do.
837 ((eq vc-follow-symlinks nil)
838 (message
839 "Warning: symbolic link to %s-controlled source file" link-type))
840 ((or (not (eq vc-follow-symlinks 'ask))
841 ;; If we already visited this file by following
842 ;; the link, don't ask again if we try to visit
843 ;; it again. GUD does that, and repeated questions
844 ;; are painful.
845 (get-file-buffer
846 (abbreviate-file-name
847 (file-chase-links buffer-file-name))))
848
849 (vc-follow-link)
850 (message "Followed link to %s" buffer-file-name)
851 (vc-find-file-hook))
852 (t
853 (if (yes-or-no-p (format
854 "Symbolic link to %s-controlled source file; follow link? " link-type))
855 (progn (vc-follow-link)
856 (message "Followed link to %s" buffer-file-name)
857 (vc-find-file-hook))
858 (message
859 "Warning: editing through the link bypasses version control")
860 ))))))))
861
862 (add-hook 'find-file-hook 'vc-find-file-hook)
863
864 ;; more hooks, this time for file-not-found
865 (defun vc-file-not-found-hook ()
866 "When file is not found, try to check it out from version control.
867 Returns t if checkout was successful, nil otherwise.
868 Used in `find-file-not-found-functions'."
869 ;; When a file does not exist, ignore cached info about it
870 ;; from a previous visit.
871 (vc-file-clearprops buffer-file-name)
872 (let ((backend (vc-backend buffer-file-name)))
873 (if backend (vc-call-backend backend 'find-file-not-found-hook))))
874
875 (defun vc-default-find-file-not-found-hook (backend)
876 (if (yes-or-no-p
877 (format "File %s was lost; check out from version control? "
878 (file-name-nondirectory buffer-file-name)))
879 (save-excursion
880 (require 'vc)
881 (setq default-directory (file-name-directory buffer-file-name))
882 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
883
884 (add-hook 'find-file-not-found-functions 'vc-file-not-found-hook)
885
886 (defun vc-kill-buffer-hook ()
887 "Discard VC info about a file when we kill its buffer."
888 (if buffer-file-name
889 (vc-file-clearprops buffer-file-name)))
890
891 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
892
893 ;; Now arrange for (autoloaded) bindings of the main package.
894 ;; Bindings for this have to go in the global map, as we'll often
895 ;; want to call them from random buffers.
896
897 ;; Autoloading works fine, but it prevents shortcuts from appearing
898 ;; in the menu because they don't exist yet when the menu is built.
899 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
900 (defvar vc-prefix-map
901 (let ((map (make-sparse-keymap)))
902 (define-key map "a" 'vc-update-change-log)
903 (define-key map "b" 'vc-switch-backend)
904 (define-key map "c" 'vc-rollback)
905 (define-key map "d" 'vc-directory)
906 (define-key map "g" 'vc-annotate)
907 (define-key map "h" 'vc-insert-headers)
908 (define-key map "i" 'vc-register)
909 (define-key map "l" 'vc-print-log)
910 (define-key map "m" 'vc-merge)
911 (define-key map "r" 'vc-retrieve-snapshot)
912 (define-key map "s" 'vc-create-snapshot)
913 (define-key map "u" 'vc-revert)
914 (define-key map "v" 'vc-next-action)
915 (define-key map "+" 'vc-update)
916 (define-key map "=" 'vc-diff)
917 (define-key map "~" 'vc-version-other-window)
918 map))
919 (fset 'vc-prefix-map vc-prefix-map)
920 (define-key global-map "\C-xv" 'vc-prefix-map)
921
922 (if (not (boundp 'vc-menu-map))
923 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
924 ;; vc-menu-map.
925 ()
926 ;;(define-key vc-menu-map [show-files]
927 ;; '("Show Files under VC" . (vc-directory t)))
928 (define-key vc-menu-map [vc-retrieve-snapshot]
929 '("Retrieve Snapshot" . vc-retrieve-snapshot))
930 (define-key vc-menu-map [vc-create-snapshot]
931 '("Create Snapshot" . vc-create-snapshot))
932 (define-key vc-menu-map [vc-directory] '("VC Directory Listing" . vc-directory))
933 (define-key vc-menu-map [separator1] '("----"))
934 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
935 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
936 (define-key vc-menu-map [vc-version-other-window]
937 '("Show Other Version" . vc-version-other-window))
938 (define-key vc-menu-map [vc-diff] '("Compare with Base Version" . vc-diff))
939 (define-key vc-menu-map [vc-update-change-log]
940 '("Update ChangeLog" . vc-update-change-log))
941 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
942 (define-key vc-menu-map [separator2] '("----"))
943 (define-key vc-menu-map [vc-insert-header]
944 '("Insert Header" . vc-insert-headers))
945 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-rollback))
946 (define-key vc-menu-map [vc-revert]
947 '("Revert to Base Version" . vc-revert))
948 (define-key vc-menu-map [vc-update]
949 '("Update to Latest Version" . vc-update))
950 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
951 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
952
953 ;; These are not correct and it's not currently clear how doing it
954 ;; better (with more complicated expressions) might slow things down
955 ;; on older systems.
956
957 ;;(put 'vc-rename-file 'menu-enable 'vc-mode)
958 ;;(put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
959 ;;(put 'vc-version-other-window 'menu-enable 'vc-mode)
960 ;;(put 'vc-diff 'menu-enable 'vc-mode)
961 ;;(put 'vc-update-change-log 'menu-enable
962 ;; '(member (vc-buffer-backend) '(RCS CVS)))
963 ;;(put 'vc-print-log 'menu-enable 'vc-mode)
964 ;;(put 'vc-rollback 'menu-enable 'vc-mode)
965 ;;(put 'vc-revert 'menu-enable 'vc-mode)
966 ;;(put 'vc-insert-headers 'menu-enable 'vc-mode)
967 ;;(put 'vc-next-action 'menu-enable 'vc-mode)
968 ;;(put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
969
970 (provide 'vc-hooks)
971
972 ;; arch-tag: 2e5a6fa7-1d30-48e2-8bd0-e3d335f04f32
973 ;;; vc-hooks.el ends here