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