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