* lisp/subr.el (internal--called-interactively-p--get-frame): Avoid filling
[emacs.git] / lisp / find-dired.el
blobae7cc1c8629351af5763e3f0d0bac23687d33cee
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 (insert " " args "\n")
214 (setq buffer-read-only t)
215 (let ((proc (get-buffer-process (current-buffer))))
216 (set-process-filter proc (function find-dired-filter))
217 (set-process-sentinel proc (function find-dired-sentinel))
218 ;; Initialize the process marker; it is used by the filter.
219 (move-marker (process-mark proc) 1 (current-buffer)))
220 (setq mode-line-process '(":%s"))))
222 (defun kill-find ()
223 "Kill the `find' process running in the current buffer."
224 (interactive)
225 (let ((find (get-buffer-process (current-buffer))))
226 (and find (eq (process-status find) 'run)
227 (eq (process-filter find) (function find-dired-filter))
228 (condition-case nil
229 (delete-process find)
230 (error nil)))))
232 ;;;###autoload
233 (defun find-name-dired (dir pattern)
234 "Search DIR recursively for files matching the globbing pattern PATTERN,
235 and run dired on those files.
236 PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
237 The command run (after changing into DIR) is
239 find . -name 'PATTERN' -ls"
240 (interactive
241 "DFind-name (directory): \nsFind-name (filename wildcard): ")
242 (find-dired dir (concat find-name-arg " " (shell-quote-argument pattern))))
244 ;; This functionality suggested by
245 ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
246 ;; Subject: find-dired, lookfor-dired
247 ;; Date: 10 May 91 17:50:00 GMT
248 ;; Organization: University of Waterloo
250 (defalias 'lookfor-dired 'find-grep-dired)
251 ;;;###autoload
252 (defun find-grep-dired (dir regexp)
253 "Find files in DIR containing a regexp REGEXP and start Dired on output.
254 The command run (after changing into DIR) is
256 find . \\( -type f -exec `grep-program' `find-grep-options' \\
257 -e REGEXP {} \\; \\) -ls
259 where the car of the variable `find-ls-option' specifies what to
260 use in place of \"-ls\" as the final argument."
261 ;; Doc used to say "Thus ARG can also contain additional grep options."
262 ;; i) Presumably ARG == REGEXP?
263 ;; ii) No it can't have options, since it gets shell-quoted.
264 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
265 ;; find -exec doesn't allow shell i/o redirections in the command,
266 ;; or we could use `grep -l >/dev/null'
267 ;; We use -type f, not ! -type d, to avoid getting screwed
268 ;; by FIFOs and devices. I'm not sure what's best to do
269 ;; about symlinks, so as far as I know this is not wrong.
270 (find-dired dir
271 (concat "-type f -exec " grep-program " " find-grep-options " -e "
272 (shell-quote-argument regexp)
274 (shell-quote-argument "{}")
276 ;; Doesn't work with "+".
277 (shell-quote-argument ";"))))
279 (defun find-dired-filter (proc string)
280 ;; Filter for \\[find-dired] processes.
281 (let ((buf (process-buffer proc))
282 (inhibit-read-only t))
283 (if (buffer-name buf)
284 (with-current-buffer buf
285 (save-excursion
286 (save-restriction
287 (widen)
288 (let ((buffer-read-only nil)
289 (beg (point-max))
290 (l-opt (and (consp find-ls-option)
291 (string-match "l" (cdr find-ls-option))))
292 (ls-regexp (concat "^ +[^ \t\r\n]+\\( +[^ \t\r\n]+\\) +"
293 "[^ \t\r\n]+ +[^ \t\r\n]+\\( +[0-9]+\\)")))
294 (goto-char beg)
295 (insert string)
296 (goto-char beg)
297 (or (looking-at "^")
298 (forward-line 1))
299 (while (looking-at "^")
300 (insert " ")
301 (forward-line 1))
302 ;; Convert ` ./FILE' to ` FILE'
303 ;; This would lose if the current chunk of output
304 ;; starts or ends within the ` ./', so back up a bit:
305 (goto-char (- beg 3)) ; no error if < 0
306 (while (search-forward " ./" nil t)
307 (delete-region (point) (- (point) 2)))
308 ;; Pad the number of links and file size. This is a
309 ;; quick and dirty way of getting the columns to line up
310 ;; most of the time, but it's not foolproof.
311 (when l-opt
312 (goto-char beg)
313 (goto-char (line-beginning-position))
314 (while (re-search-forward ls-regexp nil t)
315 (replace-match (format "%4s" (match-string 1))
316 nil nil nil 1)
317 (replace-match (format "%9s" (match-string 2))
318 nil nil nil 2)
319 (forward-line 1)))
320 ;; Find all the complete lines in the unprocessed
321 ;; output and process it to add text properties.
322 (goto-char (point-max))
323 (if (search-backward "\n" (process-mark proc) t)
324 (progn
325 (dired-insert-set-properties (process-mark proc)
326 (1+ (point)))
327 (move-marker (process-mark proc) (1+ (point)))))))))
328 ;; The buffer has been killed.
329 (delete-process proc))))
331 (defun find-dired-sentinel (proc state)
332 ;; Sentinel for \\[find-dired] processes.
333 (let ((buf (process-buffer proc))
334 (inhibit-read-only t))
335 (if (buffer-name buf)
336 (with-current-buffer buf
337 (let ((buffer-read-only nil))
338 (save-excursion
339 (goto-char (point-max))
340 (insert "\n find " state)
341 (forward-char -1) ;Back up before \n at end of STATE.
342 (insert " at " (substring (current-time-string) 0 19))
343 (forward-char 1)
344 (setq mode-line-process
345 (concat ":"
346 (symbol-name (process-status proc))))
347 ;; Since the buffer and mode line will show that the
348 ;; process is dead, we can delete it now. Otherwise it
349 ;; will stay around until M-x list-processes.
350 (delete-process proc)
351 (force-mode-line-update)))
352 (message "find-dired %s finished." (current-buffer))))))
355 (provide 'find-dired)
357 ;;; find-dired.el ends here