]> code.delx.au - gnu-emacs/blob - lisp/vc-hooks.el
*** empty log message ***
[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, 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)
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 ;; Also, Meta-CVS (corresponsding to MCVS) is unsupported.
79 (defcustom vc-directory-exclusion-list '("SCCS" "RCS" "CVS" "MCVS"
80 ".svn" ".git" ".hg" ".bzr"
81 "_MTN" "_darcs" "{arch}")
82 "List of directory names to be ignored when walking directory trees."
83 :type '(repeat string)
84 :group 'vc)
85
86 (defcustom vc-path nil
87 "List of extra directories to search for version control commands."
88 :type '(repeat directory)
89 :group 'vc)
90
91 (defcustom vc-make-backup-files nil
92 "If non-nil, backups of registered files are made as with other files.
93 If nil (the default), files covered by version control don't get backups."
94 :type 'boolean
95 :group 'vc
96 :group 'backup)
97
98 (defcustom vc-follow-symlinks 'ask
99 "What to do if visiting a symbolic link to a file under version control.
100 Editing such a file through the link bypasses the version control system,
101 which is dangerous and probably not what you want.
102
103 If this variable is t, VC follows the link and visits the real file,
104 telling you about it in the echo area. If it is `ask', VC asks for
105 confirmation whether it should follow the link. If nil, the link is
106 visited and a warning displayed."
107 :type '(choice (const :tag "Ask for confirmation" ask)
108 (const :tag "Visit link and warn" nil)
109 (const :tag "Follow link" t))
110 :group 'vc)
111
112 (defcustom vc-display-status t
113 "If non-nil, display revision number and lock status in modeline.
114 Otherwise, not displayed."
115 :type 'boolean
116 :group 'vc)
117
118
119 (defcustom vc-consult-headers t
120 "If non-nil, identify work files by searching for version headers."
121 :type 'boolean
122 :group 'vc)
123
124 (defcustom vc-keep-workfiles t
125 "If non-nil, don't delete working files after registering changes.
126 If the back-end is CVS, workfiles are always kept, regardless of the
127 value of this flag."
128 :type 'boolean
129 :group 'vc)
130
131 (defcustom vc-mistrust-permissions nil
132 "If non-nil, don't assume permissions/ownership track version-control status.
133 If nil, do rely on the permissions.
134 See also variable `vc-consult-headers'."
135 :type 'boolean
136 :group 'vc)
137
138 (defun vc-mistrust-permissions (file)
139 "Internal access function to variable `vc-mistrust-permissions' for FILE."
140 (or (eq vc-mistrust-permissions 't)
141 (and vc-mistrust-permissions
142 (funcall vc-mistrust-permissions
143 (vc-backend-subdirectory-name file)))))
144
145 (defcustom vc-stay-local t
146 "Non-nil means use local operations when possible for remote repositories.
147 This avoids slow queries over the network and instead uses heuristics
148 and past information to determine the current status of a file.
149
150 The value can also be a regular expression or list of regular
151 expressions to match against the host name of a repository; then VC
152 only stays local for hosts that match it. Alternatively, the value
153 can be a list of regular expressions where the first element is the
154 symbol `except'; then VC always stays local except for hosts matched
155 by these regular expressions."
156 :type '(choice (const :tag "Always stay local" t)
157 (const :tag "Don't stay local" nil)
158 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
159 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
160 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
161 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
162 :version "22.1"
163 :group 'vc)
164
165 (defun vc-stay-local-p (file)
166 "Return non-nil if VC should stay local when handling FILE.
167 This uses the `repository-hostname' backend operation.
168 If FILE is a list of files, return non-nil if any of them
169 individually should stay local."
170 (if (listp file)
171 (delq nil (mapcar 'vc-stay-local-p file))
172 (let* ((backend (vc-backend file))
173 (sym (vc-make-backend-sym backend 'stay-local))
174 (stay-local (if (boundp sym) (symbol-value sym) t)))
175 (if (eq stay-local t) (setq stay-local vc-stay-local))
176 (if (symbolp stay-local) stay-local
177 (let ((dirname (if (file-directory-p file)
178 (directory-file-name file)
179 (file-name-directory file))))
180 (eq 'yes
181 (or (vc-file-getprop dirname 'vc-stay-local-p)
182 (vc-file-setprop
183 dirname 'vc-stay-local-p
184 (let ((hostname (vc-call-backend
185 backend 'repository-hostname dirname)))
186 (if (not hostname)
187 'no
188 (let ((default t))
189 (if (eq (car-safe stay-local) 'except)
190 (setq default nil stay-local (cdr stay-local)))
191 (when (consp stay-local)
192 (setq stay-local
193 (mapconcat 'identity stay-local "\\|")))
194 (if (if (string-match stay-local hostname)
195 default (not default))
196 'yes 'no))))))))))))
197
198 ;;; This is handled specially now.
199 ;; Tell Emacs about this new kind of minor mode
200 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
201
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 &optional invert)
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 Optional arg INVERT non-nil reverses the sense of the check;
329 the root is the last directory for which WITNESS *is* found."
330 ;; Represent /home/luser/foo as ~/foo so that we don't try to look for
331 ;; witnesses in /home or in /.
332 (setq file (abbreviate-file-name file))
333 (let ((root nil)
334 (prev-file file)
335 ;; `user' is not initialized outside the loop because
336 ;; `file' may not exist, so we may have to walk up part of the
337 ;; hierarchy before we find the "initial UID".
338 (user nil)
339 try)
340 (while (not (or root
341 (null file)
342 ;; As a heuristic, we stop looking up the hierarchy of
343 ;; directories as soon as we find a directory belonging
344 ;; to another user. This should save us from looking in
345 ;; things like /net and /afs. This assumes that all the
346 ;; files inside a project belong to the same user.
347 (let ((prev-user user))
348 (setq user (nth 2 (file-attributes file)))
349 (and prev-user (not (equal user prev-user))))
350 (string-match vc-ignore-dir-regexp file)))
351 (setq try (file-exists-p (expand-file-name witness file)))
352 (cond ((and invert (not try)) (setq root prev-file))
353 ((and (not invert) try) (setq root file))
354 ((equal file (setq prev-file file
355 file (file-name-directory
356 (directory-file-name file))))
357 (setq file nil))))
358 ;; Handle the case where ~/WITNESS exists and the original FILE is "~".
359 ;; (This occurs, for example, when placing dotfiles under RCS.)
360 (when (and (not root) invert prev-file)
361 (setq root prev-file))
362 root))
363
364 ;; Access functions to file properties
365 ;; (Properties should be _set_ using vc-file-setprop, but
366 ;; _retrieved_ only through these functions, which decide
367 ;; if the property is already known or not. A property should
368 ;; only be retrieved by vc-file-getprop if there is no
369 ;; access function.)
370
371 ;; properties indicating the backend being used for FILE
372
373 (defun vc-registered (file)
374 "Return non-nil if FILE is registered in a version control system.
375
376 This function performs the check each time it is called. To rely
377 on the result of a previous call, use `vc-backend' instead. If the
378 file was previously registered under a certain backend, then that
379 backend is tried first."
380 (let (handler)
381 (cond
382 ((and (file-name-directory file) (string-match vc-ignore-dir-regexp (file-name-directory file)))
383 nil)
384 ((and (boundp 'file-name-handler-alist)
385 (setq handler (find-file-name-handler file 'vc-registered)))
386 ;; handler should set vc-backend and return t if registered
387 (funcall handler 'vc-registered file))
388 (t
389 ;; There is no file name handler.
390 ;; Try vc-BACKEND-registered for each handled BACKEND.
391 (catch 'found
392 (let ((backend (vc-file-getprop file 'vc-backend)))
393 (mapc
394 (lambda (b)
395 (and (vc-call-backend b 'registered file)
396 (vc-file-setprop file 'vc-backend b)
397 (throw 'found t)))
398 (if (or (not backend) (eq backend 'none))
399 vc-handled-backends
400 (cons backend vc-handled-backends))))
401 ;; File is not registered.
402 (vc-file-setprop file 'vc-backend 'none)
403 nil)))))
404
405 (defun vc-backend (file-or-list)
406 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
407 If the argument is a list, the files must all have the same back end."
408 ;; `file' can be nil in several places (typically due to the use of
409 ;; code like (vc-backend buffer-file-name)).
410 (cond ((stringp file-or-list)
411 (let ((property (vc-file-getprop file-or-list 'vc-backend)))
412 ;; Note that internally, Emacs remembers unregistered
413 ;; files by setting the property to `none'.
414 (cond ((eq property 'none) nil)
415 (property)
416 ;; vc-registered sets the vc-backend property
417 (t (if (vc-registered file-or-list)
418 (vc-file-getprop file-or-list 'vc-backend)
419 nil)))))
420 ((and file-or-list (listp file-or-list))
421 (vc-backend (car file-or-list)))
422 (t
423 nil)))
424
425
426 (defun vc-backend-subdirectory-name (file)
427 "Return where the master and lock FILEs for the current directory are kept."
428 (symbol-name (vc-backend file)))
429
430 (defun vc-name (file)
431 "Return the master name of FILE.
432 If the file is not registered, or the master name is not known, return nil."
433 ;; TODO: This should ultimately become obsolete, at least up here
434 ;; in vc-hooks.
435 (or (vc-file-getprop file 'vc-name)
436 ;; force computation of the property by calling
437 ;; vc-BACKEND-registered explicitly
438 (if (and (vc-backend file)
439 (vc-call-backend (vc-backend file) 'registered file))
440 (vc-file-getprop file 'vc-name))))
441
442 (defun vc-checkout-model (backend files)
443 "Indicate how FILES are checked out.
444
445 If FILES are not registered, this function always returns nil.
446 For registered files, the possible values are:
447
448 'implicit FILES are always writeable, and checked out `implicitly'
449 when the user saves the first changes to the file.
450
451 'locking FILES are read-only if up-to-date; user must type
452 \\[vc-next-action] before editing. Strict locking
453 is assumed.
454
455 'announce FILES are read-only if up-to-date; user must type
456 \\[vc-next-action] before editing. But other users
457 may be editing at the same time."
458 (vc-call-backend backend 'checkout-model files))
459
460 (defun vc-user-login-name (file)
461 "Return the name under which the user accesses the given FILE."
462 (or (and (eq (string-match tramp-file-name-regexp file) 0)
463 ;; tramp case: execute "whoami" via tramp
464 (let ((default-directory (file-name-directory file)))
465 (with-temp-buffer
466 (if (not (zerop (process-file "whoami" nil t)))
467 ;; fall through if "whoami" didn't work
468 nil
469 ;; remove trailing newline
470 (delete-region (1- (point-max)) (point-max))
471 (buffer-string)))))
472 ;; normal case
473 (user-login-name)
474 ;; if user-login-name is nil, return the UID as a string
475 (number-to-string (user-uid))))
476
477 (defun vc-state (file)
478 "Return the version control state of FILE.
479
480 If FILE is not registered, this function always returns nil.
481 For registered files, the value returned is one of:
482
483 'up-to-date The working file is unmodified with respect to the
484 latest version on the current branch, and not locked.
485
486 'edited The working file has been edited by the user. If
487 locking is used for the file, this state means that
488 the current version is locked by the calling user.
489
490 USER The current version of the working file is locked by
491 some other USER (a string).
492
493 'needs-update The file has not been edited by the user, but there is
494 a more recent version on the current branch stored
495 in the master file.
496
497 'needs-merge The file has been edited by the user, and there is also
498 a more recent version on the current branch stored in
499 the master file. This state can only occur if locking
500 is not used for the file.
501
502 'unlocked-changes The working version of the file is not locked,
503 but the working file has been changed with respect
504 to that version. This state can only occur for files
505 with locking; it represents an erroneous condition that
506 should be resolved by the user (vc-next-action will
507 prompt the user to do it).
508
509 'added Scheduled to go into the repository on the next commit.
510 Often represented by vc-working-revision = \"0\" in VCSes
511 with monotonic IDs like Subversion and Mercurial.
512
513 'removed Scheduled to be deleted from the repository on next commit.
514
515 'conflict The file contains conflicts as the result of a merge.
516 For now the conflicts are text conflicts. In the
517 future this might be extended to deal with metadata
518 conflicts too.
519
520 'missing The file is not present in the file system, but the VC
521 system still tracks it.
522
523 'ignored The file showed up in a dir-status listing with a flag
524 indicating the version-control system is ignoring it,
525 Note: This property is not set reliably (some VCSes
526 don't have useful directory-status commands) so assume
527 that any file with vc-state nil might be ignorable
528 without VC knowing it.
529
530 'unregistered The file is not under version control.
531
532 A return of nil from this function means we have no information on the
533 status of this file."
534 ;; Note: in Emacs 22 and older, return of nil meant the file was
535 ;; unregistered. This is potentially a source of
536 ;; backward-compatibility bugs.
537
538 ;; FIXME: New (sub)states needed (?):
539 ;; - `copied' and `moved' (might be handled by `removed' and `added')
540 (or (vc-file-getprop file 'vc-state)
541 (when (> (length file) 0)
542 (let ((backend (vc-backend file)))
543 (when backend
544 (vc-file-setprop
545 file 'vc-state
546 (vc-call-backend backend 'state-heuristic file)))))))
547
548 (defsubst vc-up-to-date-p (file)
549 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
550 (eq (vc-state file) 'up-to-date))
551
552 (defun vc-default-state-heuristic (backend file)
553 "Default implementation of vc-BACKEND-state-heuristic.
554 It simply calls the real state computation function `vc-BACKEND-state'
555 and does not employ any heuristic at all."
556 (vc-call-backend backend 'state file))
557
558 (defun vc-workfile-unchanged-p (file)
559 "Return non-nil if FILE has not changed since the last checkout."
560 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
561 (lastmod (nth 5 (file-attributes file))))
562 ;; This is a shortcut for determining when the workfile is
563 ;; unchanged. It can fail under some circumstances; see the
564 ;; discussion in bug#694.
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-backend backend '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 backend 'diff))))
587 (not (eq (caddr err) 4)))
588 (signal (car err) (cdr err))
589 (vc-call-backend backend 'diff (list file)))))))
590
591 (defun vc-working-revision (file)
592 "Return the repository version from which FILE was checked out.
593 If FILE is not registered, this function always returns nil."
594 (or (vc-file-getprop file 'vc-working-revision)
595 (let ((backend (vc-backend file)))
596 (when backend
597 (vc-file-setprop file 'vc-working-revision
598 (vc-call-backend backend '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-backend backend 'make-version-backups-p file)
749 (vc-make-version-backup file)))))
750
751 (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
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 ;; Try to avoid unnecessary work, a *vc-dir* buffer is only
773 ;; present if this is true.
774 (when (memq 'vc-dir-resynch-file after-save-hook)
775 (vc-dir-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-backend backend '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 (add-hook 'mode-line-hook 'vc-mode-line nil t)
905 (cond
906 ((with-demoted-errors (vc-backend buffer-file-name))
907 ;; Compute the state and put it in the modeline.
908 (vc-mode-line buffer-file-name)
909 (unless vc-make-backup-files
910 ;; Use this variable, not make-backup-files,
911 ;; because this is for things that depend on the file name.
912 (set (make-local-variable 'backup-inhibited) t))
913 ;; Let the backend setup any buffer-local things he needs.
914 (vc-call-backend (vc-backend buffer-file-name) 'find-file-hook))
915 ((let ((link-type (and (file-symlink-p buffer-file-name)
916 (vc-backend (file-chase-links buffer-file-name)))))
917 (cond ((not link-type) nil) ;Nothing to do.
918 ((eq vc-follow-symlinks nil)
919 (message
920 "Warning: symbolic link to %s-controlled source file" link-type))
921 ((or (not (eq vc-follow-symlinks 'ask))
922 ;; If we already visited this file by following
923 ;; the link, don't ask again if we try to visit
924 ;; it again. GUD does that, and repeated questions
925 ;; are painful.
926 (get-file-buffer
927 (abbreviate-file-name
928 (file-chase-links buffer-file-name))))
929
930 (vc-follow-link)
931 (message "Followed link to %s" buffer-file-name)
932 (vc-find-file-hook))
933 (t
934 (if (yes-or-no-p (format
935 "Symbolic link to %s-controlled source file; follow link? " link-type))
936 (progn (vc-follow-link)
937 (message "Followed link to %s" buffer-file-name)
938 (vc-find-file-hook))
939 (message
940 "Warning: editing through the link bypasses version control")
941 ))))))))
942
943 (add-hook 'find-file-hook 'vc-find-file-hook)
944
945 ;; more hooks, this time for file-not-found
946 (defun vc-file-not-found-hook ()
947 "When file is not found, try to check it out from version control.
948 Returns t if checkout was successful, nil otherwise.
949 Used in `find-file-not-found-functions'."
950 ;; When a file does not exist, ignore cached info about it
951 ;; from a previous visit.
952 (vc-file-clearprops buffer-file-name)
953 (let ((backend (vc-backend buffer-file-name)))
954 (when backend (vc-call-backend backend 'find-file-not-found-hook))))
955
956 (defun vc-default-find-file-not-found-hook (backend)
957 ;; This used to do what vc-rcs-find-file-not-found-hook does, but it only
958 ;; really makes sense for RCS. For other backends, better not do anything.
959 nil)
960
961 (add-hook 'find-file-not-found-functions 'vc-file-not-found-hook)
962
963 (defun vc-kill-buffer-hook ()
964 "Discard VC info about a file when we kill its buffer."
965 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
966
967 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
968
969 ;; Now arrange for (autoloaded) bindings of the main package.
970 ;; Bindings for this have to go in the global map, as we'll often
971 ;; want to call them from random buffers.
972
973 ;; Autoloading works fine, but it prevents shortcuts from appearing
974 ;; in the menu because they don't exist yet when the menu is built.
975 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
976 (defvar vc-prefix-map
977 (let ((map (make-sparse-keymap)))
978 (define-key map "a" 'vc-update-change-log)
979 (define-key map "b" 'vc-switch-backend)
980 (define-key map "c" 'vc-rollback)
981 (define-key map "d" 'vc-dir)
982 (define-key map "g" 'vc-annotate)
983 (define-key map "h" 'vc-insert-headers)
984 (define-key map "i" 'vc-register)
985 (define-key map "l" 'vc-print-log)
986 (define-key map "m" 'vc-merge)
987 (define-key map "r" 'vc-retrieve-tag)
988 (define-key map "s" 'vc-create-tag)
989 (define-key map "u" 'vc-revert)
990 (define-key map "v" 'vc-next-action)
991 (define-key map "+" 'vc-update)
992 (define-key map "=" 'vc-diff)
993 (define-key map "~" 'vc-revision-other-window)
994 map))
995 (fset 'vc-prefix-map vc-prefix-map)
996 (define-key global-map "\C-xv" 'vc-prefix-map)
997
998 (defvar vc-menu-map
999 (let ((map (make-sparse-keymap "Version Control")))
1000 ;;(define-key map [show-files]
1001 ;; '("Show Files under VC" . (vc-directory t)))
1002 (define-key map [vc-retrieve-tag]
1003 '(menu-item "Retrieve Tag" vc-retrieve-tag
1004 :help "Retrieve tagged version or branch"))
1005 (define-key map [vc-create-tag]
1006 '(menu-item "Create Tag" vc-create-tag
1007 :help "Create version tag"))
1008 (define-key map [separator1] '("----"))
1009 (define-key map [vc-annotate]
1010 '(menu-item "Annotate" vc-annotate
1011 :help "Display the edit history of the current file using colors"))
1012 (define-key map [vc-rename-file]
1013 '(menu-item "Rename File" vc-rename-file
1014 :help "Rename file"))
1015 (define-key map [vc-revision-other-window]
1016 '(menu-item "Show Other Version" vc-revision-other-window
1017 :help "Visit another version of the current file in another window"))
1018 (define-key map [vc-diff]
1019 '(menu-item "Compare with Base Version" vc-diff
1020 :help "Compare file set with the base version"))
1021 (define-key map [vc-update-change-log]
1022 '(menu-item "Update ChangeLog" vc-update-change-log
1023 :help "Find change log file and add entries from recent version control logs"))
1024 (define-key map [vc-print-log]
1025 '(menu-item "Show History" vc-print-log
1026 :help "List the change log of the current file set in a window"))
1027 (define-key map [separator2] '("----"))
1028 (define-key map [vc-insert-header]
1029 '(menu-item "Insert Header" vc-insert-headers
1030 :help "Insert headers into a file for use with a version control system.
1031 "))
1032 (define-key map [undo]
1033 '(menu-item "Undo Last Check-In" vc-rollback
1034 :help "Remove the most recent changeset committed to the repository"))
1035 (define-key map [vc-revert]
1036 '(menu-item "Revert to Base Version" vc-revert
1037 :help "Revert working copies of the selected file set to their repository contents"))
1038 (define-key map [vc-update]
1039 '(menu-item "Update to Latest Version" vc-update
1040 :help "Update the current fileset's files to their tip revisions"))
1041 (define-key map [vc-next-action]
1042 '(menu-item "Check In/Out" vc-next-action
1043 :help "Do the next logical version control operation on the current fileset"))
1044 (define-key map [vc-register]
1045 '(menu-item "Register" vc-register
1046 :help "Register file set into a version control system"))
1047 (define-key map [vc-dir]
1048 '(menu-item "VC Dir" vc-dir
1049 :help "Show the VC status of files in a directory"))
1050 map))
1051
1052 (defalias 'vc-menu-map vc-menu-map)
1053
1054 (declare-function vc-responsible-backend "vc" (file &optional register))
1055
1056 (defun vc-menu-map-filter (orig-binding)
1057 (if (and (symbolp orig-binding) (fboundp orig-binding))
1058 (setq orig-binding (indirect-function orig-binding)))
1059 (let ((ext-binding
1060 (when vc-mode
1061 (vc-call-backend
1062 (if buffer-file-name
1063 (vc-backend buffer-file-name)
1064 (vc-responsible-backend default-directory))
1065 'extra-menu))))
1066 ;; Give the VC backend a chance to add menu entries
1067 ;; specific for that backend.
1068 (if (null ext-binding)
1069 orig-binding
1070 (append orig-binding
1071 '((ext-menu-separator "---"))
1072 ext-binding))))
1073
1074 (defun vc-default-extra-menu (backend)
1075 nil)
1076
1077 (provide 'vc-hooks)
1078
1079 ;; arch-tag: 2e5a6fa7-1d30-48e2-8bd0-e3d335f04f32
1080 ;;; vc-hooks.el ends here