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