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