*** empty log message ***
[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
8321b22a 8;; $Id: vc-hooks.el,v 1.1 2000/01/10 13:25:12 gerd Exp gerd $
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
8321b22a 197 ;; recent entry matching the template; this works for RCS format dates only.
e1c0c2d1
KH
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))))
8321b22a
GM
216 ;; Most (but not all) versions of RCS use two-digit years
217 ;; to represent dates in the range 1900 through 1999.
218 ;; The two-digit and four-digit notations can both appear
219 ;; in the same file. Normalize the two-digit versions.
220 (save-match-data
221 (if (string-match "\\`[0-9][0-9]\\." date)
222 (setq date (concat "19" date))))
e1c0c2d1
KH
223 (if (string< latest-date date)
224 (progn
225 (setq latest-date date)
226 (setq latest-val
227 (vc-match-substring (elt p 1)))))))
228 (if file
229 (progn (vc-file-setprop file (car properties) latest-val)
230 (setq properties (cdr properties))))
231 latest-val)))))
232 patterns)
233 )
234
02d383eb
RS
235(defun vc-insert-file (file &optional limit blocksize)
236 ;; Insert the contents of FILE into the current buffer.
237 ;; Optional argument LIMIT is a regexp. If present,
238 ;; the file is inserted in chunks of size BLOCKSIZE
a7acbbe4 239 ;; (default 8 kByte), until the first occurrence of
02d383eb
RS
240 ;; LIMIT is found. The function returns nil if FILE
241 ;; doesn't exist.
07de4c3d 242 (erase-buffer)
02d383eb
RS
243 (cond ((file-exists-p file)
244 (cond (limit
245 (if (not blocksize) (setq blocksize 8192))
246 (let (found s)
247 (while (not found)
248 (setq s (buffer-size))
249 (goto-char (1+ s))
250 (setq found
251 (or (zerop (car (cdr
252 (insert-file-contents file nil s
253 (+ s blocksize)))))
254 (progn (beginning-of-line)
255 (re-search-forward limit nil t)))))))
256 (t (insert-file-contents file)))
257 (set-buffer-modified-p nil)
258 (auto-save-mode nil)
259 t)
260 (t nil)))
261
262(defun vc-parse-locks (file locks)
263 ;; Parse RCS or SCCS locks.
264 ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
265 ;; which is returned and stored into the property `vc-master-locks'.
266 (if (not locks)
267 (vc-file-setprop file 'vc-master-locks 'none)
268 (let ((found t) (index 0) master-locks version user)
269 (cond ((eq (vc-backend file) 'SCCS)
270 (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
271 locks index)
272 (setq version (substring locks
273 (match-beginning 1) (match-end 1)))
274 (setq user (substring locks
275 (match-beginning 2) (match-end 2)))
276 (setq master-locks (append master-locks
277 (list (cons version user))))
278 (setq index (match-end 0))))
279 ((eq (vc-backend file) 'RCS)
280 (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
281 locks index)
282 (setq version (substring locks
283 (match-beginning 2) (match-end 2)))
284 (setq user (substring locks
285 (match-beginning 1) (match-end 1)))
286 (setq master-locks (append master-locks
287 (list (cons version user))))
e66eac08
AS
288 (setq index (match-end 0)))
289 (if (string-match ";[ \t\n]+strict;" locks index)
290 (vc-file-setprop file 'vc-checkout-model 'manual)
291 (vc-file-setprop file 'vc-checkout-model 'implicit))))
02d383eb
RS
292 (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
293
7064821c
AS
294(defun vc-simple-command (okstatus command file &rest args)
295 ;; Simple version of vc-do-command, for use in vc-hooks only.
296 ;; Don't switch to the *vc-info* buffer before running the
297 ;; command, because that would change its default directory
298 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
299 (erase-buffer))
300 (let ((exec-path (append vc-path exec-path)) exec-status
301 ;; Add vc-path to PATH for the execution of this command.
302 (process-environment
303 (cons (concat "PATH=" (getenv "PATH")
304 path-separator
305 (mapconcat 'identity vc-path path-separator))
306 process-environment)))
307 (setq exec-status
308 (apply 'call-process command nil "*vc-info*" nil
309 (append args (list file))))
310 (cond ((> exec-status okstatus)
311 (switch-to-buffer (get-file-buffer file))
312 (shrink-window-if-larger-than-buffer
313 (display-buffer "*vc-info*"))
314 (error "Couldn't find version control information")))
315 exec-status))
316
eaff65c8 317(defun vc-parse-cvs-status (&optional full)
8aa81ea8 318 ;; Parse output of "cvs status" command in the current buffer and
eaff65c8
AS
319 ;; set file properties accordingly. Unless FULL is t, parse only
320 ;; essential information.
321 (let (file status)
322 (goto-char (point-min))
323 (if (re-search-forward "^File: " nil t)
324 (cond
325 ((looking-at "no file") nil)
326 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
327 (setq file (concat default-directory (match-string 1)))
328 (vc-file-setprop file 'vc-backend 'CVS)
329 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
330 (setq status "Unknown")
331 (setq status (match-string 1)))
332 (if (and full
333 (re-search-forward
334 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)"
335 nil t))
336 (vc-file-setprop file 'vc-latest-version (match-string 2)))
337 (cond
338 ((string-match "Up-to-date" status)
339 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
340 (vc-file-setprop file 'vc-checkout-time
341 (nth 5 (file-attributes file))))
342 ((vc-file-setprop file 'vc-cvs-status
343 (cond
344 ((string-match "Locally Modified" status) 'locally-modified)
345 ((string-match "Needs Merge" status) 'needs-merge)
346 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
347 'needs-checkout)
edad8a09
AS
348 ((string-match "Unresolved Conflict" status)
349 'unresolved-conflict)
350 ((string-match "File had conflicts on merge" status)
351 'unresolved-conflict)
eaff65c8
AS
352 ((string-match "Locally Added" status) 'locally-added)
353 ((string-match "New file!" status) 'locally-added)
354 (t 'unknown))))))))))
8aa81ea8 355
02d383eb
RS
356(defun vc-fetch-master-properties (file)
357 ;; Fetch those properties of FILE that are stored in the master file.
1efcbf46
RS
358 ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
359 ;; here because that is slow.
360 ;; That gets done if/when the functions vc-latest-version
361 ;; and vc-your-latest-version get called.
02d383eb
RS
362 (save-excursion
363 (cond
364 ((eq (vc-backend file) 'SCCS)
365 (set-buffer (get-buffer-create "*vc-info*"))
366 (if (vc-insert-file (vc-lock-file file))
07de4c3d 367 (vc-parse-locks file (buffer-string))
02d383eb
RS
368 (vc-file-setprop file 'vc-master-locks 'none))
369 (vc-insert-file (vc-name file) "^\001e")
370 (vc-parse-buffer
371 (list '("^\001d D \\([^ ]+\\)" 1)
372 (list (concat "^\001d D \\([^ ]+\\) .* "
2a11c6f3 373 (regexp-quote (vc-user-login-name)) " ") 1))
02d383eb
RS
374 file
375 '(vc-latest-version vc-your-latest-version)))
376
377 ((eq (vc-backend file) 'RCS)
378 (set-buffer (get-buffer-create "*vc-info*"))
e66eac08 379 (vc-insert-file (vc-name file) "^[0-9]")
02d383eb
RS
380 (vc-parse-buffer
381 (list '("^head[ \t\n]+\\([^;]+\\);" 1)
382 '("^branch[ \t\n]+\\([^;]+\\);" 1)
e66eac08 383 '("^locks[ \t\n]*\\([^;]*;\\([ \t\n]*strict;\\)?\\)" 1))
02d383eb
RS
384 file
385 '(vc-head-version
386 vc-default-branch
1efcbf46 387 vc-master-locks))
af5e65b9
AS
388 ;; determine vc-master-workfile-version: it is either the head
389 ;; of the trunk, the head of the default branch, or the
390 ;; "default branch" itself, if that is a full revision number.
02d383eb
RS
391 (let ((default-branch (vc-file-getprop file 'vc-default-branch)))
392 (cond
393 ;; no default branch
394 ((or (not default-branch) (string= "" default-branch))
af5e65b9 395 (vc-file-setprop file 'vc-master-workfile-version
02d383eb
RS
396 (vc-file-getprop file 'vc-head-version)))
397 ;; default branch is actually a revision
398 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
399 default-branch)
af5e65b9
AS
400 (vc-file-setprop file 'vc-master-workfile-version default-branch))
401 ;; else, search for the head of the default branch
07de4c3d 402 (t (vc-insert-file (vc-name file) "^desc")
1efcbf46 403 (vc-parse-buffer (list (list
02d383eb
RS
404 (concat "^\\("
405 (regexp-quote default-branch)
406 "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
af5e65b9 407 file '(vc-master-workfile-version)))))
02d383eb
RS
408 ;; translate the locks
409 (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
410
411 ((eq (vc-backend file) 'CVS)
7064821c 412 (save-excursion
719dcee9
AS
413 ;; Call "cvs status" in the right directory, passing only the
414 ;; nondirectory part of the file name -- otherwise CVS might
415 ;; silently give a wrong result.
416 (let ((default-directory (file-name-directory file)))
417 (vc-simple-command 0 "cvs" (file-name-nondirectory file) "status"))
7064821c 418 (set-buffer (get-buffer "*vc-info*"))
eaff65c8 419 (vc-parse-cvs-status t))))
3be2a362
RS
420 (if (get-buffer "*vc-info*")
421 (kill-buffer (get-buffer "*vc-info*")))))
e1c0c2d1
KH
422
423;;; Functions that determine property values, by examining the
424;;; working file, the master file, or log program output
425
426(defun vc-consult-rcs-headers (file)
427 ;; Search for RCS headers in FILE, and set properties
428 ;; accordingly. This function can be disabled by setting
429 ;; vc-consult-headers to nil.
430 ;; Returns: nil if no headers were found
431 ;; (or if the feature is disabled,
432 ;; or if there is currently no buffer
433 ;; visiting FILE)
434 ;; 'rev if a workfile revision was found
435 ;; 'rev-and-lock if revision and lock info was found
e66eac08 436 (cond
e1c0c2d1 437 ((or (not vc-consult-headers)
02d383eb 438 (not (get-file-buffer file))) nil)
e66eac08
AS
439 ((let (status version locking-user)
440 (save-excursion
e1c0c2d1
KH
441 (set-buffer (get-file-buffer file))
442 (goto-char (point-min))
443 (cond
444 ;; search for $Id or $Header
445 ;; -------------------------
b13a9b1b
RS
446 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
447 ((or (and (search-forward "$Id\ : " nil t)
1efcbf46
RS
448 (looking-at "[^ ]+ \\([0-9.]+\\) "))
449 (and (progn (goto-char (point-min))
b13a9b1b 450 (search-forward "$Header\ : " nil t))
1efcbf46 451 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
04094290 452 (goto-char (match-end 0))
e1c0c2d1 453 ;; if found, store the revision number ...
55572259
KH
454 (setq version (buffer-substring-no-properties (match-beginning 1)
455 (match-end 1)))
e66eac08
AS
456 ;; ... and check for the locking state
457 (cond
458 ((looking-at
459 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
460 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
461 "[^ ]+ [^ ]+ ")) ; author & state
462 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
ce27f264 463 (cond
e66eac08
AS
464 ;; unlocked revision
465 ((looking-at "\\$")
466 (setq locking-user 'none)
467 (setq status 'rev-and-lock))
468 ;; revision is locked by some user
469 ((looking-at "\\([^ ]+\\) \\$")
470 (setq locking-user
55572259
KH
471 (buffer-substring-no-properties (match-beginning 1)
472 (match-end 1)))
e66eac08
AS
473 (setq status 'rev-and-lock))
474 ;; everything else: false
475 (nil)))
476 ;; unexpected information in
477 ;; keyword string --> quit
478 (nil)))
e1c0c2d1
KH
479 ;; search for $Revision
480 ;; --------------------
481 ((re-search-forward (concat "\\$"
482 "Revision: \\([0-9.]+\\) \\$")
483 nil t)
484 ;; if found, store the revision number ...
55572259
KH
485 (setq version (buffer-substring-no-properties (match-beginning 1)
486 (match-end 1)))
e66eac08
AS
487 ;; and see if there's any lock information
488 (goto-char (point-min))
489 (if (re-search-forward (concat "\\$" "Locker:") nil t)
490 (cond ((looking-at " \\([^ ]+\\) \\$")
55572259
KH
491 (setq locking-user (buffer-substring-no-properties
492 (match-beginning 1)
493 (match-end 1)))
e66eac08
AS
494 (setq status 'rev-and-lock))
495 ((looking-at " *\\$")
496 (setq locking-user 'none)
497 (setq status 'rev-and-lock))
498 (t
499 (setq locking-user 'none)
500 (setq status 'rev-and-lock)))
501 (setq status 'rev)))
e1c0c2d1
KH
502 ;; else: nothing found
503 ;; -------------------
e66eac08
AS
504 (t nil)))
505 (if status (vc-file-setprop file 'vc-workfile-version version))
506 (and (eq status 'rev-and-lock)
507 (eq (vc-backend file) 'RCS)
508 (vc-file-setprop file 'vc-locking-user locking-user)
509 ;; If the file has headers, we don't want to query the master file,
510 ;; because that would eliminate all the performance gain the headers
511 ;; brought us. We therefore use a heuristic for the checkout model
512 ;; now: If we trust the file permissions, and the file is not
513 ;; locked, then if the file is read-only the checkout model is
514 ;; `manual', otherwise `implicit'.
515 (not (vc-mistrust-permissions file))
516 (not (vc-locking-user file))
517 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
518 (vc-file-setprop file 'vc-checkout-model 'manual)
7064821c
AS
519 (vc-file-setprop file 'vc-checkout-model 'implicit)))
520 status))))
e1c0c2d1 521
02d383eb
RS
522;;; Access functions to file properties
523;;; (Properties should be _set_ using vc-file-setprop, but
524;;; _retrieved_ only through these functions, which decide
525;;; if the property is already known or not. A property should
526;;; only be retrieved by vc-file-getprop if there is no
527;;; access function.)
528
529;;; properties indicating the backend
530;;; being used for FILE
e1c0c2d1
KH
531
532(defun vc-backend-subdirectory-name (&optional file)
533 ;; Where the master and lock files for the current directory are kept
534 (symbol-name
535 (or
536 (and file (vc-backend file))
537 vc-default-back-end
538 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
539
02d383eb 540(defun vc-name (file)
b23a2306
AS
541 "Return the master name of a file, nil if it is not registered.
542For CVS, the full name of CVS/Entries is returned."
02d383eb 543 (or (vc-file-getprop file 'vc-name)
8aa81ea8
RS
544 ;; Use the caching mechanism of vc-backend, below.
545 (if (vc-backend file)
546 (vc-file-getprop file 'vc-name))))
e1c0c2d1 547
02d383eb
RS
548(defun vc-backend (file)
549 "Return the version-control type of a file, nil if it is not registered."
8aa81ea8
RS
550 ;; Note that internally, Emacs remembers unregistered
551 ;; files by setting the property to `none'.
552 (if file
553 (let ((property (vc-file-getprop file 'vc-backend))
554 (name-and-type))
555 (cond ((eq property 'none) nil)
556 (property)
557 (t (setq name-and-type (vc-registered file))
558 (if name-and-type
559 (progn
560 (vc-file-setprop file 'vc-name (car name-and-type))
561 (vc-file-setprop file 'vc-backend (cdr name-and-type)))
562 (vc-file-setprop file 'vc-backend 'none)
563 nil))))))
e1c0c2d1 564
04446ed0
AS
565(defun vc-checkout-model (file)
566 ;; Return `manual' if the user has to type C-x C-q to check out FILE.
e66eac08
AS
567 ;; Return `implicit' if the file can be modified without locking it first.
568 (or
569 (vc-file-getprop file 'vc-checkout-model)
570 (cond
571 ((eq (vc-backend file) 'SCCS)
572 (vc-file-setprop file 'vc-checkout-model 'manual))
573 ((eq (vc-backend file) 'RCS)
574 (vc-consult-rcs-headers file)
575 (or (vc-file-getprop file 'vc-checkout-model)
576 (progn (vc-fetch-master-properties file)
577 (vc-file-getprop file 'vc-checkout-model))))
578 ((eq (vc-backend file) 'CVS)
579 (vc-file-setprop file 'vc-checkout-model
403d549c
RS
580 (cond
581 ((getenv "CVSREAD") 'manual)
582 ;; If the file is not writeable, this is probably because the
583 ;; file is being "watched" by other developers. Use "manual"
584 ;; checkout in this case. (If vc-mistrust-permissions was t,
585 ;; we actually shouldn't trust this, but there is no other way
586 ;; to learn this from CVS at the moment (version 1.9).)
587 ((string-match "r-..-..-." (nth 8 (file-attributes file)))
588 'manual)
589 (t 'implicit)))))))
04446ed0 590
02d383eb 591;;; properties indicating the locking state
e1c0c2d1
KH
592
593(defun vc-cvs-status (file)
594 ;; Return the cvs status of FILE
595 ;; (Status field in output of "cvs status")
596 (cond ((vc-file-getprop file 'vc-cvs-status))
02d383eb 597 (t (vc-fetch-master-properties file)
e1c0c2d1
KH
598 (vc-file-getprop file 'vc-cvs-status))))
599
02d383eb
RS
600(defun vc-master-locks (file)
601 ;; Return the lock entries in the master of FILE.
602 ;; Return 'none if there are no such entries, and a list
603 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
604 (cond ((vc-file-getprop file 'vc-master-locks))
605 (t (vc-fetch-master-properties file)
606 (vc-file-getprop file 'vc-master-locks))))
607
608(defun vc-master-locking-user (file)
609 ;; Return the master file's idea of who is locking
610 ;; the current workfile version of FILE.
611 ;; Return 'none if it is not locked.
612 (let ((master-locks (vc-master-locks file)) lock)
613 (if (eq master-locks 'none) 'none
614 ;; search for a lock on the current workfile version
615 (setq lock (assoc (vc-workfile-version file) master-locks))
616 (cond (lock (cdr lock))
617 ('none)))))
618
7064821c
AS
619(defun vc-lock-from-permissions (file)
620 ;; If the permissions can be trusted for this file, determine the
621 ;; locking state from them. Returns (user-login-name), `none', or nil.
622 ;; This implementation assumes that any file which is under version
623 ;; control and has -rw-r--r-- is locked by its owner. This is true
624 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
625 ;; We have to be careful not to exclude files with execute bits on;
626 ;; scripts can be under version control too. Also, we must ignore the
627 ;; group-read and other-read bits, since paranoid users turn them off.
628 ;; This hack wins because calls to the somewhat expensive
629 ;; `vc-fetch-master-properties' function only have to be made if
630 ;; (a) the file is locked by someone other than the current user,
631 ;; or (b) some untoward manipulation behind vc's back has changed
632 ;; the owner or the `group' or `other' write bits.
633 (let ((attributes (file-attributes file)))
634 (if (not (vc-mistrust-permissions file))
635 (cond ((string-match ".r-..-..-." (nth 8 attributes))
636 (vc-file-setprop file 'vc-locking-user 'none))
637 ((and (= (nth 2 attributes) (user-uid))
638 (string-match ".rw..-..-." (nth 8 attributes)))
2a11c6f3 639 (vc-file-setprop file 'vc-locking-user (vc-user-login-name)))
7064821c
AS
640 (nil)))))
641
2a11c6f3
AS
642(defun vc-user-login-name (&optional uid)
643 ;; Return the name under which the user is logged in, as a string.
644 ;; (With optional argument UID, return the name of that user.)
645 ;; This function does the same as `user-login-name', but unlike
646 ;; that, it never returns nil. If a UID cannot be resolved, that
647 ;; UID is returned as a string.
648 (or (user-login-name uid)
649 (and uid (number-to-string uid))
650 (number-to-string (user-uid))))
651
7064821c 652(defun vc-file-owner (file)
1525b516 653 ;; Return who owns FILE (user name, as a string).
2a11c6f3 654 (vc-user-login-name (nth 2 (file-attributes file))))
7064821c
AS
655
656(defun vc-rcs-lock-from-diff (file)
657 ;; Diff the file against the master version. If differences are found,
31888047
AS
658 ;; mark the file locked. This is only used for RCS with non-strict
659 ;; locking. (If "rcsdiff" doesn't understand --brief, we do a double-take
660 ;; and remember the fact for the future.)
661 (let* ((version (concat "-r" (vc-workfile-version file)))
662 (status (if (eq vc-rcsdiff-knows-brief 'no)
663 (vc-simple-command 1 "rcsdiff" file version)
664 (vc-simple-command 2 "rcsdiff" file "--brief" version))))
665 (if (eq status 2)
666 (if (not vc-rcsdiff-knows-brief)
667 (setq vc-rcsdiff-knows-brief 'no
668 status (vc-simple-command 1 "rcsdiff" file version))
669 (error "rcsdiff failed."))
670 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
671 (if (zerop status)
672 (vc-file-setprop file 'vc-locking-user 'none)
673 (vc-file-setprop file 'vc-locking-user (vc-file-owner file)))))
7064821c 674
e1c0c2d1 675(defun vc-locking-user (file)
02d383eb 676 ;; Return the name of the person currently holding a lock on FILE.
1525b516 677 ;; Return nil if there is no such person.
02d383eb 678 ;; Under CVS, a file is considered locked if it has been modified since
7064821c 679 ;; it was checked out.
02d383eb
RS
680 ;; The property is cached. It is only looked up if it is currently nil.
681 ;; Note that, for a file that is not locked, the actual property value
7064821c 682 ;; is `none', to distinguish it from an unknown locking state. That value
02d383eb
RS
683 ;; is converted to nil by this function, and returned to the caller.
684 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
685 (if locking-user
686 ;; if we already know the property, return it
687 (if (eq locking-user 'none) nil locking-user)
688
689 ;; otherwise, infer the property...
690 (cond
02d383eb 691 ((eq (vc-backend file) 'CVS)
7064821c
AS
692 (or (and (eq (vc-checkout-model file) 'manual)
693 (vc-lock-from-permissions file))
b23a2306
AS
694 (and (equal (vc-file-getprop file 'vc-checkout-time)
695 (nth 5 (file-attributes file)))
696 (vc-file-setprop file 'vc-locking-user 'none))
1525b516 697 (vc-file-setprop file 'vc-locking-user (vc-file-owner file))))
7064821c
AS
698
699 ((eq (vc-backend file) 'RCS)
700 (let (p-lock)
701
702 ;; Check for RCS headers first
703 (or (eq (vc-consult-rcs-headers file) 'rev-and-lock)
704
705 ;; If there are no headers, try to learn it
706 ;; from the permissions.
707 (and (setq p-lock (vc-lock-from-permissions file))
708 (if (eq p-lock 'none)
709
710 ;; If the permissions say "not locked", we know
711 ;; that the checkout model must be `manual'.
712 (vc-file-setprop file 'vc-checkout-model 'manual)
713
714 ;; If the permissions say "locked", we can only trust
715 ;; this *if* the checkout model is `manual'.
716 (eq (vc-checkout-model file) 'manual)))
717
718 ;; Otherwise, use lock information from the master file.
719 (vc-file-setprop file 'vc-locking-user
720 (vc-master-locking-user file)))
721
722 ;; Finally, if the file is not explicitly locked
723 ;; it might still be locked implicitly.
724 (and (eq (vc-file-getprop file 'vc-locking-user) 'none)
725 (eq (vc-checkout-model file) 'implicit)
726 (vc-rcs-lock-from-diff file))))
727
728 ((eq (vc-backend file) 'SCCS)
729 (or (vc-lock-from-permissions file)
730 (vc-file-setprop file 'vc-locking-user
aadce164 731 (vc-master-locking-user file)))))
7064821c
AS
732
733 ;; convert a possible 'none value
734 (setq locking-user (vc-file-getprop file 'vc-locking-user))
aadce164 735 (if (eq locking-user 'none) nil locking-user))))
02d383eb
RS
736
737;;; properties to store current and recent version numbers
e1c0c2d1
KH
738
739(defun vc-latest-version (file)
740 ;; Return version level of the latest version of FILE
02d383eb 741 (cond ((vc-file-getprop file 'vc-latest-version))
1efcbf46 742 (t (vc-fetch-properties file)
02d383eb 743 (vc-file-getprop file 'vc-latest-version))))
e1c0c2d1
KH
744
745(defun vc-your-latest-version (file)
746 ;; Return version level of the latest version of FILE checked in by you
02d383eb 747 (cond ((vc-file-getprop file 'vc-your-latest-version))
1efcbf46 748 (t (vc-fetch-properties file)
02d383eb 749 (vc-file-getprop file 'vc-your-latest-version))))
e1c0c2d1 750
af5e65b9
AS
751(defun vc-master-workfile-version (file)
752 ;; Return the master file's idea of what is the current workfile version.
e1c0c2d1 753 ;; This property is defined for RCS only.
af5e65b9 754 (cond ((vc-file-getprop file 'vc-master-workfile-version))
02d383eb 755 (t (vc-fetch-master-properties file)
af5e65b9 756 (vc-file-getprop file 'vc-master-workfile-version))))
e1c0c2d1 757
1efcbf46
RS
758(defun vc-fetch-properties (file)
759 ;; Fetch vc-latest-version and vc-your-latest-version
760 ;; if that wasn't already done.
04094290
RS
761 (cond
762 ((eq (vc-backend file) 'RCS)
ee526b55
RS
763 (save-excursion
764 (set-buffer (get-buffer-create "*vc-info*"))
765 (vc-insert-file (vc-name file) "^desc")
766 (vc-parse-buffer
767 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
768 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
769 "date[ \t]+\\([0-9.]+\\);[ \t]+"
770 "author[ \t]+"
2a11c6f3 771 (regexp-quote (vc-user-login-name)) ";") 1 2))
ee526b55 772 file
07de4c3d
RS
773 '(vc-latest-version vc-your-latest-version))
774 (if (get-buffer "*vc-info*")
775 (kill-buffer (get-buffer "*vc-info*")))))
04094290 776 (t (vc-fetch-master-properties file))
1efcbf46
RS
777 ))
778
e1c0c2d1
KH
779(defun vc-workfile-version (file)
780 ;; Return version level of the current workfile FILE
781 ;; This is attempted by first looking at the RCS keywords.
782 ;; If there are no keywords in the working file,
af5e65b9 783 ;; vc-master-workfile-version is taken.
e1c0c2d1
KH
784 ;; Note that this property is cached, that is, it is only
785 ;; looked up if it is nil.
786 ;; For SCCS, this property is equivalent to vc-latest-version.
787 (cond ((vc-file-getprop file 'vc-workfile-version))
788 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
789 ((eq (vc-backend file) 'RCS)
790 (if (vc-consult-rcs-headers file)
791 (vc-file-getprop file 'vc-workfile-version)
af5e65b9 792 (let ((rev (cond ((vc-master-workfile-version file))
e1c0c2d1
KH
793 ((vc-latest-version file)))))
794 (vc-file-setprop file 'vc-workfile-version rev)
795 rev)))
796 ((eq (vc-backend file) 'CVS)
797 (if (vc-consult-rcs-headers file) ;; CVS
798 (vc-file-getprop file 'vc-workfile-version)
7b0e1b8f
KH
799 (catch 'found
800 (vc-find-cvs-master (file-name-directory file)
801 (file-name-nondirectory file)))
e1c0c2d1 802 (vc-file-getprop file 'vc-workfile-version)))))
f2ee4191 803
594722a8
ER
804;;; actual version-control code starts here
805
806(defun vc-registered (file)
18c8a18e
PE
807 (let (handler handlers)
808 (if (boundp 'file-name-handler-alist)
b993101e 809 (setq handler (find-file-name-handler file 'vc-registered)))
18c8a18e
PE
810 (if handler
811 (funcall handler 'vc-registered file)
812 ;; Search for a master corresponding to the given file
813 (let ((dirname (or (file-name-directory file) ""))
814 (basename (file-name-nondirectory file)))
815 (catch 'found
816 (mapcar
817 (function (lambda (s)
174edc13
RS
818 (if (atom s)
819 (funcall s dirname basename)
820 (let ((trial (format (car s) dirname basename)))
821 (if (and (file-exists-p trial)
822 ;; Make sure the file we found with name
823 ;; TRIAL is not the source file itself.
824 ;; That can happen with RCS-style names
825 ;; if the file name is truncated
826 ;; (e.g. to 14 chars). See if either
827 ;; directory or attributes differ.
828 (or (not (string= dirname
829 (file-name-directory trial)))
830 (not (equal
831 (file-attributes file)
832 (file-attributes trial)))))
833 (throw 'found (cons trial (cdr s))))))))
18c8a18e
PE
834 vc-master-templates)
835 nil)))))
594722a8 836
809c22a2
AS
837(defun vc-sccs-project-dir ()
838 ;; Return the full pathname of the SCCS PROJECTDIR, if it exists,
839 ;; otherwise nil. The PROJECTDIR is indicated by the environment
840 ;; variable of the same name. If its value starts with a slash,
841 ;; it must be an absolute path name that points to the
842 ;; directory where SCCS history files reside. If it does not
843 ;; begin with a slash, it is taken as the name of a user,
844 ;; and history files reside in an "src" or "source" subdirectory
845 ;; of that user's home directory.
846 (let ((project-dir (getenv "PROJECTDIR")))
847 (and project-dir
848 (if (eq (elt project-dir 0) ?/)
849 (if (file-exists-p (concat project-dir "/SCCS"))
850 (concat project-dir "/SCCS/")
851 (if (file-exists-p project-dir)
852 project-dir))
853 (setq project-dir (expand-file-name (concat "~" project-dir)))
854 (let (trial)
855 (setq trial (concat project-dir "/src/SCCS"))
856 (if (file-exists-p trial)
857 (concat trial "/")
858 (setq trial (concat project-dir "/src"))
859 (if (file-exists-p trial)
860 (concat trial "/")
861 (setq trial (concat project-dir "/source/SCCS"))
862 (if (file-exists-p trial)
863 (concat trial "/")
864 (setq trial (concat project-dir "/source/"))
865 (if (file-exists-p trial)
866 (concat trial "/"))))))))))
867
868(defun vc-search-sccs-project-dir (dirname basename)
869 ;; Check if there is a master file for BASENAME in the
870 ;; SCCS project directory. If yes, throw `found' as
871 ;; expected by vc-registered. If not, return nil.
872 (let* ((project-dir (vc-sccs-project-dir))
873 (master-file (and project-dir (concat project-dir "s." basename))))
874 (and master-file
875 (file-exists-p master-file)
876 (throw 'found (cons master-file 'SCCS)))))
877
174edc13
RS
878(defun vc-find-cvs-master (dirname basename)
879 ;; Check if DIRNAME/BASENAME is handled by CVS.
809c22a2 880 ;; If it is, do a (throw 'found (cons MASTER-FILE 'CVS)).
b23a2306
AS
881 ;; Note: This function throws the name of CVS/Entries
882 ;; NOT that of the RCS master file (because we wouldn't be able
883 ;; to access it under remote CVS).
884 ;; The function returns nil if DIRNAME/BASENAME is not handled by CVS.
0b086efb
RS
885 (if (and vc-handle-cvs
886 (file-directory-p (concat dirname "CVS/"))
b23a2306 887 (file-readable-p (concat dirname "CVS/Entries")))
702220f3 888 (let ((file (concat dirname basename))
702220f3 889 buffer)
174edc13
RS
890 (unwind-protect
891 (save-excursion
7064821c
AS
892 (setq buffer (set-buffer (get-buffer-create "*vc-info*")))
893 (vc-insert-file (concat dirname "CVS/Entries"))
174edc13 894 (goto-char (point-min))
9fe89e73
AS
895 ;; make sure that the file name is searched
896 ;; case-sensitively - case-fold-search is a buffer-local
897 ;; variable, so setting it here won't affect any other buffers
898 (setq case-fold-search nil)
174edc13 899 (cond
702220f3
AS
900 ;; entry for a "locally added" file (not yet committed)
901 ((re-search-forward
902 (concat "^/" (regexp-quote basename) "/0/") nil t)
903 (vc-file-setprop file 'vc-checkout-time 0)
904 (vc-file-setprop file 'vc-workfile-version "0")
905 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
a93de381 906 ;; normal entry
174edc13 907 ((re-search-forward
b23a2306 908 (concat "^/" (regexp-quote basename)
702220f3
AS
909 ;; revision
910 "/\\([^/]*\\)"
911 ;; timestamp
912 "/[A-Z][a-z][a-z]" ;; week day (irrelevant)
913 " \\([A-Z][a-z][a-z]\\)" ;; month name
914 " *\\([0-9]*\\)" ;; day of month
915 " \\([0-9]*\\):\\([0-9]*\\):\\([0-9]*\\)" ;; hms
916 " \\([0-9]*\\)" ;; year
917 ;; optional conflict field
918 "\\(+[^/]*\\)?/")
174edc13 919 nil t)
b23a2306
AS
920 ;; We found it. Store away version number now that we
921 ;; are anyhow so close to finding it.
922 (vc-file-setprop file
f2ee4191 923 'vc-workfile-version
b23a2306
AS
924 (match-string 1))
925 ;; If the file hasn't been modified since checkout,
926 ;; store the checkout-time.
c18381d1 927 (let ((mtime (nth 5 (file-attributes file)))
702220f3
AS
928 (second (string-to-number (match-string 6)))
929 (minute (string-to-number (match-string 5)))
930 (hour (string-to-number (match-string 4)))
931 (day (string-to-number (match-string 3)))
932 (year (string-to-number (match-string 7))))
c18381d1
PE
933 (if (equal mtime
934 (encode-time
935 second minute hour day
936 (/ (string-match
702220f3 937 (match-string 2)
c18381d1
PE
938 "xxxJanFebMarAprMayJunJulAugSepOctNovDec")
939 3)
940 year 0))
719dcee9
AS
941 (vc-file-setprop file 'vc-checkout-time mtime)
942 (vc-file-setprop file 'vc-checkout-time 0)))
b23a2306 943 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
702220f3
AS
944 ;; entry with arbitrary text as timestamp
945 ;; (this means we should consider it modified)
a93de381 946 ((re-search-forward
702220f3
AS
947 (concat "^/" (regexp-quote basename)
948 ;; revision
949 "/\\([^/]*\\)"
950 ;; timestamp (arbitrary text)
951 "/[^/]*"
952 ;; optional conflict field
953 "\\(+[^/]*\\)?/")
954 nil t)
955 ;; We found it. Store away version number now that we
956 ;; are anyhow so close to finding it.
957 (vc-file-setprop file 'vc-workfile-version (match-string 1))
274c59c9
KH
958 (vc-file-setprop file 'vc-checkout-time 0)
959 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
702220f3 960 (t nil)))
7064821c 961 (kill-buffer buffer)))))
174edc13 962
f2ee4191
RS
963(defun vc-buffer-backend ()
964 "Return the version-control type of the visited file, or nil if none."
965 (if (eq vc-buffer-backend t)
e1c0c2d1 966 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
f2ee4191
RS
967 vc-buffer-backend))
968
c844616c 969(defun vc-toggle-read-only (&optional verbose)
c43e436c
RS
970 "Change read-only status of current buffer, perhaps via version control.
971If the buffer is visiting a file registered with version control,
972then check the file in or out. Otherwise, just change the read-only flag
ec44193a
KH
973of the buffer.
974With prefix argument, ask for version number to check in or check out.
975Check-out of a specified version number does not lock the file;
976to do that, use this command a second time with no argument."
c844616c 977 (interactive "P")
702220f3
AS
978 (if (or (and (boundp 'vc-dired-mode) vc-dired-mode)
979 ;; use boundp because vc.el might not be loaded
980 (vc-backend (buffer-file-name)))
c844616c 981 (vc-next-action verbose)
594722a8 982 (toggle-read-only)))
c43e436c 983(define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
594722a8 984
e66eac08
AS
985(defun vc-after-save ()
986 ;; Function to be called by basic-save-buffer (in files.el).
987 ;; If the file in the current buffer is under version control,
988 ;; not locked, and the checkout model for it is `implicit',
989 ;; mark it "locked" and redisplay the mode line.
990 (let ((file (buffer-file-name)))
8aa81ea8 991 (and (vc-backend file)
b23a2306
AS
992 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
993 (nth 5 (file-attributes file)))
994 ;; File has been saved in the same second in which
995 ;; it was checked out. Clear the checkout-time
996 ;; to avoid confusion.
997 (vc-file-setprop file 'vc-checkout-time nil))
998 t)
e66eac08
AS
999 (not (vc-locking-user file))
1000 (eq (vc-checkout-model file) 'implicit)
2a11c6f3 1001 (vc-file-setprop file 'vc-locking-user (vc-user-login-name))
b23a2306
AS
1002 (or (and (eq (vc-backend file) 'CVS)
1003 (vc-file-setprop file 'vc-cvs-status nil))
1004 t)
1005 (vc-mode-line file))))
04446ed0 1006
594722a8 1007(defun vc-mode-line (file &optional label)
7bc2b98b 1008 "Set `vc-mode' to display type of version control for FILE.
594722a8 1009The value is set in the current buffer, which should be the buffer
624c0e9d
RS
1010visiting FILE. Second optional arg LABEL is put in place of version
1011control system name."
18c8a18e 1012 (interactive (list buffer-file-name nil))
7b0e1b8f 1013 (let ((vc-type (vc-backend file)))
f2ee4191 1014 (setq vc-mode
7b0e1b8f
KH
1015 (and vc-type
1016 (concat " " (or label (symbol-name vc-type))
1017 (and vc-display-status (vc-status file)))))
9becbeca
RS
1018 ;; If the file is locked by some other user, make
1019 ;; the buffer read-only. Like this, even root
defccde3 1020 ;; cannot modify a file that someone else has locked.
7b0e1b8f
KH
1021 (and vc-type
1022 (equal file (buffer-file-name))
e66eac08 1023 (vc-locking-user file)
2a11c6f3 1024 (not (string= (vc-user-login-name) (vc-locking-user file)))
e66eac08 1025 (setq buffer-read-only t))
defccde3
RS
1026 ;; If the user is root, and the file is not owner-writable,
1027 ;; then pretend that we can't write it
1028 ;; even though we can (because root can write anything).
1029 ;; This way, even root cannot modify a file that isn't locked.
9becbeca
RS
1030 (and vc-type
1031 (equal file (buffer-file-name))
1032 (not buffer-read-only)
1033 (zerop (user-real-uid))
1034 (zerop (logand (file-modes (buffer-file-name)) 128))
1035 (setq buffer-read-only t))
f2ee4191
RS
1036 (force-mode-line-update)
1037 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
1038 vc-type))
594722a8 1039
e1c0c2d1 1040(defun vc-status (file)
45c92c0c 1041 ;; Return string for placement in modeline by `vc-mode-line'.
e1c0c2d1 1042 ;; Format:
624c0e9d 1043 ;;
e1c0c2d1
KH
1044 ;; "-REV" if the revision is not locked
1045 ;; ":REV" if the revision is locked by the user
1046 ;; ":LOCKER:REV" if the revision is locked by somebody else
1047 ;; " @@" for a CVS file that is added, but not yet committed
174edc13 1048 ;;
e1c0c2d1
KH
1049 ;; In the CVS case, a "locked" working file is a
1050 ;; working file that is modified with respect to the master.
e66eac08
AS
1051 ;; The file is "locked" from the moment when the user saves
1052 ;; the modified buffer.
e1c0c2d1
KH
1053 ;;
1054 ;; This function assumes that the file is registered.
1055
1056 (let ((locker (vc-locking-user file))
1057 (rev (vc-workfile-version file)))
1058 (cond ((string= "0" rev)
1059 " @@")
38179d42
RM
1060 ((not locker)
1061 (concat "-" rev))
2a11c6f3 1062 ((string= locker (vc-user-login-name))
e1c0c2d1
KH
1063 (concat ":" rev))
1064 (t
1065 (concat ":" locker ":" rev)))))
f2ee4191 1066
a3a39848
AS
1067(defun vc-follow-link ()
1068 ;; If the current buffer visits a symbolic link, this function makes it
1069 ;; visit the real file instead. If the real file is already visited in
1070 ;; another buffer, make that buffer current, and kill the buffer
1071 ;; that visits the link.
566f2169 1072 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
e7f5ddc2
RS
1073 (true-buffer (find-buffer-visiting truename))
1074 (this-buffer (current-buffer)))
1075 (if (eq true-buffer this-buffer)
1076 (progn
d8221951 1077 (kill-buffer this-buffer)
e7f5ddc2
RS
1078 ;; In principle, we could do something like set-visited-file-name.
1079 ;; However, it can't be exactly the same as set-visited-file-name.
1080 ;; I'm not going to work out the details right now. -- rms.
d8221951 1081 (set-buffer (find-file-noselect truename)))
e7f5ddc2
RS
1082 (set-buffer true-buffer)
1083 (kill-buffer this-buffer))))
a3a39848 1084
594722a8
ER
1085;;; install a call to the above as a find-file hook
1086(defun vc-find-file-hook ()
18c8a18e
PE
1087 ;; Recompute whether file is version controlled,
1088 ;; if user has killed the buffer and revisited.
f2ee4191 1089 (cond
b499b50b 1090 ((and (not vc-ignore-vc-files) buffer-file-name)
f2ee4191
RS
1091 (vc-file-clearprops buffer-file-name)
1092 (cond
e1c0c2d1 1093 ((vc-backend buffer-file-name)
f2ee4191
RS
1094 (vc-mode-line buffer-file-name)
1095 (cond ((not vc-make-backup-files)
1096 ;; Use this variable, not make-backup-files,
1097 ;; because this is for things that depend on the file name.
1098 (make-local-variable 'backup-inhibited)
45fc7cc3
RS
1099 (setq backup-inhibited t))))
1100 ((let* ((link (file-symlink-p buffer-file-name))
227d2bed 1101 (link-type (and link (vc-backend (file-chase-links link)))))
45fc7cc3 1102 (if link-type
b8063212
AS
1103 (cond ((eq vc-follow-symlinks nil)
1104 (message
1105 "Warning: symbolic link to %s-controlled source file" link-type))
566f2169
RS
1106 ((or (not (eq vc-follow-symlinks 'ask))
1107 ;; If we already visited this file by following
1108 ;; the link, don't ask again if we try to visit
1109 ;; it again. GUD does that, and repeated questions
1110 ;; are painful.
1111 (get-file-buffer
1112 (abbreviate-file-name (file-chase-links buffer-file-name))))
1113
1114 (vc-follow-link)
1115 (message "Followed link to %s" buffer-file-name)
1116 (vc-find-file-hook))
1117 (t
b8063212
AS
1118 (if (yes-or-no-p (format
1119 "Symbolic link to %s-controlled source file; follow link? " link-type))
a3a39848 1120 (progn (vc-follow-link)
b8063212
AS
1121 (message "Followed link to %s" buffer-file-name)
1122 (vc-find-file-hook))
1123 (message
1124 "Warning: editing through the link bypasses version control")
566f2169 1125 ))))))))))
594722a8 1126
6379911c 1127(add-hook 'find-file-hooks 'vc-find-file-hook)
594722a8
ER
1128
1129;;; more hooks, this time for file-not-found
1130(defun vc-file-not-found-hook ()
1131 "When file is not found, try to check it out from RCS or SCCS.
1132Returns t if checkout was successful, nil otherwise."
5f42a1d4
RS
1133 ;; When a file does not exist, ignore cached info about it
1134 ;; from a previous visit.
1135 (vc-file-clearprops buffer-file-name)
b499b50b
DL
1136 (if (and (not vc-ignore-vc-files)
1137 (vc-backend buffer-file-name))
624c0e9d 1138 (save-excursion
594722a8 1139 (require 'vc)
02d383eb 1140 (setq default-directory (file-name-directory (buffer-file-name)))
594722a8
ER
1141 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
1142
6379911c 1143(add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
594722a8 1144
f2ee4191
RS
1145;; Discard info about a file when we kill its buffer.
1146(defun vc-kill-buffer-hook ()
1147 (if (stringp (buffer-file-name))
1148 (progn
1149 (vc-file-clearprops (buffer-file-name))
1150 (kill-local-variable 'vc-buffer-backend))))
1151
1152;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
1153
594722a8 1154;;; Now arrange for bindings and autoloading of the main package.
7bc2b98b
ER
1155;;; Bindings for this have to go in the global map, as we'll often
1156;;; want to call them from random buffers.
594722a8
ER
1157
1158(setq vc-prefix-map (lookup-key global-map "\C-xv"))
1159(if (not (keymapp vc-prefix-map))
1160 (progn
1161 (setq vc-prefix-map (make-sparse-keymap))
1162 (define-key global-map "\C-xv" vc-prefix-map)
1163 (define-key vc-prefix-map "a" 'vc-update-change-log)
1164 (define-key vc-prefix-map "c" 'vc-cancel-version)
18c8a18e 1165 (define-key vc-prefix-map "d" 'vc-directory)
14aa11f4 1166 (define-key vc-prefix-map "g" 'vc-annotate)
594722a8
ER
1167 (define-key vc-prefix-map "h" 'vc-insert-headers)
1168 (define-key vc-prefix-map "i" 'vc-register)
1169 (define-key vc-prefix-map "l" 'vc-print-log)
e27827dd 1170 (define-key vc-prefix-map "m" 'vc-merge)
594722a8
ER
1171 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
1172 (define-key vc-prefix-map "s" 'vc-create-snapshot)
1173 (define-key vc-prefix-map "u" 'vc-revert-buffer)
1174 (define-key vc-prefix-map "v" 'vc-next-action)
18c8a18e 1175 (define-key vc-prefix-map "=" 'vc-diff)
624c0e9d
RS
1176 (define-key vc-prefix-map "~" 'vc-version-other-window)))
1177
b662fbb8
RM
1178(if (not (boundp 'vc-menu-map))
1179 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
1180 ;; vc-menu-map.
1181 ()
1182 ;;(define-key vc-menu-map [show-files]
1183 ;; '("Show Files under VC" . (vc-directory t)))
9bef02e1
RS
1184 (define-key vc-menu-map [vc-retrieve-snapshot]
1185 '("Retrieve Snapshot" . vc-retrieve-snapshot))
1186 (define-key vc-menu-map [vc-create-snapshot]
1187 '("Create Snapshot" . vc-create-snapshot))
b64ea387 1188 (define-key vc-menu-map [vc-directory] '("VC Directory Listing" . vc-directory))
b662fbb8 1189 (define-key vc-menu-map [separator1] '("----"))
14aa11f4 1190 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
b662fbb8
RM
1191 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
1192 (define-key vc-menu-map [vc-version-other-window]
1193 '("Show Other Version" . vc-version-other-window))
1194 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
1195 (define-key vc-menu-map [vc-update-change-log]
1196 '("Update ChangeLog" . vc-update-change-log))
1197 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
1198 (define-key vc-menu-map [separator2] '("----"))
1199 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
1200 (define-key vc-menu-map [vc-revert-buffer]
1201 '("Revert to Last Version" . vc-revert-buffer))
1202 (define-key vc-menu-map [vc-insert-header]
1203 '("Insert Header" . vc-insert-headers))
9314395d 1204 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
738efc8e
KH
1205 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
1206
b64ea387
DL
1207;;; These are not correct and it's not currently clear how doing it
1208;;; better (with more complicated expressions) might slow things down
1209;;; on older systems.
1210
1211;;;(put 'vc-rename-file 'menu-enable 'vc-mode)
1212;;;(put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
1213;;;(put 'vc-version-other-window 'menu-enable 'vc-mode)
1214;;;(put 'vc-diff 'menu-enable 'vc-mode)
1215;;;(put 'vc-update-change-log 'menu-enable
1216;;; '(eq (vc-buffer-backend) 'RCS))
1217;;;(put 'vc-print-log 'menu-enable 'vc-mode)
1218;;;(put 'vc-cancel-version 'menu-enable 'vc-mode)
1219;;;(put 'vc-revert-buffer 'menu-enable 'vc-mode)
1220;;;(put 'vc-insert-headers 'menu-enable 'vc-mode)
1221;;;(put 'vc-next-action 'menu-enable 'vc-mode)
1222;;;(put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
594722a8 1223
594722a8
ER
1224(provide 'vc-hooks)
1225
1226;;; vc-hooks.el ends here