Add 2010 to copyright years.
[bpt/emacs.git] / lisp / vc-dir.el
1 ;;; vc-dir.el --- Directory status display under VC
2
3 ;; Copyright (C) 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Dan Nicolaescu <dann@ics.uci.edu>
7 ;; Keywords: tools
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Credits:
25
26 ;; The original VC directory status implementation was based on dired.
27 ;; This implementation was inspired by PCL-CVS.
28 ;; Many people contributed comments, ideas and code to this
29 ;; implementation. These include:
30 ;;
31 ;; Alexandre Julliard <julliard@winehq.org>
32 ;; Stefan Monnier <monnier@iro.umontreal.ca>
33 ;; Tom Tromey <tromey@redhat.com>
34
35 ;;; Commentary:
36 ;;
37
38 ;;; Todo: see vc.el.
39
40 (require 'vc-hooks)
41 (require 'vc)
42 (require 'tool-bar)
43 (require 'ewoc)
44
45 ;;; Code:
46 (eval-when-compile
47 (require 'cl))
48
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)
54
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)
73
74 (defvar vc-ewoc nil)
75
76 (defvar vc-dir-process-buffer nil
77 "The buffer used for the asynchronous call that computes status.")
78
79 (defvar vc-dir-backend nil
80 "The backend used by the current *vc-dir* buffer.")
81
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)))
88
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)))))
113
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"))
147
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"))
155
156 (define-key map [mark]
157 '(menu-item "Mark" vc-dir-mark
158 :help "Mark the current file or all files in the region"))
159
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.")
212
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))))
226
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)
270
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.")
279
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)))
291
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)))
296
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))
323
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)))))))
334
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 (dotname (file-relative-name default-directory)))
361 ;; Insert . if it is not present.
362 (unless node
363 (ewoc-enter-last
364 vc-ewoc (vc-dir-create-fileinfo
365 dotname nil nil nil default-directory))
366 (setq node (ewoc-nth vc-ewoc 0)))
367
368 (while (and entry node)
369 (let* ((entryfile (car entry))
370 (entrydir (file-name-directory (directory-file-name
371 (expand-file-name entryfile))))
372 (nodedir (vc-dir-node-directory node)))
373 (cond
374 ;; First try to find the directory.
375 ((string-lessp nodedir entrydir)
376 (setq node (ewoc-next vc-ewoc node)))
377 ((string-equal nodedir entrydir)
378 ;; Found the directory, find the place for the file name.
379 (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
380 (cond
381 ((string= nodefile dotname)
382 (setq node (ewoc-next vc-ewoc node)))
383 ((string-lessp nodefile entryfile)
384 (setq node (ewoc-next vc-ewoc node)))
385 ((string-equal nodefile entryfile)
386 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
387 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
388 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
389 (ewoc-invalidate vc-ewoc node)
390 (setq entries (cdr entries))
391 (setq entry (car entries))
392 (setq node (ewoc-next vc-ewoc node)))
393 (t
394 (ewoc-enter-before vc-ewoc node
395 (apply 'vc-dir-create-fileinfo entry))
396 (setq entries (cdr entries))
397 (setq entry (car entries))))))
398 (t
399 ;; We might need to insert a directory node if the
400 ;; previous node was in a different directory.
401 (let* ((rd (file-relative-name entrydir))
402 (prev-node (ewoc-prev vc-ewoc node))
403 (prev-dir (vc-dir-node-directory prev-node)))
404 (unless (string-equal entrydir prev-dir)
405 (ewoc-enter-before
406 vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
407 ;; Now insert the node itself.
408 (ewoc-enter-before vc-ewoc node
409 (apply 'vc-dir-create-fileinfo entry))
410 (setq entries (cdr entries) entry (car entries))))))
411 ;; We're past the last node, all remaining entries go to the end.
412 (unless (or node noinsert)
413 (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
414 (dolist (entry entries)
415 (let ((entrydir (file-name-directory
416 (directory-file-name (expand-file-name (car entry))))))
417 ;; Insert a directory node if needed.
418 (unless (string-equal lastdir entrydir)
419 (setq lastdir entrydir)
420 (let ((rd (file-relative-name entrydir)))
421 (ewoc-enter-last
422 vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
423 ;; Now insert the node itself.
424 (ewoc-enter-last vc-ewoc
425 (apply 'vc-dir-create-fileinfo entry)))))))))
426
427 (defun vc-dir-busy ()
428 (and (buffer-live-p vc-dir-process-buffer)
429 (get-buffer-process vc-dir-process-buffer)))
430
431 (defun vc-dir-kill-dir-status-process ()
432 "Kill the temporary buffer and associated process."
433 (interactive)
434 (when (buffer-live-p vc-dir-process-buffer)
435 (let ((proc (get-buffer-process vc-dir-process-buffer)))
436 (when proc (delete-process proc))
437 (setq vc-dir-process-buffer nil)
438 (setq mode-line-process nil))))
439
440 (defun vc-dir-kill-query ()
441 ;; Make sure that when the status buffer is killed the update
442 ;; process running in background is also killed.
443 (if (vc-dir-busy)
444 (when (y-or-n-p "Status update process running, really kill status buffer? ")
445 (vc-dir-kill-dir-status-process)
446 t)
447 t))
448
449 (defun vc-dir-next-line (arg)
450 "Go to the next line.
451 If a prefix argument is given, move by that many lines."
452 (interactive "p")
453 (with-no-warnings
454 (ewoc-goto-next vc-ewoc arg)
455 (vc-dir-move-to-goal-column)))
456
457 (defun vc-dir-previous-line (arg)
458 "Go to the previous line.
459 If a prefix argument is given, move by that many lines."
460 (interactive "p")
461 (ewoc-goto-prev vc-ewoc arg)
462 (vc-dir-move-to-goal-column))
463
464 (defun vc-dir-next-directory ()
465 "Go to the next directory."
466 (interactive)
467 (let ((orig (point)))
468 (if
469 (catch 'foundit
470 (while t
471 (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
472 (cond ((not next)
473 (throw 'foundit t))
474 (t
475 (progn
476 (ewoc-goto-node vc-ewoc next)
477 (vc-dir-move-to-goal-column)
478 (if (vc-dir-fileinfo->directory (ewoc-data next))
479 (throw 'foundit nil))))))))
480 (goto-char orig))))
481
482 (defun vc-dir-previous-directory ()
483 "Go to the previous directory."
484 (interactive)
485 (let ((orig (point)))
486 (if
487 (catch 'foundit
488 (while t
489 (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
490 (cond ((not prev)
491 (throw 'foundit t))
492 (t
493 (progn
494 (ewoc-goto-node vc-ewoc prev)
495 (vc-dir-move-to-goal-column)
496 (if (vc-dir-fileinfo->directory (ewoc-data prev))
497 (throw 'foundit nil))))))))
498 (goto-char orig))))
499
500 (defun vc-dir-mark-unmark (mark-unmark-function)
501 (if (use-region-p)
502 (let ((firstl (line-number-at-pos (region-beginning)))
503 (lastl (line-number-at-pos (region-end))))
504 (save-excursion
505 (goto-char (region-beginning))
506 (while (<= (line-number-at-pos) lastl)
507 (funcall mark-unmark-function))))
508 (funcall mark-unmark-function)))
509
510 (defun vc-dir-parent-marked-p (arg)
511 ;; Return nil if none of the parent directories of arg is marked.
512 (let* ((argdir (vc-dir-node-directory arg))
513 (arglen (length argdir))
514 (crt arg)
515 data dir)
516 ;; Go through the predecessors, checking if any directory that is
517 ;; a parent is marked.
518 (while (setq crt (ewoc-prev vc-ewoc crt))
519 (setq data (ewoc-data crt))
520 (setq dir (vc-dir-node-directory crt))
521 (when (and (vc-dir-fileinfo->directory data)
522 (vc-string-prefix-p dir argdir))
523 (when (vc-dir-fileinfo->marked data)
524 (error "Cannot mark `%s', parent directory `%s' marked"
525 (vc-dir-fileinfo->name (ewoc-data arg))
526 (vc-dir-fileinfo->name data)))))
527 nil))
528
529 (defun vc-dir-children-marked-p (arg)
530 ;; Return nil if none of the children of arg is marked.
531 (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
532 (is-child t)
533 (crt arg)
534 data dir)
535 (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
536 (setq data (ewoc-data crt))
537 (setq dir (vc-dir-node-directory crt))
538 (if (string-match argdir-re dir)
539 (when (vc-dir-fileinfo->marked data)
540 (error "Cannot mark `%s', child `%s' marked"
541 (vc-dir-fileinfo->name (ewoc-data arg))
542 (vc-dir-fileinfo->name data)))
543 ;; We are done, we got to an entry that is not a child of `arg'.
544 (setq is-child nil)))
545 nil))
546
547 (defun vc-dir-mark-file (&optional arg)
548 ;; Mark ARG or the current file and move to the next line.
549 (let* ((crt (or arg (ewoc-locate vc-ewoc)))
550 (file (ewoc-data crt))
551 (isdir (vc-dir-fileinfo->directory file)))
552 (when (or (and isdir (not (vc-dir-children-marked-p crt)))
553 (and (not isdir) (not (vc-dir-parent-marked-p crt))))
554 (setf (vc-dir-fileinfo->marked file) t)
555 (ewoc-invalidate vc-ewoc crt)
556 (unless (or arg (mouse-event-p last-command-event))
557 (vc-dir-next-line 1)))))
558
559 (defun vc-dir-mark ()
560 "Mark the current file or all files in the region.
561 If the region is active, mark all the files in the region.
562 Otherwise mark the file on the current line and move to the next
563 line."
564 (interactive)
565 (vc-dir-mark-unmark 'vc-dir-mark-file))
566
567 (defun vc-dir-mark-all-files (arg)
568 "Mark all files with the same state as the current one.
569 With a prefix argument mark all files.
570 If the current entry is a directory, mark all child files.
571
572 The commands operate on files that are on the same state.
573 This command is intended to make it easy to select all files that
574 share the same state."
575 (interactive "P")
576 (if arg
577 ;; Mark all files.
578 (progn
579 ;; First check that no directory is marked, we can't mark
580 ;; files in that case.
581 (ewoc-map
582 (lambda (filearg)
583 (when (and (vc-dir-fileinfo->directory filearg)
584 (vc-dir-fileinfo->marked filearg))
585 (error "Cannot mark all files, directory `%s' marked"
586 (vc-dir-fileinfo->name filearg))))
587 vc-ewoc)
588 (ewoc-map
589 (lambda (filearg)
590 (unless (vc-dir-fileinfo->marked filearg)
591 (setf (vc-dir-fileinfo->marked filearg) t)
592 t))
593 vc-ewoc))
594 (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
595 (if (vc-dir-fileinfo->directory data)
596 ;; It's a directory, mark child files.
597 (let ((crt (ewoc-locate vc-ewoc)))
598 (unless (vc-dir-children-marked-p crt)
599 (while (setq crt (ewoc-next vc-ewoc crt))
600 (let ((crt-data (ewoc-data crt)))
601 (unless (vc-dir-fileinfo->directory crt-data)
602 (setf (vc-dir-fileinfo->marked crt-data) t)
603 (ewoc-invalidate vc-ewoc crt))))))
604 ;; It's a file
605 (let ((state (vc-dir-fileinfo->state data))
606 (crt (ewoc-nth vc-ewoc 0)))
607 (while crt
608 (let ((crt-data (ewoc-data crt)))
609 (when (and (not (vc-dir-fileinfo->marked crt-data))
610 (eq (vc-dir-fileinfo->state crt-data) state)
611 (not (vc-dir-fileinfo->directory crt-data)))
612 (vc-dir-mark-file crt)))
613 (setq crt (ewoc-next vc-ewoc crt))))))))
614
615 (defun vc-dir-unmark-file ()
616 ;; Unmark the current file and move to the next line.
617 (let* ((crt (ewoc-locate vc-ewoc))
618 (file (ewoc-data crt)))
619 (setf (vc-dir-fileinfo->marked file) nil)
620 (ewoc-invalidate vc-ewoc crt)
621 (unless (mouse-event-p last-command-event)
622 (vc-dir-next-line 1))))
623
624 (defun vc-dir-unmark ()
625 "Unmark the current file or all files in the region.
626 If the region is active, unmark all the files in the region.
627 Otherwise mark the file on the current line and move to the next
628 line."
629 (interactive)
630 (vc-dir-mark-unmark 'vc-dir-unmark-file))
631
632 (defun vc-dir-unmark-file-up ()
633 "Move to the previous line and unmark the file."
634 (interactive)
635 ;; If we're on the first line, we won't move up, but we will still
636 ;; remove the mark. This seems a bit odd but it is what buffer-menu
637 ;; does.
638 (let* ((prev (ewoc-goto-prev vc-ewoc 1))
639 (file (ewoc-data prev)))
640 (setf (vc-dir-fileinfo->marked file) nil)
641 (ewoc-invalidate vc-ewoc prev)
642 (vc-dir-move-to-goal-column)))
643
644 (defun vc-dir-unmark-all-files (arg)
645 "Unmark all files with the same state as the current one.
646 With a prefix argument unmark all files.
647 If the current entry is a directory, unmark all the child files.
648
649 The commands operate on files that are on the same state.
650 This command is intended to make it easy to deselect all files
651 that share the same state."
652 (interactive "P")
653 (if arg
654 (ewoc-map
655 (lambda (filearg)
656 (when (vc-dir-fileinfo->marked filearg)
657 (setf (vc-dir-fileinfo->marked filearg) nil)
658 t))
659 vc-ewoc)
660 (let* ((crt (ewoc-locate vc-ewoc))
661 (data (ewoc-data crt)))
662 (if (vc-dir-fileinfo->directory data)
663 ;; It's a directory, unmark child files.
664 (while (setq crt (ewoc-next vc-ewoc crt))
665 (let ((crt-data (ewoc-data crt)))
666 (unless (vc-dir-fileinfo->directory crt-data)
667 (setf (vc-dir-fileinfo->marked crt-data) nil)
668 (ewoc-invalidate vc-ewoc crt))))
669 ;; It's a file
670 (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
671 (ewoc-map
672 (lambda (filearg)
673 (when (and (vc-dir-fileinfo->marked filearg)
674 (eq (vc-dir-fileinfo->state filearg) crt-state))
675 (setf (vc-dir-fileinfo->marked filearg) nil)
676 t))
677 vc-ewoc))))))
678
679 (defun vc-dir-toggle-mark-file ()
680 (let* ((crt (ewoc-locate vc-ewoc))
681 (file (ewoc-data crt)))
682 (if (vc-dir-fileinfo->marked file)
683 (vc-dir-unmark-file)
684 (vc-dir-mark-file))))
685
686 (defun vc-dir-toggle-mark (e)
687 (interactive "e")
688 (vc-dir-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
689
690 (defun vc-dir-delete-file ()
691 "Delete the marked files, or the current file if no marks."
692 (interactive)
693 (mapc 'vc-delete-file (or (vc-dir-marked-files)
694 (list (vc-dir-current-file)))))
695
696 (defun vc-dir-find-file ()
697 "Find the file on the current line."
698 (interactive)
699 (find-file (vc-dir-current-file)))
700
701 (defun vc-dir-find-file-other-window (&optional event)
702 "Find the file on the current line, in another window."
703 (interactive (list last-nonmenu-event))
704 (if event (posn-set-point (event-end event)))
705 (find-file-other-window (vc-dir-current-file)))
706
707 (defun vc-dir-isearch ()
708 "Search for a string through all marked buffers using Isearch."
709 (interactive)
710 (multi-isearch-files
711 (mapcar 'car (vc-dir-marked-only-files-and-states))))
712
713 (defun vc-dir-isearch-regexp ()
714 "Search for a regexp through all marked buffers using Isearch."
715 (interactive)
716 (multi-isearch-files-regexp
717 (mapcar 'car (vc-dir-marked-only-files-and-states))))
718
719 (defun vc-dir-search (regexp)
720 "Search through all marked files for a match for REGEXP.
721 For marked directories, use the files displayed from those directories.
722 Stops when a match is found.
723 To continue searching for next match, use command \\[tags-loop-continue]."
724 (interactive "sSearch marked files (regexp): ")
725 (tags-search regexp '(mapcar 'car (vc-dir-marked-only-files-and-states))))
726
727 (defun vc-dir-query-replace-regexp (from to &optional delimited)
728 "Do `query-replace-regexp' of FROM with TO, on all marked files.
729 For marked directories, use the files displayed from those directories.
730 If a directory is marked, then use the files displayed for that directory.
731 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
732 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
733 with the command \\[tags-loop-continue]."
734 ;; FIXME: this is almost a copy of `dired-do-replace-regexp'. This
735 ;; should probably be made generic and used in both places instead of
736 ;; duplicating it here.
737 (interactive
738 (let ((common
739 (query-replace-read-args
740 "Query replace regexp in marked files" t t)))
741 (list (nth 0 common) (nth 1 common) (nth 2 common))))
742 (dolist (file (mapcar 'car (vc-dir-marked-only-files-and-states)))
743 (let ((buffer (get-file-buffer file)))
744 (if (and buffer (with-current-buffer buffer
745 buffer-read-only))
746 (error "File `%s' is visited read-only" file))))
747 (tags-query-replace from to delimited
748 '(mapcar 'car (vc-dir-marked-only-files-and-states))))
749
750 (defun vc-dir-current-file ()
751 (let ((node (ewoc-locate vc-ewoc)))
752 (unless node
753 (error "No file available"))
754 (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
755
756 (defun vc-dir-marked-files ()
757 "Return the list of marked files."
758 (mapcar
759 (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
760 (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
761
762 (defun vc-dir-marked-only-files-and-states ()
763 "Return the list of conses (FILE . STATE) for the marked files.
764 For marked directories return the corresponding conses for the
765 child files."
766 (let ((crt (ewoc-nth vc-ewoc 0))
767 result)
768 (while crt
769 (let ((crt-data (ewoc-data crt)))
770 (if (vc-dir-fileinfo->marked crt-data)
771 ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
772 (if (vc-dir-fileinfo->directory crt-data)
773 (let* ((dir (vc-dir-fileinfo->directory crt-data))
774 (dirlen (length dir))
775 data)
776 (while
777 (and (setq crt (ewoc-next vc-ewoc crt))
778 (vc-string-prefix-p dir
779 (progn
780 (setq data (ewoc-data crt))
781 (vc-dir-node-directory crt))))
782 (unless (vc-dir-fileinfo->directory data)
783 (push
784 (cons (expand-file-name (vc-dir-fileinfo->name data))
785 (vc-dir-fileinfo->state data))
786 result))))
787 (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
788 (vc-dir-fileinfo->state crt-data))
789 result)
790 (setq crt (ewoc-next vc-ewoc crt)))
791 (setq crt (ewoc-next vc-ewoc crt)))))
792 (nreverse result)))
793
794 (defun vc-dir-child-files-and-states ()
795 "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
796 If it is a file, return the corresponding cons for the file itself."
797 (let* ((crt (ewoc-locate vc-ewoc))
798 (crt-data (ewoc-data crt))
799 result)
800 (if (vc-dir-fileinfo->directory crt-data)
801 (let* ((dir (vc-dir-fileinfo->directory crt-data))
802 (dirlen (length dir))
803 data)
804 (while
805 (and (setq crt (ewoc-next vc-ewoc crt))
806 (vc-string-prefix-p dir (progn
807 (setq data (ewoc-data crt))
808 (vc-dir-node-directory crt))))
809 (unless (vc-dir-fileinfo->directory data)
810 (push
811 (cons (expand-file-name (vc-dir-fileinfo->name data))
812 (vc-dir-fileinfo->state data))
813 result))))
814 (push
815 (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
816 (vc-dir-fileinfo->state crt-data)) result))
817 (nreverse result)))
818
819 (defun vc-dir-recompute-file-state (fname def-dir)
820 (let* ((file-short (file-relative-name fname def-dir))
821 (remove-me-when-CVS-works
822 (when (eq vc-dir-backend 'CVS)
823 ;; FIXME: Warning: UGLY HACK. The CVS backend caches the state
824 ;; info, this forces the backend to update it.
825 (vc-call-backend vc-dir-backend 'registered fname)))
826 (state (vc-call-backend vc-dir-backend 'state fname))
827 (extra (vc-call-backend vc-dir-backend
828 'status-fileinfo-extra fname)))
829 (list file-short state extra)))
830
831 (defun vc-dir-find-child-files (dirname)
832 ;; Give a DIRNAME string return the list of all child files shown in
833 ;; the current *vc-dir* buffer.
834 (let ((crt (ewoc-nth vc-ewoc 0))
835 children
836 dname)
837 ;; Find DIR
838 (while (and crt (not (vc-string-prefix-p
839 dirname (vc-dir-node-directory crt))))
840 (setq crt (ewoc-next vc-ewoc crt)))
841 (while (and crt (vc-string-prefix-p
842 dirname
843 (setq dname (vc-dir-node-directory crt))))
844 (let ((data (ewoc-data crt)))
845 (unless (vc-dir-fileinfo->directory data)
846 (push (expand-file-name (vc-dir-fileinfo->name data)) children)))
847 (setq crt (ewoc-next vc-ewoc crt)))
848 children))
849
850 (defun vc-dir-resync-directory-files (dirname)
851 ;; Update the entries for all the child files of DIRNAME shown in
852 ;; the current *vc-dir* buffer.
853 (let ((files (vc-dir-find-child-files dirname))
854 (ddir default-directory)
855 fileentries)
856 (when files
857 (dolist (crt files)
858 (push (vc-dir-recompute-file-state crt ddir)
859 fileentries))
860 (vc-dir-update fileentries (current-buffer)))))
861
862 (defun vc-dir-resynch-file (&optional fname)
863 "Update the entries for FNAME in any directory buffers that list it."
864 (let ((file (or fname (expand-file-name buffer-file-name)))
865 (drop '()))
866 (save-current-buffer
867 ;; look for a vc-dir buffer that might show this file.
868 (dolist (status-buf vc-dir-buffers)
869 (if (not (buffer-live-p status-buf))
870 (push status-buf drop)
871 (set-buffer status-buf)
872 (if (not (derived-mode-p 'vc-dir-mode))
873 (push status-buf drop)
874 (let ((ddir default-directory))
875 (when (vc-string-prefix-p ddir file)
876 (if (file-directory-p file)
877 (vc-dir-resync-directory-files file)
878 (let ((state (vc-dir-recompute-file-state file ddir)))
879 (vc-dir-update
880 (list state)
881 status-buf (eq (cadr state) 'up-to-date))))))))))
882 ;; Remove out-of-date entries from vc-dir-buffers.
883 (dolist (b drop) (setq vc-dir-buffers (delq b vc-dir-buffers)))))
884
885 (defvar use-vc-backend) ;; dynamically bound
886
887 (define-derived-mode vc-dir-mode special-mode "VC dir"
888 "Major mode for VC directory buffers.
889 Marking/Unmarking key bindings and actions:
890 m - mark a file/directory
891 - if the region is active, mark all the files in region.
892 Restrictions: - a file cannot be marked if any parent directory is marked
893 - a directory cannot be marked if any child file or
894 directory is marked
895 u - unmark a file/directory
896 - if the region is active, unmark all the files in region.
897 M - if the cursor is on a file: mark all the files with the same state as
898 the current file
899 - if the cursor is on a directory: mark all child files
900 - with a prefix argument: mark all files
901 U - if the cursor is on a file: unmark all the files with the same state
902 as the current file
903 - if the cursor is on a directory: unmark all child files
904 - with a prefix argument: unmark all files
905 mouse-2 - toggles the mark state
906
907 VC commands
908 VC commands in the `C-x v' prefix can be used.
909 VC commands act on the marked entries. If nothing is marked, VC
910 commands act on the current entry.
911
912 Search & Replace
913 S - searches the marked files
914 Q - does a query replace on the marked files
915 M-s a C-s - does an isearch on the marked files
916 M-s a C-M-s - does a regexp isearch on the marked files
917 If nothing is marked, these commands act on the current entry.
918 When a directory is current or marked, the Search & Replace
919 commands act on the child files of that directory that are displayed in
920 the *vc-dir* buffer.
921
922 \\{vc-dir-mode-map}"
923 (set (make-local-variable 'vc-dir-backend) use-vc-backend)
924 (setq buffer-read-only t)
925 (when (boundp 'tool-bar-map)
926 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
927 (let ((buffer-read-only nil))
928 (erase-buffer)
929 (set (make-local-variable 'vc-dir-process-buffer) nil)
930 (set (make-local-variable 'vc-ewoc) (ewoc-create #'vc-dir-printer))
931 (set (make-local-variable 'revert-buffer-function)
932 'vc-dir-revert-buffer-function)
933 (setq list-buffers-directory (expand-file-name "*vc-dir*" default-directory))
934 (add-to-list 'vc-dir-buffers (current-buffer))
935 ;; Make sure that if the directory buffer is killed, the update
936 ;; process running in the background is also killed.
937 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
938 (vc-dir-refresh)))
939
940 (defun vc-dir-headers (backend dir)
941 "Display the headers in the *VC dir* buffer.
942 It calls the `dir-extra-headers' backend method to display backend
943 specific headers."
944 (concat
945 ;; First layout the common headers.
946 (propertize "VC backend : " 'face 'font-lock-type-face)
947 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
948 (propertize "Working dir: " 'face 'font-lock-type-face)
949 (propertize (format "%s\n" dir) 'face 'font-lock-variable-name-face)
950 ;; Then the backend specific ones.
951 (vc-call-backend backend 'dir-extra-headers dir)
952 "\n"))
953
954 (defun vc-dir-refresh-files (files default-state)
955 "Refresh some files in the *VC-dir* buffer."
956 (let ((def-dir default-directory)
957 (backend vc-dir-backend))
958 (vc-set-mode-line-busy-indicator)
959 ;; Call the `dir-status-file' backend function.
960 ;; `dir-status-file' is supposed to be asynchronous.
961 ;; It should compute the results, and then call the function
962 ;; passed as an argument in order to update the vc-dir buffer
963 ;; with the results.
964 (unless (buffer-live-p vc-dir-process-buffer)
965 (setq vc-dir-process-buffer
966 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
967 (lexical-let ((buffer (current-buffer)))
968 (with-current-buffer vc-dir-process-buffer
969 (cd def-dir)
970 (erase-buffer)
971 (vc-call-backend
972 backend 'dir-status-files def-dir files default-state
973 (lambda (entries &optional more-to-come)
974 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
975 ;; If MORE-TO-COME is true, then more updates will come from
976 ;; the asynchronous process.
977 (with-current-buffer buffer
978 (vc-dir-update entries buffer)
979 (unless more-to-come
980 (setq mode-line-process nil)
981 ;; Remove the ones that haven't been updated at all.
982 ;; Those not-updated are those whose state is nil because the
983 ;; file/dir doesn't exist and isn't versioned.
984 (ewoc-filter vc-ewoc
985 (lambda (info)
986 ;; The state for directory entries might
987 ;; have been changed to 'up-to-date,
988 ;; reset it, othewise it will be removed when doing 'x'
989 ;; next time.
990 ;; FIXME: There should be a more elegant way to do this.
991 (when (and (vc-dir-fileinfo->directory info)
992 (eq (vc-dir-fileinfo->state info)
993 'up-to-date))
994 (setf (vc-dir-fileinfo->state info) nil))
995
996 (not (vc-dir-fileinfo->needs-update info))))))))))))
997
998 (defun vc-dir-revert-buffer-function (&optional ignore-auto noconfirm)
999 (vc-dir-refresh))
1000
1001 (defun vc-dir-refresh ()
1002 "Refresh the contents of the *VC-dir* buffer.
1003 Throw an error if another update process is in progress."
1004 (interactive)
1005 (if (vc-dir-busy)
1006 (error "Another update process is in progress, cannot run two at a time")
1007 (let ((def-dir default-directory)
1008 (backend vc-dir-backend))
1009 (vc-set-mode-line-busy-indicator)
1010 ;; Call the `dir-status' backend function.
1011 ;; `dir-status' is supposed to be asynchronous.
1012 ;; It should compute the results, and then call the function
1013 ;; passed as an argument in order to update the vc-dir buffer
1014 ;; with the results.
1015
1016 ;; Create a buffer that can be used by `dir-status' and call
1017 ;; `dir-status' with this buffer as the current buffer. Use
1018 ;; `vc-dir-process-buffer' to remember this buffer, so that
1019 ;; it can be used later to kill the update process in case it
1020 ;; takes too long.
1021 (unless (buffer-live-p vc-dir-process-buffer)
1022 (setq vc-dir-process-buffer
1023 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
1024 ;; set the needs-update flag on all non-directory entries
1025 (ewoc-map (lambda (info)
1026 (unless (vc-dir-fileinfo->directory info)
1027 (setf (vc-dir-fileinfo->needs-update info) t) nil))
1028 vc-ewoc)
1029 (lexical-let ((buffer (current-buffer)))
1030 (with-current-buffer vc-dir-process-buffer
1031 (cd def-dir)
1032 (erase-buffer)
1033 (vc-call-backend
1034 backend 'dir-status def-dir
1035 (lambda (entries &optional more-to-come)
1036 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1037 ;; If MORE-TO-COME is true, then more updates will come from
1038 ;; the asynchronous process.
1039 (with-current-buffer buffer
1040 (vc-dir-update entries buffer)
1041 (unless more-to-come
1042 (let ((remaining
1043 (ewoc-collect
1044 vc-ewoc 'vc-dir-fileinfo->needs-update)))
1045 (if remaining
1046 (vc-dir-refresh-files
1047 (mapcar 'vc-dir-fileinfo->name remaining)
1048 'up-to-date)
1049 (setq mode-line-process nil)))))))))
1050 (ewoc-set-hf vc-ewoc (vc-dir-headers backend def-dir) ""))))
1051
1052 (defun vc-dir-show-fileentry (file)
1053 "Insert an entry for a specific file into the current *VC-dir* listing.
1054 This is typically used if the file is up-to-date (or has been added
1055 outside of VC) and one wants to do some operation on it."
1056 (interactive "fShow file: ")
1057 (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
1058
1059 (defun vc-dir-hide-up-to-date ()
1060 "Hide up-to-date items from display."
1061 (interactive)
1062 (let ((crt (ewoc-nth vc-ewoc -1))
1063 (first (ewoc-nth vc-ewoc 0)))
1064 ;; Go over from the last item to the first and remove the
1065 ;; up-to-date files and directories with no child files.
1066 (while (not (eq crt first))
1067 (let* ((data (ewoc-data crt))
1068 (dir (vc-dir-fileinfo->directory data))
1069 (next (ewoc-next vc-ewoc crt))
1070 (prev (ewoc-prev vc-ewoc crt))
1071 ;; ewoc-delete does not work without this...
1072 (inhibit-read-only t))
1073 (when (or
1074 ;; Remove directories with no child files.
1075 (and dir
1076 (or
1077 ;; Nothing follows this directory.
1078 (not next)
1079 ;; Next item is a directory.
1080 (vc-dir-fileinfo->directory (ewoc-data next))))
1081 ;; Remove files in the up-to-date state.
1082 (eq (vc-dir-fileinfo->state data) 'up-to-date))
1083 (ewoc-delete vc-ewoc crt))
1084 (setq crt prev)))))
1085
1086 (defun vc-dir-printer (fileentry)
1087 (vc-call-backend vc-dir-backend 'dir-printer fileentry))
1088
1089 (defun vc-dir-deduce-fileset (&optional state-model-only-files)
1090 (let ((marked (vc-dir-marked-files))
1091 files
1092 only-files-list
1093 state
1094 model)
1095 (if marked
1096 (progn
1097 (setq files marked)
1098 (when state-model-only-files
1099 (setq only-files-list (vc-dir-marked-only-files-and-states))))
1100 (let ((crt (vc-dir-current-file)))
1101 (setq files (list crt))
1102 (when state-model-only-files
1103 (setq only-files-list (vc-dir-child-files-and-states)))))
1104
1105 (when state-model-only-files
1106 (setq state (cdar only-files-list))
1107 ;; Check that all files are in a consistent state, since we use that
1108 ;; state to decide which operation to perform.
1109 (dolist (crt (cdr only-files-list))
1110 (unless (vc-compatible-state (cdr crt) state)
1111 (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"
1112 (car crt) (cdr crt) (caar only-files-list) state)))
1113 (setq only-files-list (mapcar 'car only-files-list))
1114 (when (and state (not (eq state 'unregistered)))
1115 (setq model (vc-checkout-model vc-dir-backend only-files-list))))
1116 (list vc-dir-backend files only-files-list state model)))
1117
1118 ;;;###autoload
1119 (defun vc-dir (dir &optional backend)
1120 "Show the VC status for \"interesting\" files in and below DIR.
1121 This allows you to mark files and perform VC operations on them.
1122 The list omits files which are up to date, with no changes in your copy
1123 or the repository, if there is nothing in particular to say about them.
1124
1125 Preparing the list of file status takes time; when the buffer
1126 first appears, it has only the first few lines of summary information.
1127 The file lines appear later.
1128
1129 Optional second argument BACKEND specifies the VC backend to use.
1130 Interactively, a prefix argument means to ask for the backend.
1131
1132 These are the commands available for use in the file status buffer:
1133
1134 \\{vc-dir-mode-map}"
1135
1136 (interactive
1137 (list
1138 ;; When you hit C-x v d in a visited VC file,
1139 ;; the *vc-dir* buffer visits the directory under its truename;
1140 ;; therefore it makes sense to always do that.
1141 ;; Otherwise if you do C-x v d -> C-x C-f -> C-c v d
1142 ;; you may get a new *vc-dir* buffer, different from the original
1143 (file-truename (read-file-name "VC status for directory: "
1144 default-directory default-directory t
1145 nil #'file-directory-p))
1146 (if current-prefix-arg
1147 (intern
1148 (completing-read
1149 "Use VC backend: "
1150 (mapcar (lambda (b) (list (symbol-name b)))
1151 vc-handled-backends)
1152 nil t nil nil)))))
1153 (unless backend
1154 (setq backend (vc-responsible-backend dir)))
1155 (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir backend))
1156 (if (derived-mode-p 'vc-dir-mode)
1157 (vc-dir-refresh)
1158 ;; FIXME: find a better way to pass the backend to `vc-dir-mode'.
1159 (let ((use-vc-backend backend))
1160 (vc-dir-mode))))
1161
1162 (defun vc-default-dir-extra-headers (backend dir)
1163 ;; Be loud by default to remind people to add code to display
1164 ;; backend specific headers.
1165 ;; XXX: change this to return nil before the release.
1166 (concat
1167 (propertize "Extra : " 'face 'font-lock-type-face)
1168 (propertize "Please add backend specific headers here. It's easy!"
1169 'face 'font-lock-warning-face)))
1170
1171 (defvar vc-dir-filename-mouse-map
1172 (let ((map (make-sparse-keymap)))
1173 (define-key map [mouse-2] 'vc-dir-find-file-other-window)
1174 map)
1175 "Local keymap for visiting a file.")
1176
1177 (defun vc-default-dir-printer (backend fileentry)
1178 "Pretty print FILEENTRY."
1179 ;; If you change the layout here, change vc-dir-move-to-goal-column.
1180 ;; VC backends can implement backend specific versions of this
1181 ;; function. Changes here might need to be reflected in the
1182 ;; vc-BACKEND-dir-printer functions.
1183 (let* ((isdir (vc-dir-fileinfo->directory fileentry))
1184 (state (if isdir "" (vc-dir-fileinfo->state fileentry)))
1185 (filename (vc-dir-fileinfo->name fileentry)))
1186 (insert
1187 (propertize
1188 (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1189 'face 'font-lock-type-face)
1190 " "
1191 (propertize
1192 (format "%-20s" state)
1193 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1194 ((memq state '(missing conflict)) 'font-lock-warning-face)
1195 (t 'font-lock-variable-name-face))
1196 'mouse-face 'highlight)
1197 " "
1198 (propertize
1199 (format "%s" filename)
1200 'face
1201 (if isdir 'font-lock-comment-delimiter-face 'font-lock-function-name-face)
1202 'help-echo
1203 (if isdir
1204 "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
1205 "File\nmouse-3: Pop-up menu")
1206 'mouse-face 'highlight
1207 'keymap vc-dir-filename-mouse-map))))
1208
1209 (defun vc-default-extra-status-menu (backend)
1210 nil)
1211
1212 (defun vc-default-status-fileinfo-extra (backend file)
1213 "Default absence of extra information returned for a file."
1214 nil)
1215
1216 (provide 'vc-dir)
1217
1218 ;; arch-tag: 0274a2e3-e8e9-4b1a-a73c-e8b9129d5d15
1219 ;;; vc-dir.el ends here