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