Trailing whitespace deleted.
[bpt/emacs.git] / lisp / vc.el
CommitLineData
66321b2f 1;;; vc.el --- drive a version-control system from within Emacs
594722a8 2
891b8b69 3;; Copyright (C) 1992,93,94,95,96,97,98,2000,2001 Free Software Foundation, Inc.
594722a8 4
0e362f54
GM
5;; Author: FSF (see below for full credits)
6;; Maintainer: Andre Spiegel <spiegel@gnu.org>
284b3043 7;; Keywords: tools
594722a8 8
f1180544 9;; $Id: vc.el,v 1.347 2002/12/26 14:04:21 spiegel Exp $
045e1aa5 10
594722a8
ER
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
15;; the Free Software Foundation; either version 2, or (at your option)
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
EN
24;; along with GNU Emacs; see the file COPYING. If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
594722a8 27
0e362f54
GM
28;;; Credits:
29
30;; VC was initially designed and implemented by Eric S. Raymond
31;; <esr@snark.thyrsus.com>. Over the years, many people have
32;; contributed substantial amounts of work to VC. These include:
33;; Per Cederqvist <ceder@lysator.liu.se>
34;; Paul Eggert <eggert@twinsun.com>
35;; Sebastian Kremer <sk@thp.uni-koeln.de>
36;; Martin Lorentzson <martinl@gnu.org>
166a0ef7 37;; Dave Love <fx@gnu.org>
0e362f54 38;; Stefan Monnier <monnier@cs.yale.edu>
6f1ecae4 39;; J.D. Smith <jdsmith@alum.mit.edu>
0e362f54
GM
40;; Andre Spiegel <spiegel@gnu.org>
41;; Richard Stallman <rms@gnu.org>
6f1ecae4 42;; Thien-Thi Nguyen <ttn@gnu.org>
0e362f54 43
594722a8
ER
44;;; Commentary:
45
1a2f456b
ER
46;; This mode is fully documented in the Emacs user's manual.
47;;
632e9525 48;; Supported version-control systems presently include SCCS, RCS, and CVS.
b0c9bc8c
AS
49;;
50;; Some features will not work with old RCS versions. Where
51;; appropriate, VC finds out which version you have, and allows or
0e362f54 52;; disallows those features (stealing locks, for example, works only
b0c9bc8c 53;; from 5.6.2 onwards).
632e9525
RS
54;; Even initial checkins will fail if your RCS version is so old that ci
55;; doesn't understand -t-; this has been known to happen to people running
0e362f54 56;; NExTSTEP 3.0.
594722a8 57;;
0e362f54 58;; You can support the RCS -x option by customizing vc-rcs-master-templates.
594722a8
ER
59;;
60;; Proper function of the SCCS diff commands requires the shellscript vcdiff
61;; to be installed somewhere on Emacs's path for executables.
62;;
1a2f456b 63;; If your site uses the ChangeLog convention supported by Emacs, the
594be62e 64;; function vc-comment-to-change-log should prove a useful checkin hook.
1a2f456b 65;;
594722a8
ER
66;; The vc code maintains some internal state in order to reduce expensive
67;; version-control operations to a minimum. Some names are only computed
34291cd2 68;; once. If you perform version control operations with RCS/SCCS/CVS while
594722a8
ER
69;; vc's back is turned, or move/rename master files while vc is running,
70;; vc may get seriously confused. Don't do these things!
71;;
72;; Developer's notes on some concurrency issues are included at the end of
73;; the file.
0e362f54 74;;
fcb500ea
AS
75;; ADDING SUPPORT FOR OTHER BACKENDS
76;;
77;; VC can use arbitrary version control systems as a backend. To add
78;; support for a new backend named SYS, write a library vc-sys.el that
79;; contains functions of the form `vc-sys-...' (note that SYS is in lower
80;; case for the function and library names). VC will use that library if
81;; you put the symbol SYS somewhere into the list of
82;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
83;; returns non-nil for a file, all SYS-specific versions of VC commands
84;; will be available for that file.
85;;
86;; VC keeps some per-file information in the form of properties (see
87;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
88;; do not generally need to be aware of these properties. For example,
89;; `vc-sys-workfile-version' should compute the workfile version and
90;; return it; it should not look it up in the property, and it needn't
91;; store it there either. However, if a backend-specific function does
92;; store a value in a property, that value takes precedence over any
99cb8c8b 93;; value that the generic code might want to set (check for uses of
fcb500ea
AS
94;; the macro `with-vc-properties' in vc.el).
95;;
96;; In the list of functions below, each identifier needs to be prepended
97;; with `vc-sys-'. Some of the functions are mandatory (marked with a
98;; `*'), others are optional (`-').
99;;
100;; STATE-QUERYING FUNCTIONS
101;;
0e362f54 102;; * registered (file)
fcb500ea
AS
103;;
104;; Return non-nil if FILE is registered in this backend.
105;;
99cb8c8b 106;; * state (file)
fcb500ea
AS
107;;
108;; Return the current version control state of FILE. For a list of
109;; possible values, see `vc-state'. This function should do a full and
110;; reliable state computation; it is usually called immediately after
111;; C-x v v. If you want to use a faster heuristic when visiting a
112;; file, put that into `state-heuristic' below.
113;;
0e362f54 114;; - state-heuristic (file)
fcb500ea
AS
115;;
116;; If provided, this function is used to estimate the version control
117;; state of FILE at visiting time. It should be considerably faster
118;; than the implementation of `state'. For a list of possible values,
119;; see the doc string of `vc-state'.
120;;
0e362f54 121;; - dir-state (dir)
fcb500ea
AS
122;;
123;; If provided, this function is used to find the version control state
124;; of all files in DIR in a fast way. The function should not return
125;; anything, but rather store the files' states into the corresponding
126;; `vc-state' properties.
127;;
aae91380 128;; * workfile-version (file)
fcb500ea
AS
129;;
130;; Return the current workfile version of FILE.
131;;
132;; - latest-on-branch-p (file)
133;;
134;; Return non-nil if the current workfile version of FILE is the latest
135;; on its branch. The default implementation always returns t, which
136;; means that working with non-current versions is not supported by
137;; default.
138;;
0e362f54 139;; * checkout-model (file)
fcb500ea
AS
140;;
141;; Indicate whether FILE needs to be "checked out" before it can be
142;; edited. See `vc-checkout-model' for a list of possible values.
143;;
aae91380 144;; - workfile-unchanged-p (file)
fcb500ea
AS
145;;
146;; Return non-nil if FILE is unchanged from its current workfile
147;; version. This function should do a brief comparison of FILE's
148;; contents with those of the master version. If the backend does not
149;; have such a brief-comparison feature, the default implementation of
150;; this function can be used, which delegates to a full
f1180544 151;; vc-BACKEND-diff. (Note that vc-BACKEND-diff must not run
655388b5 152;; asynchronously in this case.)
fcb500ea 153;;
0e362f54 154;; - mode-line-string (file)
fcb500ea
AS
155;;
156;; If provided, this function should return the VC-specific mode line
157;; string for FILE. The default implementation deals well with all
158;; states that `vc-state' can return.
159;;
aae91380 160;; - dired-state-info (file)
fcb500ea
AS
161;;
162;; Translate the `vc-state' property of FILE into a string that can be
163;; used in a vc-dired buffer. The default implementation deals well
164;; with all states that `vc-state' can return.
165;;
166;; STATE-CHANGING FUNCTIONS
167;;
168;; * register (file &optional rev comment)
169;;
170;; Register FILE in this backend. Optionally, an initial revision REV
171;; and an initial description of the file, COMMENT, may be specified.
ecd50f65
AS
172;; The implementation should pass the value of vc-register-switches
173;; to the backend command.
fcb500ea 174;;
b470cb65
AS
175;; - init-version (file)
176;;
177;; The initial version to use when registering FILE if one is not
178;; specified by the user. If not provided, the variable
179;; vc-default-init-version is used instead.
180;;
099bd78a 181;; - responsible-p (file)
fcb500ea
AS
182;;
183;; Return non-nil if this backend considers itself "responsible" for
184;; FILE, which can also be a directory. This function is used to find
185;; out what backend to use for registration of new files and for things
186;; like change log generation. The default implementation always
187;; returns nil.
188;;
0e362f54 189;; - could-register (file)
fcb500ea
AS
190;;
191;; Return non-nil if FILE could be registered under this backend. The
192;; default implementation always returns t.
193;;
aae91380 194;; - receive-file (file rev)
fcb500ea
AS
195;;
196;; Let this backend "receive" a file that is already registered under
197;; another backend. The default implementation simply calls `register'
198;; for FILE, but it can be overridden to do something more specific,
199;; e.g. keep revision numbers consistent or choose editing modes for
200;; FILE that resemble those of the other backend.
201;;
202;; - unregister (file)
203;;
204;; Unregister FILE from this backend. This is only needed if this
205;; backend may be used as a "more local" backend for temporary editing.
206;;
aae91380 207;; * checkin (file rev comment)
fcb500ea
AS
208;;
209;; Commit changes in FILE to this backend. If REV is non-nil, that
210;; should become the new revision number. COMMENT is used as a
ecd50f65
AS
211;; check-in comment. The implementation should pass the value of
212;; vc-checkin-switches to the backend command.
fcb500ea 213;;
fd8160f7
SM
214;; * find-version (file rev buffer)
215;;
216;; Fetch revision REV of file FILE and put it into BUFFER.
217;; If REV is the empty string, fetch the head of the trunk.
218;; The implementation should pass the value of vc-checkout-switches
219;; to the backend command.
220;;
221;; * checkout (file &optional editable rev)
fcb500ea
AS
222;;
223;; Check out revision REV of FILE into the working area. If EDITABLE
224;; is non-nil, FILE should be writable by the user and if locking is
225;; used for FILE, a lock should also be set. If REV is non-nil, that
4279775b
AS
226;; is the revision to check out (default is current workfile version).
227;; If REV is t, that means to check out the head of the current branch;
228;; if it is the empty string, check out the head of the trunk.
229;; The implementation should pass the value of vc-checkout-switches
fd8160f7 230;; to the backend command.
fcb500ea 231;;
bbfc07d3 232;; * revert (file &optional contents-done)
fcb500ea 233;;
bbfc07d3
AS
234;; Revert FILE back to the current workfile version. If optional
235;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
236;; already been reverted from a version backup, and this function
237;; only needs to update the status of FILE within the backend.
fcb500ea
AS
238;;
239;; - cancel-version (file editable)
240;;
241;; Cancel the current workfile version of FILE, i.e. remove it from the
242;; master. EDITABLE non-nil means that FILE should be writable
243;; afterwards, and if locking is used for FILE, then a lock should also
244;; be set. If this function is not provided, trying to cancel a
245;; version is caught as an error.
246;;
aae91380 247;; - merge (file rev1 rev2)
fcb500ea
AS
248;;
249;; Merge the changes between REV1 and REV2 into the current working file.
250;;
aae91380 251;; - merge-news (file)
fcb500ea
AS
252;;
253;; Merge recent changes from the current branch into FILE.
254;;
aae91380 255;; - steal-lock (file &optional version)
fcb500ea
AS
256;;
257;; Steal any lock on the current workfile version of FILE, or on
258;; VERSION if that is provided. This function is only needed if
259;; locking is used for files under this backend, and if files can
260;; indeed be locked by other users.
261;;
262;; HISTORY FUNCTIONS
263;;
aae91380 264;; * print-log (file)
fcb500ea 265;;
ad339989 266;; Insert the revision log of FILE into the *vc* buffer.
fcb500ea 267;;
aae91380 268;; - show-log-entry (version)
fcb500ea
AS
269;;
270;; If provided, search the log entry for VERSION in the current buffer,
271;; and make sure it is displayed in the buffer's window. The default
272;; implementation of this function works for RCS-style logs.
273;;
aae91380 274;; - wash-log (file)
fcb500ea
AS
275;;
276;; Remove all non-comment information from the output of print-log. The
277;; default implementation of this function works for RCS-style logs.
278;;
0e362f54 279;; - logentry-check ()
fcb500ea
AS
280;;
281;; If defined, this function is run to find out whether the user
282;; entered a valid log entry for check-in. The log entry is in the
283;; current buffer, and if it is not a valid one, the function should
284;; throw an error.
285;;
aae91380 286;; - comment-history (file)
fcb500ea
AS
287;;
288;; Return a string containing all log entries that were made for FILE.
289;; This is used for transferring a file from one backend to another,
290;; retaining comment information. The default implementation of this
291;; function does this by calling print-log and then wash-log, and
292;; returning the resulting buffer contents as a string.
293;;
aae91380 294;; - update-changelog (files)
fcb500ea
AS
295;;
296;; Using recent log entries, create ChangeLog entries for FILES, or for
297;; all files at or below the default-directory if FILES is nil. The
298;; default implementation runs rcs2log, which handles RCS- and
299;; CVS-style logs.
300;;
0e362f54 301;; * diff (file &optional rev1 rev2)
fcb500ea 302;;
3f19e412
AS
303;; Insert the diff for FILE into the *vc-diff* buffer. If REV1 and
304;; REV2 are non-nil, report differences from REV1 to REV2. If REV1
305;; is nil, use the current workfile version (as found in the
306;; repository) as the older version; if REV2 is nil, use the current
307;; workfile contents as the newer version. This function should
308;; pass the value of (vc-diff-switches-list BACKEND) to the backend
309;; command. It should return a status of either 0 (no differences
310;; found), or 1 (either non-empty diff or the diff is run
311;; asynchronously).
fcb500ea 312;;
99cb8c8b 313;; - diff-tree (dir &optional rev1 rev2)
2c87edc1
AS
314;;
315;; Insert the diff for all files at and below DIR into the *vc-diff*
99cb8c8b 316;; buffer. The meaning of REV1 and REV2 is the same as for
2c87edc1
AS
317;; vc-BACKEND-diff. The default implementation does an explicit tree
318;; walk, calling vc-BACKEND-diff for each individual file.
319;;
aae91380 320;; - annotate-command (file buf rev)
fcb500ea
AS
321;;
322;; If this function is provided, it should produce an annotated version
323;; of FILE in BUF, relative to version REV. This is currently only
324;; implemented for CVS, using the `cvs annotate' command.
325;;
75665141 326;; - annotate-time ()
fcb500ea
AS
327;;
328;; Only required if `annotate-command' is defined for the backend.
75665141
AS
329;; Return the time of the next line of annotation at or after point,
330;; as a floating point fractional number of days. The helper
331;; function `vc-annotate-convert-time' may be useful for converting
332;; multi-part times as returned by `current-time' and `encode-time'
0ff9b955 333;; to this format. Return nil if no more lines of annotation appear
75665141
AS
334;; in the buffer. You can safely assume that point is placed at the
335;; beginning of each line, starting at `point-min'. The buffer that
336;; point is placed in is the Annotate output, as defined by the
337;; relevant backend.
338;;
339;; - annotate-current-time ()
340;;
341;; Only required if `annotate-command' is defined for the backend,
342;; AND you'd like the current time considered to be anything besides
343;; (vs-annotate-convert-time (current-time)) -- i.e. the current
344;; time with hours, minutes, and seconds included. Probably safe to
345;; ignore. Return the current-time, in units of fractional days.
fcb500ea
AS
346;;
347;; SNAPSHOT SYSTEM
348;;
0e362f54 349;; - create-snapshot (dir name branchp)
fcb500ea
AS
350;;
351;; Take a snapshot of the current state of files under DIR and name it
352;; NAME. This should make sure that files are up-to-date before
353;; proceeding with the action. DIR can also be a file and if BRANCHP
354;; is specified, NAME should be created as a branch and DIR should be
355;; checked out under this new branch. The default implementation does
356;; not support branches but does a sanity check, a tree traversal and
357;; for each file calls `assign-name'.
358;;
359;; - assign-name (file name)
360;;
361;; Give name NAME to the current version of FILE, assuming it is
362;; up-to-date. Only used by the default version of `create-snapshot'.
363;;
0e362f54 364;; - retrieve-snapshot (dir name update)
fcb500ea
AS
365;;
366;; Retrieve a named snapshot of all registered files at or below DIR.
367;; If UPDATE is non-nil, then update buffers of any files in the
368;; snapshot that are currently visited. The default implementation
369;; does a sanity check whether there aren't any uncommitted changes at
370;; or below DIR, and then performs a tree walk, using the `checkout'
371;; function to retrieve the corresponding versions.
372;;
373;; MISCELLANEOUS
374;;
aae91380 375;; - make-version-backups-p (file)
fcb500ea
AS
376;;
377;; Return non-nil if unmodified repository versions of FILE should be
378;; backed up locally. If this is done, VC can perform `diff' and
379;; `revert' operations itself, without calling the backend system. The
380;; default implementation always returns nil.
381;;
869131a5
AS
382;; - previous-version (file rev)
383;;
384;; Return the version number that precedes REV for FILE.
385;;
aae91380 386;; - check-headers ()
fcb500ea
AS
387;;
388;; Return non-nil if the current buffer contains any version headers.
389;;
aae91380 390;; - clear-headers ()
fcb500ea
AS
391;;
392;; In the current buffer, reset all version headers to their unexpanded
393;; form. This function should be provided if the state-querying code
394;; for this backend uses the version headers to determine the state of
395;; a file. This function will then be called whenever VC changes the
396;; version control state in such a way that the headers would give
397;; wrong information.
398;;
099bd78a 399;; - rename-file (old new)
fcb500ea
AS
400;;
401;; Rename file OLD to NEW, both in the working area and in the
402;; repository. If this function is not provided, the command
403;; `vc-rename-file' will signal an error.
aae91380 404
fcb500ea 405;;; Code:
0e362f54 406
594722a8 407(require 'vc-hooks)
8c0aaf40 408(require 'ring)
0e362f54 409(eval-when-compile
7849e179 410 (require 'cl)
099bd78a
SM
411 (require 'compile)
412 (require 'dired) ; for dired-map-over-marks macro
413 (require 'dired-aux)) ; for dired-kill-{line,tree}
8c0aaf40
ER
414
415(if (not (assoc 'vc-parent-buffer minor-mode-alist))
416 (setq minor-mode-alist
417 (cons '(vc-parent-buffer vc-parent-buffer-name)
418 minor-mode-alist)))
594722a8
ER
419
420;; General customization
421
0101cc40
RS
422(defgroup vc nil
423 "Version-control system in Emacs."
424 :group 'tools)
425
426(defcustom vc-suppress-confirm nil
427 "*If non-nil, treat user as expert; suppress yes-no prompts on some things."
428 :type 'boolean
429 :group 'vc)
430
2c4eea90
KH
431(defcustom vc-delete-logbuf-window t
432 "*If non-nil, delete the *VC-log* buffer and window after each logical action.
433If nil, bury that buffer instead.
434This is most useful if you have multiple windows on a frame and would like to
435preserve the setting."
436 :type 'boolean
437 :group 'vc)
438
0101cc40
RS
439(defcustom vc-initial-comment nil
440 "*If non-nil, prompt for initial comment when a file is registered."
441 :type 'boolean
442 :group 'vc)
443
0d53f466
AS
444(defcustom vc-default-init-version "1.1"
445 "*A string used as the default version number when a new file is registered.
b470cb65
AS
446This can be overridden by giving a prefix argument to \\[vc-register]. This
447can also be overridden by a particular VC backend."
0d53f466 448 :type 'string
cd32a7ba
DN
449 :group 'vc
450 :version "20.3")
0d53f466 451
0101cc40
RS
452(defcustom vc-command-messages nil
453 "*If non-nil, display run messages from back-end commands."
454 :type 'boolean
455 :group 'vc)
456
457(defcustom vc-checkin-switches nil
458 "*A string or list of strings specifying extra switches for checkin.
459These are passed to the checkin program by \\[vc-checkin]."
460 :type '(choice (const :tag "None" nil)
461 (string :tag "Argument String")
462 (repeat :tag "Argument List"
463 :value ("")
464 string))
465 :group 'vc)
466
467(defcustom vc-checkout-switches nil
468 "*A string or list of strings specifying extra switches for checkout.
469These are passed to the checkout program by \\[vc-checkout]."
470 :type '(choice (const :tag "None" nil)
471 (string :tag "Argument String")
472 (repeat :tag "Argument List"
473 :value ("")
474 string))
475 :group 'vc)
476
477(defcustom vc-register-switches nil
478 "*A string or list of strings; extra switches for registering a file.
479These are passed to the checkin program by \\[vc-register]."
480 :type '(choice (const :tag "None" nil)
481 (string :tag "Argument String")
482 (repeat :tag "Argument List"
483 :value ("")
484 string))
485 :group 'vc)
486
0e362f54
GM
487(defcustom vc-dired-listing-switches "-al"
488 "*Switches passed to `ls' for vc-dired. MUST contain the `l' option."
489 :type 'string
490 :group 'vc
6f41eeb5 491 :version "21.1")
0e362f54 492
3b574573
AS
493(defcustom vc-dired-recurse t
494 "*If non-nil, show directory trees recursively in VC Dired."
495 :type 'boolean
496 :group 'vc
497 :version "20.3")
498
499(defcustom vc-dired-terse-display t
500 "*If non-nil, show only locked files in VC Dired."
501 :type 'boolean
502 :group 'vc
503 :version "20.3")
504
0101cc40 505(defcustom vc-directory-exclusion-list '("SCCS" "RCS" "CVS")
6f1ecae4 506 "*List of directory names to be ignored when walking directory trees."
0101cc40
RS
507 :type '(repeat string)
508 :group 'vc)
666a0ebb 509
8c0aaf40
ER
510(defconst vc-maximum-comment-ring-size 32
511 "Maximum number of saved comments in the comment ring.")
512
acc5b122
AS
513(defcustom vc-diff-switches nil
514 "*A string or list of strings specifying switches for diff under VC.
b453817c
AS
515When running diff under a given BACKEND, VC concatenates the values of
516`diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
517get the switches for that command. Thus, `vc-diff-switches' should
518contain switches that are specific to version control, but not
519specific to any particular backend."
acc5b122
AS
520 :type '(choice (const :tag "None" nil)
521 (string :tag "Argument String")
522 (repeat :tag "Argument List"
523 :value ("")
524 string))
525 :group 'vc
526 :version "21.1")
527
7cad930d
AS
528;;;###autoload
529(defcustom vc-checkout-hook nil
6f1ecae4 530 "*Normal hook (list of functions) run after checking out a file.
7cad930d
AS
531See `run-hooks'."
532 :type 'hook
533 :group 'vc
534 :version "21.1")
535
75665141 536(defcustom vc-annotate-display-mode nil
6f1ecae4 537 "Which mode to color the output of \\[vc-annotate] with by default."
75665141
AS
538 :type '(choice (const :tag "Default" nil)
539 (const :tag "Scale to Oldest" scale)
540 (const :tag "Scale Oldest->Newest" fullscale)
541 (number :tag "Specify Fractional Number of Days"
542 :value "20.5"))
543 :group 'vc)
544
0e362f54
GM
545;;;###autoload
546(defcustom vc-checkin-hook nil
547 "*Normal hook (list of functions) run after a checkin is done.
548See `run-hooks'."
549 :type 'hook
550 :options '(vc-comment-to-change-log)
551 :group 'vc)
552
553;;;###autoload
554(defcustom vc-before-checkin-hook nil
6f1ecae4 555 "*Normal hook (list of functions) run before a file is checked in.
0e362f54
GM
556See `run-hooks'."
557 :type 'hook
558 :group 'vc)
559
560(defcustom vc-logentry-check-hook nil
561 "*Normal hook run by `vc-backend-logentry-check'.
562Use this to impose your own rules on the entry in addition to any the
563version control backend imposes itself."
564 :type 'hook
565 :group 'vc)
2e810285 566
0e362f54 567;; Annotate customization
7d2d9482 568(defcustom vc-annotate-color-map
75665141
AS
569 '(( 20. . "#FF0000")
570 ( 40. . "#FF3800")
571 ( 60. . "#FF7000")
572 ( 80. . "#FFA800")
573 (100. . "#FFE000")
574 (120. . "#E7FF00")
575 (140. . "#AFFF00")
576 (160. . "#77FF00")
577 (180. . "#3FFF00")
578 (200. . "#07FF00")
579 (220. . "#00FF31")
580 (240. . "#00FF69")
581 (260. . "#00FFA1")
582 (280. . "#00FFD9")
583 (300. . "#00EEFF")
584 (320. . "#00B6FF")
585 (340. . "#007EFF"))
6f1ecae4 586 "*Association list of age versus color, for \\[vc-annotate].
75665141
AS
587Ages are given in units of fractional days. Default is eighteen steps
588using a twenty day increment."
0e362f54 589 :type 'alist
7d2d9482
RS
590 :group 'vc)
591
592(defcustom vc-annotate-very-old-color "#0046FF"
6f1ecae4 593 "*Color for lines older than the current color range in \\[vc-annotate]]."
7d2d9482
RS
594 :type 'string
595 :group 'vc)
596
597(defcustom vc-annotate-background "black"
598 "*Background color for \\[vc-annotate].
599Default color is used if nil."
600 :type 'string
601 :group 'vc)
602
603(defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
604 "*Menu elements for the mode-specific menu of VC-Annotate mode.
605List of factors, used to expand/compress the time scale. See `vc-annotate'."
0e362f54 606 :type '(repeat number)
7d2d9482
RS
607 :group 'vc)
608
0e362f54
GM
609;; vc-annotate functionality (CVS only).
610(defvar vc-annotate-mode nil
611 "Variable indicating if VC-Annotate mode is active.")
f0b188ed 612
0e362f54
GM
613(defvar vc-annotate-mode-map
614 (let ((m (make-sparse-keymap)))
615 (define-key m [menu-bar] (make-sparse-keymap "VC-Annotate"))
616 m)
617 "Local keymap used for VC-Annotate mode.")
67242a23 618
0e362f54
GM
619(defvar vc-annotate-mode-menu nil
620 "Local keymap used for VC-Annotate mode's menu bar menu.")
7d2d9482 621
594722a8
ER
622;; Header-insertion hair
623
0101cc40 624(defcustom vc-static-header-alist
594722a8
ER
625 '(("\\.c$" .
626 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
0e362f54
GM
627 "*Associate static header string templates with file types.
628A \%s in the template is replaced with the first string associated with
099bd78a 629the file's version control type in `vc-header-alist'."
0101cc40
RS
630 :type '(repeat (cons :format "%v"
631 (regexp :tag "File Type")
632 (string :tag "Header String")))
633 :group 'vc)
7b4f934d 634
0101cc40 635(defcustom vc-comment-alist
594722a8 636 '((nroff-mode ".\\\"" ""))
6f1ecae4 637 "*Special comment delimiters for generating VC headers.
099bd78a
SM
638Add an entry in this list if you need to override the normal `comment-start'
639and `comment-end' variables. This will only be necessary if the mode language
0101cc40
RS
640is sensitive to blank lines."
641 :type '(repeat (list :format "%v"
642 (symbol :tag "Mode")
643 (string :tag "Comment Start")
644 (string :tag "Comment End")))
645 :group 'vc)
594722a8 646
0101cc40 647(defcustom vc-checkout-carefully (= (user-uid) 0)
6f1ecae4
AS
648 "*This variable is obsolete
649The corresponding checks are always done now.
650From the old doc string:
651
652Non-nil means be extra-careful in checkout.
bbf97570 653Verify that the file really is not locked
0101cc40
RS
654and that its contents match what the master file says."
655 :type 'boolean
656 :group 'vc)
bbf97570 657
0e362f54 658\f
ec402ad4 659;; The main keymap
b0c9bc8c 660
0e362f54
GM
661;; Initialization code, to be done just once at load-time
662(defvar vc-log-mode-map
663 (let ((map (make-sparse-keymap)))
928ef6d9 664 (set-keymap-parent map text-mode-map)
0e362f54
GM
665 (define-key map "\M-n" 'vc-next-comment)
666 (define-key map "\M-p" 'vc-previous-comment)
667 (define-key map "\M-r" 'vc-comment-search-reverse)
668 (define-key map "\M-s" 'vc-comment-search-forward)
669 (define-key map "\C-c\C-c" 'vc-finish-logentry)
670 map))
671;; Compatibility with old name. Should we bother ?
672(defvar vc-log-entry-mode vc-log-mode-map)
b0c9bc8c 673
0e362f54 674\f
594722a8 675;; Variables the user doesn't need to know about.
594722a8 676(defvar vc-log-operation nil)
67242a23 677(defvar vc-log-after-operation-hook nil)
0e362f54 678(defvar vc-annotate-buffers nil
099bd78a
SM
679 "Alist of current \"Annotate\" buffers and their corresponding backends.
680The keys are \(BUFFER . BACKEND\). See also `vc-annotate-get-backend'.")
dbf87856
RS
681;; In a log entry buffer, this is a local variable
682;; that points to the buffer for which it was made
683;; (either a file, or a VC dired buffer).
1a2f456b 684(defvar vc-parent-buffer nil)
0e362f54 685(put 'vc-parent-buffer 'permanent-local t)
8c0aaf40 686(defvar vc-parent-buffer-name nil)
0e362f54 687(put 'vc-parent-buffer-name 'permanent-local t)
594722a8 688
db59472c
RS
689(defvar vc-log-file)
690(defvar vc-log-version)
691
8c0aaf40 692(defvar vc-dired-mode nil)
e1f297e6
ER
693(make-variable-buffer-local 'vc-dired-mode)
694
c9b35ece 695(defvar vc-comment-ring (make-ring vc-maximum-comment-ring-size))
8c0aaf40 696(defvar vc-comment-ring-index nil)
0e362f54 697(defvar vc-last-comment-match "")
c8de1d91 698
ec402ad4
SM
699;; functions that operate on RCS revision numbers. This code should
700;; also be moved into the backends. It stays for now, however, since
701;; it is used in code below.
c8de1d91 702(defun vc-trunk-p (rev)
099bd78a 703 "Return t if REV is a revision on the trunk."
c8de1d91
AS
704 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
705
ccb141e8 706(defun vc-branch-p (rev)
099bd78a 707 "Return t if REV is a branch revision."
ccb141e8
AS
708 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
709
a10cd561 710;;;###autoload
c8de1d91 711(defun vc-branch-part (rev)
099bd78a 712 "Return the branch part of a revision number REV."
869131a5
AS
713 (let ((index (string-match "\\.[0-9]+\\'" rev)))
714 (if index
715 (substring rev 0 index))))
c8de1d91 716
c0d66cb2 717(defun vc-minor-part (rev)
099bd78a 718 "Return the minor version number of a revision number REV."
c0d66cb2
RS
719 (string-match "[0-9]+\\'" rev)
720 (substring rev (match-beginning 0) (match-end 0)))
721
869131a5
AS
722(defun vc-default-previous-version (backend file rev)
723 "Guess the version number immediately preceding REV for FILE.
724This default implementation works for <major>.<minor>-style version numbers
f1180544 725as used by RCS and CVS."
c0d66cb2
RS
726 (let ((branch (vc-branch-part rev))
727 (minor-num (string-to-number (vc-minor-part rev))))
869131a5
AS
728 (when branch
729 (if (> minor-num 1)
730 ;; version does probably not start a branch or release
731 (concat branch "." (number-to-string (1- minor-num)))
732 (if (vc-trunk-p rev)
733 ;; we are at the beginning of the trunk --
734 ;; don't know anything to return here
735 nil
736 ;; we are at the beginning of a branch --
737 ;; return version of starting point
738 (vc-branch-part branch))))))
c0d66cb2 739
594722a8
ER
740;; File property caching
741
8c0aaf40
ER
742(defun vc-clear-context ()
743 "Clear all cached file properties and the comment ring."
744 (interactive)
fd828872 745 (fillarray vc-file-prop-obarray 0)
8c0aaf40
ER
746 ;; Note: there is potential for minor lossage here if there is an open
747 ;; log buffer with a nonzero local value of vc-comment-ring-index.
c9b35ece 748 (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
8c0aaf40 749
099bd78a 750(defmacro with-vc-properties (file form settings)
6f1ecae4
AS
751 "Execute FORM, then maybe set per-file properties for FILE.
752SETTINGS is an association list of property/value pairs. After
753executing FORM, set those properties from SETTINGS that have not yet
754been updated to their corresponding values."
169f0cae 755 `(let ((vc-touched-properties (list t)))
099bd78a
SM
756 ,form
757 (mapcar (lambda (setting)
a3255400 758 (let ((property (car setting)))
099bd78a 759 (unless (memq property vc-touched-properties)
169f0cae 760 (put (intern ,file vc-file-prop-obarray)
a3255400 761 property (cdr setting)))))
099bd78a
SM
762 ,settings)))
763
594722a8
ER
764;; Random helper functions
765
0e362f54 766(defsubst vc-editable-p (file)
6f1ecae4 767 "Return non-nil if FILE can be edited."
0e362f54 768 (or (eq (vc-checkout-model file) 'implicit)
7849e179 769 (memq (vc-state file) '(edited needs-merge))))
c8de1d91 770
ec402ad4 771;; Two macros for elisp programming
709822e8
AS
772;;;###autoload
773(defmacro with-vc-file (file comment &rest body)
6f1ecae4 774 "Check out a writable copy of FILE if necessary, then execute BODY.
0e362f54
GM
775Check in FILE with COMMENT (a string) after BODY has been executed.
776FILE is passed through `expand-file-name'; BODY executed within
777`save-excursion'. If FILE is not under version control, or locked by
709822e8 778somebody else, signal error."
169f0cae
AS
779 (let ((filevar (make-symbol "file")))
780 `(let ((,filevar (expand-file-name ,file)))
781 (or (vc-backend ,filevar)
782 (error (format "File not under version control: `%s'" file)))
783 (unless (vc-editable-p ,filevar)
784 (let ((state (vc-state ,filevar)))
f1180544 785 (if (stringp state)
169f0cae
AS
786 (error (format "`%s' is locking `%s'" state ,filevar))
787 (vc-checkout ,filevar t))))
788 (save-excursion
789 ,@body)
790 (vc-checkin ,filevar nil ,comment))))
791
792(put 'with-vc-file 'lisp-indent-function 2)
709822e8
AS
793
794;;;###autoload
795(defmacro edit-vc-file (file comment &rest body)
0e362f54
GM
796 "Edit FILE under version control, executing body.
797Checkin with COMMENT after executing BODY.
709822e8
AS
798This macro uses `with-vc-file', passing args to it.
799However, before executing BODY, find FILE, and after BODY, save buffer."
169f0cae
AS
800 (let ((filevar (make-symbol "file")))
801 `(let ((,filevar (expand-file-name ,file)))
802 (with-vc-file
803 ,filevar ,comment
804 (set-buffer (find-file-noselect ,filevar))
805 ,@body
806 (save-buffer)))))
807
808(put 'edit-vc-file 'lisp-indent-function 2)
709822e8 809
b6909007 810(defun vc-ensure-vc-buffer ()
099bd78a 811 "Make sure that the current buffer visits a version-controlled file."
b6909007
AS
812 (if vc-dired-mode
813 (set-buffer (find-file-noselect (dired-get-filename)))
814 (while vc-parent-buffer
815 (pop-to-buffer vc-parent-buffer))
816 (if (not (buffer-file-name))
817 (error "Buffer %s is not associated with a file" (buffer-name))
818 (if (not (vc-backend (buffer-file-name)))
819 (error "File %s is not under version control" (buffer-file-name))))))
7ef84cf9 820
594722a8 821(defvar vc-binary-assoc nil)
87a00c4f
EZ
822(defvar vc-binary-suffixes
823 (if (memq system-type '(ms-dos windows-nt))
824 '(".exe" ".com" ".bat" ".cmd" ".btm" "")
825 '("")))
0e362f54
GM
826
827(defun vc-process-filter (p s)
099bd78a 828 "An alternative output filter for async process P.
0e362f54
GM
829The only difference with the default filter is to insert S after markers."
830 (with-current-buffer (process-buffer p)
831 (save-excursion
832 (let ((inhibit-read-only t))
833 (goto-char (process-mark p))
834 (insert s)
835 (set-marker (process-mark p) (point))))))
836
837(defun vc-setup-buffer (&optional buf)
6f1ecae4 838 "Prepare BUF for executing a VC command and make it current.
0e362f54
GM
839BUF defaults to \"*vc*\", can be a string and will be created if necessary."
840 (unless buf (setq buf "*vc*"))
841 (let ((camefrom (current-buffer))
842 (olddir default-directory))
843 (set-buffer (get-buffer-create buf))
844 (kill-all-local-variables)
845 (set (make-local-variable 'vc-parent-buffer) camefrom)
846 (set (make-local-variable 'vc-parent-buffer-name)
847 (concat " from " (buffer-name camefrom)))
848 (setq default-directory olddir)
849 (let ((inhibit-read-only t))
850 (erase-buffer))))
851
852(defun vc-exec-after (code)
853 "Eval CODE when the current buffer's process is done.
854If the current buffer has no process, just evaluate CODE.
855Else, add CODE to the process' sentinel."
856 (let ((proc (get-buffer-process (current-buffer))))
857 (cond
858 ;; If there's no background process, just execute the code.
859 ((null proc) (eval code))
860 ;; If the background process has exited, reap it and try again
861 ((eq (process-status proc) 'exit)
862 (delete-process proc)
863 (vc-exec-after code))
864 ;; If a process is running, add CODE to the sentinel
865 ((eq (process-status proc) 'run)
866 (let ((sentinel (process-sentinel proc)))
867 (set-process-sentinel proc
868 `(lambda (p s)
869 (with-current-buffer ',(current-buffer)
870 (goto-char (process-mark p))
6f41eeb5 871 ,@(append (cdr (cdr (cdr ;strip off `with-current-buffer buf
0e362f54
GM
872 ; (goto-char...)'
873 (car (cdr (cdr ;strip off `lambda (p s)'
874 sentinel))))))
875 (list `(vc-exec-after ',code))))))))
876 (t (error "Unexpected process state"))))
877 nil)
878
879(defvar vc-post-command-functions nil
880 "Hook run at the end of `vc-do-command'.
881Each function is called inside the buffer in which the command was run
6f1ecae4 882and is passed 3 arguments: the COMMAND, the FILE and the FLAGS.")
0e362f54 883
a84b80ba 884;;;###autoload
0e362f54 885(defun vc-do-command (buffer okstatus command file &rest flags)
6f1ecae4 886 "Execute a VC command, notifying user and checking for errors.
ad339989
AS
887Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
888current buffer if BUFFER is t. If the destination buffer is not
889already current, set it up properly and erase it. The command is
890considered successful if its exit status does not exceed OKSTATUS (if
891OKSTATUS is nil, that means to ignore errors, if it is 'async, that
892means not to wait for termination of the subprocess). FILE is the
893name of the working file (may also be nil, to execute commands that
894don't expect a file name). If an optional list of FLAGS is present,
0e362f54 895that is inserted into the command line before the filename."
b0c9bc8c 896 (and file (setq file (expand-file-name file)))
594722a8 897 (if vc-command-messages
02da6253 898 (message "Running %s on %s..." command file))
0e362f54 899 (save-current-buffer
ad339989
AS
900 (unless (or (eq buffer t)
901 (and (stringp buffer)
902 (string= (buffer-name) buffer))
903 (eq buffer (current-buffer)))
904 (vc-setup-buffer buffer))
0e362f54
GM
905 (let ((squeezed nil)
906 (inhibit-read-only t)
907 (status 0))
908 (setq squeezed (delq nil (copy-sequence flags)))
909 (when file
910 ;; FIXME: file-relative-name can return a bogus result because
911 ;; it doesn't look at the actual file-system to see if symlinks
912 ;; come into play.
913 (setq squeezed (append squeezed (list (file-relative-name file)))))
914 (let ((exec-path (append vc-path exec-path))
915 ;; Add vc-path to PATH for the execution of this command.
916 (process-environment
917 (cons (concat "PATH=" (getenv "PATH")
918 path-separator
919 (mapconcat 'identity vc-path path-separator))
920 process-environment))
921 (w32-quote-process-args t))
922 (if (eq okstatus 'async)
923 (let ((proc (apply 'start-process command (current-buffer) command
924 squeezed)))
d9ed4100
AS
925 (unless (active-minibuffer-window)
926 (message "Running %s in the background..." command))
0e362f54
GM
927 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
928 (set-process-filter proc 'vc-process-filter)
929 (vc-exec-after
d9ed4100
AS
930 `(unless (active-minibuffer-window)
931 (message "Running %s in the background... done" ',command))))
0e362f54
GM
932 (setq status (apply 'call-process command nil t nil squeezed))
933 (when (or (not (integerp status)) (and okstatus (< okstatus status)))
934 (pop-to-buffer (current-buffer))
935 (goto-char (point-min))
936 (shrink-window-if-larger-than-buffer)
937 (error "Running %s...FAILED (%s)" command
938 (if (integerp status) (format "status %d" status) status))))
939 (if vc-command-messages
940 (message "Running %s...OK" command)))
941 (vc-exec-after
942 `(run-hook-with-args 'vc-post-command-functions ',command ',file ',flags))
943 status)))
594722a8 944
c4ae7096 945(defun vc-position-context (posn)
099bd78a
SM
946 "Save a bit of the text around POSN in the current buffer.
947Used to help us find the corresponding position again later
948if markers are destroyed or corrupted."
0e362f54
GM
949 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
950 ;; rcs.el mode.
c4ae7096
JB
951 (list posn
952 (buffer-size)
953 (buffer-substring posn
954 (min (point-max) (+ posn 100)))))
955
c4ae7096 956(defun vc-find-position-by-context (context)
6f1ecae4
AS
957 "Return the position of CONTEXT in the current buffer.
958If CONTEXT cannot be found, return nil."
c4ae7096
JB
959 (let ((context-string (nth 2 context)))
960 (if (equal "" context-string)
961 (point-max)
962 (save-excursion
963 (let ((diff (- (nth 1 context) (buffer-size))))
964 (if (< diff 0) (setq diff (- diff)))
965 (goto-char (nth 0 context))
966 (if (or (search-forward context-string nil t)
967 ;; Can't use search-backward since the match may continue
968 ;; after point.
969 (progn (goto-char (- (point) diff (length context-string)))
970 ;; goto-char doesn't signal an error at
971 ;; beginning of buffer like backward-char would
972 (search-forward context-string nil t)))
973 ;; to beginning of OSTRING
974 (- (point) (length context-string))))))))
975
4b398f5d 976(defun vc-context-matches-p (posn context)
099bd78a 977 "Return t if POSN matches CONTEXT, nil otherwise."
4b398f5d
AS
978 (let* ((context-string (nth 2 context))
979 (len (length context-string))
980 (end (+ posn len)))
981 (if (> end (1+ (buffer-size)))
982 nil
983 (string= context-string (buffer-substring posn end)))))
984
c8de1d91 985(defun vc-buffer-context ()
099bd78a
SM
986 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
987Used by `vc-restore-buffer-context' to later restore the context."
c4ae7096 988 (let ((point-context (vc-position-context (point)))
cfadef63
RS
989 ;; Use mark-marker to avoid confusion in transient-mark-mode.
990 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
991 (vc-position-context (mark-marker))))
992 ;; Make the right thing happen in transient-mark-mode.
ab877583
RM
993 (mark-active nil)
994 ;; We may want to reparse the compilation buffer after revert
995 (reparse (and (boundp 'compilation-error-list) ;compile loaded
996 (let ((curbuf (current-buffer)))
997 ;; Construct a list; each elt is nil or a buffer
998 ;; iff that buffer is a compilation output buffer
999 ;; that contains markers into the current buffer.
1000 (save-excursion
0e362f54 1001 (mapcar (lambda (buffer)
ab877583
RM
1002 (set-buffer buffer)
1003 (let ((errors (or
1004 compilation-old-error-list
1005 compilation-error-list))
1006 (buffer-error-marked-p nil))
6fb6ab11 1007 (while (and (consp errors)
ab877583 1008 (not buffer-error-marked-p))
a1bda481 1009 (and (markerp (cdr (car errors)))
e9c8e248
RM
1010 (eq buffer
1011 (marker-buffer
a1bda481 1012 (cdr (car errors))))
e9c8e248 1013 (setq buffer-error-marked-p t))
ab877583 1014 (setq errors (cdr errors)))
0e362f54 1015 (if buffer-error-marked-p buffer)))
ab877583 1016 (buffer-list)))))))
c8de1d91
AS
1017 (list point-context mark-context reparse)))
1018
1019(defun vc-restore-buffer-context (context)
0e362f54 1020 "Restore point/mark, and reparse any affected compilation buffers.
099bd78a 1021CONTEXT is that which `vc-buffer-context' returns."
c8de1d91
AS
1022 (let ((point-context (nth 0 context))
1023 (mark-context (nth 1 context))
1024 (reparse (nth 2 context)))
ab877583
RM
1025 ;; Reparse affected compilation buffers.
1026 (while reparse
1027 (if (car reparse)
0e362f54 1028 (with-current-buffer (car reparse)
ab877583
RM
1029 (let ((compilation-last-buffer (current-buffer)) ;select buffer
1030 ;; Record the position in the compilation buffer of
1031 ;; the last error next-error went to.
1032 (error-pos (marker-position
1033 (car (car-safe compilation-error-list)))))
1034 ;; Reparse the error messages as far as they were parsed before.
1035 (compile-reinitialize-errors '(4) compilation-parsing-end)
1036 ;; Move the pointer up to find the error we were at before
1037 ;; reparsing. Now next-error should properly go to the next one.
1038 (while (and compilation-error-list
27f2f10b 1039 (/= error-pos (car (car compilation-error-list))))
ab877583
RM
1040 (setq compilation-error-list (cdr compilation-error-list))))))
1041 (setq reparse (cdr reparse)))
e1f297e6 1042
4b398f5d
AS
1043 ;; if necessary, restore point and mark
1044 (if (not (vc-context-matches-p (point) point-context))
1045 (let ((new-point (vc-find-position-by-context point-context)))
1046 (if new-point (goto-char new-point))))
01e02ab3
AS
1047 (and mark-active
1048 mark-context
1049 (not (vc-context-matches-p (mark) mark-context))
1050 (let ((new-mark (vc-find-position-by-context mark-context)))
1051 (if new-mark (set-mark new-mark))))))
c4ae7096 1052
c8de1d91 1053(defun vc-revert-buffer1 (&optional arg no-confirm)
6f1ecae4
AS
1054 "Revert buffer, keeping point and mark where user expects them.
1055Try to be clever in the face of changes due to expanded version control
099bd78a
SM
1056key words. This is important for typeahead to work as expected.
1057ARG and NO-CONFIRM are passed on to `revert-buffer'."
c8de1d91
AS
1058 (interactive "P")
1059 (widen)
1060 (let ((context (vc-buffer-context)))
4b398f5d
AS
1061 ;; Use save-excursion here, because it may be able to restore point
1062 ;; and mark properly even in cases where vc-restore-buffer-context
0e362f54 1063 ;; would fail. However, save-excursion might also get it wrong --
4b398f5d
AS
1064 ;; in this case, vc-restore-buffer-context gives it a second try.
1065 (save-excursion
0e362f54 1066 ;; t means don't call normal-mode;
4b398f5d
AS
1067 ;; that's to preserve various minor modes.
1068 (revert-buffer arg no-confirm t))
c8de1d91
AS
1069 (vc-restore-buffer-context context)))
1070
594722a8 1071
97d3f950 1072(defun vc-buffer-sync (&optional not-urgent)
099bd78a 1073 "Make sure the current buffer and its working file are in sync.
0e362f54 1074NOT-URGENT means it is ok to continue if the user says not to save."
bbf97570 1075 (if (buffer-modified-p)
97d3f950
RS
1076 (if (or vc-suppress-confirm
1077 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1078 (save-buffer)
0e362f54 1079 (unless not-urgent
97d3f950
RS
1080 (error "Aborted")))))
1081
336203a2 1082(defun vc-default-latest-on-branch-p (backend file)
6f1ecae4
AS
1083 "Return non-nil if FILE is the latest on its branch.
1084This default implementation always returns non-nil, which means that
1085editing non-current versions is not supported by default."
38d88d32
AS
1086 t)
1087
0e362f54
GM
1088(defun vc-recompute-state (file)
1089 "Force a recomputation of the version control state of FILE.
1090The state is computed using the exact, and possibly expensive
1091function `vc-BACKEND-state', not the heuristic."
1092 (vc-file-setprop file 'vc-state (vc-call state file)))
e1f297e6 1093
0e362f54 1094(defun vc-next-action-on-file (file verbose &optional comment)
6f1ecae4 1095 "Do The Right Thing for a given FILE under version control.
0e362f54
GM
1096If COMMENT is specified, it will be used as an admin or checkin comment.
1097If VERBOSE is non-nil, query the user rather than using default parameters."
1098 (let ((visited (get-file-buffer file))
1099 state version)
1100 (when visited
b4c40474
AS
1101 (if vc-dired-mode
1102 (switch-to-buffer-other-window visited)
1103 (set-buffer visited))
0e362f54
GM
1104 ;; Check relation of buffer and file, and make sure
1105 ;; user knows what he's doing. First, finding the file
1106 ;; will check whether the file on disk is newer.
b4c40474
AS
1107 ;; Ignore buffer-read-only during this test, and
1108 ;; preserve find-file-literally.
165e43b6 1109 (let ((buffer-read-only (not (file-writable-p file))))
b4c40474 1110 (find-file-noselect file nil find-file-literally))
0e362f54
GM
1111 (if (not (verify-visited-file-modtime (current-buffer)))
1112 (if (yes-or-no-p "Replace file on disk with buffer contents? ")
1113 (write-file (buffer-file-name))
1114 (error "Aborted"))
1115 ;; Now, check if we have unsaved changes.
1116 (vc-buffer-sync t)
1117 (if (buffer-modified-p)
1118 (or (y-or-n-p "Operate on disk file, keeping modified buffer? ")
1119 (error "Aborted")))))
46e33aee 1120
0e362f54 1121 ;; Do the right thing
8989ab56 1122 (if (not (vc-registered file))
0e362f54
GM
1123 (vc-register verbose comment)
1124 (vc-recompute-state file)
0ab66291 1125 (if visited (vc-mode-line file))
0e362f54
GM
1126 (setq state (vc-state file))
1127 (cond
1128 ;; up-to-date
1129 ((or (eq state 'up-to-date)
1130 (and verbose (eq state 'needs-patch)))
1131 (cond
1132 (verbose
1133 ;; go to a different version
ceec5a0c 1134 (setq version
1d502d5a 1135 (read-string "Branch, version, or backend to move to: "))
ceec5a0c 1136 (let ((vsym (intern-soft (upcase version))))
1d502d5a
AS
1137 (if (member vsym vc-handled-backends)
1138 (vc-transfer-file file vsym)
ceec5a0c 1139 (vc-checkout file (eq (vc-checkout-model file) 'implicit)
1d502d5a 1140 version))))
0e362f54
GM
1141 ((not (eq (vc-checkout-model file) 'implicit))
1142 ;; check the file out
1143 (vc-checkout file t))
1144 (t
1145 ;; do nothing
1146 (message "%s is up-to-date" file))))
46e33aee 1147
0e362f54 1148 ;; Abnormal: edited but read-only
fd8092f0
SM
1149 ((and visited (eq state 'edited)
1150 buffer-read-only (not (file-writable-p file)))
0e362f54
GM
1151 ;; Make the file+buffer read-write. If the user really wanted to
1152 ;; commit, he'll get a chance to do that next time around, anyway.
1153 (message "File is edited but read-only; making it writable")
1154 (set-file-modes buffer-file-name
1155 (logior (file-modes buffer-file-name) 128))
1156 (toggle-read-only -1))
46e33aee 1157
0e362f54
GM
1158 ;; edited
1159 ((eq state 'edited)
6f41eeb5 1160 (cond
0e362f54
GM
1161 ;; For files with locking, if the file does not contain
1162 ;; any changes, just let go of the lock, i.e. revert.
1163 ((and (not (eq (vc-checkout-model file) 'implicit))
1164 (vc-workfile-unchanged-p file)
1165 ;; If buffer is modified, that means the user just
1166 ;; said no to saving it; in that case, don't revert,
1167 ;; because the user might intend to save after
1168 ;; finishing the log entry.
1169 (not (and visited (buffer-modified-p))))
1170 ;; DO NOT revert the file without asking the user!
1171 (if (not visited) (find-file-other-window file))
1172 (if (yes-or-no-p "Revert to master version? ")
1173 (vc-revert-buffer)))
1174 (t ;; normal action
1d502d5a
AS
1175 (if (not verbose)
1176 (vc-checkin file nil comment)
1177 (setq version (read-string "New version or backend: "))
1178 (let ((vsym (intern (upcase version))))
1179 (if (member vsym vc-handled-backends)
1180 (vc-transfer-file file vsym)
1181 (vc-checkin file version comment)))))))
46e33aee 1182
0e362f54
GM
1183 ;; locked by somebody else
1184 ((stringp state)
1185 (if comment
1186 (error "Sorry, you can't steal the lock on %s this way"
1187 (file-name-nondirectory file)))
1188 (vc-steal-lock file
1189 (if verbose (read-string "Version to steal: ")
1190 (vc-workfile-version file))
1191 state))
46e33aee 1192
0e362f54
GM
1193 ;; needs-patch
1194 ((eq state 'needs-patch)
6f41eeb5 1195 (if (yes-or-no-p (format
0e362f54
GM
1196 "%s is not up-to-date. Get latest version? "
1197 (file-name-nondirectory file)))
4279775b 1198 (vc-checkout file (eq (vc-checkout-model file) 'implicit) t)
0e362f54
GM
1199 (if (and (not (eq (vc-checkout-model file) 'implicit))
1200 (yes-or-no-p "Lock this version? "))
1201 (vc-checkout file t)
1202 (error "Aborted"))))
46e33aee 1203
0e362f54
GM
1204 ;; needs-merge
1205 ((eq state 'needs-merge)
6f41eeb5 1206 (if (yes-or-no-p (format
0e362f54
GM
1207 "%s is not up-to-date. Merge in changes now? "
1208 (file-name-nondirectory file)))
1209 (vc-maybe-resolve-conflicts file (vc-call merge-news file))
1210 (error "Aborted")))
46e33aee 1211
0e362f54
GM
1212 ;; unlocked-changes
1213 ((eq state 'unlocked-changes)
1214 (if (not visited) (find-file-other-window file))
1215 (if (save-window-excursion
1216 (vc-version-diff file (vc-workfile-version file) nil)
1217 (goto-char (point-min))
ceec5a0c 1218 (let ((inhibit-read-only t))
f4f34b33 1219 (insert
ceec5a0c 1220 (format "Changes to %s since last lock:\n\n" file)))
0e362f54
GM
1221 (not (beep))
1222 (yes-or-no-p (concat "File has unlocked changes. "
1223 "Claim lock retaining changes? ")))
1224 (progn (vc-call steal-lock file)
165e43b6 1225 (clear-visited-file-modtime)
0e362f54
GM
1226 ;; Must clear any headers here because they wouldn't
1227 ;; show that the file is locked now.
1228 (vc-clear-headers file)
165e43b6 1229 (write-file (buffer-file-name))
0e362f54
GM
1230 (vc-mode-line file))
1231 (if (not (yes-or-no-p
1232 "Revert to checked-in version, instead? "))
1233 (error "Checkout aborted")
1234 (vc-revert-buffer1 t t)
1235 (vc-checkout file t))))))))
e1f297e6 1236
beba4bd9
AS
1237(defvar vc-dired-window-configuration)
1238
e1f297e6 1239(defun vc-next-action-dired (file rev comment)
099bd78a
SM
1240 "Call `vc-next-action-on-file' on all the marked files.
1241Ignores FILE and REV, but passes on COMMENT."
3d30b8bc 1242 (let ((dired-buffer (current-buffer))
8dd71345 1243 (dired-dir default-directory))
632e9525 1244 (dired-map-over-marks
3b574573 1245 (let ((file (dired-get-filename)))
b0c9bc8c 1246 (message "Processing %s..." file)
0e362f54
GM
1247 (vc-next-action-on-file file nil comment)
1248 (set-buffer dired-buffer)
3d30b8bc 1249 (set-window-configuration vc-dired-window-configuration)
b0c9bc8c 1250 (message "Processing %s...done" file))
3d30b8bc
RS
1251 nil t))
1252 (dired-move-to-filename))
e1f297e6 1253
637a8ae9 1254;; Here's the major entry point.
594722a8 1255
637a8ae9 1256;;;###autoload
594722a8 1257(defun vc-next-action (verbose)
6f1ecae4 1258 "Do the next logical version control operation on the current file.
0e362f54
GM
1259
1260If you call this from within a VC dired buffer with no files marked,
c6d4f628 1261it will operate on the file in the current line.
0e362f54
GM
1262
1263If you call this from within a VC dired buffer, and one or more
c6d4f628
RS
1264files are marked, it will accept a log message and then operate on
1265each one. The log message will be used as a comment for any register
1266or checkin operations, but ignored when doing checkouts. Attempted
1267lock steals will raise an error.
0e362f54
GM
1268
1269A prefix argument lets you specify the version number to use.
80688f5c
RS
1270
1271For RCS and SCCS files:
594722a8 1272 If the file is not already registered, this registers it for version
4b81132c 1273control.
594722a8 1274 If the file is registered and not locked by anyone, this checks out
34291cd2 1275a writable and locked file ready for editing.
594722a8
ER
1276 If the file is checked out and locked by the calling user, this
1277first checks to see if the file has changed since checkout. If not,
1278it performs a revert.
e1f297e6
ER
1279 If the file has been changed, this pops up a buffer for entry
1280of a log message; when the message has been entered, it checks in the
594722a8 1281resulting changes along with the log message as change commentary. If
34291cd2 1282the variable `vc-keep-workfiles' is non-nil (which is its default), a
594722a8
ER
1283read-only copy of the changed file is left in place afterwards.
1284 If the file is registered and locked by someone else, you are given
e1f297e6 1285the option to steal the lock.
80688f5c
RS
1286
1287For CVS files:
1288 If the file is not already registered, this registers it for version
1289control. This does a \"cvs add\", but no \"cvs commit\".
1290 If the file is added but not committed, it is committed.
80688f5c
RS
1291 If your working file is changed, but the repository file is
1292unchanged, this pops up a buffer for entry of a log message; when the
1293message has been entered, it checks in the resulting changes along
1294with the logmessage as change commentary. A writable file is retained.
1295 If the repository file is changed, you are asked if you want to
c6d4f628 1296merge in the changes into your working copy."
bbf97570 1297
594722a8 1298 (interactive "P")
8c0aaf40
ER
1299 (catch 'nogo
1300 (if vc-dired-mode
1301 (let ((files (dired-get-marked-files)))
3d30b8bc
RS
1302 (set (make-local-variable 'vc-dired-window-configuration)
1303 (current-window-configuration))
0e362f54 1304 (if (string= ""
8dd71345 1305 (mapconcat
0e362f54
GM
1306 (lambda (f)
1307 (if (not (vc-up-to-date-p f)) "@" ""))
b0c9bc8c
AS
1308 files ""))
1309 (vc-next-action-dired nil nil "dummy")
0ab66291 1310 (vc-start-entry nil nil nil nil
b0c9bc8c
AS
1311 "Enter a change comment for the marked files."
1312 'vc-next-action-dired))
8dd71345 1313 (throw 'nogo nil)))
dc08a6b5
AS
1314 (while vc-parent-buffer
1315 (pop-to-buffer vc-parent-buffer))
1316 (if buffer-file-name
1317 (vc-next-action-on-file buffer-file-name verbose)
1318 (error "Buffer %s is not associated with a file" (buffer-name)))))
594722a8 1319
ec402ad4 1320;; These functions help the vc-next-action entry point
594722a8 1321
637a8ae9 1322;;;###autoload
0e362f54 1323(defun vc-register (&optional set-version comment)
099bd78a 1324 "Register the current file into a version control system.
e1d95cc4 1325With prefix argument SET-VERSION, allow user to specify initial version
0e362f54
GM
1326level. If COMMENT is present, use that as an initial comment.
1327
e1d95cc4 1328The version control system to use is found by cycling through the list
0e362f54
GM
1329`vc-handled-backends'. The first backend in that list which declares
1330itself responsible for the file (usually because other files in that
1331directory are already registered under that backend) will be used to
1332register the file. If no backend declares itself responsible, the
1333first backend that could register the file is used."
594722a8 1334 (interactive "P")
099bd78a 1335 (unless buffer-file-name (error "No visited file"))
0e362f54
GM
1336 (when (vc-backend buffer-file-name)
1337 (if (vc-registered buffer-file-name)
1338 (error "This file is already registered")
1339 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1340 (error "Aborted"))))
02da6253
PE
1341 ;; Watch out for new buffers of size 0: the corresponding file
1342 ;; does not exist yet, even though buffer-modified-p is nil.
1343 (if (and (not (buffer-modified-p))
1344 (zerop (buffer-size))
1345 (not (file-exists-p buffer-file-name)))
1346 (set-buffer-modified-p t))
594722a8 1347 (vc-buffer-sync)
46e33aee 1348
0e362f54
GM
1349 (vc-start-entry buffer-file-name
1350 (if set-version
1d502d5a
AS
1351 (read-string (format "Initial version level for %s: "
1352 (buffer-name)))
b470cb65
AS
1353 (let ((backend (vc-responsible-backend buffer-file-name)))
1354 (if (vc-find-backend-function backend 'init-version)
1355 (vc-call-backend backend 'init-version)
1356 vc-default-init-version)))
0e362f54 1357 (or comment (not vc-initial-comment))
0ab66291 1358 nil
0e362f54
GM
1359 "Enter initial comment."
1360 (lambda (file rev comment)
1361 (message "Registering %s... " file)
8989ab56 1362 (let ((backend (vc-responsible-backend file t)))
e1d95cc4 1363 (vc-file-clearprops file)
0e362f54
GM
1364 (vc-call-backend backend 'register file rev comment)
1365 (vc-file-setprop file 'vc-backend backend)
1366 (unless vc-make-backup-files
1367 (make-local-variable 'backup-inhibited)
1368 (setq backup-inhibited t)))
1369 (message "Registering %s... done" file))))
1370
8989ab56
AS
1371
1372(defun vc-responsible-backend (file &optional register)
1373 "Return the name of a backend system that is responsible for FILE.
46e33aee 1374The optional argument REGISTER means that a backend suitable for
8989ab56
AS
1375registration should be found.
1376
1377If REGISTER is nil, then if FILE is already registered, return the
1378backend of FILE. If FILE is not registered, or a directory, then the
1379first backend in `vc-handled-backends' that declares itself
1380responsible for FILE is returned. If no backend declares itself
1381responsible, return the first backend.
1382
1383If REGISTER is non-nil, return the first responsible backend under
1384which FILE is not yet registered. If there is no such backend, return
1385the first backend under which FILE is not yet registered, but could
1386be registered."
1387 (if (not vc-handled-backends)
1388 (error "No handled backends"))
1389 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1390 (catch 'found
1391 ;; First try: find a responsible backend. If this is for registration,
1392 ;; it must be a backend under which FILE is not yet registered.
1393 (dolist (backend vc-handled-backends)
1394 (and (or (not register)
1395 (not (vc-call-backend backend 'registered file)))
1396 (vc-call-backend backend 'responsible-p file)
1397 (throw 'found backend)))
1398 ;; no responsible backend
1399 (if (not register)
1400 ;; if this is not for registration, the first backend must do
1401 (car vc-handled-backends)
46e33aee 1402 ;; for registration, we need to find a new backend that
8989ab56
AS
1403 ;; could register FILE
1404 (dolist (backend vc-handled-backends)
1405 (and (not (vc-call-backend backend 'registered file))
1406 (vc-call-backend backend 'could-register file)
1407 (throw 'found backend)))
1408 (error "No backend that could register")))))
0e362f54 1409
099bd78a 1410(defun vc-default-responsible-p (backend file)
46e33aee 1411 "Indicate whether BACKEND is reponsible for FILE.
099bd78a
SM
1412The default is to return nil always."
1413 nil)
1414
0e362f54 1415(defun vc-default-could-register (backend file)
6f41eeb5 1416 "Return non-nil if BACKEND could be used to register FILE.
0e362f54
GM
1417The default implementation returns t for all files."
1418 t)
594722a8 1419
624b4662 1420(defun vc-resynch-window (file &optional keep noquery)
099bd78a
SM
1421 "If FILE is in the current buffer, either revert or unvisit it.
1422The choice between revert (to see expanded keywords) and unvisit depends on
1423`vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
0e362f54
GM
1424reverting. NOQUERY should be t *only* if it is known the only
1425difference between the buffer and the file is due to version control
1426rather than user editing!"
594722a8
ER
1427 (and (string= buffer-file-name file)
1428 (if keep
1429 (progn
1ab31687 1430 (vc-revert-buffer1 t noquery)
0e362f54
GM
1431 ;; TODO: Adjusting view mode might no longer be necessary
1432 ;; after RMS change to files.el of 1999-08-08. Investigate
1433 ;; this when we install the new VC.
f8791ebe
AS
1434 (and view-read-only
1435 (if (file-writable-p file)
1436 (and view-mode
1437 (let ((view-old-buffer-read-only nil))
1438 (view-mode-exit)))
1439 (and (not view-mode)
1440 (not (eq (get major-mode 'mode-class) 'special))
1441 (view-mode-enter))))
594722a8 1442 (vc-mode-line buffer-file-name))
88a2ffaf 1443 (kill-buffer (current-buffer)))))
594722a8 1444
503b5c85 1445(defun vc-resynch-buffer (file &optional keep noquery)
0e362f54 1446 "If FILE is currently visited, resynch its buffer."
4b398f5d
AS
1447 (if (string= buffer-file-name file)
1448 (vc-resynch-window file keep noquery)
1449 (let ((buffer (get-file-buffer file)))
1450 (if buffer
0e362f54
GM
1451 (with-current-buffer buffer
1452 (vc-resynch-window file keep noquery)))))
1453 (vc-dired-resynch-file file))
503b5c85 1454
0ab66291 1455(defun vc-start-entry (file rev comment initial-contents msg action &optional after-hook)
46e33aee 1456 "Accept a comment for an operation on FILE revision REV.
6f41eeb5 1457If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
0ab66291
AS
1458action on close to ACTION. If COMMENT is a string and
1459INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1460contents of the log entry buffer. If COMMENT is a string and
1461INITIAL-CONTENTS is nil, do action immediately as if the user had
1462entered COMMENT. If COMMENT is t, also do action immediately with an
46e33aee
TTN
1463empty comment. Remember the file's buffer in `vc-parent-buffer'
1464\(current one if no file). AFTER-HOOK specifies the local value
0ab66291
AS
1465for vc-log-operation-hook."
1466 (let ((parent (or (and file (get-file-buffer file)) (current-buffer))))
f0b188ed
RS
1467 (if vc-before-checkin-hook
1468 (if file
0e362f54 1469 (with-current-buffer parent
f0b188ed
RS
1470 (run-hooks 'vc-before-checkin-hook))
1471 (run-hooks 'vc-before-checkin-hook)))
0ab66291 1472 (if (and comment (not initial-contents))
e1f297e6
ER
1473 (set-buffer (get-buffer-create "*VC-log*"))
1474 (pop-to-buffer (get-buffer-create "*VC-log*")))
8c0aaf40
ER
1475 (set (make-local-variable 'vc-parent-buffer) parent)
1476 (set (make-local-variable 'vc-parent-buffer-name)
1477 (concat " from " (buffer-name vc-parent-buffer)))
7e869659 1478 (if file (vc-mode-line file))
099bd78a 1479 (vc-log-edit file)
b965445f
RS
1480 (make-local-variable 'vc-log-after-operation-hook)
1481 (if after-hook
1482 (setq vc-log-after-operation-hook after-hook))
e1f297e6 1483 (setq vc-log-operation action)
e1f297e6 1484 (setq vc-log-version rev)
347bef30
SM
1485 (when comment
1486 (erase-buffer)
1487 (when (stringp comment) (insert comment)))
1488 (if (or (not comment) initial-contents)
1489 (message "%s Type C-c C-c when done" msg)
1490 (vc-finish-logentry (eq comment t)))))
e1f297e6 1491
c6d4f628 1492(defun vc-checkout (file &optional writable rev)
099bd78a
SM
1493 "Retrieve a copy of the revision REV of FILE.
1494If WRITABLE is non-nil, make sure the retrieved file is writable.
133a84aa
AS
1495REV defaults to the latest revision.
1496
1497After check-out, runs the normal hook `vc-checkout-hook'."
ffda0460
AS
1498 (and writable
1499 (not rev)
10b48cc4 1500 (vc-call make-version-backups-p file)
ffda0460 1501 (vc-up-to-date-p file)
10b48cc4 1502 (vc-make-version-backup file))
099bd78a
SM
1503 (with-vc-properties
1504 file
1505 (condition-case err
1506 (vc-call checkout file writable rev)
1507 (file-error
1508 ;; Maybe the backend is not installed ;-(
1509 (when writable
1510 (let ((buf (get-file-buffer file)))
1511 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1512 (signal (car err) (cdr err))))
a3255400
SM
1513 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1514 (not writable))
1515 (if (vc-call latest-on-branch-p file)
1516 'up-to-date
1517 'needs-patch)
1518 'edited))
1519 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
7cad930d
AS
1520 (vc-resynch-buffer file t t)
1521 (run-hooks 'vc-checkout-hook))
594722a8 1522
0e362f54 1523(defun vc-steal-lock (file rev owner)
099bd78a 1524 "Steal the lock on FILE."
29fc1ce9 1525 (let (file-description)
29fc1ce9
RS
1526 (if rev
1527 (setq file-description (format "%s:%s" file rev))
1528 (setq file-description file))
4bc504c8
RS
1529 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1530 file-description owner)))
0e362f54 1531 (error "Steal canceled"))
869131a5
AS
1532 (message "Stealing lock on %s..." file)
1533 (with-vc-properties
1534 file
1535 (vc-call steal-lock file rev)
1536 `((vc-state . edited)))
1537 (vc-resynch-buffer file t t)
1538 (message "Stealing lock on %s...done" file)
1539 ;; Write mail after actually stealing, because if the stealing
1540 ;; goes wrong, we don't want to send any mail.
1541 (compose-mail owner (format "Stolen lock on %s" file-description))
29fc1ce9 1542 (setq default-directory (expand-file-name "~/"))
29fc1ce9
RS
1543 (goto-char (point-max))
1544 (insert
1545 (format "I stole the lock on %s, " file-description)
1546 (current-time-string)
1547 ".\n")
1548 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
594722a8 1549
0ab66291 1550(defun vc-checkin (file &optional rev comment initial-contents)
6f41eeb5 1551 "Check in FILE.
0e362f54
GM
1552The optional argument REV may be a string specifying the new version
1553level (if nil increment the current level). COMMENT is a comment
0ab66291
AS
1554string; if omitted, a buffer is popped up to accept a comment. If
1555INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1556of the log entry buffer.
0e362f54
GM
1557
1558If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1559that the version control system supports this mode of operation.
861f3c29
DL
1560
1561Runs the normal hook `vc-checkin-hook'."
6f41eeb5 1562 (vc-start-entry
0ab66291 1563 file rev comment initial-contents
6f41eeb5 1564 "Enter a change comment."
0e362f54
GM
1565 (lambda (file rev comment)
1566 (message "Checking in %s..." file)
1567 ;; "This log message intentionally left almost blank".
1568 ;; RCS 5.7 gripes about white-space-only comments too.
1569 (or (and comment (string-match "[^\t\n ]" comment))
1570 (setq comment "*** empty log message ***"))
46e33aee 1571 (with-vc-properties
099bd78a
SM
1572 file
1573 ;; Change buffers to get local value of vc-checkin-switches.
1574 (with-current-buffer (or (get-file-buffer file) (current-buffer))
ab750f9c 1575 (progn
ffda0460 1576 (vc-call checkin file rev comment)
ab750f9c 1577 (vc-delete-automatic-version-backups file)))
a3255400
SM
1578 `((vc-state . up-to-date)
1579 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1580 (vc-workfile-version . nil)))
0e362f54
GM
1581 (message "Checking in %s...done" file))
1582 'vc-checkin-hook))
594722a8 1583
3b4dd9a9 1584(defun vc-comment-to-change-log (&optional whoami file-name)
6f1ecae4
AS
1585 "Enter last VC comment into the change log for the current file.
1586WHOAMI (interactive prefix) non-nil means prompt for user name
1587and site. FILE-NAME is the name of the change log; if nil, use
1588`change-log-default-name'.
861f3c29 1589
6f1ecae4
AS
1590This may be useful as a `vc-checkin-hook' to update change logs
1591automatically."
43cea1ab
RM
1592 (interactive (if current-prefix-arg
1593 (list current-prefix-arg
1594 (prompt-for-change-log-name))))
41208291
KH
1595 ;; Make sure the defvar for add-log-current-defun-function has been executed
1596 ;; before binding it.
1597 (require 'add-log)
3b4dd9a9
RM
1598 (let (;; Extract the comment first so we get any error before doing anything.
1599 (comment (ring-ref vc-comment-ring 0))
43cea1ab 1600 ;; Don't let add-change-log-entry insert a defun name.
3b4dd9a9
RM
1601 (add-log-current-defun-function 'ignore)
1602 end)
1603 ;; Call add-log to do half the work.
43cea1ab 1604 (add-change-log-entry whoami file-name t t)
3b4dd9a9
RM
1605 ;; Insert the VC comment, leaving point before it.
1606 (setq end (save-excursion (insert comment) (point-marker)))
1607 (if (looking-at "\\s *\\s(")
1608 ;; It starts with an open-paren, as in "(foo): Frobbed."
43cea1ab 1609 ;; So remove the ": " add-log inserted.
3b4dd9a9
RM
1610 (delete-char -2))
1611 ;; Canonicalize the white space between the file name and comment.
1612 (just-one-space)
1613 ;; Indent rest of the text the same way add-log indented the first line.
1614 (let ((indentation (current-indentation)))
1615 (save-excursion
1616 (while (< (point) end)
1617 (forward-line 1)
1618 (indent-to indentation))
c124b1b4 1619 (setq end (point))))
3b4dd9a9 1620 ;; Fill the inserted text, preserving open-parens at bol.
6b60c5d1
BG
1621 (let ((paragraph-separate (concat paragraph-separate "\\|\\s *\\s("))
1622 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
43cea1ab 1623 (beginning-of-line)
c124b1b4
RM
1624 (fill-region (point) end))
1625 ;; Canonicalize the white space at the end of the entry so it is
1626 ;; separated from the next entry by a single blank line.
1627 (skip-syntax-forward " " end)
1628 (delete-char (- (skip-syntax-backward " ")))
1629 (or (eobp) (looking-at "\n\n")
1630 (insert "\n"))))
3b4dd9a9 1631
8c0aaf40 1632(defun vc-finish-logentry (&optional nocomment)
6f1ecae4
AS
1633 "Complete the operation implied by the current log entry.
1634Use the contents of the current buffer as a check-in or registration
1635comment. If the optional arg NOCOMMENT is non-nil, then don't check
1636the buffer contents as a comment, and don't add it to
1637`vc-comment-ring'."
594722a8 1638 (interactive)
8c0aaf40 1639 ;; Check and record the comment, if any.
0e362f54
GM
1640 (unless nocomment
1641 ;; Comment too long?
1642 (vc-call-backend (or (and vc-log-file (vc-backend vc-log-file))
1643 (vc-responsible-backend default-directory))
1644 'logentry-check)
1645 (run-hooks 'vc-logentry-check-hook)
1646 ;; Record the comment in the comment ring
1647 (let ((comment (buffer-string)))
1648 (unless (and (ring-p vc-comment-ring)
1649 (not (ring-empty-p vc-comment-ring))
1650 (equal comment (ring-ref vc-comment-ring 0)))
1651 (ring-insert vc-comment-ring comment))))
b2396d1f 1652 ;; Sync parent buffer in case the user modified it while editing the comment.
cdaf7a1a 1653 ;; But not if it is a vc-dired buffer.
0e362f54
GM
1654 (with-current-buffer vc-parent-buffer
1655 (or vc-dired-mode (vc-buffer-sync)))
61dee1e7
AS
1656 (if (not vc-log-operation) (error "No log operation is pending"))
1657 ;; save the parameters held in buffer-local variables
1658 (let ((log-operation vc-log-operation)
1659 (log-file vc-log-file)
1660 (log-version vc-log-version)
1661 (log-entry (buffer-string))
2c4eea90
KH
1662 (after-hook vc-log-after-operation-hook)
1663 (tmp-vc-parent-buffer vc-parent-buffer))
e2bef5c3 1664 (pop-to-buffer vc-parent-buffer)
61dee1e7
AS
1665 ;; OK, do it to it
1666 (save-excursion
0e362f54 1667 (funcall log-operation
61dee1e7
AS
1668 log-file
1669 log-version
1670 log-entry))
df1e7b91
KH
1671 ;; Remove checkin window (after the checkin so that if that fails
1672 ;; we don't zap the *VC-log* buffer and the typing therein).
1673 (let ((logbuf (get-buffer "*VC-log*")))
2c4eea90
KH
1674 (cond ((and logbuf vc-delete-logbuf-window)
1675 (delete-windows-on logbuf (selected-frame))
262c8cea 1676 ;; Kill buffer and delete any other dedicated windows/frames.
2c4eea90 1677 (kill-buffer logbuf))
0ab66291
AS
1678 (logbuf (pop-to-buffer "*VC-log*")
1679 (bury-buffer)
1680 (pop-to-buffer tmp-vc-parent-buffer))))
e2bef5c3 1681 ;; Now make sure we see the expanded headers
46e33aee 1682 (if log-file
0ab66291 1683 (vc-resynch-buffer log-file vc-keep-workfiles t))
0e362f54 1684 (if vc-dired-mode
0ab66291 1685 (dired-move-to-filename))
37667a5c 1686 (run-hooks after-hook 'vc-finish-logentry-hook)))
594722a8
ER
1687
1688;; Code for access to the comment ring
1689
0e362f54 1690(defun vc-new-comment-index (stride len)
6f1ecae4
AS
1691 "Return the comment index STRIDE elements from the current one.
1692LEN is the length of `vc-comment-ring'."
0e362f54
GM
1693 (mod (cond
1694 (vc-comment-ring-index (+ vc-comment-ring-index stride))
1695 ;; Initialize the index on the first use of this command
1696 ;; so that the first M-p gets index 0, and the first M-n gets
1697 ;; index -1.
1698 ((> stride 0) (1- stride))
1699 (t stride))
1700 len))
1701
8c0aaf40 1702(defun vc-previous-comment (arg)
6f1ecae4
AS
1703 "Cycle backwards through comment history.
1704With a numeric prefix ARG, go back ARG comments."
8c0aaf40
ER
1705 (interactive "*p")
1706 (let ((len (ring-length vc-comment-ring)))
0e362f54
GM
1707 (if (<= len 0)
1708 (progn (message "Empty comment ring") (ding))
1709 (erase-buffer)
1710 (setq vc-comment-ring-index (vc-new-comment-index arg len))
1711 (message "Comment %d" (1+ vc-comment-ring-index))
1712 (insert (ring-ref vc-comment-ring vc-comment-ring-index)))))
8c0aaf40
ER
1713
1714(defun vc-next-comment (arg)
6f1ecae4
AS
1715 "Cycle forwards through comment history.
1716With a numeric prefix ARG, go forward ARG comments."
8c0aaf40
ER
1717 (interactive "*p")
1718 (vc-previous-comment (- arg)))
1719
0e362f54 1720(defun vc-comment-search-reverse (str &optional stride)
6f1ecae4
AS
1721 "Search backwards through comment history for substring match of STR.
1722If the optional argument STRIDE is present, that is a step-width to use
1723when going through the comment ring."
0e362f54
GM
1724 ;; Why substring rather than regexp ? -sm
1725 (interactive
1726 (list (read-string "Comment substring: " nil nil vc-last-comment-match)))
1727 (unless stride (setq stride 1))
8c0aaf40
ER
1728 (if (string= str "")
1729 (setq str vc-last-comment-match)
1730 (setq vc-last-comment-match str))
0e362f54
GM
1731 (let* ((str (regexp-quote str))
1732 (len (ring-length vc-comment-ring))
1733 (n (vc-new-comment-index stride len)))
1734 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
1735 (not (string-match str (ring-ref vc-comment-ring n))))
1736 (setq n (+ n stride)))
1737 (setq vc-comment-ring-index n)
1738 (vc-previous-comment 0)))
8c0aaf40
ER
1739
1740(defun vc-comment-search-forward (str)
6f1ecae4 1741 "Search forwards through comment history for a substring match of STR."
0e362f54
GM
1742 (interactive
1743 (list (read-string "Comment substring: " nil nil vc-last-comment-match)))
1744 (vc-comment-search-reverse str -1))
594722a8
ER
1745
1746;; Additional entry points for examining version histories
1747
637a8ae9 1748;;;###autoload
97d3f950 1749(defun vc-diff (historic &optional not-urgent)
e8ee1ccf 1750 "Display diffs between file versions.
6f1ecae4
AS
1751Normally this compares the current file and buffer with the most
1752recent checked in version of that file. This uses no arguments. With
1753a prefix argument HISTORIC, it reads the file name to use and two
1754version designators specifying which versions to compare. The
1755optional argument NOT-URGENT non-nil means it is ok to say no to
1756saving the buffer."
48078f8f 1757 (interactive (list current-prefix-arg t))
594722a8
ER
1758 (if historic
1759 (call-interactively 'vc-version-diff)
2c87edc1 1760 (vc-ensure-vc-buffer)
0e362f54 1761 (let ((file buffer-file-name))
c0d66cb2 1762 (vc-buffer-sync not-urgent)
0e362f54
GM
1763 (if (vc-workfile-unchanged-p buffer-file-name)
1764 (message "No changes to %s since latest version" file)
1765 (vc-version-diff file nil nil)))))
594722a8
ER
1766
1767(defun vc-version-diff (file rel1 rel2)
6f1ecae4
AS
1768 "List the differences between FILE's versions REL1 and REL2.
1769If REL1 is empty or nil it means to use the current workfile version;
1770REL2 empty or nil means the current file contents. FILE may also be
1771a directory, in that case, generate diffs between the correponding
1772versions of all registered files in or below it."
0e362f54 1773 (interactive
4e6473c8
GM
1774 (let ((file (expand-file-name
1775 (read-file-name (if buffer-file-name
1776 "File or dir to diff: (default visited file) "
1777 "File or dir to diff: ")
1778 default-directory buffer-file-name t)))
c0d66cb2
RS
1779 (rel1-default nil) (rel2-default nil))
1780 ;; compute default versions based on the file state
1781 (cond
0e362f54
GM
1782 ;; if it's a directory, don't supply any version default
1783 ((file-directory-p file)
c0d66cb2 1784 nil)
0e362f54
GM
1785 ;; if the file is not up-to-date, use current version as older version
1786 ((not (vc-up-to-date-p file))
c0d66cb2
RS
1787 (setq rel1-default (vc-workfile-version file)))
1788 ;; if the file is not locked, use last and previous version as default
1789 (t
f1180544 1790 (setq rel1-default (vc-call previous-version file
869131a5 1791 (vc-workfile-version file)))
0e362f54 1792 (if (string= rel1-default "") (setq rel1-default nil))
c0d66cb2
RS
1793 (setq rel2-default (vc-workfile-version file))))
1794 ;; construct argument list
0e362f54 1795 (list file
8e710301
RS
1796 (read-string (if rel1-default
1797 (concat "Older version: (default "
1798 rel1-default ") ")
1799 "Older version: ")
1800 nil nil rel1-default)
1801 (read-string (if rel2-default
1802 (concat "Newer version: (default "
1803 rel2-default ") ")
ba27415c 1804 "Newer version (default: current source): ")
8e710301 1805 nil nil rel2-default))))
594722a8 1806 (if (file-directory-p file)
ffda0460 1807 ;; recursive directory diff
ad339989
AS
1808 (progn
1809 (vc-setup-buffer "*vc-diff*")
ffda0460
AS
1810 (if (string-equal rel1 "") (setq rel1 nil))
1811 (if (string-equal rel2 "") (setq rel2 nil))
ad339989
AS
1812 (let ((inhibit-read-only t))
1813 (insert "Diffs between "
1814 (or rel1 "last version checked in")
1815 " and "
1816 (or rel2 "current workfile(s)")
1817 ":\n\n"))
2c87edc1
AS
1818 (let ((dir (file-name-as-directory file)))
1819 (vc-call-backend (vc-responsible-backend dir)
1820 'diff-tree dir rel1 rel2))
0e362f54
GM
1821 (vc-exec-after `(let ((inhibit-read-only t))
1822 (insert "\nEnd of diffs.\n"))))
ffda0460 1823 ;; single file diff
2c87edc1 1824 (vc-diff-internal file rel1 rel2))
ad339989 1825 (set-buffer "*vc-diff*")
0e362f54
GM
1826 (if (and (zerop (buffer-size))
1827 (not (get-buffer-process (current-buffer))))
1828 (progn
1829 (if rel1
1830 (if rel2
1831 (message "No changes to %s between %s and %s" file rel1 rel2)
1832 (message "No changes to %s since %s" file rel1))
1833 (message "No changes to %s since latest version" file))
1834 nil)
1835 (pop-to-buffer (current-buffer))
1836 ;; Gnus-5.8.5 sets up an autoload for diff-mode, even if it's
1837 ;; not available. Work around that.
1838 (if (require 'diff-mode nil t) (diff-mode))
66321b2f
SM
1839 (vc-exec-after '(let ((inhibit-read-only t))
1840 (if (eq (buffer-size) 0)
1841 (insert "No differences found.\n"))
1842 (goto-char (point-min))
1843 (shrink-window-if-larger-than-buffer)))
0e362f54 1844 t))
594722a8 1845
2c87edc1
AS
1846(defun vc-diff-internal (file rel1 rel2)
1847 "Run diff to compare FILE's revisions REL1 and REL2.
1848Output goes to the current buffer, which is assumed properly set up.
1849The exit status of the diff command is returned.
1850
1851This function takes care to set up a proper coding system for diff output.
1852If both revisions are available as local files, then it also does not
1853actually call the backend, but performs a local diff."
1854 (if (or (not rel1) (string-equal rel1 ""))
1855 (setq rel1 (vc-workfile-version file)))
1856 (if (string-equal rel2 "")
1857 (setq rel2 nil))
1858 (let ((file-rel1 (vc-version-backup-file file rel1))
1859 (file-rel2 (if (not rel2)
1860 file
1861 (vc-version-backup-file file rel2)))
1862 (coding-system-for-read (vc-coding-system-for-diff file)))
1863 (if (and file-rel1 file-rel2)
1864 (apply 'vc-do-command "*vc-diff*" 1 "diff" nil
1865 (append (if (listp diff-switches)
1866 diff-switches
1867 (list diff-switches))
1868 (if (listp vc-diff-switches)
1869 vc-diff-switches
1870 (list vc-diff-switches))
1871 (list (file-relative-name file-rel1)
1872 (file-relative-name file-rel2))))
1873 (vc-call diff file rel1 rel2))))
1874
acc5b122 1875(defmacro vc-diff-switches-list (backend)
6f1ecae4 1876 "Return the list of switches to use for executing diff under BACKEND."
99cb8c8b 1877 `(append
acc5b122
AS
1878 (if (listp diff-switches) diff-switches (list diff-switches))
1879 (if (listp vc-diff-switches) vc-diff-switches (list vc-diff-switches))
b470cb65 1880 (let* ((backend-switches-symbol
4e5f52e5 1881 (intern (concat "vc-" (downcase (symbol-name ,backend))
b470cb65
AS
1882 "-diff-switches")))
1883 (backend-switches
1884 (if (boundp backend-switches-symbol)
1885 (eval backend-switches-symbol)
1886 nil)))
acc5b122
AS
1887 (if (listp backend-switches) backend-switches (list backend-switches)))))
1888
2c87edc1 1889(defun vc-default-diff-tree (backend dir rel1 rel2)
6f1ecae4 1890 "List differences for all registered files at and below DIR.
2c87edc1 1891The meaning of REL1 and REL2 is the same as for `vc-version-diff'."
99cb8c8b 1892 ;; This implementation does an explicit tree walk, and calls
2c87edc1
AS
1893 ;; vc-BACKEND-diff directly for each file. An optimization
1894 ;; would be to use `vc-diff-internal', so that diffs can be local,
1895 ;; and to call it only for files that are actually changed.
1896 ;; However, this is expensive for some backends, and so it is left
1897 ;; to backend-specific implementations.
1898 (setq default-directory dir)
1899 (vc-file-tree-walk
1900 default-directory
1901 (lambda (f)
1902 (vc-exec-after
99cb8c8b 1903 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
2c87edc1 1904 (message "Looking at %s" ',f)
99cb8c8b 1905 (vc-call-backend ',(vc-backend f)
2c87edc1
AS
1906 'diff ',f ',rel1 ',rel2))))))
1907
1908(defun vc-coding-system-for-diff (file)
1909 "Return the coding system for reading diff output for FILE."
1910 (or coding-system-for-read
99cb8c8b 1911 ;; if we already have this file open,
2c87edc1
AS
1912 ;; use the buffer's coding system
1913 (let ((buf (find-buffer-visiting file)))
1914 (if buf (with-current-buffer buf
1915 buffer-file-coding-system)))
1916 ;; otherwise, try to find one based on the file name
f66a6225 1917 (car (find-operation-coding-system 'insert-file-contents file))
2c87edc1
AS
1918 ;; and a final fallback
1919 'undecided))
1920
f1818994
PE
1921;;;###autoload
1922(defun vc-version-other-window (rev)
6f1ecae4
AS
1923 "Visit version REV of the current file in another window.
1924If the current file is named `F', the version is named `F.~REV~'.
1925If `F.~REV~' already exists, use it instead of checking it out again."
0e362f54 1926 (interactive "sVersion to visit (default is workfile version): ")
b6909007 1927 (vc-ensure-vc-buffer)
5e011cb2
SM
1928 (let* ((file buffer-file-name)
1929 (version (if (string-equal rev "")
1930 (vc-workfile-version file)
ac0aae44
AS
1931 rev)))
1932 (switch-to-buffer-other-window (vc-find-version file version))))
1933
1934(defun vc-find-version (file version)
1935 "Read VERSION of FILE into a buffer and return the buffer."
1936 (let ((automatic-backup (vc-version-backup-file-name file version))
fd8160f7
SM
1937 (filebuf (or (get-file-buffer file) (current-buffer)))
1938 (filename (vc-version-backup-file-name file version 'manual)))
1939 (unless (file-exists-p filename)
10b48cc4 1940 (if (file-exists-p automatic-backup)
fd8160f7
SM
1941 (rename-file automatic-backup filename nil)
1942 (message "Checking out %s..." filename)
1943 (with-current-buffer filebuf
1944 (let ((failed t))
1945 (unwind-protect
1946 (let ((coding-system-for-read 'no-conversion)
1947 (coding-system-for-write 'no-conversion))
1948 (with-temp-file filename
1949 (let ((outbuf (current-buffer)))
1950 ;; Change buffer to get local value of
1951 ;; vc-checkout-switches.
1952 (with-current-buffer filebuf
1953 (vc-call find-version file version outbuf))))
1954 (setq failed nil))
1955 (if (and failed (file-exists-p filename))
1956 (delete-file filename))))
1957 (vc-mode-line file))
1958 (message "Checking out %s...done" filename)))
1959 (find-file-noselect filename)))
1960
1961(defun vc-default-find-version (backend file rev buffer)
1962 "Provide the new `find-version' op based on the old `checkout' op.
1963This is only for compatibility with old backends. They should be updated
1964to provide the `find-version' operation instead."
1965 (let ((tmpfile (make-temp-file (expand-file-name file))))
1966 (unwind-protect
1967 (progn
1968 (vc-call-backend backend 'checkout file nil rev tmpfile)
1969 (with-current-buffer buffer
1970 (insert-file-contents-literally tmpfile)))
1971 (delete-file tmpfile))))
f1818994 1972
594722a8
ER
1973;; Header-insertion code
1974
637a8ae9 1975;;;###autoload
594722a8 1976(defun vc-insert-headers ()
6f1ecae4 1977 "Insert headers into a file for use with a version control system.
b524ce9f 1978Headers desired are inserted at point, and are pulled from
0e362f54 1979the variable `vc-BACKEND-header'."
594722a8 1980 (interactive)
b6909007 1981 (vc-ensure-vc-buffer)
594722a8
ER
1982 (save-excursion
1983 (save-restriction
1984 (widen)
1985 (if (or (not (vc-check-headers))
820bde8d 1986 (y-or-n-p "Version headers already exist. Insert another set? "))
594722a8
ER
1987 (progn
1988 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
1989 (comment-start-vc (or (car delims) comment-start "#"))
1990 (comment-end-vc (or (car (cdr delims)) comment-end ""))
0e362f54
GM
1991 (hdsym (vc-make-backend-sym (vc-backend (buffer-file-name))
1992 'header))
1993 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
1994 (mapcar (lambda (s)
1995 (insert comment-start-vc "\t" s "\t"
1996 comment-end-vc "\n"))
594722a8
ER
1997 hdstrings)
1998 (if vc-static-header-alist
0e362f54
GM
1999 (mapcar (lambda (f)
2000 (if (string-match (car f) buffer-file-name)
2001 (insert (format (cdr f) (car hdstrings)))))
594722a8
ER
2002 vc-static-header-alist))
2003 )
2004 )))))
2005
0e362f54 2006(defun vc-clear-headers (&optional file)
099bd78a 2007 "Clear all version headers in the current buffer (or FILE).
6f1ecae4 2008The headers are reset to their non-expanded form."
0e362f54
GM
2009 (let* ((filename (or file buffer-file-name))
2010 (visited (find-buffer-visiting filename))
2011 (backend (vc-backend filename)))
2012 (when (vc-find-backend-function backend 'clear-headers)
6f41eeb5 2013 (if visited
0e362f54
GM
2014 (let ((context (vc-buffer-context)))
2015 ;; save-excursion may be able to relocate point and mark
2016 ;; properly. If it fails, vc-restore-buffer-context
2017 ;; will give it a second try.
2018 (save-excursion
2019 (vc-call-backend backend 'clear-headers))
2020 (vc-restore-buffer-context context))
7849e179 2021 (set-buffer (find-file-noselect filename))
0e362f54
GM
2022 (vc-call-backend backend 'clear-headers)
2023 (kill-buffer filename)))))
c8de1d91 2024
b6909007 2025;;;###autoload
099bd78a
SM
2026(defun vc-merge ()
2027 "Merge changes between two versions into the current buffer's file.
2028This asks for two versions to merge from in the minibuffer. If the
2029first version is a branch number, then merge all changes from that
2030branch. If the first version is empty, merge news, i.e. recent changes
2031from the current branch.
0e362f54
GM
2032
2033See Info node `Merging'."
099bd78a 2034 (interactive)
ccb141e8
AS
2035 (vc-ensure-vc-buffer)
2036 (vc-buffer-sync)
2037 (let* ((file buffer-file-name)
2038 (backend (vc-backend file))
0e362f54 2039 (state (vc-state file))
099bd78a 2040 first-version second-version status)
0e362f54 2041 (cond
0e362f54
GM
2042 ((stringp state)
2043 (error "File is locked by %s" state))
2044 ((not (vc-editable-p file))
2045 (if (y-or-n-p
2046 "File must be checked out for merging. Check out now? ")
2047 (vc-checkout file t)
2048 (error "Merge aborted"))))
46e33aee 2049 (setq first-version
099bd78a
SM
2050 (read-string (concat "Branch or version to merge from "
2051 "(default: news on current branch): ")))
2052 (if (string= first-version "")
2053 (if (not (vc-find-backend-function backend 'merge-news))
2054 (error "Sorry, merging news is not implemented for %s" backend)
2055 (setq status (vc-call merge-news file)))
2056 (if (not (vc-find-backend-function backend 'merge))
2057 (error "Sorry, merging is not implemented for %s" backend)
2058 (if (not (vc-branch-p first-version))
46e33aee
TTN
2059 (setq second-version
2060 (read-string "Second version: "
099bd78a
SM
2061 (concat (vc-branch-part first-version) ".")))
2062 ;; We want to merge an entire branch. Set versions
2063 ;; accordingly, so that vc-BACKEND-merge understands us.
2064 (setq second-version first-version)
2065 ;; first-version must be the starting point of the branch
2066 (setq first-version (vc-branch-part first-version)))
2067 (setq status (vc-call merge file first-version second-version))))
2068 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
0e362f54
GM
2069
2070(defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
2071 (vc-resynch-buffer file t (not (buffer-modified-p)))
2072 (if (zerop status) (message "Merge successful")
928ef6d9 2073 (smerge-mode 1)
0e362f54 2074 (if (y-or-n-p "Conflicts detected. Resolve them now? ")
edc37d12 2075 (vc-resolve-conflicts name-A name-B)
0e362f54 2076 (message "File contains conflict markers"))))
ccb141e8
AS
2077
2078;;;###autoload
1168d6c2 2079(defalias 'vc-resolve-conflicts 'smerge-ediff)
18483cf0 2080
2f119435 2081;; The VC directory major mode. Coopt Dired for this.
e1f297e6
ER
2082;; All VC commands get mapped into logical equivalents.
2083
beba4bd9
AS
2084(defvar vc-dired-switches)
2085(defvar vc-dired-terse-mode)
2086
0e362f54
GM
2087(defvar vc-dired-mode-map
2088 (let ((map (make-sparse-keymap))
2089 (vmap (make-sparse-keymap)))
ec402ad4 2090 (define-key map "\C-xv" vmap)
0e362f54 2091 (define-key map "v" vmap)
ec402ad4 2092 (set-keymap-parent vmap vc-prefix-map)
0e362f54
GM
2093 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2094 map))
2095
2f119435 2096(define-derived-mode vc-dired-mode dired-mode "Dired under VC"
0e362f54
GM
2097 "The major mode used in VC directory buffers.
2098
2099It works like Dired, but lists only files under version control, with
2100the current VC state of each file being indicated in the place of the
2101file's link count, owner, group and size. Subdirectories are also
2102listed, and you may insert them into the buffer as desired, like in
2103Dired.
2104
2105All Dired commands operate normally, with the exception of `v', which
2106is redefined as the version control prefix, so that you can type
3d30b8bc
RS
2107`vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2108the file named in the current Dired buffer line. `vv' invokes
2109`vc-next-action' on this file, or on all files currently marked.
2110There is a special command, `*l', to mark all files currently locked."
099bd78a
SM
2111 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2112 ;; We do it here because dired might not be loaded yet
2113 ;; when vc-dired-mode-map is initialized.
2114 (set-keymap-parent vc-dired-mode-map dired-mode-map)
421f0bfe 2115 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
edcb979f 2116 ;; The following is slightly modified from dired.el,
62c2e775
AS
2117 ;; because file lines look a bit different in vc-dired-mode
2118 ;; (the column before the date does not end in a digit).
edcb979f 2119 (set (make-local-variable 'dired-move-to-filename-regexp)
62c2e775 2120 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
570050dc
AS
2121 ;; In some locales, month abbreviations are as short as 2 letters,
2122 ;; and they can be followed by ".".
2123 (month (concat l l "+\\.?"))
2124 (s " ")
2125 (yyyy "[0-9][0-9][0-9][0-9]")
2126 (dd "[ 0-3][0-9]")
2127 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2128 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
2129 (zone "[-+][0-2][0-9][0-5][0-9]")
2130 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
2131 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
2132 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
2133 "\\|" yyyy "-" iso-mm-dd "\\)"))
2134 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
2135 s "+"
2136 "\\(" HH:MM "\\|" yyyy "\\)"))
2137 (western-comma (concat month s "+" dd "," s "+" yyyy))
2138 ;; Japanese MS-Windows ls-lisp has one-digit months, and
2139 ;; omits the Kanji characters after month and day-of-month.
2140 (mm "[ 0-1]?[0-9]")
2141 (japanese
2142 (concat mm l "?" s dd l "?" s "+"
2143 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
2144 ;; the .* below ensures that we find the last match on a line
62c2e775 2145 (concat ".*" s
570050dc
AS
2146 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
2147 s "+")))
a0019b45
AS
2148 (and (boundp 'vc-dired-switches)
2149 vc-dired-switches
2150 (set (make-local-variable 'dired-actual-switches)
2151 vc-dired-switches))
3b574573 2152 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2f119435
AS
2153 (setq vc-dired-mode t))
2154
3b574573
AS
2155(defun vc-dired-toggle-terse-mode ()
2156 "Toggle terse display in VC Dired."
2157 (interactive)
2158 (if (not vc-dired-mode)
2159 nil
2160 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2161 (if vc-dired-terse-mode
2162 (vc-dired-hook)
2163 (revert-buffer))))
2164
3d30b8bc
RS
2165(defun vc-dired-mark-locked ()
2166 "Mark all files currently locked."
2167 (interactive)
2168 (dired-mark-if (let ((f (dired-get-filename nil t)))
2169 (and f
2170 (not (file-directory-p f))
0e362f54 2171 (not (vc-up-to-date-p f))))
3d30b8bc
RS
2172 "locked file"))
2173
2174(define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2175
0e362f54
GM
2176(defun vc-default-dired-state-info (backend file)
2177 (let ((state (vc-state file)))
2178 (cond
2179 ((stringp state) (concat "(" state ")"))
2180 ((eq state 'edited) (concat "(" (vc-user-login-name) ")"))
2181 ((eq state 'needs-merge) "(merge)")
2182 ((eq state 'needs-patch) "(patch)")
2183 ((eq state 'unlocked-changes) "(stale)"))))
b0c9bc8c 2184
b9a35ba5 2185(defun vc-dired-reformat-line (vc-info)
0e362f54 2186 "Reformat a directory-listing line.
b9a35ba5 2187Replace various columns with version control information, VC-INFO.
0e362f54 2188This code, like dired, assumes UNIX -l format."
3d30b8bc 2189 (beginning-of-line)
f1180544 2190 (when (re-search-forward
685e7bca
AS
2191 ;; Match link count, owner, group, size. Group may be missing,
2192 ;; and only the size is present in OS/2 -l format.
2193 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
2194 (line-end-position) t)
2195 (replace-match (substring (concat vc-info " ") 0 10)
2196 t t nil 1)))
3d30b8bc
RS
2197
2198(defun vc-dired-hook ()
0e362f54
GM
2199 "Reformat the listing according to version control.
2200Called by dired after any portion of a vc-dired buffer has been read in."
3d30b8bc 2201 (message "Getting version information... ")
eccceb78 2202 (let (subdir filename (buffer-read-only nil) cvs-dir)
3d30b8bc 2203 (goto-char (point-min))
0e362f54
GM
2204 (while (not (eobp))
2205 (cond
3d30b8bc
RS
2206 ;; subdir header line
2207 ((setq subdir (dired-get-subdir))
0e362f54
GM
2208 ;; if the backend supports it, get the state
2209 ;; of all files in this directory at once
2210 (let ((backend (vc-responsible-backend subdir)))
2211 (if (vc-find-backend-function backend 'dir-state)
2212 (vc-call-backend backend 'dir-state subdir)))
3d30b8bc
RS
2213 (forward-line 1)
2214 ;; erase (but don't remove) the "total" line
0e362f54
GM
2215 (delete-region (point) (line-end-position))
2216 (beginning-of-line)
2217 (forward-line 1))
2218 ;; file line
3d30b8bc
RS
2219 ((setq filename (dired-get-filename nil t))
2220 (cond
3b574573 2221 ;; subdir
3d30b8bc 2222 ((file-directory-p filename)
0e362f54
GM
2223 (cond
2224 ((member (file-name-nondirectory filename)
3b574573
AS
2225 vc-directory-exclusion-list)
2226 (let ((pos (point)))
2227 (dired-kill-tree filename)
2228 (goto-char pos)
2229 (dired-kill-line)))
2230 (vc-dired-terse-mode
633cee46
AS
2231 ;; Don't show directories in terse mode. Don't use
2232 ;; dired-kill-line to remove it, because in recursive listings,
2233 ;; that would remove the directory contents as well.
0e362f54 2234 (delete-region (line-beginning-position)
633cee46 2235 (progn (forward-line 1) (point))))
3b574573
AS
2236 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2237 (dired-kill-line))
2238 (t
3d30b8bc 2239 (vc-dired-reformat-line nil)
3b574573
AS
2240 (forward-line 1))))
2241 ;; ordinary file
0e362f54
GM
2242 ((and (vc-backend filename)
2243 (not (and vc-dired-terse-mode
2244 (vc-up-to-date-p filename))))
2245 (vc-dired-reformat-line (vc-call dired-state-info filename))
3d30b8bc 2246 (forward-line 1))
0e362f54 2247 (t
3d30b8bc
RS
2248 (dired-kill-line))))
2249 ;; any other line
3b574573
AS
2250 (t (forward-line 1))))
2251 (vc-dired-purge))
2252 (message "Getting version information... done")
2253 (save-restriction
2254 (widen)
633cee46
AS
2255 (cond ((eq (count-lines (point-min) (point-max)) 1)
2256 (goto-char (point-min))
2257 (message "No files locked under %s" default-directory)))))
3b574573
AS
2258
2259(defun vc-dired-purge ()
0e362f54 2260 "Remove empty subdirs."
3b574573
AS
2261 (let (subdir)
2262 (goto-char (point-min))
2263 (while (setq subdir (dired-get-subdir))
2264 (forward-line 2)
2265 (if (dired-get-filename nil t)
2266 (if (not (dired-next-subdir 1 t))
2267 (goto-char (point-max)))
2268 (forward-line -2)
2269 (if (not (string= (dired-current-directory) default-directory))
2270 (dired-do-kill-lines t "")
633cee46
AS
2271 ;; We cannot remove the top level directory.
2272 ;; Just make it look a little nicer.
2273 (forward-line 1)
2274 (kill-line)
3b574573
AS
2275 (if (not (dired-next-subdir 1 t))
2276 (goto-char (point-max))))))
2277 (goto-char (point-min))))
2f119435 2278
0e362f54
GM
2279(defun vc-dired-buffers-for-dir (dir)
2280 "Return a list of all vc-dired buffers that currently display DIR."
2281 (let (result)
099bd78a
SM
2282 ;; Check whether dired is loaded.
2283 (when (fboundp 'dired-buffers-for-dir)
2284 (mapcar (lambda (buffer)
2285 (with-current-buffer buffer
2286 (if vc-dired-mode
2287 (setq result (append result (list buffer))))))
2288 (dired-buffers-for-dir dir)))
0e362f54
GM
2289 result))
2290
2291(defun vc-dired-resynch-file (file)
2292 "Update the entries for FILE in any VC Dired buffers that list it."
2293 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2294 (when buffers
2295 (mapcar (lambda (buffer)
2296 (with-current-buffer buffer
2297 (if (dired-goto-file file)
2298 ;; bind vc-dired-terse-mode to nil so that
2299 ;; files won't vanish when they are checked in
2300 (let ((vc-dired-terse-mode nil))
2301 (dired-do-redisplay 1)))))
2302 buffers))))
2303
637a8ae9 2304;;;###autoload
0e362f54
GM
2305(defun vc-directory (dir read-switches)
2306 "Create a buffer in VC Dired Mode for directory DIR.
2307
2308See Info node `VC Dired Mode'.
2309
2310With prefix arg READ-SWITCHES, specify a value to override
2311`dired-listing-switches' when generating the listing."
2f119435 2312 (interactive "DDired under VC (directory): \nP")
0e362f54 2313 (let ((vc-dired-switches (concat vc-dired-listing-switches
3b574573 2314 (if vc-dired-recurse "R" ""))))
0e362f54 2315 (if read-switches
3b574573
AS
2316 (setq vc-dired-switches
2317 (read-string "Dired listing switches: "
2318 vc-dired-switches)))
3d30b8bc
RS
2319 (require 'dired)
2320 (require 'dired-aux)
0e362f54
GM
2321 (switch-to-buffer
2322 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2323 vc-dired-switches
3d30b8bc 2324 'vc-dired-mode))))
e70bdc98 2325
594722a8
ER
2326
2327;; Named-configuration entry points
2328
0e362f54 2329(defun vc-snapshot-precondition (dir)
6f1ecae4 2330 "Scan the tree below DIR, looking for files not up-to-date.
099bd78a
SM
2331If any file is not up-to-date, return the name of the first such file.
2332\(This means, neither snapshot creation nor retrieval is allowed.\)
2333If one or more of the files are currently visited, return `visited'.
2334Otherwise, return nil."
503b5c85
RS
2335 (let ((status nil))
2336 (catch 'vc-locked-example
2337 (vc-file-tree-walk
0e362f54
GM
2338 dir
2339 (lambda (f)
2340 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2341 (if (get-file-buffer f) (setq status 'visited)))))
503b5c85 2342 status)))
594722a8 2343
637a8ae9 2344;;;###autoload
0e362f54 2345(defun vc-create-snapshot (dir name branchp)
6f41eeb5 2346 "Descending recursively from DIR, make a snapshot called NAME.
0e362f54
GM
2347For each registered file, the version level of its latest version
2348becomes part of the named configuration. If the prefix argument
2349BRANCHP is given, the snapshot is made as a new branch and the files
2350are checked out in that new branch."
2351 (interactive
2352 (list (read-file-name "Directory: " default-directory default-directory t)
2353 (read-string "New snapshot name: ")
2354 current-prefix-arg))
2355 (message "Making %s... " (if branchp "branch" "snapshot"))
2356 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2357 (vc-call-backend (vc-responsible-backend dir)
2358 'create-snapshot dir name branchp)
2359 (message "Making %s... done" (if branchp "branch" "snapshot")))
2360
2361(defun vc-default-create-snapshot (backend dir name branchp)
6f41eeb5 2362 (when branchp
0e362f54
GM
2363 (error "VC backend %s does not support module branches" backend))
2364 (let ((result (vc-snapshot-precondition dir)))
503b5c85 2365 (if (stringp result)
0e362f54 2366 (error "File %s is not up-to-date" result)
1dabb4e6 2367 (vc-file-tree-walk
0e362f54
GM
2368 dir
2369 (lambda (f)
2370 (vc-call assign-name f name))))))
594722a8 2371
637a8ae9 2372;;;###autoload
0e362f54 2373(defun vc-retrieve-snapshot (dir name)
099bd78a
SM
2374 "Descending recursively from DIR, retrieve the snapshot called NAME.
2375If NAME is empty, it refers to the latest versions.
2376If locking is used for the files in DIR, then there must not be any
2377locked files at or below DIR (but if NAME is empty, locked files are
2378allowed and simply skipped)."
0e362f54
GM
2379 (interactive
2380 (list (read-file-name "Directory: " default-directory default-directory t)
2381 (read-string "Snapshot name to retrieve (default latest versions): ")))
2382 (let ((update (yes-or-no-p "Update any affected buffers? "))
2383 (msg (if (or (not name) (string= name ""))
2384 (format "Updating %s... " (abbreviate-file-name dir))
2385 (format "Retrieving snapshot into %s... "
2386 (abbreviate-file-name dir)))))
2387 (message msg)
2388 (vc-call-backend (vc-responsible-backend dir)
2389 'retrieve-snapshot dir name update)
2390 (message (concat msg "done"))))
2391
2392(defun vc-default-retrieve-snapshot (backend dir name update)
2393 (if (string= name "")
2394 (progn
2395 (vc-file-tree-walk
2396 dir
2397 (lambda (f) (and
2398 (vc-up-to-date-p f)
2399 (vc-error-occurred
2400 (vc-call checkout f nil "")
2401 (if update (vc-resynch-buffer f t t)))))))
2402 (let ((result (vc-snapshot-precondition dir)))
2403 (if (stringp result)
2404 (error "File %s is locked" result)
2405 (setq update (and (eq result 'visited) update))
2406 (vc-file-tree-walk
2407 dir
fd8092f0
SM
2408 (lambda (f) (vc-error-occurred
2409 (vc-call checkout f nil name)
2410 (if update (vc-resynch-buffer f t t)))))))))
594722a8
ER
2411
2412;; Miscellaneous other entry points
2413
637a8ae9 2414;;;###autoload
594722a8
ER
2415(defun vc-print-log ()
2416 "List the change log of the current buffer in a window."
2417 (interactive)
b6909007
AS
2418 (vc-ensure-vc-buffer)
2419 (let ((file buffer-file-name))
0e362f54 2420 (vc-call print-log file)
ad339989 2421 (set-buffer "*vc*")
0e362f54 2422 (pop-to-buffer (current-buffer))
928ef6d9 2423 (log-view-mode)
0e362f54 2424 (vc-exec-after
99cb8c8b 2425 `(let ((inhibit-read-only t))
0e362f54
GM
2426 (goto-char (point-max)) (forward-line -1)
2427 (while (looking-at "=*\n")
2428 (delete-char (- (match-end 0) (match-beginning 0)))
2429 (forward-line -1))
2430 (goto-char (point-min))
2431 (if (looking-at "[\b\t\n\v\f\r ]+")
2432 (delete-char (- (match-end 0) (match-beginning 0))))
2433 (shrink-window-if-larger-than-buffer)
2434 ;; move point to the log entry for the current version
02e43de8
SM
2435 (vc-call-backend ',(vc-backend file)
2436 'show-log-entry
2437 ',(vc-workfile-version file))
99cb8c8b 2438 (set-buffer-modified-p nil)))))
594722a8 2439
48e9072d 2440(defun vc-default-show-log-entry (backend rev)
928ef6d9 2441 (log-view-goto-rev rev))
02e43de8 2442
0ab66291 2443(defun vc-default-comment-history (backend file)
6f1ecae4 2444 "Return a string with all log entries stored in BACKEND for FILE."
0ab66291 2445 (if (vc-find-backend-function backend 'print-log)
f66a6225 2446 (with-current-buffer "*vc*"
0ab66291
AS
2447 (vc-call print-log file)
2448 (vc-call wash-log file)
2449 (buffer-string))))
2450
2451(defun vc-default-wash-log (backend file)
2452 "Remove all non-comment information from log output.
2453This default implementation works for RCS logs; backends should override
2454it if their logs are not in RCS format."
2455 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
2456 "\\(branches: .*;\n\\)?"
2457 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
2458 (goto-char (point-max)) (forward-line -1)
2459 (while (looking-at "=*\n")
2460 (delete-char (- (match-end 0) (match-beginning 0)))
2461 (forward-line -1))
2462 (goto-char (point-min))
2463 (if (looking-at "[\b\t\n\v\f\r ]+")
2464 (delete-char (- (match-end 0) (match-beginning 0))))
2465 (goto-char (point-min))
2466 (re-search-forward separator nil t)
2467 (delete-region (point-min) (point))
2468 (while (re-search-forward separator nil t)
2469 (delete-region (match-beginning 0) (match-end 0)))))
2470
637a8ae9 2471;;;###autoload
594722a8 2472(defun vc-revert-buffer ()
6f1ecae4 2473 "Revert the current buffer's file to the version it was based on.
9c95ac44 2474This asks for confirmation if the buffer contents are not identical
7849e179
SM
2475to that version. This function does not automatically pick up newer
2476changes found in the master file; use \\[universal-argument] \\[vc-next-action] to do so."
594722a8 2477 (interactive)
b6909007 2478 (vc-ensure-vc-buffer)
d607ebcc
AS
2479 ;; Make sure buffer is saved. If the user says `no', abort since
2480 ;; we cannot show the changes and ask for confirmation to discard them.
2481 (vc-buffer-sync nil)
594722a8 2482 (let ((file buffer-file-name)
221cc4f4
RS
2483 ;; This operation should always ask for confirmation.
2484 (vc-suppress-confirm nil)
ffda0460
AS
2485 (obuf (current-buffer))
2486 status)
c96da2b0
AS
2487 (if (vc-up-to-date-p file)
2488 (unless (yes-or-no-p "File seems up-to-date. Revert anyway? ")
2489 (error "Revert canceled")))
0e362f54 2490 (unless (vc-workfile-unchanged-p file)
a3255400
SM
2491 ;; vc-diff selects the new window, which is not what we want:
2492 ;; if the new window is on another frame, that'd require the user
2493 ;; moving her mouse to answer the yes-or-no-p question.
2494 (let ((win (save-selected-window
2495 (setq status (vc-diff nil t)) (selected-window))))
2496 (vc-exec-after `(message nil))
2497 (when status
2498 (unwind-protect
2499 (unless (yes-or-no-p "Discard changes? ")
ffda0460 2500 (error "Revert canceled"))
a3255400
SM
2501 (select-window win)
2502 (if (one-window-p t)
2503 (if (window-dedicated-p (selected-window))
2504 (make-frame-invisible))
2505 (delete-window))))))
751fa747 2506 (set-buffer obuf)
0e362f54
GM
2507 ;; Do the reverting
2508 (message "Reverting %s..." file)
045e1aa5 2509 (vc-revert-file file)
0e362f54 2510 (message "Reverting %s...done" file)))
594722a8 2511
9f30fc99
AS
2512;;;###autoload
2513(defun vc-update ()
2514 "Update the current buffer's file to the latest version on its branch.
2515If the file contains no changes, and is not locked, then this simply replaces
2516the working file with the latest version on its branch. If the file contains
f1180544 2517changes, and the backend supports merging news, then any recent changes from
9f30fc99
AS
2518the current branch are merged into the working file."
2519 (interactive)
2520 (vc-ensure-vc-buffer)
2521 (vc-buffer-sync nil)
2522 (let ((file buffer-file-name))
2523 (if (vc-up-to-date-p file)
2524 (vc-checkout file nil "")
2525 (if (eq (vc-checkout-model file) 'locking)
2526 (if (eq (vc-state file) 'edited)
f1180544
JB
2527 (error
2528 (substitute-command-keys
9f30fc99 2529 "File is locked--type \\[vc-revert-buffer] to discard changes"))
f1180544 2530 (error
9f30fc99 2531 (substitute-command-keys
f1180544 2532 "Unexpected file state (%s)--type \\[vc-next-action] to correct")
9f30fc99
AS
2533 (vc-state file)))
2534 (if (not (vc-find-backend-function (vc-backend file) 'merge-news))
f1180544 2535 (error "Sorry, merging news is not implemented for %s"
9f30fc99
AS
2536 (vc-backend file))
2537 (vc-call merge-news file)
2538 (vc-resynch-window file t t))))))
2539
ffda0460 2540(defun vc-version-backup-file (file &optional rev)
66321b2f
SM
2541 "Return name of backup file for revision REV of FILE.
2542If version backups should be used for FILE, and there exists
ffda0460 2543such a backup for REV or the current workfile version of file,
66321b2f 2544return its name; otherwise return nil."
10b48cc4 2545 (when (vc-call make-version-backups-p file)
ffda0460 2546 (let ((backup-file (vc-version-backup-file-name file rev)))
10b48cc4
AS
2547 (if (file-exists-p backup-file)
2548 backup-file
2549 ;; there is no automatic backup, but maybe the user made one manually
2550 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2551 (if (file-exists-p backup-file)
2552 backup-file)))))
ffda0460 2553
045e1aa5
AS
2554(defun vc-revert-file (file)
2555 "Revert FILE back to the version it was based on."
045e1aa5
AS
2556 (with-vc-properties
2557 file
ffda0460 2558 (let ((backup-file (vc-version-backup-file file)))
bbfc07d3 2559 (when backup-file
ffda0460 2560 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
bbfc07d3
AS
2561 (vc-delete-automatic-version-backups file))
2562 (vc-call revert file backup-file))
a3255400
SM
2563 `((vc-state . up-to-date)
2564 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
045e1aa5
AS
2565 (vc-resynch-buffer file t t))
2566
637a8ae9 2567;;;###autoload
594722a8 2568(defun vc-cancel-version (norevert)
34291cd2 2569 "Get rid of most recently checked in version of this file.
099bd78a 2570A prefix argument NOREVERT means do not revert the buffer afterwards."
594722a8 2571 (interactive "P")
b6909007 2572 (vc-ensure-vc-buffer)
099bd78a
SM
2573 (let* ((file (buffer-file-name))
2574 (backend (vc-backend file))
2575 (target (vc-workfile-version file))
7e48e092 2576 (config (current-window-configuration)) done)
0e362f54 2577 (cond
099bd78a 2578 ((not (vc-find-backend-function backend 'cancel-version))
0e362f54 2579 (error "Sorry, canceling versions is not supported under %s" backend))
099bd78a 2580 ((not (vc-call latest-on-branch-p file))
0e362f54 2581 (error "This is not the latest version; VC cannot cancel it"))
099bd78a 2582 ((not (vc-up-to-date-p file))
0e362f54 2583 (error (substitute-command-keys "File is not up to date; use \\[vc-revert-buffer] to discard changes"))))
7e48e092 2584 (if (null (yes-or-no-p (format "Remove version %s from master? " target)))
099bd78a 2585 (error "Aborted")
0e362f54
GM
2586 (setq norevert (or norevert (not
2587 (yes-or-no-p "Revert buffer to most recent remaining version? "))))
2588
099bd78a
SM
2589 (message "Removing last change from %s..." file)
2590 (with-vc-properties
2591 file
2592 (vc-call cancel-version file norevert)
a3255400 2593 `((vc-state . ,(if norevert 'edited 'up-to-date))
46e33aee
TTN
2594 (vc-checkout-time . ,(if norevert
2595 0
099bd78a 2596 (nth 5 (file-attributes file))))
a3255400 2597 (vc-workfile-version . nil)))
099bd78a
SM
2598 (message "Removing last change from %s...done" file)
2599
2600 (cond
2601 (norevert ;; clear version headers and mark the buffer modified
2602 (set-visited-file-name file)
2603 (when (not vc-make-backup-files)
2604 ;; inhibit backup for this buffer
2605 (make-local-variable 'backup-inhibited)
2606 (setq backup-inhibited t))
2607 (setq buffer-read-only nil)
2608 (vc-clear-headers)
2609 (vc-mode-line file)
2610 (vc-dired-resynch-file file))
2611 (t ;; revert buffer to file on disk
2612 (vc-resynch-buffer file t t)))
0e362f54
GM
2613 (message "Version %s has been removed from the master" target))))
2614
76e5906d 2615;;;###autoload
1d502d5a 2616(defun vc-switch-backend (file backend)
7849e179 2617 "Make BACKEND the current version control system for FILE.
1d502d5a
AS
2618FILE must already be registered in BACKEND. The change is not
2619permanent, only for the current session. This function only changes
7849e179
SM
2620VC's perspective on FILE, it does not register or unregister it.
2621By default, this command cycles through the registered backends.
2622To get a prompt, use a prefix argument."
2623 (interactive
1d502d5a
AS
2624 (list
2625 buffer-file-name
7849e179
SM
2626 (let ((backend (vc-backend buffer-file-name))
2627 (backends nil))
2628 ;; Find the registered backends.
2629 (dolist (backend vc-handled-backends)
2630 (when (vc-call-backend backend 'registered buffer-file-name)
2631 (push backend backends)))
2632 ;; Find the next backend.
ceec5a0c 2633 (let ((def (car (delq backend (append (memq backend backends) backends))))
7849e179
SM
2634 (others (delete backend backends)))
2635 (cond
2636 ((null others) (error "No other backend to switch to"))
2637 (current-prefix-arg
2638 (intern
2639 (upcase
2640 (completing-read
2641 (format "Switch to backend [%s]: " def)
2642 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2643 nil t nil nil (downcase (symbol-name def))))))
2644 (t def))))))
ceec5a0c 2645 (unless (eq backend (vc-backend file))
ceec5a0c
SM
2646 (vc-file-clearprops file)
2647 (vc-file-setprop file 'vc-backend backend)
2648 ;; Force recomputation of the state
a3255400
SM
2649 (unless (vc-call-backend backend 'registered file)
2650 (vc-file-clearprops file)
2651 (error "%s is not registered in %s" file backend))
ceec5a0c 2652 (vc-mode-line file)))
1d502d5a 2653
21b50296 2654;;;###autoload
1d502d5a 2655(defun vc-transfer-file (file new-backend)
ceec5a0c 2656 "Transfer FILE to another version control system NEW-BACKEND.
1d502d5a 2657If NEW-BACKEND has a higher precedence than FILE's current backend
ceec5a0c 2658\(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
1d502d5a
AS
2659NEW-BACKEND, using the version number from the current backend as the
2660base level. If NEW-BACKEND has a lower precedence than the current
2661backend, then commit all changes that were made under the current
2662backend to NEW-BACKEND, and unregister FILE from the current backend.
2663\(If FILE is not yet registered under NEW-BACKEND, register it.)"
72cfc5fb
AS
2664 (let* ((old-backend (vc-backend file))
2665 (edited (memq (vc-state file) '(edited needs-merge)))
2666 (registered (vc-call-backend new-backend 'registered file))
2667 (move
2668 (and registered ; Never move if not registered in new-backend yet.
2669 ;; move if new-backend comes later in vc-handled-backends
2670 (or (memq new-backend (memq old-backend vc-handled-backends))
ffda0460 2671 (y-or-n-p "Final transfer? "))))
72cfc5fb 2672 (comment nil))
1d502d5a 2673 (if (eq old-backend new-backend)
72cfc5fb
AS
2674 (error "%s is the current backend of %s" new-backend file))
2675 (if registered
2676 (set-file-modes file (logior (file-modes file) 128))
2677 ;; `registered' might have switched under us.
2678 (vc-switch-backend file old-backend)
ffda0460 2679 (let* ((rev (vc-workfile-version file))
c1b1b393 2680 (modified-file (and edited (make-temp-file file)))
ffda0460 2681 (unmodified-file (and modified-file (vc-version-backup-file file))))
72cfc5fb
AS
2682 ;; Go back to the base unmodified file.
2683 (unwind-protect
2684 (progn
ffda0460 2685 (when modified-file
c1b1b393 2686 (copy-file file modified-file 'ok-if-already-exists)
ffda0460
AS
2687 ;; If we have a local copy of the unmodified file, handle that
2688 ;; here and not in vc-revert-file because we don't want to
2689 ;; delete that copy -- it is still useful for OLD-BACKEND.
2690 (if unmodified-file
2691 (copy-file unmodified-file file 'ok-if-already-exists)
2692 (if (y-or-n-p "Get base version from master? ")
2693 (vc-revert-file file))))
72cfc5fb 2694 (vc-call-backend new-backend 'receive-file file rev))
ffda0460 2695 (when modified-file
72cfc5fb
AS
2696 (vc-switch-backend file new-backend)
2697 (unless (eq (vc-checkout-model file) 'implicit)
2698 (vc-checkout file t nil))
ffda0460
AS
2699 (rename-file modified-file file 'ok-if-already-exists)
2700 (vc-file-setprop file 'vc-checkout-time nil)))))
72cfc5fb
AS
2701 (when move
2702 (vc-switch-backend file old-backend)
2703 (setq comment (vc-call comment-history file))
2704 (vc-call unregister file))
2705 (vc-switch-backend file new-backend)
2706 (when (or move edited)
1d502d5a 2707 (vc-file-setprop file 'vc-state 'edited)
ffda0460 2708 (vc-mode-line file)
0ab66291 2709 (vc-checkin file nil comment (stringp comment)))))
1d502d5a 2710
72cfc5fb
AS
2711(defun vc-default-unregister (backend file)
2712 "Default implementation of `vc-unregister', signals an error."
2713 (error "Unregistering files is not supported for %s" backend))
2714
2715(defun vc-default-receive-file (backend file rev)
2716 "Let BACKEND receive FILE from another version control system."
2717 (vc-call-backend backend 'register file rev ""))
2718
0e362f54
GM
2719(defun vc-rename-master (oldmaster newfile templates)
2720 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2721 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2722 (newdir (or (file-name-directory newfile) ""))
2723 (newbase (file-name-nondirectory newfile))
2724 (masters
2725 ;; List of potential master files for `newfile'
2726 (mapcar
2727 (lambda (s) (vc-possible-master s newdir newbase))
2728 templates)))
2729 (if (or (file-symlink-p oldmaster)
2730 (file-symlink-p (file-name-directory oldmaster)))
6f1ecae4 2731 (error "This is unsafe in the presence of symbolic links"))
0e362f54
GM
2732 (rename-file
2733 oldmaster
2734 (catch 'found
2735 ;; If possible, keep the master file in the same directory.
2736 (mapcar (lambda (f)
2737 (if (and f (string= (file-name-directory (expand-file-name f))
2738 dir))
2739 (throw 'found f)))
2740 masters)
2741 ;; If not, just use the first possible place.
2742 (mapcar (lambda (f)
2743 (and f
2744 (or (not (setq dir (file-name-directory f)))
2745 (file-directory-p dir))
2746 (throw 'found f)))
2747 masters)
2748 (error "New file lacks a version control directory")))))
594722a8 2749
29fc1ce9 2750;;;###autoload
594722a8 2751(defun vc-rename-file (old new)
34291cd2
RS
2752 "Rename file OLD to NEW, and rename its master file likewise."
2753 (interactive "fVC rename file: \nFRename to: ")
0e362f54
GM
2754 (let ((oldbuf (get-file-buffer old))
2755 (backend (vc-backend old)))
2756 (unless (or (null backend) (vc-find-backend-function backend 'rename-file))
2757 (error "Renaming files under %s is not supported in VC" backend))
d52f0de9 2758 (if (and oldbuf (buffer-modified-p oldbuf))
590cc449 2759 (error "Please save files before moving them"))
594722a8 2760 (if (get-file-buffer new)
590cc449 2761 (error "Already editing new file name"))
d52f0de9
RS
2762 (if (file-exists-p new)
2763 (error "New file already exists"))
0e362f54
GM
2764 (when backend
2765 (if (and backend (not (vc-up-to-date-p old)))
2766 (error "Please check in files before moving them"))
2767 (vc-call-backend backend 'rename-file old new))
2768 ;; Move the actual file (unless the backend did it already)
2769 (if (or (not backend) (file-exists-p old))
2770 (rename-file old new))
2771 ;; ?? Renaming a file might change its contents due to keyword expansion.
2772 ;; We should really check out a new copy if the old copy was precisely equal
2773 ;; to some checked in version. However, testing for this is tricky....
594722a8 2774 (if oldbuf
0e362f54 2775 (with-current-buffer oldbuf
4c145b9e
RS
2776 (let ((buffer-read-only buffer-read-only))
2777 (set-visited-file-name new))
2778 (vc-backend new)
2779 (vc-mode-line new)
0e362f54
GM
2780 (set-buffer-modified-p nil)))))
2781
2782;; Only defined in very recent Emacsen
2783(defvar small-temporary-file-directory nil)
594722a8 2784
637a8ae9 2785;;;###autoload
f35ecf88 2786(defun vc-update-change-log (&rest args)
0e362f54 2787 "Find change log file and add entries from recent version control logs.
d68e6990 2788Normally, find log entries for all registered files in the default
0e362f54 2789directory.
d68e6990 2790
099bd78a 2791With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
d68e6990
RS
2792
2793With any numeric prefix arg, find log entries for all currently visited
2794files that are under version control. This puts all the entries in the
2795log for the default directory, which may not be appropriate.
2796
099bd78a 2797From a program, any ARGS are assumed to be filenames for which
0e362f54 2798log entries should be gathered."
67242a23
RM
2799 (interactive
2800 (cond ((consp current-prefix-arg) ;C-u
2801 (list buffer-file-name))
2802 (current-prefix-arg ;Numeric argument.
2803 (let ((files nil)
2804 (buffers (buffer-list))
2805 file)
2806 (while buffers
2807 (setq file (buffer-file-name (car buffers)))
f3c61d82 2808 (and file (vc-backend file)
4b40fdea 2809 (setq files (cons file files)))
67242a23 2810 (setq buffers (cdr buffers)))
4b40fdea
PE
2811 files))
2812 (t
0e362f54
GM
2813 ;; Don't supply any filenames to backend; this means
2814 ;; it should find all relevant files relative to
2815 ;; the default-directory.
73a9679c 2816 nil)))
0e362f54
GM
2817 (vc-call-backend (vc-responsible-backend default-directory)
2818 'update-changelog args))
2819
2820(defun vc-default-update-changelog (backend files)
099bd78a
SM
2821 "Default implementation of update-changelog.
2822Uses `rcs2log' which only works for RCS and CVS."
0e362f54 2823 ;; FIXME: We (c|sh)ould add support for cvs2cl
449decf5 2824 (let ((odefault default-directory)
124c852b
RS
2825 (changelog (find-change-log))
2826 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
c1b1b393 2827 (tempfile (make-temp-file
57c298c4
EZ
2828 (expand-file-name "vc"
2829 (or small-temporary-file-directory
2830 temporary-file-directory))))
b91916f3 2831 (full-name (or add-log-full-name
8172cd86
AS
2832 (user-full-name)
2833 (user-login-name)
2834 (format "uid%d" (number-to-string (user-uid)))))
b91916f3
RS
2835 (mailing-address (or add-log-mailing-address
2836 user-mail-address)))
124c852b 2837 (find-file-other-window changelog)
41dfb835
RS
2838 (barf-if-buffer-read-only)
2839 (vc-buffer-sync)
2840 (undo-boundary)
2841 (goto-char (point-min))
2842 (push-mark)
2843 (message "Computing change log entries...")
4b40fdea 2844 (message "Computing change log entries... %s"
124c852b
RS
2845 (unwind-protect
2846 (progn
0e362f54 2847 (setq default-directory odefault)
6f41eeb5
DL
2848 (if (eq 0 (apply 'call-process
2849 (expand-file-name "rcs2log"
2850 exec-directory)
0e362f54
GM
2851 nil (list t tempfile) nil
2852 "-c" changelog
2853 "-u" (concat (vc-user-login-name)
2854 "\t" full-name
2855 "\t" mailing-address)
2856 (mapcar
2857 (lambda (f)
2858 (file-relative-name
2859 (if (file-name-absolute-p f)
2860 f
2861 (concat odefault f))))
2862 files)))
2863 "done"
124c852b
RS
2864 (pop-to-buffer
2865 (set-buffer (get-buffer-create "*vc*")))
2866 (erase-buffer)
2867 (insert-file tempfile)
2868 "failed"))
0e362f54 2869 (setq default-directory (file-name-directory changelog))
124c852b 2870 (delete-file tempfile)))))
7d2d9482 2871
ec402ad4 2872;; Annotate functionality
7d2d9482 2873
f80f7bc2
RS
2874;; Declare globally instead of additional parameter to
2875;; temp-buffer-show-function (not possible to pass more than one
75665141
AS
2876;; parameter). The use of annotate-ratio is deprecated in favor of
2877;; annotate-mode, which replaces it with the more sensible "span-to
2878;; days", along with autoscaling support.
099bd78a
SM
2879(defvar vc-annotate-ratio nil "Global variable.")
2880(defvar vc-annotate-backend nil "Global variable.")
0e362f54 2881
f66a6225
SM
2882(defconst vc-annotate-font-lock-keywords
2883 ;; The fontification is done by vc-annotate-lines instead of font-lock.
2884 '((vc-annotate-lines)))
2885
0e362f54 2886(defun vc-annotate-get-backend (buffer)
099bd78a 2887 "Return the backend matching \"Annotate\" buffer BUFFER.
0ff9b955 2888Return nil if no match made. Associations are made based on
0e362f54
GM
2889`vc-annotate-buffers'."
2890 (cdr (assoc buffer vc-annotate-buffers)))
7d2d9482 2891
0e362f54 2892(define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
6f1ecae4 2893 "Major mode for output buffers of the `vc-annotate' command.
7d2d9482
RS
2894
2895You can use the mode-specific menu to alter the time-span of the used
2896colors. See variable `vc-annotate-menu-elements' for customizing the
2897menu items."
f66a6225
SM
2898 (set (make-local-variable 'truncate-lines) t)
2899 (set (make-local-variable 'font-lock-defaults)
2900 '(vc-annotate-font-lock-keywords t))
7d2d9482
RS
2901 (vc-annotate-add-menu))
2902
75665141 2903(defun vc-annotate-display-default (&optional ratio)
6f1ecae4
AS
2904 "Display the output of \\[vc-annotate] using the default color range.
2905The color range is given by `vc-annotate-color-map', scaled by RATIO
2906if present. The current time is used as the offset."
0e362f54 2907 (interactive "e")
f80f7bc2 2908 (message "Redisplaying annotation...")
99cb8c8b 2909 (vc-annotate-display
75665141 2910 (if ratio (vc-annotate-time-span vc-annotate-color-map ratio)))
f80f7bc2 2911 (message "Redisplaying annotation...done"))
7d2d9482 2912
75665141 2913(defun vc-annotate-display-autoscale (&optional full)
6f1ecae4
AS
2914 "Highlight the output of \\[vc-annotate]] using an autoscaled color map.
2915Autoscaling means that the map is scaled from the current time to the
2916oldest annotation in the buffer, or, with argument FULL non-nil, to
2917cover the range from the oldest annotation to the newest."
75665141
AS
2918 (interactive)
2919 (let ((newest 0.0)
2920 (oldest 999999.) ;Any CVS users at the founding of Rome?
2921 (current (vc-annotate-convert-time (current-time)))
2922 date)
2923 (message "Redisplaying annotation...")
2924 ;; Run through this file and find the oldest and newest dates annotated.
2925 (save-excursion
2926 (goto-char (point-min))
2927 (while (setq date (vc-call-backend vc-annotate-backend 'annotate-time))
2928 (if (> date newest)
2929 (setq newest date))
2930 (if (< date oldest)
2931 (setq oldest date))))
2932 (vc-annotate-display
2933 (vc-annotate-time-span ;return the scaled colormap.
2934 vc-annotate-color-map
99cb8c8b 2935 (/ (- (if full newest current) oldest)
75665141
AS
2936 (vc-annotate-car-last-cons vc-annotate-color-map)))
2937 (if full newest))
99cb8c8b
SS
2938 (message "Redisplaying annotation...done \(%s\)"
2939 (if full
2940 (format "Spanned from %.1f to %.1f days old"
75665141
AS
2941 (- current oldest)
2942 (- current newest))
2943 (format "Spanned to %.1f days old" (- current oldest))))))
2944
2945;; Menu -- Using easymenu.el
7d2d9482 2946(defun vc-annotate-add-menu ()
0e362f54 2947 "Add the menu 'Annotate' to the menu bar in VC-Annotate mode."
75665141
AS
2948 (let ((menu-elements vc-annotate-menu-elements)
2949 (menu-def
2950 '("VC-Annotate"
2951 ["Default" (unless (null vc-annotate-display-mode)
2952 (setq vc-annotate-display-mode nil)
2953 (vc-annotate-display-select))
2954 :style toggle :selected (null vc-annotate-display-mode)]))
2955 (oldest-in-map (vc-annotate-car-last-cons vc-annotate-color-map)))
7d2d9482
RS
2956 (while menu-elements
2957 (let* ((element (car menu-elements))
75665141 2958 (days (* element oldest-in-map)))
7d2d9482 2959 (setq menu-elements (cdr menu-elements))
99cb8c8b
SS
2960 (setq menu-def
2961 (append menu-def
75665141
AS
2962 `([,(format "Span %.1f days" days)
2963 (unless (and (numberp vc-annotate-display-mode)
2964 (= vc-annotate-display-mode ,days))
2965 (vc-annotate-display-select nil ,days))
99cb8c8b 2966 :style toggle :selected
75665141
AS
2967 (and (numberp vc-annotate-display-mode)
2968 (= vc-annotate-display-mode ,days)) ])))))
99cb8c8b
SS
2969 (setq menu-def
2970 (append menu-def
75665141
AS
2971 (list
2972 ["Span ..."
99cb8c8b 2973 (let ((days
75665141
AS
2974 (float (string-to-number
2975 (read-string "Span how many days? ")))))
2976 (vc-annotate-display-select nil days)) t])
2977 (list "--")
99cb8c8b
SS
2978 (list
2979 ["Span to Oldest"
75665141
AS
2980 (unless (eq vc-annotate-display-mode 'scale)
2981 (vc-annotate-display-select nil 'scale))
99cb8c8b 2982 :style toggle :selected
75665141 2983 (eq vc-annotate-display-mode 'scale)])
99cb8c8b
SS
2984 (list
2985 ["Span Oldest->Newest"
75665141
AS
2986 (unless (eq vc-annotate-display-mode 'fullscale)
2987 (vc-annotate-display-select nil 'fullscale))
99cb8c8b 2988 :style toggle :selected
75665141
AS
2989 (eq vc-annotate-display-mode 'fullscale)])))
2990 ;; Define the menu
2991 (if (or (featurep 'easymenu) (load "easymenu" t))
99cb8c8b 2992 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
75665141
AS
2993 "VC Annotate Display Menu" menu-def))))
2994
2995(defun vc-annotate-display-select (&optional buffer mode)
6f1ecae4
AS
2996 "Highlight the output of \\[vc-annotate].
2997By default, the current buffer is highlighted, unless overridden by
2998BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
2999use; you may override this using the second optional arg MODE."
3000 (interactive)
75665141
AS
3001 (if mode (setq vc-annotate-display-mode mode))
3002 (when buffer
3003 (set-buffer buffer)
3004 (display-buffer buffer))
3005 (if (not vc-annotate-mode) ; Turn on vc-annotate-mode if not done
3006 (vc-annotate-mode))
f66a6225
SM
3007 (cond ((null vc-annotate-display-mode)
3008 (vc-annotate-display-default vc-annotate-ratio))
3009 ;; One of the auto-scaling modes
3010 ((eq vc-annotate-display-mode 'scale)
3011 (vc-annotate-display-autoscale))
3012 ((eq vc-annotate-display-mode 'fullscale)
3013 (vc-annotate-display-autoscale t))
75665141
AS
3014 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
3015 (vc-annotate-display-default
99cb8c8b 3016 (/ vc-annotate-display-mode (vc-annotate-car-last-cons
75665141 3017 vc-annotate-color-map))))
f66a6225
SM
3018 (t (error "No such display mode: %s"
3019 vc-annotate-display-mode))))
0e362f54
GM
3020
3021;;;; (defun vc-BACKEND-annotate-command (file buffer) ...)
3022;;;; Execute "annotate" on FILE by using `call-process' and insert
3023;;;; the contents in BUFFER.
3024
7d2d9482 3025;;;###autoload
afe35502 3026(defun vc-annotate (prefix)
1cec418c
AS
3027 "Display the edit history of the current file using colours.
3028
3029This command creates a buffer that shows, for each line of the current
3030file, when it was last edited and by whom. Additionally, colours are
3031used to show the age of each line--blue means oldest, red means
3032youngest, and intermediate colours indicate intermediate ages. By
3033default, the time scale stretches back one year into the past;
3034everything that is older than that is shown in blue.
3035
3036With a prefix argument, this command asks two questions in the
3037minibuffer. First, you may enter a version number; then the buffer
3038displays and annotates that version instead of the current version
eb407e67 3039\(type RET in the minibuffer to leave that default unchanged). Then,
ecd50f65
AS
3040you are prompted for the time span in days which the color range
3041should cover. For example, a time span of 20 days means that changes
3042over the past 20 days are shown in red to blue, according to their
3043age, and everything that is older than that is shown in blue.
1cec418c
AS
3044
3045Customization variables:
7d2d9482
RS
3046
3047`vc-annotate-menu-elements' customizes the menu elements of the
3048mode-specific menu. `vc-annotate-color-map' and
3049`vc-annotate-very-old-color' defines the mapping of time to
3050colors. `vc-annotate-background' specifies the background color."
afe35502 3051 (interactive "P")
b6909007 3052 (vc-ensure-vc-buffer)
08eab195 3053 (let* ((temp-buffer-name (concat "*Annotate " (buffer-name) "*"))
75665141 3054 (temp-buffer-show-function 'vc-annotate-display-select)
08eab195 3055 (rev (vc-workfile-version (buffer-file-name)))
99cb8c8b
SS
3056 (vc-annotate-version
3057 (if prefix (read-string
3058 (format "Annotate from version: (default %s) " rev)
08eab195 3059 nil nil rev)
75665141 3060 rev)))
99cb8c8b 3061 (if prefix
75665141
AS
3062 (setq vc-annotate-display-mode
3063 (float (string-to-number
99cb8c8b 3064 (read-string "Annotate span days: (default 20) "
75665141
AS
3065 nil nil "20")))))
3066 (setq vc-annotate-backend (vc-backend (buffer-file-name)))
afe35502 3067 (message "Annotating...")
099bd78a
SM
3068 (if (not (vc-find-backend-function vc-annotate-backend 'annotate-command))
3069 (error "Sorry, annotating is not implemented for %s"
3070 vc-annotate-backend))
46e33aee 3071 (with-output-to-temp-buffer temp-buffer-name
0e362f54
GM
3072 (vc-call-backend vc-annotate-backend 'annotate-command
3073 (file-name-nondirectory (buffer-file-name))
afe35502
AS
3074 (get-buffer temp-buffer-name)
3075 vc-annotate-version))
0e362f54
GM
3076 ;; Don't use the temp-buffer-name until the buffer is created
3077 ;; (only after `with-output-to-temp-buffer'.)
6f41eeb5 3078 (setq vc-annotate-buffers
0e362f54 3079 (append vc-annotate-buffers
08eab195
AS
3080 (list (cons (get-buffer temp-buffer-name) vc-annotate-backend))))
3081 (message "Annotating... done")))
7d2d9482 3082
f70419a8
RS
3083(defun vc-annotate-car-last-cons (a-list)
3084 "Return car of last cons in association list A-LIST."
3085 (if (not (eq nil (cdr a-list)))
3086 (vc-annotate-car-last-cons (cdr a-list))
3087 (car (car a-list))))
3088
3089(defun vc-annotate-time-span (a-list span &optional quantize)
6f1ecae4 3090 "Apply factor SPAN to the time-span of association list A-LIST.
0e362f54
GM
3091Return the new alist.
3092Optionally quantize to the factor of QUANTIZE."
7d2d9482 3093 ;; Apply span to each car of every cons
0e362f54 3094 (if (not (eq nil a-list))
f70419a8
RS
3095 (append (list (cons (* (car (car a-list)) span)
3096 (cdr (car a-list))))
0e362f54
GM
3097 (vc-annotate-time-span (nthcdr (or quantize ; optional
3098 1) ; Default to cdr
f70419a8
RS
3099 a-list) span quantize))))
3100
3101(defun vc-annotate-compcar (threshold a-list)
6f1ecae4
AS
3102 "Test successive cons cells of A-LIST against THRESHOLD.
3103Return the first cons cell with a car that is not less than THRESHOLD,
3104nil if no such cell exists."
f70419a8
RS
3105 (let ((i 1)
3106 (tmp-cons (car a-list)))
3107 (while (and tmp-cons (< (car tmp-cons) threshold))
3108 (setq tmp-cons (car (nthcdr i a-list)))
3109 (setq i (+ i 1)))
3110 tmp-cons)) ; Return the appropriate value
3111
75665141 3112(defun vc-annotate-convert-time (time)
6f1ecae4
AS
3113 "Convert a time value to a floating-point number of days.
3114The argument TIME is a list as returned by `current-time' or
3115`encode-time', only the first two elements of that list are considered."
75665141
AS
3116 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
3117
3118(defun vc-annotate-difference (&optional offset)
6f1ecae4
AS
3119 "Return the time span in days to the next annotation.
3120This calls the backend function annotate-time, and returns the
3121difference in days between the time returned and the current time,
3122or OFFSET if present."
75665141
AS
3123 (let ((next-time (vc-call-backend vc-annotate-backend 'annotate-time)))
3124 (if next-time
99cb8c8b 3125 (- (or offset
75665141
AS
3126 (vc-call-backend vc-annotate-backend 'annotate-current-time))
3127 next-time))))
3128
3129(defun vc-default-annotate-current-time (backend)
3130 "Return the current time, encoded as fractional days."
3131 (vc-annotate-convert-time (current-time)))
99cb8c8b 3132
75665141 3133(defun vc-annotate-display (&optional color-map offset)
6f1ecae4 3134 "Highlight `vc-annotate' output in the current buffer.
f66a6225
SM
3135COLOR-MAP, if present, overrides `vc-annotate-color-map'.
3136The annotations are relative to the current time, unless overridden by OFFSET.
6f1ecae4
AS
3137
3138This function is obsolete, and has been replaced by
f66a6225
SM
3139`vc-annotate-display-select'."
3140 (if (and color-map (not (eq color-map vc-annotate-color-map)))
3141 (set (make-local-variable 'vc-annotate-color-map) color-map))
3142 (set (make-local-variable 'vc-annotate-offset) offset)
3143 (font-lock-mode 1))
3144
3145(defvar vc-annotate-offset nil)
3146
3147(defun vc-annotate-lines (limit)
3148 (let (difference)
3149 (while (and (< (point) limit)
3150 (setq difference (vc-annotate-difference vc-annotate-offset)))
3151 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
3152 (cons nil vc-annotate-very-old-color)))
3153 ;; substring from index 1 to remove any leading `#' in the name
3154 (face-name (concat "vc-annotate-face-" (substring (cdr color) 1)))
3155 ;; Make the face if not done.
3156 (face (or (intern-soft face-name)
3157 (let ((tmp-face (make-face (intern face-name))))
3158 (set-face-foreground tmp-face (cdr color))
3159 (if vc-annotate-background
99cb8c8b 3160 (set-face-background tmp-face
75665141 3161 vc-annotate-background))
f66a6225
SM
3162 tmp-face))) ; Return the face
3163 (point (point))
3164 overlay)
f70419a8 3165 (forward-line 1)
f66a6225
SM
3166 (put-text-property point (point) 'face face)))
3167 ;; Pretend to font-lock there were no matches.
3168 nil))
7d2d9482 3169\f
c6d4f628 3170;; Collect back-end-dependent stuff here
594722a8 3171
0e362f54 3172(defalias 'vc-default-logentry-check 'ignore)
594722a8 3173
594722a8
ER
3174(defun vc-check-headers ()
3175 "Check if the current file has any headers in it."
3176 (interactive)
0e362f54 3177 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
594722a8 3178
aae91380
AS
3179(defun vc-default-check-headers (backend)
3180 "Default implementation of check-headers; always returns nil."
3181 nil)
3182
594722a8
ER
3183;; Back-end-dependent stuff ends here.
3184
3185;; Set up key bindings for use while editing log messages
3186
0e362f54 3187(defun vc-log-edit (file)
928ef6d9 3188 "Set up `log-edit' for use with VC on FILE."
099bd78a
SM
3189 (setq default-directory
3190 (if file (file-name-directory file)
3191 (with-current-buffer vc-parent-buffer default-directory)))
928ef6d9
SM
3192 (log-edit 'vc-finish-logentry nil
3193 (if file `(lambda () ',(list (file-name-nondirectory file)))
3194 ;; If FILE is nil, we were called from vc-dired.
3195 (lambda ()
3196 (with-current-buffer vc-parent-buffer
3197 (dired-get-marked-files t)))))
0e362f54
GM
3198 (set (make-local-variable 'vc-log-file) file)
3199 (make-local-variable 'vc-log-version)
099bd78a 3200 (set-buffer-modified-p nil)
0e362f54 3201 (setq buffer-file-name nil))
594722a8 3202
ec402ad4 3203;; These things should probably be generally available
594722a8 3204
2f119435
AS
3205(defun vc-file-tree-walk (dirname func &rest args)
3206 "Walk recursively through DIRNAME.
0e362f54 3207Invoke FUNC f ARGS on each VC-managed file f underneath it."
2f119435
AS
3208 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3209 (message "Traversing directory %s...done" dirname))
02da6253
PE
3210
3211(defun vc-file-tree-walk-internal (file func args)
3212 (if (not (file-directory-p file))
0e362f54 3213 (if (vc-backend file) (apply func file args))
993a1a44 3214 (message "Traversing directory %s..." (abbreviate-file-name file))
02da6253
PE
3215 (let ((dir (file-name-as-directory file)))
3216 (mapcar
0e362f54
GM
3217 (lambda (f) (or
3218 (string-equal f ".")
3219 (string-equal f "..")
3220 (member f vc-directory-exclusion-list)
3221 (let ((dirf (expand-file-name f dir)))
3222 (or
3223 (file-symlink-p dirf);; Avoid possible loops
3224 (vc-file-tree-walk-internal dirf func args)))))
02da6253 3225 (directory-files dir)))))
594722a8
ER
3226
3227(provide 'vc)
3228
ec402ad4
SM
3229;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
3230;;
ec402ad4
SM
3231;; These may be useful to anyone who has to debug or extend the package.
3232;; (Note that this information corresponds to versions 5.x. Some of it
3233;; might have been invalidated by the additions to support branching
3234;; and RCS keyword lookup. AS, 1995/03/24)
3235;;
3236;; A fundamental problem in VC is that there are time windows between
3237;; vc-next-action's computations of the file's version-control state and
3238;; the actions that change it. This is a window open to lossage in a
3239;; multi-user environment; someone else could nip in and change the state
3240;; of the master during it.
3241;;
3242;; The performance problem is that rlog/prs calls are very expensive; we want
3243;; to avoid them as much as possible.
3244;;
3245;; ANALYSIS:
3246;;
3247;; The performance problem, it turns out, simplifies in practice to the
3248;; problem of making vc-state fast. The two other functions that call
3249;; prs/rlog will not be so commonly used that the slowdown is a problem; one
3250;; makes snapshots, the other deletes the calling user's last change in the
3251;; master.
3252;;
3253;; The race condition implies that we have to either (a) lock the master
3254;; during the entire execution of vc-next-action, or (b) detect and
3255;; recover from errors resulting from dispatch on an out-of-date state.
3256;;
3257;; Alternative (a) appears to be infeasible. The problem is that we can't
3258;; guarantee that the lock will ever be removed. Suppose a user starts a
3259;; checkin, the change message buffer pops up, and the user, having wandered
3260;; off to do something else, simply forgets about it?
3261;;
3262;; Alternative (b), on the other hand, works well with a cheap way to speed up
3263;; vc-state. Usually, if a file is registered, we can read its locked/
3264;; unlocked state and its current owner from its permissions.
3265;;
3266;; This shortcut will fail if someone has manually changed the workfile's
3267;; permissions; also if developers are munging the workfile in several
3268;; directories, with symlinks to a master (in this latter case, the
3269;; permissions shortcut will fail to detect a lock asserted from another
3270;; directory).
3271;;
3272;; Note that these cases correspond exactly to the errors which could happen
3273;; because of a competing checkin/checkout race in between two instances of
3274;; vc-next-action.
3275;;
3276;; For VC's purposes, a workfile/master pair may have the following states:
3277;;
3278;; A. Unregistered. There is a workfile, there is no master.
3279;;
3280;; B. Registered and not locked by anyone.
3281;;
3282;; C. Locked by calling user and unchanged.
3283;;
3284;; D. Locked by the calling user and changed.
3285;;
3286;; E. Locked by someone other than the calling user.
3287;;
3288;; This makes for 25 states and 20 error conditions. Here's the matrix:
3289;;
3290;; VC's idea of state
3291;; |
3292;; V Actual state RCS action SCCS action Effect
3293;; A B C D E
3294;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
3295;; B 5 . 6 7 8 co -l get -e checkout
3296;; C 9 10 . 11 12 co -u unget; get revert
3297;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
3298;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
3299;;
3300;; All commands take the master file name as a last argument (not shown).
3301;;
3302;; In the discussion below, a "self-race" is a pathological situation in
3303;; which VC operations are being attempted simultaneously by two or more
3304;; Emacsen running under the same username.
3305;;
3306;; The vc-next-action code has the following windows:
3307;;
3308;; Window P:
3309;; Between the check for existence of a master file and the call to
3310;; admin/checkin in vc-buffer-admin (apparent state A). This window may
3311;; never close if the initial-comment feature is on.
3312;;
3313;; Window Q:
3314;; Between the call to vc-workfile-unchanged-p in and the immediately
3315;; following revert (apparent state C).
3316;;
3317;; Window R:
3318;; Between the call to vc-workfile-unchanged-p in and the following
3319;; checkin (apparent state D). This window may never close.
3320;;
3321;; Window S:
3322;; Between the unlock and the immediately following checkout during a
3323;; revert operation (apparent state C). Included in window Q.
3324;;
3325;; Window T:
3326;; Between vc-state and the following checkout (apparent state B).
3327;;
3328;; Window U:
3329;; Between vc-state and the following revert (apparent state C).
3330;; Includes windows Q and S.
3331;;
3332;; Window V:
3333;; Between vc-state and the following checkin (apparent state
3334;; D). This window may never be closed if the user fails to complete the
3335;; checkin message. Includes window R.
3336;;
3337;; Window W:
3338;; Between vc-state and the following steal-lock (apparent
3339;; state E). This window may never close if the user fails to complete
3340;; the steal-lock message. Includes window X.
3341;;
3342;; Window X:
3343;; Between the unlock and the immediately following re-lock during a
3344;; steal-lock operation (apparent state E). This window may never close
3345;; if the user fails to complete the steal-lock message.
3346;;
3347;; Errors:
3348;;
3349;; Apparent state A ---
3350;;
3351;; 1. File looked unregistered but is actually registered and not locked.
3352;;
3353;; Potential cause: someone else's admin during window P, with
3354;; caller's admin happening before their checkout.
3355;;
3356;; RCS: Prior to version 5.6.4, ci fails with message
3357;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
3358;; ci -i option and the message is "<file>,v: already exists".
3359;; SCCS: admin will fail with error (ad19).
3360;;
3361;; We can let these errors be passed up to the user.
3362;;
3363;; 2. File looked unregistered but is actually locked by caller, unchanged.
3364;;
3365;; Potential cause: self-race during window P.
3366;;
3367;; RCS: Prior to version 5.6.4, reverts the file to the last saved
3368;; version and unlocks it. From 5.6.4 onwards, VC uses the new
3369;; ci -i option, failing with message "<file>,v: already exists".
3370;; SCCS: will fail with error (ad19).
3371;;
3372;; Either of these consequences is acceptable.
3373;;
3374;; 3. File looked unregistered but is actually locked by caller, changed.
3375;;
3376;; Potential cause: self-race during window P.
3377;;
3378;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
3379;; a delta with a null change comment (the -t- switch will be
3380;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
3381;; failing with message "<file>,v: already exists".
3382;; SCCS: will fail with error (ad19).
3383;;
3384;; 4. File looked unregistered but is locked by someone else.
0e362f54 3385;;;
ec402ad4
SM
3386;; Potential cause: someone else's admin during window P, with
3387;; caller's admin happening *after* their checkout.
3388;;
3389;; RCS: Prior to version 5.6.4, ci fails with a
3390;; "no lock set by <user>" message. From 5.6.4 onwards,
3391;; VC uses the new ci -i option, failing with message
3392;; "<file>,v: already exists".
3393;; SCCS: will fail with error (ad19).
3394;;
3395;; We can let these errors be passed up to the user.
3396;;
3397;; Apparent state B ---
3398;;
3399;; 5. File looked registered and not locked, but is actually unregistered.
3400;;
3401;; Potential cause: master file got nuked during window P.
3402;;
3403;; RCS: will fail with "RCS/<file>: No such file or directory"
3404;; SCCS: will fail with error ut4.
3405;;
3406;; We can let these errors be passed up to the user.
3407;;
3408;; 6. File looked registered and not locked, but is actually locked by the
3409;; calling user and unchanged.
3410;;
3411;; Potential cause: self-race during window T.
3412;;
3413;; RCS: in the same directory as the previous workfile, co -l will fail
3414;; with "co error: writable foo exists; checkout aborted". In any other
3415;; directory, checkout will succeed.
3416;; SCCS: will fail with ge17.
3417;;
3418;; Either of these consequences is acceptable.
3419;;
3420;; 7. File looked registered and not locked, but is actually locked by the
3421;; calling user and changed.
3422;;
3423;; As case 6.
3424;;
3425;; 8. File looked registered and not locked, but is actually locked by another
3426;; user.
3427;;
3428;; Potential cause: someone else checks it out during window T.
3429;;
3430;; RCS: co error: revision 1.3 already locked by <user>
3431;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
3432;;
3433;; We can let these errors be passed up to the user.
3434;;
3435;; Apparent state C ---
3436;;
3437;; 9. File looks locked by calling user and unchanged, but is unregistered.
3438;;
3439;; As case 5.
3440;;
3441;; 10. File looks locked by calling user and unchanged, but is actually not
3442;; locked.
3443;;
3444;; Potential cause: a self-race in window U, or by the revert's
3445;; landing during window X of some other user's steal-lock or window S
3446;; of another user's revert.
3447;;
3448;; RCS: succeeds, refreshing the file from the identical version in
3449;; the master.
3450;; SCCS: fails with error ut4 (p file nonexistent).
3451;;
3452;; Either of these consequences is acceptable.
3453;;
3454;; 11. File is locked by calling user. It looks unchanged, but is actually
3455;; changed.
3456;;
3457;; Potential cause: the file would have to be touched by a self-race
3458;; during window Q.
3459;;
3460;; The revert will succeed, removing whatever changes came with
3461;; the touch. It is theoretically possible that work could be lost.
3462;;
3463;; 12. File looks like it's locked by the calling user and unchanged, but
3464;; it's actually locked by someone else.
3465;;
3466;; Potential cause: a steal-lock in window V.
3467;;
3468;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
3469;; SCCS: fails with error un2
3470;;
3471;; We can pass these errors up to the user.
3472;;
3473;; Apparent state D ---
3474;;
3475;; 13. File looks like it's locked by the calling user and changed, but it's
3476;; actually unregistered.
3477;;
3478;; Potential cause: master file got nuked during window P.
3479;;
3480;; RCS: Prior to version 5.6.4, checks in the user's version as an
3481;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
3482;; option, failing with message "no such file or directory".
3483;; SCCS: will fail with error ut4.
3484;;
3485;; This case is kind of nasty. Under RCS prior to version 5.6.4,
3486;; VC may fail to detect the loss of previous version information.
3487;;
3488;; 14. File looks like it's locked by the calling user and changed, but it's
3489;; actually unlocked.
3490;;
3491;; Potential cause: self-race in window V, or the checkin happening
3492;; during the window X of someone else's steal-lock or window S of
3493;; someone else's revert.
3494;;
3495;; RCS: ci will fail with "no lock set by <user>".
3496;; SCCS: delta will fail with error ut4.
3497;;
3498;; 15. File looks like it's locked by the calling user and changed, but it's
3499;; actually locked by the calling user and unchanged.
3500;;
3501;; Potential cause: another self-race --- a whole checkin/checkout
3502;; sequence by the calling user would have to land in window R.
3503;;
3504;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
3505;; RCS: reverts to the file state as of the second user's checkin, leaving
3506;; the file unlocked.
3507;;
3508;; It is theoretically possible that work could be lost under RCS.
3509;;
3510;; 16. File looks like it's locked by the calling user and changed, but it's
3511;; actually locked by a different user.
3512;;
3513;; RCS: ci error: no lock set by <user>
3514;; SCCS: unget will fail with error un2
3515;;
3516;; We can pass these errors up to the user.
3517;;
3518;; Apparent state E ---
3519;;
3520;; 17. File looks like it's locked by some other user, but it's actually
3521;; unregistered.
3522;;
3523;; As case 13.
3524;;
3525;; 18. File looks like it's locked by some other user, but it's actually
3526;; unlocked.
3527;;
3528;; Potential cause: someone released a lock during window W.
3529;;
3530;; RCS: The calling user will get the lock on the file.
3531;; SCCS: unget -n will fail with cm4.
3532;;
3533;; Either of these consequences will be OK.
3534;;
3535;; 19. File looks like it's locked by some other user, but it's actually
3536;; locked by the calling user and unchanged.
3537;;
3538;; Potential cause: the other user relinquishing a lock followed by
3539;; a self-race, both in window W.
3540;;
3541;; Under both RCS and SCCS, both unlock and lock will succeed, making
3542;; the sequence a no-op.
3543;;
3544;; 20. File looks like it's locked by some other user, but it's actually
3545;; locked by the calling user and changed.
3546;;
3547;; As case 19.
3548;;
3549;; PROBLEM CASES:
3550;;
3551;; In order of decreasing severity:
3552;;
3553;; Cases 11 and 15 are the only ones that potentially lose work.
3554;; They would require a self-race for this to happen.
3555;;
3556;; Case 13 in RCS loses information about previous deltas, retaining
3557;; only the information in the current workfile. This can only happen
3558;; if the master file gets nuked in window P.
3559;;
3560;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
3561;; no change comment in the master. This would require a self-race in
3562;; window P or R respectively.
3563;;
3564;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
3565;;
3566;; Unfortunately, it appears to me that no recovery is possible in these
3567;; cases. They don't yield error messages, so there's no way to tell that
3568;; a race condition has occurred.
3569;;
3570;; All other cases don't change either the workfile or the master, and
3571;; trigger command errors which the user will see.
3572;;
3573;; Thus, there is no explicit recovery code.
594722a8
ER
3574
3575;;; vc.el ends here