| 1 | ;;; find-dired.el --- run a `find' command and dired the output |
| 2 | |
| 3 | ;; Copyright (C) 1992, 1994-1995, 2000-2014 Free Software Foundation, |
| 4 | ;; Inc. |
| 5 | |
| 6 | ;; Author: Roland McGrath <roland@gnu.org>, |
| 7 | ;; Sebastian Kremer <sk@thp.uni-koeln.de> |
| 8 | ;; Maintainer: emacs-devel@gnu.org |
| 9 | ;; Keywords: unix |
| 10 | |
| 11 | ;; This file is part of GNU Emacs. |
| 12 | |
| 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 14 | ;; it under the terms of the GNU General Public License as published by |
| 15 | ;; the Free Software Foundation, either version 3 of the License, or |
| 16 | ;; (at your option) any later version. |
| 17 | |
| 18 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | ;; GNU General Public License for more details. |
| 22 | |
| 23 | ;; You should have received a copy of the GNU General Public License |
| 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 25 | |
| 26 | ;;; Commentary: |
| 27 | |
| 28 | ;;; Code: |
| 29 | |
| 30 | (require 'dired) |
| 31 | |
| 32 | (defgroup find-dired nil |
| 33 | "Run a `find' command and Dired the output." |
| 34 | :group 'dired |
| 35 | :prefix "find-") |
| 36 | |
| 37 | ;; FIXME this option does not really belong in this file, it's more general. |
| 38 | ;; Eg cf some tests in grep.el. |
| 39 | (defcustom find-exec-terminator |
| 40 | (if (eq 0 |
| 41 | (ignore-errors |
| 42 | (process-file find-program nil nil nil |
| 43 | null-device "-exec" "echo" "{}" "+"))) |
| 44 | "+" |
| 45 | (shell-quote-argument ";")) |
| 46 | "String that terminates \"find -exec COMMAND {} \". |
| 47 | The value should include any needed quoting for the shell. |
| 48 | Common values are \"+\" and \"\\\\;\", with the former more efficient |
| 49 | than the latter." |
| 50 | :version "24.1" |
| 51 | :group 'find-dired |
| 52 | :type 'string) |
| 53 | |
| 54 | ;; find's -ls corresponds to these switches. |
| 55 | ;; Note -b, at least GNU find quotes spaces etc. in filenames |
| 56 | (defcustom find-ls-option |
| 57 | (if (eq 0 |
| 58 | (ignore-errors |
| 59 | (process-file find-program nil nil nil null-device "-ls"))) |
| 60 | (cons "-ls" |
| 61 | (if (eq system-type 'berkeley-unix) |
| 62 | "-gilsb" |
| 63 | "-dilsb")) |
| 64 | (cons |
| 65 | (format "-exec ls -ld {} %s" find-exec-terminator) |
| 66 | "-ld")) |
| 67 | "A pair of options to produce and parse an `ls -l'-type list from `find'. |
| 68 | This is a cons of two strings (FIND-OPTION . LS-SWITCHES). |
| 69 | FIND-OPTION is the option (or options) passed to `find' to produce |
| 70 | a file listing in the desired format. LS-SWITCHES is a set of |
| 71 | `ls' switches that tell dired how to parse the output of `find'. |
| 72 | |
| 73 | The two options must be set to compatible values. |
| 74 | For example, to use human-readable file sizes with GNU ls: |
| 75 | \(\"-exec ls -ldh {} +\" . \"-ldh\") |
| 76 | |
| 77 | To use GNU find's inbuilt \"-ls\" option to list files: |
| 78 | \(\"-ls\" . \"-dilsb\") |
| 79 | since GNU find's output has the same format as using GNU ls with |
| 80 | the options \"-dilsb\"." |
| 81 | :version "24.1" ; add tests for -ls and -exec + support |
| 82 | :type '(cons (string :tag "Find Option") |
| 83 | (string :tag "Ls Switches")) |
| 84 | :group 'find-dired) |
| 85 | |
| 86 | (defcustom find-ls-subdir-switches |
| 87 | (if (string-match "-[a-z]*b" (cdr find-ls-option)) |
| 88 | "-alb" |
| 89 | "-al") |
| 90 | "`ls' switches for inserting subdirectories in `*Find*' buffers. |
| 91 | This should contain the \"-l\" switch. |
| 92 | Use the \"-F\" or \"-b\" switches if and only if you also use |
| 93 | them for `find-ls-option'." |
| 94 | :version "24.1" ; add -b test |
| 95 | :type 'string |
| 96 | :group 'find-dired) |
| 97 | |
| 98 | (defcustom find-grep-options |
| 99 | (if (or (eq system-type 'berkeley-unix) |
| 100 | (string-match "solaris2\\|irix" system-configuration)) |
| 101 | "-s" "-q") |
| 102 | "Option to grep to be as silent as possible. |
| 103 | On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it. |
| 104 | On other systems, the closest you can come is to use `-l'." |
| 105 | :type 'string |
| 106 | :group 'find-dired) |
| 107 | |
| 108 | ;; This used to be autoloaded (see bug#4387). |
| 109 | (defcustom find-name-arg |
| 110 | (if read-file-name-completion-ignore-case |
| 111 | "-iname" |
| 112 | "-name") |
| 113 | "Argument used to specify file name pattern. |
| 114 | If `read-file-name-completion-ignore-case' is non-nil, -iname is used so that |
| 115 | find also ignores case. Otherwise, -name is used." |
| 116 | :type 'string |
| 117 | :group 'find-dired |
| 118 | :version "22.2") |
| 119 | |
| 120 | (defvar find-args nil |
| 121 | "Last arguments given to `find' by \\[find-dired].") |
| 122 | |
| 123 | ;; History of find-args values entered in the minibuffer. |
| 124 | (defvar find-args-history nil) |
| 125 | |
| 126 | (defvar dired-sort-inhibit) |
| 127 | |
| 128 | ;;;###autoload |
| 129 | (defun find-dired (dir args) |
| 130 | "Run `find' and go into Dired mode on a buffer of the output. |
| 131 | The command run (after changing into DIR) is essentially |
| 132 | |
| 133 | find . \\( ARGS \\) -ls |
| 134 | |
| 135 | except that the car of the variable `find-ls-option' specifies what to |
| 136 | use in place of \"-ls\" as the final argument." |
| 137 | (interactive (list (read-directory-name "Run find in directory: " nil "" t) |
| 138 | (read-string "Run find (with args): " find-args |
| 139 | '(find-args-history . 1)))) |
| 140 | (let ((dired-buffers dired-buffers)) |
| 141 | ;; Expand DIR ("" means default-directory), and make sure it has a |
| 142 | ;; trailing slash. |
| 143 | (setq dir (file-name-as-directory (expand-file-name dir))) |
| 144 | ;; Check that it's really a directory. |
| 145 | (or (file-directory-p dir) |
| 146 | (error "find-dired needs a directory: %s" dir)) |
| 147 | (switch-to-buffer (get-buffer-create "*Find*")) |
| 148 | |
| 149 | ;; See if there's still a `find' running, and offer to kill |
| 150 | ;; it first, if it is. |
| 151 | (let ((find (get-buffer-process (current-buffer)))) |
| 152 | (when find |
| 153 | (if (or (not (eq (process-status find) 'run)) |
| 154 | (yes-or-no-p "A `find' process is running; kill it? ")) |
| 155 | (condition-case nil |
| 156 | (progn |
| 157 | (interrupt-process find) |
| 158 | (sit-for 1) |
| 159 | (delete-process find)) |
| 160 | (error nil)) |
| 161 | (error "Cannot have two processes in `%s' at once" (buffer-name))))) |
| 162 | |
| 163 | (widen) |
| 164 | (kill-all-local-variables) |
| 165 | (setq buffer-read-only nil) |
| 166 | (erase-buffer) |
| 167 | (setq default-directory dir |
| 168 | find-args args ; save for next interactive call |
| 169 | args (concat find-program " . " |
| 170 | (if (string= args "") |
| 171 | "" |
| 172 | (concat |
| 173 | (shell-quote-argument "(") |
| 174 | " " args " " |
| 175 | (shell-quote-argument ")") |
| 176 | " ")) |
| 177 | (if (string-match "\\`\\(.*\\) {} \\(\\\\;\\|+\\)\\'" |
| 178 | (car find-ls-option)) |
| 179 | (format "%s %s %s" |
| 180 | (match-string 1 (car find-ls-option)) |
| 181 | (shell-quote-argument "{}") |
| 182 | find-exec-terminator) |
| 183 | (car find-ls-option)))) |
| 184 | ;; Start the find process. |
| 185 | (shell-command (concat args "&") (current-buffer)) |
| 186 | ;; The next statement will bomb in classic dired (no optional arg allowed) |
| 187 | (dired-mode dir (cdr find-ls-option)) |
| 188 | (let ((map (make-sparse-keymap))) |
| 189 | (set-keymap-parent map (current-local-map)) |
| 190 | (define-key map "\C-c\C-k" 'kill-find) |
| 191 | (use-local-map map)) |
| 192 | (make-local-variable 'dired-sort-inhibit) |
| 193 | (setq dired-sort-inhibit t) |
| 194 | (set (make-local-variable 'revert-buffer-function) |
| 195 | `(lambda (ignore-auto noconfirm) |
| 196 | (find-dired ,dir ,find-args))) |
| 197 | ;; Set subdir-alist so that Tree Dired will work: |
| 198 | (if (fboundp 'dired-simple-subdir-alist) |
| 199 | ;; will work even with nested dired format (dired-nstd.el,v 1.15 |
| 200 | ;; and later) |
| 201 | (dired-simple-subdir-alist) |
| 202 | ;; else we have an ancient tree dired (or classic dired, where |
| 203 | ;; this does no harm) |
| 204 | (set (make-local-variable 'dired-subdir-alist) |
| 205 | (list (cons default-directory (point-min-marker))))) |
| 206 | (set (make-local-variable 'dired-subdir-switches) find-ls-subdir-switches) |
| 207 | (setq buffer-read-only nil) |
| 208 | ;; Subdir headlerline must come first because the first marker in |
| 209 | ;; subdir-alist points there. |
| 210 | (insert " " dir ":\n") |
| 211 | ;; Make second line a ``find'' line in analogy to the ``total'' or |
| 212 | ;; ``wildcard'' line. |
| 213 | (let ((point (point))) |
| 214 | (insert " " args "\n") |
| 215 | (dired-insert-set-properties point (point))) |
| 216 | (setq buffer-read-only t) |
| 217 | (let ((proc (get-buffer-process (current-buffer)))) |
| 218 | (set-process-filter proc (function find-dired-filter)) |
| 219 | (set-process-sentinel proc (function find-dired-sentinel)) |
| 220 | ;; Initialize the process marker; it is used by the filter. |
| 221 | (move-marker (process-mark proc) (point) (current-buffer))) |
| 222 | (setq mode-line-process '(":%s")))) |
| 223 | |
| 224 | (defun kill-find () |
| 225 | "Kill the `find' process running in the current buffer." |
| 226 | (interactive) |
| 227 | (let ((find (get-buffer-process (current-buffer)))) |
| 228 | (and find (eq (process-status find) 'run) |
| 229 | (eq (process-filter find) (function find-dired-filter)) |
| 230 | (condition-case nil |
| 231 | (delete-process find) |
| 232 | (error nil))))) |
| 233 | |
| 234 | ;;;###autoload |
| 235 | (defun find-name-dired (dir pattern) |
| 236 | "Search DIR recursively for files matching the globbing pattern PATTERN, |
| 237 | and run Dired on those files. |
| 238 | PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted. |
| 239 | The default command run (after changing into DIR) is |
| 240 | |
| 241 | find . -name 'PATTERN' -ls |
| 242 | |
| 243 | See `find-name-arg' to customize the arguments." |
| 244 | (interactive |
| 245 | "DFind-name (directory): \nsFind-name (filename wildcard): ") |
| 246 | (find-dired dir (concat find-name-arg " " (shell-quote-argument pattern)))) |
| 247 | |
| 248 | ;; This functionality suggested by |
| 249 | ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc) |
| 250 | ;; Subject: find-dired, lookfor-dired |
| 251 | ;; Date: 10 May 91 17:50:00 GMT |
| 252 | ;; Organization: University of Waterloo |
| 253 | |
| 254 | (defalias 'lookfor-dired 'find-grep-dired) |
| 255 | ;;;###autoload |
| 256 | (defun find-grep-dired (dir regexp) |
| 257 | "Find files in DIR matching a regexp REGEXP and start Dired on output. |
| 258 | The command run (after changing into DIR) is |
| 259 | |
| 260 | find . \\( -type f -exec `grep-program' `find-grep-options' \\ |
| 261 | -e REGEXP {} \\; \\) -ls |
| 262 | |
| 263 | where the car of the variable `find-ls-option' specifies what to |
| 264 | use in place of \"-ls\" as the final argument." |
| 265 | ;; Doc used to say "Thus ARG can also contain additional grep options." |
| 266 | ;; i) Presumably ARG == REGEXP? |
| 267 | ;; ii) No it can't have options, since it gets shell-quoted. |
| 268 | (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ") |
| 269 | ;; find -exec doesn't allow shell i/o redirections in the command, |
| 270 | ;; or we could use `grep -l >/dev/null' |
| 271 | ;; We use -type f, not ! -type d, to avoid getting screwed |
| 272 | ;; by FIFOs and devices. I'm not sure what's best to do |
| 273 | ;; about symlinks, so as far as I know this is not wrong. |
| 274 | (find-dired dir |
| 275 | (concat "-type f -exec " grep-program " " find-grep-options " -e " |
| 276 | (shell-quote-argument regexp) |
| 277 | " " |
| 278 | (shell-quote-argument "{}") |
| 279 | " " |
| 280 | ;; Doesn't work with "+". |
| 281 | (shell-quote-argument ";")))) |
| 282 | |
| 283 | (defun find-dired-filter (proc string) |
| 284 | ;; Filter for \\[find-dired] processes. |
| 285 | (let ((buf (process-buffer proc)) |
| 286 | (inhibit-read-only t)) |
| 287 | (if (buffer-name buf) |
| 288 | (with-current-buffer buf |
| 289 | (save-excursion |
| 290 | (save-restriction |
| 291 | (widen) |
| 292 | (let ((buffer-read-only nil) |
| 293 | (beg (point-max)) |
| 294 | (l-opt (and (consp find-ls-option) |
| 295 | (string-match "l" (cdr find-ls-option)))) |
| 296 | (ls-regexp (concat "^ +[^ \t\r\n]+\\( +[^ \t\r\n]+\\) +" |
| 297 | "[^ \t\r\n]+ +[^ \t\r\n]+\\( +[0-9]+\\)"))) |
| 298 | (goto-char beg) |
| 299 | (insert string) |
| 300 | (goto-char beg) |
| 301 | (or (looking-at "^") |
| 302 | (forward-line 1)) |
| 303 | (while (looking-at "^") |
| 304 | (insert " ") |
| 305 | (forward-line 1)) |
| 306 | ;; Convert ` ./FILE' to ` FILE' |
| 307 | ;; This would lose if the current chunk of output |
| 308 | ;; starts or ends within the ` ./', so back up a bit: |
| 309 | (goto-char (- beg 3)) ; no error if < 0 |
| 310 | (while (search-forward " ./" nil t) |
| 311 | (delete-region (point) (- (point) 2))) |
| 312 | ;; Pad the number of links and file size. This is a |
| 313 | ;; quick and dirty way of getting the columns to line up |
| 314 | ;; most of the time, but it's not foolproof. |
| 315 | (when l-opt |
| 316 | (goto-char beg) |
| 317 | (goto-char (line-beginning-position)) |
| 318 | (while (re-search-forward ls-regexp nil t) |
| 319 | (replace-match (format "%4s" (match-string 1)) |
| 320 | nil nil nil 1) |
| 321 | (replace-match (format "%9s" (match-string 2)) |
| 322 | nil nil nil 2) |
| 323 | (forward-line 1))) |
| 324 | ;; Find all the complete lines in the unprocessed |
| 325 | ;; output and process it to add text properties. |
| 326 | (goto-char (point-max)) |
| 327 | (if (search-backward "\n" (process-mark proc) t) |
| 328 | (progn |
| 329 | (dired-insert-set-properties (process-mark proc) |
| 330 | (1+ (point))) |
| 331 | (move-marker (process-mark proc) (1+ (point))))))))) |
| 332 | ;; The buffer has been killed. |
| 333 | (delete-process proc)))) |
| 334 | |
| 335 | (defun find-dired-sentinel (proc state) |
| 336 | ;; Sentinel for \\[find-dired] processes. |
| 337 | (let ((buf (process-buffer proc)) |
| 338 | (inhibit-read-only t)) |
| 339 | (if (buffer-name buf) |
| 340 | (with-current-buffer buf |
| 341 | (let ((buffer-read-only nil)) |
| 342 | (save-excursion |
| 343 | (goto-char (point-max)) |
| 344 | (let ((point (point))) |
| 345 | (insert "\n find " state) |
| 346 | (forward-char -1) ;Back up before \n at end of STATE. |
| 347 | (insert " at " (substring (current-time-string) 0 19)) |
| 348 | (dired-insert-set-properties point (point))) |
| 349 | (setq mode-line-process |
| 350 | (concat ":" |
| 351 | (symbol-name (process-status proc)))) |
| 352 | ;; Since the buffer and mode line will show that the |
| 353 | ;; process is dead, we can delete it now. Otherwise it |
| 354 | ;; will stay around until M-x list-processes. |
| 355 | (delete-process proc) |
| 356 | (force-mode-line-update))) |
| 357 | (message "find-dired %s finished." (current-buffer)))))) |
| 358 | |
| 359 | \f |
| 360 | (provide 'find-dired) |
| 361 | |
| 362 | ;;; find-dired.el ends here |