* lisp/vc/vc-dispatcher.el (vc-log-edit): Setup the Summary&Author headers.
[bpt/emacs.git] / lisp / vc / vc-dispatcher.el
CommitLineData
bcd7a0a4 1;;; vc-dispatcher.el -- generic command-dispatcher facility. -*- lexical-binding: t -*-
92d1eebf 2
ab422c4d 3;; Copyright (C) 2008-2013 Free Software Foundation, Inc.
92d1eebf
ER
4
5;; Author: FSF (see below for full credits)
6;; Maintainer: Eric S. Raymond <esr@thyrsus.com>
9766adfb 7;; Keywords: vc tools
bd78fa1d 8;; Package: vc
92d1eebf
ER
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
92d1eebf 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
92d1eebf
ER
16
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.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
92d1eebf
ER
24
25;;; Credits:
26
27;; Designed and implemented by Eric S. Raymond, originally as part of VC mode.
dbf2c858 28;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the
821677fb 29;; vc-dir front end.
92d1eebf
ER
30
31;;; Commentary:
32
33;; Goals:
34;;
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
b1d87c10 39;; to version-control systems.
92d1eebf
ER
40;;
41;; vc-dispatcher.el is written to decouple the UI issues in such front
b1d87c10 42;; ends from their application-specific logic. It also provides a
92d1eebf
ER
43;; service layer for running the slave commands either synchronously
44;; or asynchronously and managing the message/error logs from the
45;; command runs.
46;;
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.
53
54;; Dispatcher's universe:
55;;
56;; The universe consists of the file tree rooted at the current
b1d87c10
GM
57;; directory. The dispatcher's upper layer deduces some subset
58;; of the file tree from the state of the currently visited buffer
92d1eebf
ER
59;; and returns that subset, presumably to a client mode.
60;;
06ad7cd8
ER
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.
92d1eebf
ER
63;;
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
b1d87c10 68;; executed when the user types C-c to ship the buffer contents. In
92d1eebf
ER
69;; either case the command messages and error (if any) will remain
70;; available in a status buffer.
71
d99d06ec
ER
72;; Special behavior of dispatcher directory buffers:
73;;
038608c7 74;; In dispatcher directory buffers, facilities to perform basic
d99d06ec
ER
75;; navigation and selection operations are provided by keymap and menu
76;; entries that dispatcher sets up itself, so they'll be uniform
038608c7 77;; across all dispatcher-using client modes. Client modes are
d99d06ec
ER
78;; expected to append to these to provide mode-specific bindings.
79;;
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
b1d87c10 83;; treat all entries with the same state as the currently selected one as
038608c7 84;; a unit.
821677fb 85
dab68082 86;; The interface:
821677fb
ER
87;;
88;; The main interface to the lower level is vc-do-command. This launches a
b1d87c10 89;; command, synchronously or asynchronously, making the output available
4096c5f2 90;; in a command log buffer. Two other functions, (vc-start-logentry) and
821677fb 91;; (vc-finish-logentry), allow you to associate a command closure with an
b1d87c10 92;; annotation buffer so that when the user confirms the comment the closure
821677fb
ER
93;; is run (with the comment as part of its context).
94;;
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
b1d87c10
GM
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
c8d4f1a0
ER
102;; coincide.
103;;
8c6eab5a 104;; When the client mode adds a local vc-mode-line-hook to a buffer, it
c8d4f1a0 105;; will be called with the buffer file name as argument whenever the
40ba43b4 106;; dispatcher resyncs the buffer.
821677fb 107
d99d06ec
ER
108;; To do:
109;;
373b6519 110;; - log buffers need font-locking.
d99d06ec 111;;
83affd96 112
1915fb44 113;; General customization
83affd96
ER
114(defcustom vc-logentry-check-hook nil
115 "Normal hook run by `vc-finish-logentry'.
116Use this to impose your own rules on the entry in addition to any the
4f61cc3e 117dispatcher client mode imposes itself."
83affd96
ER
118 :type 'hook
119 :group 'vc)
120
17f039f3 121(defcustom vc-delete-logbuf-window t
400aec1a 122 "If non-nil, delete the log buffer and window after each logical action.
17f039f3
ER
123If nil, bury that buffer instead.
124This is most useful if you have multiple windows on a frame and would like to
125preserve the setting."
126 :type 'boolean
127 :group 'vc)
128
129(defcustom vc-command-messages nil
130 "If non-nil, display run messages from back-end commands."
131 :type 'boolean
132 :group 'vc)
133
821677fb
ER
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)
138
83affd96 139;; Variables the user doesn't need to know about.
17f039f3 140
83affd96
ER
141(defvar vc-log-operation nil)
142(defvar vc-log-after-operation-hook nil)
143(defvar vc-log-fileset)
83affd96
ER
144
145;; In a log entry buffer, this is a local variable
146;; that points to the buffer for which it was made
d536a904 147;; (either a file, or a directory buffer).
83affd96
ER
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)
152
92d1eebf
ER
153;; Common command execution logic
154
155(defun vc-process-filter (p s)
156 "An alternative output filter for async process P.
157One difference with the default filter is that this inserts S after markers.
158Another 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))))))))
168
7265c6e8
ER
169(defun vc-setup-buffer (buf)
170 "Prepare BUF for executing a slave command and make it current."
92d1eebf
ER
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))))
182
183(defvar vc-sentinel-movepoint) ;Dynamically scoped.
184
bcd7a0a4
SM
185(defun vc--process-sentinel (p code)
186 (let ((buf (process-buffer p)))
92d1eebf
ER
187 ;; Impatient users sometime kill "slow" buffers; check liveness
188 ;; to avoid "error in process sentinel: Selecting deleted buffer".
189 (when (buffer-live-p buf)
92d1eebf
ER
190 (with-current-buffer buf
191 (setq mode-line-process
192 (let ((status (process-status p)))
193 ;; Leave mode-line uncluttered, normally.
194 (unless (eq 'exit status)
195 (format " (%s)" status))))
bcd7a0a4
SM
196 (let (vc-sentinel-movepoint
197 (m (process-mark p)))
92d1eebf
ER
198 ;; Normally, we want async code such as sentinels to not move point.
199 (save-excursion
bcd7a0a4 200 (goto-char m)
9c750eba
SM
201 ;; Each sentinel may move point and the next one should be run
202 ;; at that new point. We could get the same result by having
203 ;; each sentinel read&set process-mark, but since `cmd' needs
204 ;; to work both for async and sync processes, this would be
205 ;; difficult to achieve.
bcd7a0a4
SM
206 (vc-exec-after code)
207 (move-marker m (point)))
92d1eebf
ER
208 ;; But sometimes the sentinels really want to move point.
209 (when vc-sentinel-movepoint
210 (let ((win (get-buffer-window (current-buffer) 0)))
211 (if (not win)
212 (goto-char vc-sentinel-movepoint)
213 (with-selected-window win
214 (goto-char vc-sentinel-movepoint))))))))))
215
216(defun vc-set-mode-line-busy-indicator ()
217 (setq mode-line-process
218 (concat " " (propertize "[waiting...]"
219 'face 'mode-line-emphasis
220 'help-echo
d536a904 221 "A command is in progress in this buffer"))))
92d1eebf
ER
222
223(defun vc-exec-after (code)
224 "Eval CODE when the current buffer's process is done.
225If the current buffer has no process, just evaluate CODE.
bcd7a0a4 226Else, add CODE to the process' sentinel.
4c528aab 227CODE should be a function of no arguments."
92d1eebf
ER
228 (let ((proc (get-buffer-process (current-buffer))))
229 (cond
230 ;; If there's no background process, just execute the code.
231 ;; We used to explicitly call delete-process on exited processes,
232 ;; but this led to timing problems causing process output to be
233 ;; lost. Terminated processes get deleted automatically
234 ;; anyway. -- cyd
235 ((or (null proc) (eq (process-status proc) 'exit))
236 ;; Make sure we've read the process's output before going further.
237 (when proc (accept-process-output proc))
bcd7a0a4 238 (if (functionp code) (funcall code) (eval code)))
92d1eebf
ER
239 ;; If a process is running, add CODE to the sentinel
240 ((eq (process-status proc) 'run)
241 (vc-set-mode-line-busy-indicator)
bcd7a0a4
SM
242 (letrec ((fun (lambda (p _msg)
243 (remove-function (process-sentinel p) fun)
244 (vc--process-sentinel p code))))
245 (add-function :after (process-sentinel proc) fun)))
92d1eebf
ER
246 (t (error "Unexpected process state"))))
247 nil)
248
9c750eba
SM
249(defmacro vc-run-delayed (&rest body)
250 (declare (indent 0) (debug t))
251 `(vc-exec-after (lambda () ,@body)))
252
92d1eebf
ER
253(defvar vc-post-command-functions nil
254 "Hook run at the end of `vc-do-command'.
255Each function is called inside the buffer in which the command was run
256and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
257
258(defvar w32-quote-process-args)
259
260(defun vc-delistify (filelist)
261 "Smash a FILELIST into a file list string suitable for info messages."
262 ;; FIXME what about file names with spaces?
263 (if (not filelist) "." (mapconcat 'identity filelist " ")))
264
265;;;###autoload
266(defun vc-do-command (buffer okstatus command file-or-list &rest flags)
06ad7cd8 267 "Execute a slave command, notifying user and checking for errors.
2888a97e
ER
268Output from COMMAND goes to BUFFER, or the current buffer if
269BUFFER is t. If the destination buffer is not already current,
270set it up properly and erase it. The command is considered
271successful if its exit status does not exceed OKSTATUS (if
272OKSTATUS is nil, that means to ignore error status, if it is
273`async', that means not to wait for termination of the
274subprocess; if it is t it means to ignore all execution errors).
275FILE-OR-LIST is the name of a working file; it may be a list of
276files or be nil (to execute commands that don't expect a file
277name or set of files). If an optional list of FLAGS is present,
4096c5f2
DN
278that is inserted into the command line before the filename.
279Return the return value of the slave command in the synchronous
280case, and the process object in the asynchronous case."
92d1eebf
ER
281 ;; FIXME: file-relative-name can return a bogus result because
282 ;; it doesn't look at the actual file-system to see if symlinks
283 ;; come into play.
284 (let* ((files
285 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
286 (if (listp file-or-list) file-or-list (list file-or-list))))
287 (full-command
288 ;; What we're doing here is preparing a version of the command
b1d87c10 289 ;; for display in a debug-progress message. If it's fewer than
92d1eebf
ER
290 ;; 20 characters display the entire command (without trailing
291 ;; newline). Otherwise display the first 20 followed by an ellipsis.
292 (concat (if (string= (substring command -1) "\n")
293 (substring command 0 -1)
294 command)
295 " "
296 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
297 " " (vc-delistify files))))
298 (save-current-buffer
299 (unless (or (eq buffer t)
300 (and (stringp buffer)
301 (string= (buffer-name) buffer))
302 (eq buffer (current-buffer)))
2888a97e 303 (vc-setup-buffer buffer))
92d1eebf
ER
304 ;; If there's some previous async process still running, just kill it.
305 (let ((oldproc (get-buffer-process (current-buffer))))
306 ;; If we wanted to wait for oldproc to finish before doing
307 ;; something, we'd have used vc-eval-after.
308 ;; Use `delete-process' rather than `kill-process' because we don't
309 ;; want any of its output to appear from now on.
4096c5f2 310 (when oldproc (delete-process oldproc)))
92d1eebf
ER
311 (let ((squeezed (remq nil flags))
312 (inhibit-read-only t)
313 (status 0))
314 (when files
315 (setq squeezed (nconc squeezed files)))
b0287b39 316 (let (;; Since some functions need to parse the output
d4efdaea 317 ;; from external commands, set LC_MESSAGES to C.
b0287b39 318 (process-environment (cons "LC_MESSAGES=C" process-environment))
92d1eebf 319 (w32-quote-process-args t))
92d1eebf
ER
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))))
4096c5f2
DN
326 (when vc-command-messages
327 (message "Running %s in background..." full-command))
31dcede0
SM
328 ;; Get rid of the default message insertion, in case we don't
329 ;; set a sentinel explicitly.
330 (set-process-sentinel proc #'ignore)
92d1eebf 331 (set-process-filter proc 'vc-process-filter)
4096c5f2
DN
332 (setq status proc)
333 (when vc-command-messages
9c750eba
SM
334 (vc-run-delayed
335 (message "Running %s in background... done" full-command))))
b1d87c10 336 ;; Run synchronously
92d1eebf
ER
337 (when vc-command-messages
338 (message "Running %s in foreground..." full-command))
339 (let ((buffer-undo-list t))
340 (setq status (apply 'process-file command nil t nil squeezed)))
341 (when (and (not (eq t okstatus))
342 (or (not (integerp status))
343 (and okstatus (< okstatus status))))
344 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
345 (pop-to-buffer (current-buffer))
346 (goto-char (point-min))
347 (shrink-window-if-larger-than-buffer))
348 (error "Running %s...FAILED (%s)" full-command
4096c5f2
DN
349 (if (integerp status) (format "status %d" status) status)))
350 (when vc-command-messages
351 (message "Running %s...OK = %d" full-command status))))
9c750eba
SM
352 (vc-run-delayed
353 (run-hook-with-args 'vc-post-command-functions
354 command file-or-list flags))
92d1eebf
ER
355 status))))
356
9bfe5783
CY
357(defun vc-do-async-command (buffer root command &rest args)
358 "Run COMMAND asynchronously with ARGS, displaying the result.
359Send the output to BUFFER, which should be a buffer or the name
360of a buffer, which is created.
361ROOT should be the directory in which the command should be run.
362Display the buffer in some window, but don't select it."
363 (let* ((dir default-directory)
3ab713fd 364 (inhibit-read-only t)
9bfe5783
CY
365 window new-window-start)
366 (setq buffer (get-buffer-create buffer))
367 (if (get-buffer-process buffer)
368 (error "Another VC action on %s is running" root))
369 (with-current-buffer buffer
370 (setq default-directory root)
371 (goto-char (point-max))
372 (unless (eq (point) (point-min))
373 (insert "\f\n"))
374 (setq new-window-start (point))
659114fd 375 (insert "Running \"" command)
9bfe5783
CY
376 (dolist (arg args)
377 (insert " " arg))
378 (insert "\"...\n")
379 ;; Run in the original working directory.
380 (let ((default-directory dir))
381 (apply 'vc-do-command t 'async command nil args)))
382 (setq window (display-buffer buffer))
383 (if window
a2b6e5d6
CY
384 (set-window-start window new-window-start))
385 buffer))
386
bcd7a0a4
SM
387(defvar compilation-error-regexp-alist)
388
8a4e6db8
SS
389(defun vc-compilation-mode (backend)
390 "Setup `compilation-mode' after with the appropriate `compilation-error-regexp-alist'."
b86a85ca 391 (require 'compile)
8a4e6db8
SS
392 (let* ((error-regexp-alist
393 (vc-make-backend-sym backend 'error-regexp-alist))
b86a85ca
GM
394 (error-regexp-alist (and (boundp error-regexp-alist)
395 (symbol-value error-regexp-alist))))
396 (let ((compilation-error-regexp-alist error-regexp-alist))
397 (compilation-mode))
8a4e6db8 398 (set (make-local-variable 'compilation-error-regexp-alist)
b86a85ca 399 error-regexp-alist)))
8a4e6db8 400
dea01c6e
GM
401(declare-function vc-dir-refresh "vc-dir" ())
402
a2b6e5d6
CY
403(defun vc-set-async-update (process-buffer)
404 "Set a `vc-exec-after' action appropriate to the current buffer.
405This action will update the current buffer after the current
406asynchronous VC command has completed. PROCESS-BUFFER is the
407buffer for the asynchronous VC process.
408
409If the current buffer is a VC Dir buffer, call `vc-dir-refresh'.
410If the current buffer is a Dired buffer, revert it."
411 (let* ((buf (current-buffer))
412 (tick (buffer-modified-tick buf)))
413 (cond
414 ((derived-mode-p 'vc-dir-mode)
415 (with-current-buffer process-buffer
9c750eba
SM
416 (vc-run-delayed
417 (if (buffer-live-p buf)
418 (with-current-buffer buf
419 (vc-dir-refresh))))))
a2b6e5d6
CY
420 ((derived-mode-p 'dired-mode)
421 (with-current-buffer process-buffer
9c750eba
SM
422 (vc-run-delayed
423 (and (buffer-live-p buf)
424 (= (buffer-modified-tick buf) tick)
425 (with-current-buffer buf
426 (revert-buffer)))))))))
9bfe5783 427
17f039f3 428;; These functions are used to ensure that the view the user sees is up to date
b1d87c10 429;; even if the dispatcher client mode has messed with file contents (as in,
17f039f3
ER
430;; for example, VCS keyword expansion).
431
432(declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
433
434(defun vc-position-context (posn)
435 "Save a bit of the text around POSN in the current buffer.
436Used to help us find the corresponding position again later
437if markers are destroyed or corrupted."
438 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
439 ;; rcs.el mode.
440 (list posn
441 (buffer-size)
442 (buffer-substring posn
443 (min (point-max) (+ posn 100)))))
444
445(defun vc-find-position-by-context (context)
446 "Return the position of CONTEXT in the current buffer.
447If CONTEXT cannot be found, return nil."
448 (let ((context-string (nth 2 context)))
449 (if (equal "" context-string)
450 (point-max)
451 (save-excursion
452 (let ((diff (- (nth 1 context) (buffer-size))))
453 (when (< diff 0) (setq diff (- diff)))
454 (goto-char (nth 0 context))
455 (if (or (search-forward context-string nil t)
456 ;; Can't use search-backward since the match may continue
457 ;; after point.
458 (progn (goto-char (- (point) diff (length context-string)))
459 ;; goto-char doesn't signal an error at
460 ;; beginning of buffer like backward-char would
461 (search-forward context-string nil t)))
462 ;; to beginning of OSTRING
463 (- (point) (length context-string))))))))
464
465(defun vc-context-matches-p (posn context)
466 "Return t if POSN matches CONTEXT, nil otherwise."
467 (let* ((context-string (nth 2 context))
468 (len (length context-string))
469 (end (+ posn len)))
470 (if (> end (1+ (buffer-size)))
471 nil
472 (string= context-string (buffer-substring posn end)))))
473
474(defun vc-buffer-context ()
475 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
476Used by `vc-restore-buffer-context' to later restore the context."
477 (let ((point-context (vc-position-context (point)))
478 ;; Use mark-marker to avoid confusion in transient-mark-mode.
9ba4a350 479 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
17f039f3
ER
480 (vc-position-context (mark-marker))))
481 ;; Make the right thing happen in transient-mark-mode.
9ba4a350 482 (mark-active nil))
bcd7a0a4 483 (list point-context mark-context)))
17f039f3
ER
484
485(defun vc-restore-buffer-context (context)
486 "Restore point/mark, and reparse any affected compilation buffers.
487CONTEXT is that which `vc-buffer-context' returns."
488 (let ((point-context (nth 0 context))
9ba4a350 489 (mark-context (nth 1 context)))
17f039f3
ER
490 ;; if necessary, restore point and mark
491 (if (not (vc-context-matches-p (point) point-context))
492 (let ((new-point (vc-find-position-by-context point-context)))
493 (when new-point (goto-char new-point))))
494 (and mark-active
495 mark-context
496 (not (vc-context-matches-p (mark) mark-context))
497 (let ((new-mark (vc-find-position-by-context mark-context)))
498 (when new-mark (set-mark new-mark))))))
499
500(defun vc-revert-buffer-internal (&optional arg no-confirm)
501 "Revert buffer, keeping point and mark where user expects them.
502Try to be clever in the face of changes due to expanded version-control
503key words. This is important for typeahead to work as expected.
504ARG and NO-CONFIRM are passed on to `revert-buffer'."
505 (interactive "P")
506 (widen)
507 (let ((context (vc-buffer-context)))
508 ;; Use save-excursion here, because it may be able to restore point
509 ;; and mark properly even in cases where vc-restore-buffer-context
510 ;; would fail. However, save-excursion might also get it wrong --
511 ;; in this case, vc-restore-buffer-context gives it a second try.
512 (save-excursion
513 ;; t means don't call normal-mode;
514 ;; that's to preserve various minor modes.
515 (revert-buffer arg no-confirm t))
516 (vc-restore-buffer-context context)))
517
8c6eab5a
SM
518(defvar vc-mode-line-hook nil)
519(make-variable-buffer-local 'vc-mode-line-hook)
520(put 'vc-mode-line-hook 'permanent-local t)
521
bcd7a0a4
SM
522(defvar view-old-buffer-read-only)
523
00fd1147 524(defun vc-resynch-window (file &optional keep noquery reset-vc-info)
17f039f3
ER
525 "If FILE is in the current buffer, either revert or unvisit it.
526The choice between revert (to see expanded keywords) and unvisit
527depends on KEEP. NOQUERY if non-nil inhibits confirmation for
528reverting. NOQUERY should be t *only* if it is known the only
529difference between the buffer and the file is due to
530modifications by the dispatcher client code, rather than user
531editing!"
532 (and (string= buffer-file-name file)
533 (if keep
cf885595 534 (when (file-exists-p file)
00fd1147
DN
535 (when reset-vc-info
536 (vc-file-clearprops file))
17f039f3 537 (vc-revert-buffer-internal t noquery)
3c53763c
DN
538
539 ;; VC operations might toggle the read-only state. In
540 ;; that case we need to adjust the `view-mode' status
541 ;; when `view-read-only' is non-nil.
17f039f3
ER
542 (and view-read-only
543 (if (file-writable-p file)
544 (and view-mode
545 (let ((view-old-buffer-read-only nil))
546 (view-mode-exit)))
547 (and (not view-mode)
548 (not (eq (get major-mode 'mode-class) 'special))
549 (view-mode-enter))))
3c53763c 550
8c6eab5a
SM
551 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
552 (run-hook-with-args 'vc-mode-line-hook buffer-file-name))
17f039f3
ER
553 (kill-buffer (current-buffer)))))
554
74d0991f 555(declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
d923f4ac 556
00fd1147 557(defun vc-resynch-buffers-in-directory (directory &optional keep noquery reset-vc-info)
d923f4ac
DN
558 "Resync all buffers that visit files in DIRECTORY."
559 (dolist (buffer (buffer-list))
560 (let ((fname (buffer-file-name buffer)))
121b8917 561 (when (and fname (string-prefix-p directory fname))
ba83b7b6 562 (with-current-buffer buffer
00fd1147 563 (vc-resynch-buffer fname keep noquery reset-vc-info))))))
74d0991f 564
00fd1147 565(defun vc-resynch-buffer (file &optional keep noquery reset-vc-info)
17f039f3
ER
566 "If FILE is currently visited, resynch its buffer."
567 (if (string= buffer-file-name file)
00fd1147 568 (vc-resynch-window file keep noquery reset-vc-info)
d923f4ac 569 (if (file-directory-p file)
00fd1147 570 (vc-resynch-buffers-in-directory file keep noquery reset-vc-info)
d923f4ac
DN
571 (let ((buffer (get-file-buffer file)))
572 (when buffer
573 (with-current-buffer buffer
00fd1147 574 (vc-resynch-window file keep noquery reset-vc-info))))))
e02d8ae7
DN
575 ;; Try to avoid unnecessary work, a *vc-dir* buffer is only present
576 ;; if this is true.
cf885595 577 (when vc-dir-buffers
e02d8ae7 578 (vc-dir-resynch-file file)))
17f039f3 579
dab68082
ER
580(defun vc-buffer-sync (&optional not-urgent)
581 "Make sure the current buffer and its working file are in sync.
582NOT-URGENT means it is ok to continue if the user says not to save."
583 (when (buffer-modified-p)
584 (if (or vc-suppress-confirm
585 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
586 (save-buffer)
587 (unless not-urgent
588 (error "Aborted")))))
589
83affd96
ER
590;; Command closures
591
dab68082
ER
592;; Set up key bindings for use while editing log messages
593
9af57756 594(defun vc-log-edit (fileset mode backend)
d536a904 595 "Set up `log-edit' for use on FILE."
dab68082 596 (setq default-directory
9af57756 597 (buffer-local-value 'default-directory vc-parent-buffer))
dab68082 598 (log-edit 'vc-finish-logentry
1b10adb6 599 t
e97a42c1
SM
600 `((log-edit-listfun . (lambda ()
601 ;; FIXME: Should expand the list
602 ;; for directories.
603 (mapcar 'file-relative-name
604 ',fileset)))
9af57756
CY
605 (log-edit-diff-function . vc-diff)
606 (log-edit-vc-backend . ,backend)
607 (vc-log-fileset . ,fileset))
09158997
DN
608 nil
609 mode)
dab68082
ER
610 (set-buffer-modified-p nil)
611 (setq buffer-file-name nil))
612
9af57756 613(defun vc-start-logentry (files comment initial-contents msg logbuf mode action &optional after-hook backend)
e97a42c1 614 "Accept a comment for an operation on FILES.
400aec1a 615If COMMENT is nil, pop up a LOGBUF buffer, emit MSG, and set the
83affd96
ER
616action on close to ACTION. If COMMENT is a string and
617INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
618contents of the log entry buffer. If COMMENT is a string and
619INITIAL-CONTENTS is nil, do action immediately as if the user had
620entered COMMENT. If COMMENT is t, also do action immediately with an
621empty comment. Remember the file's buffer in `vc-parent-buffer'
549f324a
GM
622\(current one if no file). Puts the log-entry buffer in major-mode
623MODE, defaulting to `log-edit-mode' if MODE is nil.
9af57756
CY
624AFTER-HOOK specifies the local value for `vc-log-after-operation-hook'.
625BACKEND, if non-nil, specifies a VC backend for the Log Edit buffer."
83affd96 626 (let ((parent
4903369a
ER
627 (if (vc-dispatcher-browsing)
628 ;; If we are called from a directory browser, the parent buffer is
83affd96
ER
629 ;; the current buffer.
630 (current-buffer)
631 (if (and files (equal (length files) 1))
632 (get-file-buffer (car files))
633 (current-buffer)))))
83affd96 634 (if (and comment (not initial-contents))
400aec1a
ER
635 (set-buffer (get-buffer-create logbuf))
636 (pop-to-buffer (get-buffer-create logbuf)))
83affd96
ER
637 (set (make-local-variable 'vc-parent-buffer) parent)
638 (set (make-local-variable 'vc-parent-buffer-name)
639 (concat " from " (buffer-name vc-parent-buffer)))
9af57756 640 (vc-log-edit files mode backend)
83affd96
ER
641 (make-local-variable 'vc-log-after-operation-hook)
642 (when after-hook
643 (setq vc-log-after-operation-hook after-hook))
644 (setq vc-log-operation action)
83affd96
ER
645 (when comment
646 (erase-buffer)
647 (when (stringp comment) (insert comment)))
648 (if (or (not comment) initial-contents)
649 (message "%s Type C-c C-c when done" msg)
650 (vc-finish-logentry (eq comment t)))))
651
74d0991f 652(declare-function vc-dir-move-to-goal-column "vc-dir" ())
e97a42c1
SM
653;; vc-finish-logentry is typically called from a log-edit buffer (see
654;; vc-start-logentry).
83affd96
ER
655(defun vc-finish-logentry (&optional nocomment)
656 "Complete the operation implied by the current log entry.
657Use the contents of the current buffer as a check-in or registration
658comment. If the optional arg NOCOMMENT is non-nil, then don't check
659the buffer contents as a comment."
660 (interactive)
661 ;; Check and record the comment, if any.
662 (unless nocomment
663 (run-hooks 'vc-logentry-check-hook))
664 ;; Sync parent buffer in case the user modified it while editing the comment.
e02d8ae7 665 ;; But not if it is a vc-dir buffer.
83affd96 666 (with-current-buffer vc-parent-buffer
4903369a 667 (or (vc-dispatcher-browsing) (vc-buffer-sync)))
83affd96
ER
668 (unless vc-log-operation
669 (error "No log operation is pending"))
09158997 670
83affd96 671 ;; save the parameters held in buffer-local variables
400aec1a
ER
672 (let ((logbuf (current-buffer))
673 (log-operation vc-log-operation)
e97a42c1
SM
674 ;; FIXME: When coming from VC-Dir, we should check that the
675 ;; set of selected files is still equal to vc-log-fileset,
676 ;; to avoid surprises.
83affd96 677 (log-fileset vc-log-fileset)
83affd96 678 (log-entry (buffer-string))
cb4f9513 679 (after-hook vc-log-after-operation-hook))
83affd96
ER
680 (pop-to-buffer vc-parent-buffer)
681 ;; OK, do it to it
682 (save-excursion
683 (funcall log-operation
684 log-fileset
e97a42c1 685 log-entry))
366ca7f3
MR
686
687 ;; Quit windows on logbuf.
688 (cond
689 ((not logbuf))
690 (vc-delete-logbuf-window
691 (quit-windows-on logbuf t (selected-frame)))
692 (t
693 (quit-windows-on logbuf nil 0)))
694
83affd96
ER
695 ;; Now make sure we see the expanded headers
696 (when log-fileset
697 (mapc
698 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
699 log-fileset))
4903369a 700 (when (vc-dispatcher-browsing)
83affd96
ER
701 (vc-dir-move-to-goal-column))
702 (run-hooks after-hook 'vc-finish-logentry-hook)))
703
b236ab0d
ER
704(defun vc-dispatcher-browsing ()
705 "Are we in a directory browser buffer?"
8243c746
SM
706 (derived-mode-p 'vc-dir-mode))
707
74d0991f
DN
708;; These are unused.
709;; (defun vc-dispatcher-in-fileset-p (fileset)
710;; (let ((member nil))
711;; (while (and (not member) fileset)
712;; (let ((elem (pop fileset)))
713;; (if (if (file-directory-p elem)
714;; (eq t (compare-strings buffer-file-name nil (length elem)
715;; elem nil nil))
716;; (eq (current-buffer) (get-file-buffer elem)))
717;; (setq member t))))
718;; member))
719
720;; (defun vc-dispatcher-selection-set (&optional observer)
721;; "Deduce a set of files to which to apply an operation. Return a cons
722;; cell (SELECTION . FILESET), where SELECTION is what the user chose
723;; and FILES is the flist with any directories replaced by the listed files
724;; within them.
725
726;; If we're in a directory display, the fileset is the list of marked files (if
727;; there is one) else the file on the current line. If not in a directory
728;; display, but the current buffer visits a file, the fileset is a singleton
729;; containing that file. Otherwise, throw an error."
730;; (let ((selection
731;; (cond
732;; ;; Browsing with vc-dir
733;; ((vc-dispatcher-browsing)
734;; ;; If no files are marked, temporarily mark current file
735;; ;; and choose on that basis (so we get subordinate files)
736;; (if (not (vc-dir-marked-files))
737;; (prog2
738;; (vc-dir-mark-file)
739;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
740;; (vc-dir-unmark-all-files t))
741;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
742;; ;; Visiting an eligible file
743;; ((buffer-file-name)
744;; (cons (list buffer-file-name) (list buffer-file-name)))
745;; ;; No eligible file -- if there's a parent buffer, deduce from there
746;; ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
747;; (with-current-buffer vc-parent-buffer
748;; (vc-dispatcher-browsing))))
749;; (with-current-buffer vc-parent-buffer
750;; (vc-dispatcher-selection-set)))
751;; ;; No good set here, throw error
752;; (t (error "No fileset is available here")))))
753;; ;; We assume, in order to avoid unpleasant surprises to the user,
754;; ;; that a fileset is not in good shape to be handed to the user if the
755;; ;; buffers visiting the fileset don't match the on-disk contents.
756;; (unless observer
757;; (save-some-buffers
758;; nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
759;; selection))
b236ab0d 760
d9a3d80e
DN
761(provide 'vc-dispatcher)
762
b1ddeeb7 763;;; vc-dispatcher.el ends here