(vc-fetch-cvs-status): Don't specify DIR on the command line.
[bpt/emacs.git] / lisp / vc-hooks.el
CommitLineData
aae56ea7 1;;; vc-hooks.el --- resident support for version-control
594722a8 2
b499b50b 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998 Free Software Foundation, Inc.
594722a8 4
28a25aa5
AS
5;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6;; Maintainer: Andre Spiegel <spiegel@inf.fu-berlin.de>
594722a8 7
cd32a7ba 8;; $Id: vc-hooks.el,v 1.105 1998/04/05 18:44:35 spiegel Exp done $
f4c72097 9
594722a8
ER
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
594722a8
ER
26
27;;; Commentary:
28
f2ee4191
RS
29;; This is the always-loaded portion of VC.
30;; It takes care VC-related activities that are done when you visit a file,
31;; so that vc.el itself is loaded only when you use a VC command.
594722a8
ER
32;; See the commentary of vc.el.
33
34;;; Code:
35
e1c0c2d1
KH
36;; Customization Variables (the rest is in vc.el)
37
50bec091 38(defcustom vc-default-back-end nil
e1c0c2d1 39 "*Back-end actually used by this interface; may be SCCS or RCS.
50bec091
KH
40The value is only computed when needed to avoid an expensive search."
41 :type '(choice (const nil) (const RCS) (const SCCS))
42 :group 'vc)
e1c0c2d1 43
50bec091 44(defcustom vc-handle-cvs t
0b086efb 45 "*If non-nil, use VC for files managed with CVS.
50bec091
KH
46If it is nil, don't use VC for those files."
47 :type 'boolean
48 :group 'vc)
0b086efb 49
50bec091 50(defcustom vc-rcsdiff-knows-brief nil
31888047
AS
51 "*Indicates whether rcsdiff understands the --brief option.
52The value is either `yes', `no', or nil. If it is nil, VC tries
50bec091
KH
53to use --brief and sets this variable to remember whether it worked."
54 :type '(choice (const nil) (const yes) (const no))
55 :group 'vc)
31888047 56
50bec091 57(defcustom vc-path
e1c0c2d1
KH
58 (if (file-directory-p "/usr/sccs")
59 '("/usr/sccs")
60 nil)
50bec091
KH
61 "*List of extra directories to search for version control commands."
62 :type '(repeat directory)
63 :group 'vc)
e1c0c2d1 64
50bec091 65(defcustom vc-master-templates
594722a8 66 '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
174edc13 67 ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS)
809c22a2
AS
68 vc-find-cvs-master
69 vc-search-sccs-project-dir)
594722a8
ER
70 "*Where to look for version-control master files.
71The first pair corresponding to a given back end is used as a template
c20369f0 72when creating new masters.
50bec091
KH
73Setting this variable to nil turns off use of VC entirely."
74 :type '(repeat sexp)
75 :group 'vc)
594722a8 76
50bec091 77(defcustom vc-make-backup-files nil
5032bd23 78 "*If non-nil, backups of registered files are made as with other files.
50bec091
KH
79If nil (the default), files covered by version control don't get backups."
80 :type 'boolean
81 :group 'vc)
594722a8 82
50bec091 83(defcustom vc-follow-symlinks 'ask
b8063212
AS
84 "*Indicates what to do if you visit a symbolic link to a file
85that is under version control. Editing such a file through the
86link bypasses the version control system, which is dangerous and
87probably not what you want.
88 If this variable is t, VC follows the link and visits the real file,
89telling you about it in the echo area. If it is `ask', VC asks for
90confirmation whether it should follow the link. If nil, the link is
50bec091
KH
91visited and a warning displayed."
92 :type '(choice (const ask) (const nil) (const t))
93 :group 'vc)
b8063212 94
50bec091 95(defcustom vc-display-status t
624c0e9d 96 "*If non-nil, display revision number and lock status in modeline.
50bec091
KH
97Otherwise, not displayed."
98 :type 'boolean
99 :group 'vc)
100
198d5c00 101
50bec091
KH
102(defcustom vc-consult-headers t
103 "*If non-nil, identify work files by searching for version headers."
104 :type 'boolean
105 :group 'vc)
e1c0c2d1 106
50bec091 107(defcustom vc-keep-workfiles t
e1c0c2d1
KH
108 "*If non-nil, don't delete working files after registering changes.
109If the back-end is CVS, workfiles are always kept, regardless of the
50bec091
KH
110value of this flag."
111 :type 'boolean
112 :group 'vc)
e1c0c2d1 113
50bec091 114(defcustom vc-mistrust-permissions nil
95420811
AS
115 "*If non-nil, don't assume that permissions and ownership track
116version-control status. If nil, do rely on the permissions.
50bec091
KH
117See also variable `vc-consult-headers'."
118 :type 'boolean
119 :group 'vc)
e66eac08 120
b499b50b
DL
121(defcustom vc-ignore-vc-files nil
122 "*If non-nil don't look for version control information when finding files.
123
124It may be useful to set this if (say) you edit files in a directory
125containing corresponding RCS files but don't have RCS available;
126similarly for other version control systems."
127 :type 'boolean
cd32a7ba
DN
128 :group 'vc
129 :version "20.3")
b499b50b 130
e66eac08
AS
131(defun vc-mistrust-permissions (file)
132 ;; Access function to the above.
133 (or (eq vc-mistrust-permissions 't)
134 (and vc-mistrust-permissions
135 (funcall vc-mistrust-permissions
136 (vc-backend-subdirectory-name file)))))
137
594722a8 138;; Tell Emacs about this new kind of minor mode
7bc2b98b
ER
139(if (not (assoc 'vc-mode minor-mode-alist))
140 (setq minor-mode-alist (cons '(vc-mode vc-mode)
594722a8
ER
141 minor-mode-alist)))
142
7bc2b98b 143(make-variable-buffer-local 'vc-mode)
c43e436c 144(put 'vc-mode 'permanent-local t)
594722a8
ER
145
146;; We need a notion of per-file properties because the version
f2ee4191
RS
147;; control state of a file is expensive to derive --- we compute
148;; them when the file is initially found, keep them up to date
149;; during any subsequent VC operations, and forget them when
150;; the buffer is killed.
594722a8 151
80169ab5
ER
152(defmacro vc-error-occurred (&rest body)
153 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
154
594722a8
ER
155(defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
156 "Obarray for per-file properties.")
157
f2ee4191
RS
158(defvar vc-buffer-backend t)
159(make-variable-buffer-local 'vc-buffer-backend)
160
594722a8
ER
161(defun vc-file-setprop (file property value)
162 ;; set per-file property
163 (put (intern file vc-file-prop-obarray) property value))
164
165(defun vc-file-getprop (file property)
166 ;; get per-file property
167 (get (intern file vc-file-prop-obarray) property))
168
e1c0c2d1
KH
169(defun vc-file-clearprops (file)
170 ;; clear all properties of a given file
171 (setplist (intern file vc-file-prop-obarray) nil))
f2ee4191 172
02d383eb
RS
173;;; Functions that determine property values, by examining the
174;;; working file, the master file, or log program output
e1c0c2d1
KH
175
176(defun vc-match-substring (bn)
177 (buffer-substring (match-beginning bn) (match-end bn)))
178
179(defun vc-lock-file (file)
180 ;; Generate lock file name corresponding to FILE
181 (let ((master (vc-name file)))
182 (and
183 master
184 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
185 (concat
186 (substring master (match-beginning 1) (match-end 1))
187 "p."
188 (substring master (match-beginning 2) (match-end 2))))))
189
190(defun vc-parse-buffer (patterns &optional file properties)
191 ;; Use PATTERNS to parse information out of the current buffer.
192 ;; Each element of PATTERNS is a list of 2 to 3 elements. The first element
193 ;; is the pattern to be matched, and the second (an integer) is the
194 ;; number of the subexpression that should be returned. If there's
195 ;; a third element (also the number of a subexpression), that
196 ;; subexpression is assumed to be a date field and we want the most
197 ;; recent entry matching the template.
198 ;; If FILE and PROPERTIES are given, the latter must be a list of
199 ;; properties of the same length as PATTERNS; each property is assigned
200 ;; the corresponding value.
201 (mapcar (function (lambda (p)
202 (goto-char (point-min))
203 (cond
204 ((eq (length p) 2) ;; search for first entry
205 (let ((value nil))
206 (if (re-search-forward (car p) nil t)
207 (setq value (vc-match-substring (elt p 1))))
208 (if file
209 (progn (vc-file-setprop file (car properties) value)
210 (setq properties (cdr properties))))
211 value))
212 ((eq (length p) 3) ;; search for latest entry
213 (let ((latest-date "") (latest-val))
214 (while (re-search-forward (car p) nil t)
215 (let ((date (vc-match-substring (elt p 2))))
216 (if (string< latest-date date)
217 (progn
218 (setq latest-date date)
219 (setq latest-val
220 (vc-match-substring (elt p 1)))))))
221 (if file
222 (progn (vc-file-setprop file (car properties) latest-val)
223 (setq properties (cdr properties))))
224 latest-val)))))
225 patterns)
226 )
227
02d383eb
RS
228(defun vc-insert-file (file &optional limit blocksize)
229 ;; Insert the contents of FILE into the current buffer.
230 ;; Optional argument LIMIT is a regexp. If present,
231 ;; the file is inserted in chunks of size BLOCKSIZE
a7acbbe4 232 ;; (default 8 kByte), until the first occurrence of
02d383eb
RS
233 ;; LIMIT is found. The function returns nil if FILE
234 ;; doesn't exist.
07de4c3d 235 (erase-buffer)
02d383eb
RS
236 (cond ((file-exists-p file)
237 (cond (limit
238 (if (not blocksize) (setq blocksize 8192))
239 (let (found s)
240 (while (not found)
241 (setq s (buffer-size))
242 (goto-char (1+ s))
243 (setq found
244 (or (zerop (car (cdr
245 (insert-file-contents file nil s
246 (+ s blocksize)))))
247 (progn (beginning-of-line)
248 (re-search-forward limit nil t)))))))
249 (t (insert-file-contents file)))
250 (set-buffer-modified-p nil)
251 (auto-save-mode nil)
252 t)
253 (t nil)))
254
255(defun vc-parse-locks (file locks)
256 ;; Parse RCS or SCCS locks.
257 ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
258 ;; which is returned and stored into the property `vc-master-locks'.
259 (if (not locks)
260 (vc-file-setprop file 'vc-master-locks 'none)
261 (let ((found t) (index 0) master-locks version user)
262 (cond ((eq (vc-backend file) 'SCCS)
263 (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
264 locks index)
265 (setq version (substring locks
266 (match-beginning 1) (match-end 1)))
267 (setq user (substring locks
268 (match-beginning 2) (match-end 2)))
269 (setq master-locks (append master-locks
270 (list (cons version user))))
271 (setq index (match-end 0))))
272 ((eq (vc-backend file) 'RCS)
273 (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
274 locks index)
275 (setq version (substring locks
276 (match-beginning 2) (match-end 2)))
277 (setq user (substring locks
278 (match-beginning 1) (match-end 1)))
279 (setq master-locks (append master-locks
280 (list (cons version user))))
e66eac08
AS
281 (setq index (match-end 0)))
282 (if (string-match ";[ \t\n]+strict;" locks index)
283 (vc-file-setprop file 'vc-checkout-model 'manual)
284 (vc-file-setprop file 'vc-checkout-model 'implicit))))
02d383eb
RS
285 (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
286
7064821c
AS
287(defun vc-simple-command (okstatus command file &rest args)
288 ;; Simple version of vc-do-command, for use in vc-hooks only.
289 ;; Don't switch to the *vc-info* buffer before running the
290 ;; command, because that would change its default directory
291 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
292 (erase-buffer))
293 (let ((exec-path (append vc-path exec-path)) exec-status
294 ;; Add vc-path to PATH for the execution of this command.
295 (process-environment
296 (cons (concat "PATH=" (getenv "PATH")
297 path-separator
298 (mapconcat 'identity vc-path path-separator))
299 process-environment)))
300 (setq exec-status
301 (apply 'call-process command nil "*vc-info*" nil
302 (append args (list file))))
303 (cond ((> exec-status okstatus)
304 (switch-to-buffer (get-file-buffer file))
305 (shrink-window-if-larger-than-buffer
306 (display-buffer "*vc-info*"))
307 (error "Couldn't find version control information")))
308 exec-status))
309
8aa81ea8
RS
310(defun vc-parse-cvs-status (&optional file)
311 ;; Parse output of "cvs status" command in the current buffer and
312 ;; set file properties accordingly. If argument FILE is given, it
313 ;; must be the name of the file to which the status output applies,
314 ;; otherwise FILE is derived from the status output itself.
315 (or file
316 (progn (goto-char (point-min))
317 (re-search-forward "^File: \\([^ \t]+\\)" nil t)
318 (setq file (concat default-directory (match-string 1)))))
319 (vc-parse-buffer
320 ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
321 ;; and CVS 1.4a1 says "Repository revision:".
322 '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
323 ("^File: [^ \t]+[ \t]+Status: \\(.*\\)" 1))
324 file
325 '(vc-latest-version vc-cvs-status))
326 ;; Translate those status values that we understand into symbols.
327 ;; Any other value is converted to nil.
328 (let ((status (vc-file-getprop file 'vc-cvs-status)))
329 (cond
330 ((string-match "Up-to-date" status)
331 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
332 (vc-file-setprop file 'vc-checkout-time
333 (nth 5 (file-attributes file))))
334 ((vc-file-setprop file 'vc-cvs-status
335 (cond
336 ((string-match "Locally Modified" status) 'locally-modified)
337 ((string-match "Needs Merge" status) 'needs-merge)
338 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
339 'needs-checkout)
340 ((string-match "Unresolved Conflict" status) 'unresolved-conflict)
341 ((string-match "Locally Added" status) 'locally-added)
342 ((string-match "New file!" status) 'locally-added)
343 (t 'unknown)))))))
344
02d383eb
RS
345(defun vc-fetch-master-properties (file)
346 ;; Fetch those properties of FILE that are stored in the master file.
1efcbf46
RS
347 ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
348 ;; here because that is slow.
349 ;; That gets done if/when the functions vc-latest-version
350 ;; and vc-your-latest-version get called.
02d383eb
RS
351 (save-excursion
352 (cond
353 ((eq (vc-backend file) 'SCCS)
354 (set-buffer (get-buffer-create "*vc-info*"))
355 (if (vc-insert-file (vc-lock-file file))
07de4c3d 356 (vc-parse-locks file (buffer-string))
02d383eb
RS
357 (vc-file-setprop file 'vc-master-locks 'none))
358 (vc-insert-file (vc-name file) "^\001e")
359 (vc-parse-buffer
360 (list '("^\001d D \\([^ ]+\\)" 1)
361 (list (concat "^\001d D \\([^ ]+\\) .* "
2a11c6f3 362 (regexp-quote (vc-user-login-name)) " ") 1))
02d383eb
RS
363 file
364 '(vc-latest-version vc-your-latest-version)))
365
366 ((eq (vc-backend file) 'RCS)
367 (set-buffer (get-buffer-create "*vc-info*"))
e66eac08 368 (vc-insert-file (vc-name file) "^[0-9]")
02d383eb
RS
369 (vc-parse-buffer
370 (list '("^head[ \t\n]+\\([^;]+\\);" 1)
371 '("^branch[ \t\n]+\\([^;]+\\);" 1)
e66eac08 372 '("^locks[ \t\n]*\\([^;]*;\\([ \t\n]*strict;\\)?\\)" 1))
02d383eb
RS
373 file
374 '(vc-head-version
375 vc-default-branch
1efcbf46 376 vc-master-locks))
af5e65b9
AS
377 ;; determine vc-master-workfile-version: it is either the head
378 ;; of the trunk, the head of the default branch, or the
379 ;; "default branch" itself, if that is a full revision number.
02d383eb
RS
380 (let ((default-branch (vc-file-getprop file 'vc-default-branch)))
381 (cond
382 ;; no default branch
383 ((or (not default-branch) (string= "" default-branch))
af5e65b9 384 (vc-file-setprop file 'vc-master-workfile-version
02d383eb
RS
385 (vc-file-getprop file 'vc-head-version)))
386 ;; default branch is actually a revision
387 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
388 default-branch)
af5e65b9
AS
389 (vc-file-setprop file 'vc-master-workfile-version default-branch))
390 ;; else, search for the head of the default branch
07de4c3d 391 (t (vc-insert-file (vc-name file) "^desc")
1efcbf46 392 (vc-parse-buffer (list (list
02d383eb
RS
393 (concat "^\\("
394 (regexp-quote default-branch)
395 "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
af5e65b9 396 file '(vc-master-workfile-version)))))
02d383eb
RS
397 ;; translate the locks
398 (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
399
400 ((eq (vc-backend file) 'CVS)
7064821c 401 (save-excursion
719dcee9
AS
402 ;; Call "cvs status" in the right directory, passing only the
403 ;; nondirectory part of the file name -- otherwise CVS might
404 ;; silently give a wrong result.
405 (let ((default-directory (file-name-directory file)))
406 (vc-simple-command 0 "cvs" (file-name-nondirectory file) "status"))
7064821c 407 (set-buffer (get-buffer "*vc-info*"))
8aa81ea8 408 (vc-parse-cvs-status file))))
3be2a362
RS
409 (if (get-buffer "*vc-info*")
410 (kill-buffer (get-buffer "*vc-info*")))))
e1c0c2d1
KH
411
412;;; Functions that determine property values, by examining the
413;;; working file, the master file, or log program output
414
415(defun vc-consult-rcs-headers (file)
416 ;; Search for RCS headers in FILE, and set properties
417 ;; accordingly. This function can be disabled by setting
418 ;; vc-consult-headers to nil.
419 ;; Returns: nil if no headers were found
420 ;; (or if the feature is disabled,
421 ;; or if there is currently no buffer
422 ;; visiting FILE)
423 ;; 'rev if a workfile revision was found
424 ;; 'rev-and-lock if revision and lock info was found
e66eac08 425 (cond
e1c0c2d1 426 ((or (not vc-consult-headers)
02d383eb 427 (not (get-file-buffer file))) nil)
e66eac08
AS
428 ((let (status version locking-user)
429 (save-excursion
e1c0c2d1
KH
430 (set-buffer (get-file-buffer file))
431 (goto-char (point-min))
432 (cond
433 ;; search for $Id or $Header
434 ;; -------------------------
1efcbf46
RS
435 ((or (and (search-forward "$Id: " nil t)
436 (looking-at "[^ ]+ \\([0-9.]+\\) "))
437 (and (progn (goto-char (point-min))
fceee007 438 (search-forward "$Header: " nil t))
1efcbf46 439 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
04094290 440 (goto-char (match-end 0))
e1c0c2d1 441 ;; if found, store the revision number ...
55572259
KH
442 (setq version (buffer-substring-no-properties (match-beginning 1)
443 (match-end 1)))
e66eac08
AS
444 ;; ... and check for the locking state
445 (cond
446 ((looking-at
447 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
448 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
449 "[^ ]+ [^ ]+ ")) ; author & state
450 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
ce27f264 451 (cond
e66eac08
AS
452 ;; unlocked revision
453 ((looking-at "\\$")
454 (setq locking-user 'none)
455 (setq status 'rev-and-lock))
456 ;; revision is locked by some user
457 ((looking-at "\\([^ ]+\\) \\$")
458 (setq locking-user
55572259
KH
459 (buffer-substring-no-properties (match-beginning 1)
460 (match-end 1)))
e66eac08
AS
461 (setq status 'rev-and-lock))
462 ;; everything else: false
463 (nil)))
464 ;; unexpected information in
465 ;; keyword string --> quit
466 (nil)))
e1c0c2d1
KH
467 ;; search for $Revision
468 ;; --------------------
469 ((re-search-forward (concat "\\$"
470 "Revision: \\([0-9.]+\\) \\$")
471 nil t)
472 ;; if found, store the revision number ...
55572259
KH
473 (setq version (buffer-substring-no-properties (match-beginning 1)
474 (match-end 1)))
e66eac08
AS
475 ;; and see if there's any lock information
476 (goto-char (point-min))
477 (if (re-search-forward (concat "\\$" "Locker:") nil t)
478 (cond ((looking-at " \\([^ ]+\\) \\$")
55572259
KH
479 (setq locking-user (buffer-substring-no-properties
480 (match-beginning 1)
481 (match-end 1)))
e66eac08
AS
482 (setq status 'rev-and-lock))
483 ((looking-at " *\\$")
484 (setq locking-user 'none)
485 (setq status 'rev-and-lock))
486 (t
487 (setq locking-user 'none)
488 (setq status 'rev-and-lock)))
489 (setq status 'rev)))
e1c0c2d1
KH
490 ;; else: nothing found
491 ;; -------------------
e66eac08
AS
492 (t nil)))
493 (if status (vc-file-setprop file 'vc-workfile-version version))
494 (and (eq status 'rev-and-lock)
495 (eq (vc-backend file) 'RCS)
496 (vc-file-setprop file 'vc-locking-user locking-user)
497 ;; If the file has headers, we don't want to query the master file,
498 ;; because that would eliminate all the performance gain the headers
499 ;; brought us. We therefore use a heuristic for the checkout model
500 ;; now: If we trust the file permissions, and the file is not
501 ;; locked, then if the file is read-only the checkout model is
502 ;; `manual', otherwise `implicit'.
503 (not (vc-mistrust-permissions file))
504 (not (vc-locking-user file))
505 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
506 (vc-file-setprop file 'vc-checkout-model 'manual)
7064821c
AS
507 (vc-file-setprop file 'vc-checkout-model 'implicit)))
508 status))))
e1c0c2d1 509
02d383eb
RS
510;;; Access functions to file properties
511;;; (Properties should be _set_ using vc-file-setprop, but
512;;; _retrieved_ only through these functions, which decide
513;;; if the property is already known or not. A property should
514;;; only be retrieved by vc-file-getprop if there is no
515;;; access function.)
516
517;;; properties indicating the backend
518;;; being used for FILE
e1c0c2d1
KH
519
520(defun vc-backend-subdirectory-name (&optional file)
521 ;; Where the master and lock files for the current directory are kept
522 (symbol-name
523 (or
524 (and file (vc-backend file))
525 vc-default-back-end
526 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
527
02d383eb 528(defun vc-name (file)
b23a2306
AS
529 "Return the master name of a file, nil if it is not registered.
530For CVS, the full name of CVS/Entries is returned."
02d383eb 531 (or (vc-file-getprop file 'vc-name)
8aa81ea8
RS
532 ;; Use the caching mechanism of vc-backend, below.
533 (if (vc-backend file)
534 (vc-file-getprop file 'vc-name))))
e1c0c2d1 535
02d383eb
RS
536(defun vc-backend (file)
537 "Return the version-control type of a file, nil if it is not registered."
8aa81ea8
RS
538 ;; Note that internally, Emacs remembers unregistered
539 ;; files by setting the property to `none'.
540 (if file
541 (let ((property (vc-file-getprop file 'vc-backend))
542 (name-and-type))
543 (cond ((eq property 'none) nil)
544 (property)
545 (t (setq name-and-type (vc-registered file))
546 (if name-and-type
547 (progn
548 (vc-file-setprop file 'vc-name (car name-and-type))
549 (vc-file-setprop file 'vc-backend (cdr name-and-type)))
550 (vc-file-setprop file 'vc-backend 'none)
551 nil))))))
e1c0c2d1 552
04446ed0
AS
553(defun vc-checkout-model (file)
554 ;; Return `manual' if the user has to type C-x C-q to check out FILE.
e66eac08
AS
555 ;; Return `implicit' if the file can be modified without locking it first.
556 (or
557 (vc-file-getprop file 'vc-checkout-model)
558 (cond
559 ((eq (vc-backend file) 'SCCS)
560 (vc-file-setprop file 'vc-checkout-model 'manual))
561 ((eq (vc-backend file) 'RCS)
562 (vc-consult-rcs-headers file)
563 (or (vc-file-getprop file 'vc-checkout-model)
564 (progn (vc-fetch-master-properties file)
565 (vc-file-getprop file 'vc-checkout-model))))
566 ((eq (vc-backend file) 'CVS)
567 (vc-file-setprop file 'vc-checkout-model
403d549c
RS
568 (cond
569 ((getenv "CVSREAD") 'manual)
570 ;; If the file is not writeable, this is probably because the
571 ;; file is being "watched" by other developers. Use "manual"
572 ;; checkout in this case. (If vc-mistrust-permissions was t,
573 ;; we actually shouldn't trust this, but there is no other way
574 ;; to learn this from CVS at the moment (version 1.9).)
575 ((string-match "r-..-..-." (nth 8 (file-attributes file)))
576 'manual)
577 (t 'implicit)))))))
04446ed0 578
02d383eb 579;;; properties indicating the locking state
e1c0c2d1
KH
580
581(defun vc-cvs-status (file)
582 ;; Return the cvs status of FILE
583 ;; (Status field in output of "cvs status")
584 (cond ((vc-file-getprop file 'vc-cvs-status))
02d383eb 585 (t (vc-fetch-master-properties file)
e1c0c2d1
KH
586 (vc-file-getprop file 'vc-cvs-status))))
587
02d383eb
RS
588(defun vc-master-locks (file)
589 ;; Return the lock entries in the master of FILE.
590 ;; Return 'none if there are no such entries, and a list
591 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
592 (cond ((vc-file-getprop file 'vc-master-locks))
593 (t (vc-fetch-master-properties file)
594 (vc-file-getprop file 'vc-master-locks))))
595
596(defun vc-master-locking-user (file)
597 ;; Return the master file's idea of who is locking
598 ;; the current workfile version of FILE.
599 ;; Return 'none if it is not locked.
600 (let ((master-locks (vc-master-locks file)) lock)
601 (if (eq master-locks 'none) 'none
602 ;; search for a lock on the current workfile version
603 (setq lock (assoc (vc-workfile-version file) master-locks))
604 (cond (lock (cdr lock))
605 ('none)))))
606
7064821c
AS
607(defun vc-lock-from-permissions (file)
608 ;; If the permissions can be trusted for this file, determine the
609 ;; locking state from them. Returns (user-login-name), `none', or nil.
610 ;; This implementation assumes that any file which is under version
611 ;; control and has -rw-r--r-- is locked by its owner. This is true
612 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
613 ;; We have to be careful not to exclude files with execute bits on;
614 ;; scripts can be under version control too. Also, we must ignore the
615 ;; group-read and other-read bits, since paranoid users turn them off.
616 ;; This hack wins because calls to the somewhat expensive
617 ;; `vc-fetch-master-properties' function only have to be made if
618 ;; (a) the file is locked by someone other than the current user,
619 ;; or (b) some untoward manipulation behind vc's back has changed
620 ;; the owner or the `group' or `other' write bits.
621 (let ((attributes (file-attributes file)))
622 (if (not (vc-mistrust-permissions file))
623 (cond ((string-match ".r-..-..-." (nth 8 attributes))
624 (vc-file-setprop file 'vc-locking-user 'none))
625 ((and (= (nth 2 attributes) (user-uid))
626 (string-match ".rw..-..-." (nth 8 attributes)))
2a11c6f3 627 (vc-file-setprop file 'vc-locking-user (vc-user-login-name)))
7064821c
AS
628 (nil)))))
629
2a11c6f3
AS
630(defun vc-user-login-name (&optional uid)
631 ;; Return the name under which the user is logged in, as a string.
632 ;; (With optional argument UID, return the name of that user.)
633 ;; This function does the same as `user-login-name', but unlike
634 ;; that, it never returns nil. If a UID cannot be resolved, that
635 ;; UID is returned as a string.
636 (or (user-login-name uid)
637 (and uid (number-to-string uid))
638 (number-to-string (user-uid))))
639
7064821c 640(defun vc-file-owner (file)
1525b516 641 ;; Return who owns FILE (user name, as a string).
2a11c6f3 642 (vc-user-login-name (nth 2 (file-attributes file))))
7064821c
AS
643
644(defun vc-rcs-lock-from-diff (file)
645 ;; Diff the file against the master version. If differences are found,
31888047
AS
646 ;; mark the file locked. This is only used for RCS with non-strict
647 ;; locking. (If "rcsdiff" doesn't understand --brief, we do a double-take
648 ;; and remember the fact for the future.)
649 (let* ((version (concat "-r" (vc-workfile-version file)))
650 (status (if (eq vc-rcsdiff-knows-brief 'no)
651 (vc-simple-command 1 "rcsdiff" file version)
652 (vc-simple-command 2 "rcsdiff" file "--brief" version))))
653 (if (eq status 2)
654 (if (not vc-rcsdiff-knows-brief)
655 (setq vc-rcsdiff-knows-brief 'no
656 status (vc-simple-command 1 "rcsdiff" file version))
657 (error "rcsdiff failed."))
658 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
659 (if (zerop status)
660 (vc-file-setprop file 'vc-locking-user 'none)
661 (vc-file-setprop file 'vc-locking-user (vc-file-owner file)))))
7064821c 662
e1c0c2d1 663(defun vc-locking-user (file)
02d383eb 664 ;; Return the name of the person currently holding a lock on FILE.
1525b516 665 ;; Return nil if there is no such person.
02d383eb 666 ;; Under CVS, a file is considered locked if it has been modified since
7064821c 667 ;; it was checked out.
02d383eb
RS
668 ;; The property is cached. It is only looked up if it is currently nil.
669 ;; Note that, for a file that is not locked, the actual property value
7064821c 670 ;; is `none', to distinguish it from an unknown locking state. That value
02d383eb
RS
671 ;; is converted to nil by this function, and returned to the caller.
672 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
673 (if locking-user
674 ;; if we already know the property, return it
675 (if (eq locking-user 'none) nil locking-user)
676
677 ;; otherwise, infer the property...
678 (cond
02d383eb 679 ((eq (vc-backend file) 'CVS)
7064821c
AS
680 (or (and (eq (vc-checkout-model file) 'manual)
681 (vc-lock-from-permissions file))
b23a2306
AS
682 (and (equal (vc-file-getprop file 'vc-checkout-time)
683 (nth 5 (file-attributes file)))
684 (vc-file-setprop file 'vc-locking-user 'none))
1525b516 685 (vc-file-setprop file 'vc-locking-user (vc-file-owner file))))
7064821c
AS
686
687 ((eq (vc-backend file) 'RCS)
688 (let (p-lock)
689
690 ;; Check for RCS headers first
691 (or (eq (vc-consult-rcs-headers file) 'rev-and-lock)
692
693 ;; If there are no headers, try to learn it
694 ;; from the permissions.
695 (and (setq p-lock (vc-lock-from-permissions file))
696 (if (eq p-lock 'none)
697
698 ;; If the permissions say "not locked", we know
699 ;; that the checkout model must be `manual'.
700 (vc-file-setprop file 'vc-checkout-model 'manual)
701
702 ;; If the permissions say "locked", we can only trust
703 ;; this *if* the checkout model is `manual'.
704 (eq (vc-checkout-model file) 'manual)))
705
706 ;; Otherwise, use lock information from the master file.
707 (vc-file-setprop file 'vc-locking-user
708 (vc-master-locking-user file)))
709
710 ;; Finally, if the file is not explicitly locked
711 ;; it might still be locked implicitly.
712 (and (eq (vc-file-getprop file 'vc-locking-user) 'none)
713 (eq (vc-checkout-model file) 'implicit)
714 (vc-rcs-lock-from-diff file))))
715
716 ((eq (vc-backend file) 'SCCS)
717 (or (vc-lock-from-permissions file)
718 (vc-file-setprop file 'vc-locking-user
aadce164 719 (vc-master-locking-user file)))))
7064821c
AS
720
721 ;; convert a possible 'none value
722 (setq locking-user (vc-file-getprop file 'vc-locking-user))
aadce164 723 (if (eq locking-user 'none) nil locking-user))))
02d383eb
RS
724
725;;; properties to store current and recent version numbers
e1c0c2d1
KH
726
727(defun vc-latest-version (file)
728 ;; Return version level of the latest version of FILE
02d383eb 729 (cond ((vc-file-getprop file 'vc-latest-version))
1efcbf46 730 (t (vc-fetch-properties file)
02d383eb 731 (vc-file-getprop file 'vc-latest-version))))
e1c0c2d1
KH
732
733(defun vc-your-latest-version (file)
734 ;; Return version level of the latest version of FILE checked in by you
02d383eb 735 (cond ((vc-file-getprop file 'vc-your-latest-version))
1efcbf46 736 (t (vc-fetch-properties file)
02d383eb 737 (vc-file-getprop file 'vc-your-latest-version))))
e1c0c2d1 738
af5e65b9
AS
739(defun vc-master-workfile-version (file)
740 ;; Return the master file's idea of what is the current workfile version.
e1c0c2d1 741 ;; This property is defined for RCS only.
af5e65b9 742 (cond ((vc-file-getprop file 'vc-master-workfile-version))
02d383eb 743 (t (vc-fetch-master-properties file)
af5e65b9 744 (vc-file-getprop file 'vc-master-workfile-version))))
e1c0c2d1 745
1efcbf46
RS
746(defun vc-fetch-properties (file)
747 ;; Fetch vc-latest-version and vc-your-latest-version
748 ;; if that wasn't already done.
04094290
RS
749 (cond
750 ((eq (vc-backend file) 'RCS)
ee526b55
RS
751 (save-excursion
752 (set-buffer (get-buffer-create "*vc-info*"))
753 (vc-insert-file (vc-name file) "^desc")
754 (vc-parse-buffer
755 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
756 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
757 "date[ \t]+\\([0-9.]+\\);[ \t]+"
758 "author[ \t]+"
2a11c6f3 759 (regexp-quote (vc-user-login-name)) ";") 1 2))
ee526b55 760 file
07de4c3d
RS
761 '(vc-latest-version vc-your-latest-version))
762 (if (get-buffer "*vc-info*")
763 (kill-buffer (get-buffer "*vc-info*")))))
04094290 764 (t (vc-fetch-master-properties file))
1efcbf46
RS
765 ))
766
e1c0c2d1
KH
767(defun vc-workfile-version (file)
768 ;; Return version level of the current workfile FILE
769 ;; This is attempted by first looking at the RCS keywords.
770 ;; If there are no keywords in the working file,
af5e65b9 771 ;; vc-master-workfile-version is taken.
e1c0c2d1
KH
772 ;; Note that this property is cached, that is, it is only
773 ;; looked up if it is nil.
774 ;; For SCCS, this property is equivalent to vc-latest-version.
775 (cond ((vc-file-getprop file 'vc-workfile-version))
776 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
777 ((eq (vc-backend file) 'RCS)
778 (if (vc-consult-rcs-headers file)
779 (vc-file-getprop file 'vc-workfile-version)
af5e65b9 780 (let ((rev (cond ((vc-master-workfile-version file))
e1c0c2d1
KH
781 ((vc-latest-version file)))))
782 (vc-file-setprop file 'vc-workfile-version rev)
783 rev)))
784 ((eq (vc-backend file) 'CVS)
785 (if (vc-consult-rcs-headers file) ;; CVS
786 (vc-file-getprop file 'vc-workfile-version)
7b0e1b8f
KH
787 (catch 'found
788 (vc-find-cvs-master (file-name-directory file)
789 (file-name-nondirectory file)))
e1c0c2d1 790 (vc-file-getprop file 'vc-workfile-version)))))
f2ee4191 791
594722a8
ER
792;;; actual version-control code starts here
793
794(defun vc-registered (file)
18c8a18e
PE
795 (let (handler handlers)
796 (if (boundp 'file-name-handler-alist)
b993101e 797 (setq handler (find-file-name-handler file 'vc-registered)))
18c8a18e
PE
798 (if handler
799 (funcall handler 'vc-registered file)
800 ;; Search for a master corresponding to the given file
801 (let ((dirname (or (file-name-directory file) ""))
802 (basename (file-name-nondirectory file)))
803 (catch 'found
804 (mapcar
805 (function (lambda (s)
174edc13
RS
806 (if (atom s)
807 (funcall s dirname basename)
808 (let ((trial (format (car s) dirname basename)))
809 (if (and (file-exists-p trial)
810 ;; Make sure the file we found with name
811 ;; TRIAL is not the source file itself.
812 ;; That can happen with RCS-style names
813 ;; if the file name is truncated
814 ;; (e.g. to 14 chars). See if either
815 ;; directory or attributes differ.
816 (or (not (string= dirname
817 (file-name-directory trial)))
818 (not (equal
819 (file-attributes file)
820 (file-attributes trial)))))
821 (throw 'found (cons trial (cdr s))))))))
18c8a18e
PE
822 vc-master-templates)
823 nil)))))
594722a8 824
809c22a2
AS
825(defun vc-sccs-project-dir ()
826 ;; Return the full pathname of the SCCS PROJECTDIR, if it exists,
827 ;; otherwise nil. The PROJECTDIR is indicated by the environment
828 ;; variable of the same name. If its value starts with a slash,
829 ;; it must be an absolute path name that points to the
830 ;; directory where SCCS history files reside. If it does not
831 ;; begin with a slash, it is taken as the name of a user,
832 ;; and history files reside in an "src" or "source" subdirectory
833 ;; of that user's home directory.
834 (let ((project-dir (getenv "PROJECTDIR")))
835 (and project-dir
836 (if (eq (elt project-dir 0) ?/)
837 (if (file-exists-p (concat project-dir "/SCCS"))
838 (concat project-dir "/SCCS/")
839 (if (file-exists-p project-dir)
840 project-dir))
841 (setq project-dir (expand-file-name (concat "~" project-dir)))
842 (let (trial)
843 (setq trial (concat project-dir "/src/SCCS"))
844 (if (file-exists-p trial)
845 (concat trial "/")
846 (setq trial (concat project-dir "/src"))
847 (if (file-exists-p trial)
848 (concat trial "/")
849 (setq trial (concat project-dir "/source/SCCS"))
850 (if (file-exists-p trial)
851 (concat trial "/")
852 (setq trial (concat project-dir "/source/"))
853 (if (file-exists-p trial)
854 (concat trial "/"))))))))))
855
856(defun vc-search-sccs-project-dir (dirname basename)
857 ;; Check if there is a master file for BASENAME in the
858 ;; SCCS project directory. If yes, throw `found' as
859 ;; expected by vc-registered. If not, return nil.
860 (let* ((project-dir (vc-sccs-project-dir))
861 (master-file (and project-dir (concat project-dir "s." basename))))
862 (and master-file
863 (file-exists-p master-file)
864 (throw 'found (cons master-file 'SCCS)))))
865
174edc13
RS
866(defun vc-find-cvs-master (dirname basename)
867 ;; Check if DIRNAME/BASENAME is handled by CVS.
809c22a2 868 ;; If it is, do a (throw 'found (cons MASTER-FILE 'CVS)).
b23a2306
AS
869 ;; Note: This function throws the name of CVS/Entries
870 ;; NOT that of the RCS master file (because we wouldn't be able
871 ;; to access it under remote CVS).
872 ;; The function returns nil if DIRNAME/BASENAME is not handled by CVS.
0b086efb
RS
873 (if (and vc-handle-cvs
874 (file-directory-p (concat dirname "CVS/"))
b23a2306 875 (file-readable-p (concat dirname "CVS/Entries")))
702220f3
AS
876 (let ((file (concat dirname basename))
877 ;; make sure that the file name is searched
878 ;; case-sensitively
879 (case-fold-search nil)
880 buffer)
174edc13
RS
881 (unwind-protect
882 (save-excursion
7064821c
AS
883 (setq buffer (set-buffer (get-buffer-create "*vc-info*")))
884 (vc-insert-file (concat dirname "CVS/Entries"))
174edc13
RS
885 (goto-char (point-min))
886 (cond
702220f3
AS
887 ;; entry for a "locally added" file (not yet committed)
888 ((re-search-forward
889 (concat "^/" (regexp-quote basename) "/0/") nil t)
890 (vc-file-setprop file 'vc-checkout-time 0)
891 (vc-file-setprop file 'vc-workfile-version "0")
892 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
a93de381 893 ;; normal entry
174edc13 894 ((re-search-forward
b23a2306 895 (concat "^/" (regexp-quote basename)
702220f3
AS
896 ;; revision
897 "/\\([^/]*\\)"
898 ;; timestamp
899 "/[A-Z][a-z][a-z]" ;; week day (irrelevant)
900 " \\([A-Z][a-z][a-z]\\)" ;; month name
901 " *\\([0-9]*\\)" ;; day of month
902 " \\([0-9]*\\):\\([0-9]*\\):\\([0-9]*\\)" ;; hms
903 " \\([0-9]*\\)" ;; year
904 ;; optional conflict field
905 "\\(+[^/]*\\)?/")
174edc13 906 nil t)
b23a2306
AS
907 ;; We found it. Store away version number now that we
908 ;; are anyhow so close to finding it.
909 (vc-file-setprop file
f2ee4191 910 'vc-workfile-version
b23a2306
AS
911 (match-string 1))
912 ;; If the file hasn't been modified since checkout,
913 ;; store the checkout-time.
c18381d1 914 (let ((mtime (nth 5 (file-attributes file)))
702220f3
AS
915 (second (string-to-number (match-string 6)))
916 (minute (string-to-number (match-string 5)))
917 (hour (string-to-number (match-string 4)))
918 (day (string-to-number (match-string 3)))
919 (year (string-to-number (match-string 7))))
c18381d1
PE
920 (if (equal mtime
921 (encode-time
922 second minute hour day
923 (/ (string-match
702220f3 924 (match-string 2)
c18381d1
PE
925 "xxxJanFebMarAprMayJunJulAugSepOctNovDec")
926 3)
927 year 0))
719dcee9
AS
928 (vc-file-setprop file 'vc-checkout-time mtime)
929 (vc-file-setprop file 'vc-checkout-time 0)))
b23a2306 930 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
702220f3
AS
931 ;; entry with arbitrary text as timestamp
932 ;; (this means we should consider it modified)
a93de381 933 ((re-search-forward
702220f3
AS
934 (concat "^/" (regexp-quote basename)
935 ;; revision
936 "/\\([^/]*\\)"
937 ;; timestamp (arbitrary text)
938 "/[^/]*"
939 ;; optional conflict field
940 "\\(+[^/]*\\)?/")
941 nil t)
942 ;; We found it. Store away version number now that we
943 ;; are anyhow so close to finding it.
944 (vc-file-setprop file 'vc-workfile-version (match-string 1))
274c59c9
KH
945 (vc-file-setprop file 'vc-checkout-time 0)
946 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
702220f3 947 (t nil)))
7064821c 948 (kill-buffer buffer)))))
174edc13 949
f2ee4191
RS
950(defun vc-buffer-backend ()
951 "Return the version-control type of the visited file, or nil if none."
952 (if (eq vc-buffer-backend t)
e1c0c2d1 953 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
f2ee4191
RS
954 vc-buffer-backend))
955
c844616c 956(defun vc-toggle-read-only (&optional verbose)
c43e436c
RS
957 "Change read-only status of current buffer, perhaps via version control.
958If the buffer is visiting a file registered with version control,
959then check the file in or out. Otherwise, just change the read-only flag
f2ee4191 960of the buffer. With prefix argument, ask for version number."
c844616c 961 (interactive "P")
702220f3
AS
962 (if (or (and (boundp 'vc-dired-mode) vc-dired-mode)
963 ;; use boundp because vc.el might not be loaded
964 (vc-backend (buffer-file-name)))
c844616c 965 (vc-next-action verbose)
594722a8 966 (toggle-read-only)))
c43e436c 967(define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
594722a8 968
e66eac08
AS
969(defun vc-after-save ()
970 ;; Function to be called by basic-save-buffer (in files.el).
971 ;; If the file in the current buffer is under version control,
972 ;; not locked, and the checkout model for it is `implicit',
973 ;; mark it "locked" and redisplay the mode line.
974 (let ((file (buffer-file-name)))
8aa81ea8 975 (and (vc-backend file)
b23a2306
AS
976 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
977 (nth 5 (file-attributes file)))
978 ;; File has been saved in the same second in which
979 ;; it was checked out. Clear the checkout-time
980 ;; to avoid confusion.
981 (vc-file-setprop file 'vc-checkout-time nil))
982 t)
e66eac08
AS
983 (not (vc-locking-user file))
984 (eq (vc-checkout-model file) 'implicit)
2a11c6f3 985 (vc-file-setprop file 'vc-locking-user (vc-user-login-name))
b23a2306
AS
986 (or (and (eq (vc-backend file) 'CVS)
987 (vc-file-setprop file 'vc-cvs-status nil))
988 t)
989 (vc-mode-line file))))
04446ed0 990
594722a8 991(defun vc-mode-line (file &optional label)
7bc2b98b 992 "Set `vc-mode' to display type of version control for FILE.
594722a8 993The value is set in the current buffer, which should be the buffer
624c0e9d
RS
994visiting FILE. Second optional arg LABEL is put in place of version
995control system name."
18c8a18e 996 (interactive (list buffer-file-name nil))
7b0e1b8f 997 (let ((vc-type (vc-backend file)))
f2ee4191 998 (setq vc-mode
7b0e1b8f
KH
999 (and vc-type
1000 (concat " " (or label (symbol-name vc-type))
1001 (and vc-display-status (vc-status file)))))
9becbeca
RS
1002 ;; If the file is locked by some other user, make
1003 ;; the buffer read-only. Like this, even root
defccde3 1004 ;; cannot modify a file that someone else has locked.
7b0e1b8f
KH
1005 (and vc-type
1006 (equal file (buffer-file-name))
e66eac08 1007 (vc-locking-user file)
2a11c6f3 1008 (not (string= (vc-user-login-name) (vc-locking-user file)))
e66eac08 1009 (setq buffer-read-only t))
defccde3
RS
1010 ;; If the user is root, and the file is not owner-writable,
1011 ;; then pretend that we can't write it
1012 ;; even though we can (because root can write anything).
1013 ;; This way, even root cannot modify a file that isn't locked.
9becbeca
RS
1014 (and vc-type
1015 (equal file (buffer-file-name))
1016 (not buffer-read-only)
1017 (zerop (user-real-uid))
1018 (zerop (logand (file-modes (buffer-file-name)) 128))
1019 (setq buffer-read-only t))
f2ee4191
RS
1020 (force-mode-line-update)
1021 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
1022 vc-type))
594722a8 1023
e1c0c2d1 1024(defun vc-status (file)
45c92c0c 1025 ;; Return string for placement in modeline by `vc-mode-line'.
e1c0c2d1 1026 ;; Format:
624c0e9d 1027 ;;
e1c0c2d1
KH
1028 ;; "-REV" if the revision is not locked
1029 ;; ":REV" if the revision is locked by the user
1030 ;; ":LOCKER:REV" if the revision is locked by somebody else
1031 ;; " @@" for a CVS file that is added, but not yet committed
174edc13 1032 ;;
e1c0c2d1
KH
1033 ;; In the CVS case, a "locked" working file is a
1034 ;; working file that is modified with respect to the master.
e66eac08
AS
1035 ;; The file is "locked" from the moment when the user saves
1036 ;; the modified buffer.
e1c0c2d1
KH
1037 ;;
1038 ;; This function assumes that the file is registered.
1039
1040 (let ((locker (vc-locking-user file))
1041 (rev (vc-workfile-version file)))
1042 (cond ((string= "0" rev)
1043 " @@")
38179d42
RM
1044 ((not locker)
1045 (concat "-" rev))
2a11c6f3 1046 ((string= locker (vc-user-login-name))
e1c0c2d1
KH
1047 (concat ":" rev))
1048 (t
1049 (concat ":" locker ":" rev)))))
f2ee4191 1050
a3a39848
AS
1051(defun vc-follow-link ()
1052 ;; If the current buffer visits a symbolic link, this function makes it
1053 ;; visit the real file instead. If the real file is already visited in
1054 ;; another buffer, make that buffer current, and kill the buffer
1055 ;; that visits the link.
566f2169 1056 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
e7f5ddc2
RS
1057 (true-buffer (find-buffer-visiting truename))
1058 (this-buffer (current-buffer)))
1059 (if (eq true-buffer this-buffer)
1060 (progn
d8221951 1061 (kill-buffer this-buffer)
e7f5ddc2
RS
1062 ;; In principle, we could do something like set-visited-file-name.
1063 ;; However, it can't be exactly the same as set-visited-file-name.
1064 ;; I'm not going to work out the details right now. -- rms.
d8221951 1065 (set-buffer (find-file-noselect truename)))
e7f5ddc2
RS
1066 (set-buffer true-buffer)
1067 (kill-buffer this-buffer))))
a3a39848 1068
594722a8
ER
1069;;; install a call to the above as a find-file hook
1070(defun vc-find-file-hook ()
18c8a18e
PE
1071 ;; Recompute whether file is version controlled,
1072 ;; if user has killed the buffer and revisited.
f2ee4191 1073 (cond
b499b50b 1074 ((and (not vc-ignore-vc-files) buffer-file-name)
f2ee4191
RS
1075 (vc-file-clearprops buffer-file-name)
1076 (cond
e1c0c2d1 1077 ((vc-backend buffer-file-name)
f2ee4191
RS
1078 (vc-mode-line buffer-file-name)
1079 (cond ((not vc-make-backup-files)
1080 ;; Use this variable, not make-backup-files,
1081 ;; because this is for things that depend on the file name.
1082 (make-local-variable 'backup-inhibited)
45fc7cc3
RS
1083 (setq backup-inhibited t))))
1084 ((let* ((link (file-symlink-p buffer-file-name))
227d2bed 1085 (link-type (and link (vc-backend (file-chase-links link)))))
45fc7cc3 1086 (if link-type
b8063212
AS
1087 (cond ((eq vc-follow-symlinks nil)
1088 (message
1089 "Warning: symbolic link to %s-controlled source file" link-type))
566f2169
RS
1090 ((or (not (eq vc-follow-symlinks 'ask))
1091 ;; If we already visited this file by following
1092 ;; the link, don't ask again if we try to visit
1093 ;; it again. GUD does that, and repeated questions
1094 ;; are painful.
1095 (get-file-buffer
1096 (abbreviate-file-name (file-chase-links buffer-file-name))))
1097
1098 (vc-follow-link)
1099 (message "Followed link to %s" buffer-file-name)
1100 (vc-find-file-hook))
1101 (t
b8063212
AS
1102 (if (yes-or-no-p (format
1103 "Symbolic link to %s-controlled source file; follow link? " link-type))
a3a39848 1104 (progn (vc-follow-link)
b8063212
AS
1105 (message "Followed link to %s" buffer-file-name)
1106 (vc-find-file-hook))
1107 (message
1108 "Warning: editing through the link bypasses version control")
566f2169 1109 ))))))))))
594722a8 1110
6379911c 1111(add-hook 'find-file-hooks 'vc-find-file-hook)
594722a8
ER
1112
1113;;; more hooks, this time for file-not-found
1114(defun vc-file-not-found-hook ()
1115 "When file is not found, try to check it out from RCS or SCCS.
1116Returns t if checkout was successful, nil otherwise."
b499b50b
DL
1117 (if (and (not vc-ignore-vc-files)
1118 (vc-backend buffer-file-name))
624c0e9d 1119 (save-excursion
594722a8 1120 (require 'vc)
02d383eb 1121 (setq default-directory (file-name-directory (buffer-file-name)))
594722a8
ER
1122 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
1123
6379911c 1124(add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
594722a8 1125
f2ee4191
RS
1126;; Discard info about a file when we kill its buffer.
1127(defun vc-kill-buffer-hook ()
1128 (if (stringp (buffer-file-name))
1129 (progn
1130 (vc-file-clearprops (buffer-file-name))
1131 (kill-local-variable 'vc-buffer-backend))))
1132
1133;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
1134
594722a8 1135;;; Now arrange for bindings and autoloading of the main package.
7bc2b98b
ER
1136;;; Bindings for this have to go in the global map, as we'll often
1137;;; want to call them from random buffers.
594722a8
ER
1138
1139(setq vc-prefix-map (lookup-key global-map "\C-xv"))
1140(if (not (keymapp vc-prefix-map))
1141 (progn
1142 (setq vc-prefix-map (make-sparse-keymap))
1143 (define-key global-map "\C-xv" vc-prefix-map)
1144 (define-key vc-prefix-map "a" 'vc-update-change-log)
1145 (define-key vc-prefix-map "c" 'vc-cancel-version)
18c8a18e 1146 (define-key vc-prefix-map "d" 'vc-directory)
14aa11f4 1147 (define-key vc-prefix-map "g" 'vc-annotate)
594722a8
ER
1148 (define-key vc-prefix-map "h" 'vc-insert-headers)
1149 (define-key vc-prefix-map "i" 'vc-register)
1150 (define-key vc-prefix-map "l" 'vc-print-log)
e27827dd 1151 (define-key vc-prefix-map "m" 'vc-merge)
594722a8
ER
1152 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
1153 (define-key vc-prefix-map "s" 'vc-create-snapshot)
1154 (define-key vc-prefix-map "u" 'vc-revert-buffer)
1155 (define-key vc-prefix-map "v" 'vc-next-action)
18c8a18e 1156 (define-key vc-prefix-map "=" 'vc-diff)
624c0e9d
RS
1157 (define-key vc-prefix-map "~" 'vc-version-other-window)))
1158
b662fbb8
RM
1159(if (not (boundp 'vc-menu-map))
1160 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
1161 ;; vc-menu-map.
1162 ()
1163 ;;(define-key vc-menu-map [show-files]
1164 ;; '("Show Files under VC" . (vc-directory t)))
9bef02e1
RS
1165 (define-key vc-menu-map [vc-retrieve-snapshot]
1166 '("Retrieve Snapshot" . vc-retrieve-snapshot))
1167 (define-key vc-menu-map [vc-create-snapshot]
1168 '("Create Snapshot" . vc-create-snapshot))
b662fbb8
RM
1169 (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
1170 (define-key vc-menu-map [separator1] '("----"))
14aa11f4 1171 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
b662fbb8
RM
1172 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
1173 (define-key vc-menu-map [vc-version-other-window]
1174 '("Show Other Version" . vc-version-other-window))
1175 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
1176 (define-key vc-menu-map [vc-update-change-log]
1177 '("Update ChangeLog" . vc-update-change-log))
1178 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
1179 (define-key vc-menu-map [separator2] '("----"))
1180 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
1181 (define-key vc-menu-map [vc-revert-buffer]
1182 '("Revert to Last Version" . vc-revert-buffer))
1183 (define-key vc-menu-map [vc-insert-header]
1184 '("Insert Header" . vc-insert-headers))
9314395d 1185 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
738efc8e
KH
1186 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
1187
1188(put 'vc-rename-file 'menu-enable 'vc-mode)
14aa11f4 1189(put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
738efc8e
KH
1190(put 'vc-version-other-window 'menu-enable 'vc-mode)
1191(put 'vc-diff 'menu-enable 'vc-mode)
1192(put 'vc-update-change-log 'menu-enable
1193 '(eq (vc-buffer-backend) 'RCS))
1194(put 'vc-print-log 'menu-enable 'vc-mode)
1195(put 'vc-cancel-version 'menu-enable 'vc-mode)
1196(put 'vc-revert-buffer 'menu-enable 'vc-mode)
1197(put 'vc-insert-headers 'menu-enable 'vc-mode)
1198(put 'vc-next-action 'menu-enable 'vc-mode)
738efc8e 1199(put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
594722a8 1200
594722a8
ER
1201(provide 'vc-hooks)
1202
1203;;; vc-hooks.el ends here