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