Sync to HEAD
[bpt/emacs.git] / lisp / vc-hooks.el
CommitLineData
aae56ea7 1;;; vc-hooks.el --- resident support for version-control
594722a8 2
6b61353c 3;; Copyright (C) 1992,93,94,95,96,98,99,2000,03,2004
0bfb74a9 4;; Free Software Foundation, Inc.
594722a8 5
0e0d9831
GM
6;; Author: FSF (see vc.el for full credits)
7;; Maintainer: Andre Spiegel <spiegel@gnu.org>
594722a8 8
6b61353c 9;; $Id: vc-hooks.el,v 1.165 2004/03/28 17:38:03 monnier Exp $
f4c72097 10
594722a8
ER
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b578f267
EN
24;; along with GNU Emacs; see the file COPYING. If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
594722a8
ER
27
28;;; Commentary:
29
0e0d9831
GM
30;; This is the always-loaded portion of VC. It takes care of
31;; VC-related activities that are done when you visit a file, so that
32;; vc.el itself is loaded only when you use a VC command. See the
33;; commentary of vc.el.
594722a8
ER
34
35;;; Code:
36
c3ce5e29
AS
37(eval-when-compile
38 (require 'cl))
099bd78a 39
e1c0c2d1
KH
40;; Customization Variables (the rest is in vc.el)
41
e18cf2ee 42(defvar vc-ignore-vc-files nil)
6b61353c
KH
43(make-obsolete-variable 'vc-ignore-vc-files
44 "set `vc-handled-backends' to nil to disable VC.")
45
e18cf2ee 46(defvar vc-master-templates ())
6b61353c
KH
47(make-obsolete-variable 'vc-master-templates
48 "to define master templates for a given BACKEND, use
49vc-BACKEND-master-templates. To enable or disable VC for a given
50BACKEND, use `vc-handled-backends'.")
51
e18cf2ee
JB
52(defvar vc-header-alist ())
53(make-obsolete-variable 'vc-header-alist 'vc-BACKEND-header)
0e0d9831 54
6b61353c
KH
55(defcustom vc-handled-backends '(RCS CVS SVN SCCS Arch MCVS)
56 ;; Arch and MCVS come last because they are per-tree rather than per-dir.
0e0d9831
GM
57 "*List of version control backends for which VC will be used.
58Entries in this list will be tried in order to determine whether a
59file is under that sort of version control.
60Removing an entry from the list prevents VC from being activated
61when visiting a file managed by that backend.
62An empty list disables VC altogether."
63 :type '(repeat symbol)
099bd78a 64 :version "21.1"
50bec091 65 :group 'vc)
31888047 66
50bec091 67(defcustom vc-path
e1c0c2d1
KH
68 (if (file-directory-p "/usr/sccs")
69 '("/usr/sccs")
70 nil)
50bec091
KH
71 "*List of extra directories to search for version control commands."
72 :type '(repeat directory)
73 :group 'vc)
e1c0c2d1 74
50bec091 75(defcustom vc-make-backup-files nil
5032bd23 76 "*If non-nil, backups of registered files are made as with other files.
50bec091
KH
77If nil (the default), files covered by version control don't get backups."
78 :type 'boolean
8f383484
DL
79 :group 'vc
80 :group 'backup)
594722a8 81
50bec091 82(defcustom vc-follow-symlinks 'ask
0e0d9831
GM
83 "*What to do if visiting a symbolic link to a file under version control.
84Editing such a file through the link bypasses the version control system,
85which is dangerous and probably not what you want.
86
87If this variable is t, VC follows the link and visits the real file,
b8063212
AS
88telling you about it in the echo area. If it is `ask', VC asks for
89confirmation whether it should follow the link. If nil, the link is
50bec091 90visited and a warning displayed."
0e0d9831
GM
91 :type '(choice (const :tag "Ask for confirmation" ask)
92 (const :tag "Visit link and warn" nil)
93 (const :tag "Follow link" t))
50bec091 94 :group 'vc)
b8063212 95
50bec091 96(defcustom vc-display-status t
624c0e9d 97 "*If non-nil, display revision number and lock status in modeline.
50bec091
KH
98Otherwise, not displayed."
99 :type 'boolean
100 :group 'vc)
101
198d5c00 102
50bec091
KH
103(defcustom vc-consult-headers t
104 "*If non-nil, identify work files by searching for version headers."
105 :type 'boolean
106 :group 'vc)
e1c0c2d1 107
50bec091 108(defcustom vc-keep-workfiles t
e1c0c2d1
KH
109 "*If non-nil, don't delete working files after registering changes.
110If the back-end is CVS, workfiles are always kept, regardless of the
50bec091
KH
111value of this flag."
112 :type 'boolean
113 :group 'vc)
e1c0c2d1 114
50bec091 115(defcustom vc-mistrust-permissions nil
0e0d9831
GM
116 "*If non-nil, don't assume permissions/ownership track version-control status.
117If nil, do rely on the permissions.
50bec091
KH
118See also variable `vc-consult-headers'."
119 :type 'boolean
120 :group 'vc)
e66eac08
AS
121
122(defun vc-mistrust-permissions (file)
0e0d9831 123 "Internal access function to variable `vc-mistrust-permissions' for FILE."
e66eac08
AS
124 (or (eq vc-mistrust-permissions 't)
125 (and vc-mistrust-permissions
0e0d9831 126 (funcall vc-mistrust-permissions
e66eac08
AS
127 (vc-backend-subdirectory-name file)))))
128
113414a9
SM
129(defcustom vc-stay-local t
130 "*Non-nil means use local operations when possible for remote repositories.
131This avoids slow queries over the network and instead uses heuristics
132and past information to determine the current status of a file.
133
134The value can also be a regular expression or list of regular
135expressions to match against the host name of a repository; then VC
136only stays local for hosts that match it. Alternatively, the value
137can be a list of regular expressions where the first element is the
138symbol `except'; then VC always stays local except for hosts matched
139by these regular expressions."
140 :type '(choice (const :tag "Always stay local" t)
141 (const :tag "Don't stay local" nil)
142 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
143 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
144 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
145 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
146 :version "21.4"
147 :group 'vc)
148
149(defun vc-stay-local-p (file)
150 "Return non-nil if VC should stay local when handling FILE.
151This uses the `repository-hostname' backend operation."
152 (let* ((backend (vc-backend file))
153 (sym (vc-make-backend-sym backend 'stay-local))
154 (stay-local (if (boundp sym) (symbol-value sym) t)))
155 (if (eq stay-local t) (setq stay-local vc-stay-local))
156 (if (symbolp stay-local) stay-local
157 (let ((dirname (if (file-directory-p file)
158 (directory-file-name file)
159 (file-name-directory file))))
160 (eq 'yes
161 (or (vc-file-getprop dirname 'vc-stay-local-p)
162 (vc-file-setprop
163 dirname 'vc-stay-local-p
164 (let ((hostname (vc-call-backend
165 backend 'repository-hostname dirname)))
166 (if (not hostname)
167 'no
168 (let ((default t))
169 (if (eq (car-safe stay-local) 'except)
170 (setq default nil stay-local (cdr stay-local)))
171 (when (consp stay-local)
172 (setq stay-local
173 (mapconcat 'identity stay-local "\\|")))
174 (if (if (string-match stay-local hostname)
175 default (not default))
176 'yes 'no)))))))))))
177
9bd06e95 178;;; This is handled specially now.
594722a8 179;; Tell Emacs about this new kind of minor mode
9bd06e95 180;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
594722a8 181
7bc2b98b 182(make-variable-buffer-local 'vc-mode)
c43e436c 183(put 'vc-mode 'permanent-local t)
594722a8 184
14fef9de
SM
185(defun vc-mode (&optional arg)
186 ;; Dummy function for C-h m
187 "Version Control minor mode.
188This minor mode is automatically activated whenever you visit a file under
189control of one of the revision control systems in `vc-handled-backends'.
190VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
191\\{vc-prefix-map}")
192
099bd78a 193(defmacro vc-error-occurred (&rest body)
becd6193 194 `(condition-case nil (progn ,@body nil) (error t)))
099bd78a 195
594722a8 196;; We need a notion of per-file properties because the version
f2ee4191 197;; control state of a file is expensive to derive --- we compute
0e0d9831 198;; them when the file is initially found, keep them up to date
f2ee4191
RS
199;; during any subsequent VC operations, and forget them when
200;; the buffer is killed.
594722a8 201
becd6193 202(defvar vc-file-prop-obarray (make-vector 17 0)
594722a8
ER
203 "Obarray for per-file properties.")
204
099bd78a
SM
205(defvar vc-touched-properties nil)
206
594722a8 207(defun vc-file-setprop (file property value)
0e0d9831 208 "Set per-file VC PROPERTY for FILE to VALUE."
099bd78a
SM
209 (if (and vc-touched-properties
210 (not (memq property vc-touched-properties)))
211 (setq vc-touched-properties (append (list property)
212 vc-touched-properties)))
594722a8
ER
213 (put (intern file vc-file-prop-obarray) property value))
214
215(defun vc-file-getprop (file property)
099bd78a 216 "Get per-file VC PROPERTY for FILE."
594722a8
ER
217 (get (intern file vc-file-prop-obarray) property))
218
e1c0c2d1 219(defun vc-file-clearprops (file)
0e0d9831 220 "Clear all VC properties of FILE."
e1c0c2d1 221 (setplist (intern file vc-file-prop-obarray) nil))
f2ee4191 222
0e0d9831
GM
223\f
224;; We keep properties on each symbol naming a backend as follows:
225;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
226
227(defun vc-make-backend-sym (backend sym)
228 "Return BACKEND-specific version of VC symbol SYM."
229 (intern (concat "vc-" (downcase (symbol-name backend))
230 "-" (symbol-name sym))))
231
232(defun vc-find-backend-function (backend fun)
233 "Return BACKEND-specific implementation of FUN.
f8b72742 234If there is no such implementation, return the default implementation;
0e0d9831
GM
235if that doesn't exist either, return nil."
236 (let ((f (vc-make-backend-sym backend fun)))
237 (if (fboundp f) f
238 ;; Load vc-BACKEND.el if needed.
239 (require (intern (concat "vc-" (downcase (symbol-name backend)))))
240 (if (fboundp f) f
241 (let ((def (vc-make-backend-sym 'default fun)))
242 (if (fboundp def) (cons def backend) nil))))))
243
244(defun vc-call-backend (backend function-name &rest args)
245 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
246Calls
247
248 (apply 'vc-BACKEND-FUN ARGS)
249
250if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
251and else calls
252
253 (apply 'vc-default-FUN BACKEND ARGS)
254
255It is usually called via the `vc-call' macro."
0bfb74a9
SM
256 (let ((f (assoc function-name (get backend 'vc-functions))))
257 (if f (setq f (cdr f))
0e0d9831 258 (setq f (vc-find-backend-function backend function-name))
0bfb74a9
SM
259 (push (cons function-name f) (get backend 'vc-functions)))
260 (cond
261 ((null f)
262 (error "Sorry, %s is not implemented for %s" function-name backend))
263 ((consp f) (apply (car f) (cdr f) args))
264 (t (apply f args)))))
0e0d9831
GM
265
266(defmacro vc-call (fun file &rest args)
267 ;; BEWARE!! `file' is evaluated twice!!
268 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
0e0d9831
GM
269\f
270(defsubst vc-parse-buffer (pattern i)
271 "Find PATTERN in the current buffer and return its Ith submatch."
272 (goto-char (point-min))
273 (if (re-search-forward pattern nil t)
274 (match-string i)))
e1c0c2d1 275
02d383eb 276(defun vc-insert-file (file &optional limit blocksize)
0e0d9831
GM
277 "Insert the contents of FILE into the current buffer.
278
279Optional argument LIMIT is a regexp. If present, the file is inserted
280in chunks of size BLOCKSIZE (default 8 kByte), until the first
f8b72742 281occurrence of LIMIT is found. Anything from the start of that occurrence
e30140ce
AS
282to the end of the buffer is then deleted. The function returns
283non-nil if FILE exists and its contents were successfully inserted."
07de4c3d 284 (erase-buffer)
ff40374a
AS
285 (when (file-exists-p file)
286 (if (not limit)
287 (insert-file-contents file)
288 (if (not blocksize) (setq blocksize 8192))
4d2806e2
SM
289 (let ((filepos 0))
290 (while
291 (and (< 0 (cadr (insert-file-contents
292 file nil filepos (incf filepos blocksize))))
293 (progn (beginning-of-line)
e30140ce
AS
294 (let ((pos (re-search-forward limit nil 'move)))
295 (if pos (delete-region (match-beginning 0)
296 (point-max)))
297 (not pos)))))))
ff40374a
AS
298 (set-buffer-modified-p nil)
299 t))
02d383eb 300
14fef9de
SM
301;; Access functions to file properties
302;; (Properties should be _set_ using vc-file-setprop, but
303;; _retrieved_ only through these functions, which decide
304;; if the property is already known or not. A property should
305;; only be retrieved by vc-file-getprop if there is no
306;; access function.)
02d383eb 307
14fef9de 308;; properties indicating the backend being used for FILE
e1c0c2d1 309
0e0d9831
GM
310(defun vc-registered (file)
311 "Return non-nil if FILE is registered in a version control system.
312
1f22ad24
AS
313This function performs the check each time it is called. To rely
314on the result of a previous call, use `vc-backend' instead. If the
315file was previously registered under a certain backend, then that
316backend is tried first."
0e0d9831
GM
317 (let (handler)
318 (if (boundp 'file-name-handler-alist)
319 (setq handler (find-file-name-handler file 'vc-registered)))
320 (if handler
321 ;; handler should set vc-backend and return t if registered
322 (funcall handler 'vc-registered file)
323 ;; There is no file name handler.
324 ;; Try vc-BACKEND-registered for each handled BACKEND.
325 (catch 'found
1f22ad24
AS
326 (let ((backend (vc-file-getprop file 'vc-backend)))
327 (mapcar
328 (lambda (b)
329 (and (vc-call-backend b 'registered file)
330 (vc-file-setprop file 'vc-backend b)
331 (throw 'found t)))
f8b72742 332 (if (or (not backend) (eq backend 'none))
1f22ad24
AS
333 vc-handled-backends
334 (cons backend vc-handled-backends))))
0e0d9831
GM
335 ;; File is not registered.
336 (vc-file-setprop file 'vc-backend 'none)
337 nil))))
338
339(defun vc-backend (file)
340 "Return the version control type of FILE, nil if it is not registered."
341 ;; `file' can be nil in several places (typically due to the use of
5232a436 342 ;; code like (vc-backend buffer-file-name)).
0e0d9831
GM
343 (when (stringp file)
344 (let ((property (vc-file-getprop file 'vc-backend)))
345 ;; Note that internally, Emacs remembers unregistered
346 ;; files by setting the property to `none'.
347 (cond ((eq property 'none) nil)
348 (property)
349 ;; vc-registered sets the vc-backend property
350 (t (if (vc-registered file)
351 (vc-file-getprop file 'vc-backend)
352 nil))))))
353
354(defun vc-backend-subdirectory-name (file)
355 "Return where the master and lock FILEs for the current directory are kept."
356 (symbol-name (vc-backend file)))
e1c0c2d1 357
02d383eb 358(defun vc-name (file)
5eb2b516
DL
359 "Return the master name of FILE.
360If the file is not registered, or the master name is not known, return nil."
361 ;; TODO: This should ultimately become obsolete, at least up here
0e0d9831 362 ;; in vc-hooks.
02d383eb 363 (or (vc-file-getprop file 'vc-name)
64341022
AS
364 ;; force computation of the property by calling
365 ;; vc-BACKEND-registered explicitly
366 (if (and (vc-backend file)
367 (vc-call-backend (vc-backend file) 'registered file))
8aa81ea8 368 (vc-file-getprop file 'vc-name))))
e1c0c2d1 369
04446ed0 370(defun vc-checkout-model (file)
0e0d9831
GM
371 "Indicate how FILE is checked out.
372
cb5af02a
AS
373If FILE is not registered, this function always returns nil.
374For registered files, the possible values are:
0e0d9831 375
cb5af02a 376 'implicit FILE is always writeable, and checked out `implicitly'
0e0d9831
GM
377 when the user saves the first changes to the file.
378
cb5af02a 379 'locking FILE is read-only if up-to-date; user must type
db8afaee 380 \\[vc-next-action] before editing. Strict locking
0e0d9831
GM
381 is assumed.
382
cb5af02a 383 'announce FILE is read-only if up-to-date; user must type
db8afaee 384 \\[vc-next-action] before editing. But other users
0e0d9831
GM
385 may be editing at the same time."
386 (or (vc-file-getprop file 'vc-checkout-model)
cb5af02a
AS
387 (if (vc-backend file)
388 (vc-file-setprop file 'vc-checkout-model
389 (vc-call checkout-model file)))))
7064821c 390
2a11c6f3 391(defun vc-user-login-name (&optional uid)
0e0d9831
GM
392 "Return the name under which the user is logged in, as a string.
393\(With optional argument UID, return the name of that user.)
394This function does the same as function `user-login-name', but unlike
395that, it never returns nil. If a UID cannot be resolved, that
396UID is returned as a string."
2a11c6f3 397 (or (user-login-name uid)
0e0d9831
GM
398 (number-to-string (or uid (user-uid)))))
399
400(defun vc-state (file)
401 "Return the version control state of FILE.
402
cb5af02a
AS
403If FILE is not registered, this function always returns nil.
404For registered files, the value returned is one of:
0e0d9831
GM
405
406 'up-to-date The working file is unmodified with respect to the
407 latest version on the current branch, and not locked.
408
409 'edited The working file has been edited by the user. If
410 locking is used for the file, this state means that
411 the current version is locked by the calling user.
412
413 USER The current version of the working file is locked by
414 some other USER (a string).
f8b72742 415
0e0d9831
GM
416 'needs-patch The file has not been edited by the user, but there is
417 a more recent version on the current branch stored
418 in the master file.
419
420 'needs-merge The file has been edited by the user, and there is also
421 a more recent version on the current branch stored in
422 the master file. This state can only occur if locking
423 is not used for the file.
424
425 'unlocked-changes The current version of the working file is not locked,
426 but the working file has been changed with respect
427 to that version. This state can only occur for files
428 with locking; it represents an erroneous condition that
429 should be resolved by the user (vc-next-action will
430 prompt the user to do it)."
2a3897f5
SM
431 ;; FIXME: New (sub)states needed (?):
432 ;; - `added' (i.e. `edited' but with no base version yet,
433 ;; typically represented by vc-workfile-version = "0")
434 ;; - `conflict' (i.e. `edited' with conflict markers)
435 ;; - `removed'
436 ;; - `copied' and `moved' (might be handled by `removed' and `added')
0e0d9831 437 (or (vc-file-getprop file 'vc-state)
cb5af02a
AS
438 (if (vc-backend file)
439 (vc-file-setprop file 'vc-state
440 (vc-call state-heuristic file)))))
0e0d9831
GM
441
442(defsubst vc-up-to-date-p (file)
443 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
444 (eq (vc-state file) 'up-to-date))
445
446(defun vc-default-state-heuristic (backend file)
5eb2b516
DL
447 "Default implementation of vc-state-heuristic.
448It simply calls the real state computation function `vc-BACKEND-state'
449and does not employ any heuristic at all."
0e0d9831 450 (vc-call-backend backend 'state file))
1efcbf46 451
e767004f
AS
452(defun vc-workfile-unchanged-p (file)
453 "Return non-nil if FILE has not changed since the last checkout."
454 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
455 (lastmod (nth 5 (file-attributes file))))
456 (if checkout-time
457 (equal checkout-time lastmod)
458 (let ((unchanged (vc-call workfile-unchanged-p file)))
459 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
460 unchanged))))
461
462(defun vc-default-workfile-unchanged-p (backend file)
463 "Check if FILE is unchanged by diffing against the master version.
464Return non-nil if FILE is unchanged."
6b61353c
KH
465 (zerop (condition-case err
466 ;; If the implementation supports it, let the output
467 ;; go to *vc*, not *vc-diff*, since this is an internal call.
468 (vc-call diff file nil nil "*vc*")
469 (wrong-number-of-arguments
470 ;; If this error came from the above call to vc-BACKEND-diff,
471 ;; try again without the optional buffer argument (for
472 ;; backward compatibility). Otherwise, resignal.
473 (if (or (not (eq (cadr err)
474 (indirect-function
475 (vc-find-backend-function (vc-backend file)
476 'diff))))
477 (not (eq (caddr err) 5)))
478 (signal wrong-number-of-arguments err)
479 (vc-call diff file))))))
e767004f 480
e1c0c2d1 481(defun vc-workfile-version (file)
cb5af02a
AS
482 "Return the version level of the current workfile FILE.
483If FILE is not registered, this function always returns nil."
0e0d9831 484 (or (vc-file-getprop file 'vc-workfile-version)
cb5af02a
AS
485 (if (vc-backend file)
486 (vc-file-setprop file 'vc-workfile-version
487 (vc-call workfile-version file)))))
f2ee4191 488
0e0d9831
GM
489(defun vc-default-registered (backend file)
490 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
491 (let ((sym (vc-make-backend-sym backend 'master-templates)))
492 (unless (get backend 'vc-templates-grabbed)
493 (put backend 'vc-templates-grabbed t)
494 (set sym (append (delq nil
495 (mapcar
496 (lambda (template)
497 (and (consp template)
498 (eq (cdr template) backend)
499 (car template)))
3756d481
AS
500 (with-no-warnings
501 vc-master-templates)))
0e0d9831
GM
502 (symbol-value sym))))
503 (let ((result (vc-check-master-templates file (symbol-value sym))))
504 (if (stringp result)
505 (vc-file-setprop file 'vc-name result)
506 nil)))) ; Not registered
507
508(defun vc-possible-master (s dirname basename)
509 (cond
510 ((stringp s) (format s dirname basename))
511 ((functionp s)
512 ;; The template is a function to invoke. If the
513 ;; function returns non-nil, that means it has found a
514 ;; master. For backward compatibility, we also handle
515 ;; the case that the function throws a 'found atom
516 ;; and a pair (cons MASTER-FILE BACKEND).
517 (let ((result (catch 'found (funcall s dirname basename))))
518 (if (consp result) (car result) result)))))
519
520(defun vc-check-master-templates (file templates)
cb5af02a 521 "Return non-nil if there is a master corresponding to FILE.
0e0d9831
GM
522
523TEMPLATES is a list of strings or functions. If an element is a
524string, it must be a control string as required by `format', with two
525string placeholders, such as \"%sRCS/%s,v\". The directory part of
526FILE is substituted for the first placeholder, the basename of FILE
527for the second. If a file with the resulting name exists, it is taken
528as the master of FILE, and returned.
529
530If an element of TEMPLATES is a function, it is called with the
531directory part and the basename of FILE as arguments. It should
532return non-nil if it finds a master; that value is then returned by
533this function."
534 (let ((dirname (or (file-name-directory file) ""))
535 (basename (file-name-nondirectory file)))
536 (catch 'found
5eb2b516 537 (mapcar
0e0d9831
GM
538 (lambda (s)
539 (let ((trial (vc-possible-master s dirname basename)))
540 (if (and trial (file-exists-p trial)
541 ;; Make sure the file we found with name
542 ;; TRIAL is not the source file itself.
543 ;; That can happen with RCS-style names if
544 ;; the file name is truncated (e.g. to 14
545 ;; chars). See if either directory or
546 ;; attributes differ.
547 (or (not (string= dirname
548 (file-name-directory trial)))
549 (not (equal (file-attributes file)
550 (file-attributes trial)))))
551 (throw 'found trial))))
552 templates))))
f2ee4191 553
c844616c 554(defun vc-toggle-read-only (&optional verbose)
c43e436c 555 "Change read-only status of current buffer, perhaps via version control.
db8afaee 556
c43e436c
RS
557If the buffer is visiting a file registered with version control,
558then check the file in or out. Otherwise, just change the read-only flag
ec44193a
KH
559of the buffer.
560With prefix argument, ask for version number to check in or check out.
561Check-out of a specified version number does not lock the file;
db8afaee
AS
562to do that, use this command a second time with no argument.
563
564If you bind this function to \\[toggle-read-only], then Emacs checks files
565in or out whenever you toggle the read-only flag."
c844616c 566 (interactive "P")
702220f3 567 (if (or (and (boundp 'vc-dired-mode) vc-dired-mode)
0e0d9831 568 ;; use boundp because vc.el might not be loaded
5232a436 569 (vc-backend buffer-file-name))
c844616c 570 (vc-next-action verbose)
594722a8
ER
571 (toggle-read-only)))
572
e896a9e1 573(defun vc-default-make-version-backups-p (backend file)
cb5af02a 574 "Return non-nil if unmodified versions should be backed up locally.
9aa10a43 575The default is to switch off this feature."
d445a975
AS
576 nil)
577
e896a9e1
AS
578(defun vc-version-backup-file-name (file &optional rev manual regexp)
579 "Return a backup file name for REV or the current version of FILE.
580If MANUAL is non-nil it means that a name for backups created by
581the user should be returned; if REGEXP is non-nil that means to return
582a regexp for matching all such backup files, regardless of the version."
e3f955b6
AS
583 (if regexp
584 (concat (regexp-quote (file-name-nondirectory file))
585 "\\.~[0-9.]+" (unless manual "\\.") "~")
f8b72742
JB
586 (expand-file-name (concat (file-name-nondirectory file)
587 ".~" (or rev (vc-workfile-version file))
e3f955b6
AS
588 (unless manual ".") "~")
589 (file-name-directory file))))
e896a9e1
AS
590
591(defun vc-delete-automatic-version-backups (file)
592 "Delete all existing automatic version backups for FILE."
d455f4f7
SM
593 (condition-case nil
594 (mapcar
595 'delete-file
79e954d0 596 (directory-files (or (file-name-directory file) default-directory) t
d455f4f7
SM
597 (vc-version-backup-file-name file nil nil t)))
598 ;; Don't fail when the directory doesn't exist.
599 (file-error nil)))
e896a9e1
AS
600
601(defun vc-make-version-backup (file)
602 "Make a backup copy of FILE, which is assumed in sync with the repository.
603Before doing that, check if there are any old backups and get rid of them."
48b15d3f 604 (unless (and (fboundp 'msdos-long-file-names)
6b61353c 605 (not (with-no-warnings (msdos-long-file-names))))
48b15d3f
AS
606 (vc-delete-automatic-version-backups file)
607 (copy-file file (vc-version-backup-file-name file)
608 nil 'keep-date)))
d445a975
AS
609
610(defun vc-before-save ()
611 "Function to be called by `basic-save-buffer' (in files.el)."
612 ;; If the file on disk is still in sync with the repository,
613 ;; and version backups should be made, copy the file to
614 ;; another name. This enables local diffs and local reverting.
5232a436 615 (let ((file buffer-file-name))
d445a975
AS
616 (and (vc-backend file)
617 (vc-up-to-date-p file)
618 (eq (vc-checkout-model file) 'implicit)
e896a9e1
AS
619 (vc-call make-version-backups-p file)
620 (vc-make-version-backup file))))
d445a975 621
e66eac08 622(defun vc-after-save ()
0e0d9831 623 "Function to be called by `basic-save-buffer' (in files.el)."
5eb2b516 624 ;; If the file in the current buffer is under version control,
0e0d9831
GM
625 ;; up-to-date, and locking is not used for the file, set
626 ;; the state to 'edited and redisplay the mode line.
5232a436 627 (let ((file buffer-file-name))
8aa81ea8 628 (and (vc-backend file)
b23a2306
AS
629 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
630 (nth 5 (file-attributes file)))
631 ;; File has been saved in the same second in which
632 ;; it was checked out. Clear the checkout-time
633 ;; to avoid confusion.
634 (vc-file-setprop file 'vc-checkout-time nil))
635 t)
0e0d9831
GM
636 (vc-up-to-date-p file)
637 (eq (vc-checkout-model file) 'implicit)
638 (vc-file-setprop file 'vc-state 'edited)
639 (vc-mode-line file)
099bd78a
SM
640 (if (featurep 'vc)
641 ;; If VC is not loaded, then there can't be
642 ;; any VC Dired buffer to synchronize.
643 (vc-dired-resynch-file file)))))
04446ed0 644
0e0d9831 645(defun vc-mode-line (file)
7bc2b98b 646 "Set `vc-mode' to display type of version control for FILE.
594722a8 647The value is set in the current buffer, which should be the buffer
0e0d9831 648visiting FILE."
67c6f446 649 (interactive (list buffer-file-name))
5232a436
SM
650 (let ((backend (vc-backend file)))
651 (if (not backend)
652 (setq vc-mode nil)
653 (setq vc-mode (concat " " (if vc-display-status
654 (vc-call mode-line-string file)
655 (symbol-name backend))))
656 ;; If the file is locked by some other user, make
657 ;; the buffer read-only. Like this, even root
658 ;; cannot modify a file that someone else has locked.
659 (and (equal file buffer-file-name)
660 (stringp (vc-state file))
661 (setq buffer-read-only t))
662 ;; If the user is root, and the file is not owner-writable,
663 ;; then pretend that we can't write it
664 ;; even though we can (because root can write anything).
2a3897f5
SM
665 ;; This way, even root cannot modify a file that isn't locked.
666 (and (equal file buffer-file-name)
667 (not buffer-read-only)
5232a436
SM
668 (zerop (user-real-uid))
669 (zerop (logand (file-modes buffer-file-name) 128))
670 (setq buffer-read-only t)))
671 (force-mode-line-update)
672 backend))
0e0d9831
GM
673
674(defun vc-default-mode-line-string (backend file)
675 "Return string for placement in modeline by `vc-mode-line' for FILE.
676Format:
677
678 \"BACKEND-REV\" if the file is up-to-date
679 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
680 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
0e0d9831
GM
681
682This function assumes that the file is registered."
683 (setq backend (symbol-name backend))
684 (let ((state (vc-state file))
685 (rev (vc-workfile-version file)))
099bd78a 686 (cond ((or (eq state 'up-to-date)
0e0d9831
GM
687 (eq state 'needs-patch))
688 (concat backend "-" rev))
689 ((stringp state)
690 (concat backend ":" state ":" rev))
691 (t
692 ;; Not just for the 'edited state, but also a fallback
693 ;; for all other states. Think about different symbols
694 ;; for 'needs-patch and 'needs-merge.
695 (concat backend ":" rev)))))
f2ee4191 696
a3a39848 697(defun vc-follow-link ()
0e0d9831
GM
698 "If current buffer visits a symbolic link, visit the real file.
699If the real file is already visited in another buffer, make that buffer
700current, and kill the buffer that visits the link."
566f2169 701 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
e7f5ddc2
RS
702 (true-buffer (find-buffer-visiting truename))
703 (this-buffer (current-buffer)))
704 (if (eq true-buffer this-buffer)
705 (progn
d8221951 706 (kill-buffer this-buffer)
e7f5ddc2
RS
707 ;; In principle, we could do something like set-visited-file-name.
708 ;; However, it can't be exactly the same as set-visited-file-name.
709 ;; I'm not going to work out the details right now. -- rms.
d8221951 710 (set-buffer (find-file-noselect truename)))
e7f5ddc2
RS
711 (set-buffer true-buffer)
712 (kill-buffer this-buffer))))
a3a39848 713
6b61353c
KH
714(defun vc-default-find-file-hook (backend)
715 nil)
716
594722a8 717(defun vc-find-file-hook ()
be4d6a6f 718 "Function for `find-file-hook' activating VC mode if appropriate."
18c8a18e
PE
719 ;; Recompute whether file is version controlled,
720 ;; if user has killed the buffer and revisited.
060a1456
AS
721 (if vc-mode
722 (setq vc-mode nil))
0e0d9831 723 (when buffer-file-name
f2ee4191
RS
724 (vc-file-clearprops buffer-file-name)
725 (cond
e1c0c2d1 726 ((vc-backend buffer-file-name)
0bfb74a9 727 ;; Compute the state and put it in the modeline.
f2ee4191 728 (vc-mode-line buffer-file-name)
0bfb74a9
SM
729 (unless vc-make-backup-files
730 ;; Use this variable, not make-backup-files,
731 ;; because this is for things that depend on the file name.
6b61353c
KH
732 (set (make-local-variable 'backup-inhibited) t))
733 ;; Let the backend setup any buffer-local things he needs.
734 (vc-call-backend (vc-backend buffer-file-name) 'find-file-hook))
45fc7cc3 735 ((let* ((link (file-symlink-p buffer-file-name))
227d2bed 736 (link-type (and link (vc-backend (file-chase-links link)))))
0bfb74a9
SM
737 (cond ((not link-type) nil) ;Nothing to do.
738 ((eq vc-follow-symlinks nil)
739 (message
b8063212 740 "Warning: symbolic link to %s-controlled source file" link-type))
0bfb74a9
SM
741 ((or (not (eq vc-follow-symlinks 'ask))
742 ;; If we already visited this file by following
743 ;; the link, don't ask again if we try to visit
744 ;; it again. GUD does that, and repeated questions
745 ;; are painful.
746 (get-file-buffer
747 (abbreviate-file-name
748 (file-chase-links buffer-file-name))))
749
750 (vc-follow-link)
751 (message "Followed link to %s" buffer-file-name)
752 (vc-find-file-hook))
753 (t
754 (if (yes-or-no-p (format
b8063212 755 "Symbolic link to %s-controlled source file; follow link? " link-type))
0bfb74a9
SM
756 (progn (vc-follow-link)
757 (message "Followed link to %s" buffer-file-name)
758 (vc-find-file-hook))
759 (message
b8063212 760 "Warning: editing through the link bypasses version control")
0bfb74a9 761 ))))))))
594722a8 762
be4d6a6f 763(add-hook 'find-file-hook 'vc-find-file-hook)
594722a8 764
14fef9de 765;; more hooks, this time for file-not-found
594722a8 766(defun vc-file-not-found-hook ()
0e0d9831
GM
767 "When file is not found, try to check it out from version control.
768Returns t if checkout was successful, nil otherwise.
d3d558a9 769Used in `find-file-not-found-functions'."
5f42a1d4
RS
770 ;; When a file does not exist, ignore cached info about it
771 ;; from a previous visit.
772 (vc-file-clearprops buffer-file-name)
6b61353c
KH
773 (let ((backend (vc-backend buffer-file-name)))
774 (if backend (vc-call-backend backend 'find-file-not-found-hook))))
775
776(defun vc-default-find-file-not-found-hook (backend)
777 (if (yes-or-no-p
778 (format "File %s was lost; check out from version control? "
779 (file-name-nondirectory buffer-file-name)))
780 (save-excursion
781 (require 'vc)
782 (setq default-directory (file-name-directory buffer-file-name))
783 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
594722a8 784
3756d481 785(add-hook 'find-file-not-found-functions 'vc-file-not-found-hook)
594722a8 786
f2ee4191 787(defun vc-kill-buffer-hook ()
0e0d9831 788 "Discard VC info about a file when we kill its buffer."
5232a436
SM
789 (if buffer-file-name
790 (vc-file-clearprops buffer-file-name)))
f2ee4191 791
cd32d5d1 792(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
f2ee4191 793
14fef9de
SM
794;; Now arrange for (autoloaded) bindings of the main package.
795;; Bindings for this have to go in the global map, as we'll often
796;; want to call them from random buffers.
797
798;; Autoloading works fine, but it prevents shortcuts from appearing
799;; in the menu because they don't exist yet when the menu is built.
800;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
801(defvar vc-prefix-map
802 (let ((map (make-sparse-keymap)))
803 (define-key map "a" 'vc-update-change-log)
804 (define-key map "b" 'vc-switch-backend)
805 (define-key map "c" 'vc-cancel-version)
806 (define-key map "d" 'vc-directory)
807 (define-key map "g" 'vc-annotate)
808 (define-key map "h" 'vc-insert-headers)
809 (define-key map "i" 'vc-register)
810 (define-key map "l" 'vc-print-log)
811 (define-key map "m" 'vc-merge)
812 (define-key map "r" 'vc-retrieve-snapshot)
813 (define-key map "s" 'vc-create-snapshot)
814 (define-key map "u" 'vc-revert-buffer)
815 (define-key map "v" 'vc-next-action)
816 (define-key map "=" 'vc-diff)
817 (define-key map "~" 'vc-version-other-window)
818 map))
819(fset 'vc-prefix-map vc-prefix-map)
0e0d9831 820(define-key global-map "\C-xv" 'vc-prefix-map)
624c0e9d 821
b662fbb8
RM
822(if (not (boundp 'vc-menu-map))
823 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
824 ;; vc-menu-map.
825 ()
826 ;;(define-key vc-menu-map [show-files]
827 ;; '("Show Files under VC" . (vc-directory t)))
9bef02e1
RS
828 (define-key vc-menu-map [vc-retrieve-snapshot]
829 '("Retrieve Snapshot" . vc-retrieve-snapshot))
830 (define-key vc-menu-map [vc-create-snapshot]
831 '("Create Snapshot" . vc-create-snapshot))
b64ea387 832 (define-key vc-menu-map [vc-directory] '("VC Directory Listing" . vc-directory))
b662fbb8 833 (define-key vc-menu-map [separator1] '("----"))
14aa11f4 834 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
b662fbb8
RM
835 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
836 (define-key vc-menu-map [vc-version-other-window]
837 '("Show Other Version" . vc-version-other-window))
c1cd70af 838 (define-key vc-menu-map [vc-diff] '("Compare with Base Version" . vc-diff))
b662fbb8
RM
839 (define-key vc-menu-map [vc-update-change-log]
840 '("Update ChangeLog" . vc-update-change-log))
841 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
842 (define-key vc-menu-map [separator2] '("----"))
b662fbb8
RM
843 (define-key vc-menu-map [vc-insert-header]
844 '("Insert Header" . vc-insert-headers))
c1cd70af
AS
845 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
846 (define-key vc-menu-map [vc-revert-buffer]
847 '("Revert to Base Version" . vc-revert-buffer))
848 (define-key vc-menu-map [vc-update]
849 '("Update to Latest Version" . vc-update))
9314395d 850 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
738efc8e
KH
851 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
852
14fef9de
SM
853;; These are not correct and it's not currently clear how doing it
854;; better (with more complicated expressions) might slow things down
855;; on older systems.
856
857;;(put 'vc-rename-file 'menu-enable 'vc-mode)
858;;(put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
859;;(put 'vc-version-other-window 'menu-enable 'vc-mode)
860;;(put 'vc-diff 'menu-enable 'vc-mode)
861;;(put 'vc-update-change-log 'menu-enable
862;; '(member (vc-buffer-backend) '(RCS CVS)))
863;;(put 'vc-print-log 'menu-enable 'vc-mode)
864;;(put 'vc-cancel-version 'menu-enable 'vc-mode)
865;;(put 'vc-revert-buffer 'menu-enable 'vc-mode)
866;;(put 'vc-insert-headers 'menu-enable 'vc-mode)
867;;(put 'vc-next-action 'menu-enable 'vc-mode)
868;;(put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
594722a8 869
594722a8
ER
870(provide 'vc-hooks)
871
6b61353c 872;;; arch-tag: 2e5a6fa7-1d30-48e2-8bd0-e3d335f04f32
594722a8 873;;; vc-hooks.el ends here