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