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