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