(DONT_DEFINE_SIGNAL): Macro deleted.
[bpt/emacs.git] / lisp / vc.el
CommitLineData
594722a8
ER
1;;; vc.el --- drive a version-control system from within Emacs
2
d733c5ec 3;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
594722a8
ER
4
5;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
4561b08b 6;; Maintainer: eggert@twinsun.com
1892807a 7;; Version: 5.5
594722a8
ER
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25;;; Commentary:
26
1a2f456b
ER
27;; This mode is fully documented in the Emacs user's manual.
28;;
594722a8
ER
29;; This was designed and implemented by Eric Raymond <esr@snark.thyrsus.com>.
30;; Paul Eggert <eggert@twinsun.com>, Sebastian Kremer <sk@thp.uni-koeln.de>,
31;; and Richard Stallman contributed valuable criticism, support, and testing.
32;;
33;; Supported version-control systems presently include SCCS and RCS;
d0b81713
RS
34;; the RCS lock-stealing code doesn't work right unless you use RCS 5.6.2
35;; or newer. Currently (January 1994) that is only a beta test release.
594722a8
ER
36;;
37;; The RCS code assumes strict locking. You can support the RCS -x option
38;; by adding pairs to the vc-master-templates list.
39;;
40;; Proper function of the SCCS diff commands requires the shellscript vcdiff
41;; to be installed somewhere on Emacs's path for executables.
42;;
1a2f456b 43;; If your site uses the ChangeLog convention supported by Emacs, the
594be62e 44;; function vc-comment-to-change-log should prove a useful checkin hook.
1a2f456b 45;;
594722a8 46;; This code depends on call-process passing back the subprocess exit
e1f297e6 47;; status. Thus, you need Emacs 18.58 or later to run it. For the
6ed5075c 48;; vc-directory command to work properly as documented, you need 19.
7ef84cf9 49;; You also need Emacs 19's ring.el.
594722a8
ER
50;;
51;; The vc code maintains some internal state in order to reduce expensive
52;; version-control operations to a minimum. Some names are only computed
34291cd2 53;; once. If you perform version control operations with RCS/SCCS/CVS while
594722a8
ER
54;; vc's back is turned, or move/rename master files while vc is running,
55;; vc may get seriously confused. Don't do these things!
56;;
57;; Developer's notes on some concurrency issues are included at the end of
58;; the file.
59
60;;; Code:
61
62(require 'vc-hooks)
8c0aaf40 63(require 'ring)
5c6f8be0 64(eval-when-compile (require 'dired)) ; for dired-map-over-marks macro
8c0aaf40
ER
65
66(if (not (assoc 'vc-parent-buffer minor-mode-alist))
67 (setq minor-mode-alist
68 (cons '(vc-parent-buffer vc-parent-buffer-name)
69 minor-mode-alist)))
594722a8
ER
70
71;; General customization
72
73(defvar vc-default-back-end nil
74 "*Back-end actually used by this interface; may be SCCS or RCS.
75The value is only computed when needed to avoid an expensive search.")
594722a8 76(defvar vc-suppress-confirm nil
34291cd2 77 "*If non-nil, treat user as expert; suppress yes-no prompts on some things.")
594722a8
ER
78(defvar vc-keep-workfiles t
79 "*If non-nil, don't delete working files after registering changes.")
80(defvar vc-initial-comment nil
81 "*Prompt for initial comment when a file is registered.")
82(defvar vc-command-messages nil
83 "*Display run messages from back-end commands.")
84(defvar vc-mistrust-permissions 'file-symlink-p
85 "*Don't assume that permissions and ownership track version-control status.")
666a0ebb
RM
86(defvar vc-checkin-switches nil
87 "*Extra switches passed to the checkin program by \\[vc-checkin].")
282aa6a2
RS
88(defvar vc-path
89 (if (file-exists-p "/usr/sccs")
90 '("/usr/sccs") nil)
91 "*List of extra directories to search for version control commands.")
666a0ebb 92
8c0aaf40
ER
93(defconst vc-maximum-comment-ring-size 32
94 "Maximum number of saved comments in the comment ring.")
95
2e810285
RS
96;;; This is duplicated in diff.el.
97(defvar diff-switches "-c"
98 "*A string or list of strings specifying switches to be be passed to diff.")
99
67242a23
RM
100;;;###autoload
101(defvar vc-checkin-hook nil
34291cd2 102 "*List of functions called after a checkin is done. See `run-hooks'.")
67242a23 103
594722a8
ER
104;; Header-insertion hair
105
106(defvar vc-header-alist
107 '((SCCS "\%W\%") (RCS "\$Id\$"))
820bde8d 108 "*Header keywords to be inserted when `vc-insert-headers' is executed.")
ba5c3004 109(defvar vc-static-header-alist
594722a8
ER
110 '(("\\.c$" .
111 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
112 "*Associate static header string templates with file types. A \%s in the
113template is replaced with the first string associated with the file's
34291cd2 114version-control type in `vc-header-alist'.")
7b4f934d 115
594722a8
ER
116(defvar vc-comment-alist
117 '((nroff-mode ".\\\"" ""))
118 "*Special comment delimiters to be used in generating vc headers only.
119Add an entry in this list if you need to override the normal comment-start
120and comment-end variables. This will only be necessary if the mode language
121is sensitive to blank lines.")
122
bbf97570
RS
123;; Default is to be extra careful for super-user.
124(defvar vc-checkout-carefully (= (user-uid) 0)
125 "*Non-nil means be extra-careful in checkout.
126Verify that the file really is not locked
127and that its contents match what the master file says.")
128
594722a8
ER
129;; Variables the user doesn't need to know about.
130(defvar vc-log-entry-mode nil)
131(defvar vc-log-operation nil)
67242a23 132(defvar vc-log-after-operation-hook nil)
34291cd2 133(defvar vc-checkout-writable-buffer-hook 'vc-checkout-writable-buffer)
dbf87856
RS
134;; In a log entry buffer, this is a local variable
135;; that points to the buffer for which it was made
136;; (either a file, or a VC dired buffer).
1a2f456b 137(defvar vc-parent-buffer nil)
8c0aaf40 138(defvar vc-parent-buffer-name nil)
594722a8 139
db59472c
RS
140(defvar vc-log-file)
141(defvar vc-log-version)
142
594722a8
ER
143(defconst vc-name-assoc-file "VC-names")
144
8c0aaf40 145(defvar vc-dired-mode nil)
e1f297e6
ER
146(make-variable-buffer-local 'vc-dired-mode)
147
8c0aaf40
ER
148(defvar vc-comment-ring nil)
149(defvar vc-comment-ring-index nil)
150(defvar vc-last-comment-match nil)
151
594722a8
ER
152;; File property caching
153
154(defun vc-file-clearprops (file)
155 ;; clear all properties of a given file
156 (setplist (intern file vc-file-prop-obarray) nil))
157
8c0aaf40
ER
158(defun vc-clear-context ()
159 "Clear all cached file properties and the comment ring."
160 (interactive)
161 (fillarray vc-file-prop-obarray nil)
162 ;; Note: there is potential for minor lossage here if there is an open
163 ;; log buffer with a nonzero local value of vc-comment-ring-index.
164 (setq vc-comment-ring nil))
165
594722a8
ER
166;; Random helper functions
167
7ef84cf9
RS
168(defun vc-registration-error (file)
169 (if file
590cc449
RS
170 (error "File %s is not under version control" file)
171 (error "Buffer %s is not associated with a file" (buffer-name))))
7ef84cf9 172
594722a8
ER
173(defvar vc-binary-assoc nil)
174
175(defun vc-find-binary (name)
176 "Look for a command anywhere on the subprocess-command search path."
177 (or (cdr (assoc name vc-binary-assoc))
deb9ebc6
PE
178 (catch 'found
179 (mapcar
180 (function
181 (lambda (s)
182 (if s
183 (let ((full (concat s "/" name)))
184 (if (file-executable-p full)
185 (progn
186 (setq vc-binary-assoc
187 (cons (cons name full) vc-binary-assoc))
188 (throw 'found full)))))))
189 exec-path)
190 nil)))
594722a8
ER
191
192(defun vc-do-command (okstatus command file &rest flags)
193 "Execute a version-control command, notifying user and checking for errors.
194The command is successful if its exit status does not exceed OKSTATUS.
195Output from COMMAND goes to buffer *vc*. The last argument of the command is
196the master name of FILE; this is appended to an optional list of FLAGS."
197 (setq file (expand-file-name file))
198 (if vc-command-messages
02da6253 199 (message "Running %s on %s..." command file))
1a2f456b 200 (let ((obuf (current-buffer)) (camefrom (current-buffer))
594722a8
ER
201 (squeezed nil)
202 (vc-file (and file (vc-name file)))
203 status)
204 (set-buffer (get-buffer-create "*vc*"))
8c0aaf40
ER
205 (set (make-local-variable 'vc-parent-buffer) camefrom)
206 (set (make-local-variable 'vc-parent-buffer-name)
207 (concat " from " (buffer-name camefrom)))
208
594722a8 209 (erase-buffer)
315e49ed
JB
210
211 ;; This is so that command arguments typed in the *vc* buffer will
212 ;; have reasonable defaults.
213 (setq default-directory (file-name-directory file))
214
594722a8
ER
215 (mapcar
216 (function (lambda (s) (and s (setq squeezed (append squeezed (list s))))))
217 flags)
218 (if vc-file
219 (setq squeezed (append squeezed (list vc-file))))
282aa6a2 220 (let ((default-directory (file-name-directory (or file "./")))
eadcb02c
RS
221 (exec-path (if vc-path (append exec-path vc-path) exec-path))
222 ;; Add vc-path to PATH for the execution of this command.
223 (process-environment
224 (cons (concat "PATH=" (getenv "PATH")
225 ":" (mapconcat 'identity vc-path ":"))
226 process-environment)))
1a2f456b 227 (setq status (apply 'call-process command nil t nil squeezed)))
594722a8 228 (goto-char (point-max))
4805f679 229 (forward-line -1)
594722a8
ER
230 (if (or (not (integerp status)) (< okstatus status))
231 (progn
594722a8 232 (pop-to-buffer "*vc*")
594722a8 233 (goto-char (point-min))
4c2d8cf9 234 (shrink-window-if-larger-than-buffer)
02da6253
PE
235 (error "Running %s...FAILED (%s)" command
236 (if (integerp status)
237 (format "status %d" status)
238 status))
594722a8
ER
239 )
240 (if vc-command-messages
02da6253 241 (message "Running %s...OK" command))
594722a8
ER
242 )
243 (set-buffer obuf)
244 status)
245 )
246
c4ae7096
JB
247;;; Save a bit of the text around POSN in the current buffer, to help
248;;; us find the corresponding position again later. This works even
249;;; if all markers are destroyed or corrupted.
250(defun vc-position-context (posn)
251 (list posn
252 (buffer-size)
253 (buffer-substring posn
254 (min (point-max) (+ posn 100)))))
255
256;;; Return the position of CONTEXT in the current buffer, or nil if we
257;;; couldn't find it.
258(defun vc-find-position-by-context (context)
259 (let ((context-string (nth 2 context)))
260 (if (equal "" context-string)
261 (point-max)
262 (save-excursion
263 (let ((diff (- (nth 1 context) (buffer-size))))
264 (if (< diff 0) (setq diff (- diff)))
265 (goto-char (nth 0 context))
266 (if (or (search-forward context-string nil t)
267 ;; Can't use search-backward since the match may continue
268 ;; after point.
269 (progn (goto-char (- (point) diff (length context-string)))
270 ;; goto-char doesn't signal an error at
271 ;; beginning of buffer like backward-char would
272 (search-forward context-string nil t)))
273 ;; to beginning of OSTRING
274 (- (point) (length context-string))))))))
275
594722a8 276(defun vc-revert-buffer1 (&optional arg no-confirm)
7b4f934d 277 ;; Most of this was shamelessly lifted from Sebastian Kremer's rcs.el mode.
c4ae7096 278 ;; Revert buffer, try to keep point and mark where user expects them in spite
594722a8
ER
279 ;; of changes because of expanded version-control key words.
280 ;; This is quite important since otherwise typeahead won't work as expected.
281 (interactive "P")
282 (widen)
c4ae7096 283 (let ((point-context (vc-position-context (point)))
cfadef63
RS
284 ;; Use mark-marker to avoid confusion in transient-mark-mode.
285 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
286 (vc-position-context (mark-marker))))
287 ;; Make the right thing happen in transient-mark-mode.
ab877583
RM
288 (mark-active nil)
289 ;; We may want to reparse the compilation buffer after revert
290 (reparse (and (boundp 'compilation-error-list) ;compile loaded
291 (let ((curbuf (current-buffer)))
292 ;; Construct a list; each elt is nil or a buffer
293 ;; iff that buffer is a compilation output buffer
294 ;; that contains markers into the current buffer.
295 (save-excursion
7ef84cf9
RS
296 (mapcar (function
297 (lambda (buffer)
ab877583
RM
298 (set-buffer buffer)
299 (let ((errors (or
300 compilation-old-error-list
301 compilation-error-list))
302 (buffer-error-marked-p nil))
6fb6ab11 303 (while (and (consp errors)
ab877583 304 (not buffer-error-marked-p))
a1bda481 305 (and (markerp (cdr (car errors)))
e9c8e248
RM
306 (eq buffer
307 (marker-buffer
a1bda481 308 (cdr (car errors))))
e9c8e248 309 (setq buffer-error-marked-p t))
ab877583 310 (setq errors (cdr errors)))
7ef84cf9 311 (if buffer-error-marked-p buffer))))
ab877583 312 (buffer-list)))))))
7b4f934d
ER
313
314 ;; the actual revisit
594722a8 315 (revert-buffer arg no-confirm)
7b4f934d 316
ab877583
RM
317 ;; Reparse affected compilation buffers.
318 (while reparse
319 (if (car reparse)
320 (save-excursion
321 (set-buffer (car reparse))
322 (let ((compilation-last-buffer (current-buffer)) ;select buffer
323 ;; Record the position in the compilation buffer of
324 ;; the last error next-error went to.
325 (error-pos (marker-position
326 (car (car-safe compilation-error-list)))))
327 ;; Reparse the error messages as far as they were parsed before.
328 (compile-reinitialize-errors '(4) compilation-parsing-end)
329 ;; Move the pointer up to find the error we were at before
330 ;; reparsing. Now next-error should properly go to the next one.
331 (while (and compilation-error-list
27f2f10b 332 (/= error-pos (car (car compilation-error-list))))
ab877583
RM
333 (setq compilation-error-list (cdr compilation-error-list))))))
334 (setq reparse (cdr reparse)))
e1f297e6 335
7b4f934d 336 ;; Restore point and mark
c4ae7096
JB
337 (let ((new-point (vc-find-position-by-context point-context)))
338 (if new-point (goto-char new-point)))
339 (if mark-context
340 (let ((new-mark (vc-find-position-by-context mark-context)))
341 (if new-mark (set-mark new-mark))))))
342
594722a8 343
97d3f950 344(defun vc-buffer-sync (&optional not-urgent)
594722a8 345 ;; Make sure the current buffer and its working file are in sync
97d3f950 346 ;; NOT-URGENT means it is ok to continue if the user says not to save.
bbf97570 347 (if (buffer-modified-p)
97d3f950
RS
348 (if (or vc-suppress-confirm
349 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
350 (save-buffer)
351 (if not-urgent
352 nil
353 (error "Aborted")))))
354
594722a8 355
9a51ed3a 356(defun vc-workfile-unchanged-p (file &optional want-differences-if-changed)
594722a8
ER
357 ;; Has the given workfile changed since last checkout?
358 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
359 (lastmod (nth 5 (file-attributes file))))
9a51ed3a
PE
360 (or (equal checkout-time lastmod)
361 (and (or (not checkout-time) want-differences-if-changed)
362 (let ((unchanged (zerop (vc-backend-diff file nil nil
363 (not want-differences-if-changed)))))
364 ;; 0 stands for an unknown time; it can't match any mod time.
365 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
366 unchanged)))))
594722a8 367
e1f297e6
ER
368(defun vc-next-action-on-file (file verbose &optional comment)
369 ;;; If comment is specified, it will be used as an admin or checkin comment.
370 (let (owner version (vc-file (vc-name file)))
371 (cond
372
373 ;; if there is no master file corresponding, create one
374 ((not vc-file)
b965445f 375 (vc-register verbose comment))
e1f297e6
ER
376
377 ;; if there is no lock on the file, assert one and get it
378 ((not (setq owner (vc-locking-user file)))
bbf97570 379 (if (and vc-checkout-carefully
9a51ed3a 380 (not (vc-workfile-unchanged-p file t)))
bbf97570
RS
381 (if (save-window-excursion
382 (pop-to-buffer "*vc*")
383 (goto-char (point-min))
384 (insert-string (format "Changes to %s since last lock:\n\n"
385 file))
386 (not (beep))
387 (yes-or-no-p
388 (concat "File has unlocked changes, "
389 "claim lock retaining changes? ")))
390 (progn (vc-backend-steal file)
391 (vc-mode-line file))
392 (if (not (yes-or-no-p "Revert to checked-in version, instead? "))
393 (error "Checkout aborted.")
394 (vc-revert-buffer1 t t)
395 (vc-checkout-writable-buffer file))
396 )
397 (vc-checkout-writable-buffer file)))
e1f297e6
ER
398
399 ;; a checked-out version exists, but the user may not own the lock
400 ((not (string-equal owner (user-login-name)))
401 (if comment
590cc449 402 (error "Sorry, you can't steal the lock on %s this way" file))
e1f297e6
ER
403 (vc-steal-lock
404 file
405 (and verbose (read-string "Version to steal: "))
406 owner))
407
408 ;; OK, user owns the lock on the file
8c0aaf40 409 (t
e1f297e6
ER
410 (find-file file)
411
412 ;; give luser a chance to save before checking in.
413 (vc-buffer-sync)
414
415 ;; Revert if file is unchanged and buffer is too.
416 ;; If buffer is modified, that means the user just said no
417 ;; to saving it; in that case, don't revert,
418 ;; because the user might intend to save
419 ;; after finishing the log entry.
420 (if (and (vc-workfile-unchanged-p file)
421 (not (buffer-modified-p)))
422 (progn
423 (vc-backend-revert file)
424 ;; DO NOT revert the file without asking the user!
425 (vc-resynch-window file t nil))
426
427 ;; user may want to set nonstandard parameters
428 (if verbose
429 (setq version (read-string "New version level: ")))
430
431 ;; OK, let's do the checkin
432 (vc-checkin file version comment)
8c0aaf40 433 )))))
e1f297e6
ER
434
435(defun vc-next-action-dired (file rev comment)
436 ;; We've accepted a log comment, now do a vc-next-action using it on all
437 ;; marked files.
438 (set-buffer vc-parent-buffer)
439 (dired-map-over-marks
440 (save-window-excursion
8c0aaf40
ER
441 (let ((file (dired-get-filename)))
442 (message "Processing %s..." file)
443 (vc-next-action-on-file file nil comment)
444 (message "Processing %s...done" file)))
445 nil t)
e1f297e6
ER
446 )
447
637a8ae9 448;; Here's the major entry point.
594722a8 449
637a8ae9 450;;;###autoload
594722a8
ER
451(defun vc-next-action (verbose)
452 "Do the next logical checkin or checkout operation on the current file.
453 If the file is not already registered, this registers it for version
34291cd2 454control and then retrieves a writable, locked copy for editing.
594722a8 455 If the file is registered and not locked by anyone, this checks out
34291cd2 456a writable and locked file ready for editing.
594722a8
ER
457 If the file is checked out and locked by the calling user, this
458first checks to see if the file has changed since checkout. If not,
459it performs a revert.
e1f297e6
ER
460 If the file has been changed, this pops up a buffer for entry
461of a log message; when the message has been entered, it checks in the
594722a8 462resulting changes along with the log message as change commentary. If
34291cd2 463the variable `vc-keep-workfiles' is non-nil (which is its default), a
594722a8
ER
464read-only copy of the changed file is left in place afterwards.
465 If the file is registered and locked by someone else, you are given
e1f297e6
ER
466the option to steal the lock.
467 If you call this from within a VC dired buffer with no files marked,
468it will operate on the file in the current line.
469 If you call this from within a VC dired buffer, and one or more
470files are marked, it will accept a log message and then operate on
471each one. The log message will be used as a comment for any register
472or checkin operations, but ignored when doing checkouts. Attempted
bbf97570
RS
473lock steals will raise an error.
474
475 For checkin, a prefix argument lets you specify the version number to use."
594722a8 476 (interactive "P")
8c0aaf40
ER
477 (catch 'nogo
478 (if vc-dired-mode
479 (let ((files (dired-get-marked-files)))
480 (if (= (length files) 1)
cdaf7a1a 481 (find-file-other-window (car files))
8c0aaf40
ER
482 (vc-start-entry nil nil nil
483 "Enter a change comment for the marked files."
484 'vc-next-action-dired)
9a47d40b 485 (throw 'nogo nil))))
e1f297e6 486 (while vc-parent-buffer
1a2f456b 487 (pop-to-buffer vc-parent-buffer))
e1f297e6
ER
488 (if buffer-file-name
489 (vc-next-action-on-file buffer-file-name verbose)
7ef84cf9 490 (vc-registration-error nil))))
594722a8
ER
491
492;;; These functions help the vc-next-action entry point
493
34291cd2
RS
494(defun vc-checkout-writable-buffer (&optional file)
495 "Retrieve a writable copy of the latest version of the current buffer's file."
8c0aaf40 496 (vc-checkout (or file (buffer-file-name)) t)
02da6253
PE
497 )
498
637a8ae9 499;;;###autoload
e1f297e6 500(defun vc-register (&optional override comment)
594722a8
ER
501 "Register the current file into your version-control system."
502 (interactive "P")
503 (if (vc-name buffer-file-name)
590cc449 504 (error "This file is already registered"))
02da6253
PE
505 ;; Watch out for new buffers of size 0: the corresponding file
506 ;; does not exist yet, even though buffer-modified-p is nil.
507 (if (and (not (buffer-modified-p))
508 (zerop (buffer-size))
509 (not (file-exists-p buffer-file-name)))
510 (set-buffer-modified-p t))
594722a8
ER
511 (vc-buffer-sync)
512 (vc-admin
513 buffer-file-name
e1f297e6
ER
514 (and override
515 (read-string
516 (format "Initial version level for %s: " buffer-file-name))))
594722a8
ER
517 )
518
624b4662 519(defun vc-resynch-window (file &optional keep noquery)
594722a8
ER
520 ;; If the given file is in the current buffer,
521 ;; either revert on it so we see expanded keyworks,
522 ;; or unvisit it (depending on vc-keep-workfiles)
624b4662
RS
523 ;; NOQUERY if non-nil inhibits confirmation for reverting.
524 ;; NOQUERY should be t *only* if it is known the only difference
525 ;; between the buffer and the file is due to RCS rather than user editing!
594722a8
ER
526 (and (string= buffer-file-name file)
527 (if keep
528 (progn
1ab31687 529 (vc-revert-buffer1 t noquery)
594722a8
ER
530 (vc-mode-line buffer-file-name))
531 (progn
532 (delete-window)
533 (kill-buffer (current-buffer))))))
534
b965445f 535(defun vc-start-entry (file rev comment msg action &optional after-hook)
e1f297e6
ER
536 ;; Accept a comment for an operation on FILE revision REV. If COMMENT
537 ;; is nil, pop up a VC-log buffer, emit MSG, and set the
538 ;; action on close to ACTION; otherwise, do action immediately.
cdaf7a1a 539 ;; Remember the file's buffer in vc-parent-buffer (current one if no file).
b965445f 540 ;; AFTER-HOOK specifies the local value for vc-log-operation-hook.
e1f297e6
ER
541 (let ((parent (if file (find-file-noselect file) (current-buffer))))
542 (if comment
543 (set-buffer (get-buffer-create "*VC-log*"))
544 (pop-to-buffer (get-buffer-create "*VC-log*")))
8c0aaf40
ER
545 (set (make-local-variable 'vc-parent-buffer) parent)
546 (set (make-local-variable 'vc-parent-buffer-name)
547 (concat " from " (buffer-name vc-parent-buffer)))
cf50f717 548 (vc-mode-line (or file " (no file)"))
e1f297e6 549 (vc-log-mode)
b965445f
RS
550 (make-local-variable 'vc-log-after-operation-hook)
551 (if after-hook
552 (setq vc-log-after-operation-hook after-hook))
e1f297e6
ER
553 (setq vc-log-operation action)
554 (setq vc-log-file file)
555 (setq vc-log-version rev)
556 (if comment
557 (progn
558 (erase-buffer)
8c0aaf40
ER
559 (if (eq comment t)
560 (vc-finish-logentry t)
561 (insert comment)
562 (vc-finish-logentry nil)))
e1f297e6 563 (message "%s Type C-c C-c when done." msg))))
594722a8 564
e1f297e6 565(defun vc-admin (file rev &optional comment)
624b4662 566 "Check a file into your version-control system.
594722a8 567FILE is the unmodified name of the file. REV should be the base version
e1f297e6 568level to check it in under. COMMENT, if specified, is the checkin comment."
b965445f
RS
569 (vc-start-entry file rev
570 (or comment (not vc-initial-comment))
571 "Enter initial comment." 'vc-backend-admin
6bcb1d4e 572 nil))
e1f297e6 573
34291cd2 574(defun vc-checkout (file &optional writable)
e1f297e6
ER
575 "Retrieve a copy of the latest version of the given file."
576 ;; If ftp is on this system and the name matches the ange-ftp format
577 ;; for a remote file, the user is trying something that won't work.
578 (if (and (string-match "^/[^/:]+:" file) (vc-find-binary "ftp"))
579 (error "Sorry, you can't check out files over FTP"))
34291cd2 580 (vc-backend-checkout file writable)
e1f297e6
ER
581 (if (string-equal file buffer-file-name)
582 (vc-resynch-window file t t))
583 )
594722a8
ER
584
585(defun vc-steal-lock (file rev &optional owner)
586 "Steal the lock on the current workfile."
587 (interactive)
588 (if (not owner)
589 (setq owner (vc-locking-user file)))
820bde8d 590 (if (not (y-or-n-p (format "Take the lock on %s:%s from %s? " file rev owner)))
590cc449 591 (error "Steal cancelled"))
ad014629
RS
592 (pop-to-buffer (get-buffer-create "*VC-mail*"))
593 (setq default-directory (expand-file-name "~/"))
594 (auto-save-mode auto-save-default)
595 (mail-mode)
596 (erase-buffer)
00b3f57b 597 (mail-setup owner (format "%s:%s" file rev) nil nil nil
ad014629
RS
598 (list (list 'vc-finish-steal file rev)))
599 (goto-char (point-max))
594722a8 600 (insert
ad014629 601 (format "I stole the lock on %s:%s, " file rev)
594722a8 602 (current-time-string)
ad014629
RS
603 ".\n")
604 (message "Please explain why you stole the lock. Type C-c C-c when done."))
594722a8 605
ad014629 606;; This is called when the notification has been sent.
594722a8 607(defun vc-finish-steal (file version)
594722a8 608 (vc-backend-steal file version)
624b4662 609 (vc-resynch-window file t t))
594722a8 610
594722a8
ER
611(defun vc-checkin (file &optional rev comment)
612 "Check in the file specified by FILE.
613The optional argument REV may be a string specifying the new version level
67242a23 614\(if nil increment the current level). The file is either retained with write
34291cd2 615permissions zeroed, or deleted (according to the value of `vc-keep-workfiles').
594722a8
ER
616COMMENT is a comment string; if omitted, a buffer is
617popped up to accept a comment."
61446fe7 618 (vc-start-entry file rev comment
b965445f
RS
619 "Enter a change comment." 'vc-backend-checkin
620 'vc-checkin-hook))
594722a8 621
1a2f456b
ER
622;;; Here is a checkin hook that may prove useful to sites using the
623;;; ChangeLog facility supported by Emacs.
3b4dd9a9
RM
624(defun vc-comment-to-change-log (&optional whoami file-name)
625 "Enter last VC comment into change log file for current buffer's file.
626Optional arg (interactive prefix) non-nil means prompt for user name and site.
627Second arg is file name of change log. \
628If nil, uses `change-log-default-name'."
43cea1ab
RM
629 (interactive (if current-prefix-arg
630 (list current-prefix-arg
631 (prompt-for-change-log-name))))
41208291
KH
632 ;; Make sure the defvar for add-log-current-defun-function has been executed
633 ;; before binding it.
634 (require 'add-log)
3b4dd9a9
RM
635 (let (;; Extract the comment first so we get any error before doing anything.
636 (comment (ring-ref vc-comment-ring 0))
43cea1ab 637 ;; Don't let add-change-log-entry insert a defun name.
3b4dd9a9
RM
638 (add-log-current-defun-function 'ignore)
639 end)
640 ;; Call add-log to do half the work.
43cea1ab 641 (add-change-log-entry whoami file-name t t)
3b4dd9a9
RM
642 ;; Insert the VC comment, leaving point before it.
643 (setq end (save-excursion (insert comment) (point-marker)))
644 (if (looking-at "\\s *\\s(")
645 ;; It starts with an open-paren, as in "(foo): Frobbed."
43cea1ab 646 ;; So remove the ": " add-log inserted.
3b4dd9a9
RM
647 (delete-char -2))
648 ;; Canonicalize the white space between the file name and comment.
649 (just-one-space)
650 ;; Indent rest of the text the same way add-log indented the first line.
651 (let ((indentation (current-indentation)))
652 (save-excursion
653 (while (< (point) end)
654 (forward-line 1)
655 (indent-to indentation))
c124b1b4 656 (setq end (point))))
3b4dd9a9
RM
657 ;; Fill the inserted text, preserving open-parens at bol.
658 (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
659 (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
43cea1ab 660 (beginning-of-line)
c124b1b4
RM
661 (fill-region (point) end))
662 ;; Canonicalize the white space at the end of the entry so it is
663 ;; separated from the next entry by a single blank line.
664 (skip-syntax-forward " " end)
665 (delete-char (- (skip-syntax-backward " ")))
666 (or (eobp) (looking-at "\n\n")
667 (insert "\n"))))
3b4dd9a9 668
1a2f456b 669
8c0aaf40 670(defun vc-finish-logentry (&optional nocomment)
594722a8
ER
671 "Complete the operation implied by the current log entry."
672 (interactive)
8c0aaf40
ER
673 ;; Check and record the comment, if any.
674 (if (not nocomment)
675 (progn
676 (goto-char (point-max))
677 (if (not (bolp))
678 (newline))
679 ;; Comment too long?
680 (vc-backend-logentry-check vc-log-file)
681 ;; Record the comment in the comment ring
682 (if (null vc-comment-ring)
683 (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
684 (ring-insert vc-comment-ring (buffer-string))
685 ))
b2396d1f 686 ;; Sync parent buffer in case the user modified it while editing the comment.
cdaf7a1a 687 ;; But not if it is a vc-dired buffer.
b2396d1f 688 (save-excursion
cdaf7a1a
RS
689 (set-buffer vc-parent-buffer)
690 (or vc-dired-mode
691 (vc-buffer-sync)))
594722a8
ER
692 ;; OK, do it to it
693 (if vc-log-operation
e1f297e6
ER
694 (save-excursion
695 (funcall vc-log-operation
696 vc-log-file
697 vc-log-version
698 (buffer-string)))
590cc449 699 (error "No log operation is pending"))
e2bef5c3
RS
700 ;; save the vc-log-after-operation-hook of log buffer
701 (let ((after-hook vc-log-after-operation-hook))
702 ;; Return to "parent" buffer of this checkin and remove checkin window
703 (pop-to-buffer vc-parent-buffer)
704 (let ((logbuf (get-buffer "*VC-log*")))
705 (delete-windows-on logbuf)
706 (kill-buffer logbuf))
707 ;; Now make sure we see the expanded headers
708 (if buffer-file-name
e1f297e6 709 (vc-resynch-window buffer-file-name vc-keep-workfiles t))
e2bef5c3 710 (run-hooks after-hook)))
594722a8
ER
711
712;; Code for access to the comment ring
713
8c0aaf40
ER
714(defun vc-previous-comment (arg)
715 "Cycle backwards through comment history."
716 (interactive "*p")
717 (let ((len (ring-length vc-comment-ring)))
718 (cond ((<= len 0)
719 (message "Empty comment ring")
720 (ding))
721 (t
722 (erase-buffer)
723 ;; Initialize the index on the first use of this command
724 ;; so that the first M-p gets index 0, and the first M-n gets
725 ;; index -1.
726 (if (null vc-comment-ring-index)
727 (setq vc-comment-ring-index
728 (if (> arg 0) -1
729 (if (< arg 0) 1 0))))
730 (setq vc-comment-ring-index
c6bfcd12 731 (mod (+ vc-comment-ring-index arg) len))
8c0aaf40
ER
732 (message "%d" (1+ vc-comment-ring-index))
733 (insert (ring-ref vc-comment-ring vc-comment-ring-index))))))
734
735(defun vc-next-comment (arg)
736 "Cycle forwards through comment history."
737 (interactive "*p")
738 (vc-previous-comment (- arg)))
739
740(defun vc-comment-search-reverse (str)
741 "Searches backwards through comment history for substring match."
742 (interactive "sComment substring: ")
743 (if (string= str "")
744 (setq str vc-last-comment-match)
745 (setq vc-last-comment-match str))
746 (if (null vc-comment-ring-index)
747 (setq vc-comment-ring-index -1))
748 (let ((str (regexp-quote str))
749 (len (ring-length vc-comment-ring))
750 (n (1+ vc-comment-ring-index)))
751 (while (and (< n len) (not (string-match str (ring-ref vc-comment-ring n))))
752 (setq n (+ n 1)))
753 (cond ((< n len)
754 (vc-previous-comment (- n vc-comment-ring-index)))
755 (t (error "Not found")))))
756
757(defun vc-comment-search-forward (str)
758 "Searches forwards through comment history for substring match."
759 (interactive "sComment substring: ")
760 (if (string= str "")
761 (setq str vc-last-comment-match)
762 (setq vc-last-comment-match str))
763 (if (null vc-comment-ring-index)
764 (setq vc-comment-ring-index 0))
765 (let ((str (regexp-quote str))
766 (len (ring-length vc-comment-ring))
767 (n vc-comment-ring-index))
768 (while (and (>= n 0) (not (string-match str (ring-ref vc-comment-ring n))))
769 (setq n (- n 1)))
770 (cond ((>= n 0)
771 (vc-next-comment (- n vc-comment-ring-index)))
772 (t (error "Not found")))))
594722a8
ER
773
774;; Additional entry points for examining version histories
775
637a8ae9 776;;;###autoload
97d3f950 777(defun vc-diff (historic &optional not-urgent)
e8ee1ccf
RS
778 "Display diffs between file versions.
779Normally this compares the current file and buffer with the most recent
780checked in version of that file. This uses no arguments.
781With a prefix argument, it reads the file name to use
782and two version designators specifying which versions to compare."
594722a8 783 (interactive "P")
e1f297e6
ER
784 (if vc-dired-mode
785 (set-buffer (find-file-noselect (dired-get-filename))))
8ac8c82f 786 (while vc-parent-buffer
1a2f456b 787 (pop-to-buffer vc-parent-buffer))
594722a8
ER
788 (if historic
789 (call-interactively 'vc-version-diff)
8c0aaf40 790 (if (or (null buffer-file-name) (null (vc-name buffer-file-name)))
064726ac
JB
791 (error
792 "There is no version-control master associated with this buffer"))
02da6253 793 (let ((file buffer-file-name)
594722a8 794 unchanged)
7ef84cf9
RS
795 (or (and file (vc-name file))
796 (vc-registration-error file))
97d3f950 797 (vc-buffer-sync not-urgent)
594722a8
ER
798 (setq unchanged (vc-workfile-unchanged-p buffer-file-name))
799 (if unchanged
02da6253 800 (message "No changes to %s since latest version." file)
9a51ed3a 801 (vc-backend-diff file)
8c0aaf40
ER
802 ;; Ideally, we'd like at this point to parse the diff so that
803 ;; the buffer effectively goes into compilation mode and we
804 ;; can visit the old and new change locations via next-error.
805 ;; Unfortunately, this is just too painful to do. The basic
806 ;; problem is that the `old' file doesn't exist to be
807 ;; visited. This plays hell with numerous assumptions in
808 ;; the diff.el and compile.el machinery.
809 (pop-to-buffer "*vc*")
b246f571
RS
810 (pop-to-buffer "*vc*")
811 (if (= 0 (buffer-size))
812 (progn
813 (setq unchanged t)
814 (message "No changes to %s since latest version." file))
4c2d8cf9 815 (goto-char (point-min))
064726ac
JB
816 (shrink-window-if-larger-than-buffer)))
817 (not unchanged))))
594722a8
ER
818
819(defun vc-version-diff (file rel1 rel2)
820 "For FILE, report diffs between two stored versions REL1 and REL2 of it.
821If FILE is a directory, generate diffs between versions for all registered
822files in or below it."
3234e2a3 823 (interactive "FFile or directory to diff: \nsOlder version: \nsNewer version: ")
594722a8
ER
824 (if (string-equal rel1 "") (setq rel1 nil))
825 (if (string-equal rel2 "") (setq rel2 nil))
826 (if (file-directory-p file)
1a2f456b 827 (let ((camefrom (current-buffer)))
594722a8 828 (set-buffer (get-buffer-create "*vc-status*"))
8c0aaf40
ER
829 (set (make-local-variable 'vc-parent-buffer) camefrom)
830 (set (make-local-variable 'vc-parent-buffer-name)
831 (concat " from " (buffer-name camefrom)))
594722a8 832 (erase-buffer)
3234e2a3
ER
833 (insert "Diffs between "
834 (or rel1 "last version checked in")
835 " and "
836 (or rel2 "current workfile(s)")
837 ":\n\n")
594722a8 838 (set-buffer (get-buffer-create "*vc*"))
e1f297e6 839 (cd file)
594722a8
ER
840 (vc-file-tree-walk
841 (function (lambda (f)
a9a59766 842 (message "Looking at %s" f)
594722a8 843 (and
3234e2a3
ER
844 (not (file-directory-p f))
845 (vc-registered f)
02da6253
PE
846 (vc-backend-diff f rel1 rel2)
847 (append-to-buffer "*vc-status*" (point-min) (point-max)))
848 )))
594722a8
ER
849 (pop-to-buffer "*vc-status*")
850 (insert "\nEnd of diffs.\n")
851 (goto-char (point-min))
852 (set-buffer-modified-p nil)
853 )
36bed8bc
RS
854 (if (zerop (vc-backend-diff file rel1 rel2))
855 (message "No changes to %s between %s and %s." file rel1 rel2)
24f1977f 856 (pop-to-buffer "*vc*"))))
594722a8 857
f1818994
PE
858;;;###autoload
859(defun vc-version-other-window (rev)
860 "Visit version REV of the current buffer in another window.
861If the current buffer is named `F', the version is named `F.~REV~'.
862If `F.~REV~' already exists, it is used instead of being re-created."
863 (interactive "sVersion to visit (default is latest version): ")
864 (if vc-dired-mode
865 (set-buffer (find-file-noselect (dired-get-filename))))
866 (while vc-parent-buffer
867 (pop-to-buffer vc-parent-buffer))
868 (if (and buffer-file-name (vc-name buffer-file-name))
869 (let* ((version (if (string-equal rev "")
870 (vc-latest-version buffer-file-name)
871 rev))
872 (filename (concat buffer-file-name ".~" version "~")))
873 (or (file-exists-p filename)
874 (vc-backend-checkout buffer-file-name nil version filename))
875 (find-file-other-window filename))
876 (vc-registration-error buffer-file-name)))
877
594722a8
ER
878;; Header-insertion code
879
637a8ae9 880;;;###autoload
594722a8
ER
881(defun vc-insert-headers ()
882 "Insert headers in a file for use with your version-control system.
883Headers desired are inserted at the start of the buffer, and are pulled from
34291cd2 884the variable `vc-header-alist'."
594722a8 885 (interactive)
e1f297e6
ER
886 (if vc-dired-mode
887 (find-file-other-window (dired-get-filename)))
8ac8c82f 888 (while vc-parent-buffer
1a2f456b 889 (pop-to-buffer vc-parent-buffer))
594722a8
ER
890 (save-excursion
891 (save-restriction
892 (widen)
893 (if (or (not (vc-check-headers))
820bde8d 894 (y-or-n-p "Version headers already exist. Insert another set? "))
594722a8
ER
895 (progn
896 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
897 (comment-start-vc (or (car delims) comment-start "#"))
898 (comment-end-vc (or (car (cdr delims)) comment-end ""))
899 (hdstrings (cdr (assoc (vc-backend-deduce (buffer-file-name)) vc-header-alist))))
900 (mapcar (function (lambda (s)
901 (insert comment-start-vc "\t" s "\t"
902 comment-end-vc "\n")))
903 hdstrings)
904 (if vc-static-header-alist
905 (mapcar (function (lambda (f)
906 (if (string-match (car f) buffer-file-name)
907 (insert (format (cdr f) (car hdstrings))))))
908 vc-static-header-alist))
909 )
910 )))))
911
e1f297e6
ER
912;; The VC directory submode. Coopt Dired for this.
913;; All VC commands get mapped into logical equivalents.
914
ee7e9c88
RS
915(defvar vc-dired-prefix-map (make-sparse-keymap))
916(define-key vc-dired-prefix-map "\C-xv" vc-prefix-map)
917
1434b7aa
RS
918(or (not (boundp 'minor-mode-map-alist))
919 (assq 'vc-dired-mode minor-mode-map-alist)
e1f297e6 920 (setq minor-mode-map-alist
4f6e4ad6 921 (cons (cons 'vc-dired-mode vc-dired-prefix-map)
ee7e9c88 922 minor-mode-map-alist)))
e1f297e6
ER
923
924(defun vc-dired-mode ()
925 "The augmented Dired minor mode used in VC directory buffers.
926All Dired commands operate normally. Users currently locking listed files
ee7e9c88 927are listed in place of the file's owner and group.
e1f297e6
ER
928Keystrokes bound to VC commands will execute as though they had been called
929on a buffer attached to the file named in the current Dired buffer line."
930 (setq vc-dired-mode t)
931 (setq vc-mode " under VC"))
594722a8 932
8c0aaf40
ER
933(defun vc-dired-reformat-line (x)
934 ;; Hack a directory-listing line, plugging in locking-user info in
34291cd2
RS
935 ;; place of the user and group info. Should have the beneficial
936 ;; side-effect of shortening the listing line. Each call starts with
8c0aaf40
ER
937 ;; point immediately following the dired mark area on the line to be
938 ;; hacked.
939 ;;
940 ;; Simplest possible one:
941 ;; (insert (concat x "\t")))
942 ;;
943 ;; This code, like dired, assumes UNIX -l format.
944 (forward-word 1) ;; skip over any extra field due to -ibs options
945 (if x (setq x (concat "(" x ")")))
946 (if (re-search-forward "\\([0-9]+ \\).................\\( .*\\)" nil 0)
947 (let ((rep (substring (concat x " ") 0 9)))
948 (replace-match (concat "\\1" rep "\\2") t)))
949 )
950
6ed5075c
RS
951;;; Note in Emacs 18 the following defun gets overridden
952;;; with the symbol 'vc-directory-18. See below.
637a8ae9 953;;;###autoload
594722a8
ER
954(defun vc-directory (verbose)
955 "Show version-control status of all files under the current directory."
956 (interactive "P")
e1f297e6
ER
957 (let (nonempty
958 (dl (length default-directory))
959 (filelist nil) (userlist nil)
8c0aaf40
ER
960 dired-buf
961 dired-buf-mod-count)
e1f297e6
ER
962 (vc-file-tree-walk
963 (function (lambda (f)
964 (if (vc-registered f)
965 (let ((user (vc-locking-user f)))
966 (and (or verbose user)
967 (setq filelist (cons (substring f dl) filelist))
968 (setq userlist (cons user userlist))))))))
594722a8 969 (save-excursion
34291cd2 970 ;; This uses a semi-documented feature of dired; giving a switch
8c0aaf40
ER
971 ;; argument forces the buffer to refresh each time.
972 (dired
973 (cons default-directory (nreverse filelist))
974 dired-listing-switches)
975 (setq dired-buf (current-buffer))
976 (setq nonempty (not (zerop (buffer-size)))))
594722a8
ER
977 (if nonempty
978 (progn
e1f297e6
ER
979 (pop-to-buffer dired-buf)
980 (vc-dired-mode)
981 (goto-char (point-min))
982 (setq buffer-read-only nil)
8c0aaf40 983 (forward-line 1) ;; Skip header line
e1f297e6 984 (mapcar
7ef84cf9
RS
985 (function
986 (lambda (x)
8c0aaf40
ER
987 (forward-char 2) ;; skip dired's mark area
988 (vc-dired-reformat-line x)
7ef84cf9 989 (forward-line 1))) ;; go to next line
8c0aaf40 990 (nreverse userlist))
e1f297e6
ER
991 (setq buffer-read-only t)
992 (goto-char (point-min))
993 )
02da6253
PE
994 (message "No files are currently %s under %s"
995 (if verbose "registered" "locked") default-directory))
594722a8
ER
996 ))
997
6ed5075c
RS
998;; Emacs 18 version
999(defun vc-directory-18 (verbose)
1000 "Show version-control status of all files under the current directory."
1001 (interactive "P")
0ee7d623 1002 (let (nonempty (dir default-directory))
6ed5075c
RS
1003 (save-excursion
1004 (set-buffer (get-buffer-create "*vc-status*"))
1005 (erase-buffer)
0ee7d623 1006 (cd dir)
6ed5075c
RS
1007 (vc-file-tree-walk
1008 (function (lambda (f)
1009 (if (vc-registered f)
1010 (let ((user (vc-locking-user f)))
1011 (if (or user verbose)
1012 (insert (format
1013 "%s %s\n"
1014 (concat user) f))))))))
1015 (setq nonempty (not (zerop (buffer-size)))))
1016 (if nonempty
1017 (progn
1018 (pop-to-buffer "*vc-status*" t)
4c2d8cf9
RS
1019 (goto-char (point-min))
1020 (shrink-window-if-larger-than-buffer)))
6ed5075c
RS
1021 (message "No files are currently %s under %s"
1022 (if verbose "registered" "locked") default-directory))
064726ac 1023 )
6ed5075c
RS
1024
1025(or (boundp 'minor-mode-map-alist)
1026 (fset 'vc-directory 'vc-directory-18))
1027
7ef84cf9
RS
1028; Emacs 18 also lacks these.
1029(or (boundp 'compilation-old-error-list)
1030 (setq compilation-old-error-list nil))
1031
594722a8
ER
1032;; Named-configuration support for SCCS
1033
1034(defun vc-add-triple (name file rev)
1035 (save-excursion
1036 (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
1037 (goto-char (point-max))
1038 (insert name "\t:\t" file "\t" rev "\n")
1039 (basic-save-buffer)
1040 (kill-buffer (current-buffer))
1041 ))
1042
1043(defun vc-record-rename (file newname)
1044 (save-excursion
1045 (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
1046 (goto-char (point-min))
7d847440
RS
1047 ;; (replace-regexp (concat ":" (regexp-quote file) "$") (concat ":" newname))
1048 (while (re-search-forward (concat ":" (regexp-quote file) "$") nil t)
1049 (replace-match (concat ":" newname) nil nil))
594722a8
ER
1050 (basic-save-buffer)
1051 (kill-buffer (current-buffer))
1052 ))
1053
1054(defun vc-lookup-triple (file name)
7b4f934d
ER
1055 ;; Return the numeric version corresponding to a named snapshot of file
1056 ;; If name is nil or a version number string it's just passed through
47ca02a6 1057 (cond ((null name) name)
7b4f934d
ER
1058 ((let ((firstchar (aref name 0)))
1059 (and (>= firstchar ?0) (<= firstchar ?9)))
1060 name)
1061 (t
1062 (car (vc-master-info
1063 (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file)
1064 (list (concat name "\t:\t" file "\t\\(.+\\)"))))
1065 )))
594722a8
ER
1066
1067;; Named-configuration entry points
1068
1dabb4e6
PE
1069(defun vc-locked-example ()
1070 ;; Return an example of why the current directory is not ready to be snapshot
1071 ;; or nil if no such example exists.
1072 (catch 'vc-locked-example
02da6253
PE
1073 (vc-file-tree-walk
1074 (function (lambda (f)
1075 (if (and (vc-registered f) (vc-locking-user f))
1dabb4e6
PE
1076 (throw 'vc-locked-example f)))))
1077 nil))
594722a8 1078
637a8ae9 1079;;;###autoload
594722a8
ER
1080(defun vc-create-snapshot (name)
1081 "Make a snapshot called NAME.
1082The snapshot is made from all registered files at or below the current
1083directory. For each file, the version level of its latest
1084version becomes part of the named configuration."
1085 (interactive "sNew snapshot name: ")
1dabb4e6
PE
1086 (let ((locked (vc-locked-example)))
1087 (if locked
1088 (error "File %s is locked" locked)
1089 (vc-file-tree-walk
1090 (function (lambda (f) (and
1091 (vc-name f)
1092 (vc-backend-assign-name f name)))))
1093 )))
594722a8 1094
637a8ae9 1095;;;###autoload
594722a8
ER
1096(defun vc-retrieve-snapshot (name)
1097 "Retrieve the snapshot called NAME.
1098This function fails if any files are locked at or below the current directory
1099Otherwise, all registered files are checked out (unlocked) at their version
1100levels in the snapshot."
1101 (interactive "sSnapshot name to retrieve: ")
1dabb4e6
PE
1102 (let ((locked (vc-locked-example)))
1103 (if locked
1104 (error "File %s is locked" locked)
1105 (vc-file-tree-walk
1106 (function (lambda (f) (and
1107 (vc-name f)
1108 (vc-error-occurred
1109 (vc-backend-checkout f nil name))))))
1110 )))
594722a8
ER
1111
1112;; Miscellaneous other entry points
1113
637a8ae9 1114;;;###autoload
594722a8
ER
1115(defun vc-print-log ()
1116 "List the change log of the current buffer in a window."
1117 (interactive)
e1f297e6
ER
1118 (if vc-dired-mode
1119 (set-buffer (find-file-noselect (dired-get-filename))))
8ac8c82f 1120 (while vc-parent-buffer
1a2f456b 1121 (pop-to-buffer vc-parent-buffer))
594722a8
ER
1122 (if (and buffer-file-name (vc-name buffer-file-name))
1123 (progn
1124 (vc-backend-print-log buffer-file-name)
1125 (pop-to-buffer (get-buffer-create "*vc*"))
4805f679
BF
1126 (while (looking-at "=*\n")
1127 (delete-char (- (match-end 0) (match-beginning 0)))
1128 (forward-line -1))
594722a8 1129 (goto-char (point-min))
4805f679
BF
1130 (if (looking-at "[\b\t\n\v\f\r ]+")
1131 (delete-char (- (match-end 0) (match-beginning 0))))
4c2d8cf9 1132 (shrink-window-if-larger-than-buffer)
594722a8 1133 )
7ef84cf9 1134 (vc-registration-error buffer-file-name)
594722a8
ER
1135 )
1136 )
1137
637a8ae9 1138;;;###autoload
594722a8 1139(defun vc-revert-buffer ()
9c95ac44
RS
1140 "Revert the current buffer's file back to the latest checked-in version.
1141This asks for confirmation if the buffer contents are not identical
1142to that version."
594722a8 1143 (interactive)
e1f297e6
ER
1144 (if vc-dired-mode
1145 (find-file-other-window (dired-get-filename)))
8ac8c82f 1146 (while vc-parent-buffer
1a2f456b 1147 (pop-to-buffer vc-parent-buffer))
594722a8 1148 (let ((file buffer-file-name)
97d3f950 1149 (obuf (current-buffer)) (changed (vc-diff nil t)))
9c95ac44
RS
1150 (if (and changed (or vc-suppress-confirm
1151 (not (yes-or-no-p "Discard changes? "))))
594722a8
ER
1152 (progn
1153 (delete-window)
590cc449 1154 (error "Revert cancelled"))
594722a8
ER
1155 (set-buffer obuf))
1156 (if changed
1157 (delete-window))
1158 (vc-backend-revert file)
624b4662 1159 (vc-resynch-window file t t)
594722a8
ER
1160 )
1161 )
1162
637a8ae9 1163;;;###autoload
594722a8 1164(defun vc-cancel-version (norevert)
34291cd2
RS
1165 "Get rid of most recently checked in version of this file.
1166A prefix argument means do not revert the buffer afterwards."
594722a8 1167 (interactive "P")
e1f297e6
ER
1168 (if vc-dired-mode
1169 (find-file-other-window (dired-get-filename)))
8ac8c82f
ER
1170 (while vc-parent-buffer
1171 (pop-to-buffer vc-parent-buffer))
1172 (let* ((target (concat (vc-latest-version (buffer-file-name))))
1173 (yours (concat (vc-your-latest-version (buffer-file-name))))
7b4f934d 1174 (prompt (if (string-equal yours target)
590cc449
RS
1175 "Remove your version %s from master? "
1176 "Version %s was not your change. Remove it anyway? ")))
7b4f934d
ER
1177 (if (null (yes-or-no-p (format prompt target)))
1178 nil
1179 (vc-backend-uncheck (buffer-file-name) target)
34291cd2
RS
1180 (if (or norevert
1181 (not (yes-or-no-p "Revert buffer to most recent remaining version? ")))
7b4f934d
ER
1182 (vc-mode-line (buffer-file-name))
1183 (vc-checkout (buffer-file-name) nil)))
8ac8c82f 1184 ))
594722a8
ER
1185
1186(defun vc-rename-file (old new)
34291cd2
RS
1187 "Rename file OLD to NEW, and rename its master file likewise."
1188 (interactive "fVC rename file: \nFRename to: ")
594722a8 1189 (let ((oldbuf (get-file-buffer old)))
d52f0de9 1190 (if (and oldbuf (buffer-modified-p oldbuf))
590cc449 1191 (error "Please save files before moving them"))
594722a8 1192 (if (get-file-buffer new)
590cc449 1193 (error "Already editing new file name"))
d52f0de9
RS
1194 (if (file-exists-p new)
1195 (error "New file already exists"))
594722a8
ER
1196 (let ((oldmaster (vc-name old)))
1197 (if oldmaster
d52f0de9
RS
1198 (progn
1199 (if (vc-locking-user old)
1200 (error "Please check in files before moving them"))
1201 (if (or (file-symlink-p oldmaster)
1202 ;; This had FILE, I changed it to OLD. -- rms.
1203 (file-symlink-p (vc-backend-subdirectory-name old)))
1204 (error "This is not a safe thing to do in the presence of symbolic links"))
1205 (rename-file
1206 oldmaster
1207 (let ((backend (vc-backend-deduce old))
1208 (newdir (or (file-name-directory new) ""))
1209 (newbase (file-name-nondirectory new)))
1210 (catch 'found
1211 (mapcar
1212 (function
1213 (lambda (s)
1214 (if (eq backend (cdr s))
1215 (let* ((newmaster (format (car s) newdir newbase))
1216 (newmasterdir (file-name-directory newmaster)))
1217 (if (or (not newmasterdir)
1218 (file-directory-p newmasterdir))
1219 (throw 'found newmaster))))))
1220 vc-master-templates)
1221 (error "New file lacks a version control directory"))))))
594722a8
ER
1222 (if (or (not oldmaster) (file-exists-p old))
1223 (rename-file old new)))
1224; ?? Renaming a file might change its contents due to keyword expansion.
1225; We should really check out a new copy if the old copy was precisely equal
1226; to some checked in version. However, testing for this is tricky....
1227 (if oldbuf
1228 (save-excursion
1229 (set-buffer oldbuf)
1230 (set-visited-file-name new)
1231 (set-buffer-modified-p nil))))
60213ed0
RS
1232 ;; This had FILE, I changed it to OLD. -- rms.
1233 (vc-backend-dispatch old
594722a8
ER
1234 (vc-record-rename old new)
1235 nil)
1236 )
1237
637a8ae9 1238;;;###autoload
f35ecf88
RM
1239(defun vc-update-change-log (&rest args)
1240 "Find change log file and add entries from recent RCS logs.
1241The mark is left at the end of the text prepended to the change log.
1242With prefix arg of C-u, only find log entries for the current buffer's file.
1243With any numeric prefix arg, find log entries for all files currently visited.
4b40fdea 1244Otherwise, find log entries for all registered files in the default directory.
f35ecf88 1245From a program, any arguments are passed to the `rcs2log' script."
67242a23
RM
1246 (interactive
1247 (cond ((consp current-prefix-arg) ;C-u
1248 (list buffer-file-name))
1249 (current-prefix-arg ;Numeric argument.
1250 (let ((files nil)
1251 (buffers (buffer-list))
1252 file)
1253 (while buffers
1254 (setq file (buffer-file-name (car buffers)))
1255 (and file (vc-backend-deduce file)
4b40fdea 1256 (setq files (cons file files)))
67242a23 1257 (setq buffers (cdr buffers)))
4b40fdea
PE
1258 files))
1259 (t
1260 (let ((RCS (concat default-directory "RCS")))
1261 (and (file-directory-p RCS)
1262 (mapcar (function
1263 (lambda (f)
1264 (if (string-match "\\(.*\\),v$" f)
1265 (substring f 0 (match-end 1))
1266 f)))
1267 (directory-files RCS nil "...\\|^[^.]\\|^.[^.]")))))))
41dfb835
RS
1268 (let ((odefault default-directory))
1269 (find-file-other-window (find-change-log))
1270 (barf-if-buffer-read-only)
1271 (vc-buffer-sync)
1272 (undo-boundary)
1273 (goto-char (point-min))
1274 (push-mark)
1275 (message "Computing change log entries...")
4b40fdea
PE
1276 (message "Computing change log entries... %s"
1277 (if (or (null args)
1278 (eq 0 (apply 'call-process "rcs2log" nil t nil
ce37bf20
RS
1279 "-n"
1280 (user-login-name)
1281 (user-full-name)
1282 user-mail-address
4b40fdea
PE
1283 (mapcar (function
1284 (lambda (f)
1285 (file-relative-name
1286 (if (file-name-absolute-p f)
1287 f
1288 (concat odefault f)))))
1289 args))))
1290 "done" "failed"))))
594722a8
ER
1291
1292;; Functions for querying the master and lock files.
1293
caf15d4f 1294(defun vc-match-substring (bn)
594722a8
ER
1295 (buffer-substring (match-beginning bn) (match-end bn)))
1296
1297(defun vc-parse-buffer (patterns &optional file properties)
1298 ;; Use PATTERNS to parse information out of the current buffer
1299 ;; by matching each regular expression in the list and returning \\1.
1300 ;; If a regexp has two tag brackets, assume the second is a date
1301 ;; field and we want the most recent entry matching the template.
1302 ;; If FILE and PROPERTIES are given, the latter must be a list of
1303 ;; properties of the same length as PATTERNS; each property is assigned
1304 ;; the corresponding value.
1305 (mapcar (function (lambda (p)
1306 (goto-char (point-min))
1307 (if (string-match "\\\\(.*\\\\(" p)
1308 (let ((latest-date "") (latest-val))
1309 (while (re-search-forward p nil t)
caf15d4f 1310 (let ((date (vc-match-substring 2)))
594722a8
ER
1311 (if (string< latest-date date)
1312 (progn
1313 (setq latest-date date)
1314 (setq latest-val
caf15d4f 1315 (vc-match-substring 1))))))
594722a8
ER
1316 latest-val))
1317 (prog1
1892807a
ER
1318 (let ((value nil))
1319 (if (re-search-forward p nil t)
1320 (setq value (vc-match-substring 1)))
1321 (if file
1322 (vc-file-setprop file (car properties) value))
1323 value)
594722a8
ER
1324 (setq properties (cdr properties)))))
1325 patterns)
1326 )
1327
1328(defun vc-master-info (file fields &optional rfile properties)
1329 ;; Search for information in a master file.
1330 (if (and file (file-exists-p file))
1331 (save-excursion
1332 (let ((buf))
1333 (setq buf (create-file-buffer file))
1334 (set-buffer buf))
1335 (erase-buffer)
1336 (insert-file-contents file nil)
1337 (set-buffer-modified-p nil)
1338 (auto-save-mode nil)
1339 (prog1
1340 (vc-parse-buffer fields rfile properties)
1341 (kill-buffer (current-buffer)))
1342 )
1343 (if rfile
1344 (mapcar
1345 (function (lambda (p) (vc-file-setprop rfile p nil)))
1346 properties))
1347 )
1348 )
1349
1350(defun vc-log-info (command file patterns &optional properties)
1351 ;; Search for information in log program output
1352 (if (and file (file-exists-p file))
1353 (save-excursion
1354 (let ((buf))
1355 (setq buf (get-buffer-create "*vc*"))
1356 (set-buffer buf))
1357 (apply 'vc-do-command 0 command file nil)
1358 (set-buffer-modified-p nil)
1359 (prog1
1360 (vc-parse-buffer patterns file properties)
1361 (kill-buffer (current-buffer))
1362 )
1363 )
1364 (if file
1365 (mapcar
1366 (function (lambda (p) (vc-file-setprop file p nil)))
1367 properties))
1368 )
1369 )
1370
1371(defun vc-locking-user (file)
1372 "Return the name of the person currently holding a lock on FILE.
1373Return nil if there is no such person."
e1f297e6 1374 (setq file (expand-file-name file)) ;; ??? Work around bug in 19.0.4
594722a8
ER
1375 (if (or (not vc-keep-workfiles)
1376 (eq vc-mistrust-permissions 't)
1377 (and vc-mistrust-permissions
1378 (funcall vc-mistrust-permissions (vc-backend-subdirectory-name file))))
1379 (vc-true-locking-user file)
1380 ;; This implementation assumes that any file which is under version
1381 ;; control and has -rw-r--r-- is locked by its owner. This is true
1382 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
1383 ;; We have to be careful not to exclude files with execute bits on;
73960ffd
PE
1384 ;; scripts can be under version control too. Also, we must ignore
1385 ;; the group-read and other-read bits, since paranoid users turn them off.
1386 ;; This hack wins because calls to the very expensive vc-fetch-properties
594722a8
ER
1387 ;; function only have to be made if (a) the file is locked by someone
1388 ;; other than the current user, or (b) some untoward manipulation
e1f297e6
ER
1389 ;; behind vc's back has changed the owner or the `group' or `other'
1390 ;; write bits.
594722a8 1391 (let ((attributes (file-attributes file)))
73960ffd 1392 (cond ((string-match ".r-..-..-." (nth 8 attributes))
594722a8
ER
1393 nil)
1394 ((and (= (nth 2 attributes) (user-uid))
73960ffd 1395 (string-match ".rw..-..-." (nth 8 attributes)))
594722a8
ER
1396 (user-login-name))
1397 (t
1398 (vc-true-locking-user file))))))
1399
1400(defun vc-true-locking-user (file)
1401 ;; The slow but reliable version
1402 (vc-fetch-properties file)
1403 (vc-file-getprop file 'vc-locking-user))
1404
1405(defun vc-latest-version (file)
1406 ;; Return version level of the latest version of FILE
1407 (vc-fetch-properties file)
1408 (vc-file-getprop file 'vc-latest-version))
1409
1410(defun vc-your-latest-version (file)
1411 ;; Return version level of the latest version of FILE checked in by you
1412 (vc-fetch-properties file)
1413 (vc-file-getprop file 'vc-your-latest-version))
1414
1415;; Collect back-end-dependent stuff here
1416;;
1417;; Everything eventually funnels through these functions. To implement
1418;; support for a new version-control system, add another branch to the
7b4f934d
ER
1419;; vc-backend-dispatch macro and fill it in in each call. The variable
1420;; vc-master-templates in vc-hooks.el will also have to change.
594722a8
ER
1421
1422(defmacro vc-backend-dispatch (f s r)
1423 "Execute FORM1 or FORM2 depending on whether we're using SCCS or RCS."
1424 (list 'let (list (list 'type (list 'vc-backend-deduce f)))
1425 (list 'cond
1426 (list (list 'eq 'type (quote 'SCCS)) s) ;; SCCS
1427 (list (list 'eq 'type (quote 'RCS)) r) ;; RCS
1428 )))
1429
1430(defun vc-lock-file (file)
1431 ;; Generate lock file name corresponding to FILE
1432 (let ((master (vc-name file)))
1433 (and
1434 master
1435 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
1436 (concat
1437 (substring master (match-beginning 1) (match-end 1))
1438 "p."
1439 (substring master (match-beginning 2) (match-end 2))))))
1440
1441
1442(defun vc-fetch-properties (file)
1443 ;; Re-fetch all properties associated with the given file.
1444 ;; Currently these properties are:
1445 ;; vc-locking-user
1446 ;; vc-locked-version
1447 ;; vc-latest-version
1448 ;; vc-your-latest-version
1449 (vc-backend-dispatch
1450 file
1451 ;; SCCS
1452 (progn
1453 (vc-master-info (vc-lock-file file)
1454 (list
1455 "^[^ ]+ [^ ]+ \\([^ ]+\\)"
1456 "^\\([^ ]+\\)")
1457 file
1458 '(vc-locking-user vc-locked-version))
1459 (vc-master-info (vc-name file)
1460 (list
1461 "^\001d D \\([^ ]+\\)"
1462 (concat "^\001d D \\([^ ]+\\) .* "
1463 (regexp-quote (user-login-name)) " ")
1464 )
1465 file
1466 '(vc-latest-version vc-your-latest-version))
1467 )
1468 ;; RCS
1469 (vc-log-info "rlog" file
1470 (list
1471 "^locks: strict\n\t\\([^:]+\\)"
1472 "^locks: strict\n\t[^:]+: \\(.+\\)"
1473 "^revision[\t ]+\\([0-9.]+\\).*\ndate: \\([ /0-9:]+\\);"
1474 (concat
3234e2a3 1475 "^revision[\t ]+\\([0-9.]+\\)\n.*author: "
594722a8 1476 (regexp-quote (user-login-name))
3234e2a3 1477 ";"))
594722a8
ER
1478 '(vc-locking-user vc-locked-version
1479 vc-latest-version vc-your-latest-version))
1480 ))
1481
1482(defun vc-backend-subdirectory-name (&optional file)
1483 ;; Where the master and lock files for the current directory are kept
1484 (symbol-name
1485 (or
1486 (and file (vc-backend-deduce file))
1487 vc-default-back-end
1488 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
1489
1490(defun vc-backend-admin (file &optional rev comment)
1491 ;; Register a file into the version-control system
1492 ;; Automatically retrieves a read-only version of the file with
1493 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1494 ;; it deletes the workfile.
1495 (vc-file-clearprops file)
1496 (or vc-default-back-end
1497 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))
1498 (message "Registering %s..." file)
1499 (let ((backend
1500 (cond
1501 ((file-exists-p (vc-backend-subdirectory-name)) vc-default-back-end)
1502 ((file-exists-p "RCS") 'RCS)
1503 ((file-exists-p "SCCS") 'SCCS)
1504 (t vc-default-back-end))))
1505 (cond ((eq backend 'SCCS)
1506 (vc-do-command 0 "admin" file ;; SCCS
1507 (and rev (concat "-r" rev))
1508 "-fb"
1509 (concat "-i" file)
1510 (and comment (concat "-y" comment))
1511 (format
1512 (car (rassq 'SCCS vc-master-templates))
1513 (or (file-name-directory file) "")
1514 (file-name-nondirectory file)))
1515 (delete-file file)
1516 (if vc-keep-workfiles
1517 (vc-do-command 0 "get" file)))
1518 ((eq backend 'RCS)
1519 (vc-do-command 0 "ci" file ;; RCS
1520 (concat (if vc-keep-workfiles "-u" "-r") rev)
1521 (and comment (concat "-t-" comment))
1522 file)
1523 )))
1524 (message "Registering %s...done" file)
1525 )
1526
f1818994 1527(defun vc-backend-checkout (file &optional writable rev workfile)
594722a8 1528 ;; Retrieve a copy of a saved version into a workfile
f1818994
PE
1529 (let ((filename (or workfile file)))
1530 (message "Checking out %s..." filename)
1531 (vc-backend-dispatch file
c9ce5e2c
RS
1532 (if workfile ;; SCCS
1533 ;; Some SCCS implementations allow checking out directly to a
1534 ;; file using the -G option, but then some don't so use the
1535 ;; least common denominator approach and use the -p option
1536 ;; ala RCS.
1537 (let ((vc-modes (logior (file-modes (vc-name file))
1538 (if writable 128 0)))
1539 (failed t))
1540 (unwind-protect
1541 (progn
1542 (vc-do-command
1543 0 "/bin/sh" file "-c"
1544 (format "umask %o; exec >\"$1\" || exit; shift; umask %o; exec get \"$@\""
1545 (logand 511 (lognot vc-modes))
1546 (logand 511 (lognot (default-file-modes))))
1547 "" ; dummy argument for shell's $0
1548 filename
1549 (if writable "-e")
1550 "-p" (and rev (concat "-r" (vc-lookup-triple file rev))))
1551 (setq failed nil))
1552 (and failed (file-exists-p filename) (delete-file filename))))
1553 (vc-do-command 0 "get" file ;; SCCS
1554 (if writable "-e")
1555 (and rev (concat "-r" (vc-lookup-triple file rev)))))
f1818994
PE
1556 (if workfile ;; RCS
1557 ;; RCS doesn't let us check out into arbitrary file names directly.
1558 ;; Use `co -p' and make stdout point to the correct file.
219a7d3f 1559 (let ((vc-modes (logior (file-modes (vc-name file))
f1818994
PE
1560 (if writable 128 0)))
1561 (failed t))
1562 (unwind-protect
1563 (progn
f1818994
PE
1564 (vc-do-command
1565 0 "/bin/sh" file "-c"
219a7d3f
PE
1566 (format "umask %o; exec >\"$1\" || exit; shift; umask %o; exec co \"$@\""
1567 (logand 511 (lognot vc-modes))
1568 (logand 511 (lognot (default-file-modes))))
f1818994
PE
1569 "" ; dummy argument for shell's $0
1570 filename
1571 (if writable "-l")
1572 (concat "-p" rev))
1573 (setq failed nil))
f1818994
PE
1574 (and failed (file-exists-p filename) (delete-file filename))))
1575 (vc-do-command 0 "co" file
1576 (if writable "-l")
1577 (and rev (concat "-r" rev))))
594722a8 1578 )
f1818994
PE
1579 (or workfile
1580 (vc-file-setprop file 'vc-checkout-time (nth 5 (file-attributes file))))
1581 (message "Checking out %s...done" filename))
594722a8
ER
1582 )
1583
1584(defun vc-backend-logentry-check (file)
1585 (vc-backend-dispatch file
8c0aaf40 1586 (if (>= (buffer-size) 512) ;; SCCS
594722a8
ER
1587 (progn
1588 (goto-char 512)
1589 (error
590cc449 1590 "Log must be less than 512 characters; point is now at pos 512")))
594722a8
ER
1591 nil)
1592 )
1593
1594(defun vc-backend-checkin (file &optional rev comment)
1595 ;; Register changes to FILE as level REV with explanatory COMMENT.
1596 ;; Automatically retrieves a read-only version of the file with
1597 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1598 ;; it deletes the workfile.
1599 (message "Checking in %s..." file)
2ea8ce47
RM
1600 (save-excursion
1601 ;; Change buffers to get local value of vc-checkin-switches.
1602 (set-buffer (or (get-file-buffer file) (current-buffer)))
1603 (vc-backend-dispatch file
1604 (progn
1605 (apply 'vc-do-command 0 "delta" file
1606 (if rev (concat "-r" rev))
1607 (concat "-y" comment)
1608 vc-checkin-switches)
1609 (if vc-keep-workfiles
1610 (vc-do-command 0 "get" file))
1611 )
1612 (apply 'vc-do-command 0 "ci" file
1613 (concat (if vc-keep-workfiles "-u" "-r") rev)
1614 (concat "-m" comment)
1615 vc-checkin-switches)
1616 ))
594722a8
ER
1617 (vc-file-setprop file 'vc-locking-user nil)
1618 (message "Checking in %s...done" file)
1619 )
1620
1621(defun vc-backend-revert (file)
1622 ;; Revert file to latest checked-in version.
1623 (message "Reverting %s..." file)
1624 (vc-backend-dispatch
1625 file
1626 (progn ;; SCCS
1627 (vc-do-command 0 "unget" file nil)
1628 (vc-do-command 0 "get" file nil))
d76b8d38 1629 (vc-do-command 0 "co" file "-f" "-u")) ;; RCS. This deletes the work file.
594722a8
ER
1630 (vc-file-setprop file 'vc-locking-user nil)
1631 (message "Reverting %s...done" file)
1632 )
1633
1634(defun vc-backend-steal (file &optional rev)
1635 ;; Steal the lock on the current workfile. Needs RCS 5.6.2 or later for -M.
1636 (message "Stealing lock on %s..." file)
af1b8606 1637 (vc-backend-dispatch file
d76b8d38
PE
1638 (progn
1639 (vc-do-command 0 "unget" file "-n" (if rev (concat "-r" rev)))
1640 (vc-do-command 0 "get" file "-g" (if rev (concat "-r" rev)))
1641 )
fd6217ac 1642 (vc-do-command 0 "rcs" file "-M" (concat "-u" rev) (concat "-l" rev)))
594722a8
ER
1643 (vc-file-setprop file 'vc-locking-user (user-login-name))
1644 (message "Stealing lock on %s...done" file)
1645 )
1646
1647(defun vc-backend-uncheck (file target)
1648 ;; Undo the latest checkin. Note: this code will have to get a lot
1649 ;; smarter when we support multiple branches.
1650 (message "Removing last change from %s..." file)
1651 (vc-backend-dispatch file
1652 (vc-do-command 0 "rmdel" file (concat "-r" target))
1653 (vc-do-command 0 "rcs" file (concat "-o" target))
1654 )
1655 (message "Removing last change from %s...done" file)
1656 )
1657
1658(defun vc-backend-print-log (file)
1659 ;; Print change log associated with FILE to buffer *vc*.
1660 (vc-do-command 0
1661 (vc-backend-dispatch file "prs" "rlog")
1662 file)
1663 )
1664
1665(defun vc-backend-assign-name (file name)
1666 ;; Assign to a FILE's latest version a given NAME.
1667 (vc-backend-dispatch file
1668 (vc-add-triple name file (vc-latest-version file)) ;; SCCS
1669 (vc-do-command 0 "rcs" file (concat "-n" name ":")) ;; RCS
3234e2a3 1670 )
3234e2a3 1671 )
594722a8 1672
9a51ed3a
PE
1673(defun vc-backend-diff (file &optional oldvers newvers cmp)
1674 ;; Get a difference report between two versions of FILE.
1675 ;; Get only a brief comparison report if CMP, a difference report otherwise.
7b4f934d
ER
1676 (if (eq (vc-backend-deduce file) 'SCCS)
1677 (setq oldvers (vc-lookup-triple file oldvers))
1678 (setq newvers (vc-lookup-triple file newvers)))
9a51ed3a
PE
1679 (let* ((command (or (vc-backend-dispatch file "vcdiff" "rcsdiff")
1680 (vc-registration-error file)))
1681 (options (append (list (and cmp "--brief")
1682 "-q"
1683 (and oldvers (concat "-r" oldvers))
1684 (and newvers (concat "-r" newvers)))
1685 (and (not cmp)
1686 (if (listp diff-switches)
1687 diff-switches
1688 (list diff-switches)))))
1689 (status (apply 'vc-do-command 2 command file options)))
1690 ;; Some RCS versions don't understand "--brief"; work around this.
1691 (if (eq status 2)
cec574b9 1692 (apply 'vc-do-command 1 command file (if cmp (cdr options) options))
9a51ed3a 1693 status)))
594722a8
ER
1694
1695(defun vc-check-headers ()
1696 "Check if the current file has any headers in it."
1697 (interactive)
1698 (save-excursion
1699 (goto-char (point-min))
1700 (vc-backend-dispatch buffer-file-name
1701 (re-search-forward "%[MIRLBSDHTEGUYFPQCZWA]%" nil t) ;; SCCS
1702 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t) ;; RCS
1703 )
1704 ))
1705
1706;; Back-end-dependent stuff ends here.
1707
1708;; Set up key bindings for use while editing log messages
1709
1710(defun vc-log-mode ()
1711 "Minor mode for driving version-control tools.
1712These bindings are added to the global keymap when you enter this mode:
1713\\[vc-next-action] perform next logical version-control operation on current file
1714\\[vc-register] register current file
1715\\[vc-toggle-read-only] like next-action, but won't register files
1716\\[vc-insert-headers] insert version-control headers in current file
1717\\[vc-print-log] display change history of current file
1718\\[vc-revert-buffer] revert buffer to latest version
1719\\[vc-cancel-version] undo latest checkin
1720\\[vc-diff] show diffs between file versions
f1818994 1721\\[vc-version-other-window] visit old version in another window
594722a8
ER
1722\\[vc-directory] show all files locked by any user in or below .
1723\\[vc-update-change-log] add change log entry from recent checkins
1724
1725While you are entering a change log message for a version, the following
1726additional bindings will be in effect.
1727
1728\\[vc-finish-logentry] proceed with check in, ending log message entry
1729
1730Whenever you do a checkin, your log comment is added to a ring of
1731saved comments. These can be recalled as follows:
1732
1733\\[vc-next-comment] replace region with next message in comment ring
1734\\[vc-previous-comment] replace region with previous message in comment ring
8c0aaf40
ER
1735\\[vc-comment-search-reverse] search backward for regexp in the comment ring
1736\\[vc-comment-search-forward] search backward for regexp in the comment ring
594722a8
ER
1737
1738Entry to the change-log submode calls the value of text-mode-hook, then
1739the value of vc-log-mode-hook.
1740
1741Global user options:
1742 vc-initial-comment If non-nil, require user to enter a change
1743 comment upon first checkin of the file.
1744
1745 vc-keep-workfiles Non-nil value prevents workfiles from being
1746 deleted when changes are checked in
1747
1748 vc-suppress-confirm Suppresses some confirmation prompts,
1749 notably for reversions.
1750
7b4f934d 1751 vc-header-alist Which keywords to insert when adding headers
594722a8
ER
1752 with \\[vc-insert-headers]. Defaults to
1753 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under RCS.
1754
1755 vc-static-header-alist By default, version headers inserted in C files
1756 get stuffed in a static string area so that
1757 ident(RCS) or what(SCCS) can see them in the
1758 compiled object code. You can override this
1759 by setting this variable to nil, or change
1760 the header template by changing it.
1761
1762 vc-command-messages if non-nil, display run messages from the
1763 actual version-control utilities (this is
1764 intended primarily for people hacking vc
1765 itself).
1766"
1767 (interactive)
1768 (set-syntax-table text-mode-syntax-table)
1769 (use-local-map vc-log-entry-mode)
1770 (setq local-abbrev-table text-mode-abbrev-table)
1771 (setq major-mode 'vc-log-mode)
1772 (setq mode-name "VC-Log")
1773 (make-local-variable 'vc-log-file)
1774 (make-local-variable 'vc-log-version)
8c0aaf40 1775 (make-local-variable 'vc-comment-ring-index)
594722a8
ER
1776 (set-buffer-modified-p nil)
1777 (setq buffer-file-name nil)
1778 (run-hooks 'text-mode-hook 'vc-log-mode-hook)
1779)
1780
1781;; Initialization code, to be done just once at load-time
1782(if vc-log-entry-mode
1783 nil
1784 (setq vc-log-entry-mode (make-sparse-keymap))
1785 (define-key vc-log-entry-mode "\M-n" 'vc-next-comment)
1786 (define-key vc-log-entry-mode "\M-p" 'vc-previous-comment)
8c0aaf40 1787 (define-key vc-log-entry-mode "\M-r" 'vc-comment-search-reverse)
594722a8
ER
1788 (define-key vc-log-entry-mode "\M-s" 'vc-comment-search-forward)
1789 (define-key vc-log-entry-mode "\C-c\C-c" 'vc-finish-logentry)
1790 )
1791
1792;;; These things should probably be generally available
1793
02da6253 1794(defun vc-file-tree-walk (func &rest args)
34291cd2
RS
1795 "Walk recursively through default directory.
1796Invoke FUNC f ARGS on each non-directory file f underneath it."
02da6253
PE
1797 (vc-file-tree-walk-internal default-directory func args)
1798 (message "Traversing directory %s...done" default-directory))
1799
1800(defun vc-file-tree-walk-internal (file func args)
1801 (if (not (file-directory-p file))
1802 (apply func file args)
1803 (message "Traversing directory %s..." file)
1804 (let ((dir (file-name-as-directory file)))
1805 (mapcar
1806 (function
1807 (lambda (f) (or
1808 (string-equal f ".")
1809 (string-equal f "..")
1810 (let ((dirf (concat dir f)))
1811 (or
1812 (file-symlink-p dirf) ;; Avoid possible loops
1813 (vc-file-tree-walk-internal dirf func args))))))
1814 (directory-files dir)))))
594722a8
ER
1815
1816(provide 'vc)
1817
1818;;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
1819;;;
1820;;; These may be useful to anyone who has to debug or extend the package.
1821;;;
1822;;; A fundamental problem in VC is that there are time windows between
1823;;; vc-next-action's computations of the file's version-control state and
1824;;; the actions that change it. This is a window open to lossage in a
1825;;; multi-user environment; someone else could nip in and change the state
1826;;; of the master during it.
1827;;;
1828;;; The performance problem is that rlog/prs calls are very expensive; we want
1829;;; to avoid them as much as possible.
1830;;;
1831;;; ANALYSIS:
1832;;;
1833;;; The performance problem, it turns out, simplifies in practice to the
1834;;; problem of making vc-locking-user fast. The two other functions that call
1835;;; prs/rlog will not be so commonly used that the slowdown is a problem; one
1836;;; makes snapshots, the other deletes the calling user's last change in the
1837;;; master.
1838;;;
1839;;; The race condition implies that we have to either (a) lock the master
1840;;; during the entire execution of vc-next-action, or (b) detect and
1841;;; recover from errors resulting from dispatch on an out-of-date state.
1842;;;
1843;;; Alternative (a) appears to be unfeasible. The problem is that we can't
1844;;; guarantee that the lock will ever be removed. Suppose a user starts a
1845;;; checkin, the change message buffer pops up, and the user, having wandered
1846;;; off to do something else, simply forgets about it?
1847;;;
1848;;; Alternative (b), on the other hand, works well with a cheap way to speed up
1849;;; vc-locking-user. Usually, if a file is registered, we can read its locked/
1850;;; unlocked state and its current owner from its permissions.
1851;;;
1852;;; This shortcut will fail if someone has manually changed the workfile's
1853;;; permissions; also if developers are munging the workfile in several
1854;;; directories, with symlinks to a master (in this latter case, the
1855;;; permissions shortcut will fail to detect a lock asserted from another
1856;;; directory).
1857;;;
1858;;; Note that these cases correspond exactly to the errors which could happen
1859;;; because of a competing checkin/checkout race in between two instances of
1860;;; vc-next-action.
1861;;;
1862;;; For VC's purposes, a workfile/master pair may have the following states:
1863;;;
1864;;; A. Unregistered. There is a workfile, there is no master.
1865;;;
1866;;; B. Registered and not locked by anyone.
1867;;;
1868;;; C. Locked by calling user and unchanged.
1869;;;
1870;;; D. Locked by the calling user and changed.
1871;;;
1872;;; E. Locked by someone other than the calling user.
1873;;;
1874;;; This makes for 25 states and 20 error conditions. Here's the matrix:
1875;;;
1876;;; VC's idea of state
1877;;; |
1878;;; V Actual state RCS action SCCS action Effect
1879;;; A B C D E
1880;;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
1881;;; B 5 . 6 7 8 co -l get -e checkout
1882;;; C 9 10 . 11 12 co -u unget; get revert
1883;;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
1884;;; E 17 18 19 20 . rcs -u -M ; rcs -l unget -n ; get -g steal lock
1885;;;
1886;;; All commands take the master file name as a last argument (not shown).
1887;;;
1888;;; In the discussion below, a "self-race" is a pathological situation in
1889;;; which VC operations are being attempted simultaneously by two or more
1890;;; Emacsen running under the same username.
1891;;;
1892;;; The vc-next-action code has the following windows:
1893;;;
1894;;; Window P:
1895;;; Between the check for existence of a master file and the call to
1896;;; admin/checkin in vc-buffer-admin (apparent state A). This window may
1897;;; never close if the initial-comment feature is on.
1898;;;
1899;;; Window Q:
1900;;; Between the call to vc-workfile-unchanged-p in and the immediately
1901;;; following revert (apparent state C).
1902;;;
1903;;; Window R:
1904;;; Between the call to vc-workfile-unchanged-p in and the following
1905;;; checkin (apparent state D). This window may never close.
1906;;;
1907;;; Window S:
1908;;; Between the unlock and the immediately following checkout during a
1909;;; revert operation (apparent state C). Included in window Q.
1910;;;
1911;;; Window T:
1912;;; Between vc-locking-user and the following checkout (apparent state B).
1913;;;
1914;;; Window U:
1915;;; Between vc-locking-user and the following revert (apparent state C).
1916;;; Includes windows Q and S.
1917;;;
1918;;; Window V:
1919;;; Between vc-locking-user and the following checkin (apparent state
1920;;; D). This window may never be closed if the user fails to complete the
1921;;; checkin message. Includes window R.
1922;;;
1923;;; Window W:
1924;;; Between vc-locking-user and the following steal-lock (apparent
34291cd2 1925;;; state E). This window may never close if the user fails to complete
594722a8
ER
1926;;; the steal-lock message. Includes window X.
1927;;;
1928;;; Window X:
1929;;; Between the unlock and the immediately following re-lock during a
1930;;; steal-lock operation (apparent state E). This window may never cloce
1931;;; if the user fails to complete the steal-lock message.
1932;;;
1933;;; Errors:
1934;;;
1935;;; Apparent state A ---
1936;;;
1937;;; 1. File looked unregistered but is actually registered and not locked.
1938;;;
1939;;; Potential cause: someone else's admin during window P, with
1940;;; caller's admin happening before their checkout.
1941;;;
1942;;; RCS: ci will fail with a "no lock set by <user>" message.
1943;;; SCCS: admin will fail with error (ad19).
1944;;;
1945;;; We can let these errors be passed up to the user.
1946;;;
1947;;; 2. File looked unregistered but is actually locked by caller, unchanged.
1948;;;
1949;;; Potential cause: self-race during window P.
1950;;;
1951;;; RCS: will revert the file to the last saved version and unlock it.
1952;;; SCCS: will fail with error (ad19).
1953;;;
1954;;; Either of these consequences is acceptable.
1955;;;
1956;;; 3. File looked unregistered but is actually locked by caller, changed.
1957;;;
1958;;; Potential cause: self-race during window P.
1959;;;
1960;;; RCS: will register the caller's workfile as a delta with a
1961;;; null change comment (the -t- switch will be ignored).
1962;;; SCCS: will fail with error (ad19).
1963;;;
1964;;; 4. File looked unregistered but is locked by someone else.
1965;;;
1966;;; Potential cause: someone else's admin during window P, with
1967;;; caller's admin happening *after* their checkout.
1968;;;
1969;;; RCS: will fail with a "no lock set by <user>" message.
1970;;; SCCS: will fail with error (ad19).
1971;;;
1972;;; We can let these errors be passed up to the user.
1973;;;
1974;;; Apparent state B ---
1975;;;
1976;;; 5. File looked registered and not locked, but is actually unregistered.
1977;;;
1978;;; Potential cause: master file got nuked during window P.
1979;;;
1980;;; RCS: will fail with "RCS/<file>: No such file or directory"
1981;;; SCCS: will fail with error ut4.
1982;;;
1983;;; We can let these errors be passed up to the user.
1984;;;
1985;;; 6. File looked registered and not locked, but is actually locked by the
1986;;; calling user and unchanged.
1987;;;
1988;;; Potential cause: self-race during window T.
1989;;;
1990;;; RCS: in the same directory as the previous workfile, co -l will fail
1991;;; with "co error: writable foo exists; checkout aborted". In any other
1992;;; directory, checkout will succeed.
1993;;; SCCS: will fail with ge17.
1994;;;
1995;;; Either of these consequences is acceptable.
1996;;;
1997;;; 7. File looked registered and not locked, but is actually locked by the
1998;;; calling user and changed.
1999;;;
2000;;; As case 6.
2001;;;
2002;;; 8. File looked registered and not locked, but is actually locked by another
2003;;; user.
2004;;;
2005;;; Potential cause: someone else checks it out during window T.
2006;;;
2007;;; RCS: co error: revision 1.3 already locked by <user>
2008;;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
2009;;;
2010;;; We can let these errors be passed up to the user.
2011;;;
2012;;; Apparent state C ---
2013;;;
2014;;; 9. File looks locked by calling user and unchanged, but is unregistered.
2015;;;
2016;;; As case 5.
2017;;;
2018;;; 10. File looks locked by calling user and unchanged, but is actually not
2019;;; locked.
2020;;;
2021;;; Potential cause: a self-race in window U, or by the revert's
2022;;; landing during window X of some other user's steal-lock or window S
2023;;; of another user's revert.
2024;;;
2025;;; RCS: succeeds, refreshing the file from the identical version in
2026;;; the master.
2027;;; SCCS: fails with error ut4 (p file nonexistent).
2028;;;
2029;;; Either of these consequences is acceptable.
2030;;;
2031;;; 11. File is locked by calling user. It looks unchanged, but is actually
2032;;; changed.
2033;;;
2034;;; Potential cause: the file would have to be touched by a self-race
2035;;; during window Q.
2036;;;
2037;;; The revert will succeed, removing whatever changes came with
2038;;; the touch. It is theoretically possible that work could be lost.
2039;;;
2040;;; 12. File looks like it's locked by the calling user and unchanged, but
2041;;; it's actually locked by someone else.
2042;;;
2043;;; Potential cause: a steal-lock in window V.
2044;;;
2045;;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
2046;;; SCCS: fails with error un2
2047;;;
2048;;; We can pass these errors up to the user.
2049;;;
2050;;; Apparent state D ---
2051;;;
2052;;; 13. File looks like it's locked by the calling user and changed, but it's
2053;;; actually unregistered.
2054;;;
2055;;; Potential cause: master file got nuked during window P.
2056;;;
2057;;; RCS: Checks in the user's version as an initial delta.
2058;;; SCCS: will fail with error ut4.
2059;;;
2060;;; This case is kind of nasty. It means VC may fail to detect the
2061;;; loss of previous version information.
2062;;;
2063;;; 14. File looks like it's locked by the calling user and changed, but it's
2064;;; actually unlocked.
2065;;;
2066;;; Potential cause: self-race in window V, or the checkin happening
2067;;; during the window X of someone else's steal-lock or window S of
2068;;; someone else's revert.
2069;;;
2070;;; RCS: ci will fail with "no lock set by <user>".
2071;;; SCCS: delta will fail with error ut4.
2072;;;
2073;;; 15. File looks like it's locked by the calling user and changed, but it's
2074;;; actually locked by the calling user and unchanged.
2075;;;
2076;;; Potential cause: another self-race --- a whole checkin/checkout
2077;;; sequence by the calling user would have to land in window R.
2078;;;
2079;;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
2080;;; RCS: reverts to the file state as of the second user's checkin, leaving
2081;;; the file unlocked.
2082;;;
2083;;; It is theoretically possible that work could be lost under RCS.
2084;;;
2085;;; 16. File looks like it's locked by the calling user and changed, but it's
2086;;; actually locked by a different user.
2087;;;
2088;;; RCS: ci error: no lock set by <user>
2089;;; SCCS: unget will fail with error un2
2090;;;
2091;;; We can pass these errors up to the user.
2092;;;
2093;;; Apparent state E ---
2094;;;
2095;;; 17. File looks like it's locked by some other user, but it's actually
2096;;; unregistered.
2097;;;
2098;;; As case 13.
2099;;;
2100;;; 18. File looks like it's locked by some other user, but it's actually
2101;;; unlocked.
2102;;;
2103;;; Potential cause: someone released a lock during window W.
2104;;;
2105;;; RCS: The calling user will get the lock on the file.
2106;;; SCCS: unget -n will fail with cm4.
2107;;;
2108;;; Either of these consequences will be OK.
2109;;;
2110;;; 19. File looks like it's locked by some other user, but it's actually
2111;;; locked by the calling user and unchanged.
2112;;;
2113;;; Potential cause: the other user relinquishing a lock followed by
2114;;; a self-race, both in window W.
2115;;;
2116;;; Under both RCS and SCCS, both unlock and lock will succeed, making
2117;;; the sequence a no-op.
2118;;;
2119;;; 20. File looks like it's locked by some other user, but it's actually
2120;;; locked by the calling user and changed.
2121;;;
2122;;; As case 19.
2123;;;
2124;;; PROBLEM CASES:
2125;;;
2126;;; In order of decreasing severity:
2127;;;
2128;;; Cases 11 and 15 under RCS are the only one that potentially lose work.
2129;;; They would require a self-race for this to happen.
2130;;;
2131;;; Case 13 in RCS loses information about previous deltas, retaining
2132;;; only the information in the current workfile. This can only happen
2133;;; if the master file gets nuked in window P.
2134;;;
2135;;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
2136;;; no change comment in the master. This would require a self-race in
2137;;; window P or R respectively.
2138;;;
2139;;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
2140;;;
2141;;; Unfortunately, it appears to me that no recovery is possible in these
2142;;; cases. They don't yield error messages, so there's no way to tell that
2143;;; a race condition has occurred.
2144;;;
2145;;; All other cases don't change either the workfile or the master, and
2146;;; trigger command errors which the user will see.
2147;;;
2148;;; Thus, there is no explicit recovery code.
2149
2150;;; vc.el ends here