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