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