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