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