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