]> code.delx.au - gnu-emacs/blob - lisp/vc-dispatcher.el
(vc-delete-file): Don't try to resynch the buffer.
[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 'modeline-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)))
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-dispatcher*" ,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-update (entries buffer &optional noinsert)
849 "Update BUFFER's ewoc from the list of ENTRIES.
850 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
851 ;; Add ENTRIES to the vc-dir buffer BUFFER.
852 (with-current-buffer buffer
853 ;; Insert the entries sorted by name into the ewoc.
854 ;; We assume the ewoc is sorted too, which should be the
855 ;; case if we always add entries with vc-dir-update.
856 (setq entries
857 ;; Sort: first files and then subdirectories.
858 ;; XXX: this is VERY inefficient, it computes the directory
859 ;; names too many times
860 (sort entries
861 (lambda (entry1 entry2)
862 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
863 (dir2 (file-name-directory (expand-file-name (car entry2)))))
864 (cond
865 ((string< dir1 dir2) t)
866 ((not (string= dir1 dir2)) nil)
867 ((string< (car entry1) (car entry2))))))))
868 ;; Insert directory entries in the right places.
869 (let ((entry (car entries))
870 (node (ewoc-nth vc-ewoc 0)))
871 ;; Insert . if it is not present.
872 (unless node
873 (let ((rd (file-relative-name default-directory)))
874 (ewoc-enter-last
875 vc-ewoc (vc-dir-create-fileinfo
876 rd nil nil nil (expand-file-name default-directory))))
877 (setq node (ewoc-nth vc-ewoc 0)))
878
879 (while (and entry node)
880 (let* ((entryfile (car entry))
881 (entrydir (file-name-directory (expand-file-name entryfile)))
882 (nodedir
883 (or (vc-dir-fileinfo->directory (ewoc-data node))
884 (file-name-directory
885 (expand-file-name
886 (vc-dir-fileinfo->name (ewoc-data node)))))))
887 (cond
888 ;; First try to find the directory.
889 ((string-lessp nodedir entrydir)
890 (setq node (ewoc-next vc-ewoc node)))
891 ((string-equal nodedir entrydir)
892 ;; Found the directory, find the place for the file name.
893 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
894 (cond
895 ((string-lessp nodefile entryfile)
896 (setq node (ewoc-next vc-ewoc node)))
897 ((string-equal nodefile entryfile)
898 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
899 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
900 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
901 (ewoc-invalidate vc-ewoc node)
902 (setq entries (cdr entries) entry (car entries))
903 (setq node (ewoc-next vc-ewoc node)))
904 (t
905 (ewoc-enter-before vc-ewoc node
906 (apply 'vc-dir-create-fileinfo entry))
907 (setq entries (cdr entries) entry (car entries))))))
908 (t
909 ;; We need to insert a directory node
910 (let ((rd (file-relative-name entrydir)))
911 (ewoc-enter-last
912 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))
913 ;; Now insert the node itself.
914 (ewoc-enter-before vc-ewoc node
915 (apply 'vc-dir-create-fileinfo entry))
916 (setq entries (cdr entries) entry (car entries))))))
917 ;; We're past the last node, all remaining entries go to the end.
918 (unless (or node noinsert)
919 (let* ((lastnode (ewoc-nth vc-ewoc -1))
920 (lastdir
921 (or (vc-dir-fileinfo->directory (ewoc-data lastnode))
922 (file-name-directory
923 (expand-file-name
924 (vc-dir-fileinfo->name (ewoc-data lastnode)))))))
925 (dolist (entry entries)
926 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
927 ;; Insert a directory node if needed.
928 (unless (string-equal lastdir entrydir)
929 (setq lastdir entrydir)
930 (let ((rd (file-relative-name entrydir)))
931 (ewoc-enter-last
932 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
933 ;; Now insert the node itself.
934 (ewoc-enter-last vc-ewoc
935 (apply 'vc-dir-create-fileinfo entry)))))))))
936
937 (defun vc-dir-busy ()
938 (and (buffer-live-p vc-dir-process-buffer)
939 (get-buffer-process vc-dir-process-buffer)))
940
941 (defun vc-dir-kill-dir-status-process ()
942 "Kill the temporary buffer and associated process."
943 (interactive)
944 (when (buffer-live-p vc-dir-process-buffer)
945 (let ((proc (get-buffer-process vc-dir-process-buffer)))
946 (when proc (delete-process proc))
947 (setq vc-dir-process-buffer nil)
948 (setq mode-line-process nil))))
949
950 (defun vc-dir-kill-query ()
951 ;; Make sure that when the status buffer is killed the update
952 ;; process running in background is also killed.
953 (if (vc-dir-busy)
954 (when (y-or-n-p "Status update process running, really kill status buffer?")
955 (vc-dir-kill-dir-status-process)
956 t)
957 t))
958
959 (defun vc-dir-next-line (arg)
960 "Go to the next line.
961 If a prefix argument is given, move by that many lines."
962 (interactive "p")
963 (with-no-warnings
964 (ewoc-goto-next vc-ewoc arg)
965 (vc-dir-move-to-goal-column)))
966
967 (defun vc-dir-previous-line (arg)
968 "Go to the previous line.
969 If a prefix argument is given, move by that many lines."
970 (interactive "p")
971 (ewoc-goto-prev vc-ewoc arg)
972 (vc-dir-move-to-goal-column))
973
974 (defun vc-dir-next-directory ()
975 "Go to the next directory."
976 (interactive)
977 (let ((orig (point)))
978 (if
979 (catch 'foundit
980 (while t
981 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
982 (cond ((not next)
983 (throw 'foundit t))
984 (t
985 (progn
986 (ewoc-goto-node vc-ewoc next)
987 (vc-dir-move-to-goal-column)
988 (if (vc-dir-fileinfo->directory (ewoc-data next))
989 (throw 'foundit nil))))))))
990 (goto-char orig))))
991
992 (defun vc-dir-previous-directory ()
993 "Go to the previous directory."
994 (interactive)
995 (let ((orig (point)))
996 (if
997 (catch 'foundit
998 (while t
999 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
1000 (cond ((not prev)
1001 (throw 'foundit t))
1002 (t
1003 (progn
1004 (ewoc-goto-node vc-ewoc prev)
1005 (vc-dir-move-to-goal-column)
1006 (if (vc-dir-fileinfo->directory (ewoc-data prev))
1007 (throw 'foundit nil))))))))
1008 (goto-char orig))))
1009
1010 (defun vc-dir-mark-unmark (mark-unmark-function)
1011 (if (use-region-p)
1012 (let ((firstl (line-number-at-pos (region-beginning)))
1013 (lastl (line-number-at-pos (region-end))))
1014 (save-excursion
1015 (goto-char (region-beginning))
1016 (while (<= (line-number-at-pos) lastl)
1017 (funcall mark-unmark-function))))
1018 (funcall mark-unmark-function)))
1019
1020 (defun vc-dir-parent-marked-p (arg)
1021 ;; Return nil if none of the parent directories of arg is marked.
1022 (let* ((argdata (ewoc-data arg))
1023 (argdir
1024 (let ((crtdir (vc-dir-fileinfo->directory argdata)))
1025 (if crtdir
1026 crtdir
1027 (file-name-directory (expand-file-name
1028 (vc-dir-fileinfo->name argdata))))))
1029 (arglen (length argdir))
1030 (crt arg)
1031 data dir)
1032 ;; Go through the predecessors, checking if any directory that is
1033 ;; a parent is marked.
1034 (while (setq crt (ewoc-prev vc-ewoc crt))
1035 (setq data (ewoc-data crt))
1036 (setq dir
1037 (let ((crtdir (vc-dir-fileinfo->directory data)))
1038 (if crtdir
1039 crtdir
1040 (file-name-directory (expand-file-name
1041 (vc-dir-fileinfo->name data))))))
1042
1043 (when (and (vc-dir-fileinfo->directory data)
1044 (string-equal (substring argdir 0 (length dir)) dir))
1045 (when (vc-dir-fileinfo->marked data)
1046 (error "Cannot mark `%s', parent directory `%s' marked"
1047 (vc-dir-fileinfo->name argdata)
1048 (vc-dir-fileinfo->name data)))))
1049 nil))
1050
1051 (defun vc-dir-children-marked-p (arg)
1052 ;; Return nil if none of the children of arg is marked.
1053 (let* ((argdata (ewoc-data arg))
1054 (argdir (vc-dir-fileinfo->directory argdata))
1055 (arglen (length argdir))
1056 (is-child t)
1057 (crt arg)
1058 data dir)
1059 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1060 (setq data (ewoc-data crt))
1061 (setq dir
1062 (let ((crtdir (vc-dir-fileinfo->directory data)))
1063 (if crtdir
1064 crtdir
1065 (file-name-directory (expand-file-name
1066 (vc-dir-fileinfo->name data))))))
1067 (if (string-equal argdir (substring dir 0 arglen))
1068 (when (vc-dir-fileinfo->marked data)
1069 (error "Cannot mark `%s', child `%s' marked"
1070 (vc-dir-fileinfo->name argdata)
1071 (vc-dir-fileinfo->name data)))
1072 ;; We are done, we got to an entry that is not a child of `arg'.
1073 (setq is-child nil)))
1074 nil))
1075
1076 (defun vc-dir-mark-file (&optional arg)
1077 ;; Mark ARG or the current file and move to the next line.
1078 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1079 (file (ewoc-data crt))
1080 (isdir (vc-dir-fileinfo->directory file)))
1081 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1082 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1083 (setf (vc-dir-fileinfo->marked file) t)
1084 (ewoc-invalidate vc-ewoc crt)
1085 (unless (or arg (mouse-event-p last-command-event))
1086 (vc-dir-next-line 1)))))
1087
1088 (defun vc-dir-mark ()
1089 "Mark the current file or all files in the region.
1090 If the region is active, mark all the files in the region.
1091 Otherwise mark the file on the current line and move to the next
1092 line."
1093 (interactive)
1094 (vc-dir-mark-unmark 'vc-dir-mark-file))
1095
1096 (defun vc-dir-mark-all-files (arg)
1097 "Mark all files with the same state as the current one.
1098 With a prefix argument mark all files.
1099 If the current entry is a directory, mark all child files.
1100
1101 The commands operate on files that are on the same state.
1102 This command is intended to make it easy to select all files that
1103 share the same state."
1104 (interactive "P")
1105 (if arg
1106 ;; Mark all files.
1107 (progn
1108 ;; First check that no directory is marked, we can't mark
1109 ;; files in that case.
1110 (ewoc-map
1111 (lambda (filearg)
1112 (when (and (vc-dir-fileinfo->directory filearg)
1113 (vc-dir-fileinfo->directory filearg))
1114 (error "Cannot mark all files, directory `%s' marked"
1115 (vc-dir-fileinfo->name filearg))))
1116 vc-ewoc)
1117 (ewoc-map
1118 (lambda (filearg)
1119 (unless (vc-dir-fileinfo->marked filearg)
1120 (setf (vc-dir-fileinfo->marked filearg) t)
1121 t))
1122 vc-ewoc))
1123 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1124 (if (vc-dir-fileinfo->directory data)
1125 ;; It's a directory, mark child files.
1126 (let ((crt (ewoc-locate vc-ewoc)))
1127 (unless (vc-dir-children-marked-p crt)
1128 (while (setq crt (ewoc-next vc-ewoc crt))
1129 (let ((crt-data (ewoc-data crt)))
1130 (unless (vc-dir-fileinfo->directory crt-data)
1131 (setf (vc-dir-fileinfo->marked crt-data) t)
1132 (ewoc-invalidate vc-ewoc crt))))))
1133 ;; It's a file
1134 (let ((state (vc-dir-fileinfo->state data))
1135 (crt (ewoc-nth vc-ewoc 0)))
1136 (while crt
1137 (let ((crt-data (ewoc-data crt)))
1138 (when (and (not (vc-dir-fileinfo->marked crt-data))
1139 (eq (vc-dir-fileinfo->state crt-data) state)
1140 (not (vc-dir-fileinfo->directory crt-data)))
1141 (vc-dir-mark-file crt)))
1142 (setq crt (ewoc-next vc-ewoc crt))))))))
1143
1144 (defun vc-dir-unmark-file ()
1145 ;; Unmark the current file and move to the next line.
1146 (let* ((crt (ewoc-locate vc-ewoc))
1147 (file (ewoc-data crt)))
1148 (setf (vc-dir-fileinfo->marked file) nil)
1149 (ewoc-invalidate vc-ewoc crt)
1150 (unless (mouse-event-p last-command-event)
1151 (vc-dir-next-line 1))))
1152
1153 (defun vc-dir-unmark ()
1154 "Unmark the current file or all files in the region.
1155 If the region is active, unmark all the files in the region.
1156 Otherwise mark the file on the current line and move to the next
1157 line."
1158 (interactive)
1159 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1160
1161 (defun vc-dir-unmark-file-up ()
1162 "Move to the previous line and unmark the file."
1163 (interactive)
1164 ;; If we're on the first line, we won't move up, but we will still
1165 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1166 ;; does.
1167 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1168 (file (ewoc-data prev)))
1169 (setf (vc-dir-fileinfo->marked file) nil)
1170 (ewoc-invalidate vc-ewoc prev)
1171 (vc-dir-move-to-goal-column)))
1172
1173 (defun vc-dir-unmark-all-files (arg)
1174 "Unmark all files with the same state as the current one.
1175 With a prefix argument unmark all files.
1176 If the current entry is a directory, unmark all the child files.
1177
1178 The commands operate on files that are on the same state.
1179 This command is intended to make it easy to deselect all files
1180 that share the same state."
1181 (interactive "P")
1182 (if arg
1183 (ewoc-map
1184 (lambda (filearg)
1185 (when (vc-dir-fileinfo->marked filearg)
1186 (setf (vc-dir-fileinfo->marked filearg) nil)
1187 t))
1188 vc-ewoc)
1189 (let* ((crt (ewoc-locate vc-ewoc))
1190 (data (ewoc-data crt)))
1191 (if (vc-dir-fileinfo->directory data)
1192 ;; It's a directory, unmark child files.
1193 (while (setq crt (ewoc-next vc-ewoc crt))
1194 (let ((crt-data (ewoc-data crt)))
1195 (unless (vc-dir-fileinfo->directory crt-data)
1196 (setf (vc-dir-fileinfo->marked crt-data) nil)
1197 (ewoc-invalidate vc-ewoc crt))))
1198 ;; It's a file
1199 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1200 (ewoc-map
1201 (lambda (filearg)
1202 (when (and (vc-dir-fileinfo->marked filearg)
1203 (eq (vc-dir-fileinfo->state filearg) crt-state))
1204 (setf (vc-dir-fileinfo->marked filearg) nil)
1205 t))
1206 vc-ewoc))))))
1207
1208 (defun vc-dir-toggle-mark-file ()
1209 (let* ((crt (ewoc-locate vc-ewoc))
1210 (file (ewoc-data crt)))
1211 (if (vc-dir-fileinfo->marked file)
1212 (vc-dir-unmark-file)
1213 (vc-dir-mark-file))))
1214
1215 (defun vc-dir-toggle-mark (e)
1216 (interactive "e")
1217 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1218
1219 (defun vc-dir-delete-file ()
1220 "Delete the marked files, or the current file if no marks."
1221 (interactive)
1222 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1223 (list (vc-dir-current-file)))))
1224
1225 (defun vc-dir-find-file ()
1226 "Find the file on the current line."
1227 (interactive)
1228 (find-file (vc-dir-current-file)))
1229
1230 (defun vc-dir-find-file-other-window ()
1231 "Find the file on the current line, in another window."
1232 (interactive)
1233 (find-file-other-window (vc-dir-current-file)))
1234
1235 (defun vc-dir-current-file ()
1236 (let ((node (ewoc-locate vc-ewoc)))
1237 (unless node
1238 (error "No file available."))
1239 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1240
1241 (defun vc-dir-marked-files ()
1242 "Return the list of marked files."
1243 (mapcar
1244 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1245 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1246
1247 (defun vc-dir-marked-only-files ()
1248 "Return the list of marked files, For marked directories return child files."
1249 (let ((crt (ewoc-nth vc-ewoc 0))
1250 result)
1251 (while crt
1252 (let ((crt-data (ewoc-data crt)))
1253 (if (vc-dir-fileinfo->marked crt-data)
1254 (if (vc-dir-fileinfo->directory crt-data)
1255 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1256 (dirlen (length dir))
1257 data)
1258 (while
1259 (and (setq crt (ewoc-next vc-ewoc crt))
1260 (string-equal
1261 (substring
1262 (progn
1263 (setq data (ewoc-data crt))
1264 (let ((crtdir (vc-dir-fileinfo->directory data)))
1265 (if crtdir
1266 crtdir
1267 (file-name-directory
1268 (expand-file-name
1269 (vc-dir-fileinfo->name data))))))
1270 0 dirlen)
1271 dir))
1272 (unless (vc-dir-fileinfo->directory data)
1273 (push (vc-dir-fileinfo->name data) result))))
1274 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1275 (setq crt (ewoc-next vc-ewoc crt)))
1276 (setq crt (ewoc-next vc-ewoc crt)))))
1277 result))
1278
1279 (defun vc-directory-resynch-file (&optional fname)
1280 "Update the entries for FILE in any directory buffers that list it."
1281 (let ((file (or fname (expand-file-name buffer-file-name))))
1282 ;; The vc-dir case
1283 (let ((found-vc-dir-buf nil))
1284 (save-excursion
1285 (dolist (status-buf (buffer-list))
1286 (set-buffer status-buf)
1287 ;; look for a vc-dir buffer that might show this file.
1288 (when (eq major-mode 'vc-dir-mode)
1289 (setq found-vc-dir-buf t)
1290 (let ((ddir (expand-file-name default-directory)))
1291 ;; This test is cvs-string-prefix-p
1292 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
1293 (let*
1294 ((file-short (substring file (length ddir)))
1295 (state
1296 (funcall (vc-client-object->file-to-state vc-client-mode)
1297 file))
1298 (extra
1299 (funcall (vc-client-object->file-to-extra vc-client-mode)
1300 file))
1301 (entry
1302 (list file-short state extra)))
1303 (vc-dir-update (list entry) status-buf))))))
1304 ;; We didn't find any vc-dir buffers, remove the hook, it is
1305 ;; not needed.
1306 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-directory-resynch-file))))))
1307
1308 (defun vc-dir-mode (client-object)
1309 "Major mode for dispatcher directory buffers.
1310 Marking/Unmarking key bindings and actions:
1311 m - marks a file/directory or if the region is active, mark all the files
1312 in region.
1313 Restrictions: - a file cannot be marked if any parent directory is marked
1314 - a directory cannot be marked if any child file or
1315 directory is marked
1316 u - marks a file/directory or if the region is active, unmark all the files
1317 in region.
1318 M - if the cursor is on a file: mark all the files with the same state as
1319 the current file
1320 - if the cursor is on a directory: mark all child files
1321 - with a prefix argument: mark all files
1322 U - if the cursor is on a file: unmark all the files with the same state
1323 as the current file
1324 - if the cursor is on a directory: unmark all child files
1325 - with a prefix argument: unmark all files
1326
1327
1328 \\{vc-dir-mode-map}"
1329 (setq mode-name (vc-client-object->name client-object))
1330 (setq major-mode 'vc-dir-mode)
1331 (setq buffer-read-only t)
1332 (use-local-map vc-dir-mode-map)
1333 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
1334 (set (make-local-variable 'vc-client-mode) client-object)
1335 (let ((buffer-read-only nil))
1336 (erase-buffer)
1337 (set (make-local-variable 'vc-dir-process-buffer) nil)
1338 (set (make-local-variable 'vc-ewoc)
1339 (ewoc-create (vc-client-object->file-to-info client-object)
1340 (vc-client-object->headers client-object)))
1341 (add-hook 'after-save-hook 'vc-directory-resynch-file)
1342 ;; Make sure that if the directory buffer is killed, the update
1343 ;; process running in the background is also killed.
1344 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1345 (funcall (vc-client-object->updater client-object)))
1346 (run-hooks 'vc-dir-mode-hook))
1347
1348 (put 'vc-dir-mode 'mode-class 'special)
1349
1350 (defun vc-dispatcher-browsing ()
1351 "Are we in a directory browser buffer?"
1352 (derived-mode-p 'vc-dir-mode))
1353
1354 (defun vc-dispatcher-in-fileset-p (fileset)
1355 (let ((member nil))
1356 (while (and (not member) fileset)
1357 (let ((elem (pop fileset)))
1358 (if (if (file-directory-p elem)
1359 (eq t (compare-strings buffer-file-name nil (length elem)
1360 elem nil nil))
1361 (eq (current-buffer) (get-file-buffer elem)))
1362 (setq member t))))
1363 member))
1364
1365 (defun vc-dispatcher-selection-set (&optional observer)
1366 "Deduce a set of files to which to apply an operation. Return a cons
1367 cell (SELECTION . FILESET), where SELECTION is what the user chose
1368 and FILES is the flist with any directories replaced by the listed files
1369 within them.
1370
1371 If we're in a directory display, the fileset is the list of marked files (if
1372 there is one) else the file on the current line. If not in a directory
1373 display, but the current buffer visits a file, the fileset is a singleton
1374 containing that file. Otherwise, throw an error."
1375 (let ((selection
1376 (cond
1377 ;; Browsing with vc-dir
1378 ((vc-dispatcher-browsing)
1379 ;; If no files are marked, temporarily mark current file
1380 ;; and choose on that basis (so we get subordinate files)
1381 (if (not (vc-dir-marked-files))
1382 (prog2
1383 (vc-dir-mark-file)
1384 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
1385 (vc-dir-unmark-all-files t))
1386 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
1387 ;; Visiting an eligible file
1388 ((buffer-file-name)
1389 (cons (list buffer-file-name) (list buffer-file-name)))
1390 ;; No eligible file -- if there's a parent buffer, deduce from there
1391 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1392 (with-current-buffer vc-parent-buffer
1393 (vc-dispatcher-browsing))))
1394 (with-current-buffer vc-parent-buffer
1395 (vc-dispatcher-selection-set)))
1396 ;; No good set here, throw error
1397 (t (error "No fileset is available here")))))
1398 ;; We assume, in order to avoid unpleasant surprises to the user,
1399 ;; that a fileset is not in good shape to be handed to the user if the
1400 ;; buffers visiting the fileset don't match the on-disk contents.
1401 (if (not observer)
1402 (save-some-buffers
1403 nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
1404 selection))
1405
1406 (provide 'vc-dispatcher)
1407
1408 ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
1409 ;;; vc-dispatcher.el ends here