Refill some long/short copyright headers.
[bpt/emacs.git] / lisp / vc / vc-dir.el
CommitLineData
74d0991f
DN
1;;; vc-dir.el --- Directory status display under VC
2
95df8112 3;; Copyright (C) 2007-2011 Free Software Foundation, Inc.
74d0991f
DN
4
5;; Author: Dan Nicolaescu <dann@ics.uci.edu>
9766adfb 6;; Keywords: vc tools
bd78fa1d 7;; Package: vc
74d0991f
DN
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24;;; Credits:
25
26;; The original VC directory status implementation was based on dired.
27;; This implementation was inspired by PCL-CVS.
28;; Many people contributed comments, ideas and code to this
29;; implementation. These include:
a1fc8acb 30;;
74d0991f
DN
31;; Alexandre Julliard <julliard@winehq.org>
32;; Stefan Monnier <monnier@iro.umontreal.ca>
33;; Tom Tromey <tromey@redhat.com>
34
35;;; Commentary:
a1fc8acb 36;;
74d0991f
DN
37
38;;; Todo: see vc.el.
39
40(require 'vc-hooks)
41(require 'vc)
50925e72 42(require 'tool-bar)
74d0991f
DN
43(require 'ewoc)
44
45;;; Code:
46(eval-when-compile
47 (require 'cl))
48
49(defcustom vc-dir-mode-hook nil
50 "Normal hook run by `vc-dir-mode'.
51See `run-hooks'."
52 :type 'hook
53 :group 'vc)
54
55;; Used to store information for the files displayed in the directory buffer.
56;; Each item displayed corresponds to one of these defstructs.
57(defstruct (vc-dir-fileinfo
58 (:copier nil)
59 (:type list) ;So we can use `member' on lists of FIs.
60 (:constructor
61 ;; We could define it as an alias for `list'.
62 vc-dir-create-fileinfo (name state &optional extra marked directory))
63 (:conc-name vc-dir-fileinfo->))
64 name ;Keep it as first, for `member'.
65 state
c4c0a44b 66 ;; For storing backend specific information.
74d0991f
DN
67 extra
68 marked
69 ;; To keep track of not updated files during a global refresh
70 needs-update
71 ;; To distinguish files and directories.
72 directory)
73
74d0991f 74(defvar vc-ewoc nil)
c4c0a44b 75
74d0991f
DN
76(defvar vc-dir-process-buffer nil
77 "The buffer used for the asynchronous call that computes status.")
78
c4c0a44b
DN
79(defvar vc-dir-backend nil
80 "The backend used by the current *vc-dir* buffer.")
81
74d0991f
DN
82(defun vc-dir-move-to-goal-column ()
83 ;; Used to keep the cursor on the file name column.
84 (beginning-of-line)
85 (unless (eolp)
13ad7457 86 ;; Must be in sync with vc-default-dir-printer.
74d0991f
DN
87 (forward-char 25)))
88
c4c0a44b 89(defun vc-dir-prepare-status-buffer (bname dir backend &optional create-new)
74d0991f 90 "Find a buffer named BNAME showing DIR, or create a new one."
4714a481 91 (setq dir (file-name-as-directory (expand-file-name dir)))
dcbbecd4
SM
92 (let* ;; Look for another buffer name BNAME visiting the same directory.
93 ((buf (save-excursion
94 (unless create-new
95 (dolist (buffer vc-dir-buffers)
96 (when (buffer-live-p buffer)
97 (set-buffer buffer)
98 (when (and (derived-mode-p 'vc-dir-mode)
99 (eq vc-dir-backend backend)
100 (string= default-directory dir))
101 (return buffer))))))))
74d0991f
DN
102 (or buf
103 ;; Create a new buffer named BNAME.
1dfae2a2
JB
104 ;; We pass a filename to create-file-buffer because it is what
105 ;; the function expects, and also what uniquify needs (if active)
106 (with-current-buffer (create-file-buffer (expand-file-name bname dir))
74d0991f
DN
107 (cd dir)
108 (vc-setup-buffer (current-buffer))
109 ;; Reset the vc-parent-buffer-name so that it does not appear
110 ;; in the mode-line.
111 (setq vc-parent-buffer-name nil)
112 (current-buffer)))))
113
114(defvar vc-dir-menu-map
115 (let ((map (make-sparse-keymap "VC-dir")))
116 (define-key map [quit]
117 '(menu-item "Quit" quit-window
118 :help "Quit"))
119 (define-key map [kill]
120 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
121 :enable (vc-dir-busy)
122 :help "Kill the command that updates the directory buffer"))
123 (define-key map [refresh]
c4c0a44b 124 '(menu-item "Refresh" revert-buffer
74d0991f
DN
125 :enable (not (vc-dir-busy))
126 :help "Refresh the contents of the directory buffer"))
c4c0a44b
DN
127 (define-key map [remup]
128 '(menu-item "Hide up-to-date" vc-dir-hide-up-to-date
129 :help "Hide up-to-date items from display"))
74d0991f
DN
130 ;; Movement.
131 (define-key map [sepmv] '("--"))
132 (define-key map [next-line]
133 '(menu-item "Next line" vc-dir-next-line
134 :help "Go to the next line" :keys "n"))
135 (define-key map [previous-line]
136 '(menu-item "Previous line" vc-dir-previous-line
137 :help "Go to the previous line"))
138 ;; Marking.
139 (define-key map [sepmrk] '("--"))
140 (define-key map [unmark-all]
141 '(menu-item "Unmark All" vc-dir-unmark-all-files
142 :help "Unmark all files that are in the same state as the current file\
143\nWith prefix argument unmark all files"))
144 (define-key map [unmark-previous]
145 '(menu-item "Unmark previous " vc-dir-unmark-file-up
146 :help "Move to the previous line and unmark the file"))
147
148 (define-key map [mark-all]
149 '(menu-item "Mark All" vc-dir-mark-all-files
150 :help "Mark all files that are in the same state as the current file\
151\nWith prefix argument mark all files"))
152 (define-key map [unmark]
153 '(menu-item "Unmark" vc-dir-unmark
154 :help "Unmark the current file or all files in the region"))
155
156 (define-key map [mark]
157 '(menu-item "Mark" vc-dir-mark
158 :help "Mark the current file or all files in the region"))
159
160 (define-key map [sepopn] '("--"))
f8b4aa41 161 (define-key map [qr]
ba7e37c8 162 '(menu-item "Query Replace in Files..." vc-dir-query-replace-regexp
f8b4aa41 163 :help "Replace a string in the marked files"))
834d998e 164 (define-key map [se]
ba7e37c8 165 '(menu-item "Search Files..." vc-dir-search
834d998e 166 :help "Search a regexp in the marked files"))
ba7e37c8
DN
167 (define-key map [ires]
168 '(menu-item "Isearch Regexp Files..." vc-dir-isearch-regexp
169 :help "Incremental search a regexp in the marked files"))
170 (define-key map [ise]
171 '(menu-item "Isearch Files..." vc-dir-isearch
172 :help "Incremental search a string in the marked files"))
74d0991f
DN
173 (define-key map [open-other]
174 '(menu-item "Open in other window" vc-dir-find-file-other-window
175 :help "Find the file on the current line, in another window"))
176 (define-key map [open]
177 '(menu-item "Open file" vc-dir-find-file
178 :help "Find the file on the current line"))
c4c0a44b
DN
179 (define-key map [sepvcdet] '("--"))
180 ;; FIXME: This needs a key binding. And maybe a better name
181 ;; ("Insert" like PCL-CVS uses does not sound that great either)...
182 (define-key map [ins]
183 '(menu-item "Show File" vc-dir-show-fileentry
184 :help "Show a file in the VC status listing even though it might be up to date"))
185 (define-key map [annotate]
186 '(menu-item "Annotate" vc-annotate
187 :help "Display the edit history of the current file using colors"))
188 (define-key map [diff]
189 '(menu-item "Compare with Base Version" vc-diff
190 :help "Compare file set with the base version"))
54d3626e
DN
191 (define-key map [logo]
192 '(menu-item "Show Outgoing Log" vc-log-outgoing
193 :help "Show a log of changes that will be sent with a push operation"))
194 (define-key map [logi]
195 '(menu-item "Show Incoming Log" vc-log-incoming
196 :help "Show a log of changes that will be received with a pull operation"))
c4c0a44b 197 (define-key map [log]
900503ae 198 '(menu-item "Show History" vc-print-log
54d3626e
DN
199 :help "List the change log of the current file set in a window"))
200 (define-key map [rlog]
201 '(menu-item "Show Top of the Tree History " vc-print-root-log
202 :help "List the change log for the current tree in a window"))
c4c0a44b
DN
203 ;; VC commands.
204 (define-key map [sepvccmd] '("--"))
205 (define-key map [update]
206 '(menu-item "Update to latest version" vc-update
207 :help "Update the current fileset's files to their tip revisions"))
208 (define-key map [revert]
209 '(menu-item "Revert to base version" vc-revert
210 :help "Revert working copies of the selected fileset to their repository contents."))
211 (define-key map [next-action]
212 ;; FIXME: This really really really needs a better name!
213 ;; And a key binding too.
214 '(menu-item "Check In/Out" vc-next-action
215 :help "Do the next logical version control operation on the current fileset"))
216 (define-key map [register]
217 '(menu-item "Register" vc-register
218 :help "Register file set into the version control system"))
74d0991f 219 map)
426bf359 220 "Menu for VC dir.")
74d0991f 221
c4c0a44b
DN
222;; VC backends can use this to add mode-specific menu items to
223;; vc-dir-menu-map.
74d0991f
DN
224(defun vc-dir-menu-map-filter (orig-binding)
225 (when (and (symbolp orig-binding) (fboundp orig-binding))
226 (setq orig-binding (indirect-function orig-binding)))
227 (let ((ext-binding
c4c0a44b
DN
228 (when (derived-mode-p 'vc-dir-mode)
229 (vc-call-backend vc-dir-backend 'extra-status-menu))))
74d0991f
DN
230 (if (null ext-binding)
231 orig-binding
232 (append orig-binding
233 '("----")
234 ext-binding))))
235
236(defvar vc-dir-mode-map
f8b4aa41 237 (let ((map (make-sparse-keymap)))
c4c0a44b
DN
238 ;; VC commands
239 (define-key map "v" 'vc-next-action) ;; C-x v v
240 (define-key map "=" 'vc-diff) ;; C-x v =
241 (define-key map "i" 'vc-register) ;; C-x v i
242 (define-key map "+" 'vc-update) ;; C-x v +
243 (define-key map "l" 'vc-print-log) ;; C-x v l
244 ;; More confusing than helpful, probably
db18c5fd
SS
245 ;;(define-key map "R" 'vc-revert) ;; u is taken by vc-dir-unmark.
246 ;;(define-key map "A" 'vc-annotate) ;; g is taken by revert-buffer
247 ;; bound by `special-mode'.
74d0991f
DN
248 ;; Marking.
249 (define-key map "m" 'vc-dir-mark)
250 (define-key map "M" 'vc-dir-mark-all-files)
251 (define-key map "u" 'vc-dir-unmark)
252 (define-key map "U" 'vc-dir-unmark-all-files)
253 (define-key map "\C-?" 'vc-dir-unmark-file-up)
254 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
255 ;; Movement.
256 (define-key map "n" 'vc-dir-next-line)
257 (define-key map " " 'vc-dir-next-line)
258 (define-key map "\t" 'vc-dir-next-directory)
259 (define-key map "p" 'vc-dir-previous-line)
260 (define-key map [backtab] 'vc-dir-previous-directory)
261 ;;; Rebind paragraph-movement commands.
262 (define-key map "\M-}" 'vc-dir-next-directory)
263 (define-key map "\M-{" 'vc-dir-previous-directory)
264 (define-key map [C-down] 'vc-dir-next-directory)
265 (define-key map [C-up] 'vc-dir-previous-directory)
266 ;; The remainder.
267 (define-key map "f" 'vc-dir-find-file)
268 (define-key map "\C-m" 'vc-dir-find-file)
269 (define-key map "o" 'vc-dir-find-file-other-window)
74d0991f
DN
270 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
271 (define-key map [down-mouse-3] 'vc-dir-menu)
272 (define-key map [mouse-2] 'vc-dir-toggle-mark)
d03b3d23 273 (define-key map [follow-link] 'mouse-face)
c4c0a44b 274 (define-key map "x" 'vc-dir-hide-up-to-date)
a544e7c1 275 (define-key map [?\C-k] 'vc-dir-kill-line)
834d998e 276 (define-key map "S" 'vc-dir-search) ;; FIXME: Maybe use A like dired?
f8b4aa41 277 (define-key map "Q" 'vc-dir-query-replace-regexp)
ba7e37c8
DN
278 (define-key map (kbd "M-s a C-s") 'vc-dir-isearch)
279 (define-key map (kbd "M-s a M-C-s") 'vc-dir-isearch-regexp)
74d0991f
DN
280
281 ;; Hook up the menu.
282 (define-key map [menu-bar vc-dir-mode]
283 `(menu-item
c4c0a44b
DN
284 ;; VC backends can use this to add mode-specific menu items to
285 ;; vc-dir-menu-map.
74d0991f
DN
286 "VC-dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
287 map)
288 "Keymap for directory buffer.")
289
7995501c 290(defmacro vc-dir-at-event (event &rest body)
426bf359
JB
291 "Evaluate BODY with point located at event-start of EVENT.
292If BODY uses EVENT, it should be a variable,
74d0991f 293 otherwise it will be evaluated twice."
7995501c 294 (let ((posn (make-symbol "vc-dir-at-event-posn")))
5da5a66f
SS
295 `(save-excursion
296 (unless (equal ,event '(tool-bar))
297 (let ((,posn (event-start ,event)))
298 (set-buffer (window-buffer (posn-window ,posn)))
299 (goto-char (posn-point ,posn))))
300 ,@body)))
74d0991f
DN
301
302(defun vc-dir-menu (e)
db18c5fd 303 "Popup the VC dir menu."
74d0991f 304 (interactive "e")
7995501c 305 (vc-dir-at-event e (popup-menu vc-dir-menu-map e)))
74d0991f
DN
306
307(defvar vc-dir-tool-bar-map
308 (let ((map (make-sparse-keymap)))
900503ae
CY
309 (tool-bar-local-item-from-menu 'find-file "new" map nil
310 :label "New File" :vert-only t)
311 (tool-bar-local-item-from-menu 'menu-find-file-existing "open" map nil
312 :label "Open" :vert-only t)
313 (tool-bar-local-item-from-menu 'dired "diropen" map nil
314 :vert-only t)
315 (tool-bar-local-item-from-menu 'quit-window "close" map vc-dir-mode-map
316 :vert-only t)
317 (tool-bar-local-item-from-menu 'vc-next-action "saveas" map
318 vc-dir-mode-map :label "Commit")
74d0991f 319 (tool-bar-local-item-from-menu 'vc-print-log "info"
900503ae
CY
320 map vc-dir-mode-map
321 :label "Log")
322 (define-key-after map [separator-1] menu-bar-separator)
74d0991f 323 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
f904c0f9 324 map vc-dir-mode-map
900503ae
CY
325 :label "Stop" :vert-only t)
326 (tool-bar-local-item-from-menu 'revert-buffer "refresh"
327 map vc-dir-mode-map :vert-only t)
328 (define-key-after map [separator-2] menu-bar-separator)
329 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [cut])
330 "cut" map nil :vert-only t)
331 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [copy])
332 "copy" map nil :vert-only t)
333 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [paste])
334 "paste" map nil :vert-only t)
335 (define-key-after map [separator-3] menu-bar-separator)
336 (tool-bar-local-item-from-menu 'isearch-forward
337 "search" map nil
338 :label "Search" :vert-only t)
74d0991f
DN
339 map))
340
341(defun vc-dir-node-directory (node)
342 ;; Compute the directory for NODE.
b4dc7d98 343 ;; If it's a directory node, get it from the node.
74d0991f
DN
344 (let ((data (ewoc-data node)))
345 (or (vc-dir-fileinfo->directory data)
346 ;; Otherwise compute it from the file name.
347 (file-name-directory
b8e54362
DN
348 (directory-file-name
349 (expand-file-name
350 (vc-dir-fileinfo->name data)))))))
74d0991f
DN
351
352(defun vc-dir-update (entries buffer &optional noinsert)
353 "Update BUFFER's ewoc from the list of ENTRIES.
354If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
355 ;; Add ENTRIES to the vc-dir buffer BUFFER.
356 (with-current-buffer buffer
357 ;; Insert the entries sorted by name into the ewoc.
358 ;; We assume the ewoc is sorted too, which should be the
359 ;; case if we always add entries with vc-dir-update.
360 (setq entries
361 ;; Sort: first files and then subdirectories.
362 ;; XXX: this is VERY inefficient, it computes the directory
363 ;; names too many times
364 (sort entries
365 (lambda (entry1 entry2)
b8e54362
DN
366 (let ((dir1 (file-name-directory
367 (directory-file-name (expand-file-name (car entry1)))))
368 (dir2 (file-name-directory
369 (directory-file-name (expand-file-name (car entry2))))))
74d0991f
DN
370 (cond
371 ((string< dir1 dir2) t)
372 ((not (string= dir1 dir2)) nil)
373 ((string< (car entry1) (car entry2))))))))
374 ;; Insert directory entries in the right places.
375 (let ((entry (car entries))
02ffe8e3 376 (node (ewoc-nth vc-ewoc 0))
290736f2 377 (to-remove nil)
02ffe8e3 378 (dotname (file-relative-name default-directory)))
74d0991f
DN
379 ;; Insert . if it is not present.
380 (unless node
02ffe8e3
DN
381 (ewoc-enter-last
382 vc-ewoc (vc-dir-create-fileinfo
dcbbecd4 383 dotname nil nil nil default-directory))
74d0991f 384 (setq node (ewoc-nth vc-ewoc 0)))
a1fc8acb 385
74d0991f
DN
386 (while (and entry node)
387 (let* ((entryfile (car entry))
b8e54362
DN
388 (entrydir (file-name-directory (directory-file-name
389 (expand-file-name entryfile))))
74d0991f
DN
390 (nodedir (vc-dir-node-directory node)))
391 (cond
392 ;; First try to find the directory.
393 ((string-lessp nodedir entrydir)
394 (setq node (ewoc-next vc-ewoc node)))
395 ((string-equal nodedir entrydir)
396 ;; Found the directory, find the place for the file name.
397 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
398 (cond
02ffe8e3
DN
399 ((string= nodefile dotname)
400 (setq node (ewoc-next vc-ewoc node)))
74d0991f
DN
401 ((string-lessp nodefile entryfile)
402 (setq node (ewoc-next vc-ewoc node)))
403 ((string-equal nodefile entryfile)
290736f2
DN
404 (if (nth 1 entry)
405 (progn
406 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
407 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
408 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
409 (ewoc-invalidate vc-ewoc node))
410 ;; If the state is nil, the file does not exist
411 ;; anymore, so remember the entry so we can remove
412 ;; it after we are done inserting all ENTRIES.
413 (push node to-remove))
a1fc8acb 414 (setq entries (cdr entries))
74d0991f
DN
415 (setq entry (car entries))
416 (setq node (ewoc-next vc-ewoc node)))
417 (t
d0cb23ca
DN
418 (unless noinsert
419 (ewoc-enter-before vc-ewoc node
420 (apply 'vc-dir-create-fileinfo entry)))
74d0991f
DN
421 (setq entries (cdr entries))
422 (setq entry (car entries))))))
423 (t
d0cb23ca
DN
424 (unless noinsert
425 ;; We might need to insert a directory node if the
426 ;; previous node was in a different directory.
427 (let* ((rd (file-relative-name entrydir))
428 (prev-node (ewoc-prev vc-ewoc node))
429 (prev-dir (vc-dir-node-directory prev-node)))
430 (unless (string-equal entrydir prev-dir)
431 (ewoc-enter-before
432 vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
433 ;; Now insert the node itself.
434 (ewoc-enter-before vc-ewoc node
435 (apply 'vc-dir-create-fileinfo entry)))
74d0991f
DN
436 (setq entries (cdr entries) entry (car entries))))))
437 ;; We're past the last node, all remaining entries go to the end.
438 (unless (or node noinsert)
439 (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
440 (dolist (entry entries)
b8e54362
DN
441 (let ((entrydir (file-name-directory
442 (directory-file-name (expand-file-name (car entry))))))
74d0991f
DN
443 ;; Insert a directory node if needed.
444 (unless (string-equal lastdir entrydir)
445 (setq lastdir entrydir)
446 (let ((rd (file-relative-name entrydir)))
447 (ewoc-enter-last
448 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
449 ;; Now insert the node itself.
450 (ewoc-enter-last vc-ewoc
290736f2
DN
451 (apply 'vc-dir-create-fileinfo entry))))))
452 (when to-remove
453 (let ((inhibit-read-only t))
454 (apply 'ewoc-delete vc-ewoc (nreverse to-remove)))))))
74d0991f
DN
455
456(defun vc-dir-busy ()
457 (and (buffer-live-p vc-dir-process-buffer)
458 (get-buffer-process vc-dir-process-buffer)))
459
460(defun vc-dir-kill-dir-status-process ()
461 "Kill the temporary buffer and associated process."
462 (interactive)
463 (when (buffer-live-p vc-dir-process-buffer)
464 (let ((proc (get-buffer-process vc-dir-process-buffer)))
465 (when proc (delete-process proc))
466 (setq vc-dir-process-buffer nil)
467 (setq mode-line-process nil))))
468
469(defun vc-dir-kill-query ()
470 ;; Make sure that when the status buffer is killed the update
471 ;; process running in background is also killed.
472 (if (vc-dir-busy)
473 (when (y-or-n-p "Status update process running, really kill status buffer? ")
474 (vc-dir-kill-dir-status-process)
475 t)
476 t))
477
478(defun vc-dir-next-line (arg)
479 "Go to the next line.
480If a prefix argument is given, move by that many lines."
481 (interactive "p")
482 (with-no-warnings
483 (ewoc-goto-next vc-ewoc arg)
484 (vc-dir-move-to-goal-column)))
485
486(defun vc-dir-previous-line (arg)
487 "Go to the previous line.
488If a prefix argument is given, move by that many lines."
489 (interactive "p")
490 (ewoc-goto-prev vc-ewoc arg)
491 (vc-dir-move-to-goal-column))
492
493(defun vc-dir-next-directory ()
494 "Go to the next directory."
495 (interactive)
496 (let ((orig (point)))
497 (if
498 (catch 'foundit
499 (while t
500 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
501 (cond ((not next)
502 (throw 'foundit t))
503 (t
504 (progn
505 (ewoc-goto-node vc-ewoc next)
506 (vc-dir-move-to-goal-column)
507 (if (vc-dir-fileinfo->directory (ewoc-data next))
508 (throw 'foundit nil))))))))
509 (goto-char orig))))
510
511(defun vc-dir-previous-directory ()
512 "Go to the previous directory."
513 (interactive)
514 (let ((orig (point)))
515 (if
516 (catch 'foundit
517 (while t
518 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
519 (cond ((not prev)
520 (throw 'foundit t))
521 (t
522 (progn
523 (ewoc-goto-node vc-ewoc prev)
524 (vc-dir-move-to-goal-column)
525 (if (vc-dir-fileinfo->directory (ewoc-data prev))
526 (throw 'foundit nil))))))))
527 (goto-char orig))))
528
529(defun vc-dir-mark-unmark (mark-unmark-function)
530 (if (use-region-p)
531 (let ((firstl (line-number-at-pos (region-beginning)))
532 (lastl (line-number-at-pos (region-end))))
533 (save-excursion
534 (goto-char (region-beginning))
535 (while (<= (line-number-at-pos) lastl)
536 (funcall mark-unmark-function))))
537 (funcall mark-unmark-function)))
538
74d0991f
DN
539(defun vc-dir-parent-marked-p (arg)
540 ;; Return nil if none of the parent directories of arg is marked.
541 (let* ((argdir (vc-dir-node-directory arg))
542 (arglen (length argdir))
543 (crt arg)
544 data dir)
545 ;; Go through the predecessors, checking if any directory that is
546 ;; a parent is marked.
547 (while (setq crt (ewoc-prev vc-ewoc crt))
548 (setq data (ewoc-data crt))
549 (setq dir (vc-dir-node-directory crt))
550 (when (and (vc-dir-fileinfo->directory data)
551 (vc-string-prefix-p dir argdir))
552 (when (vc-dir-fileinfo->marked data)
553 (error "Cannot mark `%s', parent directory `%s' marked"
554 (vc-dir-fileinfo->name (ewoc-data arg))
555 (vc-dir-fileinfo->name data)))))
556 nil))
557
558(defun vc-dir-children-marked-p (arg)
559 ;; Return nil if none of the children of arg is marked.
560 (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
561 (is-child t)
562 (crt arg)
563 data dir)
564 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
565 (setq data (ewoc-data crt))
566 (setq dir (vc-dir-node-directory crt))
567 (if (string-match argdir-re dir)
568 (when (vc-dir-fileinfo->marked data)
569 (error "Cannot mark `%s', child `%s' marked"
570 (vc-dir-fileinfo->name (ewoc-data arg))
571 (vc-dir-fileinfo->name data)))
572 ;; We are done, we got to an entry that is not a child of `arg'.
573 (setq is-child nil)))
574 nil))
575
576(defun vc-dir-mark-file (&optional arg)
577 ;; Mark ARG or the current file and move to the next line.
578 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
579 (file (ewoc-data crt))
580 (isdir (vc-dir-fileinfo->directory file)))
581 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
582 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
583 (setf (vc-dir-fileinfo->marked file) t)
584 (ewoc-invalidate vc-ewoc crt)
585 (unless (or arg (mouse-event-p last-command-event))
586 (vc-dir-next-line 1)))))
587
588(defun vc-dir-mark ()
589 "Mark the current file or all files in the region.
590If the region is active, mark all the files in the region.
591Otherwise mark the file on the current line and move to the next
592line."
593 (interactive)
594 (vc-dir-mark-unmark 'vc-dir-mark-file))
595
596(defun vc-dir-mark-all-files (arg)
597 "Mark all files with the same state as the current one.
598With a prefix argument mark all files.
599If the current entry is a directory, mark all child files.
600
601The commands operate on files that are on the same state.
602This command is intended to make it easy to select all files that
603share the same state."
604 (interactive "P")
605 (if arg
606 ;; Mark all files.
607 (progn
608 ;; First check that no directory is marked, we can't mark
609 ;; files in that case.
610 (ewoc-map
611 (lambda (filearg)
612 (when (and (vc-dir-fileinfo->directory filearg)
613 (vc-dir-fileinfo->marked filearg))
614 (error "Cannot mark all files, directory `%s' marked"
615 (vc-dir-fileinfo->name filearg))))
616 vc-ewoc)
617 (ewoc-map
618 (lambda (filearg)
619 (unless (vc-dir-fileinfo->marked filearg)
620 (setf (vc-dir-fileinfo->marked filearg) t)
621 t))
622 vc-ewoc))
623 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
624 (if (vc-dir-fileinfo->directory data)
625 ;; It's a directory, mark child files.
626 (let ((crt (ewoc-locate vc-ewoc)))
627 (unless (vc-dir-children-marked-p crt)
628 (while (setq crt (ewoc-next vc-ewoc crt))
629 (let ((crt-data (ewoc-data crt)))
630 (unless (vc-dir-fileinfo->directory crt-data)
631 (setf (vc-dir-fileinfo->marked crt-data) t)
632 (ewoc-invalidate vc-ewoc crt))))))
633 ;; It's a file
634 (let ((state (vc-dir-fileinfo->state data))
635 (crt (ewoc-nth vc-ewoc 0)))
636 (while crt
637 (let ((crt-data (ewoc-data crt)))
638 (when (and (not (vc-dir-fileinfo->marked crt-data))
639 (eq (vc-dir-fileinfo->state crt-data) state)
640 (not (vc-dir-fileinfo->directory crt-data)))
641 (vc-dir-mark-file crt)))
642 (setq crt (ewoc-next vc-ewoc crt))))))))
643
644(defun vc-dir-unmark-file ()
645 ;; Unmark the current file and move to the next line.
646 (let* ((crt (ewoc-locate vc-ewoc))
647 (file (ewoc-data crt)))
648 (setf (vc-dir-fileinfo->marked file) nil)
649 (ewoc-invalidate vc-ewoc crt)
650 (unless (mouse-event-p last-command-event)
651 (vc-dir-next-line 1))))
652
653(defun vc-dir-unmark ()
654 "Unmark the current file or all files in the region.
655If the region is active, unmark all the files in the region.
656Otherwise mark the file on the current line and move to the next
657line."
658 (interactive)
659 (vc-dir-mark-unmark 'vc-dir-unmark-file))
660
661(defun vc-dir-unmark-file-up ()
662 "Move to the previous line and unmark the file."
663 (interactive)
664 ;; If we're on the first line, we won't move up, but we will still
665 ;; remove the mark. This seems a bit odd but it is what buffer-menu
666 ;; does.
667 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
668 (file (ewoc-data prev)))
669 (setf (vc-dir-fileinfo->marked file) nil)
670 (ewoc-invalidate vc-ewoc prev)
671 (vc-dir-move-to-goal-column)))
672
673(defun vc-dir-unmark-all-files (arg)
674 "Unmark all files with the same state as the current one.
675With a prefix argument unmark all files.
676If the current entry is a directory, unmark all the child files.
677
678The commands operate on files that are on the same state.
679This command is intended to make it easy to deselect all files
680that share the same state."
681 (interactive "P")
682 (if arg
683 (ewoc-map
684 (lambda (filearg)
685 (when (vc-dir-fileinfo->marked filearg)
686 (setf (vc-dir-fileinfo->marked filearg) nil)
687 t))
688 vc-ewoc)
689 (let* ((crt (ewoc-locate vc-ewoc))
690 (data (ewoc-data crt)))
691 (if (vc-dir-fileinfo->directory data)
692 ;; It's a directory, unmark child files.
693 (while (setq crt (ewoc-next vc-ewoc crt))
694 (let ((crt-data (ewoc-data crt)))
695 (unless (vc-dir-fileinfo->directory crt-data)
696 (setf (vc-dir-fileinfo->marked crt-data) nil)
697 (ewoc-invalidate vc-ewoc crt))))
698 ;; It's a file
699 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
700 (ewoc-map
701 (lambda (filearg)
702 (when (and (vc-dir-fileinfo->marked filearg)
703 (eq (vc-dir-fileinfo->state filearg) crt-state))
704 (setf (vc-dir-fileinfo->marked filearg) nil)
705 t))
706 vc-ewoc))))))
707
708(defun vc-dir-toggle-mark-file ()
709 (let* ((crt (ewoc-locate vc-ewoc))
710 (file (ewoc-data crt)))
711 (if (vc-dir-fileinfo->marked file)
712 (vc-dir-unmark-file)
713 (vc-dir-mark-file))))
714
715(defun vc-dir-toggle-mark (e)
716 (interactive "e")
7995501c 717 (vc-dir-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
74d0991f
DN
718
719(defun vc-dir-delete-file ()
720 "Delete the marked files, or the current file if no marks."
721 (interactive)
722 (mapc 'vc-delete-file (or (vc-dir-marked-files)
723 (list (vc-dir-current-file)))))
724
725(defun vc-dir-find-file ()
726 "Find the file on the current line."
727 (interactive)
728 (find-file (vc-dir-current-file)))
729
f8d5a47f 730(defun vc-dir-find-file-other-window (&optional event)
74d0991f 731 "Find the file on the current line, in another window."
f8d5a47f
NR
732 (interactive (list last-nonmenu-event))
733 (if event (posn-set-point (event-end event)))
74d0991f
DN
734 (find-file-other-window (vc-dir-current-file)))
735
ba7e37c8
DN
736(defun vc-dir-isearch ()
737 "Search for a string through all marked buffers using Isearch."
738 (interactive)
739 (multi-isearch-files
740 (mapcar 'car (vc-dir-marked-only-files-and-states))))
741
742(defun vc-dir-isearch-regexp ()
743 "Search for a regexp through all marked buffers using Isearch."
744 (interactive)
745 (multi-isearch-files-regexp
746 (mapcar 'car (vc-dir-marked-only-files-and-states))))
747
834d998e
DN
748(defun vc-dir-search (regexp)
749 "Search through all marked files for a match for REGEXP.
750For marked directories, use the files displayed from those directories.
751Stops when a match is found.
752To continue searching for next match, use command \\[tags-loop-continue]."
753 (interactive "sSearch marked files (regexp): ")
754 (tags-search regexp '(mapcar 'car (vc-dir-marked-only-files-and-states))))
755
f8b4aa41
DN
756(defun vc-dir-query-replace-regexp (from to &optional delimited)
757 "Do `query-replace-regexp' of FROM with TO, on all marked files.
758If a directory is marked, then use the files displayed for that directory.
759Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
760If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
761with the command \\[tags-loop-continue]."
ac73d955 762 ;; FIXME: this is almost a copy of `dired-do-query-replace-regexp'. This
f8b4aa41
DN
763 ;; should probably be made generic and used in both places instead of
764 ;; duplicating it here.
765 (interactive
766 (let ((common
767 (query-replace-read-args
768 "Query replace regexp in marked files" t t)))
769 (list (nth 0 common) (nth 1 common) (nth 2 common))))
770 (dolist (file (mapcar 'car (vc-dir-marked-only-files-and-states)))
771 (let ((buffer (get-file-buffer file)))
772 (if (and buffer (with-current-buffer buffer
773 buffer-read-only))
774 (error "File `%s' is visited read-only" file))))
775 (tags-query-replace from to delimited
776 '(mapcar 'car (vc-dir-marked-only-files-and-states))))
777
74d0991f
DN
778(defun vc-dir-current-file ()
779 (let ((node (ewoc-locate vc-ewoc)))
780 (unless node
781 (error "No file available"))
782 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
783
784(defun vc-dir-marked-files ()
785 "Return the list of marked files."
786 (mapcar
787 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
788 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
789
2913a58d
DN
790(defun vc-dir-marked-only-files-and-states ()
791 "Return the list of conses (FILE . STATE) for the marked files.
792For marked directories return the corresponding conses for the
793child files."
74d0991f
DN
794 (let ((crt (ewoc-nth vc-ewoc 0))
795 result)
796 (while crt
797 (let ((crt-data (ewoc-data crt)))
798 (if (vc-dir-fileinfo->marked crt-data)
2913a58d 799 ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
74d0991f
DN
800 (if (vc-dir-fileinfo->directory crt-data)
801 (let* ((dir (vc-dir-fileinfo->directory crt-data))
802 (dirlen (length dir))
803 data)
804 (while
805 (and (setq crt (ewoc-next vc-ewoc crt))
806 (vc-string-prefix-p dir
807 (progn
808 (setq data (ewoc-data crt))
809 (vc-dir-node-directory crt))))
810 (unless (vc-dir-fileinfo->directory data)
a1fc8acb 811 (push
2913a58d
DN
812 (cons (expand-file-name (vc-dir-fileinfo->name data))
813 (vc-dir-fileinfo->state data))
814 result))))
815 (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
816 (vc-dir-fileinfo->state crt-data))
817 result)
74d0991f
DN
818 (setq crt (ewoc-next vc-ewoc crt)))
819 (setq crt (ewoc-next vc-ewoc crt)))))
9c6c6d1f 820 (nreverse result)))
74d0991f 821
2913a58d
DN
822(defun vc-dir-child-files-and-states ()
823 "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
824If it is a file, return the corresponding cons for the file itself."
74d0991f
DN
825 (let* ((crt (ewoc-locate vc-ewoc))
826 (crt-data (ewoc-data crt))
827 result)
828 (if (vc-dir-fileinfo->directory crt-data)
829 (let* ((dir (vc-dir-fileinfo->directory crt-data))
830 (dirlen (length dir))
831 data)
832 (while
833 (and (setq crt (ewoc-next vc-ewoc crt))
834 (vc-string-prefix-p dir (progn
835 (setq data (ewoc-data crt))
836 (vc-dir-node-directory crt))))
837 (unless (vc-dir-fileinfo->directory data)
a1fc8acb 838 (push
2913a58d
DN
839 (cons (expand-file-name (vc-dir-fileinfo->name data))
840 (vc-dir-fileinfo->state data))
841 result))))
a1fc8acb 842 (push
2913a58d
DN
843 (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
844 (vc-dir-fileinfo->state crt-data)) result))
9c6c6d1f 845 (nreverse result)))
74d0991f 846
d923f4ac
DN
847(defun vc-dir-recompute-file-state (fname def-dir)
848 (let* ((file-short (file-relative-name fname def-dir))
776f7a5f
DN
849 (remove-me-when-CVS-works
850 (when (eq vc-dir-backend 'CVS)
851 ;; FIXME: Warning: UGLY HACK. The CVS backend caches the state
852 ;; info, this forces the backend to update it.
276db9eb 853 (vc-call-backend vc-dir-backend 'registered fname)))
d923f4ac
DN
854 (state (vc-call-backend vc-dir-backend 'state fname))
855 (extra (vc-call-backend vc-dir-backend
856 'status-fileinfo-extra fname)))
857 (list file-short state extra)))
858
859(defun vc-dir-find-child-files (dirname)
860 ;; Give a DIRNAME string return the list of all child files shown in
861 ;; the current *vc-dir* buffer.
862 (let ((crt (ewoc-nth vc-ewoc 0))
863 children
864 dname)
865 ;; Find DIR
866 (while (and crt (not (vc-string-prefix-p
867 dirname (vc-dir-node-directory crt))))
868 (setq crt (ewoc-next vc-ewoc crt)))
869 (while (and crt (vc-string-prefix-p
870 dirname
871 (setq dname (vc-dir-node-directory crt))))
872 (let ((data (ewoc-data crt)))
873 (unless (vc-dir-fileinfo->directory data)
874 (push (expand-file-name (vc-dir-fileinfo->name data)) children)))
875 (setq crt (ewoc-next vc-ewoc crt)))
876 children))
877
878(defun vc-dir-resync-directory-files (dirname)
879 ;; Update the entries for all the child files of DIRNAME shown in
880 ;; the current *vc-dir* buffer.
881 (let ((files (vc-dir-find-child-files dirname))
dcbbecd4 882 (ddir default-directory)
d923f4ac
DN
883 fileentries)
884 (when files
885 (dolist (crt files)
886 (push (vc-dir-recompute-file-state crt ddir)
887 fileentries))
888 (vc-dir-update fileentries (current-buffer)))))
889
74d0991f 890(defun vc-dir-resynch-file (&optional fname)
426bf359 891 "Update the entries for FNAME in any directory buffers that list it."
d923f4ac 892 (let ((file (or fname (expand-file-name buffer-file-name)))
dcbbecd4
SM
893 (drop '()))
894 (save-current-buffer
895 ;; look for a vc-dir buffer that might show this file.
896 (dolist (status-buf vc-dir-buffers)
897 (if (not (buffer-live-p status-buf))
898 (push status-buf drop)
899 (set-buffer status-buf)
900 (if (not (derived-mode-p 'vc-dir-mode))
901 (push status-buf drop)
902 (let ((ddir default-directory))
903 (when (vc-string-prefix-p ddir file)
904 (if (file-directory-p file)
4d0bbcb6
DN
905 (progn
906 (vc-dir-resync-directory-files file)
907 (ewoc-set-hf vc-ewoc
908 (vc-dir-headers vc-dir-backend default-directory) ""))
d0cb23ca
DN
909 (let* ((complete-state (vc-dir-recompute-file-state file ddir))
910 (state (cadr complete-state)))
dcbbecd4 911 (vc-dir-update
d0cb23ca
DN
912 (list complete-state)
913 status-buf (or (not state)
914 (eq state 'up-to-date)))))))))))
dcbbecd4
SM
915 ;; Remove out-of-date entries from vc-dir-buffers.
916 (dolist (b drop) (setq vc-dir-buffers (delq b vc-dir-buffers)))))
74d0991f 917
c4c0a44b
DN
918(defvar use-vc-backend) ;; dynamically bound
919
920(define-derived-mode vc-dir-mode special-mode "VC dir"
db18c5fd 921 "Major mode for VC directory buffers.
74d0991f 922Marking/Unmarking key bindings and actions:
ae42a852
DN
923m - mark a file/directory
924 - if the region is active, mark all the files in region.
74d0991f
DN
925 Restrictions: - a file cannot be marked if any parent directory is marked
926 - a directory cannot be marked if any child file or
927 directory is marked
ae42a852
DN
928u - unmark a file/directory
929 - if the region is active, unmark all the files in region.
74d0991f
DN
930M - if the cursor is on a file: mark all the files with the same state as
931 the current file
932 - if the cursor is on a directory: mark all child files
933 - with a prefix argument: mark all files
934U - if the cursor is on a file: unmark all the files with the same state
935 as the current file
936 - if the cursor is on a directory: unmark all child files
937 - with a prefix argument: unmark all files
ae42a852 938mouse-2 - toggles the mark state
74d0991f 939
db18c5fd 940VC commands
ae42a852
DN
941VC commands in the `C-x v' prefix can be used.
942VC commands act on the marked entries. If nothing is marked, VC
943commands act on the current entry.
db18c5fd
SS
944
945Search & Replace
946S - searches the marked files
947Q - does a query replace on the marked files
948M-s a C-s - does an isearch on the marked files
49c2119d 949M-s a C-M-s - does a regexp isearch on the marked files
db18c5fd
SS
950If nothing is marked, these commands act on the current entry.
951When a directory is current or marked, the Search & Replace
ae42a852
DN
952commands act on the child files of that directory that are displayed in
953the *vc-dir* buffer.
74d0991f
DN
954
955\\{vc-dir-mode-map}"
c4c0a44b 956 (set (make-local-variable 'vc-dir-backend) use-vc-backend)
74d0991f 957 (setq buffer-read-only t)
c4c0a44b
DN
958 (when (boundp 'tool-bar-map)
959 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
74d0991f
DN
960 (let ((buffer-read-only nil))
961 (erase-buffer)
962 (set (make-local-variable 'vc-dir-process-buffer) nil)
13ad7457 963 (set (make-local-variable 'vc-ewoc) (ewoc-create #'vc-dir-printer))
c4c0a44b
DN
964 (set (make-local-variable 'revert-buffer-function)
965 'vc-dir-revert-buffer-function)
1dfae2a2 966 (setq list-buffers-directory (expand-file-name "*vc-dir*" default-directory))
dcbbecd4 967 (add-to-list 'vc-dir-buffers (current-buffer))
74d0991f
DN
968 ;; Make sure that if the directory buffer is killed, the update
969 ;; process running in the background is also killed.
970 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
8117868f 971 (hack-dir-local-variables-non-file-buffer)
c4c0a44b 972 (vc-dir-refresh)))
74d0991f
DN
973
974(defun vc-dir-headers (backend dir)
975 "Display the headers in the *VC dir* buffer.
13ad7457 976It calls the `dir-extra-headers' backend method to display backend
74d0991f
DN
977specific headers."
978 (concat
a7200025 979 ;; First layout the common headers.
74d0991f
DN
980 (propertize "VC backend : " 'face 'font-lock-type-face)
981 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
982 (propertize "Working dir: " 'face 'font-lock-type-face)
b2b8574b
SM
983 (propertize (format "%s\n" (abbreviate-file-name dir))
984 'face 'font-lock-variable-name-face)
a7200025
DN
985 ;; Then the backend specific ones.
986 (vc-call-backend backend 'dir-extra-headers dir)
987 "\n"))
74d0991f
DN
988
989(defun vc-dir-refresh-files (files default-state)
990 "Refresh some files in the *VC-dir* buffer."
991 (let ((def-dir default-directory)
992 (backend vc-dir-backend))
993 (vc-set-mode-line-busy-indicator)
994 ;; Call the `dir-status-file' backend function.
995 ;; `dir-status-file' is supposed to be asynchronous.
996 ;; It should compute the results, and then call the function
997 ;; passed as an argument in order to update the vc-dir buffer
998 ;; with the results.
999 (unless (buffer-live-p vc-dir-process-buffer)
1000 (setq vc-dir-process-buffer
1001 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
1002 (lexical-let ((buffer (current-buffer)))
1003 (with-current-buffer vc-dir-process-buffer
1004 (cd def-dir)
1005 (erase-buffer)
1006 (vc-call-backend
1007 backend 'dir-status-files def-dir files default-state
1008 (lambda (entries &optional more-to-come)
1009 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1010 ;; If MORE-TO-COME is true, then more updates will come from
1011 ;; the asynchronous process.
1012 (with-current-buffer buffer
1013 (vc-dir-update entries buffer)
1014 (unless more-to-come
1015 (setq mode-line-process nil)
1016 ;; Remove the ones that haven't been updated at all.
1017 ;; Those not-updated are those whose state is nil because the
1018 ;; file/dir doesn't exist and isn't versioned.
1019 (ewoc-filter vc-ewoc
1020 (lambda (info)
1021 ;; The state for directory entries might
1022 ;; have been changed to 'up-to-date,
1023 ;; reset it, othewise it will be removed when doing 'x'
1024 ;; next time.
1025 ;; FIXME: There should be a more elegant way to do this.
1026 (when (and (vc-dir-fileinfo->directory info)
1027 (eq (vc-dir-fileinfo->state info)
1028 'up-to-date))
1029 (setf (vc-dir-fileinfo->state info) nil))
1030
1031 (not (vc-dir-fileinfo->needs-update info))))))))))))
1032
c4c0a44b
DN
1033(defun vc-dir-revert-buffer-function (&optional ignore-auto noconfirm)
1034 (vc-dir-refresh))
1035
74d0991f
DN
1036(defun vc-dir-refresh ()
1037 "Refresh the contents of the *VC-dir* buffer.
1038Throw an error if another update process is in progress."
1039 (interactive)
1040 (if (vc-dir-busy)
1041 (error "Another update process is in progress, cannot run two at a time")
1042 (let ((def-dir default-directory)
1043 (backend vc-dir-backend))
1044 (vc-set-mode-line-busy-indicator)
1045 ;; Call the `dir-status' backend function.
1046 ;; `dir-status' is supposed to be asynchronous.
1047 ;; It should compute the results, and then call the function
1048 ;; passed as an argument in order to update the vc-dir buffer
1049 ;; with the results.
1050
1051 ;; Create a buffer that can be used by `dir-status' and call
1052 ;; `dir-status' with this buffer as the current buffer. Use
1053 ;; `vc-dir-process-buffer' to remember this buffer, so that
1054 ;; it can be used later to kill the update process in case it
1055 ;; takes too long.
1056 (unless (buffer-live-p vc-dir-process-buffer)
1057 (setq vc-dir-process-buffer
1058 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
29ce30b3
DN
1059 ;; set the needs-update flag on all non-directory entries
1060 (ewoc-map (lambda (info)
1061 (unless (vc-dir-fileinfo->directory info)
1062 (setf (vc-dir-fileinfo->needs-update info) t) nil))
74d0991f
DN
1063 vc-ewoc)
1064 (lexical-let ((buffer (current-buffer)))
1065 (with-current-buffer vc-dir-process-buffer
1066 (cd def-dir)
1067 (erase-buffer)
1068 (vc-call-backend
1069 backend 'dir-status def-dir
1070 (lambda (entries &optional more-to-come)
1071 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1072 ;; If MORE-TO-COME is true, then more updates will come from
1073 ;; the asynchronous process.
1074 (with-current-buffer buffer
1075 (vc-dir-update entries buffer)
1076 (unless more-to-come
1077 (let ((remaining
1078 (ewoc-collect
1079 vc-ewoc 'vc-dir-fileinfo->needs-update)))
1080 (if remaining
1081 (vc-dir-refresh-files
1082 (mapcar 'vc-dir-fileinfo->name remaining)
1083 'up-to-date)
7aaf911c
DN
1084 (setq mode-line-process nil)))))))))
1085 (ewoc-set-hf vc-ewoc (vc-dir-headers backend def-dir) ""))))
74d0991f
DN
1086
1087(defun vc-dir-show-fileentry (file)
1088 "Insert an entry for a specific file into the current *VC-dir* listing.
1089This is typically used if the file is up-to-date (or has been added
1090outside of VC) and one wants to do some operation on it."
1091 (interactive "fShow file: ")
1092 (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
1093
1094(defun vc-dir-hide-up-to-date ()
1095 "Hide up-to-date items from display."
1096 (interactive)
f731e2f9
DN
1097 (let ((crt (ewoc-nth vc-ewoc -1))
1098 (first (ewoc-nth vc-ewoc 0)))
1099 ;; Go over from the last item to the first and remove the
1100 ;; up-to-date files and directories with no child files.
1101 (while (not (eq crt first))
1102 (let* ((data (ewoc-data crt))
1103 (dir (vc-dir-fileinfo->directory data))
1104 (next (ewoc-next vc-ewoc crt))
1105 (prev (ewoc-prev vc-ewoc crt))
1106 ;; ewoc-delete does not work without this...
1107 (inhibit-read-only t))
1108 (when (or
1109 ;; Remove directories with no child files.
1110 (and dir
1111 (or
1112 ;; Nothing follows this directory.
1113 (not next)
1114 ;; Next item is a directory.
1115 (vc-dir-fileinfo->directory (ewoc-data next))))
1116 ;; Remove files in the up-to-date state.
1117 (eq (vc-dir-fileinfo->state data) 'up-to-date))
1118 (ewoc-delete vc-ewoc crt))
1119 (setq crt prev)))))
74d0991f 1120
a544e7c1
SM
1121(defun vc-dir-kill-line ()
1122 "Remove the current line from display."
1123 (interactive)
1124 (let ((crt (ewoc-locate vc-ewoc))
1125 (inhibit-read-only t))
1126 (ewoc-delete vc-ewoc crt)))
1127
13ad7457
DN
1128(defun vc-dir-printer (fileentry)
1129 (vc-call-backend vc-dir-backend 'dir-printer fileentry))
74d0991f 1130
2913a58d
DN
1131(defun vc-dir-deduce-fileset (&optional state-model-only-files)
1132 (let ((marked (vc-dir-marked-files))
1133 files
1134 only-files-list
1135 state
1136 model)
1137 (if marked
1138 (progn
1139 (setq files marked)
1140 (when state-model-only-files
1141 (setq only-files-list (vc-dir-marked-only-files-and-states))))
1142 (let ((crt (vc-dir-current-file)))
1143 (setq files (list crt))
a1fc8acb 1144 (when state-model-only-files
2913a58d
DN
1145 (setq only-files-list (vc-dir-child-files-and-states)))))
1146
1147 (when state-model-only-files
1148 (setq state (cdar only-files-list))
1149 ;; Check that all files are in a consistent state, since we use that
1150 ;; state to decide which operation to perform.
1151 (dolist (crt (cdr only-files-list))
1152 (unless (vc-compatible-state (cdr crt) state)
2bc9f1df 1153 (error "When applying VC operations to multiple files, the files are required\nto be in similar VC states.\n%s in state %s clashes with %s in state %s"
2913a58d
DN
1154 (car crt) (cdr crt) (caar only-files-list) state)))
1155 (setq only-files-list (mapcar 'car only-files-list))
1156 (when (and state (not (eq state 'unregistered)))
1157 (setq model (vc-checkout-model vc-dir-backend only-files-list))))
1158 (list vc-dir-backend files only-files-list state model)))
1159
74d0991f 1160;;;###autoload
b861de91 1161(defun vc-dir (dir &optional backend)
2399e875
RS
1162 "Show the VC status for \"interesting\" files in and below DIR.
1163This allows you to mark files and perform VC operations on them.
1164The list omits files which are up to date, with no changes in your copy
1165or the repository, if there is nothing in particular to say about them.
1166
1167Preparing the list of file status takes time; when the buffer
1168first appears, it has only the first few lines of summary information.
1169The file lines appear later.
1170
b861de91 1171Optional second argument BACKEND specifies the VC backend to use.
2399e875
RS
1172Interactively, a prefix argument means to ask for the backend.
1173
1174These are the commands available for use in the file status buffer:
1175
35639eb4 1176\\{vc-dir-mode-map}"
2399e875 1177
c4c0a44b
DN
1178 (interactive
1179 (list
9b9b7655
SS
1180 ;; When you hit C-x v d in a visited VC file,
1181 ;; the *vc-dir* buffer visits the directory under its truename;
1182 ;; therefore it makes sense to always do that.
1183 ;; Otherwise if you do C-x v d -> C-x C-f -> C-c v d
1184 ;; you may get a new *vc-dir* buffer, different from the original
1185 (file-truename (read-file-name "VC status for directory: "
1186 default-directory default-directory t
1187 nil #'file-directory-p))
c4c0a44b
DN
1188 (if current-prefix-arg
1189 (intern
1190 (completing-read
1191 "Use VC backend: "
b861de91
AS
1192 (mapcar (lambda (b) (list (symbol-name b)))
1193 vc-handled-backends)
1194 nil t nil nil)))))
1195 (unless backend
1196 (setq backend (vc-responsible-backend dir)))
d24e10b1
GM
1197 (let (pop-up-windows) ; based on cvs-examine; bug#6204
1198 (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir backend)))
c4c0a44b 1199 (if (derived-mode-p 'vc-dir-mode)
74d0991f 1200 (vc-dir-refresh)
c4c0a44b
DN
1201 ;; FIXME: find a better way to pass the backend to `vc-dir-mode'.
1202 (let ((use-vc-backend backend))
1203 (vc-dir-mode))))
74d0991f 1204
13ad7457 1205(defun vc-default-dir-extra-headers (backend dir)
74d0991f
DN
1206 ;; Be loud by default to remind people to add code to display
1207 ;; backend specific headers.
1208 ;; XXX: change this to return nil before the release.
1209 (concat
1210 (propertize "Extra : " 'face 'font-lock-type-face)
1211 (propertize "Please add backend specific headers here. It's easy!"
1212 'face 'font-lock-warning-face)))
1213
c7f9e440 1214(defvar vc-dir-filename-mouse-map
f8d5a47f
NR
1215 (let ((map (make-sparse-keymap)))
1216 (define-key map [mouse-2] 'vc-dir-find-file-other-window)
1217 map)
1218 "Local keymap for visiting a file.")
1219
13ad7457 1220(defun vc-default-dir-printer (backend fileentry)
74d0991f
DN
1221 "Pretty print FILEENTRY."
1222 ;; If you change the layout here, change vc-dir-move-to-goal-column.
c7f9e440
DN
1223 ;; VC backends can implement backend specific versions of this
1224 ;; function. Changes here might need to be reflected in the
1225 ;; vc-BACKEND-dir-printer functions.
74d0991f 1226 (let* ((isdir (vc-dir-fileinfo->directory fileentry))
c4c0a44b 1227 (state (if isdir "" (vc-dir-fileinfo->state fileentry)))
74d0991f 1228 (filename (vc-dir-fileinfo->name fileentry)))
74d0991f
DN
1229 (insert
1230 (propertize
1231 (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1232 'face 'font-lock-type-face)
1233 " "
1234 (propertize
1235 (format "%-20s" state)
1236 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1237 ((memq state '(missing conflict)) 'font-lock-warning-face)
1238 (t 'font-lock-variable-name-face))
1239 'mouse-face 'highlight)
1240 " "
1241 (propertize
1242 (format "%s" filename)
c4c0a44b
DN
1243 'face
1244 (if isdir 'font-lock-comment-delimiter-face 'font-lock-function-name-face)
1245 'help-echo
a1fc8acb
JB
1246 (if isdir
1247 "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
c4c0a44b 1248 "File\nmouse-3: Pop-up menu")
f8d5a47f 1249 'mouse-face 'highlight
c7f9e440 1250 'keymap vc-dir-filename-mouse-map))))
74d0991f
DN
1251
1252(defun vc-default-extra-status-menu (backend)
1253 nil)
1254
1255(defun vc-default-status-fileinfo-extra (backend file)
1256 "Default absence of extra information returned for a file."
1257 nil)
1258
1259(provide 'vc-dir)
1260
1261;;; vc-dir.el ends here