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