vc-dir.el 53.6 KB
Newer Older
Stefan Monnier's avatar
Stefan Monnier committed
1
;;; vc-dir.el --- Directory status display under VC  -*- lexical-binding: t -*-
Dan Nicolaescu's avatar
Dan Nicolaescu committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2007-2019 Free Software Foundation, Inc.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
4

Paul Eggert's avatar
Paul Eggert committed
5
;; Author: Dan Nicolaescu <dann@ics.uci.edu>
6
;; Keywords: vc tools
7
;; Package: vc
Dan Nicolaescu's avatar
Dan Nicolaescu committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
23 24 25 26 27 28 29

;;; Credits:

;; The original VC directory status implementation was based on dired.
;; This implementation was inspired by PCL-CVS.
;; Many people contributed comments, ideas and code to this
;; implementation.  These include:
30
;;
Dan Nicolaescu's avatar
Dan Nicolaescu committed
31 32 33 34 35
;;   Alexandre Julliard  <julliard@winehq.org>
;;   Stefan Monnier  <monnier@iro.umontreal.ca>
;;   Tom Tromey  <tromey@redhat.com>

;;; Commentary:
36
;;
Dan Nicolaescu's avatar
Dan Nicolaescu committed
37 38 39 40 41

;;; Todo:  see vc.el.

(require 'vc-hooks)
(require 'vc)
42
(require 'tool-bar)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
43
(require 'ewoc)
44
(require 'seq)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
45 46

;;; Code:
47
(eval-when-compile (require 'cl-lib))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
48

49 50
(declare-function fileloop-continue "fileloop")

Dan Nicolaescu's avatar
Dan Nicolaescu committed
51 52 53 54 55 56 57 58
(defcustom vc-dir-mode-hook nil
  "Normal hook run by `vc-dir-mode'.
See `run-hooks'."
  :type 'hook
  :group 'vc)

;; Used to store information for the files displayed in the directory buffer.
;; Each item displayed corresponds to one of these defstructs.
59
(cl-defstruct (vc-dir-fileinfo
Dan Nicolaescu's avatar
Dan Nicolaescu committed
60 61 62 63 64 65 66 67
            (:copier nil)
            (:type list)            ;So we can use `member' on lists of FIs.
            (:constructor
             ;; We could define it as an alias for `list'.
	     vc-dir-create-fileinfo (name state &optional extra marked directory))
            (:conc-name vc-dir-fileinfo->))
  name                                  ;Keep it as first, for `member'.
  state
Dan Nicolaescu's avatar
Dan Nicolaescu committed
68
  ;; For storing backend specific information.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
69 70 71 72 73 74 75 76
  extra
  marked
  ;; To keep track of not updated files during a global refresh
  needs-update
  ;; To distinguish files and directories.
  directory)

(defvar vc-ewoc nil)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
77

Dan Nicolaescu's avatar
Dan Nicolaescu committed
78 79 80
(defvar vc-dir-process-buffer nil
  "The buffer used for the asynchronous call that computes status.")

Dan Nicolaescu's avatar
Dan Nicolaescu committed
81 82 83
(defvar vc-dir-backend nil
  "The backend used by the current *vc-dir* buffer.")

Dan Nicolaescu's avatar
Dan Nicolaescu committed
84 85 86 87
(defun vc-dir-move-to-goal-column ()
  ;; Used to keep the cursor on the file name column.
  (beginning-of-line)
  (unless (eolp)
88
    ;; Must be in sync with vc-default-dir-printer.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
89 90
    (forward-char 25)))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
91
(defun vc-dir-prepare-status-buffer (bname dir backend &optional create-new)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
92
  "Find a buffer named BNAME showing DIR, or create a new one."
93
  (setq dir (file-name-as-directory (expand-file-name dir)))
94 95 96
  (let* ;; Look for another buffer name BNAME visiting the same directory.
      ((buf (save-excursion
              (unless create-new
97
                (cl-dolist (buffer vc-dir-buffers)
98 99 100 101 102
                  (when (buffer-live-p buffer)
                    (set-buffer buffer)
                    (when (and (derived-mode-p 'vc-dir-mode)
                               (eq vc-dir-backend backend)
                               (string= default-directory dir))
103
                      (cl-return buffer))))))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
104 105
    (or buf
        ;; Create a new buffer named BNAME.
Juanma Barranquero's avatar
Juanma Barranquero committed
106 107 108
	;; We pass a filename to create-file-buffer because it is what
	;; the function expects, and also what uniquify needs (if active)
        (with-current-buffer (create-file-buffer (expand-file-name bname dir))
109
          (setq default-directory dir)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
110 111 112 113 114 115 116
          (vc-setup-buffer (current-buffer))
          ;; Reset the vc-parent-buffer-name so that it does not appear
          ;; in the mode-line.
          (setq vc-parent-buffer-name nil)
          (current-buffer)))))

(defvar vc-dir-menu-map
Glenn Morris's avatar
Glenn Morris committed
117
  (let ((map (make-sparse-keymap "VC-Dir")))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
118 119 120 121 122 123 124 125
    (define-key map [quit]
      '(menu-item "Quit" quit-window
		  :help "Quit"))
    (define-key map [kill]
      '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
		  :enable (vc-dir-busy)
		  :help "Kill the command that updates the directory buffer"))
    (define-key map [refresh]
Dan Nicolaescu's avatar
Dan Nicolaescu committed
126
      '(menu-item "Refresh" revert-buffer
Dan Nicolaescu's avatar
Dan Nicolaescu committed
127 128
		  :enable (not (vc-dir-busy))
		  :help "Refresh the contents of the directory buffer"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
129
    (define-key map [remup]
130
      '(menu-item "Hide Up-to-date" vc-dir-hide-up-to-date
Dan Nicolaescu's avatar
Dan Nicolaescu committed
131
		  :help "Hide up-to-date items from display"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
132 133 134
    ;; Movement.
    (define-key map [sepmv] '("--"))
    (define-key map [next-line]
135
      '(menu-item "Next Line" vc-dir-next-line
Dan Nicolaescu's avatar
Dan Nicolaescu committed
136 137
		  :help "Go to the next line" :keys "n"))
    (define-key map [previous-line]
138
      '(menu-item "Previous Line" vc-dir-previous-line
Dan Nicolaescu's avatar
Dan Nicolaescu committed
139 140 141 142 143 144 145 146
		  :help "Go to the previous line"))
    ;; Marking.
    (define-key map [sepmrk] '("--"))
    (define-key map [unmark-all]
      '(menu-item "Unmark All" vc-dir-unmark-all-files
		  :help "Unmark all files that are in the same state as the current file\
\nWith prefix argument unmark all files"))
    (define-key map [unmark-previous]
147
      '(menu-item "Unmark Previous " vc-dir-unmark-file-up
Dan Nicolaescu's avatar
Dan Nicolaescu committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		  :help "Move to the previous line and unmark the file"))

    (define-key map [mark-all]
      '(menu-item "Mark All" vc-dir-mark-all-files
		  :help "Mark all files that are in the same state as the current file\
\nWith prefix argument mark all files"))
    (define-key map [unmark]
      '(menu-item "Unmark" vc-dir-unmark
		  :help "Unmark the current file or all files in the region"))

    (define-key map [mark]
      '(menu-item "Mark" vc-dir-mark
		  :help "Mark the current file or all files in the region"))

    (define-key map [sepopn] '("--"))
163
    (define-key map [qr]
164
      '(menu-item "Query Replace in Files..." vc-dir-query-replace-regexp
165
		  :help "Replace a string in the marked files"))
166
    (define-key map [se]
167
      '(menu-item "Search Files..." vc-dir-search
168
		  :help "Search a regexp in the marked files"))
169 170 171 172 173 174
    (define-key map [ires]
      '(menu-item "Isearch Regexp Files..." vc-dir-isearch-regexp
		  :help "Incremental search a regexp in the marked files"))
    (define-key map [ise]
      '(menu-item "Isearch Files..." vc-dir-isearch
		  :help "Incremental search a string in the marked files"))
175 176 177
    (define-key map [display]
      '(menu-item "Display in Other Window" vc-dir-display-file
		  :help "Display the file on the current line, in another window"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
178
    (define-key map [open-other]
179
      '(menu-item "Open in Other Window" vc-dir-find-file-other-window
Dan Nicolaescu's avatar
Dan Nicolaescu committed
180 181
		  :help "Find the file on the current line, in another window"))
    (define-key map [open]
182
      '(menu-item "Open File" vc-dir-find-file
Dan Nicolaescu's avatar
Dan Nicolaescu committed
183
		  :help "Find the file on the current line"))
184
    (define-key map [delete]
185 186
      '(menu-item "Delete" vc-dir-clean-files
		  :help "Delete the unregistered marked files"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
187 188 189 190 191 192 193 194 195 196 197 198
    (define-key map [sepvcdet] '("--"))
    ;; FIXME: This needs a key binding.  And maybe a better name
    ;; ("Insert" like PCL-CVS uses does not sound that great either)...
    (define-key map [ins]
      '(menu-item "Show File" vc-dir-show-fileentry
		  :help "Show a file in the VC status listing even though it might be up to date"))
    (define-key map [annotate]
      '(menu-item "Annotate" vc-annotate
		  :help "Display the edit history of the current file using colors"))
    (define-key map [diff]
      '(menu-item "Compare with Base Version" vc-diff
		  :help "Compare file set with the base version"))
199 200 201 202 203 204
    (define-key map [logo]
      '(menu-item "Show Outgoing Log" vc-log-outgoing
		  :help "Show a log of changes that will be sent with a push operation"))
    (define-key map [logi]
      '(menu-item "Show Incoming Log" vc-log-incoming
		  :help "Show a log of changes that will be received with a pull operation"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
205
    (define-key map [log]
206
      '(menu-item "Show History" vc-print-log
207 208 209 210
		  :help "List the change log of the current file set in a window"))
    (define-key map [rlog]
      '(menu-item "Show Top of the Tree History " vc-print-root-log
		  :help "List the change log for the current tree in a window"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
211 212
    ;; VC commands.
    (define-key map [sepvccmd] '("--"))
Glenn Morris's avatar
Glenn Morris committed
213 214 215 216
    (define-key map [push]
      '(menu-item "Push Changes" vc-push
		  :enable (vc-find-backend-function vc-dir-backend 'push)
		  :help "Push the current branch's changes"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
217
    (define-key map [update]
218
      '(menu-item "Update to Latest Version" vc-update
Dan Nicolaescu's avatar
Dan Nicolaescu committed
219 220
		  :help "Update the current fileset's files to their tip revisions"))
    (define-key map [revert]
221
      '(menu-item "Revert to Base Version" vc-revert
Dan Nicolaescu's avatar
Dan Nicolaescu committed
222 223 224 225 226 227 228 229 230
		  :help "Revert working copies of the selected fileset to their repository contents."))
    (define-key map [next-action]
      ;; FIXME: This really really really needs a better name!
      ;; And a key binding too.
      '(menu-item "Check In/Out" vc-next-action
		  :help "Do the next logical version control operation on the current fileset"))
    (define-key map [register]
      '(menu-item "Register" vc-register
		  :help "Register file set into the version control system"))
Xue Fuqiao's avatar
Xue Fuqiao committed
231 232 233
    (define-key map [ignore]
      '(menu-item "Ignore Current File" vc-dir-ignore
		  :help "Ignore the current file under current version control system"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
234
    map)
235
  "Menu for VC dir.")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
236

Dan Nicolaescu's avatar
Dan Nicolaescu committed
237 238
;; VC backends can use this to add mode-specific menu items to
;; vc-dir-menu-map.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
239 240 241 242
(defun vc-dir-menu-map-filter (orig-binding)
  (when (and (symbolp orig-binding) (fboundp orig-binding))
    (setq orig-binding (indirect-function orig-binding)))
  (let ((ext-binding
Dan Nicolaescu's avatar
Dan Nicolaescu committed
243 244
         (when (derived-mode-p 'vc-dir-mode)
	   (vc-call-backend vc-dir-backend 'extra-status-menu))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
245 246 247 248 249 250 251
    (if (null ext-binding)
	orig-binding
      (append orig-binding
	      '("----")
	      ext-binding))))

(defvar vc-dir-mode-map
252
  (let ((map (make-sparse-keymap)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
253 254 255
    ;; VC commands
    (define-key map "v" 'vc-next-action)   ;; C-x v v
    (define-key map "=" 'vc-diff)	   ;; C-x v =
Xue Fuqiao's avatar
Xue Fuqiao committed
256
    (define-key map "D" 'vc-root-diff)	   ;; C-x v D
Dan Nicolaescu's avatar
Dan Nicolaescu committed
257 258
    (define-key map "i" 'vc-register)	   ;; C-x v i
    (define-key map "+" 'vc-update)	   ;; C-x v +
Glenn Morris's avatar
Glenn Morris committed
259 260
    ;; I'd prefer some kind of symmetry with vc-update:
    (define-key map "P" 'vc-push)	   ;; C-x v P
Dan Nicolaescu's avatar
Dan Nicolaescu committed
261
    (define-key map "l" 'vc-print-log)	   ;; C-x v l
262
    (define-key map "L" 'vc-print-root-log) ;; C-x v L
263
    (define-key map "I" 'vc-log-incoming)   ;; C-x v I
264
    (define-key map "O" 'vc-log-outgoing)   ;; C-x v O
Dan Nicolaescu's avatar
Dan Nicolaescu committed
265
    ;; More confusing than helpful, probably
266 267 268
    ;;(define-key map "R" 'vc-revert) ;; u is taken by vc-dir-unmark.
    ;;(define-key map "A" 'vc-annotate) ;; g is taken by revert-buffer
    ;;                                     bound by `special-mode'.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
269 270
    ;; Marking.
    (define-key map "m" 'vc-dir-mark)
271
    (define-key map "d" 'vc-dir-clean-files)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
272 273 274 275 276 277 278 279 280 281
    (define-key map "M" 'vc-dir-mark-all-files)
    (define-key map "u" 'vc-dir-unmark)
    (define-key map "U" 'vc-dir-unmark-all-files)
    (define-key map "\C-?" 'vc-dir-unmark-file-up)
    (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
    ;; Movement.
    (define-key map "n" 'vc-dir-next-line)
    (define-key map " " 'vc-dir-next-line)
    (define-key map "\t" 'vc-dir-next-directory)
    (define-key map "p" 'vc-dir-previous-line)
282
    (define-key map [?\S-\ ] 'vc-dir-previous-line)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
283 284 285 286 287 288 289 290
    (define-key map [backtab] 'vc-dir-previous-directory)
    ;;; Rebind paragraph-movement commands.
    (define-key map "\M-}" 'vc-dir-next-directory)
    (define-key map "\M-{" 'vc-dir-previous-directory)
    (define-key map [C-down] 'vc-dir-next-directory)
    (define-key map [C-up] 'vc-dir-previous-directory)
    ;; The remainder.
    (define-key map "f" 'vc-dir-find-file)
Bob Rogers's avatar
Bob Rogers committed
291
    (define-key map "e" 'vc-dir-find-file) ; dired-mode compatibility
Dan Nicolaescu's avatar
Dan Nicolaescu committed
292 293
    (define-key map "\C-m" 'vc-dir-find-file)
    (define-key map "o" 'vc-dir-find-file-other-window)
294
    (define-key map "\C-o" 'vc-dir-display-file)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
295 296 297
    (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
    (define-key map [down-mouse-3] 'vc-dir-menu)
    (define-key map [mouse-2] 'vc-dir-toggle-mark)
298
    (define-key map [follow-link] 'mouse-face)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
299
    (define-key map "x" 'vc-dir-hide-up-to-date)
300
    (define-key map [?\C-k] 'vc-dir-kill-line)
301
    (define-key map "S" 'vc-dir-search) ;; FIXME: Maybe use A like dired?
302
    (define-key map "Q" 'vc-dir-query-replace-regexp)
303 304
    (define-key map (kbd "M-s a C-s")   'vc-dir-isearch)
    (define-key map (kbd "M-s a M-C-s") 'vc-dir-isearch-regexp)
Xue Fuqiao's avatar
Xue Fuqiao committed
305
    (define-key map "G" 'vc-dir-ignore)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
306

Tom Tromey's avatar
Tom Tromey committed
307 308 309 310 311 312
    (let ((branch-map (make-sparse-keymap)))
      (define-key map "B" branch-map)
      (define-key branch-map "c" 'vc-create-tag)
      (define-key branch-map "l" 'vc-print-branch-log)
      (define-key branch-map "s" 'vc-retrieve-tag))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
313 314 315
    ;; Hook up the menu.
    (define-key map [menu-bar vc-dir-mode]
      `(menu-item
Dan Nicolaescu's avatar
Dan Nicolaescu committed
316 317
	;; VC backends can use this to add mode-specific menu items to
	;; vc-dir-menu-map.
Glenn Morris's avatar
Glenn Morris committed
318
	"VC-Dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
319 320 321
    map)
  "Keymap for directory buffer.")

322
(defmacro vc-dir-at-event (event &rest body)
323 324
  "Evaluate BODY with point located at event-start of EVENT.
If BODY uses EVENT, it should be a variable,
Dan Nicolaescu's avatar
Dan Nicolaescu committed
325
 otherwise it will be evaluated twice."
326
  (let ((posn (make-symbol "vc-dir-at-event-posn")))
327 328 329 330 331 332
    `(save-excursion
       (unless (equal ,event '(tool-bar))
         (let ((,posn (event-start ,event)))
           (set-buffer (window-buffer (posn-window ,posn)))
           (goto-char (posn-point ,posn))))
       ,@body)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
333 334

(defun vc-dir-menu (e)
335
  "Popup the VC dir menu."
Dan Nicolaescu's avatar
Dan Nicolaescu committed
336
  (interactive "e")
337
  (vc-dir-at-event e (popup-menu vc-dir-menu-map e)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
338 339 340

(defvar vc-dir-tool-bar-map
  (let ((map (make-sparse-keymap)))
341 342 343 344 345 346 347 348 349 350
    (tool-bar-local-item-from-menu 'find-file "new" map nil
				   :label "New File" :vert-only t)
    (tool-bar-local-item-from-menu 'menu-find-file-existing "open" map nil
				   :label "Open" :vert-only t)
    (tool-bar-local-item-from-menu 'dired "diropen" map nil
				   :vert-only t)
    (tool-bar-local-item-from-menu 'quit-window "close" map vc-dir-mode-map
				   :vert-only t)
    (tool-bar-local-item-from-menu 'vc-next-action "saveas" map
				   vc-dir-mode-map :label "Commit")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
351
    (tool-bar-local-item-from-menu 'vc-print-log "info"
352 353 354
    				   map vc-dir-mode-map
				   :label "Log")
    (define-key-after map [separator-1] menu-bar-separator)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
355
    (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
356
				   map vc-dir-mode-map
357 358 359 360 361 362 363 364 365 366 367 368 369 370
				   :label "Stop" :vert-only t)
    (tool-bar-local-item-from-menu 'revert-buffer "refresh"
				   map vc-dir-mode-map :vert-only t)
    (define-key-after map [separator-2] menu-bar-separator)
    (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [cut])
				   "cut" map nil :vert-only t)
    (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [copy])
				   "copy" map nil :vert-only t)
    (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [paste])
				   "paste" map nil :vert-only t)
    (define-key-after map [separator-3] menu-bar-separator)
    (tool-bar-local-item-from-menu 'isearch-forward
    				   "search" map nil
				   :label "Search" :vert-only t)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
371 372 373 374
    map))

(defun vc-dir-node-directory (node)
  ;; Compute the directory for NODE.
Juanma Barranquero's avatar
Juanma Barranquero committed
375
  ;; If it's a directory node, get it from the node.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
376 377 378 379
  (let ((data (ewoc-data node)))
    (or (vc-dir-fileinfo->directory data)
	;; Otherwise compute it from the file name.
	(file-name-directory
380 381 382
	 (directory-file-name
	  (expand-file-name
	   (vc-dir-fileinfo->name data)))))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

(defun vc-dir-update (entries buffer &optional noinsert)
  "Update BUFFER's ewoc from the list of ENTRIES.
If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
  ;; Add ENTRIES to the vc-dir buffer BUFFER.
  (with-current-buffer buffer
    ;; Insert the entries sorted by name into the ewoc.
    ;; We assume the ewoc is sorted too, which should be the
    ;; case if we always add entries with vc-dir-update.
    (setq entries
	  ;; Sort: first files and then subdirectories.
	  ;; XXX: this is VERY inefficient, it computes the directory
	  ;; names too many times
	  (sort entries
		(lambda (entry1 entry2)
398 399 400 401
		  (let ((dir1 (file-name-directory
			        (directory-file-name (expand-file-name (car entry1)))))
			(dir2 (file-name-directory
			       (directory-file-name (expand-file-name (car entry2))))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
402 403 404 405 406 407
		    (cond
		     ((string< dir1 dir2) t)
		     ((not (string= dir1 dir2)) nil)
		     ((string< (car entry1) (car entry2))))))))
    ;; Insert directory entries in the right places.
    (let ((entry (car entries))
408
	  (node (ewoc-nth vc-ewoc 0))
409
	  (to-remove nil)
410
	  (dotname (file-relative-name default-directory)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
411 412
      ;; Insert . if it is not present.
      (unless node
413 414
	(ewoc-enter-last
	 vc-ewoc (vc-dir-create-fileinfo
415
		  dotname nil nil nil default-directory))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
416
	(setq node (ewoc-nth vc-ewoc 0)))
417

Dan Nicolaescu's avatar
Dan Nicolaescu committed
418 419
      (while (and entry node)
	(let* ((entryfile (car entry))
420 421
	       (entrydir (file-name-directory (directory-file-name
					       (expand-file-name entryfile))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
422 423 424 425 426 427 428 429 430
	       (nodedir (vc-dir-node-directory node)))
	  (cond
	   ;; First try to find the directory.
	   ((string-lessp nodedir entrydir)
	    (setq node (ewoc-next vc-ewoc node)))
	   ((string-equal nodedir entrydir)
	    ;; Found the directory, find the place for the file name.
	    (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
	      (cond
431 432
	       ((string= nodefile dotname)
		(setq node (ewoc-next vc-ewoc node)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
433 434 435
	       ((string-lessp nodefile entryfile)
		(setq node (ewoc-next vc-ewoc node)))
	       ((string-equal nodefile entryfile)
436 437 438 439 440 441 442 443 444 445
		(if (nth 1 entry)
		    (progn
		      (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
		      (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
		      (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
		      (ewoc-invalidate vc-ewoc node))
		  ;; If the state is nil, the file does not exist
		  ;; anymore, so remember the entry so we can remove
		  ;; it after we are done inserting all ENTRIES.
		  (push node to-remove))
446
		(setq entries (cdr entries))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
447 448 449
		(setq entry (car entries))
		(setq node (ewoc-next vc-ewoc node)))
	       (t
450 451 452
		(unless noinsert
		  (ewoc-enter-before vc-ewoc node
				     (apply 'vc-dir-create-fileinfo entry)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
453 454 455
		(setq entries (cdr entries))
		(setq entry (car entries))))))
	   (t
456 457 458 459 460
	    (unless noinsert
	      ;; We might need to insert a directory node if the
	      ;; previous node was in a different directory.
	      (let* ((rd (file-relative-name entrydir))
		     (prev-node (ewoc-prev vc-ewoc node))
461 462
		     (prev-dir (if prev-node
				   (vc-dir-node-directory prev-node))))
463 464 465 466 467 468
		(unless (string-equal entrydir prev-dir)
		  (ewoc-enter-before
		   vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
	      ;; Now insert the node itself.
	      (ewoc-enter-before vc-ewoc node
				 (apply 'vc-dir-create-fileinfo entry)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
469 470 471 472 473
	    (setq entries (cdr entries) entry (car entries))))))
      ;; We're past the last node, all remaining entries go to the end.
      (unless (or node noinsert)
	(let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
	  (dolist (entry entries)
474 475
	    (let ((entrydir (file-name-directory
			     (directory-file-name (expand-file-name (car entry))))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
476 477 478 479 480 481 482 483
	      ;; Insert a directory node if needed.
	      (unless (string-equal lastdir entrydir)
		(setq lastdir entrydir)
		(let ((rd (file-relative-name entrydir)))
		  (ewoc-enter-last
		   vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
	      ;; Now insert the node itself.
	      (ewoc-enter-last vc-ewoc
484 485 486 487
			       (apply 'vc-dir-create-fileinfo entry))))))
      (when to-remove
	(let ((inhibit-read-only t))
	  (apply 'ewoc-delete vc-ewoc (nreverse to-remove)))))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

(defun vc-dir-busy ()
  (and (buffer-live-p vc-dir-process-buffer)
       (get-buffer-process vc-dir-process-buffer)))

(defun vc-dir-kill-dir-status-process ()
  "Kill the temporary buffer and associated process."
  (interactive)
  (when (buffer-live-p vc-dir-process-buffer)
    (let ((proc (get-buffer-process vc-dir-process-buffer)))
      (when proc (delete-process proc))
      (setq vc-dir-process-buffer nil)
      (setq mode-line-process nil))))

(defun vc-dir-kill-query ()
  ;; Make sure that when the status buffer is killed the update
  ;; process running in background is also killed.
  (if (vc-dir-busy)
    (when (y-or-n-p "Status update process running, really kill status buffer? ")
      (vc-dir-kill-dir-status-process)
      t)
    t))

(defun vc-dir-next-line (arg)
  "Go to the next line.
If a prefix argument is given, move by that many lines."
  (interactive "p")
  (with-no-warnings
    (ewoc-goto-next vc-ewoc arg)
    (vc-dir-move-to-goal-column)))

(defun vc-dir-previous-line (arg)
  "Go to the previous line.
If a prefix argument is given, move by that many lines."
  (interactive "p")
  (ewoc-goto-prev vc-ewoc arg)
  (vc-dir-move-to-goal-column))

(defun vc-dir-next-directory ()
  "Go to the next directory."
  (interactive)
  (let ((orig (point)))
    (if
	(catch 'foundit
	  (while t
	    (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
	      (cond ((not next)
		     (throw 'foundit t))
		    (t
		     (progn
		       (ewoc-goto-node vc-ewoc next)
		       (vc-dir-move-to-goal-column)
		       (if (vc-dir-fileinfo->directory (ewoc-data next))
			   (throw 'foundit nil))))))))
	(goto-char orig))))

(defun vc-dir-previous-directory ()
  "Go to the previous directory."
  (interactive)
  (let ((orig (point)))
    (if
	(catch 'foundit
	  (while t
	    (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
	      (cond ((not prev)
		     (throw 'foundit t))
		    (t
		     (progn
		       (ewoc-goto-node vc-ewoc prev)
		       (vc-dir-move-to-goal-column)
		       (if (vc-dir-fileinfo->directory (ewoc-data prev))
			   (throw 'foundit nil))))))))
	(goto-char orig))))

(defun vc-dir-mark-unmark (mark-unmark-function)
  (if (use-region-p)
564
      (let ((processed-line nil)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
565 566 567
	    (lastl (line-number-at-pos (region-end))))
	(save-excursion
	  (goto-char (region-beginning))
568 569 570 571 572
	  (while (and (<= (line-number-at-pos) lastl)
                      ;; We make sure to not get stuck processing the
                      ;; same line in an infinite loop.
		      (not (eq processed-line (line-number-at-pos))))
	    (setq processed-line (line-number-at-pos))
573 574 575 576 577 578
	    (condition-case nil
		(funcall mark-unmark-function)
	      ;; `vc-dir-mark-file' signals an error if we try marking
	      ;; a directory containing marked files in its tree, or a
	      ;; file in a marked directory tree.  Just continue.
	      (error (vc-dir-next-line 1))))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
579 580 581
    (funcall mark-unmark-function)))

(defun vc-dir-parent-marked-p (arg)
582 583
  ;; Non-nil iff a parent directory of arg is marked.
  ;; Return value, if non-nil is the `ewoc-data' for the marked parent.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
584
  (let* ((argdir (vc-dir-node-directory arg))
Stefan Monnier's avatar
Stefan Monnier committed
585
	 ;; (arglen (length argdir))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
586
	 (crt arg)
587
	 (found nil))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
588 589
    ;; Go through the predecessors, checking if any directory that is
    ;; a parent is marked.
590 591 592 593 594
    (while (and (null found)
		(setq crt (ewoc-prev vc-ewoc crt)))
      (let ((data (ewoc-data crt))
	    (dir (vc-dir-node-directory crt)))
	(and (vc-dir-fileinfo->directory data)
595
	     (string-prefix-p dir argdir)
596 597 598
	     (vc-dir-fileinfo->marked data)
	     (setq found data))))
    found))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
599 600

(defun vc-dir-children-marked-p (arg)
601 602
  ;; Non-nil iff a child of ARG is marked.
  ;; Return value, if non-nil, is the `ewoc-data' for the marked child.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
603 604 605
  (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
	 (is-child t)
	 (crt arg)
606 607 608 609 610 611 612 613 614 615 616 617
	 (found nil))
    (while (and is-child
		(null found)
		(setq crt (ewoc-next vc-ewoc crt)))
      (let ((data (ewoc-data crt))
	    (dir (vc-dir-node-directory crt)))
	(if (string-match argdir-re dir)
	    (if (vc-dir-fileinfo->marked data)
		(setq found data))
	  ;; We are done, we got to an entry that is not a child of `arg'.
	  (setq is-child nil))))
    found))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
618 619 620 621 622

(defun vc-dir-mark-file (&optional arg)
  ;; Mark ARG or the current file and move to the next line.
  (let* ((crt (or arg (ewoc-locate vc-ewoc)))
         (file (ewoc-data crt))
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	 (isdir (vc-dir-fileinfo->directory file))
	 ;; Forbid marking a directory containing marked files in its
	 ;; tree, or a file in a marked directory tree.
	 (conflict (if isdir
		       (vc-dir-children-marked-p crt)
		     (vc-dir-parent-marked-p crt))))
    (when conflict
      (error (if isdir
		 "File `%s' in this directory is already marked"
	       "Parent directory `%s' is already marked")
	     (vc-dir-fileinfo->name conflict)))
    (setf (vc-dir-fileinfo->marked file) t)
    (ewoc-invalidate vc-ewoc crt)
    (unless (or arg (mouse-event-p last-command-event))
      (vc-dir-next-line 1))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

(defun vc-dir-mark ()
  "Mark the current file or all files in the region.
If the region is active, mark all the files in the region.
Otherwise mark the file on the current line and move to the next
line."
  (interactive)
  (vc-dir-mark-unmark 'vc-dir-mark-file))

(defun vc-dir-mark-all-files (arg)
  "Mark all files with the same state as the current one.
With a prefix argument mark all files.
If the current entry is a directory, mark all child files.

The commands operate on files that are on the same state.
This command is intended to make it easy to select all files that
share the same state."
  (interactive "P")
  (if arg
      ;; Mark all files.
      (progn
	;; First check that no directory is marked, we can't mark
	;; files in that case.
	(ewoc-map
	 (lambda (filearg)
	   (when (and (vc-dir-fileinfo->directory filearg)
		      (vc-dir-fileinfo->marked filearg))
	     (error "Cannot mark all files, directory `%s' marked"
		    (vc-dir-fileinfo->name filearg))))
	 vc-ewoc)
	(ewoc-map
	 (lambda (filearg)
	   (unless (vc-dir-fileinfo->marked filearg)
	     (setf (vc-dir-fileinfo->marked filearg) t)
	     t))
	 vc-ewoc))
674 675
    (let* ((crt  (ewoc-locate vc-ewoc))
	   (data (ewoc-data crt)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
676 677
      (if (vc-dir-fileinfo->directory data)
	  ;; It's a directory, mark child files.
678 679 680 681 682 683
	  (let (crt-data)
	    (while (and (setq crt (ewoc-next vc-ewoc crt))
			(setq crt-data (ewoc-data crt))
			(not (vc-dir-fileinfo->directory crt-data)))
	      (setf (vc-dir-fileinfo->marked crt-data) t)
	      (ewoc-invalidate vc-ewoc crt)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
684
	;; It's a file
685 686
	(let ((state (vc-dir-fileinfo->state data)))
	  (setq crt (ewoc-nth vc-ewoc 0))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	  (while crt
	    (let ((crt-data (ewoc-data crt)))
	      (when (and (not (vc-dir-fileinfo->marked crt-data))
			 (eq (vc-dir-fileinfo->state crt-data) state)
			 (not (vc-dir-fileinfo->directory crt-data)))
		(vc-dir-mark-file crt)))
	    (setq crt (ewoc-next vc-ewoc crt))))))))

(defun vc-dir-unmark-file ()
  ;; Unmark the current file and move to the next line.
  (let* ((crt (ewoc-locate vc-ewoc))
         (file (ewoc-data crt)))
    (setf (vc-dir-fileinfo->marked file) nil)
    (ewoc-invalidate vc-ewoc crt)
    (unless (mouse-event-p last-command-event)
      (vc-dir-next-line 1))))

(defun vc-dir-unmark ()
  "Unmark the current file or all files in the region.
If the region is active, unmark all the files in the region.
707
Otherwise unmark the file on the current line and move to the next
Dan Nicolaescu's avatar
Dan Nicolaescu committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
line."
  (interactive)
  (vc-dir-mark-unmark 'vc-dir-unmark-file))

(defun vc-dir-unmark-file-up ()
  "Move to the previous line and unmark the file."
  (interactive)
  ;; If we're on the first line, we won't move up, but we will still
  ;; remove the mark.  This seems a bit odd but it is what buffer-menu
  ;; does.
  (let* ((prev (ewoc-goto-prev vc-ewoc 1))
	 (file (ewoc-data prev)))
    (setf (vc-dir-fileinfo->marked file) nil)
    (ewoc-invalidate vc-ewoc prev)
    (vc-dir-move-to-goal-column)))

(defun vc-dir-unmark-all-files (arg)
  "Unmark all files with the same state as the current one.
With a prefix argument unmark all files.
If the current entry is a directory, unmark all the child files.

The commands operate on files that are on the same state.
This command is intended to make it easy to deselect all files
that share the same state."
  (interactive "P")
  (if arg
      (ewoc-map
       (lambda (filearg)
	 (when (vc-dir-fileinfo->marked filearg)
	   (setf (vc-dir-fileinfo->marked filearg) nil)
	   t))
       vc-ewoc)
    (let* ((crt (ewoc-locate vc-ewoc))
	   (data (ewoc-data crt)))
      (if (vc-dir-fileinfo->directory data)
	  ;; It's a directory, unmark child files.
	  (while (setq crt (ewoc-next vc-ewoc crt))
	    (let ((crt-data (ewoc-data crt)))
	      (unless (vc-dir-fileinfo->directory crt-data)
		(setf (vc-dir-fileinfo->marked crt-data) nil)
		(ewoc-invalidate vc-ewoc crt))))
	;; It's a file
	(let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
	  (ewoc-map
	   (lambda (filearg)
	     (when (and (vc-dir-fileinfo->marked filearg)
			(eq (vc-dir-fileinfo->state filearg) crt-state))
	       (setf (vc-dir-fileinfo->marked filearg) nil)
	       t))
	   vc-ewoc))))))

(defun vc-dir-toggle-mark-file ()
  (let* ((crt (ewoc-locate vc-ewoc))
         (file (ewoc-data crt)))
    (if (vc-dir-fileinfo->marked file)
	(vc-dir-unmark-file)
      (vc-dir-mark-file))))

(defun vc-dir-toggle-mark (e)
  (interactive "e")
768
  (vc-dir-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
769

770
(defun vc-dir-clean-files ()
771 772 773 774
  "Delete the marked files, or the current file if no marks.
The files will not be marked as deleted in the version control
system; see `vc-dir-delete-file'."
  (interactive)
775
  (let* ((files (or (vc-dir-marked-files)
776
                    (list (vc-dir-current-file))))
777 778 779 780 781 782
         (tracked
          (seq-filter (lambda (file)
                        (not (eq (vc-call-backend vc-dir-backend 'state file)
                                 'unregistered)))
                      files)))
    (when tracked
783 784 785
      (user-error (ngettext "Trying to clean tracked file: %s"
                            "Trying to clean tracked files: %s"
                            (length tracked))
786 787 788
                  (mapconcat #'file-name-nondirectory tracked ", ")))
    (map-y-or-n-p "Delete %s? " #'delete-file files)
    (revert-buffer)))
789

Dan Nicolaescu's avatar
Dan Nicolaescu committed
790
(defun vc-dir-delete-file ()
791 792 793
  "Delete the marked files, or the current file if no marks.
The files will also be marked as deleted in the version control
system."
Dan Nicolaescu's avatar
Dan Nicolaescu committed
794 795 796 797 798 799 800 801 802
  (interactive)
  (mapc 'vc-delete-file (or (vc-dir-marked-files)
                            (list (vc-dir-current-file)))))

(defun vc-dir-find-file ()
  "Find the file on the current line."
  (interactive)
  (find-file (vc-dir-current-file)))

803
(defun vc-dir-find-file-other-window (&optional event)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
804
  "Find the file on the current line, in another window."
805 806
  (interactive (list last-nonmenu-event))
  (if event (posn-set-point (event-end event)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
807 808
  (find-file-other-window (vc-dir-current-file)))

809 810 811 812 813 814 815
(defun vc-dir-display-file (&optional event)
  "Display the file on the current line, in another window."
  (interactive (list last-nonmenu-event))
  (if event (posn-set-point (event-end event)))
  (display-buffer (find-file-noselect (vc-dir-current-file))
		  t))

816 817 818 819 820
(defun vc-dir-view-file ()
  "Examine a file on the current line in view mode."
  (interactive)
  (view-file (vc-dir-current-file)))

821 822 823 824 825 826 827 828 829 830 831 832
(defun vc-dir-isearch ()
  "Search for a string through all marked buffers using Isearch."
  (interactive)
  (multi-isearch-files
   (mapcar 'car (vc-dir-marked-only-files-and-states))))

(defun vc-dir-isearch-regexp ()
  "Search for a regexp through all marked buffers using Isearch."
  (interactive)
  (multi-isearch-files-regexp
   (mapcar 'car (vc-dir-marked-only-files-and-states))))

833 834 835 836 837 838
(defun vc-dir-search (regexp)
  "Search through all marked files for a match for REGEXP.
For marked directories, use the files displayed from those directories.
Stops when a match is found.
To continue searching for next match, use command \\[tags-loop-continue]."
  (interactive "sSearch marked files (regexp): ")
839
  (tags-search regexp
840
               (mapcar #'car (vc-dir-marked-only-files-and-states))))
841

842 843 844 845 846 847
(defun vc-dir-query-replace-regexp (from to &optional delimited)
  "Do `query-replace-regexp' of FROM with TO, on all marked files.
If a directory is marked, then use the files displayed for that directory.
Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
with the command \\[tags-loop-continue]."
848
  ;; FIXME: this is almost a copy of `dired-do-query-replace-regexp'.  This
849 850 851 852 853 854 855 856 857 858 859 860
  ;; should probably be made generic and used in both places instead of
  ;; duplicating it here.
  (interactive
   (let ((common
	  (query-replace-read-args
	   "Query replace regexp in marked files" t t)))
     (list (nth 0 common) (nth 1 common) (nth 2 common))))
  (dolist (file (mapcar 'car (vc-dir-marked-only-files-and-states)))
    (let ((buffer (get-file-buffer file)))
      (if (and buffer (with-current-buffer buffer
			buffer-read-only))
	  (error "File `%s' is visited read-only" file))))
861 862 863 864 865
  (fileloop-initialize-replace
   from to (mapcar 'car (vc-dir-marked-only-files-and-states))
   (if (equal from (downcase from)) nil 'default)
   delimited)
  (fileloop-continue))
866

867 868 869 870 871 872 873 874 875 876 877 878
(defun vc-dir-ignore (&optional arg)
  "Ignore the current file.
If a prefix argument is given, ignore all marked files."
  (interactive "P")
  (if arg
      (ewoc-map
       (lambda (filearg)
	 (when (vc-dir-fileinfo->marked filearg)
	   (vc-ignore (vc-dir-fileinfo->name filearg))
	   t))
       vc-ewoc)
    (vc-ignore (vc-dir-current-file))))
Xue Fuqiao's avatar
Xue Fuqiao committed
879

Dan Nicolaescu's avatar
Dan Nicolaescu committed
880 881 882 883 884 885 886 887 888 889 890 891
(defun vc-dir-current-file ()
  (let ((node (ewoc-locate vc-ewoc)))
    (unless node
      (error "No file available"))
    (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))

(defun vc-dir-marked-files ()
  "Return the list of marked files."
  (mapcar
   (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
   (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))

892 893 894 895
(defun vc-dir-marked-only-files-and-states ()
  "Return the list of conses (FILE . STATE) for the marked files.
For marked directories return the corresponding conses for the
child files."
Dan Nicolaescu's avatar
Dan Nicolaescu committed
896 897 898 899 900
  (let ((crt (ewoc-nth vc-ewoc 0))
	result)
    (while crt
      (let ((crt-data (ewoc-data crt)))
	(if (vc-dir-fileinfo->marked crt-data)
901
	    ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
902 903
	    (if (vc-dir-fileinfo->directory crt-data)
		(let* ((dir (vc-dir-fileinfo->directory crt-data))
Stefan Monnier's avatar
Stefan Monnier committed
904
		       ;; (dirlen (length dir))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
905 906 907
		       data)
		  (while
		      (and (setq crt (ewoc-next vc-ewoc crt))
908
			   (string-prefix-p dir
Dan Nicolaescu's avatar
Dan Nicolaescu committed
909 910 911 912
                                               (progn
                                                 (setq data (ewoc-data crt))
                                                 (vc-dir-node-directory crt))))
		    (unless (vc-dir-fileinfo->directory data)
913
		      (push
914 915 916 917 918 919
		       (cons (expand-file-name (vc-dir-fileinfo->name data))
			     (vc-dir-fileinfo->state data))
		       result))))
	      (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
			  (vc-dir-fileinfo->state crt-data))
		    result)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
920 921
	      (setq crt (ewoc-next vc-ewoc crt)))
	  (setq crt (ewoc-next vc-ewoc crt)))))
922
    (nreverse result)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
923

924 925 926
(defun vc-dir-child-files-and-states ()
  "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
If it is a file, return the corresponding cons for the file itself."
Dan Nicolaescu's avatar
Dan Nicolaescu committed
927 928 929 930 931
  (let* ((crt (ewoc-locate vc-ewoc))
	 (crt-data (ewoc-data crt))
         result)
    (if (vc-dir-fileinfo->directory crt-data)
	(let* ((dir (vc-dir-fileinfo->directory crt-data))
Stefan Monnier's avatar
Stefan Monnier committed
932
	       ;; (dirlen (length dir))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
933 934 935
	       data)
	  (while
	      (and (setq crt (ewoc-next vc-ewoc crt))
936
                   (string-prefix-p dir (progn
Dan Nicolaescu's avatar
Dan Nicolaescu committed
937 938 939
                                             (setq data (ewoc-data crt))
                                             (vc-dir-node-directory crt))))
	    (unless (vc-dir-fileinfo->directory data)
940
	      (push
941 942 943
	       (cons (expand-file-name (vc-dir-fileinfo->name data))
		     (vc-dir-fileinfo->state data))
	       result))))
944
      (push
945 946
       (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
	     (vc-dir-fileinfo->state crt-data)) result))
947
    (nreverse result)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
948

949 950
(defun vc-dir-recompute-file-state (fname def-dir)
  (let* ((file-short (file-relative-name fname def-dir))
Stefan Monnier's avatar
Stefan Monnier committed
951
	 (_remove-me-when-CVS-works
952 953 954
	  (when (eq vc-dir-backend 'CVS)
	    ;; FIXME: Warning: UGLY HACK.  The CVS backend caches the state
	    ;; info, this forces the backend to update it.
955
	    (vc-call-backend vc-dir-backend 'registered fname)))
956 957 958 959 960 961 962 963 964
	 (state (vc-call-backend vc-dir-backend 'state fname))
	 (extra (vc-call-backend vc-dir-backend
				 'status-fileinfo-extra fname)))
    (list file-short state extra)))

(defun vc-dir-find-child-files (dirname)
  ;; Give a DIRNAME string return the list of all child files shown in
  ;; the current *vc-dir* buffer.
  (let ((crt (ewoc-nth vc-ewoc 0))
Stefan Monnier's avatar
Stefan Monnier committed
965
	children)
966
    ;; Find DIR
967
    (while (and crt (not (string-prefix-p
968 969
			  dirname (vc-dir-node-directory crt))))
      (setq crt (ewoc-next vc-ewoc crt)))
970
    (while (and crt (string-prefix-p
971
		     dirname
Stefan Monnier's avatar
Stefan Monnier committed
972
                     (vc-dir-node-directory crt)))
973 974 975 976 977 978 979 980 981 982
      (let ((data (ewoc-data crt)))
	(unless (vc-dir-fileinfo->directory data)
	  (push (expand-file-name (vc-dir-fileinfo->name data)) children)))
      (setq crt (ewoc-next vc-ewoc crt)))
    children))

(defun vc-dir-resync-directory-files (dirname)
  ;; Update the entries for all the child files of DIRNAME shown in
  ;; the current *vc-dir* buffer.
  (let ((files (vc-dir-find-child-files dirname))
983
	(ddir default-directory)
984 985 986 987 988 989 990
	fileentries)
    (when files
      (dolist (crt files)
	(push (vc-dir-recompute-file-state crt ddir)
	      fileentries))
      (vc-dir-update fileentries (current-buffer)))))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
991
(defun vc-dir-resynch-file (&optional fname)
992
  "Update the entries for FNAME in any directory buffers that list it."
993
  (let ((file (expand-file-name (or fname buffer-file-name)))
994 995 996 997 998 999 1000 1001 1002 1003
        (drop '()))
    (save-current-buffer
      ;; look for a vc-dir buffer that might show this file.
      (dolist (status-buf vc-dir-buffers)
        (if (not (buffer-live-p status-buf))
            (push status-buf drop)
          (set-buffer status-buf)
          (if (not (derived-mode-p 'vc-dir-mode))
              (push status-buf drop)
            (let ((ddir default-directory))
1004
              (when (string-prefix-p ddir file)
1005
                (if (file-directory-p file)
1006 1007 1008 1009
		    (progn
		      (vc-dir-resync-directory-files file)
		      (ewoc-set-hf vc-ewoc
				   (vc-dir-headers vc-dir-backend default-directory) ""))
1010 1011
                  (let* ((complete-state (vc-dir-recompute-file-state file ddir))
			 (state (cadr complete-state)))
1012
                    (vc-dir-update
1013 1014 1015
                     (list complete-state)
                     status-buf (or (not state)
				    (eq state 'up-to-date)))))))))))
1016 1017
    ;; Remove out-of-date entries from vc-dir-buffers.
    (dolist (b drop) (setq vc-dir-buffers (delq b vc-dir-buffers)))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1018

Dan Nicolaescu's avatar
Dan Nicolaescu committed
1019 1020 1021
(defvar use-vc-backend)  ;; dynamically bound

(define-derived-mode vc-dir-mode special-mode "VC dir"
1022
  "Major mode for VC directory buffers.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1023
Marking/Unmarking key bindings and actions:
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1024 1025
m - mark a file/directory
  - if the region is active, mark all the files in region.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1026 1027 1028
    Restrictions: - a file cannot be marked if any parent directory is marked
                  - a directory cannot be marked if any child file or
                    directory is marked
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1029 1030
u - unmark a file/directory
  - if the region is active, unmark all the files in region.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1031 1032 1033 1034 1035 1036 1037 1038
M - if the cursor is on a file: mark all the files with the same state as
      the current file
  - if the cursor is on a directory: mark all child files
  - with a prefix argument: mark all files
U - if the cursor is on a file: unmark all the files with the same state
      as the current file
  - if the cursor is on a directory: unmark all child files
  - with a prefix argument: unmark all files
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1039
mouse-2  - toggles the mark state
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1040

1041
VC commands
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1042 1043 1044
VC commands in the `C-x v' prefix can be used.
VC commands act on the marked entries.  If nothing is marked, VC
commands act on the current entry.
1045 1046 1047 1048 1049

Search & Replace
S - searches the marked files
Q - does a query replace on the marked files
M-s a C-s - does an isearch on the marked files
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1050
M-s a C-M-s - does a regexp isearch on the marked files
1051 1052
If nothing is marked, these commands act on the current entry.
When a directory is current or marked, the Search & Replace
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1053 1054
commands act on the child files of that directory that are displayed in
the *vc-dir* buffer.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1055 1056

\\{vc-dir-mode-map}"
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1057
  (set (make-local-variable 'vc-dir-backend) use-vc-backend)
1058 1059
  (set (make-local-variable 'desktop-save-buffer)
       'vc-dir-desktop-buffer-misc-data)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1060
  (setq buffer-read-only t)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1061 1062
  (when (boundp 'tool-bar-map)
    (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1063 1064 1065
  (let ((buffer-read-only nil))
    (erase-buffer)
    (set (make-local-variable 'vc-dir-process-buffer) nil)
1066
    (set (make-local-variable 'vc-ewoc) (ewoc-create #'vc-dir-printer))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1067 1068
    (set (make-local-variable 'revert-buffer-function)
	 'vc-dir-revert-buffer-function)
Juanma Barranquero's avatar
Juanma Barranquero committed
1069
    (setq list-buffers-directory (expand-file-name "*vc-dir*" default-directory))
1070
    (add-to-list 'vc-dir-buffers (current-buffer))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1071 1072 1073
    ;; Make sure that if the directory buffer is killed, the update
    ;; process running in the background is also killed.
    (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
1074
    (hack-dir-local-variables-non-file-buffer)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1075
    (vc-dir-refresh)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1076 1077 1078

(defun vc-dir-headers (backend dir)
  "Display the headers in the *VC dir* buffer.
1079
It calls the `dir-extra-headers' backend method to display backend
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1080 1081
specific headers."
  (concat
1082
   ;; First layout the common headers.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1083 1084 1085
   (propertize "VC backend : " 'face 'font-lock-type-face)
   (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
   (propertize "Working dir: " 'face 'font-lock-type-face)
1086 1087
   (propertize (format "%s\n" (abbreviate-file-name dir))
               'face 'font-lock-variable-name-face)
1088 1089 1090
   ;; Then the backend specific ones.
   (vc-call-backend backend 'dir-extra-headers dir)
   "\n"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1091

1092
(defun vc-dir-refresh-files (files)
Paul Eggert's avatar
Paul Eggert committed
1093
  "Refresh some files in the *VC-dir* buffer."
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1094 1095 1096
  (let ((def-dir default-directory)
	(backend vc-dir-backend))
    (vc-set-mode-line-busy-indicator)
Juanma Barranquero's avatar
Juanma Barranquero committed
1097 1098
    ;; Call the `dir-status-files' backend function.
    ;; `dir-status-files' is supposed to be asynchronous.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1099 1100 1101 1102 1103 1104
    ;; It should compute the results, and then call the function
    ;; passed as an argument in order to update the vc-dir buffer
    ;; with the results.
    (unless (buffer-live-p vc-dir-process-buffer)
      (setq vc-dir-process-buffer
            (generate-new-buffer (format " *VC-%s* tmp status" backend))))
Stefan Monnier's avatar
Stefan Monnier committed
1105
    (let ((buffer (current-buffer)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1106
      (with-current-buffer vc-dir-process-buffer
1107
        (setq default-directory def-dir)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1108 1109
        (erase-buffer)
        (vc-call-backend
1110
         backend 'dir-status-files def-dir files
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
         (lambda (entries &optional more-to-come)
           ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
           ;; If MORE-TO-COME is true, then more updates will come from
           ;; the asynchronous process.
           (with-current-buffer buffer
             (vc-dir-update entries buffer)
             (unless more-to-come
               (setq mode-line-process nil)
               ;; Remove the ones that haven't been updated at all.
               ;; Those not-updated are those whose state is nil because the
               ;; file/dir doesn't exist and isn't versioned.
               (ewoc-filter vc-ewoc
                            (lambda (info)
			      ;; The state for directory entries might
			      ;; have been changed to 'up-to-date,
Paul Eggert's avatar
Paul Eggert committed
1126
			      ;; reset it, otherwise it will be removed when doing 'x'
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1127 1128 1129 1130 1131 1132 1133 1134 1135
			      ;; next time.
			      ;; FIXME: There should be a more elegant way to do this.
			      (when (and (vc-dir-fileinfo->directory info)
					 (eq (vc-dir-fileinfo->state info)
					     'up-to-date))
				(setf (vc-dir-fileinfo->state info) nil))

                              (not (vc-dir-fileinfo->needs-update info))))))))))))

Stefan Monnier's avatar
Stefan Monnier committed
1136
(defun vc-dir-revert-buffer-function (&optional _ignore-auto _noconfirm)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1137 1138
  (vc-dir-refresh))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
(defun vc-dir-refresh ()
  "Refresh the contents of the *VC-dir* buffer.
Throw an error if another update process is in progress."
  (interactive)
  (if (vc-dir-busy)
      (error "Another update process is in progress, cannot run two at a time")
    (let ((def-dir default-directory)
	  (backend vc-dir-backend))
      (vc-set-mode-line-busy-indicator)
      ;; Call the `dir-status' backend function.
      ;; `dir-status' is supposed to be asynchronous.
      ;; It should compute the results, and then call the function
      ;; passed as an argument in order to update the vc-dir buffer
      ;; with the results.

      ;; Create a buffer that can be used by `dir-status' and call
      ;; `dir-status' with this buffer as the current buffer.  Use
      ;; `vc-dir-process-buffer' to remember this buffer, so that
      ;; it can be used later to kill the update process in case it
      ;; takes too long.
      (unless (buffer-live-p vc-dir-process-buffer)
        (setq vc-dir-process-buffer
              (generate-new-buffer (format " *VC-%s* tmp status" backend))))
1162 1163 1164 1165
      ;; set the needs-update flag on all non-directory entries
      (ewoc-map (lambda (info)
		  (unless (vc-dir-fileinfo->directory info)
		    (setf (vc-dir-fileinfo->needs-update info) t) nil))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1166
                vc-ewoc)
1167 1168 1169
      ;; Bzr has serious locking problems, so setup the headers first (this is
      ;; synchronous) rather than doing it while dir-status is running.
      (ewoc-set-hf vc-ewoc (vc-dir-headers backend def-dir) "")
Stefan Monnier's avatar
Stefan Monnier committed
1170
      (let ((buffer (current-buffer)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1171
        (with-current-buffer vc-dir-process-buffer
1172
          (setq default-directory def-dir)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1173 1174
          (erase-buffer)
          (vc-call-backend
1175
           backend 'dir-status-files def-dir nil
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
           (lambda (entries &optional more-to-come)
             ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
             ;; If MORE-TO-COME is true, then more updates will come from
             ;; the asynchronous process.
             (with-current-buffer buffer
               (vc-dir-update entries buffer)
               (unless more-to-come
                 (let ((remaining
                        (ewoc-collect
                         vc-ewoc 'vc-dir-fileinfo->needs-update)))
                   (if remaining
                       (vc-dir-refresh-files
1188
                        (mapcar 'vc-dir-fileinfo->name remaining))
1189
                     (setq mode-line-process nil))))))))))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1190 1191 1192 1193 1194 1195 1196 1197

(defun vc-dir-show-fileentry (file)
  "Insert an entry for a specific file into the current *VC-dir* listing.
This is typically used if the file is up-to-date (or has been added
outside of VC) and one wants to do some operation on it."
  (interactive "fShow file: ")
  (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))

1198 1199 1200 1201
(defun vc-dir-hide-state (&optional state)
  "Hide items that are in STATE from display.
See `vc-state' for valid values of STATE.

Dmitry Gutov's avatar
Dmitry Gutov committed
1202
If STATE is nil, hide both `up-to-date' and `ignored' items.
1203 1204

Interactively, if `current-prefix-arg' is non-nil, set STATE to
Dmitry Gutov's avatar
Dmitry Gutov committed
1205
state of item at point, if any."
1206 1207 1208 1209 1210
  (interactive (list
		(and current-prefix-arg
		     ;; Command is prefixed.  Infer STATE from point.
		     (let ((node (ewoc-locate vc-ewoc)))
		       (and node (vc-dir-fileinfo->state (ewoc-data node)))))))
Dmitry Gutov's avatar
Dmitry Gutov committed
1211 1212 1213
  (if state
      (message "Hiding items in state \"%s\"" state)
    (message "Hiding up-to-date and ignored items"))
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223