* src/fileio.c (Fwrite_region): Delay the defaulting to beg&z to after
[emacs.git] / lisp / vc-dispatcher.el
blobf62b3b0744fcb58ca627eee067df24909555b5a0
1 ;;; vc-dispatcher.el -- generic command-dispatcher facility.
3 ;; Copyright (C) 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: FSF (see below for full credits)
7 ;; Maintainer: Eric S. Raymond <esr@thyrsus.com>
8 ;; Keywords: tools
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Credits:
27 ;; Designed and implemented by Eric S. Raymond, originally as part of VC mode.
28 ;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the
29 ;; vc-dir front end.
31 ;;; Commentary:
33 ;; Goals:
35 ;; There is a class of front-ending problems that Emacs might be used
36 ;; to address that involves selecting sets of files, or possibly
37 ;; directories, and passing the selection set to slave commands. The
38 ;; prototypical example, from which this code is derived, is talking
39 ;; to version-control systems.
41 ;; vc-dispatcher.el is written to decouple the UI issues in such front
42 ;; ends from their application-specific logic. It also provides a
43 ;; service layer for running the slave commands either synchronously
44 ;; or asynchronously and managing the message/error logs from the
45 ;; command runs.
47 ;; Similar UI problems can be expected to come up in applications
48 ;; areas other than VCSes; IDEs and document search are two obvious ones.
49 ;; This mode is intended to ensure that the Emacs interfaces for all such
50 ;; beasts are consistent and carefully designed. But even if nothing
51 ;; but VC ever uses it, getting the layer separation right will be
52 ;; a valuable thing.
54 ;; Dispatcher's universe:
56 ;; The universe consists of the file tree rooted at the current
57 ;; directory. The dispatcher's upper layer deduces some subset
58 ;; of the file tree from the state of the currently visited buffer
59 ;; and returns that subset, presumably to a client mode.
61 ;; The user may be looking at either of two different views; a buffer
62 ;; visiting a file, or a directory buffer generated by vc-dispatcher.
64 ;; The lower layer of this mode runs commands in subprocesses, either
65 ;; synchronously or asynchronously. Commands may be launched in one
66 ;; of two ways: they may be run immediately, or the calling mode can
67 ;; create a closure associated with a text-entry buffer, to be
68 ;; executed when the user types C-c to ship the buffer contents. In
69 ;; either case the command messages and error (if any) will remain
70 ;; available in a status buffer.
72 ;; Special behavior of dispatcher directory buffers:
74 ;; In dispatcher directory buffers, facilities to perform basic
75 ;; navigation and selection operations are provided by keymap and menu
76 ;; entries that dispatcher sets up itself, so they'll be uniform
77 ;; across all dispatcher-using client modes. Client modes are
78 ;; expected to append to these to provide mode-specific bindings.
80 ;; The standard map associates a 'state' slot (that the client mode
81 ;; may set) with each directory entry. The dispatcher knows nothing
82 ;; about the semantics of individual states, but mark and unmark commands
83 ;; treat all entries with the same state as the currently selected one as
84 ;; a unit.
86 ;; The interface:
88 ;; The main interface to the lower level is vc-do-command. This launches a
89 ;; 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).
95 ;; The interface to the upper level has the two main entry points (vc-dir)
96 ;; and (vc-dispatcher-selection-set) and a couple of convenience functions.
97 ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set)
98 ;; returns a selection set of files, either the marked files in a browsing
99 ;; buffer or the singleton set consisting of the file visited by the current
100 ;; buffer (when that is appropriate). It also does what is needed to ensure
101 ;; that on-disk files and the contents of their visiting Emacs buffers
102 ;; coincide.
104 ;; When the client mode adds a local mode-line-hook to a buffer, it
105 ;; will be called with the buffer file name as argument whenever the
106 ;; dispatcher resynchs the buffer.
108 ;; To do:
110 ;; - vc-dir-kill-dir-status-process should not be specific to dir-status,
111 ;; it should work for other async commands done through vc-do-command
112 ;; as well,
114 ;; - log buffers need font-locking.
116 ;; - vc-dir needs mouse bindings.
118 ;; - vc-dir toolbar needs more icons.
120 ;; - vc-dir-menu-map-filter hook call needs to be moved to vc.el.
123 (require 'ewoc)
125 (eval-when-compile
126 (require 'cl))
128 ;; General customization
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)
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)
144 (defcustom vc-command-messages nil
145 "If non-nil, display run messages from back-end commands."
146 :type 'boolean
147 :group 'vc)
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)
154 ;; Variables the user doesn't need to know about.
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)
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)
170 ;; Common command execution logic
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))))))))
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))))
200 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
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))))))))))
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"))))
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)
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.")
279 (defvar w32-quote-process-args)
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 " ")))
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)
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))))
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).
386 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
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)))))
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))))))))
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)))
426 (string= context-string (buffer-substring posn end)))))
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)))
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))))))
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)))
472 (defun vc-resynch-window (file &optional keep noquery)
473 "If FILE is in the current buffer, either revert or unvisit it.
474 The choice between revert (to see expanded keywords) and unvisit
475 depends on KEEP. NOQUERY if non-nil inhibits confirmation for
476 reverting. NOQUERY should be t *only* if it is known the only
477 difference between the buffer and the file is due to
478 modifications by the dispatcher client code, rather than user
479 editing!"
480 (and (string= buffer-file-name file)
481 (if keep
482 (progn
483 (vc-revert-buffer-internal t noquery)
484 ;; TODO: Adjusting view mode might no longer be necessary
485 ;; after RMS change to files.el of 1999-08-08. Investigate
486 ;; this when we install the new VC.
487 (and view-read-only
488 (if (file-writable-p file)
489 (and view-mode
490 (let ((view-old-buffer-read-only nil))
491 (view-mode-exit)))
492 (and (not view-mode)
493 (not (eq (get major-mode 'mode-class) 'special))
494 (view-mode-enter))))
495 (run-hook-with-args 'mode-line-hook buffer-file-name))
496 (kill-buffer (current-buffer)))))
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 ;; Try to avoid unnecessary work, a *vc-dir* buffer is only present
507 ;; if this is true.
508 (when (memq 'vc-dir-resynch-file after-save-hook)
509 (vc-dir-resynch-file file)))
511 (defun vc-buffer-sync (&optional not-urgent)
512 "Make sure the current buffer and its working file are in sync.
513 NOT-URGENT means it is ok to continue if the user says not to save."
514 (when (buffer-modified-p)
515 (if (or vc-suppress-confirm
516 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
517 (save-buffer)
518 (unless not-urgent
519 (error "Aborted")))))
521 ;; Command closures
523 ;; Set up key bindings for use while editing log messages
525 (defun vc-log-edit (fileset)
526 "Set up `log-edit' for use on FILE."
527 (setq default-directory
528 (with-current-buffer vc-parent-buffer default-directory))
529 (log-edit 'vc-finish-logentry
531 `((log-edit-listfun . (lambda () ',fileset))
532 (log-edit-diff-function . (lambda () (vc-diff nil)))))
533 (set (make-local-variable 'vc-log-fileset) fileset)
534 (make-local-variable 'vc-log-extra)
535 (set-buffer-modified-p nil)
536 (setq buffer-file-name nil))
538 (defun vc-start-logentry (files extra comment initial-contents msg logbuf action &optional after-hook)
539 "Accept a comment for an operation on FILES with extra data EXTRA.
540 If COMMENT is nil, pop up a LOGBUF buffer, emit MSG, and set the
541 action on close to ACTION. If COMMENT is a string and
542 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
543 contents of the log entry buffer. If COMMENT is a string and
544 INITIAL-CONTENTS is nil, do action immediately as if the user had
545 entered COMMENT. If COMMENT is t, also do action immediately with an
546 empty comment. Remember the file's buffer in `vc-parent-buffer'
547 \(current one if no file). AFTER-HOOK specifies the local value
548 for `vc-log-after-operation-hook'."
549 (let ((parent
550 (if (vc-dispatcher-browsing)
551 ;; If we are called from a directory browser, the parent buffer is
552 ;; the current buffer.
553 (current-buffer)
554 (if (and files (equal (length files) 1))
555 (get-file-buffer (car files))
556 (current-buffer)))))
557 (if (and comment (not initial-contents))
558 (set-buffer (get-buffer-create logbuf))
559 (pop-to-buffer (get-buffer-create logbuf)))
560 (set (make-local-variable 'vc-parent-buffer) parent)
561 (set (make-local-variable 'vc-parent-buffer-name)
562 (concat " from " (buffer-name vc-parent-buffer)))
563 (vc-log-edit files)
564 (make-local-variable 'vc-log-after-operation-hook)
565 (when after-hook
566 (setq vc-log-after-operation-hook after-hook))
567 (setq vc-log-operation action)
568 (setq vc-log-extra extra)
569 (when comment
570 (erase-buffer)
571 (when (stringp comment) (insert comment)))
572 (if (or (not comment) initial-contents)
573 (message "%s Type C-c C-c when done" msg)
574 (vc-finish-logentry (eq comment t)))))
576 (defun vc-finish-logentry (&optional nocomment)
577 "Complete the operation implied by the current log entry.
578 Use the contents of the current buffer as a check-in or registration
579 comment. If the optional arg NOCOMMENT is non-nil, then don't check
580 the buffer contents as a comment."
581 (interactive)
582 ;; Check and record the comment, if any.
583 (unless nocomment
584 (run-hooks 'vc-logentry-check-hook))
585 ;; Sync parent buffer in case the user modified it while editing the comment.
586 ;; But not if it is a vc-dir buffer.
587 (with-current-buffer vc-parent-buffer
588 (or (vc-dispatcher-browsing) (vc-buffer-sync)))
589 (unless vc-log-operation
590 (error "No log operation is pending"))
591 ;; save the parameters held in buffer-local variables
592 (let ((logbuf (current-buffer))
593 (log-operation vc-log-operation)
594 (log-fileset vc-log-fileset)
595 (log-extra vc-log-extra)
596 (log-entry (buffer-string))
597 (after-hook vc-log-after-operation-hook)
598 (tmp-vc-parent-buffer vc-parent-buffer))
599 (pop-to-buffer vc-parent-buffer)
600 ;; OK, do it to it
601 (save-excursion
602 (funcall log-operation
603 log-fileset
604 log-extra
605 log-entry))
606 ;; Remove checkin window (after the checkin so that if that fails
607 ;; we don't zap the log buffer and the typing therein).
608 ;; -- IMO this should be replaced with quit-window
609 (cond ((and logbuf vc-delete-logbuf-window)
610 (delete-windows-on logbuf (selected-frame))
611 ;; Kill buffer and delete any other dedicated windows/frames.
612 (kill-buffer logbuf))
613 (logbuf (pop-to-buffer logbuf)
614 (bury-buffer)
615 (pop-to-buffer tmp-vc-parent-buffer)))
616 ;; Now make sure we see the expanded headers
617 (when log-fileset
618 (mapc
619 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
620 log-fileset))
621 (when (vc-dispatcher-browsing)
622 (vc-dir-move-to-goal-column))
623 (run-hooks after-hook 'vc-finish-logentry-hook)))
625 ;; The ewoc-based vc-directory implementation
627 (defcustom vc-dir-mode-hook nil
628 "Normal hook run by `vc-dir-mode'.
629 See `run-hooks'."
630 :type 'hook
631 :group 'vc)
633 ;; Used to store information for the files displayed in the directory buffer.
634 ;; Each item displayed corresponds to one of these defstructs.
635 (defstruct (vc-dir-fileinfo
636 (:copier nil)
637 (:type list) ;So we can use `member' on lists of FIs.
638 (:constructor
639 ;; We could define it as an alias for `list'.
640 vc-dir-create-fileinfo (name state &optional extra marked directory))
641 (:conc-name vc-dir-fileinfo->))
642 name ;Keep it as first, for `member'.
643 state
644 ;; For storing client-mode specific information.
645 extra
646 marked
647 ;; To keep track of not updated files during a global refresh
648 needs-update
649 ;; To distinguish files and directories.
650 directory)
652 ;; Used to describe a dispatcher client mode.
653 (defstruct (vc-client-object
654 (:copier nil)
655 (:constructor
656 vc-create-client-object (name
657 headers
658 file-to-info
659 file-to-state
660 file-to-extra
661 updater
662 extra-menu))
663 (:conc-name vc-client-object->))
664 name
665 headers
666 file-to-info
667 file-to-state
668 file-to-extra
669 updater
670 extra-menu)
672 (defvar vc-ewoc nil)
673 (defvar vc-dir-process-buffer nil
674 "The buffer used for the asynchronous call that computes status.")
676 (defun vc-dir-move-to-goal-column ()
677 ;; Used to keep the cursor on the file name column.
678 (beginning-of-line)
679 ;; Must be in sync with vc-default-status-printer.
680 (forward-char 25))
682 (defun vc-dir-prepare-status-buffer (bname dir &optional create-new)
683 "Find a buffer named BNAME showing DIR, or create a new one."
684 (setq dir (expand-file-name dir))
685 (let*
686 ;; Look for another buffer name BNAME visiting the same directory.
687 ((buf (save-excursion
688 (unless create-new
689 (dolist (buffer (buffer-list))
690 (set-buffer buffer)
691 (when (and (vc-dispatcher-browsing)
692 (string= (expand-file-name default-directory) dir))
693 (return buffer)))))))
694 (or buf
695 ;; Create a new buffer named BNAME.
696 (with-current-buffer (create-file-buffer bname)
697 (cd dir)
698 (vc-setup-buffer (current-buffer))
699 ;; Reset the vc-parent-buffer-name so that it does not appear
700 ;; in the mode-line.
701 (setq vc-parent-buffer-name nil)
702 (current-buffer)))))
704 (defvar vc-dir-menu-map
705 (let ((map (make-sparse-keymap "VC-dir")))
706 (define-key map [quit]
707 '(menu-item "Quit" quit-window
708 :help "Quit"))
709 (define-key map [kill]
710 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
711 :enable (vc-dir-busy)
712 :help "Kill the command that updates the directory buffer"))
713 (define-key map [refresh]
714 '(menu-item "Refresh" vc-dir-refresh
715 :enable (not (vc-dir-busy))
716 :help "Refresh the contents of the directory buffer"))
717 ;; Movement.
718 (define-key map [sepmv] '("--"))
719 (define-key map [next-line]
720 '(menu-item "Next line" vc-dir-next-line
721 :help "Go to the next line" :keys "n"))
722 (define-key map [previous-line]
723 '(menu-item "Previous line" vc-dir-previous-line
724 :help "Go to the previous line"))
725 ;; Marking.
726 (define-key map [sepmrk] '("--"))
727 (define-key map [unmark-all]
728 '(menu-item "Unmark All" vc-dir-unmark-all-files
729 :help "Unmark all files that are in the same state as the current file\
730 \nWith prefix argument unmark all files"))
731 (define-key map [unmark-previous]
732 '(menu-item "Unmark previous " vc-dir-unmark-file-up
733 :help "Move to the previous line and unmark the file"))
735 (define-key map [mark-all]
736 '(menu-item "Mark All" vc-dir-mark-all-files
737 :help "Mark all files that are in the same state as the current file\
738 \nWith prefix argument mark all files"))
739 (define-key map [unmark]
740 '(menu-item "Unmark" vc-dir-unmark
741 :help "Unmark the current file or all files in the region"))
743 (define-key map [mark]
744 '(menu-item "Mark" vc-dir-mark
745 :help "Mark the current file or all files in the region"))
747 (define-key map [sepopn] '("--"))
748 (define-key map [open-other]
749 '(menu-item "Open in other window" vc-dir-find-file-other-window
750 :help "Find the file on the current line, in another window"))
751 (define-key map [open]
752 '(menu-item "Open file" vc-dir-find-file
753 :help "Find the file on the current line"))
754 map)
755 "Menu for dispatcher status")
757 ;; This is used so that client modes can add mode-specific menu
758 ;; items to vc-dir-menu-map.
759 (defun vc-dir-menu-map-filter (orig-binding)
760 (when (and (symbolp orig-binding) (fboundp orig-binding))
761 (setq orig-binding (indirect-function orig-binding)))
762 (let ((ext-binding
763 (funcall (vc-client-object->extra-menu vc-client-mode))))
764 (if (null ext-binding)
765 orig-binding
766 (append orig-binding
767 '("----")
768 ext-binding))))
770 (defvar vc-dir-mode-map
771 (let ((map (make-keymap)))
772 (suppress-keymap map)
773 ;; Marking.
774 (define-key map "m" 'vc-dir-mark)
775 (define-key map "M" 'vc-dir-mark-all-files)
776 (define-key map "u" 'vc-dir-unmark)
777 (define-key map "U" 'vc-dir-unmark-all-files)
778 (define-key map "\C-?" 'vc-dir-unmark-file-up)
779 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
780 ;; Movement.
781 (define-key map "n" 'vc-dir-next-line)
782 (define-key map " " 'vc-dir-next-line)
783 (define-key map "\t" 'vc-dir-next-directory)
784 (define-key map "p" 'vc-dir-previous-line)
785 (define-key map [backtab] 'vc-dir-previous-directory)
786 ;;; Rebind paragraph-movement commands.
787 (define-key map "\M-}" 'vc-dir-next-directory)
788 (define-key map "\M-{" 'vc-dir-previous-directory)
789 (define-key map [C-down] 'vc-dir-next-directory)
790 (define-key map [C-up] 'vc-dir-previous-directory)
791 ;; The remainder.
792 (define-key map "f" 'vc-dir-find-file)
793 (define-key map "\C-m" 'vc-dir-find-file)
794 (define-key map "o" 'vc-dir-find-file-other-window)
795 (define-key map "q" 'quit-window)
796 (define-key map "g" 'vc-dir-refresh)
797 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
798 (define-key map [down-mouse-3] 'vc-dir-menu)
799 (define-key map [mouse-2] 'vc-dir-toggle-mark)
801 ;; Hook up the menu.
802 (define-key map [menu-bar vc-dir-mode]
803 `(menu-item
804 ;; This is used so that client modes can add mode-specific
805 ;; menu items to vc-dir-menu-map.
806 "VC-dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
807 map)
808 "Keymap for directory buffer.")
810 (defmacro vc-at-event (event &rest body)
811 "Evaluate `body' with point located at event-start of `event'.
812 If `body' uses `event', it should be a variable,
813 otherwise it will be evaluated twice."
814 (let ((posn (make-symbol "vc-at-event-posn")))
815 `(let ((,posn (event-start ,event)))
816 (save-excursion
817 (set-buffer (window-buffer (posn-window ,posn)))
818 (goto-char (posn-point ,posn))
819 ,@body))))
821 (defun vc-dir-menu (e)
822 "Popup the dispatcher status menu."
823 (interactive "e")
824 (vc-at-event e (popup-menu vc-dir-menu-map e)))
826 (defvar vc-dir-tool-bar-map
827 (let ((map (make-sparse-keymap)))
828 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
829 map vc-dir-mode-map)
830 (tool-bar-local-item "bookmark_add"
831 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
832 :help "Toggle mark on current item")
833 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
834 map vc-dir-mode-map
835 :rtl "right-arrow")
836 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
837 map vc-dir-mode-map
838 :rtl "left-arrow")
839 (tool-bar-local-item-from-menu 'vc-print-log "info"
840 map vc-dir-mode-map)
841 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
842 map vc-dir-mode-map)
843 (tool-bar-local-item-from-menu 'nonincremental-search-forward
844 "search" map)
845 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
846 map vc-dir-mode-map)
847 (tool-bar-local-item-from-menu 'quit-window "exit"
848 map vc-dir-mode-map)
849 map))
851 (defun vc-dir-node-directory (node)
852 ;; Compute the directory for NODE.
853 ;; If it's a directory node, get it from the the node.
854 (let ((data (ewoc-data node)))
855 (or (vc-dir-fileinfo->directory data)
856 ;; Otherwise compute it from the file name.
857 (file-name-directory
858 (expand-file-name
859 (vc-dir-fileinfo->name data))))))
861 (defun vc-dir-update (entries buffer &optional noinsert)
862 "Update BUFFER's ewoc from the list of ENTRIES.
863 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
864 ;; Add ENTRIES to the vc-dir buffer BUFFER.
865 (with-current-buffer buffer
866 ;; Insert the entries sorted by name into the ewoc.
867 ;; We assume the ewoc is sorted too, which should be the
868 ;; case if we always add entries with vc-dir-update.
869 (setq entries
870 ;; Sort: first files and then subdirectories.
871 ;; XXX: this is VERY inefficient, it computes the directory
872 ;; names too many times
873 (sort entries
874 (lambda (entry1 entry2)
875 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
876 (dir2 (file-name-directory (expand-file-name (car entry2)))))
877 (cond
878 ((string< dir1 dir2) t)
879 ((not (string= dir1 dir2)) nil)
880 ((string< (car entry1) (car entry2))))))))
881 ;; Insert directory entries in the right places.
882 (let ((entry (car entries))
883 (node (ewoc-nth vc-ewoc 0)))
884 ;; Insert . if it is not present.
885 (unless node
886 (let ((rd (file-relative-name default-directory)))
887 (ewoc-enter-last
888 vc-ewoc (vc-dir-create-fileinfo
889 rd nil nil nil (expand-file-name default-directory))))
890 (setq node (ewoc-nth vc-ewoc 0)))
892 (while (and entry node)
893 (let* ((entryfile (car entry))
894 (entrydir (file-name-directory (expand-file-name entryfile)))
895 (nodedir (vc-dir-node-directory node)))
896 (cond
897 ;; First try to find the directory.
898 ((string-lessp nodedir entrydir)
899 (setq node (ewoc-next vc-ewoc node)))
900 ((string-equal nodedir entrydir)
901 ;; Found the directory, find the place for the file name.
902 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
903 (cond
904 ((string-lessp nodefile entryfile)
905 (setq node (ewoc-next vc-ewoc node)))
906 ((string-equal nodefile entryfile)
907 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
908 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
909 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
910 (ewoc-invalidate vc-ewoc node)
911 (setq entries (cdr entries))
912 (setq entry (car entries))
913 (setq node (ewoc-next vc-ewoc node)))
915 (ewoc-enter-before vc-ewoc node
916 (apply 'vc-dir-create-fileinfo entry))
917 (setq entries (cdr entries))
918 (setq entry (car entries))))))
920 ;; We might need to insert a directory node if the
921 ;; previous node was in a different directory.
922 (let* ((rd (file-relative-name entrydir))
923 (prev-node (ewoc-prev vc-ewoc node))
924 (prev-dir (vc-dir-node-directory prev-node)))
925 (unless (string-equal entrydir prev-dir)
926 (ewoc-enter-before
927 vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
928 ;; Now insert the node itself.
929 (ewoc-enter-before vc-ewoc node
930 (apply 'vc-dir-create-fileinfo entry))
931 (setq entries (cdr entries) entry (car entries))))))
932 ;; We're past the last node, all remaining entries go to the end.
933 (unless (or node noinsert)
934 (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
935 (dolist (entry entries)
936 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
937 ;; Insert a directory node if needed.
938 (unless (string-equal lastdir entrydir)
939 (setq lastdir entrydir)
940 (let ((rd (file-relative-name entrydir)))
941 (ewoc-enter-last
942 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
943 ;; Now insert the node itself.
944 (ewoc-enter-last vc-ewoc
945 (apply 'vc-dir-create-fileinfo entry)))))))))
947 (defun vc-dir-busy ()
948 (and (buffer-live-p vc-dir-process-buffer)
949 (get-buffer-process vc-dir-process-buffer)))
951 (defun vc-dir-kill-dir-status-process ()
952 "Kill the temporary buffer and associated process."
953 (interactive)
954 (when (buffer-live-p vc-dir-process-buffer)
955 (let ((proc (get-buffer-process vc-dir-process-buffer)))
956 (when proc (delete-process proc))
957 (setq vc-dir-process-buffer nil)
958 (setq mode-line-process nil))))
960 (defun vc-dir-kill-query ()
961 ;; Make sure that when the status buffer is killed the update
962 ;; process running in background is also killed.
963 (if (vc-dir-busy)
964 (when (y-or-n-p "Status update process running, really kill status buffer?")
965 (vc-dir-kill-dir-status-process)
969 (defun vc-dir-next-line (arg)
970 "Go to the next line.
971 If a prefix argument is given, move by that many lines."
972 (interactive "p")
973 (with-no-warnings
974 (ewoc-goto-next vc-ewoc arg)
975 (vc-dir-move-to-goal-column)))
977 (defun vc-dir-previous-line (arg)
978 "Go to the previous line.
979 If a prefix argument is given, move by that many lines."
980 (interactive "p")
981 (ewoc-goto-prev vc-ewoc arg)
982 (vc-dir-move-to-goal-column))
984 (defun vc-dir-next-directory ()
985 "Go to the next directory."
986 (interactive)
987 (let ((orig (point)))
989 (catch 'foundit
990 (while t
991 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
992 (cond ((not next)
993 (throw 'foundit t))
995 (progn
996 (ewoc-goto-node vc-ewoc next)
997 (vc-dir-move-to-goal-column)
998 (if (vc-dir-fileinfo->directory (ewoc-data next))
999 (throw 'foundit nil))))))))
1000 (goto-char orig))))
1002 (defun vc-dir-previous-directory ()
1003 "Go to the previous directory."
1004 (interactive)
1005 (let ((orig (point)))
1007 (catch 'foundit
1008 (while t
1009 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
1010 (cond ((not prev)
1011 (throw 'foundit t))
1013 (progn
1014 (ewoc-goto-node vc-ewoc prev)
1015 (vc-dir-move-to-goal-column)
1016 (if (vc-dir-fileinfo->directory (ewoc-data prev))
1017 (throw 'foundit nil))))))))
1018 (goto-char orig))))
1020 (defun vc-dir-mark-unmark (mark-unmark-function)
1021 (if (use-region-p)
1022 (let ((firstl (line-number-at-pos (region-beginning)))
1023 (lastl (line-number-at-pos (region-end))))
1024 (save-excursion
1025 (goto-char (region-beginning))
1026 (while (<= (line-number-at-pos) lastl)
1027 (funcall mark-unmark-function))))
1028 (funcall mark-unmark-function)))
1030 (defun vc-string-prefix-p (prefix string)
1031 (let ((lpref (length prefix)))
1032 (and (>= (length string) lpref)
1033 (eq t (compare-strings prefix nil nil string nil lpref)))))
1035 (defun vc-dir-parent-marked-p (arg)
1036 ;; Return nil if none of the parent directories of arg is marked.
1037 (let* ((argdir (vc-dir-node-directory arg))
1038 (arglen (length argdir))
1039 (crt arg)
1040 data dir)
1041 ;; Go through the predecessors, checking if any directory that is
1042 ;; a parent is marked.
1043 (while (setq crt (ewoc-prev vc-ewoc crt))
1044 (setq data (ewoc-data crt))
1045 (setq dir (vc-dir-node-directory crt))
1046 (when (and (vc-dir-fileinfo->directory data)
1047 (vc-string-prefix-p dir argdir))
1048 (when (vc-dir-fileinfo->marked data)
1049 (error "Cannot mark `%s', parent directory `%s' marked"
1050 (vc-dir-fileinfo->name (ewoc-data arg))
1051 (vc-dir-fileinfo->name data)))))
1052 nil))
1054 (defun vc-dir-children-marked-p (arg)
1055 ;; Return nil if none of the children of arg is marked.
1056 (let* ((argdir (vc-dir-node-directory arg))
1057 (arglen (length argdir))
1058 (is-child t)
1059 (crt arg)
1060 data dir)
1061 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1062 (setq data (ewoc-data crt))
1063 (setq dir (vc-dir-node-directory crt))
1064 (if (string-equal argdir (substring dir 0 arglen))
1065 (when (vc-dir-fileinfo->marked data)
1066 (error "Cannot mark `%s', child `%s' marked"
1067 (vc-dir-fileinfo->name (ewoc-data arg))
1068 (vc-dir-fileinfo->name data)))
1069 ;; We are done, we got to an entry that is not a child of `arg'.
1070 (setq is-child nil)))
1071 nil))
1073 (defun vc-dir-mark-file (&optional arg)
1074 ;; Mark ARG or the current file and move to the next line.
1075 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1076 (file (ewoc-data crt))
1077 (isdir (vc-dir-fileinfo->directory file)))
1078 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1079 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1080 (setf (vc-dir-fileinfo->marked file) t)
1081 (ewoc-invalidate vc-ewoc crt)
1082 (unless (or arg (mouse-event-p last-command-event))
1083 (vc-dir-next-line 1)))))
1085 (defun vc-dir-mark ()
1086 "Mark the current file or all files in the region.
1087 If the region is active, mark all the files in the region.
1088 Otherwise mark the file on the current line and move to the next
1089 line."
1090 (interactive)
1091 (vc-dir-mark-unmark 'vc-dir-mark-file))
1093 (defun vc-dir-mark-all-files (arg)
1094 "Mark all files with the same state as the current one.
1095 With a prefix argument mark all files.
1096 If the current entry is a directory, mark all child files.
1098 The commands operate on files that are on the same state.
1099 This command is intended to make it easy to select all files that
1100 share the same state."
1101 (interactive "P")
1102 (if arg
1103 ;; Mark all files.
1104 (progn
1105 ;; First check that no directory is marked, we can't mark
1106 ;; files in that case.
1107 (ewoc-map
1108 (lambda (filearg)
1109 (when (and (vc-dir-fileinfo->directory filearg)
1110 (vc-dir-fileinfo->marked filearg))
1111 (error "Cannot mark all files, directory `%s' marked"
1112 (vc-dir-fileinfo->name filearg))))
1113 vc-ewoc)
1114 (ewoc-map
1115 (lambda (filearg)
1116 (unless (vc-dir-fileinfo->marked filearg)
1117 (setf (vc-dir-fileinfo->marked filearg) t)
1119 vc-ewoc))
1120 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1121 (if (vc-dir-fileinfo->directory data)
1122 ;; It's a directory, mark child files.
1123 (let ((crt (ewoc-locate vc-ewoc)))
1124 (unless (vc-dir-children-marked-p crt)
1125 (while (setq crt (ewoc-next vc-ewoc crt))
1126 (let ((crt-data (ewoc-data crt)))
1127 (unless (vc-dir-fileinfo->directory crt-data)
1128 (setf (vc-dir-fileinfo->marked crt-data) t)
1129 (ewoc-invalidate vc-ewoc crt))))))
1130 ;; It's a file
1131 (let ((state (vc-dir-fileinfo->state data))
1132 (crt (ewoc-nth vc-ewoc 0)))
1133 (while crt
1134 (let ((crt-data (ewoc-data crt)))
1135 (when (and (not (vc-dir-fileinfo->marked crt-data))
1136 (eq (vc-dir-fileinfo->state crt-data) state)
1137 (not (vc-dir-fileinfo->directory crt-data)))
1138 (vc-dir-mark-file crt)))
1139 (setq crt (ewoc-next vc-ewoc crt))))))))
1141 (defun vc-dir-unmark-file ()
1142 ;; Unmark the current file and move to the next line.
1143 (let* ((crt (ewoc-locate vc-ewoc))
1144 (file (ewoc-data crt)))
1145 (setf (vc-dir-fileinfo->marked file) nil)
1146 (ewoc-invalidate vc-ewoc crt)
1147 (unless (mouse-event-p last-command-event)
1148 (vc-dir-next-line 1))))
1150 (defun vc-dir-unmark ()
1151 "Unmark the current file or all files in the region.
1152 If the region is active, unmark all the files in the region.
1153 Otherwise mark the file on the current line and move to the next
1154 line."
1155 (interactive)
1156 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1158 (defun vc-dir-unmark-file-up ()
1159 "Move to the previous line and unmark the file."
1160 (interactive)
1161 ;; If we're on the first line, we won't move up, but we will still
1162 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1163 ;; does.
1164 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1165 (file (ewoc-data prev)))
1166 (setf (vc-dir-fileinfo->marked file) nil)
1167 (ewoc-invalidate vc-ewoc prev)
1168 (vc-dir-move-to-goal-column)))
1170 (defun vc-dir-unmark-all-files (arg)
1171 "Unmark all files with the same state as the current one.
1172 With a prefix argument unmark all files.
1173 If the current entry is a directory, unmark all the child files.
1175 The commands operate on files that are on the same state.
1176 This command is intended to make it easy to deselect all files
1177 that share the same state."
1178 (interactive "P")
1179 (if arg
1180 (ewoc-map
1181 (lambda (filearg)
1182 (when (vc-dir-fileinfo->marked filearg)
1183 (setf (vc-dir-fileinfo->marked filearg) nil)
1185 vc-ewoc)
1186 (let* ((crt (ewoc-locate vc-ewoc))
1187 (data (ewoc-data crt)))
1188 (if (vc-dir-fileinfo->directory data)
1189 ;; It's a directory, unmark child files.
1190 (while (setq crt (ewoc-next vc-ewoc crt))
1191 (let ((crt-data (ewoc-data crt)))
1192 (unless (vc-dir-fileinfo->directory crt-data)
1193 (setf (vc-dir-fileinfo->marked crt-data) nil)
1194 (ewoc-invalidate vc-ewoc crt))))
1195 ;; It's a file
1196 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1197 (ewoc-map
1198 (lambda (filearg)
1199 (when (and (vc-dir-fileinfo->marked filearg)
1200 (eq (vc-dir-fileinfo->state filearg) crt-state))
1201 (setf (vc-dir-fileinfo->marked filearg) nil)
1203 vc-ewoc))))))
1205 (defun vc-dir-toggle-mark-file ()
1206 (let* ((crt (ewoc-locate vc-ewoc))
1207 (file (ewoc-data crt)))
1208 (if (vc-dir-fileinfo->marked file)
1209 (vc-dir-unmark-file)
1210 (vc-dir-mark-file))))
1212 (defun vc-dir-toggle-mark (e)
1213 (interactive "e")
1214 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1216 (defun vc-dir-delete-file ()
1217 "Delete the marked files, or the current file if no marks."
1218 (interactive)
1219 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1220 (list (vc-dir-current-file)))))
1222 (defun vc-dir-find-file ()
1223 "Find the file on the current line."
1224 (interactive)
1225 (find-file (vc-dir-current-file)))
1227 (defun vc-dir-find-file-other-window ()
1228 "Find the file on the current line, in another window."
1229 (interactive)
1230 (find-file-other-window (vc-dir-current-file)))
1232 (defun vc-dir-current-file ()
1233 (let ((node (ewoc-locate vc-ewoc)))
1234 (unless node
1235 (error "No file available."))
1236 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1238 (defun vc-dir-marked-files ()
1239 "Return the list of marked files."
1240 (mapcar
1241 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1242 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1244 (defun vc-dir-marked-only-files ()
1245 "Return the list of marked files, for marked directories return child files."
1246 (let ((crt (ewoc-nth vc-ewoc 0))
1247 result)
1248 (while crt
1249 (let ((crt-data (ewoc-data crt)))
1250 (if (vc-dir-fileinfo->marked crt-data)
1251 ;; FIXME: use vc-dir-child-files here instead of duplicating it.
1252 (if (vc-dir-fileinfo->directory crt-data)
1253 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1254 (dirlen (length dir))
1255 data)
1256 (while
1257 (and (setq crt (ewoc-next vc-ewoc crt))
1258 (string-equal
1259 (substring
1260 (progn
1261 (setq data (ewoc-data crt))
1262 (vc-dir-node-directory crt))
1263 0 dirlen)
1264 dir))
1265 (unless (vc-dir-fileinfo->directory data)
1266 (push (expand-file-name (vc-dir-fileinfo->name data)) result))))
1267 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1268 (setq crt (ewoc-next vc-ewoc crt)))
1269 (setq crt (ewoc-next vc-ewoc crt)))))
1270 result))
1272 (defun vc-dir-child-files ()
1273 "Return the list of child files for the current entry if it's a directory.
1274 If it is a file, return the file itself."
1275 (let* ((crt (ewoc-locate vc-ewoc))
1276 (crt-data (ewoc-data crt))
1277 result)
1278 (if (vc-dir-fileinfo->directory crt-data)
1279 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1280 (dirlen (length dir))
1281 data)
1282 (while
1283 (and (setq crt (ewoc-next vc-ewoc crt))
1284 (string-equal
1285 (substring
1286 (progn
1287 (setq data (ewoc-data crt))
1288 (vc-dir-node-directory crt))
1289 0 dirlen)
1290 dir))
1291 (unless (vc-dir-fileinfo->directory data)
1292 (push (expand-file-name (vc-dir-fileinfo->name data)) result))))
1293 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result))
1294 result))
1296 (defun vc-dir-resynch-file (&optional fname)
1297 "Update the entries for FILE in any directory buffers that list it."
1298 (let ((file (or fname (expand-file-name buffer-file-name))))
1299 (if (file-directory-p file)
1300 ;; FIXME: Maybe this should never happen?
1301 ;; FIXME: But it is useful to update the state of a directory
1302 ;; (more precisely the files in the directory) after some VC
1303 ;; operations.
1305 (let ((found-vc-dir-buf nil))
1306 (save-excursion
1307 (dolist (status-buf (buffer-list))
1308 (set-buffer status-buf)
1309 ;; look for a vc-dir buffer that might show this file.
1310 (when (derived-mode-p 'vc-dir-mode)
1311 (setq found-vc-dir-buf t)
1312 (let ((ddir (expand-file-name default-directory)))
1313 (when (vc-string-prefix-p ddir file)
1314 (let*
1315 ((file-short (substring file (length ddir)))
1316 (state
1317 (funcall (vc-client-object->file-to-state vc-client-mode)
1318 file))
1319 (extra
1320 (funcall (vc-client-object->file-to-extra vc-client-mode)
1321 file))
1322 (entry
1323 (list file-short state extra)))
1324 (vc-dir-update (list entry) status-buf))))))
1325 ;; We didn't find any vc-dir buffers, remove the hook, it is
1326 ;; not needed.
1327 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-dir-resynch-file)))))))
1329 (defun vc-dir-mode (client-object)
1330 "Major mode for dispatcher directory buffers.
1331 Marking/Unmarking key bindings and actions:
1332 m - marks a file/directory or if the region is active, mark all the files
1333 in region.
1334 Restrictions: - a file cannot be marked if any parent directory is marked
1335 - a directory cannot be marked if any child file or
1336 directory is marked
1337 u - marks a file/directory or if the region is active, unmark all the files
1338 in region.
1339 M - if the cursor is on a file: mark all the files with the same state as
1340 the current file
1341 - if the cursor is on a directory: mark all child files
1342 - with a prefix argument: mark all files
1343 U - if the cursor is on a file: unmark all the files with the same state
1344 as the current file
1345 - if the cursor is on a directory: unmark all child files
1346 - with a prefix argument: unmark all files
1349 \\{vc-dir-mode-map}"
1350 (setq mode-name (vc-client-object->name client-object))
1351 (setq major-mode 'vc-dir-mode)
1352 (setq buffer-read-only t)
1353 (use-local-map vc-dir-mode-map)
1354 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
1355 (set (make-local-variable 'vc-client-mode) client-object)
1356 (let ((buffer-read-only nil))
1357 (erase-buffer)
1358 (set (make-local-variable 'vc-dir-process-buffer) nil)
1359 (set (make-local-variable 'vc-ewoc)
1360 (ewoc-create (vc-client-object->file-to-info client-object)
1361 (vc-client-object->headers client-object)))
1362 (add-hook 'after-save-hook 'vc-dir-resynch-file)
1363 ;; Make sure that if the directory buffer is killed, the update
1364 ;; process running in the background is also killed.
1365 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1366 (funcall (vc-client-object->updater client-object)))
1367 (run-hooks 'vc-dir-mode-hook))
1369 (put 'vc-dir-mode 'mode-class 'special)
1371 (defun vc-dispatcher-browsing ()
1372 "Are we in a directory browser buffer?"
1373 (derived-mode-p 'vc-dir-mode))
1375 (defun vc-dispatcher-in-fileset-p (fileset)
1376 (let ((member nil))
1377 (while (and (not member) fileset)
1378 (let ((elem (pop fileset)))
1379 (if (if (file-directory-p elem)
1380 (eq t (compare-strings buffer-file-name nil (length elem)
1381 elem nil nil))
1382 (eq (current-buffer) (get-file-buffer elem)))
1383 (setq member t))))
1384 member))
1386 (defun vc-dispatcher-selection-set (&optional observer)
1387 "Deduce a set of files to which to apply an operation. Return a cons
1388 cell (SELECTION . FILESET), where SELECTION is what the user chose
1389 and FILES is the flist with any directories replaced by the listed files
1390 within them.
1392 If we're in a directory display, the fileset is the list of marked files (if
1393 there is one) else the file on the current line. If not in a directory
1394 display, but the current buffer visits a file, the fileset is a singleton
1395 containing that file. Otherwise, throw an error."
1396 (let ((selection
1397 (cond
1398 ;; Browsing with vc-dir
1399 ((vc-dispatcher-browsing)
1400 ;; If no files are marked, temporarily mark current file
1401 ;; and choose on that basis (so we get subordinate files)
1402 (if (not (vc-dir-marked-files))
1403 (prog2
1404 (vc-dir-mark-file)
1405 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
1406 (vc-dir-unmark-all-files t))
1407 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
1408 ;; Visiting an eligible file
1409 ((buffer-file-name)
1410 (cons (list buffer-file-name) (list buffer-file-name)))
1411 ;; No eligible file -- if there's a parent buffer, deduce from there
1412 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1413 (with-current-buffer vc-parent-buffer
1414 (vc-dispatcher-browsing))))
1415 (with-current-buffer vc-parent-buffer
1416 (vc-dispatcher-selection-set)))
1417 ;; No good set here, throw error
1418 (t (error "No fileset is available here")))))
1419 ;; We assume, in order to avoid unpleasant surprises to the user,
1420 ;; that a fileset is not in good shape to be handed to the user if the
1421 ;; buffers visiting the fileset don't match the on-disk contents.
1422 (unless observer
1423 (save-some-buffers
1424 nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
1425 selection))
1427 (provide 'vc-dispatcher)
1429 ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
1430 ;;; vc-dispatcher.el ends here