(calendar-location-name, calendar-latitude)
[emacs.git] / lisp / vc.el
blob274376765c57d4a79db6d17cf9f9912ab8173ff9
1 ;;; vc.el --- drive a version-control system from within Emacs
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see below for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; Keywords: tools
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 3, 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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Credits:
30 ;; VC was initially designed and implemented by Eric S. Raymond
31 ;; <esr@thyrsus.com> in 1992. Over the years, many other people have
32 ;; contributed substantial amounts of work to VC. These include:
34 ;; Per Cederqvist <ceder@lysator.liu.se>
35 ;; Paul Eggert <eggert@twinsun.com>
36 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
37 ;; Martin Lorentzson <martinl@gnu.org>
38 ;; Dave Love <fx@gnu.org>
39 ;; Stefan Monnier <monnier@cs.yale.edu>
40 ;; J.D. Smith <jdsmith@alum.mit.edu>
41 ;; Andre Spiegel <spiegel@gnu.org>
42 ;; Richard Stallman <rms@gnu.org>
43 ;; Thien-Thi Nguyen <ttn@gnu.org>
45 ;; In July 2007 ESR returned and redesigned the mode to cope better
46 ;; with modern version-control systems that do commits by fileset
47 ;; rather than per individual file.
49 ;; Features in the new version:
50 ;; * Key commands (vc-next-action = C-x v v, vc-print-log = C-x v l, vc-revert
51 ;; = C-x v u, vc-rollback = C-x v c, vc-diff = C-x v =, vc-update = C-x v +)
52 ;; now operate on filesets rather than individual files.
53 ;; * The fileset for a command is either (a) all marked files in VC-dired
54 ;; mode, (b) the currently visited file if it's under version control,
55 ;; or (c) the current directory if the visited buffer is not under
56 ;; version control and a wildcarding-enable flag has been set.
58 ;; If you maintain a client of the mode or customize it in your .emacs,
59 ;; note that some backend functions which formerly took single file arguments
60 ;; now take a list of files. These include: register, checkin, print-log,
61 ;; rollback, and diff.
63 ;;; Commentary:
65 ;; This mode is fully documented in the Emacs user's manual.
67 ;; Supported version-control systems presently include CVS, RCS, GNU
68 ;; Arch, Subversion, Bzr, Git, Mercurial, Meta-CVS, Monotone and SCCS
69 ;; (or its free replacement, CSSC).
71 ;; Some features will not work with old RCS versions. Where
72 ;; appropriate, VC finds out which version you have, and allows or
73 ;; disallows those features (stealing locks, for example, works only
74 ;; from 5.6.2 onwards).
75 ;; Even initial checkins will fail if your RCS version is so old that ci
76 ;; doesn't understand -t-; this has been known to happen to people running
77 ;; NExTSTEP 3.0.
79 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
81 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
82 ;; to be installed somewhere on Emacs's path for executables.
84 ;; If your site uses the ChangeLog convention supported by Emacs, the
85 ;; function `log-edit-comment-to-change-log' could prove a useful checkin hook,
86 ;; although you might prefer to use C-c C-a (i.e. `log-edit-insert-changelog')
87 ;; from the commit buffer instead or to set `log-edit-setup-invert'.
89 ;; The vc code maintains some internal state in order to reduce expensive
90 ;; version-control operations to a minimum. Some names are only computed
91 ;; once. If you perform version control operations with the backend while
92 ;; vc's back is turned, or move/rename master files while vc is running,
93 ;; vc may get seriously confused. Don't do these things!
95 ;; Developer's notes on some concurrency issues are included at the end of
96 ;; the file.
98 ;; ADDING SUPPORT FOR OTHER BACKENDS
100 ;; VC can use arbitrary version control systems as a backend. To add
101 ;; support for a new backend named SYS, write a library vc-sys.el that
102 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
103 ;; case for the function and library names). VC will use that library if
104 ;; you put the symbol SYS somewhere into the list of
105 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
106 ;; returns non-nil for a file, all SYS-specific versions of VC commands
107 ;; will be available for that file.
109 ;; VC keeps some per-file information in the form of properties (see
110 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
111 ;; do not generally need to be aware of these properties. For example,
112 ;; `vc-sys-working-revision' should compute the working revision and
113 ;; return it; it should not look it up in the property, and it needn't
114 ;; store it there either. However, if a backend-specific function does
115 ;; store a value in a property, that value takes precedence over any
116 ;; value that the generic code might want to set (check for uses of
117 ;; the macro `with-vc-properties' in vc.el).
119 ;; In the list of functions below, each identifier needs to be prepended
120 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
121 ;; `*'), others are optional (`-').
123 ;; BACKEND PROPERTIES
125 ;; * revision-granularity
127 ;; Takes no arguments. Returns either 'file or 'repository. Backends
128 ;; that return 'file have per-file revision numbering; backends
129 ;; that return 'repository have per-repository revision numbering,
130 ;; so a revision level implicitly identifies a changeset
132 ;; STATE-QUERYING FUNCTIONS
134 ;; * registered (file)
136 ;; Return non-nil if FILE is registered in this backend. Both this
137 ;; function as well as `state' should be careful to fail gracefully
138 ;; in the event that the backend executable is absent. It is
139 ;; preferable that this function's body is autoloaded, that way only
140 ;; calling vc-registered does not cause the backend to be loaded
141 ;; (all the vc-FOO-registered functions are called to try to find
142 ;; the controlling backend for FILE.
144 ;; * state (file)
146 ;; Return the current version control state of FILE. For a list of
147 ;; possible values, see `vc-state'. This function should do a full and
148 ;; reliable state computation; it is usually called immediately after
149 ;; C-x v v. If you want to use a faster heuristic when visiting a
150 ;; file, put that into `state-heuristic' below.
152 ;; - state-heuristic (file)
154 ;; If provided, this function is used to estimate the version control
155 ;; state of FILE at visiting time. It should be considerably faster
156 ;; than the implementation of `state'. For a list of possible values,
157 ;; see the doc string of `vc-state'.
159 ;; - dir-state (dir)
161 ;; If provided, this function is used to find the version control
162 ;; state of as many files as possible in DIR, and all subdirectories
163 ;; of DIR, in a fast way; it is used to avoid expensive indivitual
164 ;; vc-state calls. The function should not return anything, but
165 ;; rather store the files' states into the corresponding properties.
166 ;; Two properties are required: `vc-backend' and `vc-state'. (Note:
167 ;; in older versions this method was not required to recurse into
168 ;; subdirectories.)
170 ;; - dir-status (dir update-function status-buffer)
172 ;; Produce RESULT: a list of conses of the form (file . vc-state)
173 ;; for the files in DIR. If a command needs to be run to compute
174 ;; this list, it should be run asynchronously. When RESULT is
175 ;; computed, it should be passed back by doing:
176 ;; (funcall UPDATE-FUNCTION RESULT STATUS-BUFFER)
177 ;; Return the buffer used for the asynchronous call. This buffer
178 ;; is used to kill the status update process on demand.
179 ;; This function is used by vc-status, a replacement for vc-dired.
180 ;; vc-status is still under development, and is NOT feature
181 ;; complete. As such, the requirements for this function might
182 ;; change.
183 ;; This is a replacement for dir-state.
185 ;; * working-revision (file)
187 ;; Return the working revision of FILE. This is the revision fetched
188 ;; by the last checkout or upate, not necessarily the same thing as the
189 ;; head or tip revision. Should return "0" for a file added but not yet
190 ;; committed.
192 ;; - latest-on-branch-p (file)
194 ;; Return non-nil if the working revision of FILE is the latest revision
195 ;; on its branch (many VCSes call this the 'tip' or 'head' revision).
196 ;; The default implementation always returns t, which means that
197 ;; working with non-current revisions is not supported by default.
199 ;; * checkout-model (file)
201 ;; Indicate whether FILE needs to be "checked out" before it can be
202 ;; edited. See `vc-checkout-model' for a list of possible values.
204 ;; - workfile-unchanged-p (file)
206 ;; Return non-nil if FILE is unchanged from the working revision.
207 ;; This function should do a brief comparison of FILE's contents
208 ;; with those of the repository master of the working revision. If
209 ;; the backend does not have such a brief-comparison feature, the
210 ;; default implementation of this function can be used, which
211 ;; delegates to a full vc-BACKEND-diff. (Note that vc-BACKEND-diff
212 ;; must not run asynchronously in this case, see variable
213 ;; `vc-disable-async-diff'.)
215 ;; - mode-line-string (file)
217 ;; If provided, this function should return the VC-specific mode
218 ;; line string for FILE. The returned string should have a
219 ;; `help-echo' property which is the text to be displayed as a
220 ;; tooltip when the mouse hovers over the VC entry on the mode-line.
221 ;; The default implementation deals well with all states that
222 ;; `vc-state' can return.
224 ;; - dired-state-info (file)
226 ;; Translate the `vc-state' property of FILE into a string that can be
227 ;; used in a vc-dired buffer. The default implementation deals well
228 ;; with all states that `vc-state' can return.
230 ;; STATE-CHANGING FUNCTIONS
232 ;; * create-repo (backend)
234 ;; Create an empty repository in the current directory and initialize
235 ;; it so VC mode can add files to it. For file-oriented systems, this
236 ;; need do no more than create a subdirectory with the right name.
238 ;; * register (files &optional rev comment)
240 ;; Register FILES in this backend. Optionally, an initial revision REV
241 ;; and an initial description of the file, COMMENT, may be specified,
242 ;; but it is not guaranteed that the backend will do anything with this.
243 ;; The implementation should pass the value of vc-register-switches
244 ;; to the backend command. (Note: in older versions of VC, this
245 ;; command took a single file argument and not a list.)
247 ;; - init-revision (file)
249 ;; The initial revision to use when registering FILE if one is not
250 ;; specified by the user. If not provided, the variable
251 ;; vc-default-init-revision is used instead.
253 ;; - responsible-p (file)
255 ;; Return non-nil if this backend considers itself "responsible" for
256 ;; FILE, which can also be a directory. This function is used to find
257 ;; out what backend to use for registration of new files and for things
258 ;; like change log generation. The default implementation always
259 ;; returns nil.
261 ;; - could-register (file)
263 ;; Return non-nil if FILE could be registered under this backend. The
264 ;; default implementation always returns t.
266 ;; - receive-file (file rev)
268 ;; Let this backend "receive" a file that is already registered under
269 ;; another backend. The default implementation simply calls `register'
270 ;; for FILE, but it can be overridden to do something more specific,
271 ;; e.g. keep revision numbers consistent or choose editing modes for
272 ;; FILE that resemble those of the other backend.
274 ;; - unregister (file)
276 ;; Unregister FILE from this backend. This is only needed if this
277 ;; backend may be used as a "more local" backend for temporary editing.
279 ;; * checkin (files rev comment)
281 ;; Commit changes in FILES to this backend. If REV is non-nil, that
282 ;; should become the new revision number (not all backends do
283 ;; anything with it). COMMENT is used as a check-in comment. The
284 ;; implementation should pass the value of vc-checkin-switches to
285 ;; the backend command. (Note: in older versions of VC, this
286 ;; command took a single file argument and not a list.)
288 ;; * find-revision (file rev buffer)
290 ;; Fetch revision REV of file FILE and put it into BUFFER.
291 ;; If REV is the empty string, fetch the head of the trunk.
292 ;; The implementation should pass the value of vc-checkout-switches
293 ;; to the backend command.
295 ;; * checkout (file &optional editable rev)
297 ;; Check out revision REV of FILE into the working area. If EDITABLE
298 ;; is non-nil, FILE should be writable by the user and if locking is
299 ;; used for FILE, a lock should also be set. If REV is non-nil, that
300 ;; is the revision to check out (default is the working revision).
301 ;; If REV is t, that means to check out the head of the current branch;
302 ;; if it is the empty string, check out the head of the trunk.
303 ;; The implementation should pass the value of vc-checkout-switches
304 ;; to the backend command.
306 ;; * revert (file &optional contents-done)
308 ;; Revert FILE back to the working revision. If optional
309 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
310 ;; already been reverted from a version backup, and this function
311 ;; only needs to update the status of FILE within the backend.
313 ;; - rollback (files)
315 ;; Remove the tip revision of each of FILES from the repository. If
316 ;; this function is not provided, trying to cancel a revision is
317 ;; caught as an error. (Most backends don't provide it.) (Also
318 ;; note that older versions of this backend command were called
319 ;; 'cancel-version' and took a single file arg, not a list of
320 ;; files.)
322 ;; - merge (file rev1 rev2)
324 ;; Merge the changes between REV1 and REV2 into the current working file.
326 ;; - merge-news (file)
328 ;; Merge recent changes from the current branch into FILE.
330 ;; - steal-lock (file &optional revision)
332 ;; Steal any lock on the working revision of FILE, or on REVISION if
333 ;; that is provided. This function is only needed if locking is
334 ;; used for files under this backend, and if files can indeed be
335 ;; locked by other users.
337 ;; - modify-change-comment (files rev comment)
339 ;; Modify the change comments associated with the files at the
340 ;; given revision. This is optional, many backends do not support it.
342 ;; HISTORY FUNCTIONS
344 ;; * print-log (files &optional buffer)
346 ;; Insert the revision log for FILES into BUFFER, or the *vc* buffer
347 ;; if BUFFER is nil. (Note: older versions of this function expected
348 ;; only a single file argument.)
350 ;; - log-view-mode ()
352 ;; Mode to use for the output of print-log. This defaults to
353 ;; `log-view-mode' and is expected to be changed (if at all) to a derived
354 ;; mode of `log-view-mode'.
356 ;; - show-log-entry (revision)
358 ;; If provided, search the log entry for REVISION in the current buffer,
359 ;; and make sure it is displayed in the buffer's window. The default
360 ;; implementation of this function works for RCS-style logs.
362 ;; - wash-log (file)
364 ;; Remove all non-comment information from the output of print-log.
366 ;; - logentry-check ()
368 ;; If defined, this function is run to find out whether the user
369 ;; entered a valid log entry for check-in. The log entry is in the
370 ;; current buffer, and if it is not a valid one, the function should
371 ;; throw an error.
373 ;; - comment-history (file)
375 ;; Return a string containing all log entries that were made for FILE.
376 ;; This is used for transferring a file from one backend to another,
377 ;; retaining comment information. The default implementation of this
378 ;; function does this by calling print-log and then wash-log, and
379 ;; returning the resulting buffer contents as a string.
381 ;; - update-changelog (files)
383 ;; Using recent log entries, create ChangeLog entries for FILES, or for
384 ;; all files at or below the default-directory if FILES is nil. The
385 ;; default implementation runs rcs2log, which handles RCS- and
386 ;; CVS-style logs.
388 ;; * diff (files &optional rev1 rev2 buffer)
390 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
391 ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
392 ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
393 ;; found in the repository) as the older revision; if REV2 is nil,
394 ;; use the current working-copy contents as the newer revision. This
395 ;; function should pass the value of (vc-switches BACKEND 'diff) to
396 ;; the backend command. It should return a status of either 0 (no
397 ;; differences found), or 1 (either non-empty diff or the diff is
398 ;; run asynchronously).
400 ;; - revision-completion-table (files)
402 ;; Return a completion table for existing revisions of FILES.
403 ;; The default is to not use any completion table.
405 ;; - annotate-command (file buf &optional rev)
407 ;; If this function is provided, it should produce an annotated display
408 ;; of FILE in BUF, relative to revision REV. Annotation means each line
409 ;; of FILE displayed is prefixed with version information associated with
410 ;; its addition (deleted lines leave no history) and that the text of the
411 ;; file is fontified according to age.
413 ;; - annotate-time ()
415 ;; Only required if `annotate-command' is defined for the backend.
416 ;; Return the time of the next line of annotation at or after point,
417 ;; as a floating point fractional number of days. The helper
418 ;; function `vc-annotate-convert-time' may be useful for converting
419 ;; multi-part times as returned by `current-time' and `encode-time'
420 ;; to this format. Return nil if no more lines of annotation appear
421 ;; in the buffer. You can safely assume that point is placed at the
422 ;; beginning of each line, starting at `point-min'. The buffer that
423 ;; point is placed in is the Annotate output, as defined by the
424 ;; relevant backend. This function also affects how much of the line
425 ;; is fontified; where it leaves point is where fontification begins.
427 ;; - annotate-current-time ()
429 ;; Only required if `annotate-command' is defined for the backend,
430 ;; AND you'd like the current time considered to be anything besides
431 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
432 ;; time with hours, minutes, and seconds included. Probably safe to
433 ;; ignore. Return the current-time, in units of fractional days.
435 ;; - annotate-extract-revision-at-line ()
437 ;; Only required if `annotate-command' is defined for the backend.
438 ;; Invoked from a buffer in vc-annotate-mode, return the revision
439 ;; corresponding to the current line, or nil if there is no revision
440 ;; corresponding to the current line.
442 ;; SNAPSHOT SYSTEM
444 ;; - create-snapshot (dir name branchp)
446 ;; Take a snapshot of the current state of files under DIR and name it
447 ;; NAME. This should make sure that files are up-to-date before
448 ;; proceeding with the action. DIR can also be a file and if BRANCHP
449 ;; is specified, NAME should be created as a branch and DIR should be
450 ;; checked out under this new branch. The default implementation does
451 ;; not support branches but does a sanity check, a tree traversal and
452 ;; for each file calls `assign-name'.
454 ;; - assign-name (file name)
456 ;; Give name NAME to the working revision of FILE, assuming it is
457 ;; up-to-date. Only used by the default version of `create-snapshot'.
459 ;; - retrieve-snapshot (dir name update)
461 ;; Retrieve a named snapshot of all registered files at or below DIR.
462 ;; If UPDATE is non-nil, then update buffers of any files in the
463 ;; snapshot that are currently visited. The default implementation
464 ;; does a sanity check whether there aren't any uncommitted changes at
465 ;; or below DIR, and then performs a tree walk, using the `checkout'
466 ;; function to retrieve the corresponding revisions.
468 ;; MISCELLANEOUS
470 ;; - root (dir)
472 ;; Return DIR's "root" directory, that is, a parent directory of
473 ;; DIR for which the same backend as used for DIR applies. If no
474 ;; such parent exists, this function should return DIR.
476 ;; - make-version-backups-p (file)
478 ;; Return non-nil if unmodified repository revisions of FILE should be
479 ;; backed up locally. If this is done, VC can perform `diff' and
480 ;; `revert' operations itself, without calling the backend system. The
481 ;; default implementation always returns nil.
483 ;; - repository-hostname (dirname)
485 ;; Return the hostname that the backend will have to contact
486 ;; in order to operate on a file in DIRNAME. If the return value
487 ;; is nil, it means that the repository is local.
488 ;; This function is used in `vc-stay-local-p' which backends can use
489 ;; for their convenience.
491 ;; - previous-revision (file rev)
493 ;; Return the revision number that precedes REV for FILE, or nil if no such
494 ;; revision exists.
496 ;; - next-revision (file rev)
498 ;; Return the revision number that follows REV for FILE, or nil if no such
499 ;; revision exists.
501 ;; - check-headers ()
503 ;; Return non-nil if the current buffer contains any version headers.
505 ;; - clear-headers ()
507 ;; In the current buffer, reset all version headers to their unexpanded
508 ;; form. This function should be provided if the state-querying code
509 ;; for this backend uses the version headers to determine the state of
510 ;; a file. This function will then be called whenever VC changes the
511 ;; version control state in such a way that the headers would give
512 ;; wrong information.
514 ;; - delete-file (file)
516 ;; Delete FILE and mark it as deleted in the repository. If this
517 ;; function is not provided, the command `vc-delete-file' will
518 ;; signal an error.
520 ;; - rename-file (old new)
522 ;; Rename file OLD to NEW, both in the working area and in the
523 ;; repository. If this function is not provided, the renaming
524 ;; will be done by (vc-delete-file old) and (vc-register new).
526 ;; - find-file-hook ()
528 ;; Operation called in current buffer when opening a file. This can
529 ;; be used by the backend to setup some local variables it might need.
531 ;; - find-file-not-found-hook ()
533 ;; Operation called in current buffer when opening a non-existing file.
534 ;; By default, this asks the user if she wants to check out the file.
536 ;; - extra-menu ()
538 ;; Return a menu keymap, the items in the keymap will appear at the
539 ;; end of the Version Control menu. The goal is to allow backends
540 ;; to specify extra menu items that appear in the VC menu. This way
541 ;; you can provide menu entries for functionality that is specific
542 ;; to your backend and which does not map to any of the VC generic
543 ;; concepts.
545 ;; - extra-status-menu ()
547 ;; Return a menu keymap, the items in the keymap will appear at the
548 ;; end of the VC Status menu. The goal is to allow backends to
549 ;; specify extra menu items that appear in the VC Status menu. This
550 ;; makes it possible to provide menu entries for functionality that
551 ;; is specific to a backend and which does not map to any of the VC
552 ;; generic concepts.
554 ;;; Todo:
556 ;; - vc-update/vc-merge should deal with VC systems that don't
557 ;; update/merge on a file basis, but on a whole repository basis.
559 ;; - the backend sometimes knows when a file it opens has been marked
560 ;; by the VCS as having a "conflict". Find a way to pass this info -
561 ;; to VC so that it can turn on smerge-mode when opening such a
562 ;; file.
564 ;; - the *VC-log* buffer needs font-locking.
566 ;; - make it easier to write logs, maybe C-x 4 a should add to the log
567 ;; buffer if there's one instead of the ChangeLog.
569 ;; - make vc-state for all backends return 'unregistered instead of
570 ;; nil for unregistered files, then update vc-next-action.
572 ;; - add a generic mechanism for remembering the current branch names,
573 ;; display the branch name in the mode-line. Replace
574 ;; vc-cvs-sticky-tag with that.
576 ;; - vc-register should register a fileset at a time. The backends
577 ;; already support this, only the front-end needs to be change to
578 ;; handle multiple files at a time.
580 ;; - add a mechanism to for ignoring files.
582 ;; - deal with push/pull operations.
584 ;; - decide if vc-status should replace vc-dired.
586 ;; - vc-status needs mouse bindings and some color bling.
588 ;; - vc-status needs to show missing files. It probably needs to have
589 ;; another state for those files. The user might want to restore
590 ;; them, or remove them from the VCS. C-x v v might also need
591 ;; adjustments.
593 ;; - when changing a file whose directory is shown in the vc-status
594 ;; buffer, it should be added there as "modified". (PCL-CVS does this).
596 ;; - Update the vc-status buffers after vc operations, implement the
597 ;; equivalent of vc-dired-resynch-file.
599 ;; - vc-status needs a toolbar.
601 ;; - vc-status: refresh should not completely wipe out the current
602 ;; contents of the vc-status buffer.
604 ;; - vc-diff, vc-annotate, etc. need to deal better with unregistered
605 ;; files. Now that unregistered and ignored files are shown in
606 ;; vc-dired/vc-status, it is possible that these commands are called
607 ;; for unregistered/ignored files.
609 ;; - "snapshots" should be renamed to "branches", and thoroughly reworked.
611 ;; - do not default to RCS anymore when the current directory is not
612 ;; controlled by any VCS and the user does C-x v v
615 ;;; Code:
617 (require 'vc-hooks)
618 (require 'ring)
619 (require 'tool-bar)
621 (eval-when-compile
622 (require 'cl)
623 (require 'compile)
624 (require 'dired) ; for dired-map-over-marks macro
625 (require 'dired-aux)) ; for dired-kill-{line,tree}
627 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
628 (setq minor-mode-alist
629 (cons '(vc-parent-buffer vc-parent-buffer-name)
630 minor-mode-alist)))
632 ;; General customization
634 (defgroup vc nil
635 "Version-control system in Emacs."
636 :group 'tools)
638 (defcustom vc-suppress-confirm nil
639 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
640 :type 'boolean
641 :group 'vc)
643 (defcustom vc-delete-logbuf-window t
644 "If non-nil, delete the *VC-log* buffer and window after each logical action.
645 If nil, bury that buffer instead.
646 This is most useful if you have multiple windows on a frame and would like to
647 preserve the setting."
648 :type 'boolean
649 :group 'vc)
651 (defcustom vc-initial-comment nil
652 "If non-nil, prompt for initial comment when a file is registered."
653 :type 'boolean
654 :group 'vc)
656 (defcustom vc-default-init-revision "1.1"
657 "A string used as the default revision number when a new file is registered.
658 This can be overridden by giving a prefix argument to \\[vc-register]. This
659 can also be overridden by a particular VC backend."
660 :type 'string
661 :group 'vc
662 :version "20.3")
664 (defcustom vc-command-messages nil
665 "If non-nil, display run messages from back-end commands."
666 :type 'boolean
667 :group 'vc)
669 (defcustom vc-checkin-switches nil
670 "A string or list of strings specifying extra switches for checkin.
671 These are passed to the checkin program by \\[vc-checkin]."
672 :type '(choice (const :tag "None" nil)
673 (string :tag "Argument String")
674 (repeat :tag "Argument List"
675 :value ("")
676 string))
677 :group 'vc)
679 (defcustom vc-checkout-switches nil
680 "A string or list of strings specifying extra switches for checkout.
681 These are passed to the checkout program by \\[vc-checkout]."
682 :type '(choice (const :tag "None" nil)
683 (string :tag "Argument String")
684 (repeat :tag "Argument List"
685 :value ("")
686 string))
687 :group 'vc)
689 (defcustom vc-register-switches nil
690 "A string or list of strings; extra switches for registering a file.
691 These are passed to the checkin program by \\[vc-register]."
692 :type '(choice (const :tag "None" nil)
693 (string :tag "Argument String")
694 (repeat :tag "Argument List"
695 :value ("")
696 string))
697 :group 'vc)
699 (defcustom vc-dired-listing-switches "-al"
700 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
701 :type 'string
702 :group 'vc
703 :version "21.1")
705 (defcustom vc-dired-recurse t
706 "If non-nil, show directory trees recursively in VC Dired."
707 :type 'boolean
708 :group 'vc
709 :version "20.3")
711 (defcustom vc-dired-terse-display t
712 "If non-nil, show only locked or locally modified files in VC Dired."
713 :type 'boolean
714 :group 'vc
715 :version "20.3")
717 (defcustom vc-diff-switches nil
718 "A string or list of strings specifying switches for diff under VC.
719 When running diff under a given BACKEND, VC concatenates the values of
720 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
721 get the switches for that command. Thus, `vc-diff-switches' should
722 contain switches that are specific to version control, but not
723 specific to any particular backend."
724 :type '(choice (const :tag "None" nil)
725 (string :tag "Argument String")
726 (repeat :tag "Argument List"
727 :value ("")
728 string))
729 :group 'vc
730 :version "21.1")
732 (defcustom vc-diff-knows-L nil
733 "*Indicates whether diff understands the -L option.
734 The value is either `yes', `no', or nil. If it is nil, VC tries
735 to use -L and sets this variable to remember whether it worked."
736 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
737 :group 'vc)
739 (defcustom vc-allow-async-revert nil
740 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
741 Enabling this option means that you can confirm a revert operation even
742 if the local changes in the file have not been found and displayed yet."
743 :type '(choice (const :tag "No" nil)
744 (const :tag "Yes" t))
745 :group 'vc
746 :version "22.1")
748 ;;;###autoload
749 (defcustom vc-checkout-hook nil
750 "Normal hook (list of functions) run after checking out a file.
751 See `run-hooks'."
752 :type 'hook
753 :group 'vc
754 :version "21.1")
756 (defcustom vc-annotate-display-mode 'fullscale
757 "Which mode to color the output of \\[vc-annotate] with by default."
758 :type '(choice (const :tag "By Color Map Range" nil)
759 (const :tag "Scale to Oldest" scale)
760 (const :tag "Scale Oldest->Newest" fullscale)
761 (number :tag "Specify Fractional Number of Days"
762 :value "20.5"))
763 :group 'vc)
765 ;;;###autoload
766 (defcustom vc-checkin-hook nil
767 "Normal hook (list of functions) run after commit or file checkin.
768 See also `log-edit-done-hook'."
769 :type 'hook
770 :options '(log-edit-comment-to-change-log)
771 :group 'vc)
773 ;;;###autoload
774 (defcustom vc-before-checkin-hook nil
775 "Normal hook (list of functions) run before a commit or a file checkin.
776 See `run-hooks'."
777 :type 'hook
778 :group 'vc)
780 (defcustom vc-logentry-check-hook nil
781 "Normal hook run by `vc-backend-logentry-check'.
782 Use this to impose your own rules on the entry in addition to any the
783 version control backend imposes itself."
784 :type 'hook
785 :group 'vc)
787 ;; Annotate customization
788 (defcustom vc-annotate-color-map
789 (if (and (tty-display-color-p) (<= (display-color-cells) 8))
790 ;; A custom sorted TTY colormap
791 (let* ((colors
792 (sort
793 (delq nil
794 (mapcar (lambda (x)
795 (if (not (or
796 (string-equal (car x) "white")
797 (string-equal (car x) "black") ))
798 (car x)))
799 (tty-color-alist)))
800 (lambda (a b)
801 (cond
802 ((or (string-equal a "red") (string-equal b "blue")) t)
803 ((or (string-equal b "red") (string-equal a "blue")) nil)
804 ((string-equal a "yellow") t)
805 ((string-equal b "yellow") nil)
806 ((string-equal a "cyan") t)
807 ((string-equal b "cyan") nil)
808 ((string-equal a "green") t)
809 ((string-equal b "green") nil)
810 ((string-equal a "magenta") t)
811 ((string-equal b "magenta") nil)
812 (t (string< a b))))))
813 (date 20.)
814 (delta (/ (- 360. date) (1- (length colors)))))
815 (mapcar (lambda (x)
816 (prog1
817 (cons date x)
818 (setq date (+ date delta)))) colors))
819 ;; Normal colormap: hue stepped from 0-240deg, value=1., saturation=0.75
820 '(( 20. . "#FF3F3F")
821 ( 40. . "#FF6C3F")
822 ( 60. . "#FF993F")
823 ( 80. . "#FFC63F")
824 (100. . "#FFF33F")
825 (120. . "#DDFF3F")
826 (140. . "#B0FF3F")
827 (160. . "#83FF3F")
828 (180. . "#56FF3F")
829 (200. . "#3FFF56")
830 (220. . "#3FFF83")
831 (240. . "#3FFFB0")
832 (260. . "#3FFFDD")
833 (280. . "#3FF3FF")
834 (300. . "#3FC6FF")
835 (320. . "#3F99FF")
836 (340. . "#3F6CFF")
837 (360. . "#3F3FFF")))
838 "Association list of age versus color, for \\[vc-annotate].
839 Ages are given in units of fractional days. Default is eighteen
840 steps using a twenty day increment, from red to blue. For TTY
841 displays with 8 or fewer colors, the default is red to blue with
842 all other colors between (excluding black and white)."
843 :type 'alist
844 :group 'vc)
846 (defcustom vc-annotate-very-old-color "#3F3FFF"
847 "Color for lines older than the current color range in \\[vc-annotate]]."
848 :type 'string
849 :group 'vc)
851 (defcustom vc-annotate-background "black"
852 "Background color for \\[vc-annotate].
853 Default color is used if nil."
854 :type '(choice (const :tag "Default background" nil) (color))
855 :group 'vc)
857 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
858 "Menu elements for the mode-specific menu of VC-Annotate mode.
859 List of factors, used to expand/compress the time scale. See `vc-annotate'."
860 :type '(repeat number)
861 :group 'vc)
863 (defvar vc-annotate-mode-map
864 (let ((m (make-sparse-keymap)))
865 (define-key m "A" 'vc-annotate-revision-previous-to-line)
866 (define-key m "D" 'vc-annotate-show-diff-revision-at-line)
867 (define-key m "f" 'vc-annotate-find-revision-at-line)
868 (define-key m "J" 'vc-annotate-revision-at-line)
869 (define-key m "L" 'vc-annotate-show-log-revision-at-line)
870 (define-key m "N" 'vc-annotate-next-revision)
871 (define-key m "P" 'vc-annotate-prev-revision)
872 (define-key m "W" 'vc-annotate-working-revision)
873 (define-key m "V" 'vc-annotate-toggle-annotation-visibility)
875 "Local keymap used for VC-Annotate mode.")
877 ;; Header-insertion hair
879 (defcustom vc-static-header-alist
880 '(("\\.c\\'" .
881 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
882 "*Associate static header string templates with file types.
883 A \%s in the template is replaced with the first string associated with
884 the file's version control type in `vc-header-alist'."
885 :type '(repeat (cons :format "%v"
886 (regexp :tag "File Type")
887 (string :tag "Header String")))
888 :group 'vc)
890 (defcustom vc-comment-alist
891 '((nroff-mode ".\\\"" ""))
892 "*Special comment delimiters for generating VC headers.
893 Add an entry in this list if you need to override the normal `comment-start'
894 and `comment-end' variables. This will only be necessary if the mode language
895 is sensitive to blank lines."
896 :type '(repeat (list :format "%v"
897 (symbol :tag "Mode")
898 (string :tag "Comment Start")
899 (string :tag "Comment End")))
900 :group 'vc)
902 (defcustom vc-checkout-carefully (= (user-uid) 0)
903 "*Non-nil means be extra-careful in checkout.
904 Verify that the file really is not locked
905 and that its contents match what the master file says."
906 :type 'boolean
907 :group 'vc)
908 (make-obsolete-variable 'vc-checkout-carefully
909 "the corresponding checks are always done now."
910 "21.1")
913 ;; Variables the user doesn't need to know about.
914 (defvar vc-log-operation nil)
915 (defvar vc-log-after-operation-hook nil)
917 ;; In a log entry buffer, this is a local variable
918 ;; that points to the buffer for which it was made
919 ;; (either a file, or a VC dired buffer).
920 (defvar vc-parent-buffer nil)
921 (put 'vc-parent-buffer 'permanent-local t)
922 (defvar vc-parent-buffer-name nil)
923 (put 'vc-parent-buffer-name 'permanent-local t)
925 (defvar vc-disable-async-diff nil
926 "VC sets this to t locally to disable some async diff operations.
927 Backends that offer asynchronous diffs should respect this variable
928 in their implementation of vc-BACKEND-diff.")
930 (defvar vc-log-fileset)
931 (defvar vc-log-revision)
933 (defvar vc-dired-mode nil)
934 (make-variable-buffer-local 'vc-dired-mode)
936 ;; File property caching
938 (defun vc-clear-context ()
939 "Clear all cached file properties."
940 (interactive)
941 (fillarray vc-file-prop-obarray 0))
943 (defmacro with-vc-properties (files form settings)
944 "Execute FORM, then maybe set per-file properties for FILES.
945 SETTINGS is an association list of property/value pairs. After
946 executing FORM, set those properties from SETTINGS that have not yet
947 been updated to their corresponding values."
948 (declare (debug t))
949 `(let ((vc-touched-properties (list t)))
950 ,form
951 (dolist (file ,files)
952 (dolist (setting ,settings)
953 (let ((property (car setting)))
954 (unless (memq property vc-touched-properties)
955 (put (intern file vc-file-prop-obarray)
956 property (cdr setting))))))))
958 ;; Two macros for elisp programming
960 ;;;###autoload
961 (defmacro with-vc-file (file comment &rest body)
962 "Check out a writable copy of FILE if necessary, then execute BODY.
963 Check in FILE with COMMENT (a string) after BODY has been executed.
964 FILE is passed through `expand-file-name'; BODY executed within
965 `save-excursion'. If FILE is not under version control, or you are
966 using a locking version-control system and the file is locked by
967 somebody else, signal error."
968 (declare (debug t) (indent 2))
969 (let ((filevar (make-symbol "file")))
970 `(let ((,filevar (expand-file-name ,file)))
971 (or (vc-backend ,filevar)
972 (error "File not under version control: `%s'" file))
973 (unless (vc-editable-p ,filevar)
974 (let ((state (vc-state ,filevar)))
975 (if (stringp state)
976 (error "`%s' is locking `%s'" state ,filevar)
977 (vc-checkout ,filevar t))))
978 (save-excursion
979 ,@body)
980 (vc-checkin (list ,filevar) nil ,comment))))
982 ;;;###autoload
983 (defmacro edit-vc-file (file comment &rest body)
984 "Edit FILE under version control, executing body.
985 Checkin with COMMENT after executing BODY.
986 This macro uses `with-vc-file', passing args to it.
987 However, before executing BODY, find FILE, and after BODY, save buffer."
988 (declare (debug t) (indent 2))
989 (let ((filevar (make-symbol "file")))
990 `(let ((,filevar (expand-file-name ,file)))
991 (with-vc-file
992 ,filevar ,comment
993 (set-buffer (find-file-noselect ,filevar))
994 ,@body
995 (save-buffer)))))
997 ;; Common command execution logic to be used by backends
999 (defun vc-process-filter (p s)
1000 "An alternative output filter for async process P.
1001 One difference with the default filter is that this inserts S after markers.
1002 Another is that undo information is not kept."
1003 (let ((buffer (process-buffer p)))
1004 (when (buffer-live-p buffer)
1005 (with-current-buffer buffer
1006 (save-excursion
1007 (let ((buffer-undo-list t)
1008 (inhibit-read-only t))
1009 (goto-char (process-mark p))
1010 (insert s)
1011 (set-marker (process-mark p) (point))))))))
1013 (defun vc-setup-buffer (&optional buf)
1014 "Prepare BUF for executing a VC command and make it current.
1015 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
1016 (unless buf (setq buf "*vc*"))
1017 (let ((camefrom (current-buffer))
1018 (olddir default-directory))
1019 (set-buffer (get-buffer-create buf))
1020 (kill-all-local-variables)
1021 (set (make-local-variable 'vc-parent-buffer) camefrom)
1022 (set (make-local-variable 'vc-parent-buffer-name)
1023 (concat " from " (buffer-name camefrom)))
1024 (setq default-directory olddir)
1025 (let ((buffer-undo-list t)
1026 (inhibit-read-only t))
1027 (erase-buffer))))
1029 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
1031 (defun vc-process-sentinel (p s)
1032 (let ((previous (process-get p 'vc-previous-sentinel))
1033 (buf (process-buffer p)))
1034 ;; Impatient users sometime kill "slow" buffers; check liveness
1035 ;; to avoid "error in process sentinel: Selecting deleted buffer".
1036 (when (buffer-live-p buf)
1037 (if previous (funcall previous p s))
1038 (with-current-buffer buf
1039 (setq mode-line-process
1040 (let ((status (process-status p)))
1041 ;; Leave mode-line uncluttered, normally.
1042 ;; (Let known any weirdness in-form-ally. ;-) --ttn
1043 (unless (eq 'exit status)
1044 (format " (%s)" status))))
1045 (let (vc-sentinel-movepoint)
1046 ;; Normally, we want async code such as sentinels to not move point.
1047 (save-excursion
1048 (goto-char (process-mark p))
1049 (let ((cmds (process-get p 'vc-sentinel-commands)))
1050 (process-put p 'vc-sentinel-commands nil)
1051 (dolist (cmd cmds)
1052 ;; Each sentinel may move point and the next one should be run
1053 ;; at that new point. We could get the same result by having
1054 ;; each sentinel read&set process-mark, but since `cmd' needs
1055 ;; to work both for async and sync processes, this would be
1056 ;; difficult to achieve.
1057 (vc-exec-after cmd))))
1058 ;; But sometimes the sentinels really want to move point.
1059 (if vc-sentinel-movepoint
1060 (let ((win (get-buffer-window (current-buffer) 0)))
1061 (if (not win)
1062 (goto-char vc-sentinel-movepoint)
1063 (with-selected-window win
1064 (goto-char vc-sentinel-movepoint))))))))))
1066 (defun vc-set-mode-line-busy-indicator ()
1067 (setq mode-line-process
1068 (concat " " (propertize "[waiting...]"
1069 'face 'mode-line-emphasis
1070 'help-echo
1071 "A VC command is in progress in this buffer"))))
1073 (defun vc-exec-after (code)
1074 "Eval CODE when the current buffer's process is done.
1075 If the current buffer has no process, just evaluate CODE.
1076 Else, add CODE to the process' sentinel."
1077 (let ((proc (get-buffer-process (current-buffer))))
1078 (cond
1079 ;; If there's no background process, just execute the code.
1080 ;; We used to explicitly call delete-process on exited processes,
1081 ;; but this led to timing problems causing process output to be
1082 ;; lost. Terminated processes get deleted automatically
1083 ;; anyway. -- cyd
1084 ((or (null proc) (eq (process-status proc) 'exit))
1085 ;; Make sure we've read the process's output before going further.
1086 (if proc (accept-process-output proc))
1087 (eval code))
1088 ;; If a process is running, add CODE to the sentinel
1089 ((eq (process-status proc) 'run)
1090 (vc-set-mode-line-busy-indicator)
1091 (let ((previous (process-sentinel proc)))
1092 (unless (eq previous 'vc-process-sentinel)
1093 (process-put proc 'vc-previous-sentinel previous))
1094 (set-process-sentinel proc 'vc-process-sentinel))
1095 (process-put proc 'vc-sentinel-commands
1096 ;; We keep the code fragments in the order given
1097 ;; so that vc-diff-finish's message shows up in
1098 ;; the presence of non-nil vc-command-messages.
1099 (append (process-get proc 'vc-sentinel-commands)
1100 (list code))))
1101 (t (error "Unexpected process state"))))
1102 nil)
1104 (defvar vc-post-command-functions nil
1105 "Hook run at the end of `vc-do-command'.
1106 Each function is called inside the buffer in which the command was run
1107 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
1109 (defvar w32-quote-process-args)
1111 (defun vc-delistify (filelist)
1112 "Smash a FILELIST into a file list string suitable for info messages."
1113 ;; FIXME what about file names with spaces?
1114 (if (not filelist) "." (mapconcat 'identity filelist " ")))
1116 ;;;###autoload
1117 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
1118 "Execute a VC command, notifying user and checking for errors.
1119 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
1120 current buffer if BUFFER is t. If the destination buffer is not
1121 already current, set it up properly and erase it. The command is
1122 considered successful if its exit status does not exceed OKSTATUS (if
1123 OKSTATUS is nil, that means to ignore error status, if it is `async', that
1124 means not to wait for termination of the subprocess; if it is t it means to
1125 ignore all execution errors). FILE-OR-LIST is the name of a working file;
1126 it may be a list of files or be nil (to execute commands that don't expect
1127 a file name or set of files). If an optional list of FLAGS is present,
1128 that is inserted into the command line before the filename."
1129 ;; FIXME: file-relative-name can return a bogus result because
1130 ;; it doesn't look at the actual file-system to see if symlinks
1131 ;; come into play.
1132 (let* ((files
1133 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
1134 (if (listp file-or-list) file-or-list (list file-or-list))))
1135 (full-command
1136 ;; What we're doing here is preparing a version of the command
1137 ;; for display in a debug-progess message. If it's fewer than
1138 ;; 20 characters display the entire command (without trailing
1139 ;; newline). Otherwise display the first 20 followed by an ellipsis.
1140 (concat (if (string= (substring command -1) "\n")
1141 (substring command 0 -1)
1142 command)
1144 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
1145 " " (vc-delistify files))))
1146 (save-current-buffer
1147 (unless (or (eq buffer t)
1148 (and (stringp buffer)
1149 (string= (buffer-name) buffer))
1150 (eq buffer (current-buffer)))
1151 (vc-setup-buffer buffer))
1152 ;; If there's some previous async process still running, just kill it.
1153 (let ((oldproc (get-buffer-process (current-buffer))))
1154 ;; If we wanted to wait for oldproc to finish before doing
1155 ;; something, we'd have used vc-eval-after.
1156 ;; Use `delete-process' rather than `kill-process' because we don't
1157 ;; want any of its output to appear from now on.
1158 (if oldproc (delete-process oldproc)))
1159 (let ((squeezed (remq nil flags))
1160 (inhibit-read-only t)
1161 (status 0))
1162 (when files
1163 (setq squeezed (nconc squeezed files)))
1164 (let ((exec-path (append vc-path exec-path))
1165 ;; Add vc-path to PATH for the execution of this command.
1166 (process-environment
1167 (cons (concat "PATH=" (getenv "PATH")
1168 path-separator
1169 (mapconcat 'identity vc-path path-separator))
1170 process-environment))
1171 (w32-quote-process-args t))
1172 (if (and (eq okstatus 'async) (file-remote-p default-directory))
1173 ;; start-process does not support remote execution
1174 (setq okstatus nil))
1175 (if (eq okstatus 'async)
1176 ;; Run asynchronously.
1177 (let ((proc
1178 (let ((process-connection-type nil))
1179 (apply 'start-file-process command (current-buffer)
1180 command squeezed))))
1181 (if vc-command-messages
1182 (message "Running %s in background..." full-command))
1183 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
1184 (set-process-filter proc 'vc-process-filter)
1185 (vc-exec-after
1186 `(if vc-command-messages
1187 (message "Running %s in background... done" ',full-command))))
1188 ;; Run synchrously
1189 (if vc-command-messages
1190 (message "Running %s in foreground..." full-command))
1191 (let ((buffer-undo-list t))
1192 (setq status (apply 'process-file command nil t nil squeezed)))
1193 (when (and (not (eq t okstatus))
1194 (or (not (integerp status))
1195 (and okstatus (< okstatus status))))
1196 (pop-to-buffer (current-buffer))
1197 (goto-char (point-min))
1198 (shrink-window-if-larger-than-buffer)
1199 (error "Running %s...FAILED (%s)" full-command
1200 (if (integerp status) (format "status %d" status) status))))
1201 ;; We're done. But don't emit a status message if running
1202 ;; asychronously, it would just mislead.
1203 (if (and vc-command-messages (not (eq okstatus 'async)))
1204 (message "Running %s...OK = %d" full-command status)))
1205 (vc-exec-after
1206 `(run-hook-with-args 'vc-post-command-functions
1207 ',command ',file-or-list ',flags))
1208 status))))
1210 (defun vc-position-context (posn)
1211 "Save a bit of the text around POSN in the current buffer.
1212 Used to help us find the corresponding position again later
1213 if markers are destroyed or corrupted."
1214 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
1215 ;; rcs.el mode.
1216 (list posn
1217 (buffer-size)
1218 (buffer-substring posn
1219 (min (point-max) (+ posn 100)))))
1221 (defun vc-find-position-by-context (context)
1222 "Return the position of CONTEXT in the current buffer.
1223 If CONTEXT cannot be found, return nil."
1224 (let ((context-string (nth 2 context)))
1225 (if (equal "" context-string)
1226 (point-max)
1227 (save-excursion
1228 (let ((diff (- (nth 1 context) (buffer-size))))
1229 (if (< diff 0) (setq diff (- diff)))
1230 (goto-char (nth 0 context))
1231 (if (or (search-forward context-string nil t)
1232 ;; Can't use search-backward since the match may continue
1233 ;; after point.
1234 (progn (goto-char (- (point) diff (length context-string)))
1235 ;; goto-char doesn't signal an error at
1236 ;; beginning of buffer like backward-char would
1237 (search-forward context-string nil t)))
1238 ;; to beginning of OSTRING
1239 (- (point) (length context-string))))))))
1241 (defun vc-context-matches-p (posn context)
1242 "Return t if POSN matches CONTEXT, nil otherwise."
1243 (let* ((context-string (nth 2 context))
1244 (len (length context-string))
1245 (end (+ posn len)))
1246 (if (> end (1+ (buffer-size)))
1248 (string= context-string (buffer-substring posn end)))))
1250 (defun vc-buffer-context ()
1251 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
1252 Used by `vc-restore-buffer-context' to later restore the context."
1253 (let ((point-context (vc-position-context (point)))
1254 ;; Use mark-marker to avoid confusion in transient-mark-mode.
1255 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
1256 (vc-position-context (mark-marker))))
1257 ;; Make the right thing happen in transient-mark-mode.
1258 (mark-active nil)
1259 ;; The new compilation code does not use compilation-error-list any
1260 ;; more, so the code below is now ineffective and might as well
1261 ;; be disabled. -- Stef
1262 ;; ;; We may want to reparse the compilation buffer after revert
1263 ;; (reparse (and (boundp 'compilation-error-list) ;compile loaded
1264 ;; ;; Construct a list; each elt is nil or a buffer
1265 ;; ;; if that buffer is a compilation output buffer
1266 ;; ;; that contains markers into the current buffer.
1267 ;; (save-current-buffer
1268 ;; (mapcar (lambda (buffer)
1269 ;; (set-buffer buffer)
1270 ;; (let ((errors (or
1271 ;; compilation-old-error-list
1272 ;; compilation-error-list))
1273 ;; (buffer-error-marked-p nil))
1274 ;; (while (and (consp errors)
1275 ;; (not buffer-error-marked-p))
1276 ;; (and (markerp (cdr (car errors)))
1277 ;; (eq buffer
1278 ;; (marker-buffer
1279 ;; (cdr (car errors))))
1280 ;; (setq buffer-error-marked-p t))
1281 ;; (setq errors (cdr errors)))
1282 ;; (if buffer-error-marked-p buffer)))
1283 ;; (buffer-list)))))
1284 (reparse nil))
1285 (list point-context mark-context reparse)))
1287 (defun vc-restore-buffer-context (context)
1288 "Restore point/mark, and reparse any affected compilation buffers.
1289 CONTEXT is that which `vc-buffer-context' returns."
1290 (let ((point-context (nth 0 context))
1291 (mark-context (nth 1 context))
1292 ;; (reparse (nth 2 context))
1294 ;; The new compilation code does not use compilation-error-list any
1295 ;; more, so the code below is now ineffective and might as well
1296 ;; be disabled. -- Stef
1297 ;; ;; Reparse affected compilation buffers.
1298 ;; (while reparse
1299 ;; (if (car reparse)
1300 ;; (with-current-buffer (car reparse)
1301 ;; (let ((compilation-last-buffer (current-buffer)) ;select buffer
1302 ;; ;; Record the position in the compilation buffer of
1303 ;; ;; the last error next-error went to.
1304 ;; (error-pos (marker-position
1305 ;; (car (car-safe compilation-error-list)))))
1306 ;; ;; Reparse the error messages as far as they were parsed before.
1307 ;; (compile-reinitialize-errors '(4) compilation-parsing-end)
1308 ;; ;; Move the pointer up to find the error we were at before
1309 ;; ;; reparsing. Now next-error should properly go to the next one.
1310 ;; (while (and compilation-error-list
1311 ;; (/= error-pos (car (car compilation-error-list))))
1312 ;; (setq compilation-error-list (cdr compilation-error-list))))))
1313 ;; (setq reparse (cdr reparse)))
1315 ;; if necessary, restore point and mark
1316 (if (not (vc-context-matches-p (point) point-context))
1317 (let ((new-point (vc-find-position-by-context point-context)))
1318 (if new-point (goto-char new-point))))
1319 (and mark-active
1320 mark-context
1321 (not (vc-context-matches-p (mark) mark-context))
1322 (let ((new-mark (vc-find-position-by-context mark-context)))
1323 (if new-mark (set-mark new-mark))))))
1325 ;;; Code for deducing what fileset and backend to assume
1327 (defun vc-responsible-backend (file &optional register)
1328 "Return the name of a backend system that is responsible for FILE.
1329 The optional argument REGISTER means that a backend suitable for
1330 registration should be found.
1332 If REGISTER is nil, then if FILE is already registered, return the
1333 backend of FILE. If FILE is not registered, or a directory, then the
1334 first backend in `vc-handled-backends' that declares itself
1335 responsible for FILE is returned. If no backend declares itself
1336 responsible, return the first backend.
1338 If REGISTER is non-nil, return the first responsible backend under
1339 which FILE is not yet registered. If there is no such backend, return
1340 the first backend under which FILE is not yet registered, but could
1341 be registered."
1342 (if (not vc-handled-backends)
1343 (error "No handled backends"))
1344 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1345 (catch 'found
1346 ;; First try: find a responsible backend. If this is for registration,
1347 ;; it must be a backend under which FILE is not yet registered.
1348 (dolist (backend vc-handled-backends)
1349 (and (or (not register)
1350 (not (vc-call-backend backend 'registered file)))
1351 (vc-call-backend backend 'responsible-p file)
1352 (throw 'found backend)))
1353 ;; no responsible backend
1354 (if (not register)
1355 ;; if this is not for registration, the first backend must do
1356 (car vc-handled-backends)
1357 ;; for registration, we need to find a new backend that
1358 ;; could register FILE
1359 (dolist (backend vc-handled-backends)
1360 (and (not (vc-call-backend backend 'registered file))
1361 (vc-call-backend backend 'could-register file)
1362 (throw 'found backend)))
1363 (error "No backend that could register")))))
1365 (defun vc-expand-dirs (file-or-dir-list)
1366 "Expands directories in a file list specification.
1367 Only files already under version control are noticed."
1368 ;; FIXME: Kill this function.
1369 (let ((flattened '()))
1370 (dolist (node file-or-dir-list)
1371 (vc-file-tree-walk
1372 node (lambda (f) (if (vc-backend f) (push f flattened)))))
1373 (nreverse flattened)))
1375 (defun vc-deduce-fileset (&optional allow-directory-wildcard allow-unregistered)
1376 "Deduce a set of files and a backend to which to apply an operation.
1378 If we're in VC-dired mode, the fileset is the list of marked files.
1379 Otherwise, if we're looking at a buffer visiting a version-controlled file,
1380 the fileset is a singleton containing this file.
1381 If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on
1382 and we're in a dired buffer, select the current directory.
1383 If none of these conditions is met, but ALLOW_UNREGISTERED is in and the
1384 visited file is not registered, return a singletin fileset containing it.
1385 Otherwise, throw an error."
1386 (cond (vc-dired-mode
1387 (let ((marked (dired-map-over-marks (dired-get-filename) nil)))
1388 (unless marked
1389 (error "No files have been selected."))
1390 ;; All members of the fileset must have the same backend
1391 (let ((firstbackend (vc-backend (car marked))))
1392 (dolist (f (cdr marked))
1393 (unless (eq (vc-backend f) firstbackend)
1394 (error "All members of a fileset must be under the same version-control system."))))
1395 marked))
1396 ((eq major-mode 'vc-status-mode)
1397 (let ((marked (vc-status-marked-files)))
1398 (if marked
1399 marked
1400 (list (vc-status-current-file)))))
1401 ((vc-backend buffer-file-name)
1402 (list buffer-file-name))
1403 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1404 (with-current-buffer vc-parent-buffer
1405 (or vc-dired-mode (eq major-mode 'vc-status-mode)))))
1406 (progn
1407 (set-buffer vc-parent-buffer)
1408 (vc-deduce-fileset)))
1409 ;; This is guarded by an enabling arg so users won't potentially
1410 ;; shoot themselves in the foot by modifying a fileset they can't
1411 ;; verify by eyeball. Allow it for nondestructive commands like
1412 ;; making diffs, or possibly for destructive ones that have
1413 ;; confirmation prompts.
1414 ((and allow-directory-wildcard
1415 ;; I think this is a misfeature. For now, I'll leave it in, but
1416 ;; I'll disable it anywhere else than in dired buffers. --Stef
1417 (and (derived-mode-p 'dired-mode)
1418 (equal buffer-file-name nil)
1419 (equal list-buffers-directory default-directory)))
1420 (progn
1421 (message "All version-controlled files below %s selected."
1422 default-directory)
1423 (list default-directory)))
1424 ((and allow-unregistered (not (vc-registered buffer-file-name)))
1425 (list buffer-file-name))
1426 (t (error "No fileset is available here."))))
1428 (defun vc-ensure-vc-buffer ()
1429 "Make sure that the current buffer visits a version-controlled file."
1430 (cond
1431 (vc-dired-mode
1432 (set-buffer (find-file-noselect (dired-get-filename))))
1433 ((eq major-mode 'vc-status-mode)
1434 (set-buffer (find-file-noselect (vc-status-current-file))))
1436 (while (and vc-parent-buffer
1437 (buffer-live-p vc-parent-buffer)
1438 ;; Avoid infinite looping when vc-parent-buffer and
1439 ;; current buffer are the same buffer.
1440 (not (eq vc-parent-buffer (current-buffer))))
1441 (set-buffer vc-parent-buffer))
1442 (if (not buffer-file-name)
1443 (error "Buffer %s is not associated with a file" (buffer-name))
1444 (if (not (vc-backend buffer-file-name))
1445 (error "File %s is not under version control" buffer-file-name))))))
1447 ;;; Support for the C-x v v command. This is where all the single-file-oriented
1448 ;;; code from before the fileset rewrite lives.
1450 (defsubst vc-editable-p (file)
1451 "Return non-nil if FILE can be edited."
1452 (or (eq (vc-checkout-model file) 'implicit)
1453 (memq (vc-state file) '(edited needs-merge))))
1455 (defun vc-revert-buffer-internal (&optional arg no-confirm)
1456 "Revert buffer, keeping point and mark where user expects them.
1457 Try to be clever in the face of changes due to expanded version-control
1458 key words. This is important for typeahead to work as expected.
1459 ARG and NO-CONFIRM are passed on to `revert-buffer'."
1460 (interactive "P")
1461 (widen)
1462 (let ((context (vc-buffer-context)))
1463 ;; Use save-excursion here, because it may be able to restore point
1464 ;; and mark properly even in cases where vc-restore-buffer-context
1465 ;; would fail. However, save-excursion might also get it wrong --
1466 ;; in this case, vc-restore-buffer-context gives it a second try.
1467 (save-excursion
1468 ;; t means don't call normal-mode;
1469 ;; that's to preserve various minor modes.
1470 (revert-buffer arg no-confirm t))
1471 (vc-restore-buffer-context context)))
1473 (defun vc-buffer-sync (&optional not-urgent)
1474 "Make sure the current buffer and its working file are in sync.
1475 NOT-URGENT means it is ok to continue if the user says not to save."
1476 (if (buffer-modified-p)
1477 (if (or vc-suppress-confirm
1478 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1479 (save-buffer)
1480 (unless not-urgent
1481 (error "Aborted")))))
1483 (defvar vc-dired-window-configuration)
1485 (defun vc-compatible-state (p q)
1486 "Controls which states can be in the same commit."
1488 (eq p q)
1489 (and (member p '(edited added removed)) (member q '(edited added removed)))))
1491 ;; Here's the major entry point.
1493 ;;;###autoload
1494 (defun vc-next-action (verbose)
1495 "Do the next logical version control operation on the current fileset.
1496 This requires that all files in the fileset be in the same state.
1498 For locking systems:
1499 If every file is not already registered, this registers each for version
1500 control.
1501 If every file is registered and not locked by anyone, this checks out
1502 a writable and locked file of each ready for editing.
1503 If every file is checked out and locked by the calling user, this
1504 first checks to see if each file has changed since checkout. If not,
1505 it performs a revert on that file.
1506 If every file has been changed, this pops up a buffer for entry
1507 of a log message; when the message has been entered, it checks in the
1508 resulting changes along with the log message as change commentary. If
1509 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1510 read-only copy of each changed file is left in place afterwards.
1511 If the affected file is registered and locked by someone else, you are
1512 given the option to steal the lock(s).
1514 For merging systems:
1515 If every file is not already registered, this registers each one for version
1516 control. This does an add, but not a commit.
1517 If every file is added but not committed, each one is committed.
1518 If every working file is changed, but the corresponding repository file is
1519 unchanged, this pops up a buffer for entry of a log message; when the
1520 message has been entered, it checks in the resulting changes along
1521 with the logmessage as change commentary. A writable file is retained.
1522 If the repository file is changed, you are asked if you want to
1523 merge in the changes into your working copy."
1524 (interactive "P")
1525 (let* ((files (vc-deduce-fileset nil t))
1526 (state (vc-state (car files)))
1527 (model (vc-checkout-model (car files)))
1528 revision)
1529 ;; Verify that the fileset is homogenous
1530 (dolist (file (cdr files))
1531 (unless (vc-compatible-state (vc-state file) state)
1532 (error "Fileset is in a mixed-up state"))
1533 (unless (eq (vc-checkout-model file) model)
1534 (error "Fileset has mixed checkout models")))
1535 ;; Check for buffers in the fileset not matching the on-disk contents.
1536 (dolist (file files)
1537 (let ((visited (get-file-buffer file)))
1538 (when visited
1539 (if (or vc-dired-mode (eq major-mode 'vc-status-mode))
1540 (switch-to-buffer-other-window visited)
1541 (set-buffer visited))
1542 ;; Check relation of buffer and file, and make sure
1543 ;; user knows what he's doing. First, finding the file
1544 ;; will check whether the file on disk is newer.
1545 ;; Ignore buffer-read-only during this test, and
1546 ;; preserve find-file-literally.
1547 (let ((buffer-read-only (not (file-writable-p file))))
1548 (find-file-noselect file nil find-file-literally))
1549 (if (not (verify-visited-file-modtime (current-buffer)))
1550 (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file))
1551 (write-file buffer-file-name)
1552 (error "Aborted"))
1553 ;; Now, check if we have unsaved changes.
1554 (vc-buffer-sync t)
1555 (when (buffer-modified-p)
1556 (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file))
1557 (error "Aborted")))))))
1558 ;; Do the right thing
1559 (cond
1560 ;; Files aren't registered
1561 ((or (not state) ;; RCS uses nil for unregistered files.
1562 (eq state 'unregistered)
1563 (eq state 'ignored))
1564 (mapc 'vc-register files))
1565 ;; Files are up-to-date, or need a merge and user specified a revision
1566 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-patch)))
1567 (cond
1568 (verbose
1569 ;; go to a different revision
1570 (setq revision (read-string "Branch, revision, or backend to move to: "))
1571 (let ((vsym (intern-soft (upcase revision))))
1572 (if (member vsym vc-handled-backends)
1573 (dolist (file files) (vc-transfer-file file vsym))
1574 (dolist (file files)
1575 (vc-checkout file (eq model 'implicit) revision)))))
1576 ((not (eq model 'implicit))
1577 ;; check the files out
1578 (dolist (file files) (vc-checkout file t)))
1580 ;; do nothing
1581 (message "Fileset is up-to-date"))))
1582 ;; Files have local changes
1583 ((vc-compatible-state state 'edited)
1584 (let ((ready-for-commit files))
1585 ;; If files are edited but read-only, give user a chance to correct
1586 (dolist (file files)
1587 (unless (file-writable-p file)
1588 ;; Make the file+buffer read-write.
1589 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue?" file))
1590 (error "Aborted"))
1591 (set-file-modes file (logior (file-modes file) 128))
1592 (let ((visited (get-file-buffer file)))
1593 (when visited
1594 (with-current-buffer visited
1595 (toggle-read-only -1))))))
1596 ;; Allow user to revert files with no changes
1597 (save-excursion
1598 (dolist (file files)
1599 (let ((visited (get-file-buffer file)))
1600 ;; For files with locking, if the file does not contain
1601 ;; any changes, just let go of the lock, i.e. revert.
1602 (when (and (not (eq model 'implicit))
1603 (vc-workfile-unchanged-p file)
1604 ;; If buffer is modified, that means the user just
1605 ;; said no to saving it; in that case, don't revert,
1606 ;; because the user might intend to save after
1607 ;; finishing the log entry and committing.
1608 (not (and visited (buffer-modified-p))))
1609 (vc-revert-file file)
1610 (delete file ready-for-commit)))))
1611 ;; Remaining files need to be committed
1612 (if (not ready-for-commit)
1613 (message "No files remain to be committed")
1614 (if (not verbose)
1615 (vc-checkin ready-for-commit)
1616 (progn
1617 (setq revision (read-string "New revision or backend: "))
1618 (let ((vsym (intern (upcase revision))))
1619 (if (member vsym vc-handled-backends)
1620 (dolist (file files) (vc-transfer-file file vsym))
1621 (vc-checkin ready-for-commit revision))))))))
1622 ;; locked by somebody else (locking VCSes only)
1623 ((stringp state)
1624 ;; In the old days, we computed the revision once and used it on
1625 ;; the single file. Then, for the 2007-2008 fileset rewrite, we
1626 ;; computed the revision once (incorrectly, using a free var) and
1627 ;; used it on all files. To fix the free var bug, we can either
1628 ;; use `(car files)' or do what we do here: distribute the
1629 ;; revision computation among `files'. Although this may be
1630 ;; tedious for those backends where a "revision" is a trans-file
1631 ;; concept, it is nonetheless correct for both those and (more
1632 ;; importantly) for those where "revision" is a per-file concept.
1633 ;; If the intersection of the former group and "locking VCSes" is
1634 ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
1635 ;; pre-computation approach of yore.
1636 (dolist (file files)
1637 (vc-steal-lock
1638 file (if verbose
1639 (read-string (format "%s revision to steal: " file))
1640 (vc-working-revision file))
1641 state)))
1642 ;; needs-patch
1643 ((eq state 'needs-patch)
1644 (dolist (file files)
1645 (if (yes-or-no-p (format
1646 "%s is not up-to-date. Get latest revision? "
1647 (file-name-nondirectory file)))
1648 (vc-checkout file (eq model 'implicit) t)
1649 (when (and (not (eq model 'implicit))
1650 (yes-or-no-p "Lock this revision? "))
1651 (vc-checkout file t)))))
1652 ;; needs-merge
1653 ((eq state 'needs-merge)
1654 (dolist (file files)
1655 (when (yes-or-no-p (format
1656 "%s is not up-to-date. Merge in changes now? "
1657 (file-name-nondirectory file)))
1658 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))
1660 ;; unlocked-changes
1661 ((eq state 'unlocked-changes)
1662 (dolist (file files)
1663 (if (not (equal buffer-file-name file))
1664 (find-file-other-window file))
1665 (if (save-window-excursion
1666 (vc-diff-internal nil (list file) (vc-working-revision file) nil)
1667 (goto-char (point-min))
1668 (let ((inhibit-read-only t))
1669 (insert
1670 (format "Changes to %s since last lock:\n\n" file)))
1671 (not (beep))
1672 (yes-or-no-p (concat "File has unlocked changes. "
1673 "Claim lock retaining changes? ")))
1674 (progn (vc-call steal-lock file)
1675 (clear-visited-file-modtime)
1676 ;; Must clear any headers here because they wouldn't
1677 ;; show that the file is locked now.
1678 (vc-clear-headers file)
1679 (write-file buffer-file-name)
1680 (vc-mode-line file))
1681 (if (not (yes-or-no-p
1682 "Revert to checked-in revision, instead? "))
1683 (error "Checkout aborted")
1684 (vc-revert-buffer-internal t t)
1685 (vc-checkout file t))))))))
1687 (defun vc-create-repo (backend)
1688 "Create an empty repository in the current directory."
1689 (interactive
1690 (list
1691 (intern
1692 (upcase
1693 (completing-read
1694 "Create repository for: "
1695 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1696 nil t)))))
1697 (vc-call-backend backend 'create-repo))
1699 ;;;###autoload
1700 (defun vc-register (&optional fname set-revision comment)
1701 "Register into a version control system.
1702 If FNAME is given register that file, otherwise register the current file.
1703 With prefix argument SET-REVISION, allow user to specify initial revision
1704 level. If COMMENT is present, use that as an initial comment.
1706 The version control system to use is found by cycling through the list
1707 `vc-handled-backends'. The first backend in that list which declares
1708 itself responsible for the file (usually because other files in that
1709 directory are already registered under that backend) will be used to
1710 register the file. If no backend declares itself responsible, the
1711 first backend that could register the file is used."
1712 (interactive "P")
1713 (when (and (null fname) (null buffer-file-name)) (error "No visited file"))
1715 (let ((bname (if fname (get-file-buffer fname) (current-buffer))))
1716 (unless fname (setq fname buffer-file-name))
1717 (when (vc-backend fname)
1718 (if (vc-registered fname)
1719 (error "This file is already registered")
1720 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1721 (error "Aborted"))))
1722 ;; Watch out for new buffers of size 0: the corresponding file
1723 ;; does not exist yet, even though buffer-modified-p is nil.
1724 (when bname
1725 (with-current-buffer bname
1726 (if (and (not (buffer-modified-p))
1727 (zerop (buffer-size))
1728 (not (file-exists-p buffer-file-name)))
1729 (set-buffer-modified-p t))
1730 (vc-buffer-sync)))
1731 (vc-start-entry (list fname)
1732 (if set-revision
1733 (read-string (format "Initial revision level for %s: "
1734 fname))
1735 (vc-call-backend (vc-responsible-backend fname)
1736 'init-revision))
1737 (or comment (not vc-initial-comment))
1739 "Enter initial comment."
1740 (lambda (files rev comment)
1741 (dolist (file files)
1742 (message "Registering %s... " file)
1743 (let ((backend (vc-responsible-backend file t)))
1744 (vc-file-clearprops file)
1745 (vc-call-backend backend 'register (list file) rev comment)
1746 (vc-file-setprop file 'vc-backend backend)
1747 (unless vc-make-backup-files
1748 (make-local-variable 'backup-inhibited)
1749 (setq backup-inhibited t)))
1750 (message "Registering %s... done" file))))))
1752 (defun vc-register-with (backend)
1753 "Register the current file with a specified back end."
1754 (interactive "SBackend: ")
1755 (if (not (member backend vc-handled-backends))
1756 (error "Unknown back end."))
1757 (let ((vc-handled-backends (list backend)))
1758 (call-interactively 'vc-register)))
1760 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
1762 (defun vc-resynch-window (file &optional keep noquery)
1763 "If FILE is in the current buffer, either revert or unvisit it.
1764 The choice between revert (to see expanded keywords) and unvisit depends on
1765 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1766 reverting. NOQUERY should be t *only* if it is known the only
1767 difference between the buffer and the file is due to version control
1768 rather than user editing!"
1769 (and (string= buffer-file-name file)
1770 (if keep
1771 (progn
1772 (vc-revert-buffer-internal t noquery)
1773 ;; TODO: Adjusting view mode might no longer be necessary
1774 ;; after RMS change to files.el of 1999-08-08. Investigate
1775 ;; this when we install the new VC.
1776 (and view-read-only
1777 (if (file-writable-p file)
1778 (and view-mode
1779 (let ((view-old-buffer-read-only nil))
1780 (view-mode-exit)))
1781 (and (not view-mode)
1782 (not (eq (get major-mode 'mode-class) 'special))
1783 (view-mode-enter))))
1784 (vc-mode-line buffer-file-name))
1785 (kill-buffer (current-buffer)))))
1787 (defun vc-resynch-buffer (file &optional keep noquery)
1788 "If FILE is currently visited, resynch its buffer."
1789 (if (string= buffer-file-name file)
1790 (vc-resynch-window file keep noquery)
1791 (let ((buffer (get-file-buffer file)))
1792 (if buffer
1793 (with-current-buffer buffer
1794 (vc-resynch-window file keep noquery)))))
1795 (vc-dired-resynch-file file))
1797 (defun vc-start-entry (files rev comment initial-contents msg action &optional after-hook)
1798 "Accept a comment for an operation on FILES revision REV.
1799 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1800 action on close to ACTION. If COMMENT is a string and
1801 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1802 contents of the log entry buffer. If COMMENT is a string and
1803 INITIAL-CONTENTS is nil, do action immediately as if the user had
1804 entered COMMENT. If COMMENT is t, also do action immediately with an
1805 empty comment. Remember the file's buffer in `vc-parent-buffer'
1806 \(current one if no file). AFTER-HOOK specifies the local value
1807 for `vc-log-after-operation-hook'."
1808 (let ((parent
1809 (if (or (eq major-mode 'vc-dired-mode) (eq major-mode 'vc-status-mode))
1810 ;; If we are called from VC dired, the parent buffer is
1811 ;; the current buffer.
1812 (current-buffer)
1813 (if (and files (equal (length files) 1))
1814 (get-file-buffer (car files))
1815 (current-buffer)))))
1816 (when vc-before-checkin-hook
1817 (if files
1818 (with-current-buffer parent
1819 (run-hooks 'vc-before-checkin-hook))
1820 (run-hooks 'vc-before-checkin-hook)))
1821 (if (and comment (not initial-contents))
1822 (set-buffer (get-buffer-create "*VC-log*"))
1823 (pop-to-buffer (get-buffer-create "*VC-log*")))
1824 (set (make-local-variable 'vc-parent-buffer) parent)
1825 (set (make-local-variable 'vc-parent-buffer-name)
1826 (concat " from " (buffer-name vc-parent-buffer)))
1827 ;;(if file (vc-mode-line file))
1828 (vc-log-edit files)
1829 (make-local-variable 'vc-log-after-operation-hook)
1830 (when after-hook
1831 (setq vc-log-after-operation-hook after-hook))
1832 (setq vc-log-operation action)
1833 (setq vc-log-revision rev)
1834 (when comment
1835 (erase-buffer)
1836 (when (stringp comment) (insert comment)))
1837 (if (or (not comment) initial-contents)
1838 (message "%s Type C-c C-c when done" msg)
1839 (vc-finish-logentry (eq comment t)))))
1841 (defun vc-checkout (file &optional writable rev)
1842 "Retrieve a copy of the revision REV of FILE.
1843 If WRITABLE is non-nil, make sure the retrieved file is writable.
1844 REV defaults to the latest revision.
1846 After check-out, runs the normal hook `vc-checkout-hook'."
1847 (and writable
1848 (not rev)
1849 (vc-call make-version-backups-p file)
1850 (vc-up-to-date-p file)
1851 (vc-make-version-backup file))
1852 (with-vc-properties
1853 (list file)
1854 (condition-case err
1855 (vc-call checkout file writable rev)
1856 (file-error
1857 ;; Maybe the backend is not installed ;-(
1858 (when writable
1859 (let ((buf (get-file-buffer file)))
1860 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1861 (signal (car err) (cdr err))))
1862 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1863 (not writable))
1864 (if (vc-call latest-on-branch-p file)
1865 'up-to-date
1866 'needs-patch)
1867 'edited))
1868 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1869 (vc-resynch-buffer file t t)
1870 (run-hooks 'vc-checkout-hook))
1872 (defun vc-steal-lock (file rev owner)
1873 "Steal the lock on FILE."
1874 (let (file-description)
1875 (if rev
1876 (setq file-description (format "%s:%s" file rev))
1877 (setq file-description file))
1878 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1879 file-description owner)))
1880 (error "Steal canceled"))
1881 (message "Stealing lock on %s..." file)
1882 (with-vc-properties
1883 (list file)
1884 (vc-call steal-lock file rev)
1885 `((vc-state . edited)))
1886 (vc-resynch-buffer file t t)
1887 (message "Stealing lock on %s...done" file)
1888 ;; Write mail after actually stealing, because if the stealing
1889 ;; goes wrong, we don't want to send any mail.
1890 (compose-mail owner (format "Stolen lock on %s" file-description))
1891 (setq default-directory (expand-file-name "~/"))
1892 (goto-char (point-max))
1893 (insert
1894 (format "I stole the lock on %s, " file-description)
1895 (current-time-string)
1896 ".\n")
1897 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1899 (defun vc-checkin (files &optional rev comment initial-contents)
1900 "Check in FILES.
1901 The optional argument REV may be a string specifying the new revision
1902 level (if nil increment the current level). COMMENT is a comment
1903 string; if omitted, a buffer is popped up to accept a comment. If
1904 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1905 of the log entry buffer.
1907 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1908 that the version control system supports this mode of operation.
1910 Runs the normal hook `vc-checkin-hook'."
1911 (vc-start-entry
1912 files rev comment initial-contents
1913 "Enter a change comment."
1914 (lambda (files rev comment)
1915 (message "Checking in %s..." (vc-delistify files))
1916 ;; "This log message intentionally left almost blank".
1917 ;; RCS 5.7 gripes about white-space-only comments too.
1918 (or (and comment (string-match "[^\t\n ]" comment))
1919 (setq comment "*** empty log message ***"))
1920 (with-vc-properties
1921 files
1922 ;; We used to change buffers to get local value of vc-checkin-switches,
1923 ;; but 'the' local buffer is not a well-defined concept for filesets.
1924 (progn
1925 (vc-call checkin files rev comment)
1926 (mapc 'vc-delete-automatic-version-backups files))
1927 `((vc-state . up-to-date)
1928 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1929 (vc-working-revision . nil)))
1930 (message "Checking in %s...done" (vc-delistify files)))
1931 'vc-checkin-hook))
1933 (defun vc-finish-logentry (&optional nocomment)
1934 "Complete the operation implied by the current log entry.
1935 Use the contents of the current buffer as a check-in or registration
1936 comment. If the optional arg NOCOMMENT is non-nil, then don't check
1937 the buffer contents as a comment."
1938 (interactive)
1939 ;; Check and record the comment, if any.
1940 (unless nocomment
1941 ;; Comment too long?
1942 (vc-call-backend (or (if vc-log-fileset (vc-backend vc-log-fileset))
1943 (vc-responsible-backend default-directory))
1944 'logentry-check)
1945 (run-hooks 'vc-logentry-check-hook))
1946 ;; Sync parent buffer in case the user modified it while editing the comment.
1947 ;; But not if it is a vc-dired buffer.
1948 (with-current-buffer vc-parent-buffer
1949 (or vc-dired-mode (eq major-mode 'vc-status-mode) (vc-buffer-sync)))
1950 (if (not vc-log-operation)
1951 (error "No log operation is pending"))
1952 ;; save the parameters held in buffer-local variables
1953 (let ((log-operation vc-log-operation)
1954 (log-fileset vc-log-fileset)
1955 (log-revision vc-log-revision)
1956 (log-entry (buffer-string))
1957 (after-hook vc-log-after-operation-hook)
1958 (tmp-vc-parent-buffer vc-parent-buffer))
1959 (pop-to-buffer vc-parent-buffer)
1960 ;; OK, do it to it
1961 (save-excursion
1962 (funcall log-operation
1963 log-fileset
1964 log-revision
1965 log-entry))
1966 ;; Remove checkin window (after the checkin so that if that fails
1967 ;; we don't zap the *VC-log* buffer and the typing therein).
1968 (let ((logbuf (get-buffer "*VC-log*")))
1969 (cond ((and logbuf vc-delete-logbuf-window)
1970 (delete-windows-on logbuf (selected-frame))
1971 ;; Kill buffer and delete any other dedicated windows/frames.
1972 (kill-buffer logbuf))
1973 (logbuf (pop-to-buffer "*VC-log*")
1974 (bury-buffer)
1975 (pop-to-buffer tmp-vc-parent-buffer))))
1976 ;; Now make sure we see the expanded headers
1977 (if log-fileset
1978 (mapc
1979 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
1980 log-fileset))
1981 (if (or vc-dired-mode (eq major-mode 'vc-status-mode))
1982 (dired-move-to-filename))
1983 (run-hooks after-hook 'vc-finish-logentry-hook)))
1985 ;;; Additional entry points for examining version histories
1987 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
1988 ;; "List differences for all registered files at and below DIR.
1989 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
1990 ;; ;; This implementation does an explicit tree walk, and calls
1991 ;; ;; vc-BACKEND-diff directly for each file. An optimization
1992 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
1993 ;; ;; and to call it only for files that are actually changed.
1994 ;; ;; However, this is expensive for some backends, and so it is left
1995 ;; ;; to backend-specific implementations.
1996 ;; (setq default-directory dir)
1997 ;; (vc-file-tree-walk
1998 ;; default-directory
1999 ;; (lambda (f)
2000 ;; (vc-exec-after
2001 ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
2002 ;; (message "Looking at %s" ',f)
2003 ;; (vc-call-backend ',(vc-backend f)
2004 ;; 'diff (list ',f) ',rev1 ',rev2))))))
2006 (defun vc-coding-system-for-diff (file)
2007 "Return the coding system for reading diff output for FILE."
2008 (or coding-system-for-read
2009 ;; if we already have this file open,
2010 ;; use the buffer's coding system
2011 (let ((buf (find-buffer-visiting file)))
2012 (if buf (with-current-buffer buf
2013 buffer-file-coding-system)))
2014 ;; otherwise, try to find one based on the file name
2015 (car (find-operation-coding-system 'insert-file-contents file))
2016 ;; and a final fallback
2017 'undecided))
2019 (defun vc-switches (backend op)
2020 (let ((switches
2021 (or (if backend
2022 (let ((sym (vc-make-backend-sym
2023 backend (intern (concat (symbol-name op)
2024 "-switches")))))
2025 (if (boundp sym) (symbol-value sym))))
2026 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
2027 (if (boundp sym) (symbol-value sym)))
2028 (cond
2029 ((eq op 'diff) diff-switches)))))
2030 (if (stringp switches) (list switches)
2031 ;; If not a list, return nil.
2032 ;; This is so we can set vc-diff-switches to t to override
2033 ;; any switches in diff-switches.
2034 (if (listp switches) switches))))
2036 ;; Old def for compatibility with Emacs-21.[123].
2037 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
2038 (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
2040 (defun vc-diff-finish (buffer messages)
2041 ;; The empty sync output case has already been handled, so the only
2042 ;; possibility of an empty output is for an async process.
2043 (when (buffer-live-p buffer)
2044 (let ((window (get-buffer-window buffer t))
2045 (emptyp (zerop (buffer-size buffer))))
2046 (with-current-buffer buffer
2047 (and messages emptyp
2048 (let ((inhibit-read-only t))
2049 (insert (cdr messages) ".\n")
2050 (message "%s" (cdr messages))))
2051 (goto-char (point-min))
2052 (when window
2053 (shrink-window-if-larger-than-buffer window)))
2054 (when (and messages (not emptyp))
2055 (message "%sdone" (car messages))))))
2057 (defvar vc-diff-added-files nil
2058 "If non-nil, diff added files by comparing them to /dev/null.")
2060 (defun vc-diff-internal (async files rev1 rev2 &optional verbose)
2061 "Report diffs between two revisions of a fileset.
2062 Diff output goes to the *vc-diff* buffer. The function
2063 returns t if the buffer had changes, nil otherwise."
2064 (let* ((messages (cons (format "Finding changes in %s..."
2065 (vc-delistify files))
2066 (format "No changes between %s and %s"
2067 (or rev1 "working revision")
2068 (or rev2 "workfile"))))
2069 ;; Set coding system based on the first file. It's a kluge,
2070 ;; but the only way to set it for each file included would
2071 ;; be to call the back end separately for each file.
2072 (coding-system-for-read
2073 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
2074 (vc-setup-buffer "*vc-diff*")
2075 (message "%s" (car messages))
2076 ;; Many backends don't handle well the case of a file that has been
2077 ;; added but not yet committed to the repo (notably CVS and Subversion).
2078 ;; Do that work here so the backends don't have to futz with it. --ESR
2080 ;; Actually most backends (including CVS) have options to control the
2081 ;; behavior since which one is better depends on the user and on the
2082 ;; situation). Worse yet: this code does not handle the case where
2083 ;; `file' is a directory which contains added files.
2084 ;; I made it conditional on vc-diff-added-files but it should probably
2085 ;; just be removed (or copied/moved to specific backends). --Stef.
2086 (when vc-diff-added-files
2087 (let ((filtered '()))
2088 (dolist (file files)
2089 (if (or (file-directory-p file)
2090 (not (string= (vc-working-revision file) "0")))
2091 (push file filtered)
2092 ;; This file is added but not yet committed;
2093 ;; there is no master file to diff against.
2094 (if (or rev1 rev2)
2095 (error "No revisions of %s exist" file)
2096 ;; We regard this as "changed".
2097 ;; Diff it against /dev/null.
2098 (apply 'vc-do-command "*vc-diff*"
2099 1 "diff" file
2100 (append (vc-switches nil 'diff) '("/dev/null"))))))
2101 (setq files (nreverse filtered))))
2102 (let ((vc-disable-async-diff (not async)))
2103 (vc-call diff files rev1 rev2 "*vc-diff*"))
2104 (set-buffer "*vc-diff*")
2105 (if (and (zerop (buffer-size))
2106 (not (get-buffer-process (current-buffer))))
2107 ;; Treat this case specially so as not to pop the buffer.
2108 (progn
2109 (message "%s" (cdr messages))
2110 nil)
2111 (diff-mode)
2112 ;; Make the *vc-diff* buffer read only, the diff-mode key
2113 ;; bindings are nicer for read only buffers. pcl-cvs does the
2114 ;; same thing.
2115 (setq buffer-read-only t)
2116 (vc-exec-after `(vc-diff-finish ,(current-buffer) ',(when verbose
2117 messages)))
2118 ;; Display the buffer, but at the end because it can change point.
2119 (pop-to-buffer (current-buffer))
2120 ;; In the async case, we return t even if there are no differences
2121 ;; because we don't know that yet.
2122 t)))
2124 ;;;###autoload
2125 (defun vc-version-diff (files rev1 rev2)
2126 "Report diffs between revisions of the fileset in the repository history."
2127 (interactive
2128 (let* ((files (vc-deduce-fileset t))
2129 (first (car files))
2130 (completion-table
2131 (vc-call revision-completion-table files))
2132 (rev1-default nil)
2133 (rev2-default nil))
2134 (cond
2135 ;; someday we may be able to do revision completion on non-singleton
2136 ;; filesets, but not yet.
2137 ((/= (length files) 1)
2138 nil)
2139 ;; if it's a directory, don't supply any revision default
2140 ((file-directory-p first)
2141 nil)
2142 ;; if the file is not up-to-date, use working revision as older revision
2143 ((not (vc-up-to-date-p first))
2144 (setq rev1-default (vc-working-revision first)))
2145 ;; if the file is not locked, use last and previous revisions as defaults
2147 (setq rev1-default (vc-call previous-revision first
2148 (vc-working-revision first)))
2149 (if (string= rev1-default "") (setq rev1-default nil))
2150 (setq rev2-default (vc-working-revision first))))
2151 ;; construct argument list
2152 (let* ((rev1-prompt (if rev1-default
2153 (concat "Older revision (default "
2154 rev1-default "): ")
2155 "Older revision: "))
2156 (rev2-prompt (concat "Newer revision (default "
2157 (or rev2-default "current source") "): "))
2158 (rev1 (if completion-table
2159 (completing-read rev1-prompt completion-table
2160 nil nil nil nil rev1-default)
2161 (read-string rev1-prompt nil nil rev1-default)))
2162 (rev2 (if completion-table
2163 (completing-read rev2-prompt completion-table
2164 nil nil nil nil rev2-default)
2165 (read-string rev2-prompt nil nil rev2-default))))
2166 (if (string= rev1 "") (setq rev1 nil))
2167 (if (string= rev2 "") (setq rev2 nil))
2168 (list files rev1 rev2))))
2169 (if (and (not rev1) rev2)
2170 (error "Not a valid revision range."))
2171 (vc-diff-internal t files rev1 rev2 (interactive-p)))
2173 ;; (defun vc-contains-version-controlled-file (dir)
2174 ;; "Return t if DIR contains a version-controlled file, nil otherwise."
2175 ;; (catch 'found
2176 ;; (mapc (lambda (f) (and (not (file-directory-p f)) (vc-backend f) (throw 'found 't))) (directory-files dir))
2177 ;; nil))
2179 ;;;###autoload
2180 (defun vc-diff (historic &optional not-urgent)
2181 "Display diffs between file revisions.
2182 Normally this compares the currently selected fileset with their
2183 working revisions. With a prefix argument HISTORIC, it reads two revision
2184 designators specifying which revisions to compare.
2186 If no current fileset is available (that is, we are not in
2187 VC-Dired mode and the visited file of the current buffer is not
2188 under version control) and we're in a Dired buffer, use
2189 the current directory.
2190 The optional argument NOT-URGENT non-nil means it is ok to say no to
2191 saving the buffer."
2192 (interactive (list current-prefix-arg t))
2193 (if historic
2194 (call-interactively 'vc-version-diff)
2195 (let* ((files (vc-deduce-fileset t)))
2196 (if buffer-file-name (vc-buffer-sync not-urgent))
2197 (vc-diff-internal t files nil nil (interactive-p)))))
2200 ;;;###autoload
2201 (defun vc-revision-other-window (rev)
2202 "Visit revision REV of the current file in another window.
2203 If the current file is named `F', the revision is named `F.~REV~'.
2204 If `F.~REV~' already exists, use it instead of checking it out again."
2205 (interactive
2206 (save-current-buffer
2207 (vc-ensure-vc-buffer)
2208 (let ((completion-table
2209 (vc-call revision-completion-table buffer-file-name))
2210 (prompt "Revision to visit (default is working revision): "))
2211 (list
2212 (if completion-table
2213 (completing-read prompt completion-table)
2214 (read-string prompt))))))
2215 (vc-ensure-vc-buffer)
2216 (let* ((file buffer-file-name)
2217 (revision (if (string-equal rev "")
2218 (vc-working-revision file)
2219 rev)))
2220 (switch-to-buffer-other-window (vc-find-revision file revision))))
2222 (defun vc-find-revision (file revision)
2223 "Read REVISION of FILE into a buffer and return the buffer."
2224 (let ((automatic-backup (vc-version-backup-file-name file revision))
2225 (filebuf (or (get-file-buffer file) (current-buffer)))
2226 (filename (vc-version-backup-file-name file revision 'manual)))
2227 (unless (file-exists-p filename)
2228 (if (file-exists-p automatic-backup)
2229 (rename-file automatic-backup filename nil)
2230 (message "Checking out %s..." filename)
2231 (with-current-buffer filebuf
2232 (let ((failed t))
2233 (unwind-protect
2234 (let ((coding-system-for-read 'no-conversion)
2235 (coding-system-for-write 'no-conversion))
2236 (with-temp-file filename
2237 (let ((outbuf (current-buffer)))
2238 ;; Change buffer to get local value of
2239 ;; vc-checkout-switches.
2240 (with-current-buffer filebuf
2241 (vc-call find-revision file revision outbuf))))
2242 (setq failed nil))
2243 (when (and failed (file-exists-p filename))
2244 (delete-file filename))))
2245 (vc-mode-line file))
2246 (message "Checking out %s...done" filename)))
2247 (let ((result-buf (find-file-noselect filename)))
2248 (with-current-buffer result-buf
2249 ;; Set the parent buffer so that things like
2250 ;; C-x v g, C-x v l, ... etc work.
2251 (set (make-local-variable 'vc-parent-buffer) filebuf))
2252 result-buf)))
2254 ;; Header-insertion code
2256 ;;;###autoload
2257 (defun vc-insert-headers ()
2258 "Insert headers into a file for use with a version control system.
2259 Headers desired are inserted at point, and are pulled from
2260 the variable `vc-BACKEND-header'."
2261 (interactive)
2262 (vc-ensure-vc-buffer)
2263 (save-excursion
2264 (save-restriction
2265 (widen)
2266 (if (or (not (vc-check-headers))
2267 (y-or-n-p "Version headers already exist. Insert another set? "))
2268 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
2269 (comment-start-vc (or (car delims) comment-start "#"))
2270 (comment-end-vc (or (car (cdr delims)) comment-end ""))
2271 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
2272 'header))
2273 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
2274 (dolist (s hdstrings)
2275 (insert comment-start-vc "\t" s "\t"
2276 comment-end-vc "\n"))
2277 (if vc-static-header-alist
2278 (dolist (f vc-static-header-alist)
2279 (if (string-match (car f) buffer-file-name)
2280 (insert (format (cdr f) (car hdstrings)))))))))))
2282 (defun vc-clear-headers (&optional file)
2283 "Clear all version headers in the current buffer (or FILE).
2284 The headers are reset to their non-expanded form."
2285 (let* ((filename (or file buffer-file-name))
2286 (visited (find-buffer-visiting filename))
2287 (backend (vc-backend filename)))
2288 (when (vc-find-backend-function backend 'clear-headers)
2289 (if visited
2290 (let ((context (vc-buffer-context)))
2291 ;; save-excursion may be able to relocate point and mark
2292 ;; properly. If it fails, vc-restore-buffer-context
2293 ;; will give it a second try.
2294 (save-excursion
2295 (vc-call-backend backend 'clear-headers))
2296 (vc-restore-buffer-context context))
2297 (set-buffer (find-file-noselect filename))
2298 (vc-call-backend backend 'clear-headers)
2299 (kill-buffer filename)))))
2301 (defun vc-modify-change-comment (files rev oldcomment)
2302 "Edit the comment associated with the given files and revision."
2303 (vc-start-entry
2304 files rev oldcomment t
2305 "Enter a replacement change comment."
2306 (lambda (files rev comment)
2307 (vc-call-backend
2308 ;; Less of a kluge than it looks like; log-view mode only passes
2309 ;; this function a singleton list. Arguments left in this form in
2310 ;; case the more general operation ever becomes meaningful.
2311 (vc-responsible-backend (car files))
2312 'modify-change-comment files rev comment))))
2314 ;;;###autoload
2315 (defun vc-merge ()
2316 "Merge changes between two revisions into the current buffer's file.
2317 This asks for two revisions to merge from in the minibuffer. If the
2318 first revision is a branch number, then merge all changes from that
2319 branch. If the first revision is empty, merge news, i.e. recent changes
2320 from the current branch.
2322 See Info node `Merging'."
2323 (interactive)
2324 (vc-ensure-vc-buffer)
2325 (vc-buffer-sync)
2326 (let* ((file buffer-file-name)
2327 (backend (vc-backend file))
2328 (state (vc-state file))
2329 first-revision second-revision status)
2330 (cond
2331 ((stringp state) ;; Locking VCses only
2332 (error "File is locked by %s" state))
2333 ((not (vc-editable-p file))
2334 (if (y-or-n-p
2335 "File must be checked out for merging. Check out now? ")
2336 (vc-checkout file t)
2337 (error "Merge aborted"))))
2338 (setq first-revision
2339 (read-string (concat "Branch or revision to merge from "
2340 "(default news on current branch): ")))
2341 (if (string= first-revision "")
2342 (if (not (vc-find-backend-function backend 'merge-news))
2343 (error "Sorry, merging news is not implemented for %s" backend)
2344 (setq status (vc-call merge-news file)))
2345 (if (not (vc-find-backend-function backend 'merge))
2346 (error "Sorry, merging is not implemented for %s" backend)
2347 (if (not (vc-branch-p first-revision))
2348 (setq second-revision
2349 (read-string "Second revision: "
2350 (concat (vc-branch-part first-revision) ".")))
2351 ;; We want to merge an entire branch. Set revisions
2352 ;; accordingly, so that vc-BACKEND-merge understands us.
2353 (setq second-revision first-revision)
2354 ;; first-revision must be the starting point of the branch
2355 (setq first-revision (vc-branch-part first-revision)))
2356 (setq status (vc-call merge file first-revision second-revision))))
2357 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
2359 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
2360 (vc-resynch-buffer file t (not (buffer-modified-p)))
2361 (if (zerop status) (message "Merge successful")
2362 (smerge-mode 1)
2363 (message "File contains conflicts.")))
2365 ;;;###autoload
2366 (defalias 'vc-resolve-conflicts 'smerge-ediff)
2368 ;; The VC directory major mode. Coopt Dired for this.
2369 ;; All VC commands get mapped into logical equivalents.
2371 (defvar vc-dired-switches)
2372 (defvar vc-dired-terse-mode)
2374 (defvar vc-dired-mode-map
2375 (let ((map (make-sparse-keymap))
2376 (vmap (make-sparse-keymap)))
2377 (define-key map "\C-xv" vmap)
2378 (define-key map "v" vmap)
2379 (set-keymap-parent vmap vc-prefix-map)
2380 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2381 map))
2383 (define-derived-mode vc-dired-mode dired-mode "Dired under "
2384 "The major mode used in VC directory buffers.
2386 It works like Dired, but lists only files under version control, with
2387 the current VC state of each file being indicated in the place of the
2388 file's link count, owner, group and size. Subdirectories are also
2389 listed, and you may insert them into the buffer as desired, like in
2390 Dired.
2392 All Dired commands operate normally, with the exception of `v', which
2393 is redefined as the version control prefix, so that you can type
2394 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2395 the file named in the current Dired buffer line. `vv' invokes
2396 `vc-next-action' on this file, or on all files currently marked.
2397 There is a special command, `*l', to mark all files currently locked."
2398 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2399 ;; We do it here because dired might not be loaded yet
2400 ;; when vc-dired-mode-map is initialized.
2401 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2402 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2403 ;; The following is slightly modified from files.el,
2404 ;; because file lines look a bit different in vc-dired-mode
2405 ;; (the column before the date does not end in a digit).
2406 ;; albinus: It should be done in the original declaration. Problem
2407 ;; is the optional empty state-info; otherwise ")" would be good
2408 ;; enough as delimeter.
2409 (set (make-local-variable 'directory-listing-before-filename-regexp)
2410 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2411 ;; In some locales, month abbreviations are as short as 2 letters,
2412 ;; and they can be followed by ".".
2413 (month (concat l l "+\\.?"))
2414 (s " ")
2415 (yyyy "[0-9][0-9][0-9][0-9]")
2416 (dd "[ 0-3][0-9]")
2417 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2418 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
2419 (zone "[-+][0-2][0-9][0-5][0-9]")
2420 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
2421 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
2422 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
2423 "\\|" yyyy "-" iso-mm-dd "\\)"))
2424 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
2425 s "+"
2426 "\\(" HH:MM "\\|" yyyy "\\)"))
2427 (western-comma (concat month s "+" dd "," s "+" yyyy))
2428 ;; Japanese MS-Windows ls-lisp has one-digit months, and
2429 ;; omits the Kanji characters after month and day-of-month.
2430 (mm "[ 0-1]?[0-9]")
2431 (japanese
2432 (concat mm l "?" s dd l "?" s "+"
2433 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
2434 ;; the .* below ensures that we find the last match on a line
2435 (concat ".*" s
2436 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
2437 s "+")))
2438 (and (boundp 'vc-dired-switches)
2439 vc-dired-switches
2440 (set (make-local-variable 'dired-actual-switches)
2441 vc-dired-switches))
2442 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2443 (let ((backend-name (symbol-name (vc-responsible-backend
2444 default-directory))))
2445 (setq mode-name (concat mode-name backend-name))
2446 ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
2447 (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
2448 (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
2449 (cons backend-name vc-dire-menu-map) 'subdir)))
2450 (setq vc-dired-mode t))
2452 (defun vc-dired-toggle-terse-mode ()
2453 "Toggle terse display in VC Dired."
2454 (interactive)
2455 (if (not vc-dired-mode)
2457 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2458 (if vc-dired-terse-mode
2459 (vc-dired-hook)
2460 (revert-buffer))))
2462 (defun vc-dired-mark-locked ()
2463 "Mark all files currently locked."
2464 (interactive)
2465 (dired-mark-if (let ((f (dired-get-filename nil t)))
2466 (and f
2467 (not (file-directory-p f))
2468 (not (vc-up-to-date-p f))))
2469 "locked file"))
2471 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2473 (defun vc-dired-reformat-line (vc-info)
2474 "Reformat a directory-listing line.
2475 Replace various columns with version control information, VC-INFO.
2476 This code, like dired, assumes UNIX -l format."
2477 (beginning-of-line)
2478 (when (re-search-forward
2479 ;; Match link count, owner, group, size. Group may be missing,
2480 ;; and only the size is present in OS/2 -l format.
2481 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
2482 (line-end-position) t)
2483 (replace-match (substring (concat vc-info " ") 0 10)
2484 t t nil 1)))
2486 (defun vc-dired-ignorable-p (filename)
2487 "Should FILENAME be ignored in VC-Dired listings?"
2488 (catch t
2489 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
2490 (dolist (ignorable completion-ignored-extensions)
2491 (let ((ext (substring filename
2492 (- (length filename)
2493 (length ignorable)))))
2494 (if (string= ignorable ext) (throw t t))))
2495 ;; Ignore Makefiles derived from something else
2496 (when (string= (file-name-nondirectory filename) "Makefile")
2497 (let* ((dir (file-name-directory filename))
2498 (peers (directory-files (or dir default-directory))))
2499 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
2500 (throw t t))))
2501 nil))
2503 (defun vc-dired-hook ()
2504 "Reformat the listing according to version control.
2505 Called by dired after any portion of a vc-dired buffer has been read in."
2506 (message "Getting version information... ")
2507 ;; if the backend supports it, get the state
2508 ;; of all files in this directory at once
2509 (let ((backend (vc-responsible-backend default-directory)))
2510 ;; check `backend' can really handle `default-directory'.
2511 (if (and (vc-call-backend backend 'responsible-p default-directory)
2512 (vc-find-backend-function backend 'dir-state))
2513 (vc-call-backend backend 'dir-state default-directory)))
2514 (let (filename
2515 (inhibit-read-only t)
2516 (buffer-undo-list t))
2517 (goto-char (point-min))
2518 (while (not (eobp))
2519 (cond
2520 ;; subdir header line
2521 ((dired-get-subdir)
2522 (forward-line 1)
2523 ;; erase (but don't remove) the "total" line
2524 (delete-region (point) (line-end-position))
2525 (beginning-of-line)
2526 (forward-line 1))
2527 ;; file line
2528 ((setq filename (dired-get-filename nil t))
2529 (cond
2530 ;; subdir
2531 ((file-directory-p filename)
2532 (cond
2533 ((member (file-name-nondirectory filename)
2534 vc-directory-exclusion-list)
2535 (let ((pos (point)))
2536 (dired-kill-tree filename)
2537 (goto-char pos)
2538 (dired-kill-line)))
2539 (vc-dired-terse-mode
2540 ;; Don't show directories in terse mode. Don't use
2541 ;; dired-kill-line to remove it, because in recursive listings,
2542 ;; that would remove the directory contents as well.
2543 (delete-region (line-beginning-position)
2544 (progn (forward-line 1) (point))))
2545 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2546 (dired-kill-line))
2548 (vc-dired-reformat-line nil)
2549 (forward-line 1))))
2550 ;; Try to head off calling the expensive state query -
2551 ;; ignore object files, TeX intermediate files, and so forth.
2552 ((vc-dired-ignorable-p filename)
2553 (dired-kill-line))
2554 ;; Ordinary file -- call the (possibly expensive) state query
2556 ;; First case: unregistered or unknown. (Unknown shouldn't happen here)
2557 ((member (vc-state filename) '(nil unregistered))
2558 (if vc-dired-terse-mode
2559 (dired-kill-line)
2560 (vc-dired-reformat-line "?")
2561 (forward-line 1)))
2562 ;; Either we're in non-terse mode or it's out of date
2563 ((not (and vc-dired-terse-mode (vc-up-to-date-p filename)))
2564 (vc-dired-reformat-line (vc-call dired-state-info filename))
2565 (forward-line 1))
2566 ;; Remaining cases are under version control but uninteresting
2568 (dired-kill-line))))
2569 ;; any other line
2570 (t (forward-line 1))))
2571 (vc-dired-purge))
2572 (message "Getting version information... done")
2573 (save-restriction
2574 (widen)
2575 (cond ((eq (count-lines (point-min) (point-max)) 1)
2576 (goto-char (point-min))
2577 (message "No changes pending under %s" default-directory)))))
2579 (defun vc-dired-purge ()
2580 "Remove empty subdirs."
2581 (goto-char (point-min))
2582 (while (dired-get-subdir)
2583 (forward-line 2)
2584 (if (dired-get-filename nil t)
2585 (if (not (dired-next-subdir 1 t))
2586 (goto-char (point-max)))
2587 (forward-line -2)
2588 (if (not (string= (dired-current-directory) default-directory))
2589 (dired-do-kill-lines t "")
2590 ;; We cannot remove the top level directory.
2591 ;; Just make it look a little nicer.
2592 (forward-line 1)
2593 (or (eobp) (kill-line))
2594 (if (not (dired-next-subdir 1 t))
2595 (goto-char (point-max))))))
2596 (goto-char (point-min)))
2598 (defun vc-dired-buffers-for-dir (dir)
2599 "Return a list of all vc-dired buffers that currently display DIR."
2600 (let (result)
2601 ;; Check whether dired is loaded.
2602 (when (fboundp 'dired-buffers-for-dir)
2603 (dolist (buffer (dired-buffers-for-dir dir))
2604 (with-current-buffer buffer
2605 (if vc-dired-mode
2606 (push buffer result)))))
2607 (nreverse result)))
2609 (defun vc-dired-resynch-file (file)
2610 "Update the entries for FILE in any VC Dired buffers that list it."
2611 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2612 (when buffers
2613 (mapcar (lambda (buffer)
2614 (with-current-buffer buffer
2615 (if (dired-goto-file file)
2616 ;; bind vc-dired-terse-mode to nil so that
2617 ;; files won't vanish when they are checked in
2618 (let ((vc-dired-terse-mode nil))
2619 (dired-do-redisplay 1)))))
2620 buffers))))
2622 ;;;###autoload
2623 (defun vc-directory (dir read-switches)
2624 "Create a buffer in VC Dired Mode for directory DIR.
2626 See Info node `VC Dired Mode'.
2628 With prefix arg READ-SWITCHES, specify a value to override
2629 `dired-listing-switches' when generating the listing."
2630 (interactive "DDired under VC (directory): \nP")
2631 (let ((vc-dired-switches (concat vc-dired-listing-switches
2632 (if vc-dired-recurse "R" ""))))
2633 (if read-switches
2634 (setq vc-dired-switches
2635 (read-string "Dired listing switches: "
2636 vc-dired-switches)))
2637 (require 'dired)
2638 (require 'dired-aux)
2639 (switch-to-buffer
2640 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2641 vc-dired-switches
2642 'vc-dired-mode))))
2644 ;;; Experimental code for the vc-dired replacement
2645 (require 'ewoc)
2647 (defstruct (vc-status-fileinfo
2648 (:copier nil)
2649 (:constructor vc-status-create-fileinfo (state name &optional marked))
2650 (:conc-name vc-status-fileinfo->))
2651 marked
2652 state
2653 name)
2655 (defvar vc-status nil)
2657 (defun vc-status-headers (backend dir)
2658 (concat
2659 (format "VC backend : %s\n" backend)
2660 "Repository : The repository goes here\n"
2661 (format "Working dir: %s\n" dir)))
2663 (defun vc-status-printer (fileentry)
2664 "Pretty print FILEENTRY."
2665 ;; If you change the layout here, change vc-status-move-to-goal-column.
2666 (insert
2667 (propertize
2668 (format "%c" (if (vc-status-fileinfo->marked fileentry) ?* ? ))
2669 'face 'font-lock-type-face)
2671 (propertize
2672 (format "%-20s" (vc-status-fileinfo->state fileentry))
2673 'face 'font-lock-variable-name-face
2674 'mouse-face 'highlight)
2676 (propertize
2677 (format "%s" (vc-status-fileinfo->name fileentry))
2678 'face 'font-lock-function-name-face
2679 'mouse-face 'highlight)))
2681 (defun vc-status-move-to-goal-column ()
2682 (beginning-of-line)
2683 ;; Must be in sync with vc-status-printer.
2684 (forward-char 25))
2686 ;;;###autoload
2687 (defun vc-status (dir)
2688 "Show the VC status for DIR."
2689 (interactive "DVC status for directory: ")
2690 (vc-setup-buffer "*vc-status*")
2691 (switch-to-buffer "*vc-status*")
2692 (cd dir)
2693 (vc-status-mode))
2695 (defvar vc-status-menu-map
2696 (let ((map (make-sparse-keymap "VC-status")))
2697 (define-key map [quit]
2698 '(menu-item "Quit" bury-buffer
2699 :help "Quit"))
2700 (define-key map [refresh]
2701 '(menu-item "Refresh" vc-status-refresh
2702 :help "Refresh the contents of the VC status buffer"))
2704 ;; VC commands.
2705 (define-key map [separator-vc-commands] '("--"))
2706 (define-key map [annotate]
2707 '(menu-item "Annotate" vc-annotate
2708 :help "Display the edit history of the current file using colors"))
2709 (define-key map [diff]
2710 '(menu-item "Compare with Base Version" vc-diff
2711 :help "Compare file set with the base version"))
2712 (define-key map [register]
2713 '(menu-item "Register" vc-status-register
2714 :help "Register file set into the version control system"))
2715 ;; vc-print-log uses the current buffer, not a file.
2716 ;; (define-key map [log]
2717 ;; '(menu-item "Show history" vc-status-print-log
2718 ;; :help "List the change log of the current file set in a window"))
2720 ;; Movement.
2721 (define-key map [separator-movement] '("--"))
2722 (define-key map [next-line]
2723 '(menu-item "Next line" vc-status-next-line
2724 :help "Go to the next line" :keys "n"))
2725 (define-key map [previous-line]
2726 '(menu-item "Previous line" vc-status-previous-line
2727 :help "Go to the previous line"))
2728 ;; Marking.
2729 (define-key map [separator-marking] '("--"))
2730 (define-key map [unmark-all]
2731 '(menu-item "Unmark All" vc-status-unmark-all-files
2732 :help "Unmark all files that are in the same state as the current file\
2733 \nWith prefix argument unmark all files"))
2734 (define-key map [unmark-previous]
2735 '(menu-item "Unmark previous " vc-status-unmark-file-up
2736 :help "Move to the previous line and unmark the file"))
2738 (define-key map [mark-all]
2739 '(menu-item "Mark All" vc-status-mark-all-files
2740 :help "Mark all files that are in the same state as the current file\
2741 \nWith prefix argument mark all files"))
2742 (define-key map [unmark]
2743 '(menu-item "Unmark" vc-status-unmark
2744 :help "Unmark the current file or all files in the region"))
2746 (define-key map [mark]
2747 '(menu-item "Mark" vc-status-mark
2748 :help "Mark the current file or all files in the region"))
2750 (define-key map [separator-open] '("--"))
2751 (define-key map [open-other]
2752 '(menu-item "Open in other window" vc-status-find-file-other-window
2753 :help "Find the file on the current line, in another window"))
2754 (define-key map [open]
2755 '(menu-item "Open file" vc-status-find-file
2756 :help "Find the file on the current line"))
2757 map)
2758 "Menu for VC status")
2760 (defalias 'vc-status-menu-map vc-status-menu-map)
2762 (defvar vc-status-mode-map
2763 (let ((map (make-keymap)))
2764 (suppress-keymap map)
2765 ;; Marking.
2766 (define-key map "m" 'vc-status-mark)
2767 (define-key map "M" 'vc-status-mark-all-files)
2768 (define-key map "u" 'vc-status-unmark)
2769 (define-key map "\C-?" 'vc-status-unmark-file-up)
2770 (define-key map "\M-\C-?" 'vc-status-unmark-all-files)
2771 ;; Movement.
2772 (define-key map "n" 'vc-status-next-line)
2773 (define-key map " " 'vc-status-next-line)
2774 (define-key map "\t" 'vc-status-next-line)
2775 (define-key map "p" 'vc-status-previous-line)
2776 (define-key map [backtab] 'vc-status-previous-line)
2777 ;; VC commands.
2778 (define-key map "=" 'vc-diff)
2779 (define-key map "a" 'vc-status-register)
2780 ;; Can't be "g" (as in vc map), so "A" for "Annotate".
2781 (define-key map "A" 'vc-annotate)
2782 ;; vc-print-log uses the current buffer, not a file.
2783 ;; (define-key map "l" 'vc-status-print-log)
2784 ;; The remainder.
2785 (define-key map "f" 'vc-status-find-file)
2786 (define-key map "o" 'vc-status-find-file-other-window)
2787 (define-key map "q" 'bury-buffer)
2788 (define-key map "g" 'vc-status-refresh)
2789 (define-key map "\C-c\C-c" 'vc-status-kill-dir-status-process)
2790 ;; Not working yet. Functions like vc-status-find-file need to
2791 ;; find the file from the mouse position, not `point'.
2792 ;; (define-key map [(down-mouse-3)] 'vc-status-menu)
2794 ;; Hook up the menu.
2795 (define-key map [menu-bar vc-status-mode]
2796 '(menu-item
2797 ;; This is used to that VC backends could add backend specific
2798 ;; menu items to vc-status-menu-map.
2799 "VC Status" vc-status-menu-map :filter vc-status-menu-map-filter))
2800 map)
2801 "Keymap for VC status")
2803 (defun vc-default-extra-status-menu (backend)
2804 nil)
2806 (defun vc-status-menu-map-filter (orig-binding)
2807 (if (boundp 'vc-ignore-menu-filter)
2808 orig-binding
2809 (when (and (symbolp orig-binding) (fboundp orig-binding))
2810 (setq orig-binding (indirect-function orig-binding)))
2811 (let ((ext-binding
2812 (vc-call-backend (vc-responsible-backend default-directory)
2813 'extra-status-menu)))
2814 (if (null ext-binding)
2815 orig-binding
2816 (append orig-binding
2817 '("----")
2818 ext-binding)))))
2820 (defun vc-status-menu (e)
2821 "Popup the VC status menu."
2822 (interactive "e")
2823 (popup-menu vc-status-menu-map e))
2825 (defvar vc-status-tool-bar-map
2826 (if (display-graphic-p)
2827 (let ((map (make-sparse-keymap))
2828 (vc-ignore-menu-filter t)) ;; Backend may not support vc-status
2829 (tool-bar-local-item-from-menu 'vc-status-find-file "open"
2830 map vc-status-mode-map)
2831 (tool-bar-local-item "bookmark_add"
2832 'vc-status-toggle-mark 'vc-status-toggle-mark map
2833 :help "Toggle mark on current item")
2834 (tool-bar-local-item-from-menu 'vc-status-previous-line "left-arrow"
2835 map vc-status-mode-map
2836 :rtl "right-arrow")
2837 (tool-bar-local-item-from-menu 'vc-status-next-line "right-arrow"
2838 map vc-status-mode-map
2839 :rtl "left-arrow")
2840 (tool-bar-local-item-from-menu 'vc-status-refresh "refresh"
2841 map vc-status-mode-map)
2842 (tool-bar-local-item-from-menu 'nonincremental-search-forward
2843 "search" map)
2844 (tool-bar-local-item-from-menu 'bury-buffer "exit"
2845 map vc-status-mode-map)
2846 map)))
2849 (defvar vc-status-process-buffer nil
2850 "The buffer used for the asynchronous call that computes the VC status.")
2852 (defvar vc-status-crt-marked nil
2853 "The list of marked files before `vc-status-refresh'.")
2855 (defun vc-status-mode ()
2856 "Major mode for VC status.
2857 \\{vc-status-mode-map}"
2858 (setq mode-name "*VC Status*")
2859 (setq major-mode 'vc-status-mode)
2860 (setq buffer-read-only t)
2861 (set (make-local-variable 'vc-status-crt-marked) nil)
2862 (use-local-map vc-status-mode-map)
2863 (set (make-local-variable 'tool-bar-map) vc-status-tool-bar-map)
2864 (let ((buffer-read-only nil)
2865 (backend (vc-responsible-backend default-directory))
2866 entries)
2867 (erase-buffer)
2868 (set (make-local-variable 'vc-status-process-buffer) nil)
2869 (set (make-local-variable 'vc-status)
2870 (ewoc-create #'vc-status-printer
2871 (vc-status-headers backend default-directory)))
2872 (vc-status-refresh)))
2874 (put 'vc-status-mode 'mode-class 'special)
2876 (defun vc-update-vc-status-buffer (entries buffer)
2877 (with-current-buffer buffer
2878 (when entries
2879 ;; Insert the entries we got into the ewoc.
2880 (dolist (entry entries)
2881 (ewoc-enter-last vc-status
2882 (vc-status-create-fileinfo (cdr entry) (car entry))))
2883 ;; If we had marked items before the refresh, try mark them here.
2884 ;; XXX: there should be a better way to do this...
2885 (when vc-status-crt-marked
2886 (ewoc-map
2887 (lambda (arg)
2888 (when (member (vc-status-fileinfo->name arg) vc-status-crt-marked)
2889 (setf (vc-status-fileinfo->marked arg) t)))
2890 vc-status))
2891 (ewoc-goto-node vc-status (ewoc-nth vc-status 0)))
2892 ;; We are done, turn of the in progress message in the mode-line.
2893 (setq mode-line-process nil)))
2895 (defun vc-add-to-vc-status-buffer (entry buffer)
2896 ;; Add one ENTRY to the vc-status buffer BUFFER.
2897 ;; This will be used to automatically add files with the "modified"
2898 ;; state when saving them.
2900 ;; ENTRY is (FILENAME . STATE)
2901 (with-current-buffer buffer
2902 (let ((crt (ewoc-nth vc-status 0))
2903 (fname (car entry)))
2904 ;; First try to see if there's already an entry with that name
2905 ;; in the ewoc.
2906 (while (and crt (not (string= (vc-status-fileinfo->name
2907 (ewoc-data crt)) fname)))
2908 (setq crt (ewoc-next vc-status crt)))
2909 (if crt
2910 (progn
2911 ;; Found the file, just update the status.
2912 (setf (vc-status-fileinfo->state (ewoc-data crt)) (cdr entry))
2913 (ewoc-invalidate vc-status crt))
2914 ;; Could not find the file, insert a new entry.
2915 (ewoc-enter-last
2916 vc-status (vc-status-create-fileinfo (cdr entry) (car entry)))))))
2918 (defun vc-status-refresh ()
2919 "Refresh the contents of the VC status buffer."
2920 (interactive)
2922 ;; This is not very efficient; ewoc could use a new function here.
2923 ;; We clear the ewoc, but remember the marked files so that we can
2924 ;; mark them after the refresh is done.
2925 (setq vc-status-crt-marked
2926 (mapcar
2927 (lambda (elem)
2928 (vc-status-fileinfo->name elem))
2929 (ewoc-collect
2930 vc-status
2931 (lambda (crt) (vc-status-fileinfo->marked crt)))))
2932 (ewoc-filter vc-status (lambda (node) nil))
2934 (let ((backend (vc-responsible-backend default-directory)))
2935 (vc-set-mode-line-busy-indicator)
2936 ;; Call the dir-status backend function. dir-status is supposed to
2937 ;; be asynchronous. It should compute the results and call the
2938 ;; function passed as a an arg to update the vc-status buffer with
2939 ;; the results.
2940 (setq vc-status-process-buffer
2941 (vc-call-backend
2942 backend 'dir-status default-directory
2943 #'vc-update-vc-status-buffer (current-buffer)))))
2945 (defun vc-status-kill-dir-status-process ()
2946 "Kill the temporary buffer and associated process."
2947 (interactive)
2948 (when (and (bufferp vc-status-process-buffer)
2949 (buffer-live-p vc-status-process-buffer))
2950 (let ((proc (get-buffer-process vc-status-process-buffer)))
2951 (when proc (delete-process proc))
2952 (setq mode-line-process nil))))
2954 (defun vc-status-next-line (arg)
2955 "Go to the next line.
2956 If a prefix argument is given, move by that many lines."
2957 (interactive "p")
2958 (ewoc-goto-next vc-status arg)
2959 (vc-status-move-to-goal-column))
2961 (defun vc-status-previous-line (arg)
2962 "Go to the previous line.
2963 If a prefix argument is given, move by that many lines."
2964 (interactive "p")
2965 (ewoc-goto-prev vc-status arg)
2966 (vc-status-move-to-goal-column))
2968 (defun vc-status-mark-unmark (mark-unmark-function)
2969 (if (use-region-p)
2970 (let ((firstl (line-number-at-pos (region-beginning)))
2971 (lastl (line-number-at-pos (region-end))))
2972 (save-excursion
2973 (goto-char (region-beginning))
2974 (while (<= (line-number-at-pos) lastl)
2975 (funcall mark-unmark-function))))
2976 (funcall mark-unmark-function)))
2978 (defun vc-status-mark-file ()
2979 ;; Mark the current file and move to the next line.
2980 (let* ((crt (ewoc-locate vc-status))
2981 (file (ewoc-data crt)))
2982 (setf (vc-status-fileinfo->marked file) t)
2983 (ewoc-invalidate vc-status crt)
2984 (vc-status-next-line 1)))
2986 (defun vc-status-mark ()
2987 "Mark the current file or all files in the region.
2988 If the region is active, mark all the files in the region.
2989 Otherwise mark the file on the current line and move to the next
2990 line."
2991 (interactive)
2992 (vc-status-mark-unmark 'vc-status-mark-file))
2995 ;; XXX: Should this take the region into consideration?
2996 (defun vc-status-mark-all-files (arg)
2997 "Mark all files with the same state as the current one.
2998 With a prefix argument mark all files.
3000 The VC commands operate on files that are on the same state.
3001 This command is intended to make it easy to select all files that
3002 share the same state."
3003 (interactive "P")
3004 (if arg
3005 (ewoc-map
3006 (lambda (filearg)
3007 (unless (vc-status-fileinfo->marked filearg)
3008 (setf (vc-status-fileinfo->marked filearg) t)
3010 vc-status)
3011 (let* ((crt (ewoc-locate vc-status))
3012 (crt-state (vc-status-fileinfo->state (ewoc-data crt))))
3013 (ewoc-map
3014 (lambda (filearg)
3015 (when (and (not (vc-status-fileinfo->marked filearg))
3016 (eq (vc-status-fileinfo->state filearg) crt-state))
3017 (setf (vc-status-fileinfo->marked filearg) t)
3019 vc-status))))
3021 (defun vc-status-unmark-file ()
3022 ;; Unmark the current file and move to the next line.
3023 (let* ((crt (ewoc-locate vc-status))
3024 (file (ewoc-data crt)))
3025 (setf (vc-status-fileinfo->marked file) nil)
3026 (ewoc-invalidate vc-status crt)
3027 (vc-status-next-line 1)))
3029 (defun vc-status-unmark ()
3030 "Unmark the current file or all files in the region.
3031 If the region is active, unmark all the files in the region.
3032 Otherwise mark the file on the current line and move to the next
3033 line."
3034 (interactive)
3035 (vc-status-mark-unmark 'vc-status-unmark-file))
3037 (defun vc-status-unmark-file-up ()
3038 "Move to the previous line and unmark the file."
3039 (interactive)
3040 ;; If we're on the first line, we won't move up, but we will still
3041 ;; remove the mark. This seems a bit odd but it is what buffer-menu
3042 ;; does.
3043 (let* ((prev (ewoc-goto-prev vc-status 1))
3044 (file (ewoc-data prev)))
3045 (setf (vc-status-fileinfo->marked file) nil)
3046 (ewoc-invalidate vc-status prev)
3047 (vc-status-move-to-goal-column)))
3049 (defun vc-status-unmark-all-files (arg)
3050 "Unmark all files with the same state as the current one.
3051 With a prefix argument mark all files.
3053 The VC commands operate on files that are on the same state.
3054 This command is intended to make it easy to deselect all files
3055 that share the same state."
3056 (interactive "P")
3057 (if arg
3058 (ewoc-map
3059 (lambda (filearg)
3060 (when (vc-status-fileinfo->marked filearg)
3061 (setf (vc-status-fileinfo->marked filearg) nil)
3063 vc-status)
3064 (let* ((crt (ewoc-locate vc-status))
3065 (crt-state (vc-status-fileinfo->state (ewoc-data crt))))
3066 (ewoc-map
3067 (lambda (filearg)
3068 (when (and (vc-status-fileinfo->marked filearg)
3069 (eq (vc-status-fileinfo->state filearg) crt-state))
3070 (setf (vc-status-fileinfo->marked filearg) nil)
3072 vc-status))))
3074 (defun vc-status-toggle-mark-file ()
3075 (let* ((crt (ewoc-locate vc-status))
3076 (file (ewoc-data crt)))
3077 (if (vc-status-fileinfo->marked file)
3078 (vc-status-unmark-file)
3079 (vc-status-mark-file))))
3081 (defun vc-status-toggle-mark ()
3082 (interactive)
3083 (vc-status-mark-unmark 'vc-status-toggle-mark-file))
3085 (defun vc-status-register ()
3086 "Register the marked files, or the current file if no marks."
3087 (interactive)
3088 (let ((files (or (vc-status-marked-files)
3089 (list (vc-status-current-file)))))
3090 (dolist (file files)
3091 (vc-register file))))
3093 (defun vc-status-find-file ()
3094 "Find the file on the current line."
3095 (interactive)
3096 (find-file (vc-status-current-file)))
3098 (defun vc-status-find-file-other-window ()
3099 "Find the file on the current line, in another window."
3100 (interactive)
3101 (find-file-other-window (vc-status-current-file)))
3103 (defun vc-status-current-file ()
3104 (let ((node (ewoc-locate vc-status)))
3105 (unless node
3106 (error "No file available."))
3107 (expand-file-name (vc-status-fileinfo->name (ewoc-data node)))))
3109 (defun vc-status-marked-files ()
3110 "Return the list of marked files"
3111 (mapcar
3112 (lambda (elem)
3113 (expand-file-name (vc-status-fileinfo->name elem)))
3114 (ewoc-collect
3115 vc-status
3116 (lambda (crt) (vc-status-fileinfo->marked crt)))))
3118 ;;; End experimental code.
3120 ;; Named-configuration entry points
3122 (defun vc-snapshot-precondition (dir)
3123 "Scan the tree below DIR, looking for files not up-to-date.
3124 If any file is not up-to-date, return the name of the first such file.
3125 \(This means, neither snapshot creation nor retrieval is allowed.\)
3126 If one or more of the files are currently visited, return `visited'.
3127 Otherwise, return nil."
3128 (let ((status nil))
3129 (catch 'vc-locked-example
3130 (vc-file-tree-walk
3132 (lambda (f)
3133 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
3134 (if (get-file-buffer f) (setq status 'visited)))))
3135 status)))
3137 ;;;###autoload
3138 (defun vc-create-snapshot (dir name branchp)
3139 "Descending recursively from DIR, make a snapshot called NAME.
3140 For each registered file, the working revision becomes part of
3141 the named configuration. If the prefix argument BRANCHP is
3142 given, the snapshot is made as a new branch and the files are
3143 checked out in that new branch."
3144 (interactive
3145 (list (read-file-name "Directory: " default-directory default-directory t)
3146 (read-string "New snapshot name: ")
3147 current-prefix-arg))
3148 (message "Making %s... " (if branchp "branch" "snapshot"))
3149 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
3150 (vc-call-backend (vc-responsible-backend dir)
3151 'create-snapshot dir name branchp)
3152 (message "Making %s... done" (if branchp "branch" "snapshot")))
3154 ;;;###autoload
3155 (defun vc-retrieve-snapshot (dir name)
3156 "Descending recursively from DIR, retrieve the snapshot called NAME.
3157 If NAME is empty, it refers to the latest revisions.
3158 If locking is used for the files in DIR, then there must not be any
3159 locked files at or below DIR (but if NAME is empty, locked files are
3160 allowed and simply skipped)."
3161 (interactive
3162 (list (read-file-name "Directory: " default-directory default-directory t)
3163 (read-string "Snapshot name to retrieve (default latest revisions): ")))
3164 (let ((update (yes-or-no-p "Update any affected buffers? "))
3165 (msg (if (or (not name) (string= name ""))
3166 (format "Updating %s... " (abbreviate-file-name dir))
3167 (format "Retrieving snapshot into %s... "
3168 (abbreviate-file-name dir)))))
3169 (message "%s" msg)
3170 (vc-call-backend (vc-responsible-backend dir)
3171 'retrieve-snapshot dir name update)
3172 (message "%s" (concat msg "done"))))
3174 ;; Miscellaneous other entry points
3176 ;;;###autoload
3177 (defun vc-print-log (&optional working-revision)
3178 "List the change log of the current fileset in a window.
3179 If WORKING-REVISION is non-nil, leave the point at that revision."
3180 (interactive)
3181 (let* ((files (vc-deduce-fileset))
3182 (backend (vc-backend files))
3183 (working-revision (or working-revision (vc-working-revision (car files)))))
3184 ;; Don't switch to the output buffer before running the command,
3185 ;; so that any buffer-local settings in the vc-controlled
3186 ;; buffer can be accessed by the command.
3187 (vc-call-backend backend 'print-log files "*vc-change-log*")
3188 (pop-to-buffer "*vc-change-log*")
3189 (vc-exec-after
3190 `(let ((inhibit-read-only t))
3191 (vc-call-backend ',backend 'log-view-mode)
3192 (goto-char (point-max)) (forward-line -1)
3193 (while (looking-at "=*\n")
3194 (delete-char (- (match-end 0) (match-beginning 0)))
3195 (forward-line -1))
3196 (goto-char (point-min))
3197 (if (looking-at "[\b\t\n\v\f\r ]+")
3198 (delete-char (- (match-end 0) (match-beginning 0))))
3199 (shrink-window-if-larger-than-buffer)
3200 ;; move point to the log entry for the working revision
3201 (vc-call-backend ',backend 'show-log-entry ',working-revision)
3202 (setq vc-sentinel-movepoint (point))
3203 (set-buffer-modified-p nil)))))
3205 ;;;###autoload
3206 (defun vc-revert ()
3207 "Revert working copies of the selected fileset to their repository contents.
3208 This asks for confirmation if the buffer contents are not identical
3209 to the working revision (except for keyword expansion)."
3210 (interactive)
3211 (let* ((files (vc-deduce-fileset)))
3212 ;; If any of the files is visited by the current buffer, make
3213 ;; sure buffer is saved. If the user says `no', abort since
3214 ;; we cannot show the changes and ask for confirmation to
3215 ;; discard them.
3216 (if (or (not files) (memq (buffer-file-name) files))
3217 (vc-buffer-sync nil))
3218 (dolist (file files)
3219 (let ((buf (get-file-buffer file)))
3220 (if (and buf (buffer-modified-p buf))
3221 (error "Please kill or save all modified buffers before reverting.")))
3222 (if (vc-up-to-date-p file)
3223 (unless (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
3224 (error "Revert canceled"))))
3225 (if (vc-diff-internal vc-allow-async-revert files nil nil)
3226 (progn
3227 (unless (yes-or-no-p (format "Discard changes in %s? " (vc-delistify files)))
3228 (error "Revert canceled"))
3229 (delete-windows-on "*vc-diff*")
3230 (kill-buffer "*vc-diff*")))
3231 (dolist (file files)
3232 (progn
3233 (message "Reverting %s..." (vc-delistify files))
3234 (vc-revert-file file)
3235 (message "Reverting %s...done" (vc-delistify files))))))
3237 ;;;###autoload
3238 (defun vc-rollback ()
3239 "Roll back (remove) the most recent changeset committed to the repository.
3240 This may be either a file-level or a repository-level operation,
3241 depending on the underlying version-control system."
3242 (interactive)
3243 (let* ((files (vc-deduce-fileset))
3244 (backend (vc-backend files))
3245 (granularity (vc-call-backend backend 'revision-granularity)))
3246 (unless (vc-find-backend-function backend 'rollback)
3247 (error "Rollback is not supported in %s" backend))
3248 (if (and (not (eq granularity 'repository)) (/= (length files) 1))
3249 (error "Rollback requires a singleton fileset or repository versioning"))
3250 (if (not (vc-call latest-on-branch-p (car files)))
3251 (error "Rollback is only possible at the tip revision."))
3252 ;; If any of the files is visited by the current buffer, make
3253 ;; sure buffer is saved. If the user says `no', abort since
3254 ;; we cannot show the changes and ask for confirmation to
3255 ;; discard them.
3256 (if (or (not files) (memq (buffer-file-name) files))
3257 (vc-buffer-sync nil))
3258 (dolist (file files)
3259 (if (buffer-modified-p (get-file-buffer file))
3260 (error "Please kill or save all modified buffers before rollback."))
3261 (if (not (vc-up-to-date-p file))
3262 (error "Please revert all modified workfiles before rollback.")))
3263 ;; Accumulate changes associated with the fileset
3264 (vc-setup-buffer "*vc-diff*")
3265 (not-modified)
3266 (message "Finding changes...")
3267 (let* ((tip (vc-working-revision (car files)))
3268 (previous (vc-call previous-revision (car files) tip)))
3269 (vc-diff-internal nil files previous tip))
3270 ;; Display changes
3271 (unless (yes-or-no-p "Discard these revisions? ")
3272 (error "Rollback canceled"))
3273 (delete-windows-on "*vc-diff*")
3274 (kill-buffer"*vc-diff*")
3275 ;; Do the actual reversions
3276 (message "Rolling back %s..." (vc-delistify files))
3277 (with-vc-properties
3278 files
3279 (vc-call-backend backend 'rollback files)
3280 `((vc-state . ,'up-to-date)
3281 (vc-checkout-time . , (nth 5 (file-attributes file)))
3282 (vc-working-revision . nil)))
3283 (dolist (f files) (vc-resynch-buffer f t t))
3284 (message "Rolling back %s...done" (vc-delistify files))))
3286 ;;;###autoload
3287 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
3289 ;;;###autoload
3290 (defun vc-update ()
3291 "Update the current fileset's files to their tip revisions.
3292 For each one that contains no changes, and is not locked, then this simply
3293 replaces the work file with the latest revision on its branch. If the file
3294 contains changes, and the backend supports merging news, then any recent
3295 changes from the current branch are merged into the working file."
3296 (interactive)
3297 (dolist (file (vc-deduce-fileset))
3298 (if (buffer-modified-p (get-file-buffer file))
3299 (error "Please kill or save all modified buffers before updating."))
3300 (if (vc-up-to-date-p file)
3301 (vc-checkout file nil t)
3302 (if (eq (vc-checkout-model file) 'locking)
3303 (if (eq (vc-state file) 'edited)
3304 (error "%s"
3305 (substitute-command-keys
3306 "File is locked--type \\[vc-revert] to discard changes"))
3307 (error "Unexpected file state (%s) -- type %s"
3308 (vc-state file)
3309 (substitute-command-keys
3310 "\\[vc-next-action] to correct")))
3311 (if (not (vc-find-backend-function (vc-backend file) 'merge-news))
3312 (error "Sorry, merging news is not implemented for %s"
3313 (vc-backend file))
3314 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))))
3316 (defun vc-version-backup-file (file &optional rev)
3317 "Return name of backup file for revision REV of FILE.
3318 If version backups should be used for FILE, and there exists
3319 such a backup for REV or the working revision of file, return
3320 its name; otherwise return nil."
3321 (when (vc-call make-version-backups-p file)
3322 (let ((backup-file (vc-version-backup-file-name file rev)))
3323 (if (file-exists-p backup-file)
3324 backup-file
3325 ;; there is no automatic backup, but maybe the user made one manually
3326 (setq backup-file (vc-version-backup-file-name file rev 'manual))
3327 (if (file-exists-p backup-file)
3328 backup-file)))))
3330 (defun vc-revert-file (file)
3331 "Revert FILE back to the repository working revision it was based on."
3332 (with-vc-properties
3333 (list file)
3334 (let ((backup-file (vc-version-backup-file file)))
3335 (when backup-file
3336 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
3337 (vc-delete-automatic-version-backups file))
3338 (vc-call revert file backup-file))
3339 `((vc-state . up-to-date)
3340 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
3341 (vc-resynch-buffer file t t))
3343 ;;;###autoload
3344 (defun vc-switch-backend (file backend)
3345 "Make BACKEND the current version control system for FILE.
3346 FILE must already be registered in BACKEND. The change is not
3347 permanent, only for the current session. This function only changes
3348 VC's perspective on FILE, it does not register or unregister it.
3349 By default, this command cycles through the registered backends.
3350 To get a prompt, use a prefix argument."
3351 (interactive
3352 (list
3353 (or buffer-file-name
3354 (error "There is no version-controlled file in this buffer"))
3355 (let ((backend (vc-backend buffer-file-name))
3356 (backends nil))
3357 (unless backend
3358 (error "File %s is not under version control" buffer-file-name))
3359 ;; Find the registered backends.
3360 (dolist (backend vc-handled-backends)
3361 (when (vc-call-backend backend 'registered buffer-file-name)
3362 (push backend backends)))
3363 ;; Find the next backend.
3364 (let ((def (car (delq backend (append (memq backend backends) backends))))
3365 (others (delete backend backends)))
3366 (cond
3367 ((null others) (error "No other backend to switch to"))
3368 (current-prefix-arg
3369 (intern
3370 (upcase
3371 (completing-read
3372 (format "Switch to backend [%s]: " def)
3373 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
3374 nil t nil nil (downcase (symbol-name def))))))
3375 (t def))))))
3376 (unless (eq backend (vc-backend file))
3377 (vc-file-clearprops file)
3378 (vc-file-setprop file 'vc-backend backend)
3379 ;; Force recomputation of the state
3380 (unless (vc-call-backend backend 'registered file)
3381 (vc-file-clearprops file)
3382 (error "%s is not registered in %s" file backend))
3383 (vc-mode-line file)))
3385 ;;;###autoload
3386 (defun vc-transfer-file (file new-backend)
3387 "Transfer FILE to another version control system NEW-BACKEND.
3388 If NEW-BACKEND has a higher precedence than FILE's current backend
3389 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
3390 NEW-BACKEND, using the revision number from the current backend as the
3391 base level. If NEW-BACKEND has a lower precedence than the current
3392 backend, then commit all changes that were made under the current
3393 backend to NEW-BACKEND, and unregister FILE from the current backend.
3394 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
3395 (let* ((old-backend (vc-backend file))
3396 (edited (memq (vc-state file) '(edited needs-merge)))
3397 (registered (vc-call-backend new-backend 'registered file))
3398 (move
3399 (and registered ; Never move if not registered in new-backend yet.
3400 ;; move if new-backend comes later in vc-handled-backends
3401 (or (memq new-backend (memq old-backend vc-handled-backends))
3402 (y-or-n-p "Final transfer? "))))
3403 (comment nil))
3404 (if (eq old-backend new-backend)
3405 (error "%s is the current backend of %s" new-backend file))
3406 (if registered
3407 (set-file-modes file (logior (file-modes file) 128))
3408 ;; `registered' might have switched under us.
3409 (vc-switch-backend file old-backend)
3410 (let* ((rev (vc-working-revision file))
3411 (modified-file (and edited (make-temp-file file)))
3412 (unmodified-file (and modified-file (vc-version-backup-file file))))
3413 ;; Go back to the base unmodified file.
3414 (unwind-protect
3415 (progn
3416 (when modified-file
3417 (copy-file file modified-file 'ok-if-already-exists)
3418 ;; If we have a local copy of the unmodified file, handle that
3419 ;; here and not in vc-revert-file because we don't want to
3420 ;; delete that copy -- it is still useful for OLD-BACKEND.
3421 (if unmodified-file
3422 (copy-file unmodified-file file
3423 'ok-if-already-exists 'keep-date)
3424 (if (y-or-n-p "Get base revision from master? ")
3425 (vc-revert-file file))))
3426 (vc-call-backend new-backend 'receive-file file rev))
3427 (when modified-file
3428 (vc-switch-backend file new-backend)
3429 (unless (eq (vc-checkout-model file) 'implicit)
3430 (vc-checkout file t nil))
3431 (rename-file modified-file file 'ok-if-already-exists)
3432 (vc-file-setprop file 'vc-checkout-time nil)))))
3433 (when move
3434 (vc-switch-backend file old-backend)
3435 (setq comment (vc-call comment-history file))
3436 (vc-call unregister file))
3437 (vc-switch-backend file new-backend)
3438 (when (or move edited)
3439 (vc-file-setprop file 'vc-state 'edited)
3440 (vc-mode-line file)
3441 (vc-checkin file nil comment (stringp comment)))))
3443 (defun vc-rename-master (oldmaster newfile templates)
3444 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
3445 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
3446 (newdir (or (file-name-directory newfile) ""))
3447 (newbase (file-name-nondirectory newfile))
3448 (masters
3449 ;; List of potential master files for `newfile'
3450 (mapcar
3451 (lambda (s) (vc-possible-master s newdir newbase))
3452 templates)))
3453 (if (or (file-symlink-p oldmaster)
3454 (file-symlink-p (file-name-directory oldmaster)))
3455 (error "This is unsafe in the presence of symbolic links"))
3456 (rename-file
3457 oldmaster
3458 (catch 'found
3459 ;; If possible, keep the master file in the same directory.
3460 (dolist (f masters)
3461 (if (and f (string= (file-name-directory (expand-file-name f)) dir))
3462 (throw 'found f)))
3463 ;; If not, just use the first possible place.
3464 (dolist (f masters)
3465 (and f (or (not (setq dir (file-name-directory f)))
3466 (file-directory-p dir))
3467 (throw 'found f)))
3468 (error "New file lacks a version control directory")))))
3470 (defun vc-delete-file (file)
3471 "Delete file and mark it as such in the version control system."
3472 (interactive "fVC delete file: ")
3473 (let ((buf (get-file-buffer file))
3474 (backend (vc-backend file)))
3475 (unless backend
3476 (error "File %s is not under version control"
3477 (file-name-nondirectory file)))
3478 (unless (vc-find-backend-function backend 'delete-file)
3479 (error "Deleting files under %s is not supported in VC" backend))
3480 (if (and buf (buffer-modified-p buf))
3481 (error "Please save files before deleting them"))
3482 (unless (y-or-n-p (format "Really want to delete %s? "
3483 (file-name-nondirectory file)))
3484 (error "Abort!"))
3485 (unless (or (file-directory-p file) (null make-backup-files))
3486 (with-current-buffer (or buf (find-file-noselect file))
3487 (let ((backup-inhibited nil))
3488 (backup-buffer))))
3489 (vc-call delete-file file)
3490 ;; If the backend hasn't deleted the file itself, let's do it for him.
3491 (if (file-exists-p file) (delete-file file))))
3493 ;;;###autoload
3494 (defun vc-rename-file (old new)
3495 "Rename file OLD to NEW, and rename its master file likewise."
3496 (interactive "fVC rename file: \nFRename to: ")
3497 (let ((oldbuf (get-file-buffer old)))
3498 (if (and oldbuf (buffer-modified-p oldbuf))
3499 (error "Please save files before moving them"))
3500 (if (get-file-buffer new)
3501 (error "Already editing new file name"))
3502 (if (file-exists-p new)
3503 (error "New file already exists"))
3504 (let ((state (vc-state old)))
3505 (unless (memq state '(up-to-date edited))
3506 (error "Please %s files before moving them"
3507 (if (stringp state) "check in" "update"))))
3508 (vc-call rename-file old new)
3509 (vc-file-clearprops old)
3510 ;; Move the actual file (unless the backend did it already)
3511 (if (file-exists-p old) (rename-file old new))
3512 ;; ?? Renaming a file might change its contents due to keyword expansion.
3513 ;; We should really check out a new copy if the old copy was precisely equal
3514 ;; to some checked-in revision. However, testing for this is tricky....
3515 (if oldbuf
3516 (with-current-buffer oldbuf
3517 (let ((buffer-read-only buffer-read-only))
3518 (set-visited-file-name new))
3519 (vc-backend new)
3520 (vc-mode-line new)
3521 (set-buffer-modified-p nil)))))
3523 ;;;###autoload
3524 (defun vc-update-change-log (&rest args)
3525 "Find change log file and add entries from recent version control logs.
3526 Normally, find log entries for all registered files in the default
3527 directory.
3529 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
3531 With any numeric prefix arg, find log entries for all currently visited
3532 files that are under version control. This puts all the entries in the
3533 log for the default directory, which may not be appropriate.
3535 From a program, any ARGS are assumed to be filenames for which
3536 log entries should be gathered."
3537 (interactive
3538 (cond ((consp current-prefix-arg) ;C-u
3539 (list buffer-file-name))
3540 (current-prefix-arg ;Numeric argument.
3541 (let ((files nil)
3542 (buffers (buffer-list))
3543 file)
3544 (while buffers
3545 (setq file (buffer-file-name (car buffers)))
3546 (and file (vc-backend file)
3547 (setq files (cons file files)))
3548 (setq buffers (cdr buffers)))
3549 files))
3551 ;; Don't supply any filenames to backend; this means
3552 ;; it should find all relevant files relative to
3553 ;; the default-directory.
3554 nil)))
3555 (vc-call-backend (vc-responsible-backend default-directory)
3556 'update-changelog args))
3558 ;;; The default back end. Assumes RCS-like revision numbering.
3560 (defun vc-default-revision-granularity ()
3561 (error "Your backend will not work with this version of VC mode."))
3563 ;; functions that operate on RCS revision numbers. This code should
3564 ;; also be moved into the backends. It stays for now, however, since
3565 ;; it is used in code below.
3566 ;;;###autoload
3567 (defun vc-trunk-p (rev)
3568 "Return t if REV is a revision on the trunk."
3569 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
3571 (defun vc-branch-p (rev)
3572 "Return t if REV is a branch revision."
3573 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
3575 ;;;###autoload
3576 (defun vc-branch-part (rev)
3577 "Return the branch part of a revision number REV."
3578 (let ((index (string-match "\\.[0-9]+\\'" rev)))
3579 (if index
3580 (substring rev 0 index))))
3582 (defun vc-minor-part (rev)
3583 "Return the minor revision number of a revision number REV."
3584 (string-match "[0-9]+\\'" rev)
3585 (substring rev (match-beginning 0) (match-end 0)))
3587 (defun vc-default-previous-revision (backend file rev)
3588 "Return the revision number immediately preceding REV for FILE,
3589 or nil if there is no previous revision. This default
3590 implementation works for MAJOR.MINOR-style revision numbers as
3591 used by RCS and CVS."
3592 (let ((branch (vc-branch-part rev))
3593 (minor-num (string-to-number (vc-minor-part rev))))
3594 (when branch
3595 (if (> minor-num 1)
3596 ;; revision does probably not start a branch or release
3597 (concat branch "." (number-to-string (1- minor-num)))
3598 (if (vc-trunk-p rev)
3599 ;; we are at the beginning of the trunk --
3600 ;; don't know anything to return here
3602 ;; we are at the beginning of a branch --
3603 ;; return revision of starting point
3604 (vc-branch-part branch))))))
3606 (defun vc-default-next-revision (backend file rev)
3607 "Return the revision number immediately following REV for FILE,
3608 or nil if there is no next revision. This default implementation
3609 works for MAJOR.MINOR-style revision numbers as used by RCS
3610 and CVS."
3611 (when (not (string= rev (vc-working-revision file)))
3612 (let ((branch (vc-branch-part rev))
3613 (minor-num (string-to-number (vc-minor-part rev))))
3614 (concat branch "." (number-to-string (1+ minor-num))))))
3616 (defun vc-default-responsible-p (backend file)
3617 "Indicate whether BACKEND is reponsible for FILE.
3618 The default is to return nil always."
3619 nil)
3621 (defun vc-default-could-register (backend file)
3622 "Return non-nil if BACKEND could be used to register FILE.
3623 The default implementation returns t for all files."
3626 (defun vc-default-latest-on-branch-p (backend file)
3627 "Return non-nil if FILE is the latest on its branch.
3628 This default implementation always returns non-nil, which means that
3629 editing non-current revisions is not supported by default."
3632 (defun vc-default-init-revision (backend) vc-default-init-revision)
3634 (defalias 'vc-cvs-update-changelog 'vc-update-changelog-rcs2log)
3635 (defalias 'vc-rcs-update-changelog 'vc-update-changelog-rcs2log)
3636 ;; FIXME: This should probably be moved to vc-rcs.el and replaced in
3637 ;; vc-cvs.el by code using cvs2cl.
3638 (defun vc-update-changelog-rcs2log (files)
3639 "Default implementation of update-changelog.
3640 Uses `rcs2log' which only works for RCS and CVS."
3641 ;; FIXME: We (c|sh)ould add support for cvs2cl
3642 (let ((odefault default-directory)
3643 (changelog (find-change-log))
3644 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
3645 (tempfile (make-temp-file
3646 (expand-file-name "vc"
3647 (or small-temporary-file-directory
3648 temporary-file-directory))))
3649 (login-name (or user-login-name
3650 (format "uid%d" (number-to-string (user-uid)))))
3651 (full-name (or add-log-full-name
3652 (user-full-name)
3653 (user-login-name)
3654 (format "uid%d" (number-to-string (user-uid)))))
3655 (mailing-address (or add-log-mailing-address
3656 user-mail-address)))
3657 (find-file-other-window changelog)
3658 (barf-if-buffer-read-only)
3659 (vc-buffer-sync)
3660 (undo-boundary)
3661 (goto-char (point-min))
3662 (push-mark)
3663 (message "Computing change log entries...")
3664 (message "Computing change log entries... %s"
3665 (unwind-protect
3666 (progn
3667 (setq default-directory odefault)
3668 (if (eq 0 (apply 'call-process
3669 (expand-file-name "rcs2log"
3670 exec-directory)
3671 nil (list t tempfile) nil
3672 "-c" changelog
3673 "-u" (concat login-name
3674 "\t" full-name
3675 "\t" mailing-address)
3676 (mapcar
3677 (lambda (f)
3678 (file-relative-name
3679 (expand-file-name f odefault)))
3680 files)))
3681 "done"
3682 (pop-to-buffer (get-buffer-create "*vc*"))
3683 (erase-buffer)
3684 (insert-file-contents tempfile)
3685 "failed"))
3686 (setq default-directory (file-name-directory changelog))
3687 (delete-file tempfile)))))
3689 (defun vc-default-find-revision (backend file rev buffer)
3690 "Provide the new `find-revision' op based on the old `checkout' op.
3691 This is only for compatibility with old backends. They should be updated
3692 to provide the `find-revision' operation instead."
3693 (let ((tmpfile (make-temp-file (expand-file-name file))))
3694 (unwind-protect
3695 (progn
3696 (vc-call-backend backend 'checkout file nil rev tmpfile)
3697 (with-current-buffer buffer
3698 (insert-file-contents-literally tmpfile)))
3699 (delete-file tmpfile))))
3701 (defun vc-default-dired-state-info (backend file)
3702 (let* ((state (vc-state file))
3703 (statestring
3704 (cond
3705 ((stringp state) (concat "(" state ")"))
3706 ((eq state 'edited) (concat "(" (vc-user-login-name file) ")"))
3707 ((eq state 'needs-merge) "(merge)")
3708 ((eq state 'needs-patch) "(patch)")
3709 ((eq state 'added) "(added)")
3710 ((eq state 'removed) "(removed)")
3711 ((eq state 'ignored) "(ignored)") ;; dired-hook filters this out
3712 ((eq state 'unregistered) "?")
3713 ((eq state 'unlocked-changes) "(stale)")
3714 ((not state) "(unknown)")))
3715 (buffer
3716 (get-file-buffer file))
3717 (modflag
3718 (if (and buffer (buffer-modified-p buffer)) "+" "")))
3719 (concat statestring modflag)))
3721 (defun vc-default-rename-file (backend old new)
3722 (condition-case nil
3723 (add-name-to-file old new)
3724 (error (rename-file old new)))
3725 (vc-delete-file old)
3726 (with-current-buffer (find-file-noselect new)
3727 (vc-register)))
3729 (defalias 'vc-default-logentry-check 'ignore)
3730 (defalias 'vc-default-check-headers 'ignore)
3732 (defun vc-default-log-view-mode (backend) (log-view-mode))
3734 (defun vc-default-show-log-entry (backend rev)
3735 (with-no-warnings
3736 (log-view-goto-rev rev)))
3738 (defun vc-default-comment-history (backend file)
3739 "Return a string with all log entries stored in BACKEND for FILE."
3740 (if (vc-find-backend-function backend 'print-log)
3741 (with-current-buffer "*vc*"
3742 (vc-call print-log (list file))
3743 (vc-call-backend backend 'wash-log)
3744 (buffer-string))))
3746 (defun vc-default-receive-file (backend file rev)
3747 "Let BACKEND receive FILE from another version control system."
3748 (vc-call-backend backend 'register file rev ""))
3750 (defun vc-default-create-snapshot (backend dir name branchp)
3751 (when branchp
3752 (error "VC backend %s does not support module branches" backend))
3753 (let ((result (vc-snapshot-precondition dir)))
3754 (if (stringp result)
3755 (error "File %s is not up-to-date" result)
3756 (vc-file-tree-walk
3758 (lambda (f)
3759 (vc-call assign-name f name))))))
3761 (defun vc-default-retrieve-snapshot (backend dir name update)
3762 (if (string= name "")
3763 (progn
3764 (vc-file-tree-walk
3766 (lambda (f) (and
3767 (vc-up-to-date-p f)
3768 (vc-error-occurred
3769 (vc-call checkout f nil "")
3770 (if update (vc-resynch-buffer f t t)))))))
3771 (let ((result (vc-snapshot-precondition dir)))
3772 (if (stringp result)
3773 (error "File %s is locked" result)
3774 (setq update (and (eq result 'visited) update))
3775 (vc-file-tree-walk
3777 (lambda (f) (vc-error-occurred
3778 (vc-call checkout f nil name)
3779 (if update (vc-resynch-buffer f t t)))))))))
3781 (defun vc-default-revert (backend file contents-done)
3782 (unless contents-done
3783 (let ((rev (vc-working-revision file))
3784 (file-buffer (or (get-file-buffer file) (current-buffer))))
3785 (message "Checking out %s..." file)
3786 (let ((failed t)
3787 (backup-name (car (find-backup-file-name file))))
3788 (when backup-name
3789 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
3790 (unless (file-writable-p file)
3791 (set-file-modes file (logior (file-modes file) 128))))
3792 (unwind-protect
3793 (let ((coding-system-for-read 'no-conversion)
3794 (coding-system-for-write 'no-conversion))
3795 (with-temp-file file
3796 (let ((outbuf (current-buffer)))
3797 ;; Change buffer to get local value of vc-checkout-switches.
3798 (with-current-buffer file-buffer
3799 (let ((default-directory (file-name-directory file)))
3800 (vc-call find-revision file rev outbuf)))))
3801 (setq failed nil))
3802 (when backup-name
3803 (if failed
3804 (rename-file backup-name file 'ok-if-already-exists)
3805 (and (not vc-make-backup-files) (delete-file backup-name))))))
3806 (message "Checking out %s...done" file))))
3808 (defalias 'vc-default-revision-completion-table 'ignore)
3810 (defun vc-check-headers ()
3811 "Check if the current file has any headers in it."
3812 (interactive)
3813 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3815 ;;; Annotate functionality
3817 ;; Declare globally instead of additional parameter to
3818 ;; temp-buffer-show-function (not possible to pass more than one
3819 ;; parameter). The use of annotate-ratio is deprecated in favor of
3820 ;; annotate-mode, which replaces it with the more sensible "span-to
3821 ;; days", along with autoscaling support.
3822 (defvar vc-annotate-ratio nil "Global variable.")
3824 ;; internal buffer-local variables
3825 (defvar vc-annotate-backend nil)
3826 (defvar vc-annotate-parent-file nil)
3827 (defvar vc-annotate-parent-rev nil)
3828 (defvar vc-annotate-parent-display-mode nil)
3830 (defconst vc-annotate-font-lock-keywords
3831 ;; The fontification is done by vc-annotate-lines instead of font-lock.
3832 '((vc-annotate-lines)))
3834 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
3835 "Major mode for output buffers of the `vc-annotate' command.
3837 You can use the mode-specific menu to alter the time-span of the used
3838 colors. See variable `vc-annotate-menu-elements' for customizing the
3839 menu items."
3840 ;; Frob buffer-invisibility-spec so that if it is originally a naked t,
3841 ;; it will become a list, to avoid initial annotations being invisible.
3842 (add-to-invisibility-spec 'foo)
3843 (remove-from-invisibility-spec 'foo)
3844 (set (make-local-variable 'truncate-lines) t)
3845 (set (make-local-variable 'font-lock-defaults)
3846 '(vc-annotate-font-lock-keywords t))
3847 (view-mode 1))
3849 (defun vc-annotate-toggle-annotation-visibility ()
3850 "Toggle whether or not the annotation is visible."
3851 (interactive)
3852 (funcall (if (memq 'vc-annotate-annotation buffer-invisibility-spec)
3853 'remove-from-invisibility-spec
3854 'add-to-invisibility-spec)
3855 'vc-annotate-annotation)
3856 (force-window-update (current-buffer)))
3858 (defun vc-annotate-display-default (ratio)
3859 "Display the output of \\[vc-annotate] using the default color range.
3860 The color range is given by `vc-annotate-color-map', scaled by RATIO.
3861 The current time is used as the offset."
3862 (interactive (progn (kill-local-variable 'vc-annotate-color-map) '(1.0)))
3863 (message "Redisplaying annotation...")
3864 (vc-annotate-display ratio)
3865 (message "Redisplaying annotation...done"))
3867 (defun vc-annotate-oldest-in-map (color-map)
3868 "Return the oldest time in the COLOR-MAP."
3869 ;; Since entries should be sorted, we can just use the last one.
3870 (caar (last color-map)))
3872 (defun vc-annotate-get-time-set-line-props ()
3873 (let ((bol (point))
3874 (date (vc-call-backend vc-annotate-backend 'annotate-time))
3875 (inhibit-read-only t))
3876 (assert (>= (point) bol))
3877 (put-text-property bol (point) 'invisible 'vc-annotate-annotation)
3878 date))
3880 (defun vc-annotate-display-autoscale (&optional full)
3881 "Highlight the output of \\[vc-annotate] using an autoscaled color map.
3882 Autoscaling means that the map is scaled from the current time to the
3883 oldest annotation in the buffer, or, with prefix argument FULL, to
3884 cover the range from the oldest annotation to the newest."
3885 (interactive "P")
3886 (let ((newest 0.0)
3887 (oldest 999999.) ;Any CVS users at the founding of Rome?
3888 (current (vc-annotate-convert-time (current-time)))
3889 date)
3890 (message "Redisplaying annotation...")
3891 ;; Run through this file and find the oldest and newest dates annotated.
3892 (save-excursion
3893 (goto-char (point-min))
3894 (while (not (eobp))
3895 (when (setq date (vc-annotate-get-time-set-line-props))
3896 (if (> date newest)
3897 (setq newest date))
3898 (if (< date oldest)
3899 (setq oldest date)))
3900 (forward-line 1)))
3901 (vc-annotate-display
3902 (/ (- (if full newest current) oldest)
3903 (vc-annotate-oldest-in-map vc-annotate-color-map))
3904 (if full newest))
3905 (message "Redisplaying annotation...done \(%s\)"
3906 (if full
3907 (format "Spanned from %.1f to %.1f days old"
3908 (- current oldest)
3909 (- current newest))
3910 (format "Spanned to %.1f days old" (- current oldest))))))
3912 ;; Menu -- Using easymenu.el
3913 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
3914 "VC Annotate Display Menu"
3915 `("VC-Annotate"
3916 ["By Color Map Range" (unless (null vc-annotate-display-mode)
3917 (setq vc-annotate-display-mode nil)
3918 (vc-annotate-display-select))
3919 :style toggle :selected (null vc-annotate-display-mode)]
3920 ,@(let ((oldest-in-map (vc-annotate-oldest-in-map vc-annotate-color-map)))
3921 (mapcar (lambda (element)
3922 (let ((days (* element oldest-in-map)))
3923 `[,(format "Span %.1f days" days)
3924 (vc-annotate-display-select nil ,days)
3925 :style toggle :selected
3926 (eql vc-annotate-display-mode ,days) ]))
3927 vc-annotate-menu-elements))
3928 ["Span ..."
3929 (vc-annotate-display-select
3930 nil (float (string-to-number (read-string "Span how many days? "))))]
3931 "--"
3932 ["Span to Oldest"
3933 (unless (eq vc-annotate-display-mode 'scale)
3934 (vc-annotate-display-select nil 'scale))
3935 :help
3936 "Use an autoscaled color map from the oldest annotation to the current time"
3937 :style toggle :selected
3938 (eq vc-annotate-display-mode 'scale)]
3939 ["Span Oldest->Newest"
3940 (unless (eq vc-annotate-display-mode 'fullscale)
3941 (vc-annotate-display-select nil 'fullscale))
3942 :help
3943 "Use an autoscaled color map from the oldest to the newest annotation"
3944 :style toggle :selected
3945 (eq vc-annotate-display-mode 'fullscale)]
3946 "--"
3947 ["Toggle annotation visibility" vc-annotate-toggle-annotation-visibility
3948 :help
3949 "Toggle whether the annotation is visible or not"]
3950 ["Annotate previous revision" vc-annotate-prev-revision
3951 :help "Visit the annotation of the revision previous to this one"]
3952 ["Annotate next revision" vc-annotate-next-revision
3953 :help "Visit the annotation of the revision after this one"]
3954 ["Annotate revision at line" vc-annotate-revision-at-line
3955 :help
3956 "Visit the annotation of the revision identified in the current line"]
3957 ["Annotate revision previous to line" vc-annotate-revision-previous-to-line
3958 :help "Visit the annotation of the revision before the revision at line"]
3959 ["Annotate latest revision" vc-annotate-working-revision
3960 :help "Visit the annotation of the working revision of this file"]
3961 ["Show log of revision at line" vc-annotate-show-log-revision-at-line
3962 :help "Visit the log of the revision at line"]
3963 ["Show diff of revision at line" vc-annotate-show-diff-revision-at-line
3964 :help
3965 "Visit the diff of the revision at line from its previous revision"]
3966 ["Visit revision at line" vc-annotate-find-revision-at-line
3967 :help "Visit the revision identified in the current line"]))
3969 (defun vc-annotate-display-select (&optional buffer mode)
3970 "Highlight the output of \\[vc-annotate].
3971 By default, the current buffer is highlighted, unless overridden by
3972 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
3973 use; you may override this using the second optional arg MODE."
3974 (interactive)
3975 (if mode (setq vc-annotate-display-mode mode))
3976 (pop-to-buffer (or buffer (current-buffer)))
3977 (cond ((null vc-annotate-display-mode)
3978 ;; The ratio is global, thus relative to the global color-map.
3979 (kill-local-variable 'vc-annotate-color-map)
3980 (vc-annotate-display-default (or vc-annotate-ratio 1.0)))
3981 ;; One of the auto-scaling modes
3982 ((eq vc-annotate-display-mode 'scale)
3983 (vc-exec-after `(vc-annotate-display-autoscale)))
3984 ((eq vc-annotate-display-mode 'fullscale)
3985 (vc-exec-after `(vc-annotate-display-autoscale t)))
3986 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
3987 (vc-annotate-display-default
3988 (/ vc-annotate-display-mode
3989 (vc-annotate-oldest-in-map vc-annotate-color-map))))
3990 (t (error "No such display mode: %s"
3991 vc-annotate-display-mode))))
3993 ;;;###autoload
3994 (defun vc-annotate (file rev &optional display-mode buf move-point-to)
3995 "Display the edit history of the current file using colors.
3997 This command creates a buffer that shows, for each line of the current
3998 file, when it was last edited and by whom. Additionally, colors are
3999 used to show the age of each line--blue means oldest, red means
4000 youngest, and intermediate colors indicate intermediate ages. By
4001 default, the time scale stretches back one year into the past;
4002 everything that is older than that is shown in blue.
4004 With a prefix argument, this command asks two questions in the
4005 minibuffer. First, you may enter a revision number; then the buffer
4006 displays and annotates that revision instead of the working revision
4007 \(type RET in the minibuffer to leave that default unchanged). Then,
4008 you are prompted for the time span in days which the color range
4009 should cover. For example, a time span of 20 days means that changes
4010 over the past 20 days are shown in red to blue, according to their
4011 age, and everything that is older than that is shown in blue.
4013 If MOVE-POINT-TO is given, move the point to that line.
4015 Customization variables:
4017 `vc-annotate-menu-elements' customizes the menu elements of the
4018 mode-specific menu. `vc-annotate-color-map' and
4019 `vc-annotate-very-old-color' define the mapping of time to colors.
4020 `vc-annotate-background' specifies the background color."
4021 (interactive
4022 (save-current-buffer
4023 (vc-ensure-vc-buffer)
4024 (list buffer-file-name
4025 (let ((def (vc-working-revision buffer-file-name)))
4026 (if (null current-prefix-arg) def
4027 (read-string
4028 (format "Annotate from revision (default %s): " def)
4029 nil nil def)))
4030 (if (null current-prefix-arg)
4031 vc-annotate-display-mode
4032 (float (string-to-number
4033 (read-string "Annotate span days (default 20): "
4034 nil nil "20")))))))
4035 (vc-ensure-vc-buffer)
4036 (setq vc-annotate-display-mode display-mode) ;Not sure why. --Stef
4037 (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
4038 (temp-buffer-show-function 'vc-annotate-display-select)
4039 ;; If BUF is specified, we presume the caller maintains current line,
4040 ;; so we don't need to do it here. This implementation may give
4041 ;; strange results occasionally in the case of REV != WORKFILE-REV.
4042 (current-line (or move-point-to (unless buf (line-number-at-pos)))))
4043 (message "Annotating...")
4044 ;; If BUF is specified it tells in which buffer we should put the
4045 ;; annotations. This is used when switching annotations to another
4046 ;; revision, so we should update the buffer's name.
4047 (if buf (with-current-buffer buf
4048 (rename-buffer temp-buffer-name t)
4049 ;; In case it had to be uniquified.
4050 (setq temp-buffer-name (buffer-name))))
4051 (with-output-to-temp-buffer temp-buffer-name
4052 (vc-call annotate-command file (get-buffer temp-buffer-name) rev)
4053 ;; we must setup the mode first, and then set our local
4054 ;; variables before the show-function is called at the exit of
4055 ;; with-output-to-temp-buffer
4056 (with-current-buffer temp-buffer-name
4057 (if (not (equal major-mode 'vc-annotate-mode))
4058 (vc-annotate-mode))
4059 (set (make-local-variable 'vc-annotate-backend) (vc-backend file))
4060 (set (make-local-variable 'vc-annotate-parent-file) file)
4061 (set (make-local-variable 'vc-annotate-parent-rev) rev)
4062 (set (make-local-variable 'vc-annotate-parent-display-mode)
4063 display-mode)))
4065 (with-current-buffer temp-buffer-name
4066 (vc-exec-after
4067 `(progn
4068 ;; Ideally, we'd rather not move point if the user has already
4069 ;; moved it elsewhere, but really point here is not the position
4070 ;; of the user's cursor :-(
4071 (when ,current-line ;(and (bobp))
4072 (goto-line ,current-line)
4073 (setq vc-sentinel-movepoint (point)))
4074 (unless (active-minibuffer-window)
4075 (message "Annotating... done")))))))
4077 (defun vc-annotate-prev-revision (prefix)
4078 "Visit the annotation of the revision previous to this one.
4080 With a numeric prefix argument, annotate the revision that many
4081 revisions previous."
4082 (interactive "p")
4083 (vc-annotate-warp-revision (- 0 prefix)))
4085 (defun vc-annotate-next-revision (prefix)
4086 "Visit the annotation of the revision after this one.
4088 With a numeric prefix argument, annotate the revision that many
4089 revisions after."
4090 (interactive "p")
4091 (vc-annotate-warp-revision prefix))
4093 (defun vc-annotate-working-revision ()
4094 "Visit the annotation of the working revision of this file."
4095 (interactive)
4096 (if (not (equal major-mode 'vc-annotate-mode))
4097 (message "Cannot be invoked outside of a vc annotate buffer")
4098 (let ((warp-rev (vc-working-revision vc-annotate-parent-file)))
4099 (if (equal warp-rev vc-annotate-parent-rev)
4100 (message "Already at revision %s" warp-rev)
4101 (vc-annotate-warp-revision warp-rev)))))
4103 (defun vc-annotate-extract-revision-at-line ()
4104 "Extract the revision number of the current line."
4105 ;; This function must be invoked from a buffer in vc-annotate-mode
4106 (vc-call-backend vc-annotate-backend 'annotate-extract-revision-at-line))
4108 (defun vc-annotate-revision-at-line ()
4109 "Visit the annotation of the revision identified in the current line."
4110 (interactive)
4111 (if (not (equal major-mode 'vc-annotate-mode))
4112 (message "Cannot be invoked outside of a vc annotate buffer")
4113 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4114 (if (not rev-at-line)
4115 (message "Cannot extract revision number from the current line")
4116 (if (equal rev-at-line vc-annotate-parent-rev)
4117 (message "Already at revision %s" rev-at-line)
4118 (vc-annotate-warp-revision rev-at-line))))))
4120 (defun vc-annotate-find-revision-at-line ()
4121 "Visit the revision identified in the current line."
4122 (interactive)
4123 (if (not (equal major-mode 'vc-annotate-mode))
4124 (message "Cannot be invoked outside of a vc annotate buffer")
4125 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4126 (if (not rev-at-line)
4127 (message "Cannot extract revision number from the current line")
4128 (vc-revision-other-window rev-at-line)))))
4130 (defun vc-annotate-revision-previous-to-line ()
4131 "Visit the annotation of the revision before the revision at line."
4132 (interactive)
4133 (if (not (equal major-mode 'vc-annotate-mode))
4134 (message "Cannot be invoked outside of a vc annotate buffer")
4135 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
4136 (prev-rev nil))
4137 (if (not rev-at-line)
4138 (message "Cannot extract revision number from the current line")
4139 (setq prev-rev
4140 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
4141 (vc-annotate-warp-revision prev-rev)))))
4143 (defun vc-annotate-show-log-revision-at-line ()
4144 "Visit the log of the revision at line."
4145 (interactive)
4146 (if (not (equal major-mode 'vc-annotate-mode))
4147 (message "Cannot be invoked outside of a vc annotate buffer")
4148 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4149 (if (not rev-at-line)
4150 (message "Cannot extract revision number from the current line")
4151 (vc-print-log rev-at-line)))))
4153 (defun vc-annotate-show-diff-revision-at-line ()
4154 "Visit the diff of the revision at line from its previous revision."
4155 (interactive)
4156 (if (not (equal major-mode 'vc-annotate-mode))
4157 (message "Cannot be invoked outside of a vc annotate buffer")
4158 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
4159 (prev-rev nil))
4160 (if (not rev-at-line)
4161 (message "Cannot extract revision number from the current line")
4162 (setq prev-rev
4163 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
4164 (if (not prev-rev)
4165 (message "Cannot diff from any revision prior to %s" rev-at-line)
4166 (save-window-excursion
4167 (vc-diff-internal nil (list vc-annotate-parent-file)
4168 prev-rev rev-at-line))
4169 (switch-to-buffer "*vc-diff*"))))))
4171 (defun vc-annotate-warp-revision (revspec)
4172 "Annotate the revision described by REVSPEC.
4174 If REVSPEC is a positive integer, warp that many revisions
4175 forward, if possible, otherwise echo a warning message. If
4176 REVSPEC is a negative integer, warp that many revisions backward,
4177 if possible, otherwise echo a warning message. If REVSPEC is a
4178 string, then it describes a revision number, so warp to that
4179 revision."
4180 (if (not (equal major-mode 'vc-annotate-mode))
4181 (message "Cannot be invoked outside of a vc annotate buffer")
4182 (let* ((buf (current-buffer))
4183 (oldline (line-number-at-pos))
4184 (revspeccopy revspec)
4185 (newrev nil))
4186 (cond
4187 ((and (integerp revspec) (> revspec 0))
4188 (setq newrev vc-annotate-parent-rev)
4189 (while (and (> revspec 0) newrev)
4190 (setq newrev (vc-call next-revision
4191 vc-annotate-parent-file newrev))
4192 (setq revspec (1- revspec)))
4193 (if (not newrev)
4194 (message "Cannot increment %d revisions from revision %s"
4195 revspeccopy vc-annotate-parent-rev)))
4196 ((and (integerp revspec) (< revspec 0))
4197 (setq newrev vc-annotate-parent-rev)
4198 (while (and (< revspec 0) newrev)
4199 (setq newrev (vc-call previous-revision
4200 vc-annotate-parent-file newrev))
4201 (setq revspec (1+ revspec)))
4202 (if (not newrev)
4203 (message "Cannot decrement %d revisions from revision %s"
4204 (- 0 revspeccopy) vc-annotate-parent-rev)))
4205 ((stringp revspec) (setq newrev revspec))
4206 (t (error "Invalid argument to vc-annotate-warp-revision")))
4207 (when newrev
4208 (vc-annotate vc-annotate-parent-file newrev
4209 vc-annotate-parent-display-mode
4211 ;; Pass the current line so that vc-annotate will
4212 ;; place the point in the line.
4213 (min oldline (progn (goto-char (point-max))
4214 (forward-line -1)
4215 (line-number-at-pos))))))))
4217 (defun vc-annotate-compcar (threshold a-list)
4218 "Test successive cons cells of A-LIST against THRESHOLD.
4219 Return the first cons cell with a car that is not less than THRESHOLD,
4220 nil if no such cell exists."
4221 (let ((i 1)
4222 (tmp-cons (car a-list)))
4223 (while (and tmp-cons (< (car tmp-cons) threshold))
4224 (setq tmp-cons (car (nthcdr i a-list)))
4225 (setq i (+ i 1)))
4226 tmp-cons)) ; Return the appropriate value
4228 (defun vc-annotate-convert-time (time)
4229 "Convert a time value to a floating-point number of days.
4230 The argument TIME is a list as returned by `current-time' or
4231 `encode-time', only the first two elements of that list are considered."
4232 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
4234 (defun vc-annotate-difference (&optional offset)
4235 "Return the time span in days to the next annotation.
4236 This calls the backend function annotate-time, and returns the
4237 difference in days between the time returned and the current time,
4238 or OFFSET if present."
4239 (let ((next-time (vc-annotate-get-time-set-line-props)))
4240 (if next-time
4241 (- (or offset
4242 (vc-call-backend vc-annotate-backend 'annotate-current-time))
4243 next-time))))
4245 (defun vc-default-annotate-current-time (backend)
4246 "Return the current time, encoded as fractional days."
4247 (vc-annotate-convert-time (current-time)))
4249 (defvar vc-annotate-offset nil)
4251 (defun vc-annotate-display (ratio &optional offset)
4252 "Highlight `vc-annotate' output in the current buffer.
4253 RATIO, is the expansion that should be applied to `vc-annotate-color-map'.
4254 The annotations are relative to the current time, unless overridden by OFFSET."
4255 (if (/= ratio 1.0)
4256 (set (make-local-variable 'vc-annotate-color-map)
4257 (mapcar (lambda (elem) (cons (* (car elem) ratio) (cdr elem)))
4258 vc-annotate-color-map)))
4259 (set (make-local-variable 'vc-annotate-offset) offset)
4260 (font-lock-mode 1))
4262 (defun vc-annotate-lines (limit)
4263 (while (< (point) limit)
4264 (let ((difference (vc-annotate-difference vc-annotate-offset))
4265 (start (point))
4266 (end (progn (forward-line 1) (point))))
4267 (when difference
4268 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
4269 (cons nil vc-annotate-very-old-color)))
4270 ;; substring from index 1 to remove any leading `#' in the name
4271 (face-name (concat "vc-annotate-face-"
4272 (if (string-equal
4273 (substring (cdr color) 0 1) "#")
4274 (substring (cdr color) 1)
4275 (cdr color))))
4276 ;; Make the face if not done.
4277 (face (or (intern-soft face-name)
4278 (let ((tmp-face (make-face (intern face-name))))
4279 (set-face-foreground tmp-face (cdr color))
4280 (if vc-annotate-background
4281 (set-face-background tmp-face
4282 vc-annotate-background))
4283 tmp-face)))) ; Return the face
4284 (put-text-property start end 'face face)))))
4285 ;; Pretend to font-lock there were no matches.
4286 nil)
4289 ;; Set up key bindings for use while editing log messages
4291 (defun vc-log-edit (fileset)
4292 "Set up `log-edit' for use with VC on FILE."
4293 (setq default-directory
4294 (with-current-buffer vc-parent-buffer default-directory))
4295 (log-edit 'vc-finish-logentry
4297 `((log-edit-listfun . (lambda () ',fileset))
4298 (log-edit-diff-function . (lambda () (vc-diff nil)))))
4299 (set (make-local-variable 'vc-log-fileset) fileset)
4300 (make-local-variable 'vc-log-revision)
4301 (set-buffer-modified-p nil)
4302 (setq buffer-file-name nil))
4304 ;; These things should probably be generally available
4306 (defun vc-file-tree-walk (dirname func &rest args)
4307 "Walk recursively through DIRNAME.
4308 Invoke FUNC f ARGS on each VC-managed file f underneath it."
4309 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
4310 (message "Traversing directory %s...done" dirname))
4312 (defun vc-file-tree-walk-internal (file func args)
4313 (if (not (file-directory-p file))
4314 (if (vc-backend file) (apply func file args))
4315 (message "Traversing directory %s..." (abbreviate-file-name file))
4316 (let ((dir (file-name-as-directory file)))
4317 (mapcar
4318 (lambda (f) (or
4319 (string-equal f ".")
4320 (string-equal f "..")
4321 (member f vc-directory-exclusion-list)
4322 (let ((dirf (expand-file-name f dir)))
4324 (file-symlink-p dirf) ;; Avoid possible loops.
4325 (vc-file-tree-walk-internal dirf func args)))))
4326 (directory-files dir)))))
4328 (provide 'vc)
4330 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
4332 ;; These may be useful to anyone who has to debug or extend the package.
4333 ;; (Note that this information corresponds to versions 5.x. Some of it
4334 ;; might have been invalidated by the additions to support branching
4335 ;; and RCS keyword lookup. AS, 1995/03/24)
4337 ;; A fundamental problem in VC is that there are time windows between
4338 ;; vc-next-action's computations of the file's version-control state and
4339 ;; the actions that change it. This is a window open to lossage in a
4340 ;; multi-user environment; someone else could nip in and change the state
4341 ;; of the master during it.
4343 ;; The performance problem is that rlog/prs calls are very expensive; we want
4344 ;; to avoid them as much as possible.
4346 ;; ANALYSIS:
4348 ;; The performance problem, it turns out, simplifies in practice to the
4349 ;; problem of making vc-state fast. The two other functions that call
4350 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
4351 ;; makes snapshots, the other deletes the calling user's last change in the
4352 ;; master.
4354 ;; The race condition implies that we have to either (a) lock the master
4355 ;; during the entire execution of vc-next-action, or (b) detect and
4356 ;; recover from errors resulting from dispatch on an out-of-date state.
4358 ;; Alternative (a) appears to be infeasible. The problem is that we can't
4359 ;; guarantee that the lock will ever be removed. Suppose a user starts a
4360 ;; checkin, the change message buffer pops up, and the user, having wandered
4361 ;; off to do something else, simply forgets about it?
4363 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
4364 ;; vc-state. Usually, if a file is registered, we can read its locked/
4365 ;; unlocked state and its current owner from its permissions.
4367 ;; This shortcut will fail if someone has manually changed the workfile's
4368 ;; permissions; also if developers are munging the workfile in several
4369 ;; directories, with symlinks to a master (in this latter case, the
4370 ;; permissions shortcut will fail to detect a lock asserted from another
4371 ;; directory).
4373 ;; Note that these cases correspond exactly to the errors which could happen
4374 ;; because of a competing checkin/checkout race in between two instances of
4375 ;; vc-next-action.
4377 ;; For VC's purposes, a workfile/master pair may have the following states:
4379 ;; A. Unregistered. There is a workfile, there is no master.
4381 ;; B. Registered and not locked by anyone.
4383 ;; C. Locked by calling user and unchanged.
4385 ;; D. Locked by the calling user and changed.
4387 ;; E. Locked by someone other than the calling user.
4389 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
4391 ;; VC's idea of state
4392 ;; |
4393 ;; V Actual state RCS action SCCS action Effect
4394 ;; A B C D E
4395 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
4396 ;; B 5 . 6 7 8 co -l get -e checkout
4397 ;; C 9 10 . 11 12 co -u unget; get revert
4398 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
4399 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
4401 ;; All commands take the master file name as a last argument (not shown).
4403 ;; In the discussion below, a "self-race" is a pathological situation in
4404 ;; which VC operations are being attempted simultaneously by two or more
4405 ;; Emacsen running under the same username.
4407 ;; The vc-next-action code has the following windows:
4409 ;; Window P:
4410 ;; Between the check for existence of a master file and the call to
4411 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
4412 ;; never close if the initial-comment feature is on.
4414 ;; Window Q:
4415 ;; Between the call to vc-workfile-unchanged-p in and the immediately
4416 ;; following revert (apparent state C).
4418 ;; Window R:
4419 ;; Between the call to vc-workfile-unchanged-p in and the following
4420 ;; checkin (apparent state D). This window may never close.
4422 ;; Window S:
4423 ;; Between the unlock and the immediately following checkout during a
4424 ;; revert operation (apparent state C). Included in window Q.
4426 ;; Window T:
4427 ;; Between vc-state and the following checkout (apparent state B).
4429 ;; Window U:
4430 ;; Between vc-state and the following revert (apparent state C).
4431 ;; Includes windows Q and S.
4433 ;; Window V:
4434 ;; Between vc-state and the following checkin (apparent state
4435 ;; D). This window may never be closed if the user fails to complete the
4436 ;; checkin message. Includes window R.
4438 ;; Window W:
4439 ;; Between vc-state and the following steal-lock (apparent
4440 ;; state E). This window may never close if the user fails to complete
4441 ;; the steal-lock message. Includes window X.
4443 ;; Window X:
4444 ;; Between the unlock and the immediately following re-lock during a
4445 ;; steal-lock operation (apparent state E). This window may never close
4446 ;; if the user fails to complete the steal-lock message.
4448 ;; Errors:
4450 ;; Apparent state A ---
4452 ;; 1. File looked unregistered but is actually registered and not locked.
4454 ;; Potential cause: someone else's admin during window P, with
4455 ;; caller's admin happening before their checkout.
4457 ;; RCS: Prior to version 5.6.4, ci fails with message
4458 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
4459 ;; ci -i option and the message is "<file>,v: already exists".
4460 ;; SCCS: admin will fail with error (ad19).
4462 ;; We can let these errors be passed up to the user.
4464 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
4466 ;; Potential cause: self-race during window P.
4468 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
4469 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
4470 ;; ci -i option, failing with message "<file>,v: already exists".
4471 ;; SCCS: will fail with error (ad19).
4473 ;; Either of these consequences is acceptable.
4475 ;; 3. File looked unregistered but is actually locked by caller, changed.
4477 ;; Potential cause: self-race during window P.
4479 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
4480 ;; a delta with a null change comment (the -t- switch will be
4481 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
4482 ;; failing with message "<file>,v: already exists".
4483 ;; SCCS: will fail with error (ad19).
4485 ;; 4. File looked unregistered but is locked by someone else.
4487 ;; Potential cause: someone else's admin during window P, with
4488 ;; caller's admin happening *after* their checkout.
4490 ;; RCS: Prior to version 5.6.4, ci fails with a
4491 ;; "no lock set by <user>" message. From 5.6.4 onwards,
4492 ;; VC uses the new ci -i option, failing with message
4493 ;; "<file>,v: already exists".
4494 ;; SCCS: will fail with error (ad19).
4496 ;; We can let these errors be passed up to the user.
4498 ;; Apparent state B ---
4500 ;; 5. File looked registered and not locked, but is actually unregistered.
4502 ;; Potential cause: master file got nuked during window P.
4504 ;; RCS: will fail with "RCS/<file>: No such file or directory"
4505 ;; SCCS: will fail with error ut4.
4507 ;; We can let these errors be passed up to the user.
4509 ;; 6. File looked registered and not locked, but is actually locked by the
4510 ;; calling user and unchanged.
4512 ;; Potential cause: self-race during window T.
4514 ;; RCS: in the same directory as the previous workfile, co -l will fail
4515 ;; with "co error: writable foo exists; checkout aborted". In any other
4516 ;; directory, checkout will succeed.
4517 ;; SCCS: will fail with ge17.
4519 ;; Either of these consequences is acceptable.
4521 ;; 7. File looked registered and not locked, but is actually locked by the
4522 ;; calling user and changed.
4524 ;; As case 6.
4526 ;; 8. File looked registered and not locked, but is actually locked by another
4527 ;; user.
4529 ;; Potential cause: someone else checks it out during window T.
4531 ;; RCS: co error: revision 1.3 already locked by <user>
4532 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
4534 ;; We can let these errors be passed up to the user.
4536 ;; Apparent state C ---
4538 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
4540 ;; As case 5.
4542 ;; 10. File looks locked by calling user and unchanged, but is actually not
4543 ;; locked.
4545 ;; Potential cause: a self-race in window U, or by the revert's
4546 ;; landing during window X of some other user's steal-lock or window S
4547 ;; of another user's revert.
4549 ;; RCS: succeeds, refreshing the file from the identical version in
4550 ;; the master.
4551 ;; SCCS: fails with error ut4 (p file nonexistent).
4553 ;; Either of these consequences is acceptable.
4555 ;; 11. File is locked by calling user. It looks unchanged, but is actually
4556 ;; changed.
4558 ;; Potential cause: the file would have to be touched by a self-race
4559 ;; during window Q.
4561 ;; The revert will succeed, removing whatever changes came with
4562 ;; the touch. It is theoretically possible that work could be lost.
4564 ;; 12. File looks like it's locked by the calling user and unchanged, but
4565 ;; it's actually locked by someone else.
4567 ;; Potential cause: a steal-lock in window V.
4569 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
4570 ;; SCCS: fails with error un2
4572 ;; We can pass these errors up to the user.
4574 ;; Apparent state D ---
4576 ;; 13. File looks like it's locked by the calling user and changed, but it's
4577 ;; actually unregistered.
4579 ;; Potential cause: master file got nuked during window P.
4581 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
4582 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
4583 ;; option, failing with message "no such file or directory".
4584 ;; SCCS: will fail with error ut4.
4586 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
4587 ;; VC may fail to detect the loss of previous version information.
4589 ;; 14. File looks like it's locked by the calling user and changed, but it's
4590 ;; actually unlocked.
4592 ;; Potential cause: self-race in window V, or the checkin happening
4593 ;; during the window X of someone else's steal-lock or window S of
4594 ;; someone else's revert.
4596 ;; RCS: ci will fail with "no lock set by <user>".
4597 ;; SCCS: delta will fail with error ut4.
4599 ;; 15. File looks like it's locked by the calling user and changed, but it's
4600 ;; actually locked by the calling user and unchanged.
4602 ;; Potential cause: another self-race --- a whole checkin/checkout
4603 ;; sequence by the calling user would have to land in window R.
4605 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
4606 ;; RCS: reverts to the file state as of the second user's checkin, leaving
4607 ;; the file unlocked.
4609 ;; It is theoretically possible that work could be lost under RCS.
4611 ;; 16. File looks like it's locked by the calling user and changed, but it's
4612 ;; actually locked by a different user.
4614 ;; RCS: ci error: no lock set by <user>
4615 ;; SCCS: unget will fail with error un2
4617 ;; We can pass these errors up to the user.
4619 ;; Apparent state E ---
4621 ;; 17. File looks like it's locked by some other user, but it's actually
4622 ;; unregistered.
4624 ;; As case 13.
4626 ;; 18. File looks like it's locked by some other user, but it's actually
4627 ;; unlocked.
4629 ;; Potential cause: someone released a lock during window W.
4631 ;; RCS: The calling user will get the lock on the file.
4632 ;; SCCS: unget -n will fail with cm4.
4634 ;; Either of these consequences will be OK.
4636 ;; 19. File looks like it's locked by some other user, but it's actually
4637 ;; locked by the calling user and unchanged.
4639 ;; Potential cause: the other user relinquishing a lock followed by
4640 ;; a self-race, both in window W.
4642 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
4643 ;; the sequence a no-op.
4645 ;; 20. File looks like it's locked by some other user, but it's actually
4646 ;; locked by the calling user and changed.
4648 ;; As case 19.
4650 ;; PROBLEM CASES:
4652 ;; In order of decreasing severity:
4654 ;; Cases 11 and 15 are the only ones that potentially lose work.
4655 ;; They would require a self-race for this to happen.
4657 ;; Case 13 in RCS loses information about previous deltas, retaining
4658 ;; only the information in the current workfile. This can only happen
4659 ;; if the master file gets nuked in window P.
4661 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
4662 ;; no change comment in the master. This would require a self-race in
4663 ;; window P or R respectively.
4665 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
4667 ;; Unfortunately, it appears to me that no recovery is possible in these
4668 ;; cases. They don't yield error messages, so there's no way to tell that
4669 ;; a race condition has occurred.
4671 ;; All other cases don't change either the workfile or the master, and
4672 ;; trigger command errors which the user will see.
4674 ;; Thus, there is no explicit recovery code.
4676 ;; arch-tag: ca82c1de-3091-4e26-af92-460abc6213a6
4677 ;;; vc.el ends here