]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-dir.el
merge from trunk
[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 (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 ;; More confusing than helpful, probably
249 ;;(define-key map "R" 'vc-revert) ;; u is taken by vc-dir-unmark.
250 ;;(define-key map "A" 'vc-annotate) ;; g is taken by revert-buffer
251 ;; bound by `special-mode'.
252 ;; Marking.
253 (define-key map "m" 'vc-dir-mark)
254 (define-key map "M" 'vc-dir-mark-all-files)
255 (define-key map "u" 'vc-dir-unmark)
256 (define-key map "U" 'vc-dir-unmark-all-files)
257 (define-key map "\C-?" 'vc-dir-unmark-file-up)
258 (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
259 ;; Movement.
260 (define-key map "n" 'vc-dir-next-line)
261 (define-key map " " 'vc-dir-next-line)
262 (define-key map "\t" 'vc-dir-next-directory)
263 (define-key map "p" 'vc-dir-previous-line)
264 (define-key map [backtab] 'vc-dir-previous-directory)
265 ;;; Rebind paragraph-movement commands.
266 (define-key map "\M-}" 'vc-dir-next-directory)
267 (define-key map "\M-{" 'vc-dir-previous-directory)
268 (define-key map [C-down] 'vc-dir-next-directory)
269 (define-key map [C-up] 'vc-dir-previous-directory)
270 ;; The remainder.
271 (define-key map "f" 'vc-dir-find-file)
272 (define-key map "e" 'vc-dir-find-file) ; dired-mode compatibility
273 (define-key map "\C-m" 'vc-dir-find-file)
274 (define-key map "o" 'vc-dir-find-file-other-window)
275 (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
276 (define-key map [down-mouse-3] 'vc-dir-menu)
277 (define-key map [mouse-2] 'vc-dir-toggle-mark)
278 (define-key map [follow-link] 'mouse-face)
279 (define-key map "x" 'vc-dir-hide-up-to-date)
280 (define-key map [?\C-k] 'vc-dir-kill-line)
281 (define-key map "S" 'vc-dir-search) ;; FIXME: Maybe use A like dired?
282 (define-key map "Q" 'vc-dir-query-replace-regexp)
283 (define-key map (kbd "M-s a C-s") 'vc-dir-isearch)
284 (define-key map (kbd "M-s a M-C-s") 'vc-dir-isearch-regexp)
285 (define-key map "G" 'vc-dir-ignore)
286
287 ;; Hook up the menu.
288 (define-key map [menu-bar vc-dir-mode]
289 `(menu-item
290 ;; VC backends can use this to add mode-specific menu items to
291 ;; vc-dir-menu-map.
292 "VC-dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
293 map)
294 "Keymap for directory buffer.")
295
296 (defmacro vc-dir-at-event (event &rest body)
297 "Evaluate BODY with point located at event-start of EVENT.
298 If BODY uses EVENT, it should be a variable,
299 otherwise it will be evaluated twice."
300 (let ((posn (make-symbol "vc-dir-at-event-posn")))
301 `(save-excursion
302 (unless (equal ,event '(tool-bar))
303 (let ((,posn (event-start ,event)))
304 (set-buffer (window-buffer (posn-window ,posn)))
305 (goto-char (posn-point ,posn))))
306 ,@body)))
307
308 (defun vc-dir-menu (e)
309 "Popup the VC dir menu."
310 (interactive "e")
311 (vc-dir-at-event e (popup-menu vc-dir-menu-map e)))
312
313 (defvar vc-dir-tool-bar-map
314 (let ((map (make-sparse-keymap)))
315 (tool-bar-local-item-from-menu 'find-file "new" map nil
316 :label "New File" :vert-only t)
317 (tool-bar-local-item-from-menu 'menu-find-file-existing "open" map nil
318 :label "Open" :vert-only t)
319 (tool-bar-local-item-from-menu 'dired "diropen" map nil
320 :vert-only t)
321 (tool-bar-local-item-from-menu 'quit-window "close" map vc-dir-mode-map
322 :vert-only t)
323 (tool-bar-local-item-from-menu 'vc-next-action "saveas" map
324 vc-dir-mode-map :label "Commit")
325 (tool-bar-local-item-from-menu 'vc-print-log "info"
326 map vc-dir-mode-map
327 :label "Log")
328 (define-key-after map [separator-1] menu-bar-separator)
329 (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
330 map vc-dir-mode-map
331 :label "Stop" :vert-only t)
332 (tool-bar-local-item-from-menu 'revert-buffer "refresh"
333 map vc-dir-mode-map :vert-only t)
334 (define-key-after map [separator-2] menu-bar-separator)
335 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [cut])
336 "cut" map nil :vert-only t)
337 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [copy])
338 "copy" map nil :vert-only t)
339 (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [paste])
340 "paste" map nil :vert-only t)
341 (define-key-after map [separator-3] menu-bar-separator)
342 (tool-bar-local-item-from-menu 'isearch-forward
343 "search" map nil
344 :label "Search" :vert-only t)
345 map))
346
347 (defun vc-dir-node-directory (node)
348 ;; Compute the directory for NODE.
349 ;; If it's a directory node, get it from the node.
350 (let ((data (ewoc-data node)))
351 (or (vc-dir-fileinfo->directory data)
352 ;; Otherwise compute it from the file name.
353 (file-name-directory
354 (directory-file-name
355 (expand-file-name
356 (vc-dir-fileinfo->name data)))))))
357
358 (defun vc-dir-update (entries buffer &optional noinsert)
359 "Update BUFFER's ewoc from the list of ENTRIES.
360 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
361 ;; Add ENTRIES to the vc-dir buffer BUFFER.
362 (with-current-buffer buffer
363 ;; Insert the entries sorted by name into the ewoc.
364 ;; We assume the ewoc is sorted too, which should be the
365 ;; case if we always add entries with vc-dir-update.
366 (setq entries
367 ;; Sort: first files and then subdirectories.
368 ;; XXX: this is VERY inefficient, it computes the directory
369 ;; names too many times
370 (sort entries
371 (lambda (entry1 entry2)
372 (let ((dir1 (file-name-directory
373 (directory-file-name (expand-file-name (car entry1)))))
374 (dir2 (file-name-directory
375 (directory-file-name (expand-file-name (car entry2))))))
376 (cond
377 ((string< dir1 dir2) t)
378 ((not (string= dir1 dir2)) nil)
379 ((string< (car entry1) (car entry2))))))))
380 ;; Insert directory entries in the right places.
381 (let ((entry (car entries))
382 (node (ewoc-nth vc-ewoc 0))
383 (to-remove nil)
384 (dotname (file-relative-name default-directory)))
385 ;; Insert . if it is not present.
386 (unless node
387 (ewoc-enter-last
388 vc-ewoc (vc-dir-create-fileinfo
389 dotname nil nil nil default-directory))
390 (setq node (ewoc-nth vc-ewoc 0)))
391
392 (while (and entry node)
393 (let* ((entryfile (car entry))
394 (entrydir (file-name-directory (directory-file-name
395 (expand-file-name entryfile))))
396 (nodedir (vc-dir-node-directory node)))
397 (cond
398 ;; First try to find the directory.
399 ((string-lessp nodedir entrydir)
400 (setq node (ewoc-next vc-ewoc node)))
401 ((string-equal nodedir entrydir)
402 ;; Found the directory, find the place for the file name.
403 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
404 (cond
405 ((string= nodefile dotname)
406 (setq node (ewoc-next vc-ewoc node)))
407 ((string-lessp nodefile entryfile)
408 (setq node (ewoc-next vc-ewoc node)))
409 ((string-equal nodefile entryfile)
410 (if (nth 1 entry)
411 (progn
412 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
413 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
414 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
415 (ewoc-invalidate vc-ewoc node))
416 ;; If the state is nil, the file does not exist
417 ;; anymore, so remember the entry so we can remove
418 ;; it after we are done inserting all ENTRIES.
419 (push node to-remove))
420 (setq entries (cdr entries))
421 (setq entry (car entries))
422 (setq node (ewoc-next vc-ewoc node)))
423 (t
424 (unless noinsert
425 (ewoc-enter-before vc-ewoc node
426 (apply 'vc-dir-create-fileinfo entry)))
427 (setq entries (cdr entries))
428 (setq entry (car entries))))))
429 (t
430 (unless noinsert
431 ;; We might need to insert a directory node if the
432 ;; previous node was in a different directory.
433 (let* ((rd (file-relative-name entrydir))
434 (prev-node (ewoc-prev vc-ewoc node))
435 (prev-dir (vc-dir-node-directory prev-node)))
436 (unless (string-equal entrydir prev-dir)
437 (ewoc-enter-before
438 vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
439 ;; Now insert the node itself.
440 (ewoc-enter-before vc-ewoc node
441 (apply 'vc-dir-create-fileinfo entry)))
442 (setq entries (cdr entries) entry (car entries))))))
443 ;; We're past the last node, all remaining entries go to the end.
444 (unless (or node noinsert)
445 (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
446 (dolist (entry entries)
447 (let ((entrydir (file-name-directory
448 (directory-file-name (expand-file-name (car entry))))))
449 ;; Insert a directory node if needed.
450 (unless (string-equal lastdir entrydir)
451 (setq lastdir entrydir)
452 (let ((rd (file-relative-name entrydir)))
453 (ewoc-enter-last
454 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
455 ;; Now insert the node itself.
456 (ewoc-enter-last vc-ewoc
457 (apply 'vc-dir-create-fileinfo entry))))))
458 (when to-remove
459 (let ((inhibit-read-only t))
460 (apply 'ewoc-delete vc-ewoc (nreverse to-remove)))))))
461
462 (defun vc-dir-busy ()
463 (and (buffer-live-p vc-dir-process-buffer)
464 (get-buffer-process vc-dir-process-buffer)))
465
466 (defun vc-dir-kill-dir-status-process ()
467 "Kill the temporary buffer and associated process."
468 (interactive)
469 (when (buffer-live-p vc-dir-process-buffer)
470 (let ((proc (get-buffer-process vc-dir-process-buffer)))
471 (when proc (delete-process proc))
472 (setq vc-dir-process-buffer nil)
473 (setq mode-line-process nil))))
474
475 (defun vc-dir-kill-query ()
476 ;; Make sure that when the status buffer is killed the update
477 ;; process running in background is also killed.
478 (if (vc-dir-busy)
479 (when (y-or-n-p "Status update process running, really kill status buffer? ")
480 (vc-dir-kill-dir-status-process)
481 t)
482 t))
483
484 (defun vc-dir-next-line (arg)
485 "Go to the next line.
486 If a prefix argument is given, move by that many lines."
487 (interactive "p")
488 (with-no-warnings
489 (ewoc-goto-next vc-ewoc arg)
490 (vc-dir-move-to-goal-column)))
491
492 (defun vc-dir-previous-line (arg)
493 "Go to the previous line.
494 If a prefix argument is given, move by that many lines."
495 (interactive "p")
496 (ewoc-goto-prev vc-ewoc arg)
497 (vc-dir-move-to-goal-column))
498
499 (defun vc-dir-next-directory ()
500 "Go to the next directory."
501 (interactive)
502 (let ((orig (point)))
503 (if
504 (catch 'foundit
505 (while t
506 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
507 (cond ((not next)
508 (throw 'foundit t))
509 (t
510 (progn
511 (ewoc-goto-node vc-ewoc next)
512 (vc-dir-move-to-goal-column)
513 (if (vc-dir-fileinfo->directory (ewoc-data next))
514 (throw 'foundit nil))))))))
515 (goto-char orig))))
516
517 (defun vc-dir-previous-directory ()
518 "Go to the previous directory."
519 (interactive)
520 (let ((orig (point)))
521 (if
522 (catch 'foundit
523 (while t
524 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
525 (cond ((not prev)
526 (throw 'foundit t))
527 (t
528 (progn
529 (ewoc-goto-node vc-ewoc prev)
530 (vc-dir-move-to-goal-column)
531 (if (vc-dir-fileinfo->directory (ewoc-data prev))
532 (throw 'foundit nil))))))))
533 (goto-char orig))))
534
535 (defun vc-dir-mark-unmark (mark-unmark-function)
536 (if (use-region-p)
537 (let (;; (firstl (line-number-at-pos (region-beginning)))
538 (lastl (line-number-at-pos (region-end))))
539 (save-excursion
540 (goto-char (region-beginning))
541 (while (<= (line-number-at-pos) lastl)
542 (condition-case nil
543 (funcall mark-unmark-function)
544 ;; `vc-dir-mark-file' signals an error if we try marking
545 ;; a directory containing marked files in its tree, or a
546 ;; file in a marked directory tree. Just continue.
547 (error (vc-dir-next-line 1))))))
548 (funcall mark-unmark-function)))
549
550 (defun vc-dir-parent-marked-p (arg)
551 ;; Non-nil iff a parent directory of arg is marked.
552 ;; Return value, if non-nil is the `ewoc-data' for the marked parent.
553 (let* ((argdir (vc-dir-node-directory arg))
554 ;; (arglen (length argdir))
555 (crt arg)
556 (found nil))
557 ;; Go through the predecessors, checking if any directory that is
558 ;; a parent is marked.
559 (while (and (null found)
560 (setq crt (ewoc-prev vc-ewoc crt)))
561 (let ((data (ewoc-data crt))
562 (dir (vc-dir-node-directory crt)))
563 (and (vc-dir-fileinfo->directory data)
564 (string-prefix-p dir argdir)
565 (vc-dir-fileinfo->marked data)
566 (setq found data))))
567 found))
568
569 (defun vc-dir-children-marked-p (arg)
570 ;; Non-nil iff a child of ARG is marked.
571 ;; Return value, if non-nil, is the `ewoc-data' for the marked child.
572 (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
573 (is-child t)
574 (crt arg)
575 (found nil))
576 (while (and is-child
577 (null found)
578 (setq crt (ewoc-next vc-ewoc crt)))
579 (let ((data (ewoc-data crt))
580 (dir (vc-dir-node-directory crt)))
581 (if (string-match argdir-re dir)
582 (if (vc-dir-fileinfo->marked data)
583 (setq found data))
584 ;; We are done, we got to an entry that is not a child of `arg'.
585 (setq is-child nil))))
586 found))
587
588 (defun vc-dir-mark-file (&optional arg)
589 ;; Mark ARG or the current file and move to the next line.
590 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
591 (file (ewoc-data crt))
592 (isdir (vc-dir-fileinfo->directory file))
593 ;; Forbid marking a directory containing marked files in its
594 ;; tree, or a file in a marked directory tree.
595 (conflict (if isdir
596 (vc-dir-children-marked-p crt)
597 (vc-dir-parent-marked-p crt))))
598 (when conflict
599 (error (if isdir
600 "File `%s' in this directory is already marked"
601 "Parent directory `%s' is already marked")
602 (vc-dir-fileinfo->name conflict)))
603 (setf (vc-dir-fileinfo->marked file) t)
604 (ewoc-invalidate vc-ewoc crt)
605 (unless (or arg (mouse-event-p last-command-event))
606 (vc-dir-next-line 1))))
607
608 (defun vc-dir-mark ()
609 "Mark the current file or all files in the region.
610 If the region is active, mark all the files in the region.
611 Otherwise mark the file on the current line and move to the next
612 line."
613 (interactive)
614 (vc-dir-mark-unmark 'vc-dir-mark-file))
615
616 (defun vc-dir-mark-all-files (arg)
617 "Mark all files with the same state as the current one.
618 With a prefix argument mark all files.
619 If the current entry is a directory, mark all child files.
620
621 The commands operate on files that are on the same state.
622 This command is intended to make it easy to select all files that
623 share the same state."
624 (interactive "P")
625 (if arg
626 ;; Mark all files.
627 (progn
628 ;; First check that no directory is marked, we can't mark
629 ;; files in that case.
630 (ewoc-map
631 (lambda (filearg)
632 (when (and (vc-dir-fileinfo->directory filearg)
633 (vc-dir-fileinfo->marked filearg))
634 (error "Cannot mark all files, directory `%s' marked"
635 (vc-dir-fileinfo->name filearg))))
636 vc-ewoc)
637 (ewoc-map
638 (lambda (filearg)
639 (unless (vc-dir-fileinfo->marked filearg)
640 (setf (vc-dir-fileinfo->marked filearg) t)
641 t))
642 vc-ewoc))
643 (let* ((crt (ewoc-locate vc-ewoc))
644 (data (ewoc-data crt)))
645 (if (vc-dir-fileinfo->directory data)
646 ;; It's a directory, mark child files.
647 (let (crt-data)
648 (while (and (setq crt (ewoc-next vc-ewoc crt))
649 (setq crt-data (ewoc-data crt))
650 (not (vc-dir-fileinfo->directory crt-data)))
651 (setf (vc-dir-fileinfo->marked crt-data) t)
652 (ewoc-invalidate vc-ewoc crt)))
653 ;; It's a file
654 (let ((state (vc-dir-fileinfo->state data)))
655 (setq crt (ewoc-nth vc-ewoc 0))
656 (while crt
657 (let ((crt-data (ewoc-data crt)))
658 (when (and (not (vc-dir-fileinfo->marked crt-data))
659 (eq (vc-dir-fileinfo->state crt-data) state)
660 (not (vc-dir-fileinfo->directory crt-data)))
661 (vc-dir-mark-file crt)))
662 (setq crt (ewoc-next vc-ewoc crt))))))))
663
664 (defun vc-dir-unmark-file ()
665 ;; Unmark the current file and move to the next line.
666 (let* ((crt (ewoc-locate vc-ewoc))
667 (file (ewoc-data crt)))
668 (setf (vc-dir-fileinfo->marked file) nil)
669 (ewoc-invalidate vc-ewoc crt)
670 (unless (mouse-event-p last-command-event)
671 (vc-dir-next-line 1))))
672
673 (defun vc-dir-unmark ()
674 "Unmark the current file or all files in the region.
675 If the region is active, unmark all the files in the region.
676 Otherwise mark the file on the current line and move to the next
677 line."
678 (interactive)
679 (vc-dir-mark-unmark 'vc-dir-unmark-file))
680
681 (defun vc-dir-unmark-file-up ()
682 "Move to the previous line and unmark the file."
683 (interactive)
684 ;; If we're on the first line, we won't move up, but we will still
685 ;; remove the mark. This seems a bit odd but it is what buffer-menu
686 ;; does.
687 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
688 (file (ewoc-data prev)))
689 (setf (vc-dir-fileinfo->marked file) nil)
690 (ewoc-invalidate vc-ewoc prev)
691 (vc-dir-move-to-goal-column)))
692
693 (defun vc-dir-unmark-all-files (arg)
694 "Unmark all files with the same state as the current one.
695 With a prefix argument unmark all files.
696 If the current entry is a directory, unmark all the child files.
697
698 The commands operate on files that are on the same state.
699 This command is intended to make it easy to deselect all files
700 that share the same state."
701 (interactive "P")
702 (if arg
703 (ewoc-map
704 (lambda (filearg)
705 (when (vc-dir-fileinfo->marked filearg)
706 (setf (vc-dir-fileinfo->marked filearg) nil)
707 t))
708 vc-ewoc)
709 (let* ((crt (ewoc-locate vc-ewoc))
710 (data (ewoc-data crt)))
711 (if (vc-dir-fileinfo->directory data)
712 ;; It's a directory, unmark child files.
713 (while (setq crt (ewoc-next vc-ewoc crt))
714 (let ((crt-data (ewoc-data crt)))
715 (unless (vc-dir-fileinfo->directory crt-data)
716 (setf (vc-dir-fileinfo->marked crt-data) nil)
717 (ewoc-invalidate vc-ewoc crt))))
718 ;; It's a file
719 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
720 (ewoc-map
721 (lambda (filearg)
722 (when (and (vc-dir-fileinfo->marked filearg)
723 (eq (vc-dir-fileinfo->state filearg) crt-state))
724 (setf (vc-dir-fileinfo->marked filearg) nil)
725 t))
726 vc-ewoc))))))
727
728 (defun vc-dir-toggle-mark-file ()
729 (let* ((crt (ewoc-locate vc-ewoc))
730 (file (ewoc-data crt)))
731 (if (vc-dir-fileinfo->marked file)
732 (vc-dir-unmark-file)
733 (vc-dir-mark-file))))
734
735 (defun vc-dir-toggle-mark (e)
736 (interactive "e")
737 (vc-dir-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
738
739 (defun vc-dir-delete-file ()
740 "Delete the marked files, or the current file if no marks."
741 (interactive)
742 (mapc 'vc-delete-file (or (vc-dir-marked-files)
743 (list (vc-dir-current-file)))))
744
745 (defun vc-dir-find-file ()
746 "Find the file on the current line."
747 (interactive)
748 (find-file (vc-dir-current-file)))
749
750 (defun vc-dir-find-file-other-window (&optional event)
751 "Find the file on the current line, in another window."
752 (interactive (list last-nonmenu-event))
753 (if event (posn-set-point (event-end event)))
754 (find-file-other-window (vc-dir-current-file)))
755
756 (defun vc-dir-isearch ()
757 "Search for a string through all marked buffers using Isearch."
758 (interactive)
759 (multi-isearch-files
760 (mapcar 'car (vc-dir-marked-only-files-and-states))))
761
762 (defun vc-dir-isearch-regexp ()
763 "Search for a regexp through all marked buffers using Isearch."
764 (interactive)
765 (multi-isearch-files-regexp
766 (mapcar 'car (vc-dir-marked-only-files-and-states))))
767
768 (defun vc-dir-search (regexp)
769 "Search through all marked files for a match for REGEXP.
770 For marked directories, use the files displayed from those directories.
771 Stops when a match is found.
772 To continue searching for next match, use command \\[tags-loop-continue]."
773 (interactive "sSearch marked files (regexp): ")
774 (tags-search regexp '(mapcar 'car (vc-dir-marked-only-files-and-states))))
775
776 (defun vc-dir-query-replace-regexp (from to &optional delimited)
777 "Do `query-replace-regexp' of FROM with TO, on all marked files.
778 If a directory is marked, then use the files displayed for that directory.
779 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
780 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
781 with the command \\[tags-loop-continue]."
782 ;; FIXME: this is almost a copy of `dired-do-query-replace-regexp'. This
783 ;; should probably be made generic and used in both places instead of
784 ;; duplicating it here.
785 (interactive
786 (let ((common
787 (query-replace-read-args
788 "Query replace regexp in marked files" t t)))
789 (list (nth 0 common) (nth 1 common) (nth 2 common))))
790 (dolist (file (mapcar 'car (vc-dir-marked-only-files-and-states)))
791 (let ((buffer (get-file-buffer file)))
792 (if (and buffer (with-current-buffer buffer
793 buffer-read-only))
794 (error "File `%s' is visited read-only" file))))
795 (tags-query-replace from to delimited
796 '(mapcar 'car (vc-dir-marked-only-files-and-states))))
797
798 (defun vc-dir-ignore ()
799 "Ignore the current file."
800 (interactive)
801 (vc-ignore (vc-dir-current-file)))
802
803 (defun vc-dir-current-file ()
804 (let ((node (ewoc-locate vc-ewoc)))
805 (unless node
806 (error "No file available"))
807 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
808
809 (defun vc-dir-marked-files ()
810 "Return the list of marked files."
811 (mapcar
812 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
813 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
814
815 (defun vc-dir-marked-only-files-and-states ()
816 "Return the list of conses (FILE . STATE) for the marked files.
817 For marked directories return the corresponding conses for the
818 child files."
819 (let ((crt (ewoc-nth vc-ewoc 0))
820 result)
821 (while crt
822 (let ((crt-data (ewoc-data crt)))
823 (if (vc-dir-fileinfo->marked crt-data)
824 ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
825 (if (vc-dir-fileinfo->directory crt-data)
826 (let* ((dir (vc-dir-fileinfo->directory crt-data))
827 ;; (dirlen (length dir))
828 data)
829 (while
830 (and (setq crt (ewoc-next vc-ewoc crt))
831 (string-prefix-p dir
832 (progn
833 (setq data (ewoc-data crt))
834 (vc-dir-node-directory crt))))
835 (unless (vc-dir-fileinfo->directory data)
836 (push
837 (cons (expand-file-name (vc-dir-fileinfo->name data))
838 (vc-dir-fileinfo->state data))
839 result))))
840 (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
841 (vc-dir-fileinfo->state crt-data))
842 result)
843 (setq crt (ewoc-next vc-ewoc crt)))
844 (setq crt (ewoc-next vc-ewoc crt)))))
845 (nreverse result)))
846
847 (defun vc-dir-child-files-and-states ()
848 "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
849 If it is a file, return the corresponding cons for the file itself."
850 (let* ((crt (ewoc-locate vc-ewoc))
851 (crt-data (ewoc-data crt))
852 result)
853 (if (vc-dir-fileinfo->directory crt-data)
854 (let* ((dir (vc-dir-fileinfo->directory crt-data))
855 ;; (dirlen (length dir))
856 data)
857 (while
858 (and (setq crt (ewoc-next vc-ewoc crt))
859 (string-prefix-p dir (progn
860 (setq data (ewoc-data crt))
861 (vc-dir-node-directory crt))))
862 (unless (vc-dir-fileinfo->directory data)
863 (push
864 (cons (expand-file-name (vc-dir-fileinfo->name data))
865 (vc-dir-fileinfo->state data))
866 result))))
867 (push
868 (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
869 (vc-dir-fileinfo->state crt-data)) result))
870 (nreverse result)))
871
872 (defun vc-dir-recompute-file-state (fname def-dir)
873 (let* ((file-short (file-relative-name fname def-dir))
874 (_remove-me-when-CVS-works
875 (when (eq vc-dir-backend 'CVS)
876 ;; FIXME: Warning: UGLY HACK. The CVS backend caches the state
877 ;; info, this forces the backend to update it.
878 (vc-call-backend vc-dir-backend 'registered fname)))
879 (state (vc-call-backend vc-dir-backend 'state fname))
880 (extra (vc-call-backend vc-dir-backend
881 'status-fileinfo-extra fname)))
882 (list file-short state extra)))
883
884 (defun vc-dir-find-child-files (dirname)
885 ;; Give a DIRNAME string return the list of all child files shown in
886 ;; the current *vc-dir* buffer.
887 (let ((crt (ewoc-nth vc-ewoc 0))
888 children)
889 ;; Find DIR
890 (while (and crt (not (string-prefix-p
891 dirname (vc-dir-node-directory crt))))
892 (setq crt (ewoc-next vc-ewoc crt)))
893 (while (and crt (string-prefix-p
894 dirname
895 (vc-dir-node-directory crt)))
896 (let ((data (ewoc-data crt)))
897 (unless (vc-dir-fileinfo->directory data)
898 (push (expand-file-name (vc-dir-fileinfo->name data)) children)))
899 (setq crt (ewoc-next vc-ewoc crt)))
900 children))
901
902 (defun vc-dir-resync-directory-files (dirname)
903 ;; Update the entries for all the child files of DIRNAME shown in
904 ;; the current *vc-dir* buffer.
905 (let ((files (vc-dir-find-child-files dirname))
906 (ddir default-directory)
907 fileentries)
908 (when files
909 (dolist (crt files)
910 (push (vc-dir-recompute-file-state crt ddir)
911 fileentries))
912 (vc-dir-update fileentries (current-buffer)))))
913
914 (defun vc-dir-resynch-file (&optional fname)
915 "Update the entries for FNAME in any directory buffers that list it."
916 (let ((file (or fname (expand-file-name buffer-file-name)))
917 (drop '()))
918 (save-current-buffer
919 ;; look for a vc-dir buffer that might show this file.
920 (dolist (status-buf vc-dir-buffers)
921 (if (not (buffer-live-p status-buf))
922 (push status-buf drop)
923 (set-buffer status-buf)
924 (if (not (derived-mode-p 'vc-dir-mode))
925 (push status-buf drop)
926 (let ((ddir default-directory))
927 (when (string-prefix-p ddir file)
928 (if (file-directory-p file)
929 (progn
930 (vc-dir-resync-directory-files file)
931 (ewoc-set-hf vc-ewoc
932 (vc-dir-headers vc-dir-backend default-directory) ""))
933 (let* ((complete-state (vc-dir-recompute-file-state file ddir))
934 (state (cadr complete-state)))
935 (vc-dir-update
936 (list complete-state)
937 status-buf (or (not state)
938 (eq state 'up-to-date)))))))))))
939 ;; Remove out-of-date entries from vc-dir-buffers.
940 (dolist (b drop) (setq vc-dir-buffers (delq b vc-dir-buffers)))))
941
942 (defvar use-vc-backend) ;; dynamically bound
943
944 ;; Autoload cookie needed by desktop.el.
945 ;;;###autoload
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