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