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