Back out menu move, it broke something obscure.
[emacs.git] / lisp / vc-dispatcher.el
blob3bfa65f915be213ca52a4f146c3480f0d3a048f4
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 ;; The new compilation code does not use compilation-error-list any
453 ;; more, so the code below is now ineffective and might as well
454 ;; be disabled. -- Stef
455 ;; ;; We may want to reparse the compilation buffer after revert
456 ;; (reparse (and (boundp 'compilation-error-list) ;compile loaded
457 ;; ;; Construct a list; each elt is nil or a buffer
458 ;; ;; if that buffer is a compilation output buffer
459 ;; ;; that contains markers into the current buffer.
460 ;; (save-current-buffer
461 ;; (mapcar (lambda (buffer)
462 ;; (set-buffer buffer)
463 ;; (let ((errors (or
464 ;; compilation-old-error-list
465 ;; compilation-error-list))
466 ;; (buffer-error-marked-p nil))
467 ;; (while (and (consp errors)
468 ;; (not buffer-error-marked-p))
469 ;; (and (markerp (cdr (car errors)))
470 ;; (eq buffer
471 ;; (marker-buffer
472 ;; (cdr (car errors))))
473 ;; (setq buffer-error-marked-p t))
474 ;; (setq errors (cdr errors)))
475 ;; (if buffer-error-marked-p buffer)))
476 ;; (buffer-list)))))
477 (reparse nil))
478 (list point-context mark-context reparse)))
480 (defun vc-restore-buffer-context (context)
481 "Restore point/mark, and reparse any affected compilation buffers.
482 CONTEXT is that which `vc-buffer-context' returns."
483 (let ((point-context (nth 0 context))
484 (mark-context (nth 1 context))
485 ;; (reparse (nth 2 context))
487 ;; The new compilation code does not use compilation-error-list any
488 ;; more, so the code below is now ineffective and might as well
489 ;; be disabled. -- Stef
490 ;; ;; Reparse affected compilation buffers.
491 ;; (while reparse
492 ;; (if (car reparse)
493 ;; (with-current-buffer (car reparse)
494 ;; (let ((compilation-last-buffer (current-buffer)) ;select buffer
495 ;; ;; Record the position in the compilation buffer of
496 ;; ;; the last error next-error went to.
497 ;; (error-pos (marker-position
498 ;; (car (car-safe compilation-error-list)))))
499 ;; ;; Reparse the error messages as far as they were parsed before.
500 ;; (compile-reinitialize-errors '(4) compilation-parsing-end)
501 ;; ;; Move the pointer up to find the error we were at before
502 ;; ;; reparsing. Now next-error should properly go to the next one.
503 ;; (while (and compilation-error-list
504 ;; (/= error-pos (car (car compilation-error-list))))
505 ;; (setq compilation-error-list (cdr compilation-error-list))))))
506 ;; (setq reparse (cdr reparse)))
508 ;; if necessary, restore point and mark
509 (if (not (vc-context-matches-p (point) point-context))
510 (let ((new-point (vc-find-position-by-context point-context)))
511 (when new-point (goto-char new-point))))
512 (and mark-active
513 mark-context
514 (not (vc-context-matches-p (mark) mark-context))
515 (let ((new-mark (vc-find-position-by-context mark-context)))
516 (when new-mark (set-mark new-mark))))))
518 (defun vc-revert-buffer-internal (&optional arg no-confirm)
519 "Revert buffer, keeping point and mark where user expects them.
520 Try to be clever in the face of changes due to expanded version-control
521 key words. This is important for typeahead to work as expected.
522 ARG and NO-CONFIRM are passed on to `revert-buffer'."
523 (interactive "P")
524 (widen)
525 (let ((context (vc-buffer-context)))
526 ;; Use save-excursion here, because it may be able to restore point
527 ;; and mark properly even in cases where vc-restore-buffer-context
528 ;; would fail. However, save-excursion might also get it wrong --
529 ;; in this case, vc-restore-buffer-context gives it a second try.
530 (save-excursion
531 ;; t means don't call normal-mode;
532 ;; that's to preserve various minor modes.
533 (revert-buffer arg no-confirm t))
534 (vc-restore-buffer-context context)))
536 (defun vc-resynch-window (file &optional keep noquery)
537 "If FILE is in the current buffer, either revert or unvisit it.
538 The choice between revert (to see expanded keywords) and unvisit
539 depends on KEEP. NOQUERY if non-nil inhibits confirmation for
540 reverting. NOQUERY should be t *only* if it is known the only
541 difference between the buffer and the file is due to
542 modifications by the dispatcher client code, rather than user
543 editing!"
544 (and (string= buffer-file-name file)
545 (if keep
546 (progn
547 (vc-revert-buffer-internal t noquery)
548 ;; TODO: Adjusting view mode might no longer be necessary
549 ;; after RMS change to files.el of 1999-08-08. Investigate
550 ;; this when we install the new VC.
551 (and view-read-only
552 (if (file-writable-p file)
553 (and view-mode
554 (let ((view-old-buffer-read-only nil))
555 (view-mode-exit)))
556 (and (not view-mode)
557 (not (eq (get major-mode 'mode-class) 'special))
558 (view-mode-enter))))
559 (run-hook-with-args 'modeline-hook buffer-file-name))
560 (kill-buffer (current-buffer)))))
562 (defun vc-resynch-buffer (file &optional keep noquery)
563 "If FILE is currently visited, resynch its buffer."
564 (if (string= buffer-file-name file)
565 (vc-resynch-window file keep noquery)
566 (let ((buffer (get-file-buffer file)))
567 (when buffer
568 (with-current-buffer buffer
569 (vc-resynch-window file keep noquery)))))
570 (vc-directory-resynch-file file)
571 (when (memq 'vc-dir-mark-buffer-changed after-save-hook)
572 (let ((buffer (get-file-buffer file)))
573 (vc-dir-mark-buffer-changed file))))
575 ;; Command closures
577 (defun vc-start-logentry (files extra comment initial-contents msg action &optional after-hook)
578 "Accept a comment for an operation on FILES with extra data EXTRA.
579 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
580 action on close to ACTION. If COMMENT is a string and
581 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
582 contents of the log entry buffer. If COMMENT is a string and
583 INITIAL-CONTENTS is nil, do action immediately as if the user had
584 entered COMMENT. If COMMENT is t, also do action immediately with an
585 empty comment. Remember the file's buffer in `vc-parent-buffer'
586 \(current one if no file). AFTER-HOOK specifies the local value
587 for `vc-log-after-operation-hook'."
588 (let ((parent
589 (if (or (eq major-mode 'vc-dired-mode) (eq major-mode 'vc-dir-mode))
590 ;; If we are called from VC dired, the parent buffer is
591 ;; the current buffer.
592 (current-buffer)
593 (if (and files (equal (length files) 1))
594 (get-file-buffer (car files))
595 (current-buffer)))))
596 (if (and comment (not initial-contents))
597 (set-buffer (get-buffer-create "*VC-log*"))
598 (pop-to-buffer (get-buffer-create "*VC-log*")))
599 (set (make-local-variable 'vc-parent-buffer) parent)
600 (set (make-local-variable 'vc-parent-buffer-name)
601 (concat " from " (buffer-name vc-parent-buffer)))
602 (vc-log-edit files)
603 (make-local-variable 'vc-log-after-operation-hook)
604 (when after-hook
605 (setq vc-log-after-operation-hook after-hook))
606 (setq vc-log-operation action)
607 (setq vc-log-extra extra)
608 (when comment
609 (erase-buffer)
610 (when (stringp comment) (insert comment)))
611 (if (or (not comment) initial-contents)
612 (message "%s Type C-c C-c when done" msg)
613 (vc-finish-logentry (eq comment t)))))
615 (defun vc-finish-logentry (&optional nocomment)
616 "Complete the operation implied by the current log entry.
617 Use the contents of the current buffer as a check-in or registration
618 comment. If the optional arg NOCOMMENT is non-nil, then don't check
619 the buffer contents as a comment."
620 (interactive)
621 ;; Check and record the comment, if any.
622 (unless nocomment
623 (run-hooks 'vc-logentry-check-hook))
624 ;; Sync parent buffer in case the user modified it while editing the comment.
625 ;; But not if it is a vc-dired buffer.
626 (with-current-buffer vc-parent-buffer
627 (or vc-dired-mode (eq major-mode 'vc-dir-mode) (vc-buffer-sync)))
628 (unless vc-log-operation
629 (error "No log operation is pending"))
630 ;; save the parameters held in buffer-local variables
631 (let ((log-operation vc-log-operation)
632 (log-fileset vc-log-fileset)
633 (log-extra vc-log-extra)
634 (log-entry (buffer-string))
635 (after-hook vc-log-after-operation-hook)
636 (tmp-vc-parent-buffer vc-parent-buffer))
637 (pop-to-buffer vc-parent-buffer)
638 ;; OK, do it to it
639 (save-excursion
640 (funcall log-operation
641 log-fileset
642 log-extra
643 log-entry))
644 ;; Remove checkin window (after the checkin so that if that fails
645 ;; we don't zap the *VC-log* buffer and the typing therein).
646 ;; -- IMO this should be replaced with quit-window
647 (let ((logbuf (get-buffer "*VC-log*")))
648 (cond ((and logbuf vc-delete-logbuf-window)
649 (delete-windows-on logbuf (selected-frame))
650 ;; Kill buffer and delete any other dedicated windows/frames.
651 (kill-buffer logbuf))
652 (logbuf (pop-to-buffer "*VC-log*")
653 (bury-buffer)
654 (pop-to-buffer tmp-vc-parent-buffer))))
655 ;; Now make sure we see the expanded headers
656 (when log-fileset
657 (mapc
658 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
659 log-fileset))
660 (when vc-dired-mode
661 (dired-move-to-filename))
662 (when (eq major-mode 'vc-dir-mode)
663 (vc-dir-move-to-goal-column))
664 (run-hooks after-hook 'vc-finish-logentry-hook)))
666 ;; VC-Dired mode
667 ;; FIXME: to be removed when vc-dir support is finished
669 (defcustom vc-dired-listing-switches "-al"
670 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
671 :type 'string
672 :group 'vc
673 :version "21.1")
675 (defcustom vc-dired-recurse t
676 "If non-nil, show directory trees recursively in VC Dired."
677 :type 'boolean
678 :group 'vc
679 :version "20.3")
681 (defcustom vc-dired-terse-display t
682 "If non-nil, show only locked or locally modified files in VC Dired."
683 :type 'boolean
684 :group 'vc
685 :version "20.3")
687 (defvar vc-dired-mode nil)
688 (defvar vc-dired-window-configuration)
689 (defvar vc-dired-switches)
690 (defvar vc-dired-terse-mode)
692 (make-variable-buffer-local 'vc-dired-mode)
694 (defvar vc-dired-mode-map
695 (let ((map (make-sparse-keymap))
696 (vmap (make-sparse-keymap)))
697 (define-key map "\C-xv" vmap)
698 (define-key map "v" vmap)
699 (set-keymap-parent vmap vc-prefix-map)
700 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
701 map))
703 (define-derived-mode vc-dired-mode dired-mode "Dired under VC"
704 "The major mode used in VC directory buffers.
706 It works like Dired, but lists only files under version control, with
707 the current VC state of each file being indicated in the place of the
708 file's link count, owner, group and size. Subdirectories are also
709 listed, and you may insert them into the buffer as desired, like in
710 Dired.
712 All Dired commands operate normally, with the exception of `v', which
713 is redefined as the version control prefix, so that you can type
714 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
715 the file named in the current Dired buffer line. `vv' invokes
716 `vc-next-action' on this file, or on all files currently marked.
717 There is a special command, `*l', to mark all files currently locked."
718 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
719 ;; We do it here because dired might not be loaded yet
720 ;; when vc-dired-mode-map is initialized.
721 (set-keymap-parent vc-dired-mode-map dired-mode-map)
722 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
723 ;; The following is slightly modified from files.el,
724 ;; because file lines look a bit different in vc-dired-mode
725 ;; (the column before the date does not end in a digit).
726 ;; albinus: It should be done in the original declaration. Problem
727 ;; is the optional empty state-info; otherwise ")" would be good
728 ;; enough as delimeter.
729 (set (make-local-variable 'directory-listing-before-filename-regexp)
730 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
731 ;; In some locales, month abbreviations are as short as 2 letters,
732 ;; and they can be followed by ".".
733 (month (concat l l "+\\.?"))
734 (s " ")
735 (yyyy "[0-9][0-9][0-9][0-9]")
736 (dd "[ 0-3][0-9]")
737 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
738 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
739 (zone "[-+][0-2][0-9][0-5][0-9]")
740 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
741 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
742 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
743 "\\|" yyyy "-" iso-mm-dd "\\)"))
744 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
745 s "+"
746 "\\(" HH:MM "\\|" yyyy "\\)"))
747 (western-comma (concat month s "+" dd "," s "+" yyyy))
748 ;; Japanese MS-Windows ls-lisp has one-digit months, and
749 ;; omits the Kanji characters after month and day-of-month.
750 (mm "[ 0-1]?[0-9]")
751 (japanese
752 (concat mm l "?" s dd l "?" s "+"
753 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
754 ;; the .* below ensures that we find the last match on a line
755 (concat ".*" s
756 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
757 s "+")))
758 (and (boundp 'vc-dired-switches)
759 vc-dired-switches
760 (set (make-local-variable 'dired-actual-switches)
761 vc-dired-switches))
762 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
763 ;;(let ((backend-name (symbol-name (vc-responsible-backend
764 ;; default-directory))))
765 ;; (setq mode-name (concat mode-name backend-name))
766 ;; ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
767 ;; (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
768 ;; (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
769 ;; (cons backend-name vc-dire-menu-map) 'subdir)))
770 (setq vc-dired-mode t))
772 (defun vc-dired-toggle-terse-mode ()
773 "Toggle terse display in VC Dired."
774 (interactive)
775 (if (not vc-dired-mode)
777 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
778 (if vc-dired-terse-mode
779 (vc-dired-hook)
780 (revert-buffer))))
782 (defun vc-dired-mark-locked ()
783 "Mark all files currently locked."
784 (interactive)
785 (dired-mark-if (let ((f (dired-get-filename nil t)))
786 (and f
787 (not (file-directory-p f))
788 (not (vc-up-to-date-p f))))
789 "locked file"))
791 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
793 (defun vc-dired-reformat-line (vc-info)
794 "Reformat a directory-listing line.
795 Replace various columns with version control information, VC-INFO.
796 This code, like dired, assumes UNIX -l format."
797 (beginning-of-line)
798 (when (re-search-forward
799 ;; Match link count, owner, group, size. Group may be missing,
800 ;; and only the size is present in OS/2 -l format.
801 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
802 (line-end-position) t)
803 (replace-match (substring (concat vc-info " ") 0 10)
804 t t nil 1)))
806 (defun vc-dired-ignorable-p (filename)
807 "Should FILENAME be ignored in VC-Dired listings?"
808 (catch t
809 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
810 (dolist (ignorable completion-ignored-extensions)
811 (let ((ext (substring filename
812 (- (length filename)
813 (length ignorable)))))
814 (if (string= ignorable ext) (throw t t))))
815 ;; Ignore Makefiles derived from something else
816 (when (string= (file-name-nondirectory filename) "Makefile")
817 (let* ((dir (file-name-directory filename))
818 (peers (directory-files (or dir default-directory))))
819 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
820 (throw t t))))
821 nil))
823 (defun vc-dired-purge ()
824 "Remove empty subdirs."
825 (goto-char (point-min))
826 (while (dired-get-subdir)
827 (forward-line 2)
828 (if (dired-get-filename nil t)
829 (if (not (dired-next-subdir 1 t))
830 (goto-char (point-max)))
831 (forward-line -2)
832 (if (not (string= (dired-current-directory) default-directory))
833 (dired-do-kill-lines t "")
834 ;; We cannot remove the top level directory.
835 ;; Just make it look a little nicer.
836 (forward-line 1)
837 (or (eobp) (kill-line))
838 (if (not (dired-next-subdir 1 t))
839 (goto-char (point-max))))))
840 (goto-char (point-min)))
842 (defun vc-dired-buffers-for-dir (dir)
843 "Return a list of all vc-dired buffers that currently display DIR."
844 (let (result)
845 ;; Check whether dired is loaded.
846 (when (fboundp 'dired-buffers-for-dir)
847 (dolist (buffer (dired-buffers-for-dir dir))
848 (with-current-buffer buffer
849 (when vc-dired-mode
850 (push buffer result)))))
851 (nreverse result)))
853 (defun vc-directory-resynch-file (file)
854 "Update the entries for FILE in any VC Dired buffers that list it."
855 ;;FIXME This needs to be implemented so it works for vc-dir
856 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
857 (when buffers
858 (mapcar (lambda (buffer)
859 (with-current-buffer buffer
860 (when (dired-goto-file file)
861 ;; bind vc-dired-terse-mode to nil so that
862 ;; files won't vanish when they are checked in
863 (let ((vc-dired-terse-mode nil))
864 (dired-do-redisplay 1)))))
865 buffers))))
867 ;;;###autoload
868 (defun vc-directory (dir read-switches)
869 "Create a buffer in VC Dired Mode for directory DIR.
871 See Info node `VC Dired Mode'.
873 With prefix arg READ-SWITCHES, specify a value to override
874 `dired-listing-switches' when generating the listing."
875 (interactive "DDired under VC (directory): \nP")
876 (let ((vc-dired-switches (concat vc-dired-listing-switches
877 (if vc-dired-recurse "R" ""))))
878 (if read-switches
879 (setq vc-dired-switches
880 (read-string "Dired listing switches: "
881 vc-dired-switches)))
882 (require 'dired)
883 (require 'dired-aux)
884 (switch-to-buffer
885 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
886 vc-dired-switches
887 'vc-dired-mode))))
889 ;; The ewoc-based vc-directory implementation
891 (defcustom vc-dir-mode-hook nil
892 "Normal hook run by `vc-dir-mode'.
893 See `run-hooks'."
894 :type 'hook
895 :group 'vc)
897 ;; Used to store information for the files displayed in the *VC status* buffer.
898 ;; Each item displayed corresponds to one of these defstructs.
899 (defstruct (vc-dir-fileinfo
900 (:copier nil)
901 (:type list) ;So we can use `member' on lists of FIs.
902 (:constructor
903 ;; We could define it as an alias for `list'.
904 vc-dir-create-fileinfo (name state &optional extra marked directory))
905 (:conc-name vc-dir-fileinfo->))
906 name ;Keep it as first, for `member'.
907 state
908 ;; For storing client-mode specific information.
909 extra
910 marked
911 ;; To keep track of not updated files during a global refresh
912 needs-update
913 ;; To distinguish files and directories.
914 directory)
916 ;; Used to describe a dispatcher client mode.
917 (defstruct (vc-client-object
918 (:copier nil)
919 (:constructor
920 vc-create-client-object (name
921 headers
922 file-to-info
923 file-to-state
924 file-to-extra
925 updater))
926 (:conc-name vc-client-object->))
927 name
928 headers
929 file-to-info
930 file-to-state
931 file-to-extra
932 updater)
934 (defvar vc-ewoc nil)
935 (defvar vc-dir-process-buffer nil
936 "The buffer used for the asynchronous call that computes the VC status.")
938 (defun vc-dir-move-to-goal-column ()
939 ;; Used to keep the cursor on the file name column.
940 (beginning-of-line)
941 ;; Must be in sync with vc-default-status-printer.
942 (forward-char 25))
944 (defun vc-dir-prepare-status-buffer (dir &optional create-new)
945 "Find a *vc-dir* buffer showing DIR, or create a new one."
946 (setq dir (expand-file-name dir))
947 (let* ((bname "*vc-dir*")
948 ;; Look for another *vc-dir* buffer visiting the same directory.
949 (buf (save-excursion
950 (unless create-new
951 (dolist (buffer (buffer-list))
952 (set-buffer buffer)
953 (when (and (eq major-mode 'vc-dir-mode)
954 (string= (expand-file-name default-directory) dir))
955 (return buffer)))))))
956 (or buf
957 ;; Create a new *vc-dir* buffer.
958 (with-current-buffer (create-file-buffer bname)
959 (cd dir)
960 (vc-setup-buffer (current-buffer))
961 ;; Reset the vc-parent-buffer-name so that it does not appear
962 ;; in the mode-line.
963 (setq vc-parent-buffer-name nil)
964 (current-buffer)))))
966 (defvar vc-dir-menu-map
967 (let ((map (make-sparse-keymap "VC-dir")))
968 (define-key map [quit]
969 '(menu-item "Quit" quit-window
970 :help "Quit"))
971 (define-key map [kill]
972 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
973 :enable (vc-dir-busy)
974 :help "Kill the command that updates VC status buffer"))
975 (define-key map [refresh]
976 '(menu-item "Refresh" vc-dir-refresh
977 :enable (not (vc-dir-busy))
978 :help "Refresh the contents of the VC status buffer"))
979 ;; Movement.
980 (define-key map [sepmv] '("--"))
981 (define-key map [next-line]
982 '(menu-item "Next line" vc-dir-next-line
983 :help "Go to the next line" :keys "n"))
984 (define-key map [previous-line]
985 '(menu-item "Previous line" vc-dir-previous-line
986 :help "Go to the previous line"))
987 ;; Marking.
988 (define-key map [sepmrk] '("--"))
989 (define-key map [unmark-all]
990 '(menu-item "Unmark All" vc-dir-unmark-all-files
991 :help "Unmark all files that are in the same state as the current file\
992 \nWith prefix argument unmark all files"))
993 (define-key map [unmark-previous]
994 '(menu-item "Unmark previous " vc-dir-unmark-file-up
995 :help "Move to the previous line and unmark the file"))
997 (define-key map [mark-all]
998 '(menu-item "Mark All" vc-dir-mark-all-files
999 :help "Mark all files that are in the same state as the current file\
1000 \nWith prefix argument mark all files"))
1001 (define-key map [unmark]
1002 '(menu-item "Unmark" vc-dir-unmark
1003 :help "Unmark the current file or all files in the region"))
1005 (define-key map [mark]
1006 '(menu-item "Mark" vc-dir-mark
1007 :help "Mark the current file or all files in the region"))
1009 (define-key map [sepopn] '("--"))
1010 (define-key map [open-other]
1011 '(menu-item "Open in other window" vc-dir-find-file-other-window
1012 :help "Find the file on the current line, in another window"))
1013 (define-key map [open]
1014 '(menu-item "Open file" vc-dir-find-file
1015 :help "Find the file on the current line"))
1016 ;; FIXME: Stuff starting here should be appended by vc
1017 ;; VC info details
1018 (define-key map [sepvcdet] '("--"))
1019 (define-key map [remup]
1020 '(menu-item "Hide up-to-date" vc-dir-hide-up-to-date
1021 :help "Hide up-to-date items from display"))
1022 ;; FIXME: This needs a key binding. And maybe a better name
1023 ;; ("Insert" like PCL-CVS uses does not sound that great either)...
1024 (define-key map [ins]
1025 '(menu-item "Show File" vc-dir-show-fileentry
1026 :help "Show a file in the VC status listing even though it might be up to date"))
1027 (define-key map [annotate]
1028 '(menu-item "Annotate" vc-annotate
1029 :help "Display the edit history of the current file using colors"))
1030 (define-key map [diff]
1031 '(menu-item "Compare with Base Version" vc-diff
1032 :help "Compare file set with the base version"))
1033 (define-key map [log]
1034 '(menu-item "Show history" vc-print-log
1035 :help "List the change log of the current file set in a window"))
1036 ;; VC commands.
1037 (define-key map [sepvccmd] '("--"))
1038 (define-key map [update]
1039 '(menu-item "Update to latest version" vc-update
1040 :help "Update the current fileset's files to their tip revisions"))
1041 (define-key map [revert]
1042 '(menu-item "Revert to base version" vc-revert
1043 :help "Revert working copies of the selected fileset to their repository contents."))
1044 (define-key map [next-action]
1045 ;; FIXME: This really really really needs a better name!
1046 ;; And a key binding too.
1047 '(menu-item "Check In/Out" vc-next-action
1048 :help "Do the next logical version control operation on the current fileset"))
1049 (define-key map [register]
1050 '(menu-item "Register" vc-dir-register
1051 :help "Register file set into the version control system"))
1052 map)
1053 "Menu for VC status")
1055 (defalias 'vc-dir-menu-map vc-dir-menu-map)
1057 (defvar vc-dir-mode-map
1058 (let ((map (make-keymap)))
1059 (suppress-keymap map)
1060 ;; Marking.
1061 (define-key map "m" 'vc-dir-mark)
1062 (define-key map "M" 'vc-dir-mark-all-files)
1063 (define-key map "u" 'vc-dir-unmark)
1064 (define-key map "U" 'vc-dir-unmark-all-files)
1065 (define-key map "\C-?" 'vc-dir-unmark-file-up)
1066 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
1067 ;; Movement.
1068 (define-key map "n" 'vc-dir-next-line)
1069 (define-key map " " 'vc-dir-next-line)
1070 (define-key map "\t" 'vc-dir-next-line)
1071 (define-key map "p" 'vc-dir-previous-line)
1072 (define-key map [backtab] 'vc-dir-previous-line)
1073 ;; The remainder.
1074 (define-key map "f" 'vc-dir-find-file)
1075 (define-key map "\C-m" 'vc-dir-find-file)
1076 (define-key map "o" 'vc-dir-find-file-other-window)
1077 (define-key map "q" 'quit-window)
1078 (define-key map "g" 'vc-dir-refresh)
1079 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
1080 (define-key map [(down-mouse-3)] 'vc-dir-menu)
1081 (define-key map [(mouse-2)] 'vc-dir-toggle-mark)
1083 ;; FIXME: Calls back into vc.el
1084 ;; Hook up the menu.
1085 (define-key map [menu-bar vc-dir-mode]
1086 '(menu-item
1087 ;; This is used so that client modes can add mode-specific
1088 ;; menu items to vc-dir-menu-map.
1089 "VC Status" vc-dir-menu-map :filter vc-dir-menu-map-filter))
1090 map)
1091 "Keymap for VC status")
1093 (defmacro vc-at-event (event &rest body)
1094 "Evaluate `body' wich point located at event-start of `event'.
1095 If `body' uses `event', it should be a variable,
1096 otherwise it will be evaluated twice."
1097 (let ((posn (gensym "vc-at-event-posn")))
1098 `(let ((,posn (event-start ,event)))
1099 (save-excursion
1100 (set-buffer (window-buffer (posn-window ,posn)))
1101 (goto-char (posn-point ,posn))
1102 ,@body))))
1104 (defun vc-dir-menu (e)
1105 "Popup the VC status menu."
1106 (interactive "e")
1107 (vc-at-event e (popup-menu vc-dir-menu-map e)))
1109 (defvar vc-dir-tool-bar-map
1110 (let ((map (make-sparse-keymap)))
1111 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
1112 map vc-dir-mode-map)
1113 (tool-bar-local-item "bookmark_add"
1114 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
1115 :help "Toggle mark on current item")
1116 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
1117 map vc-dir-mode-map
1118 :rtl "right-arrow")
1119 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
1120 map vc-dir-mode-map
1121 :rtl "left-arrow")
1122 (tool-bar-local-item-from-menu 'vc-print-log "info"
1123 map vc-dir-mode-map)
1124 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
1125 map vc-dir-mode-map)
1126 (tool-bar-local-item-from-menu 'nonincremental-search-forward
1127 "search" map)
1128 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
1129 map vc-dir-mode-map)
1130 (tool-bar-local-item-from-menu 'quit-window "exit"
1131 map vc-dir-mode-map)
1132 map))
1134 ;; t if directories should be shown in vc-dir.
1135 ;; WORK IN PROGRESS! DO NOT SET this! ONLY set it if you want to help
1136 ;; write code for this feature. This variable will likely disappear
1137 ;; when the work is done.
1138 (defvar vc-dir-insert-directories nil)
1140 (defun vc-dir-update (entries buffer &optional noinsert)
1141 "Update BUFFER's ewoc from the list of ENTRIES.
1142 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
1143 ;; Add ENTRIES to the vc-dir buffer BUFFER.
1144 (with-current-buffer buffer
1145 ;; Insert the entries sorted by name into the ewoc.
1146 ;; We assume the ewoc is sorted too, which should be the
1147 ;; case if we always add entries with vc-dir-update.
1148 (setq entries
1149 ;; Sort: first files and then subdirectories.
1150 ;; XXX: this is VERY inefficient, it computes the directory
1151 ;; names too many times
1152 (sort entries
1153 (lambda (entry1 entry2)
1154 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
1155 (dir2 (file-name-directory (expand-file-name (car entry2)))))
1156 (cond
1157 ((string< dir1 dir2) t)
1158 ((not (string= dir1 dir2)) nil)
1159 ((string< (car entry1) (car entry2))))))))
1160 (if (not vc-dir-insert-directories)
1161 (let ((entry (car entries))
1162 (node (ewoc-nth vc-ewoc 0)))
1163 (while (and entry node)
1164 (let ((entryfile (car entry))
1165 (nodefile (vc-dir-fileinfo->name (ewoc-data node))))
1166 (cond
1167 ((string-lessp nodefile entryfile)
1168 (setq node (ewoc-next vc-ewoc node)))
1169 ((string-lessp entryfile nodefile)
1170 (unless noinsert
1171 (ewoc-enter-before vc-ewoc node
1172 (apply 'vc-dir-create-fileinfo entry)))
1173 (setq entries (cdr entries) entry (car entries)))
1175 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
1176 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
1177 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
1178 (ewoc-invalidate vc-ewoc node)
1179 (setq entries (cdr entries) entry (car entries))
1180 (setq node (ewoc-next vc-ewoc node))))))
1181 (unless (or node noinsert)
1182 ;; We're past the last node, all remaining entries go to the end.
1183 (while entries
1184 (ewoc-enter-last vc-ewoc
1185 (apply 'vc-dir-create-fileinfo (pop entries))))))
1186 ;; Insert directory entries in the right places.
1187 (let ((entry (car entries))
1188 (node (ewoc-nth vc-ewoc 0)))
1189 ;; Insert . if it is not present.
1190 (unless node
1191 (let ((rd (file-relative-name default-directory)))
1192 (ewoc-enter-last
1193 vc-ewoc (vc-dir-create-fileinfo
1194 rd nil nil nil (expand-file-name default-directory))))
1195 (setq node (ewoc-nth vc-ewoc 0)))
1197 (while (and entry node)
1198 (let* ((entryfile (car entry))
1199 (entrydir (file-name-directory (expand-file-name entryfile)))
1200 (nodedir
1201 (or (vc-dir-fileinfo->directory (ewoc-data node))
1202 (file-name-directory
1203 (expand-file-name
1204 (vc-dir-fileinfo->name (ewoc-data node)))))))
1205 (cond
1206 ;; First try to find the directory.
1207 ((string-lessp nodedir entrydir)
1208 (setq node (ewoc-next vc-ewoc node)))
1209 ((string-equal nodedir entrydir)
1210 ;; Found the directory, find the place for the file name.
1211 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
1212 (cond
1213 ((string-lessp nodefile entryfile)
1214 (setq node (ewoc-next vc-ewoc node)))
1215 ((string-equal nodefile entryfile)
1216 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
1217 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
1218 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
1219 (ewoc-invalidate vc-ewoc node)
1220 (setq entries (cdr entries) entry (car entries))
1221 (setq node (ewoc-next vc-ewoc node)))
1223 (ewoc-enter-before vc-ewoc node
1224 (apply 'vc-dir-create-fileinfo entry))
1225 (setq entries (cdr entries) entry (car entries))))))
1227 ;; We need to insert a directory node
1228 (let ((rd (file-relative-name entrydir)))
1229 (ewoc-enter-last
1230 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))
1231 ;; Now insert the node itself.
1232 (ewoc-enter-before vc-ewoc node
1233 (apply 'vc-dir-create-fileinfo entry))
1234 (setq entries (cdr entries) entry (car entries))))))
1235 ;; We're past the last node, all remaining entries go to the end.
1236 (unless (or node noinsert)
1237 (let* ((lastnode (ewoc-nth vc-ewoc -1))
1238 (lastdir
1239 (or (vc-dir-fileinfo->directory (ewoc-data lastnode))
1240 (file-name-directory
1241 (expand-file-name
1242 (vc-dir-fileinfo->name (ewoc-data lastnode)))))))
1243 (dolist (entry entries)
1244 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
1245 ;; Insert a directory node if needed.
1246 (unless (string-equal lastdir entrydir)
1247 (setq lastdir entrydir)
1248 (let ((rd (file-relative-name entrydir)))
1249 (ewoc-enter-last
1250 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
1251 ;; Now insert the node itself.
1252 (ewoc-enter-last vc-ewoc
1253 (apply 'vc-dir-create-fileinfo entry))))))))))
1255 (defun vc-dir-busy ()
1256 (and (buffer-live-p vc-dir-process-buffer)
1257 (get-buffer-process vc-dir-process-buffer)))
1259 (defun vc-dir-kill-dir-status-process ()
1260 "Kill the temporary buffer and associated process."
1261 (interactive)
1262 (when (buffer-live-p vc-dir-process-buffer)
1263 (let ((proc (get-buffer-process vc-dir-process-buffer)))
1264 (when proc (delete-process proc))
1265 (setq vc-dir-process-buffer nil)
1266 (setq mode-line-process nil))))
1268 (defun vc-dir-kill-query ()
1269 ;; Make sure that when the VC status buffer is killed the update
1270 ;; process running in background is also killed.
1271 (if (vc-dir-busy)
1272 (when (y-or-n-p "Status update process running, really kill status buffer?")
1273 (vc-dir-kill-dir-status-process)
1277 (defun vc-dir-next-line (arg)
1278 "Go to the next line.
1279 If a prefix argument is given, move by that many lines."
1280 (interactive "p")
1281 (ewoc-goto-next vc-ewoc arg)
1282 (vc-dir-move-to-goal-column))
1284 (defun vc-dir-previous-line (arg)
1285 "Go to the previous line.
1286 If a prefix argument is given, move by that many lines."
1287 (interactive "p")
1288 (ewoc-goto-prev vc-ewoc arg)
1289 (vc-dir-move-to-goal-column))
1291 (defun vc-dir-mark-unmark (mark-unmark-function)
1292 (if (use-region-p)
1293 (let ((firstl (line-number-at-pos (region-beginning)))
1294 (lastl (line-number-at-pos (region-end))))
1295 (save-excursion
1296 (goto-char (region-beginning))
1297 (while (<= (line-number-at-pos) lastl)
1298 (funcall mark-unmark-function))))
1299 (funcall mark-unmark-function)))
1301 (defun vc-dir-parent-marked-p (arg)
1302 (when vc-dir-insert-directories
1303 ;; Return nil if none of the parent directories of arg is marked.
1304 (let* ((argdata (ewoc-data arg))
1305 (argdir
1306 (let ((crtdir (vc-dir-fileinfo->directory argdata)))
1307 (if crtdir
1308 crtdir
1309 (file-name-directory (expand-file-name
1310 (vc-dir-fileinfo->name argdata))))))
1311 (arglen (length argdir))
1312 (crt arg)
1313 data dir)
1314 ;; Go through the predecessors, checking if any directory that is
1315 ;; a parent is marked.
1316 (while (setq crt (ewoc-prev vc-ewoc crt))
1317 (setq data (ewoc-data crt))
1318 (setq dir
1319 (let ((crtdir (vc-dir-fileinfo->directory data)))
1320 (if crtdir
1321 crtdir
1322 (file-name-directory (expand-file-name
1323 (vc-dir-fileinfo->name data))))))
1325 (when (and (vc-dir-fileinfo->directory data)
1326 (string-equal (substring argdir 0 (length dir)) dir))
1327 (when (vc-dir-fileinfo->marked data)
1328 (error "Cannot mark `%s', parent directory `%s' marked"
1329 (vc-dir-fileinfo->name argdata)
1330 (vc-dir-fileinfo->name data)))))
1331 nil)))
1333 (defun vc-dir-children-marked-p (arg)
1334 ;; Return nil if none of the children of arg is marked.
1335 (when vc-dir-insert-directories
1336 (let* ((argdata (ewoc-data arg))
1337 (argdir (vc-dir-fileinfo->directory argdata))
1338 (arglen (length argdir))
1339 (is-child t)
1340 (crt arg)
1341 data dir)
1342 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1343 (setq data (ewoc-data crt))
1344 (setq dir
1345 (let ((crtdir (vc-dir-fileinfo->directory data)))
1346 (if crtdir
1347 crtdir
1348 (file-name-directory (expand-file-name
1349 (vc-dir-fileinfo->name data))))))
1350 (if (string-equal argdir (substring dir 0 arglen))
1351 (when (vc-dir-fileinfo->marked data)
1352 (error "Cannot mark `%s', child `%s' marked"
1353 (vc-dir-fileinfo->name argdata)
1354 (vc-dir-fileinfo->name data)))
1355 ;; We are done, we got to an entry that is not a child of `arg'.
1356 (setq is-child nil)))
1357 nil)))
1359 (defun vc-dir-mark-file (&optional arg)
1360 ;; Mark ARG or the current file and move to the next line.
1361 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1362 (file (ewoc-data crt))
1363 (isdir (vc-dir-fileinfo->directory file)))
1364 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1365 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1366 (setf (vc-dir-fileinfo->marked file) t)
1367 (ewoc-invalidate vc-ewoc crt)
1368 (unless (or arg (mouse-event-p last-command-event))
1369 (vc-dir-next-line 1)))))
1371 (defun vc-dir-mark ()
1372 "Mark the current file or all files in the region.
1373 If the region is active, mark all the files in the region.
1374 Otherwise mark the file on the current line and move to the next
1375 line."
1376 (interactive)
1377 (vc-dir-mark-unmark 'vc-dir-mark-file))
1379 (defun vc-dir-mark-all-files (arg)
1380 "Mark all files with the same state as the current one.
1381 With a prefix argument mark all files.
1382 If the current entry is a directory, mark all child files.
1384 The VC commands operate on files that are on the same state.
1385 This command is intended to make it easy to select all files that
1386 share the same state."
1387 (interactive "P")
1388 (if arg
1389 ;; Mark all files.
1390 (progn
1391 ;; First check that no directory is marked, we can't mark
1392 ;; files in that case.
1393 (ewoc-map
1394 (lambda (filearg)
1395 (when (and (vc-dir-fileinfo->directory filearg)
1396 (vc-dir-fileinfo->directory filearg))
1397 (error "Cannot mark all files, directory `%s' marked"
1398 (vc-dir-fileinfo->name filearg))))
1399 vc-ewoc)
1400 (ewoc-map
1401 (lambda (filearg)
1402 (unless (vc-dir-fileinfo->marked filearg)
1403 (setf (vc-dir-fileinfo->marked filearg) t)
1405 vc-ewoc))
1406 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1407 (if (vc-dir-fileinfo->directory data)
1408 ;; It's a directory, mark child files.
1409 (let ((crt (ewoc-locate vc-ewoc)))
1410 (unless (vc-dir-children-marked-p crt)
1411 (while (setq crt (ewoc-next vc-ewoc crt))
1412 (let ((crt-data (ewoc-data crt)))
1413 (unless (vc-dir-fileinfo->directory crt-data)
1414 (setf (vc-dir-fileinfo->marked crt-data) t)
1415 (ewoc-invalidate vc-ewoc crt))))))
1416 ;; It's a file
1417 (let ((state (vc-dir-fileinfo->state data))
1418 (crt (ewoc-nth vc-ewoc 0)))
1419 (while crt
1420 (let ((crt-data (ewoc-data crt)))
1421 (when (and (not (vc-dir-fileinfo->marked crt-data))
1422 (eq (vc-dir-fileinfo->state crt-data) state)
1423 (not (vc-dir-fileinfo->directory crt-data)))
1424 (vc-dir-mark-file crt)))
1425 (setq crt (ewoc-next vc-ewoc crt))))))))
1427 (defun vc-dir-unmark-file ()
1428 ;; Unmark the current file and move to the next line.
1429 (let* ((crt (ewoc-locate vc-ewoc))
1430 (file (ewoc-data crt)))
1431 (setf (vc-dir-fileinfo->marked file) nil)
1432 (ewoc-invalidate vc-ewoc crt)
1433 (unless (mouse-event-p last-command-event)
1434 (vc-dir-next-line 1))))
1436 (defun vc-dir-unmark ()
1437 "Unmark the current file or all files in the region.
1438 If the region is active, unmark all the files in the region.
1439 Otherwise mark the file on the current line and move to the next
1440 line."
1441 (interactive)
1442 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1444 (defun vc-dir-unmark-file-up ()
1445 "Move to the previous line and unmark the file."
1446 (interactive)
1447 ;; If we're on the first line, we won't move up, but we will still
1448 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1449 ;; does.
1450 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1451 (file (ewoc-data prev)))
1452 (setf (vc-dir-fileinfo->marked file) nil)
1453 (ewoc-invalidate vc-ewoc prev)
1454 (vc-dir-move-to-goal-column)))
1456 (defun vc-dir-unmark-all-files (arg)
1457 "Unmark all files with the same state as the current one.
1458 With a prefix argument unmark all files.
1459 If the current entry is a directory, unmark all the child files.
1461 The VC commands operate on files that are on the same state.
1462 This command is intended to make it easy to deselect all files
1463 that share the same state."
1464 (interactive "P")
1465 (if arg
1466 (ewoc-map
1467 (lambda (filearg)
1468 (when (vc-dir-fileinfo->marked filearg)
1469 (setf (vc-dir-fileinfo->marked filearg) nil)
1471 vc-ewoc)
1472 (let* ((crt (ewoc-locate vc-ewoc))
1473 (data (ewoc-data crt)))
1474 (if (vc-dir-fileinfo->directory data)
1475 ;; It's a directory, unmark child files.
1476 (while (setq crt (ewoc-next vc-ewoc crt))
1477 (let ((crt-data (ewoc-data crt)))
1478 (unless (vc-dir-fileinfo->directory crt-data)
1479 (setf (vc-dir-fileinfo->marked crt-data) nil)
1480 (ewoc-invalidate vc-ewoc crt))))
1481 ;; It's a file
1482 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1483 (ewoc-map
1484 (lambda (filearg)
1485 (when (and (vc-dir-fileinfo->marked filearg)
1486 (eq (vc-dir-fileinfo->state filearg) crt-state))
1487 (setf (vc-dir-fileinfo->marked filearg) nil)
1489 vc-ewoc))))))
1491 (defun vc-dir-toggle-mark-file ()
1492 (let* ((crt (ewoc-locate vc-ewoc))
1493 (file (ewoc-data crt)))
1494 (if (vc-dir-fileinfo->marked file)
1495 (vc-dir-unmark-file)
1496 (vc-dir-mark-file))))
1498 (defun vc-dir-toggle-mark (e)
1499 (interactive "e")
1500 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1502 (defun vc-dir-delete-file ()
1503 "Delete the marked files, or the current file if no marks."
1504 (interactive)
1505 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1506 (list (vc-dir-current-file)))))
1508 (defun vc-dir-find-file ()
1509 "Find the file on the current line."
1510 (interactive)
1511 (find-file (vc-dir-current-file)))
1513 (defun vc-dir-find-file-other-window ()
1514 "Find the file on the current line, in another window."
1515 (interactive)
1516 (find-file-other-window (vc-dir-current-file)))
1518 (defun vc-dir-current-file ()
1519 (let ((node (ewoc-locate vc-ewoc)))
1520 (unless node
1521 (error "No file available."))
1522 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1524 (defun vc-dir-marked-files ()
1525 "Return the list of marked files."
1526 (mapcar
1527 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1528 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1530 (defun vc-dir-marked-only-files ()
1531 "Return the list of marked files, For marked directories return child files."
1532 (let ((crt (ewoc-nth vc-ewoc 0))
1533 result)
1534 (while crt
1535 (let ((crt-data (ewoc-data crt)))
1536 (if (vc-dir-fileinfo->marked crt-data)
1537 (if (vc-dir-fileinfo->directory crt-data)
1538 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1539 (dirlen (length dir))
1540 data)
1541 (while
1542 (and (setq crt (ewoc-next vc-ewoc crt))
1543 (string-equal
1544 (substring
1545 (progn
1546 (setq data (ewoc-data crt))
1547 (let ((crtdir (vc-dir-fileinfo->directory data)))
1548 (if crtdir
1549 crtdir
1550 (file-name-directory
1551 (expand-file-name
1552 (vc-dir-fileinfo->name data))))))
1553 0 dirlen)
1554 dir))
1555 (unless (vc-dir-fileinfo->directory data)
1556 (push (vc-dir-fileinfo->name data) result))))
1557 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1558 (setq crt (ewoc-next vc-ewoc crt)))
1559 (setq crt (ewoc-next vc-ewoc crt)))))
1560 result))
1562 (defun vc-dir-mark-buffer-changed (&optional fname)
1563 (let* ((file (or fname (expand-file-name buffer-file-name)))
1564 (found-vc-dir-buf nil))
1565 (save-excursion
1566 (dolist (status-buf (buffer-list))
1567 (set-buffer status-buf)
1568 ;; look for a vc-dir buffer that might show this file.
1569 (when (eq major-mode 'vc-dir-mode)
1570 (setq found-vc-dir-buf t)
1571 (let ((ddir (expand-file-name default-directory)))
1572 ;; This test is cvs-string-prefix-p
1573 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
1574 (let*
1575 ((file-short (substring file (length ddir)))
1576 (state
1577 (funcall (vc-client-object->file-to-state vc-client-mode)
1578 file))
1579 (extra
1580 (funcall (vc-client-object->file-to-extra vc-client-mode)
1581 file))
1582 (entry
1583 (list file-short state extra)))
1584 (vc-dir-update (list entry) status-buf))))))
1585 ;; We didn't find any vc-dir buffers, remove the hook, it is
1586 ;; not needed.
1587 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-dir-mark-buffer-changed)))))
1589 (defun vc-dir-mode (client-object)
1590 "Major mode for showing the VC status for a directory.
1591 Marking/Unmarking key bindings and actions:
1592 m - marks a file/directory or if the region is active, mark all the files
1593 in region.
1594 Restrictions: - a file cannot be marked if any parent directory is marked
1595 - a directory cannot be marked if any child file or
1596 directory is marked
1597 u - marks a file/directory or if the region is active, unmark all the files
1598 in region.
1599 M - if the cursor is on a file: mark all the files with the same VC state as
1600 the current file
1601 - if the cursor is on a directory: mark all child files
1602 - with a prefix argument: mark all files
1603 U - if the cursor is on a file: unmark all the files with the same VC state
1604 as the current file
1605 - if the cursor is on a directory: unmark all child files
1606 - with a prefix argument: unmark all files
1609 \\{vc-dir-mode-map}"
1610 (setq mode-name (vc-client-object->name client-object))
1611 (setq major-mode 'vc-dir-mode)
1612 (setq buffer-read-only t)
1613 (use-local-map vc-dir-mode-map)
1614 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
1615 (set (make-local-variable 'vc-client-mode) client-object)
1616 (let ((buffer-read-only nil))
1617 (erase-buffer)
1618 (set (make-local-variable 'vc-dir-process-buffer) nil)
1619 (set (make-local-variable 'vc-ewoc)
1620 (ewoc-create (vc-client-object->file-to-info client-object)
1621 (vc-client-object->headers client-object)))
1622 (add-hook 'after-save-hook 'vc-dir-mark-buffer-changed)
1623 ;; Make sure that if the VC status buffer is killed, the update
1624 ;; process running in the background is also killed.
1625 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1626 (funcall (vc-client-object->updater client-object)))
1627 (run-hooks 'vc-dir-mode-hook))
1629 (put 'vc-dir-mode 'mode-class 'special)
1631 (defun vc-buffer-sync (&optional not-urgent)
1632 "Make sure the current buffer and its working file are in sync.
1633 NOT-URGENT means it is ok to continue if the user says not to save."
1634 (when (buffer-modified-p)
1635 (if (or vc-suppress-confirm
1636 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1637 (save-buffer)
1638 (unless not-urgent
1639 (error "Aborted")))))
1641 (defun vc-dispatcher-browsing ()
1642 "Are we in a directory browser buffer?"
1643 (or vc-dired-mode (eq major-mode 'vc-dir-mode)))
1645 (defun vc-dispatcher-selection-set (eligible
1646 &optional
1647 allow-directory-wildcard
1648 allow-ineligible
1649 include-files-not-directories)
1650 "Deduce a set of files to which to apply an operation. Return the fileset.
1651 If we're in VC-dired mode, the fileset is the list of marked files.
1652 Otherwise, if we're looking at a buffer for which ELIGIBLE returns non-NIL,
1653 the fileset is a singleton containing this file.
1654 If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on
1655 and we're in a dired buffer, select the current directory.
1656 If none of these conditions is met, but ALLOW-INELIGIBLE is on and the
1657 visited file is not registered, return a singleton fileset containing it.
1658 If INCLUDE-FILES-NOT-DIRECTORIES then if directories are marked,
1659 return the list of VC files in those directories instead of
1660 the directories themselves.
1661 Otherwise, throw an error."
1662 (let ((files
1663 (cond
1664 ;; Browsing with dired
1665 (vc-dired-mode
1666 (let ((marked (dired-map-over-marks (dired-get-filename) nil)))
1667 (if marked
1668 marked
1669 (error "No files have been selected."))))
1670 ;; Browsing with vc-dir
1671 ((eq major-mode 'vc-dir-mode)
1673 (if include-files-not-directories
1674 (vc-dir-marked-only-files)
1675 (vc-dir-marked-files))
1676 (list (vc-dir-current-file))))
1677 ;; Visiting an eligible file
1678 ((funcall eligible buffer-file-name)
1679 (list buffer-file-name))
1680 ;; No eligible file -- if there's a parent buffer, deuce from there
1681 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1682 (with-current-buffer vc-parent-buffer
1683 (vc-dispatcher-browsing))))
1684 (progn
1685 (set-buffer vc-parent-buffer)
1686 (vc-dispatcher-selection-set eligible)))
1687 ;; No parent buffer, we may want to select entire directory
1689 ;; This is guarded by an enabling arg so users won't potentially
1690 ;; shoot themselves in the foot by modifying a fileset they can't
1691 ;; verify by eyeball. Allow it for nondestructive commands like
1692 ;; making diffs, or possibly for destructive ones that have
1693 ;; confirmation prompts.
1694 ((and allow-directory-wildcard
1695 ;; I think this is a misfeature. For now, I'll leave it in, but
1696 ;; I'll disable it anywhere else than in dired buffers. --Stef
1697 (and (derived-mode-p 'dired-mode)
1698 (equal buffer-file-name nil)
1699 (equal list-buffers-directory default-directory)))
1700 (progn
1701 (message "All eligible files below %s selected."
1702 default-directory)
1703 (list default-directory)))
1704 ;; Last, if we're allowing ineligible files and visiting one, select it.
1705 ((and allow-ineligible (not (eligible buffer-file-name)))
1706 (list buffer-file-name))
1707 ;; No good set here, throw error
1708 (t (error "No fileset is available here.")))))
1709 ;; We assume, in order to avoid unpleasant surprises to the user,
1710 ;; that a fileset is not in good shape to be handed to the user if the
1711 ;; buffers visting the fileset don't match the on-disk contents.
1712 (dolist (file files)
1713 (let ((visited (get-file-buffer file)))
1714 (when visited
1715 (if (or vc-dired-mode (eq major-mode 'vc-dir-mode))
1716 (switch-to-buffer-other-window visited)
1717 (set-buffer visited))
1718 ;; Check relation of buffer and file, and make sure
1719 ;; user knows what he's doing. First, finding the file
1720 ;; will check whether the file on disk is newer.
1721 ;; Ignore buffer-read-only during this test, and
1722 ;; preserve find-file-literally.
1723 (let ((buffer-read-only (not (file-writable-p file))))
1724 (find-file-noselect file nil find-file-literally))
1725 (if (not (verify-visited-file-modtime (current-buffer)))
1726 (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file))
1727 (write-file buffer-file-name)
1728 (error "Aborted"))
1729 ;; Now, check if we have unsaved changes.
1730 (vc-buffer-sync t)
1731 (when (buffer-modified-p)
1732 (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file))
1733 (error "Aborted")))))))
1734 files))
1736 ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
1737 ;;; vc-dispatcher.el ends here