* lisp/vc/vc-mtn.el:
[bpt/emacs.git] / lisp / vc / vc-dir.el
CommitLineData
0d42eb3e 1;;; vc-dir.el --- Directory status display under VC -*- lexical-binding: t -*-
74d0991f 2
121b8917 3;; Copyright (C) 2007-2012 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))
0adf5618 107 (setq default-directory dir)
74d0991f
DN
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 127 (define-key map [remup]
7cc6e154 128 '(menu-item "Hide Up-to-date" vc-dir-hide-up-to-date
c4c0a44b 129 :help "Hide up-to-date items from display"))
74d0991f
DN
130 ;; Movement.
131 (define-key map [sepmv] '("--"))
132 (define-key map [next-line]
7cc6e154 133 '(menu-item "Next Line" vc-dir-next-line
74d0991f
DN
134 :help "Go to the next line" :keys "n"))
135 (define-key map [previous-line]
7cc6e154 136 '(menu-item "Previous Line" vc-dir-previous-line
74d0991f
DN
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]
7cc6e154 145 '(menu-item "Unmark Previous " vc-dir-unmark-file-up
74d0991f
DN
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 173 (define-key map [open-other]
7cc6e154 174 '(menu-item "Open in Other Window" vc-dir-find-file-other-window
74d0991f
DN
175 :help "Find the file on the current line, in another window"))
176 (define-key map [open]
7cc6e154 177 '(menu-item "Open File" vc-dir-find-file
74d0991f 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]
7cc6e154 206 '(menu-item "Update to Latest Version" vc-update
c4c0a44b
DN
207 :help "Update the current fileset's files to their tip revisions"))
208 (define-key map [revert]
7cc6e154 209 '(menu-item "Revert to Base Version" vc-revert
c4c0a44b
DN
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)
c7a74801 268 (define-key map "e" 'vc-dir-find-file) ; dired-mode compatibility
74d0991f
DN
269 (define-key map "\C-m" 'vc-dir-find-file)
270 (define-key map "o" 'vc-dir-find-file-other-window)
74d0991f
DN
271 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
272 (define-key map [down-mouse-3] 'vc-dir-menu)
273 (define-key map [mouse-2] 'vc-dir-toggle-mark)
d03b3d23 274 (define-key map [follow-link] 'mouse-face)
c4c0a44b 275 (define-key map "x" 'vc-dir-hide-up-to-date)
a544e7c1 276 (define-key map [?\C-k] 'vc-dir-kill-line)
834d998e 277 (define-key map "S" 'vc-dir-search) ;; FIXME: Maybe use A like dired?
f8b4aa41 278 (define-key map "Q" 'vc-dir-query-replace-regexp)
ba7e37c8
DN
279 (define-key map (kbd "M-s a C-s") 'vc-dir-isearch)
280 (define-key map (kbd "M-s a M-C-s") 'vc-dir-isearch-regexp)
74d0991f
DN
281
282 ;; Hook up the menu.
283 (define-key map [menu-bar vc-dir-mode]
284 `(menu-item
c4c0a44b
DN
285 ;; VC backends can use this to add mode-specific menu items to
286 ;; vc-dir-menu-map.
74d0991f
DN
287 "VC-dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
288 map)
289 "Keymap for directory buffer.")
290
7995501c 291(defmacro vc-dir-at-event (event &rest body)
426bf359
JB
292 "Evaluate BODY with point located at event-start of EVENT.
293If BODY uses EVENT, it should be a variable,
74d0991f 294 otherwise it will be evaluated twice."
7995501c 295 (let ((posn (make-symbol "vc-dir-at-event-posn")))
5da5a66f
SS
296 `(save-excursion
297 (unless (equal ,event '(tool-bar))
298 (let ((,posn (event-start ,event)))
299 (set-buffer (window-buffer (posn-window ,posn)))
300 (goto-char (posn-point ,posn))))
301 ,@body)))
74d0991f
DN
302
303(defun vc-dir-menu (e)
db18c5fd 304 "Popup the VC dir menu."
74d0991f 305 (interactive "e")
7995501c 306 (vc-dir-at-event e (popup-menu vc-dir-menu-map e)))
74d0991f
DN
307
308(defvar vc-dir-tool-bar-map
309 (let ((map (make-sparse-keymap)))
900503ae
CY
310 (tool-bar-local-item-from-menu 'find-file "new" map nil
311 :label "New File" :vert-only t)
312 (tool-bar-local-item-from-menu 'menu-find-file-existing "open" map nil
313 :label "Open" :vert-only t)
314 (tool-bar-local-item-from-menu 'dired "diropen" map nil
315 :vert-only t)
316 (tool-bar-local-item-from-menu 'quit-window "close" map vc-dir-mode-map
317 :vert-only t)
318 (tool-bar-local-item-from-menu 'vc-next-action "saveas" map
319 vc-dir-mode-map :label "Commit")
74d0991f 320 (tool-bar-local-item-from-menu 'vc-print-log "info"
900503ae
CY
321 map vc-dir-mode-map
322 :label "Log")
323 (define-key-after map [separator-1] menu-bar-separator)
74d0991f 324 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
f904c0f9 325 map vc-dir-mode-map
900503ae
CY
326 :label "Stop" :vert-only t)
327 (tool-bar-local-item-from-menu 'revert-buffer "refresh"
328 map vc-dir-mode-map :vert-only t)
329 (define-key-after map [separator-2] menu-bar-separator)
330 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [cut])
331 "cut" map nil :vert-only t)
332 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [copy])
333 "copy" map nil :vert-only t)
334 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [paste])
335 "paste" map nil :vert-only t)
336 (define-key-after map [separator-3] menu-bar-separator)
337 (tool-bar-local-item-from-menu 'isearch-forward
338 "search" map nil
339 :label "Search" :vert-only t)
74d0991f
DN
340 map))
341
342(defun vc-dir-node-directory (node)
343 ;; Compute the directory for NODE.
b4dc7d98 344 ;; If it's a directory node, get it from the node.
74d0991f
DN
345 (let ((data (ewoc-data node)))
346 (or (vc-dir-fileinfo->directory data)
347 ;; Otherwise compute it from the file name.
348 (file-name-directory
b8e54362
DN
349 (directory-file-name
350 (expand-file-name
351 (vc-dir-fileinfo->name data)))))))
74d0991f
DN
352
353(defun vc-dir-update (entries buffer &optional noinsert)
354 "Update BUFFER's ewoc from the list of ENTRIES.
355If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
356 ;; Add ENTRIES to the vc-dir buffer BUFFER.
357 (with-current-buffer buffer
358 ;; Insert the entries sorted by name into the ewoc.
359 ;; We assume the ewoc is sorted too, which should be the
360 ;; case if we always add entries with vc-dir-update.
361 (setq entries
362 ;; Sort: first files and then subdirectories.
363 ;; XXX: this is VERY inefficient, it computes the directory
364 ;; names too many times
365 (sort entries
366 (lambda (entry1 entry2)
b8e54362
DN
367 (let ((dir1 (file-name-directory
368 (directory-file-name (expand-file-name (car entry1)))))
369 (dir2 (file-name-directory
370 (directory-file-name (expand-file-name (car entry2))))))
74d0991f
DN
371 (cond
372 ((string< dir1 dir2) t)
373 ((not (string= dir1 dir2)) nil)
374 ((string< (car entry1) (car entry2))))))))
375 ;; Insert directory entries in the right places.
376 (let ((entry (car entries))
02ffe8e3 377 (node (ewoc-nth vc-ewoc 0))
290736f2 378 (to-remove nil)
02ffe8e3 379 (dotname (file-relative-name default-directory)))
74d0991f
DN
380 ;; Insert . if it is not present.
381 (unless node
02ffe8e3
DN
382 (ewoc-enter-last
383 vc-ewoc (vc-dir-create-fileinfo
dcbbecd4 384 dotname nil nil nil default-directory))
74d0991f 385 (setq node (ewoc-nth vc-ewoc 0)))
a1fc8acb 386
74d0991f
DN
387 (while (and entry node)
388 (let* ((entryfile (car entry))
b8e54362
DN
389 (entrydir (file-name-directory (directory-file-name
390 (expand-file-name entryfile))))
74d0991f
DN
391 (nodedir (vc-dir-node-directory node)))
392 (cond
393 ;; First try to find the directory.
394 ((string-lessp nodedir entrydir)
395 (setq node (ewoc-next vc-ewoc node)))
396 ((string-equal nodedir entrydir)
397 ;; Found the directory, find the place for the file name.
398 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
399 (cond
02ffe8e3
DN
400 ((string= nodefile dotname)
401 (setq node (ewoc-next vc-ewoc node)))
74d0991f
DN
402 ((string-lessp nodefile entryfile)
403 (setq node (ewoc-next vc-ewoc node)))
404 ((string-equal nodefile entryfile)
290736f2
DN
405 (if (nth 1 entry)
406 (progn
407 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
408 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
409 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
410 (ewoc-invalidate vc-ewoc node))
411 ;; If the state is nil, the file does not exist
412 ;; anymore, so remember the entry so we can remove
413 ;; it after we are done inserting all ENTRIES.
414 (push node to-remove))
a1fc8acb 415 (setq entries (cdr entries))
74d0991f
DN
416 (setq entry (car entries))
417 (setq node (ewoc-next vc-ewoc node)))
418 (t
d0cb23ca
DN
419 (unless noinsert
420 (ewoc-enter-before vc-ewoc node
421 (apply 'vc-dir-create-fileinfo entry)))
74d0991f
DN
422 (setq entries (cdr entries))
423 (setq entry (car entries))))))
424 (t
d0cb23ca
DN
425 (unless noinsert
426 ;; We might need to insert a directory node if the
427 ;; previous node was in a different directory.
428 (let* ((rd (file-relative-name entrydir))
429 (prev-node (ewoc-prev vc-ewoc node))
430 (prev-dir (vc-dir-node-directory prev-node)))
431 (unless (string-equal entrydir prev-dir)
432 (ewoc-enter-before
433 vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
434 ;; Now insert the node itself.
435 (ewoc-enter-before vc-ewoc node
436 (apply 'vc-dir-create-fileinfo entry)))
74d0991f
DN
437 (setq entries (cdr entries) entry (car entries))))))
438 ;; We're past the last node, all remaining entries go to the end.
439 (unless (or node noinsert)
440 (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
441 (dolist (entry entries)
b8e54362
DN
442 (let ((entrydir (file-name-directory
443 (directory-file-name (expand-file-name (car entry))))))
74d0991f
DN
444 ;; Insert a directory node if needed.
445 (unless (string-equal lastdir entrydir)
446 (setq lastdir entrydir)
447 (let ((rd (file-relative-name entrydir)))
448 (ewoc-enter-last
449 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
450 ;; Now insert the node itself.
451 (ewoc-enter-last vc-ewoc
290736f2
DN
452 (apply 'vc-dir-create-fileinfo entry))))))
453 (when to-remove
454 (let ((inhibit-read-only t))
455 (apply 'ewoc-delete vc-ewoc (nreverse to-remove)))))))
74d0991f
DN
456
457(defun vc-dir-busy ()
458 (and (buffer-live-p vc-dir-process-buffer)
459 (get-buffer-process vc-dir-process-buffer)))
460
461(defun vc-dir-kill-dir-status-process ()
462 "Kill the temporary buffer and associated process."
463 (interactive)
464 (when (buffer-live-p vc-dir-process-buffer)
465 (let ((proc (get-buffer-process vc-dir-process-buffer)))
466 (when proc (delete-process proc))
467 (setq vc-dir-process-buffer nil)
468 (setq mode-line-process nil))))
469
470(defun vc-dir-kill-query ()
471 ;; Make sure that when the status buffer is killed the update
472 ;; process running in background is also killed.
473 (if (vc-dir-busy)
474 (when (y-or-n-p "Status update process running, really kill status buffer? ")
475 (vc-dir-kill-dir-status-process)
476 t)
477 t))
478
479(defun vc-dir-next-line (arg)
480 "Go to the next line.
481If a prefix argument is given, move by that many lines."
482 (interactive "p")
483 (with-no-warnings
484 (ewoc-goto-next vc-ewoc arg)
485 (vc-dir-move-to-goal-column)))
486
487(defun vc-dir-previous-line (arg)
488 "Go to the previous line.
489If a prefix argument is given, move by that many lines."
490 (interactive "p")
491 (ewoc-goto-prev vc-ewoc arg)
492 (vc-dir-move-to-goal-column))
493
494(defun vc-dir-next-directory ()
495 "Go to the next directory."
496 (interactive)
497 (let ((orig (point)))
498 (if
499 (catch 'foundit
500 (while t
501 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
502 (cond ((not next)
503 (throw 'foundit t))
504 (t
505 (progn
506 (ewoc-goto-node vc-ewoc next)
507 (vc-dir-move-to-goal-column)
508 (if (vc-dir-fileinfo->directory (ewoc-data next))
509 (throw 'foundit nil))))))))
510 (goto-char orig))))
511
512(defun vc-dir-previous-directory ()
513 "Go to the previous directory."
514 (interactive)
515 (let ((orig (point)))
516 (if
517 (catch 'foundit
518 (while t
519 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
520 (cond ((not prev)
521 (throw 'foundit t))
522 (t
523 (progn
524 (ewoc-goto-node vc-ewoc prev)
525 (vc-dir-move-to-goal-column)
526 (if (vc-dir-fileinfo->directory (ewoc-data prev))
527 (throw 'foundit nil))))))))
528 (goto-char orig))))
529
530(defun vc-dir-mark-unmark (mark-unmark-function)
531 (if (use-region-p)
0d42eb3e 532 (let (;; (firstl (line-number-at-pos (region-beginning)))
74d0991f
DN
533 (lastl (line-number-at-pos (region-end))))
534 (save-excursion
535 (goto-char (region-beginning))
536 (while (<= (line-number-at-pos) lastl)
a6198c90
CY
537 (condition-case nil
538 (funcall mark-unmark-function)
539 ;; `vc-dir-mark-file' signals an error if we try marking
540 ;; a directory containing marked files in its tree, or a
541 ;; file in a marked directory tree. Just continue.
542 (error (vc-dir-next-line 1))))))
74d0991f
DN
543 (funcall mark-unmark-function)))
544
74d0991f 545(defun vc-dir-parent-marked-p (arg)
a6198c90
CY
546 ;; Non-nil iff a parent directory of arg is marked.
547 ;; Return value, if non-nil is the `ewoc-data' for the marked parent.
74d0991f 548 (let* ((argdir (vc-dir-node-directory arg))
0d42eb3e 549 ;; (arglen (length argdir))
74d0991f 550 (crt arg)
a6198c90 551 (found nil))
74d0991f
DN
552 ;; Go through the predecessors, checking if any directory that is
553 ;; a parent is marked.
a6198c90
CY
554 (while (and (null found)
555 (setq crt (ewoc-prev vc-ewoc crt)))
556 (let ((data (ewoc-data crt))
557 (dir (vc-dir-node-directory crt)))
558 (and (vc-dir-fileinfo->directory data)
121b8917 559 (string-prefix-p dir argdir)
a6198c90
CY
560 (vc-dir-fileinfo->marked data)
561 (setq found data))))
562 found))
74d0991f
DN
563
564(defun vc-dir-children-marked-p (arg)
a6198c90
CY
565 ;; Non-nil iff a child of ARG is marked.
566 ;; Return value, if non-nil, is the `ewoc-data' for the marked child.
74d0991f
DN
567 (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
568 (is-child t)
569 (crt arg)
a6198c90
CY
570 (found nil))
571 (while (and is-child
572 (null found)
573 (setq crt (ewoc-next vc-ewoc crt)))
574 (let ((data (ewoc-data crt))
575 (dir (vc-dir-node-directory crt)))
576 (if (string-match argdir-re dir)
577 (if (vc-dir-fileinfo->marked data)
578 (setq found data))
579 ;; We are done, we got to an entry that is not a child of `arg'.
580 (setq is-child nil))))
581 found))
74d0991f
DN
582
583(defun vc-dir-mark-file (&optional arg)
584 ;; Mark ARG or the current file and move to the next line.
585 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
586 (file (ewoc-data crt))
a6198c90
CY
587 (isdir (vc-dir-fileinfo->directory file))
588 ;; Forbid marking a directory containing marked files in its
589 ;; tree, or a file in a marked directory tree.
590 (conflict (if isdir
591 (vc-dir-children-marked-p crt)
592 (vc-dir-parent-marked-p crt))))
593 (when conflict
594 (error (if isdir
595 "File `%s' in this directory is already marked"
596 "Parent directory `%s' is already marked")
597 (vc-dir-fileinfo->name conflict)))
598 (setf (vc-dir-fileinfo->marked file) t)
599 (ewoc-invalidate vc-ewoc crt)
600 (unless (or arg (mouse-event-p last-command-event))
601 (vc-dir-next-line 1))))
74d0991f
DN
602
603(defun vc-dir-mark ()
604 "Mark the current file or all files in the region.
605If the region is active, mark all the files in the region.
606Otherwise mark the file on the current line and move to the next
607line."
608 (interactive)
609 (vc-dir-mark-unmark 'vc-dir-mark-file))
610
611(defun vc-dir-mark-all-files (arg)
612 "Mark all files with the same state as the current one.
613With a prefix argument mark all files.
614If the current entry is a directory, mark all child files.
615
616The commands operate on files that are on the same state.
617This command is intended to make it easy to select all files that
618share the same state."
619 (interactive "P")
620 (if arg
621 ;; Mark all files.
622 (progn
623 ;; First check that no directory is marked, we can't mark
624 ;; files in that case.
625 (ewoc-map
626 (lambda (filearg)
627 (when (and (vc-dir-fileinfo->directory filearg)
628 (vc-dir-fileinfo->marked filearg))
629 (error "Cannot mark all files, directory `%s' marked"
630 (vc-dir-fileinfo->name filearg))))
631 vc-ewoc)
632 (ewoc-map
633 (lambda (filearg)
634 (unless (vc-dir-fileinfo->marked filearg)
635 (setf (vc-dir-fileinfo->marked filearg) t)
636 t))
637 vc-ewoc))
a6198c90
CY
638 (let* ((crt (ewoc-locate vc-ewoc))
639 (data (ewoc-data crt)))
74d0991f
DN
640 (if (vc-dir-fileinfo->directory data)
641 ;; It's a directory, mark child files.
a6198c90
CY
642 (let (crt-data)
643 (while (and (setq crt (ewoc-next vc-ewoc crt))
644 (setq crt-data (ewoc-data crt))
645 (not (vc-dir-fileinfo->directory crt-data)))
646 (setf (vc-dir-fileinfo->marked crt-data) t)
647 (ewoc-invalidate vc-ewoc crt)))
74d0991f 648 ;; It's a file
a6198c90
CY
649 (let ((state (vc-dir-fileinfo->state data)))
650 (setq crt (ewoc-nth vc-ewoc 0))
74d0991f
DN
651 (while crt
652 (let ((crt-data (ewoc-data crt)))
653 (when (and (not (vc-dir-fileinfo->marked crt-data))
654 (eq (vc-dir-fileinfo->state crt-data) state)
655 (not (vc-dir-fileinfo->directory crt-data)))
656 (vc-dir-mark-file crt)))
657 (setq crt (ewoc-next vc-ewoc crt))))))))
658
659(defun vc-dir-unmark-file ()
660 ;; Unmark the current file and move to the next line.
661 (let* ((crt (ewoc-locate vc-ewoc))
662 (file (ewoc-data crt)))
663 (setf (vc-dir-fileinfo->marked file) nil)
664 (ewoc-invalidate vc-ewoc crt)
665 (unless (mouse-event-p last-command-event)
666 (vc-dir-next-line 1))))
667
668(defun vc-dir-unmark ()
669 "Unmark the current file or all files in the region.
670If the region is active, unmark all the files in the region.
671Otherwise mark the file on the current line and move to the next
672line."
673 (interactive)
674 (vc-dir-mark-unmark 'vc-dir-unmark-file))
675
676(defun vc-dir-unmark-file-up ()
677 "Move to the previous line and unmark the file."
678 (interactive)
679 ;; If we're on the first line, we won't move up, but we will still
680 ;; remove the mark. This seems a bit odd but it is what buffer-menu
681 ;; does.
682 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
683 (file (ewoc-data prev)))
684 (setf (vc-dir-fileinfo->marked file) nil)
685 (ewoc-invalidate vc-ewoc prev)
686 (vc-dir-move-to-goal-column)))
687
688(defun vc-dir-unmark-all-files (arg)
689 "Unmark all files with the same state as the current one.
690With a prefix argument unmark all files.
691If the current entry is a directory, unmark all the child files.
692
693The commands operate on files that are on the same state.
694This command is intended to make it easy to deselect all files
695that share the same state."
696 (interactive "P")
697 (if arg
698 (ewoc-map
699 (lambda (filearg)
700 (when (vc-dir-fileinfo->marked filearg)
701 (setf (vc-dir-fileinfo->marked filearg) nil)
702 t))
703 vc-ewoc)
704 (let* ((crt (ewoc-locate vc-ewoc))
705 (data (ewoc-data crt)))
706 (if (vc-dir-fileinfo->directory data)
707 ;; It's a directory, unmark child files.
708 (while (setq crt (ewoc-next vc-ewoc crt))
709 (let ((crt-data (ewoc-data crt)))
710 (unless (vc-dir-fileinfo->directory crt-data)
711 (setf (vc-dir-fileinfo->marked crt-data) nil)
712 (ewoc-invalidate vc-ewoc crt))))
713 ;; It's a file
714 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
715 (ewoc-map
716 (lambda (filearg)
717 (when (and (vc-dir-fileinfo->marked filearg)
718 (eq (vc-dir-fileinfo->state filearg) crt-state))
719 (setf (vc-dir-fileinfo->marked filearg) nil)
720 t))
721 vc-ewoc))))))
722
723(defun vc-dir-toggle-mark-file ()
724 (let* ((crt (ewoc-locate vc-ewoc))
725 (file (ewoc-data crt)))
726 (if (vc-dir-fileinfo->marked file)
727 (vc-dir-unmark-file)
728 (vc-dir-mark-file))))
729
730(defun vc-dir-toggle-mark (e)
731 (interactive "e")
7995501c 732 (vc-dir-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
74d0991f
DN
733
734(defun vc-dir-delete-file ()
735 "Delete the marked files, or the current file if no marks."
736 (interactive)
737 (mapc 'vc-delete-file (or (vc-dir-marked-files)
738 (list (vc-dir-current-file)))))
739
740(defun vc-dir-find-file ()
741 "Find the file on the current line."
742 (interactive)
743 (find-file (vc-dir-current-file)))
744
f8d5a47f 745(defun vc-dir-find-file-other-window (&optional event)
74d0991f 746 "Find the file on the current line, in another window."
f8d5a47f
NR
747 (interactive (list last-nonmenu-event))
748 (if event (posn-set-point (event-end event)))
74d0991f
DN
749 (find-file-other-window (vc-dir-current-file)))
750
ba7e37c8
DN
751(defun vc-dir-isearch ()
752 "Search for a string through all marked buffers using Isearch."
753 (interactive)
754 (multi-isearch-files
755 (mapcar 'car (vc-dir-marked-only-files-and-states))))
756
757(defun vc-dir-isearch-regexp ()
758 "Search for a regexp through all marked buffers using Isearch."
759 (interactive)
760 (multi-isearch-files-regexp
761 (mapcar 'car (vc-dir-marked-only-files-and-states))))
762
834d998e
DN
763(defun vc-dir-search (regexp)
764 "Search through all marked files for a match for REGEXP.
765For marked directories, use the files displayed from those directories.
766Stops when a match is found.
767To continue searching for next match, use command \\[tags-loop-continue]."
768 (interactive "sSearch marked files (regexp): ")
769 (tags-search regexp '(mapcar 'car (vc-dir-marked-only-files-and-states))))
770
f8b4aa41
DN
771(defun vc-dir-query-replace-regexp (from to &optional delimited)
772 "Do `query-replace-regexp' of FROM with TO, on all marked files.
773If a directory is marked, then use the files displayed for that directory.
774Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
775If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
776with the command \\[tags-loop-continue]."
ac73d955 777 ;; FIXME: this is almost a copy of `dired-do-query-replace-regexp'. This
f8b4aa41
DN
778 ;; should probably be made generic and used in both places instead of
779 ;; duplicating it here.
780 (interactive
781 (let ((common
782 (query-replace-read-args
783 "Query replace regexp in marked files" t t)))
784 (list (nth 0 common) (nth 1 common) (nth 2 common))))
785 (dolist (file (mapcar 'car (vc-dir-marked-only-files-and-states)))
786 (let ((buffer (get-file-buffer file)))
787 (if (and buffer (with-current-buffer buffer
788 buffer-read-only))
789 (error "File `%s' is visited read-only" file))))
790 (tags-query-replace from to delimited
791 '(mapcar 'car (vc-dir-marked-only-files-and-states))))
792
74d0991f
DN
793(defun vc-dir-current-file ()
794 (let ((node (ewoc-locate vc-ewoc)))
795 (unless node
796 (error "No file available"))
797 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
798
799(defun vc-dir-marked-files ()
800 "Return the list of marked files."
801 (mapcar
802 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
803 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
804
2913a58d
DN
805(defun vc-dir-marked-only-files-and-states ()
806 "Return the list of conses (FILE . STATE) for the marked files.
807For marked directories return the corresponding conses for the
808child files."
74d0991f
DN
809 (let ((crt (ewoc-nth vc-ewoc 0))
810 result)
811 (while crt
812 (let ((crt-data (ewoc-data crt)))
813 (if (vc-dir-fileinfo->marked crt-data)
2913a58d 814 ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
74d0991f
DN
815 (if (vc-dir-fileinfo->directory crt-data)
816 (let* ((dir (vc-dir-fileinfo->directory crt-data))
0d42eb3e 817 ;; (dirlen (length dir))
74d0991f
DN
818 data)
819 (while
820 (and (setq crt (ewoc-next vc-ewoc crt))
121b8917 821 (string-prefix-p dir
74d0991f
DN
822 (progn
823 (setq data (ewoc-data crt))
824 (vc-dir-node-directory crt))))
825 (unless (vc-dir-fileinfo->directory data)
a1fc8acb 826 (push
2913a58d
DN
827 (cons (expand-file-name (vc-dir-fileinfo->name data))
828 (vc-dir-fileinfo->state data))
829 result))))
830 (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
831 (vc-dir-fileinfo->state crt-data))
832 result)
74d0991f
DN
833 (setq crt (ewoc-next vc-ewoc crt)))
834 (setq crt (ewoc-next vc-ewoc crt)))))
9c6c6d1f 835 (nreverse result)))
74d0991f 836
2913a58d
DN
837(defun vc-dir-child-files-and-states ()
838 "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
839If it is a file, return the corresponding cons for the file itself."
74d0991f
DN
840 (let* ((crt (ewoc-locate vc-ewoc))
841 (crt-data (ewoc-data crt))
842 result)
843 (if (vc-dir-fileinfo->directory crt-data)
844 (let* ((dir (vc-dir-fileinfo->directory crt-data))
0d42eb3e 845 ;; (dirlen (length dir))
74d0991f
DN
846 data)
847 (while
848 (and (setq crt (ewoc-next vc-ewoc crt))
121b8917 849 (string-prefix-p dir (progn
74d0991f
DN
850 (setq data (ewoc-data crt))
851 (vc-dir-node-directory crt))))
852 (unless (vc-dir-fileinfo->directory data)
a1fc8acb 853 (push
2913a58d
DN
854 (cons (expand-file-name (vc-dir-fileinfo->name data))
855 (vc-dir-fileinfo->state data))
856 result))))
a1fc8acb 857 (push
2913a58d
DN
858 (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
859 (vc-dir-fileinfo->state crt-data)) result))
9c6c6d1f 860 (nreverse result)))
74d0991f 861
d923f4ac
DN
862(defun vc-dir-recompute-file-state (fname def-dir)
863 (let* ((file-short (file-relative-name fname def-dir))
0d42eb3e 864 (_remove-me-when-CVS-works
776f7a5f
DN
865 (when (eq vc-dir-backend 'CVS)
866 ;; FIXME: Warning: UGLY HACK. The CVS backend caches the state
867 ;; info, this forces the backend to update it.
276db9eb 868 (vc-call-backend vc-dir-backend 'registered fname)))
d923f4ac
DN
869 (state (vc-call-backend vc-dir-backend 'state fname))
870 (extra (vc-call-backend vc-dir-backend
871 'status-fileinfo-extra fname)))
872 (list file-short state extra)))
873
874(defun vc-dir-find-child-files (dirname)
875 ;; Give a DIRNAME string return the list of all child files shown in
876 ;; the current *vc-dir* buffer.
877 (let ((crt (ewoc-nth vc-ewoc 0))
0d42eb3e 878 children)
d923f4ac 879 ;; Find DIR
121b8917 880 (while (and crt (not (string-prefix-p
d923f4ac
DN
881 dirname (vc-dir-node-directory crt))))
882 (setq crt (ewoc-next vc-ewoc crt)))
121b8917 883 (while (and crt (string-prefix-p
d923f4ac 884 dirname
0d42eb3e 885 (vc-dir-node-directory crt)))
d923f4ac
DN
886 (let ((data (ewoc-data crt)))
887 (unless (vc-dir-fileinfo->directory data)
888 (push (expand-file-name (vc-dir-fileinfo->name data)) children)))
889 (setq crt (ewoc-next vc-ewoc crt)))
890 children))
891
892(defun vc-dir-resync-directory-files (dirname)
893 ;; Update the entries for all the child files of DIRNAME shown in
894 ;; the current *vc-dir* buffer.
895 (let ((files (vc-dir-find-child-files dirname))
dcbbecd4 896 (ddir default-directory)
d923f4ac
DN
897 fileentries)
898 (when files
899 (dolist (crt files)
900 (push (vc-dir-recompute-file-state crt ddir)
901 fileentries))
902 (vc-dir-update fileentries (current-buffer)))))
903
74d0991f 904(defun vc-dir-resynch-file (&optional fname)
426bf359 905 "Update the entries for FNAME in any directory buffers that list it."
d923f4ac 906 (let ((file (or fname (expand-file-name buffer-file-name)))
dcbbecd4
SM
907 (drop '()))
908 (save-current-buffer
909 ;; look for a vc-dir buffer that might show this file.
910 (dolist (status-buf vc-dir-buffers)
911 (if (not (buffer-live-p status-buf))
912 (push status-buf drop)
913 (set-buffer status-buf)
914 (if (not (derived-mode-p 'vc-dir-mode))
915 (push status-buf drop)
916 (let ((ddir default-directory))
121b8917 917 (when (string-prefix-p ddir file)
dcbbecd4 918 (if (file-directory-p file)
4d0bbcb6
DN
919 (progn
920 (vc-dir-resync-directory-files file)
921 (ewoc-set-hf vc-ewoc
922 (vc-dir-headers vc-dir-backend default-directory) ""))
d0cb23ca
DN
923 (let* ((complete-state (vc-dir-recompute-file-state file ddir))
924 (state (cadr complete-state)))
dcbbecd4 925 (vc-dir-update
d0cb23ca
DN
926 (list complete-state)
927 status-buf (or (not state)
928 (eq state 'up-to-date)))))))))))
dcbbecd4
SM
929 ;; Remove out-of-date entries from vc-dir-buffers.
930 (dolist (b drop) (setq vc-dir-buffers (delq b vc-dir-buffers)))))
74d0991f 931
c4c0a44b
DN
932(defvar use-vc-backend) ;; dynamically bound
933
934(define-derived-mode vc-dir-mode special-mode "VC dir"
db18c5fd 935 "Major mode for VC directory buffers.
74d0991f 936Marking/Unmarking key bindings and actions:
ae42a852
DN
937m - mark a file/directory
938 - if the region is active, mark all the files in region.
74d0991f
DN
939 Restrictions: - a file cannot be marked if any parent directory is marked
940 - a directory cannot be marked if any child file or
941 directory is marked
ae42a852
DN
942u - unmark a file/directory
943 - if the region is active, unmark all the files in region.
74d0991f
DN
944M - if the cursor is on a file: mark all the files with the same state as
945 the current file
946 - if the cursor is on a directory: mark all child files
947 - with a prefix argument: mark all files
948U - if the cursor is on a file: unmark all the files with the same state
949 as the current file
950 - if the cursor is on a directory: unmark all child files
951 - with a prefix argument: unmark all files
ae42a852 952mouse-2 - toggles the mark state
74d0991f 953
db18c5fd 954VC commands
ae42a852
DN
955VC commands in the `C-x v' prefix can be used.
956VC commands act on the marked entries. If nothing is marked, VC
957commands act on the current entry.
db18c5fd
SS
958
959Search & Replace
960S - searches the marked files
961Q - does a query replace on the marked files
962M-s a C-s - does an isearch on the marked files
49c2119d 963M-s a C-M-s - does a regexp isearch on the marked files
db18c5fd
SS
964If nothing is marked, these commands act on the current entry.
965When a directory is current or marked, the Search & Replace
ae42a852
DN
966commands act on the child files of that directory that are displayed in
967the *vc-dir* buffer.
74d0991f
DN
968
969\\{vc-dir-mode-map}"
c4c0a44b 970 (set (make-local-variable 'vc-dir-backend) use-vc-backend)
74d0991f 971 (setq buffer-read-only t)
c4c0a44b
DN
972 (when (boundp 'tool-bar-map)
973 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
74d0991f
DN
974 (let ((buffer-read-only nil))
975 (erase-buffer)
976 (set (make-local-variable 'vc-dir-process-buffer) nil)
13ad7457 977 (set (make-local-variable 'vc-ewoc) (ewoc-create #'vc-dir-printer))
c4c0a44b
DN
978 (set (make-local-variable 'revert-buffer-function)
979 'vc-dir-revert-buffer-function)
1dfae2a2 980 (setq list-buffers-directory (expand-file-name "*vc-dir*" default-directory))
dcbbecd4 981 (add-to-list 'vc-dir-buffers (current-buffer))
74d0991f
DN
982 ;; Make sure that if the directory buffer is killed, the update
983 ;; process running in the background is also killed.
984 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
8117868f 985 (hack-dir-local-variables-non-file-buffer)
c4c0a44b 986 (vc-dir-refresh)))
74d0991f
DN
987
988(defun vc-dir-headers (backend dir)
989 "Display the headers in the *VC dir* buffer.
13ad7457 990It calls the `dir-extra-headers' backend method to display backend
74d0991f
DN
991specific headers."
992 (concat
a7200025 993 ;; First layout the common headers.
74d0991f
DN
994 (propertize "VC backend : " 'face 'font-lock-type-face)
995 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
996 (propertize "Working dir: " 'face 'font-lock-type-face)
b2b8574b
SM
997 (propertize (format "%s\n" (abbreviate-file-name dir))
998 'face 'font-lock-variable-name-face)
a7200025
DN
999 ;; Then the backend specific ones.
1000 (vc-call-backend backend 'dir-extra-headers dir)
1001 "\n"))
74d0991f
DN
1002
1003(defun vc-dir-refresh-files (files default-state)
1004 "Refresh some files in the *VC-dir* buffer."
1005 (let ((def-dir default-directory)
1006 (backend vc-dir-backend))
1007 (vc-set-mode-line-busy-indicator)
db22a3c2
JB
1008 ;; Call the `dir-status-files' backend function.
1009 ;; `dir-status-files' is supposed to be asynchronous.
74d0991f
DN
1010 ;; It should compute the results, and then call the function
1011 ;; passed as an argument in order to update the vc-dir buffer
1012 ;; with the results.
1013 (unless (buffer-live-p vc-dir-process-buffer)
1014 (setq vc-dir-process-buffer
1015 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
0d42eb3e 1016 (let ((buffer (current-buffer)))
74d0991f 1017 (with-current-buffer vc-dir-process-buffer
0adf5618 1018 (setq default-directory def-dir)
74d0991f
DN
1019 (erase-buffer)
1020 (vc-call-backend
1021 backend 'dir-status-files def-dir files default-state
1022 (lambda (entries &optional more-to-come)
1023 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1024 ;; If MORE-TO-COME is true, then more updates will come from
1025 ;; the asynchronous process.
1026 (with-current-buffer buffer
1027 (vc-dir-update entries buffer)
1028 (unless more-to-come
1029 (setq mode-line-process nil)
1030 ;; Remove the ones that haven't been updated at all.
1031 ;; Those not-updated are those whose state is nil because the
1032 ;; file/dir doesn't exist and isn't versioned.
1033 (ewoc-filter vc-ewoc
1034 (lambda (info)
1035 ;; The state for directory entries might
1036 ;; have been changed to 'up-to-date,
e1dbe924 1037 ;; reset it, otherwise it will be removed when doing 'x'
74d0991f
DN
1038 ;; next time.
1039 ;; FIXME: There should be a more elegant way to do this.
1040 (when (and (vc-dir-fileinfo->directory info)
1041 (eq (vc-dir-fileinfo->state info)
1042 'up-to-date))
1043 (setf (vc-dir-fileinfo->state info) nil))
1044
1045 (not (vc-dir-fileinfo->needs-update info))))))))))))
1046
0d42eb3e 1047(defun vc-dir-revert-buffer-function (&optional _ignore-auto _noconfirm)
c4c0a44b
DN
1048 (vc-dir-refresh))
1049
74d0991f
DN
1050(defun vc-dir-refresh ()
1051 "Refresh the contents of the *VC-dir* buffer.
1052Throw an error if another update process is in progress."
1053 (interactive)
1054 (if (vc-dir-busy)
1055 (error "Another update process is in progress, cannot run two at a time")
1056 (let ((def-dir default-directory)
1057 (backend vc-dir-backend))
1058 (vc-set-mode-line-busy-indicator)
1059 ;; Call the `dir-status' backend function.
1060 ;; `dir-status' is supposed to be asynchronous.
1061 ;; It should compute the results, and then call the function
1062 ;; passed as an argument in order to update the vc-dir buffer
1063 ;; with the results.
1064
1065 ;; Create a buffer that can be used by `dir-status' and call
1066 ;; `dir-status' with this buffer as the current buffer. Use
1067 ;; `vc-dir-process-buffer' to remember this buffer, so that
1068 ;; it can be used later to kill the update process in case it
1069 ;; takes too long.
1070 (unless (buffer-live-p vc-dir-process-buffer)
1071 (setq vc-dir-process-buffer
1072 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
29ce30b3
DN
1073 ;; set the needs-update flag on all non-directory entries
1074 (ewoc-map (lambda (info)
1075 (unless (vc-dir-fileinfo->directory info)
1076 (setf (vc-dir-fileinfo->needs-update info) t) nil))
74d0991f 1077 vc-ewoc)
cf77dd27
SM
1078 ;; Bzr has serious locking problems, so setup the headers first (this is
1079 ;; synchronous) rather than doing it while dir-status is running.
1080 (ewoc-set-hf vc-ewoc (vc-dir-headers backend def-dir) "")
0d42eb3e 1081 (let ((buffer (current-buffer)))
74d0991f 1082 (with-current-buffer vc-dir-process-buffer
0adf5618 1083 (setq default-directory def-dir)
74d0991f
DN
1084 (erase-buffer)
1085 (vc-call-backend
1086 backend 'dir-status def-dir
1087 (lambda (entries &optional more-to-come)
1088 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1089 ;; If MORE-TO-COME is true, then more updates will come from
1090 ;; the asynchronous process.
1091 (with-current-buffer buffer
1092 (vc-dir-update entries buffer)
1093 (unless more-to-come
1094 (let ((remaining
1095 (ewoc-collect
1096 vc-ewoc 'vc-dir-fileinfo->needs-update)))
1097 (if remaining
1098 (vc-dir-refresh-files
1099 (mapcar 'vc-dir-fileinfo->name remaining)
1100 'up-to-date)
cf77dd27 1101 (setq mode-line-process nil))))))))))))
74d0991f
DN
1102
1103(defun vc-dir-show-fileentry (file)
1104 "Insert an entry for a specific file into the current *VC-dir* listing.
1105This is typically used if the file is up-to-date (or has been added
1106outside of VC) and one wants to do some operation on it."
1107 (interactive "fShow file: ")
1108 (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
1109
1110(defun vc-dir-hide-up-to-date ()
1111 "Hide up-to-date items from display."
1112 (interactive)
f731e2f9
DN
1113 (let ((crt (ewoc-nth vc-ewoc -1))
1114 (first (ewoc-nth vc-ewoc 0)))
1115 ;; Go over from the last item to the first and remove the
1116 ;; up-to-date files and directories with no child files.
1117 (while (not (eq crt first))
1118 (let* ((data (ewoc-data crt))
1119 (dir (vc-dir-fileinfo->directory data))
1120 (next (ewoc-next vc-ewoc crt))
1121 (prev (ewoc-prev vc-ewoc crt))
1122 ;; ewoc-delete does not work without this...
1123 (inhibit-read-only t))
1124 (when (or
1125 ;; Remove directories with no child files.
1126 (and dir
1127 (or
1128 ;; Nothing follows this directory.
1129 (not next)
1130 ;; Next item is a directory.
1131 (vc-dir-fileinfo->directory (ewoc-data next))))
1132 ;; Remove files in the up-to-date state.
1133 (eq (vc-dir-fileinfo->state data) 'up-to-date))
1134 (ewoc-delete vc-ewoc crt))
1135 (setq crt prev)))))
74d0991f 1136
a544e7c1
SM
1137(defun vc-dir-kill-line ()
1138 "Remove the current line from display."
1139 (interactive)
1140 (let ((crt (ewoc-locate vc-ewoc))
1141 (inhibit-read-only t))
1142 (ewoc-delete vc-ewoc crt)))
1143
13ad7457
DN
1144(defun vc-dir-printer (fileentry)
1145 (vc-call-backend vc-dir-backend 'dir-printer fileentry))
74d0991f 1146
2913a58d
DN
1147(defun vc-dir-deduce-fileset (&optional state-model-only-files)
1148 (let ((marked (vc-dir-marked-files))
1149 files
1150 only-files-list
1151 state
1152 model)
1153 (if marked
1154 (progn
1155 (setq files marked)
1156 (when state-model-only-files
1157 (setq only-files-list (vc-dir-marked-only-files-and-states))))
1158 (let ((crt (vc-dir-current-file)))
1159 (setq files (list crt))
a1fc8acb 1160 (when state-model-only-files
2913a58d
DN
1161 (setq only-files-list (vc-dir-child-files-and-states)))))
1162
1163 (when state-model-only-files
1164 (setq state (cdar only-files-list))
1165 ;; Check that all files are in a consistent state, since we use that
1166 ;; state to decide which operation to perform.
1167 (dolist (crt (cdr only-files-list))
1168 (unless (vc-compatible-state (cdr crt) state)
2bc9f1df 1169 (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
1170 (car crt) (cdr crt) (caar only-files-list) state)))
1171 (setq only-files-list (mapcar 'car only-files-list))
1172 (when (and state (not (eq state 'unregistered)))
1173 (setq model (vc-checkout-model vc-dir-backend only-files-list))))
1174 (list vc-dir-backend files only-files-list state model)))
1175
74d0991f 1176;;;###autoload
b861de91 1177(defun vc-dir (dir &optional backend)
2399e875
RS
1178 "Show the VC status for \"interesting\" files in and below DIR.
1179This allows you to mark files and perform VC operations on them.
1180The list omits files which are up to date, with no changes in your copy
1181or the repository, if there is nothing in particular to say about them.
1182
1183Preparing the list of file status takes time; when the buffer
1184first appears, it has only the first few lines of summary information.
1185The file lines appear later.
1186
b861de91 1187Optional second argument BACKEND specifies the VC backend to use.
2399e875
RS
1188Interactively, a prefix argument means to ask for the backend.
1189
1190These are the commands available for use in the file status buffer:
1191
35639eb4 1192\\{vc-dir-mode-map}"
2399e875 1193
c4c0a44b
DN
1194 (interactive
1195 (list
9b9b7655
SS
1196 ;; When you hit C-x v d in a visited VC file,
1197 ;; the *vc-dir* buffer visits the directory under its truename;
1198 ;; therefore it makes sense to always do that.
1199 ;; Otherwise if you do C-x v d -> C-x C-f -> C-c v d
1200 ;; you may get a new *vc-dir* buffer, different from the original
7e27ce9c
AL
1201 (file-truename (read-directory-name "VC status for directory: "
1202 default-directory default-directory t
1203 nil))
c4c0a44b
DN
1204 (if current-prefix-arg
1205 (intern
1206 (completing-read
1207 "Use VC backend: "
b861de91
AS
1208 (mapcar (lambda (b) (list (symbol-name b)))
1209 vc-handled-backends)
1210 nil t nil nil)))))
1211 (unless backend
1212 (setq backend (vc-responsible-backend dir)))
d24e10b1
GM
1213 (let (pop-up-windows) ; based on cvs-examine; bug#6204
1214 (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir backend)))
c4c0a44b 1215 (if (derived-mode-p 'vc-dir-mode)
74d0991f 1216 (vc-dir-refresh)
c4c0a44b
DN
1217 ;; FIXME: find a better way to pass the backend to `vc-dir-mode'.
1218 (let ((use-vc-backend backend))
1219 (vc-dir-mode))))
74d0991f 1220
0d42eb3e 1221(defun vc-default-dir-extra-headers (_backend _dir)
74d0991f
DN
1222 ;; Be loud by default to remind people to add code to display
1223 ;; backend specific headers.
1224 ;; XXX: change this to return nil before the release.
1225 (concat
1226 (propertize "Extra : " 'face 'font-lock-type-face)
1227 (propertize "Please add backend specific headers here. It's easy!"
1228 'face 'font-lock-warning-face)))
1229
c7f9e440 1230(defvar vc-dir-filename-mouse-map
f8d5a47f
NR
1231 (let ((map (make-sparse-keymap)))
1232 (define-key map [mouse-2] 'vc-dir-find-file-other-window)
1233 map)
1234 "Local keymap for visiting a file.")
1235
0d42eb3e 1236(defun vc-default-dir-printer (_backend fileentry)
74d0991f
DN
1237 "Pretty print FILEENTRY."
1238 ;; If you change the layout here, change vc-dir-move-to-goal-column.
c7f9e440
DN
1239 ;; VC backends can implement backend specific versions of this
1240 ;; function. Changes here might need to be reflected in the
1241 ;; vc-BACKEND-dir-printer functions.
74d0991f 1242 (let* ((isdir (vc-dir-fileinfo->directory fileentry))
c4c0a44b 1243 (state (if isdir "" (vc-dir-fileinfo->state fileentry)))
74d0991f 1244 (filename (vc-dir-fileinfo->name fileentry)))
74d0991f
DN
1245 (insert
1246 (propertize
1247 (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1248 'face 'font-lock-type-face)
1249 " "
1250 (propertize
1251 (format "%-20s" state)
1252 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1253 ((memq state '(missing conflict)) 'font-lock-warning-face)
f0fb8059 1254 ((eq state 'edited) 'font-lock-constant-face)
74d0991f
DN
1255 (t 'font-lock-variable-name-face))
1256 'mouse-face 'highlight)
1257 " "
1258 (propertize
1259 (format "%s" filename)
c4c0a44b
DN
1260 'face
1261 (if isdir 'font-lock-comment-delimiter-face 'font-lock-function-name-face)
1262 'help-echo
a1fc8acb
JB
1263 (if isdir
1264 "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
c4c0a44b 1265 "File\nmouse-3: Pop-up menu")
f8d5a47f 1266 'mouse-face 'highlight
c7f9e440 1267 'keymap vc-dir-filename-mouse-map))))
74d0991f 1268
0d42eb3e 1269(defun vc-default-extra-status-menu (_backend)
74d0991f
DN
1270 nil)
1271
0d42eb3e 1272(defun vc-default-status-fileinfo-extra (_backend _file)
74d0991f
DN
1273 "Default absence of extra information returned for a file."
1274 nil)
1275
1276(provide 'vc-dir)
1277
1278;;; vc-dir.el ends here