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