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