Install the actual changes missing in previous revision
[emacs.git] / lisp / find-dired.el
blobf9a0c015bf73d8dd5fc61e2c1a0eef5f579b2e7f
1 ;;; find-dired.el --- run a `find' command and dired the output
3 ;; Copyright (C) 1992, 1994-1995, 2000-2013 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Roland McGrath <roland@gnu.org>,
7 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
8 ;; Maintainer: FSF
9 ;; Keywords: unix
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/>.
26 ;;; Commentary:
28 ;;; Code:
30 (require 'dired)
32 (defgroup find-dired nil
33 "Run a `find' command and dired the output."
34 :group 'dired
35 :prefix "find-")
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)
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'.
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"))
84 :group 'find-dired)
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)
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)
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")
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)
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
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
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*"))
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)))))
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 "")
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"))))
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)))))
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 command run (after changing into DIR) is
241 find . -name 'PATTERN' -ls"
242 (interactive
243 "DFind-name (directory): \nsFind-name (filename wildcard): ")
244 (find-dired dir (concat find-name-arg " " (shell-quote-argument pattern))))
246 ;; This functionality suggested by
247 ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
248 ;; Subject: find-dired, lookfor-dired
249 ;; Date: 10 May 91 17:50:00 GMT
250 ;; Organization: University of Waterloo
252 (defalias 'lookfor-dired 'find-grep-dired)
253 ;;;###autoload
254 (defun find-grep-dired (dir regexp)
255 "Find files in DIR containing a regexp REGEXP and start Dired on output.
256 The command run (after changing into DIR) is
258 find . \\( -type f -exec `grep-program' `find-grep-options' \\
259 -e REGEXP {} \\; \\) -ls
261 where the car of the variable `find-ls-option' specifies what to
262 use in place of \"-ls\" as the final argument."
263 ;; Doc used to say "Thus ARG can also contain additional grep options."
264 ;; i) Presumably ARG == REGEXP?
265 ;; ii) No it can't have options, since it gets shell-quoted.
266 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
267 ;; find -exec doesn't allow shell i/o redirections in the command,
268 ;; or we could use `grep -l >/dev/null'
269 ;; We use -type f, not ! -type d, to avoid getting screwed
270 ;; by FIFOs and devices. I'm not sure what's best to do
271 ;; about symlinks, so as far as I know this is not wrong.
272 (find-dired dir
273 (concat "-type f -exec " grep-program " " find-grep-options " -e "
274 (shell-quote-argument regexp)
276 (shell-quote-argument "{}")
278 ;; Doesn't work with "+".
279 (shell-quote-argument ";"))))
281 (defun find-dired-filter (proc string)
282 ;; Filter for \\[find-dired] processes.
283 (let ((buf (process-buffer proc))
284 (inhibit-read-only t))
285 (if (buffer-name buf)
286 (with-current-buffer buf
287 (save-excursion
288 (save-restriction
289 (widen)
290 (let ((buffer-read-only nil)
291 (beg (point-max))
292 (l-opt (and (consp find-ls-option)
293 (string-match "l" (cdr find-ls-option))))
294 (ls-regexp (concat "^ +[^ \t\r\n]+\\( +[^ \t\r\n]+\\) +"
295 "[^ \t\r\n]+ +[^ \t\r\n]+\\( +[0-9]+\\)")))
296 (goto-char beg)
297 (insert string)
298 (goto-char beg)
299 (or (looking-at "^")
300 (forward-line 1))
301 (while (looking-at "^")
302 (insert " ")
303 (forward-line 1))
304 ;; Convert ` ./FILE' to ` FILE'
305 ;; This would lose if the current chunk of output
306 ;; starts or ends within the ` ./', so back up a bit:
307 (goto-char (- beg 3)) ; no error if < 0
308 (while (search-forward " ./" nil t)
309 (delete-region (point) (- (point) 2)))
310 ;; Pad the number of links and file size. This is a
311 ;; quick and dirty way of getting the columns to line up
312 ;; most of the time, but it's not foolproof.
313 (when l-opt
314 (goto-char beg)
315 (goto-char (line-beginning-position))
316 (while (re-search-forward ls-regexp nil t)
317 (replace-match (format "%4s" (match-string 1))
318 nil nil nil 1)
319 (replace-match (format "%9s" (match-string 2))
320 nil nil nil 2)
321 (forward-line 1)))
322 ;; Find all the complete lines in the unprocessed
323 ;; output and process it to add text properties.
324 (goto-char (point-max))
325 (if (search-backward "\n" (process-mark proc) t)
326 (progn
327 (dired-insert-set-properties (process-mark proc)
328 (1+ (point)))
329 (move-marker (process-mark proc) (1+ (point)))))))))
330 ;; The buffer has been killed.
331 (delete-process proc))))
333 (defun find-dired-sentinel (proc state)
334 ;; Sentinel for \\[find-dired] processes.
335 (let ((buf (process-buffer proc))
336 (inhibit-read-only t))
337 (if (buffer-name buf)
338 (with-current-buffer buf
339 (let ((buffer-read-only nil))
340 (save-excursion
341 (goto-char (point-max))
342 (let ((point (point)))
343 (insert "\n find " state)
344 (forward-char -1) ;Back up before \n at end of STATE.
345 (insert " at " (substring (current-time-string) 0 19))
346 (dired-insert-set-properties point (point)))
347 (setq mode-line-process
348 (concat ":"
349 (symbol-name (process-status proc))))
350 ;; Since the buffer and mode line will show that the
351 ;; process is dead, we can delete it now. Otherwise it
352 ;; will stay around until M-x list-processes.
353 (delete-process proc)
354 (force-mode-line-update)))
355 (message "find-dired %s finished." (current-buffer))))))
358 (provide 'find-dired)
360 ;;; find-dired.el ends here