(skip_invisible): Use new retval of TEXT_PROP_MEANS_INVISIBLE.
[emacs.git] / lisp / vc.el
blob6a20527d06f6f8695ad9cab24481f9bbb3aa8c32
1 ;;; vc.el --- drive a version-control system from within Emacs
3 ;; Copyright (C) 1992,93,94,95,96,97,98,2000,2001 Free Software Foundation, Inc.
5 ;; Author: FSF (see below for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7 ;; Keywords: tools
9 ;; $Id: vc.el,v 1.310 2001/09/22 20:04:21 monnier Exp $
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Credits:
30 ;; VC was initially designed and implemented by Eric S. Raymond
31 ;; <esr@snark.thyrsus.com>. Over the years, many people have
32 ;; contributed substantial amounts of work to VC. These include:
33 ;; Per Cederqvist <ceder@lysator.liu.se>
34 ;; Paul Eggert <eggert@twinsun.com>
35 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
36 ;; Martin Lorentzson <martinl@gnu.org>
37 ;; Dave Love <fx@gnu.org>
38 ;; Stefan Monnier <monnier@cs.yale.edu>
39 ;; Andre Spiegel <spiegel@gnu.org>
40 ;; Richard Stallman <rms@gnu.org>
41 ;; ttn@netcom.com
43 ;;; Commentary:
45 ;; This mode is fully documented in the Emacs user's manual.
47 ;; Supported version-control systems presently include SCCS, RCS, and CVS.
49 ;; Some features will not work with old RCS versions. Where
50 ;; appropriate, VC finds out which version you have, and allows or
51 ;; disallows those features (stealing locks, for example, works only
52 ;; from 5.6.2 onwards).
53 ;; Even initial checkins will fail if your RCS version is so old that ci
54 ;; doesn't understand -t-; this has been known to happen to people running
55 ;; NExTSTEP 3.0.
57 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
59 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
60 ;; to be installed somewhere on Emacs's path for executables.
62 ;; If your site uses the ChangeLog convention supported by Emacs, the
63 ;; function vc-comment-to-change-log should prove a useful checkin hook.
65 ;; The vc code maintains some internal state in order to reduce expensive
66 ;; version-control operations to a minimum. Some names are only computed
67 ;; once. If you perform version control operations with RCS/SCCS/CVS while
68 ;; vc's back is turned, or move/rename master files while vc is running,
69 ;; vc may get seriously confused. Don't do these things!
71 ;; Developer's notes on some concurrency issues are included at the end of
72 ;; the file.
74 ;; ADDING SUPPORT FOR OTHER BACKENDS
76 ;; VC can use arbitrary version control systems as a backend. To add
77 ;; support for a new backend named SYS, write a library vc-sys.el that
78 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
79 ;; case for the function and library names). VC will use that library if
80 ;; you put the symbol SYS somewhere into the list of
81 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
82 ;; returns non-nil for a file, all SYS-specific versions of VC commands
83 ;; will be available for that file.
85 ;; VC keeps some per-file information in the form of properties (see
86 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
87 ;; do not generally need to be aware of these properties. For example,
88 ;; `vc-sys-workfile-version' should compute the workfile version and
89 ;; return it; it should not look it up in the property, and it needn't
90 ;; store it there either. However, if a backend-specific function does
91 ;; store a value in a property, that value takes precedence over any
92 ;; value that the generic code might want to set (check for uses of
93 ;; the macro `with-vc-properties' in vc.el).
95 ;; In the list of functions below, each identifier needs to be prepended
96 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
97 ;; `*'), others are optional (`-').
99 ;; STATE-QUERYING FUNCTIONS
101 ;; * registered (file)
103 ;; Return non-nil if FILE is registered in this backend.
105 ;; * state (file)
107 ;; Return the current version control state of FILE. For a list of
108 ;; possible values, see `vc-state'. This function should do a full and
109 ;; reliable state computation; it is usually called immediately after
110 ;; C-x v v. If you want to use a faster heuristic when visiting a
111 ;; file, put that into `state-heuristic' below.
113 ;; - state-heuristic (file)
115 ;; If provided, this function is used to estimate the version control
116 ;; state of FILE at visiting time. It should be considerably faster
117 ;; than the implementation of `state'. For a list of possible values,
118 ;; see the doc string of `vc-state'.
120 ;; - dir-state (dir)
122 ;; If provided, this function is used to find the version control state
123 ;; of all files in DIR in a fast way. The function should not return
124 ;; anything, but rather store the files' states into the corresponding
125 ;; `vc-state' properties.
127 ;; * workfile-version (file)
129 ;; Return the current workfile version of FILE.
131 ;; - latest-on-branch-p (file)
133 ;; Return non-nil if the current workfile version of FILE is the latest
134 ;; on its branch. The default implementation always returns t, which
135 ;; means that working with non-current versions is not supported by
136 ;; default.
138 ;; * checkout-model (file)
140 ;; Indicate whether FILE needs to be "checked out" before it can be
141 ;; edited. See `vc-checkout-model' for a list of possible values.
143 ;; - workfile-unchanged-p (file)
145 ;; Return non-nil if FILE is unchanged from its current workfile
146 ;; version. This function should do a brief comparison of FILE's
147 ;; contents with those of the master version. If the backend does not
148 ;; have such a brief-comparison feature, the default implementation of
149 ;; this function can be used, which delegates to a full
150 ;; vc-BACKEND-diff.
152 ;; - mode-line-string (file)
154 ;; If provided, this function should return the VC-specific mode line
155 ;; string for FILE. The default implementation deals well with all
156 ;; states that `vc-state' can return.
158 ;; - dired-state-info (file)
160 ;; Translate the `vc-state' property of FILE into a string that can be
161 ;; used in a vc-dired buffer. The default implementation deals well
162 ;; with all states that `vc-state' can return.
164 ;; STATE-CHANGING FUNCTIONS
166 ;; * register (file &optional rev comment)
168 ;; Register FILE in this backend. Optionally, an initial revision REV
169 ;; and an initial description of the file, COMMENT, may be specified.
171 ;; - responsible-p (file)
173 ;; Return non-nil if this backend considers itself "responsible" for
174 ;; FILE, which can also be a directory. This function is used to find
175 ;; out what backend to use for registration of new files and for things
176 ;; like change log generation. The default implementation always
177 ;; returns nil.
179 ;; - could-register (file)
181 ;; Return non-nil if FILE could be registered under this backend. The
182 ;; default implementation always returns t.
184 ;; - receive-file (file rev)
186 ;; Let this backend "receive" a file that is already registered under
187 ;; another backend. The default implementation simply calls `register'
188 ;; for FILE, but it can be overridden to do something more specific,
189 ;; e.g. keep revision numbers consistent or choose editing modes for
190 ;; FILE that resemble those of the other backend.
192 ;; - unregister (file)
194 ;; Unregister FILE from this backend. This is only needed if this
195 ;; backend may be used as a "more local" backend for temporary editing.
197 ;; * checkin (file rev comment)
199 ;; Commit changes in FILE to this backend. If REV is non-nil, that
200 ;; should become the new revision number. COMMENT is used as a
201 ;; check-in comment.
203 ;; * checkout (file &optional editable rev destfile)
205 ;; Check out revision REV of FILE into the working area. If EDITABLE
206 ;; is non-nil, FILE should be writable by the user and if locking is
207 ;; used for FILE, a lock should also be set. If REV is non-nil, that
208 ;; is the revision to check out (default is current workfile version);
209 ;; if REV is the empty string, that means to check out the head of the
210 ;; trunk. If optional arg DESTFILE is given, it is an alternate
211 ;; filename to write the contents to.
213 ;; * revert (file &optional contents-done)
215 ;; Revert FILE back to the current workfile version. If optional
216 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
217 ;; already been reverted from a version backup, and this function
218 ;; only needs to update the status of FILE within the backend.
220 ;; - cancel-version (file editable)
222 ;; Cancel the current workfile version of FILE, i.e. remove it from the
223 ;; master. EDITABLE non-nil means that FILE should be writable
224 ;; afterwards, and if locking is used for FILE, then a lock should also
225 ;; be set. If this function is not provided, trying to cancel a
226 ;; version is caught as an error.
228 ;; - merge (file rev1 rev2)
230 ;; Merge the changes between REV1 and REV2 into the current working file.
232 ;; - merge-news (file)
234 ;; Merge recent changes from the current branch into FILE.
236 ;; - steal-lock (file &optional version)
238 ;; Steal any lock on the current workfile version of FILE, or on
239 ;; VERSION if that is provided. This function is only needed if
240 ;; locking is used for files under this backend, and if files can
241 ;; indeed be locked by other users.
243 ;; HISTORY FUNCTIONS
245 ;; * print-log (file)
247 ;; Insert the revision log of FILE into the *vc* buffer.
249 ;; - show-log-entry (version)
251 ;; If provided, search the log entry for VERSION in the current buffer,
252 ;; and make sure it is displayed in the buffer's window. The default
253 ;; implementation of this function works for RCS-style logs.
255 ;; - wash-log (file)
257 ;; Remove all non-comment information from the output of print-log. The
258 ;; default implementation of this function works for RCS-style logs.
260 ;; - logentry-check ()
262 ;; If defined, this function is run to find out whether the user
263 ;; entered a valid log entry for check-in. The log entry is in the
264 ;; current buffer, and if it is not a valid one, the function should
265 ;; throw an error.
267 ;; - comment-history (file)
269 ;; Return a string containing all log entries that were made for FILE.
270 ;; This is used for transferring a file from one backend to another,
271 ;; retaining comment information. The default implementation of this
272 ;; function does this by calling print-log and then wash-log, and
273 ;; returning the resulting buffer contents as a string.
275 ;; - update-changelog (files)
277 ;; Using recent log entries, create ChangeLog entries for FILES, or for
278 ;; all files at or below the default-directory if FILES is nil. The
279 ;; default implementation runs rcs2log, which handles RCS- and
280 ;; CVS-style logs.
282 ;; * diff (file &optional rev1 rev2)
284 ;; Insert the diff for FILE into the *vc-diff* buffer. If REV1 and REV2
285 ;; are non-nil, report differences from REV1 to REV2. If REV1 is nil,
286 ;; use the current workfile version (as found in the repository) as the
287 ;; older version; if REV2 is nil, use the current workfile contents as
288 ;; the newer version. This function should return a status of either 0
289 ;; (no differences found), or 1 (either non-empty diff or the diff is
290 ;; run asynchronously).
292 ;; - annotate-command (file buf rev)
294 ;; If this function is provided, it should produce an annotated version
295 ;; of FILE in BUF, relative to version REV. This is currently only
296 ;; implemented for CVS, using the `cvs annotate' command.
298 ;; - annotate-difference (point)
300 ;; Only required if `annotate-command' is defined for the backend.
301 ;; Return the difference between the age of the line at point and the
302 ;; current time. Return NIL if there is no more comparison to be made
303 ;; in the buffer. Return value as defined for `current-time'. You can
304 ;; safely assume that point is placed at the beginning of each line,
305 ;; starting at `point-min'. The buffer that point is placed in is the
306 ;; Annotate output, as defined by the relevant backend.
308 ;; SNAPSHOT SYSTEM
310 ;; - create-snapshot (dir name branchp)
312 ;; Take a snapshot of the current state of files under DIR and name it
313 ;; NAME. This should make sure that files are up-to-date before
314 ;; proceeding with the action. DIR can also be a file and if BRANCHP
315 ;; is specified, NAME should be created as a branch and DIR should be
316 ;; checked out under this new branch. The default implementation does
317 ;; not support branches but does a sanity check, a tree traversal and
318 ;; for each file calls `assign-name'.
320 ;; - assign-name (file name)
322 ;; Give name NAME to the current version of FILE, assuming it is
323 ;; up-to-date. Only used by the default version of `create-snapshot'.
325 ;; - retrieve-snapshot (dir name update)
327 ;; Retrieve a named snapshot of all registered files at or below DIR.
328 ;; If UPDATE is non-nil, then update buffers of any files in the
329 ;; snapshot that are currently visited. The default implementation
330 ;; does a sanity check whether there aren't any uncommitted changes at
331 ;; or below DIR, and then performs a tree walk, using the `checkout'
332 ;; function to retrieve the corresponding versions.
334 ;; MISCELLANEOUS
336 ;; - make-version-backups-p (file)
338 ;; Return non-nil if unmodified repository versions of FILE should be
339 ;; backed up locally. If this is done, VC can perform `diff' and
340 ;; `revert' operations itself, without calling the backend system. The
341 ;; default implementation always returns nil.
343 ;; - check-headers ()
345 ;; Return non-nil if the current buffer contains any version headers.
347 ;; - clear-headers ()
349 ;; In the current buffer, reset all version headers to their unexpanded
350 ;; form. This function should be provided if the state-querying code
351 ;; for this backend uses the version headers to determine the state of
352 ;; a file. This function will then be called whenever VC changes the
353 ;; version control state in such a way that the headers would give
354 ;; wrong information.
356 ;; - rename-file (old new)
358 ;; Rename file OLD to NEW, both in the working area and in the
359 ;; repository. If this function is not provided, the command
360 ;; `vc-rename-file' will signal an error.
362 ;;; Code:
364 (require 'vc-hooks)
365 (require 'ring)
366 (eval-when-compile
367 (require 'cl)
368 (require 'compile)
369 (require 'dired) ; for dired-map-over-marks macro
370 (require 'dired-aux)) ; for dired-kill-{line,tree}
372 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
373 (setq minor-mode-alist
374 (cons '(vc-parent-buffer vc-parent-buffer-name)
375 minor-mode-alist)))
377 ;; General customization
379 (defgroup vc nil
380 "Version-control system in Emacs."
381 :group 'tools)
383 (defcustom vc-suppress-confirm nil
384 "*If non-nil, treat user as expert; suppress yes-no prompts on some things."
385 :type 'boolean
386 :group 'vc)
388 (defcustom vc-delete-logbuf-window t
389 "*If non-nil, delete the *VC-log* buffer and window after each logical action.
390 If nil, bury that buffer instead.
391 This is most useful if you have multiple windows on a frame and would like to
392 preserve the setting."
393 :type 'boolean
394 :group 'vc)
396 (defcustom vc-initial-comment nil
397 "*If non-nil, prompt for initial comment when a file is registered."
398 :type 'boolean
399 :group 'vc)
401 (defcustom vc-default-init-version "1.1"
402 "*A string used as the default version number when a new file is registered.
403 This can be overridden by giving a prefix argument to \\[vc-register]."
404 :type 'string
405 :group 'vc
406 :version "20.3")
408 (defcustom vc-command-messages nil
409 "*If non-nil, display run messages from back-end commands."
410 :type 'boolean
411 :group 'vc)
413 (defcustom vc-checkin-switches nil
414 "*A string or list of strings specifying extra switches for checkin.
415 These are passed to the checkin program by \\[vc-checkin]."
416 :type '(choice (const :tag "None" nil)
417 (string :tag "Argument String")
418 (repeat :tag "Argument List"
419 :value ("")
420 string))
421 :group 'vc)
423 (defcustom vc-checkout-switches nil
424 "*A string or list of strings specifying extra switches for checkout.
425 These are passed to the checkout program by \\[vc-checkout]."
426 :type '(choice (const :tag "None" nil)
427 (string :tag "Argument String")
428 (repeat :tag "Argument List"
429 :value ("")
430 string))
431 :group 'vc)
433 (defcustom vc-register-switches nil
434 "*A string or list of strings; extra switches for registering a file.
435 These are passed to the checkin program by \\[vc-register]."
436 :type '(choice (const :tag "None" nil)
437 (string :tag "Argument String")
438 (repeat :tag "Argument List"
439 :value ("")
440 string))
441 :group 'vc)
443 (defcustom vc-dired-listing-switches "-al"
444 "*Switches passed to `ls' for vc-dired. MUST contain the `l' option."
445 :type 'string
446 :group 'vc
447 :version "21.1")
449 (defcustom vc-dired-recurse t
450 "*If non-nil, show directory trees recursively in VC Dired."
451 :type 'boolean
452 :group 'vc
453 :version "20.3")
455 (defcustom vc-dired-terse-display t
456 "*If non-nil, show only locked files in VC Dired."
457 :type 'boolean
458 :group 'vc
459 :version "20.3")
461 (defcustom vc-directory-exclusion-list '("SCCS" "RCS" "CVS")
462 "*List of directory names to be ignored while recursively walking file trees."
463 :type '(repeat string)
464 :group 'vc)
466 (defconst vc-maximum-comment-ring-size 32
467 "Maximum number of saved comments in the comment ring.")
469 ;; This is duplicated in diff.el.
470 (defvar diff-switches "-c"
471 "*A string or list of strings specifying switches to be passed to diff.")
473 (defcustom vc-diff-switches nil
474 "*A string or list of strings specifying switches for diff under VC.
475 There is also an option vc-BACKEND-diff-switches for each BACKEND that
476 VC can handle."
477 :type '(choice (const :tag "None" nil)
478 (string :tag "Argument String")
479 (repeat :tag "Argument List"
480 :value ("")
481 string))
482 :group 'vc
483 :version "21.1")
485 ;;;###autoload
486 (defcustom vc-checkout-hook nil
487 "*Normal hook (list of functions) run after a file has been checked out.
488 See `run-hooks'."
489 :type 'hook
490 :group 'vc
491 :version "21.1")
493 ;;;###autoload
494 (defcustom vc-checkin-hook nil
495 "*Normal hook (list of functions) run after a checkin is done.
496 See `run-hooks'."
497 :type 'hook
498 :options '(vc-comment-to-change-log)
499 :group 'vc)
501 ;;;###autoload
502 (defcustom vc-before-checkin-hook nil
503 "*Normal hook (list of functions) run before a file gets checked in.
504 See `run-hooks'."
505 :type 'hook
506 :group 'vc)
508 (defcustom vc-logentry-check-hook nil
509 "*Normal hook run by `vc-backend-logentry-check'.
510 Use this to impose your own rules on the entry in addition to any the
511 version control backend imposes itself."
512 :type 'hook
513 :group 'vc)
515 ;; Annotate customization
516 (defcustom vc-annotate-color-map
517 '(( 26.3672 . "#FF0000")
518 ( 52.7344 . "#FF3800")
519 ( 79.1016 . "#FF7000")
520 (105.4688 . "#FFA800")
521 (131.8359 . "#FFE000")
522 (158.2031 . "#E7FF00")
523 (184.5703 . "#AFFF00")
524 (210.9375 . "#77FF00")
525 (237.3047 . "#3FFF00")
526 (263.6719 . "#07FF00")
527 (290.0391 . "#00FF31")
528 (316.4063 . "#00FF69")
529 (342.7734 . "#00FFA1")
530 (369.1406 . "#00FFD9")
531 (395.5078 . "#00EEFF")
532 (421.8750 . "#00B6FF")
533 (448.2422 . "#007EFF"))
534 "*Association list of age versus color, for \\[vc-annotate].
535 Ages are given in units of 2**-16 seconds.
536 Default is eighteen steps using a twenty day increment."
537 :type 'alist
538 :group 'vc)
540 (defcustom vc-annotate-very-old-color "#0046FF"
541 "*Color for lines older than CAR of last cons in `vc-annotate-color-map'."
542 :type 'string
543 :group 'vc)
545 (defcustom vc-annotate-background "black"
546 "*Background color for \\[vc-annotate].
547 Default color is used if nil."
548 :type 'string
549 :group 'vc)
551 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
552 "*Menu elements for the mode-specific menu of VC-Annotate mode.
553 List of factors, used to expand/compress the time scale. See `vc-annotate'."
554 :type '(repeat number)
555 :group 'vc)
557 ;; vc-annotate functionality (CVS only).
558 (defvar vc-annotate-mode nil
559 "Variable indicating if VC-Annotate mode is active.")
561 (defvar vc-annotate-mode-map
562 (let ((m (make-sparse-keymap)))
563 (define-key m [menu-bar] (make-sparse-keymap "VC-Annotate"))
565 "Local keymap used for VC-Annotate mode.")
567 (defvar vc-annotate-mode-menu nil
568 "Local keymap used for VC-Annotate mode's menu bar menu.")
570 ;; Header-insertion hair
572 (defcustom vc-static-header-alist
573 '(("\\.c$" .
574 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
575 "*Associate static header string templates with file types.
576 A \%s in the template is replaced with the first string associated with
577 the file's version control type in `vc-header-alist'."
578 :type '(repeat (cons :format "%v"
579 (regexp :tag "File Type")
580 (string :tag "Header String")))
581 :group 'vc)
583 (defcustom vc-comment-alist
584 '((nroff-mode ".\\\"" ""))
585 "*Special comment delimiters to be used in generating vc headers only.
586 Add an entry in this list if you need to override the normal `comment-start'
587 and `comment-end' variables. This will only be necessary if the mode language
588 is sensitive to blank lines."
589 :type '(repeat (list :format "%v"
590 (symbol :tag "Mode")
591 (string :tag "Comment Start")
592 (string :tag "Comment End")))
593 :group 'vc)
595 ;; Default is to be extra careful for super-user.
596 ;; TODO: This variable is no longer used; the corresponding checks
597 ;; are always done now. If that turns out to be fast enough,
598 ;; the variable can be obsoleted.
599 (defcustom vc-checkout-carefully (= (user-uid) 0)
600 "*Non-nil means be extra-careful in checkout.
601 Verify that the file really is not locked
602 and that its contents match what the master file says."
603 :type 'boolean
604 :group 'vc)
607 ;; The main keymap
609 ;; Initialization code, to be done just once at load-time
610 (defvar vc-log-mode-map
611 (let ((map (make-sparse-keymap)))
612 (define-key map "\M-n" 'vc-next-comment)
613 (define-key map "\M-p" 'vc-previous-comment)
614 (define-key map "\M-r" 'vc-comment-search-reverse)
615 (define-key map "\M-s" 'vc-comment-search-forward)
616 (define-key map "\C-c\C-c" 'vc-finish-logentry)
617 map))
618 ;; Compatibility with old name. Should we bother ?
619 (defvar vc-log-entry-mode vc-log-mode-map)
622 ;; Variables the user doesn't need to know about.
623 (defvar vc-log-operation nil)
624 (defvar vc-log-after-operation-hook nil)
625 (defvar vc-annotate-buffers nil
626 "Alist of current \"Annotate\" buffers and their corresponding backends.
627 The keys are \(BUFFER . BACKEND\). See also `vc-annotate-get-backend'.")
628 ;; In a log entry buffer, this is a local variable
629 ;; that points to the buffer for which it was made
630 ;; (either a file, or a VC dired buffer).
631 (defvar vc-parent-buffer nil)
632 (put 'vc-parent-buffer 'permanent-local t)
633 (defvar vc-parent-buffer-name nil)
634 (put 'vc-parent-buffer-name 'permanent-local t)
636 (defvar vc-log-file)
637 (defvar vc-log-version)
639 (defvar vc-dired-mode nil)
640 (make-variable-buffer-local 'vc-dired-mode)
642 (defvar vc-comment-ring (make-ring vc-maximum-comment-ring-size))
643 (defvar vc-comment-ring-index nil)
644 (defvar vc-last-comment-match "")
646 ;; functions that operate on RCS revision numbers. This code should
647 ;; also be moved into the backends. It stays for now, however, since
648 ;; it is used in code below.
649 (defun vc-trunk-p (rev)
650 "Return t if REV is a revision on the trunk."
651 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
653 (defun vc-branch-p (rev)
654 "Return t if REV is a branch revision."
655 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
657 (defun vc-branch-part (rev)
658 "Return the branch part of a revision number REV."
659 (substring rev 0 (string-match "\\.[0-9]+\\'" rev)))
661 (defun vc-minor-part (rev)
662 "Return the minor version number of a revision number REV."
663 (string-match "[0-9]+\\'" rev)
664 (substring rev (match-beginning 0) (match-end 0)))
666 (defun vc-previous-version (rev)
667 "Guess the version number immediately preceding REV."
668 (let ((branch (vc-branch-part rev))
669 (minor-num (string-to-number (vc-minor-part rev))))
670 (if (> minor-num 1)
671 ;; version does probably not start a branch or release
672 (concat branch "." (number-to-string (1- minor-num)))
673 (if (vc-trunk-p rev)
674 ;; we are at the beginning of the trunk --
675 ;; don't know anything to return here
677 ;; we are at the beginning of a branch --
678 ;; return version of starting point
679 (vc-branch-part branch)))))
681 ;; File property caching
683 (defun vc-clear-context ()
684 "Clear all cached file properties and the comment ring."
685 (interactive)
686 (fillarray vc-file-prop-obarray nil)
687 ;; Note: there is potential for minor lossage here if there is an open
688 ;; log buffer with a nonzero local value of vc-comment-ring-index.
689 (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
691 (defmacro with-vc-properties (file form settings)
692 "Execute FORM, then set per-file properties for FILE,
693 but only those that have not been set during the execution of FORM.
694 SETTINGS is a list of two-element lists, each of which has the
695 form (PROPERTY . VALUE)."
696 `(let ((vc-touched-properties (list t))
697 (filename ,file))
698 ,form
699 (mapcar (lambda (setting)
700 (let ((property (car setting)))
701 (unless (memq property vc-touched-properties)
702 (put (intern filename vc-file-prop-obarray)
703 property (cdr setting)))))
704 ,settings)))
706 ;; Random helper functions
708 (defsubst vc-editable-p (file)
709 (or (eq (vc-checkout-model file) 'implicit)
710 (memq (vc-state file) '(edited needs-merge))))
712 ;; Two macros for elisp programming
713 ;;;###autoload
714 (defmacro with-vc-file (file comment &rest body)
715 "Check out a writable copy of FILE if necessary and execute the body.
716 Check in FILE with COMMENT (a string) after BODY has been executed.
717 FILE is passed through `expand-file-name'; BODY executed within
718 `save-excursion'. If FILE is not under version control, or locked by
719 somebody else, signal error."
720 `(let ((file (expand-file-name ,file)))
721 (or (vc-backend file)
722 (error (format "File not under version control: `%s'" file)))
723 (unless (vc-editable-p file)
724 (let ((state (vc-state file)))
725 (if (stringp state) (error (format "`%s' is locking `%s'" state file))
726 (vc-checkout file t))))
727 (save-excursion
728 ,@body)
729 (vc-checkin file nil ,comment)))
730 (put 'with-vc-file 'indent-function 1)
732 ;;;###autoload
733 (defmacro edit-vc-file (file comment &rest body)
734 "Edit FILE under version control, executing body.
735 Checkin with COMMENT after executing BODY.
736 This macro uses `with-vc-file', passing args to it.
737 However, before executing BODY, find FILE, and after BODY, save buffer."
738 `(with-vc-file
739 ,file ,comment
740 (set-buffer (find-file-noselect ,file))
741 ,@body
742 (save-buffer)))
743 (put 'edit-vc-file 'indent-function 1)
745 (defun vc-ensure-vc-buffer ()
746 "Make sure that the current buffer visits a version-controlled file."
747 (if vc-dired-mode
748 (set-buffer (find-file-noselect (dired-get-filename)))
749 (while vc-parent-buffer
750 (pop-to-buffer vc-parent-buffer))
751 (if (not (buffer-file-name))
752 (error "Buffer %s is not associated with a file" (buffer-name))
753 (if (not (vc-backend (buffer-file-name)))
754 (error "File %s is not under version control" (buffer-file-name))))))
756 (defvar vc-binary-assoc nil)
757 (defvar vc-binary-suffixes
758 (if (memq system-type '(ms-dos windows-nt))
759 '(".exe" ".com" ".bat" ".cmd" ".btm" "")
760 '("")))
762 (defun vc-process-filter (p s)
763 "An alternative output filter for async process P.
764 The only difference with the default filter is to insert S after markers."
765 (with-current-buffer (process-buffer p)
766 (save-excursion
767 (let ((inhibit-read-only t))
768 (goto-char (process-mark p))
769 (insert s)
770 (set-marker (process-mark p) (point))))))
772 (defun vc-setup-buffer (&optional buf)
773 "Prepare BUF for executing a VC command and make it the current buffer.
774 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
775 (unless buf (setq buf "*vc*"))
776 (let ((camefrom (current-buffer))
777 (olddir default-directory))
778 (set-buffer (get-buffer-create buf))
779 (kill-all-local-variables)
780 (set (make-local-variable 'vc-parent-buffer) camefrom)
781 (set (make-local-variable 'vc-parent-buffer-name)
782 (concat " from " (buffer-name camefrom)))
783 (setq default-directory olddir)
784 (let ((inhibit-read-only t))
785 (erase-buffer))))
787 (defun vc-exec-after (code)
788 "Eval CODE when the current buffer's process is done.
789 If the current buffer has no process, just evaluate CODE.
790 Else, add CODE to the process' sentinel."
791 (let ((proc (get-buffer-process (current-buffer))))
792 (cond
793 ;; If there's no background process, just execute the code.
794 ((null proc) (eval code))
795 ;; If the background process has exited, reap it and try again
796 ((eq (process-status proc) 'exit)
797 (delete-process proc)
798 (vc-exec-after code))
799 ;; If a process is running, add CODE to the sentinel
800 ((eq (process-status proc) 'run)
801 (let ((sentinel (process-sentinel proc)))
802 (set-process-sentinel proc
803 `(lambda (p s)
804 (with-current-buffer ',(current-buffer)
805 (goto-char (process-mark p))
806 ,@(append (cdr (cdr (cdr ;strip off `with-current-buffer buf
807 ; (goto-char...)'
808 (car (cdr (cdr ;strip off `lambda (p s)'
809 sentinel))))))
810 (list `(vc-exec-after ',code))))))))
811 (t (error "Unexpected process state"))))
812 nil)
814 (defvar vc-post-command-functions nil
815 "Hook run at the end of `vc-do-command'.
816 Each function is called inside the buffer in which the command was run
817 and is passed 3 argument: the COMMAND, the FILE and the FLAGS.")
819 ;;;###autoload
820 (defun vc-do-command (buffer okstatus command file &rest flags)
821 "Execute a version control command, notifying user and checking for errors.
822 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
823 current buffer if BUFFER is t. If the destination buffer is not
824 already current, set it up properly and erase it. The command is
825 considered successful if its exit status does not exceed OKSTATUS (if
826 OKSTATUS is nil, that means to ignore errors, if it is 'async, that
827 means not to wait for termination of the subprocess). FILE is the
828 name of the working file (may also be nil, to execute commands that
829 don't expect a file name). If an optional list of FLAGS is present,
830 that is inserted into the command line before the filename."
831 (and file (setq file (expand-file-name file)))
832 (if vc-command-messages
833 (message "Running %s on %s..." command file))
834 (save-current-buffer
835 (unless (or (eq buffer t)
836 (and (stringp buffer)
837 (string= (buffer-name) buffer))
838 (eq buffer (current-buffer)))
839 (vc-setup-buffer buffer))
840 (let ((squeezed nil)
841 (inhibit-read-only t)
842 (status 0))
843 (setq squeezed (delq nil (copy-sequence flags)))
844 (when file
845 ;; FIXME: file-relative-name can return a bogus result because
846 ;; it doesn't look at the actual file-system to see if symlinks
847 ;; come into play.
848 (setq squeezed (append squeezed (list (file-relative-name file)))))
849 (let ((exec-path (append vc-path exec-path))
850 ;; Add vc-path to PATH for the execution of this command.
851 (process-environment
852 (cons (concat "PATH=" (getenv "PATH")
853 path-separator
854 (mapconcat 'identity vc-path path-separator))
855 process-environment))
856 (w32-quote-process-args t))
857 (if (eq okstatus 'async)
858 (let ((proc (apply 'start-process command (current-buffer) command
859 squeezed)))
860 (unless (active-minibuffer-window)
861 (message "Running %s in the background..." command))
862 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
863 (set-process-filter proc 'vc-process-filter)
864 (vc-exec-after
865 `(unless (active-minibuffer-window)
866 (message "Running %s in the background... done" ',command))))
867 (setq status (apply 'call-process command nil t nil squeezed))
868 (when (or (not (integerp status)) (and okstatus (< okstatus status)))
869 (pop-to-buffer (current-buffer))
870 (goto-char (point-min))
871 (shrink-window-if-larger-than-buffer)
872 (error "Running %s...FAILED (%s)" command
873 (if (integerp status) (format "status %d" status) status))))
874 (if vc-command-messages
875 (message "Running %s...OK" command)))
876 (vc-exec-after
877 `(run-hook-with-args 'vc-post-command-functions ',command ',file ',flags))
878 status)))
880 (defun vc-position-context (posn)
881 "Save a bit of the text around POSN in the current buffer.
882 Used to help us find the corresponding position again later
883 if markers are destroyed or corrupted."
884 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
885 ;; rcs.el mode.
886 (list posn
887 (buffer-size)
888 (buffer-substring posn
889 (min (point-max) (+ posn 100)))))
891 (defun vc-find-position-by-context (context)
892 "Return the position of CONTEXT in the current buffer, or nil if not found."
893 (let ((context-string (nth 2 context)))
894 (if (equal "" context-string)
895 (point-max)
896 (save-excursion
897 (let ((diff (- (nth 1 context) (buffer-size))))
898 (if (< diff 0) (setq diff (- diff)))
899 (goto-char (nth 0 context))
900 (if (or (search-forward context-string nil t)
901 ;; Can't use search-backward since the match may continue
902 ;; after point.
903 (progn (goto-char (- (point) diff (length context-string)))
904 ;; goto-char doesn't signal an error at
905 ;; beginning of buffer like backward-char would
906 (search-forward context-string nil t)))
907 ;; to beginning of OSTRING
908 (- (point) (length context-string))))))))
910 (defun vc-context-matches-p (posn context)
911 "Return t if POSN matches CONTEXT, nil otherwise."
912 (let* ((context-string (nth 2 context))
913 (len (length context-string))
914 (end (+ posn len)))
915 (if (> end (1+ (buffer-size)))
917 (string= context-string (buffer-substring posn end)))))
919 (defun vc-buffer-context ()
920 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
921 Used by `vc-restore-buffer-context' to later restore the context."
922 (let ((point-context (vc-position-context (point)))
923 ;; Use mark-marker to avoid confusion in transient-mark-mode.
924 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
925 (vc-position-context (mark-marker))))
926 ;; Make the right thing happen in transient-mark-mode.
927 (mark-active nil)
928 ;; We may want to reparse the compilation buffer after revert
929 (reparse (and (boundp 'compilation-error-list) ;compile loaded
930 (let ((curbuf (current-buffer)))
931 ;; Construct a list; each elt is nil or a buffer
932 ;; iff that buffer is a compilation output buffer
933 ;; that contains markers into the current buffer.
934 (save-excursion
935 (mapcar (lambda (buffer)
936 (set-buffer buffer)
937 (let ((errors (or
938 compilation-old-error-list
939 compilation-error-list))
940 (buffer-error-marked-p nil))
941 (while (and (consp errors)
942 (not buffer-error-marked-p))
943 (and (markerp (cdr (car errors)))
944 (eq buffer
945 (marker-buffer
946 (cdr (car errors))))
947 (setq buffer-error-marked-p t))
948 (setq errors (cdr errors)))
949 (if buffer-error-marked-p buffer)))
950 (buffer-list)))))))
951 (list point-context mark-context reparse)))
953 (defun vc-restore-buffer-context (context)
954 "Restore point/mark, and reparse any affected compilation buffers.
955 CONTEXT is that which `vc-buffer-context' returns."
956 (let ((point-context (nth 0 context))
957 (mark-context (nth 1 context))
958 (reparse (nth 2 context)))
959 ;; Reparse affected compilation buffers.
960 (while reparse
961 (if (car reparse)
962 (with-current-buffer (car reparse)
963 (let ((compilation-last-buffer (current-buffer)) ;select buffer
964 ;; Record the position in the compilation buffer of
965 ;; the last error next-error went to.
966 (error-pos (marker-position
967 (car (car-safe compilation-error-list)))))
968 ;; Reparse the error messages as far as they were parsed before.
969 (compile-reinitialize-errors '(4) compilation-parsing-end)
970 ;; Move the pointer up to find the error we were at before
971 ;; reparsing. Now next-error should properly go to the next one.
972 (while (and compilation-error-list
973 (/= error-pos (car (car compilation-error-list))))
974 (setq compilation-error-list (cdr compilation-error-list))))))
975 (setq reparse (cdr reparse)))
977 ;; if necessary, restore point and mark
978 (if (not (vc-context-matches-p (point) point-context))
979 (let ((new-point (vc-find-position-by-context point-context)))
980 (if new-point (goto-char new-point))))
981 (and mark-active
982 mark-context
983 (not (vc-context-matches-p (mark) mark-context))
984 (let ((new-mark (vc-find-position-by-context mark-context)))
985 (if new-mark (set-mark new-mark))))))
987 (defun vc-revert-buffer1 (&optional arg no-confirm)
988 "Revert buffer, trying to keep point and mark where user expects them.
989 Tries to be clever in the face of changes due to expanded version control
990 key words. This is important for typeahead to work as expected.
991 ARG and NO-CONFIRM are passed on to `revert-buffer'."
992 (interactive "P")
993 (widen)
994 (let ((context (vc-buffer-context)))
995 ;; Use save-excursion here, because it may be able to restore point
996 ;; and mark properly even in cases where vc-restore-buffer-context
997 ;; would fail. However, save-excursion might also get it wrong --
998 ;; in this case, vc-restore-buffer-context gives it a second try.
999 (save-excursion
1000 ;; t means don't call normal-mode;
1001 ;; that's to preserve various minor modes.
1002 (revert-buffer arg no-confirm t))
1003 (vc-restore-buffer-context context)))
1006 (defun vc-buffer-sync (&optional not-urgent)
1007 "Make sure the current buffer and its working file are in sync.
1008 NOT-URGENT means it is ok to continue if the user says not to save."
1009 (if (buffer-modified-p)
1010 (if (or vc-suppress-confirm
1011 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1012 (save-buffer)
1013 (unless not-urgent
1014 (error "Aborted")))))
1016 (defun vc-workfile-unchanged-p (file)
1017 "Has FILE changed since last checkout?"
1018 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
1019 (lastmod (nth 5 (file-attributes file))))
1020 (if checkout-time
1021 (equal checkout-time lastmod)
1022 (let ((unchanged (vc-call workfile-unchanged-p file)))
1023 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
1024 unchanged))))
1026 (defun vc-default-workfile-unchanged-p (backend file)
1027 "Default check whether FILE is unchanged: diff against master version."
1028 (zerop (vc-call diff file (vc-workfile-version file))))
1030 (defun vc-default-latest-on-branch-p (backend file)
1031 "Default check whether the current workfile version of FILE is the
1032 latest on its branch."
1035 (defun vc-recompute-state (file)
1036 "Force a recomputation of the version control state of FILE.
1037 The state is computed using the exact, and possibly expensive
1038 function `vc-BACKEND-state', not the heuristic."
1039 (vc-file-setprop file 'vc-state (vc-call state file)))
1041 (defun vc-next-action-on-file (file verbose &optional comment)
1042 "Do The Right Thing for a given version-controlled FILE.
1043 If COMMENT is specified, it will be used as an admin or checkin comment.
1044 If VERBOSE is non-nil, query the user rather than using default parameters."
1045 (let ((visited (get-file-buffer file))
1046 state version)
1047 (when visited
1048 ;; Check relation of buffer and file, and make sure
1049 ;; user knows what he's doing. First, finding the file
1050 ;; will check whether the file on disk is newer.
1051 (if vc-dired-mode
1052 (find-file-other-window file)
1053 (set-buffer (find-file-noselect file)))
1054 (if (not (verify-visited-file-modtime (current-buffer)))
1055 (if (yes-or-no-p "Replace file on disk with buffer contents? ")
1056 (write-file (buffer-file-name))
1057 (error "Aborted"))
1058 ;; Now, check if we have unsaved changes.
1059 (vc-buffer-sync t)
1060 (if (buffer-modified-p)
1061 (or (y-or-n-p "Operate on disk file, keeping modified buffer? ")
1062 (error "Aborted")))))
1064 ;; Do the right thing
1065 (if (not (vc-registered file))
1066 (vc-register verbose comment)
1067 (vc-recompute-state file)
1068 (if visited (vc-mode-line file))
1069 (setq state (vc-state file))
1070 (cond
1071 ;; up-to-date
1072 ((or (eq state 'up-to-date)
1073 (and verbose (eq state 'needs-patch)))
1074 (cond
1075 (verbose
1076 ;; go to a different version
1077 (setq version
1078 (read-string "Branch, version, or backend to move to: "))
1079 (let ((vsym (intern-soft (upcase version))))
1080 (if (member vsym vc-handled-backends)
1081 (vc-transfer-file file vsym)
1082 (vc-checkout file (eq (vc-checkout-model file) 'implicit)
1083 version))))
1084 ((not (eq (vc-checkout-model file) 'implicit))
1085 ;; check the file out
1086 (vc-checkout file t))
1088 ;; do nothing
1089 (message "%s is up-to-date" file))))
1091 ;; Abnormal: edited but read-only
1092 ((and visited (eq state 'edited)
1093 buffer-read-only (not (file-writable-p file)))
1094 ;; Make the file+buffer read-write. If the user really wanted to
1095 ;; commit, he'll get a chance to do that next time around, anyway.
1096 (message "File is edited but read-only; making it writable")
1097 (set-file-modes buffer-file-name
1098 (logior (file-modes buffer-file-name) 128))
1099 (toggle-read-only -1))
1101 ;; edited
1102 ((eq state 'edited)
1103 (cond
1104 ;; For files with locking, if the file does not contain
1105 ;; any changes, just let go of the lock, i.e. revert.
1106 ((and (not (eq (vc-checkout-model file) 'implicit))
1107 (vc-workfile-unchanged-p file)
1108 ;; If buffer is modified, that means the user just
1109 ;; said no to saving it; in that case, don't revert,
1110 ;; because the user might intend to save after
1111 ;; finishing the log entry.
1112 (not (and visited (buffer-modified-p))))
1113 ;; DO NOT revert the file without asking the user!
1114 (if (not visited) (find-file-other-window file))
1115 (if (yes-or-no-p "Revert to master version? ")
1116 (vc-revert-buffer)))
1117 (t ;; normal action
1118 (if (not verbose)
1119 (vc-checkin file nil comment)
1120 (setq version (read-string "New version or backend: "))
1121 (let ((vsym (intern (upcase version))))
1122 (if (member vsym vc-handled-backends)
1123 (vc-transfer-file file vsym)
1124 (vc-checkin file version comment)))))))
1126 ;; locked by somebody else
1127 ((stringp state)
1128 (if comment
1129 (error "Sorry, you can't steal the lock on %s this way"
1130 (file-name-nondirectory file)))
1131 (vc-steal-lock file
1132 (if verbose (read-string "Version to steal: ")
1133 (vc-workfile-version file))
1134 state))
1136 ;; needs-patch
1137 ((eq state 'needs-patch)
1138 (if (yes-or-no-p (format
1139 "%s is not up-to-date. Get latest version? "
1140 (file-name-nondirectory file)))
1141 (vc-checkout file (eq (vc-checkout-model file) 'implicit) "")
1142 (if (and (not (eq (vc-checkout-model file) 'implicit))
1143 (yes-or-no-p "Lock this version? "))
1144 (vc-checkout file t)
1145 (error "Aborted"))))
1147 ;; needs-merge
1148 ((eq state 'needs-merge)
1149 (if (yes-or-no-p (format
1150 "%s is not up-to-date. Merge in changes now? "
1151 (file-name-nondirectory file)))
1152 (vc-maybe-resolve-conflicts file (vc-call merge-news file))
1153 (error "Aborted")))
1155 ;; unlocked-changes
1156 ((eq state 'unlocked-changes)
1157 (if (not visited) (find-file-other-window file))
1158 (if (save-window-excursion
1159 (vc-version-diff file (vc-workfile-version file) nil)
1160 (goto-char (point-min))
1161 (let ((inhibit-read-only t))
1162 (insert-string
1163 (format "Changes to %s since last lock:\n\n" file)))
1164 (not (beep))
1165 (yes-or-no-p (concat "File has unlocked changes. "
1166 "Claim lock retaining changes? ")))
1167 (progn (vc-call steal-lock file)
1168 ;; Must clear any headers here because they wouldn't
1169 ;; show that the file is locked now.
1170 (vc-clear-headers file)
1171 (vc-mode-line file))
1172 (if (not (yes-or-no-p
1173 "Revert to checked-in version, instead? "))
1174 (error "Checkout aborted")
1175 (vc-revert-buffer1 t t)
1176 (vc-checkout file t))))))))
1178 (defvar vc-dired-window-configuration)
1180 (defun vc-next-action-dired (file rev comment)
1181 "Call `vc-next-action-on-file' on all the marked files.
1182 Ignores FILE and REV, but passes on COMMENT."
1183 (let ((dired-buffer (current-buffer))
1184 (dired-dir default-directory))
1185 (dired-map-over-marks
1186 (let ((file (dired-get-filename)))
1187 (message "Processing %s..." file)
1188 (vc-next-action-on-file file nil comment)
1189 (set-buffer dired-buffer)
1190 (set-window-configuration vc-dired-window-configuration)
1191 (message "Processing %s...done" file))
1192 nil t))
1193 (dired-move-to-filename))
1195 ;; Here's the major entry point.
1197 ;;;###autoload
1198 (defun vc-next-action (verbose)
1199 "Do the next logical checkin or checkout operation on the current file.
1201 If you call this from within a VC dired buffer with no files marked,
1202 it will operate on the file in the current line.
1204 If you call this from within a VC dired buffer, and one or more
1205 files are marked, it will accept a log message and then operate on
1206 each one. The log message will be used as a comment for any register
1207 or checkin operations, but ignored when doing checkouts. Attempted
1208 lock steals will raise an error.
1210 A prefix argument lets you specify the version number to use.
1212 For RCS and SCCS files:
1213 If the file is not already registered, this registers it for version
1214 control.
1215 If the file is registered and not locked by anyone, this checks out
1216 a writable and locked file ready for editing.
1217 If the file is checked out and locked by the calling user, this
1218 first checks to see if the file has changed since checkout. If not,
1219 it performs a revert.
1220 If the file has been changed, this pops up a buffer for entry
1221 of a log message; when the message has been entered, it checks in the
1222 resulting changes along with the log message as change commentary. If
1223 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1224 read-only copy of the changed file is left in place afterwards.
1225 If the file is registered and locked by someone else, you are given
1226 the option to steal the lock.
1228 For CVS files:
1229 If the file is not already registered, this registers it for version
1230 control. This does a \"cvs add\", but no \"cvs commit\".
1231 If the file is added but not committed, it is committed.
1232 If your working file is changed, but the repository file is
1233 unchanged, this pops up a buffer for entry of a log message; when the
1234 message has been entered, it checks in the resulting changes along
1235 with the logmessage as change commentary. A writable file is retained.
1236 If the repository file is changed, you are asked if you want to
1237 merge in the changes into your working copy."
1239 (interactive "P")
1240 (catch 'nogo
1241 (if vc-dired-mode
1242 (let ((files (dired-get-marked-files)))
1243 (set (make-local-variable 'vc-dired-window-configuration)
1244 (current-window-configuration))
1245 (if (string= ""
1246 (mapconcat
1247 (lambda (f)
1248 (if (not (vc-up-to-date-p f)) "@" ""))
1249 files ""))
1250 (vc-next-action-dired nil nil "dummy")
1251 (vc-start-entry nil nil nil nil
1252 "Enter a change comment for the marked files."
1253 'vc-next-action-dired))
1254 (throw 'nogo nil)))
1255 (while vc-parent-buffer
1256 (pop-to-buffer vc-parent-buffer))
1257 (if buffer-file-name
1258 (vc-next-action-on-file buffer-file-name verbose)
1259 (error "Buffer %s is not associated with a file" (buffer-name)))))
1261 ;; These functions help the vc-next-action entry point
1263 ;;;###autoload
1264 (defun vc-register (&optional set-version comment)
1265 "Register the current file into a version control system.
1266 With prefix argument SET-VERSION, allow user to specify initial version
1267 level. If COMMENT is present, use that as an initial comment.
1269 The version control system to use is found by cycling through the list
1270 `vc-handled-backends'. The first backend in that list which declares
1271 itself responsible for the file (usually because other files in that
1272 directory are already registered under that backend) will be used to
1273 register the file. If no backend declares itself responsible, the
1274 first backend that could register the file is used."
1275 (interactive "P")
1276 (unless buffer-file-name (error "No visited file"))
1277 (when (vc-backend buffer-file-name)
1278 (if (vc-registered buffer-file-name)
1279 (error "This file is already registered")
1280 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1281 (error "Aborted"))))
1282 ;; Watch out for new buffers of size 0: the corresponding file
1283 ;; does not exist yet, even though buffer-modified-p is nil.
1284 (if (and (not (buffer-modified-p))
1285 (zerop (buffer-size))
1286 (not (file-exists-p buffer-file-name)))
1287 (set-buffer-modified-p t))
1288 (vc-buffer-sync)
1290 (vc-start-entry buffer-file-name
1291 (if set-version
1292 (read-string (format "Initial version level for %s: "
1293 (buffer-name)))
1294 ;; TODO: Use backend-specific init version.
1295 vc-default-init-version)
1296 (or comment (not vc-initial-comment))
1298 "Enter initial comment."
1299 (lambda (file rev comment)
1300 (message "Registering %s... " file)
1301 (let ((backend (vc-responsible-backend file t)))
1302 (vc-file-clearprops file)
1303 (vc-call-backend backend 'register file rev comment)
1304 (vc-file-setprop file 'vc-backend backend)
1305 (unless vc-make-backup-files
1306 (make-local-variable 'backup-inhibited)
1307 (setq backup-inhibited t)))
1308 (message "Registering %s... done" file))))
1311 (defun vc-responsible-backend (file &optional register)
1312 "Return the name of a backend system that is responsible for FILE.
1313 The optional argument REGISTER means that a backend suitable for
1314 registration should be found.
1316 If REGISTER is nil, then if FILE is already registered, return the
1317 backend of FILE. If FILE is not registered, or a directory, then the
1318 first backend in `vc-handled-backends' that declares itself
1319 responsible for FILE is returned. If no backend declares itself
1320 responsible, return the first backend.
1322 If REGISTER is non-nil, return the first responsible backend under
1323 which FILE is not yet registered. If there is no such backend, return
1324 the first backend under which FILE is not yet registered, but could
1325 be registered."
1326 (if (not vc-handled-backends)
1327 (error "No handled backends"))
1328 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1329 (catch 'found
1330 ;; First try: find a responsible backend. If this is for registration,
1331 ;; it must be a backend under which FILE is not yet registered.
1332 (dolist (backend vc-handled-backends)
1333 (and (or (not register)
1334 (not (vc-call-backend backend 'registered file)))
1335 (vc-call-backend backend 'responsible-p file)
1336 (throw 'found backend)))
1337 ;; no responsible backend
1338 (if (not register)
1339 ;; if this is not for registration, the first backend must do
1340 (car vc-handled-backends)
1341 ;; for registration, we need to find a new backend that
1342 ;; could register FILE
1343 (dolist (backend vc-handled-backends)
1344 (and (not (vc-call-backend backend 'registered file))
1345 (vc-call-backend backend 'could-register file)
1346 (throw 'found backend)))
1347 (error "No backend that could register")))))
1349 (defun vc-default-responsible-p (backend file)
1350 "Indicate whether BACKEND is reponsible for FILE.
1351 The default is to return nil always."
1352 nil)
1354 (defun vc-default-could-register (backend file)
1355 "Return non-nil if BACKEND could be used to register FILE.
1356 The default implementation returns t for all files."
1359 (defun vc-resynch-window (file &optional keep noquery)
1360 "If FILE is in the current buffer, either revert or unvisit it.
1361 The choice between revert (to see expanded keywords) and unvisit depends on
1362 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1363 reverting. NOQUERY should be t *only* if it is known the only
1364 difference between the buffer and the file is due to version control
1365 rather than user editing!"
1366 (and (string= buffer-file-name file)
1367 (if keep
1368 (progn
1369 (vc-revert-buffer1 t noquery)
1370 ;; TODO: Adjusting view mode might no longer be necessary
1371 ;; after RMS change to files.el of 1999-08-08. Investigate
1372 ;; this when we install the new VC.
1373 (and view-read-only
1374 (if (file-writable-p file)
1375 (and view-mode
1376 (let ((view-old-buffer-read-only nil))
1377 (view-mode-exit)))
1378 (and (not view-mode)
1379 (not (eq (get major-mode 'mode-class) 'special))
1380 (view-mode-enter))))
1381 (vc-mode-line buffer-file-name))
1382 (kill-buffer (current-buffer)))))
1384 (defun vc-resynch-buffer (file &optional keep noquery)
1385 "If FILE is currently visited, resynch its buffer."
1386 (if (string= buffer-file-name file)
1387 (vc-resynch-window file keep noquery)
1388 (let ((buffer (get-file-buffer file)))
1389 (if buffer
1390 (with-current-buffer buffer
1391 (vc-resynch-window file keep noquery)))))
1392 (vc-dired-resynch-file file))
1394 (defun vc-start-entry (file rev comment initial-contents msg action &optional after-hook)
1395 "Accept a comment for an operation on FILE revision REV.
1396 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1397 action on close to ACTION. If COMMENT is a string and
1398 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1399 contents of the log entry buffer. If COMMENT is a string and
1400 INITIAL-CONTENTS is nil, do action immediately as if the user had
1401 entered COMMENT. If COMMENT is t, also do action immediately with an
1402 empty comment. Remember the file's buffer in `vc-parent-buffer'
1403 \(current one if no file). AFTER-HOOK specifies the local value
1404 for vc-log-operation-hook."
1405 (let ((parent (or (and file (get-file-buffer file)) (current-buffer))))
1406 (if vc-before-checkin-hook
1407 (if file
1408 (with-current-buffer parent
1409 (run-hooks 'vc-before-checkin-hook))
1410 (run-hooks 'vc-before-checkin-hook)))
1411 (if (and comment (not initial-contents))
1412 (set-buffer (get-buffer-create "*VC-log*"))
1413 (pop-to-buffer (get-buffer-create "*VC-log*")))
1414 (set (make-local-variable 'vc-parent-buffer) parent)
1415 (set (make-local-variable 'vc-parent-buffer-name)
1416 (concat " from " (buffer-name vc-parent-buffer)))
1417 (if file (vc-mode-line file))
1418 (vc-log-edit file)
1419 (make-local-variable 'vc-log-after-operation-hook)
1420 (if after-hook
1421 (setq vc-log-after-operation-hook after-hook))
1422 (setq vc-log-operation action)
1423 (setq vc-log-version rev)
1424 (when comment
1425 (erase-buffer)
1426 (when (stringp comment) (insert comment)))
1427 (if (or (not comment) initial-contents)
1428 (message "%s Type C-c C-c when done" msg)
1429 (vc-finish-logentry (eq comment t)))))
1431 (defun vc-checkout (file &optional writable rev)
1432 "Retrieve a copy of the revision REV of FILE.
1433 If WRITABLE is non-nil, make sure the retrieved file is writable.
1434 REV defaults to the latest revision.
1436 After check-out, runs the normal hook `vc-checkout-hook'."
1437 (and writable
1438 (not rev)
1439 (vc-call make-version-backups-p file)
1440 (vc-up-to-date-p file)
1441 (vc-make-version-backup file))
1442 (with-vc-properties
1443 file
1444 (condition-case err
1445 (vc-call checkout file writable rev)
1446 (file-error
1447 ;; Maybe the backend is not installed ;-(
1448 (when writable
1449 (let ((buf (get-file-buffer file)))
1450 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1451 (signal (car err) (cdr err))))
1452 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1453 (not writable))
1454 (if (vc-call latest-on-branch-p file)
1455 'up-to-date
1456 'needs-patch)
1457 'edited))
1458 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1459 (vc-resynch-buffer file t t)
1460 (run-hooks 'vc-checkout-hook))
1462 (defun vc-steal-lock (file rev owner)
1463 "Steal the lock on FILE."
1464 (let (file-description)
1465 (if rev
1466 (setq file-description (format "%s:%s" file rev))
1467 (setq file-description file))
1468 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1469 file-description owner)))
1470 (error "Steal canceled"))
1471 (compose-mail owner (format "Stolen lock on %s" file-description)
1472 nil nil nil nil
1473 (list (list 'vc-finish-steal file rev)))
1474 (setq default-directory (expand-file-name "~/"))
1475 (goto-char (point-max))
1476 (insert
1477 (format "I stole the lock on %s, " file-description)
1478 (current-time-string)
1479 ".\n")
1480 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1482 (defun vc-finish-steal (file version)
1483 ;; This is called when the notification has been sent.
1484 (message "Stealing lock on %s..." file)
1485 (with-vc-properties
1486 file
1487 (vc-call steal-lock file version)
1488 `((vc-state . edited)))
1489 (vc-resynch-buffer file t t)
1490 (message "Stealing lock on %s...done" file))
1492 (defun vc-checkin (file &optional rev comment initial-contents)
1493 "Check in FILE.
1494 The optional argument REV may be a string specifying the new version
1495 level (if nil increment the current level). COMMENT is a comment
1496 string; if omitted, a buffer is popped up to accept a comment. If
1497 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1498 of the log entry buffer.
1500 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1501 that the version control system supports this mode of operation.
1503 Runs the normal hook `vc-checkin-hook'."
1504 (vc-start-entry
1505 file rev comment initial-contents
1506 "Enter a change comment."
1507 (lambda (file rev comment)
1508 (message "Checking in %s..." file)
1509 ;; "This log message intentionally left almost blank".
1510 ;; RCS 5.7 gripes about white-space-only comments too.
1511 (or (and comment (string-match "[^\t\n ]" comment))
1512 (setq comment "*** empty log message ***"))
1513 (with-vc-properties
1514 file
1515 ;; Change buffers to get local value of vc-checkin-switches.
1516 (with-current-buffer (or (get-file-buffer file) (current-buffer))
1517 (progn
1518 (vc-call checkin file rev comment)
1519 (vc-delete-automatic-version-backups file)))
1520 `((vc-state . up-to-date)
1521 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1522 (vc-workfile-version . nil)))
1523 (message "Checking in %s...done" file))
1524 'vc-checkin-hook))
1526 (defun vc-comment-to-change-log (&optional whoami file-name)
1527 "Enter last VC comment into change log file for current buffer's file.
1528 Optional arg (interactive prefix) non-nil means prompt for user name and site.
1529 Second arg is file name of change log. \
1530 If nil, uses `change-log-default-name'.
1532 May be useful as a `vc-checkin-hook' to update change logs automatically."
1533 (interactive (if current-prefix-arg
1534 (list current-prefix-arg
1535 (prompt-for-change-log-name))))
1536 ;; Make sure the defvar for add-log-current-defun-function has been executed
1537 ;; before binding it.
1538 (require 'add-log)
1539 (let (;; Extract the comment first so we get any error before doing anything.
1540 (comment (ring-ref vc-comment-ring 0))
1541 ;; Don't let add-change-log-entry insert a defun name.
1542 (add-log-current-defun-function 'ignore)
1543 end)
1544 ;; Call add-log to do half the work.
1545 (add-change-log-entry whoami file-name t t)
1546 ;; Insert the VC comment, leaving point before it.
1547 (setq end (save-excursion (insert comment) (point-marker)))
1548 (if (looking-at "\\s *\\s(")
1549 ;; It starts with an open-paren, as in "(foo): Frobbed."
1550 ;; So remove the ": " add-log inserted.
1551 (delete-char -2))
1552 ;; Canonicalize the white space between the file name and comment.
1553 (just-one-space)
1554 ;; Indent rest of the text the same way add-log indented the first line.
1555 (let ((indentation (current-indentation)))
1556 (save-excursion
1557 (while (< (point) end)
1558 (forward-line 1)
1559 (indent-to indentation))
1560 (setq end (point))))
1561 ;; Fill the inserted text, preserving open-parens at bol.
1562 (let ((paragraph-separate (concat paragraph-separate "\\|\\s *\\s("))
1563 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
1564 (beginning-of-line)
1565 (fill-region (point) end))
1566 ;; Canonicalize the white space at the end of the entry so it is
1567 ;; separated from the next entry by a single blank line.
1568 (skip-syntax-forward " " end)
1569 (delete-char (- (skip-syntax-backward " ")))
1570 (or (eobp) (looking-at "\n\n")
1571 (insert "\n"))))
1573 (defun vc-finish-logentry (&optional nocomment)
1574 "Complete the operation implied by the current log entry."
1575 (interactive)
1576 ;; Check and record the comment, if any.
1577 (unless nocomment
1578 ;; Comment too long?
1579 (vc-call-backend (or (and vc-log-file (vc-backend vc-log-file))
1580 (vc-responsible-backend default-directory))
1581 'logentry-check)
1582 (run-hooks 'vc-logentry-check-hook)
1583 ;; Record the comment in the comment ring
1584 (let ((comment (buffer-string)))
1585 (unless (and (ring-p vc-comment-ring)
1586 (not (ring-empty-p vc-comment-ring))
1587 (equal comment (ring-ref vc-comment-ring 0)))
1588 (ring-insert vc-comment-ring comment))))
1589 ;; Sync parent buffer in case the user modified it while editing the comment.
1590 ;; But not if it is a vc-dired buffer.
1591 (with-current-buffer vc-parent-buffer
1592 (or vc-dired-mode (vc-buffer-sync)))
1593 (if (not vc-log-operation) (error "No log operation is pending"))
1594 ;; save the parameters held in buffer-local variables
1595 (let ((log-operation vc-log-operation)
1596 (log-file vc-log-file)
1597 (log-version vc-log-version)
1598 (log-entry (buffer-string))
1599 (after-hook vc-log-after-operation-hook)
1600 (tmp-vc-parent-buffer vc-parent-buffer))
1601 (pop-to-buffer vc-parent-buffer)
1602 ;; OK, do it to it
1603 (save-excursion
1604 (funcall log-operation
1605 log-file
1606 log-version
1607 log-entry))
1608 ;; Remove checkin window (after the checkin so that if that fails
1609 ;; we don't zap the *VC-log* buffer and the typing therein).
1610 (let ((logbuf (get-buffer "*VC-log*")))
1611 (cond ((and logbuf vc-delete-logbuf-window)
1612 (delete-windows-on logbuf (selected-frame))
1613 ;; Kill buffer and delete any other dedicated windows/frames.
1614 (kill-buffer logbuf))
1615 (logbuf (pop-to-buffer "*VC-log*")
1616 (bury-buffer)
1617 (pop-to-buffer tmp-vc-parent-buffer))))
1618 ;; Now make sure we see the expanded headers
1619 (if log-file
1620 (vc-resynch-buffer log-file vc-keep-workfiles t))
1621 (if vc-dired-mode
1622 (dired-move-to-filename))
1623 (run-hooks after-hook 'vc-finish-logentry-hook)))
1625 ;; Code for access to the comment ring
1627 (defun vc-new-comment-index (stride len)
1628 (mod (cond
1629 (vc-comment-ring-index (+ vc-comment-ring-index stride))
1630 ;; Initialize the index on the first use of this command
1631 ;; so that the first M-p gets index 0, and the first M-n gets
1632 ;; index -1.
1633 ((> stride 0) (1- stride))
1634 (t stride))
1635 len))
1637 (defun vc-previous-comment (arg)
1638 "Cycle backwards through comment history."
1639 (interactive "*p")
1640 (let ((len (ring-length vc-comment-ring)))
1641 (if (<= len 0)
1642 (progn (message "Empty comment ring") (ding))
1643 (erase-buffer)
1644 (setq vc-comment-ring-index (vc-new-comment-index arg len))
1645 (message "Comment %d" (1+ vc-comment-ring-index))
1646 (insert (ring-ref vc-comment-ring vc-comment-ring-index)))))
1648 (defun vc-next-comment (arg)
1649 "Cycle forwards through comment history."
1650 (interactive "*p")
1651 (vc-previous-comment (- arg)))
1653 (defun vc-comment-search-reverse (str &optional stride)
1654 "Search backwards through comment history for substring match."
1655 ;; Why substring rather than regexp ? -sm
1656 (interactive
1657 (list (read-string "Comment substring: " nil nil vc-last-comment-match)))
1658 (unless stride (setq stride 1))
1659 (if (string= str "")
1660 (setq str vc-last-comment-match)
1661 (setq vc-last-comment-match str))
1662 (let* ((str (regexp-quote str))
1663 (len (ring-length vc-comment-ring))
1664 (n (vc-new-comment-index stride len)))
1665 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
1666 (not (string-match str (ring-ref vc-comment-ring n))))
1667 (setq n (+ n stride)))
1668 (setq vc-comment-ring-index n)
1669 (vc-previous-comment 0)))
1671 (defun vc-comment-search-forward (str)
1672 "Search forwards through comment history for substring match."
1673 (interactive
1674 (list (read-string "Comment substring: " nil nil vc-last-comment-match)))
1675 (vc-comment-search-reverse str -1))
1677 ;; Additional entry points for examining version histories
1679 ;;;###autoload
1680 (defun vc-diff (historic &optional not-urgent)
1681 "Display diffs between file versions.
1682 Normally this compares the current file and buffer with the most recent
1683 checked in version of that file. This uses no arguments.
1684 With a prefix argument, it reads the file name to use
1685 and two version designators specifying which versions to compare."
1686 (interactive (list current-prefix-arg t))
1687 (vc-ensure-vc-buffer)
1688 (if historic
1689 (call-interactively 'vc-version-diff)
1690 (let ((file buffer-file-name))
1691 (vc-buffer-sync not-urgent)
1692 (if (vc-workfile-unchanged-p buffer-file-name)
1693 (message "No changes to %s since latest version" file)
1694 (vc-version-diff file nil nil)))))
1696 (defun vc-version-diff (file rel1 rel2)
1697 "For FILE, report diffs between two stored versions REL1 and REL2 of it.
1698 If FILE is a directory, generate diffs between versions for all registered
1699 files in or below it."
1700 (interactive
1701 (let ((file (expand-file-name
1702 (read-file-name (if buffer-file-name
1703 "File or dir to diff: (default visited file) "
1704 "File or dir to diff: ")
1705 default-directory buffer-file-name t)))
1706 (rel1-default nil) (rel2-default nil))
1707 ;; compute default versions based on the file state
1708 (cond
1709 ;; if it's a directory, don't supply any version default
1710 ((file-directory-p file)
1711 nil)
1712 ;; if the file is not up-to-date, use current version as older version
1713 ((not (vc-up-to-date-p file))
1714 (setq rel1-default (vc-workfile-version file)))
1715 ;; if the file is not locked, use last and previous version as default
1717 (setq rel1-default (vc-previous-version (vc-workfile-version file)))
1718 (if (string= rel1-default "") (setq rel1-default nil))
1719 (setq rel2-default (vc-workfile-version file))))
1720 ;; construct argument list
1721 (list file
1722 (read-string (if rel1-default
1723 (concat "Older version: (default "
1724 rel1-default ") ")
1725 "Older version: ")
1726 nil nil rel1-default)
1727 (read-string (if rel2-default
1728 (concat "Newer version: (default "
1729 rel2-default ") ")
1730 "Newer version (default: current source): ")
1731 nil nil rel2-default))))
1732 (if (file-directory-p file)
1733 ;; recursive directory diff
1734 (progn
1735 (vc-setup-buffer "*vc-diff*")
1736 (if (string-equal rel1 "") (setq rel1 nil))
1737 (if (string-equal rel2 "") (setq rel2 nil))
1738 (let ((inhibit-read-only t))
1739 (insert "Diffs between "
1740 (or rel1 "last version checked in")
1741 " and "
1742 (or rel2 "current workfile(s)")
1743 ":\n\n"))
1744 (setq default-directory (file-name-as-directory file))
1745 ;; FIXME: this should do a single exec in CVS.
1746 (vc-file-tree-walk
1747 default-directory
1748 (lambda (f)
1749 (vc-exec-after
1750 `(progn
1751 (message "Looking at %s" ',f)
1752 (vc-call-backend ',(vc-backend file) 'diff ',f ',rel1 ',rel2)))))
1753 (vc-exec-after `(let ((inhibit-read-only t))
1754 (insert "\nEnd of diffs.\n"))))
1755 ;; single file diff
1756 (if (or (not rel1) (string-equal rel1 ""))
1757 (setq rel1 (vc-workfile-version file)))
1758 (if (string-equal rel2 "")
1759 (setq rel2 nil))
1760 (let ((file-rel1 (vc-version-backup-file file rel1))
1761 (file-rel2 (if (not rel2)
1762 file
1763 (vc-version-backup-file file rel2))))
1764 (if (and file-rel1 file-rel2)
1765 (apply 'vc-do-command "*vc-diff*" 1 "diff" nil
1766 (append (if (listp diff-switches)
1767 diff-switches
1768 (list diff-switches))
1769 (if (listp vc-diff-switches)
1770 vc-diff-switches
1771 (list vc-diff-switches))
1772 (list (file-relative-name file-rel1)
1773 (file-relative-name file-rel2))))
1774 (vc-call diff file rel1 rel2))))
1775 (set-buffer "*vc-diff*")
1776 (if (and (zerop (buffer-size))
1777 (not (get-buffer-process (current-buffer))))
1778 (progn
1779 (if rel1
1780 (if rel2
1781 (message "No changes to %s between %s and %s" file rel1 rel2)
1782 (message "No changes to %s since %s" file rel1))
1783 (message "No changes to %s since latest version" file))
1784 nil)
1785 (pop-to-buffer (current-buffer))
1786 ;; Gnus-5.8.5 sets up an autoload for diff-mode, even if it's
1787 ;; not available. Work around that.
1788 (if (require 'diff-mode nil t) (diff-mode))
1789 (vc-exec-after '(let ((inhibit-read-only t))
1790 (if (eq (buffer-size) 0)
1791 (insert "No differences found.\n"))
1792 (goto-char (point-min))
1793 (shrink-window-if-larger-than-buffer)))
1796 (defmacro vc-diff-switches-list (backend)
1797 "Make a list of `diff-switches', `vc-diff-switches',
1798 and `vc-BACKEND-diff-switches'."
1799 `(append
1800 (if (listp diff-switches) diff-switches (list diff-switches))
1801 (if (listp vc-diff-switches) vc-diff-switches (list vc-diff-switches))
1802 (let ((backend-switches
1803 (eval (intern (concat "vc-" (symbol-name ',backend)
1804 "-diff-switches")))))
1805 (if (listp backend-switches) backend-switches (list backend-switches)))))
1807 ;;;###autoload
1808 (defun vc-version-other-window (rev)
1809 "Visit version REV of the current buffer in another window.
1810 If the current buffer is named `F', the version is named `F.~REV~'.
1811 If `F.~REV~' already exists, it is used instead of being re-created."
1812 (interactive "sVersion to visit (default is workfile version): ")
1813 (vc-ensure-vc-buffer)
1814 (let* ((file buffer-file-name)
1815 (version (if (string-equal rev "")
1816 (vc-workfile-version file)
1817 rev))
1818 (automatic-backup (vc-version-backup-file-name file version))
1819 (manual-backup (vc-version-backup-file-name file version 'manual)))
1820 (unless (file-exists-p manual-backup)
1821 (if (file-exists-p automatic-backup)
1822 (rename-file automatic-backup manual-backup nil)
1823 (vc-call checkout file nil version manual-backup)))
1824 (find-file-other-window manual-backup)))
1826 ;; Header-insertion code
1828 ;;;###autoload
1829 (defun vc-insert-headers ()
1830 "Insert headers in a file for use with your version control system.
1831 Headers desired are inserted at point, and are pulled from
1832 the variable `vc-BACKEND-header'."
1833 (interactive)
1834 (vc-ensure-vc-buffer)
1835 (save-excursion
1836 (save-restriction
1837 (widen)
1838 (if (or (not (vc-check-headers))
1839 (y-or-n-p "Version headers already exist. Insert another set? "))
1840 (progn
1841 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
1842 (comment-start-vc (or (car delims) comment-start "#"))
1843 (comment-end-vc (or (car (cdr delims)) comment-end ""))
1844 (hdsym (vc-make-backend-sym (vc-backend (buffer-file-name))
1845 'header))
1846 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
1847 (mapcar (lambda (s)
1848 (insert comment-start-vc "\t" s "\t"
1849 comment-end-vc "\n"))
1850 hdstrings)
1851 (if vc-static-header-alist
1852 (mapcar (lambda (f)
1853 (if (string-match (car f) buffer-file-name)
1854 (insert (format (cdr f) (car hdstrings)))))
1855 vc-static-header-alist))
1857 )))))
1859 (defun vc-clear-headers (&optional file)
1860 "Clear all version headers in the current buffer (or FILE).
1861 I.e. reset them to the non-expanded form."
1862 (let* ((filename (or file buffer-file-name))
1863 (visited (find-buffer-visiting filename))
1864 (backend (vc-backend filename)))
1865 (when (vc-find-backend-function backend 'clear-headers)
1866 (if visited
1867 (let ((context (vc-buffer-context)))
1868 ;; save-excursion may be able to relocate point and mark
1869 ;; properly. If it fails, vc-restore-buffer-context
1870 ;; will give it a second try.
1871 (save-excursion
1872 (vc-call-backend backend 'clear-headers))
1873 (vc-restore-buffer-context context))
1874 (set-buffer (find-file-noselect filename))
1875 (vc-call-backend backend 'clear-headers)
1876 (kill-buffer filename)))))
1878 ;;;###autoload
1879 (defun vc-merge ()
1880 "Merge changes between two versions into the current buffer's file.
1881 This asks for two versions to merge from in the minibuffer. If the
1882 first version is a branch number, then merge all changes from that
1883 branch. If the first version is empty, merge news, i.e. recent changes
1884 from the current branch.
1886 See Info node `Merging'."
1887 (interactive)
1888 (vc-ensure-vc-buffer)
1889 (vc-buffer-sync)
1890 (let* ((file buffer-file-name)
1891 (backend (vc-backend file))
1892 (state (vc-state file))
1893 first-version second-version status)
1894 (cond
1895 ((stringp state)
1896 (error "File is locked by %s" state))
1897 ((not (vc-editable-p file))
1898 (if (y-or-n-p
1899 "File must be checked out for merging. Check out now? ")
1900 (vc-checkout file t)
1901 (error "Merge aborted"))))
1902 (setq first-version
1903 (read-string (concat "Branch or version to merge from "
1904 "(default: news on current branch): ")))
1905 (if (string= first-version "")
1906 (if (not (vc-find-backend-function backend 'merge-news))
1907 (error "Sorry, merging news is not implemented for %s" backend)
1908 (setq status (vc-call merge-news file)))
1909 (if (not (vc-find-backend-function backend 'merge))
1910 (error "Sorry, merging is not implemented for %s" backend)
1911 (if (not (vc-branch-p first-version))
1912 (setq second-version
1913 (read-string "Second version: "
1914 (concat (vc-branch-part first-version) ".")))
1915 ;; We want to merge an entire branch. Set versions
1916 ;; accordingly, so that vc-BACKEND-merge understands us.
1917 (setq second-version first-version)
1918 ;; first-version must be the starting point of the branch
1919 (setq first-version (vc-branch-part first-version)))
1920 (setq status (vc-call merge file first-version second-version))))
1921 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
1923 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
1924 (vc-resynch-buffer file t (not (buffer-modified-p)))
1925 (if (zerop status) (message "Merge successful")
1926 (if (fboundp 'smerge-mode) (smerge-mode 1))
1927 (if (y-or-n-p "Conflicts detected. Resolve them now? ")
1928 (if (fboundp 'smerge-ediff)
1929 (smerge-ediff)
1930 (vc-resolve-conflicts name-A name-B))
1931 (message "File contains conflict markers"))))
1933 (defvar vc-ediff-windows)
1934 (defvar vc-ediff-result)
1935 (eval-when-compile
1936 (defvar ediff-buffer-A)
1937 (defvar ediff-buffer-B)
1938 (defvar ediff-buffer-C)
1939 (require 'ediff-util))
1940 ;;;###autoload
1941 (defun vc-resolve-conflicts (&optional name-A name-B)
1942 "Invoke ediff to resolve conflicts in the current buffer.
1943 The conflicts must be marked with rcsmerge conflict markers."
1944 (interactive)
1945 (vc-ensure-vc-buffer)
1946 (let* ((found nil)
1947 (file-name (file-name-nondirectory buffer-file-name))
1948 (your-buffer (generate-new-buffer
1949 (concat "*" file-name
1950 " " (or name-A "WORKFILE") "*")))
1951 (other-buffer (generate-new-buffer
1952 (concat "*" file-name
1953 " " (or name-B "CHECKED-IN") "*")))
1954 (result-buffer (current-buffer)))
1955 (save-excursion
1956 (set-buffer your-buffer)
1957 (erase-buffer)
1958 (insert-buffer result-buffer)
1959 (goto-char (point-min))
1960 (while (re-search-forward (concat "^<<<<<<< "
1961 (regexp-quote file-name) "\n") nil t)
1962 (setq found t)
1963 (replace-match "")
1964 (if (not (re-search-forward "^=======\n" nil t))
1965 (error "Malformed conflict marker"))
1966 (replace-match "")
1967 (let ((start (point)))
1968 (if (not (re-search-forward "^>>>>>>> [0-9.]+\n" nil t))
1969 (error "Malformed conflict marker"))
1970 (delete-region start (point))))
1971 (if (not found)
1972 (progn
1973 (kill-buffer your-buffer)
1974 (kill-buffer other-buffer)
1975 (error "No conflict markers found")))
1976 (set-buffer other-buffer)
1977 (erase-buffer)
1978 (insert-buffer result-buffer)
1979 (goto-char (point-min))
1980 (while (re-search-forward (concat "^<<<<<<< "
1981 (regexp-quote file-name) "\n") nil t)
1982 (let ((start (match-beginning 0)))
1983 (if (not (re-search-forward "^=======\n" nil t))
1984 (error "Malformed conflict marker"))
1985 (delete-region start (point))
1986 (if (not (re-search-forward "^>>>>>>> [0-9.]+\n" nil t))
1987 (error "Malformed conflict marker"))
1988 (replace-match "")))
1989 (let ((config (current-window-configuration))
1990 (ediff-default-variant 'default-B))
1992 ;; Fire up ediff.
1994 (set-buffer (ediff-merge-buffers your-buffer other-buffer))
1996 ;; Ediff is now set up, and we are in the control buffer.
1997 ;; Do a few further adjustments and take precautions for exit.
1999 (make-local-variable 'vc-ediff-windows)
2000 (setq vc-ediff-windows config)
2001 (make-local-variable 'vc-ediff-result)
2002 (setq vc-ediff-result result-buffer)
2003 (make-local-variable 'ediff-quit-hook)
2004 (setq ediff-quit-hook
2005 (lambda ()
2006 (let ((buffer-A ediff-buffer-A)
2007 (buffer-B ediff-buffer-B)
2008 (buffer-C ediff-buffer-C)
2009 (result vc-ediff-result)
2010 (windows vc-ediff-windows))
2011 (ediff-cleanup-mess)
2012 (set-buffer result)
2013 (erase-buffer)
2014 (insert-buffer buffer-C)
2015 (kill-buffer buffer-A)
2016 (kill-buffer buffer-B)
2017 (kill-buffer buffer-C)
2018 (set-window-configuration windows)
2019 (message "Conflict resolution finished; you may save the buffer"))))
2020 (message "Please resolve conflicts now; exit ediff when done")
2021 nil))))
2023 ;; The VC directory major mode. Coopt Dired for this.
2024 ;; All VC commands get mapped into logical equivalents.
2026 (defvar vc-dired-switches)
2027 (defvar vc-dired-terse-mode)
2029 (defvar vc-dired-mode-map
2030 (let ((map (make-sparse-keymap))
2031 (vmap (make-sparse-keymap)))
2032 (define-key map "\C-xv" vmap)
2033 (define-key map "v" vmap)
2034 (set-keymap-parent vmap vc-prefix-map)
2035 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2036 map))
2038 (define-derived-mode vc-dired-mode dired-mode "Dired under VC"
2039 "The major mode used in VC directory buffers.
2041 It works like Dired, but lists only files under version control, with
2042 the current VC state of each file being indicated in the place of the
2043 file's link count, owner, group and size. Subdirectories are also
2044 listed, and you may insert them into the buffer as desired, like in
2045 Dired.
2047 All Dired commands operate normally, with the exception of `v', which
2048 is redefined as the version control prefix, so that you can type
2049 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2050 the file named in the current Dired buffer line. `vv' invokes
2051 `vc-next-action' on this file, or on all files currently marked.
2052 There is a special command, `*l', to mark all files currently locked."
2053 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2054 ;; We do it here because dired might not be loaded yet
2055 ;; when vc-dired-mode-map is initialized.
2056 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2057 (make-local-hook 'dired-after-readin-hook)
2058 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2059 ;; The following is slightly modified from dired.el,
2060 ;; because file lines look a bit different in vc-dired-mode.
2061 (set (make-local-variable 'dired-move-to-filename-regexp)
2062 (let*
2063 ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2064 ;; In some locales, month abbreviations are as short as 2 letters,
2065 ;; and they can be padded on the right with spaces.
2066 (month (concat l l "+ *"))
2067 ;; Recognize any non-ASCII character.
2068 ;; The purpose is to match a Kanji character.
2069 (k "[^\0-\177]")
2070 ;; (k "[^\x00-\x7f\x80-\xff]")
2071 (s " ")
2072 (yyyy "[0-9][0-9][0-9][0-9]")
2073 (mm "[ 0-1][0-9]")
2074 (dd "[ 0-3][0-9]")
2075 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2076 (western (concat "\\(" month s dd "\\|" dd s month "\\)"
2077 s "\\(" HH:MM "\\|" s yyyy"\\|" yyyy s "\\)"))
2078 (japanese (concat mm k s dd k s "\\(" s HH:MM "\\|" yyyy k "\\)")))
2079 ;; the .* below ensures that we find the last match on a line
2080 (concat ".*" s "\\(" western "\\|" japanese "\\)" s)))
2081 (and (boundp 'vc-dired-switches)
2082 vc-dired-switches
2083 (set (make-local-variable 'dired-actual-switches)
2084 vc-dired-switches))
2085 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2086 (setq vc-dired-mode t))
2088 (defun vc-dired-toggle-terse-mode ()
2089 "Toggle terse display in VC Dired."
2090 (interactive)
2091 (if (not vc-dired-mode)
2093 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2094 (if vc-dired-terse-mode
2095 (vc-dired-hook)
2096 (revert-buffer))))
2098 (defun vc-dired-mark-locked ()
2099 "Mark all files currently locked."
2100 (interactive)
2101 (dired-mark-if (let ((f (dired-get-filename nil t)))
2102 (and f
2103 (not (file-directory-p f))
2104 (not (vc-up-to-date-p f))))
2105 "locked file"))
2107 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2109 (defun vc-default-dired-state-info (backend file)
2110 (let ((state (vc-state file)))
2111 (cond
2112 ((stringp state) (concat "(" state ")"))
2113 ((eq state 'edited) (concat "(" (vc-user-login-name) ")"))
2114 ((eq state 'needs-merge) "(merge)")
2115 ((eq state 'needs-patch) "(patch)")
2116 ((eq state 'unlocked-changes) "(stale)"))))
2118 (defun vc-dired-reformat-line (x)
2119 "Reformat a directory-listing line.
2120 Replace various columns with version control information.
2121 This code, like dired, assumes UNIX -l format."
2122 (beginning-of-line)
2123 (let ((pos (point)) limit perm date-and-file)
2124 (end-of-line)
2125 (setq limit (point))
2126 (goto-char pos)
2127 (when
2129 (re-search-forward ;; owner and group
2130 "^\\(..[drwxlts-]+ \\) *[0-9]+ [^ ]+ +[^ ]+ +[0-9]+\\( .*\\)"
2131 limit t)
2132 (re-search-forward ;; only owner displayed
2133 "^\\(..[drwxlts-]+ \\) *[0-9]+ [^ ]+ +[0-9]+\\( .*\\)"
2134 limit t)
2135 (re-search-forward ;; OS/2 -l format, no links, owner, group
2136 "^\\(..[drwxlts-]+ \\) *[0-9]+\\( .*\\)"
2137 limit t))
2138 (setq perm (match-string 1)
2139 date-and-file (match-string 2))
2140 (setq x (substring (concat x " ") 0 10))
2141 (replace-match (concat perm x date-and-file)))))
2143 (defun vc-dired-hook ()
2144 "Reformat the listing according to version control.
2145 Called by dired after any portion of a vc-dired buffer has been read in."
2146 (message "Getting version information... ")
2147 (let (subdir filename (buffer-read-only nil) cvs-dir)
2148 (goto-char (point-min))
2149 (while (not (eobp))
2150 (cond
2151 ;; subdir header line
2152 ((setq subdir (dired-get-subdir))
2153 ;; if the backend supports it, get the state
2154 ;; of all files in this directory at once
2155 (let ((backend (vc-responsible-backend subdir)))
2156 (if (vc-find-backend-function backend 'dir-state)
2157 (vc-call-backend backend 'dir-state subdir)))
2158 (forward-line 1)
2159 ;; erase (but don't remove) the "total" line
2160 (delete-region (point) (line-end-position))
2161 (beginning-of-line)
2162 (forward-line 1))
2163 ;; file line
2164 ((setq filename (dired-get-filename nil t))
2165 (cond
2166 ;; subdir
2167 ((file-directory-p filename)
2168 (cond
2169 ((member (file-name-nondirectory filename)
2170 vc-directory-exclusion-list)
2171 (let ((pos (point)))
2172 (dired-kill-tree filename)
2173 (goto-char pos)
2174 (dired-kill-line)))
2175 (vc-dired-terse-mode
2176 ;; Don't show directories in terse mode. Don't use
2177 ;; dired-kill-line to remove it, because in recursive listings,
2178 ;; that would remove the directory contents as well.
2179 (delete-region (line-beginning-position)
2180 (progn (forward-line 1) (point))))
2181 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2182 (dired-kill-line))
2184 (vc-dired-reformat-line nil)
2185 (forward-line 1))))
2186 ;; ordinary file
2187 ((and (vc-backend filename)
2188 (not (and vc-dired-terse-mode
2189 (vc-up-to-date-p filename))))
2190 (vc-dired-reformat-line (vc-call dired-state-info filename))
2191 (forward-line 1))
2193 (dired-kill-line))))
2194 ;; any other line
2195 (t (forward-line 1))))
2196 (vc-dired-purge))
2197 (message "Getting version information... done")
2198 (save-restriction
2199 (widen)
2200 (cond ((eq (count-lines (point-min) (point-max)) 1)
2201 (goto-char (point-min))
2202 (message "No files locked under %s" default-directory)))))
2204 (defun vc-dired-purge ()
2205 "Remove empty subdirs."
2206 (let (subdir)
2207 (goto-char (point-min))
2208 (while (setq subdir (dired-get-subdir))
2209 (forward-line 2)
2210 (if (dired-get-filename nil t)
2211 (if (not (dired-next-subdir 1 t))
2212 (goto-char (point-max)))
2213 (forward-line -2)
2214 (if (not (string= (dired-current-directory) default-directory))
2215 (dired-do-kill-lines t "")
2216 ;; We cannot remove the top level directory.
2217 ;; Just make it look a little nicer.
2218 (forward-line 1)
2219 (kill-line)
2220 (if (not (dired-next-subdir 1 t))
2221 (goto-char (point-max))))))
2222 (goto-char (point-min))))
2224 (defun vc-dired-buffers-for-dir (dir)
2225 "Return a list of all vc-dired buffers that currently display DIR."
2226 (let (result)
2227 ;; Check whether dired is loaded.
2228 (when (fboundp 'dired-buffers-for-dir)
2229 (mapcar (lambda (buffer)
2230 (with-current-buffer buffer
2231 (if vc-dired-mode
2232 (setq result (append result (list buffer))))))
2233 (dired-buffers-for-dir dir)))
2234 result))
2236 (defun vc-dired-resynch-file (file)
2237 "Update the entries for FILE in any VC Dired buffers that list it."
2238 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2239 (when buffers
2240 (mapcar (lambda (buffer)
2241 (with-current-buffer buffer
2242 (if (dired-goto-file file)
2243 ;; bind vc-dired-terse-mode to nil so that
2244 ;; files won't vanish when they are checked in
2245 (let ((vc-dired-terse-mode nil))
2246 (dired-do-redisplay 1)))))
2247 buffers))))
2249 ;;;###autoload
2250 (defun vc-directory (dir read-switches)
2251 "Create a buffer in VC Dired Mode for directory DIR.
2253 See Info node `VC Dired Mode'.
2255 With prefix arg READ-SWITCHES, specify a value to override
2256 `dired-listing-switches' when generating the listing."
2257 (interactive "DDired under VC (directory): \nP")
2258 (let ((vc-dired-switches (concat vc-dired-listing-switches
2259 (if vc-dired-recurse "R" ""))))
2260 (if read-switches
2261 (setq vc-dired-switches
2262 (read-string "Dired listing switches: "
2263 vc-dired-switches)))
2264 (require 'dired)
2265 (require 'dired-aux)
2266 (switch-to-buffer
2267 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2268 vc-dired-switches
2269 'vc-dired-mode))))
2272 ;; Named-configuration entry points
2274 (defun vc-snapshot-precondition (dir)
2275 "Scan the tree below DIR, looking for non-uptodate files.
2276 If any file is not up-to-date, return the name of the first such file.
2277 \(This means, neither snapshot creation nor retrieval is allowed.\)
2278 If one or more of the files are currently visited, return `visited'.
2279 Otherwise, return nil."
2280 (let ((status nil))
2281 (catch 'vc-locked-example
2282 (vc-file-tree-walk
2284 (lambda (f)
2285 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2286 (if (get-file-buffer f) (setq status 'visited)))))
2287 status)))
2289 ;;;###autoload
2290 (defun vc-create-snapshot (dir name branchp)
2291 "Descending recursively from DIR, make a snapshot called NAME.
2292 For each registered file, the version level of its latest version
2293 becomes part of the named configuration. If the prefix argument
2294 BRANCHP is given, the snapshot is made as a new branch and the files
2295 are checked out in that new branch."
2296 (interactive
2297 (list (read-file-name "Directory: " default-directory default-directory t)
2298 (read-string "New snapshot name: ")
2299 current-prefix-arg))
2300 (message "Making %s... " (if branchp "branch" "snapshot"))
2301 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2302 (vc-call-backend (vc-responsible-backend dir)
2303 'create-snapshot dir name branchp)
2304 (message "Making %s... done" (if branchp "branch" "snapshot")))
2306 (defun vc-default-create-snapshot (backend dir name branchp)
2307 (when branchp
2308 (error "VC backend %s does not support module branches" backend))
2309 (let ((result (vc-snapshot-precondition dir)))
2310 (if (stringp result)
2311 (error "File %s is not up-to-date" result)
2312 (vc-file-tree-walk
2314 (lambda (f)
2315 (vc-call assign-name f name))))))
2317 ;;;###autoload
2318 (defun vc-retrieve-snapshot (dir name)
2319 "Descending recursively from DIR, retrieve the snapshot called NAME.
2320 If NAME is empty, it refers to the latest versions.
2321 If locking is used for the files in DIR, then there must not be any
2322 locked files at or below DIR (but if NAME is empty, locked files are
2323 allowed and simply skipped)."
2324 (interactive
2325 (list (read-file-name "Directory: " default-directory default-directory t)
2326 (read-string "Snapshot name to retrieve (default latest versions): ")))
2327 (let ((update (yes-or-no-p "Update any affected buffers? "))
2328 (msg (if (or (not name) (string= name ""))
2329 (format "Updating %s... " (abbreviate-file-name dir))
2330 (format "Retrieving snapshot into %s... "
2331 (abbreviate-file-name dir)))))
2332 (message msg)
2333 (vc-call-backend (vc-responsible-backend dir)
2334 'retrieve-snapshot dir name update)
2335 (message (concat msg "done"))))
2337 (defun vc-default-retrieve-snapshot (backend dir name update)
2338 (if (string= name "")
2339 (progn
2340 (vc-file-tree-walk
2342 (lambda (f) (and
2343 (vc-up-to-date-p f)
2344 (vc-error-occurred
2345 (vc-call checkout f nil "")
2346 (if update (vc-resynch-buffer f t t)))))))
2347 (let ((result (vc-snapshot-precondition dir)))
2348 (if (stringp result)
2349 (error "File %s is locked" result)
2350 (setq update (and (eq result 'visited) update))
2351 (vc-file-tree-walk
2353 (lambda (f) (vc-error-occurred
2354 (vc-call checkout f nil name)
2355 (if update (vc-resynch-buffer f t t)))))))))
2357 ;; Miscellaneous other entry points
2359 ;;;###autoload
2360 (defun vc-print-log ()
2361 "List the change log of the current buffer in a window."
2362 (interactive)
2363 (vc-ensure-vc-buffer)
2364 (let ((file buffer-file-name))
2365 (vc-call print-log file)
2366 (set-buffer "*vc*")
2367 (pop-to-buffer (current-buffer))
2368 (if (fboundp 'log-view-mode) (log-view-mode))
2369 (vc-exec-after
2370 `(progn
2371 (goto-char (point-max)) (forward-line -1)
2372 (while (looking-at "=*\n")
2373 (delete-char (- (match-end 0) (match-beginning 0)))
2374 (forward-line -1))
2375 (goto-char (point-min))
2376 (if (looking-at "[\b\t\n\v\f\r ]+")
2377 (delete-char (- (match-end 0) (match-beginning 0))))
2378 (shrink-window-if-larger-than-buffer)
2379 ;; move point to the log entry for the current version
2380 (if (fboundp 'log-view-goto-rev)
2381 (log-view-goto-rev ',(vc-workfile-version file))
2382 (if (vc-find-backend-function ',(vc-backend file) 'show-log-entry)
2383 (vc-call-backend ',(vc-backend file)
2384 'show-log-entry
2385 ',(vc-workfile-version file))))))))
2387 (defun vc-default-comment-history (backend file)
2388 "Return a string with all log entries that were made under BACKEND for FILE."
2389 (if (vc-find-backend-function backend 'print-log)
2390 (with-temp-buffer
2391 (vc-call print-log file)
2392 (vc-call wash-log file)
2393 (buffer-string))))
2395 (defun vc-default-wash-log (backend file)
2396 "Remove all non-comment information from log output.
2397 This default implementation works for RCS logs; backends should override
2398 it if their logs are not in RCS format."
2399 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
2400 "\\(branches: .*;\n\\)?"
2401 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
2402 (goto-char (point-max)) (forward-line -1)
2403 (while (looking-at "=*\n")
2404 (delete-char (- (match-end 0) (match-beginning 0)))
2405 (forward-line -1))
2406 (goto-char (point-min))
2407 (if (looking-at "[\b\t\n\v\f\r ]+")
2408 (delete-char (- (match-end 0) (match-beginning 0))))
2409 (goto-char (point-min))
2410 (re-search-forward separator nil t)
2411 (delete-region (point-min) (point))
2412 (while (re-search-forward separator nil t)
2413 (delete-region (match-beginning 0) (match-end 0)))))
2415 ;;;###autoload
2416 (defun vc-revert-buffer ()
2417 "Revert the current buffer's file back to the version it was based on.
2418 This asks for confirmation if the buffer contents are not identical
2419 to that version. This function does not automatically pick up newer
2420 changes found in the master file; use \\[universal-argument] \\[vc-next-action] to do so."
2421 (interactive)
2422 (vc-ensure-vc-buffer)
2423 (let ((file buffer-file-name)
2424 ;; This operation should always ask for confirmation.
2425 (vc-suppress-confirm nil)
2426 (obuf (current-buffer))
2427 status)
2428 (if (vc-up-to-date-p file)
2429 (unless (yes-or-no-p "File seems up-to-date. Revert anyway? ")
2430 (error "Revert canceled")))
2431 (unless (vc-workfile-unchanged-p file)
2432 ;; vc-diff selects the new window, which is not what we want:
2433 ;; if the new window is on another frame, that'd require the user
2434 ;; moving her mouse to answer the yes-or-no-p question.
2435 (let ((win (save-selected-window
2436 (setq status (vc-diff nil t)) (selected-window))))
2437 (vc-exec-after `(message nil))
2438 (when status
2439 (unwind-protect
2440 (unless (yes-or-no-p "Discard changes? ")
2441 (error "Revert canceled"))
2442 (select-window win)
2443 (if (one-window-p t)
2444 (if (window-dedicated-p (selected-window))
2445 (make-frame-invisible))
2446 (delete-window))))))
2447 (set-buffer obuf)
2448 ;; Do the reverting
2449 (message "Reverting %s..." file)
2450 (vc-revert-file file)
2451 (message "Reverting %s...done" file)))
2453 (defun vc-version-backup-file (file &optional rev)
2454 "Return name of backup file for revision REV of FILE.
2455 If version backups should be used for FILE, and there exists
2456 such a backup for REV or the current workfile version of file,
2457 return its name; otherwise return nil."
2458 (when (vc-call make-version-backups-p file)
2459 (let ((backup-file (vc-version-backup-file-name file rev)))
2460 (if (file-exists-p backup-file)
2461 backup-file
2462 ;; there is no automatic backup, but maybe the user made one manually
2463 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2464 (if (file-exists-p backup-file)
2465 backup-file)))))
2467 (defun vc-revert-file (file)
2468 "Revert FILE back to the version it was based on."
2469 (with-vc-properties
2470 file
2471 (let ((backup-file (vc-version-backup-file file)))
2472 (when backup-file
2473 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
2474 (vc-delete-automatic-version-backups file))
2475 (vc-call revert file backup-file))
2476 `((vc-state . up-to-date)
2477 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2478 (vc-resynch-buffer file t t))
2480 ;;;###autoload
2481 (defun vc-cancel-version (norevert)
2482 "Get rid of most recently checked in version of this file.
2483 A prefix argument NOREVERT means do not revert the buffer afterwards."
2484 (interactive "P")
2485 (vc-ensure-vc-buffer)
2486 (let* ((file (buffer-file-name))
2487 (backend (vc-backend file))
2488 (target (vc-workfile-version file))
2489 (config (current-window-configuration)) done)
2490 (cond
2491 ((not (vc-find-backend-function backend 'cancel-version))
2492 (error "Sorry, canceling versions is not supported under %s" backend))
2493 ((not (vc-call latest-on-branch-p file))
2494 (error "This is not the latest version; VC cannot cancel it"))
2495 ((not (vc-up-to-date-p file))
2496 (error (substitute-command-keys "File is not up to date; use \\[vc-revert-buffer] to discard changes"))))
2497 (if (null (yes-or-no-p (format "Remove version %s from master? " target)))
2498 (error "Aborted")
2499 (setq norevert (or norevert (not
2500 (yes-or-no-p "Revert buffer to most recent remaining version? "))))
2502 (message "Removing last change from %s..." file)
2503 (with-vc-properties
2504 file
2505 (vc-call cancel-version file norevert)
2506 `((vc-state . ,(if norevert 'edited 'up-to-date))
2507 (vc-checkout-time . ,(if norevert
2509 (nth 5 (file-attributes file))))
2510 (vc-workfile-version . nil)))
2511 (message "Removing last change from %s...done" file)
2513 (cond
2514 (norevert ;; clear version headers and mark the buffer modified
2515 (set-visited-file-name file)
2516 (when (not vc-make-backup-files)
2517 ;; inhibit backup for this buffer
2518 (make-local-variable 'backup-inhibited)
2519 (setq backup-inhibited t))
2520 (setq buffer-read-only nil)
2521 (vc-clear-headers)
2522 (vc-mode-line file)
2523 (vc-dired-resynch-file file))
2524 (t ;; revert buffer to file on disk
2525 (vc-resynch-buffer file t t)))
2526 (message "Version %s has been removed from the master" target))))
2528 ;;;###autoload
2529 (defun vc-switch-backend (file backend)
2530 "Make BACKEND the current version control system for FILE.
2531 FILE must already be registered in BACKEND. The change is not
2532 permanent, only for the current session. This function only changes
2533 VC's perspective on FILE, it does not register or unregister it.
2534 By default, this command cycles through the registered backends.
2535 To get a prompt, use a prefix argument."
2536 (interactive
2537 (list
2538 buffer-file-name
2539 (let ((backend (vc-backend buffer-file-name))
2540 (backends nil))
2541 ;; Find the registered backends.
2542 (dolist (backend vc-handled-backends)
2543 (when (vc-call-backend backend 'registered buffer-file-name)
2544 (push backend backends)))
2545 ;; Find the next backend.
2546 (let ((def (car (delq backend (append (memq backend backends) backends))))
2547 (others (delete backend backends)))
2548 (cond
2549 ((null others) (error "No other backend to switch to"))
2550 (current-prefix-arg
2551 (intern
2552 (upcase
2553 (completing-read
2554 (format "Switch to backend [%s]: " def)
2555 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2556 nil t nil nil (downcase (symbol-name def))))))
2557 (t def))))))
2558 (unless (eq backend (vc-backend file))
2559 (vc-file-clearprops file)
2560 (vc-file-setprop file 'vc-backend backend)
2561 ;; Force recomputation of the state
2562 (unless (vc-call-backend backend 'registered file)
2563 (vc-file-clearprops file)
2564 (error "%s is not registered in %s" file backend))
2565 (vc-mode-line file)))
2567 ;;;###autoload
2568 (defun vc-transfer-file (file new-backend)
2569 "Transfer FILE to another version control system NEW-BACKEND.
2570 If NEW-BACKEND has a higher precedence than FILE's current backend
2571 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2572 NEW-BACKEND, using the version number from the current backend as the
2573 base level. If NEW-BACKEND has a lower precedence than the current
2574 backend, then commit all changes that were made under the current
2575 backend to NEW-BACKEND, and unregister FILE from the current backend.
2576 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2577 (let* ((old-backend (vc-backend file))
2578 (edited (memq (vc-state file) '(edited needs-merge)))
2579 (registered (vc-call-backend new-backend 'registered file))
2580 (move
2581 (and registered ; Never move if not registered in new-backend yet.
2582 ;; move if new-backend comes later in vc-handled-backends
2583 (or (memq new-backend (memq old-backend vc-handled-backends))
2584 (y-or-n-p "Final transfer? "))))
2585 (comment nil))
2586 (if (eq old-backend new-backend)
2587 (error "%s is the current backend of %s" new-backend file))
2588 (if registered
2589 (set-file-modes file (logior (file-modes file) 128))
2590 ;; `registered' might have switched under us.
2591 (vc-switch-backend file old-backend)
2592 (let* ((rev (vc-workfile-version file))
2593 (modified-file (and edited (make-temp-name file)))
2594 (unmodified-file (and modified-file (vc-version-backup-file file))))
2595 ;; Go back to the base unmodified file.
2596 (unwind-protect
2597 (progn
2598 (when modified-file
2599 (copy-file file modified-file)
2600 ;; If we have a local copy of the unmodified file, handle that
2601 ;; here and not in vc-revert-file because we don't want to
2602 ;; delete that copy -- it is still useful for OLD-BACKEND.
2603 (if unmodified-file
2604 (copy-file unmodified-file file 'ok-if-already-exists)
2605 (if (y-or-n-p "Get base version from master? ")
2606 (vc-revert-file file))))
2607 (vc-call-backend new-backend 'receive-file file rev))
2608 (when modified-file
2609 (vc-switch-backend file new-backend)
2610 (unless (eq (vc-checkout-model file) 'implicit)
2611 (vc-checkout file t nil))
2612 (rename-file modified-file file 'ok-if-already-exists)
2613 (vc-file-setprop file 'vc-checkout-time nil)))))
2614 (when move
2615 (vc-switch-backend file old-backend)
2616 (setq comment (vc-call comment-history file))
2617 (vc-call unregister file))
2618 (vc-switch-backend file new-backend)
2619 (when (or move edited)
2620 (vc-file-setprop file 'vc-state 'edited)
2621 (vc-mode-line file)
2622 (vc-checkin file nil comment (stringp comment)))))
2624 (defun vc-default-unregister (backend file)
2625 "Default implementation of `vc-unregister', signals an error."
2626 (error "Unregistering files is not supported for %s" backend))
2628 (defun vc-default-receive-file (backend file rev)
2629 "Let BACKEND receive FILE from another version control system."
2630 (vc-call-backend backend 'register file rev ""))
2632 (defun vc-rename-master (oldmaster newfile templates)
2633 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2634 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2635 (newdir (or (file-name-directory newfile) ""))
2636 (newbase (file-name-nondirectory newfile))
2637 (masters
2638 ;; List of potential master files for `newfile'
2639 (mapcar
2640 (lambda (s) (vc-possible-master s newdir newbase))
2641 templates)))
2642 (if (or (file-symlink-p oldmaster)
2643 (file-symlink-p (file-name-directory oldmaster)))
2644 (error "This unsafe in the presence of symbolic links"))
2645 (rename-file
2646 oldmaster
2647 (catch 'found
2648 ;; If possible, keep the master file in the same directory.
2649 (mapcar (lambda (f)
2650 (if (and f (string= (file-name-directory (expand-file-name f))
2651 dir))
2652 (throw 'found f)))
2653 masters)
2654 ;; If not, just use the first possible place.
2655 (mapcar (lambda (f)
2656 (and f
2657 (or (not (setq dir (file-name-directory f)))
2658 (file-directory-p dir))
2659 (throw 'found f)))
2660 masters)
2661 (error "New file lacks a version control directory")))))
2663 ;;;###autoload
2664 (defun vc-rename-file (old new)
2665 "Rename file OLD to NEW, and rename its master file likewise."
2666 (interactive "fVC rename file: \nFRename to: ")
2667 (let ((oldbuf (get-file-buffer old))
2668 (backend (vc-backend old)))
2669 (unless (or (null backend) (vc-find-backend-function backend 'rename-file))
2670 (error "Renaming files under %s is not supported in VC" backend))
2671 (if (and oldbuf (buffer-modified-p oldbuf))
2672 (error "Please save files before moving them"))
2673 (if (get-file-buffer new)
2674 (error "Already editing new file name"))
2675 (if (file-exists-p new)
2676 (error "New file already exists"))
2677 (when backend
2678 (if (and backend (not (vc-up-to-date-p old)))
2679 (error "Please check in files before moving them"))
2680 (vc-call-backend backend 'rename-file old new))
2681 ;; Move the actual file (unless the backend did it already)
2682 (if (or (not backend) (file-exists-p old))
2683 (rename-file old new))
2684 ;; ?? Renaming a file might change its contents due to keyword expansion.
2685 ;; We should really check out a new copy if the old copy was precisely equal
2686 ;; to some checked in version. However, testing for this is tricky....
2687 (if oldbuf
2688 (with-current-buffer oldbuf
2689 (let ((buffer-read-only buffer-read-only))
2690 (set-visited-file-name new))
2691 (vc-backend new)
2692 (vc-mode-line new)
2693 (set-buffer-modified-p nil)))))
2695 ;; Only defined in very recent Emacsen
2696 (defvar small-temporary-file-directory nil)
2698 ;;;###autoload
2699 (defun vc-update-change-log (&rest args)
2700 "Find change log file and add entries from recent version control logs.
2701 Normally, find log entries for all registered files in the default
2702 directory.
2704 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2706 With any numeric prefix arg, find log entries for all currently visited
2707 files that are under version control. This puts all the entries in the
2708 log for the default directory, which may not be appropriate.
2710 From a program, any ARGS are assumed to be filenames for which
2711 log entries should be gathered."
2712 (interactive
2713 (cond ((consp current-prefix-arg) ;C-u
2714 (list buffer-file-name))
2715 (current-prefix-arg ;Numeric argument.
2716 (let ((files nil)
2717 (buffers (buffer-list))
2718 file)
2719 (while buffers
2720 (setq file (buffer-file-name (car buffers)))
2721 (and file (vc-backend file)
2722 (setq files (cons file files)))
2723 (setq buffers (cdr buffers)))
2724 files))
2726 ;; Don't supply any filenames to backend; this means
2727 ;; it should find all relevant files relative to
2728 ;; the default-directory.
2729 nil)))
2730 (vc-call-backend (vc-responsible-backend default-directory)
2731 'update-changelog args))
2733 (defun vc-default-update-changelog (backend files)
2734 "Default implementation of update-changelog.
2735 Uses `rcs2log' which only works for RCS and CVS."
2736 ;; FIXME: We (c|sh)ould add support for cvs2cl
2737 (let ((odefault default-directory)
2738 (changelog (find-change-log))
2739 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
2740 (tempfile (funcall
2741 (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
2742 (expand-file-name "vc"
2743 (or small-temporary-file-directory
2744 temporary-file-directory))))
2745 (full-name (or add-log-full-name
2746 (user-full-name)
2747 (user-login-name)
2748 (format "uid%d" (number-to-string (user-uid)))))
2749 (mailing-address (or add-log-mailing-address
2750 user-mail-address)))
2751 (find-file-other-window changelog)
2752 (barf-if-buffer-read-only)
2753 (vc-buffer-sync)
2754 (undo-boundary)
2755 (goto-char (point-min))
2756 (push-mark)
2757 (message "Computing change log entries...")
2758 (message "Computing change log entries... %s"
2759 (unwind-protect
2760 (progn
2761 (setq default-directory odefault)
2762 (if (eq 0 (apply 'call-process
2763 (expand-file-name "rcs2log"
2764 exec-directory)
2765 nil (list t tempfile) nil
2766 "-c" changelog
2767 "-u" (concat (vc-user-login-name)
2768 "\t" full-name
2769 "\t" mailing-address)
2770 (mapcar
2771 (lambda (f)
2772 (file-relative-name
2773 (if (file-name-absolute-p f)
2775 (concat odefault f))))
2776 files)))
2777 "done"
2778 (pop-to-buffer
2779 (set-buffer (get-buffer-create "*vc*")))
2780 (erase-buffer)
2781 (insert-file tempfile)
2782 "failed"))
2783 (setq default-directory (file-name-directory changelog))
2784 (delete-file tempfile)))))
2786 ;; Annotate functionality
2788 ;; Declare globally instead of additional parameter to
2789 ;; temp-buffer-show-function (not possible to pass more than one
2790 ;; parameter).
2791 (defvar vc-annotate-ratio nil "Global variable.")
2792 (defvar vc-annotate-backend nil "Global variable.")
2794 (defun vc-annotate-get-backend (buffer)
2795 "Return the backend matching \"Annotate\" buffer BUFFER.
2796 Return NIL if no match made. Associations are made based on
2797 `vc-annotate-buffers'."
2798 (cdr (assoc buffer vc-annotate-buffers)))
2800 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
2801 "Major mode for buffers displaying output from the `annotate' command.
2803 You can use the mode-specific menu to alter the time-span of the used
2804 colors. See variable `vc-annotate-menu-elements' for customizing the
2805 menu items."
2806 (vc-annotate-add-menu))
2808 (defun vc-annotate-display-default (&optional event)
2809 "Use the default color spectrum for VC Annotate mode."
2810 (interactive "e")
2811 (message "Redisplaying annotation...")
2812 (vc-annotate-display (current-buffer)
2814 (vc-annotate-get-backend (current-buffer)))
2815 (message "Redisplaying annotation...done"))
2817 (defun vc-annotate-add-menu ()
2818 "Add the menu 'Annotate' to the menu bar in VC-Annotate mode."
2819 (setq vc-annotate-mode-menu (make-sparse-keymap "Annotate"))
2820 (define-key vc-annotate-mode-map [menu-bar vc-annotate-mode]
2821 (cons "VC-Annotate" vc-annotate-mode-menu))
2822 (define-key vc-annotate-mode-menu [default]
2823 '("Default" . vc-annotate-display-default))
2824 (let ((menu-elements vc-annotate-menu-elements))
2825 (while menu-elements
2826 (let* ((element (car menu-elements))
2827 (days (round (* element
2828 (vc-annotate-car-last-cons vc-annotate-color-map)
2829 0.7585))))
2830 (setq menu-elements (cdr menu-elements))
2831 (define-key vc-annotate-mode-menu
2832 (vector days)
2833 (cons (format "Span %d days"
2834 days)
2835 `(lambda ()
2836 ,(format "Use colors spanning %d days" days)
2837 (interactive)
2838 (message "Redisplaying annotation...")
2839 (vc-annotate-display
2840 (get-buffer (buffer-name))
2841 (vc-annotate-time-span vc-annotate-color-map ,element)
2842 (vc-annotate-get-backend (current-buffer)))
2843 (message "Redisplaying annotation...done"))))))))
2846 ;;;; (defun vc-BACKEND-annotate-command (file buffer) ...)
2847 ;;;; Execute "annotate" on FILE by using `call-process' and insert
2848 ;;;; the contents in BUFFER.
2850 ;;;###autoload
2851 (defun vc-annotate (prefix)
2852 "Display the edit history of the current file using colours.
2854 This command creates a buffer that shows, for each line of the current
2855 file, when it was last edited and by whom. Additionally, colours are
2856 used to show the age of each line--blue means oldest, red means
2857 youngest, and intermediate colours indicate intermediate ages. By
2858 default, the time scale stretches back one year into the past;
2859 everything that is older than that is shown in blue.
2861 With a prefix argument, this command asks two questions in the
2862 minibuffer. First, you may enter a version number; then the buffer
2863 displays and annotates that version instead of the current version
2864 \(type RET in the minibuffer to leave that default unchanged). Then,
2865 you are prompted for a stretch factor for the time scale. This makes
2866 the color range cover a time span longer or shorter than the default
2867 of one year. For example, a factor of 0.1 means that the range from
2868 red to blue stands for the past 36 days only, and everything that is
2869 older than that is shown in blue.
2871 Customization variables:
2873 `vc-annotate-menu-elements' customizes the menu elements of the
2874 mode-specific menu. `vc-annotate-color-map' and
2875 `vc-annotate-very-old-color' defines the mapping of time to
2876 colors. `vc-annotate-background' specifies the background color."
2877 (interactive "P")
2878 (vc-ensure-vc-buffer)
2879 (let* ((temp-buffer-name (concat "*Annotate " (buffer-name) "*"))
2880 (temp-buffer-show-function 'vc-annotate-display)
2881 (rev (vc-workfile-version (buffer-file-name)))
2882 (vc-annotate-version
2883 (if prefix (read-string
2884 (format "Annotate from version: (default %s) " rev)
2885 nil nil rev)
2886 rev))
2887 (vc-annotate-ratio
2888 (if prefix (string-to-number
2889 (read-string "Annotate ratio: (default 1.0) "
2890 nil nil "1.0"))
2891 1.0))
2892 (vc-annotate-backend (vc-backend (buffer-file-name))))
2893 (message "Annotating...")
2894 (if (not (vc-find-backend-function vc-annotate-backend 'annotate-command))
2895 (error "Sorry, annotating is not implemented for %s"
2896 vc-annotate-backend))
2897 (with-output-to-temp-buffer temp-buffer-name
2898 (vc-call-backend vc-annotate-backend 'annotate-command
2899 (file-name-nondirectory (buffer-file-name))
2900 (get-buffer temp-buffer-name)
2901 vc-annotate-version))
2902 ;; Don't use the temp-buffer-name until the buffer is created
2903 ;; (only after `with-output-to-temp-buffer'.)
2904 (setq vc-annotate-buffers
2905 (append vc-annotate-buffers
2906 (list (cons (get-buffer temp-buffer-name) vc-annotate-backend))))
2907 (message "Annotating... done")))
2910 (defun vc-annotate-car-last-cons (a-list)
2911 "Return car of last cons in association list A-LIST."
2912 (if (not (eq nil (cdr a-list)))
2913 (vc-annotate-car-last-cons (cdr a-list))
2914 (car (car a-list))))
2916 (defun vc-annotate-time-span (a-list span &optional quantize)
2917 "Apply factor SPAN to the time-span of association list A-LIST.
2918 Return the new alist.
2919 Optionally quantize to the factor of QUANTIZE."
2920 ;; Apply span to each car of every cons
2921 (if (not (eq nil a-list))
2922 (append (list (cons (* (car (car a-list)) span)
2923 (cdr (car a-list))))
2924 (vc-annotate-time-span (nthcdr (or quantize ; optional
2925 1) ; Default to cdr
2926 a-list) span quantize))))
2928 (defun vc-annotate-compcar (threshold a-list)
2929 "Test successive cons cells of association list A-LIST against THRESHOLD.
2930 Return the first cons cell which car is not less than THRESHOLD,
2931 nil otherwise"
2932 (let ((i 1)
2933 (tmp-cons (car a-list)))
2934 (while (and tmp-cons (< (car tmp-cons) threshold))
2935 (setq tmp-cons (car (nthcdr i a-list)))
2936 (setq i (+ i 1)))
2937 tmp-cons)) ; Return the appropriate value
2940 (defun vc-annotate-display (buffer &optional color-map backend)
2941 "Do the VC-Annotate display in BUFFER using COLOR-MAP.
2942 The original annotating file is supposed to be handled by BACKEND.
2943 If BACKEND is NIL, variable VC-ANNOTATE-BACKEND is used instead.
2944 This function is destructive on VC-ANNOTATE-BACKEND when BACKEND is non-nil."
2946 ;; Handle the case of the global variable vc-annotate-ratio being
2947 ;; set. This variable is used to pass information from function
2948 ;; vc-annotate since it is not possible to use another parameter
2949 ;; (see temp-buffer-show-function).
2950 (if (and (not color-map) vc-annotate-ratio)
2951 ;; This will only be true if called from vc-annotate with ratio
2952 ;; being non-nil.
2953 (setq color-map (vc-annotate-time-span vc-annotate-color-map
2954 vc-annotate-ratio)))
2955 (set-buffer buffer)
2956 (display-buffer buffer)
2957 (if (not vc-annotate-mode) ; Turn on vc-annotate-mode if not done
2958 (vc-annotate-mode))
2959 (goto-char (point-min)) ; Position at the top of the buffer.
2960 ;; Delete old overlays
2961 (mapcar
2962 (lambda (overlay)
2963 (if (overlay-get overlay 'vc-annotation)
2964 (delete-overlay overlay)))
2965 (overlays-in (point-min) (point-max)))
2966 (goto-char (point-min)) ; Position at the top of the buffer.
2968 (if backend (setq vc-annotate-backend backend)) ; Destructive on `vc-annotate-backend'
2970 (let ((difference (vc-call-backend vc-annotate-backend 'annotate-difference (point))))
2971 (while difference
2972 (let*
2973 ((color (or (vc-annotate-compcar
2974 difference (or color-map vc-annotate-color-map))
2975 (cons nil vc-annotate-very-old-color)))
2976 ;; substring from index 1 to remove any leading `#' in the name
2977 (face-name (concat "vc-annotate-face-" (substring (cdr color) 1)))
2978 ;; Make the face if not done.
2979 (face (or (intern-soft face-name)
2980 (let ((tmp-face (make-face (intern face-name))))
2981 (set-face-foreground tmp-face (cdr color))
2982 (if vc-annotate-background
2983 (set-face-background tmp-face vc-annotate-background))
2984 tmp-face))) ; Return the face
2985 (point (point))
2986 overlay)
2987 (forward-line 1)
2988 (setq overlay (make-overlay point (point)))
2989 (overlay-put overlay 'face face)
2990 (overlay-put overlay 'vc-annotation t))
2991 (setq difference (vc-call-backend vc-annotate-backend 'annotate-difference (point))))))
2994 ;; Collect back-end-dependent stuff here
2996 (defalias 'vc-default-logentry-check 'ignore)
2998 (defun vc-check-headers ()
2999 "Check if the current file has any headers in it."
3000 (interactive)
3001 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3003 (defun vc-default-check-headers (backend)
3004 "Default implementation of check-headers; always returns nil."
3005 nil)
3007 ;; Back-end-dependent stuff ends here.
3009 ;; Set up key bindings for use while editing log messages
3011 (define-derived-mode vc-log-mode text-mode "VC-Log"
3012 "Major mode for editing VC log entries.
3013 These bindings are added to the global keymap when you enter this mode:
3014 \\[vc-next-action] perform next logical version-control operation on current file
3015 \\[vc-register] register current file
3016 \\[vc-toggle-read-only] like next-action, but won't register files
3017 \\[vc-insert-headers] insert version-control headers in current file
3018 \\[vc-print-log] display change history of current file
3019 \\[vc-revert-buffer] revert buffer to latest version
3020 \\[vc-cancel-version] undo latest checkin
3021 \\[vc-diff] show diffs between file versions
3022 \\[vc-version-other-window] visit old version in another window
3023 \\[vc-directory] show all files locked by any user in or below .
3024 \\[vc-annotate] colorful display of the cvs annotate command
3025 \\[vc-update-change-log] add change log entry from recent checkins
3027 While you are entering a change log message for a version, the following
3028 additional bindings will be in effect.
3030 \\[vc-finish-logentry] proceed with check in, ending log message entry
3032 Whenever you do a checkin, your log comment is added to a ring of
3033 saved comments. These can be recalled as follows:
3035 \\[vc-next-comment] replace region with next message in comment ring
3036 \\[vc-previous-comment] replace region with previous message in comment ring
3037 \\[vc-comment-search-reverse] search backward for regexp in the comment ring
3038 \\[vc-comment-search-forward] search backward for regexp in the comment ring
3040 Entry to the change-log submode calls the value of `text-mode-hook', then
3041 the value of `vc-log-mode-hook'.
3043 Global user options:
3044 `vc-initial-comment' If non-nil, require user to enter a change
3045 comment upon first checkin of the file.
3047 `vc-keep-workfiles' Non-nil value prevents workfiles from being
3048 deleted when changes are checked in
3050 `vc-suppress-confirm' Suppresses some confirmation prompts.
3052 vc-BACKEND-header Which keywords to insert when adding headers
3053 with \\[vc-insert-headers]. Defaults to
3054 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under
3055 RCS and CVS.
3057 `vc-static-header-alist' By default, version headers inserted in C files
3058 get stuffed in a static string area so that
3059 ident(RCS/CVS) or what(SCCS) can see them in
3060 the compiled object code. You can override
3061 this by setting this variable to nil, or change
3062 the header template by changing it.
3064 `vc-command-messages' if non-nil, display run messages from the
3065 actual version-control utilities (this is
3066 intended primarily for people hacking vc
3067 itself)."
3068 (make-local-variable 'vc-comment-ring-index))
3070 (defun vc-log-edit (file)
3071 "Set up `log-edit' for use with VC on FILE.
3072 If `log-edit' is not available, resort to `vc-log-mode'."
3073 (setq default-directory
3074 (if file (file-name-directory file)
3075 (with-current-buffer vc-parent-buffer default-directory)))
3076 (if (fboundp 'log-edit)
3077 (log-edit 'vc-finish-logentry nil
3078 (if file `(lambda () ',(list (file-name-nondirectory file)))
3079 ;; If FILE is nil, we were called from vc-dired.
3080 (lambda ()
3081 (with-current-buffer vc-parent-buffer
3082 (dired-get-marked-files t)))))
3083 (vc-log-mode))
3084 (set (make-local-variable 'vc-log-file) file)
3085 (make-local-variable 'vc-log-version)
3086 (set-buffer-modified-p nil)
3087 (setq buffer-file-name nil))
3089 ;; These things should probably be generally available
3091 (defun vc-file-tree-walk (dirname func &rest args)
3092 "Walk recursively through DIRNAME.
3093 Invoke FUNC f ARGS on each VC-managed file f underneath it."
3094 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3095 (message "Traversing directory %s...done" dirname))
3097 (defun vc-file-tree-walk-internal (file func args)
3098 (if (not (file-directory-p file))
3099 (if (vc-backend file) (apply func file args))
3100 (message "Traversing directory %s..." (abbreviate-file-name file))
3101 (let ((dir (file-name-as-directory file)))
3102 (mapcar
3103 (lambda (f) (or
3104 (string-equal f ".")
3105 (string-equal f "..")
3106 (member f vc-directory-exclusion-list)
3107 (let ((dirf (expand-file-name f dir)))
3109 (file-symlink-p dirf);; Avoid possible loops
3110 (vc-file-tree-walk-internal dirf func args)))))
3111 (directory-files dir)))))
3113 (provide 'vc)
3115 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
3117 ;; These may be useful to anyone who has to debug or extend the package.
3118 ;; (Note that this information corresponds to versions 5.x. Some of it
3119 ;; might have been invalidated by the additions to support branching
3120 ;; and RCS keyword lookup. AS, 1995/03/24)
3122 ;; A fundamental problem in VC is that there are time windows between
3123 ;; vc-next-action's computations of the file's version-control state and
3124 ;; the actions that change it. This is a window open to lossage in a
3125 ;; multi-user environment; someone else could nip in and change the state
3126 ;; of the master during it.
3128 ;; The performance problem is that rlog/prs calls are very expensive; we want
3129 ;; to avoid them as much as possible.
3131 ;; ANALYSIS:
3133 ;; The performance problem, it turns out, simplifies in practice to the
3134 ;; problem of making vc-state fast. The two other functions that call
3135 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
3136 ;; makes snapshots, the other deletes the calling user's last change in the
3137 ;; master.
3139 ;; The race condition implies that we have to either (a) lock the master
3140 ;; during the entire execution of vc-next-action, or (b) detect and
3141 ;; recover from errors resulting from dispatch on an out-of-date state.
3143 ;; Alternative (a) appears to be infeasible. The problem is that we can't
3144 ;; guarantee that the lock will ever be removed. Suppose a user starts a
3145 ;; checkin, the change message buffer pops up, and the user, having wandered
3146 ;; off to do something else, simply forgets about it?
3148 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
3149 ;; vc-state. Usually, if a file is registered, we can read its locked/
3150 ;; unlocked state and its current owner from its permissions.
3152 ;; This shortcut will fail if someone has manually changed the workfile's
3153 ;; permissions; also if developers are munging the workfile in several
3154 ;; directories, with symlinks to a master (in this latter case, the
3155 ;; permissions shortcut will fail to detect a lock asserted from another
3156 ;; directory).
3158 ;; Note that these cases correspond exactly to the errors which could happen
3159 ;; because of a competing checkin/checkout race in between two instances of
3160 ;; vc-next-action.
3162 ;; For VC's purposes, a workfile/master pair may have the following states:
3164 ;; A. Unregistered. There is a workfile, there is no master.
3166 ;; B. Registered and not locked by anyone.
3168 ;; C. Locked by calling user and unchanged.
3170 ;; D. Locked by the calling user and changed.
3172 ;; E. Locked by someone other than the calling user.
3174 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
3176 ;; VC's idea of state
3177 ;; |
3178 ;; V Actual state RCS action SCCS action Effect
3179 ;; A B C D E
3180 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
3181 ;; B 5 . 6 7 8 co -l get -e checkout
3182 ;; C 9 10 . 11 12 co -u unget; get revert
3183 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
3184 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
3186 ;; All commands take the master file name as a last argument (not shown).
3188 ;; In the discussion below, a "self-race" is a pathological situation in
3189 ;; which VC operations are being attempted simultaneously by two or more
3190 ;; Emacsen running under the same username.
3192 ;; The vc-next-action code has the following windows:
3194 ;; Window P:
3195 ;; Between the check for existence of a master file and the call to
3196 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
3197 ;; never close if the initial-comment feature is on.
3199 ;; Window Q:
3200 ;; Between the call to vc-workfile-unchanged-p in and the immediately
3201 ;; following revert (apparent state C).
3203 ;; Window R:
3204 ;; Between the call to vc-workfile-unchanged-p in and the following
3205 ;; checkin (apparent state D). This window may never close.
3207 ;; Window S:
3208 ;; Between the unlock and the immediately following checkout during a
3209 ;; revert operation (apparent state C). Included in window Q.
3211 ;; Window T:
3212 ;; Between vc-state and the following checkout (apparent state B).
3214 ;; Window U:
3215 ;; Between vc-state and the following revert (apparent state C).
3216 ;; Includes windows Q and S.
3218 ;; Window V:
3219 ;; Between vc-state and the following checkin (apparent state
3220 ;; D). This window may never be closed if the user fails to complete the
3221 ;; checkin message. Includes window R.
3223 ;; Window W:
3224 ;; Between vc-state and the following steal-lock (apparent
3225 ;; state E). This window may never close if the user fails to complete
3226 ;; the steal-lock message. Includes window X.
3228 ;; Window X:
3229 ;; Between the unlock and the immediately following re-lock during a
3230 ;; steal-lock operation (apparent state E). This window may never close
3231 ;; if the user fails to complete the steal-lock message.
3233 ;; Errors:
3235 ;; Apparent state A ---
3237 ;; 1. File looked unregistered but is actually registered and not locked.
3239 ;; Potential cause: someone else's admin during window P, with
3240 ;; caller's admin happening before their checkout.
3242 ;; RCS: Prior to version 5.6.4, ci fails with message
3243 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
3244 ;; ci -i option and the message is "<file>,v: already exists".
3245 ;; SCCS: admin will fail with error (ad19).
3247 ;; We can let these errors be passed up to the user.
3249 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
3251 ;; Potential cause: self-race during window P.
3253 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
3254 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
3255 ;; ci -i option, failing with message "<file>,v: already exists".
3256 ;; SCCS: will fail with error (ad19).
3258 ;; Either of these consequences is acceptable.
3260 ;; 3. File looked unregistered but is actually locked by caller, changed.
3262 ;; Potential cause: self-race during window P.
3264 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
3265 ;; a delta with a null change comment (the -t- switch will be
3266 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
3267 ;; failing with message "<file>,v: already exists".
3268 ;; SCCS: will fail with error (ad19).
3270 ;; 4. File looked unregistered but is locked by someone else.
3272 ;; Potential cause: someone else's admin during window P, with
3273 ;; caller's admin happening *after* their checkout.
3275 ;; RCS: Prior to version 5.6.4, ci fails with a
3276 ;; "no lock set by <user>" message. From 5.6.4 onwards,
3277 ;; VC uses the new ci -i option, failing with message
3278 ;; "<file>,v: already exists".
3279 ;; SCCS: will fail with error (ad19).
3281 ;; We can let these errors be passed up to the user.
3283 ;; Apparent state B ---
3285 ;; 5. File looked registered and not locked, but is actually unregistered.
3287 ;; Potential cause: master file got nuked during window P.
3289 ;; RCS: will fail with "RCS/<file>: No such file or directory"
3290 ;; SCCS: will fail with error ut4.
3292 ;; We can let these errors be passed up to the user.
3294 ;; 6. File looked registered and not locked, but is actually locked by the
3295 ;; calling user and unchanged.
3297 ;; Potential cause: self-race during window T.
3299 ;; RCS: in the same directory as the previous workfile, co -l will fail
3300 ;; with "co error: writable foo exists; checkout aborted". In any other
3301 ;; directory, checkout will succeed.
3302 ;; SCCS: will fail with ge17.
3304 ;; Either of these consequences is acceptable.
3306 ;; 7. File looked registered and not locked, but is actually locked by the
3307 ;; calling user and changed.
3309 ;; As case 6.
3311 ;; 8. File looked registered and not locked, but is actually locked by another
3312 ;; user.
3314 ;; Potential cause: someone else checks it out during window T.
3316 ;; RCS: co error: revision 1.3 already locked by <user>
3317 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
3319 ;; We can let these errors be passed up to the user.
3321 ;; Apparent state C ---
3323 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
3325 ;; As case 5.
3327 ;; 10. File looks locked by calling user and unchanged, but is actually not
3328 ;; locked.
3330 ;; Potential cause: a self-race in window U, or by the revert's
3331 ;; landing during window X of some other user's steal-lock or window S
3332 ;; of another user's revert.
3334 ;; RCS: succeeds, refreshing the file from the identical version in
3335 ;; the master.
3336 ;; SCCS: fails with error ut4 (p file nonexistent).
3338 ;; Either of these consequences is acceptable.
3340 ;; 11. File is locked by calling user. It looks unchanged, but is actually
3341 ;; changed.
3343 ;; Potential cause: the file would have to be touched by a self-race
3344 ;; during window Q.
3346 ;; The revert will succeed, removing whatever changes came with
3347 ;; the touch. It is theoretically possible that work could be lost.
3349 ;; 12. File looks like it's locked by the calling user and unchanged, but
3350 ;; it's actually locked by someone else.
3352 ;; Potential cause: a steal-lock in window V.
3354 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
3355 ;; SCCS: fails with error un2
3357 ;; We can pass these errors up to the user.
3359 ;; Apparent state D ---
3361 ;; 13. File looks like it's locked by the calling user and changed, but it's
3362 ;; actually unregistered.
3364 ;; Potential cause: master file got nuked during window P.
3366 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
3367 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
3368 ;; option, failing with message "no such file or directory".
3369 ;; SCCS: will fail with error ut4.
3371 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
3372 ;; VC may fail to detect the loss of previous version information.
3374 ;; 14. File looks like it's locked by the calling user and changed, but it's
3375 ;; actually unlocked.
3377 ;; Potential cause: self-race in window V, or the checkin happening
3378 ;; during the window X of someone else's steal-lock or window S of
3379 ;; someone else's revert.
3381 ;; RCS: ci will fail with "no lock set by <user>".
3382 ;; SCCS: delta will fail with error ut4.
3384 ;; 15. File looks like it's locked by the calling user and changed, but it's
3385 ;; actually locked by the calling user and unchanged.
3387 ;; Potential cause: another self-race --- a whole checkin/checkout
3388 ;; sequence by the calling user would have to land in window R.
3390 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
3391 ;; RCS: reverts to the file state as of the second user's checkin, leaving
3392 ;; the file unlocked.
3394 ;; It is theoretically possible that work could be lost under RCS.
3396 ;; 16. File looks like it's locked by the calling user and changed, but it's
3397 ;; actually locked by a different user.
3399 ;; RCS: ci error: no lock set by <user>
3400 ;; SCCS: unget will fail with error un2
3402 ;; We can pass these errors up to the user.
3404 ;; Apparent state E ---
3406 ;; 17. File looks like it's locked by some other user, but it's actually
3407 ;; unregistered.
3409 ;; As case 13.
3411 ;; 18. File looks like it's locked by some other user, but it's actually
3412 ;; unlocked.
3414 ;; Potential cause: someone released a lock during window W.
3416 ;; RCS: The calling user will get the lock on the file.
3417 ;; SCCS: unget -n will fail with cm4.
3419 ;; Either of these consequences will be OK.
3421 ;; 19. File looks like it's locked by some other user, but it's actually
3422 ;; locked by the calling user and unchanged.
3424 ;; Potential cause: the other user relinquishing a lock followed by
3425 ;; a self-race, both in window W.
3427 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
3428 ;; the sequence a no-op.
3430 ;; 20. File looks like it's locked by some other user, but it's actually
3431 ;; locked by the calling user and changed.
3433 ;; As case 19.
3435 ;; PROBLEM CASES:
3437 ;; In order of decreasing severity:
3439 ;; Cases 11 and 15 are the only ones that potentially lose work.
3440 ;; They would require a self-race for this to happen.
3442 ;; Case 13 in RCS loses information about previous deltas, retaining
3443 ;; only the information in the current workfile. This can only happen
3444 ;; if the master file gets nuked in window P.
3446 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
3447 ;; no change comment in the master. This would require a self-race in
3448 ;; window P or R respectively.
3450 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
3452 ;; Unfortunately, it appears to me that no recovery is possible in these
3453 ;; cases. They don't yield error messages, so there's no way to tell that
3454 ;; a race condition has occurred.
3456 ;; All other cases don't change either the workfile or the master, and
3457 ;; trigger command errors which the user will see.
3459 ;; Thus, there is no explicit recovery code.
3461 ;;; vc.el ends here