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