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