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