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