1 ;;; find-dired.el --- run a `find' command and dired the output
3 ;; Copyright (C) 1992, 1994-1995, 2000-2015 Free Software Foundation,
6 ;; Author: Roland McGrath <roland@gnu.org>,
7 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
8 ;; Maintainer: emacs-devel@gnu.org
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
32 (defgroup find-dired nil
33 "Run a `find' command and Dired the output."
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
42 (process-file find-program nil nil nil
43 null-device
"-exec" "echo" "{}" "+")))
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
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
59 (process-file find-program nil nil nil null-device
"-ls")))
61 (if (eq system-type
'berkeley-unix
)
65 (format "-exec ls -ld {} %s" find-exec-terminator
)
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'.
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\")
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"))
86 (defcustom find-ls-subdir-switches
87 (if (string-match "-[a-z]*b" (cdr find-ls-option
))
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
98 (defcustom find-grep-options
99 (if (or (eq system-type
'berkeley-unix
)
100 (string-match "solaris2\\|irix" system-configuration
))
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'."
108 ;; This used to be autoloaded (see bug#4387).
109 (defcustom find-name-arg
110 (if read-file-name-completion-ignore-case
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."
120 (defvar find-args nil
121 "Last arguments given to `find' by \\[find-dired].")
123 ;; History of find-args values entered in the minibuffer.
124 (defvar find-args-history nil
)
126 (defvar dired-sort-inhibit
)
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
133 find . \\( ARGS \\) -ls
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
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*"))
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))))
153 (if (or (not (eq (process-status find
) 'run
))
155 (format-message "A `find' process is running; kill it? ")))
158 (interrupt-process find
)
160 (delete-process find
))
162 (error "Cannot have two processes in `%s' at once" (buffer-name)))))
165 (kill-all-local-variables)
166 (setq buffer-read-only nil
)
168 (setq default-directory dir
169 find-args args
; save for next interactive call
170 args
(concat find-program
" . "
171 (if (string= args
"")
174 (shell-quote-argument "(")
176 (shell-quote-argument ")")
178 (if (string-match "\\`\\(.*\\) {} \\(\\\\;\\|+\\)\\'"
179 (car find-ls-option
))
181 (match-string 1 (car find-ls-option
))
182 (shell-quote-argument "{}")
183 find-exec-terminator
)
184 (car find-ls-option
))))
185 ;; Start the find process.
186 (shell-command (concat args
"&") (current-buffer))
187 ;; The next statement will bomb in classic dired (no optional arg allowed)
188 (dired-mode dir
(cdr find-ls-option
))
189 (let ((map (make-sparse-keymap)))
190 (set-keymap-parent map
(current-local-map))
191 (define-key map
"\C-c\C-k" 'kill-find
)
193 (make-local-variable 'dired-sort-inhibit
)
194 (setq dired-sort-inhibit t
)
195 (set (make-local-variable 'revert-buffer-function
)
196 `(lambda (ignore-auto noconfirm
)
197 (find-dired ,dir
,find-args
)))
198 ;; Set subdir-alist so that Tree Dired will work:
199 (if (fboundp 'dired-simple-subdir-alist
)
200 ;; will work even with nested dired format (dired-nstd.el,v 1.15
202 (dired-simple-subdir-alist)
203 ;; else we have an ancient tree dired (or classic dired, where
204 ;; this does no harm)
205 (set (make-local-variable 'dired-subdir-alist
)
206 (list (cons default-directory
(point-min-marker)))))
207 (set (make-local-variable 'dired-subdir-switches
) find-ls-subdir-switches
)
208 (setq buffer-read-only nil
)
209 ;; Subdir headlerline must come first because the first marker in
210 ;; subdir-alist points there.
211 (insert " " dir
":\n")
212 ;; Make second line a ``find'' line in analogy to the ``total'' or
213 ;; ``wildcard'' line.
214 (let ((point (point)))
215 (insert " " args
"\n")
216 (dired-insert-set-properties point
(point)))
217 (setq buffer-read-only t
)
218 (let ((proc (get-buffer-process (current-buffer))))
219 (set-process-filter proc
(function find-dired-filter
))
220 (set-process-sentinel proc
(function find-dired-sentinel
))
221 ;; Initialize the process marker; it is used by the filter.
222 (move-marker (process-mark proc
) (point) (current-buffer)))
223 (setq mode-line-process
'(":%s"))))
226 "Kill the `find' process running in the current buffer."
228 (let ((find (get-buffer-process (current-buffer))))
229 (and find
(eq (process-status find
) 'run
)
230 (eq (process-filter find
) (function find-dired-filter
))
232 (delete-process find
)
236 (defun find-name-dired (dir pattern
)
237 "Search DIR recursively for files matching the globbing pattern PATTERN,
238 and run Dired on those files.
239 PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
240 The default command run (after changing into DIR) is
242 find . -name \\='PATTERN\\=' -ls
244 See `find-name-arg' to customize the arguments."
246 "DFind-name (directory): \nsFind-name (filename wildcard): ")
247 (find-dired dir
(concat find-name-arg
" " (shell-quote-argument pattern
))))
249 ;; This functionality suggested by
250 ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
251 ;; Subject: find-dired, lookfor-dired
252 ;; Date: 10 May 91 17:50:00 GMT
253 ;; Organization: University of Waterloo
255 (defalias 'lookfor-dired
'find-grep-dired
)
257 (defun find-grep-dired (dir regexp
)
258 "Find files in DIR matching a regexp REGEXP and start Dired on output.
259 The command run (after changing into DIR) is
261 find . \\( -type f -exec `grep-program' `find-grep-options' \\
262 -e REGEXP {} \\; \\) -ls
264 where the car of the variable `find-ls-option' specifies what to
265 use in place of \"-ls\" as the final argument."
266 ;; Doc used to say "Thus ARG can also contain additional grep options."
267 ;; i) Presumably ARG == REGEXP?
268 ;; ii) No it can't have options, since it gets shell-quoted.
269 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
270 ;; find -exec doesn't allow shell i/o redirections in the command,
271 ;; or we could use `grep -l >/dev/null'
272 ;; We use -type f, not ! -type d, to avoid getting screwed
273 ;; by FIFOs and devices. I'm not sure what's best to do
274 ;; about symlinks, so as far as I know this is not wrong.
276 (concat "-type f -exec " grep-program
" " find-grep-options
" -e "
277 (shell-quote-argument regexp
)
279 (shell-quote-argument "{}")
281 ;; Doesn't work with "+".
282 (shell-quote-argument ";"))))
284 (defun find-dired-filter (proc string
)
285 ;; Filter for \\[find-dired] processes.
286 (let ((buf (process-buffer proc
))
287 (inhibit-read-only t
))
288 (if (buffer-name buf
)
289 (with-current-buffer buf
293 (let ((buffer-read-only nil
)
295 (l-opt (and (consp find-ls-option
)
296 (string-match "l" (cdr find-ls-option
))))
297 (ls-regexp (concat "^ +[^ \t\r\n]+\\( +[^ \t\r\n]+\\) +"
298 "[^ \t\r\n]+ +[^ \t\r\n]+\\( +[0-9]+\\)")))
304 (while (looking-at "^")
307 ;; Convert ` ./FILE' to ` FILE'
308 ;; This would lose if the current chunk of output
309 ;; starts or ends within the ` ./', so back up a bit:
310 (goto-char (- beg
3)) ; no error if < 0
311 (while (search-forward " ./" nil t
)
312 (delete-region (point) (- (point) 2)))
313 ;; Pad the number of links and file size. This is a
314 ;; quick and dirty way of getting the columns to line up
315 ;; most of the time, but it's not foolproof.
318 (goto-char (line-beginning-position))
319 (while (re-search-forward ls-regexp nil t
)
320 (replace-match (format "%4s" (match-string 1))
322 (replace-match (format "%9s" (match-string 2))
325 ;; Find all the complete lines in the unprocessed
326 ;; output and process it to add text properties.
327 (goto-char (point-max))
328 (if (search-backward "\n" (process-mark proc
) t
)
330 (dired-insert-set-properties (process-mark proc
)
332 (move-marker (process-mark proc
) (1+ (point)))))))))
333 ;; The buffer has been killed.
334 (delete-process proc
))))
336 (defun find-dired-sentinel (proc state
)
337 ;; Sentinel for \\[find-dired] processes.
338 (let ((buf (process-buffer proc
))
339 (inhibit-read-only t
))
340 (if (buffer-name buf
)
341 (with-current-buffer buf
342 (let ((buffer-read-only nil
))
344 (goto-char (point-max))
345 (let ((point (point)))
346 (insert "\n find " state
)
347 (forward-char -
1) ;Back up before \n at end of STATE.
348 (insert " at " (substring (current-time-string) 0 19))
349 (dired-insert-set-properties point
(point)))
350 (setq mode-line-process
352 (symbol-name (process-status proc
))))
353 ;; Since the buffer and mode line will show that the
354 ;; process is dead, we can delete it now. Otherwise it
355 ;; will stay around until M-x list-processes.
356 (delete-process proc
)
357 (force-mode-line-update)))
358 (message "find-dired %s finished." (current-buffer))))))
361 (provide 'find-dired
)
363 ;;; find-dired.el ends here