1 ;;; vc.el --- drive a version-control system from within Emacs
3 ;; Copyright (C) 1992,93,94,95,96,97,98,2000 Free Software Foundation, Inc.
5 ;; Author: FSF (see below for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; VC was initially designed and implemented by Eric S. Raymond
28 ;; <esr@snark.thyrsus.com>. Over the years, many people have
29 ;; contributed substantial amounts of work to VC. These include:
30 ;; Per Cederqvist <ceder@lysator.liu.se>
31 ;; Paul Eggert <eggert@twinsun.com>
32 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
33 ;; Martin Lorentzson <martinl@gnu.org>
34 ;; Dave Love <d.love@gnu.org>
35 ;; Stefan Monnier <monnier@cs.yale.edu>
36 ;; Andre Spiegel <spiegel@gnu.org>
37 ;; Richard Stallman <rms@gnu.org>
42 ;; This mode is fully documented in the Emacs user's manual.
44 ;; Supported version-control systems presently include SCCS, RCS, and CVS.
46 ;; Some features will not work with old RCS versions. Where
47 ;; appropriate, VC finds out which version you have, and allows or
48 ;; disallows those features (stealing locks, for example, works only
49 ;; from 5.6.2 onwards).
50 ;; Even initial checkins will fail if your RCS version is so old that ci
51 ;; doesn't understand -t-; this has been known to happen to people running
54 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
56 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
57 ;; to be installed somewhere on Emacs's path for executables.
59 ;; If your site uses the ChangeLog convention supported by Emacs, the
60 ;; function vc-comment-to-change-log should prove a useful checkin hook.
62 ;; The vc code maintains some internal state in order to reduce expensive
63 ;; version-control operations to a minimum. Some names are only computed
64 ;; once. If you perform version control operations with RCS/SCCS/CVS while
65 ;; vc's back is turned, or move/rename master files while vc is running,
66 ;; vc may get seriously confused. Don't do these things!
68 ;; Developer's notes on some concurrency issues are included at the end of
73 ;;;;;;;;;;;;;;;;; Backend-specific functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75 ;; for each operation FUN, the backend should provide a function vc-BACKEND-FUN.
76 ;; Operations marked with a `-' instead of a `*' are optional.
78 ;; * registered (file)
80 ;; - state-heuristic (file)
81 ;; The default behavior delegates to `state'.
83 ;; * checkout-model (file)
84 ;; - mode-line-string (file)
85 ;; * workfile-version (file)
87 ;; - merge-news (file)
88 ;; Only needed if state `needs-merge' is possible.
89 ;; - merge (file rev1 rev2)
90 ;; - steal-lock (file &optional version)
91 ;; Only required if files can be locked by somebody else.
92 ;; * register (file rev comment)
93 ;; * unregister (file backend)
94 ;; - receive-file (file move)
95 ;; - responsible-p (file)
96 ;; Should also work if FILE is a directory (ends with a slash).
97 ;; - could-register (file)
98 ;; * checkout (file writable &optional rev destfile)
99 ;; Checkout revision REV of FILE into DESTFILE.
100 ;; DESTFILE defaults to FILE.
101 ;; The file should be made writable if WRITABLE is non-nil.
102 ;; REV can be nil (BASE) or "" (HEAD) or any other revision.
103 ;; * checkin (file rev comment)
104 ;; - logentry-check ()
105 ;; * diff (file &optional rev1 rev2)
106 ;; Insert the diff for FILE into the current buffer.
107 ;; REV1 should default to workfile-version.
108 ;; REV2 should default to the current workfile
109 ;; Return a status of either 0 (i.e. no diff) or 1 (i.e. either non-empty
110 ;; diff or the diff is run asynchronously).
111 ;; - workfile-unchanged-p (file)
112 ;; Return non-nil if FILE is unchanged from its current workfile version.
113 ;; This function should do a brief comparison of FILE's contents
114 ;; with those of the master version. If the backend does not have
115 ;; such a brief-comparison feature, the default implementation of this
116 ;; function can be used, which delegates to a full vc-BACKEND-diff.
117 ;; - clear-headers ()
118 ;; * check-headers ()
119 ;; - dired-state-info (file)
120 ;; - create-snapshot (dir name branchp)
121 ;; Take a snapshot of the current state of files under DIR and name it NAME.
122 ;; This should make sure that files are up-to-date before proceeding
124 ;; DIR can also be a file and if BRANCHP is specified, NAME
125 ;; should be created as a branch and DIR should be checked out under
126 ;; this new branch. The default behavior does not support branches
127 ;; but does a sanity check, a tree traversal and for each file calls
129 ;; * assign-name (file name)
130 ;; Give name NAME to the current version of FILE, assuming it is
131 ;; up-to-date. Only used by the default version of `create-snapshot'.
132 ;; - retrieve-snapshot (dir name update)
133 ;; Retrieve a named snapshot of all registered files at or below DIR.
134 ;; If UPDATE is non-nil, then update buffers of any files in the snapshot
135 ;; that are currently visited.
136 ;; * print-log (file)
137 ;; Insert the revision log of FILE into the current buffer.
138 ;; - show-log-entry (version)
139 ;; - comment-history (file)
140 ;; - update-changelog (files)
141 ;; Find changelog entries for FILES, or for all files at or below
142 ;; the default-directory if FILES is nil.
143 ;; * latest-on-branch-p (file)
144 ;; - cancel-version (file writable)
145 ;; - rename-file (old new)
146 ;; - annotate-command (file buf)
147 ;; - annotate-difference (pos)
148 ;; Only required if `annotate-command' is defined for the backend.
154 (require 'dired
) ; for dired-map-over-marks macro
155 (require 'dired-aux
)) ; for dired-kill-{line,tree}
157 (if (not (assoc 'vc-parent-buffer minor-mode-alist
))
158 (setq minor-mode-alist
159 (cons '(vc-parent-buffer vc-parent-buffer-name
)
162 ;; General customization
165 "Version-control system in Emacs."
168 (defcustom vc-suppress-confirm nil
169 "*If non-nil, treat user as expert; suppress yes-no prompts on some things."
173 (defcustom vc-delete-logbuf-window t
174 "*If non-nil, delete the *VC-log* buffer and window after each logical action.
175 If nil, bury that buffer instead.
176 This is most useful if you have multiple windows on a frame and would like to
177 preserve the setting."
181 (defcustom vc-initial-comment nil
182 "*If non-nil, prompt for initial comment when a file is registered."
186 (defcustom vc-default-init-version
"1.1"
187 "*A string used as the default version number when a new file is registered.
188 This can be overridden by giving a prefix argument to \\[vc-register]."
193 (defcustom vc-command-messages nil
194 "*If non-nil, display run messages from back-end commands."
198 (defcustom vc-checkin-switches nil
199 "*A string or list of strings specifying extra switches for checkin.
200 These are passed to the checkin program by \\[vc-checkin]."
201 :type
'(choice (const :tag
"None" nil
)
202 (string :tag
"Argument String")
203 (repeat :tag
"Argument List"
208 (defcustom vc-checkout-switches nil
209 "*A string or list of strings specifying extra switches for checkout.
210 These are passed to the checkout program by \\[vc-checkout]."
211 :type
'(choice (const :tag
"None" nil
)
212 (string :tag
"Argument String")
213 (repeat :tag
"Argument List"
218 (defcustom vc-register-switches nil
219 "*A string or list of strings; extra switches for registering a file.
220 These are passed to the checkin program by \\[vc-register]."
221 :type
'(choice (const :tag
"None" nil
)
222 (string :tag
"Argument String")
223 (repeat :tag
"Argument List"
228 (defcustom vc-dired-listing-switches
"-al"
229 "*Switches passed to `ls' for vc-dired. MUST contain the `l' option."
234 (defcustom vc-dired-recurse t
235 "*If non-nil, show directory trees recursively in VC Dired."
240 (defcustom vc-dired-terse-display t
241 "*If non-nil, show only locked files in VC Dired."
246 (defcustom vc-directory-exclusion-list
'("SCCS" "RCS" "CVS")
247 "*List of directory names to be ignored while recursively walking file trees."
248 :type
'(repeat string
)
251 (defconst vc-maximum-comment-ring-size
32
252 "Maximum number of saved comments in the comment ring.")
254 ;;; This is duplicated in diff.el.
255 (defvar diff-switches
"-c"
256 "*A string or list of strings specifying switches to be passed to diff.")
259 (defcustom vc-checkin-hook nil
260 "*Normal hook (list of functions) run after a checkin is done.
263 :options
'(vc-comment-to-change-log)
267 (defcustom vc-before-checkin-hook nil
268 "*Normal hook (list of functions) run before a file gets checked in.
273 (defcustom vc-logentry-check-hook nil
274 "*Normal hook run by `vc-backend-logentry-check'.
275 Use this to impose your own rules on the entry in addition to any the
276 version control backend imposes itself."
280 ;; Annotate customization
281 (defcustom vc-annotate-color-map
282 '(( 26.3672 .
"#FF0000")
283 ( 52.7344 .
"#FF3800")
284 ( 79.1016 .
"#FF7000")
285 (105.4688 .
"#FFA800")
286 (131.8359 .
"#FFE000")
287 (158.2031 .
"#E7FF00")
288 (184.5703 .
"#AFFF00")
289 (210.9375 .
"#77FF00")
290 (237.3047 .
"#3FFF00")
291 (263.6719 .
"#07FF00")
292 (290.0391 .
"#00FF31")
293 (316.4063 .
"#00FF69")
294 (342.7734 .
"#00FFA1")
295 (369.1406 .
"#00FFD9")
296 (395.5078 .
"#00EEFF")
297 (421.8750 .
"#00B6FF")
298 (448.2422 .
"#007EFF"))
299 "*Association list of age versus color, for \\[vc-annotate].
300 Ages are given in units of 2**-16 seconds.
301 Default is eighteen steps using a twenty day increment."
305 (defcustom vc-annotate-very-old-color
"#0046FF"
306 "*Color for lines older than CAR of last cons in `vc-annotate-color-map'."
310 (defcustom vc-annotate-background
"black"
311 "*Background color for \\[vc-annotate].
312 Default color is used if nil."
316 (defcustom vc-annotate-menu-elements
'(2 0.5 0.1 0.01)
317 "*Menu elements for the mode-specific menu of VC-Annotate mode.
318 List of factors, used to expand/compress the time scale. See `vc-annotate'."
319 :type
'(repeat number
)
322 ;; vc-annotate functionality (CVS only).
323 (defvar vc-annotate-mode nil
324 "Variable indicating if VC-Annotate mode is active.")
326 (defvar vc-annotate-mode-map
327 (let ((m (make-sparse-keymap)))
328 (define-key m
[menu-bar
] (make-sparse-keymap "VC-Annotate"))
330 "Local keymap used for VC-Annotate mode.")
332 (defvar vc-annotate-mode-menu nil
333 "Local keymap used for VC-Annotate mode's menu bar menu.")
335 ;; Header-insertion hair
337 (defcustom vc-static-header-alist
339 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
340 "*Associate static header string templates with file types.
341 A \%s in the template is replaced with the first string associated with
342 the file's version control type in `vc-header-alist'."
343 :type
'(repeat (cons :format
"%v"
344 (regexp :tag
"File Type")
345 (string :tag
"Header String")))
348 (defcustom vc-comment-alist
349 '((nroff-mode ".\\\"" ""))
350 "*Special comment delimiters to be used in generating vc headers only.
351 Add an entry in this list if you need to override the normal `comment-start'
352 and `comment-end' variables. This will only be necessary if the mode language
353 is sensitive to blank lines."
354 :type
'(repeat (list :format
"%v"
356 (string :tag
"Comment Start")
357 (string :tag
"Comment End")))
360 ;; Default is to be extra careful for super-user.
361 ;; TODO: This variable is no longer used; the corresponding checks
362 ;; are always done now. If that turns out to be fast enough,
363 ;; the variable can be obsoleted.
364 (defcustom vc-checkout-carefully
(= (user-uid) 0)
365 "*Non-nil means be extra-careful in checkout.
366 Verify that the file really is not locked
367 and that its contents match what the master file says."
374 (defvar vc-prefix-map
375 (let ((map (make-sparse-keymap)))
376 (define-key map
"a" 'vc-update-change-log
)
377 (define-key map
"b" 'vc-switch-backend
)
378 (define-key map
"c" 'vc-cancel-version
)
379 (define-key map
"d" 'vc-directory
)
380 (define-key map
"g" 'vc-annotate
)
381 (define-key map
"h" 'vc-insert-headers
)
382 (define-key map
"i" 'vc-register
)
383 (define-key map
"l" 'vc-print-log
)
384 (define-key map
"m" 'vc-merge
)
385 (define-key map
"r" 'vc-retrieve-snapshot
)
386 (define-key map
"s" 'vc-create-snapshot
)
387 (define-key map
"u" 'vc-revert-buffer
)
388 (define-key map
"v" 'vc-next-action
)
389 (define-key map
"=" 'vc-diff
)
390 (define-key map
"~" 'vc-version-other-window
)
392 (fset 'vc-prefix-map vc-prefix-map
)
394 ;; Initialization code, to be done just once at load-time
395 (defvar vc-log-mode-map
396 (let ((map (make-sparse-keymap)))
397 (define-key map
"\M-n" 'vc-next-comment
)
398 (define-key map
"\M-p" 'vc-previous-comment
)
399 (define-key map
"\M-r" 'vc-comment-search-reverse
)
400 (define-key map
"\M-s" 'vc-comment-search-forward
)
401 (define-key map
"\C-c\C-c" 'vc-finish-logentry
)
403 ;; Compatibility with old name. Should we bother ?
404 (defvar vc-log-entry-mode vc-log-mode-map
)
407 ;; Variables the user doesn't need to know about.
408 (defvar vc-log-operation nil
)
409 (defvar vc-log-after-operation-hook nil
)
410 (defvar vc-annotate-buffers nil
411 "Alist of current \"Annotate\" buffers and their corresponding backends.
412 The keys are \(BUFFER . BACKEND\). See also `vc-annotate-get-backend'.")
413 ;; In a log entry buffer, this is a local variable
414 ;; that points to the buffer for which it was made
415 ;; (either a file, or a VC dired buffer).
416 (defvar vc-parent-buffer nil
)
417 (put 'vc-parent-buffer
'permanent-local t
)
418 (defvar vc-parent-buffer-name nil
)
419 (put 'vc-parent-buffer-name
'permanent-local t
)
422 (defvar vc-log-version
)
424 (defvar vc-dired-mode nil
)
425 (make-variable-buffer-local 'vc-dired-mode
)
427 (defvar vc-comment-ring
(make-ring vc-maximum-comment-ring-size
))
428 (defvar vc-comment-ring-index nil
)
429 (defvar vc-last-comment-match
"")
431 ;;; functions that operate on RCS revision numbers. This code should
432 ;;; also be moved into the backends. It stays for now, however, since
433 ;;; it is used in code below.
434 (defun vc-trunk-p (rev)
435 "Return t if REV is a revision on the trunk."
436 (not (eq nil
(string-match "\\`[0-9]+\\.[0-9]+\\'" rev
))))
438 (defun vc-branch-p (rev)
439 "Return t if REV is a branch revision."
440 (not (eq nil
(string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev
))))
442 (defun vc-branch-part (rev)
443 "Return the branch part of a revision number REV."
444 (substring rev
0 (string-match "\\.[0-9]+\\'" rev
)))
446 (defun vc-minor-part (rev)
447 "Return the minor version number of a revision number REV."
448 (string-match "[0-9]+\\'" rev
)
449 (substring rev
(match-beginning 0) (match-end 0)))
451 (defun vc-previous-version (rev)
452 "Guess the version number immediately preceding REV."
453 (let ((branch (vc-branch-part rev
))
454 (minor-num (string-to-number (vc-minor-part rev
))))
456 ;; version does probably not start a branch or release
457 (concat branch
"." (number-to-string (1- minor-num
)))
459 ;; we are at the beginning of the trunk --
460 ;; don't know anything to return here
462 ;; we are at the beginning of a branch --
463 ;; return version of starting point
464 (vc-branch-part branch
)))))
466 ;; File property caching
468 (defun vc-clear-context ()
469 "Clear all cached file properties and the comment ring."
471 (fillarray vc-file-prop-obarray nil
)
472 ;; Note: there is potential for minor lossage here if there is an open
473 ;; log buffer with a nonzero local value of vc-comment-ring-index.
474 (setq vc-comment-ring
(make-ring vc-maximum-comment-ring-size
)))
476 (defmacro with-vc-properties
(file form settings
)
477 "Execute FORM, then set per-file properties for FILE, but only those
478 that have not been set during the execution of FORM. SETTINGS is a list
479 of two-element lists, each of which has the form (PROPERTY VALUE)."
480 `(let ((vc-touched-properties (list t
))
483 (mapcar (lambda (setting)
484 (let ((property (nth 0 setting
))
485 (value (nth 1 setting
)))
486 (unless (memq property vc-touched-properties
)
487 (put (intern filename vc-file-prop-obarray
)
491 ;; Random helper functions
493 (defsubst vc-editable-p
(file)
494 (or (eq (vc-checkout-model file
) 'implicit
)
495 (eq (vc-state file
) 'edited
)
496 (eq (vc-state file
) 'needs-merge
)))
498 ;;; Two macros for elisp programming
500 (defmacro with-vc-file
(file comment
&rest body
)
501 "Check out a writable copy of FILE if necessary and execute the body.
502 Check in FILE with COMMENT (a string) after BODY has been executed.
503 FILE is passed through `expand-file-name'; BODY executed within
504 `save-excursion'. If FILE is not under version control, or locked by
505 somebody else, signal error."
506 `(let ((file (expand-file-name ,file
)))
507 (or (vc-registered file
)
508 (error (format "File not under version control: `%s'" file
)))
509 (unless (vc-editable-p file
)
510 (let ((state (vc-state file
)))
511 (if (stringp state
) (error (format "`%s' is locking `%s'" state file
))
512 (vc-checkout file t
))))
515 (vc-checkin file nil
,comment
)))
518 (defmacro edit-vc-file
(file comment
&rest body
)
519 "Edit FILE under version control, executing body.
520 Checkin with COMMENT after executing BODY.
521 This macro uses `with-vc-file', passing args to it.
522 However, before executing BODY, find FILE, and after BODY, save buffer."
529 (defun vc-ensure-vc-buffer ()
530 "Make sure that the current buffer visits a version-controlled file."
532 (set-buffer (find-file-noselect (dired-get-filename)))
533 (while vc-parent-buffer
534 (pop-to-buffer vc-parent-buffer
))
535 (if (not (buffer-file-name))
536 (error "Buffer %s is not associated with a file" (buffer-name))
537 (if (not (vc-backend (buffer-file-name)))
538 (error "File %s is not under version control" (buffer-file-name))))))
540 (defvar vc-binary-assoc nil
)
541 (defvar vc-binary-suffixes
542 (if (memq system-type
'(ms-dos windows-nt
))
543 '(".exe" ".com" ".bat" ".cmd" ".btm" "")
546 (defun vc-process-filter (p s
)
547 "An alternative output filter for async process P.
548 The only difference with the default filter is to insert S after markers."
549 (with-current-buffer (process-buffer p
)
551 (let ((inhibit-read-only t
))
552 (goto-char (process-mark p
))
554 (set-marker (process-mark p
) (point))))))
556 (defun vc-setup-buffer (&optional buf
)
557 "Prepare BUF for executing a VC command and make it the current buffer.
558 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
559 (unless buf
(setq buf
"*vc*"))
560 (let ((camefrom (current-buffer))
561 (olddir default-directory
))
562 (set-buffer (get-buffer-create buf
))
563 (kill-all-local-variables)
564 (set (make-local-variable 'vc-parent-buffer
) camefrom
)
565 (set (make-local-variable 'vc-parent-buffer-name
)
566 (concat " from " (buffer-name camefrom
)))
567 (setq default-directory olddir
)
568 (let ((inhibit-read-only t
))
571 (defun vc-exec-after (code)
572 "Eval CODE when the current buffer's process is done.
573 If the current buffer has no process, just evaluate CODE.
574 Else, add CODE to the process' sentinel."
575 (let ((proc (get-buffer-process (current-buffer))))
577 ;; If there's no background process, just execute the code.
578 ((null proc
) (eval code
))
579 ;; If the background process has exited, reap it and try again
580 ((eq (process-status proc
) 'exit
)
581 (delete-process proc
)
582 (vc-exec-after code
))
583 ;; If a process is running, add CODE to the sentinel
584 ((eq (process-status proc
) 'run
)
585 (let ((sentinel (process-sentinel proc
)))
586 (set-process-sentinel proc
588 (with-current-buffer ',(current-buffer)
589 (goto-char (process-mark p
))
590 ,@(append (cdr (cdr (cdr ;strip off `with-current-buffer buf
592 (car (cdr (cdr ;strip off `lambda (p s)'
594 (list `(vc-exec-after ',code
))))))))
595 (t (error "Unexpected process state"))))
598 (defvar vc-post-command-functions nil
599 "Hook run at the end of `vc-do-command'.
600 Each function is called inside the buffer in which the command was run
601 and is passed 3 argument: the COMMAND, the FILE and the FLAGS.")
603 (defun vc-do-command (buffer okstatus command file
&rest flags
)
604 "Execute a version control command, notifying user and checking for errors.
605 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the current
606 buffer (which is assumed to be properly setup) if BUFFER is t. The
607 command is considered successful if its exit status does not exceed
608 OKSTATUS (if OKSTATUS is nil, that means to ignore errors, if it is 'async,
609 that means not to wait for termination of the subprocess). FILE is
610 the name of the working file (may also be nil, to execute commands
611 that don't expect a file name). If an optional list of FLAGS is present,
612 that is inserted into the command line before the filename."
613 (and file
(setq file
(expand-file-name file
)))
614 (if vc-command-messages
615 (message "Running %s on %s..." command file
))
617 (unless (eq buffer t
) (vc-setup-buffer buffer
))
619 (inhibit-read-only t
)
621 (setq squeezed
(delq nil
(copy-sequence flags
)))
623 ;; FIXME: file-relative-name can return a bogus result because
624 ;; it doesn't look at the actual file-system to see if symlinks
626 (setq squeezed
(append squeezed
(list (file-relative-name file
)))))
627 (let ((exec-path (append vc-path exec-path
))
628 ;; Add vc-path to PATH for the execution of this command.
630 (cons (concat "PATH=" (getenv "PATH")
632 (mapconcat 'identity vc-path path-separator
))
633 process-environment
))
634 (w32-quote-process-args t
))
635 (if (eq okstatus
'async
)
636 (let ((proc (apply 'start-process command
(current-buffer) command
638 (message "Running %s in the background..." command
)
639 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
640 (set-process-filter proc
'vc-process-filter
)
642 `(message "Running %s in the background... done" ',command
)))
643 (setq status
(apply 'call-process command nil t nil squeezed
))
644 (when (or (not (integerp status
)) (and okstatus
(< okstatus status
)))
645 (pop-to-buffer (current-buffer))
646 (goto-char (point-min))
647 (shrink-window-if-larger-than-buffer)
648 (error "Running %s...FAILED (%s)" command
649 (if (integerp status
) (format "status %d" status
) status
))))
650 (if vc-command-messages
651 (message "Running %s...OK" command
)))
653 `(run-hook-with-args 'vc-post-command-functions
',command
',file
',flags
))
656 (defun vc-position-context (posn)
657 "Save a bit of the text around POSN in the current buffer.
658 Used to help us find the corresponding position again later
659 if markers are destroyed or corrupted."
660 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
664 (buffer-substring posn
665 (min (point-max) (+ posn
100)))))
667 (defun vc-find-position-by-context (context)
668 "Return the position of CONTEXT in the current buffer, or nil if not found."
669 (let ((context-string (nth 2 context
)))
670 (if (equal "" context-string
)
673 (let ((diff (- (nth 1 context
) (buffer-size))))
674 (if (< diff
0) (setq diff
(- diff
)))
675 (goto-char (nth 0 context
))
676 (if (or (search-forward context-string nil t
)
677 ;; Can't use search-backward since the match may continue
679 (progn (goto-char (- (point) diff
(length context-string
)))
680 ;; goto-char doesn't signal an error at
681 ;; beginning of buffer like backward-char would
682 (search-forward context-string nil t
)))
683 ;; to beginning of OSTRING
684 (- (point) (length context-string
))))))))
686 (defun vc-context-matches-p (posn context
)
687 "Return t if POSN matches CONTEXT, nil otherwise."
688 (let* ((context-string (nth 2 context
))
689 (len (length context-string
))
691 (if (> end
(1+ (buffer-size)))
693 (string= context-string
(buffer-substring posn end
)))))
695 (defun vc-buffer-context ()
696 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
697 Used by `vc-restore-buffer-context' to later restore the context."
698 (let ((point-context (vc-position-context (point)))
699 ;; Use mark-marker to avoid confusion in transient-mark-mode.
700 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
701 (vc-position-context (mark-marker))))
702 ;; Make the right thing happen in transient-mark-mode.
704 ;; We may want to reparse the compilation buffer after revert
705 (reparse (and (boundp 'compilation-error-list
) ;compile loaded
706 (let ((curbuf (current-buffer)))
707 ;; Construct a list; each elt is nil or a buffer
708 ;; iff that buffer is a compilation output buffer
709 ;; that contains markers into the current buffer.
711 (mapcar (lambda (buffer)
714 compilation-old-error-list
715 compilation-error-list
))
716 (buffer-error-marked-p nil
))
717 (while (and (consp errors
)
718 (not buffer-error-marked-p
))
719 (and (markerp (cdr (car errors
)))
723 (setq buffer-error-marked-p t
))
724 (setq errors
(cdr errors
)))
725 (if buffer-error-marked-p buffer
)))
727 (list point-context mark-context reparse
)))
729 (defun vc-restore-buffer-context (context)
730 "Restore point/mark, and reparse any affected compilation buffers.
731 CONTEXT is that which `vc-buffer-context' returns."
732 (let ((point-context (nth 0 context
))
733 (mark-context (nth 1 context
))
734 (reparse (nth 2 context
)))
735 ;; Reparse affected compilation buffers.
738 (with-current-buffer (car reparse
)
739 (let ((compilation-last-buffer (current-buffer)) ;select buffer
740 ;; Record the position in the compilation buffer of
741 ;; the last error next-error went to.
742 (error-pos (marker-position
743 (car (car-safe compilation-error-list
)))))
744 ;; Reparse the error messages as far as they were parsed before.
745 (compile-reinitialize-errors '(4) compilation-parsing-end
)
746 ;; Move the pointer up to find the error we were at before
747 ;; reparsing. Now next-error should properly go to the next one.
748 (while (and compilation-error-list
749 (/= error-pos
(car (car compilation-error-list
))))
750 (setq compilation-error-list
(cdr compilation-error-list
))))))
751 (setq reparse
(cdr reparse
)))
753 ;; if necessary, restore point and mark
754 (if (not (vc-context-matches-p (point) point-context
))
755 (let ((new-point (vc-find-position-by-context point-context
)))
756 (if new-point
(goto-char new-point
))))
759 (not (vc-context-matches-p (mark) mark-context
))
760 (let ((new-mark (vc-find-position-by-context mark-context
)))
761 (if new-mark
(set-mark new-mark
))))))
763 (defun vc-revert-buffer1 (&optional arg no-confirm
)
764 "Revert buffer, trying to keep point and mark where user expects them.
765 Tries to be clever in the face of changes due to expanded version control
766 key words. This is important for typeahead to work as expected.
767 ARG and NO-CONFIRM are passed on to `revert-buffer'."
770 (let ((context (vc-buffer-context)))
771 ;; Use save-excursion here, because it may be able to restore point
772 ;; and mark properly even in cases where vc-restore-buffer-context
773 ;; would fail. However, save-excursion might also get it wrong --
774 ;; in this case, vc-restore-buffer-context gives it a second try.
776 ;; t means don't call normal-mode;
777 ;; that's to preserve various minor modes.
778 (revert-buffer arg no-confirm t
))
779 (vc-restore-buffer-context context
)))
782 (defun vc-buffer-sync (&optional not-urgent
)
783 "Make sure the current buffer and its working file are in sync.
784 NOT-URGENT means it is ok to continue if the user says not to save."
785 (if (buffer-modified-p)
786 (if (or vc-suppress-confirm
787 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
790 (error "Aborted")))))
792 (defun vc-workfile-unchanged-p (file)
793 "Has FILE changed since last checkout?"
794 (let ((checkout-time (vc-file-getprop file
'vc-checkout-time
))
795 (lastmod (nth 5 (file-attributes file
))))
797 (equal checkout-time lastmod
)
798 (let ((unchanged (vc-call workfile-unchanged-p file
)))
799 (vc-file-setprop file
'vc-checkout-time
(if unchanged lastmod
0))
802 (defun vc-default-workfile-unchanged-p (file)
803 "Default check whether FILE is unchanged: diff against master version."
804 (zerop (vc-call diff file
(vc-workfile-version file
))))
806 (defun vc-recompute-state (file)
807 "Force a recomputation of the version control state of FILE.
808 The state is computed using the exact, and possibly expensive
809 function `vc-BACKEND-state', not the heuristic."
810 (vc-file-setprop file
'vc-state
(vc-call state file
)))
812 (defun vc-next-action-on-file (file verbose
&optional comment
)
813 "Do The Right Thing for a given version-controlled FILE.
814 If COMMENT is specified, it will be used as an admin or checkin comment.
815 If VERBOSE is non-nil, query the user rather than using default parameters."
816 (let ((visited (get-file-buffer file
))
819 ;; Check relation of buffer and file, and make sure
820 ;; user knows what he's doing. First, finding the file
821 ;; will check whether the file on disk is newer.
823 (find-file-other-window file
)
825 (if (not (verify-visited-file-modtime (current-buffer)))
826 (if (yes-or-no-p "Replace file on disk with buffer contents? ")
827 (write-file (buffer-file-name))
829 ;; Now, check if we have unsaved changes.
831 (if (buffer-modified-p)
832 (or (y-or-n-p "Operate on disk file, keeping modified buffer? ")
833 (error "Aborted")))))
835 ;; Do the right thing
836 (if (not (vc-registered file
))
837 (vc-register verbose comment
)
838 (vc-recompute-state file
)
840 (setq state
(vc-state file
))
843 ((or (eq state
'up-to-date
)
844 (and verbose
(eq state
'needs-patch
)))
847 ;; go to a different version
849 (read-string "Branch, version, or backend to move to: "))
850 (let ((vsym (intern (upcase version
))))
851 (if (member vsym vc-handled-backends
)
852 (vc-transfer-file file vsym
)
853 (vc-checkout file
(eq (vc-checkout-model file
) 'implicit
)
855 ((not (eq (vc-checkout-model file
) 'implicit
))
856 ;; check the file out
857 (vc-checkout file t
))
860 (message "%s is up-to-date" file
))))
862 ;; Abnormal: edited but read-only
863 ((and visited
(eq state
'edited
) buffer-read-only
)
864 ;; Make the file+buffer read-write. If the user really wanted to
865 ;; commit, he'll get a chance to do that next time around, anyway.
866 (message "File is edited but read-only; making it writable")
867 (set-file-modes buffer-file-name
868 (logior (file-modes buffer-file-name
) 128))
869 (toggle-read-only -
1))
874 ;; For files with locking, if the file does not contain
875 ;; any changes, just let go of the lock, i.e. revert.
876 ((and (not (eq (vc-checkout-model file
) 'implicit
))
877 (vc-workfile-unchanged-p file
)
878 ;; If buffer is modified, that means the user just
879 ;; said no to saving it; in that case, don't revert,
880 ;; because the user might intend to save after
881 ;; finishing the log entry.
882 (not (and visited
(buffer-modified-p))))
883 ;; DO NOT revert the file without asking the user!
884 (if (not visited
) (find-file-other-window file
))
885 (if (yes-or-no-p "Revert to master version? ")
889 (vc-checkin file nil comment
)
890 (setq version
(read-string "New version or backend: "))
891 (let ((vsym (intern (upcase version
))))
892 (if (member vsym vc-handled-backends
)
893 (vc-transfer-file file vsym
)
894 (vc-checkin file version comment
)))))))
896 ;; locked by somebody else
899 (error "Sorry, you can't steal the lock on %s this way"
900 (file-name-nondirectory file
)))
902 (if verbose
(read-string "Version to steal: ")
903 (vc-workfile-version file
))
907 ((eq state
'needs-patch
)
908 (if (yes-or-no-p (format
909 "%s is not up-to-date. Get latest version? "
910 (file-name-nondirectory file
)))
911 (vc-checkout file
(eq (vc-checkout-model file
) 'implicit
) "")
912 (if (and (not (eq (vc-checkout-model file
) 'implicit
))
913 (yes-or-no-p "Lock this version? "))
918 ((eq state
'needs-merge
)
919 (if (yes-or-no-p (format
920 "%s is not up-to-date. Merge in changes now? "
921 (file-name-nondirectory file
)))
922 (vc-maybe-resolve-conflicts file
(vc-call merge-news file
))
926 ((eq state
'unlocked-changes
)
927 (if (not visited
) (find-file-other-window file
))
928 (if (save-window-excursion
929 (vc-version-diff file
(vc-workfile-version file
) nil
)
930 (goto-char (point-min))
931 (insert-string (format "Changes to %s since last lock:\n\n"
934 (yes-or-no-p (concat "File has unlocked changes. "
935 "Claim lock retaining changes? ")))
936 (progn (vc-call steal-lock file
)
937 ;; Must clear any headers here because they wouldn't
938 ;; show that the file is locked now.
939 (vc-clear-headers file
)
941 (if (not (yes-or-no-p
942 "Revert to checked-in version, instead? "))
943 (error "Checkout aborted")
944 (vc-revert-buffer1 t t
)
945 (vc-checkout file t
))))))))
947 (defvar vc-dired-window-configuration
)
949 (defun vc-next-action-dired (file rev comment
)
950 "Call `vc-next-action-on-file' on all the marked files.
951 Ignores FILE and REV, but passes on COMMENT."
952 (let ((dired-buffer (current-buffer))
953 (dired-dir default-directory
))
954 (dired-map-over-marks
955 (let ((file (dired-get-filename)))
956 (message "Processing %s..." file
)
957 (vc-next-action-on-file file nil comment
)
958 (set-buffer dired-buffer
)
959 (set-window-configuration vc-dired-window-configuration
)
960 (message "Processing %s...done" file
))
962 (dired-move-to-filename))
964 ;; Here's the major entry point.
967 (defun vc-next-action (verbose)
968 "Do the next logical checkin or checkout operation on the current file.
970 If you call this from within a VC dired buffer with no files marked,
971 it will operate on the file in the current line.
973 If you call this from within a VC dired buffer, and one or more
974 files are marked, it will accept a log message and then operate on
975 each one. The log message will be used as a comment for any register
976 or checkin operations, but ignored when doing checkouts. Attempted
977 lock steals will raise an error.
979 A prefix argument lets you specify the version number to use.
981 For RCS and SCCS files:
982 If the file is not already registered, this registers it for version
984 If the file is registered and not locked by anyone, this checks out
985 a writable and locked file ready for editing.
986 If the file is checked out and locked by the calling user, this
987 first checks to see if the file has changed since checkout. If not,
988 it performs a revert.
989 If the file has been changed, this pops up a buffer for entry
990 of a log message; when the message has been entered, it checks in the
991 resulting changes along with the log message as change commentary. If
992 the variable `vc-keep-workfiles' is non-nil (which is its default), a
993 read-only copy of the changed file is left in place afterwards.
994 If the file is registered and locked by someone else, you are given
995 the option to steal the lock.
998 If the file is not already registered, this registers it for version
999 control. This does a \"cvs add\", but no \"cvs commit\".
1000 If the file is added but not committed, it is committed.
1001 If your working file is changed, but the repository file is
1002 unchanged, this pops up a buffer for entry of a log message; when the
1003 message has been entered, it checks in the resulting changes along
1004 with the logmessage as change commentary. A writable file is retained.
1005 If the repository file is changed, you are asked if you want to
1006 merge in the changes into your working copy."
1011 (let ((files (dired-get-marked-files)))
1012 (set (make-local-variable 'vc-dired-window-configuration
)
1013 (current-window-configuration))
1017 (if (not (vc-up-to-date-p f
)) "@" ""))
1019 (vc-next-action-dired nil nil
"dummy")
1020 (vc-start-entry nil nil nil
1021 "Enter a change comment for the marked files."
1022 'vc-next-action-dired
))
1024 (while vc-parent-buffer
1025 (pop-to-buffer vc-parent-buffer
))
1026 (if buffer-file-name
1027 (vc-next-action-on-file buffer-file-name verbose
)
1028 (error "Buffer %s is not associated with a file" (buffer-name)))))
1030 ;;; These functions help the vc-next-action entry point
1033 (defun vc-register (&optional set-version comment
)
1034 "Register the current file into a version control system.
1035 With prefix argument SET-VERSION, allow user to specify initial version
1036 level. If COMMENT is present, use that as an initial comment.
1038 The version control system to use is found by cycling through the list
1039 `vc-handled-backends'. The first backend in that list which declares
1040 itself responsible for the file (usually because other files in that
1041 directory are already registered under that backend) will be used to
1042 register the file. If no backend declares itself responsible, the
1043 first backend that could register the file is used."
1045 (unless buffer-file-name
(error "No visited file"))
1046 (when (vc-backend buffer-file-name
)
1047 (if (vc-registered buffer-file-name
)
1048 (error "This file is already registered")
1049 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1050 (error "Aborted"))))
1051 ;; Watch out for new buffers of size 0: the corresponding file
1052 ;; does not exist yet, even though buffer-modified-p is nil.
1053 (if (and (not (buffer-modified-p))
1054 (zerop (buffer-size))
1055 (not (file-exists-p buffer-file-name
)))
1056 (set-buffer-modified-p t
))
1059 (vc-start-entry buffer-file-name
1061 (read-string (format "Initial version level for %s: "
1063 ;; TODO: Use backend-specific init version.
1064 vc-default-init-version
)
1065 (or comment
(not vc-initial-comment
))
1066 "Enter initial comment."
1067 (lambda (file rev comment
)
1068 (message "Registering %s... " file
)
1069 (let ((backend (vc-responsible-backend file
)))
1070 (vc-file-clearprops file
)
1071 (vc-call-backend backend
'register file rev comment
)
1072 (vc-file-setprop file
'vc-backend backend
)
1073 (unless vc-make-backup-files
1074 (make-local-variable 'backup-inhibited
)
1075 (setq backup-inhibited t
)))
1076 (message "Registering %s... done" file
))))
1078 (defun vc-responsible-backend (file &optional register
)
1079 "Return the name of the backend system that is responsible for FILE.
1080 If no backend in variable `vc-handled-backends' declares itself
1081 responsible, the first backend in that list will be returned (if optional
1082 arg REGISTER is non-nil, return the first backend that could register the
1084 FILE can also be a directory name (ending with a slash)."
1085 (if (null vc-handled-backends
)
1086 (error "Cannot register, no backends in `vc-handled-backends'"))
1087 (or (and (not (file-directory-p file
)) (vc-backend file
))
1089 (mapcar (lambda (backend)
1090 (if (vc-call-backend backend
'responsible-p file
)
1091 (throw 'found backend
)))
1092 vc-handled-backends
)
1094 (mapcar (lambda (backend)
1095 (if (vc-call-backend backend
'could-register file
)
1096 (throw 'found backend
)))
1097 vc-handled-backends
)
1098 (car vc-handled-backends
)))))
1100 (defun vc-default-responsible-p (backend file
)
1101 "Indicate whether BACKEND is reponsible for FILE.
1102 The default is to return nil always."
1105 (defun vc-default-could-register (backend file
)
1106 "Return non-nil if BACKEND could be used to register FILE.
1107 The default implementation returns t for all files."
1110 (defun vc-unregister (file backend
)
1111 "Unregister FILE from version control system BACKEND."
1112 (vc-call-backend backend
'unregister file
)
1113 (vc-file-clearprops file
))
1115 (defun vc-default-unregister (backend file
)
1116 "Default implementation of vc-unregister, signals an error."
1117 (error "Unregistering files is not supported for %s" backend
))
1119 (defun vc-resynch-window (file &optional keep noquery
)
1120 "If FILE is in the current buffer, either revert or unvisit it.
1121 The choice between revert (to see expanded keywords) and unvisit depends on
1122 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1123 reverting. NOQUERY should be t *only* if it is known the only
1124 difference between the buffer and the file is due to version control
1125 rather than user editing!"
1126 (and (string= buffer-file-name file
)
1129 (vc-revert-buffer1 t noquery
)
1130 ;; TODO: Adjusting view mode might no longer be necessary
1131 ;; after RMS change to files.el of 1999-08-08. Investigate
1132 ;; this when we install the new VC.
1134 (if (file-writable-p file
)
1136 (let ((view-old-buffer-read-only nil
))
1138 (and (not view-mode
)
1139 (not (eq (get major-mode
'mode-class
) 'special
))
1140 (view-mode-enter))))
1141 (vc-mode-line buffer-file-name
))
1142 (kill-buffer (current-buffer)))))
1144 (defun vc-resynch-buffer (file &optional keep noquery
)
1145 "If FILE is currently visited, resynch its buffer."
1146 (if (string= buffer-file-name file
)
1147 (vc-resynch-window file keep noquery
)
1148 (let ((buffer (get-file-buffer file
)))
1150 (with-current-buffer buffer
1151 (vc-resynch-window file keep noquery
)))))
1152 (vc-dired-resynch-file file
))
1154 (defun vc-start-entry (file rev comment msg action
&optional after-hook
)
1155 "Accept a comment for an operation on FILE revision REV.
1156 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1157 action on close to ACTION; otherwise, do action immediately. Remember
1158 the file's buffer in `vc-parent-buffer' (current one if no file).
1159 AFTER-HOOK specifies the local value for vc-log-operation-hook."
1160 (let ((parent (if file
(find-file-noselect file
) (current-buffer))))
1161 (if vc-before-checkin-hook
1163 (with-current-buffer parent
1164 (run-hooks 'vc-before-checkin-hook
))
1165 (run-hooks 'vc-before-checkin-hook
)))
1167 (set-buffer (get-buffer-create "*VC-log*"))
1168 (pop-to-buffer (get-buffer-create "*VC-log*")))
1169 (set (make-local-variable 'vc-parent-buffer
) parent
)
1170 (set (make-local-variable 'vc-parent-buffer-name
)
1171 (concat " from " (buffer-name vc-parent-buffer
)))
1172 (if file
(vc-mode-line file
))
1174 (make-local-variable 'vc-log-after-operation-hook
)
1176 (setq vc-log-after-operation-hook after-hook
))
1177 (setq vc-log-operation action
)
1178 (setq vc-log-version rev
)
1183 (vc-finish-logentry t
)
1185 (vc-finish-logentry nil
)))
1186 (message "%s Type C-c C-c when done" msg
))))
1188 (defun vc-checkout (file &optional writable rev
)
1189 "Retrieve a copy of the revision REV of FILE.
1190 If WRITABLE is non-nil, make sure the retrieved file is writable.
1191 REV defaults to the latest revision."
1195 (vc-call checkout file writable rev
)
1197 ;; Maybe the backend is not installed ;-(
1199 (let ((buf (get-file-buffer file
)))
1200 (when buf
(with-current-buffer buf
(toggle-read-only -
1)))))
1201 (signal (car err
) (cdr err
))))
1202 `((vc-state ,(if (or (eq (vc-checkout-model file
) 'implicit
)
1204 (if (vc-call latest-on-branch-p file
)
1208 (vc-checkout-time ,(nth 5 (file-attributes file
)))))
1209 (vc-resynch-buffer file t t
))
1211 (defun vc-steal-lock (file rev owner
)
1212 "Steal the lock on FILE."
1213 (let (file-description)
1215 (setq file-description
(format "%s:%s" file rev
))
1216 (setq file-description file
))
1217 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1218 file-description owner
)))
1219 (error "Steal canceled"))
1220 (compose-mail owner
(format "Stolen lock on %s" file-description
)
1222 (list (list 'vc-finish-steal file rev
)))
1223 (setq default-directory
(expand-file-name "~/"))
1224 (goto-char (point-max))
1226 (format "I stole the lock on %s, " file-description
)
1227 (current-time-string)
1229 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1231 (defun vc-finish-steal (file version
)
1232 ;; This is called when the notification has been sent.
1233 (message "Stealing lock on %s..." file
)
1236 (vc-call steal-lock file version
)
1237 `((vc-state edited
)))
1238 (vc-resynch-buffer file t t
)
1239 (message "Stealing lock on %s...done" file
))
1241 (defun vc-checkin (file &optional rev comment
)
1243 The optional argument REV may be a string specifying the new version
1244 level (if nil increment the current level). COMMENT is a comment
1245 string; if omitted, a buffer is popped up to accept a comment.
1247 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1248 that the version control system supports this mode of operation.
1250 Runs the normal hook `vc-checkin-hook'."
1253 "Enter a change comment."
1254 (lambda (file rev comment
)
1255 (message "Checking in %s..." file
)
1256 ;; "This log message intentionally left almost blank".
1257 ;; RCS 5.7 gripes about white-space-only comments too.
1258 (or (and comment
(string-match "[^\t\n ]" comment
))
1259 (setq comment
"*** empty log message ***"))
1262 ;; Change buffers to get local value of vc-checkin-switches.
1263 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
1264 (vc-call checkin file rev comment
))
1265 `((vc-state up-to-date
)
1266 (vc-checkout-time ,(nth 5 (file-attributes file
)))
1267 (vc-workfile-version nil
)))
1268 (message "Checking in %s...done" file
))
1271 (defun vc-comment-to-change-log (&optional whoami file-name
)
1272 "Enter last VC comment into change log file for current buffer's file.
1273 Optional arg (interactive prefix) non-nil means prompt for user name and site.
1274 Second arg is file name of change log. \
1275 If nil, uses `change-log-default-name'.
1277 May be useful as a `vc-checkin-hook' to update change logs automatically."
1278 (interactive (if current-prefix-arg
1279 (list current-prefix-arg
1280 (prompt-for-change-log-name))))
1281 ;; Make sure the defvar for add-log-current-defun-function has been executed
1282 ;; before binding it.
1284 (let (;; Extract the comment first so we get any error before doing anything.
1285 (comment (ring-ref vc-comment-ring
0))
1286 ;; Don't let add-change-log-entry insert a defun name.
1287 (add-log-current-defun-function 'ignore
)
1289 ;; Call add-log to do half the work.
1290 (add-change-log-entry whoami file-name t t
)
1291 ;; Insert the VC comment, leaving point before it.
1292 (setq end
(save-excursion (insert comment
) (point-marker)))
1293 (if (looking-at "\\s *\\s(")
1294 ;; It starts with an open-paren, as in "(foo): Frobbed."
1295 ;; So remove the ": " add-log inserted.
1297 ;; Canonicalize the white space between the file name and comment.
1299 ;; Indent rest of the text the same way add-log indented the first line.
1300 (let ((indentation (current-indentation)))
1302 (while (< (point) end
)
1304 (indent-to indentation
))
1305 (setq end
(point))))
1306 ;; Fill the inserted text, preserving open-parens at bol.
1307 (let ((paragraph-separate (concat paragraph-separate
"\\|\\s *\\s("))
1308 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
1310 (fill-region (point) end
))
1311 ;; Canonicalize the white space at the end of the entry so it is
1312 ;; separated from the next entry by a single blank line.
1313 (skip-syntax-forward " " end
)
1314 (delete-char (- (skip-syntax-backward " ")))
1315 (or (eobp) (looking-at "\n\n")
1318 (defun vc-finish-logentry (&optional nocomment
)
1319 "Complete the operation implied by the current log entry."
1321 ;; Check and record the comment, if any.
1323 ;; Comment too long?
1324 (vc-call-backend (or (and vc-log-file
(vc-backend vc-log-file
))
1325 (vc-responsible-backend default-directory
))
1327 (run-hooks 'vc-logentry-check-hook
)
1328 ;; Record the comment in the comment ring
1329 (let ((comment (buffer-string)))
1330 (unless (and (ring-p vc-comment-ring
)
1331 (not (ring-empty-p vc-comment-ring
))
1332 (equal comment
(ring-ref vc-comment-ring
0)))
1333 (ring-insert vc-comment-ring comment
))))
1334 ;; Sync parent buffer in case the user modified it while editing the comment.
1335 ;; But not if it is a vc-dired buffer.
1336 (with-current-buffer vc-parent-buffer
1337 (or vc-dired-mode
(vc-buffer-sync)))
1338 (if (not vc-log-operation
) (error "No log operation is pending"))
1339 ;; save the parameters held in buffer-local variables
1340 (let ((log-operation vc-log-operation
)
1341 (log-file vc-log-file
)
1342 (log-version vc-log-version
)
1343 (log-entry (buffer-string))
1344 (after-hook vc-log-after-operation-hook
)
1345 (tmp-vc-parent-buffer vc-parent-buffer
))
1346 (pop-to-buffer vc-parent-buffer
)
1349 (funcall log-operation
1353 ;; Remove checkin window (after the checkin so that if that fails
1354 ;; we don't zap the *VC-log* buffer and the typing therein).
1355 (let ((logbuf (get-buffer "*VC-log*")))
1356 (cond ((and logbuf vc-delete-logbuf-window
)
1357 (delete-windows-on logbuf
(selected-frame))
1358 ;; Kill buffer and delete any other dedicated windows/frames.
1359 (kill-buffer logbuf
))
1360 (t (pop-to-buffer "*VC-log*")
1362 (pop-to-buffer tmp-vc-parent-buffer
))))
1363 ;; Now make sure we see the expanded headers
1364 (if buffer-file-name
1365 (vc-resynch-buffer buffer-file-name vc-keep-workfiles t
))
1367 (dired-move-to-filename))
1368 (run-hooks after-hook
'vc-finish-logentry-hook
)))
1370 ;; Code for access to the comment ring
1372 (defun vc-new-comment-index (stride len
)
1374 (vc-comment-ring-index (+ vc-comment-ring-index stride
))
1375 ;; Initialize the index on the first use of this command
1376 ;; so that the first M-p gets index 0, and the first M-n gets
1378 ((> stride
0) (1- stride
))
1382 (defun vc-previous-comment (arg)
1383 "Cycle backwards through comment history."
1385 (let ((len (ring-length vc-comment-ring
)))
1387 (progn (message "Empty comment ring") (ding))
1389 (setq vc-comment-ring-index
(vc-new-comment-index arg len
))
1390 (message "Comment %d" (1+ vc-comment-ring-index
))
1391 (insert (ring-ref vc-comment-ring vc-comment-ring-index
)))))
1393 (defun vc-next-comment (arg)
1394 "Cycle forwards through comment history."
1396 (vc-previous-comment (- arg
)))
1398 (defun vc-comment-search-reverse (str &optional stride
)
1399 "Searches backwards through comment history for substring match."
1400 ;; Why substring rather than regexp ? -sm
1402 (list (read-string "Comment substring: " nil nil vc-last-comment-match
)))
1403 (unless stride
(setq stride
1))
1404 (if (string= str
"")
1405 (setq str vc-last-comment-match
)
1406 (setq vc-last-comment-match str
))
1407 (let* ((str (regexp-quote str
))
1408 (len (ring-length vc-comment-ring
))
1409 (n (vc-new-comment-index stride len
)))
1410 (while (progn (when (or (>= n len
) (< n
0)) (error "Not found"))
1411 (not (string-match str
(ring-ref vc-comment-ring n
))))
1412 (setq n
(+ n stride
)))
1413 (setq vc-comment-ring-index n
)
1414 (vc-previous-comment 0)))
1416 (defun vc-comment-search-forward (str)
1417 "Searches forwards through comment history for substring match."
1419 (list (read-string "Comment substring: " nil nil vc-last-comment-match
)))
1420 (vc-comment-search-reverse str -
1))
1422 ;; Additional entry points for examining version histories
1425 (defun vc-diff (historic &optional not-urgent
)
1426 "Display diffs between file versions.
1427 Normally this compares the current file and buffer with the most recent
1428 checked in version of that file. This uses no arguments.
1429 With a prefix argument, it reads the file name to use
1430 and two version designators specifying which versions to compare."
1431 (interactive (list current-prefix-arg t
))
1432 (vc-ensure-vc-buffer)
1434 (call-interactively 'vc-version-diff
)
1435 (let ((file buffer-file-name
))
1436 (vc-buffer-sync not-urgent
)
1437 (if (vc-workfile-unchanged-p buffer-file-name
)
1438 (message "No changes to %s since latest version" file
)
1439 (vc-version-diff file nil nil
)))))
1441 (defun vc-version-diff (file rel1 rel2
)
1442 "For FILE, report diffs between two stored versions REL1 and REL2 of it.
1443 If FILE is a directory, generate diffs between versions for all registered
1444 files in or below it."
1446 (let ((file (expand-file-name
1447 (read-file-name (if buffer-file-name
1448 "File or dir to diff: (default visited file) "
1449 "File or dir to diff: ")
1450 default-directory buffer-file-name t
)))
1451 (rel1-default nil
) (rel2-default nil
))
1452 ;; compute default versions based on the file state
1454 ;; if it's a directory, don't supply any version default
1455 ((file-directory-p file
)
1457 ;; if the file is not up-to-date, use current version as older version
1458 ((not (vc-up-to-date-p file
))
1459 (setq rel1-default
(vc-workfile-version file
)))
1460 ;; if the file is not locked, use last and previous version as default
1462 (setq rel1-default
(vc-previous-version (vc-workfile-version file
)))
1463 (if (string= rel1-default
"") (setq rel1-default nil
))
1464 (setq rel2-default
(vc-workfile-version file
))))
1465 ;; construct argument list
1467 (read-string (if rel1-default
1468 (concat "Older version: (default "
1471 nil nil rel1-default
)
1472 (read-string (if rel2-default
1473 (concat "Newer version: (default "
1475 "Newer version (default: current source): ")
1476 nil nil rel2-default
))))
1477 (if (string-equal rel1
"") (setq rel1 nil
))
1478 (if (string-equal rel2
"") (setq rel2 nil
))
1479 (vc-setup-buffer "*vc-diff*")
1480 (if (file-directory-p file
)
1481 (let ((inhibit-read-only t
))
1482 (insert "Diffs between "
1483 (or rel1
"last version checked in")
1485 (or rel2
"current workfile(s)")
1487 (setq default-directory
(file-name-as-directory file
))
1488 ;; FIXME: this should do a single exec in CVS.
1494 (message "Looking at %s" ',f
)
1495 (vc-call-backend ',(vc-backend file
) 'diff
',f
',rel1
',rel2
)))))
1496 (vc-exec-after `(let ((inhibit-read-only t
))
1497 (insert "\nEnd of diffs.\n"))))
1499 (cd (file-name-directory file
))
1500 (vc-call diff file rel1 rel2
))
1501 (if (and (zerop (buffer-size))
1502 (not (get-buffer-process (current-buffer))))
1506 (message "No changes to %s between %s and %s" file rel1 rel2
)
1507 (message "No changes to %s since %s" file rel1
))
1508 (message "No changes to %s since latest version" file
))
1510 (pop-to-buffer (current-buffer))
1511 ;; Gnus-5.8.5 sets up an autoload for diff-mode, even if it's
1512 ;; not available. Work around that.
1513 (if (require 'diff-mode nil t
) (diff-mode))
1514 (vc-exec-after '(progn (if (eq (buffer-size) 0)
1515 (insert "No differences found.\n"))
1516 (goto-char (point-min))
1517 (shrink-window-if-larger-than-buffer)))
1521 (defun vc-version-other-window (rev)
1522 "Visit version REV of the current buffer in another window.
1523 If the current buffer is named `F', the version is named `F.~REV~'.
1524 If `F.~REV~' already exists, it is used instead of being re-created."
1525 (interactive "sVersion to visit (default is workfile version): ")
1526 (vc-ensure-vc-buffer)
1527 (let* ((version (if (string-equal rev
"")
1528 (vc-workfile-version buffer-file-name
)
1530 (filename (concat buffer-file-name
".~" version
"~")))
1531 (or (file-exists-p filename
)
1532 (vc-call checkout buffer-file-name nil version filename
))
1533 (find-file-other-window filename
)))
1535 ;; Header-insertion code
1538 (defun vc-insert-headers ()
1539 "Insert headers in a file for use with your version control system.
1540 Headers desired are inserted at point, and are pulled from
1541 the variable `vc-BACKEND-header'."
1543 (vc-ensure-vc-buffer)
1547 (if (or (not (vc-check-headers))
1548 (y-or-n-p "Version headers already exist. Insert another set? "))
1550 (let* ((delims (cdr (assq major-mode vc-comment-alist
)))
1551 (comment-start-vc (or (car delims
) comment-start
"#"))
1552 (comment-end-vc (or (car (cdr delims
)) comment-end
""))
1553 (hdsym (vc-make-backend-sym (vc-backend (buffer-file-name))
1555 (hdstrings (and (boundp hdsym
) (symbol-value hdsym
))))
1557 (insert comment-start-vc
"\t" s
"\t"
1558 comment-end-vc
"\n"))
1560 (if vc-static-header-alist
1562 (if (string-match (car f
) buffer-file-name
)
1563 (insert (format (cdr f
) (car hdstrings
)))))
1564 vc-static-header-alist
))
1568 (defun vc-clear-headers (&optional file
)
1569 "Clear all version headers in the current buffer (or FILE).
1570 I.e. reset them to the non-expanded form."
1571 (let* ((filename (or file buffer-file-name
))
1572 (visited (find-buffer-visiting filename
))
1573 (backend (vc-backend filename
)))
1574 (when (vc-find-backend-function backend
'clear-headers
)
1576 (let ((context (vc-buffer-context)))
1577 ;; save-excursion may be able to relocate point and mark
1578 ;; properly. If it fails, vc-restore-buffer-context
1579 ;; will give it a second try.
1581 (vc-call-backend backend
'clear-headers
))
1582 (vc-restore-buffer-context context
))
1583 (find-file filename
)
1584 (vc-call-backend backend
'clear-headers
)
1585 (kill-buffer filename
)))))
1589 "Merge changes between two versions into the current buffer's file.
1590 This asks for two versions to merge from in the minibuffer. If the
1591 first version is a branch number, then merge all changes from that
1592 branch. If the first version is empty, merge news, i.e. recent changes
1593 from the current branch.
1595 See Info node `Merging'."
1597 (vc-ensure-vc-buffer)
1599 (let* ((file buffer-file-name
)
1600 (backend (vc-backend file
))
1601 (state (vc-state file
))
1602 first-version second-version status
)
1605 (error "File is locked by %s" state
))
1606 ((not (vc-editable-p file
))
1608 "File must be checked out for merging. Check out now? ")
1609 (vc-checkout file t
)
1610 (error "Merge aborted"))))
1612 (read-string (concat "Branch or version to merge from "
1613 "(default: news on current branch): ")))
1614 (if (string= first-version
"")
1615 (if (not (vc-find-backend-function backend
'merge-news
))
1616 (error "Sorry, merging news is not implemented for %s" backend
)
1617 (setq status
(vc-call merge-news file
)))
1618 (if (not (vc-find-backend-function backend
'merge
))
1619 (error "Sorry, merging is not implemented for %s" backend
)
1620 (if (not (vc-branch-p first-version
))
1621 (setq second-version
1622 (read-string "Second version: "
1623 (concat (vc-branch-part first-version
) ".")))
1624 ;; We want to merge an entire branch. Set versions
1625 ;; accordingly, so that vc-BACKEND-merge understands us.
1626 (setq second-version first-version
)
1627 ;; first-version must be the starting point of the branch
1628 (setq first-version
(vc-branch-part first-version
)))
1629 (setq status
(vc-call merge file first-version second-version
))))
1630 (vc-maybe-resolve-conflicts file status
"WORKFILE" "MERGE SOURCE")))
1632 (defun vc-maybe-resolve-conflicts (file status
&optional name-A name-B
)
1633 (vc-resynch-buffer file t
(not (buffer-modified-p)))
1634 (if (zerop status
) (message "Merge successful")
1635 (if (fboundp 'smerge-mode
) (smerge-mode 1))
1636 (if (y-or-n-p "Conflicts detected. Resolve them now? ")
1637 (if (fboundp 'smerge-ediff
)
1639 (vc-resolve-conflicts name-A name-B
))
1640 (message "File contains conflict markers"))))
1642 (defvar vc-ediff-windows
)
1643 (defvar vc-ediff-result
)
1645 (defvar ediff-buffer-A
)
1646 (defvar ediff-buffer-B
)
1647 (defvar ediff-buffer-C
)
1648 (require 'ediff-util
))
1650 (defun vc-resolve-conflicts (&optional name-A name-B
)
1651 "Invoke ediff to resolve conflicts in the current buffer.
1652 The conflicts must be marked with rcsmerge conflict markers."
1654 (vc-ensure-vc-buffer)
1656 (file-name (file-name-nondirectory buffer-file-name
))
1657 (your-buffer (generate-new-buffer
1658 (concat "*" file-name
1659 " " (or name-A
"WORKFILE") "*")))
1660 (other-buffer (generate-new-buffer
1661 (concat "*" file-name
1662 " " (or name-B
"CHECKED-IN") "*")))
1663 (result-buffer (current-buffer)))
1665 (set-buffer your-buffer
)
1667 (insert-buffer result-buffer
)
1668 (goto-char (point-min))
1669 (while (re-search-forward (concat "^<<<<<<< "
1670 (regexp-quote file-name
) "\n") nil t
)
1673 (if (not (re-search-forward "^=======\n" nil t
))
1674 (error "Malformed conflict marker"))
1676 (let ((start (point)))
1677 (if (not (re-search-forward "^>>>>>>> [0-9.]+\n" nil t
))
1678 (error "Malformed conflict marker"))
1679 (delete-region start
(point))))
1682 (kill-buffer your-buffer
)
1683 (kill-buffer other-buffer
)
1684 (error "No conflict markers found")))
1685 (set-buffer other-buffer
)
1687 (insert-buffer result-buffer
)
1688 (goto-char (point-min))
1689 (while (re-search-forward (concat "^<<<<<<< "
1690 (regexp-quote file-name
) "\n") nil t
)
1691 (let ((start (match-beginning 0)))
1692 (if (not (re-search-forward "^=======\n" nil t
))
1693 (error "Malformed conflict marker"))
1694 (delete-region start
(point))
1695 (if (not (re-search-forward "^>>>>>>> [0-9.]+\n" nil t
))
1696 (error "Malformed conflict marker"))
1697 (replace-match "")))
1698 (let ((config (current-window-configuration))
1699 (ediff-default-variant 'default-B
))
1703 (set-buffer (ediff-merge-buffers your-buffer other-buffer
))
1705 ;; Ediff is now set up, and we are in the control buffer.
1706 ;; Do a few further adjustments and take precautions for exit.
1708 (make-local-variable 'vc-ediff-windows
)
1709 (setq vc-ediff-windows config
)
1710 (make-local-variable 'vc-ediff-result
)
1711 (setq vc-ediff-result result-buffer
)
1712 (make-local-variable 'ediff-quit-hook
)
1713 (setq ediff-quit-hook
1715 (let ((buffer-A ediff-buffer-A
)
1716 (buffer-B ediff-buffer-B
)
1717 (buffer-C ediff-buffer-C
)
1718 (result vc-ediff-result
)
1719 (windows vc-ediff-windows
))
1720 (ediff-cleanup-mess)
1723 (insert-buffer buffer-C
)
1724 (kill-buffer buffer-A
)
1725 (kill-buffer buffer-B
)
1726 (kill-buffer buffer-C
)
1727 (set-window-configuration windows
)
1728 (message "Conflict resolution finished; you may save the buffer"))))
1729 (message "Please resolve conflicts now; exit ediff when done")
1732 ;; The VC directory major mode. Coopt Dired for this.
1733 ;; All VC commands get mapped into logical equivalents.
1735 (defvar vc-dired-switches
)
1736 (defvar vc-dired-terse-mode
)
1738 (defvar vc-dired-mode-map
1739 (let ((map (make-sparse-keymap))
1740 (vmap (make-sparse-keymap)))
1741 (define-key map
"\C-xv" vc-prefix-map
)
1742 ;; Emacs-20 has a lousy keymap inheritance that won't work here.
1743 ;; Emacs-21's is still lousy but just better enough that it'd work. -sm
1744 ;; (set-keymap-parent vmap vc-prefix-map)
1745 (setq vmap vc-prefix-map
)
1746 (define-key map
"v" vmap
)
1747 (define-key vmap
"t" 'vc-dired-toggle-terse-mode
)
1750 (define-derived-mode vc-dired-mode dired-mode
"Dired under VC"
1751 "The major mode used in VC directory buffers.
1753 It works like Dired, but lists only files under version control, with
1754 the current VC state of each file being indicated in the place of the
1755 file's link count, owner, group and size. Subdirectories are also
1756 listed, and you may insert them into the buffer as desired, like in
1759 All Dired commands operate normally, with the exception of `v', which
1760 is redefined as the version control prefix, so that you can type
1761 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
1762 the file named in the current Dired buffer line. `vv' invokes
1763 `vc-next-action' on this file, or on all files currently marked.
1764 There is a special command, `*l', to mark all files currently locked."
1765 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
1766 ;; We do it here because dired might not be loaded yet
1767 ;; when vc-dired-mode-map is initialized.
1768 (set-keymap-parent vc-dired-mode-map dired-mode-map
)
1769 (make-local-hook 'dired-after-readin-hook
)
1770 (add-hook 'dired-after-readin-hook
'vc-dired-hook nil t
)
1771 ;; The following is slightly modified from dired.el,
1772 ;; because file lines look a bit different in vc-dired-mode.
1773 (set (make-local-variable 'dired-move-to-filename-regexp
)
1775 ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
1776 ;; In some locales, month abbreviations are as short as 2 letters,
1777 ;; and they can be padded on the right with spaces.
1778 (month (concat l l
"+ *"))
1779 ;; Recognize any non-ASCII character.
1780 ;; The purpose is to match a Kanji character.
1782 ;; (k "[^\x00-\x7f\x80-\xff]")
1784 (yyyy "[0-9][0-9][0-9][0-9]")
1787 (HH:MM
"[ 0-2][0-9]:[0-5][0-9]")
1788 (western (concat "\\(" month s dd
"\\|" dd s month
"\\)"
1789 s
"\\(" HH
:MM
"\\|" s yyyy
"\\|" yyyy s
"\\)"))
1790 (japanese (concat mm k s dd k s
"\\(" s HH
:MM
"\\|" yyyy k
"\\)")))
1791 ;; the .* below ensures that we find the last match on a line
1792 (concat ".*" s
"\\(" western
"\\|" japanese
"\\)" s
)))
1793 (and (boundp 'vc-dired-switches
)
1795 (set (make-local-variable 'dired-actual-switches
)
1797 (set (make-local-variable 'vc-dired-terse-mode
) vc-dired-terse-display
)
1798 (setq vc-dired-mode t
))
1800 (defun vc-dired-toggle-terse-mode ()
1801 "Toggle terse display in VC Dired."
1803 (if (not vc-dired-mode
)
1805 (setq vc-dired-terse-mode
(not vc-dired-terse-mode
))
1806 (if vc-dired-terse-mode
1810 (defun vc-dired-mark-locked ()
1811 "Mark all files currently locked."
1813 (dired-mark-if (let ((f (dired-get-filename nil t
)))
1815 (not (file-directory-p f
))
1816 (not (vc-up-to-date-p f
))))
1819 (define-key vc-dired-mode-map
"*l" 'vc-dired-mark-locked
)
1821 (defun vc-default-dired-state-info (backend file
)
1822 (let ((state (vc-state file
)))
1824 ((stringp state
) (concat "(" state
")"))
1825 ((eq state
'edited
) (concat "(" (vc-user-login-name) ")"))
1826 ((eq state
'needs-merge
) "(merge)")
1827 ((eq state
'needs-patch
) "(patch)")
1828 ((eq state
'unlocked-changes
) "(stale)"))))
1830 (defun vc-dired-reformat-line (x)
1831 "Reformat a directory-listing line.
1832 Replace various columns with version control information.
1833 This code, like dired, assumes UNIX -l format."
1835 (let ((pos (point)) limit perm date-and-file
)
1837 (setq limit
(point))
1841 (re-search-forward ;; owner and group
1842 "^\\(..[drwxlts-]+ \\) *[0-9]+ [^ ]+ +[^ ]+ +[0-9]+\\( .*\\)"
1844 (re-search-forward ;; only owner displayed
1845 "^\\(..[drwxlts-]+ \\) *[0-9]+ [^ ]+ +[0-9]+\\( .*\\)"
1847 (re-search-forward ;; OS/2 -l format, no links, owner, group
1848 "^\\(..[drwxlts-]+ \\) *[0-9]+\\( .*\\)"
1850 (setq perm
(match-string 1)
1851 date-and-file
(match-string 2))
1852 (setq x
(substring (concat x
" ") 0 10))
1853 (replace-match (concat perm x date-and-file
)))))
1855 (defun vc-dired-hook ()
1856 "Reformat the listing according to version control.
1857 Called by dired after any portion of a vc-dired buffer has been read in."
1858 (message "Getting version information... ")
1859 (let (subdir filename
(buffer-read-only nil
) cvs-dir
)
1860 (goto-char (point-min))
1863 ;; subdir header line
1864 ((setq subdir
(dired-get-subdir))
1865 ;; if the backend supports it, get the state
1866 ;; of all files in this directory at once
1867 (let ((backend (vc-responsible-backend subdir
)))
1868 (if (vc-find-backend-function backend
'dir-state
)
1869 (vc-call-backend backend
'dir-state subdir
)))
1871 ;; erase (but don't remove) the "total" line
1872 (delete-region (point) (line-end-position))
1876 ((setq filename
(dired-get-filename nil t
))
1879 ((file-directory-p filename
)
1881 ((member (file-name-nondirectory filename
)
1882 vc-directory-exclusion-list
)
1883 (let ((pos (point)))
1884 (dired-kill-tree filename
)
1887 (vc-dired-terse-mode
1888 ;; Don't show directories in terse mode. Don't use
1889 ;; dired-kill-line to remove it, because in recursive listings,
1890 ;; that would remove the directory contents as well.
1891 (delete-region (line-beginning-position)
1892 (progn (forward-line 1) (point))))
1893 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename
))
1896 (vc-dired-reformat-line nil
)
1899 ((and (vc-backend filename
)
1900 (not (and vc-dired-terse-mode
1901 (vc-up-to-date-p filename
))))
1902 (vc-dired-reformat-line (vc-call dired-state-info filename
))
1905 (dired-kill-line))))
1907 (t (forward-line 1))))
1909 (message "Getting version information... done")
1912 (cond ((eq (count-lines (point-min) (point-max)) 1)
1913 (goto-char (point-min))
1914 (message "No files locked under %s" default-directory
)))))
1916 (defun vc-dired-purge ()
1917 "Remove empty subdirs."
1919 (goto-char (point-min))
1920 (while (setq subdir
(dired-get-subdir))
1922 (if (dired-get-filename nil t
)
1923 (if (not (dired-next-subdir 1 t
))
1924 (goto-char (point-max)))
1926 (if (not (string= (dired-current-directory) default-directory
))
1927 (dired-do-kill-lines t
"")
1928 ;; We cannot remove the top level directory.
1929 ;; Just make it look a little nicer.
1932 (if (not (dired-next-subdir 1 t
))
1933 (goto-char (point-max))))))
1934 (goto-char (point-min))))
1936 (defun vc-dired-buffers-for-dir (dir)
1937 "Return a list of all vc-dired buffers that currently display DIR."
1939 ;; Check whether dired is loaded.
1940 (when (fboundp 'dired-buffers-for-dir
)
1941 (mapcar (lambda (buffer)
1942 (with-current-buffer buffer
1944 (setq result
(append result
(list buffer
))))))
1945 (dired-buffers-for-dir dir
)))
1948 (defun vc-dired-resynch-file (file)
1949 "Update the entries for FILE in any VC Dired buffers that list it."
1950 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file
))))
1952 (mapcar (lambda (buffer)
1953 (with-current-buffer buffer
1954 (if (dired-goto-file file
)
1955 ;; bind vc-dired-terse-mode to nil so that
1956 ;; files won't vanish when they are checked in
1957 (let ((vc-dired-terse-mode nil
))
1958 (dired-do-redisplay 1)))))
1962 (defun vc-directory (dir read-switches
)
1963 "Create a buffer in VC Dired Mode for directory DIR.
1965 See Info node `VC Dired Mode'.
1967 With prefix arg READ-SWITCHES, specify a value to override
1968 `dired-listing-switches' when generating the listing."
1969 (interactive "DDired under VC (directory): \nP")
1970 (let ((vc-dired-switches (concat vc-dired-listing-switches
1971 (if vc-dired-recurse
"R" ""))))
1973 (setq vc-dired-switches
1974 (read-string "Dired listing switches: "
1975 vc-dired-switches
)))
1977 (require 'dired-aux
)
1979 (dired-internal-noselect (expand-file-name (file-name-as-directory dir
))
1984 ;; Named-configuration entry points
1986 (defun vc-snapshot-precondition (dir)
1987 "Scan the tree below DIR, looking for non-uptodate files.
1988 If any file is not up-to-date, return the name of the first such file.
1989 \(This means, neither snapshot creation nor retrieval is allowed.\)
1990 If one or more of the files are currently visited, return `visited'.
1991 Otherwise, return nil."
1993 (catch 'vc-locked-example
1997 (if (not (vc-up-to-date-p f
)) (throw 'vc-locked-example f
)
1998 (if (get-file-buffer f
) (setq status
'visited
)))))
2002 (defun vc-create-snapshot (dir name branchp
)
2003 "Descending recursively from DIR, make a snapshot called NAME.
2004 For each registered file, the version level of its latest version
2005 becomes part of the named configuration. If the prefix argument
2006 BRANCHP is given, the snapshot is made as a new branch and the files
2007 are checked out in that new branch."
2009 (list (read-file-name "Directory: " default-directory default-directory t
)
2010 (read-string "New snapshot name: ")
2011 current-prefix-arg
))
2012 (message "Making %s... " (if branchp
"branch" "snapshot"))
2013 (if (file-directory-p dir
) (setq dir
(file-name-as-directory dir
)))
2014 (vc-call-backend (vc-responsible-backend dir
)
2015 'create-snapshot dir name branchp
)
2016 (message "Making %s... done" (if branchp
"branch" "snapshot")))
2018 (defun vc-default-create-snapshot (backend dir name branchp
)
2020 (error "VC backend %s does not support module branches" backend
))
2021 (let ((result (vc-snapshot-precondition dir
)))
2022 (if (stringp result
)
2023 (error "File %s is not up-to-date" result
)
2027 (vc-call assign-name f name
))))))
2030 (defun vc-retrieve-snapshot (dir name
)
2031 "Descending recursively from DIR, retrieve the snapshot called NAME.
2032 If NAME is empty, it refers to the latest versions.
2033 If locking is used for the files in DIR, then there must not be any
2034 locked files at or below DIR (but if NAME is empty, locked files are
2035 allowed and simply skipped)."
2037 (list (read-file-name "Directory: " default-directory default-directory t
)
2038 (read-string "Snapshot name to retrieve (default latest versions): ")))
2039 (let ((update (yes-or-no-p "Update any affected buffers? "))
2040 (msg (if (or (not name
) (string= name
""))
2041 (format "Updating %s... " (abbreviate-file-name dir
))
2042 (format "Retrieving snapshot into %s... "
2043 (abbreviate-file-name dir
)))))
2045 (vc-call-backend (vc-responsible-backend dir
)
2046 'retrieve-snapshot dir name update
)
2047 (message (concat msg
"done"))))
2049 (defun vc-default-retrieve-snapshot (backend dir name update
)
2050 (if (string= name
"")
2057 (vc-call checkout f nil
"")
2058 (if update
(vc-resynch-buffer f t t
)))))))
2059 (let ((result (vc-snapshot-precondition dir
)))
2060 (if (stringp result
)
2061 (error "File %s is locked" result
)
2062 (setq update
(and (eq result
'visited
) update
))
2067 (vc-call checkout f nil name
)
2068 (if update
(vc-resynch-buffer f t t
))))))))))
2070 ;; Miscellaneous other entry points
2073 (defun vc-print-log ()
2074 "List the change log of the current buffer in a window."
2076 (vc-ensure-vc-buffer)
2077 (let ((file buffer-file-name
))
2078 (vc-setup-buffer nil
)
2079 (setq default-directory
(file-name-directory file
))
2080 (vc-call print-log file
)
2081 (pop-to-buffer (current-buffer))
2082 (if (fboundp 'log-view-mode
) (log-view-mode))
2085 (goto-char (point-max)) (forward-line -
1)
2086 (while (looking-at "=*\n")
2087 (delete-char (- (match-end 0) (match-beginning 0)))
2089 (goto-char (point-min))
2090 (if (looking-at "[\b\t\n\v\f\r ]+")
2091 (delete-char (- (match-end 0) (match-beginning 0))))
2092 (shrink-window-if-larger-than-buffer)
2093 ;; move point to the log entry for the current version
2094 (if (fboundp 'log-view-goto-rev
)
2095 (log-view-goto-rev ',(vc-workfile-version file
))
2096 (if (vc-find-backend-function ',(vc-backend file
) 'show-log-entry
)
2097 (vc-call-backend ',(vc-backend file
)
2099 ',(vc-workfile-version file
))))))))
2102 (defun vc-revert-buffer ()
2103 "Revert the current buffer's file back to the version it was based on.
2104 This asks for confirmation if the buffer contents are not identical
2105 to that version. Note that for RCS and CVS, this function does not
2106 automatically pick up newer changes found in the master file;
2107 use \\[universal-argument] \\[vc-next-action] to do so."
2109 (vc-ensure-vc-buffer)
2110 (let ((file buffer-file-name
)
2111 ;; This operation should always ask for confirmation.
2112 (vc-suppress-confirm nil
)
2113 (obuf (current-buffer)))
2114 (unless (vc-workfile-unchanged-p file
)
2116 (vc-exec-after `(message nil
))
2118 (if (not (yes-or-no-p "Discard changes? "))
2119 (error "Revert canceled"))
2120 (if (or (window-dedicated-p (selected-window))
2121 (one-window-p t
'selected-frame
))
2122 (make-frame-invisible (selected-frame))
2126 (message "Reverting %s..." file
)
2129 (vc-call revert file
)
2130 `((vc-state up-to-date
)
2131 (vc-checkout-time ,(nth 5 (file-attributes file
)))))
2132 (vc-resynch-buffer file t t
)
2133 (message "Reverting %s...done" file
)))
2136 (defun vc-cancel-version (norevert)
2137 "Get rid of most recently checked in version of this file.
2138 A prefix argument NOREVERT means do not revert the buffer afterwards."
2140 (vc-ensure-vc-buffer)
2141 (let* ((file (buffer-file-name))
2142 (backend (vc-backend file
))
2143 (target (vc-workfile-version file
))
2144 (config (current-window-configuration)) done
)
2146 ((not (vc-find-backend-function backend
'cancel-version
))
2147 (error "Sorry, canceling versions is not supported under %s" backend
))
2148 ((not (vc-call latest-on-branch-p file
))
2149 (error "This is not the latest version; VC cannot cancel it"))
2150 ((not (vc-up-to-date-p file
))
2151 (error (substitute-command-keys "File is not up to date; use \\[vc-revert-buffer] to discard changes"))))
2152 (if (null (yes-or-no-p (format "Remove version %s from master? " target
)))
2154 (setq norevert
(or norevert
(not
2155 (yes-or-no-p "Revert buffer to most recent remaining version? "))))
2157 (message "Removing last change from %s..." file
)
2160 (vc-call cancel-version file norevert
)
2161 `((vc-state ,(if norevert
'edited
'up-to-date
))
2162 (vc-checkout-time ,(if norevert
2164 (nth 5 (file-attributes file
))))
2165 (vc-workfile-version nil
)))
2166 (message "Removing last change from %s...done" file
)
2169 (norevert ;; clear version headers and mark the buffer modified
2170 (set-visited-file-name file
)
2171 (when (not vc-make-backup-files
)
2172 ;; inhibit backup for this buffer
2173 (make-local-variable 'backup-inhibited
)
2174 (setq backup-inhibited t
))
2175 (setq buffer-read-only nil
)
2178 (vc-dired-resynch-file file
))
2179 (t ;; revert buffer to file on disk
2180 (vc-resynch-buffer file t t
)))
2181 (message "Version %s has been removed from the master" target
))))
2184 (defun vc-switch-backend (file backend
)
2185 "Make BACKEND the current version control system for FILE.
2186 FILE must already be registered in BACKEND. The change is not
2187 permanent, only for the current session. This function only changes
2188 VC's perspective on FILE, it does not register or unregister it."
2192 (intern (upcase (read-string "Switch to backend: ")))))
2193 (vc-file-clearprops file
)
2194 (vc-file-setprop file
'vc-backend backend
)
2195 (vc-resynch-buffer file t t
))
2197 (defun vc-index-of (backend)
2198 "Return the index of BACKEND in vc-handled-backends."
2199 (- (length vc-handled-backends
)
2200 (length (memq backend vc-handled-backends
))))
2203 (defun vc-transfer-file (file new-backend
)
2204 "Transfer FILE to another version control system NEW-BACKEND.
2205 If NEW-BACKEND has a higher precedence than FILE's current backend
2206 \(i.e. it comes earlier in vc-handled-backends), then register FILE in
2207 NEW-BACKEND, using the version number from the current backend as the
2208 base level. If NEW-BACKEND has a lower precedence than the current
2209 backend, then commit all changes that were made under the current
2210 backend to NEW-BACKEND, and unregister FILE from the current backend.
2211 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2212 (let ((old-backend (vc-backend file
)))
2213 (if (eq old-backend new-backend
)
2214 (error "%s is the current backend of %s"
2218 (vc-call-backend new-backend
'receive-file file
2219 (< (vc-index-of old-backend
)
2220 (vc-index-of new-backend
)))
2221 `((vc-backend ,new-backend
))))
2222 (vc-resynch-buffer file t t
)))
2224 (defun vc-default-receive-file (backend file move
)
2225 "Let BACKEND receive FILE from another version control system.
2226 If MOVE is non-nil, then FILE is unregistered from the old
2227 backend and its comment history is used as the initial contents
2228 of the log entry buffer."
2229 (let ((old-backend (vc-backend file
))
2230 (rev (vc-workfile-version file
))
2231 (state (vc-state file
))
2233 (vc-find-backend-function old-backend
'comment-history
)
2234 (vc-call 'comment-history file
))))
2235 (if move
(vc-unregister file old-backend
))
2236 (vc-file-clearprops file
)
2237 (if (not (vc-call-backend backend
'registered file
))
2240 ;; TODO: If the file was 'edited under the old backend,
2241 ;; this should actually register the version
2243 (vc-call-backend backend
'register file rev
"")
2244 `((vc-backend ,backend
)))
2245 (vc-file-setprop file
'vc-backend backend
)
2246 (vc-file-setprop file
'vc-state
'edited
)
2247 (set-file-modes file
2248 (logior (file-modes file
) 128)))
2249 (when (or move
(eq state
'edited
))
2250 (vc-file-setprop file
'vc-state
'edited
)
2251 ;; TODO: The comment history should actually become the
2252 ;; initial contents of the log entry buffer.
2253 (and comment
(ring-insert vc-comment-ring comment
))
2254 (vc-checkin file
))))
2256 (defun vc-rename-master (oldmaster newfile templates
)
2257 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2258 (let* ((dir (file-name-directory (expand-file-name oldmaster
)))
2259 (newdir (or (file-name-directory newfile
) ""))
2260 (newbase (file-name-nondirectory newfile
))
2262 ;; List of potential master files for `newfile'
2264 (lambda (s) (vc-possible-master s newdir newbase
))
2266 (if (or (file-symlink-p oldmaster
)
2267 (file-symlink-p (file-name-directory oldmaster
)))
2268 (error "This unsafe in the presence of symbolic links"))
2272 ;; If possible, keep the master file in the same directory.
2274 (if (and f
(string= (file-name-directory (expand-file-name f
))
2278 ;; If not, just use the first possible place.
2281 (or (not (setq dir
(file-name-directory f
)))
2282 (file-directory-p dir
))
2285 (error "New file lacks a version control directory")))))
2288 (defun vc-rename-file (old new
)
2289 "Rename file OLD to NEW, and rename its master file likewise."
2290 (interactive "fVC rename file: \nFRename to: ")
2291 ;; There are several ways of renaming files under CVS 1.3, but they all
2292 ;; have serious disadvantages. See the FAQ (available from think.com in
2293 ;; pub/cvs/). I'd rather send the user an error, than do something he might
2294 ;; consider to be wrong. When the famous, long-awaited rename database is
2295 ;; implemented things might change for the better. This is unlikely to occur
2296 ;; until CVS 2.0 is released. --ceder 1994-01-23 21:27:51
2297 (let ((oldbuf (get-file-buffer old
))
2298 (backend (vc-backend old
)))
2299 (unless (or (null backend
) (vc-find-backend-function backend
'rename-file
))
2300 (error "Renaming files under %s is not supported in VC" backend
))
2301 (if (and oldbuf
(buffer-modified-p oldbuf
))
2302 (error "Please save files before moving them"))
2303 (if (get-file-buffer new
)
2304 (error "Already editing new file name"))
2305 (if (file-exists-p new
)
2306 (error "New file already exists"))
2308 (if (and backend
(not (vc-up-to-date-p old
)))
2309 (error "Please check in files before moving them"))
2310 (vc-call-backend backend
'rename-file old new
))
2311 ;; Move the actual file (unless the backend did it already)
2312 (if (or (not backend
) (file-exists-p old
))
2313 (rename-file old new
))
2314 ;; ?? Renaming a file might change its contents due to keyword expansion.
2315 ;; We should really check out a new copy if the old copy was precisely equal
2316 ;; to some checked in version. However, testing for this is tricky....
2318 (with-current-buffer oldbuf
2319 (let ((buffer-read-only buffer-read-only
))
2320 (set-visited-file-name new
))
2323 (set-buffer-modified-p nil
)))))
2325 ;; Only defined in very recent Emacsen
2326 (defvar small-temporary-file-directory nil
)
2329 (defun vc-update-change-log (&rest args
)
2330 "Find change log file and add entries from recent version control logs.
2331 Normally, find log entries for all registered files in the default
2334 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2336 With any numeric prefix arg, find log entries for all currently visited
2337 files that are under version control. This puts all the entries in the
2338 log for the default directory, which may not be appropriate.
2340 From a program, any ARGS are assumed to be filenames for which
2341 log entries should be gathered."
2343 (cond ((consp current-prefix-arg
) ;C-u
2344 (list buffer-file-name
))
2345 (current-prefix-arg ;Numeric argument.
2347 (buffers (buffer-list))
2350 (setq file
(buffer-file-name (car buffers
)))
2351 (and file
(vc-backend file
)
2352 (setq files
(cons file files
)))
2353 (setq buffers
(cdr buffers
)))
2356 ;; Don't supply any filenames to backend; this means
2357 ;; it should find all relevant files relative to
2358 ;; the default-directory.
2360 (vc-call-backend (vc-responsible-backend default-directory
)
2361 'update-changelog args
))
2363 (defun vc-default-update-changelog (backend files
)
2364 "Default implementation of update-changelog.
2365 Uses `rcs2log' which only works for RCS and CVS."
2366 ;; FIXME: We (c|sh)ould add support for cvs2cl
2367 (let ((odefault default-directory
)
2368 (changelog (find-change-log))
2369 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
2371 (if (fboundp 'make-temp-file
) 'make-temp-file
'make-temp-name
)
2372 (expand-file-name "vc"
2373 (or small-temporary-file-directory
2374 temporary-file-directory
))))
2375 (full-name (or add-log-full-name
2378 (format "uid%d" (number-to-string (user-uid)))))
2379 (mailing-address (or add-log-mailing-address
2380 user-mail-address
)))
2381 (find-file-other-window changelog
)
2382 (barf-if-buffer-read-only)
2385 (goto-char (point-min))
2387 (message "Computing change log entries...")
2388 (message "Computing change log entries... %s"
2391 (setq default-directory odefault
)
2392 (if (eq 0 (apply 'call-process
2393 (expand-file-name "rcs2log"
2395 nil
(list t tempfile
) nil
2397 "-u" (concat (vc-user-login-name)
2399 "\t" mailing-address
)
2403 (if (file-name-absolute-p f
)
2405 (concat odefault f
))))
2409 (set-buffer (get-buffer-create "*vc*")))
2411 (insert-file tempfile
)
2413 (setq default-directory
(file-name-directory changelog
))
2414 (delete-file tempfile
)))))
2416 ;;; Annotate functionality
2418 ;; Declare globally instead of additional parameter to
2419 ;; temp-buffer-show-function (not possible to pass more than one
2421 (defvar vc-annotate-ratio nil
"Global variable.")
2422 (defvar vc-annotate-backend nil
"Global variable.")
2424 (defun vc-annotate-get-backend (buffer)
2425 "Return the backend matching \"Annotate\" buffer BUFFER.
2426 Return NIL if no match made. Associations are made based on
2427 `vc-annotate-buffers'."
2428 (cdr (assoc buffer vc-annotate-buffers
)))
2430 (define-derived-mode vc-annotate-mode fundamental-mode
"Annotate"
2431 "Major mode for buffers displaying output from the `annotate' command.
2433 You can use the mode-specific menu to alter the time-span of the used
2434 colors. See variable `vc-annotate-menu-elements' for customizing the
2436 (vc-annotate-add-menu))
2438 (defun vc-annotate-display-default (&optional event
)
2439 "Use the default color spectrum for VC Annotate mode."
2441 (message "Redisplaying annotation...")
2442 (vc-annotate-display (current-buffer)
2444 (vc-annotate-get-backend (current-buffer)))
2445 (message "Redisplaying annotation...done"))
2447 (defun vc-annotate-add-menu ()
2448 "Add the menu 'Annotate' to the menu bar in VC-Annotate mode."
2449 (setq vc-annotate-mode-menu
(make-sparse-keymap "Annotate"))
2450 (define-key vc-annotate-mode-map
[menu-bar vc-annotate-mode
]
2451 (cons "VC-Annotate" vc-annotate-mode-menu
))
2452 (define-key vc-annotate-mode-menu
[default]
2453 '("Default" . vc-annotate-display-default))
2454 (let ((menu-elements vc-annotate-menu-elements))
2455 (while menu-elements
2456 (let* ((element (car menu-elements))
2457 (days (round (* element
2458 (vc-annotate-car-last-cons vc-annotate-color-map)
2460 (setq menu-elements (cdr menu-elements))
2461 (define-key vc-annotate-mode-menu
2463 (cons (format "Span %d days"
2466 ,(format "Use colors spanning %d days" days)
2468 (message "Redisplaying annotation...")
2469 (vc-annotate-display
2470 (get-buffer (buffer-name))
2471 (vc-annotate-time-span vc-annotate-color-map ,element)
2472 (vc-annotate-get-backend (current-buffer)))
2473 (message "Redisplaying annotation...done"))))))))
2476 ;;;; (defun vc-BACKEND-annotate-command (file buffer) ...)
2477 ;;;; Execute "annotate" on FILE by using `call-process' and insert
2478 ;;;; the contents in BUFFER.
2481 (defun vc-annotate (ratio)
2482 "Display the result of the \"Annotate\" command using colors.
2483 \"Annotate\" is defined by `vc-BACKEND-annotate-command'. New lines
2484 are displayed in red, old in blue. A prefix argument specifies a
2485 factor for stretching the time scale.
2487 `vc-annotate-menu-elements' customizes the menu elements of the
2488 mode-specific menu. `vc-annotate-color-map' and
2489 `vc-annotate-very-old-color' defines the mapping of time to
2490 colors. `vc-annotate-background' specifies the background color."
2492 (vc-ensure-vc-buffer)
2493 (message "Annotating...")
2494 (let ((temp-buffer-name (concat "*Annotate " (buffer-name) "*"))
2495 (temp-buffer-show-function 'vc-annotate-display)
2496 (vc-annotate-ratio ratio)
2497 (vc-annotate-backend (vc-backend (buffer-file-name))))
2498 (if (not (vc-find-backend-function vc-annotate-backend 'annotate-command))
2499 (error "Sorry, annotating is not implemented for %s"
2500 vc-annotate-backend))
2501 (with-output-to-temp-buffer temp-buffer-name
2502 (vc-call-backend vc-annotate-backend 'annotate-command
2503 (file-name-nondirectory (buffer-file-name))
2504 (get-buffer temp-buffer-name)))
2505 ;; Don't use the temp-buffer-name until the buffer is created
2506 ;; (only after `with-output-to-temp-buffer'.)
2507 (setq vc-annotate-buffers
2508 (append vc-annotate-buffers
2509 (list (cons (get-buffer temp-buffer-name) vc-annotate-backend)))))
2510 (message "Annotating... done"))
2513 (defun vc-annotate-car-last-cons (a-list)
2514 "Return car of last cons in association list A-LIST."
2515 (if (not (eq nil (cdr a-list)))
2516 (vc-annotate-car-last-cons (cdr a-list))
2517 (car (car a-list))))
2519 (defun vc-annotate-time-span (a-list span &optional quantize)
2520 "Apply factor SPAN to the time-span of association list A-LIST.
2521 Return the new alist.
2522 Optionally quantize to the factor of QUANTIZE."
2523 ;; Apply span to each car of every cons
2524 (if (not (eq nil a-list))
2525 (append (list (cons (* (car (car a-list)) span)
2526 (cdr (car a-list))))
2527 (vc-annotate-time-span (nthcdr (or quantize ; optional
2529 a-list) span quantize))))
2531 (defun vc-annotate-compcar (threshold a-list)
2532 "Test successive cons cells of association list A-LIST against THRESHOLD.
2533 Return the first cons cell which car is not less than THRESHOLD,
2536 (tmp-cons (car a-list)))
2537 (while (and tmp-cons (< (car tmp-cons) threshold))
2538 (setq tmp-cons (car (nthcdr i a-list)))
2540 tmp-cons)) ; Return the appropriate value
2543 ;;;; (defun vc-BACKEND-annotate-difference (point) ...)
2545 ;;;; Return the difference between the age of the line at point and
2546 ;;;; the current time. Return NIL if there is no more comparison to
2547 ;;;; be made in the buffer. Return value as defined for
2548 ;;;; `current-time'. You can safely assume that point is placed at
2549 ;;;; the beginning of each line, starting at `point-min'. The buffer
2550 ;;;; that point is placed in is the Annotate output, as defined by
2551 ;;;; the relevant backend.
2553 (defun vc-annotate-display (buffer &optional color-map backend)
2554 "Do the VC-Annotate display in BUFFER using COLOR-MAP.
2555 The original annotating file is supposed to be handled by BACKEND.
2556 If BACKEND is NIL, variable VC-ANNOTATE-BACKEND is used instead.
2557 This function is destructive on VC-ANNOTATE-BACKEND when BACKEND is non-nil."
2559 ;; Handle the case of the global variable vc-annotate-ratio being
2560 ;; set. This variable is used to pass information from function
2561 ;; vc-annotate since it is not possible to use another parameter
2562 ;; (see temp-buffer-show-function).
2563 (if (and (not color-map) vc-annotate-ratio)
2564 ;; This will only be true if called from vc-annotate with ratio
2566 (setq color-map (vc-annotate-time-span vc-annotate-color-map
2567 vc-annotate-ratio)))
2569 (display-buffer buffer)
2570 (if (not vc-annotate-mode) ; Turn on vc-annotate-mode if not done
2572 (goto-char (point-min)) ; Position at the top of the buffer.
2573 ;; Delete old overlays
2576 (if (overlay-get overlay 'vc-annotation)
2577 (delete-overlay overlay)))
2578 (overlays-in (point-min) (point-max)))
2579 (goto-char (point-min)) ; Position at the top of the buffer.
2581 (if backend (setq vc-annotate-backend backend)) ; Destructive on `vc-annotate-backend'
2583 (let ((difference (vc-call-backend vc-annotate-backend 'annotate-difference (point))))
2586 ((color (or (vc-annotate-compcar
2587 difference (or color-map vc-annotate-color-map))
2588 (cons nil vc-annotate-very-old-color)))
2589 ;; substring from index 1 to remove any leading `#' in the name
2590 (face-name (concat "vc-annotate-face-" (substring (cdr color) 1)))
2591 ;; Make the face if not done.
2592 (face (or (intern-soft face-name)
2593 (let ((tmp-face (make-face (intern face-name))))
2594 (set-face-foreground tmp-face (cdr color))
2595 (if vc-annotate-background
2596 (set-face-background tmp-face vc-annotate-background))
2597 tmp-face))) ; Return the face
2601 (setq overlay (make-overlay point (point)))
2602 (overlay-put overlay 'face face)
2603 (overlay-put overlay 'vc-annotation t))
2604 (setq difference (vc-call-backend vc-annotate-backend 'annotate-difference (point))))))
2607 ;; Collect back-end-dependent stuff here
2609 (defalias 'vc-default-logentry-check 'ignore)
2611 (defun vc-check-headers ()
2612 "Check if the current file has any headers in it."
2614 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
2616 ;; Back-end-dependent stuff ends here.
2618 ;; Set up key bindings for use while editing log messages
2620 (define-derived-mode vc-log-mode text-mode "VC-Log"
2621 "Major mode for editing VC log entries.
2622 These bindings are added to the global keymap when you enter this mode:
2623 \\[vc-next-action] perform next logical version-control operation on current file
2624 \\[vc-register] register current file
2625 \\[vc-toggle-read-only] like next-action, but won't register files
2626 \\[vc-insert-headers] insert version-control headers in current file
2627 \\[vc-print-log] display change history of current file
2628 \\[vc-revert-buffer] revert buffer to latest version
2629 \\[vc-cancel-version] undo latest checkin
2630 \\[vc-diff] show diffs between file versions
2631 \\[vc-version-other-window] visit old version in another window
2632 \\[vc-directory] show all files locked by any user in or below .
2633 \\[vc-annotate] colorful display of the cvs annotate command
2634 \\[vc-update-change-log] add change log entry from recent checkins
2636 While you are entering a change log message for a version, the following
2637 additional bindings will be in effect.
2639 \\[vc-finish-logentry] proceed with check in, ending log message entry
2641 Whenever you do a checkin, your log comment is added to a ring of
2642 saved comments. These can be recalled as follows:
2644 \\[vc-next-comment] replace region with next message in comment ring
2645 \\[vc-previous-comment] replace region with previous message in comment ring
2646 \\[vc-comment-search-reverse] search backward for regexp in the comment ring
2647 \\[vc-comment-search-forward] search backward for regexp in the comment ring
2649 Entry to the change-log submode calls the value of `text-mode-hook', then
2650 the value of `vc-log-mode-hook'.
2652 Global user options:
2653 `vc-initial-comment' If non-nil, require user to enter a change
2654 comment upon first checkin of the file.
2656 `vc-keep-workfiles' Non-nil value prevents workfiles from being
2657 deleted when changes are checked in
2659 `vc-suppress-confirm' Suppresses some confirmation prompts,
2660 notably for reversions.
2662 vc-BACKEND-header Which keywords to insert when adding headers
2663 with \\[vc-insert-headers]. Defaults to
2664 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under
2667 `vc-static-header-alist' By default, version headers inserted in C files
2668 get stuffed in a static string area so that
2669 ident(RCS/CVS) or what(SCCS) can see them in
2670 the compiled object code. You can override
2671 this by setting this variable to nil, or change
2672 the header template by changing it.
2674 `vc-command-messages' if non-nil, display run messages from the
2675 actual version-control utilities (this is
2676 intended primarily for people hacking vc
2678 (make-local-variable 'vc-comment-ring-index))
2680 (defun vc-log-edit (file)
2681 "Set up `log-edit' for use with VC on FILE.
2682 If `log-edit' is not available, resort to `vc-log-mode'."
2683 (setq default-directory
2684 (if file (file-name-directory file)
2685 (with-current-buffer vc-parent-buffer default-directory)))
2686 (if (fboundp 'log-edit)
2687 (log-edit 'vc-finish-logentry nil
2688 (if file `(lambda () ',(list (file-name-nondirectory file)))
2689 ;; If FILE is nil, we were called from vc-dired.
2691 (with-current-buffer vc-parent-buffer
2692 (dired-get-marked-files t)))))
2694 (set (make-local-variable 'vc-log-file) file)
2695 (make-local-variable 'vc-log-version)
2696 (set-buffer-modified-p nil)
2697 (setq buffer-file-name nil))
2699 ;;; These things should probably be generally available
2701 (defun vc-file-tree-walk (dirname func &rest args)
2702 "Walk recursively through DIRNAME.
2703 Invoke FUNC f ARGS on each VC-managed file f underneath it."
2704 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
2705 (message "Traversing directory %s...done" dirname))
2707 (defun vc-file-tree-walk-internal (file func args)
2708 (if (not (file-directory-p file))
2709 (if (vc-backend file) (apply func file args))
2710 (message "Traversing directory %s..." (abbreviate-file-name file))
2711 (let ((dir (file-name-as-directory file)))
2714 (string-equal f ".")
2715 (string-equal f "..")
2716 (member f vc-directory-exclusion-list)
2717 (let ((dirf (expand-file-name f dir)))
2719 (file-symlink-p dirf);; Avoid possible loops
2720 (vc-file-tree-walk-internal dirf func args)))))
2721 (directory-files dir)))))
2725 ;;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
2727 ;;; These may be useful to anyone who has to debug or extend the package.
2728 ;;; (Note that this information corresponds to versions 5.x. Some of it
2729 ;;; might have been invalidated by the additions to support branching
2730 ;;; and RCS keyword lookup. AS, 1995/03/24)
2732 ;;; A fundamental problem in VC is that there are time windows between
2733 ;;; vc-next-action's computations of the file's version-control state and
2734 ;;; the actions that change it. This is a window open to lossage in a
2735 ;;; multi-user environment; someone else could nip in and change the state
2736 ;;; of the master during it.
2738 ;;; The performance problem is that rlog/prs calls are very expensive; we want
2739 ;;; to avoid them as much as possible.
2743 ;;; The performance problem, it turns out, simplifies in practice to the
2744 ;;; problem of making vc-state fast. The two other functions that call
2745 ;;; prs/rlog will not be so commonly used that the slowdown is a problem; one
2746 ;;; makes snapshots, the other deletes the calling user's last change in the
2749 ;;; The race condition implies that we have to either (a) lock the master
2750 ;;; during the entire execution of vc-next-action, or (b) detect and
2751 ;;; recover from errors resulting from dispatch on an out-of-date state.
2753 ;;; Alternative (a) appears to be infeasible. The problem is that we can't
2754 ;;; guarantee that the lock will ever be removed. Suppose a user starts a
2755 ;;; checkin, the change message buffer pops up, and the user, having wandered
2756 ;;; off to do something else, simply forgets about it?
2758 ;;; Alternative (b), on the other hand, works well with a cheap way to speed up
2759 ;;; vc-state. Usually, if a file is registered, we can read its locked/
2760 ;;; unlocked state and its current owner from its permissions.
2762 ;;; This shortcut will fail if someone has manually changed the workfile's
2763 ;;; permissions; also if developers are munging the workfile in several
2764 ;;; directories, with symlinks to a master (in this latter case, the
2765 ;;; permissions shortcut will fail to detect a lock asserted from another
2768 ;;; Note that these cases correspond exactly to the errors which could happen
2769 ;;; because of a competing checkin/checkout race in between two instances of
2772 ;;; For VC's purposes, a workfile/master pair may have the following states:
2774 ;;; A. Unregistered. There is a workfile, there is no master.
2776 ;;; B. Registered and not locked by anyone.
2778 ;;; C. Locked by calling user and unchanged.
2780 ;;; D. Locked by the calling user and changed.
2782 ;;; E. Locked by someone other than the calling user.
2784 ;;; This makes for 25 states and 20 error conditions. Here's the matrix:
2786 ;;; VC's idea of state
2788 ;;; V Actual state RCS action SCCS action Effect
2790 ;;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
2791 ;;; B 5 . 6 7 8 co -l get -e checkout
2792 ;;; C 9 10 . 11 12 co -u unget; get revert
2793 ;;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
2794 ;;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
2796 ;;; All commands take the master file name as a last argument (not shown).
2798 ;;; In the discussion below, a "self-race" is a pathological situation in
2799 ;;; which VC operations are being attempted simultaneously by two or more
2800 ;;; Emacsen running under the same username.
2802 ;;; The vc-next-action code has the following windows:
2805 ;;; Between the check for existence of a master file and the call to
2806 ;;; admin/checkin in vc-buffer-admin (apparent state A). This window may
2807 ;;; never close if the initial-comment feature is on.
2810 ;;; Between the call to vc-workfile-unchanged-p in and the immediately
2811 ;;; following revert (apparent state C).
2814 ;;; Between the call to vc-workfile-unchanged-p in and the following
2815 ;;; checkin (apparent state D). This window may never close.
2818 ;;; Between the unlock and the immediately following checkout during a
2819 ;;; revert operation (apparent state C). Included in window Q.
2822 ;;; Between vc-state and the following checkout (apparent state B).
2825 ;;; Between vc-state and the following revert (apparent state C).
2826 ;;; Includes windows Q and S.
2829 ;;; Between vc-state and the following checkin (apparent state
2830 ;;; D). This window may never be closed if the user fails to complete the
2831 ;;; checkin message. Includes window R.
2834 ;;; Between vc-state and the following steal-lock (apparent
2835 ;;; state E). This window may never close if the user fails to complete
2836 ;;; the steal-lock message. Includes window X.
2839 ;;; Between the unlock and the immediately following re-lock during a
2840 ;;; steal-lock operation (apparent state E). This window may never close
2841 ;;; if the user fails to complete the steal-lock message.
2845 ;;; Apparent state A ---
2847 ;;; 1. File looked unregistered but is actually registered and not locked.
2849 ;;; Potential cause: someone else's admin during window P, with
2850 ;;; caller's admin happening before their checkout.
2852 ;;; RCS: Prior to version 5.6.4, ci fails with message
2853 ;;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
2854 ;;; ci -i option and the message is "<file>,v: already exists".
2855 ;;; SCCS: admin will fail with error (ad19).
2857 ;;; We can let these errors be passed up to the user.
2859 ;;; 2. File looked unregistered but is actually locked by caller, unchanged.
2861 ;;; Potential cause: self-race during window P.
2863 ;;; RCS: Prior to version 5.6.4, reverts the file to the last saved
2864 ;;; version and unlocks it. From 5.6.4 onwards, VC uses the new
2865 ;;; ci -i option, failing with message "<file>,v: already exists".
2866 ;;; SCCS: will fail with error (ad19).
2868 ;;; Either of these consequences is acceptable.
2870 ;;; 3. File looked unregistered but is actually locked by caller, changed.
2872 ;;; Potential cause: self-race during window P.
2874 ;;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
2875 ;;; a delta with a null change comment (the -t- switch will be
2876 ;;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
2877 ;;; failing with message "<file>,v: already exists".
2878 ;;; SCCS: will fail with error (ad19).
2880 ;;; 4. File looked unregistered but is locked by someone else.
2882 ;;; Potential cause: someone else's admin during window P, with
2883 ;;; caller's admin happening *after* their checkout.
2885 ;;; RCS: Prior to version 5.6.4, ci fails with a
2886 ;;; "no lock set by <user>" message. From 5.6.4 onwards,
2887 ;;; VC uses the new ci -i option, failing with message
2888 ;;; "<file>,v: already exists".
2889 ;;; SCCS: will fail with error (ad19).
2891 ;;; We can let these errors be passed up to the user.
2893 ;;; Apparent state B ---
2895 ;;; 5. File looked registered and not locked, but is actually unregistered.
2897 ;;; Potential cause: master file got nuked during window P.
2899 ;;; RCS: will fail with "RCS/<file>: No such file or directory"
2900 ;;; SCCS: will fail with error ut4.
2902 ;;; We can let these errors be passed up to the user.
2904 ;;; 6. File looked registered and not locked, but is actually locked by the
2905 ;;; calling user and unchanged.
2907 ;;; Potential cause: self-race during window T.
2909 ;;; RCS: in the same directory as the previous workfile, co -l will fail
2910 ;;; with "co error: writable foo exists; checkout aborted". In any other
2911 ;;; directory, checkout will succeed.
2912 ;;; SCCS: will fail with ge17.
2914 ;;; Either of these consequences is acceptable.
2916 ;;; 7. File looked registered and not locked, but is actually locked by the
2917 ;;; calling user and changed.
2921 ;;; 8. File looked registered and not locked, but is actually locked by another
2924 ;;; Potential cause: someone else checks it out during window T.
2926 ;;; RCS: co error: revision 1.3 already locked by <user>
2927 ;;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
2929 ;;; We can let these errors be passed up to the user.
2931 ;;; Apparent state C ---
2933 ;;; 9. File looks locked by calling user and unchanged, but is unregistered.
2937 ;;; 10. File looks locked by calling user and unchanged, but is actually not
2940 ;;; Potential cause: a self-race in window U, or by the revert's
2941 ;;; landing during window X of some other user's steal-lock or window S
2942 ;;; of another user's revert.
2944 ;;; RCS: succeeds, refreshing the file from the identical version in
2946 ;;; SCCS: fails with error ut4 (p file nonexistent).
2948 ;;; Either of these consequences is acceptable.
2950 ;;; 11. File is locked by calling user. It looks unchanged, but is actually
2953 ;;; Potential cause: the file would have to be touched by a self-race
2954 ;;; during window Q.
2956 ;;; The revert will succeed, removing whatever changes came with
2957 ;;; the touch. It is theoretically possible that work could be lost.
2959 ;;; 12. File looks like it's locked by the calling user and unchanged, but
2960 ;;; it's actually locked by someone else.
2962 ;;; Potential cause: a steal-lock in window V.
2964 ;;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
2965 ;;; SCCS: fails with error un2
2967 ;;; We can pass these errors up to the user.
2969 ;;; Apparent state D ---
2971 ;;; 13. File looks like it's locked by the calling user and changed, but it's
2972 ;;; actually unregistered.
2974 ;;; Potential cause: master file got nuked during window P.
2976 ;;; RCS: Prior to version 5.6.4, checks in the user's version as an
2977 ;;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
2978 ;;; option, failing with message "no such file or directory".
2979 ;;; SCCS: will fail with error ut4.
2981 ;;; This case is kind of nasty. Under RCS prior to version 5.6.4,
2982 ;;; VC may fail to detect the loss of previous version information.
2984 ;;; 14. File looks like it's locked by the calling user and changed, but it's
2985 ;;; actually unlocked.
2987 ;;; Potential cause: self-race in window V, or the checkin happening
2988 ;;; during the window X of someone else's steal-lock or window S of
2989 ;;; someone else's revert.
2991 ;;; RCS: ci will fail with "no lock set by <user>".
2992 ;;; SCCS: delta will fail with error ut4.
2994 ;;; 15. File looks like it's locked by the calling user and changed, but it's
2995 ;;; actually locked by the calling user and unchanged.
2997 ;;; Potential cause: another self-race --- a whole checkin/checkout
2998 ;;; sequence by the calling user would have to land in window R.
3000 ;;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
3001 ;;; RCS: reverts to the file state as of the second user's checkin, leaving
3002 ;;; the file unlocked.
3004 ;;; It is theoretically possible that work could be lost under RCS.
3006 ;;; 16. File looks like it's locked by the calling user and changed, but it's
3007 ;;; actually locked by a different user.
3009 ;;; RCS: ci error: no lock set by <user>
3010 ;;; SCCS: unget will fail with error un2
3012 ;;; We can pass these errors up to the user.
3014 ;;; Apparent state E ---
3016 ;;; 17. File looks like it's locked by some other user, but it's actually
3021 ;;; 18. File looks like it's locked by some other user, but it's actually
3024 ;;; Potential cause: someone released a lock during window W.
3026 ;;; RCS: The calling user will get the lock on the file.
3027 ;;; SCCS: unget -n will fail with cm4.
3029 ;;; Either of these consequences will be OK.
3031 ;;; 19. File looks like it's locked by some other user, but it's actually
3032 ;;; locked by the calling user and unchanged.
3034 ;;; Potential cause: the other user relinquishing a lock followed by
3035 ;;; a self-race, both in window W.
3037 ;;; Under both RCS and SCCS, both unlock and lock will succeed, making
3038 ;;; the sequence a no-op.
3040 ;;; 20. File looks like it's locked by some other user, but it's actually
3041 ;;; locked by the calling user and changed.
3047 ;;; In order of decreasing severity:
3049 ;;; Cases 11 and 15 are the only ones that potentially lose work.
3050 ;;; They would require a self-race for this to happen.
3052 ;;; Case 13 in RCS loses information about previous deltas, retaining
3053 ;;; only the information in the current workfile. This can only happen
3054 ;;; if the master file gets nuked in window P.
3056 ;;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
3057 ;;; no change comment in the master. This would require a self-race in
3058 ;;; window P or R respectively.
3060 ;;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
3062 ;;; Unfortunately, it appears to me that no recovery is possible in these
3063 ;;; cases. They don't yield error messages, so there's no way to tell that
3064 ;;; a race condition has occurred.
3066 ;;; All other cases don't change either the workfile or the master, and
3067 ;;; trigger command errors which the user will see.
3069 ;;; Thus, there is no explicit recovery code.