(w32font_text_extents): Don't count overhang as part of width.
[bpt/emacs.git] / lisp / vc-dir.el
CommitLineData
74d0991f
DN
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:
a1fc8acb 30;;
74d0991f
DN
31;; Alexandre Julliard <julliard@winehq.org>
32;; Stefan Monnier <monnier@iro.umontreal.ca>
33;; Tom Tromey <tromey@redhat.com>
34
35;;; Commentary:
a1fc8acb 36;;
74d0991f
DN
37
38;;; Todo: see vc.el.
39
40(require 'vc-hooks)
41(require 'vc)
50925e72 42(require 'tool-bar)
74d0991f
DN
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'.
51See `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
c4c0a44b 66 ;; For storing backend specific information.
74d0991f
DN
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
74d0991f 74(defvar vc-ewoc nil)
c4c0a44b 75
74d0991f
DN
76(defvar vc-dir-process-buffer nil
77 "The buffer used for the asynchronous call that computes status.")
78
c4c0a44b
DN
79(defvar vc-dir-backend nil
80 "The backend used by the current *vc-dir* buffer.")
81
74d0991f
DN
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
c4c0a44b 89(defun vc-dir-prepare-status-buffer (bname dir backend &optional create-new)
74d0991f
DN
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)
c4c0a44b
DN
98 (when (and (derived-mode-p 'vc-dir-mode)
99 (eq vc-dir-backend backend)
74d0991f
DN
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]
c4c0a44b 122 '(menu-item "Refresh" revert-buffer
74d0991f
DN
123 :enable (not (vc-dir-busy))
124 :help "Refresh the contents of the directory buffer"))
c4c0a44b
DN
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"))
74d0991f
DN
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"))
c4c0a44b
DN
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"))
74d0991f
DN
196 map)
197 "Menu for dispatcher status")
198
c4c0a44b
DN
199;; VC backends can use this to add mode-specific menu items to
200;; vc-dir-menu-map.
74d0991f
DN
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
c4c0a44b
DN
205 (when (derived-mode-p 'vc-dir-mode)
206 (vc-call-backend vc-dir-backend 'extra-status-menu))))
74d0991f
DN
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)
c4c0a44b
DN
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
74d0991f
DN
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)
74d0991f
DN
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)
c4c0a44b 250 (define-key map "x" 'vc-dir-hide-up-to-date)
74d0991f
DN
251
252 ;; Hook up the menu.
253 (define-key map [menu-bar vc-dir-mode]
254 `(menu-item
c4c0a44b
DN
255 ;; VC backends can use this to add mode-specific menu items to
256 ;; vc-dir-menu-map.
74d0991f
DN
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'.
263If `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)
c4c0a44b 292 (tool-bar-local-item-from-menu 'revert-buffer "refresh"
74d0991f
DN
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.
314If 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)))
a1fc8acb 342
74d0991f
DN
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)
a1fc8acb 362 (setq entries (cdr entries))
74d0991f
DN
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.
422If 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.
430If 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
74d0991f
DN
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.
532If the region is active, mark all the files in the region.
533Otherwise mark the file on the current line and move to the next
534line."
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.
540With a prefix argument mark all files.
541If the current entry is a directory, mark all child files.
542
543The commands operate on files that are on the same state.
544This command is intended to make it easy to select all files that
545share 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.
597If the region is active, unmark all the files in the region.
598Otherwise mark the file on the current line and move to the next
599line."
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.
617With a prefix argument unmark all files.
618If the current entry is a directory, unmark all the child files.
619
620The commands operate on files that are on the same state.
621This command is intended to make it easy to deselect all files
622that 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
2913a58d
DN
689(defun vc-dir-marked-only-files-and-states ()
690 "Return the list of conses (FILE . STATE) for the marked files.
691For marked directories return the corresponding conses for the
692child files."
74d0991f
DN
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)
2913a58d 698 ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
74d0991f
DN
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)
a1fc8acb 710 (push
2913a58d
DN
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)
74d0991f
DN
717 (setq crt (ewoc-next vc-ewoc crt)))
718 (setq crt (ewoc-next vc-ewoc crt)))))
719 result))
720
2913a58d
DN
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.
723If it is a file, return the corresponding cons for the file itself."
74d0991f
DN
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)
a1fc8acb 737 (push
2913a58d
DN
738 (cons (expand-file-name (vc-dir-fileinfo->name data))
739 (vc-dir-fileinfo->state data))
740 result))))
a1fc8acb 741 (push
2913a58d
DN
742 (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
743 (vc-dir-fileinfo->state crt-data)) result))
74d0991f
DN
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)
a1fc8acb 750 ;; FIXME: Maybe this should never happen?
74d0991f
DN
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)))
c4c0a44b
DN
767 (state (vc-call-backend vc-dir-backend 'state file))
768 (extra (vc-call-backend vc-dir-backend
769 'status-fileinfo-extra file))
74d0991f
DN
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
c4c0a44b
DN
778(defvar use-vc-backend) ;; dynamically bound
779
780(define-derived-mode vc-dir-mode special-mode "VC dir"
74d0991f
DN
781 "Major mode for dispatcher directory buffers.
782Marking/Unmarking key bindings and actions:
783m - 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
788u - marks a file/directory or if the region is active, unmark all the files
789 in region.
790M - 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
794U - 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}"
c4c0a44b 801 (set (make-local-variable 'vc-dir-backend) use-vc-backend)
74d0991f 802 (setq buffer-read-only t)
c4c0a44b
DN
803 (when (boundp 'tool-bar-map)
804 (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
74d0991f
DN
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)
c4c0a44b
DN
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)
74d0991f
DN
813 (add-hook 'after-save-hook 'vc-dir-resynch-file)
814 ;; Make sure that if the directory buffer is killed, the update
815 ;; process running in the background is also killed.
816 (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
c4c0a44b 817 (vc-dir-refresh)))
74d0991f
DN
818
819(defun vc-dir-headers (backend dir)
820 "Display the headers in the *VC dir* buffer.
821It calls the `status-extra-headers' backend method to display backend
822specific headers."
823 (concat
824 (propertize "VC backend : " 'face 'font-lock-type-face)
825 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
826 (propertize "Working dir: " 'face 'font-lock-type-face)
827 (propertize (format "%s\n" dir) 'face 'font-lock-variable-name-face)
828 (vc-call-backend backend 'status-extra-headers dir)
829 "\n"))
830
831(defun vc-dir-refresh-files (files default-state)
832 "Refresh some files in the *VC-dir* buffer."
833 (let ((def-dir default-directory)
834 (backend vc-dir-backend))
835 (vc-set-mode-line-busy-indicator)
836 ;; Call the `dir-status-file' backend function.
837 ;; `dir-status-file' is supposed to be asynchronous.
838 ;; It should compute the results, and then call the function
839 ;; passed as an argument in order to update the vc-dir buffer
840 ;; with the results.
841 (unless (buffer-live-p vc-dir-process-buffer)
842 (setq vc-dir-process-buffer
843 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
844 (lexical-let ((buffer (current-buffer)))
845 (with-current-buffer vc-dir-process-buffer
846 (cd def-dir)
847 (erase-buffer)
848 (vc-call-backend
849 backend 'dir-status-files def-dir files default-state
850 (lambda (entries &optional more-to-come)
851 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
852 ;; If MORE-TO-COME is true, then more updates will come from
853 ;; the asynchronous process.
854 (with-current-buffer buffer
855 (vc-dir-update entries buffer)
856 (unless more-to-come
857 (setq mode-line-process nil)
858 ;; Remove the ones that haven't been updated at all.
859 ;; Those not-updated are those whose state is nil because the
860 ;; file/dir doesn't exist and isn't versioned.
861 (ewoc-filter vc-ewoc
862 (lambda (info)
863 ;; The state for directory entries might
864 ;; have been changed to 'up-to-date,
865 ;; reset it, othewise it will be removed when doing 'x'
866 ;; next time.
867 ;; FIXME: There should be a more elegant way to do this.
868 (when (and (vc-dir-fileinfo->directory info)
869 (eq (vc-dir-fileinfo->state info)
870 'up-to-date))
871 (setf (vc-dir-fileinfo->state info) nil))
872
873 (not (vc-dir-fileinfo->needs-update info))))))))))))
874
c4c0a44b
DN
875(defun vc-dir-revert-buffer-function (&optional ignore-auto noconfirm)
876 (vc-dir-refresh))
877
74d0991f
DN
878(defun vc-dir-refresh ()
879 "Refresh the contents of the *VC-dir* buffer.
880Throw an error if another update process is in progress."
881 (interactive)
882 (if (vc-dir-busy)
883 (error "Another update process is in progress, cannot run two at a time")
884 (let ((def-dir default-directory)
885 (backend vc-dir-backend))
886 (vc-set-mode-line-busy-indicator)
887 ;; Call the `dir-status' backend function.
888 ;; `dir-status' is supposed to be asynchronous.
889 ;; It should compute the results, and then call the function
890 ;; passed as an argument in order to update the vc-dir buffer
891 ;; with the results.
892
893 ;; Create a buffer that can be used by `dir-status' and call
894 ;; `dir-status' with this buffer as the current buffer. Use
895 ;; `vc-dir-process-buffer' to remember this buffer, so that
896 ;; it can be used later to kill the update process in case it
897 ;; takes too long.
898 (unless (buffer-live-p vc-dir-process-buffer)
899 (setq vc-dir-process-buffer
900 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
901 ;; set the needs-update flag on all entries
902 (ewoc-map (lambda (info) (setf (vc-dir-fileinfo->needs-update info) t) nil)
903 vc-ewoc)
904 (lexical-let ((buffer (current-buffer)))
905 (with-current-buffer vc-dir-process-buffer
906 (cd def-dir)
907 (erase-buffer)
908 (vc-call-backend
909 backend 'dir-status def-dir
910 (lambda (entries &optional more-to-come)
911 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
912 ;; If MORE-TO-COME is true, then more updates will come from
913 ;; the asynchronous process.
914 (with-current-buffer buffer
915 (vc-dir-update entries buffer)
916 (unless more-to-come
917 (let ((remaining
918 (ewoc-collect
919 vc-ewoc 'vc-dir-fileinfo->needs-update)))
920 (if remaining
921 (vc-dir-refresh-files
922 (mapcar 'vc-dir-fileinfo->name remaining)
923 'up-to-date)
924 (setq mode-line-process nil))))))))))))
925
926(defun vc-dir-show-fileentry (file)
927 "Insert an entry for a specific file into the current *VC-dir* listing.
928This is typically used if the file is up-to-date (or has been added
929outside of VC) and one wants to do some operation on it."
930 (interactive "fShow file: ")
931 (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
932
933(defun vc-dir-hide-up-to-date ()
934 "Hide up-to-date items from display."
935 (interactive)
f731e2f9
DN
936 (let ((crt (ewoc-nth vc-ewoc -1))
937 (first (ewoc-nth vc-ewoc 0)))
938 ;; Go over from the last item to the first and remove the
939 ;; up-to-date files and directories with no child files.
940 (while (not (eq crt first))
941 (let* ((data (ewoc-data crt))
942 (dir (vc-dir-fileinfo->directory data))
943 (next (ewoc-next vc-ewoc crt))
944 (prev (ewoc-prev vc-ewoc crt))
945 ;; ewoc-delete does not work without this...
946 (inhibit-read-only t))
947 (when (or
948 ;; Remove directories with no child files.
949 (and dir
950 (or
951 ;; Nothing follows this directory.
952 (not next)
953 ;; Next item is a directory.
954 (vc-dir-fileinfo->directory (ewoc-data next))))
955 ;; Remove files in the up-to-date state.
956 (eq (vc-dir-fileinfo->state data) 'up-to-date))
957 (ewoc-delete vc-ewoc crt))
958 (setq crt prev)))))
74d0991f 959
c4c0a44b 960(defun vc-dir-status-printer (fileentry)
74d0991f
DN
961 (vc-call-backend vc-dir-backend 'status-printer fileentry))
962
2913a58d
DN
963(defun vc-dir-deduce-fileset (&optional state-model-only-files)
964 (let ((marked (vc-dir-marked-files))
965 files
966 only-files-list
967 state
968 model)
969 (if marked
970 (progn
971 (setq files marked)
972 (when state-model-only-files
973 (setq only-files-list (vc-dir-marked-only-files-and-states))))
974 (let ((crt (vc-dir-current-file)))
975 (setq files (list crt))
a1fc8acb 976 (when state-model-only-files
2913a58d
DN
977 (setq only-files-list (vc-dir-child-files-and-states)))))
978
979 (when state-model-only-files
980 (setq state (cdar only-files-list))
981 ;; Check that all files are in a consistent state, since we use that
982 ;; state to decide which operation to perform.
983 (dolist (crt (cdr only-files-list))
984 (unless (vc-compatible-state (cdr crt) state)
985 (error "%s:%s clashes with %s:%s"
986 (car crt) (cdr crt) (caar only-files-list) state)))
987 (setq only-files-list (mapcar 'car only-files-list))
988 (when (and state (not (eq state 'unregistered)))
989 (setq model (vc-checkout-model vc-dir-backend only-files-list))))
990 (list vc-dir-backend files only-files-list state model)))
991
74d0991f 992;;;###autoload
b861de91 993(defun vc-dir (dir &optional backend)
c4c0a44b 994 "Show the VC status for DIR.
b861de91
AS
995Optional second argument BACKEND specifies the VC backend to use.
996Interactively, a prefix argument means to ask for the backend."
c4c0a44b
DN
997 (interactive
998 (list
999 (read-file-name "VC status for directory: "
a1fc8acb
JB
1000 default-directory default-directory t
1001 nil #'file-directory-p)
c4c0a44b
DN
1002 (if current-prefix-arg
1003 (intern
1004 (completing-read
1005 "Use VC backend: "
b861de91
AS
1006 (mapcar (lambda (b) (list (symbol-name b)))
1007 vc-handled-backends)
1008 nil t nil nil)))))
1009 (unless backend
1010 (setq backend (vc-responsible-backend dir)))
c4c0a44b
DN
1011 (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir backend))
1012 (if (derived-mode-p 'vc-dir-mode)
74d0991f 1013 (vc-dir-refresh)
c4c0a44b
DN
1014 ;; FIXME: find a better way to pass the backend to `vc-dir-mode'.
1015 (let ((use-vc-backend backend))
1016 (vc-dir-mode))))
74d0991f
DN
1017
1018(defun vc-default-status-extra-headers (backend dir)
1019 ;; Be loud by default to remind people to add code to display
1020 ;; backend specific headers.
1021 ;; XXX: change this to return nil before the release.
1022 (concat
1023 (propertize "Extra : " 'face 'font-lock-type-face)
1024 (propertize "Please add backend specific headers here. It's easy!"
1025 'face 'font-lock-warning-face)))
1026
1027(defun vc-default-status-printer (backend fileentry)
1028 "Pretty print FILEENTRY."
1029 ;; If you change the layout here, change vc-dir-move-to-goal-column.
1030 (let* ((isdir (vc-dir-fileinfo->directory fileentry))
c4c0a44b 1031 (state (if isdir "" (vc-dir-fileinfo->state fileentry)))
74d0991f 1032 (filename (vc-dir-fileinfo->name fileentry)))
74d0991f
DN
1033 (insert
1034 (propertize
1035 (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1036 'face 'font-lock-type-face)
1037 " "
1038 (propertize
1039 (format "%-20s" state)
1040 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1041 ((memq state '(missing conflict)) 'font-lock-warning-face)
1042 (t 'font-lock-variable-name-face))
1043 'mouse-face 'highlight)
1044 " "
1045 (propertize
1046 (format "%s" filename)
c4c0a44b
DN
1047 'face
1048 (if isdir 'font-lock-comment-delimiter-face 'font-lock-function-name-face)
1049 'help-echo
a1fc8acb
JB
1050 (if isdir
1051 "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
c4c0a44b 1052 "File\nmouse-3: Pop-up menu")
74d0991f
DN
1053 'mouse-face 'highlight))))
1054
1055(defun vc-default-extra-status-menu (backend)
1056 nil)
1057
1058(defun vc-default-status-fileinfo-extra (backend file)
1059 "Default absence of extra information returned for a file."
1060 nil)
1061
1062(provide 'vc-dir)
1063
81c5ac7e 1064;; arch-tag: 0274a2e3-e8e9-4b1a-a73c-e8b9129d5d15
74d0991f 1065;;; vc-dir.el ends here