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