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