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