Remove the vc-dir-insert-directories global.
[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;;
d99d06ec
ER
120;; - vc-dir needs mouse bindings.
121;;
122;; - vc-dir needs more key bindings for VC actions.
123;;
124;; - vc-dir toolbar needs more icons.
125;;
126;; - vc-dir-next-line should not print an "end of buffer" message when
127;; invoked with the cursor on the last file.
128;;
129;; - add commands to move to the prev/next directory in vc-dir.
130;;
131;; - document vc-dir in the manual.
132;;
133
92d1eebf
ER
134(provide 'vc-dispatcher)
135
4f61cc3e 136(eval-when-compile
4903369a 137 (require 'cl))
4f61cc3e 138
83affd96
ER
139;; General customization
140
141(defcustom vc-logentry-check-hook nil
142 "Normal hook run by `vc-finish-logentry'.
143Use this to impose your own rules on the entry in addition to any the
4f61cc3e 144dispatcher client mode imposes itself."
83affd96
ER
145 :type 'hook
146 :group 'vc)
147
17f039f3
ER
148(defcustom vc-delete-logbuf-window t
149 "If non-nil, delete the *VC-log* buffer and window after each logical action.
150If nil, bury that buffer instead.
151This is most useful if you have multiple windows on a frame and would like to
152preserve the setting."
153 :type 'boolean
154 :group 'vc)
155
156(defcustom vc-command-messages nil
157 "If non-nil, display run messages from back-end commands."
158 :type 'boolean
159 :group 'vc)
160
821677fb
ER
161(defcustom vc-suppress-confirm nil
162 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
163 :type 'boolean
164 :group 'vc)
165
83affd96 166;; Variables the user doesn't need to know about.
17f039f3 167
83affd96
ER
168(defvar vc-log-operation nil)
169(defvar vc-log-after-operation-hook nil)
170(defvar vc-log-fileset)
171(defvar vc-log-extra)
172
173;; In a log entry buffer, this is a local variable
174;; that points to the buffer for which it was made
4903369a 175;; (either a file, or a VC directory buffer).
83affd96
ER
176(defvar vc-parent-buffer nil)
177(put 'vc-parent-buffer 'permanent-local t)
178(defvar vc-parent-buffer-name nil)
179(put 'vc-parent-buffer-name 'permanent-local t)
180
92d1eebf
ER
181;; Common command execution logic
182
183(defun vc-process-filter (p s)
184 "An alternative output filter for async process P.
185One difference with the default filter is that this inserts S after markers.
186Another is that undo information is not kept."
187 (let ((buffer (process-buffer p)))
188 (when (buffer-live-p buffer)
189 (with-current-buffer buffer
190 (save-excursion
191 (let ((buffer-undo-list t)
192 (inhibit-read-only t))
193 (goto-char (process-mark p))
194 (insert s)
195 (set-marker (process-mark p) (point))))))))
196
7265c6e8
ER
197(defun vc-setup-buffer (buf)
198 "Prepare BUF for executing a slave command and make it current."
92d1eebf
ER
199 (let ((camefrom (current-buffer))
200 (olddir default-directory))
201 (set-buffer (get-buffer-create buf))
202 (kill-all-local-variables)
203 (set (make-local-variable 'vc-parent-buffer) camefrom)
204 (set (make-local-variable 'vc-parent-buffer-name)
205 (concat " from " (buffer-name camefrom)))
206 (setq default-directory olddir)
207 (let ((buffer-undo-list t)
208 (inhibit-read-only t))
209 (erase-buffer))))
210
211(defvar vc-sentinel-movepoint) ;Dynamically scoped.
212
213(defun vc-process-sentinel (p s)
214 (let ((previous (process-get p 'vc-previous-sentinel))
215 (buf (process-buffer p)))
216 ;; Impatient users sometime kill "slow" buffers; check liveness
217 ;; to avoid "error in process sentinel: Selecting deleted buffer".
218 (when (buffer-live-p buf)
219 (when previous (funcall previous p s))
220 (with-current-buffer buf
221 (setq mode-line-process
222 (let ((status (process-status p)))
223 ;; Leave mode-line uncluttered, normally.
224 (unless (eq 'exit status)
225 (format " (%s)" status))))
226 (let (vc-sentinel-movepoint)
227 ;; Normally, we want async code such as sentinels to not move point.
228 (save-excursion
229 (goto-char (process-mark p))
230 (let ((cmds (process-get p 'vc-sentinel-commands)))
231 (process-put p 'vc-sentinel-commands nil)
232 (dolist (cmd cmds)
233 ;; Each sentinel may move point and the next one should be run
234 ;; at that new point. We could get the same result by having
235 ;; each sentinel read&set process-mark, but since `cmd' needs
236 ;; to work both for async and sync processes, this would be
237 ;; difficult to achieve.
238 (vc-exec-after cmd))))
239 ;; But sometimes the sentinels really want to move point.
240 (when vc-sentinel-movepoint
241 (let ((win (get-buffer-window (current-buffer) 0)))
242 (if (not win)
243 (goto-char vc-sentinel-movepoint)
244 (with-selected-window win
245 (goto-char vc-sentinel-movepoint))))))))))
246
247(defun vc-set-mode-line-busy-indicator ()
248 (setq mode-line-process
249 (concat " " (propertize "[waiting...]"
250 'face 'mode-line-emphasis
251 'help-echo
252 "A VC command is in progress in this buffer"))))
253
254(defun vc-exec-after (code)
255 "Eval CODE when the current buffer's process is done.
256If the current buffer has no process, just evaluate CODE.
257Else, add CODE to the process' sentinel."
258 (let ((proc (get-buffer-process (current-buffer))))
259 (cond
260 ;; If there's no background process, just execute the code.
261 ;; We used to explicitly call delete-process on exited processes,
262 ;; but this led to timing problems causing process output to be
263 ;; lost. Terminated processes get deleted automatically
264 ;; anyway. -- cyd
265 ((or (null proc) (eq (process-status proc) 'exit))
266 ;; Make sure we've read the process's output before going further.
267 (when proc (accept-process-output proc))
268 (eval code))
269 ;; If a process is running, add CODE to the sentinel
270 ((eq (process-status proc) 'run)
271 (vc-set-mode-line-busy-indicator)
272 (let ((previous (process-sentinel proc)))
273 (unless (eq previous 'vc-process-sentinel)
274 (process-put proc 'vc-previous-sentinel previous))
275 (set-process-sentinel proc 'vc-process-sentinel))
276 (process-put proc 'vc-sentinel-commands
277 ;; We keep the code fragments in the order given
278 ;; so that vc-diff-finish's message shows up in
279 ;; the presence of non-nil vc-command-messages.
280 (append (process-get proc 'vc-sentinel-commands)
281 (list code))))
282 (t (error "Unexpected process state"))))
283 nil)
284
285(defvar vc-post-command-functions nil
286 "Hook run at the end of `vc-do-command'.
287Each function is called inside the buffer in which the command was run
288and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
289
290(defvar w32-quote-process-args)
291
292(defun vc-delistify (filelist)
293 "Smash a FILELIST into a file list string suitable for info messages."
294 ;; FIXME what about file names with spaces?
295 (if (not filelist) "." (mapconcat 'identity filelist " ")))
296
297;;;###autoload
298(defun vc-do-command (buffer okstatus command file-or-list &rest flags)
299 "Execute a VC command, notifying user and checking for errors.
300Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
301current buffer if BUFFER is t. If the destination buffer is not
302already current, set it up properly and erase it. The command is
303considered successful if its exit status does not exceed OKSTATUS (if
304OKSTATUS is nil, that means to ignore error status, if it is `async', that
305means not to wait for termination of the subprocess; if it is t it means to
306ignore all execution errors). FILE-OR-LIST is the name of a working file;
307it may be a list of files or be nil (to execute commands that don't expect
308a file name or set of files). If an optional list of FLAGS is present,
309that is inserted into the command line before the filename."
310 ;; FIXME: file-relative-name can return a bogus result because
311 ;; it doesn't look at the actual file-system to see if symlinks
312 ;; come into play.
313 (let* ((files
314 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
315 (if (listp file-or-list) file-or-list (list file-or-list))))
316 (full-command
317 ;; What we're doing here is preparing a version of the command
318 ;; for display in a debug-progess message. If it's fewer than
319 ;; 20 characters display the entire command (without trailing
320 ;; newline). Otherwise display the first 20 followed by an ellipsis.
321 (concat (if (string= (substring command -1) "\n")
322 (substring command 0 -1)
323 command)
324 " "
325 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
326 " " (vc-delistify files))))
327 (save-current-buffer
328 (unless (or (eq buffer t)
329 (and (stringp buffer)
330 (string= (buffer-name) buffer))
331 (eq buffer (current-buffer)))
b1ddeeb7 332 (vc-setup-buffer (or buffer "*vc*")))
92d1eebf
ER
333 ;; If there's some previous async process still running, just kill it.
334 (let ((oldproc (get-buffer-process (current-buffer))))
335 ;; If we wanted to wait for oldproc to finish before doing
336 ;; something, we'd have used vc-eval-after.
337 ;; Use `delete-process' rather than `kill-process' because we don't
338 ;; want any of its output to appear from now on.
339 (if oldproc (delete-process oldproc)))
340 (let ((squeezed (remq nil flags))
341 (inhibit-read-only t)
342 (status 0))
343 (when files
344 (setq squeezed (nconc squeezed files)))
345 (let ((exec-path (append vc-path exec-path))
346 ;; Add vc-path to PATH for the execution of this command.
347 (process-environment
348 (cons (concat "PATH=" (getenv "PATH")
349 path-separator
350 (mapconcat 'identity vc-path path-separator))
351 process-environment))
352 (w32-quote-process-args t))
353 (when (and (eq okstatus 'async) (file-remote-p default-directory))
354 ;; start-process does not support remote execution
355 (setq okstatus nil))
356 (if (eq okstatus 'async)
357 ;; Run asynchronously.
358 (let ((proc
359 (let ((process-connection-type nil))
360 (apply 'start-file-process command (current-buffer)
361 command squeezed))))
362 (if vc-command-messages
363 (message "Running %s in background..." full-command))
364 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
365 (set-process-filter proc 'vc-process-filter)
366 (vc-exec-after
367 `(if vc-command-messages
368 (message "Running %s in background... done" ',full-command))))
369 ;; Run synchrously
370 (when vc-command-messages
371 (message "Running %s in foreground..." full-command))
372 (let ((buffer-undo-list t))
373 (setq status (apply 'process-file command nil t nil squeezed)))
374 (when (and (not (eq t okstatus))
375 (or (not (integerp status))
376 (and okstatus (< okstatus status))))
377 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
378 (pop-to-buffer (current-buffer))
379 (goto-char (point-min))
380 (shrink-window-if-larger-than-buffer))
381 (error "Running %s...FAILED (%s)" full-command
382 (if (integerp status) (format "status %d" status) status))))
383 ;; We're done. But don't emit a status message if running
384 ;; asychronously, it would just mislead.
385 (if (and vc-command-messages (not (eq okstatus 'async)))
386 (message "Running %s...OK = %d" full-command status)))
387 (vc-exec-after
388 `(run-hook-with-args 'vc-post-command-functions
389 ',command ',file-or-list ',flags))
390 status))))
391
17f039f3
ER
392;; These functions are used to ensure that the view the user sees is up to date
393;; even if the dispatcher client mode has messed with file contents (as in,
394;; for example, VCS keyword expansion).
395
396(declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
397
398(defun vc-position-context (posn)
399 "Save a bit of the text around POSN in the current buffer.
400Used to help us find the corresponding position again later
401if markers are destroyed or corrupted."
402 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
403 ;; rcs.el mode.
404 (list posn
405 (buffer-size)
406 (buffer-substring posn
407 (min (point-max) (+ posn 100)))))
408
409(defun vc-find-position-by-context (context)
410 "Return the position of CONTEXT in the current buffer.
411If CONTEXT cannot be found, return nil."
412 (let ((context-string (nth 2 context)))
413 (if (equal "" context-string)
414 (point-max)
415 (save-excursion
416 (let ((diff (- (nth 1 context) (buffer-size))))
417 (when (< diff 0) (setq diff (- diff)))
418 (goto-char (nth 0 context))
419 (if (or (search-forward context-string nil t)
420 ;; Can't use search-backward since the match may continue
421 ;; after point.
422 (progn (goto-char (- (point) diff (length context-string)))
423 ;; goto-char doesn't signal an error at
424 ;; beginning of buffer like backward-char would
425 (search-forward context-string nil t)))
426 ;; to beginning of OSTRING
427 (- (point) (length context-string))))))))
428
429(defun vc-context-matches-p (posn context)
430 "Return t if POSN matches CONTEXT, nil otherwise."
431 (let* ((context-string (nth 2 context))
432 (len (length context-string))
433 (end (+ posn len)))
434 (if (> end (1+ (buffer-size)))
435 nil
436 (string= context-string (buffer-substring posn end)))))
437
438(defun vc-buffer-context ()
439 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
440Used by `vc-restore-buffer-context' to later restore the context."
441 (let ((point-context (vc-position-context (point)))
442 ;; Use mark-marker to avoid confusion in transient-mark-mode.
9ba4a350 443 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
17f039f3
ER
444 (vc-position-context (mark-marker))))
445 ;; Make the right thing happen in transient-mark-mode.
9ba4a350
ER
446 (mark-active nil))
447 (list point-context mark-context nil)))
17f039f3
ER
448
449(defun vc-restore-buffer-context (context)
450 "Restore point/mark, and reparse any affected compilation buffers.
451CONTEXT is that which `vc-buffer-context' returns."
452 (let ((point-context (nth 0 context))
9ba4a350 453 (mark-context (nth 1 context)))
17f039f3
ER
454 ;; if necessary, restore point and mark
455 (if (not (vc-context-matches-p (point) point-context))
456 (let ((new-point (vc-find-position-by-context point-context)))
457 (when new-point (goto-char new-point))))
458 (and mark-active
459 mark-context
460 (not (vc-context-matches-p (mark) mark-context))
461 (let ((new-mark (vc-find-position-by-context mark-context)))
462 (when new-mark (set-mark new-mark))))))
463
464(defun vc-revert-buffer-internal (&optional arg no-confirm)
465 "Revert buffer, keeping point and mark where user expects them.
466Try to be clever in the face of changes due to expanded version-control
467key words. This is important for typeahead to work as expected.
468ARG and NO-CONFIRM are passed on to `revert-buffer'."
469 (interactive "P")
470 (widen)
471 (let ((context (vc-buffer-context)))
472 ;; Use save-excursion here, because it may be able to restore point
473 ;; and mark properly even in cases where vc-restore-buffer-context
474 ;; would fail. However, save-excursion might also get it wrong --
475 ;; in this case, vc-restore-buffer-context gives it a second try.
476 (save-excursion
477 ;; t means don't call normal-mode;
478 ;; that's to preserve various minor modes.
479 (revert-buffer arg no-confirm t))
480 (vc-restore-buffer-context context)))
481
482(defun vc-resynch-window (file &optional keep noquery)
483 "If FILE is in the current buffer, either revert or unvisit it.
484The choice between revert (to see expanded keywords) and unvisit
485depends on KEEP. NOQUERY if non-nil inhibits confirmation for
486reverting. NOQUERY should be t *only* if it is known the only
487difference between the buffer and the file is due to
488modifications by the dispatcher client code, rather than user
489editing!"
490 (and (string= buffer-file-name file)
491 (if keep
492 (progn
493 (vc-revert-buffer-internal t noquery)
494 ;; TODO: Adjusting view mode might no longer be necessary
495 ;; after RMS change to files.el of 1999-08-08. Investigate
496 ;; this when we install the new VC.
497 (and view-read-only
498 (if (file-writable-p file)
499 (and view-mode
500 (let ((view-old-buffer-read-only nil))
501 (view-mode-exit)))
502 (and (not view-mode)
503 (not (eq (get major-mode 'mode-class) 'special))
504 (view-mode-enter))))
c8d4f1a0 505 (run-hook-with-args 'modeline-hook buffer-file-name))
17f039f3
ER
506 (kill-buffer (current-buffer)))))
507
508(defun vc-resynch-buffer (file &optional keep noquery)
509 "If FILE is currently visited, resynch its buffer."
510 (if (string= buffer-file-name file)
511 (vc-resynch-window file keep noquery)
512 (let ((buffer (get-file-buffer file)))
513 (when buffer
514 (with-current-buffer buffer
515 (vc-resynch-window file keep noquery)))))
4fe2e5ed 516 (vc-directory-resynch-file file))
17f039f3 517
83affd96
ER
518;; Command closures
519
520(defun vc-start-logentry (files extra comment initial-contents msg action &optional after-hook)
521 "Accept a comment for an operation on FILES with extra data EXTRA.
522If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
523action on close to ACTION. If COMMENT is a string and
524INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
525contents of the log entry buffer. If COMMENT is a string and
526INITIAL-CONTENTS is nil, do action immediately as if the user had
527entered COMMENT. If COMMENT is t, also do action immediately with an
528empty comment. Remember the file's buffer in `vc-parent-buffer'
529\(current one if no file). AFTER-HOOK specifies the local value
530for `vc-log-after-operation-hook'."
531 (let ((parent
4903369a
ER
532 (if (vc-dispatcher-browsing)
533 ;; If we are called from a directory browser, the parent buffer is
83affd96
ER
534 ;; the current buffer.
535 (current-buffer)
536 (if (and files (equal (length files) 1))
537 (get-file-buffer (car files))
538 (current-buffer)))))
83affd96
ER
539 (if (and comment (not initial-contents))
540 (set-buffer (get-buffer-create "*VC-log*"))
541 (pop-to-buffer (get-buffer-create "*VC-log*")))
542 (set (make-local-variable 'vc-parent-buffer) parent)
543 (set (make-local-variable 'vc-parent-buffer-name)
544 (concat " from " (buffer-name vc-parent-buffer)))
83affd96
ER
545 (vc-log-edit files)
546 (make-local-variable 'vc-log-after-operation-hook)
547 (when after-hook
548 (setq vc-log-after-operation-hook after-hook))
549 (setq vc-log-operation action)
550 (setq vc-log-extra extra)
551 (when comment
552 (erase-buffer)
553 (when (stringp comment) (insert comment)))
554 (if (or (not comment) initial-contents)
555 (message "%s Type C-c C-c when done" msg)
556 (vc-finish-logentry (eq comment t)))))
557
558(defun vc-finish-logentry (&optional nocomment)
559 "Complete the operation implied by the current log entry.
560Use the contents of the current buffer as a check-in or registration
561comment. If the optional arg NOCOMMENT is non-nil, then don't check
562the buffer contents as a comment."
563 (interactive)
564 ;; Check and record the comment, if any.
565 (unless nocomment
566 (run-hooks 'vc-logentry-check-hook))
567 ;; Sync parent buffer in case the user modified it while editing the comment.
4903369a 568 ;; But not if it is a vc-directory buffer.
83affd96 569 (with-current-buffer vc-parent-buffer
4903369a 570 (or (vc-dispatcher-browsing) (vc-buffer-sync)))
83affd96
ER
571 (unless vc-log-operation
572 (error "No log operation is pending"))
573 ;; save the parameters held in buffer-local variables
574 (let ((log-operation vc-log-operation)
575 (log-fileset vc-log-fileset)
576 (log-extra vc-log-extra)
577 (log-entry (buffer-string))
578 (after-hook vc-log-after-operation-hook)
579 (tmp-vc-parent-buffer vc-parent-buffer))
580 (pop-to-buffer vc-parent-buffer)
581 ;; OK, do it to it
582 (save-excursion
583 (funcall log-operation
584 log-fileset
585 log-extra
586 log-entry))
587 ;; Remove checkin window (after the checkin so that if that fails
588 ;; we don't zap the *VC-log* buffer and the typing therein).
589 ;; -- IMO this should be replaced with quit-window
590 (let ((logbuf (get-buffer "*VC-log*")))
591 (cond ((and logbuf vc-delete-logbuf-window)
592 (delete-windows-on logbuf (selected-frame))
593 ;; Kill buffer and delete any other dedicated windows/frames.
594 (kill-buffer logbuf))
595 (logbuf (pop-to-buffer "*VC-log*")
596 (bury-buffer)
597 (pop-to-buffer tmp-vc-parent-buffer))))
598 ;; Now make sure we see the expanded headers
599 (when log-fileset
600 (mapc
601 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
602 log-fileset))
4903369a 603 (when (vc-dispatcher-browsing)
83affd96
ER
604 (vc-dir-move-to-goal-column))
605 (run-hooks after-hook 'vc-finish-logentry-hook)))
606
783b505b
ER
607;; The ewoc-based vc-directory implementation
608
609(defcustom vc-dir-mode-hook nil
610 "Normal hook run by `vc-dir-mode'.
611See `run-hooks'."
612 :type 'hook
613 :group 'vc)
614
615;; Used to store information for the files displayed in the *VC status* buffer.
616;; Each item displayed corresponds to one of these defstructs.
617(defstruct (vc-dir-fileinfo
618 (:copier nil)
619 (:type list) ;So we can use `member' on lists of FIs.
620 (:constructor
621 ;; We could define it as an alias for `list'.
622 vc-dir-create-fileinfo (name state &optional extra marked directory))
623 (:conc-name vc-dir-fileinfo->))
624 name ;Keep it as first, for `member'.
625 state
626 ;; For storing client-mode specific information.
627 extra
628 marked
629 ;; To keep track of not updated files during a global refresh
630 needs-update
631 ;; To distinguish files and directories.
632 directory)
633
cb625535
ER
634;; Used to describe a dispatcher client mode.
635(defstruct (vc-client-object
636 (:copier nil)
637 (:constructor
638 vc-create-client-object (name
639 headers
640 file-to-info
641 file-to-state
642 file-to-extra
643 updater))
644 (:conc-name vc-client-object->))
645 name
646 headers
647 file-to-info
648 file-to-state
649 file-to-extra
650 updater)
651
783b505b
ER
652(defvar vc-ewoc nil)
653(defvar vc-dir-process-buffer nil
654 "The buffer used for the asynchronous call that computes the VC status.")
655
656(defun vc-dir-move-to-goal-column ()
657 ;; Used to keep the cursor on the file name column.
658 (beginning-of-line)
659 ;; Must be in sync with vc-default-status-printer.
660 (forward-char 25))
661
662(defun vc-dir-prepare-status-buffer (dir &optional create-new)
663 "Find a *vc-dir* buffer showing DIR, or create a new one."
664 (setq dir (expand-file-name dir))
665 (let* ((bname "*vc-dir*")
666 ;; Look for another *vc-dir* buffer visiting the same directory.
667 (buf (save-excursion
668 (unless create-new
669 (dolist (buffer (buffer-list))
670 (set-buffer buffer)
8243c746 671 (when (and (vc-dispatcher-browsing)
783b505b
ER
672 (string= (expand-file-name default-directory) dir))
673 (return buffer)))))))
674 (or buf
675 ;; Create a new *vc-dir* buffer.
676 (with-current-buffer (create-file-buffer bname)
677 (cd dir)
678 (vc-setup-buffer (current-buffer))
679 ;; Reset the vc-parent-buffer-name so that it does not appear
680 ;; in the mode-line.
681 (setq vc-parent-buffer-name nil)
682 (current-buffer)))))
683
684(defvar vc-dir-menu-map
685 (let ((map (make-sparse-keymap "VC-dir")))
686 (define-key map [quit]
687 '(menu-item "Quit" quit-window
688 :help "Quit"))
689 (define-key map [kill]
690 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
691 :enable (vc-dir-busy)
692 :help "Kill the command that updates VC status buffer"))
693 (define-key map [refresh]
694 '(menu-item "Refresh" vc-dir-refresh
695 :enable (not (vc-dir-busy))
696 :help "Refresh the contents of the VC status buffer"))
697 ;; Movement.
698 (define-key map [sepmv] '("--"))
699 (define-key map [next-line]
700 '(menu-item "Next line" vc-dir-next-line
701 :help "Go to the next line" :keys "n"))
702 (define-key map [previous-line]
703 '(menu-item "Previous line" vc-dir-previous-line
704 :help "Go to the previous line"))
705 ;; Marking.
706 (define-key map [sepmrk] '("--"))
707 (define-key map [unmark-all]
708 '(menu-item "Unmark All" vc-dir-unmark-all-files
709 :help "Unmark all files that are in the same state as the current file\
710\nWith prefix argument unmark all files"))
711 (define-key map [unmark-previous]
712 '(menu-item "Unmark previous " vc-dir-unmark-file-up
713 :help "Move to the previous line and unmark the file"))
714
715 (define-key map [mark-all]
716 '(menu-item "Mark All" vc-dir-mark-all-files
717 :help "Mark all files that are in the same state as the current file\
718\nWith prefix argument mark all files"))
719 (define-key map [unmark]
720 '(menu-item "Unmark" vc-dir-unmark
721 :help "Unmark the current file or all files in the region"))
722
723 (define-key map [mark]
724 '(menu-item "Mark" vc-dir-mark
725 :help "Mark the current file or all files in the region"))
726
727 (define-key map [sepopn] '("--"))
728 (define-key map [open-other]
729 '(menu-item "Open in other window" vc-dir-find-file-other-window
730 :help "Find the file on the current line, in another window"))
731 (define-key map [open]
732 '(menu-item "Open file" vc-dir-find-file
733 :help "Find the file on the current line"))
783b505b 734 map)
9a6b551b 735 "Menu for dispatcher status")
783b505b
ER
736
737(defalias 'vc-dir-menu-map vc-dir-menu-map)
738
739(defvar vc-dir-mode-map
740 (let ((map (make-keymap)))
741 (suppress-keymap map)
742 ;; Marking.
743 (define-key map "m" 'vc-dir-mark)
744 (define-key map "M" 'vc-dir-mark-all-files)
745 (define-key map "u" 'vc-dir-unmark)
746 (define-key map "U" 'vc-dir-unmark-all-files)
747 (define-key map "\C-?" 'vc-dir-unmark-file-up)
748 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
749 ;; Movement.
750 (define-key map "n" 'vc-dir-next-line)
751 (define-key map " " 'vc-dir-next-line)
752 (define-key map "\t" 'vc-dir-next-line)
753 (define-key map "p" 'vc-dir-previous-line)
754 (define-key map [backtab] 'vc-dir-previous-line)
783b505b
ER
755 ;; The remainder.
756 (define-key map "f" 'vc-dir-find-file)
757 (define-key map "\C-m" 'vc-dir-find-file)
758 (define-key map "o" 'vc-dir-find-file-other-window)
783b505b
ER
759 (define-key map "q" 'quit-window)
760 (define-key map "g" 'vc-dir-refresh)
761 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
762 (define-key map [(down-mouse-3)] 'vc-dir-menu)
763 (define-key map [(mouse-2)] 'vc-dir-toggle-mark)
9852a51d
ER
764
765 ;; FIXME: Calls back into vc.el
766 ;; Hook up the menu.
767 (define-key map [menu-bar vc-dir-mode]
768 '(menu-item
769 ;; This is used so that client modes can add mode-specific
770 ;; menu items to vc-dir-menu-map.
771 "VC Status" vc-dir-menu-map :filter vc-dir-menu-map-filter))
783b505b 772 map)
9852a51d 773 "Keymap for VC status")
783b505b
ER
774
775(defmacro vc-at-event (event &rest body)
776 "Evaluate `body' wich point located at event-start of `event'.
777If `body' uses `event', it should be a variable,
778 otherwise it will be evaluated twice."
779 (let ((posn (gensym "vc-at-event-posn")))
780 `(let ((,posn (event-start ,event)))
781 (save-excursion
782 (set-buffer (window-buffer (posn-window ,posn)))
783 (goto-char (posn-point ,posn))
784 ,@body))))
785
786(defun vc-dir-menu (e)
787 "Popup the VC status menu."
788 (interactive "e")
9852a51d 789 (vc-at-event e (popup-menu vc-dir-menu-map e)))
783b505b
ER
790
791(defvar vc-dir-tool-bar-map
792 (let ((map (make-sparse-keymap)))
793 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
794 map vc-dir-mode-map)
795 (tool-bar-local-item "bookmark_add"
796 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
797 :help "Toggle mark on current item")
798 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
799 map vc-dir-mode-map
800 :rtl "right-arrow")
801 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
802 map vc-dir-mode-map
803 :rtl "left-arrow")
804 (tool-bar-local-item-from-menu 'vc-print-log "info"
805 map vc-dir-mode-map)
806 (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh"
807 map vc-dir-mode-map)
808 (tool-bar-local-item-from-menu 'nonincremental-search-forward
809 "search" map)
810 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
811 map vc-dir-mode-map)
812 (tool-bar-local-item-from-menu 'quit-window "exit"
813 map vc-dir-mode-map)
814 map))
815
783b505b
ER
816(defun vc-dir-update (entries buffer &optional noinsert)
817 "Update BUFFER's ewoc from the list of ENTRIES.
818If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
819 ;; Add ENTRIES to the vc-dir buffer BUFFER.
820 (with-current-buffer buffer
821 ;; Insert the entries sorted by name into the ewoc.
822 ;; We assume the ewoc is sorted too, which should be the
823 ;; case if we always add entries with vc-dir-update.
824 (setq entries
825 ;; Sort: first files and then subdirectories.
826 ;; XXX: this is VERY inefficient, it computes the directory
827 ;; names too many times
828 (sort entries
829 (lambda (entry1 entry2)
830 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
831 (dir2 (file-name-directory (expand-file-name (car entry2)))))
832 (cond
833 ((string< dir1 dir2) t)
834 ((not (string= dir1 dir2)) nil)
835 ((string< (car entry1) (car entry2))))))))
e4ba96dc
ER
836 ;; Insert directory entries in the right places.
837 (let ((entry (car entries))
838 (node (ewoc-nth vc-ewoc 0)))
839 ;; Insert . if it is not present.
840 (unless node
841 (let ((rd (file-relative-name default-directory)))
842 (ewoc-enter-last
843 vc-ewoc (vc-dir-create-fileinfo
844 rd nil nil nil (expand-file-name default-directory))))
845 (setq node (ewoc-nth vc-ewoc 0)))
846
847 (while (and entry node)
848 (let* ((entryfile (car entry))
849 (entrydir (file-name-directory (expand-file-name entryfile)))
850 (nodedir
851 (or (vc-dir-fileinfo->directory (ewoc-data node))
852 (file-name-directory
853 (expand-file-name
854 (vc-dir-fileinfo->name (ewoc-data node)))))))
855 (cond
856 ;; First try to find the directory.
857 ((string-lessp nodedir entrydir)
858 (setq node (ewoc-next vc-ewoc node)))
859 ((string-equal nodedir entrydir)
860 ;; Found the directory, find the place for the file name.
861 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
783b505b
ER
862 (cond
863 ((string-lessp nodefile entryfile)
864 (setq node (ewoc-next vc-ewoc node)))
e4ba96dc 865 ((string-equal nodefile entryfile)
783b505b
ER
866 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
867 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
868 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
869 (ewoc-invalidate vc-ewoc node)
870 (setq entries (cdr entries) entry (car entries))
e4ba96dc
ER
871 (setq node (ewoc-next vc-ewoc node)))
872 (t
873 (ewoc-enter-before vc-ewoc node
874 (apply 'vc-dir-create-fileinfo entry))
875 (setq entries (cdr entries) entry (car entries))))))
876 (t
877 ;; We need to insert a directory node
878 (let ((rd (file-relative-name entrydir)))
879 (ewoc-enter-last
880 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))
783b505b 881 ;; Now insert the node itself.
e4ba96dc
ER
882 (ewoc-enter-before vc-ewoc node
883 (apply 'vc-dir-create-fileinfo entry))
884 (setq entries (cdr entries) entry (car entries))))))
885 ;; We're past the last node, all remaining entries go to the end.
886 (unless (or node noinsert)
887 (let* ((lastnode (ewoc-nth vc-ewoc -1))
888 (lastdir
889 (or (vc-dir-fileinfo->directory (ewoc-data lastnode))
890 (file-name-directory
891 (expand-file-name
892 (vc-dir-fileinfo->name (ewoc-data lastnode)))))))
893 (dolist (entry entries)
894 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
895 ;; Insert a directory node if needed.
896 (unless (string-equal lastdir entrydir)
897 (setq lastdir entrydir)
898 (let ((rd (file-relative-name entrydir)))
899 (ewoc-enter-last
900 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
901 ;; Now insert the node itself.
902 (ewoc-enter-last vc-ewoc
903 (apply 'vc-dir-create-fileinfo entry)))))))))
783b505b
ER
904
905(defun vc-dir-busy ()
906 (and (buffer-live-p vc-dir-process-buffer)
907 (get-buffer-process vc-dir-process-buffer)))
908
909(defun vc-dir-kill-dir-status-process ()
910 "Kill the temporary buffer and associated process."
911 (interactive)
912 (when (buffer-live-p vc-dir-process-buffer)
913 (let ((proc (get-buffer-process vc-dir-process-buffer)))
914 (when proc (delete-process proc))
915 (setq vc-dir-process-buffer nil)
916 (setq mode-line-process nil))))
917
918(defun vc-dir-kill-query ()
919 ;; Make sure that when the VC status buffer is killed the update
920 ;; process running in background is also killed.
921 (if (vc-dir-busy)
922 (when (y-or-n-p "Status update process running, really kill status buffer?")
923 (vc-dir-kill-dir-status-process)
924 t)
925 t))
926
927(defun vc-dir-next-line (arg)
928 "Go to the next line.
929If a prefix argument is given, move by that many lines."
930 (interactive "p")
931 (ewoc-goto-next vc-ewoc arg)
932 (vc-dir-move-to-goal-column))
933
934(defun vc-dir-previous-line (arg)
935 "Go to the previous line.
936If a prefix argument is given, move by that many lines."
937 (interactive "p")
938 (ewoc-goto-prev vc-ewoc arg)
939 (vc-dir-move-to-goal-column))
940
941(defun vc-dir-mark-unmark (mark-unmark-function)
942 (if (use-region-p)
943 (let ((firstl (line-number-at-pos (region-beginning)))
944 (lastl (line-number-at-pos (region-end))))
945 (save-excursion
946 (goto-char (region-beginning))
947 (while (<= (line-number-at-pos) lastl)
948 (funcall mark-unmark-function))))
949 (funcall mark-unmark-function)))
950
951(defun vc-dir-parent-marked-p (arg)
e4ba96dc
ER
952 ;; Return nil if none of the parent directories of arg is marked.
953 (let* ((argdata (ewoc-data arg))
954 (argdir
955 (let ((crtdir (vc-dir-fileinfo->directory argdata)))
956 (if crtdir
957 crtdir
958 (file-name-directory (expand-file-name
959 (vc-dir-fileinfo->name argdata))))))
960 (arglen (length argdir))
961 (crt arg)
962 data dir)
963 ;; Go through the predecessors, checking if any directory that is
964 ;; a parent is marked.
965 (while (setq crt (ewoc-prev vc-ewoc crt))
966 (setq data (ewoc-data crt))
967 (setq dir
968 (let ((crtdir (vc-dir-fileinfo->directory data)))
783b505b
ER
969 (if crtdir
970 crtdir
971 (file-name-directory (expand-file-name
e4ba96dc
ER
972 (vc-dir-fileinfo->name data))))))
973
974 (when (and (vc-dir-fileinfo->directory data)
975 (string-equal (substring argdir 0 (length dir)) dir))
976 (when (vc-dir-fileinfo->marked data)
977 (error "Cannot mark `%s', parent directory `%s' marked"
978 (vc-dir-fileinfo->name argdata)
979 (vc-dir-fileinfo->name data)))))
980 nil))
783b505b
ER
981
982(defun vc-dir-children-marked-p (arg)
983 ;; Return nil if none of the children of arg is marked.
e4ba96dc
ER
984 (let* ((argdata (ewoc-data arg))
985 (argdir (vc-dir-fileinfo->directory argdata))
986 (arglen (length argdir))
987 (is-child t)
988 (crt arg)
989 data dir)
990 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
991 (setq data (ewoc-data crt))
992 (setq dir
993 (let ((crtdir (vc-dir-fileinfo->directory data)))
994 (if crtdir
995 crtdir
996 (file-name-directory (expand-file-name
997 (vc-dir-fileinfo->name data))))))
998 (if (string-equal argdir (substring dir 0 arglen))
999 (when (vc-dir-fileinfo->marked data)
1000 (error "Cannot mark `%s', child `%s' marked"
1001 (vc-dir-fileinfo->name argdata)
1002 (vc-dir-fileinfo->name data)))
1003 ;; We are done, we got to an entry that is not a child of `arg'.
1004 (setq is-child nil)))
1005 nil))
783b505b
ER
1006
1007(defun vc-dir-mark-file (&optional arg)
1008 ;; Mark ARG or the current file and move to the next line.
1009 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
1010 (file (ewoc-data crt))
1011 (isdir (vc-dir-fileinfo->directory file)))
1012 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
1013 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
1014 (setf (vc-dir-fileinfo->marked file) t)
1015 (ewoc-invalidate vc-ewoc crt)
1016 (unless (or arg (mouse-event-p last-command-event))
1017 (vc-dir-next-line 1)))))
1018
1019(defun vc-dir-mark ()
1020 "Mark the current file or all files in the region.
1021If the region is active, mark all the files in the region.
1022Otherwise mark the file on the current line and move to the next
1023line."
1024 (interactive)
1025 (vc-dir-mark-unmark 'vc-dir-mark-file))
1026
1027(defun vc-dir-mark-all-files (arg)
1028 "Mark all files with the same state as the current one.
1029With a prefix argument mark all files.
1030If the current entry is a directory, mark all child files.
1031
1032The VC commands operate on files that are on the same state.
1033This command is intended to make it easy to select all files that
1034share the same state."
1035 (interactive "P")
1036 (if arg
1037 ;; Mark all files.
1038 (progn
1039 ;; First check that no directory is marked, we can't mark
1040 ;; files in that case.
1041 (ewoc-map
1042 (lambda (filearg)
1043 (when (and (vc-dir-fileinfo->directory filearg)
1044 (vc-dir-fileinfo->directory filearg))
1045 (error "Cannot mark all files, directory `%s' marked"
1046 (vc-dir-fileinfo->name filearg))))
1047 vc-ewoc)
1048 (ewoc-map
1049 (lambda (filearg)
1050 (unless (vc-dir-fileinfo->marked filearg)
1051 (setf (vc-dir-fileinfo->marked filearg) t)
1052 t))
1053 vc-ewoc))
1054 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
1055 (if (vc-dir-fileinfo->directory data)
1056 ;; It's a directory, mark child files.
1057 (let ((crt (ewoc-locate vc-ewoc)))
1058 (unless (vc-dir-children-marked-p crt)
1059 (while (setq crt (ewoc-next vc-ewoc crt))
1060 (let ((crt-data (ewoc-data crt)))
1061 (unless (vc-dir-fileinfo->directory crt-data)
1062 (setf (vc-dir-fileinfo->marked crt-data) t)
1063 (ewoc-invalidate vc-ewoc crt))))))
1064 ;; It's a file
1065 (let ((state (vc-dir-fileinfo->state data))
1066 (crt (ewoc-nth vc-ewoc 0)))
1067 (while crt
1068 (let ((crt-data (ewoc-data crt)))
1069 (when (and (not (vc-dir-fileinfo->marked crt-data))
1070 (eq (vc-dir-fileinfo->state crt-data) state)
1071 (not (vc-dir-fileinfo->directory crt-data)))
1072 (vc-dir-mark-file crt)))
1073 (setq crt (ewoc-next vc-ewoc crt))))))))
1074
1075(defun vc-dir-unmark-file ()
1076 ;; Unmark the current file and move to the next line.
1077 (let* ((crt (ewoc-locate vc-ewoc))
1078 (file (ewoc-data crt)))
1079 (setf (vc-dir-fileinfo->marked file) nil)
1080 (ewoc-invalidate vc-ewoc crt)
1081 (unless (mouse-event-p last-command-event)
1082 (vc-dir-next-line 1))))
1083
1084(defun vc-dir-unmark ()
1085 "Unmark the current file or all files in the region.
1086If the region is active, unmark all the files in the region.
1087Otherwise mark the file on the current line and move to the next
1088line."
1089 (interactive)
1090 (vc-dir-mark-unmark 'vc-dir-unmark-file))
1091
1092(defun vc-dir-unmark-file-up ()
1093 "Move to the previous line and unmark the file."
1094 (interactive)
1095 ;; If we're on the first line, we won't move up, but we will still
1096 ;; remove the mark. This seems a bit odd but it is what buffer-menu
1097 ;; does.
1098 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
1099 (file (ewoc-data prev)))
1100 (setf (vc-dir-fileinfo->marked file) nil)
1101 (ewoc-invalidate vc-ewoc prev)
1102 (vc-dir-move-to-goal-column)))
1103
1104(defun vc-dir-unmark-all-files (arg)
1105 "Unmark all files with the same state as the current one.
1106With a prefix argument unmark all files.
1107If the current entry is a directory, unmark all the child files.
1108
1109The VC commands operate on files that are on the same state.
1110This command is intended to make it easy to deselect all files
1111that share the same state."
1112 (interactive "P")
1113 (if arg
1114 (ewoc-map
1115 (lambda (filearg)
1116 (when (vc-dir-fileinfo->marked filearg)
1117 (setf (vc-dir-fileinfo->marked filearg) nil)
1118 t))
1119 vc-ewoc)
1120 (let* ((crt (ewoc-locate vc-ewoc))
1121 (data (ewoc-data crt)))
1122 (if (vc-dir-fileinfo->directory data)
1123 ;; It's a directory, unmark child files.
1124 (while (setq crt (ewoc-next vc-ewoc crt))
1125 (let ((crt-data (ewoc-data crt)))
1126 (unless (vc-dir-fileinfo->directory crt-data)
1127 (setf (vc-dir-fileinfo->marked crt-data) nil)
1128 (ewoc-invalidate vc-ewoc crt))))
1129 ;; It's a file
1130 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
1131 (ewoc-map
1132 (lambda (filearg)
1133 (when (and (vc-dir-fileinfo->marked filearg)
1134 (eq (vc-dir-fileinfo->state filearg) crt-state))
1135 (setf (vc-dir-fileinfo->marked filearg) nil)
1136 t))
1137 vc-ewoc))))))
1138
1139(defun vc-dir-toggle-mark-file ()
1140 (let* ((crt (ewoc-locate vc-ewoc))
1141 (file (ewoc-data crt)))
1142 (if (vc-dir-fileinfo->marked file)
1143 (vc-dir-unmark-file)
1144 (vc-dir-mark-file))))
1145
1146(defun vc-dir-toggle-mark (e)
1147 (interactive "e")
1148 (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
1149
1150(defun vc-dir-delete-file ()
1151 "Delete the marked files, or the current file if no marks."
1152 (interactive)
1153 (mapc 'vc-delete-file (or (vc-dir-marked-files)
1154 (list (vc-dir-current-file)))))
1155
1156(defun vc-dir-find-file ()
1157 "Find the file on the current line."
1158 (interactive)
1159 (find-file (vc-dir-current-file)))
1160
1161(defun vc-dir-find-file-other-window ()
1162 "Find the file on the current line, in another window."
1163 (interactive)
1164 (find-file-other-window (vc-dir-current-file)))
1165
1166(defun vc-dir-current-file ()
1167 (let ((node (ewoc-locate vc-ewoc)))
1168 (unless node
1169 (error "No file available."))
1170 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
1171
1172(defun vc-dir-marked-files ()
1173 "Return the list of marked files."
1174 (mapcar
1175 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
1176 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
1177
1178(defun vc-dir-marked-only-files ()
cb625535 1179 "Return the list of marked files, For marked directories return child files."
783b505b
ER
1180 (let ((crt (ewoc-nth vc-ewoc 0))
1181 result)
1182 (while crt
1183 (let ((crt-data (ewoc-data crt)))
1184 (if (vc-dir-fileinfo->marked crt-data)
1185 (if (vc-dir-fileinfo->directory crt-data)
1186 (let* ((dir (vc-dir-fileinfo->directory crt-data))
1187 (dirlen (length dir))
1188 data)
1189 (while
1190 (and (setq crt (ewoc-next vc-ewoc crt))
1191 (string-equal
1192 (substring
1193 (progn
1194 (setq data (ewoc-data crt))
1195 (let ((crtdir (vc-dir-fileinfo->directory data)))
1196 (if crtdir
1197 crtdir
1198 (file-name-directory
1199 (expand-file-name
1200 (vc-dir-fileinfo->name data))))))
1201 0 dirlen)
1202 dir))
1203 (unless (vc-dir-fileinfo->directory data)
1204 (push (vc-dir-fileinfo->name data) result))))
1205 (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result)
1206 (setq crt (ewoc-next vc-ewoc crt)))
1207 (setq crt (ewoc-next vc-ewoc crt)))))
1208 result))
038608c7 1209
4fe2e5ed
ER
1210(defun vc-directory-resynch-file (&optional fname)
1211 "Update the entries for FILE in any directory buffers that list it."
1212 (let ((file (or fname (expand-file-name buffer-file-name))))
4fe2e5ed
ER
1213 ;; The vc-dir case
1214 (let ((found-vc-dir-buf nil))
1215 (save-excursion
1216 (dolist (status-buf (buffer-list))
1217 (set-buffer status-buf)
1218 ;; look for a vc-dir buffer that might show this file.
1219 (when (eq major-mode 'vc-dir-mode)
1220 (setq found-vc-dir-buf t)
1221 (let ((ddir (expand-file-name default-directory)))
1222 ;; This test is cvs-string-prefix-p
1223 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
1224 (let*
1225 ((file-short (substring file (length ddir)))
1226 (state
1227 (funcall (vc-client-object->file-to-state vc-client-mode)
1228 file))
1229 (extra
1230 (funcall (vc-client-object->file-to-extra vc-client-mode)
1231 file))
1232 (entry
1233 (list file-short state extra)))
1234 (vc-dir-update (list entry) status-buf))))))
1235 ;; We didn't find any vc-dir buffers, remove the hook, it is
1236 ;; not needed.
1237 (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-directory-resynch-file))))))
cb625535
ER
1238
1239(defun vc-dir-mode (client-object)
1240 "Major mode for showing the VC status for a directory.
1241Marking/Unmarking key bindings and actions:
1242m - marks a file/directory or if the region is active, mark all the files
1243 in region.
1244 Restrictions: - a file cannot be marked if any parent directory is marked
1245 - a directory cannot be marked if any child file or
1246 directory is marked
1247u - marks a file/directory or if the region is active, unmark all the files
1248 in region.
1249M - if the cursor is on a file: mark all the files with the same VC state as
1250 the current file
1251 - if the cursor is on a directory: mark all child files
1252 - with a prefix argument: mark all files
1253U - if the cursor is on a file: unmark all the files with the same VC state
1254 as the current file
1255 - if the cursor is on a directory: unmark all child files
1256 - with a prefix argument: unmark all files
1257
1258
1259\\{vc-dir-mode-map}"
1260 (setq mode-name (vc-client-object->name client-object))
1261 (setq major-mode 'vc-dir-mode)
1262 (setq buffer-read-only t)
1263 (use-local-map vc-dir-mode-map)
1264 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map)
6494957a 1265 (set (make-local-variable 'vc-client-mode) client-object)
cb625535
ER
1266 (let ((buffer-read-only nil))
1267 (erase-buffer)
1268 (set (make-local-variable 'vc-dir-process-buffer) nil)
1269 (set (make-local-variable 'vc-ewoc)
1270 (ewoc-create (vc-client-object->file-to-info client-object)
1271 (vc-client-object->headers client-object)))
4fe2e5ed 1272 (add-hook 'after-save-hook 'vc-directory-resynch-file)
cb625535
ER
1273 ;; Make sure that if the VC status buffer is killed, the update
1274 ;; process running in the background is also killed.
1275 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1276 (funcall (vc-client-object->updater client-object)))
1277 (run-hooks 'vc-dir-mode-hook))
1278
1279(put 'vc-dir-mode 'mode-class 'special)
1280
be636037
ER
1281(defun vc-buffer-sync (&optional not-urgent)
1282 "Make sure the current buffer and its working file are in sync.
1283NOT-URGENT means it is ok to continue if the user says not to save."
1284 (when (buffer-modified-p)
1285 (if (or vc-suppress-confirm
1286 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1287 (save-buffer)
1288 (unless not-urgent
1289 (error "Aborted")))))
1290
b236ab0d
ER
1291(defun vc-dispatcher-browsing ()
1292 "Are we in a directory browser buffer?"
8243c746
SM
1293 (derived-mode-p 'vc-dir-mode))
1294
1295(defun vc-dispatcher-in-fileset-p (fileset)
1296 (let ((member nil))
1297 (while (and (not member) fileset)
1298 (let ((elem (pop fileset)))
1299 (if (if (file-directory-p elem)
1300 (eq t (compare-strings buffer-file-name nil (length elem)
1301 elem nil nil))
1302 (eq (current-buffer) (get-file-buffer elem)))
713b6f81
SM
1303 (setq member t))))
1304 member))
b236ab0d 1305
86048828 1306(defun vc-dispatcher-selection-set ()
327c1d6a 1307 "Deduce a set of files to which to apply an operation. Return the fileset.
86048828 1308If we're in a directory display, the fileset is the list of marked files (if
327c1d6a
SM
1309there is one) else the file on the curreent line. If not in a directory
1310display, but the current buffer visits a file, the fileset is a singleton
1311containing that file. Otherwise, throw an error."
be636037 1312 (let ((files
327c1d6a
SM
1313 (cond
1314 ;; Browsing with vc-dir
1315 ((vc-dispatcher-browsing)
1316 (or (vc-dir-marked-files) (list (vc-dir-current-file))))
1317 ;; Visiting an eligible file
1318 ((buffer-file-name)
1319 (list buffer-file-name))
1320 ;; No eligible file -- if there's a parent buffer, deduce from there
1321 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1322 (with-current-buffer vc-parent-buffer
1323 (vc-dispatcher-browsing))))
1324 (with-current-buffer vc-parent-buffer
1325 (vc-dispatcher-selection-set)))
1326 ;; No good set here, throw error
1327 (t (error "No fileset is available here")))))
8243c746
SM
1328 ;; We assume, in order to avoid unpleasant surprises to the user,
1329 ;; that a fileset is not in good shape to be handed to the user if the
86048828 1330 ;; buffers visiting the fileset don't match the on-disk contents.
8243c746
SM
1331 ;; This is actually untrue for operations like `print-log' (or `diff'
1332 ;; between two revisions), so maybe this should be moved elsewhere.
1333 (save-some-buffers
327c1d6a 1334 nil (lambda () (vc-dispatcher-in-fileset-p files)))
be636037 1335 files))
b236ab0d 1336
3eb20677 1337;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246
b1ddeeb7 1338;;; vc-dispatcher.el ends here