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