Remove assumptions about buffer names.
[emacs.git] / lisp / vc-dispatcher.el
blobe117622b7af302377f0928b3b9e121302a9b02c1
1 ;;; vc-dispatcher.el -- generic command-dispatcher facility.
3 ;; Copyright (C) 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: FSF (see below for full credits)
7 ;; Maintainer: Eric S. Raymond <esr@thyrsus.com>
8 ;; Keywords: tools
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Credits:
27 ;; Designed and implemented by Eric S. Raymond, originally as part of VC mode.
28 ;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the
29 ;; vc-dir front end.
31 ;;; Commentary:
33 ;; Goals:
35 ;; There is a class of front-ending problems that Emacs might be used
36 ;; to address that involves selecting sets of files, or possibly
37 ;; directories, and passing the selection set to slave commands. The
38 ;; prototypical example, from which this code is derived, is talking
39 ;; to version-control systems.
41 ;; vc-dispatcher.el is written to decouple the UI issues in such front
42 ;; ends from their application-specific logic. It also provides a
43 ;; service layer for running the slave commands either synchronously
44 ;; or asynchronously and managing the message/error logs from the
45 ;; command runs.
47 ;; Similar UI problems can be expected to come up in applications
48 ;; areas other than VCSes; IDEs and document search are two obvious ones.
49 ;; This mode is intended to ensure that the Emacs interfaces for all such
50 ;; beasts are consistent and carefully designed. But even if nothing
51 ;; but VC ever uses it, getting the layer separation right will be
52 ;; a valuable thing.
54 ;; Dispatcher's universe:
56 ;; The universe consists of the file tree rooted at the current
57 ;; directory. The dispatcher's upper layer deduces some subset
58 ;; of the file tree from the state of the currently visited buffer
59 ;; and returns that subset, presumably to a client mode.
61 ;; The user may be looking at either of two different views; a buffer
62 ;; visiting a file, or a directory buffer generated by vc-dispatcher.
64 ;; The lower layer of this mode runs commands in subprocesses, either
65 ;; synchronously or asynchronously. Commands may be launched in one
66 ;; of two ways: they may be run immediately, or the calling mode can
67 ;; create a closure associated with a text-entry buffer, to be
68 ;; executed when the user types C-c to ship the buffer contents. In
69 ;; either case the command messages and error (if any) will remain
70 ;; available in a status buffer.
72 ;; Special behavior of dispatcher directory buffers:
74 ;; In dispatcher directory buffers, facilities to perform basic
75 ;; navigation and selection operations are provided by keymap and menu
76 ;; entries that dispatcher sets up itself, so they'll be uniform
77 ;; across all dispatcher-using client modes. Client modes are
78 ;; expected to append to these to provide mode-specific bindings.
80 ;; The standard map associates a 'state' slot (that the client mode
81 ;; may set) with each directory entry. The dispatcher knows nothing
82 ;; about the semantics of individual states, but mark and unmark commands
83 ;; treat all entries with the same state as the currently selected one as
84 ;; a unit.
86 ;; The interface:
88 ;; The main interface to the lower level is vc-do-command. This launches a
89 ;; 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).
95 ;; The interface to the upper level has the two main entry points (vc-dir)
96 ;; and (vc-dispatcher-selection-set) and a couple of convenience functions.
97 ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set)
98 ;; returns a selection set of files, either the marked files in a browsing
99 ;; buffer or the singleton set consisting of the file visited by the current
100 ;; buffer (when that is appropriate). It also does what is needed to ensure
101 ;; that on-disk files and the contents of their visiting Emacs buffers
102 ;; coincide.
104 ;; When the client mode adds a local mode-line-hook to a buffer, it
105 ;; will be called with the buffer file name as argument whenever the
106 ;; dispatcher resynchs the buffer.
108 ;; To do:
110 ;; - vc-dir-kill-dir-status-process should not be specific to dir-status,
111 ;; it should work for other async commands done through vc-do-command
112 ;; as well,
114 ;; - log buffers need font-locking.
116 ;; - vc-dir needs mouse bindings.
118 ;; - vc-dir toolbar needs more icons.
120 ;; - vc-dir-menu-map-filter hook call needs to be moved to vc.el.
123 (require 'ewoc)
125 (eval-when-compile
126 (require 'cl))
128 ;; General customization
130 (defcustom vc-logentry-check-hook nil
131 "Normal hook run by `vc-finish-logentry'.
132 Use this to impose your own rules on the entry in addition to any the
133 dispatcher client mode imposes itself."
134 :type 'hook
135 :group 'vc)
137 (defcustom vc-delete-logbuf-window t
138 "If non-nil, delete the *VC-log* buffer and window after each logical action.
139 If nil, bury that buffer instead.
140 This is most useful if you have multiple windows on a frame and would like to
141 preserve the setting."
142 :type 'boolean
143 :group 'vc)
145 (defcustom vc-command-messages nil
146 "If non-nil, display run messages from back-end commands."
147 :type 'boolean
148 :group 'vc)
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)
155 ;; Variables the user doesn't need to know about.
157 (defvar vc-log-operation nil)
158 (defvar vc-log-after-operation-hook nil)
159 (defvar vc-log-fileset)
160 (defvar vc-log-extra)
161 (defvar vc-client-mode)
163 ;; In a log entry buffer, this is a local variable
164 ;; that points to the buffer for which it was made
165 ;; (either a file, or a directory buffer).
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)
171 ;; Common command execution logic
173 (defun vc-process-filter (p s)
174 "An alternative output filter for async process P.
175 One difference with the default filter is that this inserts S after markers.
176 Another 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))))))))
187 (defun vc-setup-buffer (buf)
188 "Prepare BUF for executing a slave command and make it current."
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))))
201 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
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))))))))))
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 command is in progress in this buffer"))))
244 (defun vc-exec-after (code)
245 "Eval CODE when the current buffer's process is done.
246 If the current buffer has no process, just evaluate CODE.
247 Else, 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)
275 (defvar vc-post-command-functions nil
276 "Hook run at the end of `vc-do-command'.
277 Each function is called inside the buffer in which the command was run
278 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
280 (defvar w32-quote-process-args)
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 " ")))
287 ;;;###autoload
288 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
289 "Execute a slave command, notifying user and checking for errors.
290 Output from COMMAND goes to BUFFER, or the current buffer if
291 BUFFER is t. If the destination buffer is not already current,
292 set it up properly and erase it. The command is considered
293 successful if its exit status does not exceed OKSTATUS (if
294 OKSTATUS is nil, that means to ignore error status, if it is
295 `async', that means not to wait for termination of the
296 subprocess; if it is t it means to ignore all execution errors).
297 FILE-OR-LIST is the name of a working file; it may be a list of
298 files or be nil (to execute commands that don't expect a file
299 name or set of files). If an optional list of FLAGS is present,
300 that 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)
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)))
323 (vc-setup-buffer buffer))
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))))
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).
387 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
389 (defun vc-position-context (posn)
390 "Save a bit of the text around POSN in the current buffer.
391 Used to help us find the corresponding position again later
392 if 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)))))
400 (defun vc-find-position-by-context (context)
401 "Return the position of CONTEXT in the current buffer.
402 If 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))))))))
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)))
427 (string= context-string (buffer-substring posn end)))))
429 (defun vc-buffer-context ()
430 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
431 Used 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.
434 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
435 (vc-position-context (mark-marker))))
436 ;; Make the right thing happen in transient-mark-mode.
437 (mark-active nil))
438 (list point-context mark-context nil)))
440 (defun vc-restore-buffer-context (context)
441 "Restore point/mark, and reparse any affected compilation buffers.
442 CONTEXT is that which `vc-buffer-context' returns."
443 (let ((point-context (nth 0 context))
444 (mark-context (nth 1 context)))
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))))))
455 (defun vc-revert-buffer-internal (&optional arg no-confirm)
456 "Revert buffer, keeping point and mark where user expects them.
457 Try to be clever in the face of changes due to expanded version-control
458 key words. This is important for typeahead to work as expected.
459 ARG 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)))
473 (defun vc-resynch-window (file &optional keep noquery)
474 "If FILE is in the current buffer, either revert or unvisit it.
475 The choice between revert (to see expanded keywords) and unvisit
476 depends on KEEP. NOQUERY if non-nil inhibits confirmation for
477 reverting. NOQUERY should be t *only* if it is known the only
478 difference between the buffer and the file is due to
479 modifications by the dispatcher client code, rather than user
480 editing!"
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))))
496 (run-hook-with-args 'modeline-hook buffer-file-name))
497 (kill-buffer (current-buffer)))))
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)))))
507 (vc-directory-resynch-file file))
509 (defun vc-buffer-sync (&optional not-urgent)
510 "Make sure the current buffer and its working file are in sync.
511 NOT-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")))))
519 ;; Command closures
521 ;; Set up key bindings for use while editing log messages
523 (defun vc-log-edit (fileset)
524 "Set up `log-edit' for use on FILE."
525 (setq default-directory
526 (with-current-buffer vc-parent-buffer default-directory))
527 (log-edit 'vc-finish-logentry
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))
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.
538 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
539 action on close to ACTION. If COMMENT is a string and
540 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
541 contents of the log entry buffer. If COMMENT is a string and
542 INITIAL-CONTENTS is nil, do action immediately as if the user had
543 entered COMMENT. If COMMENT is t, also do action immediately with an
544 empty comment. Remember the file's buffer in `vc-parent-buffer'
545 \(current one if no file). AFTER-HOOK specifies the local value
546 for `vc-log-after-operation-hook'."
547 (let ((parent
548 (if (vc-dispatcher-browsing)
549 ;; If we are called from a directory browser, the parent buffer is
550 ;; the current buffer.
551 (current-buffer)
552 (if (and files (equal (length files) 1))
553 (get-file-buffer (car files))
554 (current-buffer)))))
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)))
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)))))
574 (defun vc-finish-logentry (&optional nocomment)
575 "Complete the operation implied by the current log entry.
576 Use the contents of the current buffer as a check-in or registration
577 comment. If the optional arg NOCOMMENT is non-nil, then don't check
578 the 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.
584 ;; But not if it is a vc-directory buffer.
585 (with-current-buffer vc-parent-buffer
586 (or (vc-dispatcher-browsing) (vc-buffer-sync)))
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))
619 (when (vc-dispatcher-browsing)
620 (vc-dir-move-to-goal-column))
621 (run-hooks after-hook 'vc-finish-logentry-hook)))
623 ;; The ewoc-based vc-directory implementation
625 (defcustom vc-dir-mode-hook nil
626 "Normal hook run by `vc-dir-mode'.
627 See `run-hooks'."
628 :type 'hook
629 :group 'vc)
631 ;; Used to store information for the files displayed in the directory 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)
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 extra-menu))
661 (:conc-name vc-client-object->))
662 name
663 headers
664 file-to-info
665 file-to-state
666 file-to-extra
667 updater
668 extra-menu)
670 (defvar vc-ewoc nil)
671 (defvar vc-dir-process-buffer nil
672 "The buffer used for the asynchronous call that computes status.")
674 (defun vc-dir-move-to-goal-column ()
675 ;; Used to keep the cursor on the file name column.
676 (beginning-of-line)
677 ;; Must be in sync with vc-default-status-printer.
678 (forward-char 25))
680 (defun vc-dir-prepare-status-buffer (bname dir &optional create-new)
681 "Find a buffer named BNAME showing DIR, or create a new one."
682 (setq dir (expand-file-name dir))
683 (let*
684 ;; Look for another buffer name BNAME visiting the same directory.
685 ((buf (save-excursion
686 (unless create-new
687 (dolist (buffer (buffer-list))
688 (set-buffer buffer)
689 (when (and (vc-dispatcher-browsing)
690 (string= (expand-file-name default-directory) dir))
691 (return buffer)))))))
692 (or buf
693 ;; Create a new buffer named BNAME.
694 (with-current-buffer (create-file-buffer bname)
695 (cd dir)
696 (vc-setup-buffer (current-buffer))
697 ;; Reset the vc-parent-buffer-name so that it does not appear
698 ;; in the mode-line.
699 (setq vc-parent-buffer-name nil)
700 (current-buffer)))))
702 (defvar vc-dir-menu-map
703 (let ((map (make-sparse-keymap)))
704 (define-key map [quit]
705 '(menu-item "Quit" quit-window
706 :help "Quit"))
707 (define-key map [kill]
708 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
709 :enable (vc-dir-busy)
710 :help "Kill the command that updates the directory buffer"))
711 (define-key map [refresh]
712 '(menu-item "Refresh" vc-dir-refresh
713 :enable (not (vc-dir-busy))
714 :help "Refresh the contents of the directory buffer"))
715 ;; Movement.
716 (define-key map [sepmv] '("--"))
717 (define-key map [next-line]
718 '(menu-item "Next line" vc-dir-next-line
719 :help "Go to the next line" :keys "n"))
720 (define-key map [previous-line]
721 '(menu-item "Previous line" vc-dir-previous-line
722 :help "Go to the previous line"))
723 ;; Marking.
724 (define-key map [sepmrk] '("--"))
725 (define-key map [unmark-all]
726 '(menu-item "Unmark All" vc-dir-unmark-all-files
727 :help "Unmark all files that are in the same state as the current file\
728 \nWith prefix argument unmark all files"))
729 (define-key map [unmark-previous]
730 '(menu-item "Unmark previous " vc-dir-unmark-file-up
731 :help "Move to the previous line and unmark the file"))
733 (define-key map [mark-all]
734 '(menu-item "Mark All" vc-dir-mark-all-files
735 :help "Mark all files that are in the same state as the current file\
736 \nWith prefix argument mark all files"))
737 (define-key map [unmark]
738 '(menu-item "Unmark" vc-dir-unmark
739 :help "Unmark the current file or all files in the region"))
741 (define-key map [mark]
742 '(menu-item "Mark" vc-dir-mark
743 :help "Mark the current file or all files in the region"))
745 (define-key map [sepopn] '("--"))
746 (define-key map [open-other]
747 '(menu-item "Open in other window" vc-dir-find-file-other-window
748 :help "Find the file on the current line, in another window"))
749 (define-key map [open]
750 '(menu-item "Open file" vc-dir-find-file
751 :help "Find the file on the current line"))
752 map)
753 "Menu for dispatcher status")
755 ;; This is used to that vlient modes can add mode-specific menu
756 ;; items to vc-dir-menu-map.
757 (defun vc-dir-menu-map-filter (orig-binding)
758 (when (and (symbolp orig-binding) (fboundp orig-binding))
759 (setq orig-binding (indirect-function orig-binding)))
760 (let ((ext-binding
761 (funcall (vc-client-object->extra-menu vc-client-mode))))
762 (if (null ext-binding)
763 orig-binding
764 (append orig-binding
765 '("----")
766 ext-binding))))
768 (defvar vc-dir-mode-map
769 (let ((map (make-keymap)))
770 (suppress-keymap map)
771 ;; Marking.
772 (define-key map "m" 'vc-dir-mark)
773 (define-key map "M" 'vc-dir-mark-all-files)
774 (define-key map "u" 'vc-dir-unmark)
775 (define-key map "U" 'vc-dir-unmark-all-files)
776 (define-key map "\C-?" 'vc-dir-unmark-file-up)
777 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
778 ;; Movement.
779 (define-key map "n" 'vc-dir-next-line)
780 (define-key map " " 'vc-dir-next-line)
781 (define-key map "\t" 'vc-dir-next-line)
782 (define-key map "p" 'vc-dir-previous-line)
783 (define-key map [backtab] 'vc-dir-previous-line)
784 ;;; Rebind paragraph-movement commands.
785 (define-key map "\M-}" 'vc-dir-next-directory)
786 (define-key map "\M-{" 'vc-dir-prev-directory)
787 (define-key map "\M-<down>" 'vc-dir-next-directory)
788 (define-key map "\M-<up>" 'vc-dir-prev-directory)
789 ;; The remainder.
790 (define-key map "f" 'vc-dir-find-file)
791 (define-key map "\C-m" 'vc-dir-find-file)
792 (define-key map "o" 'vc-dir-find-file-other-window)
793 (define-key map "q" 'quit-window)
794 (define-key map "g" 'vc-dir-refresh)
795 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
796 (define-key map [(down-mouse-3)] 'vc-dir-menu)
797 (define-key map [(mouse-2)] 'vc-dir-toggle-mark)
799 ;; Hook up the menu.
800 (define-key map [menu-bar vc-dir-mode]
801 '(menu-item
802 ;; This is used so that client modes can add mode-specific
803 ;; menu items to vc-dir-menu-map.
804 "*vc-dispatcher*" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
805 map)
806 "Keymap for directory buffer.")
808 (defmacro vc-at-event (event &rest body)
809 "Evaluate `body' wich point located at event-start of `event'.
810 If `body' uses `event', it should be a variable,
811 otherwise it will be evaluated twice."
812 (let ((posn (gensym "vc-at-event-posn")))
813 `(let ((,posn (event-start ,event)))
814 (save-excursion
815 (set-buffer (window-buffer (posn-window ,posn)))
816 (goto-char (posn-point ,posn))
817 ,@body))))
819 (defun vc-dir-menu (e)
820 "Popup the dispatcher status menu."
821 (interactive "e")
822 (vc-at-event e (popup-menu vc-dir-menu-map e)))
824 (defvar vc-dir-tool-bar-map
825 (let ((map (make-sparse-keymap)))
826 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
827 map vc-dir-mode-map)
828 (tool-bar-local-item "bookmark_add"
829 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
830 :help "Toggle mark on current item")
831 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
832 map vc-dir-mode-map
833 :rtl "right-arrow")
834 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
835 map vc-dir-mode-map
836 :rtl "left-arrow")
837 (tool-bar-local-item-from-menu 'vc-print-log "info"
838 map vc-dir-mode-map)
839 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
840 map vc-dir-mode-map)
841 (tool-bar-local-item-from-menu 'nonincremental-search-forward
842 "search" map)
843 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
844 map vc-dir-mode-map)
845 (tool-bar-local-item-from-menu 'quit-window "exit"
846 map vc-dir-mode-map)
847 map))
849 (defun vc-dir-update (entries buffer &optional noinsert)
850 "Update BUFFER's ewoc from the list of ENTRIES.
851 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
852 ;; Add ENTRIES to the vc-dir buffer BUFFER.
853 (with-current-buffer buffer
854 ;; Insert the entries sorted by name into the ewoc.
855 ;; We assume the ewoc is sorted too, which should be the
856 ;; case if we always add entries with vc-dir-update.
857 (setq entries
858 ;; Sort: first files and then subdirectories.
859 ;; XXX: this is VERY inefficient, it computes the directory
860 ;; names too many times
861 (sort entries
862 (lambda (entry1 entry2)
863 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
864 (dir2 (file-name-directory (expand-file-name (car entry2)))))
865 (cond
866 ((string< dir1 dir2) t)
867 ((not (string= dir1 dir2)) nil)
868 ((string< (car entry1) (car entry2))))))))
869 ;; Insert directory entries in the right places.
870 (let ((entry (car entries))
871 (node (ewoc-nth vc-ewoc 0)))
872 ;; Insert . if it is not present.
873 (unless node
874 (let ((rd (file-relative-name default-directory)))
875 (ewoc-enter-last
876 vc-ewoc (vc-dir-create-fileinfo
877 rd nil nil nil (expand-file-name default-directory))))
878 (setq node (ewoc-nth vc-ewoc 0)))
880 (while (and entry node)
881 (let* ((entryfile (car entry))
882 (entrydir (file-name-directory (expand-file-name entryfile)))
883 (nodedir
884 (or (vc-dir-fileinfo->directory (ewoc-data node))
885 (file-name-directory
886 (expand-file-name
887 (vc-dir-fileinfo->name (ewoc-data node)))))))
888 (cond
889 ;; First try to find the directory.
890 ((string-lessp nodedir entrydir)
891 (setq node (ewoc-next vc-ewoc node)))
892 ((string-equal nodedir entrydir)
893 ;; Found the directory, find the place for the file name.
894 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
895 (cond
896 ((string-lessp nodefile entryfile)
897 (setq node (ewoc-next vc-ewoc node)))
898 ((string-equal nodefile entryfile)
899 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
900 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
901 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
902 (ewoc-invalidate vc-ewoc node)
903 (setq entries (cdr entries) entry (car entries))
904 (setq node (ewoc-next vc-ewoc node)))
906 (ewoc-enter-before vc-ewoc node
907 (apply 'vc-dir-create-fileinfo entry))
908 (setq entries (cdr entries) entry (car entries))))))
910 ;; We need to insert a directory node
911 (let ((rd (file-relative-name entrydir)))
912 (ewoc-enter-last
913 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))
914 ;; Now insert the node itself.
915 (ewoc-enter-before vc-ewoc node
916 (apply 'vc-dir-create-fileinfo entry))
917 (setq entries (cdr entries) entry (car entries))))))
918 ;; We're past the last node, all remaining entries go to the end.
919 (unless (or node noinsert)
920 (let* ((lastnode (ewoc-nth vc-ewoc -1))
921 (lastdir
922 (or (vc-dir-fileinfo->directory (ewoc-data lastnode))
923 (file-name-directory
924 (expand-file-name
925 (vc-dir-fileinfo->name (ewoc-data lastnode)))))))
926 (dolist (entry entries)
927 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
928 ;; Insert a directory node if needed.
929 (unless (string-equal lastdir entrydir)
930 (setq lastdir entrydir)
931 (let ((rd (file-relative-name entrydir)))
932 (ewoc-enter-last
933 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
934 ;; Now insert the node itself.
935 (ewoc-enter-last vc-ewoc
936 (apply 'vc-dir-create-fileinfo entry)))))))))
938 (defun vc-dir-busy ()
939 (and (buffer-live-p vc-dir-process-buffer)
940 (get-buffer-process vc-dir-process-buffer)))
942 (defun vc-dir-kill-dir-status-process ()
943 "Kill the temporary buffer and associated process."
944 (interactive)
945 (when (buffer-live-p vc-dir-process-buffer)
946 (let ((proc (get-buffer-process vc-dir-process-buffer)))
947 (when proc (delete-process proc))
948 (setq vc-dir-process-buffer nil)
949 (setq mode-line-process nil))))
951 (defun vc-dir-kill-query ()
952 ;; Make sure that when the status buffer is killed the update
953 ;; process running in background is also killed.
954 (if (vc-dir-busy)
955 (when (y-or-n-p "Status update process running, really kill status buffer?")
956 (vc-dir-kill-dir-status-process)
960 (defun vc-dir-next-line (arg)
961 "Go to the next line.
962 If a prefix argument is given, move by that many lines."
963 (interactive "p")
964 (with-no-warnings
965 (ewoc-goto-next vc-ewoc arg)
966 (vc-dir-move-to-goal-column)))
968 (defun vc-dir-previous-line (arg)
969 "Go to the previous line.
970 If a prefix argument is given, move by that many lines."
971 (interactive "p")
972 (ewoc-goto-prev vc-ewoc arg)
973 (vc-dir-move-to-goal-column))
975 (defun vc-dir-next-directory ()
976 "Go to the next directory."
977 (interactive)
978 (let ((orig (point)))
979 (if
980 (catch 'foundit
981 (while t
982 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
983 (cond ((not next)
984 (throw 'foundit t))
986 (progn
987 (ewoc-goto-node vc-ewoc next)
988 (vc-dir-move-to-goal-column)
989 (if (vc-dir-fileinfo->directory (ewoc-data next))
990 (throw 'foundit nil))))))))
991 (goto-char orig))))
993 (defun vc-dir-prev-directory ()
994 "Go to the previous directory."
995 (interactive)
996 (let ((orig (point)))
997 (if
998 (catch 'foundit
999 (while t
1000 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
1001 (cond ((not prev)
1002 (throw 'foundit t))
1004 (progn
1005 (ewoc-goto-node vc-ewoc prev)
1006 (vc-dir-move-to-goal-column)
1007 (if (vc-dir-fileinfo->directory (ewoc-data prev))
1008 (throw 'foundit nil))))))))
1009 (goto-char orig))))
1011 (defun vc-dir-mark-unmark (mark-unmark-function)
1012 (if (use-region-p)
1013 (let ((firstl (line-number-at-pos (region-beginning)))
1014 (lastl (line-number-at-pos (region-end))))
1015 (save-excursion
1016 (goto-char (region-beginning))
1017 (while (<= (line-number-at-pos) lastl)
1018 (funcall mark-unmark-function))))
1019 (funcall mark-unmark-function)))
1021 (defun vc-dir-parent-marked-p (arg)
1022 ;; Return nil if none of the parent directories of arg is marked.
1023 (let* ((argdata (ewoc-data arg))
1024 (argdir
1025 (let ((crtdir (vc-dir-fileinfo->directory argdata)))
1026 (if crtdir
1027 crtdir
1028 (file-name-directory (expand-file-name
1029 (vc-dir-fileinfo->name argdata))))))
1030 (arglen (length argdir))
1031 (crt arg)
1032 data dir)
1033 ;; Go through the predecessors, checking if any directory that is
1034 ;; a parent is marked.
1035 (while (setq crt (ewoc-prev vc-ewoc crt))
1036 (setq data (ewoc-data crt))
1037 (setq dir
1038 (let ((crtdir (vc-dir-fileinfo->directory data)))
1039 (if crtdir
1040 crtdir
1041 (file-name-directory (expand-file-name
1042 (vc-dir-fileinfo->name data))))))
1044 (when (and (vc-dir-fileinfo->directory data)
1045 (string-equal (substring argdir 0 (length dir)) dir))
1046 (when (vc-dir-fileinfo->marked data)
1047 (error "Cannot mark `%s', parent directory `%s' marked"
1048 (vc-dir-fileinfo->name argdata)
1049 (vc-dir-fileinfo->name data)))))
1050 nil))
1052 (defun vc-dir-children-marked-p (arg)
1053 ;; Return nil if none of the children of arg is marked.
1054 (let* ((argdata (ewoc-data arg))
1055 (argdir (vc-dir-fileinfo->directory argdata))
1056 (arglen (length argdir))
1057 (is-child t)
1058 (crt arg)
1059 data dir)
1060 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1061 (setq data (ewoc-data crt))
1062 (setq dir
1063 (let ((crtdir (vc-dir-fileinfo->directory data)))
1064 (if crtdir
1065 crtdir
1066 (file-name-directory (expand-file-name
1067 (vc-dir-fileinfo->name data))))))
1068 (if (string-equal argdir (substring dir 0 arglen))
1069 (when (vc-dir-fileinfo->marked data)
1070 (error "Cannot mark `%s', child `%s' marked"
1071 (vc-dir-fileinfo->name argdata)
1072 (vc-dir-fileinfo->name data)))
1073 ;; We are done, we got to an entry that is not a child of `arg'.
1074 (setq is-child nil)))
1075 nil))
1077 (defun vc-dir-mark-file (&optional arg)
1078 ;; Mark ARG or the current file and move to the next line.
1079 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1080 (file (ewoc-data crt))
1081 (isdir (vc-dir-fileinfo->directory file)))
1082 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1083 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1084 (setf (vc-dir-fileinfo->marked file) t)
1085 (ewoc-invalidate vc-ewoc crt)
1086 (unless (or arg (mouse-event-p last-command-event))
1087 (vc-dir-next-line 1)))))
1089 (defun vc-dir-mark ()
1090 "Mark the current file or all files in the region.
1091 If the region is active, mark all the files in the region.
1092 Otherwise mark the file on the current line and move to the next
1093 line."
1094 (interactive)
1095 (vc-dir-mark-unmark 'vc-dir-mark-file))
1097 (defun vc-dir-mark-all-files (arg)
1098 "Mark all files with the same state as the current one.
1099 With a prefix argument mark all files.
1100 If the current entry is a directory, mark all child files.
1102 The commands operate on files that are on the same state.
1103 This command is intended to make it easy to select all files that
1104 share the same state."
1105 (interactive "P")
1106 (if arg
1107 ;; Mark all files.
1108 (progn
1109 ;; First check that no directory is marked, we can't mark
1110 ;; files in that case.
1111 (ewoc-map
1112 (lambda (filearg)
1113 (when (and (vc-dir-fileinfo->directory filearg)
1114 (vc-dir-fileinfo->directory filearg))
1115 (error "Cannot mark all files, directory `%s' marked"
1116 (vc-dir-fileinfo->name filearg))))
1117 vc-ewoc)
1118 (ewoc-map
1119 (lambda (filearg)
1120 (unless (vc-dir-fileinfo->marked filearg)
1121 (setf (vc-dir-fileinfo->marked filearg) t)
1123 vc-ewoc))
1124 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1125 (if (vc-dir-fileinfo->directory data)
1126 ;; It's a directory, mark child files.
1127 (let ((crt (ewoc-locate vc-ewoc)))
1128 (unless (vc-dir-children-marked-p crt)
1129 (while (setq crt (ewoc-next vc-ewoc crt))
1130 (let ((crt-data (ewoc-data crt)))
1131 (unless (vc-dir-fileinfo->directory crt-data)
1132 (setf (vc-dir-fileinfo->marked crt-data) t)
1133 (ewoc-invalidate vc-ewoc crt))))))
1134 ;; It's a file
1135 (let ((state (vc-dir-fileinfo->state data))
1136 (crt (ewoc-nth vc-ewoc 0)))
1137 (while crt
1138 (let ((crt-data (ewoc-data crt)))
1139 (when (and (not (vc-dir-fileinfo->marked crt-data))
1140 (eq (vc-dir-fileinfo->state crt-data) state)
1141 (not (vc-dir-fileinfo->directory crt-data)))
1142 (vc-dir-mark-file crt)))
1143 (setq crt (ewoc-next vc-ewoc crt))))))))
1145 (defun vc-dir-unmark-file ()
1146 ;; Unmark the current file and move to the next line.
1147 (let* ((crt (ewoc-locate vc-ewoc))
1148 (file (ewoc-data crt)))
1149 (setf (vc-dir-fileinfo->marked file) nil)
1150 (ewoc-invalidate vc-ewoc crt)
1151 (unless (mouse-event-p last-command-event)
1152 (vc-dir-next-line 1))))
1154 (defun vc-dir-unmark ()
1155 "Unmark the current file or all files in the region.
1156 If the region is active, unmark all the files in the region.
1157 Otherwise mark the file on the current line and move to the next
1158 line."
1159 (interactive)
1160 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1162 (defun vc-dir-unmark-file-up ()
1163 "Move to the previous line and unmark the file."
1164 (interactive)
1165 ;; If we're on the first line, we won't move up, but we will still
1166 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1167 ;; does.
1168 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1169 (file (ewoc-data prev)))
1170 (setf (vc-dir-fileinfo->marked file) nil)
1171 (ewoc-invalidate vc-ewoc prev)
1172 (vc-dir-move-to-goal-column)))
1174 (defun vc-dir-unmark-all-files (arg)
1175 "Unmark all files with the same state as the current one.
1176 With a prefix argument unmark all files.
1177 If the current entry is a directory, unmark all the child files.
1179 The commands operate on files that are on the same state.
1180 This command is intended to make it easy to deselect all files
1181 that share the same state."
1182 (interactive "P")
1183 (if arg
1184 (ewoc-map
1185 (lambda (filearg)
1186 (when (vc-dir-fileinfo->marked filearg)
1187 (setf (vc-dir-fileinfo->marked filearg) nil)
1189 vc-ewoc)
1190 (let* ((crt (ewoc-locate vc-ewoc))
1191 (data (ewoc-data crt)))
1192 (if (vc-dir-fileinfo->directory data)
1193 ;; It's a directory, unmark child files.
1194 (while (setq crt (ewoc-next vc-ewoc crt))
1195 (let ((crt-data (ewoc-data crt)))
1196 (unless (vc-dir-fileinfo->directory crt-data)
1197 (setf (vc-dir-fileinfo->marked crt-data) nil)
1198 (ewoc-invalidate vc-ewoc crt))))
1199 ;; It's a file
1200 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1201 (ewoc-map
1202 (lambda (filearg)
1203 (when (and (vc-dir-fileinfo->marked filearg)
1204 (eq (vc-dir-fileinfo->state filearg) crt-state))
1205 (setf (vc-dir-fileinfo->marked filearg) nil)
1207 vc-ewoc))))))
1209 (defun vc-dir-toggle-mark-file ()
1210 (let* ((crt (ewoc-locate vc-ewoc))
1211 (file (ewoc-data crt)))
1212 (if (vc-dir-fileinfo->marked file)
1213 (vc-dir-unmark-file)
1214 (vc-dir-mark-file))))
1216 (defun vc-dir-toggle-mark (e)
1217 (interactive "e")
1218 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1220 (defun vc-dir-delete-file ()
1221 "Delete the marked files, or the current file if no marks."
1222 (interactive)
1223 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1224 (list (vc-dir-current-file)))))
1226 (defun vc-dir-find-file ()
1227 "Find the file on the current line."
1228 (interactive)
1229 (find-file (vc-dir-current-file)))
1231 (defun vc-dir-find-file-other-window ()
1232 "Find the file on the current line, in another window."
1233 (interactive)
1234 (find-file-other-window (vc-dir-current-file)))
1236 (defun vc-dir-current-file ()
1237 (let ((node (ewoc-locate vc-ewoc)))
1238 (unless node
1239 (error "No file available."))
1240 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1242 (defun vc-dir-marked-files ()
1243 "Return the list of marked files."
1244 (mapcar
1245 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1246 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1248 (defun vc-dir-marked-only-files ()
1249 "Return the list of marked files, For marked directories return child files."
1250 (let ((crt (ewoc-nth vc-ewoc 0))
1251 result)
1252 (while crt
1253 (let ((crt-data (ewoc-data crt)))
1254 (if (vc-dir-fileinfo->marked crt-data)
1255 (if (vc-dir-fileinfo->directory crt-data)
1256 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1257 (dirlen (length dir))
1258 data)
1259 (while
1260 (and (setq crt (ewoc-next vc-ewoc crt))
1261 (string-equal
1262 (substring
1263 (progn
1264 (setq data (ewoc-data crt))
1265 (let ((crtdir (vc-dir-fileinfo->directory data)))
1266 (if crtdir
1267 crtdir
1268 (file-name-directory
1269 (expand-file-name
1270 (vc-dir-fileinfo->name data))))))
1271 0 dirlen)
1272 dir))
1273 (unless (vc-dir-fileinfo->directory data)
1274 (push (vc-dir-fileinfo->name data) result))))
1275 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1276 (setq crt (ewoc-next vc-ewoc crt)))
1277 (setq crt (ewoc-next vc-ewoc crt)))))
1278 result))
1280 (defun vc-directory-resynch-file (&optional fname)
1281 "Update the entries for FILE in any directory buffers that list it."
1282 (let ((file (or fname (expand-file-name buffer-file-name))))
1283 ;; The vc-dir case
1284 (let ((found-vc-dir-buf nil))
1285 (save-excursion
1286 (dolist (status-buf (buffer-list))
1287 (set-buffer status-buf)
1288 ;; look for a vc-dir buffer that might show this file.
1289 (when (eq major-mode 'vc-dir-mode)
1290 (setq found-vc-dir-buf t)
1291 (let ((ddir (expand-file-name default-directory)))
1292 ;; This test is cvs-string-prefix-p
1293 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
1294 (let*
1295 ((file-short (substring file (length ddir)))
1296 (state
1297 (funcall (vc-client-object->file-to-state vc-client-mode)
1298 file))
1299 (extra
1300 (funcall (vc-client-object->file-to-extra vc-client-mode)
1301 file))
1302 (entry
1303 (list file-short state extra)))
1304 (vc-dir-update (list entry) status-buf))))))
1305 ;; We didn't find any vc-dir buffers, remove the hook, it is
1306 ;; not needed.
1307 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-directory-resynch-file))))))
1309 (defun vc-dir-mode (client-object)
1310 "Major mode for dispatcher directory buffers.
1311 Marking/Unmarking key bindings and actions:
1312 m - marks a file/directory or if the region is active, mark all the files
1313 in region.
1314 Restrictions: - a file cannot be marked if any parent directory is marked
1315 - a directory cannot be marked if any child file or
1316 directory is marked
1317 u - marks a file/directory or if the region is active, unmark all the files
1318 in region.
1319 M - if the cursor is on a file: mark all the files with the same state as
1320 the current file
1321 - if the cursor is on a directory: mark all child files
1322 - with a prefix argument: mark all files
1323 U - if the cursor is on a file: unmark all the files with the same state
1324 as the current file
1325 - if the cursor is on a directory: unmark all child files
1326 - with a prefix argument: unmark all files
1329 \\{vc-dir-mode-map}"
1330 (setq mode-name (vc-client-object->name client-object))
1331 (setq major-mode 'vc-dir-mode)
1332 (setq buffer-read-only t)
1333 (use-local-map vc-dir-mode-map)
1334 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
1335 (set (make-local-variable 'vc-client-mode) client-object)
1336 (let ((buffer-read-only nil))
1337 (erase-buffer)
1338 (set (make-local-variable 'vc-dir-process-buffer) nil)
1339 (set (make-local-variable 'vc-ewoc)
1340 (ewoc-create (vc-client-object->file-to-info client-object)
1341 (vc-client-object->headers client-object)))
1342 (add-hook 'after-save-hook 'vc-directory-resynch-file)
1343 ;; Make sure that if the directory buffer is killed, the update
1344 ;; process running in the background is also killed.
1345 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1346 (funcall (vc-client-object->updater client-object)))
1347 (run-hooks 'vc-dir-mode-hook))
1349 (put 'vc-dir-mode 'mode-class 'special)
1351 (defun vc-dispatcher-browsing ()
1352 "Are we in a directory browser buffer?"
1353 (derived-mode-p 'vc-dir-mode))
1355 (defun vc-dispatcher-in-fileset-p (fileset)
1356 (let ((member nil))
1357 (while (and (not member) fileset)
1358 (let ((elem (pop fileset)))
1359 (if (if (file-directory-p elem)
1360 (eq t (compare-strings buffer-file-name nil (length elem)
1361 elem nil nil))
1362 (eq (current-buffer) (get-file-buffer elem)))
1363 (setq member t))))
1364 member))
1366 (defun vc-dispatcher-selection-set (&optional observer)
1367 "Deduce a set of files to which to apply an operation. Return a cons
1368 cell (SELECTION . FILESET), where SELECTION is what the user chose
1369 and FILES is the flist with any directories replaced by the listed files
1370 within them.
1372 If we're in a directory display, the fileset is the list of marked files (if
1373 there is one) else the file on the curreent line. If not in a directory
1374 display, but the current buffer visits a file, the fileset is a singleton
1375 containing that file. Otherwise, throw an error."
1376 (let ((selection
1377 (cond
1378 ;; Browsing with vc-dir
1379 ((vc-dispatcher-browsing)
1380 ;; If no files are marked, temporatrily mark current file
1381 ;; and choose on that basis (so we get subordinate files)
1382 (if (not (vc-dir-marked-files))
1383 (prog2
1384 (vc-dir-mark-file)
1385 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
1386 (vc-dir-unmark-all-files t))
1387 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
1388 ;; Visiting an eligible file
1389 ((buffer-file-name)
1390 (cons (list buffer-file-name) (list buffer-file-name)))
1391 ;; No eligible file -- if there's a parent buffer, deduce from there
1392 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1393 (with-current-buffer vc-parent-buffer
1394 (vc-dispatcher-browsing))))
1395 (with-current-buffer vc-parent-buffer
1396 (vc-dispatcher-selection-set)))
1397 ;; No good set here, throw error
1398 (t (error "No fileset is available here")))))
1399 ;; We assume, in order to avoid unpleasant surprises to the user,
1400 ;; that a fileset is not in good shape to be handed to the user if the
1401 ;; buffers visiting the fileset don't match the on-disk contents.
1402 (if (not observer)
1403 (save-some-buffers
1404 nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
1405 selection))
1407 (provide 'vc-dispatcher)
1409 ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
1410 ;;; vc-dispatcher.el ends here