Remove dead code.
[emacs.git] / lisp / vc-dispatcher.el
blobdc29fda72714500fdf6f75c30117deafd4e8256a
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 attempting to select one of three contexts: an
62 ;; explicitly selected fileset, the current working directory, or a
63 ;; global (null) context. The user may be looking at either of two
64 ;; different views; a buffer visiting a file, or a directory buffer
65 ;; generated by vc-dispatcher. The main UI problem connected with
66 ;; this mode is that the user may need to be able to select any of
67 ;; these three contexts from either view.
69 ;; The lower layer of this mode runs commands in subprocesses, either
70 ;; synchronously or asynchronously. Commands may be launched in one
71 ;; of two ways: they may be run immediately, or the calling mode can
72 ;; create a closure associated with a text-entry buffer, to be
73 ;; executed when the user types C-c to ship the buffer contents. In
74 ;; either case the command messages and error (if any) will remain
75 ;; available in a status buffer.
77 ;; Special behavior of dispatcher directory buffers:
79 ;; In dispatcher directory buffers, facilities to perform basic
80 ;; navigation and selection operations are provided by keymap and menu
81 ;; entries that dispatcher sets up itself, so they'll be uniform
82 ;; across all dispatcher-using client modes. Client modes are
83 ;; expected to append to these to provide mode-specific bindings.
85 ;; The standard map associates a 'state' slot (that the client mode
86 ;; may set) with each directory entry. The dispatcher knows nothing
87 ;; about the semantics of individual states, but mark and unmark commands
88 ;; treat all entries with the same state as the currently selected one as
89 ;; a unit.
91 ;; The interface
93 ;; The main interface to the lower level is vc-do-command. This launches a
94 ;; comand, synchronously or asynchronously, making the output available
95 ;; in a command log buffer. Two other functions, (vc-start-annotation) and
96 ;; (vc-finish-logentry), allow you to associate a command closure with an
97 ;; abbotation buffer so that when the user confirms the comment the closure
98 ;; is run (with the comment as part of its context).
100 ;; The interface to the upper level has the two main entry points (vc-dir)
101 ;; and (vc-dispatcher-selection-set) and a couple of convenience functions.
102 ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set)
103 ;; returns a selection set of files, either the marked files in a browsing
104 ;; buffer or the singleton set consisting of the file visited by the current
105 ;; buffer (when that is appropriate). It also does what is needed to ensure
106 ;; that on-disk files and the contents of their visiting Emacs buffers
107 ;; coincide.
109 ;; When the client mode adds a local mode-line-hook to a buffer, it
110 ;; will be called with the buffer file name as argument whenever the
111 ;; dispatcher resynchs the buffer.
113 ;; To do:
115 ;; - vc-dir-kill-dir-status-process should not be specific to dir-status,
116 ;; it should work for other async commands as well (pull/push/...).
118 ;; - the *VC-log* buffer needs font-locking.
120 ;; - Set `vc-dir-insert-directories' to t and check what operations
121 ;; and backends do not support directory arguments and fix them.
123 ;; - vc-dir needs mouse bindings.
125 ;; - vc-dir needs more key bindings for VC actions.
127 ;; - vc-dir toolbar needs more icons.
129 ;; - vc-dir-next-line should not print an "end of buffer" message when
130 ;; invoked with the cursor on the last file.
132 ;; - add commands to move to the prev/next directory in vc-dir.
134 ;; - document vc-dir in the manual.
137 (provide 'vc-dispatcher)
139 (eval-when-compile
140 (require 'cl)
141 (require 'dired) ; for dired-map-over-marks macro
142 (require 'dired-aux)) ; for dired-kill-{line,tree}
144 ;; General customization
146 (defcustom vc-logentry-check-hook nil
147 "Normal hook run by `vc-finish-logentry'.
148 Use this to impose your own rules on the entry in addition to any the
149 dispatcher client mode imposes itself."
150 :type 'hook
151 :group 'vc)
153 (defcustom vc-delete-logbuf-window t
154 "If non-nil, delete the *VC-log* buffer and window after each logical action.
155 If nil, bury that buffer instead.
156 This is most useful if you have multiple windows on a frame and would like to
157 preserve the setting."
158 :type 'boolean
159 :group 'vc)
161 (defcustom vc-command-messages nil
162 "If non-nil, display run messages from back-end commands."
163 :type 'boolean
164 :group 'vc)
166 (defcustom vc-suppress-confirm nil
167 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
168 :type 'boolean
169 :group 'vc)
171 ;; Variables the user doesn't need to know about.
173 (defvar vc-log-operation nil)
174 (defvar vc-log-after-operation-hook nil)
175 (defvar vc-log-fileset)
176 (defvar vc-log-extra)
178 ;; In a log entry buffer, this is a local variable
179 ;; that points to the buffer for which it was made
180 ;; (either a file, or a VC dired buffer).
181 (defvar vc-parent-buffer nil)
182 (put 'vc-parent-buffer 'permanent-local t)
183 (defvar vc-parent-buffer-name nil)
184 (put 'vc-parent-buffer-name 'permanent-local t)
186 ;; Common command execution logic
188 (defun vc-process-filter (p s)
189 "An alternative output filter for async process P.
190 One difference with the default filter is that this inserts S after markers.
191 Another is that undo information is not kept."
192 (let ((buffer (process-buffer p)))
193 (when (buffer-live-p buffer)
194 (with-current-buffer buffer
195 (save-excursion
196 (let ((buffer-undo-list t)
197 (inhibit-read-only t))
198 (goto-char (process-mark p))
199 (insert s)
200 (set-marker (process-mark p) (point))))))))
202 (defun vc-setup-buffer (buf)
203 "Prepare BUF for executing a slave command and make it current."
204 (let ((camefrom (current-buffer))
205 (olddir default-directory))
206 (set-buffer (get-buffer-create buf))
207 (kill-all-local-variables)
208 (set (make-local-variable 'vc-parent-buffer) camefrom)
209 (set (make-local-variable 'vc-parent-buffer-name)
210 (concat " from " (buffer-name camefrom)))
211 (setq default-directory olddir)
212 (let ((buffer-undo-list t)
213 (inhibit-read-only t))
214 (erase-buffer))))
216 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
218 (defun vc-process-sentinel (p s)
219 (let ((previous (process-get p 'vc-previous-sentinel))
220 (buf (process-buffer p)))
221 ;; Impatient users sometime kill "slow" buffers; check liveness
222 ;; to avoid "error in process sentinel: Selecting deleted buffer".
223 (when (buffer-live-p buf)
224 (when previous (funcall previous p s))
225 (with-current-buffer buf
226 (setq mode-line-process
227 (let ((status (process-status p)))
228 ;; Leave mode-line uncluttered, normally.
229 (unless (eq 'exit status)
230 (format " (%s)" status))))
231 (let (vc-sentinel-movepoint)
232 ;; Normally, we want async code such as sentinels to not move point.
233 (save-excursion
234 (goto-char (process-mark p))
235 (let ((cmds (process-get p 'vc-sentinel-commands)))
236 (process-put p 'vc-sentinel-commands nil)
237 (dolist (cmd cmds)
238 ;; Each sentinel may move point and the next one should be run
239 ;; at that new point. We could get the same result by having
240 ;; each sentinel read&set process-mark, but since `cmd' needs
241 ;; to work both for async and sync processes, this would be
242 ;; difficult to achieve.
243 (vc-exec-after cmd))))
244 ;; But sometimes the sentinels really want to move point.
245 (when vc-sentinel-movepoint
246 (let ((win (get-buffer-window (current-buffer) 0)))
247 (if (not win)
248 (goto-char vc-sentinel-movepoint)
249 (with-selected-window win
250 (goto-char vc-sentinel-movepoint))))))))))
252 (defun vc-set-mode-line-busy-indicator ()
253 (setq mode-line-process
254 (concat " " (propertize "[waiting...]"
255 'face 'mode-line-emphasis
256 'help-echo
257 "A VC command is in progress in this buffer"))))
259 (defun vc-exec-after (code)
260 "Eval CODE when the current buffer's process is done.
261 If the current buffer has no process, just evaluate CODE.
262 Else, add CODE to the process' sentinel."
263 (let ((proc (get-buffer-process (current-buffer))))
264 (cond
265 ;; If there's no background process, just execute the code.
266 ;; We used to explicitly call delete-process on exited processes,
267 ;; but this led to timing problems causing process output to be
268 ;; lost. Terminated processes get deleted automatically
269 ;; anyway. -- cyd
270 ((or (null proc) (eq (process-status proc) 'exit))
271 ;; Make sure we've read the process's output before going further.
272 (when proc (accept-process-output proc))
273 (eval code))
274 ;; If a process is running, add CODE to the sentinel
275 ((eq (process-status proc) 'run)
276 (vc-set-mode-line-busy-indicator)
277 (let ((previous (process-sentinel proc)))
278 (unless (eq previous 'vc-process-sentinel)
279 (process-put proc 'vc-previous-sentinel previous))
280 (set-process-sentinel proc 'vc-process-sentinel))
281 (process-put proc 'vc-sentinel-commands
282 ;; We keep the code fragments in the order given
283 ;; so that vc-diff-finish's message shows up in
284 ;; the presence of non-nil vc-command-messages.
285 (append (process-get proc 'vc-sentinel-commands)
286 (list code))))
287 (t (error "Unexpected process state"))))
288 nil)
290 (defvar vc-post-command-functions nil
291 "Hook run at the end of `vc-do-command'.
292 Each function is called inside the buffer in which the command was run
293 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
295 (defvar w32-quote-process-args)
297 (defun vc-delistify (filelist)
298 "Smash a FILELIST into a file list string suitable for info messages."
299 ;; FIXME what about file names with spaces?
300 (if (not filelist) "." (mapconcat 'identity filelist " ")))
302 ;;;###autoload
303 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
304 "Execute a VC command, notifying user and checking for errors.
305 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
306 current buffer if BUFFER is t. If the destination buffer is not
307 already current, set it up properly and erase it. The command is
308 considered successful if its exit status does not exceed OKSTATUS (if
309 OKSTATUS is nil, that means to ignore error status, if it is `async', that
310 means not to wait for termination of the subprocess; if it is t it means to
311 ignore all execution errors). FILE-OR-LIST is the name of a working file;
312 it may be a list of files or be nil (to execute commands that don't expect
313 a file name or set of files). If an optional list of FLAGS is present,
314 that is inserted into the command line before the filename."
315 ;; FIXME: file-relative-name can return a bogus result because
316 ;; it doesn't look at the actual file-system to see if symlinks
317 ;; come into play.
318 (let* ((files
319 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
320 (if (listp file-or-list) file-or-list (list file-or-list))))
321 (full-command
322 ;; What we're doing here is preparing a version of the command
323 ;; for display in a debug-progess message. If it's fewer than
324 ;; 20 characters display the entire command (without trailing
325 ;; newline). Otherwise display the first 20 followed by an ellipsis.
326 (concat (if (string= (substring command -1) "\n")
327 (substring command 0 -1)
328 command)
330 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
331 " " (vc-delistify files))))
332 (save-current-buffer
333 (unless (or (eq buffer t)
334 (and (stringp buffer)
335 (string= (buffer-name) buffer))
336 (eq buffer (current-buffer)))
337 (vc-setup-buffer (or buffer "*vc*")))
338 ;; If there's some previous async process still running, just kill it.
339 (let ((oldproc (get-buffer-process (current-buffer))))
340 ;; If we wanted to wait for oldproc to finish before doing
341 ;; something, we'd have used vc-eval-after.
342 ;; Use `delete-process' rather than `kill-process' because we don't
343 ;; want any of its output to appear from now on.
344 (if oldproc (delete-process oldproc)))
345 (let ((squeezed (remq nil flags))
346 (inhibit-read-only t)
347 (status 0))
348 (when files
349 (setq squeezed (nconc squeezed files)))
350 (let ((exec-path (append vc-path exec-path))
351 ;; Add vc-path to PATH for the execution of this command.
352 (process-environment
353 (cons (concat "PATH=" (getenv "PATH")
354 path-separator
355 (mapconcat 'identity vc-path path-separator))
356 process-environment))
357 (w32-quote-process-args t))
358 (when (and (eq okstatus 'async) (file-remote-p default-directory))
359 ;; start-process does not support remote execution
360 (setq okstatus nil))
361 (if (eq okstatus 'async)
362 ;; Run asynchronously.
363 (let ((proc
364 (let ((process-connection-type nil))
365 (apply 'start-file-process command (current-buffer)
366 command squeezed))))
367 (if vc-command-messages
368 (message "Running %s in background..." full-command))
369 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
370 (set-process-filter proc 'vc-process-filter)
371 (vc-exec-after
372 `(if vc-command-messages
373 (message "Running %s in background... done" ',full-command))))
374 ;; Run synchrously
375 (when vc-command-messages
376 (message "Running %s in foreground..." full-command))
377 (let ((buffer-undo-list t))
378 (setq status (apply 'process-file command nil t nil squeezed)))
379 (when (and (not (eq t okstatus))
380 (or (not (integerp status))
381 (and okstatus (< okstatus status))))
382 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
383 (pop-to-buffer (current-buffer))
384 (goto-char (point-min))
385 (shrink-window-if-larger-than-buffer))
386 (error "Running %s...FAILED (%s)" full-command
387 (if (integerp status) (format "status %d" status) status))))
388 ;; We're done. But don't emit a status message if running
389 ;; asychronously, it would just mislead.
390 (if (and vc-command-messages (not (eq okstatus 'async)))
391 (message "Running %s...OK = %d" full-command status)))
392 (vc-exec-after
393 `(run-hook-with-args 'vc-post-command-functions
394 ',command ',file-or-list ',flags))
395 status))))
397 ;; These functions are used to ensure that the view the user sees is up to date
398 ;; even if the dispatcher client mode has messed with file contents (as in,
399 ;; for example, VCS keyword expansion).
401 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
403 (defun vc-position-context (posn)
404 "Save a bit of the text around POSN in the current buffer.
405 Used to help us find the corresponding position again later
406 if markers are destroyed or corrupted."
407 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
408 ;; rcs.el mode.
409 (list posn
410 (buffer-size)
411 (buffer-substring posn
412 (min (point-max) (+ posn 100)))))
414 (defun vc-find-position-by-context (context)
415 "Return the position of CONTEXT in the current buffer.
416 If CONTEXT cannot be found, return nil."
417 (let ((context-string (nth 2 context)))
418 (if (equal "" context-string)
419 (point-max)
420 (save-excursion
421 (let ((diff (- (nth 1 context) (buffer-size))))
422 (when (< diff 0) (setq diff (- diff)))
423 (goto-char (nth 0 context))
424 (if (or (search-forward context-string nil t)
425 ;; Can't use search-backward since the match may continue
426 ;; after point.
427 (progn (goto-char (- (point) diff (length context-string)))
428 ;; goto-char doesn't signal an error at
429 ;; beginning of buffer like backward-char would
430 (search-forward context-string nil t)))
431 ;; to beginning of OSTRING
432 (- (point) (length context-string))))))))
434 (defun vc-context-matches-p (posn context)
435 "Return t if POSN matches CONTEXT, nil otherwise."
436 (let* ((context-string (nth 2 context))
437 (len (length context-string))
438 (end (+ posn len)))
439 (if (> end (1+ (buffer-size)))
441 (string= context-string (buffer-substring posn end)))))
443 (defun vc-buffer-context ()
444 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
445 Used by `vc-restore-buffer-context' to later restore the context."
446 (let ((point-context (vc-position-context (point)))
447 ;; Use mark-marker to avoid confusion in transient-mark-mode.
448 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
449 (vc-position-context (mark-marker))))
450 ;; Make the right thing happen in transient-mark-mode.
451 (mark-active nil))
452 (list point-context mark-context nil)))
454 (defun vc-restore-buffer-context (context)
455 "Restore point/mark, and reparse any affected compilation buffers.
456 CONTEXT is that which `vc-buffer-context' returns."
457 (let ((point-context (nth 0 context))
458 (mark-context (nth 1 context)))
459 ;; if necessary, restore point and mark
460 (if (not (vc-context-matches-p (point) point-context))
461 (let ((new-point (vc-find-position-by-context point-context)))
462 (when new-point (goto-char new-point))))
463 (and mark-active
464 mark-context
465 (not (vc-context-matches-p (mark) mark-context))
466 (let ((new-mark (vc-find-position-by-context mark-context)))
467 (when new-mark (set-mark new-mark))))))
469 (defun vc-revert-buffer-internal (&optional arg no-confirm)
470 "Revert buffer, keeping point and mark where user expects them.
471 Try to be clever in the face of changes due to expanded version-control
472 key words. This is important for typeahead to work as expected.
473 ARG and NO-CONFIRM are passed on to `revert-buffer'."
474 (interactive "P")
475 (widen)
476 (let ((context (vc-buffer-context)))
477 ;; Use save-excursion here, because it may be able to restore point
478 ;; and mark properly even in cases where vc-restore-buffer-context
479 ;; would fail. However, save-excursion might also get it wrong --
480 ;; in this case, vc-restore-buffer-context gives it a second try.
481 (save-excursion
482 ;; t means don't call normal-mode;
483 ;; that's to preserve various minor modes.
484 (revert-buffer arg no-confirm t))
485 (vc-restore-buffer-context context)))
487 (defun vc-resynch-window (file &optional keep noquery)
488 "If FILE is in the current buffer, either revert or unvisit it.
489 The choice between revert (to see expanded keywords) and unvisit
490 depends on KEEP. NOQUERY if non-nil inhibits confirmation for
491 reverting. NOQUERY should be t *only* if it is known the only
492 difference between the buffer and the file is due to
493 modifications by the dispatcher client code, rather than user
494 editing!"
495 (and (string= buffer-file-name file)
496 (if keep
497 (progn
498 (vc-revert-buffer-internal t noquery)
499 ;; TODO: Adjusting view mode might no longer be necessary
500 ;; after RMS change to files.el of 1999-08-08. Investigate
501 ;; this when we install the new VC.
502 (and view-read-only
503 (if (file-writable-p file)
504 (and view-mode
505 (let ((view-old-buffer-read-only nil))
506 (view-mode-exit)))
507 (and (not view-mode)
508 (not (eq (get major-mode 'mode-class) 'special))
509 (view-mode-enter))))
510 (run-hook-with-args 'modeline-hook buffer-file-name))
511 (kill-buffer (current-buffer)))))
513 (defun vc-resynch-buffer (file &optional keep noquery)
514 "If FILE is currently visited, resynch its buffer."
515 (if (string= buffer-file-name file)
516 (vc-resynch-window file keep noquery)
517 (let ((buffer (get-file-buffer file)))
518 (when buffer
519 (with-current-buffer buffer
520 (vc-resynch-window file keep noquery)))))
521 (vc-directory-resynch-file file))
523 ;; Command closures
525 (defun vc-start-logentry (files extra comment initial-contents msg action &optional after-hook)
526 "Accept a comment for an operation on FILES with extra data EXTRA.
527 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
528 action on close to ACTION. If COMMENT is a string and
529 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
530 contents of the log entry buffer. If COMMENT is a string and
531 INITIAL-CONTENTS is nil, do action immediately as if the user had
532 entered COMMENT. If COMMENT is t, also do action immediately with an
533 empty comment. Remember the file's buffer in `vc-parent-buffer'
534 \(current one if no file). AFTER-HOOK specifies the local value
535 for `vc-log-after-operation-hook'."
536 (let ((parent
537 (if (or (eq major-mode 'vc-dired-mode) (eq major-mode 'vc-dir-mode))
538 ;; If we are called from VC dired, the parent buffer is
539 ;; the current buffer.
540 (current-buffer)
541 (if (and files (equal (length files) 1))
542 (get-file-buffer (car files))
543 (current-buffer)))))
544 (if (and comment (not initial-contents))
545 (set-buffer (get-buffer-create "*VC-log*"))
546 (pop-to-buffer (get-buffer-create "*VC-log*")))
547 (set (make-local-variable 'vc-parent-buffer) parent)
548 (set (make-local-variable 'vc-parent-buffer-name)
549 (concat " from " (buffer-name vc-parent-buffer)))
550 (vc-log-edit files)
551 (make-local-variable 'vc-log-after-operation-hook)
552 (when after-hook
553 (setq vc-log-after-operation-hook after-hook))
554 (setq vc-log-operation action)
555 (setq vc-log-extra extra)
556 (when comment
557 (erase-buffer)
558 (when (stringp comment) (insert comment)))
559 (if (or (not comment) initial-contents)
560 (message "%s Type C-c C-c when done" msg)
561 (vc-finish-logentry (eq comment t)))))
563 (defun vc-finish-logentry (&optional nocomment)
564 "Complete the operation implied by the current log entry.
565 Use the contents of the current buffer as a check-in or registration
566 comment. If the optional arg NOCOMMENT is non-nil, then don't check
567 the buffer contents as a comment."
568 (interactive)
569 ;; Check and record the comment, if any.
570 (unless nocomment
571 (run-hooks 'vc-logentry-check-hook))
572 ;; Sync parent buffer in case the user modified it while editing the comment.
573 ;; But not if it is a vc-dired buffer.
574 (with-current-buffer vc-parent-buffer
575 (or vc-dired-mode (eq major-mode 'vc-dir-mode) (vc-buffer-sync)))
576 (unless vc-log-operation
577 (error "No log operation is pending"))
578 ;; save the parameters held in buffer-local variables
579 (let ((log-operation vc-log-operation)
580 (log-fileset vc-log-fileset)
581 (log-extra vc-log-extra)
582 (log-entry (buffer-string))
583 (after-hook vc-log-after-operation-hook)
584 (tmp-vc-parent-buffer vc-parent-buffer))
585 (pop-to-buffer vc-parent-buffer)
586 ;; OK, do it to it
587 (save-excursion
588 (funcall log-operation
589 log-fileset
590 log-extra
591 log-entry))
592 ;; Remove checkin window (after the checkin so that if that fails
593 ;; we don't zap the *VC-log* buffer and the typing therein).
594 ;; -- IMO this should be replaced with quit-window
595 (let ((logbuf (get-buffer "*VC-log*")))
596 (cond ((and logbuf vc-delete-logbuf-window)
597 (delete-windows-on logbuf (selected-frame))
598 ;; Kill buffer and delete any other dedicated windows/frames.
599 (kill-buffer logbuf))
600 (logbuf (pop-to-buffer "*VC-log*")
601 (bury-buffer)
602 (pop-to-buffer tmp-vc-parent-buffer))))
603 ;; Now make sure we see the expanded headers
604 (when log-fileset
605 (mapc
606 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
607 log-fileset))
608 (when vc-dired-mode
609 (dired-move-to-filename))
610 (when (eq major-mode 'vc-dir-mode)
611 (vc-dir-move-to-goal-column))
612 (run-hooks after-hook 'vc-finish-logentry-hook)))
614 ;; VC-Dired mode
615 ;; FIXME: to be removed when vc-dir support is finished
617 (defcustom vc-dired-listing-switches "-al"
618 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
619 :type 'string
620 :group 'vc
621 :version "21.1")
623 (defcustom vc-dired-recurse t
624 "If non-nil, show directory trees recursively in VC Dired."
625 :type 'boolean
626 :group 'vc
627 :version "20.3")
629 (defcustom vc-dired-terse-display t
630 "If non-nil, show only locked or locally modified files in VC Dired."
631 :type 'boolean
632 :group 'vc
633 :version "20.3")
635 (defvar vc-dired-mode nil)
636 (defvar vc-dired-window-configuration)
637 (defvar vc-dired-switches)
638 (defvar vc-dired-terse-mode)
640 (make-variable-buffer-local 'vc-dired-mode)
642 (defvar vc-dired-mode-map
643 (let ((map (make-sparse-keymap))
644 (vmap (make-sparse-keymap)))
645 (define-key map "\C-xv" vmap)
646 (define-key map "v" vmap)
647 (set-keymap-parent vmap vc-prefix-map)
648 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
649 map))
651 (define-derived-mode vc-dired-mode dired-mode "Dired under VC"
652 "The major mode used in VC directory buffers.
654 It works like Dired, but lists only files under version control, with
655 the current VC state of each file being indicated in the place of the
656 file's link count, owner, group and size. Subdirectories are also
657 listed, and you may insert them into the buffer as desired, like in
658 Dired.
660 All Dired commands operate normally, with the exception of `v', which
661 is redefined as the version control prefix, so that you can type
662 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
663 the file named in the current Dired buffer line. `vv' invokes
664 `vc-next-action' on this file, or on all files currently marked.
665 There is a special command, `*l', to mark all files currently locked."
666 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
667 ;; We do it here because dired might not be loaded yet
668 ;; when vc-dired-mode-map is initialized.
669 (set-keymap-parent vc-dired-mode-map dired-mode-map)
670 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
671 ;; The following is slightly modified from files.el,
672 ;; because file lines look a bit different in vc-dired-mode
673 ;; (the column before the date does not end in a digit).
674 ;; albinus: It should be done in the original declaration. Problem
675 ;; is the optional empty state-info; otherwise ")" would be good
676 ;; enough as delimeter.
677 (set (make-local-variable 'directory-listing-before-filename-regexp)
678 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
679 ;; In some locales, month abbreviations are as short as 2 letters,
680 ;; and they can be followed by ".".
681 (month (concat l l "+\\.?"))
682 (s " ")
683 (yyyy "[0-9][0-9][0-9][0-9]")
684 (dd "[ 0-3][0-9]")
685 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
686 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
687 (zone "[-+][0-2][0-9][0-5][0-9]")
688 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
689 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
690 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
691 "\\|" yyyy "-" iso-mm-dd "\\)"))
692 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
693 s "+"
694 "\\(" HH:MM "\\|" yyyy "\\)"))
695 (western-comma (concat month s "+" dd "," s "+" yyyy))
696 ;; Japanese MS-Windows ls-lisp has one-digit months, and
697 ;; omits the Kanji characters after month and day-of-month.
698 (mm "[ 0-1]?[0-9]")
699 (japanese
700 (concat mm l "?" s dd l "?" s "+"
701 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
702 ;; the .* below ensures that we find the last match on a line
703 (concat ".*" s
704 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
705 s "+")))
706 (and (boundp 'vc-dired-switches)
707 vc-dired-switches
708 (set (make-local-variable 'dired-actual-switches)
709 vc-dired-switches))
710 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
711 ;;(let ((backend-name (symbol-name (vc-responsible-backend
712 ;; default-directory))))
713 ;; (setq mode-name (concat mode-name backend-name))
714 ;; ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
715 ;; (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
716 ;; (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
717 ;; (cons backend-name vc-dire-menu-map) 'subdir)))
718 (setq vc-dired-mode t))
720 (defun vc-dired-toggle-terse-mode ()
721 "Toggle terse display in VC Dired."
722 (interactive)
723 (if (not vc-dired-mode)
725 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
726 (if vc-dired-terse-mode
727 (vc-dired-hook)
728 (revert-buffer))))
730 (defun vc-dired-mark-locked ()
731 "Mark all files currently locked."
732 (interactive)
733 (dired-mark-if (let ((f (dired-get-filename nil t)))
734 (and f
735 (not (file-directory-p f))
736 (not (vc-up-to-date-p f))))
737 "locked file"))
739 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
741 (defun vc-dired-reformat-line (vc-info)
742 "Reformat a directory-listing line.
743 Replace various columns with version control information, VC-INFO.
744 This code, like dired, assumes UNIX -l format."
745 (beginning-of-line)
746 (when (re-search-forward
747 ;; Match link count, owner, group, size. Group may be missing,
748 ;; and only the size is present in OS/2 -l format.
749 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
750 (line-end-position) t)
751 (replace-match (substring (concat vc-info " ") 0 10)
752 t t nil 1)))
754 (defun vc-dired-ignorable-p (filename)
755 "Should FILENAME be ignored in VC-Dired listings?"
756 (catch t
757 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
758 (dolist (ignorable completion-ignored-extensions)
759 (let ((ext (substring filename
760 (- (length filename)
761 (length ignorable)))))
762 (if (string= ignorable ext) (throw t t))))
763 ;; Ignore Makefiles derived from something else
764 (when (string= (file-name-nondirectory filename) "Makefile")
765 (let* ((dir (file-name-directory filename))
766 (peers (directory-files (or dir default-directory))))
767 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
768 (throw t t))))
769 nil))
771 (defun vc-dired-purge ()
772 "Remove empty subdirs."
773 (goto-char (point-min))
774 (while (dired-get-subdir)
775 (forward-line 2)
776 (if (dired-get-filename nil t)
777 (if (not (dired-next-subdir 1 t))
778 (goto-char (point-max)))
779 (forward-line -2)
780 (if (not (string= (dired-current-directory) default-directory))
781 (dired-do-kill-lines t "")
782 ;; We cannot remove the top level directory.
783 ;; Just make it look a little nicer.
784 (forward-line 1)
785 (or (eobp) (kill-line))
786 (if (not (dired-next-subdir 1 t))
787 (goto-char (point-max))))))
788 (goto-char (point-min)))
790 (defun vc-dired-buffers-for-dir (dir)
791 "Return a list of all vc-dired buffers that currently display DIR."
792 (let (result)
793 ;; Check whether dired is loaded.
794 (when (fboundp 'dired-buffers-for-dir)
795 (dolist (buffer (dired-buffers-for-dir dir))
796 (with-current-buffer buffer
797 (when vc-dired-mode
798 (push buffer result)))))
799 (nreverse result)))
801 ;;;###autoload
802 (defun vc-directory (dir read-switches)
803 "Create a buffer in VC Dired Mode for directory DIR.
805 See Info node `VC Dired Mode'.
807 With prefix arg READ-SWITCHES, specify a value to override
808 `dired-listing-switches' when generating the listing."
809 (interactive "DDired under VC (directory): \nP")
810 (let ((vc-dired-switches (concat vc-dired-listing-switches
811 (if vc-dired-recurse "R" ""))))
812 (if read-switches
813 (setq vc-dired-switches
814 (read-string "Dired listing switches: "
815 vc-dired-switches)))
816 (require 'dired)
817 (require 'dired-aux)
818 (switch-to-buffer
819 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
820 vc-dired-switches
821 'vc-dired-mode))))
823 ;; The ewoc-based vc-directory implementation
825 (defcustom vc-dir-mode-hook nil
826 "Normal hook run by `vc-dir-mode'.
827 See `run-hooks'."
828 :type 'hook
829 :group 'vc)
831 ;; Used to store information for the files displayed in the *VC status* buffer.
832 ;; Each item displayed corresponds to one of these defstructs.
833 (defstruct (vc-dir-fileinfo
834 (:copier nil)
835 (:type list) ;So we can use `member' on lists of FIs.
836 (:constructor
837 ;; We could define it as an alias for `list'.
838 vc-dir-create-fileinfo (name state &optional extra marked directory))
839 (:conc-name vc-dir-fileinfo->))
840 name ;Keep it as first, for `member'.
841 state
842 ;; For storing client-mode specific information.
843 extra
844 marked
845 ;; To keep track of not updated files during a global refresh
846 needs-update
847 ;; To distinguish files and directories.
848 directory)
850 ;; Used to describe a dispatcher client mode.
851 (defstruct (vc-client-object
852 (:copier nil)
853 (:constructor
854 vc-create-client-object (name
855 headers
856 file-to-info
857 file-to-state
858 file-to-extra
859 updater))
860 (:conc-name vc-client-object->))
861 name
862 headers
863 file-to-info
864 file-to-state
865 file-to-extra
866 updater)
868 (defvar vc-ewoc nil)
869 (defvar vc-dir-process-buffer nil
870 "The buffer used for the asynchronous call that computes the VC status.")
872 (defun vc-dir-move-to-goal-column ()
873 ;; Used to keep the cursor on the file name column.
874 (beginning-of-line)
875 ;; Must be in sync with vc-default-status-printer.
876 (forward-char 25))
878 (defun vc-dir-prepare-status-buffer (dir &optional create-new)
879 "Find a *vc-dir* buffer showing DIR, or create a new one."
880 (setq dir (expand-file-name dir))
881 (let* ((bname "*vc-dir*")
882 ;; Look for another *vc-dir* buffer visiting the same directory.
883 (buf (save-excursion
884 (unless create-new
885 (dolist (buffer (buffer-list))
886 (set-buffer buffer)
887 (when (and (eq major-mode 'vc-dir-mode)
888 (string= (expand-file-name default-directory) dir))
889 (return buffer)))))))
890 (or buf
891 ;; Create a new *vc-dir* buffer.
892 (with-current-buffer (create-file-buffer bname)
893 (cd dir)
894 (vc-setup-buffer (current-buffer))
895 ;; Reset the vc-parent-buffer-name so that it does not appear
896 ;; in the mode-line.
897 (setq vc-parent-buffer-name nil)
898 (current-buffer)))))
900 (defvar vc-dir-menu-map
901 (let ((map (make-sparse-keymap "VC-dir")))
902 (define-key map [quit]
903 '(menu-item "Quit" quit-window
904 :help "Quit"))
905 (define-key map [kill]
906 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
907 :enable (vc-dir-busy)
908 :help "Kill the command that updates VC status buffer"))
909 (define-key map [refresh]
910 '(menu-item "Refresh" vc-dir-refresh
911 :enable (not (vc-dir-busy))
912 :help "Refresh the contents of the VC status buffer"))
913 ;; Movement.
914 (define-key map [sepmv] '("--"))
915 (define-key map [next-line]
916 '(menu-item "Next line" vc-dir-next-line
917 :help "Go to the next line" :keys "n"))
918 (define-key map [previous-line]
919 '(menu-item "Previous line" vc-dir-previous-line
920 :help "Go to the previous line"))
921 ;; Marking.
922 (define-key map [sepmrk] '("--"))
923 (define-key map [unmark-all]
924 '(menu-item "Unmark All" vc-dir-unmark-all-files
925 :help "Unmark all files that are in the same state as the current file\
926 \nWith prefix argument unmark all files"))
927 (define-key map [unmark-previous]
928 '(menu-item "Unmark previous " vc-dir-unmark-file-up
929 :help "Move to the previous line and unmark the file"))
931 (define-key map [mark-all]
932 '(menu-item "Mark All" vc-dir-mark-all-files
933 :help "Mark all files that are in the same state as the current file\
934 \nWith prefix argument mark all files"))
935 (define-key map [unmark]
936 '(menu-item "Unmark" vc-dir-unmark
937 :help "Unmark the current file or all files in the region"))
939 (define-key map [mark]
940 '(menu-item "Mark" vc-dir-mark
941 :help "Mark the current file or all files in the region"))
943 (define-key map [sepopn] '("--"))
944 (define-key map [open-other]
945 '(menu-item "Open in other window" vc-dir-find-file-other-window
946 :help "Find the file on the current line, in another window"))
947 (define-key map [open]
948 '(menu-item "Open file" vc-dir-find-file
949 :help "Find the file on the current line"))
950 map)
951 "Menu for dispatcher status")
953 (defalias 'vc-dir-menu-map vc-dir-menu-map)
955 (defvar vc-dir-mode-map
956 (let ((map (make-keymap)))
957 (suppress-keymap map)
958 ;; Marking.
959 (define-key map "m" 'vc-dir-mark)
960 (define-key map "M" 'vc-dir-mark-all-files)
961 (define-key map "u" 'vc-dir-unmark)
962 (define-key map "U" 'vc-dir-unmark-all-files)
963 (define-key map "\C-?" 'vc-dir-unmark-file-up)
964 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
965 ;; Movement.
966 (define-key map "n" 'vc-dir-next-line)
967 (define-key map " " 'vc-dir-next-line)
968 (define-key map "\t" 'vc-dir-next-line)
969 (define-key map "p" 'vc-dir-previous-line)
970 (define-key map [backtab] 'vc-dir-previous-line)
971 ;; The remainder.
972 (define-key map "f" 'vc-dir-find-file)
973 (define-key map "\C-m" 'vc-dir-find-file)
974 (define-key map "o" 'vc-dir-find-file-other-window)
975 (define-key map "q" 'quit-window)
976 (define-key map "g" 'vc-dir-refresh)
977 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
978 (define-key map [(down-mouse-3)] 'vc-dir-menu)
979 (define-key map [(mouse-2)] 'vc-dir-toggle-mark)
981 ;; FIXME: Calls back into vc.el
982 ;; Hook up the menu.
983 (define-key map [menu-bar vc-dir-mode]
984 '(menu-item
985 ;; This is used so that client modes can add mode-specific
986 ;; menu items to vc-dir-menu-map.
987 "VC Status" vc-dir-menu-map :filter vc-dir-menu-map-filter))
988 map)
989 "Keymap for VC status")
991 (defmacro vc-at-event (event &rest body)
992 "Evaluate `body' wich point located at event-start of `event'.
993 If `body' uses `event', it should be a variable,
994 otherwise it will be evaluated twice."
995 (let ((posn (gensym "vc-at-event-posn")))
996 `(let ((,posn (event-start ,event)))
997 (save-excursion
998 (set-buffer (window-buffer (posn-window ,posn)))
999 (goto-char (posn-point ,posn))
1000 ,@body))))
1002 (defun vc-dir-menu (e)
1003 "Popup the VC status menu."
1004 (interactive "e")
1005 (vc-at-event e (popup-menu vc-dir-menu-map e)))
1007 (defvar vc-dir-tool-bar-map
1008 (let ((map (make-sparse-keymap)))
1009 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
1010 map vc-dir-mode-map)
1011 (tool-bar-local-item "bookmark_add"
1012 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
1013 :help "Toggle mark on current item")
1014 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
1015 map vc-dir-mode-map
1016 :rtl "right-arrow")
1017 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
1018 map vc-dir-mode-map
1019 :rtl "left-arrow")
1020 (tool-bar-local-item-from-menu 'vc-print-log "info"
1021 map vc-dir-mode-map)
1022 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
1023 map vc-dir-mode-map)
1024 (tool-bar-local-item-from-menu 'nonincremental-search-forward
1025 "search" map)
1026 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
1027 map vc-dir-mode-map)
1028 (tool-bar-local-item-from-menu 'quit-window "exit"
1029 map vc-dir-mode-map)
1030 map))
1032 ;; t if directories should be shown in vc-dir.
1033 ;; WORK IN PROGRESS! This variable will likely disappear when the
1034 ;; work is done.
1035 (defvar vc-dir-insert-directories t)
1037 (defun vc-dir-update (entries buffer &optional noinsert)
1038 "Update BUFFER's ewoc from the list of ENTRIES.
1039 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
1040 ;; Add ENTRIES to the vc-dir buffer BUFFER.
1041 (with-current-buffer buffer
1042 ;; Insert the entries sorted by name into the ewoc.
1043 ;; We assume the ewoc is sorted too, which should be the
1044 ;; case if we always add entries with vc-dir-update.
1045 (setq entries
1046 ;; Sort: first files and then subdirectories.
1047 ;; XXX: this is VERY inefficient, it computes the directory
1048 ;; names too many times
1049 (sort entries
1050 (lambda (entry1 entry2)
1051 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
1052 (dir2 (file-name-directory (expand-file-name (car entry2)))))
1053 (cond
1054 ((string< dir1 dir2) t)
1055 ((not (string= dir1 dir2)) nil)
1056 ((string< (car entry1) (car entry2))))))))
1057 (if (not vc-dir-insert-directories)
1058 (let ((entry (car entries))
1059 (node (ewoc-nth vc-ewoc 0)))
1060 (while (and entry node)
1061 (let ((entryfile (car entry))
1062 (nodefile (vc-dir-fileinfo->name (ewoc-data node))))
1063 (cond
1064 ((string-lessp nodefile entryfile)
1065 (setq node (ewoc-next vc-ewoc node)))
1066 ((string-lessp entryfile nodefile)
1067 (unless noinsert
1068 (ewoc-enter-before vc-ewoc node
1069 (apply 'vc-dir-create-fileinfo entry)))
1070 (setq entries (cdr entries) entry (car entries)))
1072 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
1073 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
1074 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
1075 (ewoc-invalidate vc-ewoc node)
1076 (setq entries (cdr entries) entry (car entries))
1077 (setq node (ewoc-next vc-ewoc node))))))
1078 (unless (or node noinsert)
1079 ;; We're past the last node, all remaining entries go to the end.
1080 (while entries
1081 (ewoc-enter-last vc-ewoc
1082 (apply 'vc-dir-create-fileinfo (pop entries))))))
1083 ;; Insert directory entries in the right places.
1084 (let ((entry (car entries))
1085 (node (ewoc-nth vc-ewoc 0)))
1086 ;; Insert . if it is not present.
1087 (unless node
1088 (let ((rd (file-relative-name default-directory)))
1089 (ewoc-enter-last
1090 vc-ewoc (vc-dir-create-fileinfo
1091 rd nil nil nil (expand-file-name default-directory))))
1092 (setq node (ewoc-nth vc-ewoc 0)))
1094 (while (and entry node)
1095 (let* ((entryfile (car entry))
1096 (entrydir (file-name-directory (expand-file-name entryfile)))
1097 (nodedir
1098 (or (vc-dir-fileinfo->directory (ewoc-data node))
1099 (file-name-directory
1100 (expand-file-name
1101 (vc-dir-fileinfo->name (ewoc-data node)))))))
1102 (cond
1103 ;; First try to find the directory.
1104 ((string-lessp nodedir entrydir)
1105 (setq node (ewoc-next vc-ewoc node)))
1106 ((string-equal nodedir entrydir)
1107 ;; Found the directory, find the place for the file name.
1108 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
1109 (cond
1110 ((string-lessp nodefile entryfile)
1111 (setq node (ewoc-next vc-ewoc node)))
1112 ((string-equal nodefile entryfile)
1113 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
1114 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
1115 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
1116 (ewoc-invalidate vc-ewoc node)
1117 (setq entries (cdr entries) entry (car entries))
1118 (setq node (ewoc-next vc-ewoc node)))
1120 (ewoc-enter-before vc-ewoc node
1121 (apply 'vc-dir-create-fileinfo entry))
1122 (setq entries (cdr entries) entry (car entries))))))
1124 ;; We need to insert a directory node
1125 (let ((rd (file-relative-name entrydir)))
1126 (ewoc-enter-last
1127 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))
1128 ;; Now insert the node itself.
1129 (ewoc-enter-before vc-ewoc node
1130 (apply 'vc-dir-create-fileinfo entry))
1131 (setq entries (cdr entries) entry (car entries))))))
1132 ;; We're past the last node, all remaining entries go to the end.
1133 (unless (or node noinsert)
1134 (let* ((lastnode (ewoc-nth vc-ewoc -1))
1135 (lastdir
1136 (or (vc-dir-fileinfo->directory (ewoc-data lastnode))
1137 (file-name-directory
1138 (expand-file-name
1139 (vc-dir-fileinfo->name (ewoc-data lastnode)))))))
1140 (dolist (entry entries)
1141 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
1142 ;; Insert a directory node if needed.
1143 (unless (string-equal lastdir entrydir)
1144 (setq lastdir entrydir)
1145 (let ((rd (file-relative-name entrydir)))
1146 (ewoc-enter-last
1147 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
1148 ;; Now insert the node itself.
1149 (ewoc-enter-last vc-ewoc
1150 (apply 'vc-dir-create-fileinfo entry))))))))))
1152 (defun vc-dir-busy ()
1153 (and (buffer-live-p vc-dir-process-buffer)
1154 (get-buffer-process vc-dir-process-buffer)))
1156 (defun vc-dir-kill-dir-status-process ()
1157 "Kill the temporary buffer and associated process."
1158 (interactive)
1159 (when (buffer-live-p vc-dir-process-buffer)
1160 (let ((proc (get-buffer-process vc-dir-process-buffer)))
1161 (when proc (delete-process proc))
1162 (setq vc-dir-process-buffer nil)
1163 (setq mode-line-process nil))))
1165 (defun vc-dir-kill-query ()
1166 ;; Make sure that when the VC status buffer is killed the update
1167 ;; process running in background is also killed.
1168 (if (vc-dir-busy)
1169 (when (y-or-n-p "Status update process running, really kill status buffer?")
1170 (vc-dir-kill-dir-status-process)
1174 (defun vc-dir-next-line (arg)
1175 "Go to the next line.
1176 If a prefix argument is given, move by that many lines."
1177 (interactive "p")
1178 (ewoc-goto-next vc-ewoc arg)
1179 (vc-dir-move-to-goal-column))
1181 (defun vc-dir-previous-line (arg)
1182 "Go to the previous line.
1183 If a prefix argument is given, move by that many lines."
1184 (interactive "p")
1185 (ewoc-goto-prev vc-ewoc arg)
1186 (vc-dir-move-to-goal-column))
1188 (defun vc-dir-mark-unmark (mark-unmark-function)
1189 (if (use-region-p)
1190 (let ((firstl (line-number-at-pos (region-beginning)))
1191 (lastl (line-number-at-pos (region-end))))
1192 (save-excursion
1193 (goto-char (region-beginning))
1194 (while (<= (line-number-at-pos) lastl)
1195 (funcall mark-unmark-function))))
1196 (funcall mark-unmark-function)))
1198 (defun vc-dir-parent-marked-p (arg)
1199 (when vc-dir-insert-directories
1200 ;; Return nil if none of the parent directories of arg is marked.
1201 (let* ((argdata (ewoc-data arg))
1202 (argdir
1203 (let ((crtdir (vc-dir-fileinfo->directory argdata)))
1204 (if crtdir
1205 crtdir
1206 (file-name-directory (expand-file-name
1207 (vc-dir-fileinfo->name argdata))))))
1208 (arglen (length argdir))
1209 (crt arg)
1210 data dir)
1211 ;; Go through the predecessors, checking if any directory that is
1212 ;; a parent is marked.
1213 (while (setq crt (ewoc-prev vc-ewoc crt))
1214 (setq data (ewoc-data crt))
1215 (setq dir
1216 (let ((crtdir (vc-dir-fileinfo->directory data)))
1217 (if crtdir
1218 crtdir
1219 (file-name-directory (expand-file-name
1220 (vc-dir-fileinfo->name data))))))
1222 (when (and (vc-dir-fileinfo->directory data)
1223 (string-equal (substring argdir 0 (length dir)) dir))
1224 (when (vc-dir-fileinfo->marked data)
1225 (error "Cannot mark `%s', parent directory `%s' marked"
1226 (vc-dir-fileinfo->name argdata)
1227 (vc-dir-fileinfo->name data)))))
1228 nil)))
1230 (defun vc-dir-children-marked-p (arg)
1231 ;; Return nil if none of the children of arg is marked.
1232 (when vc-dir-insert-directories
1233 (let* ((argdata (ewoc-data arg))
1234 (argdir (vc-dir-fileinfo->directory argdata))
1235 (arglen (length argdir))
1236 (is-child t)
1237 (crt arg)
1238 data dir)
1239 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1240 (setq data (ewoc-data crt))
1241 (setq dir
1242 (let ((crtdir (vc-dir-fileinfo->directory data)))
1243 (if crtdir
1244 crtdir
1245 (file-name-directory (expand-file-name
1246 (vc-dir-fileinfo->name data))))))
1247 (if (string-equal argdir (substring dir 0 arglen))
1248 (when (vc-dir-fileinfo->marked data)
1249 (error "Cannot mark `%s', child `%s' marked"
1250 (vc-dir-fileinfo->name argdata)
1251 (vc-dir-fileinfo->name data)))
1252 ;; We are done, we got to an entry that is not a child of `arg'.
1253 (setq is-child nil)))
1254 nil)))
1256 (defun vc-dir-mark-file (&optional arg)
1257 ;; Mark ARG or the current file and move to the next line.
1258 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1259 (file (ewoc-data crt))
1260 (isdir (vc-dir-fileinfo->directory file)))
1261 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1262 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1263 (setf (vc-dir-fileinfo->marked file) t)
1264 (ewoc-invalidate vc-ewoc crt)
1265 (unless (or arg (mouse-event-p last-command-event))
1266 (vc-dir-next-line 1)))))
1268 (defun vc-dir-mark ()
1269 "Mark the current file or all files in the region.
1270 If the region is active, mark all the files in the region.
1271 Otherwise mark the file on the current line and move to the next
1272 line."
1273 (interactive)
1274 (vc-dir-mark-unmark 'vc-dir-mark-file))
1276 (defun vc-dir-mark-all-files (arg)
1277 "Mark all files with the same state as the current one.
1278 With a prefix argument mark all files.
1279 If the current entry is a directory, mark all child files.
1281 The VC commands operate on files that are on the same state.
1282 This command is intended to make it easy to select all files that
1283 share the same state."
1284 (interactive "P")
1285 (if arg
1286 ;; Mark all files.
1287 (progn
1288 ;; First check that no directory is marked, we can't mark
1289 ;; files in that case.
1290 (ewoc-map
1291 (lambda (filearg)
1292 (when (and (vc-dir-fileinfo->directory filearg)
1293 (vc-dir-fileinfo->directory filearg))
1294 (error "Cannot mark all files, directory `%s' marked"
1295 (vc-dir-fileinfo->name filearg))))
1296 vc-ewoc)
1297 (ewoc-map
1298 (lambda (filearg)
1299 (unless (vc-dir-fileinfo->marked filearg)
1300 (setf (vc-dir-fileinfo->marked filearg) t)
1302 vc-ewoc))
1303 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1304 (if (vc-dir-fileinfo->directory data)
1305 ;; It's a directory, mark child files.
1306 (let ((crt (ewoc-locate vc-ewoc)))
1307 (unless (vc-dir-children-marked-p crt)
1308 (while (setq crt (ewoc-next vc-ewoc crt))
1309 (let ((crt-data (ewoc-data crt)))
1310 (unless (vc-dir-fileinfo->directory crt-data)
1311 (setf (vc-dir-fileinfo->marked crt-data) t)
1312 (ewoc-invalidate vc-ewoc crt))))))
1313 ;; It's a file
1314 (let ((state (vc-dir-fileinfo->state data))
1315 (crt (ewoc-nth vc-ewoc 0)))
1316 (while crt
1317 (let ((crt-data (ewoc-data crt)))
1318 (when (and (not (vc-dir-fileinfo->marked crt-data))
1319 (eq (vc-dir-fileinfo->state crt-data) state)
1320 (not (vc-dir-fileinfo->directory crt-data)))
1321 (vc-dir-mark-file crt)))
1322 (setq crt (ewoc-next vc-ewoc crt))))))))
1324 (defun vc-dir-unmark-file ()
1325 ;; Unmark the current file and move to the next line.
1326 (let* ((crt (ewoc-locate vc-ewoc))
1327 (file (ewoc-data crt)))
1328 (setf (vc-dir-fileinfo->marked file) nil)
1329 (ewoc-invalidate vc-ewoc crt)
1330 (unless (mouse-event-p last-command-event)
1331 (vc-dir-next-line 1))))
1333 (defun vc-dir-unmark ()
1334 "Unmark the current file or all files in the region.
1335 If the region is active, unmark all the files in the region.
1336 Otherwise mark the file on the current line and move to the next
1337 line."
1338 (interactive)
1339 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1341 (defun vc-dir-unmark-file-up ()
1342 "Move to the previous line and unmark the file."
1343 (interactive)
1344 ;; If we're on the first line, we won't move up, but we will still
1345 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1346 ;; does.
1347 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1348 (file (ewoc-data prev)))
1349 (setf (vc-dir-fileinfo->marked file) nil)
1350 (ewoc-invalidate vc-ewoc prev)
1351 (vc-dir-move-to-goal-column)))
1353 (defun vc-dir-unmark-all-files (arg)
1354 "Unmark all files with the same state as the current one.
1355 With a prefix argument unmark all files.
1356 If the current entry is a directory, unmark all the child files.
1358 The VC commands operate on files that are on the same state.
1359 This command is intended to make it easy to deselect all files
1360 that share the same state."
1361 (interactive "P")
1362 (if arg
1363 (ewoc-map
1364 (lambda (filearg)
1365 (when (vc-dir-fileinfo->marked filearg)
1366 (setf (vc-dir-fileinfo->marked filearg) nil)
1368 vc-ewoc)
1369 (let* ((crt (ewoc-locate vc-ewoc))
1370 (data (ewoc-data crt)))
1371 (if (vc-dir-fileinfo->directory data)
1372 ;; It's a directory, unmark child files.
1373 (while (setq crt (ewoc-next vc-ewoc crt))
1374 (let ((crt-data (ewoc-data crt)))
1375 (unless (vc-dir-fileinfo->directory crt-data)
1376 (setf (vc-dir-fileinfo->marked crt-data) nil)
1377 (ewoc-invalidate vc-ewoc crt))))
1378 ;; It's a file
1379 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1380 (ewoc-map
1381 (lambda (filearg)
1382 (when (and (vc-dir-fileinfo->marked filearg)
1383 (eq (vc-dir-fileinfo->state filearg) crt-state))
1384 (setf (vc-dir-fileinfo->marked filearg) nil)
1386 vc-ewoc))))))
1388 (defun vc-dir-toggle-mark-file ()
1389 (let* ((crt (ewoc-locate vc-ewoc))
1390 (file (ewoc-data crt)))
1391 (if (vc-dir-fileinfo->marked file)
1392 (vc-dir-unmark-file)
1393 (vc-dir-mark-file))))
1395 (defun vc-dir-toggle-mark (e)
1396 (interactive "e")
1397 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1399 (defun vc-dir-delete-file ()
1400 "Delete the marked files, or the current file if no marks."
1401 (interactive)
1402 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1403 (list (vc-dir-current-file)))))
1405 (defun vc-dir-find-file ()
1406 "Find the file on the current line."
1407 (interactive)
1408 (find-file (vc-dir-current-file)))
1410 (defun vc-dir-find-file-other-window ()
1411 "Find the file on the current line, in another window."
1412 (interactive)
1413 (find-file-other-window (vc-dir-current-file)))
1415 (defun vc-dir-current-file ()
1416 (let ((node (ewoc-locate vc-ewoc)))
1417 (unless node
1418 (error "No file available."))
1419 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1421 (defun vc-dir-marked-files ()
1422 "Return the list of marked files."
1423 (mapcar
1424 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1425 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1427 (defun vc-dir-marked-only-files ()
1428 "Return the list of marked files, For marked directories return child files."
1429 (let ((crt (ewoc-nth vc-ewoc 0))
1430 result)
1431 (while crt
1432 (let ((crt-data (ewoc-data crt)))
1433 (if (vc-dir-fileinfo->marked crt-data)
1434 (if (vc-dir-fileinfo->directory crt-data)
1435 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1436 (dirlen (length dir))
1437 data)
1438 (while
1439 (and (setq crt (ewoc-next vc-ewoc crt))
1440 (string-equal
1441 (substring
1442 (progn
1443 (setq data (ewoc-data crt))
1444 (let ((crtdir (vc-dir-fileinfo->directory data)))
1445 (if crtdir
1446 crtdir
1447 (file-name-directory
1448 (expand-file-name
1449 (vc-dir-fileinfo->name data))))))
1450 0 dirlen)
1451 dir))
1452 (unless (vc-dir-fileinfo->directory data)
1453 (push (vc-dir-fileinfo->name data) result))))
1454 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1455 (setq crt (ewoc-next vc-ewoc crt)))
1456 (setq crt (ewoc-next vc-ewoc crt)))))
1457 result))
1459 (defun vc-directory-resynch-file (&optional fname)
1460 "Update the entries for FILE in any directory buffers that list it."
1461 (let ((file (or fname (expand-file-name buffer-file-name))))
1462 ;; The VC-Dired case
1463 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
1464 (when buffers
1465 (mapc (lambda (buffer)
1466 (with-current-buffer buffer
1467 (when (dired-goto-file file)
1468 ;; bind vc-dired-terse-mode to nil so that
1469 ;; files won't vanish when they are checked in
1470 (let ((vc-dired-terse-mode nil))
1471 (dired-do-redisplay 1)))))
1472 buffers)))
1473 ;; The vc-dir case
1474 (let ((found-vc-dir-buf nil))
1475 (save-excursion
1476 (dolist (status-buf (buffer-list))
1477 (set-buffer status-buf)
1478 ;; look for a vc-dir buffer that might show this file.
1479 (when (eq major-mode 'vc-dir-mode)
1480 (setq found-vc-dir-buf t)
1481 (let ((ddir (expand-file-name default-directory)))
1482 ;; This test is cvs-string-prefix-p
1483 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
1484 (let*
1485 ((file-short (substring file (length ddir)))
1486 (state
1487 (funcall (vc-client-object->file-to-state vc-client-mode)
1488 file))
1489 (extra
1490 (funcall (vc-client-object->file-to-extra vc-client-mode)
1491 file))
1492 (entry
1493 (list file-short state extra)))
1494 (vc-dir-update (list entry) status-buf))))))
1495 ;; We didn't find any vc-dir buffers, remove the hook, it is
1496 ;; not needed.
1497 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-directory-resynch-file))))))
1499 (defun vc-dir-mode (client-object)
1500 "Major mode for showing the VC status for a directory.
1501 Marking/Unmarking key bindings and actions:
1502 m - marks a file/directory or if the region is active, mark all the files
1503 in region.
1504 Restrictions: - a file cannot be marked if any parent directory is marked
1505 - a directory cannot be marked if any child file or
1506 directory is marked
1507 u - marks a file/directory or if the region is active, unmark all the files
1508 in region.
1509 M - if the cursor is on a file: mark all the files with the same VC state as
1510 the current file
1511 - if the cursor is on a directory: mark all child files
1512 - with a prefix argument: mark all files
1513 U - if the cursor is on a file: unmark all the files with the same VC state
1514 as the current file
1515 - if the cursor is on a directory: unmark all child files
1516 - with a prefix argument: unmark all files
1519 \\{vc-dir-mode-map}"
1520 (setq mode-name (vc-client-object->name client-object))
1521 (setq major-mode 'vc-dir-mode)
1522 (setq buffer-read-only t)
1523 (use-local-map vc-dir-mode-map)
1524 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
1525 (set (make-local-variable 'vc-client-mode) client-object)
1526 (let ((buffer-read-only nil))
1527 (erase-buffer)
1528 (set (make-local-variable 'vc-dir-process-buffer) nil)
1529 (set (make-local-variable 'vc-ewoc)
1530 (ewoc-create (vc-client-object->file-to-info client-object)
1531 (vc-client-object->headers client-object)))
1532 (add-hook 'after-save-hook 'vc-directory-resynch-file)
1533 ;; Make sure that if the VC status buffer is killed, the update
1534 ;; process running in the background is also killed.
1535 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1536 (funcall (vc-client-object->updater client-object)))
1537 (run-hooks 'vc-dir-mode-hook))
1539 (put 'vc-dir-mode 'mode-class 'special)
1541 (defun vc-buffer-sync (&optional not-urgent)
1542 "Make sure the current buffer and its working file are in sync.
1543 NOT-URGENT means it is ok to continue if the user says not to save."
1544 (when (buffer-modified-p)
1545 (if (or vc-suppress-confirm
1546 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1547 (save-buffer)
1548 (unless not-urgent
1549 (error "Aborted")))))
1551 (defun vc-dispatcher-browsing ()
1552 "Are we in a directory browser buffer?"
1553 (or vc-dired-mode (eq major-mode 'vc-dir-mode)))
1555 (defun vc-dispatcher-selection-set (eligible
1556 &optional
1557 allow-directory-wildcard
1558 allow-ineligible
1559 include-files-not-directories)
1560 "Deduce a set of files to which to apply an operation. Return the fileset.
1561 If we're in VC-dired mode, the fileset is the list of marked files.
1562 Otherwise, if we're looking at a buffer for which ELIGIBLE returns non-NIL,
1563 the fileset is a singleton containing this file.
1564 If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on
1565 and we're in a dired buffer, select the current directory.
1566 If none of these conditions is met, but ALLOW-INELIGIBLE is on and the
1567 visited file is not registered, return a singleton fileset containing it.
1568 If INCLUDE-FILES-NOT-DIRECTORIES then if directories are marked,
1569 return the list of VC files in those directories instead of
1570 the directories themselves.
1571 Otherwise, throw an error."
1572 (let ((files
1573 (cond
1574 ;; Browsing with dired
1575 (vc-dired-mode
1576 (let ((marked (dired-map-over-marks (dired-get-filename) nil)))
1577 (if marked
1578 marked
1579 (error "No files have been selected."))))
1580 ;; Browsing with vc-dir
1581 ((eq major-mode 'vc-dir-mode)
1583 (if include-files-not-directories
1584 (vc-dir-marked-only-files)
1585 (vc-dir-marked-files))
1586 (list (vc-dir-current-file))))
1587 ;; Visiting an eligible file
1588 ((funcall eligible buffer-file-name)
1589 (list buffer-file-name))
1590 ;; No eligible file -- if there's a parent buffer, deuce from there
1591 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1592 (with-current-buffer vc-parent-buffer
1593 (vc-dispatcher-browsing))))
1594 (progn
1595 (set-buffer vc-parent-buffer)
1596 (vc-dispatcher-selection-set eligible)))
1597 ;; No parent buffer, we may want to select entire directory
1599 ;; This is guarded by an enabling arg so users won't potentially
1600 ;; shoot themselves in the foot by modifying a fileset they can't
1601 ;; verify by eyeball. Allow it for nondestructive commands like
1602 ;; making diffs, or possibly for destructive ones that have
1603 ;; confirmation prompts.
1604 ((and allow-directory-wildcard
1605 ;; I think this is a misfeature. For now, I'll leave it in, but
1606 ;; I'll disable it anywhere else than in dired buffers. --Stef
1607 (and (derived-mode-p 'dired-mode)
1608 (equal buffer-file-name nil)
1609 (equal list-buffers-directory default-directory)))
1610 (progn
1611 (message "All eligible files below %s selected."
1612 default-directory)
1613 (list default-directory)))
1614 ;; Last, if we're allowing ineligible files and visiting one, select it.
1615 ((and allow-ineligible (not (eligible buffer-file-name)))
1616 (list buffer-file-name))
1617 ;; No good set here, throw error
1618 (t (error "No fileset is available here.")))))
1619 ;; We assume, in order to avoid unpleasant surprises to the user,
1620 ;; that a fileset is not in good shape to be handed to the user if the
1621 ;; buffers visting the fileset don't match the on-disk contents.
1622 (dolist (file files)
1623 (let ((visited (get-file-buffer file)))
1624 (when visited
1625 (if (vc-dispatcher-browsing)
1626 (switch-to-buffer-other-window visited)
1627 (set-buffer visited))
1628 ;; Check relation of buffer and file, and make sure
1629 ;; user knows what he's doing. First, finding the file
1630 ;; will check whether the file on disk is newer.
1631 ;; Ignore buffer-read-only during this test, and
1632 ;; preserve find-file-literally.
1633 (let ((buffer-read-only (not (file-writable-p file))))
1634 (find-file-noselect file nil find-file-literally))
1635 (if (not (verify-visited-file-modtime (current-buffer)))
1636 (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file))
1637 (write-file buffer-file-name)
1638 (error "Aborted"))
1639 ;; Now, check if we have unsaved changes.
1640 (vc-buffer-sync t)
1641 (when (buffer-modified-p)
1642 (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file))
1643 (error "Aborted")))))))
1644 files))
1646 ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
1647 ;;; vc-dispatcher.el ends here