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