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