* net/trampver.el: Update release number.
[emacs.git] / lisp / vc-dispatcher.el
blobd0648570bec3a546a61e528ff7fe0e156661a9f1
1 ;;; vc-dispatcher.el -- generic command-dispatcher facility.
3 ;; Copyright (C) 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: FSF (see below for full credits)
7 ;; Maintainer: Eric S. Raymond <esr@thyrsus.com>
8 ;; Keywords: tools
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Credits:
27 ;; Designed and implemented by Eric S. Raymond, originally as part of VC mode.
28 ;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the
29 ;; vc-dir front end.
31 ;;; Commentary:
33 ;; Goals:
35 ;; There is a class of front-ending problems that Emacs might be used
36 ;; to address that involves selecting sets of files, or possibly
37 ;; directories, and passing the selection set to slave commands. The
38 ;; prototypical example, from which this code is derived, is talking
39 ;; to version-control systems.
41 ;; vc-dispatcher.el is written to decouple the UI issues in such front
42 ;; ends from their application-specific logic. It also provides a
43 ;; service layer for running the slave commands either synchronously
44 ;; or asynchronously and managing the message/error logs from the
45 ;; command runs.
47 ;; Similar UI problems can be expected to come up in applications
48 ;; areas other than VCSes; IDEs and document search are two obvious ones.
49 ;; This mode is intended to ensure that the Emacs interfaces for all such
50 ;; beasts are consistent and carefully designed. But even if nothing
51 ;; but VC ever uses it, getting the layer separation right will be
52 ;; a valuable thing.
54 ;; Dispatcher's universe:
56 ;; The universe consists of the file tree rooted at the current
57 ;; directory. The dispatcher's upper layer deduces some subset
58 ;; of the file tree from the state of the currently visited buffer
59 ;; and returns that subset, presumably to a client mode.
61 ;; The user may be looking at either of two different views; a buffer
62 ;; visiting a file, or a directory buffer generated by vc-dispatcher.
64 ;; The lower layer of this mode runs commands in subprocesses, either
65 ;; synchronously or asynchronously. Commands may be launched in one
66 ;; of two ways: they may be run immediately, or the calling mode can
67 ;; create a closure associated with a text-entry buffer, to be
68 ;; executed when the user types C-c to ship the buffer contents. In
69 ;; either case the command messages and error (if any) will remain
70 ;; available in a status buffer.
72 ;; Special behavior of dispatcher directory buffers:
74 ;; In dispatcher directory buffers, facilities to perform basic
75 ;; navigation and selection operations are provided by keymap and menu
76 ;; entries that dispatcher sets up itself, so they'll be uniform
77 ;; across all dispatcher-using client modes. Client modes are
78 ;; expected to append to these to provide mode-specific bindings.
80 ;; The standard map associates a 'state' slot (that the client mode
81 ;; may set) with each directory entry. The dispatcher knows nothing
82 ;; about the semantics of individual states, but mark and unmark commands
83 ;; treat all entries with the same state as the currently selected one as
84 ;; a unit.
86 ;; The interface:
88 ;; The main interface to the lower level is vc-do-command. This launches a
89 ;; command, synchronously or asynchronously, making the output available
90 ;; in a command log buffer. Two other functions, (vc-start-logentry) and
91 ;; (vc-finish-logentry), allow you to associate a command closure with an
92 ;; annotation buffer so that when the user confirms the comment the closure
93 ;; is run (with the comment as part of its context).
95 ;; The interface to the upper level has the two main entry points (vc-dir)
96 ;; and (vc-dispatcher-selection-set) and a couple of convenience functions.
97 ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set)
98 ;; returns a selection set of files, either the marked files in a browsing
99 ;; buffer or the singleton set consisting of the file visited by the current
100 ;; buffer (when that is appropriate). It also does what is needed to ensure
101 ;; that on-disk files and the contents of their visiting Emacs buffers
102 ;; coincide.
104 ;; When the client mode adds a local vc-mode-line-hook to a buffer, it
105 ;; will be called with the buffer file name as argument whenever the
106 ;; dispatcher resynchs the buffer.
108 ;; To do:
110 ;; - log buffers need font-locking.
113 ;; General customization
114 (defcustom vc-logentry-check-hook nil
115 "Normal hook run by `vc-finish-logentry'.
116 Use this to impose your own rules on the entry in addition to any the
117 dispatcher client mode imposes itself."
118 :type 'hook
119 :group 'vc)
121 (defcustom vc-delete-logbuf-window t
122 "If non-nil, delete the log buffer and window after each logical action.
123 If nil, bury that buffer instead.
124 This is most useful if you have multiple windows on a frame and would like to
125 preserve the setting."
126 :type 'boolean
127 :group 'vc)
129 (defcustom vc-command-messages nil
130 "If non-nil, display run messages from back-end commands."
131 :type 'boolean
132 :group 'vc)
134 (defcustom vc-suppress-confirm nil
135 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
136 :type 'boolean
137 :group 'vc)
139 ;; Variables the user doesn't need to know about.
141 (defvar vc-log-operation nil)
142 (defvar vc-log-after-operation-hook nil)
143 (defvar vc-log-fileset)
145 ;; In a log entry buffer, this is a local variable
146 ;; that points to the buffer for which it was made
147 ;; (either a file, or a directory buffer).
148 (defvar vc-parent-buffer nil)
149 (put 'vc-parent-buffer 'permanent-local t)
150 (defvar vc-parent-buffer-name nil)
151 (put 'vc-parent-buffer-name 'permanent-local t)
153 ;; Common command execution logic
155 (defun vc-process-filter (p s)
156 "An alternative output filter for async process P.
157 One difference with the default filter is that this inserts S after markers.
158 Another is that undo information is not kept."
159 (let ((buffer (process-buffer p)))
160 (when (buffer-live-p buffer)
161 (with-current-buffer buffer
162 (save-excursion
163 (let ((buffer-undo-list t)
164 (inhibit-read-only t))
165 (goto-char (process-mark p))
166 (insert s)
167 (set-marker (process-mark p) (point))))))))
169 (defun vc-setup-buffer (buf)
170 "Prepare BUF for executing a slave command and make it current."
171 (let ((camefrom (current-buffer))
172 (olddir default-directory))
173 (set-buffer (get-buffer-create buf))
174 (kill-all-local-variables)
175 (set (make-local-variable 'vc-parent-buffer) camefrom)
176 (set (make-local-variable 'vc-parent-buffer-name)
177 (concat " from " (buffer-name camefrom)))
178 (setq default-directory olddir)
179 (let ((buffer-undo-list t)
180 (inhibit-read-only t))
181 (erase-buffer))))
183 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
185 (defun vc-process-sentinel (p s)
186 (let ((previous (process-get p 'vc-previous-sentinel))
187 (buf (process-buffer p)))
188 ;; Impatient users sometime kill "slow" buffers; check liveness
189 ;; to avoid "error in process sentinel: Selecting deleted buffer".
190 (when (buffer-live-p buf)
191 (when previous (funcall previous p s))
192 (with-current-buffer buf
193 (setq mode-line-process
194 (let ((status (process-status p)))
195 ;; Leave mode-line uncluttered, normally.
196 (unless (eq 'exit status)
197 (format " (%s)" status))))
198 (let (vc-sentinel-movepoint)
199 ;; Normally, we want async code such as sentinels to not move point.
200 (save-excursion
201 (goto-char (process-mark p))
202 (let ((cmds (process-get p 'vc-sentinel-commands)))
203 (process-put p 'vc-sentinel-commands nil)
204 (dolist (cmd cmds)
205 ;; Each sentinel may move point and the next one should be run
206 ;; at that new point. We could get the same result by having
207 ;; each sentinel read&set process-mark, but since `cmd' needs
208 ;; to work both for async and sync processes, this would be
209 ;; difficult to achieve.
210 (vc-exec-after cmd))))
211 ;; But sometimes the sentinels really want to move point.
212 (when vc-sentinel-movepoint
213 (let ((win (get-buffer-window (current-buffer) 0)))
214 (if (not win)
215 (goto-char vc-sentinel-movepoint)
216 (with-selected-window win
217 (goto-char vc-sentinel-movepoint))))))))))
219 (defun vc-set-mode-line-busy-indicator ()
220 (setq mode-line-process
221 (concat " " (propertize "[waiting...]"
222 'face 'mode-line-emphasis
223 'help-echo
224 "A command is in progress in this buffer"))))
226 (defun vc-exec-after (code)
227 "Eval CODE when the current buffer's process is done.
228 If the current buffer has no process, just evaluate CODE.
229 Else, add CODE to the process' sentinel."
230 (let ((proc (get-buffer-process (current-buffer))))
231 (cond
232 ;; If there's no background process, just execute the code.
233 ;; We used to explicitly call delete-process on exited processes,
234 ;; but this led to timing problems causing process output to be
235 ;; lost. Terminated processes get deleted automatically
236 ;; anyway. -- cyd
237 ((or (null proc) (eq (process-status proc) 'exit))
238 ;; Make sure we've read the process's output before going further.
239 (when proc (accept-process-output proc))
240 (eval code))
241 ;; If a process is running, add CODE to the sentinel
242 ((eq (process-status proc) 'run)
243 (vc-set-mode-line-busy-indicator)
244 (let ((previous (process-sentinel proc)))
245 (unless (eq previous 'vc-process-sentinel)
246 (process-put proc 'vc-previous-sentinel previous))
247 (set-process-sentinel proc 'vc-process-sentinel))
248 (process-put proc 'vc-sentinel-commands
249 ;; We keep the code fragments in the order given
250 ;; so that vc-diff-finish's message shows up in
251 ;; the presence of non-nil vc-command-messages.
252 (append (process-get proc 'vc-sentinel-commands)
253 (list code))))
254 (t (error "Unexpected process state"))))
255 nil)
257 (defvar vc-post-command-functions nil
258 "Hook run at the end of `vc-do-command'.
259 Each function is called inside the buffer in which the command was run
260 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
262 (defvar w32-quote-process-args)
264 (defun vc-delistify (filelist)
265 "Smash a FILELIST into a file list string suitable for info messages."
266 ;; FIXME what about file names with spaces?
267 (if (not filelist) "." (mapconcat 'identity filelist " ")))
269 ;;;###autoload
270 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
271 "Execute a slave command, notifying user and checking for errors.
272 Output from COMMAND goes to BUFFER, or the current buffer if
273 BUFFER is t. If the destination buffer is not already current,
274 set it up properly and erase it. The command is considered
275 successful if its exit status does not exceed OKSTATUS (if
276 OKSTATUS is nil, that means to ignore error status, if it is
277 `async', that means not to wait for termination of the
278 subprocess; if it is t it means to ignore all execution errors).
279 FILE-OR-LIST is the name of a working file; it may be a list of
280 files or be nil (to execute commands that don't expect a file
281 name or set of files). If an optional list of FLAGS is present,
282 that is inserted into the command line before the filename.
283 Return the return value of the slave command in the synchronous
284 case, and the process object in the asynchronous case."
285 ;; FIXME: file-relative-name can return a bogus result because
286 ;; it doesn't look at the actual file-system to see if symlinks
287 ;; come into play.
288 (let* ((files
289 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
290 (if (listp file-or-list) file-or-list (list file-or-list))))
291 (full-command
292 ;; What we're doing here is preparing a version of the command
293 ;; for display in a debug-progress message. If it's fewer than
294 ;; 20 characters display the entire command (without trailing
295 ;; newline). Otherwise display the first 20 followed by an ellipsis.
296 (concat (if (string= (substring command -1) "\n")
297 (substring command 0 -1)
298 command)
300 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
301 " " (vc-delistify files))))
302 (save-current-buffer
303 (unless (or (eq buffer t)
304 (and (stringp buffer)
305 (string= (buffer-name) buffer))
306 (eq buffer (current-buffer)))
307 (vc-setup-buffer buffer))
308 ;; If there's some previous async process still running, just kill it.
309 (let ((oldproc (get-buffer-process (current-buffer))))
310 ;; If we wanted to wait for oldproc to finish before doing
311 ;; something, we'd have used vc-eval-after.
312 ;; Use `delete-process' rather than `kill-process' because we don't
313 ;; want any of its output to appear from now on.
314 (when oldproc (delete-process oldproc)))
315 (let ((squeezed (remq nil flags))
316 (inhibit-read-only t)
317 (status 0))
318 (when files
319 (setq squeezed (nconc squeezed files)))
320 (let ((exec-path (append vc-path exec-path))
321 ;; Add vc-path to PATH for the execution of this command.
322 ;; Also, since some functions need to parse the output
323 ;; from external commands, set LC_MESSAGES to C.
324 (process-environment
325 (cons (concat "PATH=" (getenv "PATH")
326 path-separator
327 (mapconcat 'identity vc-path path-separator))
328 (cons "LC_MESSAGES=C"
329 process-environment)))
330 (w32-quote-process-args t))
331 (if (eq okstatus 'async)
332 ;; Run asynchronously.
333 (let ((proc
334 (let ((process-connection-type nil))
335 (apply 'start-file-process command (current-buffer)
336 command squeezed))))
337 (when vc-command-messages
338 (message "Running %s in background..." full-command))
339 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
340 (set-process-filter proc 'vc-process-filter)
341 (setq status proc)
342 (when vc-command-messages
343 (vc-exec-after
344 `(message "Running %s in background... done" ',full-command))))
345 ;; Run synchronously
346 (when vc-command-messages
347 (message "Running %s in foreground..." full-command))
348 (let ((buffer-undo-list t))
349 (setq status (apply 'process-file command nil t nil squeezed)))
350 (when (and (not (eq t okstatus))
351 (or (not (integerp status))
352 (and okstatus (< okstatus status))))
353 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
354 (pop-to-buffer (current-buffer))
355 (goto-char (point-min))
356 (shrink-window-if-larger-than-buffer))
357 (error "Running %s...FAILED (%s)" full-command
358 (if (integerp status) (format "status %d" status) status)))
359 (when vc-command-messages
360 (message "Running %s...OK = %d" full-command status))))
361 (vc-exec-after
362 `(run-hook-with-args 'vc-post-command-functions
363 ',command ',file-or-list ',flags))
364 status))))
366 ;; These functions are used to ensure that the view the user sees is up to date
367 ;; even if the dispatcher client mode has messed with file contents (as in,
368 ;; for example, VCS keyword expansion).
370 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
372 (defun vc-position-context (posn)
373 "Save a bit of the text around POSN in the current buffer.
374 Used to help us find the corresponding position again later
375 if markers are destroyed or corrupted."
376 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
377 ;; rcs.el mode.
378 (list posn
379 (buffer-size)
380 (buffer-substring posn
381 (min (point-max) (+ posn 100)))))
383 (defun vc-find-position-by-context (context)
384 "Return the position of CONTEXT in the current buffer.
385 If CONTEXT cannot be found, return nil."
386 (let ((context-string (nth 2 context)))
387 (if (equal "" context-string)
388 (point-max)
389 (save-excursion
390 (let ((diff (- (nth 1 context) (buffer-size))))
391 (when (< diff 0) (setq diff (- diff)))
392 (goto-char (nth 0 context))
393 (if (or (search-forward context-string nil t)
394 ;; Can't use search-backward since the match may continue
395 ;; after point.
396 (progn (goto-char (- (point) diff (length context-string)))
397 ;; goto-char doesn't signal an error at
398 ;; beginning of buffer like backward-char would
399 (search-forward context-string nil t)))
400 ;; to beginning of OSTRING
401 (- (point) (length context-string))))))))
403 (defun vc-context-matches-p (posn context)
404 "Return t if POSN matches CONTEXT, nil otherwise."
405 (let* ((context-string (nth 2 context))
406 (len (length context-string))
407 (end (+ posn len)))
408 (if (> end (1+ (buffer-size)))
410 (string= context-string (buffer-substring posn end)))))
412 (defun vc-buffer-context ()
413 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
414 Used by `vc-restore-buffer-context' to later restore the context."
415 (let ((point-context (vc-position-context (point)))
416 ;; Use mark-marker to avoid confusion in transient-mark-mode.
417 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
418 (vc-position-context (mark-marker))))
419 ;; Make the right thing happen in transient-mark-mode.
420 (mark-active nil))
421 (list point-context mark-context nil)))
423 (defun vc-restore-buffer-context (context)
424 "Restore point/mark, and reparse any affected compilation buffers.
425 CONTEXT is that which `vc-buffer-context' returns."
426 (let ((point-context (nth 0 context))
427 (mark-context (nth 1 context)))
428 ;; if necessary, restore point and mark
429 (if (not (vc-context-matches-p (point) point-context))
430 (let ((new-point (vc-find-position-by-context point-context)))
431 (when new-point (goto-char new-point))))
432 (and mark-active
433 mark-context
434 (not (vc-context-matches-p (mark) mark-context))
435 (let ((new-mark (vc-find-position-by-context mark-context)))
436 (when new-mark (set-mark new-mark))))))
438 (defun vc-revert-buffer-internal (&optional arg no-confirm)
439 "Revert buffer, keeping point and mark where user expects them.
440 Try to be clever in the face of changes due to expanded version-control
441 key words. This is important for typeahead to work as expected.
442 ARG and NO-CONFIRM are passed on to `revert-buffer'."
443 (interactive "P")
444 (widen)
445 (let ((context (vc-buffer-context)))
446 ;; Use save-excursion here, because it may be able to restore point
447 ;; and mark properly even in cases where vc-restore-buffer-context
448 ;; would fail. However, save-excursion might also get it wrong --
449 ;; in this case, vc-restore-buffer-context gives it a second try.
450 (save-excursion
451 ;; t means don't call normal-mode;
452 ;; that's to preserve various minor modes.
453 (revert-buffer arg no-confirm t))
454 (vc-restore-buffer-context context)))
456 (defvar vc-mode-line-hook nil)
457 (make-variable-buffer-local 'vc-mode-line-hook)
458 (put 'vc-mode-line-hook 'permanent-local t)
460 (defun vc-resynch-window (file &optional keep noquery reset-vc-info)
461 "If FILE is in the current buffer, either revert or unvisit it.
462 The choice between revert (to see expanded keywords) and unvisit
463 depends on KEEP. NOQUERY if non-nil inhibits confirmation for
464 reverting. NOQUERY should be t *only* if it is known the only
465 difference between the buffer and the file is due to
466 modifications by the dispatcher client code, rather than user
467 editing!"
468 (and (string= buffer-file-name file)
469 (if keep
470 (when (file-exists-p file)
471 (when reset-vc-info
472 (vc-file-clearprops file))
473 (vc-revert-buffer-internal t noquery)
475 ;; VC operations might toggle the read-only state. In
476 ;; that case we need to adjust the `view-mode' status
477 ;; when `view-read-only' is non-nil.
478 (and view-read-only
479 (if (file-writable-p file)
480 (and view-mode
481 (let ((view-old-buffer-read-only nil))
482 (view-mode-exit)))
483 (and (not view-mode)
484 (not (eq (get major-mode 'mode-class) 'special))
485 (view-mode-enter))))
487 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
488 (run-hook-with-args 'vc-mode-line-hook buffer-file-name))
489 (kill-buffer (current-buffer)))))
491 (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
492 (declare-function vc-string-prefix-p "vc" (prefix string))
494 (defun vc-resynch-buffers-in-directory (directory &optional keep noquery reset-vc-info)
495 "Resync all buffers that visit files in DIRECTORY."
496 (dolist (buffer (buffer-list))
497 (let ((fname (buffer-file-name buffer)))
498 (when (and fname (vc-string-prefix-p directory fname))
499 (with-current-buffer buffer
500 (vc-resynch-buffer fname keep noquery reset-vc-info))))))
502 (defun vc-resynch-buffer (file &optional keep noquery reset-vc-info)
503 "If FILE is currently visited, resynch its buffer."
504 (if (string= buffer-file-name file)
505 (vc-resynch-window file keep noquery reset-vc-info)
506 (if (file-directory-p file)
507 (vc-resynch-buffers-in-directory file keep noquery reset-vc-info)
508 (let ((buffer (get-file-buffer file)))
509 (when buffer
510 (with-current-buffer buffer
511 (vc-resynch-window file keep noquery reset-vc-info))))))
512 ;; Try to avoid unnecessary work, a *vc-dir* buffer is only present
513 ;; if this is true.
514 (when vc-dir-buffers
515 (vc-dir-resynch-file file)))
517 (defun vc-buffer-sync (&optional not-urgent)
518 "Make sure the current buffer and its working file are in sync.
519 NOT-URGENT means it is ok to continue if the user says not to save."
520 (when (buffer-modified-p)
521 (if (or vc-suppress-confirm
522 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
523 (save-buffer)
524 (unless not-urgent
525 (error "Aborted")))))
527 ;; Command closures
529 ;; Set up key bindings for use while editing log messages
531 (defun vc-log-edit (fileset mode)
532 "Set up `log-edit' for use on FILE."
533 (setq default-directory
534 (with-current-buffer vc-parent-buffer default-directory))
535 (log-edit 'vc-finish-logentry
537 `((log-edit-listfun . (lambda ()
538 ;; FIXME: Should expand the list
539 ;; for directories.
540 (mapcar 'file-relative-name
541 ',fileset)))
542 (log-edit-diff-function . (lambda () (vc-diff nil))))
544 mode)
545 (set (make-local-variable 'vc-log-fileset) fileset)
546 (set-buffer-modified-p nil)
547 (setq buffer-file-name nil))
549 (defun vc-start-logentry (files comment initial-contents msg logbuf mode action &optional after-hook)
550 "Accept a comment for an operation on FILES.
551 If COMMENT is nil, pop up a LOGBUF buffer, emit MSG, and set the
552 action on close to ACTION. If COMMENT is a string and
553 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
554 contents of the log entry buffer. If COMMENT is a string and
555 INITIAL-CONTENTS is nil, do action immediately as if the user had
556 entered COMMENT. If COMMENT is t, also do action immediately with an
557 empty comment. Remember the file's buffer in `vc-parent-buffer'
558 \(current one if no file). Puts the log-entry buffer in major-mode
559 MODE, defaulting to `log-edit-mode' if MODE is nil.
560 AFTER-HOOK specifies the local value for `vc-log-after-operation-hook'."
561 (let ((parent
562 (if (vc-dispatcher-browsing)
563 ;; If we are called from a directory browser, the parent buffer is
564 ;; the current buffer.
565 (current-buffer)
566 (if (and files (equal (length files) 1))
567 (get-file-buffer (car files))
568 (current-buffer)))))
569 (if (and comment (not initial-contents))
570 (set-buffer (get-buffer-create logbuf))
571 (pop-to-buffer (get-buffer-create logbuf)))
572 (set (make-local-variable 'vc-parent-buffer) parent)
573 (set (make-local-variable 'vc-parent-buffer-name)
574 (concat " from " (buffer-name vc-parent-buffer)))
575 (vc-log-edit files mode)
576 (make-local-variable 'vc-log-after-operation-hook)
577 (when after-hook
578 (setq vc-log-after-operation-hook after-hook))
579 (setq vc-log-operation action)
580 (when comment
581 (erase-buffer)
582 (when (stringp comment) (insert comment)))
583 (if (or (not comment) initial-contents)
584 (message "%s Type C-c C-c when done" msg)
585 (vc-finish-logentry (eq comment t)))))
587 (declare-function vc-dir-move-to-goal-column "vc-dir" ())
588 ;; vc-finish-logentry is typically called from a log-edit buffer (see
589 ;; vc-start-logentry).
590 (defun vc-finish-logentry (&optional nocomment)
591 "Complete the operation implied by the current log entry.
592 Use the contents of the current buffer as a check-in or registration
593 comment. If the optional arg NOCOMMENT is non-nil, then don't check
594 the buffer contents as a comment."
595 (interactive)
596 ;; Check and record the comment, if any.
597 (unless nocomment
598 (run-hooks 'vc-logentry-check-hook))
599 ;; Sync parent buffer in case the user modified it while editing the comment.
600 ;; But not if it is a vc-dir buffer.
601 (with-current-buffer vc-parent-buffer
602 (or (vc-dispatcher-browsing) (vc-buffer-sync)))
603 (unless vc-log-operation
604 (error "No log operation is pending"))
606 ;; save the parameters held in buffer-local variables
607 (let ((logbuf (current-buffer))
608 (log-operation vc-log-operation)
609 ;; FIXME: When coming from VC-Dir, we should check that the
610 ;; set of selected files is still equal to vc-log-fileset,
611 ;; to avoid surprises.
612 (log-fileset vc-log-fileset)
613 (log-entry (buffer-string))
614 (after-hook vc-log-after-operation-hook))
615 (pop-to-buffer vc-parent-buffer)
616 ;; OK, do it to it
617 (save-excursion
618 (funcall log-operation
619 log-fileset
620 log-entry))
621 ;; Remove checkin window (after the checkin so that if that fails
622 ;; we don't zap the log buffer and the typing therein).
623 ;; -- IMO this should be replaced with quit-window
624 (cond ((and logbuf vc-delete-logbuf-window)
625 (delete-windows-on logbuf (selected-frame))
626 ;; Kill buffer and delete any other dedicated windows/frames.
627 (kill-buffer logbuf))
628 (logbuf
629 (with-selected-window (or (get-buffer-window logbuf 0)
630 (selected-window))
631 (with-current-buffer logbuf
632 (bury-buffer)))))
633 ;; Now make sure we see the expanded headers
634 (when log-fileset
635 (mapc
636 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
637 log-fileset))
638 (when (vc-dispatcher-browsing)
639 (vc-dir-move-to-goal-column))
640 (run-hooks after-hook 'vc-finish-logentry-hook)))
642 (defun vc-dispatcher-browsing ()
643 "Are we in a directory browser buffer?"
644 (derived-mode-p 'vc-dir-mode))
646 ;; These are unused.
647 ;; (defun vc-dispatcher-in-fileset-p (fileset)
648 ;; (let ((member nil))
649 ;; (while (and (not member) fileset)
650 ;; (let ((elem (pop fileset)))
651 ;; (if (if (file-directory-p elem)
652 ;; (eq t (compare-strings buffer-file-name nil (length elem)
653 ;; elem nil nil))
654 ;; (eq (current-buffer) (get-file-buffer elem)))
655 ;; (setq member t))))
656 ;; member))
658 ;; (defun vc-dispatcher-selection-set (&optional observer)
659 ;; "Deduce a set of files to which to apply an operation. Return a cons
660 ;; cell (SELECTION . FILESET), where SELECTION is what the user chose
661 ;; and FILES is the flist with any directories replaced by the listed files
662 ;; within them.
664 ;; If we're in a directory display, the fileset is the list of marked files (if
665 ;; there is one) else the file on the current line. If not in a directory
666 ;; display, but the current buffer visits a file, the fileset is a singleton
667 ;; containing that file. Otherwise, throw an error."
668 ;; (let ((selection
669 ;; (cond
670 ;; ;; Browsing with vc-dir
671 ;; ((vc-dispatcher-browsing)
672 ;; ;; If no files are marked, temporarily mark current file
673 ;; ;; and choose on that basis (so we get subordinate files)
674 ;; (if (not (vc-dir-marked-files))
675 ;; (prog2
676 ;; (vc-dir-mark-file)
677 ;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
678 ;; (vc-dir-unmark-all-files t))
679 ;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
680 ;; ;; Visiting an eligible file
681 ;; ((buffer-file-name)
682 ;; (cons (list buffer-file-name) (list buffer-file-name)))
683 ;; ;; No eligible file -- if there's a parent buffer, deduce from there
684 ;; ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
685 ;; (with-current-buffer vc-parent-buffer
686 ;; (vc-dispatcher-browsing))))
687 ;; (with-current-buffer vc-parent-buffer
688 ;; (vc-dispatcher-selection-set)))
689 ;; ;; No good set here, throw error
690 ;; (t (error "No fileset is available here")))))
691 ;; ;; We assume, in order to avoid unpleasant surprises to the user,
692 ;; ;; that a fileset is not in good shape to be handed to the user if the
693 ;; ;; buffers visiting the fileset don't match the on-disk contents.
694 ;; (unless observer
695 ;; (save-some-buffers
696 ;; nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
697 ;; selection))
699 (provide 'vc-dispatcher)
701 ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
702 ;;; vc-dispatcher.el ends here