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