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