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