1 ;;; vc-dir.el --- Directory status display under VC
3 ;; Copyright (C) 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Dan Nicolaescu <dann@ics.uci.edu>
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/>.
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>
49 (defcustom vc-dir-mode-hook nil
50 "Normal hook run by `vc-dir-mode'.
55 ;; Used to store information for the files displayed in the directory buffer.
56 ;; Each item displayed corresponds to one of these defstructs.
57 (defstruct (vc-dir-fileinfo
59 (:type list
) ;So we can use `member' on lists of FIs.
61 ;; We could define it as an alias for `list'.
62 vc-dir-create-fileinfo
(name state
&optional extra marked directory
))
63 (:conc-name vc-dir-fileinfo-
>))
64 name
;Keep it as first, for `member'.
66 ;; For storing backend specific information.
69 ;; To keep track of not updated files during a global refresh
71 ;; To distinguish files and directories.
76 (defvar vc-dir-process-buffer nil
77 "The buffer used for the asynchronous call that computes status.")
79 (defvar vc-dir-backend nil
80 "The backend used by the current *vc-dir* buffer.")
82 (defun vc-dir-move-to-goal-column ()
83 ;; Used to keep the cursor on the file name column.
86 ;; Must be in sync with vc-default-dir-printer.
89 (defun vc-dir-prepare-status-buffer (bname dir backend
&optional create-new
)
90 "Find a buffer named BNAME showing DIR, or create a new one."
91 (setq dir
(file-name-as-directory (expand-file-name dir
)))
92 (let* ;; Look for another buffer name BNAME visiting the same directory.
95 (dolist (buffer vc-dir-buffers
)
96 (when (buffer-live-p buffer
)
98 (when (and (derived-mode-p 'vc-dir-mode
)
99 (eq vc-dir-backend backend
)
100 (string= default-directory dir
))
101 (return buffer
))))))))
103 ;; Create a new buffer named BNAME.
104 ;; We pass a filename to create-file-buffer because it is what
105 ;; the function expects, and also what uniquify needs (if active)
106 (with-current-buffer (create-file-buffer (expand-file-name bname dir
))
108 (vc-setup-buffer (current-buffer))
109 ;; Reset the vc-parent-buffer-name so that it does not appear
111 (setq vc-parent-buffer-name nil
)
114 (defvar vc-dir-menu-map
115 (let ((map (make-sparse-keymap "VC-dir")))
116 (define-key map
[quit]
117 '(menu-item "Quit" quit-window
119 (define-key map [kill]
120 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
121 :enable (vc-dir-busy)
122 :help "Kill the command that updates the directory buffer"))
123 (define-key map [refresh]
124 '(menu-item "Refresh" revert-buffer
125 :enable (not (vc-dir-busy))
126 :help "Refresh the contents of the directory buffer"))
127 (define-key map [remup]
128 '(menu-item "Hide up-to-date" vc-dir-hide-up-to-date
129 :help "Hide up-to-date items from display"))
131 (define-key map [sepmv] '("--"))
132 (define-key map [next-line]
133 '(menu-item "Next line" vc-dir-next-line
134 :help "Go to the next line" :keys "n"))
135 (define-key map [previous-line]
136 '(menu-item "Previous line" vc-dir-previous-line
137 :help "Go to the previous line"))
139 (define-key map [sepmrk] '("--"))
140 (define-key map [unmark-all]
141 '(menu-item "Unmark All" vc-dir-unmark-all-files
142 :help "Unmark all files that are in the same state as the current file\
143 \nWith prefix argument unmark all files"))
144 (define-key map [unmark-previous]
145 '(menu-item "Unmark previous " vc-dir-unmark-file-up
146 :help "Move to the previous line and unmark the file"))
148 (define-key map [mark-all]
149 '(menu-item "Mark All" vc-dir-mark-all-files
150 :help "Mark all files that are in the same state as the current file\
151 \nWith prefix argument mark all files"))
152 (define-key map [unmark]
153 '(menu-item "Unmark" vc-dir-unmark
154 :help "Unmark the current file or all files in the region"))
156 (define-key map [mark]
157 '(menu-item "Mark" vc-dir-mark
158 :help "Mark the current file or all files in the region"))
160 (define-key map [sepopn] '("--"))
162 '(menu-item "Query Replace in Files..." vc-dir-query-replace-regexp
163 :help "Replace a string in the marked files"))
165 '(menu-item "Search Files..." vc-dir-search
166 :help "Search a regexp in the marked files"))
167 (define-key map [ires]
168 '(menu-item "Isearch Regexp Files..." vc-dir-isearch-regexp
169 :help "Incremental search a regexp in the marked files"))
170 (define-key map [ise]
171 '(menu-item "Isearch Files..." vc-dir-isearch
172 :help "Incremental search a string in the marked files"))
173 (define-key map [open-other]
174 '(menu-item "Open in other window" vc-dir-find-file-other-window
175 :help "Find the file on the current line, in another window"))
176 (define-key map [open]
177 '(menu-item "Open file" vc-dir-find-file
178 :help "Find the file on the current line"))
179 (define-key map [sepvcdet] '("--"))
180 ;; FIXME: This needs a key binding. And maybe a better name
181 ;; ("Insert" like PCL-CVS uses does not sound that great either)...
182 (define-key map [ins]
183 '(menu-item "Show File" vc-dir-show-fileentry
184 :help "Show a file in the VC status listing even though it might be up to date"))
185 (define-key map [annotate]
186 '(menu-item "Annotate" vc-annotate
187 :help "Display the edit history of the current file using colors"))
188 (define-key map [diff]
189 '(menu-item "Compare with Base Version" vc-diff
190 :help "Compare file set with the base version"))
191 (define-key map [log]
192 '(menu-item "Show history" vc-print-log
193 :help "List the change log of the current file set in a window"))
195 (define-key map [sepvccmd] '("--"))
196 (define-key map [update]
197 '(menu-item "Update to latest version" vc-update
198 :help "Update the current fileset's files to their tip revisions"))
199 (define-key map [revert]
200 '(menu-item "Revert to base version" vc-revert
201 :help "Revert working copies of the selected fileset to their repository contents."))
202 (define-key map [next-action]
203 ;; FIXME: This really really really needs a better name!
204 ;; And a key binding too.
205 '(menu-item "Check In/Out" vc-next-action
206 :help "Do the next logical version control operation on the current fileset"))
207 (define-key map [register]
208 '(menu-item "Register" vc-register
209 :help "Register file set into the version control system"))
213 ;; VC backends can use this to add mode-specific menu items to
215 (defun vc-dir-menu-map-filter (orig-binding)
216 (when (and (symbolp orig-binding) (fboundp orig-binding))
217 (setq orig-binding (indirect-function orig-binding)))
219 (when (derived-mode-p 'vc-dir-mode)
220 (vc-call-backend vc-dir-backend 'extra-status-menu))))
221 (if (null ext-binding)
227 (defvar vc-dir-mode-map
228 (let ((map (make-sparse-keymap)))
230 (define-key map "v" 'vc-next-action) ;; C-x v v
231 (define-key map "=" 'vc-diff) ;; C-x v =
232 (define-key map "i" 'vc-register) ;; C-x v i
233 (define-key map "+" 'vc-update) ;; C-x v +
234 (define-key map "l" 'vc-print-log) ;; C-x v l
235 ;; More confusing than helpful, probably
236 ;;(define-key map "R" 'vc-revert) ;; u is taken by vc-dir-unmark.
237 ;;(define-key map "A" 'vc-annotate) ;; g is taken by revert-buffer
238 ;; bound by `special-mode'.
240 (define-key map "m" 'vc-dir-mark)
241 (define-key map "M" 'vc-dir-mark-all-files)
242 (define-key map "u" 'vc-dir-unmark)
243 (define-key map "U" 'vc-dir-unmark-all-files)
244 (define-key map "\C-?" 'vc-dir-unmark-file-up)
245 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
247 (define-key map "n" 'vc-dir-next-line)
248 (define-key map " " 'vc-dir-next-line)
249 (define-key map "\t" 'vc-dir-next-directory)
250 (define-key map "p" 'vc-dir-previous-line)
251 (define-key map [backtab] 'vc-dir-previous-directory)
252 ;;; Rebind paragraph-movement commands.
253 (define-key map "\M-}" 'vc-dir-next-directory)
254 (define-key map "\M-{" 'vc-dir-previous-directory)
255 (define-key map [C-down] 'vc-dir-next-directory)
256 (define-key map [C-up] 'vc-dir-previous-directory)
258 (define-key map "f" 'vc-dir-find-file)
259 (define-key map "\C-m" 'vc-dir-find-file)
260 (define-key map "o" 'vc-dir-find-file-other-window)
261 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
262 (define-key map [down-mouse-3] 'vc-dir-menu)
263 (define-key map [mouse-2] 'vc-dir-toggle-mark)
264 (define-key map [follow-link] 'mouse-face)
265 (define-key map "x" 'vc-dir-hide-up-to-date)
266 (define-key map [?\C-k] 'vc-dir-kill-line)
267 (define-key map "S" 'vc-dir-search) ;; FIXME: Maybe use A like dired?
268 (define-key map "Q" 'vc-dir-query-replace-regexp)
269 (define-key map (kbd "M-s a C-s") 'vc-dir-isearch)
270 (define-key map (kbd "M-s a M-C-s") 'vc-dir-isearch-regexp)
273 (define-key map [menu-bar vc-dir-mode]
275 ;; VC backends can use this to add mode-specific menu items to
277 "VC-dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
279 "Keymap for directory buffer.")
281 (defmacro vc-dir-at-event (event &rest body)
282 "Evaluate BODY with point located at event-start of EVENT.
283 If BODY uses EVENT, it should be a variable,
284 otherwise it will be evaluated twice."
285 (let ((posn (make-symbol "vc-dir-at-event-posn")))
287 (unless (equal ,event '(tool-bar))
288 (let ((,posn (event-start ,event)))
289 (set-buffer (window-buffer (posn-window ,posn)))
290 (goto-char (posn-point ,posn))))
293 (defun vc-dir-menu (e)
294 "Popup the VC dir menu."
296 (vc-dir-at-event e (popup-menu vc-dir-menu-map e)))
298 (defvar vc-dir-tool-bar-map
299 (let ((map (make-sparse-keymap)))
300 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
302 (tool-bar-local-item "bookmark_add"
303 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
304 :help "Toggle mark on current item"
305 :label "Toggle Mark")
306 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
309 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
312 (tool-bar-local-item-from-menu 'vc-print-log "info"
314 (tool-bar-local-item-from-menu 'revert-buffer "refresh"
316 (tool-bar-local-item-from-menu 'nonincremental-search-forward
319 (tool-bar-local-item-from-menu 'vc-dir-query-replace-regexp
320 "search-replace" map vc-dir-mode-map
322 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
325 (tool-bar-local-item-from-menu 'quit-window "exit"
329 (defun vc-dir-node-directory (node)
330 ;; Compute the directory for NODE.
331 ;; If it's a directory node, get it from the node.
332 (let ((data (ewoc-data node)))
333 (or (vc-dir-fileinfo->directory data)
334 ;; Otherwise compute it from the file name.
338 (vc-dir-fileinfo->name data)))))))
340 (defun vc-dir-update (entries buffer &optional noinsert)
341 "Update BUFFER's ewoc from the list of ENTRIES.
342 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
343 ;; Add ENTRIES to the vc-dir buffer BUFFER.
344 (with-current-buffer buffer
345 ;; Insert the entries sorted by name into the ewoc.
346 ;; We assume the ewoc is sorted too, which should be the
347 ;; case if we always add entries with vc-dir-update.
349 ;; Sort: first files and then subdirectories.
350 ;; XXX: this is VERY inefficient, it computes the directory
351 ;; names too many times
353 (lambda (entry1 entry2)
354 (let ((dir1 (file-name-directory
355 (directory-file-name (expand-file-name (car entry1)))))
356 (dir2 (file-name-directory
357 (directory-file-name (expand-file-name (car entry2))))))
359 ((string< dir1 dir2) t)
360 ((not (string= dir1 dir2)) nil)
361 ((string< (car entry1) (car entry2))))))))
362 ;; Insert directory entries in the right places.
363 (let ((entry (car entries))
364 (node (ewoc-nth vc-ewoc 0))
366 (dotname (file-relative-name default-directory)))
367 ;; Insert . if it is not present.
370 vc-ewoc (vc-dir-create-fileinfo
371 dotname nil nil nil default-directory))
372 (setq node (ewoc-nth vc-ewoc 0)))
374 (while (and entry node)
375 (let* ((entryfile (car entry))
376 (entrydir (file-name-directory (directory-file-name
377 (expand-file-name entryfile))))
378 (nodedir (vc-dir-node-directory node)))
380 ;; First try to find the directory.
381 ((string-lessp nodedir entrydir)
382 (setq node (ewoc-next vc-ewoc node)))
383 ((string-equal nodedir entrydir)
384 ;; Found the directory, find the place for the file name.
385 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
387 ((string= nodefile dotname)
388 (setq node (ewoc-next vc-ewoc node)))
389 ((string-lessp nodefile entryfile)
390 (setq node (ewoc-next vc-ewoc node)))
391 ((string-equal nodefile entryfile)
394 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
395 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
396 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
397 (ewoc-invalidate vc-ewoc node))
398 ;; If the state is nil, the file does not exist
399 ;; anymore, so remember the entry so we can remove
400 ;; it after we are done inserting all ENTRIES.
401 (push node to-remove))
402 (setq entries (cdr entries))
403 (setq entry (car entries))
404 (setq node (ewoc-next vc-ewoc node)))
406 (ewoc-enter-before vc-ewoc node
407 (apply 'vc-dir-create-fileinfo entry))
408 (setq entries (cdr entries))
409 (setq entry (car entries))))))
411 ;; We might need to insert a directory node if the
412 ;; previous node was in a different directory.
413 (let* ((rd (file-relative-name entrydir))
414 (prev-node (ewoc-prev vc-ewoc node))
415 (prev-dir (vc-dir-node-directory prev-node)))
416 (unless (string-equal entrydir prev-dir)
418 vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
419 ;; Now insert the node itself.
420 (ewoc-enter-before vc-ewoc node
421 (apply 'vc-dir-create-fileinfo entry))
422 (setq entries (cdr entries) entry (car entries))))))
423 ;; We're past the last node, all remaining entries go to the end.
424 (unless (or node noinsert)
425 (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
426 (dolist (entry entries)
427 (let ((entrydir (file-name-directory
428 (directory-file-name (expand-file-name (car entry))))))
429 ;; Insert a directory node if needed.
430 (unless (string-equal lastdir entrydir)
431 (setq lastdir entrydir)
432 (let ((rd (file-relative-name entrydir)))
434 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
435 ;; Now insert the node itself.
436 (ewoc-enter-last vc-ewoc
437 (apply 'vc-dir-create-fileinfo entry))))))
439 (let ((inhibit-read-only t))
440 (apply 'ewoc-delete vc-ewoc (nreverse to-remove)))))))
442 (defun vc-dir-busy ()
443 (and (buffer-live-p vc-dir-process-buffer)
444 (get-buffer-process vc-dir-process-buffer)))
446 (defun vc-dir-kill-dir-status-process ()
447 "Kill the temporary buffer and associated process."
449 (when (buffer-live-p vc-dir-process-buffer)
450 (let ((proc (get-buffer-process vc-dir-process-buffer)))
451 (when proc (delete-process proc))
452 (setq vc-dir-process-buffer nil)
453 (setq mode-line-process nil))))
455 (defun vc-dir-kill-query ()
456 ;; Make sure that when the status buffer is killed the update
457 ;; process running in background is also killed.
459 (when (y-or-n-p "Status update process running, really kill status buffer? ")
460 (vc-dir-kill-dir-status-process)
464 (defun vc-dir-next-line (arg)
465 "Go to the next line.
466 If a prefix argument is given, move by that many lines."
469 (ewoc-goto-next vc-ewoc arg)
470 (vc-dir-move-to-goal-column)))
472 (defun vc-dir-previous-line (arg)
473 "Go to the previous line.
474 If a prefix argument is given, move by that many lines."
476 (ewoc-goto-prev vc-ewoc arg)
477 (vc-dir-move-to-goal-column))
479 (defun vc-dir-next-directory ()
480 "Go to the next directory."
482 (let ((orig (point)))
486 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
491 (ewoc-goto-node vc-ewoc next)
492 (vc-dir-move-to-goal-column)
493 (if (vc-dir-fileinfo->directory (ewoc-data next))
494 (throw 'foundit nil))))))))
497 (defun vc-dir-previous-directory ()
498 "Go to the previous directory."
500 (let ((orig (point)))
504 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
509 (ewoc-goto-node vc-ewoc prev)
510 (vc-dir-move-to-goal-column)
511 (if (vc-dir-fileinfo->directory (ewoc-data prev))
512 (throw 'foundit nil))))))))
515 (defun vc-dir-mark-unmark (mark-unmark-function)
517 (let ((firstl (line-number-at-pos (region-beginning)))
518 (lastl (line-number-at-pos (region-end))))
520 (goto-char (region-beginning))
521 (while (<= (line-number-at-pos) lastl)
522 (funcall mark-unmark-function))))
523 (funcall mark-unmark-function)))
525 (defun vc-dir-parent-marked-p (arg)
526 ;; Return nil if none of the parent directories of arg is marked.
527 (let* ((argdir (vc-dir-node-directory arg))
528 (arglen (length argdir))
531 ;; Go through the predecessors, checking if any directory that is
532 ;; a parent is marked.
533 (while (setq crt (ewoc-prev vc-ewoc crt))
534 (setq data (ewoc-data crt))
535 (setq dir (vc-dir-node-directory crt))
536 (when (and (vc-dir-fileinfo->directory data)
537 (vc-string-prefix-p dir argdir))
538 (when (vc-dir-fileinfo->marked data)
539 (error "Cannot mark `%s', parent directory `%s' marked"
540 (vc-dir-fileinfo->name (ewoc-data arg))
541 (vc-dir-fileinfo->name data)))))
544 (defun vc-dir-children-marked-p (arg)
545 ;; Return nil if none of the children of arg is marked.
546 (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
550 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
551 (setq data (ewoc-data crt))
552 (setq dir (vc-dir-node-directory crt))
553 (if (string-match argdir-re dir)
554 (when (vc-dir-fileinfo->marked data)
555 (error "Cannot mark `%s', child `%s' marked"
556 (vc-dir-fileinfo->name (ewoc-data arg))
557 (vc-dir-fileinfo->name data)))
558 ;; We are done, we got to an entry that is not a child of `arg'.
559 (setq is-child nil)))
562 (defun vc-dir-mark-file (&optional arg)
563 ;; Mark ARG or the current file and move to the next line.
564 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
565 (file (ewoc-data crt))
566 (isdir (vc-dir-fileinfo->directory file)))
567 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
568 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
569 (setf (vc-dir-fileinfo->marked file) t)
570 (ewoc-invalidate vc-ewoc crt)
571 (unless (or arg (mouse-event-p last-command-event))
572 (vc-dir-next-line 1)))))
574 (defun vc-dir-mark ()
575 "Mark the current file or all files in the region.
576 If the region is active, mark all the files in the region.
577 Otherwise mark the file on the current line and move to the next
580 (vc-dir-mark-unmark 'vc-dir-mark-file))
582 (defun vc-dir-mark-all-files (arg)
583 "Mark all files with the same state as the current one.
584 With a prefix argument mark all files.
585 If the current entry is a directory, mark all child files.
587 The commands operate on files that are on the same state.
588 This command is intended to make it easy to select all files that
589 share the same state."
594 ;; First check that no directory is marked, we can't mark
595 ;; files in that case.
598 (when (and (vc-dir-fileinfo->directory filearg)
599 (vc-dir-fileinfo->marked filearg))
600 (error "Cannot mark all files, directory `%s' marked"
601 (vc-dir-fileinfo->name filearg))))
605 (unless (vc-dir-fileinfo->marked filearg)
606 (setf (vc-dir-fileinfo->marked filearg) t)
609 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
610 (if (vc-dir-fileinfo->directory data)
611 ;; It's a directory, mark child files.
612 (let ((crt (ewoc-locate vc-ewoc)))
613 (unless (vc-dir-children-marked-p crt)
614 (while (setq crt (ewoc-next vc-ewoc crt))
615 (let ((crt-data (ewoc-data crt)))
616 (unless (vc-dir-fileinfo->directory crt-data)
617 (setf (vc-dir-fileinfo->marked crt-data) t)
618 (ewoc-invalidate vc-ewoc crt))))))
620 (let ((state (vc-dir-fileinfo->state data))
621 (crt (ewoc-nth vc-ewoc 0)))
623 (let ((crt-data (ewoc-data crt)))
624 (when (and (not (vc-dir-fileinfo->marked crt-data))
625 (eq (vc-dir-fileinfo->state crt-data) state)
626 (not (vc-dir-fileinfo->directory crt-data)))
627 (vc-dir-mark-file crt)))
628 (setq crt (ewoc-next vc-ewoc crt))))))))
630 (defun vc-dir-unmark-file ()
631 ;; Unmark the current file and move to the next line.
632 (let* ((crt (ewoc-locate vc-ewoc))
633 (file (ewoc-data crt)))
634 (setf (vc-dir-fileinfo->marked file) nil)
635 (ewoc-invalidate vc-ewoc crt)
636 (unless (mouse-event-p last-command-event)
637 (vc-dir-next-line 1))))
639 (defun vc-dir-unmark ()
640 "Unmark the current file or all files in the region.
641 If the region is active, unmark all the files in the region.
642 Otherwise mark the file on the current line and move to the next
645 (vc-dir-mark-unmark 'vc-dir-unmark-file))
647 (defun vc-dir-unmark-file-up ()
648 "Move to the previous line and unmark the file."
650 ;; If we're on the first line, we won't move up, but we will still
651 ;; remove the mark. This seems a bit odd but it is what buffer-menu
653 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
654 (file (ewoc-data prev)))
655 (setf (vc-dir-fileinfo->marked file) nil)
656 (ewoc-invalidate vc-ewoc prev)
657 (vc-dir-move-to-goal-column)))
659 (defun vc-dir-unmark-all-files (arg)
660 "Unmark all files with the same state as the current one.
661 With a prefix argument unmark all files.
662 If the current entry is a directory, unmark all the child files.
664 The commands operate on files that are on the same state.
665 This command is intended to make it easy to deselect all files
666 that share the same state."
671 (when (vc-dir-fileinfo->marked filearg)
672 (setf (vc-dir-fileinfo->marked filearg) nil)
675 (let* ((crt (ewoc-locate vc-ewoc))
676 (data (ewoc-data crt)))
677 (if (vc-dir-fileinfo->directory data)
678 ;; It's a directory, unmark child files.
679 (while (setq crt (ewoc-next vc-ewoc crt))
680 (let ((crt-data (ewoc-data crt)))
681 (unless (vc-dir-fileinfo->directory crt-data)
682 (setf (vc-dir-fileinfo->marked crt-data) nil)
683 (ewoc-invalidate vc-ewoc crt))))
685 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
688 (when (and (vc-dir-fileinfo->marked filearg)
689 (eq (vc-dir-fileinfo->state filearg) crt-state))
690 (setf (vc-dir-fileinfo->marked filearg) nil)
694 (defun vc-dir-toggle-mark-file ()
695 (let* ((crt (ewoc-locate vc-ewoc))
696 (file (ewoc-data crt)))
697 (if (vc-dir-fileinfo->marked file)
699 (vc-dir-mark-file))))
701 (defun vc-dir-toggle-mark (e)
703 (vc-dir-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
705 (defun vc-dir-delete-file ()
706 "Delete the marked files, or the current file if no marks."
708 (mapc 'vc-delete-file (or (vc-dir-marked-files)
709 (list (vc-dir-current-file)))))
711 (defun vc-dir-find-file ()
712 "Find the file on the current line."
714 (find-file (vc-dir-current-file)))
716 (defun vc-dir-find-file-other-window (&optional event)
717 "Find the file on the current line, in another window."
718 (interactive (list last-nonmenu-event))
719 (if event (posn-set-point (event-end event)))
720 (find-file-other-window (vc-dir-current-file)))
722 (defun vc-dir-isearch ()
723 "Search for a string through all marked buffers using Isearch."
726 (mapcar 'car (vc-dir-marked-only-files-and-states))))
728 (defun vc-dir-isearch-regexp ()
729 "Search for a regexp through all marked buffers using Isearch."
731 (multi-isearch-files-regexp
732 (mapcar 'car (vc-dir-marked-only-files-and-states))))
734 (defun vc-dir-search (regexp)
735 "Search through all marked files for a match for REGEXP.
736 For marked directories, use the files displayed from those directories.
737 Stops when a match is found.
738 To continue searching for next match, use command \\[tags-loop-continue]."
739 (interactive "sSearch marked files (regexp): ")
740 (tags-search regexp '(mapcar 'car (vc-dir-marked-only-files-and-states))))
742 (defun vc-dir-query-replace-regexp (from to &optional delimited)
743 "Do `query-replace-regexp' of FROM with TO, on all marked files.
744 For marked directories, use the files displayed from those directories.
745 If a directory is marked, then use the files displayed for that directory.
746 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
747 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
748 with the command \\[tags-loop-continue]."
749 ;; FIXME: this is almost a copy of `dired-do-replace-regexp'. This
750 ;; should probably be made generic and used in both places instead of
751 ;; duplicating it here.
754 (query-replace-read-args
755 "Query replace regexp in marked files" t t)))
756 (list (nth 0 common) (nth 1 common) (nth 2 common))))
757 (dolist (file (mapcar 'car (vc-dir-marked-only-files-and-states)))
758 (let ((buffer (get-file-buffer file)))
759 (if (and buffer (with-current-buffer buffer
761 (error "File `%s' is visited read-only" file))))
762 (tags-query-replace from to delimited
763 '(mapcar 'car (vc-dir-marked-only-files-and-states))))
765 (defun vc-dir-current-file ()
766 (let ((node (ewoc-locate vc-ewoc)))
768 (error "No file available"))
769 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
771 (defun vc-dir-marked-files ()
772 "Return the list of marked files."
774 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
775 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
777 (defun vc-dir-marked-only-files-and-states ()
778 "Return the list of conses (FILE . STATE) for the marked files.
779 For marked directories return the corresponding conses for the
781 (let ((crt (ewoc-nth vc-ewoc 0))
784 (let ((crt-data (ewoc-data crt)))
785 (if (vc-dir-fileinfo->marked crt-data)
786 ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
787 (if (vc-dir-fileinfo->directory crt-data)
788 (let* ((dir (vc-dir-fileinfo->directory crt-data))
789 (dirlen (length dir))
792 (and (setq crt (ewoc-next vc-ewoc crt))
793 (vc-string-prefix-p dir
795 (setq data (ewoc-data crt))
796 (vc-dir-node-directory crt))))
797 (unless (vc-dir-fileinfo->directory data)
799 (cons (expand-file-name (vc-dir-fileinfo->name data))
800 (vc-dir-fileinfo->state data))
802 (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
803 (vc-dir-fileinfo->state crt-data))
805 (setq crt (ewoc-next vc-ewoc crt)))
806 (setq crt (ewoc-next vc-ewoc crt)))))
809 (defun vc-dir-child-files-and-states ()
810 "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
811 If it is a file, return the corresponding cons for the file itself."
812 (let* ((crt (ewoc-locate vc-ewoc))
813 (crt-data (ewoc-data crt))
815 (if (vc-dir-fileinfo->directory crt-data)
816 (let* ((dir (vc-dir-fileinfo->directory crt-data))
817 (dirlen (length dir))
820 (and (setq crt (ewoc-next vc-ewoc crt))
821 (vc-string-prefix-p dir (progn
822 (setq data (ewoc-data crt))
823 (vc-dir-node-directory crt))))
824 (unless (vc-dir-fileinfo->directory data)
826 (cons (expand-file-name (vc-dir-fileinfo->name data))
827 (vc-dir-fileinfo->state data))
830 (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
831 (vc-dir-fileinfo->state crt-data)) result))
834 (defun vc-dir-recompute-file-state (fname def-dir)
835 (let* ((file-short (file-relative-name fname def-dir))
836 (remove-me-when-CVS-works
837 (when (eq vc-dir-backend 'CVS)
838 ;; FIXME: Warning: UGLY HACK. The CVS backend caches the state
839 ;; info, this forces the backend to update it.
840 (vc-call-backend vc-dir-backend 'registered fname)))
841 (state (vc-call-backend vc-dir-backend 'state fname))
842 (extra (vc-call-backend vc-dir-backend
843 'status-fileinfo-extra fname)))
844 (list file-short state extra)))
846 (defun vc-dir-find-child-files (dirname)
847 ;; Give a DIRNAME string return the list of all child files shown in
848 ;; the current *vc-dir* buffer.
849 (let ((crt (ewoc-nth vc-ewoc 0))
853 (while (and crt (not (vc-string-prefix-p
854 dirname (vc-dir-node-directory crt))))
855 (setq crt (ewoc-next vc-ewoc crt)))
856 (while (and crt (vc-string-prefix-p
858 (setq dname (vc-dir-node-directory crt))))
859 (let ((data (ewoc-data crt)))
860 (unless (vc-dir-fileinfo->directory data)
861 (push (expand-file-name (vc-dir-fileinfo->name data)) children)))
862 (setq crt (ewoc-next vc-ewoc crt)))
865 (defun vc-dir-resync-directory-files (dirname)
866 ;; Update the entries for all the child files of DIRNAME shown in
867 ;; the current *vc-dir* buffer.
868 (let ((files (vc-dir-find-child-files dirname))
869 (ddir default-directory)
873 (push (vc-dir-recompute-file-state crt ddir)
875 (vc-dir-update fileentries (current-buffer)))))
877 (defun vc-dir-resynch-file (&optional fname)
878 "Update the entries for FNAME in any directory buffers that list it."
879 (let ((file (or fname (expand-file-name buffer-file-name)))
882 ;; look for a vc-dir buffer that might show this file.
883 (dolist (status-buf vc-dir-buffers)
884 (if (not (buffer-live-p status-buf))
885 (push status-buf drop)
886 (set-buffer status-buf)
887 (if (not (derived-mode-p 'vc-dir-mode))
888 (push status-buf drop)
889 (let ((ddir default-directory))
890 (when (vc-string-prefix-p ddir file)
891 (if (file-directory-p file)
893 (vc-dir-resync-directory-files file)
895 (vc-dir-headers vc-dir-backend default-directory) ""))
896 (let ((state (vc-dir-recompute-file-state file ddir)))
899 status-buf (eq (cadr state) 'up-to-date))))))))))
900 ;; Remove out-of-date entries from vc-dir-buffers.
901 (dolist (b drop) (setq vc-dir-buffers (delq b vc-dir-buffers)))))
903 (defvar use-vc-backend) ;; dynamically bound
905 (define-derived-mode vc-dir-mode special-mode "VC dir"
906 "Major mode for VC directory buffers.
907 Marking/Unmarking key bindings and actions:
908 m - mark a file/directory
909 - if the region is active, mark all the files in region.
910 Restrictions: - a file cannot be marked if any parent directory is marked
911 - a directory cannot be marked if any child file or
913 u - unmark a file/directory
914 - if the region is active, unmark all the files in region.
915 M - if the cursor is on a file: mark all the files with the same state as
917 - if the cursor is on a directory: mark all child files
918 - with a prefix argument: mark all files
919 U - if the cursor is on a file: unmark all the files with the same state
921 - if the cursor is on a directory: unmark all child files
922 - with a prefix argument: unmark all files
923 mouse-2 - toggles the mark state
926 VC commands in the `C-x v' prefix can be used.
927 VC commands act on the marked entries. If nothing is marked, VC
928 commands act on the current entry.
931 S - searches the marked files
932 Q - does a query replace on the marked files
933 M-s a C-s - does an isearch on the marked files
934 M-s a C-M-s - does a regexp isearch on the marked files
935 If nothing is marked, these commands act on the current entry.
936 When a directory is current or marked, the Search & Replace
937 commands act on the child files of that directory that are displayed in
941 (set (make-local-variable 'vc-dir-backend) use-vc-backend)
942 (setq buffer-read-only t)
943 (when (boundp 'tool-bar-map)
944 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
945 (let ((buffer-read-only nil))
947 (set (make-local-variable 'vc-dir-process-buffer) nil)
948 (set (make-local-variable 'vc-ewoc) (ewoc-create #'vc-dir-printer))
949 (set (make-local-variable 'revert-buffer-function)
950 'vc-dir-revert-buffer-function)
951 (setq list-buffers-directory (expand-file-name "*vc-dir*" default-directory))
952 (add-to-list 'vc-dir-buffers (current-buffer))
953 ;; Make sure that if the directory buffer is killed, the update
954 ;; process running in the background is also killed.
955 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
956 (hack-dir-local-variables-non-file-buffer)
959 (defun vc-dir-headers (backend dir)
960 "Display the headers in the *VC dir* buffer.
961 It calls the `dir-extra-headers' backend method to display backend
964 ;; First layout the common headers.
965 (propertize "VC backend : " 'face 'font-lock-type-face)
966 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
967 (propertize "Working dir: " 'face 'font-lock-type-face)
968 (propertize (format "%s\n" (abbreviate-file-name dir))
969 'face 'font-lock-variable-name-face)
970 ;; Then the backend specific ones.
971 (vc-call-backend backend 'dir-extra-headers dir)
974 (defun vc-dir-refresh-files (files default-state)
975 "Refresh some files in the *VC-dir* buffer."
976 (let ((def-dir default-directory)
977 (backend vc-dir-backend))
978 (vc-set-mode-line-busy-indicator)
979 ;; Call the `dir-status-file' backend function.
980 ;; `dir-status-file' is supposed to be asynchronous.
981 ;; It should compute the results, and then call the function
982 ;; passed as an argument in order to update the vc-dir buffer
984 (unless (buffer-live-p vc-dir-process-buffer)
985 (setq vc-dir-process-buffer
986 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
987 (lexical-let ((buffer (current-buffer)))
988 (with-current-buffer vc-dir-process-buffer
992 backend 'dir-status-files def-dir files default-state
993 (lambda (entries &optional more-to-come)
994 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
995 ;; If MORE-TO-COME is true, then more updates will come from
996 ;; the asynchronous process.
997 (with-current-buffer buffer
998 (vc-dir-update entries buffer)
1000 (setq mode-line-process nil)
1001 ;; Remove the ones that haven't been updated at all.
1002 ;; Those not-updated are those whose state is nil because the
1003 ;; file/dir doesn't exist and isn't versioned.
1004 (ewoc-filter vc-ewoc
1006 ;; The state for directory entries might
1007 ;; have been changed to 'up-to-date,
1008 ;; reset it, othewise it will be removed when doing 'x'
1010 ;; FIXME: There should be a more elegant way to do this.
1011 (when (and (vc-dir-fileinfo->directory info)
1012 (eq (vc-dir-fileinfo->state info)
1014 (setf (vc-dir-fileinfo->state info) nil))
1016 (not (vc-dir-fileinfo->needs-update info))))))))))))
1018 (defun vc-dir-revert-buffer-function (&optional ignore-auto noconfirm)
1021 (defun vc-dir-refresh ()
1022 "Refresh the contents of the *VC-dir* buffer.
1023 Throw an error if another update process is in progress."
1026 (error "Another update process is in progress, cannot run two at a time")
1027 (let ((def-dir default-directory)
1028 (backend vc-dir-backend))
1029 (vc-set-mode-line-busy-indicator)
1030 ;; Call the `dir-status' backend function.
1031 ;; `dir-status' is supposed to be asynchronous.
1032 ;; It should compute the results, and then call the function
1033 ;; passed as an argument in order to update the vc-dir buffer
1034 ;; with the results.
1036 ;; Create a buffer that can be used by `dir-status' and call
1037 ;; `dir-status' with this buffer as the current buffer. Use
1038 ;; `vc-dir-process-buffer' to remember this buffer, so that
1039 ;; it can be used later to kill the update process in case it
1041 (unless (buffer-live-p vc-dir-process-buffer)
1042 (setq vc-dir-process-buffer
1043 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
1044 ;; set the needs-update flag on all non-directory entries
1045 (ewoc-map (lambda (info)
1046 (unless (vc-dir-fileinfo->directory info)
1047 (setf (vc-dir-fileinfo->needs-update info) t) nil))
1049 (lexical-let ((buffer (current-buffer)))
1050 (with-current-buffer vc-dir-process-buffer
1054 backend 'dir-status def-dir
1055 (lambda (entries &optional more-to-come)
1056 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1057 ;; If MORE-TO-COME is true, then more updates will come from
1058 ;; the asynchronous process.
1059 (with-current-buffer buffer
1060 (vc-dir-update entries buffer)
1061 (unless more-to-come
1064 vc-ewoc 'vc-dir-fileinfo->needs-update)))
1066 (vc-dir-refresh-files
1067 (mapcar 'vc-dir-fileinfo->name remaining)
1069 (setq mode-line-process nil)))))))))
1070 (ewoc-set-hf vc-ewoc (vc-dir-headers backend def-dir) ""))))
1072 (defun vc-dir-show-fileentry (file)
1073 "Insert an entry for a specific file into the current *VC-dir* listing.
1074 This is typically used if the file is up-to-date (or has been added
1075 outside of VC) and one wants to do some operation on it."
1076 (interactive "fShow file: ")
1077 (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
1079 (defun vc-dir-hide-up-to-date ()
1080 "Hide up-to-date items from display."
1082 (let ((crt (ewoc-nth vc-ewoc -1))
1083 (first (ewoc-nth vc-ewoc 0)))
1084 ;; Go over from the last item to the first and remove the
1085 ;; up-to-date files and directories with no child files.
1086 (while (not (eq crt first))
1087 (let* ((data (ewoc-data crt))
1088 (dir (vc-dir-fileinfo->directory data))
1089 (next (ewoc-next vc-ewoc crt))
1090 (prev (ewoc-prev vc-ewoc crt))
1091 ;; ewoc-delete does not work without this...
1092 (inhibit-read-only t))
1094 ;; Remove directories with no child files.
1097 ;; Nothing follows this directory.
1099 ;; Next item is a directory.
1100 (vc-dir-fileinfo->directory (ewoc-data next))))
1101 ;; Remove files in the up-to-date state.
1102 (eq (vc-dir-fileinfo->state data) 'up-to-date))
1103 (ewoc-delete vc-ewoc crt))
1106 (defun vc-dir-kill-line ()
1107 "Remove the current line from display."
1109 (let ((crt (ewoc-locate vc-ewoc))
1110 (inhibit-read-only t))
1111 (ewoc-delete vc-ewoc crt)))
1113 (defun vc-dir-printer (fileentry)
1114 (vc-call-backend vc-dir-backend 'dir-printer fileentry))
1116 (defun vc-dir-deduce-fileset (&optional state-model-only-files)
1117 (let ((marked (vc-dir-marked-files))
1125 (when state-model-only-files
1126 (setq only-files-list (vc-dir-marked-only-files-and-states))))
1127 (let ((crt (vc-dir-current-file)))
1128 (setq files (list crt))
1129 (when state-model-only-files
1130 (setq only-files-list (vc-dir-child-files-and-states)))))
1132 (when state-model-only-files
1133 (setq state (cdar only-files-list))
1134 ;; Check that all files are in a consistent state, since we use that
1135 ;; state to decide which operation to perform.
1136 (dolist (crt (cdr only-files-list))
1137 (unless (vc-compatible-state (cdr crt) state)
1138 (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"
1139 (car crt) (cdr crt) (caar only-files-list) state)))
1140 (setq only-files-list (mapcar 'car only-files-list))
1141 (when (and state (not (eq state 'unregistered)))
1142 (setq model (vc-checkout-model vc-dir-backend only-files-list))))
1143 (list vc-dir-backend files only-files-list state model)))
1146 (defun vc-dir (dir &optional backend)
1147 "Show the VC status for \"interesting\" files in and below DIR.
1148 This allows you to mark files and perform VC operations on them.
1149 The list omits files which are up to date, with no changes in your copy
1150 or the repository, if there is nothing in particular to say about them.
1152 Preparing the list of file status takes time; when the buffer
1153 first appears, it has only the first few lines of summary information.
1154 The file lines appear later.
1156 Optional second argument BACKEND specifies the VC backend to use.
1157 Interactively, a prefix argument means to ask for the backend.
1159 These are the commands available for use in the file status buffer:
1161 \\{vc-dir-mode-map}"
1165 ;; When you hit C-x v d in a visited VC file,
1166 ;; the *vc-dir* buffer visits the directory under its truename;
1167 ;; therefore it makes sense to always do that.
1168 ;; Otherwise if you do C-x v d -> C-x C-f -> C-c v d
1169 ;; you may get a new *vc-dir* buffer, different from the original
1170 (file-truename (read-file-name "VC status for directory: "
1171 default-directory default-directory t
1172 nil #'file-directory-p))
1173 (if current-prefix-arg
1177 (mapcar (lambda (b) (list (symbol-name b)))
1178 vc-handled-backends)
1181 (setq backend (vc-responsible-backend dir)))
1182 (let (pop-up-windows) ; based on cvs-examine; bug#6204
1183 (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir backend)))
1184 (if (derived-mode-p 'vc-dir-mode)
1186 ;; FIXME: find a better way to pass the backend to `vc-dir-mode'.
1187 (let ((use-vc-backend backend))
1190 (defun vc-default-dir-extra-headers (backend dir)
1191 ;; Be loud by default to remind people to add code to display
1192 ;; backend specific headers.
1193 ;; XXX: change this to return nil before the release.
1195 (propertize "Extra : " 'face 'font-lock-type-face)
1196 (propertize "Please add backend specific headers here. It's easy!"
1197 'face 'font-lock-warning-face)))
1199 (defvar vc-dir-filename-mouse-map
1200 (let ((map (make-sparse-keymap)))
1201 (define-key map [mouse-2] 'vc-dir-find-file-other-window)
1203 "Local keymap for visiting a file.")
1205 (defun vc-default-dir-printer (backend fileentry)
1206 "Pretty print FILEENTRY."
1207 ;; If you change the layout here, change vc-dir-move-to-goal-column.
1208 ;; VC backends can implement backend specific versions of this
1209 ;; function. Changes here might need to be reflected in the
1210 ;; vc-BACKEND-dir-printer functions.
1211 (let* ((isdir (vc-dir-fileinfo->directory fileentry))
1212 (state (if isdir "" (vc-dir-fileinfo->state fileentry)))
1213 (filename (vc-dir-fileinfo->name fileentry)))
1216 (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1217 'face 'font-lock-type-face)
1220 (format "%-20s" state)
1221 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1222 ((memq state '(missing conflict)) 'font-lock-warning-face)
1223 (t 'font-lock-variable-name-face))
1224 'mouse-face 'highlight)
1227 (format "%s" filename)
1229 (if isdir 'font-lock-comment-delimiter-face 'font-lock-function-name-face)
1232 "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
1233 "File\nmouse-3: Pop-up menu")
1234 'mouse-face 'highlight
1235 'keymap vc-dir-filename-mouse-map))))
1237 (defun vc-default-extra-status-menu (backend)
1240 (defun vc-default-status-fileinfo-extra (backend file)
1241 "Default absence of extra information returned for a file."
1246 ;; arch-tag: 0274a2e3-e8e9-4b1a-a73c-e8b9129d5d15
1247 ;;; vc-dir.el ends here