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