1 ;;; vc-dir.el --- Directory status display under VC
3 ;; Copyright (C) 2007, 2008, 2009
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
)))
93 ;; Look for another buffer name BNAME visiting the same directory.
96 (dolist (buffer (buffer-list))
98 (when (and (derived-mode-p 'vc-dir-mode
)
99 (eq vc-dir-backend backend
)
100 (string= (expand-file-name default-directory
) dir
))
101 (return buffer
)))))))
103 ;; Create a new buffer named BNAME.
104 (with-current-buffer (create-file-buffer bname
)
106 (vc-setup-buffer (current-buffer))
107 ;; Reset the vc-parent-buffer-name so that it does not appear
109 (setq vc-parent-buffer-name nil
)
112 (defvar vc-dir-menu-map
113 (let ((map (make-sparse-keymap "VC-dir")))
114 (define-key map
[quit]
115 '(menu-item "Quit" quit-window
117 (define-key map [kill]
118 '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
119 :enable (vc-dir-busy)
120 :help "Kill the command that updates the directory buffer"))
121 (define-key map [refresh]
122 '(menu-item "Refresh" revert-buffer
123 :enable (not (vc-dir-busy))
124 :help "Refresh the contents of the directory buffer"))
125 (define-key map [remup]
126 '(menu-item "Hide up-to-date" vc-dir-hide-up-to-date
127 :help "Hide up-to-date items from display"))
129 (define-key map [sepmv] '("--"))
130 (define-key map [next-line]
131 '(menu-item "Next line" vc-dir-next-line
132 :help "Go to the next line" :keys "n"))
133 (define-key map [previous-line]
134 '(menu-item "Previous line" vc-dir-previous-line
135 :help "Go to the previous line"))
137 (define-key map [sepmrk] '("--"))
138 (define-key map [unmark-all]
139 '(menu-item "Unmark All" vc-dir-unmark-all-files
140 :help "Unmark all files that are in the same state as the current file\
141 \nWith prefix argument unmark all files"))
142 (define-key map [unmark-previous]
143 '(menu-item "Unmark previous " vc-dir-unmark-file-up
144 :help "Move to the previous line and unmark the file"))
146 (define-key map [mark-all]
147 '(menu-item "Mark All" vc-dir-mark-all-files
148 :help "Mark all files that are in the same state as the current file\
149 \nWith prefix argument mark all files"))
150 (define-key map [unmark]
151 '(menu-item "Unmark" vc-dir-unmark
152 :help "Unmark the current file or all files in the region"))
154 (define-key map [mark]
155 '(menu-item "Mark" vc-dir-mark
156 :help "Mark the current file or all files in the region"))
158 (define-key map [sepopn] '("--"))
160 '(menu-item "Query Replace in Files..." vc-dir-query-replace-regexp
161 :help "Replace a string in the marked files"))
163 '(menu-item "Search Files..." vc-dir-search
164 :help "Search a regexp in the marked files"))
165 (define-key map [ires]
166 '(menu-item "Isearch Regexp Files..." vc-dir-isearch-regexp
167 :help "Incremental search a regexp in the marked files"))
168 (define-key map [ise]
169 '(menu-item "Isearch Files..." vc-dir-isearch
170 :help "Incremental search a string in the marked files"))
171 (define-key map [open-other]
172 '(menu-item "Open in other window" vc-dir-find-file-other-window
173 :help "Find the file on the current line, in another window"))
174 (define-key map [open]
175 '(menu-item "Open file" vc-dir-find-file
176 :help "Find the file on the current line"))
177 (define-key map [sepvcdet] '("--"))
178 ;; FIXME: This needs a key binding. And maybe a better name
179 ;; ("Insert" like PCL-CVS uses does not sound that great either)...
180 (define-key map [ins]
181 '(menu-item "Show File" vc-dir-show-fileentry
182 :help "Show a file in the VC status listing even though it might be up to date"))
183 (define-key map [annotate]
184 '(menu-item "Annotate" vc-annotate
185 :help "Display the edit history of the current file using colors"))
186 (define-key map [diff]
187 '(menu-item "Compare with Base Version" vc-diff
188 :help "Compare file set with the base version"))
189 (define-key map [log]
190 '(menu-item "Show history" vc-print-log
191 :help "List the change log of the current file set in a window"))
193 (define-key map [sepvccmd] '("--"))
194 (define-key map [update]
195 '(menu-item "Update to latest version" vc-update
196 :help "Update the current fileset's files to their tip revisions"))
197 (define-key map [revert]
198 '(menu-item "Revert to base version" vc-revert
199 :help "Revert working copies of the selected fileset to their repository contents."))
200 (define-key map [next-action]
201 ;; FIXME: This really really really needs a better name!
202 ;; And a key binding too.
203 '(menu-item "Check In/Out" vc-next-action
204 :help "Do the next logical version control operation on the current fileset"))
205 (define-key map [register]
206 '(menu-item "Register" vc-register
207 :help "Register file set into the version control system"))
211 ;; VC backends can use this to add mode-specific menu items to
213 (defun vc-dir-menu-map-filter (orig-binding)
214 (when (and (symbolp orig-binding) (fboundp orig-binding))
215 (setq orig-binding (indirect-function orig-binding)))
217 (when (derived-mode-p 'vc-dir-mode)
218 (vc-call-backend vc-dir-backend 'extra-status-menu))))
219 (if (null ext-binding)
225 (defvar vc-dir-mode-map
226 (let ((map (make-sparse-keymap)))
228 (define-key map "v" 'vc-next-action) ;; C-x v v
229 (define-key map "=" 'vc-diff) ;; C-x v =
230 (define-key map "i" 'vc-register) ;; C-x v i
231 (define-key map "+" 'vc-update) ;; C-x v +
232 (define-key map "l" 'vc-print-log) ;; C-x v l
233 ;; More confusing than helpful, probably
234 ;;(define-key map "R" 'vc-revert) ;; u is taken by vc-dir-unmark.
235 ;;(define-key map "A" 'vc-annotate) ;; g is taken by revert-buffer
236 ;; bound by `special-mode'.
238 (define-key map "m" 'vc-dir-mark)
239 (define-key map "M" 'vc-dir-mark-all-files)
240 (define-key map "u" 'vc-dir-unmark)
241 (define-key map "U" 'vc-dir-unmark-all-files)
242 (define-key map "\C-?" 'vc-dir-unmark-file-up)
243 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
245 (define-key map "n" 'vc-dir-next-line)
246 (define-key map " " 'vc-dir-next-line)
247 (define-key map "\t" 'vc-dir-next-directory)
248 (define-key map "p" 'vc-dir-previous-line)
249 (define-key map [backtab] 'vc-dir-previous-directory)
250 ;;; Rebind paragraph-movement commands.
251 (define-key map "\M-}" 'vc-dir-next-directory)
252 (define-key map "\M-{" 'vc-dir-previous-directory)
253 (define-key map [C-down] 'vc-dir-next-directory)
254 (define-key map [C-up] 'vc-dir-previous-directory)
256 (define-key map "f" 'vc-dir-find-file)
257 (define-key map "\C-m" 'vc-dir-find-file)
258 (define-key map "o" 'vc-dir-find-file-other-window)
259 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
260 (define-key map [down-mouse-3] 'vc-dir-menu)
261 (define-key map [mouse-2] 'vc-dir-toggle-mark)
262 (define-key map [follow-link] 'mouse-face)
263 (define-key map "x" 'vc-dir-hide-up-to-date)
264 (define-key map "S" 'vc-dir-search) ;; FIXME: Maybe use A like dired?
265 (define-key map "Q" 'vc-dir-query-replace-regexp)
266 (define-key map (kbd "M-s a C-s") 'vc-dir-isearch)
267 (define-key map (kbd "M-s a M-C-s") 'vc-dir-isearch-regexp)
270 (define-key map [menu-bar vc-dir-mode]
272 ;; VC backends can use this to add mode-specific menu items to
274 "VC-dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
276 "Keymap for directory buffer.")
278 (defmacro vc-dir-at-event (event &rest body)
279 "Evaluate BODY with point located at event-start of EVENT.
280 If BODY uses EVENT, it should be a variable,
281 otherwise it will be evaluated twice."
282 (let ((posn (make-symbol "vc-dir-at-event-posn")))
284 (unless (equal ,event '(tool-bar))
285 (let ((,posn (event-start ,event)))
286 (set-buffer (window-buffer (posn-window ,posn)))
287 (goto-char (posn-point ,posn))))
290 (defun vc-dir-menu (e)
291 "Popup the VC dir menu."
293 (vc-dir-at-event e (popup-menu vc-dir-menu-map e)))
295 (defvar vc-dir-tool-bar-map
296 (let ((map (make-sparse-keymap)))
297 (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
299 (tool-bar-local-item "bookmark_add"
300 'vc-dir-toggle-mark 'vc-dir-toggle-mark map
301 :help "Toggle mark on current item")
302 (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
305 (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
308 (tool-bar-local-item-from-menu 'vc-print-log "info"
310 (tool-bar-local-item-from-menu 'revert-buffer "refresh"
312 (tool-bar-local-item-from-menu 'nonincremental-search-forward
314 (tool-bar-local-item-from-menu 'vc-dir-query-replace-regexp
315 "search-replace" map vc-dir-mode-map)
316 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
318 (tool-bar-local-item-from-menu 'quit-window "exit"
322 (defun vc-dir-node-directory (node)
323 ;; Compute the directory for NODE.
324 ;; If it's a directory node, get it from the the node.
325 (let ((data (ewoc-data node)))
326 (or (vc-dir-fileinfo->directory data)
327 ;; Otherwise compute it from the file name.
330 (vc-dir-fileinfo->name data))))))
332 (defun vc-dir-update (entries buffer &optional noinsert)
333 "Update BUFFER's ewoc from the list of ENTRIES.
334 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
335 ;; Add ENTRIES to the vc-dir buffer BUFFER.
336 (with-current-buffer buffer
337 ;; Insert the entries sorted by name into the ewoc.
338 ;; We assume the ewoc is sorted too, which should be the
339 ;; case if we always add entries with vc-dir-update.
341 ;; Sort: first files and then subdirectories.
342 ;; XXX: this is VERY inefficient, it computes the directory
343 ;; names too many times
345 (lambda (entry1 entry2)
346 (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
347 (dir2 (file-name-directory (expand-file-name (car entry2)))))
349 ((string< dir1 dir2) t)
350 ((not (string= dir1 dir2)) nil)
351 ((string< (car entry1) (car entry2))))))))
352 ;; Insert directory entries in the right places.
353 (let ((entry (car entries))
354 (node (ewoc-nth vc-ewoc 0)))
355 ;; Insert . if it is not present.
357 (let ((rd (file-relative-name default-directory)))
359 vc-ewoc (vc-dir-create-fileinfo
360 rd nil nil nil (expand-file-name default-directory))))
361 (setq node (ewoc-nth vc-ewoc 0)))
363 (while (and entry node)
364 (let* ((entryfile (car entry))
365 (entrydir (file-name-directory (expand-file-name entryfile)))
366 (nodedir (vc-dir-node-directory node)))
368 ;; First try to find the directory.
369 ((string-lessp nodedir entrydir)
370 (setq node (ewoc-next vc-ewoc node)))
371 ((string-equal nodedir entrydir)
372 ;; Found the directory, find the place for the file name.
373 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
375 ((string-lessp nodefile entryfile)
376 (setq node (ewoc-next vc-ewoc node)))
377 ((string-equal nodefile entryfile)
378 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
379 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
380 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
381 (ewoc-invalidate vc-ewoc node)
382 (setq entries (cdr entries))
383 (setq entry (car entries))
384 (setq node (ewoc-next vc-ewoc node)))
386 (ewoc-enter-before vc-ewoc node
387 (apply 'vc-dir-create-fileinfo entry))
388 (setq entries (cdr entries))
389 (setq entry (car entries))))))
391 ;; We might need to insert a directory node if the
392 ;; previous node was in a different directory.
393 (let* ((rd (file-relative-name entrydir))
394 (prev-node (ewoc-prev vc-ewoc node))
395 (prev-dir (vc-dir-node-directory prev-node)))
396 (unless (string-equal entrydir prev-dir)
398 vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
399 ;; Now insert the node itself.
400 (ewoc-enter-before vc-ewoc node
401 (apply 'vc-dir-create-fileinfo entry))
402 (setq entries (cdr entries) entry (car entries))))))
403 ;; We're past the last node, all remaining entries go to the end.
404 (unless (or node noinsert)
405 (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
406 (dolist (entry entries)
407 (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
408 ;; Insert a directory node if needed.
409 (unless (string-equal lastdir entrydir)
410 (setq lastdir entrydir)
411 (let ((rd (file-relative-name entrydir)))
413 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
414 ;; Now insert the node itself.
415 (ewoc-enter-last vc-ewoc
416 (apply 'vc-dir-create-fileinfo entry)))))))))
418 (defun vc-dir-busy ()
419 (and (buffer-live-p vc-dir-process-buffer)
420 (get-buffer-process vc-dir-process-buffer)))
422 (defun vc-dir-kill-dir-status-process ()
423 "Kill the temporary buffer and associated process."
425 (when (buffer-live-p vc-dir-process-buffer)
426 (let ((proc (get-buffer-process vc-dir-process-buffer)))
427 (when proc (delete-process proc))
428 (setq vc-dir-process-buffer nil)
429 (setq mode-line-process nil))))
431 (defun vc-dir-kill-query ()
432 ;; Make sure that when the status buffer is killed the update
433 ;; process running in background is also killed.
435 (when (y-or-n-p "Status update process running, really kill status buffer? ")
436 (vc-dir-kill-dir-status-process)
440 (defun vc-dir-next-line (arg)
441 "Go to the next line.
442 If a prefix argument is given, move by that many lines."
445 (ewoc-goto-next vc-ewoc arg)
446 (vc-dir-move-to-goal-column)))
448 (defun vc-dir-previous-line (arg)
449 "Go to the previous line.
450 If a prefix argument is given, move by that many lines."
452 (ewoc-goto-prev vc-ewoc arg)
453 (vc-dir-move-to-goal-column))
455 (defun vc-dir-next-directory ()
456 "Go to the next directory."
458 (let ((orig (point)))
462 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
467 (ewoc-goto-node vc-ewoc next)
468 (vc-dir-move-to-goal-column)
469 (if (vc-dir-fileinfo->directory (ewoc-data next))
470 (throw 'foundit nil))))))))
473 (defun vc-dir-previous-directory ()
474 "Go to the previous directory."
476 (let ((orig (point)))
480 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
485 (ewoc-goto-node vc-ewoc prev)
486 (vc-dir-move-to-goal-column)
487 (if (vc-dir-fileinfo->directory (ewoc-data prev))
488 (throw 'foundit nil))))))))
491 (defun vc-dir-mark-unmark (mark-unmark-function)
493 (let ((firstl (line-number-at-pos (region-beginning)))
494 (lastl (line-number-at-pos (region-end))))
496 (goto-char (region-beginning))
497 (while (<= (line-number-at-pos) lastl)
498 (funcall mark-unmark-function))))
499 (funcall mark-unmark-function)))
501 (defun vc-dir-parent-marked-p (arg)
502 ;; Return nil if none of the parent directories of arg is marked.
503 (let* ((argdir (vc-dir-node-directory arg))
504 (arglen (length argdir))
507 ;; Go through the predecessors, checking if any directory that is
508 ;; a parent is marked.
509 (while (setq crt (ewoc-prev vc-ewoc crt))
510 (setq data (ewoc-data crt))
511 (setq dir (vc-dir-node-directory crt))
512 (when (and (vc-dir-fileinfo->directory data)
513 (vc-string-prefix-p dir argdir))
514 (when (vc-dir-fileinfo->marked data)
515 (error "Cannot mark `%s', parent directory `%s' marked"
516 (vc-dir-fileinfo->name (ewoc-data arg))
517 (vc-dir-fileinfo->name data)))))
520 (defun vc-dir-children-marked-p (arg)
521 ;; Return nil if none of the children of arg is marked.
522 (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
526 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
527 (setq data (ewoc-data crt))
528 (setq dir (vc-dir-node-directory crt))
529 (if (string-match argdir-re dir)
530 (when (vc-dir-fileinfo->marked data)
531 (error "Cannot mark `%s', child `%s' marked"
532 (vc-dir-fileinfo->name (ewoc-data arg))
533 (vc-dir-fileinfo->name data)))
534 ;; We are done, we got to an entry that is not a child of `arg'.
535 (setq is-child nil)))
538 (defun vc-dir-mark-file (&optional arg)
539 ;; Mark ARG or the current file and move to the next line.
540 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
541 (file (ewoc-data crt))
542 (isdir (vc-dir-fileinfo->directory file)))
543 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
544 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
545 (setf (vc-dir-fileinfo->marked file) t)
546 (ewoc-invalidate vc-ewoc crt)
547 (unless (or arg (mouse-event-p last-command-event))
548 (vc-dir-next-line 1)))))
550 (defun vc-dir-mark ()
551 "Mark the current file or all files in the region.
552 If the region is active, mark all the files in the region.
553 Otherwise mark the file on the current line and move to the next
556 (vc-dir-mark-unmark 'vc-dir-mark-file))
558 (defun vc-dir-mark-all-files (arg)
559 "Mark all files with the same state as the current one.
560 With a prefix argument mark all files.
561 If the current entry is a directory, mark all child files.
563 The commands operate on files that are on the same state.
564 This command is intended to make it easy to select all files that
565 share the same state."
570 ;; First check that no directory is marked, we can't mark
571 ;; files in that case.
574 (when (and (vc-dir-fileinfo->directory filearg)
575 (vc-dir-fileinfo->marked filearg))
576 (error "Cannot mark all files, directory `%s' marked"
577 (vc-dir-fileinfo->name filearg))))
581 (unless (vc-dir-fileinfo->marked filearg)
582 (setf (vc-dir-fileinfo->marked filearg) t)
585 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
586 (if (vc-dir-fileinfo->directory data)
587 ;; It's a directory, mark child files.
588 (let ((crt (ewoc-locate vc-ewoc)))
589 (unless (vc-dir-children-marked-p crt)
590 (while (setq crt (ewoc-next vc-ewoc crt))
591 (let ((crt-data (ewoc-data crt)))
592 (unless (vc-dir-fileinfo->directory crt-data)
593 (setf (vc-dir-fileinfo->marked crt-data) t)
594 (ewoc-invalidate vc-ewoc crt))))))
596 (let ((state (vc-dir-fileinfo->state data))
597 (crt (ewoc-nth vc-ewoc 0)))
599 (let ((crt-data (ewoc-data crt)))
600 (when (and (not (vc-dir-fileinfo->marked crt-data))
601 (eq (vc-dir-fileinfo->state crt-data) state)
602 (not (vc-dir-fileinfo->directory crt-data)))
603 (vc-dir-mark-file crt)))
604 (setq crt (ewoc-next vc-ewoc crt))))))))
606 (defun vc-dir-unmark-file ()
607 ;; Unmark the current file and move to the next line.
608 (let* ((crt (ewoc-locate vc-ewoc))
609 (file (ewoc-data crt)))
610 (setf (vc-dir-fileinfo->marked file) nil)
611 (ewoc-invalidate vc-ewoc crt)
612 (unless (mouse-event-p last-command-event)
613 (vc-dir-next-line 1))))
615 (defun vc-dir-unmark ()
616 "Unmark the current file or all files in the region.
617 If the region is active, unmark all the files in the region.
618 Otherwise mark the file on the current line and move to the next
621 (vc-dir-mark-unmark 'vc-dir-unmark-file))
623 (defun vc-dir-unmark-file-up ()
624 "Move to the previous line and unmark the file."
626 ;; If we're on the first line, we won't move up, but we will still
627 ;; remove the mark. This seems a bit odd but it is what buffer-menu
629 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
630 (file (ewoc-data prev)))
631 (setf (vc-dir-fileinfo->marked file) nil)
632 (ewoc-invalidate vc-ewoc prev)
633 (vc-dir-move-to-goal-column)))
635 (defun vc-dir-unmark-all-files (arg)
636 "Unmark all files with the same state as the current one.
637 With a prefix argument unmark all files.
638 If the current entry is a directory, unmark all the child files.
640 The commands operate on files that are on the same state.
641 This command is intended to make it easy to deselect all files
642 that share the same state."
647 (when (vc-dir-fileinfo->marked filearg)
648 (setf (vc-dir-fileinfo->marked filearg) nil)
651 (let* ((crt (ewoc-locate vc-ewoc))
652 (data (ewoc-data crt)))
653 (if (vc-dir-fileinfo->directory data)
654 ;; It's a directory, unmark child files.
655 (while (setq crt (ewoc-next vc-ewoc crt))
656 (let ((crt-data (ewoc-data crt)))
657 (unless (vc-dir-fileinfo->directory crt-data)
658 (setf (vc-dir-fileinfo->marked crt-data) nil)
659 (ewoc-invalidate vc-ewoc crt))))
661 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
664 (when (and (vc-dir-fileinfo->marked filearg)
665 (eq (vc-dir-fileinfo->state filearg) crt-state))
666 (setf (vc-dir-fileinfo->marked filearg) nil)
670 (defun vc-dir-toggle-mark-file ()
671 (let* ((crt (ewoc-locate vc-ewoc))
672 (file (ewoc-data crt)))
673 (if (vc-dir-fileinfo->marked file)
675 (vc-dir-mark-file))))
677 (defun vc-dir-toggle-mark (e)
679 (vc-dir-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
681 (defun vc-dir-delete-file ()
682 "Delete the marked files, or the current file if no marks."
684 (mapc 'vc-delete-file (or (vc-dir-marked-files)
685 (list (vc-dir-current-file)))))
687 (defun vc-dir-find-file ()
688 "Find the file on the current line."
690 (find-file (vc-dir-current-file)))
692 (defun vc-dir-find-file-other-window ()
693 "Find the file on the current line, in another window."
695 (find-file-other-window (vc-dir-current-file)))
697 (defun vc-dir-isearch ()
698 "Search for a string through all marked buffers using Isearch."
701 (mapcar 'car (vc-dir-marked-only-files-and-states))))
703 (defun vc-dir-isearch-regexp ()
704 "Search for a regexp through all marked buffers using Isearch."
706 (multi-isearch-files-regexp
707 (mapcar 'car (vc-dir-marked-only-files-and-states))))
709 (defun vc-dir-search (regexp)
710 "Search through all marked files for a match for REGEXP.
711 For marked directories, use the files displayed from those directories.
712 Stops when a match is found.
713 To continue searching for next match, use command \\[tags-loop-continue]."
714 (interactive "sSearch marked files (regexp): ")
715 (tags-search regexp '(mapcar 'car (vc-dir-marked-only-files-and-states))))
717 (defun vc-dir-query-replace-regexp (from to &optional delimited)
718 "Do `query-replace-regexp' of FROM with TO, on all marked files.
719 For marked directories, use the files displayed from those directories.
720 If a directory is marked, then use the files displayed for that directory.
721 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
722 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
723 with the command \\[tags-loop-continue]."
724 ;; FIXME: this is almost a copy of `dired-do-replace-regexp'. This
725 ;; should probably be made generic and used in both places instead of
726 ;; duplicating it here.
729 (query-replace-read-args
730 "Query replace regexp in marked files" t t)))
731 (list (nth 0 common) (nth 1 common) (nth 2 common))))
732 (dolist (file (mapcar 'car (vc-dir-marked-only-files-and-states)))
733 (let ((buffer (get-file-buffer file)))
734 (if (and buffer (with-current-buffer buffer
736 (error "File `%s' is visited read-only" file))))
737 (tags-query-replace from to delimited
738 '(mapcar 'car (vc-dir-marked-only-files-and-states))))
740 (defun vc-dir-current-file ()
741 (let ((node (ewoc-locate vc-ewoc)))
743 (error "No file available"))
744 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
746 (defun vc-dir-marked-files ()
747 "Return the list of marked files."
749 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
750 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
752 (defun vc-dir-marked-only-files-and-states ()
753 "Return the list of conses (FILE . STATE) for the marked files.
754 For marked directories return the corresponding conses for the
756 (let ((crt (ewoc-nth vc-ewoc 0))
759 (let ((crt-data (ewoc-data crt)))
760 (if (vc-dir-fileinfo->marked crt-data)
761 ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
762 (if (vc-dir-fileinfo->directory crt-data)
763 (let* ((dir (vc-dir-fileinfo->directory crt-data))
764 (dirlen (length dir))
767 (and (setq crt (ewoc-next vc-ewoc crt))
768 (vc-string-prefix-p dir
770 (setq data (ewoc-data crt))
771 (vc-dir-node-directory crt))))
772 (unless (vc-dir-fileinfo->directory data)
774 (cons (expand-file-name (vc-dir-fileinfo->name data))
775 (vc-dir-fileinfo->state data))
777 (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
778 (vc-dir-fileinfo->state crt-data))
780 (setq crt (ewoc-next vc-ewoc crt)))
781 (setq crt (ewoc-next vc-ewoc crt)))))
784 (defun vc-dir-child-files-and-states ()
785 "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
786 If it is a file, return the corresponding cons for the file itself."
787 (let* ((crt (ewoc-locate vc-ewoc))
788 (crt-data (ewoc-data crt))
790 (if (vc-dir-fileinfo->directory crt-data)
791 (let* ((dir (vc-dir-fileinfo->directory crt-data))
792 (dirlen (length dir))
795 (and (setq crt (ewoc-next vc-ewoc crt))
796 (vc-string-prefix-p dir (progn
797 (setq data (ewoc-data crt))
798 (vc-dir-node-directory crt))))
799 (unless (vc-dir-fileinfo->directory data)
801 (cons (expand-file-name (vc-dir-fileinfo->name data))
802 (vc-dir-fileinfo->state data))
805 (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
806 (vc-dir-fileinfo->state crt-data)) result))
809 (defun vc-dir-recompute-file-state (fname def-dir)
810 (let* ((file-short (file-relative-name fname def-dir))
811 (remove-me-when-CVS-works
812 (when (eq vc-dir-backend 'CVS)
813 ;; FIXME: Warning: UGLY HACK. The CVS backend caches the state
814 ;; info, this forces the backend to update it.
815 (vc-call-backend vc-dir-backend 'registered fname)))
816 (state (vc-call-backend vc-dir-backend 'state fname))
817 (extra (vc-call-backend vc-dir-backend
818 'status-fileinfo-extra fname)))
819 (list file-short state extra)))
821 (defun vc-dir-find-child-files (dirname)
822 ;; Give a DIRNAME string return the list of all child files shown in
823 ;; the current *vc-dir* buffer.
824 (let ((crt (ewoc-nth vc-ewoc 0))
828 (while (and crt (not (vc-string-prefix-p
829 dirname (vc-dir-node-directory crt))))
830 (setq crt (ewoc-next vc-ewoc crt)))
831 (while (and crt (vc-string-prefix-p
833 (setq dname (vc-dir-node-directory crt))))
834 (let ((data (ewoc-data crt)))
835 (unless (vc-dir-fileinfo->directory data)
836 (push (expand-file-name (vc-dir-fileinfo->name data)) children)))
837 (setq crt (ewoc-next vc-ewoc crt)))
840 (defun vc-dir-resync-directory-files (dirname)
841 ;; Update the entries for all the child files of DIRNAME shown in
842 ;; the current *vc-dir* buffer.
843 (let ((files (vc-dir-find-child-files dirname))
844 (ddir (expand-file-name default-directory))
848 (push (vc-dir-recompute-file-state crt ddir)
850 (vc-dir-update fileentries (current-buffer)))))
852 (defun vc-dir-resynch-file (&optional fname)
853 "Update the entries for FNAME in any directory buffers that list it."
854 (let ((file (or fname (expand-file-name buffer-file-name)))
855 (found-vc-dir-buf nil))
857 (dolist (status-buf (buffer-list))
858 (set-buffer status-buf)
859 ;; look for a vc-dir buffer that might show this file.
860 (when (derived-mode-p 'vc-dir-mode)
861 (setq found-vc-dir-buf t)
862 (let ((ddir (expand-file-name default-directory)))
863 (when (vc-string-prefix-p ddir file)
864 (if (file-directory-p file)
865 (vc-dir-resync-directory-files file)
867 (list (vc-dir-recompute-file-state file ddir))
869 ;; We didn't find any vc-dir buffers, remove the hook, it is
871 (unless found-vc-dir-buf
872 (remove-hook 'after-save-hook 'vc-dir-resynch-file))))
874 (defvar use-vc-backend) ;; dynamically bound
876 (define-derived-mode vc-dir-mode special-mode "VC dir"
877 "Major mode for VC directory buffers.
878 Marking/Unmarking key bindings and actions:
879 m - mark a file/directory
880 - if the region is active, mark all the files in region.
881 Restrictions: - a file cannot be marked if any parent directory is marked
882 - a directory cannot be marked if any child file or
884 u - unmark a file/directory
885 - if the region is active, unmark all the files in region.
886 M - if the cursor is on a file: mark all the files with the same state as
888 - if the cursor is on a directory: mark all child files
889 - with a prefix argument: mark all files
890 U - if the cursor is on a file: unmark all the files with the same state
892 - if the cursor is on a directory: unmark all child files
893 - with a prefix argument: unmark all files
894 mouse-2 - toggles the mark state
897 VC commands in the `C-x v' prefix can be used.
898 VC commands act on the marked entries. If nothing is marked, VC
899 commands act on the current entry.
902 S - searches the marked files
903 Q - does a query replace on the marked files
904 M-s a C-s - does an isearch on the marked files
905 M-s a C-M-s - does a regexp isearch on the marked files
906 If nothing is marked, these commands act on the current entry.
907 When a directory is current or marked, the Search & Replace
908 commands act on the child files of that directory that are displayed in
912 (set (make-local-variable 'vc-dir-backend) use-vc-backend)
913 (setq buffer-read-only t)
914 (when (boundp 'tool-bar-map)
915 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
916 (let ((buffer-read-only nil))
918 (set (make-local-variable 'vc-dir-process-buffer) nil)
919 (set (make-local-variable 'vc-ewoc) (ewoc-create #'vc-dir-printer))
920 (set (make-local-variable 'revert-buffer-function)
921 'vc-dir-revert-buffer-function)
922 (set (make-local-variable 'list-buffers-directory)
923 (expand-file-name default-directory))
924 (add-hook 'after-save-hook 'vc-dir-resynch-file)
925 ;; Make sure that if the directory buffer is killed, the update
926 ;; process running in the background is also killed.
927 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
930 (defun vc-dir-headers (backend dir)
931 "Display the headers in the *VC dir* buffer.
932 It calls the `dir-extra-headers' backend method to display backend
935 ;; First layout the common headers.
936 (propertize "VC backend : " 'face 'font-lock-type-face)
937 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
938 (propertize "Working dir: " 'face 'font-lock-type-face)
939 (propertize (format "%s\n" dir) 'face 'font-lock-variable-name-face)
940 ;; Then the backend specific ones.
941 (vc-call-backend backend 'dir-extra-headers dir)
944 (defun vc-dir-refresh-files (files default-state)
945 "Refresh some files in the *VC-dir* buffer."
946 (let ((def-dir default-directory)
947 (backend vc-dir-backend))
948 (vc-set-mode-line-busy-indicator)
949 ;; Call the `dir-status-file' backend function.
950 ;; `dir-status-file' is supposed to be asynchronous.
951 ;; It should compute the results, and then call the function
952 ;; passed as an argument in order to update the vc-dir buffer
954 (unless (buffer-live-p vc-dir-process-buffer)
955 (setq vc-dir-process-buffer
956 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
957 (lexical-let ((buffer (current-buffer)))
958 (with-current-buffer vc-dir-process-buffer
962 backend 'dir-status-files def-dir files default-state
963 (lambda (entries &optional more-to-come)
964 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
965 ;; If MORE-TO-COME is true, then more updates will come from
966 ;; the asynchronous process.
967 (with-current-buffer buffer
968 (vc-dir-update entries buffer)
970 (setq mode-line-process nil)
971 ;; Remove the ones that haven't been updated at all.
972 ;; Those not-updated are those whose state is nil because the
973 ;; file/dir doesn't exist and isn't versioned.
976 ;; The state for directory entries might
977 ;; have been changed to 'up-to-date,
978 ;; reset it, othewise it will be removed when doing 'x'
980 ;; FIXME: There should be a more elegant way to do this.
981 (when (and (vc-dir-fileinfo->directory info)
982 (eq (vc-dir-fileinfo->state info)
984 (setf (vc-dir-fileinfo->state info) nil))
986 (not (vc-dir-fileinfo->needs-update info))))))))))))
988 (defun vc-dir-revert-buffer-function (&optional ignore-auto noconfirm)
991 (defun vc-dir-refresh ()
992 "Refresh the contents of the *VC-dir* buffer.
993 Throw an error if another update process is in progress."
996 (error "Another update process is in progress, cannot run two at a time")
997 (let ((def-dir default-directory)
998 (backend vc-dir-backend))
999 (vc-set-mode-line-busy-indicator)
1000 ;; Call the `dir-status' backend function.
1001 ;; `dir-status' is supposed to be asynchronous.
1002 ;; It should compute the results, and then call the function
1003 ;; passed as an argument in order to update the vc-dir buffer
1004 ;; with the results.
1006 ;; Create a buffer that can be used by `dir-status' and call
1007 ;; `dir-status' with this buffer as the current buffer. Use
1008 ;; `vc-dir-process-buffer' to remember this buffer, so that
1009 ;; it can be used later to kill the update process in case it
1011 (unless (buffer-live-p vc-dir-process-buffer)
1012 (setq vc-dir-process-buffer
1013 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
1014 ;; set the needs-update flag on all non-directory entries
1015 (ewoc-map (lambda (info)
1016 (unless (vc-dir-fileinfo->directory info)
1017 (setf (vc-dir-fileinfo->needs-update info) t) nil))
1019 (lexical-let ((buffer (current-buffer)))
1020 (with-current-buffer vc-dir-process-buffer
1024 backend 'dir-status def-dir
1025 (lambda (entries &optional more-to-come)
1026 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1027 ;; If MORE-TO-COME is true, then more updates will come from
1028 ;; the asynchronous process.
1029 (with-current-buffer buffer
1030 (vc-dir-update entries buffer)
1031 (unless more-to-come
1034 vc-ewoc 'vc-dir-fileinfo->needs-update)))
1036 (vc-dir-refresh-files
1037 (mapcar 'vc-dir-fileinfo->name remaining)
1039 (setq mode-line-process nil)))))))))
1040 (ewoc-set-hf vc-ewoc (vc-dir-headers backend def-dir) ""))))
1042 (defun vc-dir-show-fileentry (file)
1043 "Insert an entry for a specific file into the current *VC-dir* listing.
1044 This is typically used if the file is up-to-date (or has been added
1045 outside of VC) and one wants to do some operation on it."
1046 (interactive "fShow file: ")
1047 (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
1049 (defun vc-dir-hide-up-to-date ()
1050 "Hide up-to-date items from display."
1052 (let ((crt (ewoc-nth vc-ewoc -1))
1053 (first (ewoc-nth vc-ewoc 0)))
1054 ;; Go over from the last item to the first and remove the
1055 ;; up-to-date files and directories with no child files.
1056 (while (not (eq crt first))
1057 (let* ((data (ewoc-data crt))
1058 (dir (vc-dir-fileinfo->directory data))
1059 (next (ewoc-next vc-ewoc crt))
1060 (prev (ewoc-prev vc-ewoc crt))
1061 ;; ewoc-delete does not work without this...
1062 (inhibit-read-only t))
1064 ;; Remove directories with no child files.
1067 ;; Nothing follows this directory.
1069 ;; Next item is a directory.
1070 (vc-dir-fileinfo->directory (ewoc-data next))))
1071 ;; Remove files in the up-to-date state.
1072 (eq (vc-dir-fileinfo->state data) 'up-to-date))
1073 (ewoc-delete vc-ewoc crt))
1076 (defun vc-dir-printer (fileentry)
1077 (vc-call-backend vc-dir-backend 'dir-printer fileentry))
1079 (defun vc-dir-deduce-fileset (&optional state-model-only-files)
1080 (let ((marked (vc-dir-marked-files))
1088 (when state-model-only-files
1089 (setq only-files-list (vc-dir-marked-only-files-and-states))))
1090 (let ((crt (vc-dir-current-file)))
1091 (setq files (list crt))
1092 (when state-model-only-files
1093 (setq only-files-list (vc-dir-child-files-and-states)))))
1095 (when state-model-only-files
1096 (setq state (cdar only-files-list))
1097 ;; Check that all files are in a consistent state, since we use that
1098 ;; state to decide which operation to perform.
1099 (dolist (crt (cdr only-files-list))
1100 (unless (vc-compatible-state (cdr crt) state)
1101 (error "%s:%s clashes with %s:%s"
1102 (car crt) (cdr crt) (caar only-files-list) state)))
1103 (setq only-files-list (mapcar 'car only-files-list))
1104 (when (and state (not (eq state 'unregistered)))
1105 (setq model (vc-checkout-model vc-dir-backend only-files-list))))
1106 (list vc-dir-backend files only-files-list state model)))
1109 (defun vc-dir (dir &optional backend)
1110 "Show the VC status for DIR.
1111 Optional second argument BACKEND specifies the VC backend to use.
1112 Interactively, a prefix argument means to ask for the backend."
1115 ;; When you hit C-x v d in a visited VC file,
1116 ;; the *vc-dir* buffer visits the directory under its truename;
1117 ;; therefore it makes sense to always do that.
1118 ;; Otherwise if you do C-x v d -> C-x C-f -> C-c v d
1119 ;; you may get a new *vc-dir* buffer, different from the original
1120 (file-truename (read-file-name "VC status for directory: "
1121 default-directory default-directory t
1122 nil #'file-directory-p))
1123 (if current-prefix-arg
1127 (mapcar (lambda (b) (list (symbol-name b)))
1128 vc-handled-backends)
1131 (setq backend (vc-responsible-backend dir)))
1132 (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir backend))
1133 (if (derived-mode-p 'vc-dir-mode)
1135 ;; FIXME: find a better way to pass the backend to `vc-dir-mode'.
1136 (let ((use-vc-backend backend))
1139 (defun vc-default-dir-extra-headers (backend dir)
1140 ;; Be loud by default to remind people to add code to display
1141 ;; backend specific headers.
1142 ;; XXX: change this to return nil before the release.
1144 (propertize "Extra : " 'face 'font-lock-type-face)
1145 (propertize "Please add backend specific headers here. It's easy!"
1146 'face 'font-lock-warning-face)))
1148 (defun vc-default-dir-printer (backend fileentry)
1149 "Pretty print FILEENTRY."
1150 ;; If you change the layout here, change vc-dir-move-to-goal-column.
1151 (let* ((isdir (vc-dir-fileinfo->directory fileentry))
1152 (state (if isdir "" (vc-dir-fileinfo->state fileentry)))
1153 (filename (vc-dir-fileinfo->name fileentry)))
1156 (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1157 'face 'font-lock-type-face)
1160 (format "%-20s" state)
1161 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1162 ((memq state '(missing conflict)) 'font-lock-warning-face)
1163 (t 'font-lock-variable-name-face))
1164 'mouse-face 'highlight)
1167 (format "%s" filename)
1169 (if isdir 'font-lock-comment-delimiter-face 'font-lock-function-name-face)
1172 "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
1173 "File\nmouse-3: Pop-up menu")
1174 'mouse-face 'highlight))))
1176 (defun vc-default-extra-status-menu (backend)
1179 (defun vc-default-status-fileinfo-extra (backend file)
1180 "Default absence of extra information returned for a file."
1185 ;; arch-tag: 0274a2e3-e8e9-4b1a-a73c-e8b9129d5d15
1186 ;;; vc-dir.el ends here