(xml-parse-tag): Allow spaces between / and >.
[emacs.git] / lisp / find-dired.el
blob046a2b7dad65f45518cb9e63db6dfa52f16bde65
1 ;;; find-dired.el --- run a `find' command and dired the output
3 ;; Copyright (C) 1992, 1994, 1995, 2000, 2002 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 2, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;;; Code:
31 (require 'dired)
33 (defgroup find-dired nil
34 "Run a `find' command and dired the output."
35 :group 'dired
36 :prefix "find-")
38 (defcustom find-dired-find-program "find"
39 "Program used to find files."
40 :group 'dired
41 :type 'file)
43 ;; find's -ls corresponds to these switches.
44 ;; Note -b, at least GNU find quotes spaces etc. in filenames
45 ;;;###autoload
46 (defcustom find-ls-option
47 (if (eq system-type 'berkeley-unix) '("-ls" . "-gilsb")
48 '("-exec ls -ld {} \\;" . "-ld"))
49 "*Description of the option to `find' to produce an `ls -l'-type listing.
50 This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION
51 gives the option (or options) to `find' that produce the desired output.
52 LS-SWITCHES is a list of `ls' switches to tell dired how to parse the output."
53 :type '(cons (string :tag "Find Option")
54 (string :tag "Ls Switches"))
55 :group 'find-dired)
57 ;;;###autoload
58 (defcustom find-grep-options
59 (if (or (eq system-type 'berkeley-unix)
60 (string-match "solaris2" system-configuration)
61 (string-match "irix" system-configuration))
62 "-s" "-q")
63 "*Option to grep to be as silent as possible.
64 On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
65 On other systems, the closest you can come is to use `-l'."
66 :type 'string
67 :group 'find-dired)
69 (defvar find-args nil
70 "Last arguments given to `find' by \\[find-dired].")
72 ;; History of find-args values entered in the minibuffer.
73 (defvar find-args-history nil)
75 (defvar dired-sort-inhibit)
77 ;;;###autoload
78 (defun find-dired (dir args)
79 "Run `find' and go into Dired mode on a buffer of the output.
80 The command run (after changing into DIR) is
82 find . \\( ARGS \\) -ls
84 except that the variable `find-ls-option' specifies what to use
85 as the final argument."
86 (interactive (list (read-file-name "Run find in directory: " nil "" t)
87 (read-string "Run find (with args): " find-args
88 '(find-args-history . 1))))
89 (let ((dired-buffers dired-buffers))
90 ;; Expand DIR ("" means default-directory), and make sure it has a
91 ;; trailing slash.
92 (setq dir (abbreviate-file-name
93 (file-name-as-directory (expand-file-name dir))))
94 ;; Check that it's really a directory.
95 (or (file-directory-p dir)
96 (error "find-dired needs a directory: %s" dir))
97 (switch-to-buffer (get-buffer-create "*Find*"))
99 ;; See if there's still a `find' running, and offer to kill
100 ;; it first, if it is.
101 (let ((find (get-buffer-process (current-buffer))))
102 (when find
103 (if (or (not (eq (process-status find) 'run))
104 (yes-or-no-p "A `find' process is running; kill it? "))
105 (condition-case nil
106 (progn
107 (interrupt-process find)
108 (sit-for 1)
109 (delete-process find))
110 (error nil))
111 (error "Cannot have two processes in `%s' at once" (buffer-name)))))
113 (widen)
114 (kill-all-local-variables)
115 (setq buffer-read-only nil)
116 (erase-buffer)
117 (setq default-directory dir
118 find-args args ; save for next interactive call
119 args (concat find-dired-find-program " . "
120 (if (string= args "")
122 (concat "\\( " args " \\) "))
123 (car find-ls-option)))
124 ;; The next statement will bomb in classic dired (no optional arg allowed)
125 (dired-mode dir (cdr find-ls-option))
126 (make-local-variable 'dired-sort-inhibit)
127 (setq dired-sort-inhibit t)
128 (set (make-local-variable 'revert-buffer-function)
129 `(lambda (ignore-auto noconfirm)
130 (find-dired ,dir ,find-args)))
131 ;; Set subdir-alist so that Tree Dired will work:
132 (if (fboundp 'dired-simple-subdir-alist)
133 ;; will work even with nested dired format (dired-nstd.el,v 1.15
134 ;; and later)
135 (dired-simple-subdir-alist)
136 ;; else we have an ancient tree dired (or classic dired, where
137 ;; this does no harm)
138 (set (make-local-variable 'dired-subdir-alist)
139 (list (cons default-directory (point-min-marker)))))
140 (setq buffer-read-only nil)
141 ;; Subdir headlerline must come first because the first marker in
142 ;; subdir-alist points there.
143 (insert " " dir ":\n")
144 ;; Make second line a ``find'' line in analogy to the ``total'' or
145 ;; ``wildcard'' line.
146 (insert " " args "\n")
147 ;; Start the find process.
148 (let ((proc (start-process-shell-command find-dired-find-program (current-buffer) args)))
149 (set-process-filter proc (function find-dired-filter))
150 (set-process-sentinel proc (function find-dired-sentinel))
151 ;; Initialize the process marker; it is used by the filter.
152 (move-marker (process-mark proc) 1 (current-buffer)))
153 (setq mode-line-process '(":%s"))))
155 ;;;###autoload
156 (defun find-name-dired (dir pattern)
157 "Search DIR recursively for files matching the globbing pattern PATTERN,
158 and run dired on those files.
159 PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
160 The command run (after changing into DIR) is
162 find . -name 'PATTERN' -ls"
163 (interactive
164 "DFind-name (directory): \nsFind-name (filename wildcard): ")
165 (find-dired dir (concat "-name " (shell-quote-argument pattern))))
167 ;; This functionality suggested by
168 ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
169 ;; Subject: find-dired, lookfor-dired
170 ;; Date: 10 May 91 17:50:00 GMT
171 ;; Organization: University of Waterloo
173 (defalias 'lookfor-dired 'find-grep-dired)
174 ;;;###autoload
175 (defun find-grep-dired (dir regexp)
176 "Find files in DIR containing a regexp REGEXP and start Dired on output.
177 The command run (after changing into DIR) is
179 find . -exec grep -s -e REGEXP {} \\\; -ls
181 Thus ARG can also contain additional grep options."
182 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
183 ;; find -exec doesn't allow shell i/o redirections in the command,
184 ;; or we could use `grep -l >/dev/null'
185 ;; We use -type f, not ! -type d, to avoid getting screwed
186 ;; by FIFOs and devices. I'm not sure what's best to do
187 ;; about symlinks, so as far as I know this is not wrong.
188 (find-dired dir
189 (concat "-type f -exec grep " find-grep-options " -e "
190 (shell-quote-argument regexp)
191 " {} \\\; ")))
193 (defun find-dired-filter (proc string)
194 ;; Filter for \\[find-dired] processes.
195 (let ((buf (process-buffer proc)))
196 (if (buffer-name buf) ; not killed?
197 (save-excursion
198 (set-buffer buf)
199 (save-restriction
200 (widen)
201 (save-excursion
202 (let ((buffer-read-only nil)
203 (end (point-max)))
204 (goto-char end)
205 (insert string)
206 (goto-char end)
207 (or (looking-at "^")
208 (forward-line 1))
209 (while (looking-at "^")
210 (insert " ")
211 (forward-line 1))
212 ;; Convert ` ./FILE' to ` FILE'
213 ;; This would lose if the current chunk of output
214 ;; starts or ends within the ` ./', so back up a bit:
215 (goto-char (- end 3)) ; no error if < 0
216 (while (search-forward " ./" nil t)
217 (delete-region (point) (- (point) 2)))
218 ;; Find all the complete lines in the unprocessed
219 ;; output and process it to add text properties.
220 (goto-char end)
221 (if (search-backward "\n" (process-mark proc) t)
222 (progn
223 (dired-insert-set-properties (process-mark proc)
224 (1+ (point)))
225 (move-marker (process-mark proc) (1+ (point)))))
226 ))))
227 ;; The buffer has been killed.
228 (delete-process proc))))
230 (defun find-dired-sentinel (proc state)
231 ;; Sentinel for \\[find-dired] processes.
232 (let ((buf (process-buffer proc)))
233 (if (buffer-name buf)
234 (save-excursion
235 (set-buffer buf)
236 (let ((buffer-read-only nil))
237 (save-excursion
238 (goto-char (point-max))
239 (insert "\n find " state)
240 (forward-char -1) ;Back up before \n at end of STATE.
241 (insert " at " (substring (current-time-string) 0 19))
242 (forward-char 1)
243 (setq mode-line-process
244 (concat ":"
245 (symbol-name (process-status proc))))
246 ;; Since the buffer and mode line will show that the
247 ;; process is dead, we can delete it now. Otherwise it
248 ;; will stay around until M-x list-processes.
249 (delete-process proc)
250 (force-mode-line-update)))
251 (message "find-dired %s finished." (current-buffer))))))
253 (provide 'find-dired)
255 ;;; find-dired.el ends here