Fix variable names in buffer-face-mode-invoke
[emacs.git] / lisp / vc-dispatcher.el
blobc21be117f18148ebb45b3250c3999e9386f50520
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 (unless (eolp)
680 ;; Must be in sync with vc-default-status-printer.
681 (forward-char 25)))
683 (defun vc-dir-prepare-status-buffer (bname dir &optional create-new)
684 "Find a buffer named BNAME showing DIR, or create a new one."
685 (setq dir (expand-file-name dir))
686 (let*
687 ;; Look for another buffer name BNAME visiting the same directory.
688 ((buf (save-excursion
689 (unless create-new
690 (dolist (buffer (buffer-list))
691 (set-buffer buffer)
692 (when (and (vc-dispatcher-browsing)
693 (string= (expand-file-name default-directory) dir))
694 (return buffer)))))))
695 (or buf
696 ;; Create a new buffer named BNAME.
697 (with-current-buffer (create-file-buffer bname)
698 (cd dir)
699 (vc-setup-buffer (current-buffer))
700 ;; Reset the vc-parent-buffer-name so that it does not appear
701 ;; in the mode-line.
702 (setq vc-parent-buffer-name nil)
703 (current-buffer)))))
705 (defvar vc-dir-menu-map
706 (let ((map (make-sparse-keymap "VC-dir")))
707 (define-key map [quit]
708 '(menu-item "Quit" quit-window
709 :help "Quit"))
710 (define-key map [kill]
711 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
712 :enable (vc-dir-busy)
713 :help "Kill the command that updates the directory buffer"))
714 (define-key map [refresh]
715 '(menu-item "Refresh" vc-dir-refresh
716 :enable (not (vc-dir-busy))
717 :help "Refresh the contents of the directory buffer"))
718 ;; Movement.
719 (define-key map [sepmv] '("--"))
720 (define-key map [next-line]
721 '(menu-item "Next line" vc-dir-next-line
722 :help "Go to the next line" :keys "n"))
723 (define-key map [previous-line]
724 '(menu-item "Previous line" vc-dir-previous-line
725 :help "Go to the previous line"))
726 ;; Marking.
727 (define-key map [sepmrk] '("--"))
728 (define-key map [unmark-all]
729 '(menu-item "Unmark All" vc-dir-unmark-all-files
730 :help "Unmark all files that are in the same state as the current file\
731 \nWith prefix argument unmark all files"))
732 (define-key map [unmark-previous]
733 '(menu-item "Unmark previous " vc-dir-unmark-file-up
734 :help "Move to the previous line and unmark the file"))
736 (define-key map [mark-all]
737 '(menu-item "Mark All" vc-dir-mark-all-files
738 :help "Mark all files that are in the same state as the current file\
739 \nWith prefix argument mark all files"))
740 (define-key map [unmark]
741 '(menu-item "Unmark" vc-dir-unmark
742 :help "Unmark the current file or all files in the region"))
744 (define-key map [mark]
745 '(menu-item "Mark" vc-dir-mark
746 :help "Mark the current file or all files in the region"))
748 (define-key map [sepopn] '("--"))
749 (define-key map [open-other]
750 '(menu-item "Open in other window" vc-dir-find-file-other-window
751 :help "Find the file on the current line, in another window"))
752 (define-key map [open]
753 '(menu-item "Open file" vc-dir-find-file
754 :help "Find the file on the current line"))
755 map)
756 "Menu for dispatcher status")
758 ;; This is used so that client modes can add mode-specific menu
759 ;; items to vc-dir-menu-map.
760 (defun vc-dir-menu-map-filter (orig-binding)
761 (when (and (symbolp orig-binding) (fboundp orig-binding))
762 (setq orig-binding (indirect-function orig-binding)))
763 (let ((ext-binding
764 ;; This may be executed at load-time for tool-bar-local-item-from-menu
765 ;; but at that time vc-client-mode is not known (or even bound) yet.
766 (when (and (boundp 'vc-client-mode) vc-client-mode)
767 (funcall (vc-client-object->extra-menu vc-client-mode)))))
768 (if (null ext-binding)
769 orig-binding
770 (append orig-binding
771 '("----")
772 ext-binding))))
774 (defvar vc-dir-mode-map
775 (let ((map (make-keymap)))
776 (suppress-keymap map)
777 ;; Marking.
778 (define-key map "m" 'vc-dir-mark)
779 (define-key map "M" 'vc-dir-mark-all-files)
780 (define-key map "u" 'vc-dir-unmark)
781 (define-key map "U" 'vc-dir-unmark-all-files)
782 (define-key map "\C-?" 'vc-dir-unmark-file-up)
783 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
784 ;; Movement.
785 (define-key map "n" 'vc-dir-next-line)
786 (define-key map " " 'vc-dir-next-line)
787 (define-key map "\t" 'vc-dir-next-directory)
788 (define-key map "p" 'vc-dir-previous-line)
789 (define-key map [backtab] 'vc-dir-previous-directory)
790 ;;; Rebind paragraph-movement commands.
791 (define-key map "\M-}" 'vc-dir-next-directory)
792 (define-key map "\M-{" 'vc-dir-previous-directory)
793 (define-key map [C-down] 'vc-dir-next-directory)
794 (define-key map [C-up] 'vc-dir-previous-directory)
795 ;; The remainder.
796 (define-key map "f" 'vc-dir-find-file)
797 (define-key map "\C-m" 'vc-dir-find-file)
798 (define-key map "o" 'vc-dir-find-file-other-window)
799 (define-key map "q" 'quit-window)
800 (define-key map "g" 'vc-dir-refresh)
801 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
802 (define-key map [down-mouse-3] 'vc-dir-menu)
803 (define-key map [mouse-2] 'vc-dir-toggle-mark)
805 ;; Hook up the menu.
806 (define-key map [menu-bar vc-dir-mode]
807 `(menu-item
808 ;; This is used so that client modes can add mode-specific
809 ;; menu items to vc-dir-menu-map.
810 "VC-dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
811 map)
812 "Keymap for directory buffer.")
814 (defmacro vc-at-event (event &rest body)
815 "Evaluate `body' with point located at event-start of `event'.
816 If `body' uses `event', it should be a variable,
817 otherwise it will be evaluated twice."
818 (let ((posn (make-symbol "vc-at-event-posn")))
819 `(let ((,posn (event-start ,event)))
820 (save-excursion
821 (set-buffer (window-buffer (posn-window ,posn)))
822 (goto-char (posn-point ,posn))
823 ,@body))))
825 (defun vc-dir-menu (e)
826 "Popup the dispatcher status menu."
827 (interactive "e")
828 (vc-at-event e (popup-menu vc-dir-menu-map e)))
830 (defvar vc-dir-tool-bar-map
831 (let ((map (make-sparse-keymap)))
832 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
833 map vc-dir-mode-map)
834 (tool-bar-local-item "bookmark_add"
835 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
836 :help "Toggle mark on current item")
837 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
838 map vc-dir-mode-map
839 :rtl "right-arrow")
840 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
841 map vc-dir-mode-map
842 :rtl "left-arrow")
843 (tool-bar-local-item-from-menu 'vc-print-log "info"
844 map vc-dir-mode-map)
845 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
846 map vc-dir-mode-map)
847 (tool-bar-local-item-from-menu 'nonincremental-search-forward
848 "search" map)
849 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
850 map vc-dir-mode-map)
851 (tool-bar-local-item-from-menu 'quit-window "exit"
852 map vc-dir-mode-map)
853 map))
855 (defun vc-dir-node-directory (node)
856 ;; Compute the directory for NODE.
857 ;; If it's a directory node, get it from the the node.
858 (let ((data (ewoc-data node)))
859 (or (vc-dir-fileinfo->directory data)
860 ;; Otherwise compute it from the file name.
861 (file-name-directory
862 (expand-file-name
863 (vc-dir-fileinfo->name data))))))
865 (defun vc-dir-update (entries buffer &optional noinsert)
866 "Update BUFFER's ewoc from the list of ENTRIES.
867 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
868 ;; Add ENTRIES to the vc-dir buffer BUFFER.
869 (with-current-buffer buffer
870 ;; Insert the entries sorted by name into the ewoc.
871 ;; We assume the ewoc is sorted too, which should be the
872 ;; case if we always add entries with vc-dir-update.
873 (setq entries
874 ;; Sort: first files and then subdirectories.
875 ;; XXX: this is VERY inefficient, it computes the directory
876 ;; names too many times
877 (sort entries
878 (lambda (entry1 entry2)
879 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
880 (dir2 (file-name-directory (expand-file-name (car entry2)))))
881 (cond
882 ((string< dir1 dir2) t)
883 ((not (string= dir1 dir2)) nil)
884 ((string< (car entry1) (car entry2))))))))
885 ;; Insert directory entries in the right places.
886 (let ((entry (car entries))
887 (node (ewoc-nth vc-ewoc 0)))
888 ;; Insert . if it is not present.
889 (unless node
890 (let ((rd (file-relative-name default-directory)))
891 (ewoc-enter-last
892 vc-ewoc (vc-dir-create-fileinfo
893 rd nil nil nil (expand-file-name default-directory))))
894 (setq node (ewoc-nth vc-ewoc 0)))
896 (while (and entry node)
897 (let* ((entryfile (car entry))
898 (entrydir (file-name-directory (expand-file-name entryfile)))
899 (nodedir (vc-dir-node-directory node)))
900 (cond
901 ;; First try to find the directory.
902 ((string-lessp nodedir entrydir)
903 (setq node (ewoc-next vc-ewoc node)))
904 ((string-equal nodedir entrydir)
905 ;; Found the directory, find the place for the file name.
906 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
907 (cond
908 ((string-lessp nodefile entryfile)
909 (setq node (ewoc-next vc-ewoc node)))
910 ((string-equal nodefile entryfile)
911 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
912 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
913 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
914 (ewoc-invalidate vc-ewoc node)
915 (setq entries (cdr entries))
916 (setq entry (car entries))
917 (setq node (ewoc-next vc-ewoc node)))
919 (ewoc-enter-before vc-ewoc node
920 (apply 'vc-dir-create-fileinfo entry))
921 (setq entries (cdr entries))
922 (setq entry (car entries))))))
924 ;; We might need to insert a directory node if the
925 ;; previous node was in a different directory.
926 (let* ((rd (file-relative-name entrydir))
927 (prev-node (ewoc-prev vc-ewoc node))
928 (prev-dir (vc-dir-node-directory prev-node)))
929 (unless (string-equal entrydir prev-dir)
930 (ewoc-enter-before
931 vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
932 ;; Now insert the node itself.
933 (ewoc-enter-before vc-ewoc node
934 (apply 'vc-dir-create-fileinfo entry))
935 (setq entries (cdr entries) entry (car entries))))))
936 ;; We're past the last node, all remaining entries go to the end.
937 (unless (or node noinsert)
938 (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
939 (dolist (entry entries)
940 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
941 ;; Insert a directory node if needed.
942 (unless (string-equal lastdir entrydir)
943 (setq lastdir entrydir)
944 (let ((rd (file-relative-name entrydir)))
945 (ewoc-enter-last
946 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
947 ;; Now insert the node itself.
948 (ewoc-enter-last vc-ewoc
949 (apply 'vc-dir-create-fileinfo entry)))))))))
951 (defun vc-dir-busy ()
952 (and (buffer-live-p vc-dir-process-buffer)
953 (get-buffer-process vc-dir-process-buffer)))
955 (defun vc-dir-kill-dir-status-process ()
956 "Kill the temporary buffer and associated process."
957 (interactive)
958 (when (buffer-live-p vc-dir-process-buffer)
959 (let ((proc (get-buffer-process vc-dir-process-buffer)))
960 (when proc (delete-process proc))
961 (setq vc-dir-process-buffer nil)
962 (setq mode-line-process nil))))
964 (defun vc-dir-kill-query ()
965 ;; Make sure that when the status buffer is killed the update
966 ;; process running in background is also killed.
967 (if (vc-dir-busy)
968 (when (y-or-n-p "Status update process running, really kill status buffer?")
969 (vc-dir-kill-dir-status-process)
973 (defun vc-dir-next-line (arg)
974 "Go to the next line.
975 If a prefix argument is given, move by that many lines."
976 (interactive "p")
977 (with-no-warnings
978 (ewoc-goto-next vc-ewoc arg)
979 (vc-dir-move-to-goal-column)))
981 (defun vc-dir-previous-line (arg)
982 "Go to the previous line.
983 If a prefix argument is given, move by that many lines."
984 (interactive "p")
985 (ewoc-goto-prev vc-ewoc arg)
986 (vc-dir-move-to-goal-column))
988 (defun vc-dir-next-directory ()
989 "Go to the next directory."
990 (interactive)
991 (let ((orig (point)))
993 (catch 'foundit
994 (while t
995 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
996 (cond ((not next)
997 (throw 'foundit t))
999 (progn
1000 (ewoc-goto-node vc-ewoc next)
1001 (vc-dir-move-to-goal-column)
1002 (if (vc-dir-fileinfo->directory (ewoc-data next))
1003 (throw 'foundit nil))))))))
1004 (goto-char orig))))
1006 (defun vc-dir-previous-directory ()
1007 "Go to the previous directory."
1008 (interactive)
1009 (let ((orig (point)))
1011 (catch 'foundit
1012 (while t
1013 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
1014 (cond ((not prev)
1015 (throw 'foundit t))
1017 (progn
1018 (ewoc-goto-node vc-ewoc prev)
1019 (vc-dir-move-to-goal-column)
1020 (if (vc-dir-fileinfo->directory (ewoc-data prev))
1021 (throw 'foundit nil))))))))
1022 (goto-char orig))))
1024 (defun vc-dir-mark-unmark (mark-unmark-function)
1025 (if (use-region-p)
1026 (let ((firstl (line-number-at-pos (region-beginning)))
1027 (lastl (line-number-at-pos (region-end))))
1028 (save-excursion
1029 (goto-char (region-beginning))
1030 (while (<= (line-number-at-pos) lastl)
1031 (funcall mark-unmark-function))))
1032 (funcall mark-unmark-function)))
1034 (defun vc-string-prefix-p (prefix string)
1035 (let ((lpref (length prefix)))
1036 (and (>= (length string) lpref)
1037 (eq t (compare-strings prefix nil nil string nil lpref)))))
1039 (defun vc-dir-parent-marked-p (arg)
1040 ;; Return nil if none of the parent directories of arg is marked.
1041 (let* ((argdir (vc-dir-node-directory arg))
1042 (arglen (length argdir))
1043 (crt arg)
1044 data dir)
1045 ;; Go through the predecessors, checking if any directory that is
1046 ;; a parent is marked.
1047 (while (setq crt (ewoc-prev vc-ewoc crt))
1048 (setq data (ewoc-data crt))
1049 (setq dir (vc-dir-node-directory crt))
1050 (when (and (vc-dir-fileinfo->directory data)
1051 (vc-string-prefix-p dir argdir))
1052 (when (vc-dir-fileinfo->marked data)
1053 (error "Cannot mark `%s', parent directory `%s' marked"
1054 (vc-dir-fileinfo->name (ewoc-data arg))
1055 (vc-dir-fileinfo->name data)))))
1056 nil))
1058 (defun vc-dir-children-marked-p (arg)
1059 ;; Return nil if none of the children of arg is marked.
1060 (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
1061 (is-child t)
1062 (crt arg)
1063 data dir)
1064 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1065 (setq data (ewoc-data crt))
1066 (setq dir (vc-dir-node-directory crt))
1067 (if (string-match argdir-re dir)
1068 (when (vc-dir-fileinfo->marked data)
1069 (error "Cannot mark `%s', child `%s' marked"
1070 (vc-dir-fileinfo->name (ewoc-data arg))
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))
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)))))
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))
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.
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->marked 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)
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))))))))
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))))
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))
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)))
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.
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)
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)
1206 vc-ewoc))))))
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))))
1215 (defun vc-dir-toggle-mark (e)
1216 (interactive "e")
1217 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
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)))))
1225 (defun vc-dir-find-file ()
1226 "Find the file on the current line."
1227 (interactive)
1228 (find-file (vc-dir-current-file)))
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)))
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)))))
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)))
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 ;; FIXME: use vc-dir-child-files here instead of duplicating it.
1255 (if (vc-dir-fileinfo->directory crt-data)
1256 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1257 (dirlen (length dir))
1258 data)
1259 (while
1260 (and (setq crt (ewoc-next vc-ewoc crt))
1261 (vc-string-prefix-p dir
1262 (progn
1263 (setq data (ewoc-data crt))
1264 (vc-dir-node-directory crt))))
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 (vc-string-prefix-p dir (progn
1285 (setq data (ewoc-data crt))
1286 (vc-dir-node-directory crt))))
1287 (unless (vc-dir-fileinfo->directory data)
1288 (push (expand-file-name (vc-dir-fileinfo->name data)) result))))
1289 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result))
1290 result))
1292 (defun vc-dir-resynch-file (&optional fname)
1293 "Update the entries for FILE in any directory buffers that list it."
1294 (let ((file (or fname (expand-file-name buffer-file-name))))
1295 (if (file-directory-p file)
1296 ;; FIXME: Maybe this should never happen?
1297 ;; FIXME: But it is useful to update the state of a directory
1298 ;; (more precisely the files in the directory) after some VC
1299 ;; operations.
1301 (let ((found-vc-dir-buf nil))
1302 (save-excursion
1303 (dolist (status-buf (buffer-list))
1304 (set-buffer status-buf)
1305 ;; look for a vc-dir buffer that might show this file.
1306 (when (derived-mode-p 'vc-dir-mode)
1307 (setq found-vc-dir-buf t)
1308 (let ((ddir (expand-file-name default-directory)))
1309 (when (vc-string-prefix-p ddir file)
1310 (let*
1311 ;; FIXME: Any reason we don't use file-relative-name?
1312 ((file-short (substring file (length ddir)))
1313 (state (funcall (vc-client-object->file-to-state
1314 vc-client-mode)
1315 file))
1316 (extra (funcall (vc-client-object->file-to-extra
1317 vc-client-mode)
1318 file))
1319 (entry
1320 (list file-short state extra)))
1321 (vc-dir-update (list entry) status-buf))))))
1322 ;; We didn't find any vc-dir buffers, remove the hook, it is
1323 ;; not needed.
1324 (unless found-vc-dir-buf
1325 (remove-hook 'after-save-hook 'vc-dir-resynch-file)))))))
1327 (defun vc-dir-mode (client-object)
1328 "Major mode for dispatcher directory buffers.
1329 Marking/Unmarking key bindings and actions:
1330 m - marks a file/directory or if the region is active, mark all the files
1331 in region.
1332 Restrictions: - a file cannot be marked if any parent directory is marked
1333 - a directory cannot be marked if any child file or
1334 directory is marked
1335 u - marks a file/directory or if the region is active, unmark all the files
1336 in region.
1337 M - if the cursor is on a file: mark all the files with the same state as
1338 the current file
1339 - if the cursor is on a directory: mark all child files
1340 - with a prefix argument: mark all files
1341 U - if the cursor is on a file: unmark all the files with the same state
1342 as the current file
1343 - if the cursor is on a directory: unmark all child files
1344 - with a prefix argument: unmark all files
1347 \\{vc-dir-mode-map}"
1348 (setq mode-name (vc-client-object->name client-object))
1349 (setq major-mode 'vc-dir-mode)
1350 (setq buffer-read-only t)
1351 (use-local-map vc-dir-mode-map)
1352 (if (boundp 'tool-bar-map)
1353 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
1354 (set (make-local-variable 'vc-client-mode) client-object)
1355 (let ((buffer-read-only nil))
1356 (erase-buffer)
1357 (set (make-local-variable 'vc-dir-process-buffer) nil)
1358 (set (make-local-variable 'vc-ewoc)
1359 (ewoc-create (vc-client-object->file-to-info client-object)
1360 (vc-client-object->headers client-object)))
1361 (add-hook 'after-save-hook 'vc-dir-resynch-file)
1362 ;; Make sure that if the directory buffer is killed, the update
1363 ;; process running in the background is also killed.
1364 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1365 (funcall (vc-client-object->updater client-object)))
1366 (run-hooks 'vc-dir-mode-hook))
1368 (put 'vc-dir-mode 'mode-class 'special)
1370 (defun vc-dispatcher-browsing ()
1371 "Are we in a directory browser buffer?"
1372 (derived-mode-p 'vc-dir-mode))
1374 (defun vc-dispatcher-in-fileset-p (fileset)
1375 (let ((member nil))
1376 (while (and (not member) fileset)
1377 (let ((elem (pop fileset)))
1378 (if (if (file-directory-p elem)
1379 (eq t (compare-strings buffer-file-name nil (length elem)
1380 elem nil nil))
1381 (eq (current-buffer) (get-file-buffer elem)))
1382 (setq member t))))
1383 member))
1385 (defun vc-dispatcher-selection-set (&optional observer)
1386 "Deduce a set of files to which to apply an operation. Return a cons
1387 cell (SELECTION . FILESET), where SELECTION is what the user chose
1388 and FILES is the flist with any directories replaced by the listed files
1389 within them.
1391 If we're in a directory display, the fileset is the list of marked files (if
1392 there is one) else the file on the current line. If not in a directory
1393 display, but the current buffer visits a file, the fileset is a singleton
1394 containing that file. Otherwise, throw an error."
1395 (let ((selection
1396 (cond
1397 ;; Browsing with vc-dir
1398 ((vc-dispatcher-browsing)
1399 ;; If no files are marked, temporarily mark current file
1400 ;; and choose on that basis (so we get subordinate files)
1401 (if (not (vc-dir-marked-files))
1402 (prog2
1403 (vc-dir-mark-file)
1404 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
1405 (vc-dir-unmark-all-files t))
1406 (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
1407 ;; Visiting an eligible file
1408 ((buffer-file-name)
1409 (cons (list buffer-file-name) (list buffer-file-name)))
1410 ;; No eligible file -- if there's a parent buffer, deduce from there
1411 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1412 (with-current-buffer vc-parent-buffer
1413 (vc-dispatcher-browsing))))
1414 (with-current-buffer vc-parent-buffer
1415 (vc-dispatcher-selection-set)))
1416 ;; No good set here, throw error
1417 (t (error "No fileset is available here")))))
1418 ;; We assume, in order to avoid unpleasant surprises to the user,
1419 ;; that a fileset is not in good shape to be handed to the user if the
1420 ;; buffers visiting the fileset don't match the on-disk contents.
1421 (unless observer
1422 (save-some-buffers
1423 nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
1424 selection))
1426 (provide 'vc-dispatcher)
1428 ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
1429 ;;; vc-dispatcher.el ends here