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