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