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