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