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