Prevent some compilaation warnings.
[bpt/emacs.git] / lisp / vc-dispatcher.el
CommitLineData
dab68082 17;;; vc-dispatcher.el -- generic command-dispatcher facility.
92d1eebf
ER
2
3;; Copyright (C) 2008
4;; Free Software Foundation, Inc.
5
6;; Author: FSF (see below for full credits)
7;; Maintainer: Eric S. Raymond <esr@thyrsus.com>
8;; Keywords: tools
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
39;; to version-control systems.
40;;
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.
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
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.
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
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.
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
038608c7
ER
83;; treat all entries with the same state as the currently selected one as
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
89;; comand, synchronously or asynchronously, making the output available
90;; in a command log buffer. Two other functions, (vc-start-annotation) and
91;; (vc-finish-logentry), allow you to associate a command closure with an
92;; abbotation buffer so that when the user confirms the comment the closure
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
c8d4f1a0
ER
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.
103;;
104;; When the client mode adds a local mode-line-hook to a buffer, it
105;; will be called with the buffer file name as argument whenever the
106;; dispatcher resynchs the buffer.
821677fb 107
d99d06ec
ER
108;; To do:
109;;
110;; - vc-dir-kill-dir-status-process should not be specific to dir-status,
edbde353
ER
111;; it should work for other async commands done through vc-do-command
112;; as well,
d99d06ec
ER
113;;
114;; - the *VC-log* buffer needs font-locking.
115;;
d99d06ec
ER
116;; - vc-dir needs mouse bindings.
117;;
d99d06ec
ER
118;; - vc-dir toolbar needs more icons.
119;;
edbde353
ER
120;; - vc-dir-menu-map-filter hook call needs to be moved to vc.el.
121;;
d99d06ec 122
d9a3d80e 123(require 'ewoc)
92d1eebf 124
4f61cc3e 125(eval-when-compile
4903369a 126 (require 'cl))
4f61cc3e 127
83affd96
ER
128;; General customization
129
130(defcustom vc-logentry-check-hook nil
131 "Normal hook run by `vc-finish-logentry'.
132Use this to impose your own rules on the entry in addition to any the
4f61cc3e 133dispatcher client mode imposes itself."
83affd96
ER
134 :type 'hook
135 :group 'vc)
136
17f039f3
ER
137(defcustom vc-delete-logbuf-window t
138 "If non-nil, delete the *VC-log* buffer and window after each logical action.
139If nil, bury that buffer instead.
140This is most useful if you have multiple windows on a frame and would like to
141preserve the setting."
142 :type 'boolean
143 :group 'vc)
144
145(defcustom vc-command-messages nil
146 "If non-nil, display run messages from back-end commands."
147 :type 'boolean
148 :group 'vc)
149
821677fb
ER
150(defcustom vc-suppress-confirm nil
151 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
152 :type 'boolean
153 :group 'vc)
154
83affd96 155;; Variables the user doesn't need to know about.
17f039f3 156
83affd96
ER
157(defvar vc-log-operation nil)
158(defvar vc-log-after-operation-hook nil)
159(defvar vc-log-fileset)
160(defvar vc-log-extra)
dab68082 161(defvar vc-client-mode
83affd96
ER
162
163;; In a log entry buffer, this is a local variable
164;; that points to the buffer for which it was made
4903369a 165;; (either a file, or a VC directory buffer).
83affd96
ER
166(defvar vc-parent-buffer nil)
167(put 'vc-parent-buffer 'permanent-local t)
168(defvar vc-parent-buffer-name nil)
169(put 'vc-parent-buffer-name 'permanent-local t)
170
92d1eebf
ER
171;; Common command execution logic
172
173(defun vc-process-filter (p s)
174 "An alternative output filter for async process P.
175One difference with the default filter is that this inserts S after markers.
176Another is that undo information is not kept."
177 (let ((buffer (process-buffer p)))
178 (when (buffer-live-p buffer)
179 (with-current-buffer buffer
180 (save-excursion
181 (let ((buffer-undo-list t)
182 (inhibit-read-only t))
183 (goto-char (process-mark p))
184 (insert s)
185 (set-marker (process-mark p) (point))))))))
186
7265c6e8
ER
187(defun vc-setup-buffer (buf)
188 "Prepare BUF for executing a slave command and make it current."
92d1eebf
ER
189 (let ((camefrom (current-buffer))
190 (olddir default-directory))
191 (set-buffer (get-buffer-create buf))
192 (kill-all-local-variables)
193 (set (make-local-variable 'vc-parent-buffer) camefrom)
194 (set (make-local-variable 'vc-parent-buffer-name)
195 (concat " from " (buffer-name camefrom)))
196 (setq default-directory olddir)
197 (let ((buffer-undo-list t)
198 (inhibit-read-only t))
199 (erase-buffer))))
200
201(defvar vc-sentinel-movepoint) ;Dynamically scoped.
202
203(defun vc-process-sentinel (p s)
204 (let ((previous (process-get p 'vc-previous-sentinel))
205 (buf (process-buffer p)))
206 ;; Impatient users sometime kill "slow" buffers; check liveness
207 ;; to avoid "error in process sentinel: Selecting deleted buffer".
208 (when (buffer-live-p buf)
209 (when previous (funcall previous p s))
210 (with-current-buffer buf
211 (setq mode-line-process
212 (let ((status (process-status p)))
213 ;; Leave mode-line uncluttered, normally.
214 (unless (eq 'exit status)
215 (format " (%s)" status))))
216 (let (vc-sentinel-movepoint)
217 ;; Normally, we want async code such as sentinels to not move point.
218 (save-excursion
219 (goto-char (process-mark p))
220 (let ((cmds (process-get p 'vc-sentinel-commands)))
221 (process-put p 'vc-sentinel-commands nil)
222 (dolist (cmd cmds)
223 ;; Each sentinel may move point and the next one should be run
224 ;; at that new point. We could get the same result by having
225 ;; each sentinel read&set process-mark, but since `cmd' needs
226 ;; to work both for async and sync processes, this would be
227 ;; difficult to achieve.
228 (vc-exec-after cmd))))
229 ;; But sometimes the sentinels really want to move point.
230 (when vc-sentinel-movepoint
231 (let ((win (get-buffer-window (current-buffer) 0)))
232 (if (not win)
233 (goto-char vc-sentinel-movepoint)
234 (with-selected-window win
235 (goto-char vc-sentinel-movepoint))))))))))
236
237(defun vc-set-mode-line-busy-indicator ()
238 (setq mode-line-process
239 (concat " " (propertize "[waiting...]"
240 'face 'mode-line-emphasis
241 'help-echo
242 "A VC command is in progress in this buffer"))))
243
244(defun vc-exec-after (code)
245 "Eval CODE when the current buffer's process is done.
246If the current buffer has no process, just evaluate CODE.
247Else, add CODE to the process' sentinel."
248 (let ((proc (get-buffer-process (current-buffer))))
249 (cond
250 ;; If there's no background process, just execute the code.
251 ;; We used to explicitly call delete-process on exited processes,
252 ;; but this led to timing problems causing process output to be
253 ;; lost. Terminated processes get deleted automatically
254 ;; anyway. -- cyd
255 ((or (null proc) (eq (process-status proc) 'exit))
256 ;; Make sure we've read the process's output before going further.
257 (when proc (accept-process-output proc))
258 (eval code))
259 ;; If a process is running, add CODE to the sentinel
260 ((eq (process-status proc) 'run)
261 (vc-set-mode-line-busy-indicator)
262 (let ((previous (process-sentinel proc)))
263 (unless (eq previous 'vc-process-sentinel)
264 (process-put proc 'vc-previous-sentinel previous))
265 (set-process-sentinel proc 'vc-process-sentinel))
266 (process-put proc 'vc-sentinel-commands
267 ;; We keep the code fragments in the order given
268 ;; so that vc-diff-finish's message shows up in
269 ;; the presence of non-nil vc-command-messages.
270 (append (process-get proc 'vc-sentinel-commands)
271 (list code))))
272 (t (error "Unexpected process state"))))
273 nil)
274
275(defvar vc-post-command-functions nil
276 "Hook run at the end of `vc-do-command'.
277Each function is called inside the buffer in which the command was run
278and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
279
280(defvar w32-quote-process-args)
281
282(defun vc-delistify (filelist)
283 "Smash a FILELIST into a file list string suitable for info messages."
284 ;; FIXME what about file names with spaces?
285 (if (not filelist) "." (mapconcat 'identity filelist " ")))
286
287;;;###autoload
288(defun vc-do-command (buffer okstatus command file-or-list &rest flags)
06ad7cd8 289 "Execute a slave command, notifying user and checking for errors.
2888a97e
ER
290Output from COMMAND goes to BUFFER, or the current buffer if
291BUFFER is t. If the destination buffer is not already current,
292set it up properly and erase it. The command is considered
293successful if its exit status does not exceed OKSTATUS (if
294OKSTATUS is nil, that means to ignore error status, if it is
295`async', that means not to wait for termination of the
296subprocess; if it is t it means to ignore all execution errors).
297FILE-OR-LIST is the name of a working file; it may be a list of
298files or be nil (to execute commands that don't expect a file
299name or set of files). If an optional list of FLAGS is present,
92d1eebf
ER
300that is inserted into the command line before the filename."
301 ;; FIXME: file-relative-name can return a bogus result because
302 ;; it doesn't look at the actual file-system to see if symlinks
303 ;; come into play.
304 (let* ((files
305 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
306 (if (listp file-or-list) file-or-list (list file-or-list))))
307 (full-command
308 ;; What we're doing here is preparing a version of the command
309 ;; for display in a debug-progess message. If it's fewer than
310 ;; 20 characters display the entire command (without trailing
311 ;; newline). Otherwise display the first 20 followed by an ellipsis.
312 (concat (if (string= (substring command -1) "\n")
313 (substring command 0 -1)
314 command)
315 " "
316 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
317 " " (vc-delistify files))))
318 (save-current-buffer
319 (unless (or (eq buffer t)
320 (and (stringp buffer)
321 (string= (buffer-name) buffer))
322 (eq buffer (current-buffer)))
2888a97e 323 (vc-setup-buffer buffer))
92d1eebf
ER
324 ;; If there's some previous async process still running, just kill it.
325 (let ((oldproc (get-buffer-process (current-buffer))))
326 ;; If we wanted to wait for oldproc to finish before doing
327 ;; something, we'd have used vc-eval-after.
328 ;; Use `delete-process' rather than `kill-process' because we don't
329 ;; want any of its output to appear from now on.
330 (if oldproc (delete-process oldproc)))
331 (let ((squeezed (remq nil flags))
332 (inhibit-read-only t)
333 (status 0))
334 (when files
335 (setq squeezed (nconc squeezed files)))
336 (let ((exec-path (append vc-path exec-path))
337 ;; Add vc-path to PATH for the execution of this command.
338 (process-environment
339 (cons (concat "PATH=" (getenv "PATH")
340 path-separator
341 (mapconcat 'identity vc-path path-separator))
342 process-environment))
343 (w32-quote-process-args t))
344 (when (and (eq okstatus 'async) (file-remote-p default-directory))
345 ;; start-process does not support remote execution
346 (setq okstatus nil))
347 (if (eq okstatus 'async)
348 ;; Run asynchronously.
349 (let ((proc
350 (let ((process-connection-type nil))
351 (apply 'start-file-process command (current-buffer)
352 command squeezed))))
353 (if vc-command-messages
354 (message "Running %s in background..." full-command))
355 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
356 (set-process-filter proc 'vc-process-filter)
357 (vc-exec-after
358 `(if vc-command-messages
359 (message "Running %s in background... done" ',full-command))))
360 ;; Run synchrously
361 (when vc-command-messages
362 (message "Running %s in foreground..." full-command))
363 (let ((buffer-undo-list t))
364 (setq status (apply 'process-file command nil t nil squeezed)))
365 (when (and (not (eq t okstatus))
366 (or (not (integerp status))
367 (and okstatus (< okstatus status))))
368 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
369 (pop-to-buffer (current-buffer))
370 (goto-char (point-min))
371 (shrink-window-if-larger-than-buffer))
372 (error "Running %s...FAILED (%s)" full-command
373 (if (integerp status) (format "status %d" status) status))))
374 ;; We're done. But don't emit a status message if running
375 ;; asychronously, it would just mislead.
376 (if (and vc-command-messages (not (eq okstatus 'async)))
377 (message "Running %s...OK = %d" full-command status)))
378 (vc-exec-after
379 `(run-hook-with-args 'vc-post-command-functions
380 ',command ',file-or-list ',flags))
381 status))))
382
17f039f3
ER
383;; These functions are used to ensure that the view the user sees is up to date
384;; even if the dispatcher client mode has messed with file contents (as in,
385;; for example, VCS keyword expansion).
386
387(declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
388
389(defun vc-position-context (posn)
390 "Save a bit of the text around POSN in the current buffer.
391Used to help us find the corresponding position again later
392if markers are destroyed or corrupted."
393 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
394 ;; rcs.el mode.
395 (list posn
396 (buffer-size)
397 (buffer-substring posn
398 (min (point-max) (+ posn 100)))))
399
400(defun vc-find-position-by-context (context)
401 "Return the position of CONTEXT in the current buffer.
402If CONTEXT cannot be found, return nil."
403 (let ((context-string (nth 2 context)))
404 (if (equal "" context-string)
405 (point-max)
406 (save-excursion
407 (let ((diff (- (nth 1 context) (buffer-size))))
408 (when (< diff 0) (setq diff (- diff)))
409 (goto-char (nth 0 context))
410 (if (or (search-forward context-string nil t)
411 ;; Can't use search-backward since the match may continue
412 ;; after point.
413 (progn (goto-char (- (point) diff (length context-string)))
414 ;; goto-char doesn't signal an error at
415 ;; beginning of buffer like backward-char would
416 (search-forward context-string nil t)))
417 ;; to beginning of OSTRING
418 (- (point) (length context-string))))))))
419
420(defun vc-context-matches-p (posn context)
421 "Return t if POSN matches CONTEXT, nil otherwise."
422 (let* ((context-string (nth 2 context))
423 (len (length context-string))
424 (end (+ posn len)))
425 (if (> end (1+ (buffer-size)))
426 nil
427 (string= context-string (buffer-substring posn end)))))
428
429(defun vc-buffer-context ()
430 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
431Used by `vc-restore-buffer-context' to later restore the context."
432 (let ((point-context (vc-position-context (point)))
433 ;; Use mark-marker to avoid confusion in transient-mark-mode.
9ba4a350 434 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
17f039f3
ER
435 (vc-position-context (mark-marker))))
436 ;; Make the right thing happen in transient-mark-mode.
9ba4a350
ER
437 (mark-active nil))
438 (list point-context mark-context nil)))
17f039f3
ER
439
440(defun vc-restore-buffer-context (context)
441 "Restore point/mark, and reparse any affected compilation buffers.
442CONTEXT is that which `vc-buffer-context' returns."
443 (let ((point-context (nth 0 context))
9ba4a350 444 (mark-context (nth 1 context)))
17f039f3
ER
445 ;; if necessary, restore point and mark
446 (if (not (vc-context-matches-p (point) point-context))
447 (let ((new-point (vc-find-position-by-context point-context)))
448 (when new-point (goto-char new-point))))
449 (and mark-active
450 mark-context
451 (not (vc-context-matches-p (mark) mark-context))
452 (let ((new-mark (vc-find-position-by-context mark-context)))
453 (when new-mark (set-mark new-mark))))))
454
455(defun vc-revert-buffer-internal (&optional arg no-confirm)
456 "Revert buffer, keeping point and mark where user expects them.
457Try to be clever in the face of changes due to expanded version-control
458key words. This is important for typeahead to work as expected.
459ARG and NO-CONFIRM are passed on to `revert-buffer'."
460 (interactive "P")
461 (widen)
462 (let ((context (vc-buffer-context)))
463 ;; Use save-excursion here, because it may be able to restore point
464 ;; and mark properly even in cases where vc-restore-buffer-context
465 ;; would fail. However, save-excursion might also get it wrong --
466 ;; in this case, vc-restore-buffer-context gives it a second try.
467 (save-excursion
468 ;; t means don't call normal-mode;
469 ;; that's to preserve various minor modes.
470 (revert-buffer arg no-confirm t))
471 (vc-restore-buffer-context context)))
472
473(defun vc-resynch-window (file &optional keep noquery)
474 "If FILE is in the current buffer, either revert or unvisit it.
475The choice between revert (to see expanded keywords) and unvisit
476depends on KEEP. NOQUERY if non-nil inhibits confirmation for
477reverting. NOQUERY should be t *only* if it is known the only
478difference between the buffer and the file is due to
479modifications by the dispatcher client code, rather than user
480editing!"
481 (and (string= buffer-file-name file)
482 (if keep
483 (progn
484 (vc-revert-buffer-internal t noquery)
485 ;; TODO: Adjusting view mode might no longer be necessary
486 ;; after RMS change to files.el of 1999-08-08. Investigate
487 ;; this when we install the new VC.
488 (and view-read-only
489 (if (file-writable-p file)
490 (and view-mode
491 (let ((view-old-buffer-read-only nil))
492 (view-mode-exit)))
493 (and (not view-mode)
494 (not (eq (get major-mode 'mode-class) 'special))
495 (view-mode-enter))))
c8d4f1a0 496 (run-hook-with-args 'modeline-hook buffer-file-name))
17f039f3
ER
497 (kill-buffer (current-buffer)))))
498
499(defun vc-resynch-buffer (file &optional keep noquery)
500 "If FILE is currently visited, resynch its buffer."
501 (if (string= buffer-file-name file)
502 (vc-resynch-window file keep noquery)
503 (let ((buffer (get-file-buffer file)))
504 (when buffer
505 (with-current-buffer buffer
506 (vc-resynch-window file keep noquery)))))
4fe2e5ed 507 (vc-directory-resynch-file file))
17f039f3 508
dab68082
ER
509(defun vc-buffer-sync (&optional not-urgent)
510 "Make sure the current buffer and its working file are in sync.
511NOT-URGENT means it is ok to continue if the user says not to save."
512 (when (buffer-modified-p)
513 (if (or vc-suppress-confirm
514 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
515 (save-buffer)
516 (unless not-urgent
517 (error "Aborted")))))
518
83affd96
ER
519;; Command closures
520
dab68082
ER
521;; Set up key bindings for use while editing log messages
522
523(defun vc-log-edit (fileset)
524 "Set up `log-edit' for use with VC on FILE."
525 (setq default-directory
526 (with-current-buffer vc-parent-buffer default-directory))
527 (log-edit 'vc-finish-logentry
528 nil
529 `((log-edit-listfun . (lambda () ',fileset))
530 (log-edit-diff-function . (lambda () (vc-diff nil)))))
531 (set (make-local-variable 'vc-log-fileset) fileset)
532 (make-local-variable 'vc-log-extra)
533 (set-buffer-modified-p nil)
534 (setq buffer-file-name nil))
535
83affd96
ER
536(defun vc-start-logentry (files extra comment initial-contents msg action &optional after-hook)
537 "Accept a comment for an operation on FILES with extra data EXTRA.
538If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
539action on close to ACTION. If COMMENT is a string and
540INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
541contents of the log entry buffer. If COMMENT is a string and
542INITIAL-CONTENTS is nil, do action immediately as if the user had
543entered COMMENT. If COMMENT is t, also do action immediately with an
544empty comment. Remember the file's buffer in `vc-parent-buffer'
545\(current one if no file). AFTER-HOOK specifies the local value
546for `vc-log-after-operation-hook'."
547 (let ((parent
4903369a
ER
548 (if (vc-dispatcher-browsing)
549 ;; If we are called from a directory browser, the parent buffer is
83affd96
ER
550 ;; the current buffer.
551 (current-buffer)
552 (if (and files (equal (length files) 1))
553 (get-file-buffer (car files))
554 (current-buffer)))))
83affd96
ER
555 (if (and comment (not initial-contents))
556 (set-buffer (get-buffer-create "*VC-log*"))
557 (pop-to-buffer (get-buffer-create "*VC-log*")))
558 (set (make-local-variable 'vc-parent-buffer) parent)
559 (set (make-local-variable 'vc-parent-buffer-name)
560 (concat " from " (buffer-name vc-parent-buffer)))
83affd96
ER
561 (vc-log-edit files)
562 (make-local-variable 'vc-log-after-operation-hook)
563 (when after-hook
564 (setq vc-log-after-operation-hook after-hook))
565 (setq vc-log-operation action)
566 (setq vc-log-extra extra)
567 (when comment
568 (erase-buffer)
569 (when (stringp comment) (insert comment)))
570 (if (or (not comment) initial-contents)
571 (message "%s Type C-c C-c when done" msg)
572 (vc-finish-logentry (eq comment t)))))
573
574(defun vc-finish-logentry (&optional nocomment)
575 "Complete the operation implied by the current log entry.
576Use the contents of the current buffer as a check-in or registration
577comment. If the optional arg NOCOMMENT is non-nil, then don't check
578the buffer contents as a comment."
579 (interactive)
580 ;; Check and record the comment, if any.
581 (unless nocomment
582 (run-hooks 'vc-logentry-check-hook))
583 ;; Sync parent buffer in case the user modified it while editing the comment.
4903369a 584 ;; But not if it is a vc-directory buffer.
83affd96 585 (with-current-buffer vc-parent-buffer
4903369a 586 (or (vc-dispatcher-browsing) (vc-buffer-sync)))
83affd96
ER
587 (unless vc-log-operation
588 (error "No log operation is pending"))
589 ;; save the parameters held in buffer-local variables
590 (let ((log-operation vc-log-operation)
591 (log-fileset vc-log-fileset)
592 (log-extra vc-log-extra)
593 (log-entry (buffer-string))
594 (after-hook vc-log-after-operation-hook)
595 (tmp-vc-parent-buffer vc-parent-buffer))
596 (pop-to-buffer vc-parent-buffer)
597 ;; OK, do it to it
598 (save-excursion
599 (funcall log-operation
600 log-fileset
601 log-extra
602 log-entry))
603 ;; Remove checkin window (after the checkin so that if that fails
604 ;; we don't zap the *VC-log* buffer and the typing therein).
605 ;; -- IMO this should be replaced with quit-window
606 (let ((logbuf (get-buffer "*VC-log*")))
607 (cond ((and logbuf vc-delete-logbuf-window)
608 (delete-windows-on logbuf (selected-frame))
609 ;; Kill buffer and delete any other dedicated windows/frames.
610 (kill-buffer logbuf))
611 (logbuf (pop-to-buffer "*VC-log*")
612 (bury-buffer)
613 (pop-to-buffer tmp-vc-parent-buffer))))
614 ;; Now make sure we see the expanded headers
615 (when log-fileset
616 (mapc
617 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
618 log-fileset))
4903369a 619 (when (vc-dispatcher-browsing)
83affd96
ER
620 (vc-dir-move-to-goal-column))
621 (run-hooks after-hook 'vc-finish-logentry-hook)))
622
783b505b
ER
623;; The ewoc-based vc-directory implementation
624
625(defcustom vc-dir-mode-hook nil
626 "Normal hook run by `vc-dir-mode'.
627See `run-hooks'."
628 :type 'hook
629 :group 'vc)
630
631;; Used to store information for the files displayed in the *VC status* buffer.
632;; Each item displayed corresponds to one of these defstructs.
633(defstruct (vc-dir-fileinfo
634 (:copier nil)
635 (:type list) ;So we can use `member' on lists of FIs.
636 (:constructor
637 ;; We could define it as an alias for `list'.
638 vc-dir-create-fileinfo (name state &optional extra marked directory))
639 (:conc-name vc-dir-fileinfo->))
640 name ;Keep it as first, for `member'.
641 state
642 ;; For storing client-mode specific information.
643 extra
644 marked
645 ;; To keep track of not updated files during a global refresh
646 needs-update
647 ;; To distinguish files and directories.
648 directory)
649
cb625535
ER
650;; Used to describe a dispatcher client mode.
651(defstruct (vc-client-object
652 (:copier nil)
653 (:constructor
654 vc-create-client-object (name
655 headers
656 file-to-info
657 file-to-state
658 file-to-extra
659 updater))
660 (:conc-name vc-client-object->))
661 name
662 headers
663 file-to-info
664 file-to-state
665 file-to-extra
666 updater)
667
783b505b
ER
668(defvar vc-ewoc nil)
669(defvar vc-dir-process-buffer nil
670 "The buffer used for the asynchronous call that computes the VC status.")
671
672(defun vc-dir-move-to-goal-column ()
673 ;; Used to keep the cursor on the file name column.
674 (beginning-of-line)
675 ;; Must be in sync with vc-default-status-printer.
676 (forward-char 25))
677
678(defun vc-dir-prepare-status-buffer (dir &optional create-new)
679 "Find a *vc-dir* buffer showing DIR, or create a new one."
680 (setq dir (expand-file-name dir))
681 (let* ((bname "*vc-dir*")
682 ;; Look for another *vc-dir* buffer visiting the same directory.
683 (buf (save-excursion
684 (unless create-new
685 (dolist (buffer (buffer-list))
686 (set-buffer buffer)
8243c746 687 (when (and (vc-dispatcher-browsing)
783b505b
ER
688 (string= (expand-file-name default-directory) dir))
689 (return buffer)))))))
690 (or buf
691 ;; Create a new *vc-dir* buffer.
692 (with-current-buffer (create-file-buffer bname)
693 (cd dir)
694 (vc-setup-buffer (current-buffer))
695 ;; Reset the vc-parent-buffer-name so that it does not appear
696 ;; in the mode-line.
697 (setq vc-parent-buffer-name nil)
698 (current-buffer)))))
699
700(defvar vc-dir-menu-map
701 (let ((map (make-sparse-keymap "VC-dir")))
702 (define-key map [quit]
703 '(menu-item "Quit" quit-window
704 :help "Quit"))
705 (define-key map [kill]
706 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
707 :enable (vc-dir-busy)
708 :help "Kill the command that updates VC status buffer"))
709 (define-key map [refresh]
710 '(menu-item "Refresh" vc-dir-refresh
711 :enable (not (vc-dir-busy))
712 :help "Refresh the contents of the VC status buffer"))
713 ;; Movement.
714 (define-key map [sepmv] '("--"))
715 (define-key map [next-line]
716 '(menu-item "Next line" vc-dir-next-line
717 :help "Go to the next line" :keys "n"))
718 (define-key map [previous-line]
719 '(menu-item "Previous line" vc-dir-previous-line
720 :help "Go to the previous line"))
721 ;; Marking.
722 (define-key map [sepmrk] '("--"))
723 (define-key map [unmark-all]
724 '(menu-item "Unmark All" vc-dir-unmark-all-files
725 :help "Unmark all files that are in the same state as the current file\
726\nWith prefix argument unmark all files"))
727 (define-key map [unmark-previous]
728 '(menu-item "Unmark previous " vc-dir-unmark-file-up
729 :help "Move to the previous line and unmark the file"))
730
731 (define-key map [mark-all]
732 '(menu-item "Mark All" vc-dir-mark-all-files
733 :help "Mark all files that are in the same state as the current file\
734\nWith prefix argument mark all files"))
735 (define-key map [unmark]
736 '(menu-item "Unmark" vc-dir-unmark
737 :help "Unmark the current file or all files in the region"))
738
739 (define-key map [mark]
740 '(menu-item "Mark" vc-dir-mark
741 :help "Mark the current file or all files in the region"))
742
743 (define-key map [sepopn] '("--"))
744 (define-key map [open-other]
745 '(menu-item "Open in other window" vc-dir-find-file-other-window
746 :help "Find the file on the current line, in another window"))
747 (define-key map [open]
748 '(menu-item "Open file" vc-dir-find-file
749 :help "Find the file on the current line"))
783b505b 750 map)
9a6b551b 751 "Menu for dispatcher status")
783b505b
ER
752
753(defalias 'vc-dir-menu-map vc-dir-menu-map)
754
755(defvar vc-dir-mode-map
756 (let ((map (make-keymap)))
757 (suppress-keymap map)
758 ;; Marking.
759 (define-key map "m" 'vc-dir-mark)
760 (define-key map "M" 'vc-dir-mark-all-files)
761 (define-key map "u" 'vc-dir-unmark)
762 (define-key map "U" 'vc-dir-unmark-all-files)
763 (define-key map "\C-?" 'vc-dir-unmark-file-up)
764 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
765 ;; Movement.
766 (define-key map "n" 'vc-dir-next-line)
767 (define-key map " " 'vc-dir-next-line)
768 (define-key map "\t" 'vc-dir-next-line)
769 (define-key map "p" 'vc-dir-previous-line)
770 (define-key map [backtab] 'vc-dir-previous-line)
2cd5294d
ER
771 ;;; Rebind paragraph-movement commands.
772 (define-key map "\M-}" 'vc-dir-next-directory)
773 (define-key map "\M-{" 'vc-dir-prev-directory)
774 (define-key map "\M-<down>" 'vc-dir-next-directory)
775 (define-key map "\M-<up>" 'vc-dir-prev-directory)
783b505b
ER
776 ;; The remainder.
777 (define-key map "f" 'vc-dir-find-file)
778 (define-key map "\C-m" 'vc-dir-find-file)
779 (define-key map "o" 'vc-dir-find-file-other-window)
783b505b
ER
780 (define-key map "q" 'quit-window)
781 (define-key map "g" 'vc-dir-refresh)
782 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
783 (define-key map [(down-mouse-3)] 'vc-dir-menu)
784 (define-key map [(mouse-2)] 'vc-dir-toggle-mark)
9852a51d
ER
785
786 ;; FIXME: Calls back into vc.el
787 ;; Hook up the menu.
788 (define-key map [menu-bar vc-dir-mode]
789 '(menu-item
790 ;; This is used so that client modes can add mode-specific
791 ;; menu items to vc-dir-menu-map.
792 "VC Status" vc-dir-menu-map :filter vc-dir-menu-map-filter))
783b505b 793 map)
9852a51d 794 "Keymap for VC status")
783b505b
ER
795
796(defmacro vc-at-event (event &rest body)
797 "Evaluate `body' wich point located at event-start of `event'.
798If `body' uses `event', it should be a variable,
799 otherwise it will be evaluated twice."
800 (let ((posn (gensym "vc-at-event-posn")))
801 `(let ((,posn (event-start ,event)))
802 (save-excursion
803 (set-buffer (window-buffer (posn-window ,posn)))
804 (goto-char (posn-point ,posn))
805 ,@body))))
806
807(defun vc-dir-menu (e)
808 "Popup the VC status menu."
809 (interactive "e")
9852a51d 810 (vc-at-event e (popup-menu vc-dir-menu-map e)))
783b505b
ER
811
812(defvar vc-dir-tool-bar-map
813 (let ((map (make-sparse-keymap)))
814 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
815 map vc-dir-mode-map)
816 (tool-bar-local-item "bookmark_add"
817 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
818 :help "Toggle mark on current item")
819 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
820 map vc-dir-mode-map
821 :rtl "right-arrow")
822 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
823 map vc-dir-mode-map
824 :rtl "left-arrow")
825 (tool-bar-local-item-from-menu 'vc-print-log "info"
826 map vc-dir-mode-map)
827 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
828 map vc-dir-mode-map)
829 (tool-bar-local-item-from-menu 'nonincremental-search-forward
830 "search" map)
831 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
832 map vc-dir-mode-map)
833 (tool-bar-local-item-from-menu 'quit-window "exit"
834 map vc-dir-mode-map)
835 map))
836
783b505b
ER
837(defun vc-dir-update (entries buffer &optional noinsert)
838 "Update BUFFER's ewoc from the list of ENTRIES.
839If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
840 ;; Add ENTRIES to the vc-dir buffer BUFFER.
841 (with-current-buffer buffer
842 ;; Insert the entries sorted by name into the ewoc.
843 ;; We assume the ewoc is sorted too, which should be the
844 ;; case if we always add entries with vc-dir-update.
845 (setq entries
846 ;; Sort: first files and then subdirectories.
847 ;; XXX: this is VERY inefficient, it computes the directory
848 ;; names too many times
849 (sort entries
850 (lambda (entry1 entry2)
851 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
852 (dir2 (file-name-directory (expand-file-name (car entry2)))))
853 (cond
854 ((string< dir1 dir2) t)
855 ((not (string= dir1 dir2)) nil)
856 ((string< (car entry1) (car entry2))))))))
e4ba96dc
ER
857 ;; Insert directory entries in the right places.
858 (let ((entry (car entries))
859 (node (ewoc-nth vc-ewoc 0)))
860 ;; Insert . if it is not present.
861 (unless node
862 (let ((rd (file-relative-name default-directory)))
863 (ewoc-enter-last
864 vc-ewoc (vc-dir-create-fileinfo
865 rd nil nil nil (expand-file-name default-directory))))
866 (setq node (ewoc-nth vc-ewoc 0)))
867
868 (while (and entry node)
869 (let* ((entryfile (car entry))
870 (entrydir (file-name-directory (expand-file-name entryfile)))
871 (nodedir
872 (or (vc-dir-fileinfo->directory (ewoc-data node))
873 (file-name-directory
874 (expand-file-name
875 (vc-dir-fileinfo->name (ewoc-data node)))))))
876 (cond
877 ;; First try to find the directory.
878 ((string-lessp nodedir entrydir)
879 (setq node (ewoc-next vc-ewoc node)))
880 ((string-equal nodedir entrydir)
881 ;; Found the directory, find the place for the file name.
882 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
783b505b
ER
883 (cond
884 ((string-lessp nodefile entryfile)
885 (setq node (ewoc-next vc-ewoc node)))
e4ba96dc 886 ((string-equal nodefile entryfile)
783b505b
ER
887 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
888 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
889 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
890 (ewoc-invalidate vc-ewoc node)
891 (setq entries (cdr entries) entry (car entries))
e4ba96dc
ER
892 (setq node (ewoc-next vc-ewoc node)))
893 (t
894 (ewoc-enter-before vc-ewoc node
895 (apply 'vc-dir-create-fileinfo entry))
896 (setq entries (cdr entries) entry (car entries))))))
897 (t
898 ;; We need to insert a directory node
899 (let ((rd (file-relative-name entrydir)))
900 (ewoc-enter-last
901 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))
783b505b 902 ;; Now insert the node itself.
e4ba96dc
ER
903 (ewoc-enter-before vc-ewoc node
904 (apply 'vc-dir-create-fileinfo entry))
905 (setq entries (cdr entries) entry (car entries))))))
906 ;; We're past the last node, all remaining entries go to the end.
907 (unless (or node noinsert)
908 (let* ((lastnode (ewoc-nth vc-ewoc -1))
909 (lastdir
910 (or (vc-dir-fileinfo->directory (ewoc-data lastnode))
911 (file-name-directory
912 (expand-file-name
913 (vc-dir-fileinfo->name (ewoc-data lastnode)))))))
914 (dolist (entry entries)
915 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
916 ;; Insert a directory node if needed.
917 (unless (string-equal lastdir entrydir)
918 (setq lastdir entrydir)
919 (let ((rd (file-relative-name entrydir)))
920 (ewoc-enter-last
921 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
922 ;; Now insert the node itself.
923 (ewoc-enter-last vc-ewoc
924 (apply 'vc-dir-create-fileinfo entry)))))))))
783b505b
ER
925
926(defun vc-dir-busy ()
927 (and (buffer-live-p vc-dir-process-buffer)
928 (get-buffer-process vc-dir-process-buffer)))
929
930(defun vc-dir-kill-dir-status-process ()
931 "Kill the temporary buffer and associated process."
932 (interactive)
933 (when (buffer-live-p vc-dir-process-buffer)
934 (let ((proc (get-buffer-process vc-dir-process-buffer)))
935 (when proc (delete-process proc))
936 (setq vc-dir-process-buffer nil)
937 (setq mode-line-process nil))))
938
939(defun vc-dir-kill-query ()
940 ;; Make sure that when the VC status buffer is killed the update
941 ;; process running in background is also killed.
942 (if (vc-dir-busy)
943 (when (y-or-n-p "Status update process running, really kill status buffer?")
944 (vc-dir-kill-dir-status-process)
945 t)
946 t))
947
948(defun vc-dir-next-line (arg)
949 "Go to the next line.
950If a prefix argument is given, move by that many lines."
951 (interactive "p")
edbde353
ER
952 (with-no-warnings
953 (ewoc-goto-next vc-ewoc arg)
954 (vc-dir-move-to-goal-column)))
783b505b
ER
955
956(defun vc-dir-previous-line (arg)
957 "Go to the previous line.
958If a prefix argument is given, move by that many lines."
959 (interactive "p")
960 (ewoc-goto-prev vc-ewoc arg)
961 (vc-dir-move-to-goal-column))
962
2cd5294d
ER
963(defun vc-dir-next-directory ()
964 "Go to the next directory."
965 (interactive)
966 (let ((orig (point)))
967 (if
968 (catch 'foundit
969 (while t
970 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
971 (cond ((not next)
972 (throw 'foundit t))
973 (t
974 (progn
975 (ewoc-goto-node vc-ewoc next)
976 (vc-dir-move-to-goal-column)
977 (if (vc-dir-fileinfo->directory (ewoc-data next))
978 (throw 'foundit nil))))))))
979 (goto-char orig))))
980
981(defun vc-dir-prev-directory ()
982 "Go to the previous directory."
983 (interactive)
984 (let ((orig (point)))
985 (if
986 (catch 'foundit
987 (while t
988 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
989 (cond ((not prev)
990 (throw 'foundit t))
991 (t
992 (progn
993 (ewoc-goto-node vc-ewoc prev)
994 (vc-dir-move-to-goal-column)
995 (if (vc-dir-fileinfo->directory (ewoc-data prev))
996 (throw 'foundit nil))))))))
997 (goto-char orig))))
998
783b505b
ER
999(defun vc-dir-mark-unmark (mark-unmark-function)
1000 (if (use-region-p)
1001 (let ((firstl (line-number-at-pos (region-beginning)))
1002 (lastl (line-number-at-pos (region-end))))
1003 (save-excursion
1004 (goto-char (region-beginning))
1005 (while (<= (line-number-at-pos) lastl)
1006 (funcall mark-unmark-function))))
1007 (funcall mark-unmark-function)))
1008
1009(defun vc-dir-parent-marked-p (arg)
e4ba96dc
ER
1010 ;; Return nil if none of the parent directories of arg is marked.
1011 (let* ((argdata (ewoc-data arg))
1012 (argdir
1013 (let ((crtdir (vc-dir-fileinfo->directory argdata)))
1014 (if crtdir
1015 crtdir
1016 (file-name-directory (expand-file-name
1017 (vc-dir-fileinfo->name argdata))))))
1018 (arglen (length argdir))
1019 (crt arg)
1020 data dir)
1021 ;; Go through the predecessors, checking if any directory that is
1022 ;; a parent is marked.
1023 (while (setq crt (ewoc-prev vc-ewoc crt))
1024 (setq data (ewoc-data crt))
1025 (setq dir
1026 (let ((crtdir (vc-dir-fileinfo->directory data)))
783b505b
ER
1027 (if crtdir
1028 crtdir
1029 (file-name-directory (expand-file-name
e4ba96dc
ER
1030 (vc-dir-fileinfo->name data))))))
1031
1032 (when (and (vc-dir-fileinfo->directory data)
1033 (string-equal (substring argdir 0 (length dir)) dir))
1034 (when (vc-dir-fileinfo->marked data)
1035 (error "Cannot mark `%s', parent directory `%s' marked"
1036 (vc-dir-fileinfo->name argdata)
1037 (vc-dir-fileinfo->name data)))))
1038 nil))
783b505b
ER
1039
1040(defun vc-dir-children-marked-p (arg)
1041 ;; Return nil if none of the children of arg is marked.
e4ba96dc
ER
1042 (let* ((argdata (ewoc-data arg))
1043 (argdir (vc-dir-fileinfo->directory argdata))
1044 (arglen (length argdir))
1045 (is-child t)
1046 (crt arg)
1047 data dir)
1048 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1049 (setq data (ewoc-data crt))
1050 (setq dir
1051 (let ((crtdir (vc-dir-fileinfo->directory data)))
1052 (if crtdir
1053 crtdir
1054 (file-name-directory (expand-file-name
1055 (vc-dir-fileinfo->name data))))))
1056 (if (string-equal argdir (substring dir 0 arglen))
1057 (when (vc-dir-fileinfo->marked data)
1058 (error "Cannot mark `%s', child `%s' marked"
1059 (vc-dir-fileinfo->name argdata)
1060 (vc-dir-fileinfo->name data)))
1061 ;; We are done, we got to an entry that is not a child of `arg'.
1062 (setq is-child nil)))
1063 nil))
783b505b
ER
1064
1065(defun vc-dir-mark-file (&optional arg)
1066 ;; Mark ARG or the current file and move to the next line.
1067 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1068 (file (ewoc-data crt))
1069 (isdir (vc-dir-fileinfo->directory file)))
1070 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1071 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1072 (setf (vc-dir-fileinfo->marked file) t)
1073 (ewoc-invalidate vc-ewoc crt)
1074 (unless (or arg (mouse-event-p last-command-event))
1075 (vc-dir-next-line 1)))))
1076
1077(defun vc-dir-mark ()
1078 "Mark the current file or all files in the region.
1079If the region is active, mark all the files in the region.
1080Otherwise mark the file on the current line and move to the next
1081line."
1082 (interactive)
1083 (vc-dir-mark-unmark 'vc-dir-mark-file))
1084
1085(defun vc-dir-mark-all-files (arg)
1086 "Mark all files with the same state as the current one.
1087With a prefix argument mark all files.
1088If the current entry is a directory, mark all child files.
1089
1090The VC commands operate on files that are on the same state.
1091This command is intended to make it easy to select all files that
1092share the same state."
1093 (interactive "P")
1094 (if arg
1095 ;; Mark all files.
1096 (progn
1097 ;; First check that no directory is marked, we can't mark
1098 ;; files in that case.
1099 (ewoc-map
1100 (lambda (filearg)
1101 (when (and (vc-dir-fileinfo->directory filearg)
1102 (vc-dir-fileinfo->directory filearg))
1103 (error "Cannot mark all files, directory `%s' marked"
1104 (vc-dir-fileinfo->name filearg))))
1105 vc-ewoc)
1106 (ewoc-map
1107 (lambda (filearg)
1108 (unless (vc-dir-fileinfo->marked filearg)
1109 (setf (vc-dir-fileinfo->marked filearg) t)
1110 t))
1111 vc-ewoc))
1112 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1113 (if (vc-dir-fileinfo->directory data)
1114 ;; It's a directory, mark child files.
1115 (let ((crt (ewoc-locate vc-ewoc)))
1116 (unless (vc-dir-children-marked-p crt)
1117 (while (setq crt (ewoc-next vc-ewoc crt))
1118 (let ((crt-data (ewoc-data crt)))
1119 (unless (vc-dir-fileinfo->directory crt-data)
1120 (setf (vc-dir-fileinfo->marked crt-data) t)
1121 (ewoc-invalidate vc-ewoc crt))))))
1122 ;; It's a file
1123 (let ((state (vc-dir-fileinfo->state data))
1124 (crt (ewoc-nth vc-ewoc 0)))
1125 (while crt
1126 (let ((crt-data (ewoc-data crt)))
1127 (when (and (not (vc-dir-fileinfo->marked crt-data))
1128 (eq (vc-dir-fileinfo->state crt-data) state)
1129 (not (vc-dir-fileinfo->directory crt-data)))
1130 (vc-dir-mark-file crt)))
1131 (setq crt (ewoc-next vc-ewoc crt))))))))
1132
1133(defun vc-dir-unmark-file ()
1134 ;; Unmark the current file and move to the next line.
1135 (let* ((crt (ewoc-locate vc-ewoc))
1136 (file (ewoc-data crt)))
1137 (setf (vc-dir-fileinfo->marked file) nil)
1138 (ewoc-invalidate vc-ewoc crt)
1139 (unless (mouse-event-p last-command-event)
1140 (vc-dir-next-line 1))))
1141
1142(defun vc-dir-unmark ()
1143 "Unmark the current file or all files in the region.
1144If the region is active, unmark all the files in the region.
1145Otherwise mark the file on the current line and move to the next
1146line."
1147 (interactive)
1148 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1149
1150(defun vc-dir-unmark-file-up ()
1151 "Move to the previous line and unmark the file."
1152 (interactive)
1153 ;; If we're on the first line, we won't move up, but we will still
1154 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1155 ;; does.
1156 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1157 (file (ewoc-data prev)))
1158 (setf (vc-dir-fileinfo->marked file) nil)
1159 (ewoc-invalidate vc-ewoc prev)
1160 (vc-dir-move-to-goal-column)))
1161
1162(defun vc-dir-unmark-all-files (arg)
1163 "Unmark all files with the same state as the current one.
1164With a prefix argument unmark all files.
1165If the current entry is a directory, unmark all the child files.
1166
1167The VC commands operate on files that are on the same state.
1168This command is intended to make it easy to deselect all files
1169that share the same state."
1170 (interactive "P")
1171 (if arg
1172 (ewoc-map
1173 (lambda (filearg)
1174 (when (vc-dir-fileinfo->marked filearg)
1175 (setf (vc-dir-fileinfo->marked filearg) nil)
1176 t))
1177 vc-ewoc)
1178 (let* ((crt (ewoc-locate vc-ewoc))
1179 (data (ewoc-data crt)))
1180 (if (vc-dir-fileinfo->directory data)
1181 ;; It's a directory, unmark child files.
1182 (while (setq crt (ewoc-next vc-ewoc crt))
1183 (let ((crt-data (ewoc-data crt)))
1184 (unless (vc-dir-fileinfo->directory crt-data)
1185 (setf (vc-dir-fileinfo->marked crt-data) nil)
1186 (ewoc-invalidate vc-ewoc crt))))
1187 ;; It's a file
1188 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1189 (ewoc-map
1190 (lambda (filearg)
1191 (when (and (vc-dir-fileinfo->marked filearg)
1192 (eq (vc-dir-fileinfo->state filearg) crt-state))
1193 (setf (vc-dir-fileinfo->marked filearg) nil)
1194 t))
1195 vc-ewoc))))))
1196
1197(defun vc-dir-toggle-mark-file ()
1198 (let* ((crt (ewoc-locate vc-ewoc))
1199 (file (ewoc-data crt)))
1200 (if (vc-dir-fileinfo->marked file)
1201 (vc-dir-unmark-file)
1202 (vc-dir-mark-file))))
1203
1204(defun vc-dir-toggle-mark (e)
1205 (interactive "e")
1206 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1207
1208(defun vc-dir-delete-file ()
1209 "Delete the marked files, or the current file if no marks."
1210 (interactive)
1211 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1212 (list (vc-dir-current-file)))))
1213
1214(defun vc-dir-find-file ()
1215 "Find the file on the current line."
1216 (interactive)
1217 (find-file (vc-dir-current-file)))
1218
1219(defun vc-dir-find-file-other-window ()
1220 "Find the file on the current line, in another window."
1221 (interactive)
1222 (find-file-other-window (vc-dir-current-file)))
1223
1224(defun vc-dir-current-file ()
1225 (let ((node (ewoc-locate vc-ewoc)))
1226 (unless node
1227 (error "No file available."))
1228 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1229
1230(defun vc-dir-marked-files ()
1231 "Return the list of marked files."
1232 (mapcar
1233 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1234 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1235
1236(defun vc-dir-marked-only-files ()
cb625535 1237 "Return the list of marked files, For marked directories return child files."
783b505b
ER
1238 (let ((crt (ewoc-nth vc-ewoc 0))
1239 result)
1240 (while crt
1241 (let ((crt-data (ewoc-data crt)))
1242 (if (vc-dir-fileinfo->marked crt-data)
1243 (if (vc-dir-fileinfo->directory crt-data)
1244 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1245 (dirlen (length dir))
1246 data)
1247 (while
1248 (and (setq crt (ewoc-next vc-ewoc crt))
1249 (string-equal
1250 (substring
1251 (progn
1252 (setq data (ewoc-data crt))
1253 (let ((crtdir (vc-dir-fileinfo->directory data)))
1254 (if crtdir
1255 crtdir
1256 (file-name-directory
1257 (expand-file-name
1258 (vc-dir-fileinfo->name data))))))
1259 0 dirlen)
1260 dir))
1261 (unless (vc-dir-fileinfo->directory data)
1262 (push (vc-dir-fileinfo->name data) result))))
1263 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1264 (setq crt (ewoc-next vc-ewoc crt)))
1265 (setq crt (ewoc-next vc-ewoc crt)))))
1266 result))
038608c7 1267
4fe2e5ed
ER
1268(defun vc-directory-resynch-file (&optional fname)
1269 "Update the entries for FILE in any directory buffers that list it."
1270 (let ((file (or fname (expand-file-name buffer-file-name))))
4fe2e5ed
ER
1271 ;; The vc-dir case
1272 (let ((found-vc-dir-buf nil))
1273 (save-excursion
1274 (dolist (status-buf (buffer-list))
1275 (set-buffer status-buf)
1276 ;; look for a vc-dir buffer that might show this file.
1277 (when (eq major-mode 'vc-dir-mode)
1278 (setq found-vc-dir-buf t)
1279 (let ((ddir (expand-file-name default-directory)))
1280 ;; This test is cvs-string-prefix-p
1281 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
1282 (let*
1283 ((file-short (substring file (length ddir)))
1284 (state
1285 (funcall (vc-client-object->file-to-state vc-client-mode)
1286 file))
1287 (extra
1288 (funcall (vc-client-object->file-to-extra vc-client-mode)
1289 file))
1290 (entry
1291 (list file-short state extra)))
1292 (vc-dir-update (list entry) status-buf))))))
1293 ;; We didn't find any vc-dir buffers, remove the hook, it is
1294 ;; not needed.
1295 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-directory-resynch-file))))))
cb625535
ER
1296
1297(defun vc-dir-mode (client-object)
1298 "Major mode for showing the VC status for a directory.
1299Marking/Unmarking key bindings and actions:
1300m - marks a file/directory or if the region is active, mark all the files
1301 in region.
1302 Restrictions: - a file cannot be marked if any parent directory is marked
1303 - a directory cannot be marked if any child file or
1304 directory is marked
1305u - marks a file/directory or if the region is active, unmark all the files
1306 in region.
1307M - if the cursor is on a file: mark all the files with the same VC state as
1308 the current file
1309 - if the cursor is on a directory: mark all child files
1310 - with a prefix argument: mark all files
1311U - if the cursor is on a file: unmark all the files with the same VC state
1312 as the current file
1313 - if the cursor is on a directory: unmark all child files
1314 - with a prefix argument: unmark all files
1315
1316
1317\\{vc-dir-mode-map}"
1318 (setq mode-name (vc-client-object->name client-object))
1319 (setq major-mode 'vc-dir-mode)
1320 (setq buffer-read-only t)
1321 (use-local-map vc-dir-mode-map)
1322 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
6494957a 1323 (set (make-local-variable 'vc-client-mode) client-object)
cb625535
ER
1324 (let ((buffer-read-only nil))
1325 (erase-buffer)
1326 (set (make-local-variable 'vc-dir-process-buffer) nil)
1327 (set (make-local-variable 'vc-ewoc)
1328 (ewoc-create (vc-client-object->file-to-info client-object)
1329 (vc-client-object->headers client-object)))
4fe2e5ed 1330 (add-hook 'after-save-hook 'vc-directory-resynch-file)
cb625535
ER
1331 ;; Make sure that if the VC status buffer is killed, the update
1332 ;; process running in the background is also killed.
1333 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1334 (funcall (vc-client-object->updater client-object)))
1335 (run-hooks 'vc-dir-mode-hook))
1336
1337(put 'vc-dir-mode 'mode-class 'special)
1338
b236ab0d
ER
1339(defun vc-dispatcher-browsing ()
1340 "Are we in a directory browser buffer?"
8243c746
SM
1341 (derived-mode-p 'vc-dir-mode))
1342
1343(defun vc-dispatcher-in-fileset-p (fileset)
1344 (let ((member nil))
1345 (while (and (not member) fileset)
1346 (let ((elem (pop fileset)))
1347 (if (if (file-directory-p elem)
1348 (eq t (compare-strings buffer-file-name nil (length elem)
1349 elem nil nil))
1350 (eq (current-buffer) (get-file-buffer elem)))
713b6f81
SM
1351 (setq member t))))
1352 member))
b236ab0d 1353
cb5d44b6 1354(defun vc-dispatcher-selection-set (&optional observer)
327c1d6a 1355 "Deduce a set of files to which to apply an operation. Return the fileset.
86048828 1356If we're in a directory display, the fileset is the list of marked files (if
327c1d6a
SM
1357there is one) else the file on the curreent line. If not in a directory
1358display, but the current buffer visits a file, the fileset is a singleton
1359containing that file. Otherwise, throw an error."
be636037 1360 (let ((files
327c1d6a
SM
1361 (cond
1362 ;; Browsing with vc-dir
1363 ((vc-dispatcher-browsing)
1364 (or (vc-dir-marked-files) (list (vc-dir-current-file))))
1365 ;; Visiting an eligible file
1366 ((buffer-file-name)
1367 (list buffer-file-name))
1368 ;; No eligible file -- if there's a parent buffer, deduce from there
1369 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1370 (with-current-buffer vc-parent-buffer
1371 (vc-dispatcher-browsing))))
1372 (with-current-buffer vc-parent-buffer
1373 (vc-dispatcher-selection-set)))
1374 ;; No good set here, throw error
1375 (t (error "No fileset is available here")))))
8243c746
SM
1376 ;; We assume, in order to avoid unpleasant surprises to the user,
1377 ;; that a fileset is not in good shape to be handed to the user if the
86048828 1378 ;; buffers visiting the fileset don't match the on-disk contents.
cb5d44b6
ER
1379 (if (not observer)
1380 (save-some-buffers
1381 nil (lambda () (vc-dispatcher-in-fileset-p files))))
be636037 1382 files))
b236ab0d 1383
d9a3d80e
DN
1384(provide 'vc-dispatcher)
1385
3eb20677 1386;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
b1ddeeb7 1387;;; vc-dispatcher.el ends here