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