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