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