Choose images dynamically.
[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
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
92d1eebf 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
92d1eebf
ER
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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
92d1eebf
ER
24
25;;; Credits:
26
27;; Designed and implemented by Eric S. Raymond, originally as part of VC mode.
dbf2c858 28;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the
821677fb 29;; vc-dir front end.
92d1eebf
ER
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.
821677fb
ER
90
91;; The interface
92;;
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).
99;;
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
c8d4f1a0
ER
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.
108;;
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.
821677fb 112
d99d06ec
ER
113;; To do:
114;;
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/...).
117;;
118;; - the *VC-log* buffer needs font-locking.
119;;
120;; - Set `vc-dir-insert-directories' to t and check what operations
121;; and backends do not support directory arguments and fix them.
122;;
123;; - vc-dir needs mouse bindings.
124;;
125;; - vc-dir needs more key bindings for VC actions.
126;;
127;; - vc-dir toolbar needs more icons.
128;;
129;; - vc-dir-next-line should not print an "end of buffer" message when
130;; invoked with the cursor on the last file.
131;;
132;; - add commands to move to the prev/next directory in vc-dir.
133;;
134;; - document vc-dir in the manual.
135;;
136
92d1eebf
ER
137(provide 'vc-dispatcher)
138
4f61cc3e
ER
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}
143
83affd96
ER
144;; General customization
145
146(defcustom vc-logentry-check-hook nil
147 "Normal hook run by `vc-finish-logentry'.
148Use this to impose your own rules on the entry in addition to any the
4f61cc3e 149dispatcher client mode imposes itself."
83affd96
ER
150 :type 'hook
151 :group 'vc)
152
17f039f3
ER
153(defcustom vc-delete-logbuf-window t
154 "If non-nil, delete the *VC-log* buffer and window after each logical action.
155If nil, bury that buffer instead.
156This is most useful if you have multiple windows on a frame and would like to
157preserve the setting."
158 :type 'boolean
159 :group 'vc)
160
161(defcustom vc-command-messages nil
162 "If non-nil, display run messages from back-end commands."
163 :type 'boolean
164 :group 'vc)
165
821677fb
ER
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)
170
83affd96 171;; Variables the user doesn't need to know about.
17f039f3 172
83affd96
ER
173(defvar vc-log-operation nil)
174(defvar vc-log-after-operation-hook nil)
175(defvar vc-log-fileset)
176(defvar vc-log-extra)
177
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)
185
92d1eebf
ER
186;; Common command execution logic
187
188(defun vc-process-filter (p s)
189 "An alternative output filter for async process P.
190One difference with the default filter is that this inserts S after markers.
191Another 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))))))))
201
7265c6e8
ER
202(defun vc-setup-buffer (buf)
203 "Prepare BUF for executing a slave command and make it current."
92d1eebf
ER
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))))
215
216(defvar vc-sentinel-movepoint) ;Dynamically scoped.
217
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))))))))))
251
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"))))
258
259(defun vc-exec-after (code)
260 "Eval CODE when the current buffer's process is done.
261If the current buffer has no process, just evaluate CODE.
262Else, 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)
289
290(defvar vc-post-command-functions nil
291 "Hook run at the end of `vc-do-command'.
292Each function is called inside the buffer in which the command was run
293and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
294
295(defvar w32-quote-process-args)
296
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 " ")))
301
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.
305Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
306current buffer if BUFFER is t. If the destination buffer is not
307already current, set it up properly and erase it. The command is
308considered successful if its exit status does not exceed OKSTATUS (if
309OKSTATUS is nil, that means to ignore error status, if it is `async', that
310means not to wait for termination of the subprocess; if it is t it means to
311ignore all execution errors). FILE-OR-LIST is the name of a working file;
312it may be a list of files or be nil (to execute commands that don't expect
313a file name or set of files). If an optional list of FLAGS is present,
314that 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)
329 " "
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)))
b1ddeeb7 337 (vc-setup-buffer (or buffer "*vc*")))
92d1eebf
ER
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))))
396
17f039f3
ER
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).
400
401(declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
402
403(defun vc-position-context (posn)
404 "Save a bit of the text around POSN in the current buffer.
405Used to help us find the corresponding position again later
406if 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)))))
413
414(defun vc-find-position-by-context (context)
415 "Return the position of CONTEXT in the current buffer.
416If 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))))))))
433
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)))
440 nil
441 (string= context-string (buffer-substring posn end)))))
442
443(defun vc-buffer-context ()
444 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
445Used 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)))
479
480(defun vc-restore-buffer-context (context)
481 "Restore point/mark, and reparse any affected compilation buffers.
482CONTEXT 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))
486 )
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)))
507
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))))))
517
518(defun vc-revert-buffer-internal (&optional arg no-confirm)
519 "Revert buffer, keeping point and mark where user expects them.
520Try to be clever in the face of changes due to expanded version-control
521key words. This is important for typeahead to work as expected.
522ARG 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)))
535
536(defun vc-resynch-window (file &optional keep noquery)
537 "If FILE is in the current buffer, either revert or unvisit it.
538The choice between revert (to see expanded keywords) and unvisit
539depends on KEEP. NOQUERY if non-nil inhibits confirmation for
540reverting. NOQUERY should be t *only* if it is known the only
541difference between the buffer and the file is due to
542modifications by the dispatcher client code, rather than user
543editing!"
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))))
c8d4f1a0 559 (run-hook-with-args 'modeline-hook buffer-file-name))
17f039f3
ER
560 (kill-buffer (current-buffer)))))
561
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)))))
4fe2e5ed 570 (vc-directory-resynch-file file))
17f039f3 571
83affd96
ER
572;; Command closures
573
574(defun vc-start-logentry (files extra comment initial-contents msg action &optional after-hook)
575 "Accept a comment for an operation on FILES with extra data EXTRA.
576If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
577action on close to ACTION. If COMMENT is a string and
578INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
579contents of the log entry buffer. If COMMENT is a string and
580INITIAL-CONTENTS is nil, do action immediately as if the user had
581entered COMMENT. If COMMENT is t, also do action immediately with an
582empty comment. Remember the file's buffer in `vc-parent-buffer'
583\(current one if no file). AFTER-HOOK specifies the local value
584for `vc-log-after-operation-hook'."
585 (let ((parent
586 (if (or (eq major-mode 'vc-dired-mode) (eq major-mode 'vc-dir-mode))
587 ;; If we are called from VC dired, the parent buffer is
588 ;; the current buffer.
589 (current-buffer)
590 (if (and files (equal (length files) 1))
591 (get-file-buffer (car files))
592 (current-buffer)))))
83affd96
ER
593 (if (and comment (not initial-contents))
594 (set-buffer (get-buffer-create "*VC-log*"))
595 (pop-to-buffer (get-buffer-create "*VC-log*")))
596 (set (make-local-variable 'vc-parent-buffer) parent)
597 (set (make-local-variable 'vc-parent-buffer-name)
598 (concat " from " (buffer-name vc-parent-buffer)))
83affd96
ER
599 (vc-log-edit files)
600 (make-local-variable 'vc-log-after-operation-hook)
601 (when after-hook
602 (setq vc-log-after-operation-hook after-hook))
603 (setq vc-log-operation action)
604 (setq vc-log-extra extra)
605 (when comment
606 (erase-buffer)
607 (when (stringp comment) (insert comment)))
608 (if (or (not comment) initial-contents)
609 (message "%s Type C-c C-c when done" msg)
610 (vc-finish-logentry (eq comment t)))))
611
612(defun vc-finish-logentry (&optional nocomment)
613 "Complete the operation implied by the current log entry.
614Use the contents of the current buffer as a check-in or registration
615comment. If the optional arg NOCOMMENT is non-nil, then don't check
616the buffer contents as a comment."
617 (interactive)
618 ;; Check and record the comment, if any.
619 (unless nocomment
620 (run-hooks 'vc-logentry-check-hook))
621 ;; Sync parent buffer in case the user modified it while editing the comment.
622 ;; But not if it is a vc-dired buffer.
623 (with-current-buffer vc-parent-buffer
624 (or vc-dired-mode (eq major-mode 'vc-dir-mode) (vc-buffer-sync)))
625 (unless vc-log-operation
626 (error "No log operation is pending"))
627 ;; save the parameters held in buffer-local variables
628 (let ((log-operation vc-log-operation)
629 (log-fileset vc-log-fileset)
630 (log-extra vc-log-extra)
631 (log-entry (buffer-string))
632 (after-hook vc-log-after-operation-hook)
633 (tmp-vc-parent-buffer vc-parent-buffer))
634 (pop-to-buffer vc-parent-buffer)
635 ;; OK, do it to it
636 (save-excursion
637 (funcall log-operation
638 log-fileset
639 log-extra
640 log-entry))
641 ;; Remove checkin window (after the checkin so that if that fails
642 ;; we don't zap the *VC-log* buffer and the typing therein).
643 ;; -- IMO this should be replaced with quit-window
644 (let ((logbuf (get-buffer "*VC-log*")))
645 (cond ((and logbuf vc-delete-logbuf-window)
646 (delete-windows-on logbuf (selected-frame))
647 ;; Kill buffer and delete any other dedicated windows/frames.
648 (kill-buffer logbuf))
649 (logbuf (pop-to-buffer "*VC-log*")
650 (bury-buffer)
651 (pop-to-buffer tmp-vc-parent-buffer))))
652 ;; Now make sure we see the expanded headers
653 (when log-fileset
654 (mapc
655 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
656 log-fileset))
657 (when vc-dired-mode
658 (dired-move-to-filename))
659 (when (eq major-mode 'vc-dir-mode)
660 (vc-dir-move-to-goal-column))
661 (run-hooks after-hook 'vc-finish-logentry-hook)))
662
783b505b
ER
663;; VC-Dired mode
664;; FIXME: to be removed when vc-dir support is finished
4f61cc3e
ER
665
666(defcustom vc-dired-listing-switches "-al"
667 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
668 :type 'string
669 :group 'vc
670 :version "21.1")
671
672(defcustom vc-dired-recurse t
673 "If non-nil, show directory trees recursively in VC Dired."
674 :type 'boolean
675 :group 'vc
676 :version "20.3")
677
678(defcustom vc-dired-terse-display t
679 "If non-nil, show only locked or locally modified files in VC Dired."
680 :type 'boolean
681 :group 'vc
682 :version "20.3")
683
684(defvar vc-dired-mode nil)
685(defvar vc-dired-window-configuration)
4f61cc3e
ER
686(defvar vc-dired-switches)
687(defvar vc-dired-terse-mode)
688
783b505b
ER
689(make-variable-buffer-local 'vc-dired-mode)
690
4f61cc3e
ER
691(defvar vc-dired-mode-map
692 (let ((map (make-sparse-keymap))
693 (vmap (make-sparse-keymap)))
694 (define-key map "\C-xv" vmap)
695 (define-key map "v" vmap)
696 (set-keymap-parent vmap vc-prefix-map)
697 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
698 map))
699
700(define-derived-mode vc-dired-mode dired-mode "Dired under VC"
701 "The major mode used in VC directory buffers.
702
703It works like Dired, but lists only files under version control, with
704the current VC state of each file being indicated in the place of the
705file's link count, owner, group and size. Subdirectories are also
706listed, and you may insert them into the buffer as desired, like in
707Dired.
708
709All Dired commands operate normally, with the exception of `v', which
710is redefined as the version control prefix, so that you can type
711`vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
712the file named in the current Dired buffer line. `vv' invokes
713`vc-next-action' on this file, or on all files currently marked.
714There is a special command, `*l', to mark all files currently locked."
715 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
716 ;; We do it here because dired might not be loaded yet
717 ;; when vc-dired-mode-map is initialized.
718 (set-keymap-parent vc-dired-mode-map dired-mode-map)
719 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
720 ;; The following is slightly modified from files.el,
721 ;; because file lines look a bit different in vc-dired-mode
722 ;; (the column before the date does not end in a digit).
723 ;; albinus: It should be done in the original declaration. Problem
724 ;; is the optional empty state-info; otherwise ")" would be good
725 ;; enough as delimeter.
726 (set (make-local-variable 'directory-listing-before-filename-regexp)
727 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
728 ;; In some locales, month abbreviations are as short as 2 letters,
729 ;; and they can be followed by ".".
730 (month (concat l l "+\\.?"))
731 (s " ")
732 (yyyy "[0-9][0-9][0-9][0-9]")
733 (dd "[ 0-3][0-9]")
734 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
735 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
736 (zone "[-+][0-2][0-9][0-5][0-9]")
737 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
738 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
739 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
740 "\\|" yyyy "-" iso-mm-dd "\\)"))
741 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
742 s "+"
743 "\\(" HH:MM "\\|" yyyy "\\)"))
744 (western-comma (concat month s "+" dd "," s "+" yyyy))
745 ;; Japanese MS-Windows ls-lisp has one-digit months, and
746 ;; omits the Kanji characters after month and day-of-month.
747 (mm "[ 0-1]?[0-9]")
748 (japanese
749 (concat mm l "?" s dd l "?" s "+"
750 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
751 ;; the .* below ensures that we find the last match on a line
752 (concat ".*" s
753 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
754 s "+")))
755 (and (boundp 'vc-dired-switches)
756 vc-dired-switches
757 (set (make-local-variable 'dired-actual-switches)
758 vc-dired-switches))
759 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
760 ;;(let ((backend-name (symbol-name (vc-responsible-backend
761 ;; default-directory))))
762 ;; (setq mode-name (concat mode-name backend-name))
763 ;; ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
764 ;; (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
765 ;; (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
766 ;; (cons backend-name vc-dire-menu-map) 'subdir)))
767 (setq vc-dired-mode t))
768
769(defun vc-dired-toggle-terse-mode ()
770 "Toggle terse display in VC Dired."
771 (interactive)
772 (if (not vc-dired-mode)
773 nil
774 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
775 (if vc-dired-terse-mode
776 (vc-dired-hook)
777 (revert-buffer))))
778
779(defun vc-dired-mark-locked ()
780 "Mark all files currently locked."
781 (interactive)
782 (dired-mark-if (let ((f (dired-get-filename nil t)))
783 (and f
784 (not (file-directory-p f))
785 (not (vc-up-to-date-p f))))
786 "locked file"))
787
788(define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
789
790(defun vc-dired-reformat-line (vc-info)
791 "Reformat a directory-listing line.
792Replace various columns with version control information, VC-INFO.
793This code, like dired, assumes UNIX -l format."
794 (beginning-of-line)
795 (when (re-search-forward
796 ;; Match link count, owner, group, size. Group may be missing,
797 ;; and only the size is present in OS/2 -l format.
798 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
799 (line-end-position) t)
800 (replace-match (substring (concat vc-info " ") 0 10)
801 t t nil 1)))
802
803(defun vc-dired-ignorable-p (filename)
804 "Should FILENAME be ignored in VC-Dired listings?"
805 (catch t
806 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
807 (dolist (ignorable completion-ignored-extensions)
808 (let ((ext (substring filename
809 (- (length filename)
810 (length ignorable)))))
811 (if (string= ignorable ext) (throw t t))))
812 ;; Ignore Makefiles derived from something else
813 (when (string= (file-name-nondirectory filename) "Makefile")
814 (let* ((dir (file-name-directory filename))
815 (peers (directory-files (or dir default-directory))))
816 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
817 (throw t t))))
818 nil))
819
820(defun vc-dired-purge ()
821 "Remove empty subdirs."
822 (goto-char (point-min))
823 (while (dired-get-subdir)
824 (forward-line 2)
825 (if (dired-get-filename nil t)
826 (if (not (dired-next-subdir 1 t))
827 (goto-char (point-max)))
828 (forward-line -2)
829 (if (not (string= (dired-current-directory) default-directory))
830 (dired-do-kill-lines t "")
831 ;; We cannot remove the top level directory.
832 ;; Just make it look a little nicer.
833 (forward-line 1)
834 (or (eobp) (kill-line))
835 (if (not (dired-next-subdir 1 t))
836 (goto-char (point-max))))))
837 (goto-char (point-min)))
838
839(defun vc-dired-buffers-for-dir (dir)
840 "Return a list of all vc-dired buffers that currently display DIR."
841 (let (result)
842 ;; Check whether dired is loaded.
843 (when (fboundp 'dired-buffers-for-dir)
844 (dolist (buffer (dired-buffers-for-dir dir))
845 (with-current-buffer buffer
846 (when vc-dired-mode
847 (push buffer result)))))
848 (nreverse result)))
849
4f61cc3e
ER
850;;;###autoload
851(defun vc-directory (dir read-switches)
852 "Create a buffer in VC Dired Mode for directory DIR.
853
854See Info node `VC Dired Mode'.
855
856With prefix arg READ-SWITCHES, specify a value to override
857`dired-listing-switches' when generating the listing."
858 (interactive "DDired under VC (directory): \nP")
859 (let ((vc-dired-switches (concat vc-dired-listing-switches
860 (if vc-dired-recurse "R" ""))))
861 (if read-switches
862 (setq vc-dired-switches
863 (read-string "Dired listing switches: "
864 vc-dired-switches)))
865 (require 'dired)
866 (require 'dired-aux)
867 (switch-to-buffer
868 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
869 vc-dired-switches
870 'vc-dired-mode))))
871
783b505b
ER
872;; The ewoc-based vc-directory implementation
873
874(defcustom vc-dir-mode-hook nil
875 "Normal hook run by `vc-dir-mode'.
876See `run-hooks'."
877 :type 'hook
878 :group 'vc)
879
880;; Used to store information for the files displayed in the *VC status* buffer.
881;; Each item displayed corresponds to one of these defstructs.
882(defstruct (vc-dir-fileinfo
883 (:copier nil)
884 (:type list) ;So we can use `member' on lists of FIs.
885 (:constructor
886 ;; We could define it as an alias for `list'.
887 vc-dir-create-fileinfo (name state &optional extra marked directory))
888 (:conc-name vc-dir-fileinfo->))
889 name ;Keep it as first, for `member'.
890 state
891 ;; For storing client-mode specific information.
892 extra
893 marked
894 ;; To keep track of not updated files during a global refresh
895 needs-update
896 ;; To distinguish files and directories.
897 directory)
898
cb625535
ER
899;; Used to describe a dispatcher client mode.
900(defstruct (vc-client-object
901 (:copier nil)
902 (:constructor
903 vc-create-client-object (name
904 headers
905 file-to-info
906 file-to-state
907 file-to-extra
908 updater))
909 (:conc-name vc-client-object->))
910 name
911 headers
912 file-to-info
913 file-to-state
914 file-to-extra
915 updater)
916
783b505b
ER
917(defvar vc-ewoc nil)
918(defvar vc-dir-process-buffer nil
919 "The buffer used for the asynchronous call that computes the VC status.")
920
921(defun vc-dir-move-to-goal-column ()
922 ;; Used to keep the cursor on the file name column.
923 (beginning-of-line)
924 ;; Must be in sync with vc-default-status-printer.
925 (forward-char 25))
926
927(defun vc-dir-prepare-status-buffer (dir &optional create-new)
928 "Find a *vc-dir* buffer showing DIR, or create a new one."
929 (setq dir (expand-file-name dir))
930 (let* ((bname "*vc-dir*")
931 ;; Look for another *vc-dir* buffer visiting the same directory.
932 (buf (save-excursion
933 (unless create-new
934 (dolist (buffer (buffer-list))
935 (set-buffer buffer)
936 (when (and (eq major-mode 'vc-dir-mode)
937 (string= (expand-file-name default-directory) dir))
938 (return buffer)))))))
939 (or buf
940 ;; Create a new *vc-dir* buffer.
941 (with-current-buffer (create-file-buffer bname)
942 (cd dir)
943 (vc-setup-buffer (current-buffer))
944 ;; Reset the vc-parent-buffer-name so that it does not appear
945 ;; in the mode-line.
946 (setq vc-parent-buffer-name nil)
947 (current-buffer)))))
948
949(defvar vc-dir-menu-map
950 (let ((map (make-sparse-keymap "VC-dir")))
951 (define-key map [quit]
952 '(menu-item "Quit" quit-window
953 :help "Quit"))
954 (define-key map [kill]
955 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
956 :enable (vc-dir-busy)
957 :help "Kill the command that updates VC status buffer"))
958 (define-key map [refresh]
959 '(menu-item "Refresh" vc-dir-refresh
960 :enable (not (vc-dir-busy))
961 :help "Refresh the contents of the VC status buffer"))
962 ;; Movement.
963 (define-key map [sepmv] '("--"))
964 (define-key map [next-line]
965 '(menu-item "Next line" vc-dir-next-line
966 :help "Go to the next line" :keys "n"))
967 (define-key map [previous-line]
968 '(menu-item "Previous line" vc-dir-previous-line
969 :help "Go to the previous line"))
970 ;; Marking.
971 (define-key map [sepmrk] '("--"))
972 (define-key map [unmark-all]
973 '(menu-item "Unmark All" vc-dir-unmark-all-files
974 :help "Unmark all files that are in the same state as the current file\
975\nWith prefix argument unmark all files"))
976 (define-key map [unmark-previous]
977 '(menu-item "Unmark previous " vc-dir-unmark-file-up
978 :help "Move to the previous line and unmark the file"))
979
980 (define-key map [mark-all]
981 '(menu-item "Mark All" vc-dir-mark-all-files
982 :help "Mark all files that are in the same state as the current file\
983\nWith prefix argument mark all files"))
984 (define-key map [unmark]
985 '(menu-item "Unmark" vc-dir-unmark
986 :help "Unmark the current file or all files in the region"))
987
988 (define-key map [mark]
989 '(menu-item "Mark" vc-dir-mark
990 :help "Mark the current file or all files in the region"))
991
992 (define-key map [sepopn] '("--"))
993 (define-key map [open-other]
994 '(menu-item "Open in other window" vc-dir-find-file-other-window
995 :help "Find the file on the current line, in another window"))
996 (define-key map [open]
997 '(menu-item "Open file" vc-dir-find-file
998 :help "Find the file on the current line"))
783b505b 999 map)
9a6b551b 1000 "Menu for dispatcher status")
783b505b
ER
1001
1002(defalias 'vc-dir-menu-map vc-dir-menu-map)
1003
1004(defvar vc-dir-mode-map
1005 (let ((map (make-keymap)))
1006 (suppress-keymap map)
1007 ;; Marking.
1008 (define-key map "m" 'vc-dir-mark)
1009 (define-key map "M" 'vc-dir-mark-all-files)
1010 (define-key map "u" 'vc-dir-unmark)
1011 (define-key map "U" 'vc-dir-unmark-all-files)
1012 (define-key map "\C-?" 'vc-dir-unmark-file-up)
1013 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
1014 ;; Movement.
1015 (define-key map "n" 'vc-dir-next-line)
1016 (define-key map " " 'vc-dir-next-line)
1017 (define-key map "\t" 'vc-dir-next-line)
1018 (define-key map "p" 'vc-dir-previous-line)
1019 (define-key map [backtab] 'vc-dir-previous-line)
783b505b
ER
1020 ;; The remainder.
1021 (define-key map "f" 'vc-dir-find-file)
1022 (define-key map "\C-m" 'vc-dir-find-file)
1023 (define-key map "o" 'vc-dir-find-file-other-window)
783b505b
ER
1024 (define-key map "q" 'quit-window)
1025 (define-key map "g" 'vc-dir-refresh)
1026 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
1027 (define-key map [(down-mouse-3)] 'vc-dir-menu)
1028 (define-key map [(mouse-2)] 'vc-dir-toggle-mark)
9852a51d
ER
1029
1030 ;; FIXME: Calls back into vc.el
1031 ;; Hook up the menu.
1032 (define-key map [menu-bar vc-dir-mode]
1033 '(menu-item
1034 ;; This is used so that client modes can add mode-specific
1035 ;; menu items to vc-dir-menu-map.
1036 "VC Status" vc-dir-menu-map :filter vc-dir-menu-map-filter))
783b505b 1037 map)
9852a51d 1038 "Keymap for VC status")
783b505b
ER
1039
1040(defmacro vc-at-event (event &rest body)
1041 "Evaluate `body' wich point located at event-start of `event'.
1042If `body' uses `event', it should be a variable,
1043 otherwise it will be evaluated twice."
1044 (let ((posn (gensym "vc-at-event-posn")))
1045 `(let ((,posn (event-start ,event)))
1046 (save-excursion
1047 (set-buffer (window-buffer (posn-window ,posn)))
1048 (goto-char (posn-point ,posn))
1049 ,@body))))
1050
1051(defun vc-dir-menu (e)
1052 "Popup the VC status menu."
1053 (interactive "e")
9852a51d 1054 (vc-at-event e (popup-menu vc-dir-menu-map e)))
783b505b
ER
1055
1056(defvar vc-dir-tool-bar-map
1057 (let ((map (make-sparse-keymap)))
1058 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
1059 map vc-dir-mode-map)
1060 (tool-bar-local-item "bookmark_add"
1061 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
1062 :help "Toggle mark on current item")
1063 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
1064 map vc-dir-mode-map
1065 :rtl "right-arrow")
1066 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
1067 map vc-dir-mode-map
1068 :rtl "left-arrow")
1069 (tool-bar-local-item-from-menu 'vc-print-log "info"
1070 map vc-dir-mode-map)
1071 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
1072 map vc-dir-mode-map)
1073 (tool-bar-local-item-from-menu 'nonincremental-search-forward
1074 "search" map)
1075 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
1076 map vc-dir-mode-map)
1077 (tool-bar-local-item-from-menu 'quit-window "exit"
1078 map vc-dir-mode-map)
1079 map))
1080
1081;; t if directories should be shown in vc-dir.
23532cb1
DN
1082;; WORK IN PROGRESS! This variable will likely disappear when the
1083;; work is done.
1084(defvar vc-dir-insert-directories t)
783b505b
ER
1085
1086(defun vc-dir-update (entries buffer &optional noinsert)
1087 "Update BUFFER's ewoc from the list of ENTRIES.
1088If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
1089 ;; Add ENTRIES to the vc-dir buffer BUFFER.
1090 (with-current-buffer buffer
1091 ;; Insert the entries sorted by name into the ewoc.
1092 ;; We assume the ewoc is sorted too, which should be the
1093 ;; case if we always add entries with vc-dir-update.
1094 (setq entries
1095 ;; Sort: first files and then subdirectories.
1096 ;; XXX: this is VERY inefficient, it computes the directory
1097 ;; names too many times
1098 (sort entries
1099 (lambda (entry1 entry2)
1100 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
1101 (dir2 (file-name-directory (expand-file-name (car entry2)))))
1102 (cond
1103 ((string< dir1 dir2) t)
1104 ((not (string= dir1 dir2)) nil)
1105 ((string< (car entry1) (car entry2))))))))
1106 (if (not vc-dir-insert-directories)
1107 (let ((entry (car entries))
1108 (node (ewoc-nth vc-ewoc 0)))
1109 (while (and entry node)
1110 (let ((entryfile (car entry))
1111 (nodefile (vc-dir-fileinfo->name (ewoc-data node))))
1112 (cond
1113 ((string-lessp nodefile entryfile)
1114 (setq node (ewoc-next vc-ewoc node)))
1115 ((string-lessp entryfile nodefile)
1116 (unless noinsert
1117 (ewoc-enter-before vc-ewoc node
1118 (apply 'vc-dir-create-fileinfo entry)))
1119 (setq entries (cdr entries) entry (car entries)))
1120 (t
1121 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
1122 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
1123 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
1124 (ewoc-invalidate vc-ewoc node)
1125 (setq entries (cdr entries) entry (car entries))
1126 (setq node (ewoc-next vc-ewoc node))))))
1127 (unless (or node noinsert)
1128 ;; We're past the last node, all remaining entries go to the end.
1129 (while entries
1130 (ewoc-enter-last vc-ewoc
1131 (apply 'vc-dir-create-fileinfo (pop entries))))))
1132 ;; Insert directory entries in the right places.
1133 (let ((entry (car entries))
1134 (node (ewoc-nth vc-ewoc 0)))
1135 ;; Insert . if it is not present.
1136 (unless node
1137 (let ((rd (file-relative-name default-directory)))
1138 (ewoc-enter-last
1139 vc-ewoc (vc-dir-create-fileinfo
1140 rd nil nil nil (expand-file-name default-directory))))
1141 (setq node (ewoc-nth vc-ewoc 0)))
1142
1143 (while (and entry node)
1144 (let* ((entryfile (car entry))
1145 (entrydir (file-name-directory (expand-file-name entryfile)))
1146 (nodedir
1147 (or (vc-dir-fileinfo->directory (ewoc-data node))
1148 (file-name-directory
1149 (expand-file-name
1150 (vc-dir-fileinfo->name (ewoc-data node)))))))
1151 (cond
1152 ;; First try to find the directory.
1153 ((string-lessp nodedir entrydir)
1154 (setq node (ewoc-next vc-ewoc node)))
1155 ((string-equal nodedir entrydir)
1156 ;; Found the directory, find the place for the file name.
1157 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
1158 (cond
1159 ((string-lessp nodefile entryfile)
1160 (setq node (ewoc-next vc-ewoc node)))
1161 ((string-equal nodefile entryfile)
1162 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
1163 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
1164 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
1165 (ewoc-invalidate vc-ewoc node)
1166 (setq entries (cdr entries) entry (car entries))
1167 (setq node (ewoc-next vc-ewoc node)))
1168 (t
1169 (ewoc-enter-before vc-ewoc node
1170 (apply 'vc-dir-create-fileinfo entry))
1171 (setq entries (cdr entries) entry (car entries))))))
1172 (t
1173 ;; We need to insert a directory node
1174 (let ((rd (file-relative-name entrydir)))
1175 (ewoc-enter-last
1176 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))
1177 ;; Now insert the node itself.
1178 (ewoc-enter-before vc-ewoc node
1179 (apply 'vc-dir-create-fileinfo entry))
1180 (setq entries (cdr entries) entry (car entries))))))
1181 ;; We're past the last node, all remaining entries go to the end.
1182 (unless (or node noinsert)
1183 (let* ((lastnode (ewoc-nth vc-ewoc -1))
1184 (lastdir
1185 (or (vc-dir-fileinfo->directory (ewoc-data lastnode))
1186 (file-name-directory
1187 (expand-file-name
1188 (vc-dir-fileinfo->name (ewoc-data lastnode)))))))
1189 (dolist (entry entries)
1190 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
1191 ;; Insert a directory node if needed.
1192 (unless (string-equal lastdir entrydir)
1193 (setq lastdir entrydir)
1194 (let ((rd (file-relative-name entrydir)))
1195 (ewoc-enter-last
1196 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
1197 ;; Now insert the node itself.
1198 (ewoc-enter-last vc-ewoc
1199 (apply 'vc-dir-create-fileinfo entry))))))))))
1200
1201(defun vc-dir-busy ()
1202 (and (buffer-live-p vc-dir-process-buffer)
1203 (get-buffer-process vc-dir-process-buffer)))
1204
1205(defun vc-dir-kill-dir-status-process ()
1206 "Kill the temporary buffer and associated process."
1207 (interactive)
1208 (when (buffer-live-p vc-dir-process-buffer)
1209 (let ((proc (get-buffer-process vc-dir-process-buffer)))
1210 (when proc (delete-process proc))
1211 (setq vc-dir-process-buffer nil)
1212 (setq mode-line-process nil))))
1213
1214(defun vc-dir-kill-query ()
1215 ;; Make sure that when the VC status buffer is killed the update
1216 ;; process running in background is also killed.
1217 (if (vc-dir-busy)
1218 (when (y-or-n-p "Status update process running, really kill status buffer?")
1219 (vc-dir-kill-dir-status-process)
1220 t)
1221 t))
1222
1223(defun vc-dir-next-line (arg)
1224 "Go to the next line.
1225If a prefix argument is given, move by that many lines."
1226 (interactive "p")
1227 (ewoc-goto-next vc-ewoc arg)
1228 (vc-dir-move-to-goal-column))
1229
1230(defun vc-dir-previous-line (arg)
1231 "Go to the previous line.
1232If a prefix argument is given, move by that many lines."
1233 (interactive "p")
1234 (ewoc-goto-prev vc-ewoc arg)
1235 (vc-dir-move-to-goal-column))
1236
1237(defun vc-dir-mark-unmark (mark-unmark-function)
1238 (if (use-region-p)
1239 (let ((firstl (line-number-at-pos (region-beginning)))
1240 (lastl (line-number-at-pos (region-end))))
1241 (save-excursion
1242 (goto-char (region-beginning))
1243 (while (<= (line-number-at-pos) lastl)
1244 (funcall mark-unmark-function))))
1245 (funcall mark-unmark-function)))
1246
1247(defun vc-dir-parent-marked-p (arg)
1248 (when vc-dir-insert-directories
1249 ;; Return nil if none of the parent directories of arg is marked.
1250 (let* ((argdata (ewoc-data arg))
1251 (argdir
1252 (let ((crtdir (vc-dir-fileinfo->directory argdata)))
1253 (if crtdir
1254 crtdir
1255 (file-name-directory (expand-file-name
1256 (vc-dir-fileinfo->name argdata))))))
1257 (arglen (length argdir))
1258 (crt arg)
1259 data dir)
1260 ;; Go through the predecessors, checking if any directory that is
1261 ;; a parent is marked.
1262 (while (setq crt (ewoc-prev vc-ewoc crt))
1263 (setq data (ewoc-data crt))
1264 (setq dir
1265 (let ((crtdir (vc-dir-fileinfo->directory data)))
1266 (if crtdir
1267 crtdir
1268 (file-name-directory (expand-file-name
1269 (vc-dir-fileinfo->name data))))))
1270
1271 (when (and (vc-dir-fileinfo->directory data)
1272 (string-equal (substring argdir 0 (length dir)) dir))
1273 (when (vc-dir-fileinfo->marked data)
1274 (error "Cannot mark `%s', parent directory `%s' marked"
1275 (vc-dir-fileinfo->name argdata)
1276 (vc-dir-fileinfo->name data)))))
1277 nil)))
1278
1279(defun vc-dir-children-marked-p (arg)
1280 ;; Return nil if none of the children of arg is marked.
1281 (when vc-dir-insert-directories
1282 (let* ((argdata (ewoc-data arg))
1283 (argdir (vc-dir-fileinfo->directory argdata))
1284 (arglen (length argdir))
1285 (is-child t)
1286 (crt arg)
1287 data dir)
1288 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
1289 (setq data (ewoc-data crt))
1290 (setq dir
1291 (let ((crtdir (vc-dir-fileinfo->directory data)))
1292 (if crtdir
1293 crtdir
1294 (file-name-directory (expand-file-name
1295 (vc-dir-fileinfo->name data))))))
1296 (if (string-equal argdir (substring dir 0 arglen))
1297 (when (vc-dir-fileinfo->marked data)
1298 (error "Cannot mark `%s', child `%s' marked"
1299 (vc-dir-fileinfo->name argdata)
1300 (vc-dir-fileinfo->name data)))
1301 ;; We are done, we got to an entry that is not a child of `arg'.
1302 (setq is-child nil)))
1303 nil)))
1304
1305(defun vc-dir-mark-file (&optional arg)
1306 ;; Mark ARG or the current file and move to the next line.
1307 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1308 (file (ewoc-data crt))
1309 (isdir (vc-dir-fileinfo->directory file)))
1310 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1311 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1312 (setf (vc-dir-fileinfo->marked file) t)
1313 (ewoc-invalidate vc-ewoc crt)
1314 (unless (or arg (mouse-event-p last-command-event))
1315 (vc-dir-next-line 1)))))
1316
1317(defun vc-dir-mark ()
1318 "Mark the current file or all files in the region.
1319If the region is active, mark all the files in the region.
1320Otherwise mark the file on the current line and move to the next
1321line."
1322 (interactive)
1323 (vc-dir-mark-unmark 'vc-dir-mark-file))
1324
1325(defun vc-dir-mark-all-files (arg)
1326 "Mark all files with the same state as the current one.
1327With a prefix argument mark all files.
1328If the current entry is a directory, mark all child files.
1329
1330The VC commands operate on files that are on the same state.
1331This command is intended to make it easy to select all files that
1332share the same state."
1333 (interactive "P")
1334 (if arg
1335 ;; Mark all files.
1336 (progn
1337 ;; First check that no directory is marked, we can't mark
1338 ;; files in that case.
1339 (ewoc-map
1340 (lambda (filearg)
1341 (when (and (vc-dir-fileinfo->directory filearg)
1342 (vc-dir-fileinfo->directory filearg))
1343 (error "Cannot mark all files, directory `%s' marked"
1344 (vc-dir-fileinfo->name filearg))))
1345 vc-ewoc)
1346 (ewoc-map
1347 (lambda (filearg)
1348 (unless (vc-dir-fileinfo->marked filearg)
1349 (setf (vc-dir-fileinfo->marked filearg) t)
1350 t))
1351 vc-ewoc))
1352 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1353 (if (vc-dir-fileinfo->directory data)
1354 ;; It's a directory, mark child files.
1355 (let ((crt (ewoc-locate vc-ewoc)))
1356 (unless (vc-dir-children-marked-p crt)
1357 (while (setq crt (ewoc-next vc-ewoc crt))
1358 (let ((crt-data (ewoc-data crt)))
1359 (unless (vc-dir-fileinfo->directory crt-data)
1360 (setf (vc-dir-fileinfo->marked crt-data) t)
1361 (ewoc-invalidate vc-ewoc crt))))))
1362 ;; It's a file
1363 (let ((state (vc-dir-fileinfo->state data))
1364 (crt (ewoc-nth vc-ewoc 0)))
1365 (while crt
1366 (let ((crt-data (ewoc-data crt)))
1367 (when (and (not (vc-dir-fileinfo->marked crt-data))
1368 (eq (vc-dir-fileinfo->state crt-data) state)
1369 (not (vc-dir-fileinfo->directory crt-data)))
1370 (vc-dir-mark-file crt)))
1371 (setq crt (ewoc-next vc-ewoc crt))))))))
1372
1373(defun vc-dir-unmark-file ()
1374 ;; Unmark the current file and move to the next line.
1375 (let* ((crt (ewoc-locate vc-ewoc))
1376 (file (ewoc-data crt)))
1377 (setf (vc-dir-fileinfo->marked file) nil)
1378 (ewoc-invalidate vc-ewoc crt)
1379 (unless (mouse-event-p last-command-event)
1380 (vc-dir-next-line 1))))
1381
1382(defun vc-dir-unmark ()
1383 "Unmark the current file or all files in the region.
1384If the region is active, unmark all the files in the region.
1385Otherwise mark the file on the current line and move to the next
1386line."
1387 (interactive)
1388 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1389
1390(defun vc-dir-unmark-file-up ()
1391 "Move to the previous line and unmark the file."
1392 (interactive)
1393 ;; If we're on the first line, we won't move up, but we will still
1394 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1395 ;; does.
1396 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1397 (file (ewoc-data prev)))
1398 (setf (vc-dir-fileinfo->marked file) nil)
1399 (ewoc-invalidate vc-ewoc prev)
1400 (vc-dir-move-to-goal-column)))
1401
1402(defun vc-dir-unmark-all-files (arg)
1403 "Unmark all files with the same state as the current one.
1404With a prefix argument unmark all files.
1405If the current entry is a directory, unmark all the child files.
1406
1407The VC commands operate on files that are on the same state.
1408This command is intended to make it easy to deselect all files
1409that share the same state."
1410 (interactive "P")
1411 (if arg
1412 (ewoc-map
1413 (lambda (filearg)
1414 (when (vc-dir-fileinfo->marked filearg)
1415 (setf (vc-dir-fileinfo->marked filearg) nil)
1416 t))
1417 vc-ewoc)
1418 (let* ((crt (ewoc-locate vc-ewoc))
1419 (data (ewoc-data crt)))
1420 (if (vc-dir-fileinfo->directory data)
1421 ;; It's a directory, unmark child files.
1422 (while (setq crt (ewoc-next vc-ewoc crt))
1423 (let ((crt-data (ewoc-data crt)))
1424 (unless (vc-dir-fileinfo->directory crt-data)
1425 (setf (vc-dir-fileinfo->marked crt-data) nil)
1426 (ewoc-invalidate vc-ewoc crt))))
1427 ;; It's a file
1428 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1429 (ewoc-map
1430 (lambda (filearg)
1431 (when (and (vc-dir-fileinfo->marked filearg)
1432 (eq (vc-dir-fileinfo->state filearg) crt-state))
1433 (setf (vc-dir-fileinfo->marked filearg) nil)
1434 t))
1435 vc-ewoc))))))
1436
1437(defun vc-dir-toggle-mark-file ()
1438 (let* ((crt (ewoc-locate vc-ewoc))
1439 (file (ewoc-data crt)))
1440 (if (vc-dir-fileinfo->marked file)
1441 (vc-dir-unmark-file)
1442 (vc-dir-mark-file))))
1443
1444(defun vc-dir-toggle-mark (e)
1445 (interactive "e")
1446 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1447
1448(defun vc-dir-delete-file ()
1449 "Delete the marked files, or the current file if no marks."
1450 (interactive)
1451 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1452 (list (vc-dir-current-file)))))
1453
1454(defun vc-dir-find-file ()
1455 "Find the file on the current line."
1456 (interactive)
1457 (find-file (vc-dir-current-file)))
1458
1459(defun vc-dir-find-file-other-window ()
1460 "Find the file on the current line, in another window."
1461 (interactive)
1462 (find-file-other-window (vc-dir-current-file)))
1463
1464(defun vc-dir-current-file ()
1465 (let ((node (ewoc-locate vc-ewoc)))
1466 (unless node
1467 (error "No file available."))
1468 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1469
1470(defun vc-dir-marked-files ()
1471 "Return the list of marked files."
1472 (mapcar
1473 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1474 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1475
1476(defun vc-dir-marked-only-files ()
cb625535 1477 "Return the list of marked files, For marked directories return child files."
783b505b
ER
1478 (let ((crt (ewoc-nth vc-ewoc 0))
1479 result)
1480 (while crt
1481 (let ((crt-data (ewoc-data crt)))
1482 (if (vc-dir-fileinfo->marked crt-data)
1483 (if (vc-dir-fileinfo->directory crt-data)
1484 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1485 (dirlen (length dir))
1486 data)
1487 (while
1488 (and (setq crt (ewoc-next vc-ewoc crt))
1489 (string-equal
1490 (substring
1491 (progn
1492 (setq data (ewoc-data crt))
1493 (let ((crtdir (vc-dir-fileinfo->directory data)))
1494 (if crtdir
1495 crtdir
1496 (file-name-directory
1497 (expand-file-name
1498 (vc-dir-fileinfo->name data))))))
1499 0 dirlen)
1500 dir))
1501 (unless (vc-dir-fileinfo->directory data)
1502 (push (vc-dir-fileinfo->name data) result))))
1503 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1504 (setq crt (ewoc-next vc-ewoc crt)))
1505 (setq crt (ewoc-next vc-ewoc crt)))))
1506 result))
038608c7 1507
4fe2e5ed
ER
1508(defun vc-directory-resynch-file (&optional fname)
1509 "Update the entries for FILE in any directory buffers that list it."
1510 (let ((file (or fname (expand-file-name buffer-file-name))))
1511 ;; The VC-Dired case
1512 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
1513 (when buffers
1514 (mapc (lambda (buffer)
1515 (with-current-buffer buffer
1516 (when (dired-goto-file file)
1517 ;; bind vc-dired-terse-mode to nil so that
1518 ;; files won't vanish when they are checked in
1519 (let ((vc-dired-terse-mode nil))
1520 (dired-do-redisplay 1)))))
1521 buffers)))
1522 ;; The vc-dir case
1523 (let ((found-vc-dir-buf nil))
1524 (save-excursion
1525 (dolist (status-buf (buffer-list))
1526 (set-buffer status-buf)
1527 ;; look for a vc-dir buffer that might show this file.
1528 (when (eq major-mode 'vc-dir-mode)
1529 (setq found-vc-dir-buf t)
1530 (let ((ddir (expand-file-name default-directory)))
1531 ;; This test is cvs-string-prefix-p
1532 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
1533 (let*
1534 ((file-short (substring file (length ddir)))
1535 (state
1536 (funcall (vc-client-object->file-to-state vc-client-mode)
1537 file))
1538 (extra
1539 (funcall (vc-client-object->file-to-extra vc-client-mode)
1540 file))
1541 (entry
1542 (list file-short state extra)))
1543 (vc-dir-update (list entry) status-buf))))))
1544 ;; We didn't find any vc-dir buffers, remove the hook, it is
1545 ;; not needed.
1546 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-directory-resynch-file))))))
cb625535
ER
1547
1548(defun vc-dir-mode (client-object)
1549 "Major mode for showing the VC status for a directory.
1550Marking/Unmarking key bindings and actions:
1551m - marks a file/directory or if the region is active, mark all the files
1552 in region.
1553 Restrictions: - a file cannot be marked if any parent directory is marked
1554 - a directory cannot be marked if any child file or
1555 directory is marked
1556u - marks a file/directory or if the region is active, unmark all the files
1557 in region.
1558M - if the cursor is on a file: mark all the files with the same VC state as
1559 the current file
1560 - if the cursor is on a directory: mark all child files
1561 - with a prefix argument: mark all files
1562U - if the cursor is on a file: unmark all the files with the same VC state
1563 as the current file
1564 - if the cursor is on a directory: unmark all child files
1565 - with a prefix argument: unmark all files
1566
1567
1568\\{vc-dir-mode-map}"
1569 (setq mode-name (vc-client-object->name client-object))
1570 (setq major-mode 'vc-dir-mode)
1571 (setq buffer-read-only t)
1572 (use-local-map vc-dir-mode-map)
1573 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
6494957a 1574 (set (make-local-variable 'vc-client-mode) client-object)
cb625535
ER
1575 (let ((buffer-read-only nil))
1576 (erase-buffer)
1577 (set (make-local-variable 'vc-dir-process-buffer) nil)
1578 (set (make-local-variable 'vc-ewoc)
1579 (ewoc-create (vc-client-object->file-to-info client-object)
1580 (vc-client-object->headers client-object)))
4fe2e5ed 1581 (add-hook 'after-save-hook 'vc-directory-resynch-file)
cb625535
ER
1582 ;; Make sure that if the VC status buffer is killed, the update
1583 ;; process running in the background is also killed.
1584 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1585 (funcall (vc-client-object->updater client-object)))
1586 (run-hooks 'vc-dir-mode-hook))
1587
1588(put 'vc-dir-mode 'mode-class 'special)
1589
be636037
ER
1590(defun vc-buffer-sync (&optional not-urgent)
1591 "Make sure the current buffer and its working file are in sync.
1592NOT-URGENT means it is ok to continue if the user says not to save."
1593 (when (buffer-modified-p)
1594 (if (or vc-suppress-confirm
1595 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1596 (save-buffer)
1597 (unless not-urgent
1598 (error "Aborted")))))
1599
b236ab0d
ER
1600(defun vc-dispatcher-browsing ()
1601 "Are we in a directory browser buffer?"
1602 (or vc-dired-mode (eq major-mode 'vc-dir-mode)))
1603
1604(defun vc-dispatcher-selection-set (eligible
1605 &optional
1606 allow-directory-wildcard
c8d4f1a0 1607 allow-ineligible
b236ab0d
ER
1608 include-files-not-directories)
1609 "Deduce a set of files to which to apply an operation. Return the fileset.
1610If we're in VC-dired mode, the fileset is the list of marked files.
1611Otherwise, if we're looking at a buffer for which ELIGIBLE returns non-NIL,
1612the fileset is a singleton containing this file.
1613If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on
1614and we're in a dired buffer, select the current directory.
1615If none of these conditions is met, but ALLOW-INELIGIBLE is on and the
1616visited file is not registered, return a singleton fileset containing it.
1617If INCLUDE-FILES-NOT-DIRECTORIES then if directories are marked,
1618return the list of VC files in those directories instead of
1619the directories themselves.
1620Otherwise, throw an error."
be636037 1621 (let ((files
b236ab0d
ER
1622 (cond
1623 ;; Browsing with dired
1624 (vc-dired-mode
1625 (let ((marked (dired-map-over-marks (dired-get-filename) nil)))
1626 (if marked
1627 marked
1628 (error "No files have been selected."))))
1629 ;; Browsing with vc-dir
1630 ((eq major-mode 'vc-dir-mode)
1631 (or
1632 (if include-files-not-directories
1633 (vc-dir-marked-only-files)
1634 (vc-dir-marked-files))
1635 (list (vc-dir-current-file))))
1636 ;; Visiting an eligible file
1637 ((funcall eligible buffer-file-name)
1638 (list buffer-file-name))
1639 ;; No eligible file -- if there's a parent buffer, deuce from there
1640 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1641 (with-current-buffer vc-parent-buffer
1642 (vc-dispatcher-browsing))))
1643 (progn
1644 (set-buffer vc-parent-buffer)
c8d4f1a0 1645 (vc-dispatcher-selection-set eligible)))
b236ab0d
ER
1646 ;; No parent buffer, we may want to select entire directory
1647 ;;
1648 ;; This is guarded by an enabling arg so users won't potentially
1649 ;; shoot themselves in the foot by modifying a fileset they can't
1650 ;; verify by eyeball. Allow it for nondestructive commands like
1651 ;; making diffs, or possibly for destructive ones that have
1652 ;; confirmation prompts.
1653 ((and allow-directory-wildcard
1654 ;; I think this is a misfeature. For now, I'll leave it in, but
1655 ;; I'll disable it anywhere else than in dired buffers. --Stef
1656 (and (derived-mode-p 'dired-mode)
1657 (equal buffer-file-name nil)
1658 (equal list-buffers-directory default-directory)))
1659 (progn
1660 (message "All eligible files below %s selected."
1661 default-directory)
1662 (list default-directory)))
1663 ;; Last, if we're allowing ineligible files and visiting one, select it.
1664 ((and allow-ineligible (not (eligible buffer-file-name)))
1665 (list buffer-file-name))
1666 ;; No good set here, throw error
be636037
ER
1667 (t (error "No fileset is available here.")))))
1668 ;; We assume, in order to avoid unpleasant surprises to the user,
1669 ;; that a fileset is not in good shape to be handed to the user if the
1670 ;; buffers visting the fileset don't match the on-disk contents.
1671 (dolist (file files)
1672 (let ((visited (get-file-buffer file)))
1673 (when visited
e6244c1b 1674 (if (vc-dispatcher-browsing)
be636037
ER
1675 (switch-to-buffer-other-window visited)
1676 (set-buffer visited))
1677 ;; Check relation of buffer and file, and make sure
1678 ;; user knows what he's doing. First, finding the file
1679 ;; will check whether the file on disk is newer.
1680 ;; Ignore buffer-read-only during this test, and
1681 ;; preserve find-file-literally.
1682 (let ((buffer-read-only (not (file-writable-p file))))
1683 (find-file-noselect file nil find-file-literally))
1684 (if (not (verify-visited-file-modtime (current-buffer)))
1685 (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file))
1686 (write-file buffer-file-name)
1687 (error "Aborted"))
1688 ;; Now, check if we have unsaved changes.
1689 (vc-buffer-sync t)
1690 (when (buffer-modified-p)
1691 (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file))
1692 (error "Aborted")))))))
1693 files))
b236ab0d 1694
3eb20677 1695;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
b1ddeeb7 1696;;; vc-dispatcher.el ends here