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