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