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