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