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