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