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