Merge from emacs--devo--0, gnus--rel--5.10
[gnus] / lisp / vc-dir.el
1 ;;; vc-dir.el --- Directory status display under VC
2
3 ;; Copyright (C) 2007, 2008
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-status-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 (expand-file-name dir))
92   (let*
93          ;; Look for another buffer name BNAME visiting the same directory.
94          ((buf (save-excursion
95                 (unless create-new
96                   (dolist (buffer (buffer-list))
97                     (set-buffer buffer)
98                     (when (and (derived-mode-p 'vc-dir-mode)
99                                (eq vc-dir-backend backend)
100                                (string= (expand-file-name default-directory) dir))
101                       (return buffer)))))))
102     (or buf
103         ;; Create a new buffer named BNAME.
104         (with-current-buffer (create-file-buffer bname)
105           (cd dir)
106           (vc-setup-buffer (current-buffer))
107           ;; Reset the vc-parent-buffer-name so that it does not appear
108           ;; in the mode-line.
109           (setq vc-parent-buffer-name nil)
110           (current-buffer)))))
111
112 (defvar vc-dir-menu-map
113   (let ((map (make-sparse-keymap "VC-dir")))
114     (define-key map [quit]
115       '(menu-item "Quit" quit-window
116                   :help "Quit"))
117     (define-key map [kill]
118       '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
119                   :enable (vc-dir-busy)
120                   :help "Kill the command that updates the directory buffer"))
121     (define-key map [refresh]
122       '(menu-item "Refresh" revert-buffer
123                   :enable (not (vc-dir-busy))
124                   :help "Refresh the contents of the directory buffer"))
125     (define-key map [remup]
126       '(menu-item "Hide up-to-date" vc-dir-hide-up-to-date
127                   :help "Hide up-to-date items from display"))
128     ;; Movement.
129     (define-key map [sepmv] '("--"))
130     (define-key map [next-line]
131       '(menu-item "Next line" vc-dir-next-line
132                   :help "Go to the next line" :keys "n"))
133     (define-key map [previous-line]
134       '(menu-item "Previous line" vc-dir-previous-line
135                   :help "Go to the previous line"))
136     ;; Marking.
137     (define-key map [sepmrk] '("--"))
138     (define-key map [unmark-all]
139       '(menu-item "Unmark All" vc-dir-unmark-all-files
140                   :help "Unmark all files that are in the same state as the current file\
141 \nWith prefix argument unmark all files"))
142     (define-key map [unmark-previous]
143       '(menu-item "Unmark previous " vc-dir-unmark-file-up
144                   :help "Move to the previous line and unmark the file"))
145
146     (define-key map [mark-all]
147       '(menu-item "Mark All" vc-dir-mark-all-files
148                   :help "Mark all files that are in the same state as the current file\
149 \nWith prefix argument mark all files"))
150     (define-key map [unmark]
151       '(menu-item "Unmark" vc-dir-unmark
152                   :help "Unmark the current file or all files in the region"))
153
154     (define-key map [mark]
155       '(menu-item "Mark" vc-dir-mark
156                   :help "Mark the current file or all files in the region"))
157
158     (define-key map [sepopn] '("--"))
159     (define-key map [open-other]
160       '(menu-item "Open in other window" vc-dir-find-file-other-window
161                   :help "Find the file on the current line, in another window"))
162     (define-key map [open]
163       '(menu-item "Open file" vc-dir-find-file
164                   :help "Find the file on the current line"))
165     (define-key map [sepvcdet] '("--"))
166     ;; FIXME: This needs a key binding.  And maybe a better name
167     ;; ("Insert" like PCL-CVS uses does not sound that great either)...
168     (define-key map [ins]
169       '(menu-item "Show File" vc-dir-show-fileentry
170                   :help "Show a file in the VC status listing even though it might be up to date"))
171     (define-key map [annotate]
172       '(menu-item "Annotate" vc-annotate
173                   :help "Display the edit history of the current file using colors"))
174     (define-key map [diff]
175       '(menu-item "Compare with Base Version" vc-diff
176                   :help "Compare file set with the base version"))
177     (define-key map [log]
178      '(menu-item "Show history" vc-print-log
179      :help "List the change log of the current file set in a window"))
180     ;; VC commands.
181     (define-key map [sepvccmd] '("--"))
182     (define-key map [update]
183       '(menu-item "Update to latest version" vc-update
184                   :help "Update the current fileset's files to their tip revisions"))
185     (define-key map [revert]
186       '(menu-item "Revert to base version" vc-revert
187                   :help "Revert working copies of the selected fileset to their repository contents."))
188     (define-key map [next-action]
189       ;; FIXME: This really really really needs a better name!
190       ;; And a key binding too.
191       '(menu-item "Check In/Out" vc-next-action
192                   :help "Do the next logical version control operation on the current fileset"))
193     (define-key map [register]
194       '(menu-item "Register" vc-register
195                   :help "Register file set into the version control system"))
196     map)
197   "Menu for dispatcher status")
198
199 ;; VC backends can use this to add mode-specific menu items to
200 ;; vc-dir-menu-map.
201 (defun vc-dir-menu-map-filter (orig-binding)
202   (when (and (symbolp orig-binding) (fboundp orig-binding))
203     (setq orig-binding (indirect-function orig-binding)))
204   (let ((ext-binding
205          (when (derived-mode-p 'vc-dir-mode)
206            (vc-call-backend vc-dir-backend 'extra-status-menu))))
207     (if (null ext-binding)
208         orig-binding
209       (append orig-binding
210               '("----")
211               ext-binding))))
212
213 (defvar vc-dir-mode-map
214   (let ((map (make-keymap)))
215     (suppress-keymap map)
216     ;; VC commands
217     (define-key map "v" 'vc-next-action)   ;; C-x v v
218     (define-key map "=" 'vc-diff)          ;; C-x v =
219     (define-key map "i" 'vc-register)      ;; C-x v i
220     (define-key map "+" 'vc-update)        ;; C-x v +
221     (define-key map "l" 'vc-print-log)     ;; C-x v l
222     ;; More confusing than helpful, probably
223     ;;(define-key map "R" 'vc-revert) ;; u is taken by dispatcher unmark.
224     ;;(define-key map "A" 'vc-annotate) ;; g is taken by dispatcher refresh
225     ;; Marking.
226     (define-key map "m" 'vc-dir-mark)
227     (define-key map "M" 'vc-dir-mark-all-files)
228     (define-key map "u" 'vc-dir-unmark)
229     (define-key map "U" 'vc-dir-unmark-all-files)
230     (define-key map "\C-?" 'vc-dir-unmark-file-up)
231     (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
232     ;; Movement.
233     (define-key map "n" 'vc-dir-next-line)
234     (define-key map " " 'vc-dir-next-line)
235     (define-key map "\t" 'vc-dir-next-directory)
236     (define-key map "p" 'vc-dir-previous-line)
237     (define-key map [backtab] 'vc-dir-previous-directory)
238     ;;; Rebind paragraph-movement commands.
239     (define-key map "\M-}" 'vc-dir-next-directory)
240     (define-key map "\M-{" 'vc-dir-previous-directory)
241     (define-key map [C-down] 'vc-dir-next-directory)
242     (define-key map [C-up] 'vc-dir-previous-directory)
243     ;; The remainder.
244     (define-key map "f" 'vc-dir-find-file)
245     (define-key map "\C-m" 'vc-dir-find-file)
246     (define-key map "o" 'vc-dir-find-file-other-window)
247     (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
248     (define-key map [down-mouse-3] 'vc-dir-menu)
249     (define-key map [mouse-2] 'vc-dir-toggle-mark)
250     (define-key map "x" 'vc-dir-hide-up-to-date)
251
252     ;; Hook up the menu.
253     (define-key map [menu-bar vc-dir-mode]
254       `(menu-item
255         ;; VC backends can use this to add mode-specific menu items to
256         ;; vc-dir-menu-map.
257         "VC-dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
258     map)
259   "Keymap for directory buffer.")
260
261 (defmacro vc-at-event (event &rest body)
262   "Evaluate `body' with point located at event-start of `event'.
263 If `body' uses `event', it should be a variable,
264  otherwise it will be evaluated twice."
265   (let ((posn (make-symbol "vc-at-event-posn")))
266     `(let ((,posn (event-start ,event)))
267        (save-excursion
268          (set-buffer (window-buffer (posn-window ,posn)))
269          (goto-char (posn-point ,posn))
270          ,@body))))
271
272 (defun vc-dir-menu (e)
273   "Popup the dispatcher status menu."
274   (interactive "e")
275   (vc-at-event e (popup-menu vc-dir-menu-map e)))
276
277 (defvar vc-dir-tool-bar-map
278   (let ((map (make-sparse-keymap)))
279     (tool-bar-local-item-from-menu 'vc-dir-find-file "open"
280                                    map vc-dir-mode-map)
281     (tool-bar-local-item "bookmark_add"
282                          'vc-dir-toggle-mark 'vc-dir-toggle-mark map
283                          :help "Toggle mark on current item")
284     (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow"
285                                    map vc-dir-mode-map
286                                    :rtl "right-arrow")
287     (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow"
288                                    map vc-dir-mode-map
289                                    :rtl "left-arrow")
290     (tool-bar-local-item-from-menu 'vc-print-log "info"
291                                    map vc-dir-mode-map)
292     (tool-bar-local-item-from-menu 'revert-buffer "refresh"
293                                    map vc-dir-mode-map)
294     (tool-bar-local-item-from-menu 'nonincremental-search-forward
295                                    "search" map)
296     (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
297                                    map vc-dir-mode-map)
298     (tool-bar-local-item-from-menu 'quit-window "exit"
299                                    map vc-dir-mode-map)
300     map))
301
302 (defun vc-dir-node-directory (node)
303   ;; Compute the directory for NODE.
304   ;; If it's a directory node, get it from the the node.
305   (let ((data (ewoc-data node)))
306     (or (vc-dir-fileinfo->directory data)
307         ;; Otherwise compute it from the file name.
308         (file-name-directory
309          (expand-file-name
310           (vc-dir-fileinfo->name data))))))
311
312 (defun vc-dir-update (entries buffer &optional noinsert)
313   "Update BUFFER's ewoc from the list of ENTRIES.
314 If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
315   ;; Add ENTRIES to the vc-dir buffer BUFFER.
316   (with-current-buffer buffer
317     ;; Insert the entries sorted by name into the ewoc.
318     ;; We assume the ewoc is sorted too, which should be the
319     ;; case if we always add entries with vc-dir-update.
320     (setq entries
321           ;; Sort: first files and then subdirectories.
322           ;; XXX: this is VERY inefficient, it computes the directory
323           ;; names too many times
324           (sort entries
325                 (lambda (entry1 entry2)
326                   (let ((dir1 (file-name-directory (expand-file-name (car entry1))))
327                         (dir2 (file-name-directory (expand-file-name (car entry2)))))
328                     (cond
329                      ((string< dir1 dir2) t)
330                      ((not (string= dir1 dir2)) nil)
331                      ((string< (car entry1) (car entry2))))))))
332     ;; Insert directory entries in the right places.
333     (let ((entry (car entries))
334           (node (ewoc-nth vc-ewoc 0)))
335       ;; Insert . if it is not present.
336       (unless node
337         (let ((rd (file-relative-name default-directory)))
338           (ewoc-enter-last
339            vc-ewoc (vc-dir-create-fileinfo
340                     rd nil nil nil (expand-file-name default-directory))))
341         (setq node (ewoc-nth vc-ewoc 0)))
342
343       (while (and entry node)
344         (let* ((entryfile (car entry))
345                (entrydir (file-name-directory (expand-file-name entryfile)))
346                (nodedir (vc-dir-node-directory node)))
347           (cond
348            ;; First try to find the directory.
349            ((string-lessp nodedir entrydir)
350             (setq node (ewoc-next vc-ewoc node)))
351            ((string-equal nodedir entrydir)
352             ;; Found the directory, find the place for the file name.
353             (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
354               (cond
355                ((string-lessp nodefile entryfile)
356                 (setq node (ewoc-next vc-ewoc node)))
357                ((string-equal nodefile entryfile)
358                 (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
359                 (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
360                 (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
361                 (ewoc-invalidate vc-ewoc node)
362                 (setq entries (cdr entries))
363                 (setq entry (car entries))
364                 (setq node (ewoc-next vc-ewoc node)))
365                (t
366                 (ewoc-enter-before vc-ewoc node
367                                    (apply 'vc-dir-create-fileinfo entry))
368                 (setq entries (cdr entries))
369                 (setq entry (car entries))))))
370            (t
371             ;; We might need to insert a directory node if the
372             ;; previous node was in a different directory.
373             (let* ((rd (file-relative-name entrydir))
374                    (prev-node (ewoc-prev vc-ewoc node))
375                    (prev-dir (vc-dir-node-directory prev-node)))
376               (unless (string-equal entrydir prev-dir)
377                 (ewoc-enter-before
378                  vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
379             ;; Now insert the node itself.
380             (ewoc-enter-before vc-ewoc node
381                                (apply 'vc-dir-create-fileinfo entry))
382             (setq entries (cdr entries) entry (car entries))))))
383       ;; We're past the last node, all remaining entries go to the end.
384       (unless (or node noinsert)
385         (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
386           (dolist (entry entries)
387             (let ((entrydir (file-name-directory (expand-file-name (car entry)))))
388               ;; Insert a directory node if needed.
389               (unless (string-equal lastdir entrydir)
390                 (setq lastdir entrydir)
391                 (let ((rd (file-relative-name entrydir)))
392                   (ewoc-enter-last
393                    vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
394               ;; Now insert the node itself.
395               (ewoc-enter-last vc-ewoc
396                                (apply 'vc-dir-create-fileinfo entry)))))))))
397
398 (defun vc-dir-busy ()
399   (and (buffer-live-p vc-dir-process-buffer)
400        (get-buffer-process vc-dir-process-buffer)))
401
402 (defun vc-dir-kill-dir-status-process ()
403   "Kill the temporary buffer and associated process."
404   (interactive)
405   (when (buffer-live-p vc-dir-process-buffer)
406     (let ((proc (get-buffer-process vc-dir-process-buffer)))
407       (when proc (delete-process proc))
408       (setq vc-dir-process-buffer nil)
409       (setq mode-line-process nil))))
410
411 (defun vc-dir-kill-query ()
412   ;; Make sure that when the status buffer is killed the update
413   ;; process running in background is also killed.
414   (if (vc-dir-busy)
415     (when (y-or-n-p "Status update process running, really kill status buffer? ")
416       (vc-dir-kill-dir-status-process)
417       t)
418     t))
419
420 (defun vc-dir-next-line (arg)
421   "Go to the next line.
422 If a prefix argument is given, move by that many lines."
423   (interactive "p")
424   (with-no-warnings
425     (ewoc-goto-next vc-ewoc arg)
426     (vc-dir-move-to-goal-column)))
427
428 (defun vc-dir-previous-line (arg)
429   "Go to the previous line.
430 If a prefix argument is given, move by that many lines."
431   (interactive "p")
432   (ewoc-goto-prev vc-ewoc arg)
433   (vc-dir-move-to-goal-column))
434
435 (defun vc-dir-next-directory ()
436   "Go to the next directory."
437   (interactive)
438   (let ((orig (point)))
439     (if
440         (catch 'foundit
441           (while t
442             (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
443               (cond ((not next)
444                      (throw 'foundit t))
445                     (t
446                      (progn
447                        (ewoc-goto-node vc-ewoc next)
448                        (vc-dir-move-to-goal-column)
449                        (if (vc-dir-fileinfo->directory (ewoc-data next))
450                            (throw 'foundit nil))))))))
451         (goto-char orig))))
452
453 (defun vc-dir-previous-directory ()
454   "Go to the previous directory."
455   (interactive)
456   (let ((orig (point)))
457     (if
458         (catch 'foundit
459           (while t
460             (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
461               (cond ((not prev)
462                      (throw 'foundit t))
463                     (t
464                      (progn
465                        (ewoc-goto-node vc-ewoc prev)
466                        (vc-dir-move-to-goal-column)
467                        (if (vc-dir-fileinfo->directory (ewoc-data prev))
468                            (throw 'foundit nil))))))))
469         (goto-char orig))))
470
471 (defun vc-dir-mark-unmark (mark-unmark-function)
472   (if (use-region-p)
473       (let ((firstl (line-number-at-pos (region-beginning)))
474             (lastl (line-number-at-pos (region-end))))
475         (save-excursion
476           (goto-char (region-beginning))
477           (while (<= (line-number-at-pos) lastl)
478             (funcall mark-unmark-function))))
479     (funcall mark-unmark-function)))
480
481 (defun vc-dir-parent-marked-p (arg)
482   ;; Return nil if none of the parent directories of arg is marked.
483   (let* ((argdir (vc-dir-node-directory arg))
484          (arglen (length argdir))
485          (crt arg)
486          data dir)
487     ;; Go through the predecessors, checking if any directory that is
488     ;; a parent is marked.
489     (while (setq crt (ewoc-prev vc-ewoc crt))
490       (setq data (ewoc-data crt))
491       (setq dir (vc-dir-node-directory crt))
492       (when (and (vc-dir-fileinfo->directory data)
493                  (vc-string-prefix-p dir argdir))
494         (when (vc-dir-fileinfo->marked data)
495           (error "Cannot mark `%s', parent directory `%s' marked"
496                  (vc-dir-fileinfo->name (ewoc-data arg))
497                  (vc-dir-fileinfo->name data)))))
498     nil))
499
500 (defun vc-dir-children-marked-p (arg)
501   ;; Return nil if none of the children of arg is marked.
502   (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
503          (is-child t)
504          (crt arg)
505          data dir)
506     (while (and is-child (setq crt (ewoc-next vc-ewoc crt)))
507       (setq data (ewoc-data crt))
508       (setq dir (vc-dir-node-directory crt))
509       (if (string-match argdir-re dir)
510           (when (vc-dir-fileinfo->marked data)
511             (error "Cannot mark `%s', child `%s' marked"
512                    (vc-dir-fileinfo->name (ewoc-data arg))
513                    (vc-dir-fileinfo->name data)))
514         ;; We are done, we got to an entry that is not a child of `arg'.
515         (setq is-child nil)))
516     nil))
517
518 (defun vc-dir-mark-file (&optional arg)
519   ;; Mark ARG or the current file and move to the next line.
520   (let* ((crt (or arg (ewoc-locate vc-ewoc)))
521          (file (ewoc-data crt))
522          (isdir (vc-dir-fileinfo->directory file)))
523     (when (or (and isdir (not (vc-dir-children-marked-p crt)))
524               (and (not isdir) (not (vc-dir-parent-marked-p crt))))
525       (setf (vc-dir-fileinfo->marked file) t)
526       (ewoc-invalidate vc-ewoc crt)
527       (unless (or arg (mouse-event-p last-command-event))
528         (vc-dir-next-line 1)))))
529
530 (defun vc-dir-mark ()
531   "Mark the current file or all files in the region.
532 If the region is active, mark all the files in the region.
533 Otherwise mark the file on the current line and move to the next
534 line."
535   (interactive)
536   (vc-dir-mark-unmark 'vc-dir-mark-file))
537
538 (defun vc-dir-mark-all-files (arg)
539   "Mark all files with the same state as the current one.
540 With a prefix argument mark all files.
541 If the current entry is a directory, mark all child files.
542
543 The commands operate on files that are on the same state.
544 This command is intended to make it easy to select all files that
545 share the same state."
546   (interactive "P")
547   (if arg
548       ;; Mark all files.
549       (progn
550         ;; First check that no directory is marked, we can't mark
551         ;; files in that case.
552         (ewoc-map
553          (lambda (filearg)
554            (when (and (vc-dir-fileinfo->directory filearg)
555                       (vc-dir-fileinfo->marked filearg))
556              (error "Cannot mark all files, directory `%s' marked"
557                     (vc-dir-fileinfo->name filearg))))
558          vc-ewoc)
559         (ewoc-map
560          (lambda (filearg)
561            (unless (vc-dir-fileinfo->marked filearg)
562              (setf (vc-dir-fileinfo->marked filearg) t)
563              t))
564          vc-ewoc))
565     (let ((data (ewoc-data (ewoc-locate vc-ewoc))))
566       (if (vc-dir-fileinfo->directory data)
567           ;; It's a directory, mark child files.
568           (let ((crt (ewoc-locate vc-ewoc)))
569             (unless (vc-dir-children-marked-p crt)
570               (while (setq crt (ewoc-next vc-ewoc crt))
571                 (let ((crt-data (ewoc-data crt)))
572                   (unless (vc-dir-fileinfo->directory crt-data)
573                     (setf (vc-dir-fileinfo->marked crt-data) t)
574                     (ewoc-invalidate vc-ewoc crt))))))
575         ;; It's a file
576         (let ((state (vc-dir-fileinfo->state data))
577               (crt (ewoc-nth vc-ewoc 0)))
578           (while crt
579             (let ((crt-data (ewoc-data crt)))
580               (when (and (not (vc-dir-fileinfo->marked crt-data))
581                          (eq (vc-dir-fileinfo->state crt-data) state)
582                          (not (vc-dir-fileinfo->directory crt-data)))
583                 (vc-dir-mark-file crt)))
584             (setq crt (ewoc-next vc-ewoc crt))))))))
585
586 (defun vc-dir-unmark-file ()
587   ;; Unmark the current file and move to the next line.
588   (let* ((crt (ewoc-locate vc-ewoc))
589          (file (ewoc-data crt)))
590     (setf (vc-dir-fileinfo->marked file) nil)
591     (ewoc-invalidate vc-ewoc crt)
592     (unless (mouse-event-p last-command-event)
593       (vc-dir-next-line 1))))
594
595 (defun vc-dir-unmark ()
596   "Unmark the current file or all files in the region.
597 If the region is active, unmark all the files in the region.
598 Otherwise mark the file on the current line and move to the next
599 line."
600   (interactive)
601   (vc-dir-mark-unmark 'vc-dir-unmark-file))
602
603 (defun vc-dir-unmark-file-up ()
604   "Move to the previous line and unmark the file."
605   (interactive)
606   ;; If we're on the first line, we won't move up, but we will still
607   ;; remove the mark.  This seems a bit odd but it is what buffer-menu
608   ;; does.
609   (let* ((prev (ewoc-goto-prev vc-ewoc 1))
610          (file (ewoc-data prev)))
611     (setf (vc-dir-fileinfo->marked file) nil)
612     (ewoc-invalidate vc-ewoc prev)
613     (vc-dir-move-to-goal-column)))
614
615 (defun vc-dir-unmark-all-files (arg)
616   "Unmark all files with the same state as the current one.
617 With a prefix argument unmark all files.
618 If the current entry is a directory, unmark all the child files.
619
620 The commands operate on files that are on the same state.
621 This command is intended to make it easy to deselect all files
622 that share the same state."
623   (interactive "P")
624   (if arg
625       (ewoc-map
626        (lambda (filearg)
627          (when (vc-dir-fileinfo->marked filearg)
628            (setf (vc-dir-fileinfo->marked filearg) nil)
629            t))
630        vc-ewoc)
631     (let* ((crt (ewoc-locate vc-ewoc))
632            (data (ewoc-data crt)))
633       (if (vc-dir-fileinfo->directory data)
634           ;; It's a directory, unmark child files.
635           (while (setq crt (ewoc-next vc-ewoc crt))
636             (let ((crt-data (ewoc-data crt)))
637               (unless (vc-dir-fileinfo->directory crt-data)
638                 (setf (vc-dir-fileinfo->marked crt-data) nil)
639                 (ewoc-invalidate vc-ewoc crt))))
640         ;; It's a file
641         (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
642           (ewoc-map
643            (lambda (filearg)
644              (when (and (vc-dir-fileinfo->marked filearg)
645                         (eq (vc-dir-fileinfo->state filearg) crt-state))
646                (setf (vc-dir-fileinfo->marked filearg) nil)
647                t))
648            vc-ewoc))))))
649
650 (defun vc-dir-toggle-mark-file ()
651   (let* ((crt (ewoc-locate vc-ewoc))
652          (file (ewoc-data crt)))
653     (if (vc-dir-fileinfo->marked file)
654         (vc-dir-unmark-file)
655       (vc-dir-mark-file))))
656
657 (defun vc-dir-toggle-mark (e)
658   (interactive "e")
659   (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
660
661 (defun vc-dir-delete-file ()
662   "Delete the marked files, or the current file if no marks."
663   (interactive)
664   (mapc 'vc-delete-file (or (vc-dir-marked-files)
665                             (list (vc-dir-current-file)))))
666
667 (defun vc-dir-find-file ()
668   "Find the file on the current line."
669   (interactive)
670   (find-file (vc-dir-current-file)))
671
672 (defun vc-dir-find-file-other-window ()
673   "Find the file on the current line, in another window."
674   (interactive)
675   (find-file-other-window (vc-dir-current-file)))
676
677 (defun vc-dir-current-file ()
678   (let ((node (ewoc-locate vc-ewoc)))
679     (unless node
680       (error "No file available"))
681     (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
682
683 (defun vc-dir-marked-files ()
684   "Return the list of marked files."
685   (mapcar
686    (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
687    (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
688
689 (defun vc-dir-marked-only-files-and-states ()
690   "Return the list of conses (FILE . STATE) for the marked files.
691 For marked directories return the corresponding conses for the
692 child files."
693   (let ((crt (ewoc-nth vc-ewoc 0))
694         result)
695     (while crt
696       (let ((crt-data (ewoc-data crt)))
697         (if (vc-dir-fileinfo->marked crt-data)
698             ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
699             (if (vc-dir-fileinfo->directory crt-data)
700                 (let* ((dir (vc-dir-fileinfo->directory crt-data))
701                        (dirlen (length dir))
702                        data)
703                   (while
704                       (and (setq crt (ewoc-next vc-ewoc crt))
705                            (vc-string-prefix-p dir
706                                                (progn
707                                                  (setq data (ewoc-data crt))
708                                                  (vc-dir-node-directory crt))))
709                     (unless (vc-dir-fileinfo->directory data)
710                       (push
711                        (cons (expand-file-name (vc-dir-fileinfo->name data))
712                              (vc-dir-fileinfo->state data))
713                        result))))
714               (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
715                           (vc-dir-fileinfo->state crt-data))
716                     result)
717               (setq crt (ewoc-next vc-ewoc crt)))
718           (setq crt (ewoc-next vc-ewoc crt)))))
719     result))
720
721 (defun vc-dir-child-files-and-states ()
722   "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
723 If it is a file, return the corresponding cons for the file itself."
724   (let* ((crt (ewoc-locate vc-ewoc))
725          (crt-data (ewoc-data crt))
726          result)
727     (if (vc-dir-fileinfo->directory crt-data)
728         (let* ((dir (vc-dir-fileinfo->directory crt-data))
729                (dirlen (length dir))
730                data)
731           (while
732               (and (setq crt (ewoc-next vc-ewoc crt))
733                    (vc-string-prefix-p dir (progn
734                                              (setq data (ewoc-data crt))
735                                              (vc-dir-node-directory crt))))
736             (unless (vc-dir-fileinfo->directory data)
737               (push
738                (cons (expand-file-name (vc-dir-fileinfo->name data))
739                      (vc-dir-fileinfo->state data))
740                result))))
741       (push
742        (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
743              (vc-dir-fileinfo->state crt-data)) result))
744     result))
745
746 (defun vc-dir-resynch-file (&optional fname)
747   "Update the entries for FILE in any directory buffers that list it."
748   (let ((file (or fname (expand-file-name buffer-file-name))))
749     (if (file-directory-p file)
750         ;; FIXME: Maybe this should never happen?
751         ;; FIXME: But it is useful to update the state of a directory
752         ;; (more precisely the files in the directory) after some VC
753         ;; operations.
754         nil
755       (let ((found-vc-dir-buf nil))
756         (save-excursion
757           (dolist (status-buf (buffer-list))
758             (set-buffer status-buf)
759             ;; look for a vc-dir buffer that might show this file.
760             (when (derived-mode-p 'vc-dir-mode)
761               (setq found-vc-dir-buf t)
762               (let ((ddir (expand-file-name default-directory)))
763                 (when (vc-string-prefix-p ddir file)
764                   (let*
765                       ;; FIXME: Any reason we don't use file-relative-name?
766                       ((file-short (substring file (length ddir)))
767                        (state (vc-call-backend vc-dir-backend 'state file))
768                        (extra (vc-call-backend vc-dir-backend
769                                                'status-fileinfo-extra file))
770                        (entry
771                         (list file-short state extra)))
772                     (vc-dir-update (list entry) status-buf))))))
773           ;; We didn't find any vc-dir buffers, remove the hook, it is
774           ;; not needed.
775           (unless found-vc-dir-buf
776             (remove-hook 'after-save-hook 'vc-dir-resynch-file)))))))
777
778 (defvar use-vc-backend)  ;; dynamically bound
779
780 (define-derived-mode vc-dir-mode special-mode "VC dir"
781   "Major mode for dispatcher directory buffers.
782 Marking/Unmarking key bindings and actions:
783 m - marks a file/directory or if the region is active, mark all the files
784      in region.
785     Restrictions: - a file cannot be marked if any parent directory is marked
786                   - a directory cannot be marked if any child file or
787                     directory is marked
788 u - marks a file/directory or if the region is active, unmark all the files
789      in region.
790 M - if the cursor is on a file: mark all the files with the same state as
791       the current file
792   - if the cursor is on a directory: mark all child files
793   - with a prefix argument: mark all files
794 U - if the cursor is on a file: unmark all the files with the same state
795       as the current file
796   - if the cursor is on a directory: unmark all child files
797   - with a prefix argument: unmark all files
798
799
800 \\{vc-dir-mode-map}"
801   (set (make-local-variable 'vc-dir-backend) use-vc-backend)
802   (setq buffer-read-only t)
803   (when (boundp 'tool-bar-map)
804     (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
805   (let ((buffer-read-only nil))
806     (erase-buffer)
807     (set (make-local-variable 'vc-dir-process-buffer) nil)
808     (set (make-local-variable 'vc-ewoc)
809          (ewoc-create #'vc-dir-status-printer
810                       (vc-dir-headers vc-dir-backend default-directory)))
811     (set (make-local-variable 'revert-buffer-function)
812          'vc-dir-revert-buffer-function)
813     (set (make-local-variable 'list-buffers-directory)
814          (expand-file-name default-directory))
815     (add-hook 'after-save-hook 'vc-dir-resynch-file)
816     ;; Make sure that if the directory buffer is killed, the update
817     ;; process running in the background is also killed.
818     (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
819     (vc-dir-refresh)))
820
821 (defun vc-dir-headers (backend dir)
822   "Display the headers in the *VC dir* buffer.
823 It calls the `status-extra-headers' backend method to display backend
824 specific headers."
825   (concat
826    (propertize "VC backend : " 'face 'font-lock-type-face)
827    (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
828    (propertize "Working dir: " 'face 'font-lock-type-face)
829    (propertize (format "%s\n" dir) 'face 'font-lock-variable-name-face)
830    (vc-call-backend backend 'status-extra-headers dir)
831    "\n"))
832
833 (defun vc-dir-refresh-files (files default-state)
834   "Refresh some files in the *VC-dir* buffer."
835   (let ((def-dir default-directory)
836         (backend vc-dir-backend))
837     (vc-set-mode-line-busy-indicator)
838     ;; Call the `dir-status-file' backend function.
839     ;; `dir-status-file' is supposed to be asynchronous.
840     ;; It should compute the results, and then call the function
841     ;; passed as an argument in order to update the vc-dir buffer
842     ;; with the results.
843     (unless (buffer-live-p vc-dir-process-buffer)
844       (setq vc-dir-process-buffer
845             (generate-new-buffer (format " *VC-%s* tmp status" backend))))
846     (lexical-let ((buffer (current-buffer)))
847       (with-current-buffer vc-dir-process-buffer
848         (cd def-dir)
849         (erase-buffer)
850         (vc-call-backend
851          backend 'dir-status-files def-dir files default-state
852          (lambda (entries &optional more-to-come)
853            ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
854            ;; If MORE-TO-COME is true, then more updates will come from
855            ;; the asynchronous process.
856            (with-current-buffer buffer
857              (vc-dir-update entries buffer)
858              (unless more-to-come
859                (setq mode-line-process nil)
860                ;; Remove the ones that haven't been updated at all.
861                ;; Those not-updated are those whose state is nil because the
862                ;; file/dir doesn't exist and isn't versioned.
863                (ewoc-filter vc-ewoc
864                             (lambda (info)
865                               ;; The state for directory entries might
866                               ;; have been changed to 'up-to-date,
867                               ;; reset it, othewise it will be removed when doing 'x'
868                               ;; next time.
869                               ;; FIXME: There should be a more elegant way to do this.
870                               (when (and (vc-dir-fileinfo->directory info)
871                                          (eq (vc-dir-fileinfo->state info)
872                                              'up-to-date))
873                                 (setf (vc-dir-fileinfo->state info) nil))
874
875                               (not (vc-dir-fileinfo->needs-update info))))))))))))
876
877 (defun vc-dir-revert-buffer-function (&optional ignore-auto noconfirm)
878   (vc-dir-refresh))
879
880 (defun vc-dir-refresh ()
881   "Refresh the contents of the *VC-dir* buffer.
882 Throw an error if another update process is in progress."
883   (interactive)
884   (if (vc-dir-busy)
885       (error "Another update process is in progress, cannot run two at a time")
886     (let ((def-dir default-directory)
887           (backend vc-dir-backend))
888       (vc-set-mode-line-busy-indicator)
889       ;; Call the `dir-status' backend function.
890       ;; `dir-status' is supposed to be asynchronous.
891       ;; It should compute the results, and then call the function
892       ;; passed as an argument in order to update the vc-dir buffer
893       ;; with the results.
894
895       ;; Create a buffer that can be used by `dir-status' and call
896       ;; `dir-status' with this buffer as the current buffer.  Use
897       ;; `vc-dir-process-buffer' to remember this buffer, so that
898       ;; it can be used later to kill the update process in case it
899       ;; takes too long.
900       (unless (buffer-live-p vc-dir-process-buffer)
901         (setq vc-dir-process-buffer
902               (generate-new-buffer (format " *VC-%s* tmp status" backend))))
903       ;; set the needs-update flag on all entries
904       (ewoc-map (lambda (info) (setf (vc-dir-fileinfo->needs-update info) t) nil)
905                 vc-ewoc)
906       (lexical-let ((buffer (current-buffer)))
907         (with-current-buffer vc-dir-process-buffer
908           (cd def-dir)
909           (erase-buffer)
910           (vc-call-backend
911            backend 'dir-status def-dir
912            (lambda (entries &optional more-to-come)
913              ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
914              ;; If MORE-TO-COME is true, then more updates will come from
915              ;; the asynchronous process.
916              (with-current-buffer buffer
917                (vc-dir-update entries buffer)
918                (unless more-to-come
919                  (let ((remaining
920                         (ewoc-collect
921                          vc-ewoc 'vc-dir-fileinfo->needs-update)))
922                    (if remaining
923                        (vc-dir-refresh-files
924                         (mapcar 'vc-dir-fileinfo->name remaining)
925                         'up-to-date)
926                      (setq mode-line-process nil))))))))))))
927
928 (defun vc-dir-show-fileentry (file)
929   "Insert an entry for a specific file into the current *VC-dir* listing.
930 This is typically used if the file is up-to-date (or has been added
931 outside of VC) and one wants to do some operation on it."
932   (interactive "fShow file: ")
933   (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
934
935 (defun vc-dir-hide-up-to-date ()
936   "Hide up-to-date items from display."
937   (interactive)
938   (let ((crt (ewoc-nth vc-ewoc -1))
939         (first (ewoc-nth vc-ewoc 0)))
940     ;; Go over from the last item to the first and remove the
941     ;; up-to-date files and directories with no child files.
942     (while (not (eq crt first))
943       (let* ((data (ewoc-data crt))
944              (dir (vc-dir-fileinfo->directory data))
945              (next (ewoc-next vc-ewoc crt))
946              (prev (ewoc-prev vc-ewoc crt))
947              ;; ewoc-delete does not work without this...
948              (inhibit-read-only t))
949           (when (or
950                  ;; Remove directories with no child files.
951                  (and dir
952                       (or
953                        ;; Nothing follows this directory.
954                        (not next)
955                        ;; Next item is a directory.
956                        (vc-dir-fileinfo->directory (ewoc-data next))))
957                  ;; Remove files in the up-to-date state.
958                  (eq (vc-dir-fileinfo->state data) 'up-to-date))
959             (ewoc-delete vc-ewoc crt))
960           (setq crt prev)))))
961
962 (defun vc-dir-status-printer (fileentry)
963   (vc-call-backend vc-dir-backend 'status-printer fileentry))
964
965 (defun vc-dir-deduce-fileset (&optional state-model-only-files)
966   (let ((marked (vc-dir-marked-files))
967         files
968         only-files-list
969         state
970         model)
971     (if marked
972         (progn
973           (setq files marked)
974           (when state-model-only-files
975             (setq only-files-list (vc-dir-marked-only-files-and-states))))
976       (let ((crt (vc-dir-current-file)))
977         (setq files (list crt))
978         (when state-model-only-files
979           (setq only-files-list (vc-dir-child-files-and-states)))))
980
981     (when state-model-only-files
982       (setq state (cdar only-files-list))
983       ;; Check that all files are in a consistent state, since we use that
984       ;; state to decide which operation to perform.
985       (dolist (crt (cdr only-files-list))
986         (unless (vc-compatible-state (cdr crt) state)
987           (error "%s:%s clashes with %s:%s"
988                  (car crt) (cdr crt) (caar only-files-list) state)))
989       (setq only-files-list (mapcar 'car only-files-list))
990       (when (and state (not (eq state 'unregistered)))
991         (setq model (vc-checkout-model vc-dir-backend only-files-list))))
992     (list vc-dir-backend files only-files-list state model)))
993
994 ;;;###autoload
995 (defun vc-dir (dir &optional backend)
996   "Show the VC status for DIR.
997 Optional second argument BACKEND specifies the VC backend to use.
998 Interactively, a prefix argument means to ask for the backend."
999   (interactive
1000    (list
1001     (read-file-name "VC status for directory: "
1002                     default-directory default-directory t
1003                     nil #'file-directory-p)
1004     (if current-prefix-arg
1005         (intern
1006          (completing-read
1007           "Use VC backend: "
1008           (mapcar (lambda (b) (list (symbol-name b)))
1009                   vc-handled-backends)
1010           nil t nil nil)))))
1011   (unless backend
1012     (setq backend (vc-responsible-backend dir)))
1013   (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir backend))
1014   (if (derived-mode-p 'vc-dir-mode)
1015       (vc-dir-refresh)
1016     ;; FIXME: find a better way to pass the backend to `vc-dir-mode'.
1017     (let ((use-vc-backend backend))
1018       (vc-dir-mode))))
1019
1020 (defun vc-default-status-extra-headers (backend dir)
1021   ;; Be loud by default to remind people to add code to display
1022   ;; backend specific headers.
1023   ;; XXX: change this to return nil before the release.
1024   (concat
1025    (propertize "Extra      : " 'face 'font-lock-type-face)
1026    (propertize "Please add backend specific headers here.  It's easy!"
1027                'face 'font-lock-warning-face)))
1028
1029 (defun vc-default-status-printer (backend fileentry)
1030   "Pretty print FILEENTRY."
1031   ;; If you change the layout here, change vc-dir-move-to-goal-column.
1032   (let* ((isdir (vc-dir-fileinfo->directory fileentry))
1033         (state (if isdir "" (vc-dir-fileinfo->state fileentry)))
1034         (filename (vc-dir-fileinfo->name fileentry)))
1035     (insert
1036      (propertize
1037       (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1038       'face 'font-lock-type-face)
1039      "   "
1040      (propertize
1041       (format "%-20s" state)
1042       'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1043                   ((memq state '(missing conflict)) 'font-lock-warning-face)
1044                   (t 'font-lock-variable-name-face))
1045       'mouse-face 'highlight)
1046      " "
1047      (propertize
1048       (format "%s" filename)
1049       'face
1050       (if isdir 'font-lock-comment-delimiter-face 'font-lock-function-name-face)
1051       'help-echo
1052       (if isdir
1053           "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
1054         "File\nmouse-3: Pop-up menu")
1055       'mouse-face 'highlight))))
1056
1057 (defun vc-default-extra-status-menu (backend)
1058   nil)
1059
1060 (defun vc-default-status-fileinfo-extra (backend file)
1061   "Default absence of extra information returned for a file."
1062   nil)
1063
1064 (provide 'vc-dir)
1065
1066 ;; arch-tag: 0274a2e3-e8e9-4b1a-a73c-e8b9129d5d15
1067 ;;; vc-dir.el ends here