Replace independent implementations of string-prefix-p
[bpt/emacs.git] / lisp / vc / vc-dispatcher.el
CommitLineData
08a1ccb0 1;;; vc-dispatcher.el -- generic command-dispatcher facility.
92d1eebf 2
acaf905b 3;; Copyright (C) 2008-2012 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
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))))))))))
218
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
d536a904 224 "A command is in progress in this buffer"))))
92d1eebf
ER
225
226(defun vc-exec-after (code)
227 "Eval CODE when the current buffer's process is done.
228If the current buffer has no process, just evaluate CODE.
229Else, 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)
256
257(defvar vc-post-command-functions nil
258 "Hook run at the end of `vc-do-command'.
259Each function is called inside the buffer in which the command was run
260and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
261
262(defvar w32-quote-process-args)
263
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 " ")))
268
269;;;###autoload
270(defun vc-do-command (buffer okstatus command file-or-list &rest flags)
06ad7cd8 271 "Execute a slave command, notifying user and checking for errors.
2888a97e
ER
272Output from COMMAND goes to BUFFER, or the current buffer if
273BUFFER is t. If the destination buffer is not already current,
274set it up properly and erase it. The command is considered
275successful if its exit status does not exceed OKSTATUS (if
276OKSTATUS is nil, that means to ignore error status, if it is
277`async', that means not to wait for termination of the
278subprocess; if it is t it means to ignore all execution errors).
279FILE-OR-LIST is the name of a working file; it may be a list of
280files or be nil (to execute commands that don't expect a file
281name or set of files). If an optional list of FLAGS is present,
4096c5f2
DN
282that is inserted into the command line before the filename.
283Return the return value of the slave command in the synchronous
284case, and the process object in the asynchronous case."
92d1eebf
ER
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
b1d87c10 293 ;; for display in a debug-progress message. If it's fewer than
92d1eebf
ER
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)
299 " "
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)))
2888a97e 307 (vc-setup-buffer buffer))
92d1eebf
ER
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.
4096c5f2 314 (when oldproc (delete-process oldproc)))
92d1eebf
ER
315 (let ((squeezed (remq nil flags))
316 (inhibit-read-only t)
317 (status 0))
318 (when files
319 (setq squeezed (nconc squeezed files)))
b0287b39 320 (let (;; Since some functions need to parse the output
d4efdaea 321 ;; from external commands, set LC_MESSAGES to C.
b0287b39 322 (process-environment (cons "LC_MESSAGES=C" process-environment))
92d1eebf 323 (w32-quote-process-args t))
92d1eebf
ER
324 (if (eq okstatus 'async)
325 ;; Run asynchronously.
326 (let ((proc
327 (let ((process-connection-type nil))
328 (apply 'start-file-process command (current-buffer)
329 command squeezed))))
4096c5f2
DN
330 (when vc-command-messages
331 (message "Running %s in background..." full-command))
92d1eebf
ER
332 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
333 (set-process-filter proc 'vc-process-filter)
4096c5f2
DN
334 (setq status proc)
335 (when vc-command-messages
336 (vc-exec-after
337 `(message "Running %s in background... done" ',full-command))))
b1d87c10 338 ;; Run synchronously
92d1eebf
ER
339 (when vc-command-messages
340 (message "Running %s in foreground..." full-command))
341 (let ((buffer-undo-list t))
342 (setq status (apply 'process-file command nil t nil squeezed)))
343 (when (and (not (eq t okstatus))
344 (or (not (integerp status))
345 (and okstatus (< okstatus status))))
346 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
347 (pop-to-buffer (current-buffer))
348 (goto-char (point-min))
349 (shrink-window-if-larger-than-buffer))
350 (error "Running %s...FAILED (%s)" full-command
4096c5f2
DN
351 (if (integerp status) (format "status %d" status) status)))
352 (when vc-command-messages
353 (message "Running %s...OK = %d" full-command status))))
92d1eebf
ER
354 (vc-exec-after
355 `(run-hook-with-args 'vc-post-command-functions
356 ',command ',file-or-list ',flags))
357 status))))
358
9bfe5783
CY
359(defun vc-do-async-command (buffer root command &rest args)
360 "Run COMMAND asynchronously with ARGS, displaying the result.
361Send the output to BUFFER, which should be a buffer or the name
362of a buffer, which is created.
363ROOT should be the directory in which the command should be run.
364Display the buffer in some window, but don't select it."
365 (let* ((dir default-directory)
3ab713fd 366 (inhibit-read-only t)
9bfe5783
CY
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))
659114fd 377 (insert "Running \"" command)
9bfe5783
CY
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
a2b6e5d6
CY
386 (set-window-start window new-window-start))
387 buffer))
388
389(defun vc-set-async-update (process-buffer)
390 "Set a `vc-exec-after' action appropriate to the current buffer.
391This action will update the current buffer after the current
392asynchronous VC command has completed. PROCESS-BUFFER is the
393buffer for the asynchronous VC process.
394
395If the current buffer is a VC Dir buffer, call `vc-dir-refresh'.
396If the current buffer is a Dired buffer, revert it."
397 (let* ((buf (current-buffer))
398 (tick (buffer-modified-tick buf)))
399 (cond
400 ((derived-mode-p 'vc-dir-mode)
401 (with-current-buffer process-buffer
402 (vc-exec-after
403 `(if (buffer-live-p ,buf)
404 (with-current-buffer ,buf
405 (vc-dir-refresh))))))
406 ((derived-mode-p 'dired-mode)
407 (with-current-buffer process-buffer
408 (vc-exec-after
409 `(and (buffer-live-p ,buf)
410 (= (buffer-modified-tick ,buf) ,tick)
411 (with-current-buffer ,buf
412 (revert-buffer)))))))))
9bfe5783 413
17f039f3 414;; These functions are used to ensure that the view the user sees is up to date
b1d87c10 415;; even if the dispatcher client mode has messed with file contents (as in,
17f039f3
ER
416;; for example, VCS keyword expansion).
417
418(declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
419
420(defun vc-position-context (posn)
421 "Save a bit of the text around POSN in the current buffer.
422Used to help us find the corresponding position again later
423if markers are destroyed or corrupted."
424 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
425 ;; rcs.el mode.
426 (list posn
427 (buffer-size)
428 (buffer-substring posn
429 (min (point-max) (+ posn 100)))))
430
431(defun vc-find-position-by-context (context)
432 "Return the position of CONTEXT in the current buffer.
433If CONTEXT cannot be found, return nil."
434 (let ((context-string (nth 2 context)))
435 (if (equal "" context-string)
436 (point-max)
437 (save-excursion
438 (let ((diff (- (nth 1 context) (buffer-size))))
439 (when (< diff 0) (setq diff (- diff)))
440 (goto-char (nth 0 context))
441 (if (or (search-forward context-string nil t)
442 ;; Can't use search-backward since the match may continue
443 ;; after point.
444 (progn (goto-char (- (point) diff (length context-string)))
445 ;; goto-char doesn't signal an error at
446 ;; beginning of buffer like backward-char would
447 (search-forward context-string nil t)))
448 ;; to beginning of OSTRING
449 (- (point) (length context-string))))))))
450
451(defun vc-context-matches-p (posn context)
452 "Return t if POSN matches CONTEXT, nil otherwise."
453 (let* ((context-string (nth 2 context))
454 (len (length context-string))
455 (end (+ posn len)))
456 (if (> end (1+ (buffer-size)))
457 nil
458 (string= context-string (buffer-substring posn end)))))
459
460(defun vc-buffer-context ()
461 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
462Used by `vc-restore-buffer-context' to later restore the context."
463 (let ((point-context (vc-position-context (point)))
464 ;; Use mark-marker to avoid confusion in transient-mark-mode.
9ba4a350 465 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
17f039f3
ER
466 (vc-position-context (mark-marker))))
467 ;; Make the right thing happen in transient-mark-mode.
9ba4a350
ER
468 (mark-active nil))
469 (list point-context mark-context nil)))
17f039f3
ER
470
471(defun vc-restore-buffer-context (context)
472 "Restore point/mark, and reparse any affected compilation buffers.
473CONTEXT is that which `vc-buffer-context' returns."
474 (let ((point-context (nth 0 context))
9ba4a350 475 (mark-context (nth 1 context)))
17f039f3
ER
476 ;; if necessary, restore point and mark
477 (if (not (vc-context-matches-p (point) point-context))
478 (let ((new-point (vc-find-position-by-context point-context)))
479 (when new-point (goto-char new-point))))
480 (and mark-active
481 mark-context
482 (not (vc-context-matches-p (mark) mark-context))
483 (let ((new-mark (vc-find-position-by-context mark-context)))
484 (when new-mark (set-mark new-mark))))))
485
486(defun vc-revert-buffer-internal (&optional arg no-confirm)
487 "Revert buffer, keeping point and mark where user expects them.
488Try to be clever in the face of changes due to expanded version-control
489key words. This is important for typeahead to work as expected.
490ARG and NO-CONFIRM are passed on to `revert-buffer'."
491 (interactive "P")
492 (widen)
493 (let ((context (vc-buffer-context)))
494 ;; Use save-excursion here, because it may be able to restore point
495 ;; and mark properly even in cases where vc-restore-buffer-context
496 ;; would fail. However, save-excursion might also get it wrong --
497 ;; in this case, vc-restore-buffer-context gives it a second try.
498 (save-excursion
499 ;; t means don't call normal-mode;
500 ;; that's to preserve various minor modes.
501 (revert-buffer arg no-confirm t))
502 (vc-restore-buffer-context context)))
503
8c6eab5a
SM
504(defvar vc-mode-line-hook nil)
505(make-variable-buffer-local 'vc-mode-line-hook)
506(put 'vc-mode-line-hook 'permanent-local t)
507
00fd1147 508(defun vc-resynch-window (file &optional keep noquery reset-vc-info)
17f039f3
ER
509 "If FILE is in the current buffer, either revert or unvisit it.
510The choice between revert (to see expanded keywords) and unvisit
511depends on KEEP. NOQUERY if non-nil inhibits confirmation for
512reverting. NOQUERY should be t *only* if it is known the only
513difference between the buffer and the file is due to
514modifications by the dispatcher client code, rather than user
515editing!"
516 (and (string= buffer-file-name file)
517 (if keep
cf885595 518 (when (file-exists-p file)
00fd1147
DN
519 (when reset-vc-info
520 (vc-file-clearprops file))
17f039f3 521 (vc-revert-buffer-internal t noquery)
3c53763c
DN
522
523 ;; VC operations might toggle the read-only state. In
524 ;; that case we need to adjust the `view-mode' status
525 ;; when `view-read-only' is non-nil.
17f039f3
ER
526 (and view-read-only
527 (if (file-writable-p file)
528 (and view-mode
529 (let ((view-old-buffer-read-only nil))
530 (view-mode-exit)))
531 (and (not view-mode)
532 (not (eq (get major-mode 'mode-class) 'special))
533 (view-mode-enter))))
3c53763c 534
8c6eab5a
SM
535 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
536 (run-hook-with-args 'vc-mode-line-hook buffer-file-name))
17f039f3
ER
537 (kill-buffer (current-buffer)))))
538
74d0991f 539(declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
d923f4ac 540
00fd1147 541(defun vc-resynch-buffers-in-directory (directory &optional keep noquery reset-vc-info)
d923f4ac
DN
542 "Resync all buffers that visit files in DIRECTORY."
543 (dolist (buffer (buffer-list))
544 (let ((fname (buffer-file-name buffer)))
121b8917 545 (when (and fname (string-prefix-p directory fname))
ba83b7b6 546 (with-current-buffer buffer
00fd1147 547 (vc-resynch-buffer fname keep noquery reset-vc-info))))))
74d0991f 548
00fd1147 549(defun vc-resynch-buffer (file &optional keep noquery reset-vc-info)
17f039f3
ER
550 "If FILE is currently visited, resynch its buffer."
551 (if (string= buffer-file-name file)
00fd1147 552 (vc-resynch-window file keep noquery reset-vc-info)
d923f4ac 553 (if (file-directory-p file)
00fd1147 554 (vc-resynch-buffers-in-directory file keep noquery reset-vc-info)
d923f4ac
DN
555 (let ((buffer (get-file-buffer file)))
556 (when buffer
557 (with-current-buffer buffer
00fd1147 558 (vc-resynch-window file keep noquery reset-vc-info))))))
e02d8ae7
DN
559 ;; Try to avoid unnecessary work, a *vc-dir* buffer is only present
560 ;; if this is true.
cf885595 561 (when vc-dir-buffers
e02d8ae7 562 (vc-dir-resynch-file file)))
17f039f3 563
dab68082
ER
564(defun vc-buffer-sync (&optional not-urgent)
565 "Make sure the current buffer and its working file are in sync.
566NOT-URGENT means it is ok to continue if the user says not to save."
567 (when (buffer-modified-p)
568 (if (or vc-suppress-confirm
569 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
570 (save-buffer)
571 (unless not-urgent
572 (error "Aborted")))))
573
83affd96
ER
574;; Command closures
575
dab68082
ER
576;; Set up key bindings for use while editing log messages
577
09158997 578(defun vc-log-edit (fileset mode)
d536a904 579 "Set up `log-edit' for use on FILE."
dab68082
ER
580 (setq default-directory
581 (with-current-buffer vc-parent-buffer default-directory))
582 (log-edit 'vc-finish-logentry
583 nil
e97a42c1
SM
584 `((log-edit-listfun . (lambda ()
585 ;; FIXME: Should expand the list
586 ;; for directories.
587 (mapcar 'file-relative-name
588 ',fileset)))
09158997
DN
589 (log-edit-diff-function . (lambda () (vc-diff nil))))
590 nil
591 mode)
dab68082 592 (set (make-local-variable 'vc-log-fileset) fileset)
dab68082
ER
593 (set-buffer-modified-p nil)
594 (setq buffer-file-name nil))
595
e97a42c1
SM
596(defun vc-start-logentry (files comment initial-contents msg logbuf mode action &optional after-hook)
597 "Accept a comment for an operation on FILES.
400aec1a 598If COMMENT is nil, pop up a LOGBUF buffer, emit MSG, and set the
83affd96
ER
599action on close to ACTION. If COMMENT is a string and
600INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
601contents of the log entry buffer. If COMMENT is a string and
602INITIAL-CONTENTS is nil, do action immediately as if the user had
603entered COMMENT. If COMMENT is t, also do action immediately with an
604empty comment. Remember the file's buffer in `vc-parent-buffer'
549f324a
GM
605\(current one if no file). Puts the log-entry buffer in major-mode
606MODE, defaulting to `log-edit-mode' if MODE is nil.
607AFTER-HOOK specifies the local value for `vc-log-after-operation-hook'."
83affd96 608 (let ((parent
4903369a
ER
609 (if (vc-dispatcher-browsing)
610 ;; If we are called from a directory browser, the parent buffer is
83affd96
ER
611 ;; the current buffer.
612 (current-buffer)
613 (if (and files (equal (length files) 1))
614 (get-file-buffer (car files))
615 (current-buffer)))))
83affd96 616 (if (and comment (not initial-contents))
400aec1a
ER
617 (set-buffer (get-buffer-create logbuf))
618 (pop-to-buffer (get-buffer-create logbuf)))
83affd96
ER
619 (set (make-local-variable 'vc-parent-buffer) parent)
620 (set (make-local-variable 'vc-parent-buffer-name)
621 (concat " from " (buffer-name vc-parent-buffer)))
09158997 622 (vc-log-edit files mode)
83affd96
ER
623 (make-local-variable 'vc-log-after-operation-hook)
624 (when after-hook
625 (setq vc-log-after-operation-hook after-hook))
626 (setq vc-log-operation action)
83affd96
ER
627 (when comment
628 (erase-buffer)
629 (when (stringp comment) (insert comment)))
630 (if (or (not comment) initial-contents)
631 (message "%s Type C-c C-c when done" msg)
632 (vc-finish-logentry (eq comment t)))))
633
74d0991f 634(declare-function vc-dir-move-to-goal-column "vc-dir" ())
e97a42c1
SM
635;; vc-finish-logentry is typically called from a log-edit buffer (see
636;; vc-start-logentry).
83affd96
ER
637(defun vc-finish-logentry (&optional nocomment)
638 "Complete the operation implied by the current log entry.
639Use the contents of the current buffer as a check-in or registration
640comment. If the optional arg NOCOMMENT is non-nil, then don't check
641the buffer contents as a comment."
642 (interactive)
643 ;; Check and record the comment, if any.
644 (unless nocomment
645 (run-hooks 'vc-logentry-check-hook))
646 ;; Sync parent buffer in case the user modified it while editing the comment.
e02d8ae7 647 ;; But not if it is a vc-dir buffer.
83affd96 648 (with-current-buffer vc-parent-buffer
4903369a 649 (or (vc-dispatcher-browsing) (vc-buffer-sync)))
83affd96
ER
650 (unless vc-log-operation
651 (error "No log operation is pending"))
09158997 652
83affd96 653 ;; save the parameters held in buffer-local variables
400aec1a
ER
654 (let ((logbuf (current-buffer))
655 (log-operation vc-log-operation)
e97a42c1
SM
656 ;; FIXME: When coming from VC-Dir, we should check that the
657 ;; set of selected files is still equal to vc-log-fileset,
658 ;; to avoid surprises.
83affd96 659 (log-fileset vc-log-fileset)
83affd96 660 (log-entry (buffer-string))
cb4f9513 661 (after-hook vc-log-after-operation-hook))
83affd96
ER
662 (pop-to-buffer vc-parent-buffer)
663 ;; OK, do it to it
664 (save-excursion
665 (funcall log-operation
666 log-fileset
e97a42c1 667 log-entry))
366ca7f3
MR
668
669 ;; Quit windows on logbuf.
670 (cond
671 ((not logbuf))
672 (vc-delete-logbuf-window
673 (quit-windows-on logbuf t (selected-frame)))
674 (t
675 (quit-windows-on logbuf nil 0)))
676
83affd96
ER
677 ;; Now make sure we see the expanded headers
678 (when log-fileset
679 (mapc
680 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
681 log-fileset))
4903369a 682 (when (vc-dispatcher-browsing)
83affd96
ER
683 (vc-dir-move-to-goal-column))
684 (run-hooks after-hook 'vc-finish-logentry-hook)))
685
b236ab0d
ER
686(defun vc-dispatcher-browsing ()
687 "Are we in a directory browser buffer?"
8243c746
SM
688 (derived-mode-p 'vc-dir-mode))
689
74d0991f
DN
690;; These are unused.
691;; (defun vc-dispatcher-in-fileset-p (fileset)
692;; (let ((member nil))
693;; (while (and (not member) fileset)
694;; (let ((elem (pop fileset)))
695;; (if (if (file-directory-p elem)
696;; (eq t (compare-strings buffer-file-name nil (length elem)
697;; elem nil nil))
698;; (eq (current-buffer) (get-file-buffer elem)))
699;; (setq member t))))
700;; member))
701
702;; (defun vc-dispatcher-selection-set (&optional observer)
703;; "Deduce a set of files to which to apply an operation. Return a cons
704;; cell (SELECTION . FILESET), where SELECTION is what the user chose
705;; and FILES is the flist with any directories replaced by the listed files
706;; within them.
707
708;; If we're in a directory display, the fileset is the list of marked files (if
709;; there is one) else the file on the current line. If not in a directory
710;; display, but the current buffer visits a file, the fileset is a singleton
711;; containing that file. Otherwise, throw an error."
712;; (let ((selection
713;; (cond
714;; ;; Browsing with vc-dir
715;; ((vc-dispatcher-browsing)
716;; ;; If no files are marked, temporarily mark current file
717;; ;; and choose on that basis (so we get subordinate files)
718;; (if (not (vc-dir-marked-files))
719;; (prog2
720;; (vc-dir-mark-file)
721;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
722;; (vc-dir-unmark-all-files t))
723;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
724;; ;; Visiting an eligible file
725;; ((buffer-file-name)
726;; (cons (list buffer-file-name) (list buffer-file-name)))
727;; ;; No eligible file -- if there's a parent buffer, deduce from there
728;; ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
729;; (with-current-buffer vc-parent-buffer
730;; (vc-dispatcher-browsing))))
731;; (with-current-buffer vc-parent-buffer
732;; (vc-dispatcher-selection-set)))
733;; ;; No good set here, throw error
734;; (t (error "No fileset is available here")))))
735;; ;; We assume, in order to avoid unpleasant surprises to the user,
736;; ;; that a fileset is not in good shape to be handed to the user if the
737;; ;; buffers visiting the fileset don't match the on-disk contents.
738;; (unless observer
739;; (save-some-buffers
740;; nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
741;; selection))
b236ab0d 742
d9a3d80e
DN
743(provide 'vc-dispatcher)
744
b1ddeeb7 745;;; vc-dispatcher.el ends here