Followup to last change in browse-url.el
[emacs.git] / lisp / vc / vc-dispatcher.el
blobda9d34644cd553c7782f9ca005cdf385cb03635e
1 ;;; vc-dispatcher.el -- generic command-dispatcher facility. -*- lexical-binding: t -*-
3 ;; Copyright (C) 2008-2018 Free Software Foundation, Inc.
5 ;; Author: FSF (see below for full credits)
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: vc tools
8 ;; Package: vc
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 <https://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 resyncs 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 (let ((oldproc (get-buffer-process (current-buffer))))
175 ;; If we wanted to wait for oldproc to finish before doing
176 ;; something, we'd have used vc-eval-after.
177 ;; Use `delete-process' rather than `kill-process' because we don't
178 ;; want any of its output to appear from now on.
179 (when oldproc (delete-process oldproc)))
180 (kill-all-local-variables)
181 (set (make-local-variable 'vc-parent-buffer) camefrom)
182 (set (make-local-variable 'vc-parent-buffer-name)
183 (concat " from " (buffer-name camefrom)))
184 (setq default-directory olddir)
185 (let ((buffer-undo-list t)
186 (inhibit-read-only t))
187 (erase-buffer))))
189 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
191 (defun vc--process-sentinel (p code)
192 (let ((buf (process-buffer p)))
193 ;; Impatient users sometime kill "slow" buffers; check liveness
194 ;; to avoid "error in process sentinel: Selecting deleted buffer".
195 (when (buffer-live-p buf)
196 (with-current-buffer buf
197 (setq mode-line-process
198 (let ((status (process-status p)))
199 ;; Leave mode-line uncluttered, normally.
200 (unless (eq 'exit status)
201 (format " (%s)" status))))
202 (let (vc-sentinel-movepoint
203 (m (process-mark p)))
204 ;; Normally, we want async code such as sentinels to not move point.
205 (save-excursion
206 (goto-char m)
207 ;; Each sentinel may move point and the next one should be run
208 ;; at that new point. We could get the same result by having
209 ;; each sentinel read&set process-mark, but since `cmd' needs
210 ;; to work both for async and sync processes, this would be
211 ;; difficult to achieve.
212 (vc-exec-after code)
213 (move-marker m (point)))
214 ;; But sometimes the sentinels really want to move point.
215 (when vc-sentinel-movepoint
216 (let ((win (get-buffer-window (current-buffer) 0)))
217 (if (not win)
218 (goto-char vc-sentinel-movepoint)
219 (with-selected-window win
220 (goto-char vc-sentinel-movepoint))))))))))
222 (defun vc-set-mode-line-busy-indicator ()
223 (setq mode-line-process
224 (concat " " (propertize "[waiting...]"
225 'face 'mode-line-emphasis
226 'help-echo
227 "A command is in progress in this buffer"))))
229 (defun vc-exec-after (code)
230 "Eval CODE when the current buffer's process is done.
231 If the current buffer has no process, just evaluate CODE.
232 Else, add CODE to the process' sentinel.
233 CODE should be a function of no arguments."
234 (let ((proc (get-buffer-process (current-buffer))))
235 (cond
236 ;; If there's no background process, just execute the code.
237 ;; We used to explicitly call delete-process on exited processes,
238 ;; but this led to timing problems causing process output to be
239 ;; lost. Terminated processes get deleted automatically
240 ;; anyway. -- cyd
241 ((or (null proc) (eq (process-status proc) 'exit))
242 ;; Make sure we've read the process's output before going further.
243 (when proc (accept-process-output proc))
244 (if (functionp code) (funcall code) (eval code)))
245 ;; If a process is running, add CODE to the sentinel
246 ((eq (process-status proc) 'run)
247 (vc-set-mode-line-busy-indicator)
248 (letrec ((fun (lambda (p _msg)
249 (remove-function (process-sentinel p) fun)
250 (vc--process-sentinel p code))))
251 (add-function :after (process-sentinel proc) fun)))
252 (t (error "Unexpected process state"))))
253 nil)
255 (defmacro vc-run-delayed (&rest body)
256 (declare (indent 0) (debug t))
257 `(vc-exec-after (lambda () ,@body)))
259 (defvar vc-post-command-functions nil
260 "Hook run at the end of `vc-do-command'.
261 Each function is called inside the buffer in which the command was run
262 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
264 (defvar w32-quote-process-args)
266 (defun vc-delistify (filelist)
267 "Smash a FILELIST into a file list string suitable for info messages."
268 ;; FIXME what about file names with spaces?
269 (if (not filelist) "." (mapconcat 'identity filelist " ")))
271 ;;;###autoload
272 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
273 "Execute a slave command, notifying user and checking for errors.
274 Output from COMMAND goes to BUFFER, or the current buffer if
275 BUFFER is t. If the destination buffer is not already current,
276 set it up properly and erase it. The command is considered
277 successful if its exit status does not exceed OKSTATUS (if
278 OKSTATUS is nil, that means to ignore error status, if it is
279 `async', that means not to wait for termination of the
280 subprocess; if it is t it means to ignore all execution errors).
281 FILE-OR-LIST is the name of a working file; it may be a list of
282 files or be nil (to execute commands that don't expect a file
283 name or set of files). If an optional list of FLAGS is present,
284 that is inserted into the command line before the filename.
285 Return the return value of the slave command in the synchronous
286 case, and the process object in the asynchronous case."
287 ;; FIXME: file-relative-name can return a bogus result because
288 ;; it doesn't look at the actual file-system to see if symlinks
289 ;; come into play.
290 (let* ((files
291 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
292 (if (listp file-or-list) file-or-list (list file-or-list))))
293 ;; Keep entire commands in *Messages* but avoid resizing the
294 ;; echo area. Messages in this function are formatted in
295 ;; a such way that the important parts are at the beginning,
296 ;; due to potential truncation of long messages.
297 (message-truncate-lines t)
298 (full-command
299 (concat (if (string= (substring command -1) "\n")
300 (substring command 0 -1)
301 command)
302 " " (vc-delistify flags)
303 " " (vc-delistify files))))
304 (save-current-buffer
305 (unless (or (eq buffer t)
306 (and (stringp buffer)
307 (string= (buffer-name) buffer))
308 (eq buffer (current-buffer)))
309 (vc-setup-buffer buffer))
310 ;; If there's some previous async process still running, just kill it.
311 (let ((squeezed (remq nil flags))
312 (inhibit-read-only t)
313 (status 0))
314 (when files
315 (setq squeezed (nconc squeezed files)))
316 (let (;; Since some functions need to parse the output
317 ;; from external commands, set LC_MESSAGES to C.
318 (process-environment (cons "LC_MESSAGES=C" process-environment))
319 (w32-quote-process-args t))
320 (if (eq okstatus 'async)
321 ;; Run asynchronously.
322 (let ((proc
323 (let ((process-connection-type nil))
324 (apply 'start-file-process command (current-buffer)
325 command squeezed))))
326 (when vc-command-messages
327 (message "Running in background: %s" full-command))
328 ;; Get rid of the default message insertion, in case we don't
329 ;; set a sentinel explicitly.
330 (set-process-sentinel proc #'ignore)
331 (set-process-filter proc 'vc-process-filter)
332 (setq status proc)
333 (when vc-command-messages
334 (vc-run-delayed
335 (let ((message-truncate-lines t))
336 (message "Done in background: %s" full-command)))))
337 ;; Run synchronously
338 (when vc-command-messages
339 (message "Running in foreground: %s" full-command))
340 (let ((buffer-undo-list t))
341 (setq status (apply 'process-file command nil t nil squeezed)))
342 (when (and (not (eq t okstatus))
343 (or (not (integerp status))
344 (and okstatus (< okstatus status))))
345 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
346 (pop-to-buffer (current-buffer))
347 (goto-char (point-min))
348 (shrink-window-if-larger-than-buffer))
349 (error "Failed (%s): %s"
350 (if (integerp status) (format "status %d" status) status)
351 full-command))
352 (when vc-command-messages
353 (message "Done (status=%d): %s" status full-command))))
354 (vc-run-delayed
355 (run-hook-with-args 'vc-post-command-functions
356 command file-or-list flags))
357 status))))
359 (defun vc-do-async-command (buffer root command &rest args)
360 "Run COMMAND asynchronously with ARGS, displaying the result.
361 Send the output to BUFFER, which should be a buffer or the name
362 of a buffer, which is created.
363 ROOT should be the directory in which the command should be run.
364 Display the buffer in some window, but don't select it."
365 (let* ((dir default-directory)
366 (inhibit-read-only t)
367 window new-window-start)
368 (setq buffer (get-buffer-create buffer))
369 (if (get-buffer-process buffer)
370 (error "Another VC action on %s is running" root))
371 (with-current-buffer buffer
372 (setq default-directory root)
373 (goto-char (point-max))
374 (unless (eq (point) (point-min))
375 (insert "\f\n"))
376 (setq new-window-start (point))
377 (insert "Running \"" command)
378 (dolist (arg args)
379 (insert " " arg))
380 (insert "\"...\n")
381 ;; Run in the original working directory.
382 (let ((default-directory dir))
383 (apply 'vc-do-command t 'async command nil args)))
384 (setq window (display-buffer buffer))
385 (if window
386 (set-window-start window new-window-start))
387 buffer))
389 (defvar compilation-error-regexp-alist)
391 (defun vc-compilation-mode (backend)
392 "Setup `compilation-mode' after with the appropriate `compilation-error-regexp-alist'."
393 (require 'compile)
394 (let* ((error-regexp-alist
395 (vc-make-backend-sym backend 'error-regexp-alist))
396 (error-regexp-alist (and (boundp error-regexp-alist)
397 (symbol-value error-regexp-alist))))
398 (let ((compilation-error-regexp-alist error-regexp-alist))
399 (compilation-mode))
400 (set (make-local-variable 'compilation-error-regexp-alist)
401 error-regexp-alist)))
403 (declare-function vc-dir-refresh "vc-dir" ())
405 (defun vc-set-async-update (process-buffer)
406 "Set a `vc-exec-after' action appropriate to the current buffer.
407 This action will update the current buffer after the current
408 asynchronous VC command has completed. PROCESS-BUFFER is the
409 buffer for the asynchronous VC process.
411 If the current buffer is a VC Dir buffer, call `vc-dir-refresh'.
412 If the current buffer is a Dired buffer, revert it."
413 (let* ((buf (current-buffer))
414 (tick (buffer-modified-tick buf)))
415 (cond
416 ((derived-mode-p 'vc-dir-mode)
417 (with-current-buffer process-buffer
418 (vc-run-delayed
419 (if (buffer-live-p buf)
420 (with-current-buffer buf
421 (vc-dir-refresh))))))
422 ((derived-mode-p 'dired-mode)
423 (with-current-buffer process-buffer
424 (vc-run-delayed
425 (and (buffer-live-p buf)
426 (= (buffer-modified-tick buf) tick)
427 (with-current-buffer buf
428 (revert-buffer)))))))))
430 ;; These functions are used to ensure that the view the user sees is up to date
431 ;; even if the dispatcher client mode has messed with file contents (as in,
432 ;; for example, VCS keyword expansion).
434 (declare-function view-mode-exit "view" (&optional exit-only exit-action all-win))
436 (defun vc-position-context (posn)
437 "Save a bit of the text around POSN in the current buffer.
438 Used to help us find the corresponding position again later
439 if markers are destroyed or corrupted."
440 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
441 ;; rcs.el mode.
442 (list posn
443 (buffer-size)
444 (buffer-substring posn
445 (min (point-max) (+ posn 100)))))
447 (defun vc-find-position-by-context (context)
448 "Return the position of CONTEXT in the current buffer.
449 If CONTEXT cannot be found, return nil."
450 (let ((context-string (nth 2 context)))
451 (if (equal "" context-string)
452 (point-max)
453 (save-excursion
454 (let ((diff (- (nth 1 context) (buffer-size))))
455 (when (< diff 0) (setq diff (- diff)))
456 (goto-char (nth 0 context))
457 (if (or (search-forward context-string nil t)
458 ;; Can't use search-backward since the match may continue
459 ;; after point.
460 (progn (goto-char (- (point) diff (length context-string)))
461 ;; goto-char doesn't signal an error at
462 ;; beginning of buffer like backward-char would
463 (search-forward context-string nil t)))
464 ;; to beginning of OSTRING
465 (- (point) (length context-string))))))))
467 (defun vc-context-matches-p (posn context)
468 "Return t if POSN matches CONTEXT, nil otherwise."
469 (let* ((context-string (nth 2 context))
470 (len (length context-string))
471 (end (+ posn len)))
472 (if (> end (1+ (buffer-size)))
474 (string= context-string (buffer-substring posn end)))))
476 (defun vc-buffer-context ()
477 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
478 Used by `vc-restore-buffer-context' to later restore the context."
479 (let ((point-context (vc-position-context (point)))
480 ;; Use mark-marker to avoid confusion in transient-mark-mode.
481 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
482 (vc-position-context (mark-marker))))
483 ;; Make the right thing happen in transient-mark-mode.
484 (mark-active nil))
485 (list point-context mark-context)))
487 (defun vc-restore-buffer-context (context)
488 "Restore point/mark, and reparse any affected compilation buffers.
489 CONTEXT is that which `vc-buffer-context' returns."
490 (let ((point-context (nth 0 context))
491 (mark-context (nth 1 context)))
492 ;; if necessary, restore point and mark
493 (if (not (vc-context-matches-p (point) point-context))
494 (let ((new-point (vc-find-position-by-context point-context)))
495 (when new-point (goto-char new-point))))
496 (and mark-active
497 mark-context
498 (not (vc-context-matches-p (mark) mark-context))
499 (let ((new-mark (vc-find-position-by-context mark-context)))
500 (when new-mark (set-mark new-mark))))))
502 (defun vc-revert-buffer-internal (&optional arg no-confirm)
503 "Revert buffer, keeping point and mark where user expects them.
504 Try to be clever in the face of changes due to expanded version-control
505 key words. This is important for typeahead to work as expected.
506 ARG and NO-CONFIRM are passed on to `revert-buffer'."
507 (interactive "P")
508 (widen)
509 (let ((context (vc-buffer-context)))
510 ;; Use save-excursion here, because it may be able to restore point
511 ;; and mark properly even in cases where vc-restore-buffer-context
512 ;; would fail. However, save-excursion might also get it wrong --
513 ;; in this case, vc-restore-buffer-context gives it a second try.
514 (save-excursion
515 ;; t means don't call normal-mode;
516 ;; that's to preserve various minor modes.
517 (revert-buffer arg no-confirm t))
518 (vc-restore-buffer-context context)))
520 (defvar vc-mode-line-hook nil)
521 (make-variable-buffer-local 'vc-mode-line-hook)
522 (put 'vc-mode-line-hook 'permanent-local t)
524 (defvar view-old-buffer-read-only)
526 (defun vc-resynch-window (file &optional keep noquery reset-vc-info)
527 "If FILE is in the current buffer, either revert or unvisit it.
528 The choice between revert (to see expanded keywords) and unvisit
529 depends on KEEP. NOQUERY if non-nil inhibits confirmation for
530 reverting. NOQUERY should be t *only* if it is known the only
531 difference between the buffer and the file is due to
532 modifications by the dispatcher client code, rather than user
533 editing!"
534 (and (string= buffer-file-name file)
535 (if keep
536 (when (file-exists-p file)
537 (when reset-vc-info
538 (vc-file-clearprops file))
539 (vc-revert-buffer-internal t noquery)
541 ;; VC operations might toggle the read-only state. In
542 ;; that case we need to adjust the `view-mode' status
543 ;; when `view-read-only' is non-nil.
544 (and view-read-only
545 (if (file-writable-p file)
546 (and view-mode
547 (let ((view-old-buffer-read-only nil))
548 (view-mode-exit t)))
549 (and (not view-mode)
550 (not (eq (get major-mode 'mode-class) 'special))
551 (view-mode-enter))))
553 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
554 (run-hook-with-args 'vc-mode-line-hook buffer-file-name))
555 (kill-buffer (current-buffer)))))
557 (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
559 (defun vc-resynch-buffers-in-directory (directory &optional keep noquery reset-vc-info)
560 "Resync all buffers that visit files in DIRECTORY."
561 (dolist (buffer (buffer-list))
562 (let ((fname (buffer-file-name buffer)))
563 (when (and fname (string-prefix-p directory fname))
564 (with-current-buffer buffer
565 (vc-resynch-buffer fname keep noquery reset-vc-info))))))
567 (defun vc-resynch-buffer (file &optional keep noquery reset-vc-info)
568 "If FILE is currently visited, resynch its buffer."
569 (if (string= buffer-file-name file)
570 (vc-resynch-window file keep noquery reset-vc-info)
571 (if (file-directory-p file)
572 (vc-resynch-buffers-in-directory file keep noquery reset-vc-info)
573 (let ((buffer (get-file-buffer file)))
574 (when buffer
575 (with-current-buffer buffer
576 (vc-resynch-window file keep noquery reset-vc-info))))))
577 ;; Try to avoid unnecessary work, a *vc-dir* buffer is only present
578 ;; if this is true.
579 (when vc-dir-buffers
580 (vc-dir-resynch-file file)))
582 (defun vc-buffer-sync (&optional not-urgent)
583 "Make sure the current buffer and its working file are in sync.
584 NOT-URGENT means it is ok to continue if the user says not to save."
585 (let (missing)
586 (when (cond
587 ((buffer-modified-p))
588 ((not (file-exists-p buffer-file-name))
589 (setq missing t)))
590 (if (or vc-suppress-confirm
591 (y-or-n-p (format "Buffer %s %s; save it? "
592 (buffer-name)
593 (if missing
594 "is missing on disk"
595 "modified"))))
596 (save-buffer)
597 (unless not-urgent
598 (error "Aborted"))))))
600 ;; Command closures
602 ;; Set up key bindings for use while editing log messages
604 (declare-function log-edit-empty-buffer-p "log-edit" ())
606 (defun vc-log-edit (fileset mode backend)
607 "Set up `log-edit' for use on FILE."
608 (setq default-directory
609 (buffer-local-value 'default-directory vc-parent-buffer))
610 (require 'log-edit)
611 (log-edit 'vc-finish-logentry
612 ;; Setup a new log message if the log buffer is "empty",
613 ;; or was previously used for a different set of files.
614 (or (log-edit-empty-buffer-p)
615 (and (local-variable-p 'vc-log-fileset)
616 (not (equal vc-log-fileset fileset))))
617 `((log-edit-listfun
618 . (lambda ()
619 ;; FIXME: When fileset includes directories, and
620 ;; there are relevant ChangeLog files inside their
621 ;; children, we don't find them. Either handle it
622 ;; in `log-edit-insert-changelog-entries' by
623 ;; walking down the file trees, or somehow pass
624 ;; `fileset-only-files' from `vc-next-action'
625 ;; through to this function.
626 (let ((root (vc-root-dir)))
627 ;; Returns paths relative to the root, so that
628 ;; `log-edit-changelog-insert-entries'
629 ;; substitutes them in correctly later, even when
630 ;; `vc-checkin' was called from a file buffer, or
631 ;; a non-root VC-Dir buffer.
632 (mapcar
633 (lambda (file) (file-relative-name file root))
634 ',fileset))))
635 (log-edit-diff-function . vc-diff)
636 (log-edit-vc-backend . ,backend)
637 (vc-log-fileset . ,fileset))
639 mode)
640 (set-buffer-modified-p nil)
641 (setq buffer-file-name nil))
643 (defun vc-start-logentry (files comment initial-contents msg logbuf mode action &optional after-hook backend)
644 "Accept a comment for an operation on FILES.
645 If COMMENT is nil, pop up a LOGBUF buffer, emit MSG, and set the
646 action on close to ACTION. If COMMENT is a string and
647 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
648 contents of the log entry buffer. If COMMENT is a string and
649 INITIAL-CONTENTS is nil, do action immediately as if the user had
650 entered COMMENT. If COMMENT is t, also do action immediately with an
651 empty comment. Remember the file's buffer in `vc-parent-buffer'
652 \(current one if no file). Puts the log-entry buffer in major-mode
653 MODE, defaulting to `log-edit-mode' if MODE is nil.
654 AFTER-HOOK specifies the local value for `vc-log-after-operation-hook'.
655 BACKEND, if non-nil, specifies a VC backend for the Log Edit buffer."
656 (let ((parent
657 (if (vc-dispatcher-browsing)
658 ;; If we are called from a directory browser, the parent buffer is
659 ;; the current buffer.
660 (current-buffer)
661 (if (and files (equal (length files) 1))
662 (get-file-buffer (car files))
663 (current-buffer)))))
664 (if (and comment (not initial-contents))
665 (set-buffer (get-buffer-create logbuf))
666 (pop-to-buffer (get-buffer-create logbuf)))
667 (set (make-local-variable 'vc-parent-buffer) parent)
668 (set (make-local-variable 'vc-parent-buffer-name)
669 (concat " from " (buffer-name vc-parent-buffer)))
670 (vc-log-edit files mode backend)
671 (make-local-variable 'vc-log-after-operation-hook)
672 (when after-hook
673 (setq vc-log-after-operation-hook after-hook))
674 (set (make-local-variable 'vc-log-operation) action)
675 (when comment
676 (erase-buffer)
677 (when (stringp comment) (insert comment)))
678 (if (or (not comment) initial-contents)
679 (message "%s Type C-c C-c when done" msg)
680 (vc-finish-logentry (eq comment t)))))
682 (declare-function vc-dir-move-to-goal-column "vc-dir" ())
683 ;; vc-finish-logentry is typically called from a log-edit buffer (see
684 ;; vc-start-logentry).
685 (defun vc-finish-logentry (&optional nocomment)
686 "Complete the operation implied by the current log entry.
687 Use the contents of the current buffer as a check-in or registration
688 comment. If the optional arg NOCOMMENT is non-nil, then don't check
689 the buffer contents as a comment."
690 (interactive)
691 ;; Check and record the comment, if any.
692 (unless nocomment
693 (run-hooks 'vc-logentry-check-hook))
694 ;; Sync parent buffer in case the user modified it while editing the comment.
695 ;; But not if it is a vc-dir buffer.
696 (with-current-buffer vc-parent-buffer
697 (or (vc-dispatcher-browsing) (vc-buffer-sync)))
698 (unless vc-log-operation
699 (error "No log operation is pending"))
701 ;; save the parameters held in buffer-local variables
702 (let ((logbuf (current-buffer))
703 (log-operation vc-log-operation)
704 ;; FIXME: When coming from VC-Dir, we should check that the
705 ;; set of selected files is still equal to vc-log-fileset,
706 ;; to avoid surprises.
707 (log-fileset vc-log-fileset)
708 (log-entry (buffer-string))
709 (after-hook vc-log-after-operation-hook))
710 (pop-to-buffer vc-parent-buffer)
711 ;; OK, do it to it
712 (save-excursion
713 (funcall log-operation
714 log-fileset
715 log-entry))
716 (setq vc-log-operation nil)
718 ;; Quit windows on logbuf.
719 (cond
720 ((not logbuf))
721 (vc-delete-logbuf-window
722 (quit-windows-on logbuf t (selected-frame)))
724 (quit-windows-on logbuf nil 0)))
726 ;; Now make sure we see the expanded headers
727 (when log-fileset
728 (mapc
729 (lambda (file) (vc-resynch-buffer file t t))
730 log-fileset))
731 (when (vc-dispatcher-browsing)
732 (vc-dir-move-to-goal-column))
733 (run-hooks after-hook 'vc-finish-logentry-hook)))
735 (defun vc-dispatcher-browsing ()
736 "Are we in a directory browser buffer?"
737 (derived-mode-p 'vc-dir-mode))
739 ;; These are unused.
740 ;; (defun vc-dispatcher-in-fileset-p (fileset)
741 ;; (let ((member nil))
742 ;; (while (and (not member) fileset)
743 ;; (let ((elem (pop fileset)))
744 ;; (if (if (file-directory-p elem)
745 ;; (eq t (compare-strings buffer-file-name nil (length elem)
746 ;; elem nil nil))
747 ;; (eq (current-buffer) (get-file-buffer elem)))
748 ;; (setq member t))))
749 ;; member))
751 ;; (defun vc-dispatcher-selection-set (&optional observer)
752 ;; "Deduce a set of files to which to apply an operation. Return a cons
753 ;; cell (SELECTION . FILESET), where SELECTION is what the user chose
754 ;; and FILES is the flist with any directories replaced by the listed files
755 ;; within them.
757 ;; If we're in a directory display, the fileset is the list of marked files (if
758 ;; there is one) else the file on the current line. If not in a directory
759 ;; display, but the current buffer visits a file, the fileset is a singleton
760 ;; containing that file. Otherwise, throw an error."
761 ;; (let ((selection
762 ;; (cond
763 ;; ;; Browsing with vc-dir
764 ;; ((vc-dispatcher-browsing)
765 ;; ;; If no files are marked, temporarily mark current file
766 ;; ;; and choose on that basis (so we get subordinate files)
767 ;; (if (not (vc-dir-marked-files))
768 ;; (prog2
769 ;; (vc-dir-mark-file)
770 ;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
771 ;; (vc-dir-unmark-all-files t))
772 ;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
773 ;; ;; Visiting an eligible file
774 ;; ((buffer-file-name)
775 ;; (cons (list buffer-file-name) (list buffer-file-name)))
776 ;; ;; No eligible file -- if there's a parent buffer, deduce from there
777 ;; ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
778 ;; (with-current-buffer vc-parent-buffer
779 ;; (vc-dispatcher-browsing))))
780 ;; (with-current-buffer vc-parent-buffer
781 ;; (vc-dispatcher-selection-set)))
782 ;; ;; No good set here, throw error
783 ;; (t (error "No fileset is available here")))))
784 ;; ;; We assume, in order to avoid unpleasant surprises to the user,
785 ;; ;; that a fileset is not in good shape to be handed to the user if the
786 ;; ;; buffers visiting the fileset don't match the on-disk contents.
787 ;; (unless observer
788 ;; (save-some-buffers
789 ;; nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
790 ;; selection))
792 (provide 'vc-dispatcher)
794 ;;; vc-dispatcher.el ends here