b88b6adf94ba21e66cfac997277aa67c50fefeca
[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, 2008
5 ;; Free Software Foundation, Inc.
6
7 ;; Author: FSF (see below for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; Keywords: tools
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Credits:
29
30 ;; VC was initially designed and implemented by Eric S. Raymond
31 ;; <esr@thyrsus.com> in 1992. Over the years, many other people have
32 ;; contributed substantial amounts of work to VC. These include:
33 ;;
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 ;; In July 2007 ESR returned and redesigned the mode to cope better
46 ;; with modern version-control systems that do commits by fileset
47 ;; rather than per individual file.
48 ;;
49 ;; Features in the new version:
50 ;; * Key commands (vc-next-action = C-x v v, vc-print-log = C-x v l, vc-revert
51 ;; = C-x v u, vc-rollback = C-x v c, vc-diff = C-x v =, vc-update = C-x v +)
52 ;; now operate on filesets rather than individual files.
53 ;; * The fileset for a command is either (a) all marked files in VC-dired
54 ;; mode, (b) the currently visited file if it's under version control,
55 ;; or (c) the current directory if the visited buffer is not under
56 ;; version control and a wildcarding-enable flag has been set.
57 ;;
58 ;; If you maintain a client of the mode or customize it in your .emacs,
59 ;; note that some backend functions which formerly took single file arguments
60 ;; now take a list of files. These include: register, checkin, print-log,
61 ;; rollback, and diff.
62
63 ;;; Commentary:
64
65 ;; This mode is fully documented in the Emacs user's manual.
66 ;;
67 ;; Supported version-control systems presently include CVS, RCS, GNU
68 ;; Arch, Subversion, Bzr, Git, Mercurial, Meta-CVS, Monotone and SCCS
69 ;; (or its free replacement, CSSC).
70 ;;
71 ;; Some features will not work with old RCS versions. Where
72 ;; appropriate, VC finds out which version you have, and allows or
73 ;; disallows those features (stealing locks, for example, works only
74 ;; from 5.6.2 onwards).
75 ;; Even initial checkins will fail if your RCS version is so old that ci
76 ;; doesn't understand -t-; this has been known to happen to people running
77 ;; NExTSTEP 3.0.
78 ;;
79 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
80 ;;
81 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
82 ;; to be installed somewhere on Emacs's path for executables.
83 ;;
84 ;; If your site uses the ChangeLog convention supported by Emacs, the
85 ;; function `log-edit-comment-to-change-log' could prove a useful checkin hook,
86 ;; although you might prefer to use C-c C-a (i.e. `log-edit-insert-changelog')
87 ;; from the commit buffer instead or to set `log-edit-setup-invert'.
88 ;;
89 ;; The vc code maintains some internal state in order to reduce expensive
90 ;; version-control operations to a minimum. Some names are only computed
91 ;; once. If you perform version control operations with the backend while
92 ;; vc's back is turned, or move/rename master files while vc is running,
93 ;; vc may get seriously confused. Don't do these things!
94 ;;
95 ;; Developer's notes on some concurrency issues are included at the end of
96 ;; the file.
97 ;;
98 ;; ADDING SUPPORT FOR OTHER BACKENDS
99 ;;
100 ;; VC can use arbitrary version control systems as a backend. To add
101 ;; support for a new backend named SYS, write a library vc-sys.el that
102 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
103 ;; case for the function and library names). VC will use that library if
104 ;; you put the symbol SYS somewhere into the list of
105 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
106 ;; returns non-nil for a file, all SYS-specific versions of VC commands
107 ;; will be available for that file.
108 ;;
109 ;; VC keeps some per-file information in the form of properties (see
110 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
111 ;; do not generally need to be aware of these properties. For example,
112 ;; `vc-sys-working-revision' should compute the working revision and
113 ;; return it; it should not look it up in the property, and it needn't
114 ;; store it there either. However, if a backend-specific function does
115 ;; store a value in a property, that value takes precedence over any
116 ;; value that the generic code might want to set (check for uses of
117 ;; the macro `with-vc-properties' in vc.el).
118 ;;
119 ;; In the list of functions below, each identifier needs to be prepended
120 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
121 ;; `*'), others are optional (`-').
122 ;;
123 ;; BACKEND PROPERTIES
124 ;;
125 ;; * revision-granularity
126 ;;
127 ;; Takes no arguments. Returns either 'file or 'repository. Backends
128 ;; that return 'file have per-file revision numbering; backends
129 ;; that return 'repository have per-repository revision numbering,
130 ;; so a revision level implicitly identifies a changeset
131 ;;
132 ;; STATE-QUERYING FUNCTIONS
133 ;;
134 ;; * registered (file)
135 ;;
136 ;; Return non-nil if FILE is registered in this backend. Both this
137 ;; function as well as `state' should be careful to fail gracefully
138 ;; in the event that the backend executable is absent. It is
139 ;; preferable that this function's body is autoloaded, that way only
140 ;; calling vc-registered does not cause the backend to be loaded
141 ;; (all the vc-FOO-registered functions are called to try to find
142 ;; the controlling backend for FILE.
143 ;;
144 ;; * state (file)
145 ;;
146 ;; Return the current version control state of FILE. For a list of
147 ;; possible values, see `vc-state'. This function should do a full and
148 ;; reliable state computation; it is usually called immediately after
149 ;; C-x v v. If you want to use a faster heuristic when visiting a
150 ;; file, put that into `state-heuristic' below.
151 ;;
152 ;; - state-heuristic (file)
153 ;;
154 ;; If provided, this function is used to estimate the version control
155 ;; state of FILE at visiting time. It should be considerably faster
156 ;; than the implementation of `state'. For a list of possible values,
157 ;; see the doc string of `vc-state'.
158 ;;
159 ;; - dir-state (dir)
160 ;;
161 ;; If provided, this function is used to find the version control
162 ;; state of as many files as possible in DIR, and all subdirectories
163 ;; of DIR, in a fast way; it is used to avoid expensive indivitual
164 ;; vc-state calls. The function should not return anything, but
165 ;; rather store the files' states into the corresponding properties.
166 ;; Two properties are required: `vc-backend' and `vc-state'. (Note:
167 ;; in older versions this method was not required to recurse into
168 ;; subdirectories.)
169 ;;
170 ;; - dir-status (dir update-function status-buffer)
171 ;;
172 ;; Produce RESULT: a list of lists of the form (FILE VC-STATE EXTRA)
173 ;; for the files in DIR.
174 ;; EXTRA can be used for backend specific information about FILE.
175 ;; If a command needs to be run to compute this list, it should be
176 ;; run asynchronously using (current-buffer) as the buffer for the
177 ;; command. When RESULT is computed, it should be passed back by
178 ;; doing: (funcall UPDATE-FUNCTION RESULT STATUS-BUFFER nil).
179 ;; If the backend uses a process filter, hence it produces partial results,
180 ;; they can be passed back by doing:
181 ;; (funcall UPDATE-FUNCTION RESULT STATUS-BUFFER t)
182 ;; and then do a (funcall UPDATE-FUNCTION RESULT STATUS-BUFFER nil)
183 ;; when all the results have been computed.
184 ;; To provide more backend specific functionality for `vc-status'
185 ;; the following functions might be needed: `status-extra-headers',
186 ;; `status-printer', `extra-status-menu' and `status-fileinfo-extra'.
187 ;; This function is used by `vc-status', a replacement for
188 ;; `vc-dired'. vc-status is still under development, and is NOT
189 ;; feature complete. As such, the requirements for this function
190 ;; might change. This is a replacement for `dir-state'.
191 ;;
192 ;; - status-extra-headers (dir)
193 ;;
194 ;; Return a string that will be added to the *vc-status* buffer header.
195 ;;
196 ;; - status-printer (fileinfo)
197 ;;
198 ;; Pretty print the `vc-status-fileinfo' FILEINFO.
199 ;; If a backend needs to show more information than the default FILE
200 ;; and STATE in the vc-status listing, it can store that extra
201 ;; information in `vc-status-fileinfo->extra'. This function can be
202 ;; used to display that extra information in the vc-status buffer.
203 ;;
204 ;; - status-fileinfo-extra (file)
205 ;;
206 ;; Compute `vc-status-fileinfo->extra' for FILE.
207 ;;
208 ;; * working-revision (file)
209 ;;
210 ;; Return the working revision of FILE. This is the revision fetched
211 ;; by the last checkout or upate, not necessarily the same thing as the
212 ;; head or tip revision. Should return "0" for a file added but not yet
213 ;; committed.
214 ;;
215 ;; - latest-on-branch-p (file)
216 ;;
217 ;; Return non-nil if the working revision of FILE is the latest revision
218 ;; on its branch (many VCSes call this the 'tip' or 'head' revision).
219 ;; The default implementation always returns t, which means that
220 ;; working with non-current revisions is not supported by default.
221 ;;
222 ;; * checkout-model (file)
223 ;;
224 ;; Indicate whether FILE needs to be "checked out" before it can be
225 ;; edited. See `vc-checkout-model' for a list of possible values.
226 ;;
227 ;; - workfile-unchanged-p (file)
228 ;;
229 ;; Return non-nil if FILE is unchanged from the working revision.
230 ;; This function should do a brief comparison of FILE's contents
231 ;; with those of the repository master of the working revision. If
232 ;; the backend does not have such a brief-comparison feature, the
233 ;; default implementation of this function can be used, which
234 ;; delegates to a full vc-BACKEND-diff. (Note that vc-BACKEND-diff
235 ;; must not run asynchronously in this case, see variable
236 ;; `vc-disable-async-diff'.)
237 ;;
238 ;; - mode-line-string (file)
239 ;;
240 ;; If provided, this function should return the VC-specific mode
241 ;; line string for FILE. The returned string should have a
242 ;; `help-echo' property which is the text to be displayed as a
243 ;; tooltip when the mouse hovers over the VC entry on the mode-line.
244 ;; The default implementation deals well with all states that
245 ;; `vc-state' can return.
246 ;;
247 ;; - dired-state-info (file)
248 ;;
249 ;; Translate the `vc-state' property of FILE into a string that can be
250 ;; used in a vc-dired buffer. The default implementation deals well
251 ;; with all states that `vc-state' can return.
252 ;;
253 ;; STATE-CHANGING FUNCTIONS
254 ;;
255 ;; * create-repo (backend)
256 ;;
257 ;; Create an empty repository in the current directory and initialize
258 ;; it so VC mode can add files to it. For file-oriented systems, this
259 ;; need do no more than create a subdirectory with the right name.
260 ;;
261 ;; * register (files &optional rev comment)
262 ;;
263 ;; Register FILES in this backend. Optionally, an initial revision REV
264 ;; and an initial description of the file, COMMENT, may be specified,
265 ;; but it is not guaranteed that the backend will do anything with this.
266 ;; The implementation should pass the value of vc-register-switches
267 ;; to the backend command. (Note: in older versions of VC, this
268 ;; command took a single file argument and not a list.)
269 ;;
270 ;; - init-revision (file)
271 ;;
272 ;; The initial revision to use when registering FILE if one is not
273 ;; specified by the user. If not provided, the variable
274 ;; vc-default-init-revision is used instead.
275 ;;
276 ;; - responsible-p (file)
277 ;;
278 ;; Return non-nil if this backend considers itself "responsible" for
279 ;; FILE, which can also be a directory. This function is used to find
280 ;; out what backend to use for registration of new files and for things
281 ;; like change log generation. The default implementation always
282 ;; returns nil.
283 ;;
284 ;; - could-register (file)
285 ;;
286 ;; Return non-nil if FILE could be registered under this backend. The
287 ;; default implementation always returns t.
288 ;;
289 ;; - receive-file (file rev)
290 ;;
291 ;; Let this backend "receive" a file that is already registered under
292 ;; another backend. The default implementation simply calls `register'
293 ;; for FILE, but it can be overridden to do something more specific,
294 ;; e.g. keep revision numbers consistent or choose editing modes for
295 ;; FILE that resemble those of the other backend.
296 ;;
297 ;; - unregister (file)
298 ;;
299 ;; Unregister FILE from this backend. This is only needed if this
300 ;; backend may be used as a "more local" backend for temporary editing.
301 ;;
302 ;; * checkin (files rev comment)
303 ;;
304 ;; Commit changes in FILES to this backend. If REV is non-nil, that
305 ;; should become the new revision number (not all backends do
306 ;; anything with it). COMMENT is used as a check-in comment. The
307 ;; implementation should pass the value of vc-checkin-switches to
308 ;; the backend command. (Note: in older versions of VC, this
309 ;; command took a single file argument and not a list.)
310 ;;
311 ;; * find-revision (file rev buffer)
312 ;;
313 ;; Fetch revision REV of file FILE and put it into BUFFER.
314 ;; If REV is the empty string, fetch the head of the trunk.
315 ;; The implementation should pass the value of vc-checkout-switches
316 ;; to the backend command.
317 ;;
318 ;; * checkout (file &optional editable rev)
319 ;;
320 ;; Check out revision REV of FILE into the working area. If EDITABLE
321 ;; is non-nil, FILE should be writable by the user and if locking is
322 ;; used for FILE, a lock should also be set. If REV is non-nil, that
323 ;; is the revision to check out (default is the working revision).
324 ;; If REV is t, that means to check out the head of the current branch;
325 ;; if it is the empty string, check out the head of the trunk.
326 ;; The implementation should pass the value of vc-checkout-switches
327 ;; to the backend command.
328 ;;
329 ;; * revert (file &optional contents-done)
330 ;;
331 ;; Revert FILE back to the working revision. If optional
332 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
333 ;; already been reverted from a version backup, and this function
334 ;; only needs to update the status of FILE within the backend.
335 ;;
336 ;; - rollback (files)
337 ;;
338 ;; Remove the tip revision of each of FILES from the repository. If
339 ;; this function is not provided, trying to cancel a revision is
340 ;; caught as an error. (Most backends don't provide it.) (Also
341 ;; note that older versions of this backend command were called
342 ;; 'cancel-version' and took a single file arg, not a list of
343 ;; files.)
344 ;;
345 ;; - merge (file rev1 rev2)
346 ;;
347 ;; Merge the changes between REV1 and REV2 into the current working file.
348 ;;
349 ;; - merge-news (file)
350 ;;
351 ;; Merge recent changes from the current branch into FILE.
352 ;;
353 ;; - steal-lock (file &optional revision)
354 ;;
355 ;; Steal any lock on the working revision of FILE, or on REVISION if
356 ;; that is provided. This function is only needed if locking is
357 ;; used for files under this backend, and if files can indeed be
358 ;; locked by other users.
359 ;;
360 ;; - modify-change-comment (files rev comment)
361 ;;
362 ;; Modify the change comments associated with the files at the
363 ;; given revision. This is optional, many backends do not support it.
364 ;;
365 ;; HISTORY FUNCTIONS
366 ;;
367 ;; * print-log (files &optional buffer)
368 ;;
369 ;; Insert the revision log for FILES into BUFFER, or the *vc* buffer
370 ;; if BUFFER is nil. (Note: older versions of this function expected
371 ;; only a single file argument.)
372 ;;
373 ;; - log-view-mode ()
374 ;;
375 ;; Mode to use for the output of print-log. This defaults to
376 ;; `log-view-mode' and is expected to be changed (if at all) to a derived
377 ;; mode of `log-view-mode'.
378 ;;
379 ;; - show-log-entry (revision)
380 ;;
381 ;; If provided, search the log entry for REVISION in the current buffer,
382 ;; and make sure it is displayed in the buffer's window. The default
383 ;; implementation of this function works for RCS-style logs.
384 ;;
385 ;; - wash-log (file)
386 ;;
387 ;; Remove all non-comment information from the output of print-log.
388 ;;
389 ;; - logentry-check ()
390 ;;
391 ;; If defined, this function is run to find out whether the user
392 ;; entered a valid log entry for check-in. The log entry is in the
393 ;; current buffer, and if it is not a valid one, the function should
394 ;; throw an error.
395 ;;
396 ;; - comment-history (file)
397 ;;
398 ;; Return a string containing all log entries that were made for FILE.
399 ;; This is used for transferring a file from one backend to another,
400 ;; retaining comment information. The default implementation of this
401 ;; function does this by calling print-log and then wash-log, and
402 ;; returning the resulting buffer contents as a string.
403 ;;
404 ;; - update-changelog (files)
405 ;;
406 ;; Using recent log entries, create ChangeLog entries for FILES, or for
407 ;; all files at or below the default-directory if FILES is nil. The
408 ;; default implementation runs rcs2log, which handles RCS- and
409 ;; CVS-style logs.
410 ;;
411 ;; * diff (files &optional rev1 rev2 buffer)
412 ;;
413 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
414 ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
415 ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
416 ;; found in the repository) as the older revision; if REV2 is nil,
417 ;; use the current working-copy contents as the newer revision. This
418 ;; function should pass the value of (vc-switches BACKEND 'diff) to
419 ;; the backend command. It should return a status of either 0 (no
420 ;; differences found), or 1 (either non-empty diff or the diff is
421 ;; run asynchronously).
422 ;;
423 ;; - revision-completion-table (files)
424 ;;
425 ;; Return a completion table for existing revisions of FILES.
426 ;; The default is to not use any completion table.
427 ;;
428 ;; - annotate-command (file buf &optional rev)
429 ;;
430 ;; If this function is provided, it should produce an annotated display
431 ;; of FILE in BUF, relative to revision REV. Annotation means each line
432 ;; of FILE displayed is prefixed with version information associated with
433 ;; its addition (deleted lines leave no history) and that the text of the
434 ;; file is fontified according to age.
435 ;;
436 ;; - annotate-time ()
437 ;;
438 ;; Only required if `annotate-command' is defined for the backend.
439 ;; Return the time of the next line of annotation at or after point,
440 ;; as a floating point fractional number of days. The helper
441 ;; function `vc-annotate-convert-time' may be useful for converting
442 ;; multi-part times as returned by `current-time' and `encode-time'
443 ;; to this format. Return nil if no more lines of annotation appear
444 ;; in the buffer. You can safely assume that point is placed at the
445 ;; beginning of each line, starting at `point-min'. The buffer that
446 ;; point is placed in is the Annotate output, as defined by the
447 ;; relevant backend. This function also affects how much of the line
448 ;; is fontified; where it leaves point is where fontification begins.
449 ;;
450 ;; - annotate-current-time ()
451 ;;
452 ;; Only required if `annotate-command' is defined for the backend,
453 ;; AND you'd like the current time considered to be anything besides
454 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
455 ;; time with hours, minutes, and seconds included. Probably safe to
456 ;; ignore. Return the current-time, in units of fractional days.
457 ;;
458 ;; - annotate-extract-revision-at-line ()
459 ;;
460 ;; Only required if `annotate-command' is defined for the backend.
461 ;; Invoked from a buffer in vc-annotate-mode, return the revision
462 ;; corresponding to the current line, or nil if there is no revision
463 ;; corresponding to the current line.
464 ;;
465 ;; SNAPSHOT SYSTEM
466 ;;
467 ;; - create-snapshot (dir name branchp)
468 ;;
469 ;; Take a snapshot of the current state of files under DIR and name it
470 ;; NAME. This should make sure that files are up-to-date before
471 ;; proceeding with the action. DIR can also be a file and if BRANCHP
472 ;; is specified, NAME should be created as a branch and DIR should be
473 ;; checked out under this new branch. The default implementation does
474 ;; not support branches but does a sanity check, a tree traversal and
475 ;; for each file calls `assign-name'.
476 ;;
477 ;; - assign-name (file name)
478 ;;
479 ;; Give name NAME to the working revision of FILE, assuming it is
480 ;; up-to-date. Only used by the default version of `create-snapshot'.
481 ;;
482 ;; - retrieve-snapshot (dir name update)
483 ;;
484 ;; Retrieve a named snapshot of all registered files at or below DIR.
485 ;; If UPDATE is non-nil, then update buffers of any files in the
486 ;; snapshot that are currently visited. The default implementation
487 ;; does a sanity check whether there aren't any uncommitted changes at
488 ;; or below DIR, and then performs a tree walk, using the `checkout'
489 ;; function to retrieve the corresponding revisions.
490 ;;
491 ;; MISCELLANEOUS
492 ;;
493 ;; - root (dir)
494 ;;
495 ;; Return DIR's "root" directory, that is, a parent directory of
496 ;; DIR for which the same backend as used for DIR applies. If no
497 ;; such parent exists, this function should return DIR.
498 ;;
499 ;; - make-version-backups-p (file)
500 ;;
501 ;; Return non-nil if unmodified repository revisions of FILE should be
502 ;; backed up locally. If this is done, VC can perform `diff' and
503 ;; `revert' operations itself, without calling the backend system. The
504 ;; default implementation always returns nil.
505 ;;
506 ;; - repository-hostname (dirname)
507 ;;
508 ;; Return the hostname that the backend will have to contact
509 ;; in order to operate on a file in DIRNAME. If the return value
510 ;; is nil, it means that the repository is local.
511 ;; This function is used in `vc-stay-local-p' which backends can use
512 ;; for their convenience.
513 ;;
514 ;; - previous-revision (file rev)
515 ;;
516 ;; Return the revision number that precedes REV for FILE, or nil if no such
517 ;; revision exists.
518 ;;
519 ;; - next-revision (file rev)
520 ;;
521 ;; Return the revision number that follows REV for FILE, or nil if no such
522 ;; revision exists.
523 ;;
524 ;; - check-headers ()
525 ;;
526 ;; Return non-nil if the current buffer contains any version headers.
527 ;;
528 ;; - clear-headers ()
529 ;;
530 ;; In the current buffer, reset all version headers to their unexpanded
531 ;; form. This function should be provided if the state-querying code
532 ;; for this backend uses the version headers to determine the state of
533 ;; a file. This function will then be called whenever VC changes the
534 ;; version control state in such a way that the headers would give
535 ;; wrong information.
536 ;;
537 ;; - delete-file (file)
538 ;;
539 ;; Delete FILE and mark it as deleted in the repository. If this
540 ;; function is not provided, the command `vc-delete-file' will
541 ;; signal an error.
542 ;;
543 ;; - rename-file (old new)
544 ;;
545 ;; Rename file OLD to NEW, both in the working area and in the
546 ;; repository. If this function is not provided, the renaming
547 ;; will be done by (vc-delete-file old) and (vc-register new).
548 ;;
549 ;; - find-file-hook ()
550 ;;
551 ;; Operation called in current buffer when opening a file. This can
552 ;; be used by the backend to setup some local variables it might need.
553 ;;
554 ;; - find-file-not-found-hook ()
555 ;;
556 ;; Operation called in current buffer when opening a non-existing file.
557 ;; By default, this asks the user if she wants to check out the file.
558 ;;
559 ;; - extra-menu ()
560 ;;
561 ;; Return a menu keymap, the items in the keymap will appear at the
562 ;; end of the Version Control menu. The goal is to allow backends
563 ;; to specify extra menu items that appear in the VC menu. This way
564 ;; you can provide menu entries for functionality that is specific
565 ;; to your backend and which does not map to any of the VC generic
566 ;; concepts.
567 ;;
568 ;; - extra-status-menu ()
569 ;;
570 ;; Return a menu keymap, the items in the keymap will appear at the
571 ;; end of the VC Status menu. The goal is to allow backends to
572 ;; specify extra menu items that appear in the VC Status menu. This
573 ;; makes it possible to provide menu entries for functionality that
574 ;; is specific to a backend and which does not map to any of the VC
575 ;; generic concepts.
576
577 ;;; Todo:
578
579 ;; - vc-update/vc-merge should deal with VC systems that don't
580 ;; update/merge on a file basis, but on a whole repository basis.
581 ;;
582 ;; - deal with push/pull operations.
583 ;;
584 ;; - "snapshots" should be renamed to "branches", and thoroughly reworked.
585 ;;
586 ;; - the backend sometimes knows when a file it opens has been marked
587 ;; by the VCS as having a "conflict". Find a way to pass this info -
588 ;; to VC so that it can turn on smerge-mode when opening such a
589 ;; file.
590 ;;
591 ;; - add a generic mechanism for remembering the current branch names,
592 ;; display the branch name in the mode-line. Replace
593 ;; vc-cvs-sticky-tag with that.
594 ;;
595 ;; - vc-diff should be able to show the diff for all files in a
596 ;; changeset, especially for VC systems that have per repository version numbers.
597 ;; log-view should take advantage of this.
598 ;;
599 ;; - a way to do repository wide log (instead of just per file/fileset) is needed.
600 ;;
601 ;; - the *VC-log* buffer needs font-locking.
602 ;;
603 ;; - make it easier to write logs. Maybe C-x 4 a should add to the log
604 ;; buffer, if one is present, instead of adding to the ChangeLog.
605 ;;
606 ;; - make vc-state for all backends return 'unregistered instead of
607 ;; nil for unregistered files, then update vc-next-action.
608 ;;
609 ;; - vc-register should register a fileset at a time. The backends
610 ;; already support this, only the front-end needs to be change to
611 ;; handle multiple files at a time.
612 ;;
613 ;; - add a mechanism for ignoring files.
614 ;;
615 ;; - vc-next-action should do something about 'missing files. Maybe
616 ;; just warn, or offer to checkout.
617 ;;
618 ;; - decide if vc-status should replace vc-dired.
619 ;;
620 ;; - vc-status needs mouse bindings.
621 ;;
622 ;; - vc-status needs more key bindings for VC actions.
623 ;;
624 ;; - vc-status toolbar needs more icons.
625 ;;
626 ;; - the dir-status backend function should take as an argument an
627 ;; optional fileset. and return the results just for that fileset.
628 ;; This can be used to speed up status buffer updates after VC
629 ;; operations.
630 ;;
631 ;; - keep the *vc-status* buffer sorted by file name.
632 ;;
633 ;; - vc-status: refresh should not completely wipe out the current
634 ;; contents of the vc-status buffer.
635 ;;
636 ;; - vc-diff, vc-annotate, etc. need to deal better with unregistered
637 ;; files. Now that unregistered and ignored files are shown in
638 ;; vc-dired/vc-status, it is possible that these commands are called
639 ;; for unregistered/ignored files.
640 ;;
641 ;; - do not default to RCS anymore when the current directory is not
642 ;; controlled by any VCS and the user does C-x v v
643 ;;
644
645 ;;; Code:
646
647 (require 'vc-hooks)
648 (require 'tool-bar)
649
650 (eval-when-compile
651 (require 'cl)
652 (require 'dired) ; for dired-map-over-marks macro
653 (require 'dired-aux)) ; for dired-kill-{line,tree}
654
655 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
656 (setq minor-mode-alist
657 (cons '(vc-parent-buffer vc-parent-buffer-name)
658 minor-mode-alist)))
659
660 ;; General customization
661
662 (defgroup vc nil
663 "Version-control system in Emacs."
664 :group 'tools)
665
666 (defcustom vc-suppress-confirm nil
667 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
668 :type 'boolean
669 :group 'vc)
670
671 (defcustom vc-delete-logbuf-window t
672 "If non-nil, delete the *VC-log* buffer and window after each logical action.
673 If nil, bury that buffer instead.
674 This is most useful if you have multiple windows on a frame and would like to
675 preserve the setting."
676 :type 'boolean
677 :group 'vc)
678
679 (defcustom vc-initial-comment nil
680 "If non-nil, prompt for initial comment when a file is registered."
681 :type 'boolean
682 :group 'vc)
683
684 (defcustom vc-default-init-revision "1.1"
685 "A string used as the default revision number when a new file is registered.
686 This can be overridden by giving a prefix argument to \\[vc-register]. This
687 can also be overridden by a particular VC backend."
688 :type 'string
689 :group 'vc
690 :version "20.3")
691
692 (defcustom vc-command-messages nil
693 "If non-nil, display run messages from back-end commands."
694 :type 'boolean
695 :group 'vc)
696
697 (defcustom vc-checkin-switches nil
698 "A string or list of strings specifying extra switches for checkin.
699 These are passed to the checkin program by \\[vc-checkin]."
700 :type '(choice (const :tag "None" nil)
701 (string :tag "Argument String")
702 (repeat :tag "Argument List"
703 :value ("")
704 string))
705 :group 'vc)
706
707 (defcustom vc-checkout-switches nil
708 "A string or list of strings specifying extra switches for checkout.
709 These are passed to the checkout program by \\[vc-checkout]."
710 :type '(choice (const :tag "None" nil)
711 (string :tag "Argument String")
712 (repeat :tag "Argument List"
713 :value ("")
714 string))
715 :group 'vc)
716
717 (defcustom vc-register-switches nil
718 "A string or list of strings; extra switches for registering a file.
719 These are passed to the checkin program by \\[vc-register]."
720 :type '(choice (const :tag "None" nil)
721 (string :tag "Argument String")
722 (repeat :tag "Argument List"
723 :value ("")
724 string))
725 :group 'vc)
726
727 (defcustom vc-dired-listing-switches "-al"
728 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
729 :type 'string
730 :group 'vc
731 :version "21.1")
732
733 (defcustom vc-dired-recurse t
734 "If non-nil, show directory trees recursively in VC Dired."
735 :type 'boolean
736 :group 'vc
737 :version "20.3")
738
739 (defcustom vc-dired-terse-display t
740 "If non-nil, show only locked or locally modified files in VC Dired."
741 :type 'boolean
742 :group 'vc
743 :version "20.3")
744
745 (defcustom vc-diff-switches nil
746 "A string or list of strings specifying switches for diff under VC.
747 When running diff under a given BACKEND, VC concatenates the values of
748 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
749 get the switches for that command. Thus, `vc-diff-switches' should
750 contain switches that are specific to version control, but not
751 specific to any particular backend."
752 :type '(choice (const :tag "None" nil)
753 (string :tag "Argument String")
754 (repeat :tag "Argument List"
755 :value ("")
756 string))
757 :group 'vc
758 :version "21.1")
759
760 (defcustom vc-diff-knows-L nil
761 "*Indicates whether diff understands the -L option.
762 The value is either `yes', `no', or nil. If it is nil, VC tries
763 to use -L and sets this variable to remember whether it worked."
764 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
765 :group 'vc)
766
767 (defcustom vc-allow-async-revert nil
768 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
769 Enabling this option means that you can confirm a revert operation even
770 if the local changes in the file have not been found and displayed yet."
771 :type '(choice (const :tag "No" nil)
772 (const :tag "Yes" t))
773 :group 'vc
774 :version "22.1")
775
776 ;;;###autoload
777 (defcustom vc-checkout-hook nil
778 "Normal hook (list of functions) run after checking out a file.
779 See `run-hooks'."
780 :type 'hook
781 :group 'vc
782 :version "21.1")
783
784 (defcustom vc-annotate-display-mode 'fullscale
785 "Which mode to color the output of \\[vc-annotate] with by default."
786 :type '(choice (const :tag "By Color Map Range" nil)
787 (const :tag "Scale to Oldest" scale)
788 (const :tag "Scale Oldest->Newest" fullscale)
789 (number :tag "Specify Fractional Number of Days"
790 :value "20.5"))
791 :group 'vc)
792
793 ;;;###autoload
794 (defcustom vc-checkin-hook nil
795 "Normal hook (list of functions) run after commit or file checkin.
796 See also `log-edit-done-hook'."
797 :type 'hook
798 :options '(log-edit-comment-to-change-log)
799 :group 'vc)
800
801 ;;;###autoload
802 (defcustom vc-before-checkin-hook nil
803 "Normal hook (list of functions) run before a commit or a file checkin.
804 See `run-hooks'."
805 :type 'hook
806 :group 'vc)
807
808 (defcustom vc-logentry-check-hook nil
809 "Normal hook run by `vc-backend-logentry-check'.
810 Use this to impose your own rules on the entry in addition to any the
811 version control backend imposes itself."
812 :type 'hook
813 :group 'vc)
814
815 ;; Annotate customization
816 (defcustom vc-annotate-color-map
817 (if (and (tty-display-color-p) (<= (display-color-cells) 8))
818 ;; A custom sorted TTY colormap
819 (let* ((colors
820 (sort
821 (delq nil
822 (mapcar (lambda (x)
823 (if (not (or
824 (string-equal (car x) "white")
825 (string-equal (car x) "black") ))
826 (car x)))
827 (tty-color-alist)))
828 (lambda (a b)
829 (cond
830 ((or (string-equal a "red") (string-equal b "blue")) t)
831 ((or (string-equal b "red") (string-equal a "blue")) nil)
832 ((string-equal a "yellow") t)
833 ((string-equal b "yellow") nil)
834 ((string-equal a "cyan") t)
835 ((string-equal b "cyan") nil)
836 ((string-equal a "green") t)
837 ((string-equal b "green") nil)
838 ((string-equal a "magenta") t)
839 ((string-equal b "magenta") nil)
840 (t (string< a b))))))
841 (date 20.)
842 (delta (/ (- 360. date) (1- (length colors)))))
843 (mapcar (lambda (x)
844 (prog1
845 (cons date x)
846 (setq date (+ date delta)))) colors))
847 ;; Normal colormap: hue stepped from 0-240deg, value=1., saturation=0.75
848 '(( 20. . "#FF3F3F")
849 ( 40. . "#FF6C3F")
850 ( 60. . "#FF993F")
851 ( 80. . "#FFC63F")
852 (100. . "#FFF33F")
853 (120. . "#DDFF3F")
854 (140. . "#B0FF3F")
855 (160. . "#83FF3F")
856 (180. . "#56FF3F")
857 (200. . "#3FFF56")
858 (220. . "#3FFF83")
859 (240. . "#3FFFB0")
860 (260. . "#3FFFDD")
861 (280. . "#3FF3FF")
862 (300. . "#3FC6FF")
863 (320. . "#3F99FF")
864 (340. . "#3F6CFF")
865 (360. . "#3F3FFF")))
866 "Association list of age versus color, for \\[vc-annotate].
867 Ages are given in units of fractional days. Default is eighteen
868 steps using a twenty day increment, from red to blue. For TTY
869 displays with 8 or fewer colors, the default is red to blue with
870 all other colors between (excluding black and white)."
871 :type 'alist
872 :group 'vc)
873
874 (defcustom vc-annotate-very-old-color "#3F3FFF"
875 "Color for lines older than the current color range in \\[vc-annotate]]."
876 :type 'string
877 :group 'vc)
878
879 (defcustom vc-annotate-background "black"
880 "Background color for \\[vc-annotate].
881 Default color is used if nil."
882 :type '(choice (const :tag "Default background" nil) (color))
883 :group 'vc)
884
885 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
886 "Menu elements for the mode-specific menu of VC-Annotate mode.
887 List of factors, used to expand/compress the time scale. See `vc-annotate'."
888 :type '(repeat number)
889 :group 'vc)
890
891 (defvar vc-annotate-mode-map
892 (let ((m (make-sparse-keymap)))
893 (define-key m "A" 'vc-annotate-revision-previous-to-line)
894 (define-key m "D" 'vc-annotate-show-diff-revision-at-line)
895 (define-key m "f" 'vc-annotate-find-revision-at-line)
896 (define-key m "J" 'vc-annotate-revision-at-line)
897 (define-key m "L" 'vc-annotate-show-log-revision-at-line)
898 (define-key m "N" 'vc-annotate-next-revision)
899 (define-key m "P" 'vc-annotate-prev-revision)
900 (define-key m "W" 'vc-annotate-working-revision)
901 (define-key m "V" 'vc-annotate-toggle-annotation-visibility)
902 m)
903 "Local keymap used for VC-Annotate mode.")
904
905 ;; Header-insertion hair
906
907 (defcustom vc-static-header-alist
908 '(("\\.c\\'" .
909 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
910 "*Associate static header string templates with file types.
911 A \%s in the template is replaced with the first string associated with
912 the file's version control type in `vc-header-alist'."
913 :type '(repeat (cons :format "%v"
914 (regexp :tag "File Type")
915 (string :tag "Header String")))
916 :group 'vc)
917
918 (defcustom vc-comment-alist
919 '((nroff-mode ".\\\"" ""))
920 "*Special comment delimiters for generating VC headers.
921 Add an entry in this list if you need to override the normal `comment-start'
922 and `comment-end' variables. This will only be necessary if the mode language
923 is sensitive to blank lines."
924 :type '(repeat (list :format "%v"
925 (symbol :tag "Mode")
926 (string :tag "Comment Start")
927 (string :tag "Comment End")))
928 :group 'vc)
929
930 (defcustom vc-checkout-carefully (= (user-uid) 0)
931 "*Non-nil means be extra-careful in checkout.
932 Verify that the file really is not locked
933 and that its contents match what the master file says."
934 :type 'boolean
935 :group 'vc)
936 (make-obsolete-variable 'vc-checkout-carefully
937 "the corresponding checks are always done now."
938 "21.1")
939
940 \f
941 ;; Variables the user doesn't need to know about.
942 (defvar vc-log-operation nil)
943 (defvar vc-log-after-operation-hook nil)
944
945 ;; In a log entry buffer, this is a local variable
946 ;; that points to the buffer for which it was made
947 ;; (either a file, or a VC dired buffer).
948 (defvar vc-parent-buffer nil)
949 (put 'vc-parent-buffer 'permanent-local t)
950 (defvar vc-parent-buffer-name nil)
951 (put 'vc-parent-buffer-name 'permanent-local t)
952
953 (defvar vc-disable-async-diff nil
954 "VC sets this to t locally to disable some async diff operations.
955 Backends that offer asynchronous diffs should respect this variable
956 in their implementation of vc-BACKEND-diff.")
957
958 (defvar vc-log-fileset)
959 (defvar vc-log-revision)
960
961 (defvar vc-dired-mode nil)
962 (make-variable-buffer-local 'vc-dired-mode)
963
964 ;; File property caching
965
966 (defun vc-clear-context ()
967 "Clear all cached file properties."
968 (interactive)
969 (fillarray vc-file-prop-obarray 0))
970
971 (defmacro with-vc-properties (files form settings)
972 "Execute FORM, then maybe set per-file properties for FILES.
973 SETTINGS is an association list of property/value pairs. After
974 executing FORM, set those properties from SETTINGS that have not yet
975 been updated to their corresponding values."
976 (declare (debug t))
977 `(let ((vc-touched-properties (list t)))
978 ,form
979 (dolist (file ,files)
980 (dolist (setting ,settings)
981 (let ((property (car setting)))
982 (unless (memq property vc-touched-properties)
983 (put (intern file vc-file-prop-obarray)
984 property (cdr setting))))))))
985
986 ;; Two macros for elisp programming
987
988 ;;;###autoload
989 (defmacro with-vc-file (file comment &rest body)
990 "Check out a writable copy of FILE if necessary, then execute BODY.
991 Check in FILE with COMMENT (a string) after BODY has been executed.
992 FILE is passed through `expand-file-name'; BODY executed within
993 `save-excursion'. If FILE is not under version control, or you are
994 using a locking version-control system and the file is locked by
995 somebody else, signal error."
996 (declare (debug t) (indent 2))
997 (let ((filevar (make-symbol "file")))
998 `(let ((,filevar (expand-file-name ,file)))
999 (or (vc-backend ,filevar)
1000 (error "File not under version control: `%s'" file))
1001 (unless (vc-editable-p ,filevar)
1002 (let ((state (vc-state ,filevar)))
1003 (if (stringp state)
1004 (error "`%s' is locking `%s'" state ,filevar)
1005 (vc-checkout ,filevar t))))
1006 (save-excursion
1007 ,@body)
1008 (vc-checkin (list ,filevar) nil ,comment))))
1009
1010 ;;;###autoload
1011 (defmacro edit-vc-file (file comment &rest body)
1012 "Edit FILE under version control, executing body.
1013 Checkin with COMMENT after executing BODY.
1014 This macro uses `with-vc-file', passing args to it.
1015 However, before executing BODY, find FILE, and after BODY, save buffer."
1016 (declare (debug t) (indent 2))
1017 (let ((filevar (make-symbol "file")))
1018 `(let ((,filevar (expand-file-name ,file)))
1019 (with-vc-file
1020 ,filevar ,comment
1021 (set-buffer (find-file-noselect ,filevar))
1022 ,@body
1023 (save-buffer)))))
1024
1025 ;; Common command execution logic to be used by backends
1026
1027 (defun vc-process-filter (p s)
1028 "An alternative output filter for async process P.
1029 One difference with the default filter is that this inserts S after markers.
1030 Another is that undo information is not kept."
1031 (let ((buffer (process-buffer p)))
1032 (when (buffer-live-p buffer)
1033 (with-current-buffer buffer
1034 (save-excursion
1035 (let ((buffer-undo-list t)
1036 (inhibit-read-only t))
1037 (goto-char (process-mark p))
1038 (insert s)
1039 (set-marker (process-mark p) (point))))))))
1040
1041 (defun vc-setup-buffer (&optional buf)
1042 "Prepare BUF for executing a VC command and make it current.
1043 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
1044 (unless buf (setq buf "*vc*"))
1045 (let ((camefrom (current-buffer))
1046 (olddir default-directory))
1047 (set-buffer (get-buffer-create buf))
1048 (kill-all-local-variables)
1049 (set (make-local-variable 'vc-parent-buffer) camefrom)
1050 (set (make-local-variable 'vc-parent-buffer-name)
1051 (concat " from " (buffer-name camefrom)))
1052 (setq default-directory olddir)
1053 (let ((buffer-undo-list t)
1054 (inhibit-read-only t))
1055 (erase-buffer))))
1056
1057 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
1058
1059 (defun vc-process-sentinel (p s)
1060 (let ((previous (process-get p 'vc-previous-sentinel))
1061 (buf (process-buffer p)))
1062 ;; Impatient users sometime kill "slow" buffers; check liveness
1063 ;; to avoid "error in process sentinel: Selecting deleted buffer".
1064 (when (buffer-live-p buf)
1065 (if previous (funcall previous p s))
1066 (with-current-buffer buf
1067 (setq mode-line-process
1068 (let ((status (process-status p)))
1069 ;; Leave mode-line uncluttered, normally.
1070 (unless (eq 'exit status)
1071 (format " (%s)" status))))
1072 (let (vc-sentinel-movepoint)
1073 ;; Normally, we want async code such as sentinels to not move point.
1074 (save-excursion
1075 (goto-char (process-mark p))
1076 (let ((cmds (process-get p 'vc-sentinel-commands)))
1077 (process-put p 'vc-sentinel-commands nil)
1078 (dolist (cmd cmds)
1079 ;; Each sentinel may move point and the next one should be run
1080 ;; at that new point. We could get the same result by having
1081 ;; each sentinel read&set process-mark, but since `cmd' needs
1082 ;; to work both for async and sync processes, this would be
1083 ;; difficult to achieve.
1084 (vc-exec-after cmd))))
1085 ;; But sometimes the sentinels really want to move point.
1086 (if vc-sentinel-movepoint
1087 (let ((win (get-buffer-window (current-buffer) 0)))
1088 (if (not win)
1089 (goto-char vc-sentinel-movepoint)
1090 (with-selected-window win
1091 (goto-char vc-sentinel-movepoint))))))))))
1092
1093 (defun vc-set-mode-line-busy-indicator ()
1094 (setq mode-line-process
1095 (concat " " (propertize "[waiting...]"
1096 'face 'mode-line-emphasis
1097 'help-echo
1098 "A VC command is in progress in this buffer"))))
1099
1100 (defun vc-exec-after (code)
1101 "Eval CODE when the current buffer's process is done.
1102 If the current buffer has no process, just evaluate CODE.
1103 Else, add CODE to the process' sentinel."
1104 (let ((proc (get-buffer-process (current-buffer))))
1105 (cond
1106 ;; If there's no background process, just execute the code.
1107 ;; We used to explicitly call delete-process on exited processes,
1108 ;; but this led to timing problems causing process output to be
1109 ;; lost. Terminated processes get deleted automatically
1110 ;; anyway. -- cyd
1111 ((or (null proc) (eq (process-status proc) 'exit))
1112 ;; Make sure we've read the process's output before going further.
1113 (if proc (accept-process-output proc))
1114 (eval code))
1115 ;; If a process is running, add CODE to the sentinel
1116 ((eq (process-status proc) 'run)
1117 (vc-set-mode-line-busy-indicator)
1118 (let ((previous (process-sentinel proc)))
1119 (unless (eq previous 'vc-process-sentinel)
1120 (process-put proc 'vc-previous-sentinel previous))
1121 (set-process-sentinel proc 'vc-process-sentinel))
1122 (process-put proc 'vc-sentinel-commands
1123 ;; We keep the code fragments in the order given
1124 ;; so that vc-diff-finish's message shows up in
1125 ;; the presence of non-nil vc-command-messages.
1126 (append (process-get proc 'vc-sentinel-commands)
1127 (list code))))
1128 (t (error "Unexpected process state"))))
1129 nil)
1130
1131 (defvar vc-post-command-functions nil
1132 "Hook run at the end of `vc-do-command'.
1133 Each function is called inside the buffer in which the command was run
1134 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
1135
1136 (defvar w32-quote-process-args)
1137
1138 (defun vc-delistify (filelist)
1139 "Smash a FILELIST into a file list string suitable for info messages."
1140 ;; FIXME what about file names with spaces?
1141 (if (not filelist) "." (mapconcat 'identity filelist " ")))
1142
1143 ;;;###autoload
1144 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
1145 "Execute a VC command, notifying user and checking for errors.
1146 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
1147 current buffer if BUFFER is t. If the destination buffer is not
1148 already current, set it up properly and erase it. The command is
1149 considered successful if its exit status does not exceed OKSTATUS (if
1150 OKSTATUS is nil, that means to ignore error status, if it is `async', that
1151 means not to wait for termination of the subprocess; if it is t it means to
1152 ignore all execution errors). FILE-OR-LIST is the name of a working file;
1153 it may be a list of files or be nil (to execute commands that don't expect
1154 a file name or set of files). If an optional list of FLAGS is present,
1155 that is inserted into the command line before the filename."
1156 ;; FIXME: file-relative-name can return a bogus result because
1157 ;; it doesn't look at the actual file-system to see if symlinks
1158 ;; come into play.
1159 (let* ((files
1160 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
1161 (if (listp file-or-list) file-or-list (list file-or-list))))
1162 (full-command
1163 ;; What we're doing here is preparing a version of the command
1164 ;; for display in a debug-progess message. If it's fewer than
1165 ;; 20 characters display the entire command (without trailing
1166 ;; newline). Otherwise display the first 20 followed by an ellipsis.
1167 (concat (if (string= (substring command -1) "\n")
1168 (substring command 0 -1)
1169 command)
1170 " "
1171 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
1172 " " (vc-delistify files))))
1173 (save-current-buffer
1174 (unless (or (eq buffer t)
1175 (and (stringp buffer)
1176 (string= (buffer-name) buffer))
1177 (eq buffer (current-buffer)))
1178 (vc-setup-buffer buffer))
1179 ;; If there's some previous async process still running, just kill it.
1180 (let ((oldproc (get-buffer-process (current-buffer))))
1181 ;; If we wanted to wait for oldproc to finish before doing
1182 ;; something, we'd have used vc-eval-after.
1183 ;; Use `delete-process' rather than `kill-process' because we don't
1184 ;; want any of its output to appear from now on.
1185 (if oldproc (delete-process oldproc)))
1186 (let ((squeezed (remq nil flags))
1187 (inhibit-read-only t)
1188 (status 0))
1189 (when files
1190 (setq squeezed (nconc squeezed files)))
1191 (let ((exec-path (append vc-path exec-path))
1192 ;; Add vc-path to PATH for the execution of this command.
1193 (process-environment
1194 (cons (concat "PATH=" (getenv "PATH")
1195 path-separator
1196 (mapconcat 'identity vc-path path-separator))
1197 process-environment))
1198 (w32-quote-process-args t))
1199 (if (and (eq okstatus 'async) (file-remote-p default-directory))
1200 ;; start-process does not support remote execution
1201 (setq okstatus nil))
1202 (if (eq okstatus 'async)
1203 ;; Run asynchronously.
1204 (let ((proc
1205 (let ((process-connection-type nil))
1206 (apply 'start-file-process command (current-buffer)
1207 command squeezed))))
1208 (if vc-command-messages
1209 (message "Running %s in background..." full-command))
1210 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
1211 (set-process-filter proc 'vc-process-filter)
1212 (vc-exec-after
1213 `(if vc-command-messages
1214 (message "Running %s in background... done" ',full-command))))
1215 ;; Run synchrously
1216 (if vc-command-messages
1217 (message "Running %s in foreground..." full-command))
1218 (let ((buffer-undo-list t))
1219 (setq status (apply 'process-file command nil t nil squeezed)))
1220 (when (and (not (eq t okstatus))
1221 (or (not (integerp status))
1222 (and okstatus (< okstatus status))))
1223 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
1224 (pop-to-buffer (current-buffer))
1225 (goto-char (point-min))
1226 (shrink-window-if-larger-than-buffer))
1227 (error "Running %s...FAILED (%s)" full-command
1228 (if (integerp status) (format "status %d" status) status))))
1229 ;; We're done. But don't emit a status message if running
1230 ;; asychronously, it would just mislead.
1231 (if (and vc-command-messages (not (eq okstatus 'async)))
1232 (message "Running %s...OK = %d" full-command status)))
1233 (vc-exec-after
1234 `(run-hook-with-args 'vc-post-command-functions
1235 ',command ',file-or-list ',flags))
1236 status))))
1237
1238 (defun vc-position-context (posn)
1239 "Save a bit of the text around POSN in the current buffer.
1240 Used to help us find the corresponding position again later
1241 if markers are destroyed or corrupted."
1242 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
1243 ;; rcs.el mode.
1244 (list posn
1245 (buffer-size)
1246 (buffer-substring posn
1247 (min (point-max) (+ posn 100)))))
1248
1249 (defun vc-find-position-by-context (context)
1250 "Return the position of CONTEXT in the current buffer.
1251 If CONTEXT cannot be found, return nil."
1252 (let ((context-string (nth 2 context)))
1253 (if (equal "" context-string)
1254 (point-max)
1255 (save-excursion
1256 (let ((diff (- (nth 1 context) (buffer-size))))
1257 (if (< diff 0) (setq diff (- diff)))
1258 (goto-char (nth 0 context))
1259 (if (or (search-forward context-string nil t)
1260 ;; Can't use search-backward since the match may continue
1261 ;; after point.
1262 (progn (goto-char (- (point) diff (length context-string)))
1263 ;; goto-char doesn't signal an error at
1264 ;; beginning of buffer like backward-char would
1265 (search-forward context-string nil t)))
1266 ;; to beginning of OSTRING
1267 (- (point) (length context-string))))))))
1268
1269 (defun vc-context-matches-p (posn context)
1270 "Return t if POSN matches CONTEXT, nil otherwise."
1271 (let* ((context-string (nth 2 context))
1272 (len (length context-string))
1273 (end (+ posn len)))
1274 (if (> end (1+ (buffer-size)))
1275 nil
1276 (string= context-string (buffer-substring posn end)))))
1277
1278 (defun vc-buffer-context ()
1279 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
1280 Used by `vc-restore-buffer-context' to later restore the context."
1281 (let ((point-context (vc-position-context (point)))
1282 ;; Use mark-marker to avoid confusion in transient-mark-mode.
1283 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
1284 (vc-position-context (mark-marker))))
1285 ;; Make the right thing happen in transient-mark-mode.
1286 (mark-active nil)
1287 ;; The new compilation code does not use compilation-error-list any
1288 ;; more, so the code below is now ineffective and might as well
1289 ;; be disabled. -- Stef
1290 ;; ;; We may want to reparse the compilation buffer after revert
1291 ;; (reparse (and (boundp 'compilation-error-list) ;compile loaded
1292 ;; ;; Construct a list; each elt is nil or a buffer
1293 ;; ;; if that buffer is a compilation output buffer
1294 ;; ;; that contains markers into the current buffer.
1295 ;; (save-current-buffer
1296 ;; (mapcar (lambda (buffer)
1297 ;; (set-buffer buffer)
1298 ;; (let ((errors (or
1299 ;; compilation-old-error-list
1300 ;; compilation-error-list))
1301 ;; (buffer-error-marked-p nil))
1302 ;; (while (and (consp errors)
1303 ;; (not buffer-error-marked-p))
1304 ;; (and (markerp (cdr (car errors)))
1305 ;; (eq buffer
1306 ;; (marker-buffer
1307 ;; (cdr (car errors))))
1308 ;; (setq buffer-error-marked-p t))
1309 ;; (setq errors (cdr errors)))
1310 ;; (if buffer-error-marked-p buffer)))
1311 ;; (buffer-list)))))
1312 (reparse nil))
1313 (list point-context mark-context reparse)))
1314
1315 (defun vc-restore-buffer-context (context)
1316 "Restore point/mark, and reparse any affected compilation buffers.
1317 CONTEXT is that which `vc-buffer-context' returns."
1318 (let ((point-context (nth 0 context))
1319 (mark-context (nth 1 context))
1320 ;; (reparse (nth 2 context))
1321 )
1322 ;; The new compilation code does not use compilation-error-list any
1323 ;; more, so the code below is now ineffective and might as well
1324 ;; be disabled. -- Stef
1325 ;; ;; Reparse affected compilation buffers.
1326 ;; (while reparse
1327 ;; (if (car reparse)
1328 ;; (with-current-buffer (car reparse)
1329 ;; (let ((compilation-last-buffer (current-buffer)) ;select buffer
1330 ;; ;; Record the position in the compilation buffer of
1331 ;; ;; the last error next-error went to.
1332 ;; (error-pos (marker-position
1333 ;; (car (car-safe compilation-error-list)))))
1334 ;; ;; Reparse the error messages as far as they were parsed before.
1335 ;; (compile-reinitialize-errors '(4) compilation-parsing-end)
1336 ;; ;; Move the pointer up to find the error we were at before
1337 ;; ;; reparsing. Now next-error should properly go to the next one.
1338 ;; (while (and compilation-error-list
1339 ;; (/= error-pos (car (car compilation-error-list))))
1340 ;; (setq compilation-error-list (cdr compilation-error-list))))))
1341 ;; (setq reparse (cdr reparse)))
1342
1343 ;; if necessary, restore point and mark
1344 (if (not (vc-context-matches-p (point) point-context))
1345 (let ((new-point (vc-find-position-by-context point-context)))
1346 (if new-point (goto-char new-point))))
1347 (and mark-active
1348 mark-context
1349 (not (vc-context-matches-p (mark) mark-context))
1350 (let ((new-mark (vc-find-position-by-context mark-context)))
1351 (if new-mark (set-mark new-mark))))))
1352
1353 ;;; Code for deducing what fileset and backend to assume
1354
1355 (defun vc-responsible-backend (file &optional register)
1356 "Return the name of a backend system that is responsible for FILE.
1357 The optional argument REGISTER means that a backend suitable for
1358 registration should be found.
1359
1360 If REGISTER is nil, then if FILE is already registered, return the
1361 backend of FILE. If FILE is not registered, or a directory, then the
1362 first backend in `vc-handled-backends' that declares itself
1363 responsible for FILE is returned. If no backend declares itself
1364 responsible, return the first backend.
1365
1366 If REGISTER is non-nil, return the first responsible backend under
1367 which FILE is not yet registered. If there is no such backend, return
1368 the first backend under which FILE is not yet registered, but could
1369 be registered."
1370 (if (not vc-handled-backends)
1371 (error "No handled backends"))
1372 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1373 (catch 'found
1374 ;; First try: find a responsible backend. If this is for registration,
1375 ;; it must be a backend under which FILE is not yet registered.
1376 (dolist (backend vc-handled-backends)
1377 (and (or (not register)
1378 (not (vc-call-backend backend 'registered file)))
1379 (vc-call-backend backend 'responsible-p file)
1380 (throw 'found backend)))
1381 ;; no responsible backend
1382 (if (not register)
1383 ;; if this is not for registration, the first backend must do
1384 (car vc-handled-backends)
1385 ;; for registration, we need to find a new backend that
1386 ;; could register FILE
1387 (dolist (backend vc-handled-backends)
1388 (and (not (vc-call-backend backend 'registered file))
1389 (vc-call-backend backend 'could-register file)
1390 (throw 'found backend)))
1391 (error "No backend that could register")))))
1392
1393 (defun vc-expand-dirs (file-or-dir-list)
1394 "Expands directories in a file list specification.
1395 Only files already under version control are noticed."
1396 ;; FIXME: Kill this function.
1397 (let ((flattened '()))
1398 (dolist (node file-or-dir-list)
1399 (vc-file-tree-walk
1400 node (lambda (f) (if (vc-backend f) (push f flattened)))))
1401 (nreverse flattened)))
1402
1403 (defun vc-deduce-fileset (&optional allow-directory-wildcard allow-unregistered)
1404 "Deduce a set of files and a backend to which to apply an operation.
1405
1406 If we're in VC-dired mode, the fileset is the list of marked files.
1407 Otherwise, if we're looking at a buffer visiting a version-controlled file,
1408 the fileset is a singleton containing this file.
1409 If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on
1410 and we're in a dired buffer, select the current directory.
1411 If none of these conditions is met, but ALLOW_UNREGISTERED is in and the
1412 visited file is not registered, return a singletin fileset containing it.
1413 Otherwise, throw an error."
1414 (cond (vc-dired-mode
1415 (let ((marked (dired-map-over-marks (dired-get-filename) nil)))
1416 (unless marked
1417 (error "No files have been selected."))
1418 ;; All members of the fileset must have the same backend
1419 (let ((firstbackend (vc-backend (car marked))))
1420 (dolist (f (cdr marked))
1421 (unless (eq (vc-backend f) firstbackend)
1422 (error "All members of a fileset must be under the same version-control system."))))
1423 marked))
1424 ((eq major-mode 'vc-status-mode)
1425 (let ((marked (vc-status-marked-files)))
1426 (if marked
1427 marked
1428 (list (vc-status-current-file)))))
1429 ((vc-backend buffer-file-name)
1430 (list buffer-file-name))
1431 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1432 (with-current-buffer vc-parent-buffer
1433 (or vc-dired-mode (eq major-mode 'vc-status-mode)))))
1434 (progn
1435 (set-buffer vc-parent-buffer)
1436 (vc-deduce-fileset)))
1437 ;; This is guarded by an enabling arg so users won't potentially
1438 ;; shoot themselves in the foot by modifying a fileset they can't
1439 ;; verify by eyeball. Allow it for nondestructive commands like
1440 ;; making diffs, or possibly for destructive ones that have
1441 ;; confirmation prompts.
1442 ((and allow-directory-wildcard
1443 ;; I think this is a misfeature. For now, I'll leave it in, but
1444 ;; I'll disable it anywhere else than in dired buffers. --Stef
1445 (and (derived-mode-p 'dired-mode)
1446 (equal buffer-file-name nil)
1447 (equal list-buffers-directory default-directory)))
1448 (progn
1449 (message "All version-controlled files below %s selected."
1450 default-directory)
1451 (list default-directory)))
1452 ((and allow-unregistered (not (vc-registered buffer-file-name)))
1453 (list buffer-file-name))
1454 (t (error "No fileset is available here."))))
1455
1456 (defun vc-ensure-vc-buffer ()
1457 "Make sure that the current buffer visits a version-controlled file."
1458 (cond
1459 (vc-dired-mode
1460 (set-buffer (find-file-noselect (dired-get-filename))))
1461 ((eq major-mode 'vc-status-mode)
1462 (set-buffer (find-file-noselect (vc-status-current-file))))
1463 (t
1464 (while (and vc-parent-buffer
1465 (buffer-live-p vc-parent-buffer)
1466 ;; Avoid infinite looping when vc-parent-buffer and
1467 ;; current buffer are the same buffer.
1468 (not (eq vc-parent-buffer (current-buffer))))
1469 (set-buffer vc-parent-buffer))
1470 (if (not buffer-file-name)
1471 (error "Buffer %s is not associated with a file" (buffer-name))
1472 (if (not (vc-backend buffer-file-name))
1473 (error "File %s is not under version control" buffer-file-name))))))
1474
1475 ;;; Support for the C-x v v command. This is where all the single-file-oriented
1476 ;;; code from before the fileset rewrite lives.
1477
1478 (defsubst vc-editable-p (file)
1479 "Return non-nil if FILE can be edited."
1480 (or (eq (vc-checkout-model file) 'implicit)
1481 (memq (vc-state file) '(edited needs-merge))))
1482
1483 (defun vc-revert-buffer-internal (&optional arg no-confirm)
1484 "Revert buffer, keeping point and mark where user expects them.
1485 Try to be clever in the face of changes due to expanded version-control
1486 key words. This is important for typeahead to work as expected.
1487 ARG and NO-CONFIRM are passed on to `revert-buffer'."
1488 (interactive "P")
1489 (widen)
1490 (let ((context (vc-buffer-context)))
1491 ;; Use save-excursion here, because it may be able to restore point
1492 ;; and mark properly even in cases where vc-restore-buffer-context
1493 ;; would fail. However, save-excursion might also get it wrong --
1494 ;; in this case, vc-restore-buffer-context gives it a second try.
1495 (save-excursion
1496 ;; t means don't call normal-mode;
1497 ;; that's to preserve various minor modes.
1498 (revert-buffer arg no-confirm t))
1499 (vc-restore-buffer-context context)))
1500
1501 (defun vc-buffer-sync (&optional not-urgent)
1502 "Make sure the current buffer and its working file are in sync.
1503 NOT-URGENT means it is ok to continue if the user says not to save."
1504 (if (buffer-modified-p)
1505 (if (or vc-suppress-confirm
1506 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1507 (save-buffer)
1508 (unless not-urgent
1509 (error "Aborted")))))
1510
1511 (defvar vc-dired-window-configuration)
1512
1513 (defun vc-compatible-state (p q)
1514 "Controls which states can be in the same commit."
1515 (or
1516 (eq p q)
1517 (and (member p '(edited added removed)) (member q '(edited added removed)))))
1518
1519 ;; Here's the major entry point.
1520
1521 ;;;###autoload
1522 (defun vc-next-action (verbose)
1523 "Do the next logical version control operation on the current fileset.
1524 This requires that all files in the fileset be in the same state.
1525
1526 For locking systems:
1527 If every file is not already registered, this registers each for version
1528 control.
1529 If every file is registered and not locked by anyone, this checks out
1530 a writable and locked file of each ready for editing.
1531 If every file is checked out and locked by the calling user, this
1532 first checks to see if each file has changed since checkout. If not,
1533 it performs a revert on that file.
1534 If every file has been changed, this pops up a buffer for entry
1535 of a log message; when the message has been entered, it checks in the
1536 resulting changes along with the log message as change commentary. If
1537 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1538 read-only copy of each changed file is left in place afterwards.
1539 If the affected file is registered and locked by someone else, you are
1540 given the option to steal the lock(s).
1541
1542 For merging systems:
1543 If every file is not already registered, this registers each one for version
1544 control. This does an add, but not a commit.
1545 If every file is added but not committed, each one is committed.
1546 If every working file is changed, but the corresponding repository file is
1547 unchanged, this pops up a buffer for entry of a log message; when the
1548 message has been entered, it checks in the resulting changes along
1549 with the logmessage as change commentary. A writable file is retained.
1550 If the repository file is changed, you are asked if you want to
1551 merge in the changes into your working copy."
1552 (interactive "P")
1553 (let* ((files (vc-deduce-fileset nil t))
1554 (state (vc-state (car files)))
1555 (model (vc-checkout-model (car files)))
1556 revision)
1557 ;; Verify that the fileset is homogenous
1558 (dolist (file (cdr files))
1559 (unless (vc-compatible-state (vc-state file) state)
1560 (error "Fileset is in a mixed-up state"))
1561 (unless (eq (vc-checkout-model file) model)
1562 (error "Fileset has mixed checkout models")))
1563 ;; Check for buffers in the fileset not matching the on-disk contents.
1564 (dolist (file files)
1565 (let ((visited (get-file-buffer file)))
1566 (when visited
1567 (if (or vc-dired-mode (eq major-mode 'vc-status-mode))
1568 (switch-to-buffer-other-window visited)
1569 (set-buffer visited))
1570 ;; Check relation of buffer and file, and make sure
1571 ;; user knows what he's doing. First, finding the file
1572 ;; will check whether the file on disk is newer.
1573 ;; Ignore buffer-read-only during this test, and
1574 ;; preserve find-file-literally.
1575 (let ((buffer-read-only (not (file-writable-p file))))
1576 (find-file-noselect file nil find-file-literally))
1577 (if (not (verify-visited-file-modtime (current-buffer)))
1578 (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file))
1579 (write-file buffer-file-name)
1580 (error "Aborted"))
1581 ;; Now, check if we have unsaved changes.
1582 (vc-buffer-sync t)
1583 (when (buffer-modified-p)
1584 (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file))
1585 (error "Aborted")))))))
1586 ;; Do the right thing
1587 (cond
1588 ;; Files aren't registered
1589 ((or (not state) ;; RCS uses nil for unregistered files.
1590 (eq state 'unregistered)
1591 (eq state 'ignored))
1592 (mapc 'vc-register files))
1593 ;; Files are up-to-date, or need a merge and user specified a revision
1594 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-patch)))
1595 (cond
1596 (verbose
1597 ;; go to a different revision
1598 (setq revision (read-string "Branch, revision, or backend to move to: "))
1599 (let ((vsym (intern-soft (upcase revision))))
1600 (if (member vsym vc-handled-backends)
1601 (dolist (file files) (vc-transfer-file file vsym))
1602 (dolist (file files)
1603 (vc-checkout file (eq model 'implicit) revision)))))
1604 ((not (eq model 'implicit))
1605 ;; check the files out
1606 (dolist (file files) (vc-checkout file t)))
1607 (t
1608 ;; do nothing
1609 (message "Fileset is up-to-date"))))
1610 ;; Files have local changes
1611 ((vc-compatible-state state 'edited)
1612 (let ((ready-for-commit files))
1613 ;; If files are edited but read-only, give user a chance to correct
1614 (dolist (file files)
1615 (unless (file-writable-p file)
1616 ;; Make the file+buffer read-write.
1617 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue?" file))
1618 (error "Aborted"))
1619 (set-file-modes file (logior (file-modes file) 128))
1620 (let ((visited (get-file-buffer file)))
1621 (when visited
1622 (with-current-buffer visited
1623 (toggle-read-only -1))))))
1624 ;; Allow user to revert files with no changes
1625 (save-excursion
1626 (dolist (file files)
1627 (let ((visited (get-file-buffer file)))
1628 ;; For files with locking, if the file does not contain
1629 ;; any changes, just let go of the lock, i.e. revert.
1630 (when (and (not (eq model 'implicit))
1631 (vc-workfile-unchanged-p file)
1632 ;; If buffer is modified, that means the user just
1633 ;; said no to saving it; in that case, don't revert,
1634 ;; because the user might intend to save after
1635 ;; finishing the log entry and committing.
1636 (not (and visited (buffer-modified-p))))
1637 (vc-revert-file file)
1638 (delete file ready-for-commit)))))
1639 ;; Remaining files need to be committed
1640 (if (not ready-for-commit)
1641 (message "No files remain to be committed")
1642 (if (not verbose)
1643 (vc-checkin ready-for-commit)
1644 (progn
1645 (setq revision (read-string "New revision or backend: "))
1646 (let ((vsym (intern (upcase revision))))
1647 (if (member vsym vc-handled-backends)
1648 (dolist (file files) (vc-transfer-file file vsym))
1649 (vc-checkin ready-for-commit revision))))))))
1650 ;; locked by somebody else (locking VCSes only)
1651 ((stringp state)
1652 ;; In the old days, we computed the revision once and used it on
1653 ;; the single file. Then, for the 2007-2008 fileset rewrite, we
1654 ;; computed the revision once (incorrectly, using a free var) and
1655 ;; used it on all files. To fix the free var bug, we can either
1656 ;; use `(car files)' or do what we do here: distribute the
1657 ;; revision computation among `files'. Although this may be
1658 ;; tedious for those backends where a "revision" is a trans-file
1659 ;; concept, it is nonetheless correct for both those and (more
1660 ;; importantly) for those where "revision" is a per-file concept.
1661 ;; If the intersection of the former group and "locking VCSes" is
1662 ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
1663 ;; pre-computation approach of yore.
1664 (dolist (file files)
1665 (vc-steal-lock
1666 file (if verbose
1667 (read-string (format "%s revision to steal: " file))
1668 (vc-working-revision file))
1669 state)))
1670 ;; needs-patch
1671 ((eq state 'needs-patch)
1672 (dolist (file files)
1673 (if (yes-or-no-p (format
1674 "%s is not up-to-date. Get latest revision? "
1675 (file-name-nondirectory file)))
1676 (vc-checkout file (eq model 'implicit) t)
1677 (when (and (not (eq model 'implicit))
1678 (yes-or-no-p "Lock this revision? "))
1679 (vc-checkout file t)))))
1680 ;; needs-merge
1681 ((eq state 'needs-merge)
1682 (dolist (file files)
1683 (when (yes-or-no-p (format
1684 "%s is not up-to-date. Merge in changes now? "
1685 (file-name-nondirectory file)))
1686 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))
1687
1688 ;; unlocked-changes
1689 ((eq state 'unlocked-changes)
1690 (dolist (file files)
1691 (if (not (equal buffer-file-name file))
1692 (find-file-other-window file))
1693 (if (save-window-excursion
1694 (vc-diff-internal nil (list file) (vc-working-revision file) nil)
1695 (goto-char (point-min))
1696 (let ((inhibit-read-only t))
1697 (insert
1698 (format "Changes to %s since last lock:\n\n" file)))
1699 (not (beep))
1700 (yes-or-no-p (concat "File has unlocked changes. "
1701 "Claim lock retaining changes? ")))
1702 (progn (vc-call steal-lock file)
1703 (clear-visited-file-modtime)
1704 ;; Must clear any headers here because they wouldn't
1705 ;; show that the file is locked now.
1706 (vc-clear-headers file)
1707 (write-file buffer-file-name)
1708 (vc-mode-line file))
1709 (if (not (yes-or-no-p
1710 "Revert to checked-in revision, instead? "))
1711 (error "Checkout aborted")
1712 (vc-revert-buffer-internal t t)
1713 (vc-checkout file t))))))))
1714
1715 (defun vc-create-repo (backend)
1716 "Create an empty repository in the current directory."
1717 (interactive
1718 (list
1719 (intern
1720 (upcase
1721 (completing-read
1722 "Create repository for: "
1723 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1724 nil t)))))
1725 (vc-call-backend backend 'create-repo))
1726
1727 ;;;###autoload
1728 (defun vc-register (&optional fname set-revision comment)
1729 "Register into a version control system.
1730 If FNAME is given register that file, otherwise register the current file.
1731 With prefix argument SET-REVISION, allow user to specify initial revision
1732 level. If COMMENT is present, use that as an initial comment.
1733
1734 The version control system to use is found by cycling through the list
1735 `vc-handled-backends'. The first backend in that list which declares
1736 itself responsible for the file (usually because other files in that
1737 directory are already registered under that backend) will be used to
1738 register the file. If no backend declares itself responsible, the
1739 first backend that could register the file is used."
1740 (interactive "P")
1741 (when (and (null fname) (null buffer-file-name)) (error "No visited file"))
1742
1743 (let ((bname (if fname (get-file-buffer fname) (current-buffer))))
1744 (unless fname (setq fname buffer-file-name))
1745 (when (vc-backend fname)
1746 (if (vc-registered fname)
1747 (error "This file is already registered")
1748 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1749 (error "Aborted"))))
1750 ;; Watch out for new buffers of size 0: the corresponding file
1751 ;; does not exist yet, even though buffer-modified-p is nil.
1752 (when bname
1753 (with-current-buffer bname
1754 (if (and (not (buffer-modified-p))
1755 (zerop (buffer-size))
1756 (not (file-exists-p buffer-file-name)))
1757 (set-buffer-modified-p t))
1758 (vc-buffer-sync)))
1759 (vc-start-entry (list fname)
1760 (if set-revision
1761 (read-string (format "Initial revision level for %s: "
1762 fname))
1763 (vc-call-backend (vc-responsible-backend fname)
1764 'init-revision))
1765 (or comment (not vc-initial-comment))
1766 nil
1767 "Enter initial comment."
1768 (lambda (files rev comment)
1769 (dolist (file files)
1770 (message "Registering %s... " file)
1771 (let ((backend (vc-responsible-backend file t)))
1772 (vc-file-clearprops file)
1773 (vc-call-backend backend 'register (list file) rev comment)
1774 (vc-file-setprop file 'vc-backend backend)
1775 (unless vc-make-backup-files
1776 (make-local-variable 'backup-inhibited)
1777 (setq backup-inhibited t)))
1778 (message "Registering %s... done" file))))))
1779
1780 (defun vc-register-with (backend)
1781 "Register the current file with a specified back end."
1782 (interactive "SBackend: ")
1783 (if (not (member backend vc-handled-backends))
1784 (error "Unknown back end."))
1785 (let ((vc-handled-backends (list backend)))
1786 (call-interactively 'vc-register)))
1787
1788 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
1789
1790 (defun vc-resynch-window (file &optional keep noquery)
1791 "If FILE is in the current buffer, either revert or unvisit it.
1792 The choice between revert (to see expanded keywords) and unvisit depends on
1793 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1794 reverting. NOQUERY should be t *only* if it is known the only
1795 difference between the buffer and the file is due to version control
1796 rather than user editing!"
1797 (and (string= buffer-file-name file)
1798 (if keep
1799 (progn
1800 (vc-revert-buffer-internal t noquery)
1801 ;; TODO: Adjusting view mode might no longer be necessary
1802 ;; after RMS change to files.el of 1999-08-08. Investigate
1803 ;; this when we install the new VC.
1804 (and view-read-only
1805 (if (file-writable-p file)
1806 (and view-mode
1807 (let ((view-old-buffer-read-only nil))
1808 (view-mode-exit)))
1809 (and (not view-mode)
1810 (not (eq (get major-mode 'mode-class) 'special))
1811 (view-mode-enter))))
1812 (vc-mode-line buffer-file-name))
1813 (kill-buffer (current-buffer)))))
1814
1815 (defun vc-resynch-buffer (file &optional keep noquery)
1816 "If FILE is currently visited, resynch its buffer."
1817 (if (string= buffer-file-name file)
1818 (vc-resynch-window file keep noquery)
1819 (let ((buffer (get-file-buffer file)))
1820 (when buffer
1821 (with-current-buffer buffer
1822 (vc-resynch-window file keep noquery)))))
1823 (vc-dired-resynch-file file)
1824 (when (memq 'vc-status-mark-buffer-changed after-save-hook)
1825 (let ((buffer (get-file-buffer file)))
1826 (vc-status-mark-buffer-changed file))))
1827
1828
1829 (defun vc-start-entry (files rev comment initial-contents msg action &optional after-hook)
1830 "Accept a comment for an operation on FILES revision REV.
1831 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1832 action on close to ACTION. If COMMENT is a string and
1833 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1834 contents of the log entry buffer. If COMMENT is a string and
1835 INITIAL-CONTENTS is nil, do action immediately as if the user had
1836 entered COMMENT. If COMMENT is t, also do action immediately with an
1837 empty comment. Remember the file's buffer in `vc-parent-buffer'
1838 \(current one if no file). AFTER-HOOK specifies the local value
1839 for `vc-log-after-operation-hook'."
1840 (let ((parent
1841 (if (or (eq major-mode 'vc-dired-mode) (eq major-mode 'vc-status-mode))
1842 ;; If we are called from VC dired, the parent buffer is
1843 ;; the current buffer.
1844 (current-buffer)
1845 (if (and files (equal (length files) 1))
1846 (get-file-buffer (car files))
1847 (current-buffer)))))
1848 (when vc-before-checkin-hook
1849 (if files
1850 (with-current-buffer parent
1851 (run-hooks 'vc-before-checkin-hook))
1852 (run-hooks 'vc-before-checkin-hook)))
1853 (if (and comment (not initial-contents))
1854 (set-buffer (get-buffer-create "*VC-log*"))
1855 (pop-to-buffer (get-buffer-create "*VC-log*")))
1856 (set (make-local-variable 'vc-parent-buffer) parent)
1857 (set (make-local-variable 'vc-parent-buffer-name)
1858 (concat " from " (buffer-name vc-parent-buffer)))
1859 ;;(if file (vc-mode-line file))
1860 (vc-log-edit files)
1861 (make-local-variable 'vc-log-after-operation-hook)
1862 (when after-hook
1863 (setq vc-log-after-operation-hook after-hook))
1864 (setq vc-log-operation action)
1865 (setq vc-log-revision rev)
1866 (when comment
1867 (erase-buffer)
1868 (when (stringp comment) (insert comment)))
1869 (if (or (not comment) initial-contents)
1870 (message "%s Type C-c C-c when done" msg)
1871 (vc-finish-logentry (eq comment t)))))
1872
1873 (defun vc-checkout (file &optional writable rev)
1874 "Retrieve a copy of the revision REV of FILE.
1875 If WRITABLE is non-nil, make sure the retrieved file is writable.
1876 REV defaults to the latest revision.
1877
1878 After check-out, runs the normal hook `vc-checkout-hook'."
1879 (and writable
1880 (not rev)
1881 (vc-call make-version-backups-p file)
1882 (vc-up-to-date-p file)
1883 (vc-make-version-backup file))
1884 (with-vc-properties
1885 (list file)
1886 (condition-case err
1887 (vc-call checkout file writable rev)
1888 (file-error
1889 ;; Maybe the backend is not installed ;-(
1890 (when writable
1891 (let ((buf (get-file-buffer file)))
1892 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1893 (signal (car err) (cdr err))))
1894 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1895 (not writable))
1896 (if (vc-call latest-on-branch-p file)
1897 'up-to-date
1898 'needs-patch)
1899 'edited))
1900 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1901 (vc-resynch-buffer file t t)
1902 (run-hooks 'vc-checkout-hook))
1903
1904 (defun vc-steal-lock (file rev owner)
1905 "Steal the lock on FILE."
1906 (let (file-description)
1907 (if rev
1908 (setq file-description (format "%s:%s" file rev))
1909 (setq file-description file))
1910 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1911 file-description owner)))
1912 (error "Steal canceled"))
1913 (message "Stealing lock on %s..." file)
1914 (with-vc-properties
1915 (list file)
1916 (vc-call steal-lock file rev)
1917 `((vc-state . edited)))
1918 (vc-resynch-buffer file t t)
1919 (message "Stealing lock on %s...done" file)
1920 ;; Write mail after actually stealing, because if the stealing
1921 ;; goes wrong, we don't want to send any mail.
1922 (compose-mail owner (format "Stolen lock on %s" file-description))
1923 (setq default-directory (expand-file-name "~/"))
1924 (goto-char (point-max))
1925 (insert
1926 (format "I stole the lock on %s, " file-description)
1927 (current-time-string)
1928 ".\n")
1929 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1930
1931 (defun vc-checkin (files &optional rev comment initial-contents)
1932 "Check in FILES.
1933 The optional argument REV may be a string specifying the new revision
1934 level (if nil increment the current level). COMMENT is a comment
1935 string; if omitted, a buffer is popped up to accept a comment. If
1936 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1937 of the log entry buffer.
1938
1939 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1940 that the version control system supports this mode of operation.
1941
1942 Runs the normal hook `vc-checkin-hook'."
1943 (vc-start-entry
1944 files rev comment initial-contents
1945 "Enter a change comment."
1946 (lambda (files rev comment)
1947 (message "Checking in %s..." (vc-delistify files))
1948 ;; "This log message intentionally left almost blank".
1949 ;; RCS 5.7 gripes about white-space-only comments too.
1950 (or (and comment (string-match "[^\t\n ]" comment))
1951 (setq comment "*** empty log message ***"))
1952 (with-vc-properties
1953 files
1954 ;; We used to change buffers to get local value of vc-checkin-switches,
1955 ;; but 'the' local buffer is not a well-defined concept for filesets.
1956 (progn
1957 (vc-call checkin files rev comment)
1958 (mapc 'vc-delete-automatic-version-backups files))
1959 `((vc-state . up-to-date)
1960 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1961 (vc-working-revision . nil)))
1962 (message "Checking in %s...done" (vc-delistify files)))
1963 'vc-checkin-hook))
1964
1965 (defun vc-finish-logentry (&optional nocomment)
1966 "Complete the operation implied by the current log entry.
1967 Use the contents of the current buffer as a check-in or registration
1968 comment. If the optional arg NOCOMMENT is non-nil, then don't check
1969 the buffer contents as a comment."
1970 (interactive)
1971 ;; Check and record the comment, if any.
1972 (unless nocomment
1973 ;; Comment too long?
1974 (vc-call-backend (or (if vc-log-fileset (vc-backend vc-log-fileset))
1975 (vc-responsible-backend default-directory))
1976 'logentry-check)
1977 (run-hooks 'vc-logentry-check-hook))
1978 ;; Sync parent buffer in case the user modified it while editing the comment.
1979 ;; But not if it is a vc-dired buffer.
1980 (with-current-buffer vc-parent-buffer
1981 (or vc-dired-mode (eq major-mode 'vc-status-mode) (vc-buffer-sync)))
1982 (if (not vc-log-operation)
1983 (error "No log operation is pending"))
1984 ;; save the parameters held in buffer-local variables
1985 (let ((log-operation vc-log-operation)
1986 (log-fileset vc-log-fileset)
1987 (log-revision vc-log-revision)
1988 (log-entry (buffer-string))
1989 (after-hook vc-log-after-operation-hook)
1990 (tmp-vc-parent-buffer vc-parent-buffer))
1991 (pop-to-buffer vc-parent-buffer)
1992 ;; OK, do it to it
1993 (save-excursion
1994 (funcall log-operation
1995 log-fileset
1996 log-revision
1997 log-entry))
1998 ;; Remove checkin window (after the checkin so that if that fails
1999 ;; we don't zap the *VC-log* buffer and the typing therein).
2000 (let ((logbuf (get-buffer "*VC-log*")))
2001 (cond ((and logbuf vc-delete-logbuf-window)
2002 (delete-windows-on logbuf (selected-frame))
2003 ;; Kill buffer and delete any other dedicated windows/frames.
2004 (kill-buffer logbuf))
2005 (logbuf (pop-to-buffer "*VC-log*")
2006 (bury-buffer)
2007 (pop-to-buffer tmp-vc-parent-buffer))))
2008 ;; Now make sure we see the expanded headers
2009 (if log-fileset
2010 (mapc
2011 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
2012 log-fileset))
2013 (if (or vc-dired-mode (eq major-mode 'vc-status-mode))
2014 (dired-move-to-filename))
2015 (run-hooks after-hook 'vc-finish-logentry-hook)))
2016
2017 ;;; Additional entry points for examining version histories
2018
2019 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
2020 ;; "List differences for all registered files at and below DIR.
2021 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
2022 ;; ;; This implementation does an explicit tree walk, and calls
2023 ;; ;; vc-BACKEND-diff directly for each file. An optimization
2024 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
2025 ;; ;; and to call it only for files that are actually changed.
2026 ;; ;; However, this is expensive for some backends, and so it is left
2027 ;; ;; to backend-specific implementations.
2028 ;; (setq default-directory dir)
2029 ;; (vc-file-tree-walk
2030 ;; default-directory
2031 ;; (lambda (f)
2032 ;; (vc-exec-after
2033 ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
2034 ;; (message "Looking at %s" ',f)
2035 ;; (vc-call-backend ',(vc-backend f)
2036 ;; 'diff (list ',f) ',rev1 ',rev2))))))
2037
2038 (defun vc-coding-system-for-diff (file)
2039 "Return the coding system for reading diff output for FILE."
2040 (or coding-system-for-read
2041 ;; if we already have this file open,
2042 ;; use the buffer's coding system
2043 (let ((buf (find-buffer-visiting file)))
2044 (if buf (with-current-buffer buf
2045 buffer-file-coding-system)))
2046 ;; otherwise, try to find one based on the file name
2047 (car (find-operation-coding-system 'insert-file-contents file))
2048 ;; and a final fallback
2049 'undecided))
2050
2051 (defun vc-switches (backend op)
2052 (let ((switches
2053 (or (if backend
2054 (let ((sym (vc-make-backend-sym
2055 backend (intern (concat (symbol-name op)
2056 "-switches")))))
2057 (if (boundp sym) (symbol-value sym))))
2058 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
2059 (if (boundp sym) (symbol-value sym)))
2060 (cond
2061 ((eq op 'diff) diff-switches)))))
2062 (if (stringp switches) (list switches)
2063 ;; If not a list, return nil.
2064 ;; This is so we can set vc-diff-switches to t to override
2065 ;; any switches in diff-switches.
2066 (if (listp switches) switches))))
2067
2068 ;; Old def for compatibility with Emacs-21.[123].
2069 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
2070 (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
2071
2072 (defun vc-diff-finish (buffer messages)
2073 ;; The empty sync output case has already been handled, so the only
2074 ;; possibility of an empty output is for an async process.
2075 (when (buffer-live-p buffer)
2076 (let ((window (get-buffer-window buffer t))
2077 (emptyp (zerop (buffer-size buffer))))
2078 (with-current-buffer buffer
2079 (and messages emptyp
2080 (let ((inhibit-read-only t))
2081 (insert (cdr messages) ".\n")
2082 (message "%s" (cdr messages))))
2083 (goto-char (point-min))
2084 (when window
2085 (shrink-window-if-larger-than-buffer window)))
2086 (when (and messages (not emptyp))
2087 (message "%sdone" (car messages))))))
2088
2089 (defvar vc-diff-added-files nil
2090 "If non-nil, diff added files by comparing them to /dev/null.")
2091
2092 (defun vc-diff-internal (async files rev1 rev2 &optional verbose)
2093 "Report diffs between two revisions of a fileset.
2094 Diff output goes to the *vc-diff* buffer. The function
2095 returns t if the buffer had changes, nil otherwise."
2096 (let* ((messages (cons (format "Finding changes in %s..."
2097 (vc-delistify files))
2098 (format "No changes between %s and %s"
2099 (or rev1 "working revision")
2100 (or rev2 "workfile"))))
2101 ;; Set coding system based on the first file. It's a kluge,
2102 ;; but the only way to set it for each file included would
2103 ;; be to call the back end separately for each file.
2104 (coding-system-for-read
2105 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
2106 (vc-setup-buffer "*vc-diff*")
2107 (message "%s" (car messages))
2108 ;; Many backends don't handle well the case of a file that has been
2109 ;; added but not yet committed to the repo (notably CVS and Subversion).
2110 ;; Do that work here so the backends don't have to futz with it. --ESR
2111 ;;
2112 ;; Actually most backends (including CVS) have options to control the
2113 ;; behavior since which one is better depends on the user and on the
2114 ;; situation). Worse yet: this code does not handle the case where
2115 ;; `file' is a directory which contains added files.
2116 ;; I made it conditional on vc-diff-added-files but it should probably
2117 ;; just be removed (or copied/moved to specific backends). --Stef.
2118 (when vc-diff-added-files
2119 (let ((filtered '()))
2120 (dolist (file files)
2121 (if (or (file-directory-p file)
2122 (not (string= (vc-working-revision file) "0")))
2123 (push file filtered)
2124 ;; This file is added but not yet committed;
2125 ;; there is no master file to diff against.
2126 (if (or rev1 rev2)
2127 (error "No revisions of %s exist" file)
2128 ;; We regard this as "changed".
2129 ;; Diff it against /dev/null.
2130 (apply 'vc-do-command "*vc-diff*"
2131 1 "diff" file
2132 (append (vc-switches nil 'diff) '("/dev/null"))))))
2133 (setq files (nreverse filtered))))
2134 (let ((vc-disable-async-diff (not async)))
2135 (vc-call diff files rev1 rev2 "*vc-diff*"))
2136 (set-buffer "*vc-diff*")
2137 (if (and (zerop (buffer-size))
2138 (not (get-buffer-process (current-buffer))))
2139 ;; Treat this case specially so as not to pop the buffer.
2140 (progn
2141 (message "%s" (cdr messages))
2142 nil)
2143 (diff-mode)
2144 ;; Make the *vc-diff* buffer read only, the diff-mode key
2145 ;; bindings are nicer for read only buffers. pcl-cvs does the
2146 ;; same thing.
2147 (setq buffer-read-only t)
2148 (vc-exec-after `(vc-diff-finish ,(current-buffer) ',(when verbose
2149 messages)))
2150 ;; Display the buffer, but at the end because it can change point.
2151 (pop-to-buffer (current-buffer))
2152 ;; In the async case, we return t even if there are no differences
2153 ;; because we don't know that yet.
2154 t)))
2155
2156 ;;;###autoload
2157 (defun vc-version-diff (files rev1 rev2)
2158 "Report diffs between revisions of the fileset in the repository history."
2159 (interactive
2160 (let* ((files (vc-deduce-fileset t))
2161 (first (car files))
2162 (completion-table
2163 (vc-call revision-completion-table files))
2164 (rev1-default nil)
2165 (rev2-default nil))
2166 (cond
2167 ;; someday we may be able to do revision completion on non-singleton
2168 ;; filesets, but not yet.
2169 ((/= (length files) 1)
2170 nil)
2171 ;; if it's a directory, don't supply any revision default
2172 ((file-directory-p first)
2173 nil)
2174 ;; if the file is not up-to-date, use working revision as older revision
2175 ((not (vc-up-to-date-p first))
2176 (setq rev1-default (vc-working-revision first)))
2177 ;; if the file is not locked, use last and previous revisions as defaults
2178 (t
2179 (setq rev1-default (vc-call previous-revision first
2180 (vc-working-revision first)))
2181 (if (string= rev1-default "") (setq rev1-default nil))
2182 (setq rev2-default (vc-working-revision first))))
2183 ;; construct argument list
2184 (let* ((rev1-prompt (if rev1-default
2185 (concat "Older revision (default "
2186 rev1-default "): ")
2187 "Older revision: "))
2188 (rev2-prompt (concat "Newer revision (default "
2189 (or rev2-default "current source") "): "))
2190 (rev1 (if completion-table
2191 (completing-read rev1-prompt completion-table
2192 nil nil nil nil rev1-default)
2193 (read-string rev1-prompt nil nil rev1-default)))
2194 (rev2 (if completion-table
2195 (completing-read rev2-prompt completion-table
2196 nil nil nil nil rev2-default)
2197 (read-string rev2-prompt nil nil rev2-default))))
2198 (if (string= rev1 "") (setq rev1 nil))
2199 (if (string= rev2 "") (setq rev2 nil))
2200 (list files rev1 rev2))))
2201 (if (and (not rev1) rev2)
2202 (error "Not a valid revision range."))
2203 (vc-diff-internal t files rev1 rev2 (interactive-p)))
2204
2205 ;; (defun vc-contains-version-controlled-file (dir)
2206 ;; "Return t if DIR contains a version-controlled file, nil otherwise."
2207 ;; (catch 'found
2208 ;; (mapc (lambda (f) (and (not (file-directory-p f)) (vc-backend f) (throw 'found 't))) (directory-files dir))
2209 ;; nil))
2210
2211 ;;;###autoload
2212 (defun vc-diff (historic &optional not-urgent)
2213 "Display diffs between file revisions.
2214 Normally this compares the currently selected fileset with their
2215 working revisions. With a prefix argument HISTORIC, it reads two revision
2216 designators specifying which revisions to compare.
2217
2218 If no current fileset is available (that is, we are not in
2219 VC-Dired mode and the visited file of the current buffer is not
2220 under version control) and we're in a Dired buffer, use
2221 the current directory.
2222 The optional argument NOT-URGENT non-nil means it is ok to say no to
2223 saving the buffer."
2224 (interactive (list current-prefix-arg t))
2225 (if historic
2226 (call-interactively 'vc-version-diff)
2227 (let* ((files (vc-deduce-fileset t)))
2228 (if buffer-file-name (vc-buffer-sync not-urgent))
2229 (vc-diff-internal t files nil nil (interactive-p)))))
2230
2231
2232 ;;;###autoload
2233 (defun vc-revision-other-window (rev)
2234 "Visit revision REV of the current file in another window.
2235 If the current file is named `F', the revision is named `F.~REV~'.
2236 If `F.~REV~' already exists, use it instead of checking it out again."
2237 (interactive
2238 (save-current-buffer
2239 (vc-ensure-vc-buffer)
2240 (let ((completion-table
2241 (vc-call revision-completion-table buffer-file-name))
2242 (prompt "Revision to visit (default is working revision): "))
2243 (list
2244 (if completion-table
2245 (completing-read prompt completion-table)
2246 (read-string prompt))))))
2247 (vc-ensure-vc-buffer)
2248 (let* ((file buffer-file-name)
2249 (revision (if (string-equal rev "")
2250 (vc-working-revision file)
2251 rev)))
2252 (switch-to-buffer-other-window (vc-find-revision file revision))))
2253
2254 (defun vc-find-revision (file revision)
2255 "Read REVISION of FILE into a buffer and return the buffer."
2256 (let ((automatic-backup (vc-version-backup-file-name file revision))
2257 (filebuf (or (get-file-buffer file) (current-buffer)))
2258 (filename (vc-version-backup-file-name file revision 'manual)))
2259 (unless (file-exists-p filename)
2260 (if (file-exists-p automatic-backup)
2261 (rename-file automatic-backup filename nil)
2262 (message "Checking out %s..." filename)
2263 (with-current-buffer filebuf
2264 (let ((failed t))
2265 (unwind-protect
2266 (let ((coding-system-for-read 'no-conversion)
2267 (coding-system-for-write 'no-conversion))
2268 (with-temp-file filename
2269 (let ((outbuf (current-buffer)))
2270 ;; Change buffer to get local value of
2271 ;; vc-checkout-switches.
2272 (with-current-buffer filebuf
2273 (vc-call find-revision file revision outbuf))))
2274 (setq failed nil))
2275 (when (and failed (file-exists-p filename))
2276 (delete-file filename))))
2277 (vc-mode-line file))
2278 (message "Checking out %s...done" filename)))
2279 (let ((result-buf (find-file-noselect filename)))
2280 (with-current-buffer result-buf
2281 ;; Set the parent buffer so that things like
2282 ;; C-x v g, C-x v l, ... etc work.
2283 (set (make-local-variable 'vc-parent-buffer) filebuf))
2284 result-buf)))
2285
2286 ;; Header-insertion code
2287
2288 ;;;###autoload
2289 (defun vc-insert-headers ()
2290 "Insert headers into a file for use with a version control system.
2291 Headers desired are inserted at point, and are pulled from
2292 the variable `vc-BACKEND-header'."
2293 (interactive)
2294 (vc-ensure-vc-buffer)
2295 (save-excursion
2296 (save-restriction
2297 (widen)
2298 (if (or (not (vc-check-headers))
2299 (y-or-n-p "Version headers already exist. Insert another set? "))
2300 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
2301 (comment-start-vc (or (car delims) comment-start "#"))
2302 (comment-end-vc (or (car (cdr delims)) comment-end ""))
2303 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
2304 'header))
2305 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
2306 (dolist (s hdstrings)
2307 (insert comment-start-vc "\t" s "\t"
2308 comment-end-vc "\n"))
2309 (if vc-static-header-alist
2310 (dolist (f vc-static-header-alist)
2311 (if (string-match (car f) buffer-file-name)
2312 (insert (format (cdr f) (car hdstrings)))))))))))
2313
2314 (defun vc-clear-headers (&optional file)
2315 "Clear all version headers in the current buffer (or FILE).
2316 The headers are reset to their non-expanded form."
2317 (let* ((filename (or file buffer-file-name))
2318 (visited (find-buffer-visiting filename))
2319 (backend (vc-backend filename)))
2320 (when (vc-find-backend-function backend 'clear-headers)
2321 (if visited
2322 (let ((context (vc-buffer-context)))
2323 ;; save-excursion may be able to relocate point and mark
2324 ;; properly. If it fails, vc-restore-buffer-context
2325 ;; will give it a second try.
2326 (save-excursion
2327 (vc-call-backend backend 'clear-headers))
2328 (vc-restore-buffer-context context))
2329 (set-buffer (find-file-noselect filename))
2330 (vc-call-backend backend 'clear-headers)
2331 (kill-buffer filename)))))
2332
2333 (defun vc-modify-change-comment (files rev oldcomment)
2334 "Edit the comment associated with the given files and revision."
2335 (vc-start-entry
2336 files rev oldcomment t
2337 "Enter a replacement change comment."
2338 (lambda (files rev comment)
2339 (vc-call-backend
2340 ;; Less of a kluge than it looks like; log-view mode only passes
2341 ;; this function a singleton list. Arguments left in this form in
2342 ;; case the more general operation ever becomes meaningful.
2343 (vc-responsible-backend (car files))
2344 'modify-change-comment files rev comment))))
2345
2346 ;;;###autoload
2347 (defun vc-merge ()
2348 "Merge changes between two revisions into the current buffer's file.
2349 This asks for two revisions to merge from in the minibuffer. If the
2350 first revision is a branch number, then merge all changes from that
2351 branch. If the first revision is empty, merge news, i.e. recent changes
2352 from the current branch.
2353
2354 See Info node `Merging'."
2355 (interactive)
2356 (vc-ensure-vc-buffer)
2357 (vc-buffer-sync)
2358 (let* ((file buffer-file-name)
2359 (backend (vc-backend file))
2360 (state (vc-state file))
2361 first-revision second-revision status)
2362 (cond
2363 ((stringp state) ;; Locking VCses only
2364 (error "File is locked by %s" state))
2365 ((not (vc-editable-p file))
2366 (if (y-or-n-p
2367 "File must be checked out for merging. Check out now? ")
2368 (vc-checkout file t)
2369 (error "Merge aborted"))))
2370 (setq first-revision
2371 (read-string (concat "Branch or revision to merge from "
2372 "(default news on current branch): ")))
2373 (if (string= first-revision "")
2374 (if (not (vc-find-backend-function backend 'merge-news))
2375 (error "Sorry, merging news is not implemented for %s" backend)
2376 (setq status (vc-call merge-news file)))
2377 (if (not (vc-find-backend-function backend 'merge))
2378 (error "Sorry, merging is not implemented for %s" backend)
2379 (if (not (vc-branch-p first-revision))
2380 (setq second-revision
2381 (read-string "Second revision: "
2382 (concat (vc-branch-part first-revision) ".")))
2383 ;; We want to merge an entire branch. Set revisions
2384 ;; accordingly, so that vc-BACKEND-merge understands us.
2385 (setq second-revision first-revision)
2386 ;; first-revision must be the starting point of the branch
2387 (setq first-revision (vc-branch-part first-revision)))
2388 (setq status (vc-call merge file first-revision second-revision))))
2389 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
2390
2391 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
2392 (vc-resynch-buffer file t (not (buffer-modified-p)))
2393 (if (zerop status) (message "Merge successful")
2394 (smerge-mode 1)
2395 (message "File contains conflicts.")))
2396
2397 ;;;###autoload
2398 (defalias 'vc-resolve-conflicts 'smerge-ediff)
2399
2400 ;; The VC directory major mode. Coopt Dired for this.
2401 ;; All VC commands get mapped into logical equivalents.
2402
2403 (defvar vc-dired-switches)
2404 (defvar vc-dired-terse-mode)
2405
2406 (defvar vc-dired-mode-map
2407 (let ((map (make-sparse-keymap))
2408 (vmap (make-sparse-keymap)))
2409 (define-key map "\C-xv" vmap)
2410 (define-key map "v" vmap)
2411 (set-keymap-parent vmap vc-prefix-map)
2412 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2413 map))
2414
2415 (define-derived-mode vc-dired-mode dired-mode "Dired under "
2416 "The major mode used in VC directory buffers.
2417
2418 It works like Dired, but lists only files under version control, with
2419 the current VC state of each file being indicated in the place of the
2420 file's link count, owner, group and size. Subdirectories are also
2421 listed, and you may insert them into the buffer as desired, like in
2422 Dired.
2423
2424 All Dired commands operate normally, with the exception of `v', which
2425 is redefined as the version control prefix, so that you can type
2426 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2427 the file named in the current Dired buffer line. `vv' invokes
2428 `vc-next-action' on this file, or on all files currently marked.
2429 There is a special command, `*l', to mark all files currently locked."
2430 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2431 ;; We do it here because dired might not be loaded yet
2432 ;; when vc-dired-mode-map is initialized.
2433 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2434 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2435 ;; The following is slightly modified from files.el,
2436 ;; because file lines look a bit different in vc-dired-mode
2437 ;; (the column before the date does not end in a digit).
2438 ;; albinus: It should be done in the original declaration. Problem
2439 ;; is the optional empty state-info; otherwise ")" would be good
2440 ;; enough as delimeter.
2441 (set (make-local-variable 'directory-listing-before-filename-regexp)
2442 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2443 ;; In some locales, month abbreviations are as short as 2 letters,
2444 ;; and they can be followed by ".".
2445 (month (concat l l "+\\.?"))
2446 (s " ")
2447 (yyyy "[0-9][0-9][0-9][0-9]")
2448 (dd "[ 0-3][0-9]")
2449 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2450 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
2451 (zone "[-+][0-2][0-9][0-5][0-9]")
2452 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
2453 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
2454 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
2455 "\\|" yyyy "-" iso-mm-dd "\\)"))
2456 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
2457 s "+"
2458 "\\(" HH:MM "\\|" yyyy "\\)"))
2459 (western-comma (concat month s "+" dd "," s "+" yyyy))
2460 ;; Japanese MS-Windows ls-lisp has one-digit months, and
2461 ;; omits the Kanji characters after month and day-of-month.
2462 (mm "[ 0-1]?[0-9]")
2463 (japanese
2464 (concat mm l "?" s dd l "?" s "+"
2465 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
2466 ;; the .* below ensures that we find the last match on a line
2467 (concat ".*" s
2468 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
2469 s "+")))
2470 (and (boundp 'vc-dired-switches)
2471 vc-dired-switches
2472 (set (make-local-variable 'dired-actual-switches)
2473 vc-dired-switches))
2474 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2475 (let ((backend-name (symbol-name (vc-responsible-backend
2476 default-directory))))
2477 (setq mode-name (concat mode-name backend-name))
2478 ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
2479 (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
2480 (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
2481 (cons backend-name vc-dire-menu-map) 'subdir)))
2482 (setq vc-dired-mode t))
2483
2484 (defun vc-dired-toggle-terse-mode ()
2485 "Toggle terse display in VC Dired."
2486 (interactive)
2487 (if (not vc-dired-mode)
2488 nil
2489 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2490 (if vc-dired-terse-mode
2491 (vc-dired-hook)
2492 (revert-buffer))))
2493
2494 (defun vc-dired-mark-locked ()
2495 "Mark all files currently locked."
2496 (interactive)
2497 (dired-mark-if (let ((f (dired-get-filename nil t)))
2498 (and f
2499 (not (file-directory-p f))
2500 (not (vc-up-to-date-p f))))
2501 "locked file"))
2502
2503 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2504
2505 (defun vc-dired-reformat-line (vc-info)
2506 "Reformat a directory-listing line.
2507 Replace various columns with version control information, VC-INFO.
2508 This code, like dired, assumes UNIX -l format."
2509 (beginning-of-line)
2510 (when (re-search-forward
2511 ;; Match link count, owner, group, size. Group may be missing,
2512 ;; and only the size is present in OS/2 -l format.
2513 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
2514 (line-end-position) t)
2515 (replace-match (substring (concat vc-info " ") 0 10)
2516 t t nil 1)))
2517
2518 (defun vc-dired-ignorable-p (filename)
2519 "Should FILENAME be ignored in VC-Dired listings?"
2520 (catch t
2521 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
2522 (dolist (ignorable completion-ignored-extensions)
2523 (let ((ext (substring filename
2524 (- (length filename)
2525 (length ignorable)))))
2526 (if (string= ignorable ext) (throw t t))))
2527 ;; Ignore Makefiles derived from something else
2528 (when (string= (file-name-nondirectory filename) "Makefile")
2529 (let* ((dir (file-name-directory filename))
2530 (peers (directory-files (or dir default-directory))))
2531 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
2532 (throw t t))))
2533 nil))
2534
2535 (defun vc-dired-hook ()
2536 "Reformat the listing according to version control.
2537 Called by dired after any portion of a vc-dired buffer has been read in."
2538 (message "Getting version information... ")
2539 ;; if the backend supports it, get the state
2540 ;; of all files in this directory at once
2541 (let ((backend (vc-responsible-backend default-directory)))
2542 ;; check `backend' can really handle `default-directory'.
2543 (if (and (vc-call-backend backend 'responsible-p default-directory)
2544 (vc-find-backend-function backend 'dir-state))
2545 (vc-call-backend backend 'dir-state default-directory)))
2546 (let (filename
2547 (inhibit-read-only t)
2548 (buffer-undo-list t))
2549 (goto-char (point-min))
2550 (while (not (eobp))
2551 (cond
2552 ;; subdir header line
2553 ((dired-get-subdir)
2554 (forward-line 1)
2555 ;; erase (but don't remove) the "total" line
2556 (delete-region (point) (line-end-position))
2557 (beginning-of-line)
2558 (forward-line 1))
2559 ;; file line
2560 ((setq filename (dired-get-filename nil t))
2561 (cond
2562 ;; subdir
2563 ((file-directory-p filename)
2564 (cond
2565 ((member (file-name-nondirectory filename)
2566 vc-directory-exclusion-list)
2567 (let ((pos (point)))
2568 (dired-kill-tree filename)
2569 (goto-char pos)
2570 (dired-kill-line)))
2571 (vc-dired-terse-mode
2572 ;; Don't show directories in terse mode. Don't use
2573 ;; dired-kill-line to remove it, because in recursive listings,
2574 ;; that would remove the directory contents as well.
2575 (delete-region (line-beginning-position)
2576 (progn (forward-line 1) (point))))
2577 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2578 (dired-kill-line))
2579 (t
2580 (vc-dired-reformat-line nil)
2581 (forward-line 1))))
2582 ;; Try to head off calling the expensive state query -
2583 ;; ignore object files, TeX intermediate files, and so forth.
2584 ((vc-dired-ignorable-p filename)
2585 (dired-kill-line))
2586 ;; Ordinary file -- call the (possibly expensive) state query
2587 ;;
2588 ;; First case: unregistered or unknown. (Unknown shouldn't happen here)
2589 ((member (vc-state filename) '(nil unregistered))
2590 (if vc-dired-terse-mode
2591 (dired-kill-line)
2592 (vc-dired-reformat-line "?")
2593 (forward-line 1)))
2594 ;; Either we're in non-terse mode or it's out of date
2595 ((not (and vc-dired-terse-mode (vc-up-to-date-p filename)))
2596 (vc-dired-reformat-line (vc-call dired-state-info filename))
2597 (forward-line 1))
2598 ;; Remaining cases are under version control but uninteresting
2599 (t
2600 (dired-kill-line))))
2601 ;; any other line
2602 (t (forward-line 1))))
2603 (vc-dired-purge))
2604 (message "Getting version information... done")
2605 (save-restriction
2606 (widen)
2607 (cond ((eq (count-lines (point-min) (point-max)) 1)
2608 (goto-char (point-min))
2609 (message "No changes pending under %s" default-directory)))))
2610
2611 (defun vc-dired-purge ()
2612 "Remove empty subdirs."
2613 (goto-char (point-min))
2614 (while (dired-get-subdir)
2615 (forward-line 2)
2616 (if (dired-get-filename nil t)
2617 (if (not (dired-next-subdir 1 t))
2618 (goto-char (point-max)))
2619 (forward-line -2)
2620 (if (not (string= (dired-current-directory) default-directory))
2621 (dired-do-kill-lines t "")
2622 ;; We cannot remove the top level directory.
2623 ;; Just make it look a little nicer.
2624 (forward-line 1)
2625 (or (eobp) (kill-line))
2626 (if (not (dired-next-subdir 1 t))
2627 (goto-char (point-max))))))
2628 (goto-char (point-min)))
2629
2630 (defun vc-dired-buffers-for-dir (dir)
2631 "Return a list of all vc-dired buffers that currently display DIR."
2632 (let (result)
2633 ;; Check whether dired is loaded.
2634 (when (fboundp 'dired-buffers-for-dir)
2635 (dolist (buffer (dired-buffers-for-dir dir))
2636 (with-current-buffer buffer
2637 (if vc-dired-mode
2638 (push buffer result)))))
2639 (nreverse result)))
2640
2641 (defun vc-dired-resynch-file (file)
2642 "Update the entries for FILE in any VC Dired buffers that list it."
2643 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2644 (when buffers
2645 (mapcar (lambda (buffer)
2646 (with-current-buffer buffer
2647 (if (dired-goto-file file)
2648 ;; bind vc-dired-terse-mode to nil so that
2649 ;; files won't vanish when they are checked in
2650 (let ((vc-dired-terse-mode nil))
2651 (dired-do-redisplay 1)))))
2652 buffers))))
2653
2654 ;;;###autoload
2655 (defun vc-directory (dir read-switches)
2656 "Create a buffer in VC Dired Mode for directory DIR.
2657
2658 See Info node `VC Dired Mode'.
2659
2660 With prefix arg READ-SWITCHES, specify a value to override
2661 `dired-listing-switches' when generating the listing."
2662 (interactive "DDired under VC (directory): \nP")
2663 (let ((vc-dired-switches (concat vc-dired-listing-switches
2664 (if vc-dired-recurse "R" ""))))
2665 (if read-switches
2666 (setq vc-dired-switches
2667 (read-string "Dired listing switches: "
2668 vc-dired-switches)))
2669 (require 'dired)
2670 (require 'dired-aux)
2671 (switch-to-buffer
2672 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2673 vc-dired-switches
2674 'vc-dired-mode))))
2675
2676 ;;; Experimental code for the vc-dired replacement
2677 (require 'ewoc)
2678
2679
2680 ;; Used to store information for the files displayed in the *VC status* buffer.
2681 ;; Each item displayed corresponds to one of these defstructs.
2682 (defstruct (vc-status-fileinfo
2683 (:copier nil)
2684 (:constructor
2685 vc-status-create-fileinfo (name state extra &optional marked))
2686 (:conc-name vc-status-fileinfo->))
2687 marked
2688 state
2689 name
2690 ;; For storing backend specific information.
2691 extra)
2692
2693 (defvar vc-status nil)
2694
2695 (defun vc-default-status-extra-headers (backend dir)
2696 ;; Be loud by default to remind people to add coded to display
2697 ;; backend specific headers.
2698 ;; XXX: change this to return nil before the release.
2699 "Extra : Add backend specific headers here")
2700
2701 (defun vc-status-headers (backend dir)
2702 "Display the headers in the *VC status* buffer.
2703 It calls the `status-extra-headers' backend method to display backend
2704 specific headers."
2705 (concat
2706 (propertize "VC backend : " 'face 'font-lock-type-face)
2707 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
2708 (propertize "Working dir: " 'face 'font-lock-type-face)
2709 (propertize (format "%s\n" dir) 'face 'font-lock-variable-name-face)
2710 (vc-call-backend backend 'status-extra-headers dir)
2711 "\n"))
2712
2713 (defun vc-default-status-printer (backend fileentry)
2714 "Pretty print FILEENTRY."
2715 ;; If you change the layout here, change vc-status-move-to-goal-column.
2716 (let ((state (vc-status-fileinfo->state fileentry)))
2717 (insert
2718 (propertize
2719 (format "%c" (if (vc-status-fileinfo->marked fileentry) ?* ? ))
2720 'face 'font-lock-type-face)
2721 " "
2722 (propertize
2723 (format "%-20s" state)
2724 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
2725 ((eq state 'missing) 'font-lock-warning-face)
2726 (t 'font-lock-variable-name-face))
2727 'mouse-face 'highlight)
2728 " "
2729 (propertize
2730 (format "%s" (vc-status-fileinfo->name fileentry))
2731 'face 'font-lock-function-name-face
2732 'mouse-face 'highlight))))
2733
2734 (defun vc-status-printer (fileentry)
2735 (let ((backend (vc-responsible-backend default-directory)))
2736 (vc-call-backend backend 'status-printer fileentry)))
2737
2738 (defun vc-status-move-to-goal-column ()
2739 ;; Used to keep the cursor on the file name column.
2740 (beginning-of-line)
2741 ;; Must be in sync with vc-default-status-printer.
2742 (forward-char 25))
2743
2744 (defun vc-status-prepare-status-buffer (dir &optional create-new)
2745 "Find a *vc-status* buffer showing DIR, or create a new one."
2746 (setq dir (expand-file-name dir))
2747 (let* ((bname "*vc-status*")
2748 ;; Look for another *vc-status* buffer visiting the same directory.
2749 (buf (save-excursion
2750 (unless create-new
2751 (dolist (buffer (buffer-list))
2752 (set-buffer buffer)
2753 (when (and (eq major-mode 'vc-status-mode)
2754 (string= (expand-file-name default-directory) dir))
2755 (return buffer)))))))
2756 (if buf
2757 buf
2758 ;; Create a new *vc-status* buffer.
2759 (with-current-buffer (create-file-buffer bname)
2760 (cd dir)
2761 (vc-setup-buffer (current-buffer))
2762 ;; Reset the vc-parent-buffer-name so that it does not appear
2763 ;; in the mode-line.
2764 (setq vc-parent-buffer-name nil)
2765 (current-buffer)))))
2766
2767 ;;;###autoload
2768 (defun vc-status (dir)
2769 "Show the VC status for DIR."
2770 (interactive "DVC status for directory: ")
2771 (switch-to-buffer (vc-status-prepare-status-buffer dir))
2772 (if (eq major-mode 'vc-status-mode)
2773 (vc-status-refresh)
2774 (vc-status-mode)))
2775
2776 (defvar vc-status-menu-map
2777 (let ((map (make-sparse-keymap "VC-status")))
2778 (define-key map [quit]
2779 '(menu-item "Quit" bury-buffer
2780 :help "Quit"))
2781 (define-key map [kill]
2782 '(menu-item "Kill Update Command" vc-status-kill-dir-status-process
2783 :enable vc-status-process-buffer
2784 :help "Kill the command that updates VC status buffer"))
2785 (define-key map [refresh]
2786 '(menu-item "Refresh" vc-status-refresh
2787 :enable (not vc-status-process-buffer)
2788 :help "Refresh the contents of the VC status buffer"))
2789 (define-key map [remup]
2790 '(menu-item "Hide up-to-date" vc-status-hide-up-to-date
2791 :help "Hide up-to-date items from display"))
2792 ;; VC commands.
2793 (define-key map [sepvccmd] '("--"))
2794 (define-key map [annotate]
2795 '(menu-item "Annotate" vc-annotate
2796 :help "Display the edit history of the current file using colors"))
2797 (define-key map [diff]
2798 '(menu-item "Compare with Base Version" vc-diff
2799 :help "Compare file set with the base version"))
2800 (define-key map [register]
2801 '(menu-item "Register" vc-status-register
2802 :help "Register file set into the version control system"))
2803 (define-key map [update]
2804 '(menu-item "Update to latest version" vc-update
2805 :help "Update the current fileset's files to their tip revisions"))
2806 (define-key map [revert]
2807 '(menu-item "Revert to base version" vc-revert
2808 :help "Revert working copies of the selected fileset to their repository contents."))
2809 (define-key map [log]
2810 '(menu-item "Show history" vc-print-log
2811 :help "List the change log of the current file set in a window"))
2812
2813 ;; Movement.
2814 (define-key map [sepmv] '("--"))
2815 (define-key map [next-line]
2816 '(menu-item "Next line" vc-status-next-line
2817 :help "Go to the next line" :keys "n"))
2818 (define-key map [previous-line]
2819 '(menu-item "Previous line" vc-status-previous-line
2820 :help "Go to the previous line"))
2821 ;; Marking.
2822 (define-key map [sepmrk] '("--"))
2823 (define-key map [unmark-all]
2824 '(menu-item "Unmark All" vc-status-unmark-all-files
2825 :help "Unmark all files that are in the same state as the current file\
2826 \nWith prefix argument unmark all files"))
2827 (define-key map [unmark-previous]
2828 '(menu-item "Unmark previous " vc-status-unmark-file-up
2829 :help "Move to the previous line and unmark the file"))
2830
2831 (define-key map [mark-all]
2832 '(menu-item "Mark All" vc-status-mark-all-files
2833 :help "Mark all files that are in the same state as the current file\
2834 \nWith prefix argument mark all files"))
2835 (define-key map [unmark]
2836 '(menu-item "Unmark" vc-status-unmark
2837 :help "Unmark the current file or all files in the region"))
2838
2839 (define-key map [mark]
2840 '(menu-item "Mark" vc-status-mark
2841 :help "Mark the current file or all files in the region"))
2842
2843 (define-key map [sepopn] '("--"))
2844 (define-key map [open-other]
2845 '(menu-item "Open in other window" vc-status-find-file-other-window
2846 :help "Find the file on the current line, in another window"))
2847 (define-key map [open]
2848 '(menu-item "Open file" vc-status-find-file
2849 :help "Find the file on the current line"))
2850 map)
2851 "Menu for VC status")
2852
2853 (defalias 'vc-status-menu-map vc-status-menu-map)
2854
2855 (defvar vc-status-mode-map
2856 (let ((map (make-keymap)))
2857 (suppress-keymap map)
2858 ;; Marking.
2859 (define-key map "m" 'vc-status-mark)
2860 (define-key map "M" 'vc-status-mark-all-files)
2861 (define-key map "u" 'vc-status-unmark)
2862 (define-key map "\C-?" 'vc-status-unmark-file-up)
2863 (define-key map "\M-\C-?" 'vc-status-unmark-all-files)
2864 ;; Movement.
2865 (define-key map "n" 'vc-status-next-line)
2866 (define-key map " " 'vc-status-next-line)
2867 (define-key map "\t" 'vc-status-next-line)
2868 (define-key map "p" 'vc-status-previous-line)
2869 (define-key map [backtab] 'vc-status-previous-line)
2870 ;; VC commands.
2871 (define-key map "=" 'vc-diff) ;; C-x v =
2872 (define-key map "a" 'vc-status-register)
2873 (define-key map "+" 'vc-update) ;; C-x v +
2874
2875 ;;XXX: Maybe use something else here, so we can use 'U' for unmark
2876 ;;all, similar to 'M'..
2877 (define-key map "U" 'vc-revert) ;; u is taken by unmark.
2878
2879 ;; Can't be "g" (as in vc map), so "A" for "Annotate".
2880 (define-key map "A" 'vc-annotate)
2881 (define-key map "l" 'vc-print-log) ;; C-x v l
2882 ;; The remainder.
2883 (define-key map "f" 'vc-status-find-file)
2884 (define-key map "\C-m" 'vc-status-find-file)
2885 (define-key map "o" 'vc-status-find-file-other-window)
2886 (define-key map "x" 'vc-status-hide-up-to-date)
2887 (define-key map "q" 'bury-buffer)
2888 (define-key map "g" 'vc-status-refresh)
2889 (define-key map "\C-c\C-c" 'vc-status-kill-dir-status-process)
2890 ;; Not working yet. Functions like vc-status-find-file need to
2891 ;; find the file from the mouse position, not `point'.
2892 ;; (define-key map [(down-mouse-3)] 'vc-status-menu)
2893
2894 ;; Hook up the menu.
2895 (define-key map [menu-bar vc-status-mode]
2896 '(menu-item
2897 ;; This is used to that VC backends could add backend specific
2898 ;; menu items to vc-status-menu-map.
2899 "VC Status" vc-status-menu-map :filter vc-status-menu-map-filter))
2900 map)
2901 "Keymap for VC status")
2902
2903 (defun vc-default-extra-status-menu (backend)
2904 nil)
2905
2906 ;; This is used to that VC backends could add backend specific menu
2907 ;; items to vc-status-menu-map.
2908 (defun vc-status-menu-map-filter (orig-binding)
2909 (when (and (symbolp orig-binding) (fboundp orig-binding))
2910 (setq orig-binding (indirect-function orig-binding)))
2911 (let ((ext-binding
2912 (vc-call-backend (vc-responsible-backend default-directory)
2913 'extra-status-menu)))
2914 (if (null ext-binding)
2915 orig-binding
2916 (append orig-binding
2917 '("----")
2918 ext-binding))))
2919
2920 (defun vc-status-menu (e)
2921 "Popup the VC status menu."
2922 (interactive "e")
2923 (popup-menu vc-status-menu-map e))
2924
2925 (defvar vc-status-tool-bar-map
2926 (let ((map (make-sparse-keymap)))
2927 (tool-bar-local-item-from-menu 'vc-status-find-file "open"
2928 map vc-status-mode-map)
2929 (tool-bar-local-item "bookmark_add"
2930 'vc-status-toggle-mark 'vc-status-toggle-mark map
2931 :help "Toggle mark on current item")
2932 (tool-bar-local-item-from-menu 'vc-status-previous-line "left-arrow"
2933 map vc-status-mode-map
2934 :rtl "right-arrow")
2935 (tool-bar-local-item-from-menu 'vc-status-next-line "right-arrow"
2936 map vc-status-mode-map
2937 :rtl "left-arrow")
2938 (tool-bar-local-item-from-menu 'vc-print-log "info"
2939 map vc-status-mode-map)
2940 (tool-bar-local-item-from-menu 'vc-status-refresh "refresh"
2941 map vc-status-mode-map)
2942 (tool-bar-local-item-from-menu 'nonincremental-search-forward
2943 "search" map)
2944 (tool-bar-local-item-from-menu 'vc-status-kill-dir-status-process "cancel"
2945 map vc-status-mode-map)
2946 (tool-bar-local-item-from-menu 'bury-buffer "exit"
2947 map vc-status-mode-map)
2948 map))
2949
2950 (defvar vc-status-process-buffer nil
2951 "The buffer used for the asynchronous call that computes the VC status.")
2952
2953 (defvar vc-status-crt-marked nil
2954 "The list of marked files before `vc-status-refresh'.")
2955
2956 (defun vc-status-mode ()
2957 "Major mode for VC status.
2958 \\{vc-status-mode-map}"
2959 (setq mode-name "*VC Status*")
2960 (setq major-mode 'vc-status-mode)
2961 (setq buffer-read-only t)
2962 (set (make-local-variable 'vc-status-crt-marked) nil)
2963 (use-local-map vc-status-mode-map)
2964 (set (make-local-variable 'tool-bar-map) vc-status-tool-bar-map)
2965 (let ((buffer-read-only nil)
2966 (backend (vc-responsible-backend default-directory))
2967 entries)
2968 (erase-buffer)
2969 (set (make-local-variable 'vc-status-process-buffer) nil)
2970 (set (make-local-variable 'vc-status)
2971 (ewoc-create #'vc-status-printer
2972 (vc-status-headers backend default-directory)))
2973 (add-hook 'after-save-hook 'vc-status-mark-buffer-changed)
2974 (vc-status-refresh)))
2975
2976 (put 'vc-status-mode 'mode-class 'special)
2977
2978 (defun vc-update-vc-status-buffer (entries buffer &optional more-to-come)
2979 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
2980 ;; BUFFER is the *vc-status* buffer to be updated with ENTRIES
2981 ;; If MORE-TO-COME is true, then more updates will come from the
2982 ;; asynchronous process.
2983 (with-current-buffer buffer
2984 (when entries
2985 ;; Insert the entries we got into the ewoc.
2986 (dolist (entry entries)
2987 (let* ((file (car entry))
2988 (state (nth 1 entry))
2989 (extra (nth 2 entry)))
2990 (ewoc-enter-last vc-status
2991 (vc-status-create-fileinfo file state extra))))
2992 ;; If we had marked items before the refresh, try mark them here.
2993 ;; XXX: there should be a better way to do this...
2994 (when vc-status-crt-marked
2995 (ewoc-map
2996 (lambda (arg)
2997 (when (member (vc-status-fileinfo->name arg) vc-status-crt-marked)
2998 (setf (vc-status-fileinfo->marked arg) t)))
2999 vc-status))
3000 (ewoc-goto-node vc-status (ewoc-nth vc-status 0)))
3001 ;; No more updates are expected from the asynchronous process.
3002 (unless more-to-come
3003 (setq vc-status-process-buffer nil)
3004 ;; We are done, turn off the mode-line "in progress" message.
3005 (setq mode-line-process nil))))
3006
3007 (defun vc-status-add-entry (entry buffer)
3008 ;; Add one ENTRY to the vc-status buffer BUFFER.
3009 ;; This will be used to automatically add files with the "modified"
3010 ;; state when saving them.
3011
3012 ;; ENTRY has the form (FILENAME STATE EXTRA)
3013 (with-current-buffer buffer
3014 (let ((crt (ewoc-nth vc-status 0))
3015 (fname (car entry)))
3016 ;; First try to see if there's already an entry with that name
3017 ;; in the ewoc.
3018 (while (and crt (not (string= (vc-status-fileinfo->name
3019 (ewoc-data crt)) fname)))
3020 (setq crt (ewoc-next vc-status crt)))
3021 (if crt
3022 (progn
3023 ;; Found the file, just update the status.
3024 (setf (vc-status-fileinfo->state (ewoc-data crt)) (nth 1 entry))
3025 (setf (vc-status-fileinfo->extra (ewoc-data crt)) (nth 2 entry))
3026 (ewoc-invalidate vc-status crt))
3027 ;; Could not find the file, insert a new entry.
3028 (ewoc-enter-last
3029 vc-status
3030 (vc-status-create-fileinfo fname (nth 1 entry) (nth 2 entry)))))))
3031
3032 (defun vc-status-refresh ()
3033 "Refresh the contents of the VC status buffer.
3034 Throw an error if another update process is in progress."
3035 (interactive)
3036 (if vc-status-process-buffer
3037 (error "Another update process is in progress, cannot run two at a time")
3038 ;; We clear the ewoc, but remember the marked files so that we can
3039 ;; mark them again after the refresh is done.
3040 ;; This is not very efficient; ewoc could use a new function here.
3041 (setq vc-status-crt-marked
3042 (mapcar
3043 (lambda (elem)
3044 (vc-status-fileinfo->name elem))
3045 (ewoc-collect
3046 vc-status
3047 (lambda (crt) (vc-status-fileinfo->marked crt)))))
3048 (ewoc-filter vc-status (lambda (node) nil))
3049
3050 (let ((backend (vc-responsible-backend default-directory))
3051 (status-buffer (current-buffer))
3052 (def-dir default-directory))
3053 (vc-set-mode-line-busy-indicator)
3054 ;; Call the `dir-status' backend function.
3055 ;; `dir-status' is supposed to be asynchronous.
3056 ;; It should compute the results, and then call the function
3057 ;; passed as an argument in order to update the vc-status buffer
3058 ;; with the results.
3059
3060 ;; Create a buffer that can be used by `dir-status' and call
3061 ;; `dir-status' with this buffer as the current buffer. Use
3062 ;; `vc-status-process-buffer' to remember this buffer, so that
3063 ;; it can be used later to kill the update process in case it
3064 ;; takes too long.
3065 (setq vc-status-process-buffer
3066 (get-buffer-create
3067 (generate-new-buffer-name (format " *VC-%s* tmp status" backend))))
3068 (with-current-buffer vc-status-process-buffer
3069 (cd def-dir)
3070 (erase-buffer)
3071 (vc-call-backend backend 'dir-status def-dir
3072 #'vc-update-vc-status-buffer status-buffer)))))
3073
3074 (defun vc-status-kill-dir-status-process ()
3075 "Kill the temporary buffer and associated process."
3076 (interactive)
3077 (when (buffer-live-p vc-status-process-buffer)
3078 (let ((proc (get-buffer-process vc-status-process-buffer)))
3079 (when proc (delete-process proc))
3080 (setq vc-status-process-buffer nil)
3081 (setq mode-line-process nil))))
3082
3083 (defun vc-status-next-line (arg)
3084 "Go to the next line.
3085 If a prefix argument is given, move by that many lines."
3086 (interactive "p")
3087 (ewoc-goto-next vc-status arg)
3088 (vc-status-move-to-goal-column))
3089
3090 (defun vc-status-previous-line (arg)
3091 "Go to the previous line.
3092 If a prefix argument is given, move by that many lines."
3093 (interactive "p")
3094 (ewoc-goto-prev vc-status arg)
3095 (vc-status-move-to-goal-column))
3096
3097 (defun vc-status-mark-unmark (mark-unmark-function)
3098 (if (use-region-p)
3099 (let ((firstl (line-number-at-pos (region-beginning)))
3100 (lastl (line-number-at-pos (region-end))))
3101 (save-excursion
3102 (goto-char (region-beginning))
3103 (while (<= (line-number-at-pos) lastl)
3104 (funcall mark-unmark-function))))
3105 (funcall mark-unmark-function)))
3106
3107 (defun vc-status-mark-file ()
3108 ;; Mark the current file and move to the next line.
3109 (let* ((crt (ewoc-locate vc-status))
3110 (file (ewoc-data crt)))
3111 (setf (vc-status-fileinfo->marked file) t)
3112 (ewoc-invalidate vc-status crt)
3113 (vc-status-next-line 1)))
3114
3115 (defun vc-status-mark ()
3116 "Mark the current file or all files in the region.
3117 If the region is active, mark all the files in the region.
3118 Otherwise mark the file on the current line and move to the next
3119 line."
3120 (interactive)
3121 (vc-status-mark-unmark 'vc-status-mark-file))
3122
3123
3124 ;; XXX: Should this take the region into consideration?
3125 (defun vc-status-mark-all-files (arg)
3126 "Mark all files with the same state as the current one.
3127 With a prefix argument mark all files.
3128
3129 The VC commands operate on files that are on the same state.
3130 This command is intended to make it easy to select all files that
3131 share the same state."
3132 (interactive "P")
3133 (if arg
3134 (ewoc-map
3135 (lambda (filearg)
3136 (unless (vc-status-fileinfo->marked filearg)
3137 (setf (vc-status-fileinfo->marked filearg) t)
3138 t))
3139 vc-status)
3140 (let* ((crt (ewoc-locate vc-status))
3141 (crt-state (vc-status-fileinfo->state (ewoc-data crt))))
3142 (ewoc-map
3143 (lambda (filearg)
3144 (when (and (not (vc-status-fileinfo->marked filearg))
3145 (eq (vc-status-fileinfo->state filearg) crt-state))
3146 (setf (vc-status-fileinfo->marked filearg) t)
3147 t))
3148 vc-status))))
3149
3150 (defun vc-status-unmark-file ()
3151 ;; Unmark the current file and move to the next line.
3152 (let* ((crt (ewoc-locate vc-status))
3153 (file (ewoc-data crt)))
3154 (setf (vc-status-fileinfo->marked file) nil)
3155 (ewoc-invalidate vc-status crt)
3156 (vc-status-next-line 1)))
3157
3158 (defun vc-status-unmark ()
3159 "Unmark the current file or all files in the region.
3160 If the region is active, unmark all the files in the region.
3161 Otherwise mark the file on the current line and move to the next
3162 line."
3163 (interactive)
3164 (vc-status-mark-unmark 'vc-status-unmark-file))
3165
3166 (defun vc-status-unmark-file-up ()
3167 "Move to the previous line and unmark the file."
3168 (interactive)
3169 ;; If we're on the first line, we won't move up, but we will still
3170 ;; remove the mark. This seems a bit odd but it is what buffer-menu
3171 ;; does.
3172 (let* ((prev (ewoc-goto-prev vc-status 1))
3173 (file (ewoc-data prev)))
3174 (setf (vc-status-fileinfo->marked file) nil)
3175 (ewoc-invalidate vc-status prev)
3176 (vc-status-move-to-goal-column)))
3177
3178 (defun vc-status-unmark-all-files (arg)
3179 "Unmark all files with the same state as the current one.
3180 With a prefix argument mark all files.
3181
3182 The VC commands operate on files that are on the same state.
3183 This command is intended to make it easy to deselect all files
3184 that share the same state."
3185 (interactive "P")
3186 (if arg
3187 (ewoc-map
3188 (lambda (filearg)
3189 (when (vc-status-fileinfo->marked filearg)
3190 (setf (vc-status-fileinfo->marked filearg) nil)
3191 t))
3192 vc-status)
3193 (let* ((crt (ewoc-locate vc-status))
3194 (crt-state (vc-status-fileinfo->state (ewoc-data crt))))
3195 (ewoc-map
3196 (lambda (filearg)
3197 (when (and (vc-status-fileinfo->marked filearg)
3198 (eq (vc-status-fileinfo->state filearg) crt-state))
3199 (setf (vc-status-fileinfo->marked filearg) nil)
3200 t))
3201 vc-status))))
3202
3203 (defun vc-status-toggle-mark-file ()
3204 (let* ((crt (ewoc-locate vc-status))
3205 (file (ewoc-data crt)))
3206 (if (vc-status-fileinfo->marked file)
3207 (vc-status-unmark-file)
3208 (vc-status-mark-file))))
3209
3210 (defun vc-status-toggle-mark ()
3211 (interactive)
3212 (vc-status-mark-unmark 'vc-status-toggle-mark-file))
3213
3214 (defun vc-status-register ()
3215 "Register the marked files, or the current file if no marks."
3216 (interactive)
3217 (let ((files (or (vc-status-marked-files)
3218 (list (vc-status-current-file)))))
3219 (dolist (file files)
3220 (vc-register file))))
3221
3222 (defun vc-status-find-file ()
3223 "Find the file on the current line."
3224 (interactive)
3225 (find-file (vc-status-current-file)))
3226
3227 (defun vc-status-find-file-other-window ()
3228 "Find the file on the current line, in another window."
3229 (interactive)
3230 (find-file-other-window (vc-status-current-file)))
3231
3232 (defun vc-status-current-file ()
3233 (let ((node (ewoc-locate vc-status)))
3234 (unless node
3235 (error "No file available."))
3236 (expand-file-name (vc-status-fileinfo->name (ewoc-data node)))))
3237
3238 (defun vc-status-marked-files ()
3239 "Return the list of marked files"
3240 (mapcar
3241 (lambda (elem)
3242 (expand-file-name (vc-status-fileinfo->name elem)))
3243 (ewoc-collect
3244 vc-status
3245 (lambda (crt) (vc-status-fileinfo->marked crt)))))
3246
3247 (defun vc-status-hide-up-to-date ()
3248 "Hide up-to-date items from display."
3249 (interactive)
3250 (ewoc-filter
3251 vc-status
3252 (lambda (crt) (not (eq (vc-status-fileinfo->state crt) 'up-to-date)))))
3253
3254 (defun vc-default-status-fileinfo-extra (backend file)
3255 nil)
3256
3257 (defun vc-status-mark-buffer-changed (&optional fname)
3258 (let* ((file (or fname (expand-file-name buffer-file-name)))
3259 (found-vc-status-buf nil))
3260 (save-excursion
3261 (dolist (status-buf (buffer-list))
3262 (set-buffer status-buf)
3263 ;; look for a vc-status buffer that might show this file.
3264 (when (eq major-mode 'vc-status-mode)
3265 (setq found-vc-status-buf t)
3266 (let ((ddir (expand-file-name default-directory)))
3267 ;; This test is cvs-string-prefix-p
3268 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
3269 (let*
3270 ((file-short (substring file (length ddir)))
3271 (backend (vc-backend file))
3272 (state (and backend (vc-state file)))
3273 (extra
3274 (and backend
3275 (vc-call-backend backend 'status-fileinfo-extra file)))
3276 (entry
3277 (list file-short (if state state 'unregistered) extra)))
3278 (vc-status-add-entry entry status-buf))))))
3279 ;; We didn't find any vc-status buffers, remove the hook, it is
3280 ;; not needed.
3281 (unless found-vc-status-buf (remove-hook 'after-save-hook 'vc-status-mark-buffer-changed)))))
3282
3283 ;;; End experimental code.
3284
3285 ;; Named-configuration entry points
3286
3287 (defun vc-snapshot-precondition (dir)
3288 "Scan the tree below DIR, looking for files not up-to-date.
3289 If any file is not up-to-date, return the name of the first such file.
3290 \(This means, neither snapshot creation nor retrieval is allowed.\)
3291 If one or more of the files are currently visited, return `visited'.
3292 Otherwise, return nil."
3293 (let ((status nil))
3294 (catch 'vc-locked-example
3295 (vc-file-tree-walk
3296 dir
3297 (lambda (f)
3298 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
3299 (if (get-file-buffer f) (setq status 'visited)))))
3300 status)))
3301
3302 ;;;###autoload
3303 (defun vc-create-snapshot (dir name branchp)
3304 "Descending recursively from DIR, make a snapshot called NAME.
3305 For each registered file, the working revision becomes part of
3306 the named configuration. If the prefix argument BRANCHP is
3307 given, the snapshot is made as a new branch and the files are
3308 checked out in that new branch."
3309 (interactive
3310 (list (read-file-name "Directory: " default-directory default-directory t)
3311 (read-string "New snapshot name: ")
3312 current-prefix-arg))
3313 (message "Making %s... " (if branchp "branch" "snapshot"))
3314 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
3315 (vc-call-backend (vc-responsible-backend dir)
3316 'create-snapshot dir name branchp)
3317 (message "Making %s... done" (if branchp "branch" "snapshot")))
3318
3319 ;;;###autoload
3320 (defun vc-retrieve-snapshot (dir name)
3321 "Descending recursively from DIR, retrieve the snapshot called NAME.
3322 If NAME is empty, it refers to the latest revisions.
3323 If locking is used for the files in DIR, then there must not be any
3324 locked files at or below DIR (but if NAME is empty, locked files are
3325 allowed and simply skipped)."
3326 (interactive
3327 (list (read-file-name "Directory: " default-directory default-directory t)
3328 (read-string "Snapshot name to retrieve (default latest revisions): ")))
3329 (let ((update (yes-or-no-p "Update any affected buffers? "))
3330 (msg (if (or (not name) (string= name ""))
3331 (format "Updating %s... " (abbreviate-file-name dir))
3332 (format "Retrieving snapshot into %s... "
3333 (abbreviate-file-name dir)))))
3334 (message "%s" msg)
3335 (vc-call-backend (vc-responsible-backend dir)
3336 'retrieve-snapshot dir name update)
3337 (message "%s" (concat msg "done"))))
3338
3339 ;; Miscellaneous other entry points
3340
3341 ;;;###autoload
3342 (defun vc-print-log (&optional working-revision)
3343 "List the change log of the current fileset in a window.
3344 If WORKING-REVISION is non-nil, leave the point at that revision."
3345 (interactive)
3346 (let* ((files (vc-deduce-fileset))
3347 (backend (vc-backend files))
3348 (working-revision (or working-revision (vc-working-revision (car files)))))
3349 ;; Don't switch to the output buffer before running the command,
3350 ;; so that any buffer-local settings in the vc-controlled
3351 ;; buffer can be accessed by the command.
3352 (vc-call-backend backend 'print-log files "*vc-change-log*")
3353 (pop-to-buffer "*vc-change-log*")
3354 (vc-exec-after
3355 `(let ((inhibit-read-only t))
3356 (vc-call-backend ',backend 'log-view-mode)
3357 (goto-char (point-max)) (forward-line -1)
3358 (while (looking-at "=*\n")
3359 (delete-char (- (match-end 0) (match-beginning 0)))
3360 (forward-line -1))
3361 (goto-char (point-min))
3362 (if (looking-at "[\b\t\n\v\f\r ]+")
3363 (delete-char (- (match-end 0) (match-beginning 0))))
3364 (shrink-window-if-larger-than-buffer)
3365 ;; move point to the log entry for the working revision
3366 (vc-call-backend ',backend 'show-log-entry ',working-revision)
3367 (setq vc-sentinel-movepoint (point))
3368 (set-buffer-modified-p nil)))))
3369
3370 ;;;###autoload
3371 (defun vc-revert ()
3372 "Revert working copies of the selected fileset to their repository contents.
3373 This asks for confirmation if the buffer contents are not identical
3374 to the working revision (except for keyword expansion)."
3375 (interactive)
3376 (let* ((files (vc-deduce-fileset)))
3377 ;; If any of the files is visited by the current buffer, make
3378 ;; sure buffer is saved. If the user says `no', abort since
3379 ;; we cannot show the changes and ask for confirmation to
3380 ;; discard them.
3381 (if (or (not files) (memq (buffer-file-name) files))
3382 (vc-buffer-sync nil))
3383 (dolist (file files)
3384 (let ((buf (get-file-buffer file)))
3385 (if (and buf (buffer-modified-p buf))
3386 (error "Please kill or save all modified buffers before reverting.")))
3387 (if (vc-up-to-date-p file)
3388 (unless (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
3389 (error "Revert canceled"))))
3390 (if (vc-diff-internal vc-allow-async-revert files nil nil)
3391 (progn
3392 (unless (yes-or-no-p (format "Discard changes in %s? " (vc-delistify files)))
3393 (error "Revert canceled"))
3394 (delete-windows-on "*vc-diff*")
3395 (kill-buffer "*vc-diff*")))
3396 (dolist (file files)
3397 (progn
3398 (message "Reverting %s..." (vc-delistify files))
3399 (vc-revert-file file)
3400 (message "Reverting %s...done" (vc-delistify files))))))
3401
3402 ;;;###autoload
3403 (defun vc-rollback ()
3404 "Roll back (remove) the most recent changeset committed to the repository.
3405 This may be either a file-level or a repository-level operation,
3406 depending on the underlying version-control system."
3407 (interactive)
3408 (let* ((files (vc-deduce-fileset))
3409 (backend (vc-backend files))
3410 (granularity (vc-call-backend backend 'revision-granularity)))
3411 (unless (vc-find-backend-function backend 'rollback)
3412 (error "Rollback is not supported in %s" backend))
3413 (if (and (not (eq granularity 'repository)) (/= (length files) 1))
3414 (error "Rollback requires a singleton fileset or repository versioning"))
3415 (if (not (vc-call latest-on-branch-p (car files)))
3416 (error "Rollback is only possible at the tip revision."))
3417 ;; If any of the files is visited by the current buffer, make
3418 ;; sure buffer is saved. If the user says `no', abort since
3419 ;; we cannot show the changes and ask for confirmation to
3420 ;; discard them.
3421 (if (or (not files) (memq (buffer-file-name) files))
3422 (vc-buffer-sync nil))
3423 (dolist (file files)
3424 (if (buffer-modified-p (get-file-buffer file))
3425 (error "Please kill or save all modified buffers before rollback."))
3426 (if (not (vc-up-to-date-p file))
3427 (error "Please revert all modified workfiles before rollback.")))
3428 ;; Accumulate changes associated with the fileset
3429 (vc-setup-buffer "*vc-diff*")
3430 (not-modified)
3431 (message "Finding changes...")
3432 (let* ((tip (vc-working-revision (car files)))
3433 (previous (vc-call previous-revision (car files) tip)))
3434 (vc-diff-internal nil files previous tip))
3435 ;; Display changes
3436 (unless (yes-or-no-p "Discard these revisions? ")
3437 (error "Rollback canceled"))
3438 (delete-windows-on "*vc-diff*")
3439 (kill-buffer"*vc-diff*")
3440 ;; Do the actual reversions
3441 (message "Rolling back %s..." (vc-delistify files))
3442 (with-vc-properties
3443 files
3444 (vc-call-backend backend 'rollback files)
3445 `((vc-state . ,'up-to-date)
3446 (vc-checkout-time . , (nth 5 (file-attributes file)))
3447 (vc-working-revision . nil)))
3448 (dolist (f files) (vc-resynch-buffer f t t))
3449 (message "Rolling back %s...done" (vc-delistify files))))
3450
3451 ;;;###autoload
3452 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
3453
3454 ;;;###autoload
3455 (defun vc-update ()
3456 "Update the current fileset's files to their tip revisions.
3457 For each one that contains no changes, and is not locked, then this simply
3458 replaces the work file with the latest revision on its branch. If the file
3459 contains changes, and the backend supports merging news, then any recent
3460 changes from the current branch are merged into the working file."
3461 (interactive)
3462 (dolist (file (vc-deduce-fileset))
3463 (when (let ((buf (get-file-buffer file)))
3464 (and buf (buffer-modified-p buf)))
3465 (error "Please kill or save all modified buffers before updating."))
3466 (if (vc-up-to-date-p file)
3467 (vc-checkout file nil t)
3468 (if (eq (vc-checkout-model file) 'locking)
3469 (if (eq (vc-state file) 'edited)
3470 (error "%s"
3471 (substitute-command-keys
3472 "File is locked--type \\[vc-revert] to discard changes"))
3473 (error "Unexpected file state (%s) -- type %s"
3474 (vc-state file)
3475 (substitute-command-keys
3476 "\\[vc-next-action] to correct")))
3477 (if (not (vc-find-backend-function (vc-backend file) 'merge-news))
3478 (error "Sorry, merging news is not implemented for %s"
3479 (vc-backend file))
3480 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))))
3481
3482 (defun vc-version-backup-file (file &optional rev)
3483 "Return name of backup file for revision REV of FILE.
3484 If version backups should be used for FILE, and there exists
3485 such a backup for REV or the working revision of file, return
3486 its name; otherwise return nil."
3487 (when (vc-call make-version-backups-p file)
3488 (let ((backup-file (vc-version-backup-file-name file rev)))
3489 (if (file-exists-p backup-file)
3490 backup-file
3491 ;; there is no automatic backup, but maybe the user made one manually
3492 (setq backup-file (vc-version-backup-file-name file rev 'manual))
3493 (if (file-exists-p backup-file)
3494 backup-file)))))
3495
3496 (defun vc-revert-file (file)
3497 "Revert FILE back to the repository working revision it was based on."
3498 (with-vc-properties
3499 (list file)
3500 (let ((backup-file (vc-version-backup-file file)))
3501 (when backup-file
3502 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
3503 (vc-delete-automatic-version-backups file))
3504 (vc-call revert file backup-file))
3505 `((vc-state . up-to-date)
3506 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
3507 (vc-resynch-buffer file t t))
3508
3509 ;;;###autoload
3510 (defun vc-switch-backend (file backend)
3511 "Make BACKEND the current version control system for FILE.
3512 FILE must already be registered in BACKEND. The change is not
3513 permanent, only for the current session. This function only changes
3514 VC's perspective on FILE, it does not register or unregister it.
3515 By default, this command cycles through the registered backends.
3516 To get a prompt, use a prefix argument."
3517 (interactive
3518 (list
3519 (or buffer-file-name
3520 (error "There is no version-controlled file in this buffer"))
3521 (let ((backend (vc-backend buffer-file-name))
3522 (backends nil))
3523 (unless backend
3524 (error "File %s is not under version control" buffer-file-name))
3525 ;; Find the registered backends.
3526 (dolist (backend vc-handled-backends)
3527 (when (vc-call-backend backend 'registered buffer-file-name)
3528 (push backend backends)))
3529 ;; Find the next backend.
3530 (let ((def (car (delq backend (append (memq backend backends) backends))))
3531 (others (delete backend backends)))
3532 (cond
3533 ((null others) (error "No other backend to switch to"))
3534 (current-prefix-arg
3535 (intern
3536 (upcase
3537 (completing-read
3538 (format "Switch to backend [%s]: " def)
3539 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
3540 nil t nil nil (downcase (symbol-name def))))))
3541 (t def))))))
3542 (unless (eq backend (vc-backend file))
3543 (vc-file-clearprops file)
3544 (vc-file-setprop file 'vc-backend backend)
3545 ;; Force recomputation of the state
3546 (unless (vc-call-backend backend 'registered file)
3547 (vc-file-clearprops file)
3548 (error "%s is not registered in %s" file backend))
3549 (vc-mode-line file)))
3550
3551 ;;;###autoload
3552 (defun vc-transfer-file (file new-backend)
3553 "Transfer FILE to another version control system NEW-BACKEND.
3554 If NEW-BACKEND has a higher precedence than FILE's current backend
3555 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
3556 NEW-BACKEND, using the revision number from the current backend as the
3557 base level. If NEW-BACKEND has a lower precedence than the current
3558 backend, then commit all changes that were made under the current
3559 backend to NEW-BACKEND, and unregister FILE from the current backend.
3560 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
3561 (let* ((old-backend (vc-backend file))
3562 (edited (memq (vc-state file) '(edited needs-merge)))
3563 (registered (vc-call-backend new-backend 'registered file))
3564 (move
3565 (and registered ; Never move if not registered in new-backend yet.
3566 ;; move if new-backend comes later in vc-handled-backends
3567 (or (memq new-backend (memq old-backend vc-handled-backends))
3568 (y-or-n-p "Final transfer? "))))
3569 (comment nil))
3570 (if (eq old-backend new-backend)
3571 (error "%s is the current backend of %s" new-backend file))
3572 (if registered
3573 (set-file-modes file (logior (file-modes file) 128))
3574 ;; `registered' might have switched under us.
3575 (vc-switch-backend file old-backend)
3576 (let* ((rev (vc-working-revision file))
3577 (modified-file (and edited (make-temp-file file)))
3578 (unmodified-file (and modified-file (vc-version-backup-file file))))
3579 ;; Go back to the base unmodified file.
3580 (unwind-protect
3581 (progn
3582 (when modified-file
3583 (copy-file file modified-file 'ok-if-already-exists)
3584 ;; If we have a local copy of the unmodified file, handle that
3585 ;; here and not in vc-revert-file because we don't want to
3586 ;; delete that copy -- it is still useful for OLD-BACKEND.
3587 (if unmodified-file
3588 (copy-file unmodified-file file
3589 'ok-if-already-exists 'keep-date)
3590 (if (y-or-n-p "Get base revision from master? ")
3591 (vc-revert-file file))))
3592 (vc-call-backend new-backend 'receive-file file rev))
3593 (when modified-file
3594 (vc-switch-backend file new-backend)
3595 (unless (eq (vc-checkout-model file) 'implicit)
3596 (vc-checkout file t nil))
3597 (rename-file modified-file file 'ok-if-already-exists)
3598 (vc-file-setprop file 'vc-checkout-time nil)))))
3599 (when move
3600 (vc-switch-backend file old-backend)
3601 (setq comment (vc-call comment-history file))
3602 (vc-call unregister file))
3603 (vc-switch-backend file new-backend)
3604 (when (or move edited)
3605 (vc-file-setprop file 'vc-state 'edited)
3606 (vc-mode-line file)
3607 (vc-checkin file nil comment (stringp comment)))))
3608
3609 (defun vc-rename-master (oldmaster newfile templates)
3610 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
3611 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
3612 (newdir (or (file-name-directory newfile) ""))
3613 (newbase (file-name-nondirectory newfile))
3614 (masters
3615 ;; List of potential master files for `newfile'
3616 (mapcar
3617 (lambda (s) (vc-possible-master s newdir newbase))
3618 templates)))
3619 (if (or (file-symlink-p oldmaster)
3620 (file-symlink-p (file-name-directory oldmaster)))
3621 (error "This is unsafe in the presence of symbolic links"))
3622 (rename-file
3623 oldmaster
3624 (catch 'found
3625 ;; If possible, keep the master file in the same directory.
3626 (dolist (f masters)
3627 (if (and f (string= (file-name-directory (expand-file-name f)) dir))
3628 (throw 'found f)))
3629 ;; If not, just use the first possible place.
3630 (dolist (f masters)
3631 (and f (or (not (setq dir (file-name-directory f)))
3632 (file-directory-p dir))
3633 (throw 'found f)))
3634 (error "New file lacks a version control directory")))))
3635
3636 (defun vc-delete-file (file)
3637 "Delete file and mark it as such in the version control system."
3638 (interactive "fVC delete file: ")
3639 (setq file (expand-file-name file))
3640 (let ((buf (get-file-buffer file))
3641 (backend (vc-backend file)))
3642 (unless backend
3643 (error "File %s is not under version control"
3644 (file-name-nondirectory file)))
3645 (unless (vc-find-backend-function backend 'delete-file)
3646 (error "Deleting files under %s is not supported in VC" backend))
3647 (if (and buf (buffer-modified-p buf))
3648 (error "Please save files before deleting them"))
3649 (unless (y-or-n-p (format "Really want to delete %s? "
3650 (file-name-nondirectory file)))
3651 (error "Abort!"))
3652 (unless (or (file-directory-p file) (null make-backup-files))
3653 (with-current-buffer (or buf (find-file-noselect file))
3654 (let ((backup-inhibited nil))
3655 (backup-buffer))
3656 ;; If we didn't have a buffer visiting the file before this
3657 ;; command, kill the buffer created by the above
3658 ;; `find-file-noselect' call.
3659 (unless buf (kill-buffer (current-buffer)))))
3660 (vc-call delete-file file)
3661 ;; If the backend hasn't deleted the file itself, let's do it for him.
3662 (if (file-exists-p file) (delete-file file))
3663 ;; Forget what VC knew about the file.
3664 (vc-file-clearprops file)
3665 (vc-resynch-buffer file buf t)))
3666
3667 ;;;###autoload
3668 (defun vc-rename-file (old new)
3669 "Rename file OLD to NEW, and rename its master file likewise."
3670 (interactive "fVC rename file: \nFRename to: ")
3671 (let ((oldbuf (get-file-buffer old)))
3672 (if (and oldbuf (buffer-modified-p oldbuf))
3673 (error "Please save files before moving them"))
3674 (if (get-file-buffer new)
3675 (error "Already editing new file name"))
3676 (if (file-exists-p new)
3677 (error "New file already exists"))
3678 (let ((state (vc-state old)))
3679 (unless (memq state '(up-to-date edited))
3680 (error "Please %s files before moving them"
3681 (if (stringp state) "check in" "update"))))
3682 (vc-call rename-file old new)
3683 (vc-file-clearprops old)
3684 ;; Move the actual file (unless the backend did it already)
3685 (if (file-exists-p old) (rename-file old new))
3686 ;; ?? Renaming a file might change its contents due to keyword expansion.
3687 ;; We should really check out a new copy if the old copy was precisely equal
3688 ;; to some checked-in revision. However, testing for this is tricky....
3689 (if oldbuf
3690 (with-current-buffer oldbuf
3691 (let ((buffer-read-only buffer-read-only))
3692 (set-visited-file-name new))
3693 (vc-backend new)
3694 (vc-mode-line new)
3695 (set-buffer-modified-p nil)))))
3696
3697 ;;;###autoload
3698 (defun vc-update-change-log (&rest args)
3699 "Find change log file and add entries from recent version control logs.
3700 Normally, find log entries for all registered files in the default
3701 directory.
3702
3703 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
3704
3705 With any numeric prefix arg, find log entries for all currently visited
3706 files that are under version control. This puts all the entries in the
3707 log for the default directory, which may not be appropriate.
3708
3709 From a program, any ARGS are assumed to be filenames for which
3710 log entries should be gathered."
3711 (interactive
3712 (cond ((consp current-prefix-arg) ;C-u
3713 (list buffer-file-name))
3714 (current-prefix-arg ;Numeric argument.
3715 (let ((files nil)
3716 (buffers (buffer-list))
3717 file)
3718 (while buffers
3719 (setq file (buffer-file-name (car buffers)))
3720 (and file (vc-backend file)
3721 (setq files (cons file files)))
3722 (setq buffers (cdr buffers)))
3723 files))
3724 (t
3725 ;; Don't supply any filenames to backend; this means
3726 ;; it should find all relevant files relative to
3727 ;; the default-directory.
3728 nil)))
3729 (vc-call-backend (vc-responsible-backend default-directory)
3730 'update-changelog args))
3731
3732 ;;; The default back end. Assumes RCS-like revision numbering.
3733
3734 (defun vc-default-revision-granularity ()
3735 (error "Your backend will not work with this version of VC mode."))
3736
3737 ;; functions that operate on RCS revision numbers. This code should
3738 ;; also be moved into the backends. It stays for now, however, since
3739 ;; it is used in code below.
3740 ;;;###autoload
3741 (defun vc-trunk-p (rev)
3742 "Return t if REV is a revision on the trunk."
3743 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
3744
3745 (defun vc-branch-p (rev)
3746 "Return t if REV is a branch revision."
3747 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
3748
3749 ;;;###autoload
3750 (defun vc-branch-part (rev)
3751 "Return the branch part of a revision number REV."
3752 (let ((index (string-match "\\.[0-9]+\\'" rev)))
3753 (if index
3754 (substring rev 0 index))))
3755
3756 (defun vc-minor-part (rev)
3757 "Return the minor revision number of a revision number REV."
3758 (string-match "[0-9]+\\'" rev)
3759 (substring rev (match-beginning 0) (match-end 0)))
3760
3761 (defun vc-default-previous-revision (backend file rev)
3762 "Return the revision number immediately preceding REV for FILE,
3763 or nil if there is no previous revision. This default
3764 implementation works for MAJOR.MINOR-style revision numbers as
3765 used by RCS and CVS."
3766 (let ((branch (vc-branch-part rev))
3767 (minor-num (string-to-number (vc-minor-part rev))))
3768 (when branch
3769 (if (> minor-num 1)
3770 ;; revision does probably not start a branch or release
3771 (concat branch "." (number-to-string (1- minor-num)))
3772 (if (vc-trunk-p rev)
3773 ;; we are at the beginning of the trunk --
3774 ;; don't know anything to return here
3775 nil
3776 ;; we are at the beginning of a branch --
3777 ;; return revision of starting point
3778 (vc-branch-part branch))))))
3779
3780 (defun vc-default-next-revision (backend file rev)
3781 "Return the revision number immediately following REV for FILE,
3782 or nil if there is no next revision. This default implementation
3783 works for MAJOR.MINOR-style revision numbers as used by RCS
3784 and CVS."
3785 (when (not (string= rev (vc-working-revision file)))
3786 (let ((branch (vc-branch-part rev))
3787 (minor-num (string-to-number (vc-minor-part rev))))
3788 (concat branch "." (number-to-string (1+ minor-num))))))
3789
3790 (defun vc-default-responsible-p (backend file)
3791 "Indicate whether BACKEND is reponsible for FILE.
3792 The default is to return nil always."
3793 nil)
3794
3795 (defun vc-default-could-register (backend file)
3796 "Return non-nil if BACKEND could be used to register FILE.
3797 The default implementation returns t for all files."
3798 t)
3799
3800 (defun vc-default-latest-on-branch-p (backend file)
3801 "Return non-nil if FILE is the latest on its branch.
3802 This default implementation always returns non-nil, which means that
3803 editing non-current revisions is not supported by default."
3804 t)
3805
3806 (defun vc-default-init-revision (backend) vc-default-init-revision)
3807
3808 (defalias 'vc-cvs-update-changelog 'vc-update-changelog-rcs2log)
3809 (defalias 'vc-rcs-update-changelog 'vc-update-changelog-rcs2log)
3810 ;; FIXME: This should probably be moved to vc-rcs.el and replaced in
3811 ;; vc-cvs.el by code using cvs2cl.
3812 (defun vc-update-changelog-rcs2log (files)
3813 "Default implementation of update-changelog.
3814 Uses `rcs2log' which only works for RCS and CVS."
3815 ;; FIXME: We (c|sh)ould add support for cvs2cl
3816 (let ((odefault default-directory)
3817 (changelog (find-change-log))
3818 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
3819 (tempfile (make-temp-file
3820 (expand-file-name "vc"
3821 (or small-temporary-file-directory
3822 temporary-file-directory))))
3823 (login-name (or user-login-name
3824 (format "uid%d" (number-to-string (user-uid)))))
3825 (full-name (or add-log-full-name
3826 (user-full-name)
3827 (user-login-name)
3828 (format "uid%d" (number-to-string (user-uid)))))
3829 (mailing-address (or add-log-mailing-address
3830 user-mail-address)))
3831 (find-file-other-window changelog)
3832 (barf-if-buffer-read-only)
3833 (vc-buffer-sync)
3834 (undo-boundary)
3835 (goto-char (point-min))
3836 (push-mark)
3837 (message "Computing change log entries...")
3838 (message "Computing change log entries... %s"
3839 (unwind-protect
3840 (progn
3841 (setq default-directory odefault)
3842 (if (eq 0 (apply 'call-process
3843 (expand-file-name "rcs2log"
3844 exec-directory)
3845 nil (list t tempfile) nil
3846 "-c" changelog
3847 "-u" (concat login-name
3848 "\t" full-name
3849 "\t" mailing-address)
3850 (mapcar
3851 (lambda (f)
3852 (file-relative-name
3853 (expand-file-name f odefault)))
3854 files)))
3855 "done"
3856 (pop-to-buffer (get-buffer-create "*vc*"))
3857 (erase-buffer)
3858 (insert-file-contents tempfile)
3859 "failed"))
3860 (setq default-directory (file-name-directory changelog))
3861 (delete-file tempfile)))))
3862
3863 (defun vc-default-find-revision (backend file rev buffer)
3864 "Provide the new `find-revision' op based on the old `checkout' op.
3865 This is only for compatibility with old backends. They should be updated
3866 to provide the `find-revision' operation instead."
3867 (let ((tmpfile (make-temp-file (expand-file-name file))))
3868 (unwind-protect
3869 (progn
3870 (vc-call-backend backend 'checkout file nil rev tmpfile)
3871 (with-current-buffer buffer
3872 (insert-file-contents-literally tmpfile)))
3873 (delete-file tmpfile))))
3874
3875 (defun vc-default-dired-state-info (backend file)
3876 (let* ((state (vc-state file))
3877 (statestring
3878 (cond
3879 ((stringp state) (concat "(" state ")"))
3880 ((eq state 'edited) "(modified)")
3881 ((eq state 'needs-merge) "(merge)")
3882 ((eq state 'needs-patch) "(patch)")
3883 ((eq state 'added) "(added)")
3884 ((eq state 'removed) "(removed)")
3885 ((eq state 'ignored) "(ignored)") ;; dired-hook filters this out
3886 ((eq state 'unregistered) "?")
3887 ((eq state 'unlocked-changes) "(stale)")
3888 ((not state) "(unknown)")))
3889 (buffer
3890 (get-file-buffer file))
3891 (modflag
3892 (if (and buffer (buffer-modified-p buffer)) "+" "")))
3893 (concat statestring modflag)))
3894
3895 (defun vc-default-rename-file (backend old new)
3896 (condition-case nil
3897 (add-name-to-file old new)
3898 (error (rename-file old new)))
3899 (vc-delete-file old)
3900 (with-current-buffer (find-file-noselect new)
3901 (vc-register)))
3902
3903 (defalias 'vc-default-logentry-check 'ignore)
3904 (defalias 'vc-default-check-headers 'ignore)
3905
3906 (defun vc-default-log-view-mode (backend) (log-view-mode))
3907
3908 (defun vc-default-show-log-entry (backend rev)
3909 (with-no-warnings
3910 (log-view-goto-rev rev)))
3911
3912 (defun vc-default-comment-history (backend file)
3913 "Return a string with all log entries stored in BACKEND for FILE."
3914 (if (vc-find-backend-function backend 'print-log)
3915 (with-current-buffer "*vc*"
3916 (vc-call print-log (list file))
3917 (vc-call-backend backend 'wash-log)
3918 (buffer-string))))
3919
3920 (defun vc-default-receive-file (backend file rev)
3921 "Let BACKEND receive FILE from another version control system."
3922 (vc-call-backend backend 'register file rev ""))
3923
3924 (defun vc-default-create-snapshot (backend dir name branchp)
3925 (when branchp
3926 (error "VC backend %s does not support module branches" backend))
3927 (let ((result (vc-snapshot-precondition dir)))
3928 (if (stringp result)
3929 (error "File %s is not up-to-date" result)
3930 (vc-file-tree-walk
3931 dir
3932 (lambda (f)
3933 (vc-call assign-name f name))))))
3934
3935 (defun vc-default-retrieve-snapshot (backend dir name update)
3936 (if (string= name "")
3937 (progn
3938 (vc-file-tree-walk
3939 dir
3940 (lambda (f) (and
3941 (vc-up-to-date-p f)
3942 (vc-error-occurred
3943 (vc-call checkout f nil "")
3944 (if update (vc-resynch-buffer f t t)))))))
3945 (let ((result (vc-snapshot-precondition dir)))
3946 (if (stringp result)
3947 (error "File %s is locked" result)
3948 (setq update (and (eq result 'visited) update))
3949 (vc-file-tree-walk
3950 dir
3951 (lambda (f) (vc-error-occurred
3952 (vc-call checkout f nil name)
3953 (if update (vc-resynch-buffer f t t)))))))))
3954
3955 (defun vc-default-revert (backend file contents-done)
3956 (unless contents-done
3957 (let ((rev (vc-working-revision file))
3958 (file-buffer (or (get-file-buffer file) (current-buffer))))
3959 (message "Checking out %s..." file)
3960 (let ((failed t)
3961 (backup-name (car (find-backup-file-name file))))
3962 (when backup-name
3963 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
3964 (unless (file-writable-p file)
3965 (set-file-modes file (logior (file-modes file) 128))))
3966 (unwind-protect
3967 (let ((coding-system-for-read 'no-conversion)
3968 (coding-system-for-write 'no-conversion))
3969 (with-temp-file file
3970 (let ((outbuf (current-buffer)))
3971 ;; Change buffer to get local value of vc-checkout-switches.
3972 (with-current-buffer file-buffer
3973 (let ((default-directory (file-name-directory file)))
3974 (vc-call find-revision file rev outbuf)))))
3975 (setq failed nil))
3976 (when backup-name
3977 (if failed
3978 (rename-file backup-name file 'ok-if-already-exists)
3979 (and (not vc-make-backup-files) (delete-file backup-name))))))
3980 (message "Checking out %s...done" file))))
3981
3982 (defalias 'vc-default-revision-completion-table 'ignore)
3983
3984 (defun vc-check-headers ()
3985 "Check if the current file has any headers in it."
3986 (interactive)
3987 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3988
3989 ;;; Annotate functionality
3990
3991 ;; Declare globally instead of additional parameter to
3992 ;; temp-buffer-show-function (not possible to pass more than one
3993 ;; parameter). The use of annotate-ratio is deprecated in favor of
3994 ;; annotate-mode, which replaces it with the more sensible "span-to
3995 ;; days", along with autoscaling support.
3996 (defvar vc-annotate-ratio nil "Global variable.")
3997
3998 ;; internal buffer-local variables
3999 (defvar vc-annotate-backend nil)
4000 (defvar vc-annotate-parent-file nil)
4001 (defvar vc-annotate-parent-rev nil)
4002 (defvar vc-annotate-parent-display-mode nil)
4003
4004 (defconst vc-annotate-font-lock-keywords
4005 ;; The fontification is done by vc-annotate-lines instead of font-lock.
4006 '((vc-annotate-lines)))
4007
4008 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
4009 "Major mode for output buffers of the `vc-annotate' command.
4010
4011 You can use the mode-specific menu to alter the time-span of the used
4012 colors. See variable `vc-annotate-menu-elements' for customizing the
4013 menu items."
4014 ;; Frob buffer-invisibility-spec so that if it is originally a naked t,
4015 ;; it will become a list, to avoid initial annotations being invisible.
4016 (add-to-invisibility-spec 'foo)
4017 (remove-from-invisibility-spec 'foo)
4018 (set (make-local-variable 'truncate-lines) t)
4019 (set (make-local-variable 'font-lock-defaults)
4020 '(vc-annotate-font-lock-keywords t))
4021 (view-mode 1))
4022
4023 (defun vc-annotate-toggle-annotation-visibility ()
4024 "Toggle whether or not the annotation is visible."
4025 (interactive)
4026 (funcall (if (memq 'vc-annotate-annotation buffer-invisibility-spec)
4027 'remove-from-invisibility-spec
4028 'add-to-invisibility-spec)
4029 'vc-annotate-annotation)
4030 (force-window-update (current-buffer)))
4031
4032 (defun vc-annotate-display-default (ratio)
4033 "Display the output of \\[vc-annotate] using the default color range.
4034 The color range is given by `vc-annotate-color-map', scaled by RATIO.
4035 The current time is used as the offset."
4036 (interactive (progn (kill-local-variable 'vc-annotate-color-map) '(1.0)))
4037 (message "Redisplaying annotation...")
4038 (vc-annotate-display ratio)
4039 (message "Redisplaying annotation...done"))
4040
4041 (defun vc-annotate-oldest-in-map (color-map)
4042 "Return the oldest time in the COLOR-MAP."
4043 ;; Since entries should be sorted, we can just use the last one.
4044 (caar (last color-map)))
4045
4046 (defun vc-annotate-get-time-set-line-props ()
4047 (let ((bol (point))
4048 (date (vc-call-backend vc-annotate-backend 'annotate-time))
4049 (inhibit-read-only t))
4050 (assert (>= (point) bol))
4051 (put-text-property bol (point) 'invisible 'vc-annotate-annotation)
4052 date))
4053
4054 (defun vc-annotate-display-autoscale (&optional full)
4055 "Highlight the output of \\[vc-annotate] using an autoscaled color map.
4056 Autoscaling means that the map is scaled from the current time to the
4057 oldest annotation in the buffer, or, with prefix argument FULL, to
4058 cover the range from the oldest annotation to the newest."
4059 (interactive "P")
4060 (let ((newest 0.0)
4061 (oldest 999999.) ;Any CVS users at the founding of Rome?
4062 (current (vc-annotate-convert-time (current-time)))
4063 date)
4064 (message "Redisplaying annotation...")
4065 ;; Run through this file and find the oldest and newest dates annotated.
4066 (save-excursion
4067 (goto-char (point-min))
4068 (while (not (eobp))
4069 (when (setq date (vc-annotate-get-time-set-line-props))
4070 (if (> date newest)
4071 (setq newest date))
4072 (if (< date oldest)
4073 (setq oldest date)))
4074 (forward-line 1)))
4075 (vc-annotate-display
4076 (/ (- (if full newest current) oldest)
4077 (vc-annotate-oldest-in-map vc-annotate-color-map))
4078 (if full newest))
4079 (message "Redisplaying annotation...done \(%s\)"
4080 (if full
4081 (format "Spanned from %.1f to %.1f days old"
4082 (- current oldest)
4083 (- current newest))
4084 (format "Spanned to %.1f days old" (- current oldest))))))
4085
4086 ;; Menu -- Using easymenu.el
4087 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
4088 "VC Annotate Display Menu"
4089 `("VC-Annotate"
4090 ["By Color Map Range" (unless (null vc-annotate-display-mode)
4091 (setq vc-annotate-display-mode nil)
4092 (vc-annotate-display-select))
4093 :style toggle :selected (null vc-annotate-display-mode)]
4094 ,@(let ((oldest-in-map (vc-annotate-oldest-in-map vc-annotate-color-map)))
4095 (mapcar (lambda (element)
4096 (let ((days (* element oldest-in-map)))
4097 `[,(format "Span %.1f days" days)
4098 (vc-annotate-display-select nil ,days)
4099 :style toggle :selected
4100 (eql vc-annotate-display-mode ,days) ]))
4101 vc-annotate-menu-elements))
4102 ["Span ..."
4103 (vc-annotate-display-select
4104 nil (float (string-to-number (read-string "Span how many days? "))))]
4105 "--"
4106 ["Span to Oldest"
4107 (unless (eq vc-annotate-display-mode 'scale)
4108 (vc-annotate-display-select nil 'scale))
4109 :help
4110 "Use an autoscaled color map from the oldest annotation to the current time"
4111 :style toggle :selected
4112 (eq vc-annotate-display-mode 'scale)]
4113 ["Span Oldest->Newest"
4114 (unless (eq vc-annotate-display-mode 'fullscale)
4115 (vc-annotate-display-select nil 'fullscale))
4116 :help
4117 "Use an autoscaled color map from the oldest to the newest annotation"
4118 :style toggle :selected
4119 (eq vc-annotate-display-mode 'fullscale)]
4120 "--"
4121 ["Toggle annotation visibility" vc-annotate-toggle-annotation-visibility
4122 :help
4123 "Toggle whether the annotation is visible or not"]
4124 ["Annotate previous revision" vc-annotate-prev-revision
4125 :help "Visit the annotation of the revision previous to this one"]
4126 ["Annotate next revision" vc-annotate-next-revision
4127 :help "Visit the annotation of the revision after this one"]
4128 ["Annotate revision at line" vc-annotate-revision-at-line
4129 :help
4130 "Visit the annotation of the revision identified in the current line"]
4131 ["Annotate revision previous to line" vc-annotate-revision-previous-to-line
4132 :help "Visit the annotation of the revision before the revision at line"]
4133 ["Annotate latest revision" vc-annotate-working-revision
4134 :help "Visit the annotation of the working revision of this file"]
4135 ["Show log of revision at line" vc-annotate-show-log-revision-at-line
4136 :help "Visit the log of the revision at line"]
4137 ["Show diff of revision at line" vc-annotate-show-diff-revision-at-line
4138 :help
4139 "Visit the diff of the revision at line from its previous revision"]
4140 ["Visit revision at line" vc-annotate-find-revision-at-line
4141 :help "Visit the revision identified in the current line"]))
4142
4143 (defun vc-annotate-display-select (&optional buffer mode)
4144 "Highlight the output of \\[vc-annotate].
4145 By default, the current buffer is highlighted, unless overridden by
4146 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
4147 use; you may override this using the second optional arg MODE."
4148 (interactive)
4149 (if mode (setq vc-annotate-display-mode mode))
4150 (pop-to-buffer (or buffer (current-buffer)))
4151 (cond ((null vc-annotate-display-mode)
4152 ;; The ratio is global, thus relative to the global color-map.
4153 (kill-local-variable 'vc-annotate-color-map)
4154 (vc-annotate-display-default (or vc-annotate-ratio 1.0)))
4155 ;; One of the auto-scaling modes
4156 ((eq vc-annotate-display-mode 'scale)
4157 (vc-exec-after `(vc-annotate-display-autoscale)))
4158 ((eq vc-annotate-display-mode 'fullscale)
4159 (vc-exec-after `(vc-annotate-display-autoscale t)))
4160 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
4161 (vc-annotate-display-default
4162 (/ vc-annotate-display-mode
4163 (vc-annotate-oldest-in-map vc-annotate-color-map))))
4164 (t (error "No such display mode: %s"
4165 vc-annotate-display-mode))))
4166
4167 ;;;###autoload
4168 (defun vc-annotate (file rev &optional display-mode buf move-point-to)
4169 "Display the edit history of the current file using colors.
4170
4171 This command creates a buffer that shows, for each line of the current
4172 file, when it was last edited and by whom. Additionally, colors are
4173 used to show the age of each line--blue means oldest, red means
4174 youngest, and intermediate colors indicate intermediate ages. By
4175 default, the time scale stretches back one year into the past;
4176 everything that is older than that is shown in blue.
4177
4178 With a prefix argument, this command asks two questions in the
4179 minibuffer. First, you may enter a revision number; then the buffer
4180 displays and annotates that revision instead of the working revision
4181 \(type RET in the minibuffer to leave that default unchanged). Then,
4182 you are prompted for the time span in days which the color range
4183 should cover. For example, a time span of 20 days means that changes
4184 over the past 20 days are shown in red to blue, according to their
4185 age, and everything that is older than that is shown in blue.
4186
4187 If MOVE-POINT-TO is given, move the point to that line.
4188
4189 Customization variables:
4190
4191 `vc-annotate-menu-elements' customizes the menu elements of the
4192 mode-specific menu. `vc-annotate-color-map' and
4193 `vc-annotate-very-old-color' define the mapping of time to colors.
4194 `vc-annotate-background' specifies the background color."
4195 (interactive
4196 (save-current-buffer
4197 (vc-ensure-vc-buffer)
4198 (list buffer-file-name
4199 (let ((def (vc-working-revision buffer-file-name)))
4200 (if (null current-prefix-arg) def
4201 (read-string
4202 (format "Annotate from revision (default %s): " def)
4203 nil nil def)))
4204 (if (null current-prefix-arg)
4205 vc-annotate-display-mode
4206 (float (string-to-number
4207 (read-string "Annotate span days (default 20): "
4208 nil nil "20")))))))
4209 (vc-ensure-vc-buffer)
4210 (setq vc-annotate-display-mode display-mode) ;Not sure why. --Stef
4211 (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
4212 (temp-buffer-show-function 'vc-annotate-display-select)
4213 ;; If BUF is specified, we presume the caller maintains current line,
4214 ;; so we don't need to do it here. This implementation may give
4215 ;; strange results occasionally in the case of REV != WORKFILE-REV.
4216 (current-line (or move-point-to (unless buf (line-number-at-pos)))))
4217 (message "Annotating...")
4218 ;; If BUF is specified it tells in which buffer we should put the
4219 ;; annotations. This is used when switching annotations to another
4220 ;; revision, so we should update the buffer's name.
4221 (if buf (with-current-buffer buf
4222 (rename-buffer temp-buffer-name t)
4223 ;; In case it had to be uniquified.
4224 (setq temp-buffer-name (buffer-name))))
4225 (with-output-to-temp-buffer temp-buffer-name
4226 (vc-call annotate-command file (get-buffer temp-buffer-name) rev)
4227 ;; we must setup the mode first, and then set our local
4228 ;; variables before the show-function is called at the exit of
4229 ;; with-output-to-temp-buffer
4230 (with-current-buffer temp-buffer-name
4231 (if (not (equal major-mode 'vc-annotate-mode))
4232 (vc-annotate-mode))
4233 (set (make-local-variable 'vc-annotate-backend) (vc-backend file))
4234 (set (make-local-variable 'vc-annotate-parent-file) file)
4235 (set (make-local-variable 'vc-annotate-parent-rev) rev)
4236 (set (make-local-variable 'vc-annotate-parent-display-mode)
4237 display-mode)))
4238
4239 (with-current-buffer temp-buffer-name
4240 (vc-exec-after
4241 `(progn
4242 ;; Ideally, we'd rather not move point if the user has already
4243 ;; moved it elsewhere, but really point here is not the position
4244 ;; of the user's cursor :-(
4245 (when ,current-line ;(and (bobp))
4246 (goto-line ,current-line)
4247 (setq vc-sentinel-movepoint (point)))
4248 (unless (active-minibuffer-window)
4249 (message "Annotating... done")))))))
4250
4251 (defun vc-annotate-prev-revision (prefix)
4252 "Visit the annotation of the revision previous to this one.
4253
4254 With a numeric prefix argument, annotate the revision that many
4255 revisions previous."
4256 (interactive "p")
4257 (vc-annotate-warp-revision (- 0 prefix)))
4258
4259 (defun vc-annotate-next-revision (prefix)
4260 "Visit the annotation of the revision after this one.
4261
4262 With a numeric prefix argument, annotate the revision that many
4263 revisions after."
4264 (interactive "p")
4265 (vc-annotate-warp-revision prefix))
4266
4267 (defun vc-annotate-working-revision ()
4268 "Visit the annotation of the working revision of this file."
4269 (interactive)
4270 (if (not (equal major-mode 'vc-annotate-mode))
4271 (message "Cannot be invoked outside of a vc annotate buffer")
4272 (let ((warp-rev (vc-working-revision vc-annotate-parent-file)))
4273 (if (equal warp-rev vc-annotate-parent-rev)
4274 (message "Already at revision %s" warp-rev)
4275 (vc-annotate-warp-revision warp-rev)))))
4276
4277 (defun vc-annotate-extract-revision-at-line ()
4278 "Extract the revision number of the current line."
4279 ;; This function must be invoked from a buffer in vc-annotate-mode
4280 (vc-call-backend vc-annotate-backend 'annotate-extract-revision-at-line))
4281
4282 (defun vc-annotate-revision-at-line ()
4283 "Visit the annotation of the revision identified in the current line."
4284 (interactive)
4285 (if (not (equal major-mode 'vc-annotate-mode))
4286 (message "Cannot be invoked outside of a vc annotate buffer")
4287 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4288 (if (not rev-at-line)
4289 (message "Cannot extract revision number from the current line")
4290 (if (equal rev-at-line vc-annotate-parent-rev)
4291 (message "Already at revision %s" rev-at-line)
4292 (vc-annotate-warp-revision rev-at-line))))))
4293
4294 (defun vc-annotate-find-revision-at-line ()
4295 "Visit the revision identified in the current line."
4296 (interactive)
4297 (if (not (equal major-mode 'vc-annotate-mode))
4298 (message "Cannot be invoked outside of a vc annotate buffer")
4299 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4300 (if (not rev-at-line)
4301 (message "Cannot extract revision number from the current line")
4302 (vc-revision-other-window rev-at-line)))))
4303
4304 (defun vc-annotate-revision-previous-to-line ()
4305 "Visit the annotation of the revision before the revision at line."
4306 (interactive)
4307 (if (not (equal major-mode 'vc-annotate-mode))
4308 (message "Cannot be invoked outside of a vc annotate buffer")
4309 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
4310 (prev-rev nil))
4311 (if (not rev-at-line)
4312 (message "Cannot extract revision number from the current line")
4313 (setq prev-rev
4314 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
4315 (vc-annotate-warp-revision prev-rev)))))
4316
4317 (defun vc-annotate-show-log-revision-at-line ()
4318 "Visit the log of the revision at line."
4319 (interactive)
4320 (if (not (equal major-mode 'vc-annotate-mode))
4321 (message "Cannot be invoked outside of a vc annotate buffer")
4322 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4323 (if (not rev-at-line)
4324 (message "Cannot extract revision number from the current line")
4325 (vc-print-log rev-at-line)))))
4326
4327 (defun vc-annotate-show-diff-revision-at-line ()
4328 "Visit the diff of the revision at line from its previous revision."
4329 (interactive)
4330 (if (not (equal major-mode 'vc-annotate-mode))
4331 (message "Cannot be invoked outside of a vc annotate buffer")
4332 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
4333 (prev-rev nil))
4334 (if (not rev-at-line)
4335 (message "Cannot extract revision number from the current line")
4336 (setq prev-rev
4337 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
4338 (if (not prev-rev)
4339 (message "Cannot diff from any revision prior to %s" rev-at-line)
4340 (save-window-excursion
4341 (vc-diff-internal nil (list vc-annotate-parent-file)
4342 prev-rev rev-at-line))
4343 (switch-to-buffer "*vc-diff*"))))))
4344
4345 (defun vc-annotate-warp-revision (revspec)
4346 "Annotate the revision described by REVSPEC.
4347
4348 If REVSPEC is a positive integer, warp that many revisions
4349 forward, if possible, otherwise echo a warning message. If
4350 REVSPEC is a negative integer, warp that many revisions backward,
4351 if possible, otherwise echo a warning message. If REVSPEC is a
4352 string, then it describes a revision number, so warp to that
4353 revision."
4354 (if (not (equal major-mode 'vc-annotate-mode))
4355 (message "Cannot be invoked outside of a vc annotate buffer")
4356 (let* ((buf (current-buffer))
4357 (oldline (line-number-at-pos))
4358 (revspeccopy revspec)
4359 (newrev nil))
4360 (cond
4361 ((and (integerp revspec) (> revspec 0))
4362 (setq newrev vc-annotate-parent-rev)
4363 (while (and (> revspec 0) newrev)
4364 (setq newrev (vc-call next-revision
4365 vc-annotate-parent-file newrev))
4366 (setq revspec (1- revspec)))
4367 (if (not newrev)
4368 (message "Cannot increment %d revisions from revision %s"
4369 revspeccopy vc-annotate-parent-rev)))
4370 ((and (integerp revspec) (< revspec 0))
4371 (setq newrev vc-annotate-parent-rev)
4372 (while (and (< revspec 0) newrev)
4373 (setq newrev (vc-call previous-revision
4374 vc-annotate-parent-file newrev))
4375 (setq revspec (1+ revspec)))
4376 (if (not newrev)
4377 (message "Cannot decrement %d revisions from revision %s"
4378 (- 0 revspeccopy) vc-annotate-parent-rev)))
4379 ((stringp revspec) (setq newrev revspec))
4380 (t (error "Invalid argument to vc-annotate-warp-revision")))
4381 (when newrev
4382 (vc-annotate vc-annotate-parent-file newrev
4383 vc-annotate-parent-display-mode
4384 buf
4385 ;; Pass the current line so that vc-annotate will
4386 ;; place the point in the line.
4387 (min oldline (progn (goto-char (point-max))
4388 (forward-line -1)
4389 (line-number-at-pos))))))))
4390
4391 (defun vc-annotate-compcar (threshold a-list)
4392 "Test successive cons cells of A-LIST against THRESHOLD.
4393 Return the first cons cell with a car that is not less than THRESHOLD,
4394 nil if no such cell exists."
4395 (let ((i 1)
4396 (tmp-cons (car a-list)))
4397 (while (and tmp-cons (< (car tmp-cons) threshold))
4398 (setq tmp-cons (car (nthcdr i a-list)))
4399 (setq i (+ i 1)))
4400 tmp-cons)) ; Return the appropriate value
4401
4402 (defun vc-annotate-convert-time (time)
4403 "Convert a time value to a floating-point number of days.
4404 The argument TIME is a list as returned by `current-time' or
4405 `encode-time', only the first two elements of that list are considered."
4406 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
4407
4408 (defun vc-annotate-difference (&optional offset)
4409 "Return the time span in days to the next annotation.
4410 This calls the backend function annotate-time, and returns the
4411 difference in days between the time returned and the current time,
4412 or OFFSET if present."
4413 (let ((next-time (vc-annotate-get-time-set-line-props)))
4414 (if next-time
4415 (- (or offset
4416 (vc-call-backend vc-annotate-backend 'annotate-current-time))
4417 next-time))))
4418
4419 (defun vc-default-annotate-current-time (backend)
4420 "Return the current time, encoded as fractional days."
4421 (vc-annotate-convert-time (current-time)))
4422
4423 (defvar vc-annotate-offset nil)
4424
4425 (defun vc-annotate-display (ratio &optional offset)
4426 "Highlight `vc-annotate' output in the current buffer.
4427 RATIO, is the expansion that should be applied to `vc-annotate-color-map'.
4428 The annotations are relative to the current time, unless overridden by OFFSET."
4429 (if (/= ratio 1.0)
4430 (set (make-local-variable 'vc-annotate-color-map)
4431 (mapcar (lambda (elem) (cons (* (car elem) ratio) (cdr elem)))
4432 vc-annotate-color-map)))
4433 (set (make-local-variable 'vc-annotate-offset) offset)
4434 (font-lock-mode 1))
4435
4436 (defun vc-annotate-lines (limit)
4437 (while (< (point) limit)
4438 (let ((difference (vc-annotate-difference vc-annotate-offset))
4439 (start (point))
4440 (end (progn (forward-line 1) (point))))
4441 (when difference
4442 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
4443 (cons nil vc-annotate-very-old-color)))
4444 ;; substring from index 1 to remove any leading `#' in the name
4445 (face-name (concat "vc-annotate-face-"
4446 (if (string-equal
4447 (substring (cdr color) 0 1) "#")
4448 (substring (cdr color) 1)
4449 (cdr color))))
4450 ;; Make the face if not done.
4451 (face (or (intern-soft face-name)
4452 (let ((tmp-face (make-face (intern face-name))))
4453 (set-face-foreground tmp-face (cdr color))
4454 (if vc-annotate-background
4455 (set-face-background tmp-face
4456 vc-annotate-background))
4457 tmp-face)))) ; Return the face
4458 (put-text-property start end 'face face)))))
4459 ;; Pretend to font-lock there were no matches.
4460 nil)
4461 \f
4462
4463 ;; Set up key bindings for use while editing log messages
4464
4465 (defun vc-log-edit (fileset)
4466 "Set up `log-edit' for use with VC on FILE."
4467 (setq default-directory
4468 (with-current-buffer vc-parent-buffer default-directory))
4469 (log-edit 'vc-finish-logentry
4470 nil
4471 `((log-edit-listfun . (lambda () ',fileset))
4472 (log-edit-diff-function . (lambda () (vc-diff nil)))))
4473 (set (make-local-variable 'vc-log-fileset) fileset)
4474 (make-local-variable 'vc-log-revision)
4475 (set-buffer-modified-p nil)
4476 (setq buffer-file-name nil))
4477
4478 ;; These things should probably be generally available
4479
4480 (defun vc-file-tree-walk (dirname func &rest args)
4481 "Walk recursively through DIRNAME.
4482 Invoke FUNC f ARGS on each VC-managed file f underneath it."
4483 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
4484 (message "Traversing directory %s...done" dirname))
4485
4486 (defun vc-file-tree-walk-internal (file func args)
4487 (if (not (file-directory-p file))
4488 (if (vc-backend file) (apply func file args))
4489 (message "Traversing directory %s..." (abbreviate-file-name file))
4490 (let ((dir (file-name-as-directory file)))
4491 (mapcar
4492 (lambda (f) (or
4493 (string-equal f ".")
4494 (string-equal f "..")
4495 (member f vc-directory-exclusion-list)
4496 (let ((dirf (expand-file-name f dir)))
4497 (or
4498 (file-symlink-p dirf) ;; Avoid possible loops.
4499 (vc-file-tree-walk-internal dirf func args)))))
4500 (directory-files dir)))))
4501
4502 (provide 'vc)
4503
4504 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
4505 ;;
4506 ;; These may be useful to anyone who has to debug or extend the package.
4507 ;; (Note that this information corresponds to versions 5.x. Some of it
4508 ;; might have been invalidated by the additions to support branching
4509 ;; and RCS keyword lookup. AS, 1995/03/24)
4510 ;;
4511 ;; A fundamental problem in VC is that there are time windows between
4512 ;; vc-next-action's computations of the file's version-control state and
4513 ;; the actions that change it. This is a window open to lossage in a
4514 ;; multi-user environment; someone else could nip in and change the state
4515 ;; of the master during it.
4516 ;;
4517 ;; The performance problem is that rlog/prs calls are very expensive; we want
4518 ;; to avoid them as much as possible.
4519 ;;
4520 ;; ANALYSIS:
4521 ;;
4522 ;; The performance problem, it turns out, simplifies in practice to the
4523 ;; problem of making vc-state fast. The two other functions that call
4524 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
4525 ;; makes snapshots, the other deletes the calling user's last change in the
4526 ;; master.
4527 ;;
4528 ;; The race condition implies that we have to either (a) lock the master
4529 ;; during the entire execution of vc-next-action, or (b) detect and
4530 ;; recover from errors resulting from dispatch on an out-of-date state.
4531 ;;
4532 ;; Alternative (a) appears to be infeasible. The problem is that we can't
4533 ;; guarantee that the lock will ever be removed. Suppose a user starts a
4534 ;; checkin, the change message buffer pops up, and the user, having wandered
4535 ;; off to do something else, simply forgets about it?
4536 ;;
4537 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
4538 ;; vc-state. Usually, if a file is registered, we can read its locked/
4539 ;; unlocked state and its current owner from its permissions.
4540 ;;
4541 ;; This shortcut will fail if someone has manually changed the workfile's
4542 ;; permissions; also if developers are munging the workfile in several
4543 ;; directories, with symlinks to a master (in this latter case, the
4544 ;; permissions shortcut will fail to detect a lock asserted from another
4545 ;; directory).
4546 ;;
4547 ;; Note that these cases correspond exactly to the errors which could happen
4548 ;; because of a competing checkin/checkout race in between two instances of
4549 ;; vc-next-action.
4550 ;;
4551 ;; For VC's purposes, a workfile/master pair may have the following states:
4552 ;;
4553 ;; A. Unregistered. There is a workfile, there is no master.
4554 ;;
4555 ;; B. Registered and not locked by anyone.
4556 ;;
4557 ;; C. Locked by calling user and unchanged.
4558 ;;
4559 ;; D. Locked by the calling user and changed.
4560 ;;
4561 ;; E. Locked by someone other than the calling user.
4562 ;;
4563 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
4564 ;;
4565 ;; VC's idea of state
4566 ;; |
4567 ;; V Actual state RCS action SCCS action Effect
4568 ;; A B C D E
4569 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
4570 ;; B 5 . 6 7 8 co -l get -e checkout
4571 ;; C 9 10 . 11 12 co -u unget; get revert
4572 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
4573 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
4574 ;;
4575 ;; All commands take the master file name as a last argument (not shown).
4576 ;;
4577 ;; In the discussion below, a "self-race" is a pathological situation in
4578 ;; which VC operations are being attempted simultaneously by two or more
4579 ;; Emacsen running under the same username.
4580 ;;
4581 ;; The vc-next-action code has the following windows:
4582 ;;
4583 ;; Window P:
4584 ;; Between the check for existence of a master file and the call to
4585 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
4586 ;; never close if the initial-comment feature is on.
4587 ;;
4588 ;; Window Q:
4589 ;; Between the call to vc-workfile-unchanged-p in and the immediately
4590 ;; following revert (apparent state C).
4591 ;;
4592 ;; Window R:
4593 ;; Between the call to vc-workfile-unchanged-p in and the following
4594 ;; checkin (apparent state D). This window may never close.
4595 ;;
4596 ;; Window S:
4597 ;; Between the unlock and the immediately following checkout during a
4598 ;; revert operation (apparent state C). Included in window Q.
4599 ;;
4600 ;; Window T:
4601 ;; Between vc-state and the following checkout (apparent state B).
4602 ;;
4603 ;; Window U:
4604 ;; Between vc-state and the following revert (apparent state C).
4605 ;; Includes windows Q and S.
4606 ;;
4607 ;; Window V:
4608 ;; Between vc-state and the following checkin (apparent state
4609 ;; D). This window may never be closed if the user fails to complete the
4610 ;; checkin message. Includes window R.
4611 ;;
4612 ;; Window W:
4613 ;; Between vc-state and the following steal-lock (apparent
4614 ;; state E). This window may never close if the user fails to complete
4615 ;; the steal-lock message. Includes window X.
4616 ;;
4617 ;; Window X:
4618 ;; Between the unlock and the immediately following re-lock during a
4619 ;; steal-lock operation (apparent state E). This window may never close
4620 ;; if the user fails to complete the steal-lock message.
4621 ;;
4622 ;; Errors:
4623 ;;
4624 ;; Apparent state A ---
4625 ;;
4626 ;; 1. File looked unregistered but is actually registered and not locked.
4627 ;;
4628 ;; Potential cause: someone else's admin during window P, with
4629 ;; caller's admin happening before their checkout.
4630 ;;
4631 ;; RCS: Prior to version 5.6.4, ci fails with message
4632 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
4633 ;; ci -i option and the message is "<file>,v: already exists".
4634 ;; SCCS: admin will fail with error (ad19).
4635 ;;
4636 ;; We can let these errors be passed up to the user.
4637 ;;
4638 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
4639 ;;
4640 ;; Potential cause: self-race during window P.
4641 ;;
4642 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
4643 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
4644 ;; ci -i option, failing with message "<file>,v: already exists".
4645 ;; SCCS: will fail with error (ad19).
4646 ;;
4647 ;; Either of these consequences is acceptable.
4648 ;;
4649 ;; 3. File looked unregistered but is actually locked by caller, changed.
4650 ;;
4651 ;; Potential cause: self-race during window P.
4652 ;;
4653 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
4654 ;; a delta with a null change comment (the -t- switch will be
4655 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
4656 ;; failing with message "<file>,v: already exists".
4657 ;; SCCS: will fail with error (ad19).
4658 ;;
4659 ;; 4. File looked unregistered but is locked by someone else.
4660 ;;;
4661 ;; Potential cause: someone else's admin during window P, with
4662 ;; caller's admin happening *after* their checkout.
4663 ;;
4664 ;; RCS: Prior to version 5.6.4, ci fails with a
4665 ;; "no lock set by <user>" message. From 5.6.4 onwards,
4666 ;; VC uses the new ci -i option, failing with message
4667 ;; "<file>,v: already exists".
4668 ;; SCCS: will fail with error (ad19).
4669 ;;
4670 ;; We can let these errors be passed up to the user.
4671 ;;
4672 ;; Apparent state B ---
4673 ;;
4674 ;; 5. File looked registered and not locked, but is actually unregistered.
4675 ;;
4676 ;; Potential cause: master file got nuked during window P.
4677 ;;
4678 ;; RCS: will fail with "RCS/<file>: No such file or directory"
4679 ;; SCCS: will fail with error ut4.
4680 ;;
4681 ;; We can let these errors be passed up to the user.
4682 ;;
4683 ;; 6. File looked registered and not locked, but is actually locked by the
4684 ;; calling user and unchanged.
4685 ;;
4686 ;; Potential cause: self-race during window T.
4687 ;;
4688 ;; RCS: in the same directory as the previous workfile, co -l will fail
4689 ;; with "co error: writable foo exists; checkout aborted". In any other
4690 ;; directory, checkout will succeed.
4691 ;; SCCS: will fail with ge17.
4692 ;;
4693 ;; Either of these consequences is acceptable.
4694 ;;
4695 ;; 7. File looked registered and not locked, but is actually locked by the
4696 ;; calling user and changed.
4697 ;;
4698 ;; As case 6.
4699 ;;
4700 ;; 8. File looked registered and not locked, but is actually locked by another
4701 ;; user.
4702 ;;
4703 ;; Potential cause: someone else checks it out during window T.
4704 ;;
4705 ;; RCS: co error: revision 1.3 already locked by <user>
4706 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
4707 ;;
4708 ;; We can let these errors be passed up to the user.
4709 ;;
4710 ;; Apparent state C ---
4711 ;;
4712 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
4713 ;;
4714 ;; As case 5.
4715 ;;
4716 ;; 10. File looks locked by calling user and unchanged, but is actually not
4717 ;; locked.
4718 ;;
4719 ;; Potential cause: a self-race in window U, or by the revert's
4720 ;; landing during window X of some other user's steal-lock or window S
4721 ;; of another user's revert.
4722 ;;
4723 ;; RCS: succeeds, refreshing the file from the identical version in
4724 ;; the master.
4725 ;; SCCS: fails with error ut4 (p file nonexistent).
4726 ;;
4727 ;; Either of these consequences is acceptable.
4728 ;;
4729 ;; 11. File is locked by calling user. It looks unchanged, but is actually
4730 ;; changed.
4731 ;;
4732 ;; Potential cause: the file would have to be touched by a self-race
4733 ;; during window Q.
4734 ;;
4735 ;; The revert will succeed, removing whatever changes came with
4736 ;; the touch. It is theoretically possible that work could be lost.
4737 ;;
4738 ;; 12. File looks like it's locked by the calling user and unchanged, but
4739 ;; it's actually locked by someone else.
4740 ;;
4741 ;; Potential cause: a steal-lock in window V.
4742 ;;
4743 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
4744 ;; SCCS: fails with error un2
4745 ;;
4746 ;; We can pass these errors up to the user.
4747 ;;
4748 ;; Apparent state D ---
4749 ;;
4750 ;; 13. File looks like it's locked by the calling user and changed, but it's
4751 ;; actually unregistered.
4752 ;;
4753 ;; Potential cause: master file got nuked during window P.
4754 ;;
4755 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
4756 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
4757 ;; option, failing with message "no such file or directory".
4758 ;; SCCS: will fail with error ut4.
4759 ;;
4760 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
4761 ;; VC may fail to detect the loss of previous version information.
4762 ;;
4763 ;; 14. File looks like it's locked by the calling user and changed, but it's
4764 ;; actually unlocked.
4765 ;;
4766 ;; Potential cause: self-race in window V, or the checkin happening
4767 ;; during the window X of someone else's steal-lock or window S of
4768 ;; someone else's revert.
4769 ;;
4770 ;; RCS: ci will fail with "no lock set by <user>".
4771 ;; SCCS: delta will fail with error ut4.
4772 ;;
4773 ;; 15. File looks like it's locked by the calling user and changed, but it's
4774 ;; actually locked by the calling user and unchanged.
4775 ;;
4776 ;; Potential cause: another self-race --- a whole checkin/checkout
4777 ;; sequence by the calling user would have to land in window R.
4778 ;;
4779 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
4780 ;; RCS: reverts to the file state as of the second user's checkin, leaving
4781 ;; the file unlocked.
4782 ;;
4783 ;; It is theoretically possible that work could be lost under RCS.
4784 ;;
4785 ;; 16. File looks like it's locked by the calling user and changed, but it's
4786 ;; actually locked by a different user.
4787 ;;
4788 ;; RCS: ci error: no lock set by <user>
4789 ;; SCCS: unget will fail with error un2
4790 ;;
4791 ;; We can pass these errors up to the user.
4792 ;;
4793 ;; Apparent state E ---
4794 ;;
4795 ;; 17. File looks like it's locked by some other user, but it's actually
4796 ;; unregistered.
4797 ;;
4798 ;; As case 13.
4799 ;;
4800 ;; 18. File looks like it's locked by some other user, but it's actually
4801 ;; unlocked.
4802 ;;
4803 ;; Potential cause: someone released a lock during window W.
4804 ;;
4805 ;; RCS: The calling user will get the lock on the file.
4806 ;; SCCS: unget -n will fail with cm4.
4807 ;;
4808 ;; Either of these consequences will be OK.
4809 ;;
4810 ;; 19. File looks like it's locked by some other user, but it's actually
4811 ;; locked by the calling user and unchanged.
4812 ;;
4813 ;; Potential cause: the other user relinquishing a lock followed by
4814 ;; a self-race, both in window W.
4815 ;;
4816 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
4817 ;; the sequence a no-op.
4818 ;;
4819 ;; 20. File looks like it's locked by some other user, but it's actually
4820 ;; locked by the calling user and changed.
4821 ;;
4822 ;; As case 19.
4823 ;;
4824 ;; PROBLEM CASES:
4825 ;;
4826 ;; In order of decreasing severity:
4827 ;;
4828 ;; Cases 11 and 15 are the only ones that potentially lose work.
4829 ;; They would require a self-race for this to happen.
4830 ;;
4831 ;; Case 13 in RCS loses information about previous deltas, retaining
4832 ;; only the information in the current workfile. This can only happen
4833 ;; if the master file gets nuked in window P.
4834 ;;
4835 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
4836 ;; no change comment in the master. This would require a self-race in
4837 ;; window P or R respectively.
4838 ;;
4839 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
4840 ;;
4841 ;; Unfortunately, it appears to me that no recovery is possible in these
4842 ;; cases. They don't yield error messages, so there's no way to tell that
4843 ;; a race condition has occurred.
4844 ;;
4845 ;; All other cases don't change either the workfile or the master, and
4846 ;; trigger command errors which the user will see.
4847 ;;
4848 ;; Thus, there is no explicit recovery code.
4849
4850 ;; arch-tag: ca82c1de-3091-4e26-af92-460abc6213a6
4851 ;;; vc.el ends here