]> code.delx.au - gnu-emacs/blob - lisp/vc-dispatcher.el
Fix an incorrect buffer name and remove an unneeded defalias.
[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 ;; comand, synchronously or asynchronously, making the output available
90 ;; in a command log buffer. Two other functions, (vc-start-annotation) and
91 ;; (vc-finish-logentry), allow you to associate a command closure with an
92 ;; abbotation buffer so that when the user confirms the comment the closure
93 ;; is run (with the comment as part of its context).
94 ;;
95 ;; The interface to the upper level has the two main entry points (vc-dir)
96 ;; and (vc-dispatcher-selection-set) and a couple of convenience functions.
97 ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set)
98 ;; returns a selection set of files, either the marked files in a browsing
99 ;; buffer or the singleton set consisting of the file visited by the current
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
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)
136
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)
144
145 (defcustom vc-command-messages nil
146 "If non-nil, display run messages from back-end commands."
147 :type 'boolean
148 :group 'vc)
149
150 (defcustom vc-suppress-confirm nil
151 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
152 :type 'boolean
153 :group 'vc)
154
155 ;; Variables the user doesn't need to know about.
156
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)
162
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 VC 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)
170
171 ;; Common command execution logic
172
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))))))))
186
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))))
200
201 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
202
203 (defun vc-process-sentinel (p s)
204 (let ((previous (process-get p 'vc-previous-sentinel))
205 (buf (process-buffer p)))
206 ;; Impatient users sometime kill "slow" buffers; check liveness
207 ;; to avoid "error in process sentinel: Selecting deleted buffer".
208 (when (buffer-live-p buf)
209 (when previous (funcall previous p s))
210 (with-current-buffer buf
211 (setq mode-line-process
212 (let ((status (process-status p)))
213 ;; Leave mode-line uncluttered, normally.
214 (unless (eq 'exit status)
215 (format " (%s)" status))))
216 (let (vc-sentinel-movepoint)
217 ;; Normally, we want async code such as sentinels to not move point.
218 (save-excursion
219 (goto-char (process-mark p))
220 (let ((cmds (process-get p 'vc-sentinel-commands)))
221 (process-put p 'vc-sentinel-commands nil)
222 (dolist (cmd cmds)
223 ;; Each sentinel may move point and the next one should be run
224 ;; at that new point. We could get the same result by having
225 ;; each sentinel read&set process-mark, but since `cmd' needs
226 ;; to work both for async and sync processes, this would be
227 ;; difficult to achieve.
228 (vc-exec-after cmd))))
229 ;; But sometimes the sentinels really want to move point.
230 (when vc-sentinel-movepoint
231 (let ((win (get-buffer-window (current-buffer) 0)))
232 (if (not win)
233 (goto-char vc-sentinel-movepoint)
234 (with-selected-window win
235 (goto-char vc-sentinel-movepoint))))))))))
236
237 (defun vc-set-mode-line-busy-indicator ()
238 (setq mode-line-process
239 (concat " " (propertize "[waiting...]"
240 'face 'mode-line-emphasis
241 'help-echo
242 "A VC command is in progress in this buffer"))))
243
244 (defun vc-exec-after (code)
245 "Eval CODE when the current buffer's process is done.
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)
274
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.")
279
280 (defvar w32-quote-process-args)
281
282 (defun vc-delistify (filelist)
283 "Smash a FILELIST into a file list string suitable for info messages."
284 ;; FIXME what about file names with spaces?
285 (if (not filelist) "." (mapconcat 'identity filelist " ")))
286
287 ;;;###autoload
288 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
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)
315 " "
316 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
317 " " (vc-delistify files))))
318 (save-current-buffer
319 (unless (or (eq buffer t)
320 (and (stringp buffer)
321 (string= (buffer-name) buffer))
322 (eq buffer (current-buffer)))
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))))
382
383 ;; These functions are used to ensure that the view the user sees is up to date
384 ;; even if the dispatcher client mode has messed with file contents (as in,
385 ;; for example, VCS keyword expansion).
386
387 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
388
389 (defun vc-position-context (posn)
390 "Save a bit of the text around POSN in the current buffer.
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)))))
399
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))))))))
419
420 (defun vc-context-matches-p (posn context)
421 "Return t if POSN matches CONTEXT, nil otherwise."
422 (let* ((context-string (nth 2 context))
423 (len (length context-string))
424 (end (+ posn len)))
425 (if (> end (1+ (buffer-size)))
426 nil
427 (string= context-string (buffer-substring posn end)))))
428
429 (defun vc-buffer-context ()
430 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
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)))
439
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))))))
454
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)))
472
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)))))
498
499 (defun vc-resynch-buffer (file &optional keep noquery)
500 "If FILE is currently visited, resynch its buffer."
501 (if (string= buffer-file-name file)
502 (vc-resynch-window file keep noquery)
503 (let ((buffer (get-file-buffer file)))
504 (when buffer
505 (with-current-buffer buffer
506 (vc-resynch-window file keep noquery)))))
507 (vc-directory-resynch-file file))
508
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")))))
518
519 ;; Command closures
520
521 ;; Set up key bindings for use while editing log messages
522
523 (defun vc-log-edit (fileset)
524 "Set up `log-edit' for use with VC on FILE."
525 (setq default-directory
526 (with-current-buffer vc-parent-buffer default-directory))
527 (log-edit 'vc-finish-logentry
528 nil
529 `((log-edit-listfun . (lambda () ',fileset))
530 (log-edit-diff-function . (lambda () (vc-diff nil)))))
531 (set (make-local-variable 'vc-log-fileset) fileset)
532 (make-local-variable 'vc-log-extra)
533 (set-buffer-modified-p nil)
534 (setq buffer-file-name nil))
535
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)))))
573
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)))
622
623 ;; The ewoc-based vc-directory implementation
624
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)
630
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)
649
650 ;; Used to describe a dispatcher client mode.
651 (defstruct (vc-client-object
652 (:copier nil)
653 (:constructor
654 vc-create-client-object (name
655 headers
656 file-to-info
657 file-to-state
658 file-to-extra
659 updater))
660 (:conc-name vc-client-object->))
661 name
662 headers
663 file-to-info
664 file-to-state
665 file-to-extra
666 updater)
667
668 (defvar vc-ewoc nil)
669 (defvar vc-dir-process-buffer nil
670 "The buffer used for the asynchronous call that computes status.")
671
672 (defun vc-dir-move-to-goal-column ()
673 ;; Used to keep the cursor on the file name column.
674 (beginning-of-line)
675 ;; Must be in sync with vc-default-status-printer.
676 (forward-char 25))
677
678 (defun vc-dir-prepare-status-buffer (dir &optional create-new)
679 "Find a *vc-dir* buffer showing DIR, or create a new one."
680 (setq dir (expand-file-name dir))
681 (let* ((bname "*vc-dir*")
682 ;; Look for another *vc-dir* buffer visiting the same directory.
683 (buf (save-excursion
684 (unless create-new
685 (dolist (buffer (buffer-list))
686 (set-buffer buffer)
687 (when (and (vc-dispatcher-browsing)
688 (string= (expand-file-name default-directory) dir))
689 (return buffer)))))))
690 (or buf
691 ;; Create a new *vc-dir* buffer.
692 (with-current-buffer (create-file-buffer bname)
693 (cd dir)
694 (vc-setup-buffer (current-buffer))
695 ;; Reset the vc-parent-buffer-name so that it does not appear
696 ;; in the mode-line.
697 (setq vc-parent-buffer-name nil)
698 (current-buffer)))))
699
700 (defvar vc-dir-menu-map
701 (let ((map (make-sparse-keymap "VC-dir")))
702 (define-key map [quit]
703 '(menu-item "Quit" quit-window
704 :help "Quit"))
705 (define-key map [kill]
706 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
707 :enable (vc-dir-busy)
708 :help "Kill the command that updates the directory buffer"))
709 (define-key map [refresh]
710 '(menu-item "Refresh" vc-dir-refresh
711 :enable (not (vc-dir-busy))
712 :help "Refresh the contents of the directory buffer"))
713 ;; Movement.
714 (define-key map [sepmv] '("--"))
715 (define-key map [next-line]
716 '(menu-item "Next line" vc-dir-next-line
717 :help "Go to the next line" :keys "n"))
718 (define-key map [previous-line]
719 '(menu-item "Previous line" vc-dir-previous-line
720 :help "Go to the previous line"))
721 ;; Marking.
722 (define-key map [sepmrk] '("--"))
723 (define-key map [unmark-all]
724 '(menu-item "Unmark All" vc-dir-unmark-all-files
725 :help "Unmark all files that are in the same state as the current file\
726 \nWith prefix argument unmark all files"))
727 (define-key map [unmark-previous]
728 '(menu-item "Unmark previous " vc-dir-unmark-file-up
729 :help "Move to the previous line and unmark the file"))
730
731 (define-key map [mark-all]
732 '(menu-item "Mark All" vc-dir-mark-all-files
733 :help "Mark all files that are in the same state as the current file\
734 \nWith prefix argument mark all files"))
735 (define-key map [unmark]
736 '(menu-item "Unmark" vc-dir-unmark
737 :help "Unmark the current file or all files in the region"))
738
739 (define-key map [mark]
740 '(menu-item "Mark" vc-dir-mark
741 :help "Mark the current file or all files in the region"))
742
743 (define-key map [sepopn] '("--"))
744 (define-key map [open-other]
745 '(menu-item "Open in other window" vc-dir-find-file-other-window
746 :help "Find the file on the current line, in another window"))
747 (define-key map [open]
748 '(menu-item "Open file" vc-dir-find-file
749 :help "Find the file on the current line"))
750 map)
751 "Menu for dispatcher status")
752
753 (defvar vc-dir-mode-map
754 (let ((map (make-keymap)))
755 (suppress-keymap map)
756 ;; Marking.
757 (define-key map "m" 'vc-dir-mark)
758 (define-key map "M" 'vc-dir-mark-all-files)
759 (define-key map "u" 'vc-dir-unmark)
760 (define-key map "U" 'vc-dir-unmark-all-files)
761 (define-key map "\C-?" 'vc-dir-unmark-file-up)
762 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
763 ;; Movement.
764 (define-key map "n" 'vc-dir-next-line)
765 (define-key map " " 'vc-dir-next-line)
766 (define-key map "\t" 'vc-dir-next-line)
767 (define-key map "p" 'vc-dir-previous-line)
768 (define-key map [backtab] 'vc-dir-previous-line)
769 ;;; Rebind paragraph-movement commands.
770 (define-key map "\M-}" 'vc-dir-next-directory)
771 (define-key map "\M-{" 'vc-dir-prev-directory)
772 (define-key map "\M-<down>" 'vc-dir-next-directory)
773 (define-key map "\M-<up>" 'vc-dir-prev-directory)
774 ;; The remainder.
775 (define-key map "f" 'vc-dir-find-file)
776 (define-key map "\C-m" 'vc-dir-find-file)
777 (define-key map "o" 'vc-dir-find-file-other-window)
778 (define-key map "q" 'quit-window)
779 (define-key map "g" 'vc-dir-refresh)
780 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
781 (define-key map [(down-mouse-3)] 'vc-dir-menu)
782 (define-key map [(mouse-2)] 'vc-dir-toggle-mark)
783
784 ;; FIXME: Calls back into vc.el
785 ;; Hook up the menu.
786 (define-key map [menu-bar vc-dir-mode]
787 '(menu-item
788 ;; This is used so that client modes can add mode-specific
789 ;; menu items to vc-dir-menu-map.
790 "*vc-dir*" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
791 map)
792 "Keymap for directory buffer.")
793
794 (defmacro vc-at-event (event &rest body)
795 "Evaluate `body' wich point located at event-start of `event'.
796 If `body' uses `event', it should be a variable,
797 otherwise it will be evaluated twice."
798 (let ((posn (gensym "vc-at-event-posn")))
799 `(let ((,posn (event-start ,event)))
800 (save-excursion
801 (set-buffer (window-buffer (posn-window ,posn)))
802 (goto-char (posn-point ,posn))
803 ,@body))))
804
805 (defun vc-dir-menu (e)
806 "Popup the dispatcher status menu."
807 (interactive "e")
808 (vc-at-event e (popup-menu vc-dir-menu-map e)))
809
810 (defvar vc-dir-tool-bar-map
811 (let ((map (make-sparse-keymap)))
812 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
813 map vc-dir-mode-map)
814 (tool-bar-local-item "bookmark_add"
815 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
816 :help "Toggle mark on current item")
817 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
818 map vc-dir-mode-map
819 :rtl "right-arrow")
820 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
821 map vc-dir-mode-map
822 :rtl "left-arrow")
823 (tool-bar-local-item-from-menu 'vc-print-log "info"
824 map vc-dir-mode-map)
825 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
826 map vc-dir-mode-map)
827 (tool-bar-local-item-from-menu 'nonincremental-search-forward
828 "search" map)
829 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
830 map vc-dir-mode-map)
831 (tool-bar-local-item-from-menu 'quit-window "exit"
832 map vc-dir-mode-map)
833 map))
834
835 (defun vc-dir-update (entries buffer &optional noinsert)
836 "Update BUFFER's ewoc from the list of ENTRIES.
837 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
838 ;; Add ENTRIES to the vc-dir buffer BUFFER.
839 (with-current-buffer buffer
840 ;; Insert the entries sorted by name into the ewoc.
841 ;; We assume the ewoc is sorted too, which should be the
842 ;; case if we always add entries with vc-dir-update.
843 (setq entries
844 ;; Sort: first files and then subdirectories.
845 ;; XXX: this is VERY inefficient, it computes the directory
846 ;; names too many times
847 (sort entries
848 (lambda (entry1 entry2)
849 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
850 (dir2 (file-name-directory (expand-file-name (car entry2)))))
851 (cond
852 ((string< dir1 dir2) t)
853 ((not (string= dir1 dir2)) nil)
854 ((string< (car entry1) (car entry2))))))))
855 ;; Insert directory entries in the right places.
856 (let ((entry (car entries))
857 (node (ewoc-nth vc-ewoc 0)))
858 ;; Insert . if it is not present.
859 (unless node
860 (let ((rd (file-relative-name default-directory)))
861 (ewoc-enter-last
862 vc-ewoc (vc-dir-create-fileinfo
863 rd nil nil nil (expand-file-name default-directory))))
864 (setq node (ewoc-nth vc-ewoc 0)))
865
866 (while (and entry node)
867 (let* ((entryfile (car entry))
868 (entrydir (file-name-directory (expand-file-name entryfile)))
869 (nodedir
870 (or (vc-dir-fileinfo->directory (ewoc-data node))
871 (file-name-directory
872 (expand-file-name
873 (vc-dir-fileinfo->name (ewoc-data node)))))))
874 (cond
875 ;; First try to find the directory.
876 ((string-lessp nodedir entrydir)
877 (setq node (ewoc-next vc-ewoc node)))
878 ((string-equal nodedir entrydir)
879 ;; Found the directory, find the place for the file name.
880 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
881 (cond
882 ((string-lessp nodefile entryfile)
883 (setq node (ewoc-next vc-ewoc node)))
884 ((string-equal nodefile entryfile)
885 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
886 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
887 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
888 (ewoc-invalidate vc-ewoc node)
889 (setq entries (cdr entries) entry (car entries))
890 (setq node (ewoc-next vc-ewoc node)))
891 (t
892 (ewoc-enter-before vc-ewoc node
893 (apply 'vc-dir-create-fileinfo entry))
894 (setq entries (cdr entries) entry (car entries))))))
895 (t
896 ;; We need to insert a directory node
897 (let ((rd (file-relative-name entrydir)))
898 (ewoc-enter-last
899 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))
900 ;; Now insert the node itself.
901 (ewoc-enter-before vc-ewoc node
902 (apply 'vc-dir-create-fileinfo entry))
903 (setq entries (cdr entries) entry (car entries))))))
904 ;; We're past the last node, all remaining entries go to the end.
905 (unless (or node noinsert)
906 (let* ((lastnode (ewoc-nth vc-ewoc -1))
907 (lastdir
908 (or (vc-dir-fileinfo->directory (ewoc-data lastnode))
909 (file-name-directory
910 (expand-file-name
911 (vc-dir-fileinfo->name (ewoc-data lastnode)))))))
912 (dolist (entry entries)
913 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
914 ;; Insert a directory node if needed.
915 (unless (string-equal lastdir entrydir)
916 (setq lastdir entrydir)
917 (let ((rd (file-relative-name entrydir)))
918 (ewoc-enter-last
919 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
920 ;; Now insert the node itself.
921 (ewoc-enter-last vc-ewoc
922 (apply 'vc-dir-create-fileinfo entry)))))))))
923
924 (defun vc-dir-busy ()
925 (and (buffer-live-p vc-dir-process-buffer)
926 (get-buffer-process vc-dir-process-buffer)))
927
928 (defun vc-dir-kill-dir-status-process ()
929 "Kill the temporary buffer and associated process."
930 (interactive)
931 (when (buffer-live-p vc-dir-process-buffer)
932 (let ((proc (get-buffer-process vc-dir-process-buffer)))
933 (when proc (delete-process proc))
934 (setq vc-dir-process-buffer nil)
935 (setq mode-line-process nil))))
936
937 (defun vc-dir-kill-query ()
938 ;; Make sure that when the status buffer is killed the update
939 ;; process running in background is also killed.
940 (if (vc-dir-busy)
941 (when (y-or-n-p "Status update process running, really kill status buffer?")
942 (vc-dir-kill-dir-status-process)
943 t)
944 t))
945
946 (defun vc-dir-next-line (arg)
947 "Go to the next line.
948 If a prefix argument is given, move by that many lines."
949 (interactive "p")
950 (with-no-warnings
951 (ewoc-goto-next vc-ewoc arg)
952 (vc-dir-move-to-goal-column)))
953
954 (defun vc-dir-previous-line (arg)
955 "Go to the previous line.
956 If a prefix argument is given, move by that many lines."
957 (interactive "p")
958 (ewoc-goto-prev vc-ewoc arg)
959 (vc-dir-move-to-goal-column))
960
961 (defun vc-dir-next-directory ()
962 "Go to the next directory."
963 (interactive)
964 (let ((orig (point)))
965 (if
966 (catch 'foundit
967 (while t
968 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
969 (cond ((not next)
970 (throw 'foundit t))
971 (t
972 (progn
973 (ewoc-goto-node vc-ewoc next)
974 (vc-dir-move-to-goal-column)
975 (if (vc-dir-fileinfo->directory (ewoc-data next))
976 (throw 'foundit nil))))))))
977 (goto-char orig))))
978
979 (defun vc-dir-prev-directory ()
980 "Go to the previous directory."
981 (interactive)
982 (let ((orig (point)))
983 (if
984 (catch 'foundit
985 (while t
986 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
987 (cond ((not prev)
988 (throw 'foundit t))
989 (t
990 (progn
991 (ewoc-goto-node vc-ewoc prev)
992 (vc-dir-move-to-goal-column)
993 (if (vc-dir-fileinfo->directory (ewoc-data prev))
994 (throw 'foundit nil))))))))
995 (goto-char orig))))
996
997 (defun vc-dir-mark-unmark (mark-unmark-function)
998 (if (use-region-p)
999 (let ((firstl (line-number-at-pos (region-beginning)))
1000 (lastl (line-number-at-pos (region-end))))
1001 (save-excursion
1002 (goto-char (region-beginning))
1003 (while (<= (line-number-at-pos) lastl)
1004 (funcall mark-unmark-function))))
1005 (funcall mark-unmark-function)))
1006
1007 (defun vc-dir-parent-marked-p (arg)
1008 ;; Return nil if none of the parent directories of arg is marked.
1009 (let* ((argdata (ewoc-data arg))
1010 (argdir
1011 (let ((crtdir (vc-dir-fileinfo->directory argdata)))
1012 (if crtdir
1013 crtdir
1014 (file-name-directory (expand-file-name
1015 (vc-dir-fileinfo->name argdata))))))
1016 (arglen (length argdir))
1017 (crt arg)
1018 data dir)
1019 ;; Go through the predecessors, checking if any directory that is
1020 ;; a parent is marked.
1021 (while (setq crt (ewoc-prev vc-ewoc crt))
1022 (setq data (ewoc-data crt))
1023 (setq dir
1024 (let ((crtdir (vc-dir-fileinfo->directory data)))
1025 (if crtdir
1026 crtdir
1027 (file-name-directory (expand-file-name
1028 (vc-dir-fileinfo->name data))))))
1029
1030 (when (and (vc-dir-fileinfo->directory data)
1031 (string-equal (substring argdir 0 (length dir)) dir))
1032 (when (vc-dir-fileinfo->marked data)
1033 (error "Cannot mark `%s', parent directory `%s' marked"
1034 (vc-dir-fileinfo->name argdata)
1035 (vc-dir-fileinfo->name data)))))
1036 nil))
1037
1038 (defun vc-dir-children-marked-p (arg)
1039 ;; Return nil if none of the children of arg is marked.
1040 (let* ((argdata (ewoc-data arg))
1041 (argdir (vc-dir-fileinfo->directory argdata))
1042 (arglen (length argdir))
1043 (is-child t)
1044 (crt arg)
1045 data dir)
1046 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1047 (setq data (ewoc-data crt))
1048 (setq dir
1049 (let ((crtdir (vc-dir-fileinfo->directory data)))
1050 (if crtdir
1051 crtdir
1052 (file-name-directory (expand-file-name
1053 (vc-dir-fileinfo->name data))))))
1054 (if (string-equal argdir (substring dir 0 arglen))
1055 (when (vc-dir-fileinfo->marked data)
1056 (error "Cannot mark `%s', child `%s' marked"
1057 (vc-dir-fileinfo->name argdata)
1058 (vc-dir-fileinfo->name data)))
1059 ;; We are done, we got to an entry that is not a child of `arg'.
1060 (setq is-child nil)))
1061 nil))
1062
1063 (defun vc-dir-mark-file (&optional arg)
1064 ;; Mark ARG or the current file and move to the next line.
1065 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1066 (file (ewoc-data crt))
1067 (isdir (vc-dir-fileinfo->directory file)))
1068 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1069 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1070 (setf (vc-dir-fileinfo->marked file) t)
1071 (ewoc-invalidate vc-ewoc crt)
1072 (unless (or arg (mouse-event-p last-command-event))
1073 (vc-dir-next-line 1)))))
1074
1075 (defun vc-dir-mark ()
1076 "Mark the current file or all files in the region.
1077 If the region is active, mark all the files in the region.
1078 Otherwise mark the file on the current line and move to the next
1079 line."
1080 (interactive)
1081 (vc-dir-mark-unmark 'vc-dir-mark-file))
1082
1083 (defun vc-dir-mark-all-files (arg)
1084 "Mark all files with the same state as the current one.
1085 With a prefix argument mark all files.
1086 If the current entry is a directory, mark all child files.
1087
1088 The VC commands operate on files that are on the same state.
1089 This command is intended to make it easy to select all files that
1090 share the same state."
1091 (interactive "P")
1092 (if arg
1093 ;; Mark all files.
1094 (progn
1095 ;; First check that no directory is marked, we can't mark
1096 ;; files in that case.
1097 (ewoc-map
1098 (lambda (filearg)
1099 (when (and (vc-dir-fileinfo->directory filearg)
1100 (vc-dir-fileinfo->directory filearg))
1101 (error "Cannot mark all files, directory `%s' marked"
1102 (vc-dir-fileinfo->name filearg))))
1103 vc-ewoc)
1104 (ewoc-map
1105 (lambda (filearg)
1106 (unless (vc-dir-fileinfo->marked filearg)
1107 (setf (vc-dir-fileinfo->marked filearg) t)
1108 t))
1109 vc-ewoc))
1110 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1111 (if (vc-dir-fileinfo->directory data)
1112 ;; It's a directory, mark child files.
1113 (let ((crt (ewoc-locate vc-ewoc)))
1114 (unless (vc-dir-children-marked-p crt)
1115 (while (setq crt (ewoc-next vc-ewoc crt))
1116 (let ((crt-data (ewoc-data crt)))
1117 (unless (vc-dir-fileinfo->directory crt-data)
1118 (setf (vc-dir-fileinfo->marked crt-data) t)
1119 (ewoc-invalidate vc-ewoc crt))))))
1120 ;; It's a file
1121 (let ((state (vc-dir-fileinfo->state data))
1122 (crt (ewoc-nth vc-ewoc 0)))
1123 (while crt
1124 (let ((crt-data (ewoc-data crt)))
1125 (when (and (not (vc-dir-fileinfo->marked crt-data))
1126 (eq (vc-dir-fileinfo->state crt-data) state)
1127 (not (vc-dir-fileinfo->directory crt-data)))
1128 (vc-dir-mark-file crt)))
1129 (setq crt (ewoc-next vc-ewoc crt))))))))
1130
1131 (defun vc-dir-unmark-file ()
1132 ;; Unmark the current file and move to the next line.
1133 (let* ((crt (ewoc-locate vc-ewoc))
1134 (file (ewoc-data crt)))
1135 (setf (vc-dir-fileinfo->marked file) nil)
1136 (ewoc-invalidate vc-ewoc crt)
1137 (unless (mouse-event-p last-command-event)
1138 (vc-dir-next-line 1))))
1139
1140 (defun vc-dir-unmark ()
1141 "Unmark the current file or all files in the region.
1142 If the region is active, unmark all the files in the region.
1143 Otherwise mark the file on the current line and move to the next
1144 line."
1145 (interactive)
1146 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1147
1148 (defun vc-dir-unmark-file-up ()
1149 "Move to the previous line and unmark the file."
1150 (interactive)
1151 ;; If we're on the first line, we won't move up, but we will still
1152 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1153 ;; does.
1154 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1155 (file (ewoc-data prev)))
1156 (setf (vc-dir-fileinfo->marked file) nil)
1157 (ewoc-invalidate vc-ewoc prev)
1158 (vc-dir-move-to-goal-column)))
1159
1160 (defun vc-dir-unmark-all-files (arg)
1161 "Unmark all files with the same state as the current one.
1162 With a prefix argument unmark all files.
1163 If the current entry is a directory, unmark all the child files.
1164
1165 The VC commands operate on files that are on the same state.
1166 This command is intended to make it easy to deselect all files
1167 that share the same state."
1168 (interactive "P")
1169 (if arg
1170 (ewoc-map
1171 (lambda (filearg)
1172 (when (vc-dir-fileinfo->marked filearg)
1173 (setf (vc-dir-fileinfo->marked filearg) nil)
1174 t))
1175 vc-ewoc)
1176 (let* ((crt (ewoc-locate vc-ewoc))
1177 (data (ewoc-data crt)))
1178 (if (vc-dir-fileinfo->directory data)
1179 ;; It's a directory, unmark child files.
1180 (while (setq crt (ewoc-next vc-ewoc crt))
1181 (let ((crt-data (ewoc-data crt)))
1182 (unless (vc-dir-fileinfo->directory crt-data)
1183 (setf (vc-dir-fileinfo->marked crt-data) nil)
1184 (ewoc-invalidate vc-ewoc crt))))
1185 ;; It's a file
1186 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1187 (ewoc-map
1188 (lambda (filearg)
1189 (when (and (vc-dir-fileinfo->marked filearg)
1190 (eq (vc-dir-fileinfo->state filearg) crt-state))
1191 (setf (vc-dir-fileinfo->marked filearg) nil)
1192 t))
1193 vc-ewoc))))))
1194
1195 (defun vc-dir-toggle-mark-file ()
1196 (let* ((crt (ewoc-locate vc-ewoc))
1197 (file (ewoc-data crt)))
1198 (if (vc-dir-fileinfo->marked file)
1199 (vc-dir-unmark-file)
1200 (vc-dir-mark-file))))
1201
1202 (defun vc-dir-toggle-mark (e)
1203 (interactive "e")
1204 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1205
1206 (defun vc-dir-delete-file ()
1207 "Delete the marked files, or the current file if no marks."
1208 (interactive)
1209 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1210 (list (vc-dir-current-file)))))
1211
1212 (defun vc-dir-find-file ()
1213 "Find the file on the current line."
1214 (interactive)
1215 (find-file (vc-dir-current-file)))
1216
1217 (defun vc-dir-find-file-other-window ()
1218 "Find the file on the current line, in another window."
1219 (interactive)
1220 (find-file-other-window (vc-dir-current-file)))
1221
1222 (defun vc-dir-current-file ()
1223 (let ((node (ewoc-locate vc-ewoc)))
1224 (unless node
1225 (error "No file available."))
1226 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1227
1228 (defun vc-dir-marked-files ()
1229 "Return the list of marked files."
1230 (mapcar
1231 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1232 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1233
1234 (defun vc-dir-marked-only-files ()
1235 "Return the list of marked files, For marked directories return child files."
1236 (let ((crt (ewoc-nth vc-ewoc 0))
1237 result)
1238 (while crt
1239 (let ((crt-data (ewoc-data crt)))
1240 (if (vc-dir-fileinfo->marked crt-data)
1241 (if (vc-dir-fileinfo->directory crt-data)
1242 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1243 (dirlen (length dir))
1244 data)
1245 (while
1246 (and (setq crt (ewoc-next vc-ewoc crt))
1247 (string-equal
1248 (substring
1249 (progn
1250 (setq data (ewoc-data crt))
1251 (let ((crtdir (vc-dir-fileinfo->directory data)))
1252 (if crtdir
1253 crtdir
1254 (file-name-directory
1255 (expand-file-name
1256 (vc-dir-fileinfo->name data))))))
1257 0 dirlen)
1258 dir))
1259 (unless (vc-dir-fileinfo->directory data)
1260 (push (vc-dir-fileinfo->name data) result))))
1261 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1262 (setq crt (ewoc-next vc-ewoc crt)))
1263 (setq crt (ewoc-next vc-ewoc crt)))))
1264 result))
1265
1266 (defun vc-directory-resynch-file (&optional fname)
1267 "Update the entries for FILE in any directory buffers that list it."
1268 (let ((file (or fname (expand-file-name buffer-file-name))))
1269 ;; The vc-dir case
1270 (let ((found-vc-dir-buf nil))
1271 (save-excursion
1272 (dolist (status-buf (buffer-list))
1273 (set-buffer status-buf)
1274 ;; look for a vc-dir buffer that might show this file.
1275 (when (eq major-mode 'vc-dir-mode)
1276 (setq found-vc-dir-buf t)
1277 (let ((ddir (expand-file-name default-directory)))
1278 ;; This test is cvs-string-prefix-p
1279 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
1280 (let*
1281 ((file-short (substring file (length ddir)))
1282 (state
1283 (funcall (vc-client-object->file-to-state vc-client-mode)
1284 file))
1285 (extra
1286 (funcall (vc-client-object->file-to-extra vc-client-mode)
1287 file))
1288 (entry
1289 (list file-short state extra)))
1290 (vc-dir-update (list entry) status-buf))))))
1291 ;; We didn't find any vc-dir buffers, remove the hook, it is
1292 ;; not needed.
1293 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-directory-resynch-file))))))
1294
1295 (defun vc-dir-mode (client-object)
1296 "Major mode for dispatcher directory buffers.
1297 Marking/Unmarking key bindings and actions:
1298 m - marks a file/directory or if the region is active, mark all the files
1299 in region.
1300 Restrictions: - a file cannot be marked if any parent directory is marked
1301 - a directory cannot be marked if any child file or
1302 directory is marked
1303 u - marks a file/directory or if the region is active, unmark all the files
1304 in region.
1305 M - if the cursor is on a file: mark all the files with the same VC state as
1306 the current file
1307 - if the cursor is on a directory: mark all child files
1308 - with a prefix argument: mark all files
1309 U - if the cursor is on a file: unmark all the files with the same VC state
1310 as the current file
1311 - if the cursor is on a directory: unmark all child files
1312 - with a prefix argument: unmark all files
1313
1314
1315 \\{vc-dir-mode-map}"
1316 (setq mode-name (vc-client-object->name client-object))
1317 (setq major-mode 'vc-dir-mode)
1318 (setq buffer-read-only t)
1319 (use-local-map vc-dir-mode-map)
1320 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
1321 (set (make-local-variable 'vc-client-mode) client-object)
1322 (let ((buffer-read-only nil))
1323 (erase-buffer)
1324 (set (make-local-variable 'vc-dir-process-buffer) nil)
1325 (set (make-local-variable 'vc-ewoc)
1326 (ewoc-create (vc-client-object->file-to-info client-object)
1327 (vc-client-object->headers client-object)))
1328 (add-hook 'after-save-hook 'vc-directory-resynch-file)
1329 ;; Make sure that if the directory buffer is killed, the update
1330 ;; process running in the background is also killed.
1331 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1332 (funcall (vc-client-object->updater client-object)))
1333 (run-hooks 'vc-dir-mode-hook))
1334
1335 (put 'vc-dir-mode 'mode-class 'special)
1336
1337 (defun vc-dispatcher-browsing ()
1338 "Are we in a directory browser buffer?"
1339 (derived-mode-p 'vc-dir-mode))
1340
1341 (defun vc-dispatcher-in-fileset-p (fileset)
1342 (let ((member nil))
1343 (while (and (not member) fileset)
1344 (let ((elem (pop fileset)))
1345 (if (if (file-directory-p elem)
1346 (eq t (compare-strings buffer-file-name nil (length elem)
1347 elem nil nil))
1348 (eq (current-buffer) (get-file-buffer elem)))
1349 (setq member t))))
1350 member))
1351
1352 (defun vc-dispatcher-selection-set (&optional observer)
1353 "Deduce a set of files to which to apply an operation. Return a cons
1354 cell (SELECTION . FILESET), where SELECTION is what the user chose
1355 and FILES is the flist with any directories replaced by the listed files
1356 within them.
1357
1358 If we're in a directory display, the fileset is the list of marked files (if
1359 there is one) else the file on the curreent line. If not in a directory
1360 display, but the current buffer visits a file, the fileset is a singleton
1361 containing that file. Otherwise, throw an error."
1362 (let ((selection
1363 (cond
1364 ;; Browsing with vc-dir
1365 ((vc-dispatcher-browsing)
1366 ;; If no files are marked, temporatrily mark current file
1367 ;; and choose on that basis (so we get subordinate files)
1368 (if (not (vc-dir-marked-files))
1369 (prog2
1370 (vc-dir-mark-file)
1371 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
1372 (vc-dir-unmark-all-files t))
1373 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
1374 ;; Visiting an eligible file
1375 ((buffer-file-name)
1376 (cons (list buffer-file-name) (list buffer-file-name)))
1377 ;; No eligible file -- if there's a parent buffer, deduce from there
1378 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1379 (with-current-buffer vc-parent-buffer
1380 (vc-dispatcher-browsing))))
1381 (with-current-buffer vc-parent-buffer
1382 (vc-dispatcher-selection-set)))
1383 ;; No good set here, throw error
1384 (t (error "No fileset is available here")))))
1385 ;; We assume, in order to avoid unpleasant surprises to the user,
1386 ;; that a fileset is not in good shape to be handed to the user if the
1387 ;; buffers visiting the fileset don't match the on-disk contents.
1388 (if (not observer)
1389 (save-some-buffers
1390 nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
1391 selection))
1392
1393 (provide 'vc-dispatcher)
1394
1395 ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
1396 ;;; vc-dispatcher.el ends here