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