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