(send-mail-function): Customize.
[bpt/emacs.git] / lisp / find-dired.el
CommitLineData
3b1e4dd1 1;;; find-dired.el --- run a `find' command and dired the output
c0274f38 2
8ad5033b 3;; Copyright (C) 1992, 1994, 1995, 2000 Free Software Foundation, Inc.
3a801d0c 4
5762abec 5;; Author: Roland McGrath <roland@gnu.org>,
a0f81711 6;; Sebastian Kremer <sk@thp.uni-koeln.de>
5027054f 7;; Maintainer: FSF
fd7fa35a 8;; Keywords: unix
c23ae044 9
b578f267
EN
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 2, or (at your option)
15;; 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; 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.
e5167999 26
e5167999
ER
27;;; Code:
28
aa228418 29(require 'dired)
a0f81711 30
33933d45
AS
31(defgroup find-dired nil
32 "Run a `find' command and dired the output."
33 :group 'dired
34 :prefix "find-")
35
d9823c3c
PB
36(defcustom find-dired-find-program "find"
37 "Program used to find files."
38 :group 'dired
8ad5033b 39 :type 'file)
d9823c3c 40
c0d79871 41;; find's -ls corresponds to these switches.
347cd536 42;; Note -b, at least GNU find quotes spaces etc. in filenames
aa228418 43;;;###autoload
33933d45
AS
44(defcustom find-ls-option
45 (if (eq system-type 'berkeley-unix) '("-ls" . "-gilsb")
46 '("-exec ls -ld {} \\;" . "-ld"))
347cd536
RM
47 "*Description of the option to `find' to produce an `ls -l'-type listing.
48This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION
49gives the option (or options) to `find' that produce the desired output.
33933d45
AS
50LS-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)
aa228418
JB
54
55;;;###autoload
33933d45 56(defcustom find-grep-options
9f18e619
RS
57 (if (or (eq system-type 'berkeley-unix)
58 (string-match "solaris2" system-configuration)
59 (string-match "irix" system-configuration))
60 "-s" "-q")
aa228418 61 "*Option to grep to be as silent as possible.
efa2bb9c 62On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
33933d45
AS
63On other systems, the closest you can come is to use `-l'."
64 :type 'string
65 :group 'find-dired)
aa228418
JB
66
67(defvar find-args nil
68 "Last arguments given to `find' by \\[find-dired].")
90e3797f 69
347cd536
RM
70;; History of find-args values entered in the minibuffer.
71(defvar find-args-history nil)
72
90e3797f
RM
73;;;###autoload
74(defun find-dired (dir args)
0322a154 75 "Run `find' and go into Dired mode on a buffer of the output.
aa228418
JB
76The command run (after changing into DIR) is
77
db0fb4dd
RS
78 find . \\( ARGS \\) -ls
79
80except that the variable `find-ls-option' specifies what to use
81as the final argument."
90e3797f 82 (interactive (list (read-file-name "Run find in directory: " nil "" t)
347cd536
RM
83 (read-string "Run find (with args): " find-args
84 '(find-args-history . 1))))
0322a154
RS
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*"))
8ad5033b
GM
94
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)))))
108
0322a154
RS
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
d9823c3c 115 args (concat find-dired-find-program " . "
0322a154
RS
116 (if (string= args "")
117 ""
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.
d9823c3c 143 (let ((proc (start-process-shell-command find-dired-find-program (current-buffer) args)))
0322a154
RS
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"))))
90e3797f
RM
149
150;;;###autoload
151(defun find-name-dired (dir pattern)
152 "Search DIR recursively for files matching the globbing pattern PATTERN,
aa228418
JB
153and run dired on those files.
154PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
155The command run (after changing into DIR) is
156
157 find . -name 'PATTERN' -ls"
158 (interactive
159 "DFind-name (directory): \nsFind-name (filename wildcard): ")
90e3797f
RM
160 (find-dired dir (concat "-name '" pattern "'")))
161
aa228418
JB
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
167
31e1d920 168(defalias 'lookfor-dired 'find-grep-dired)
aa228418
JB
169;;;###autoload
170(defun find-grep-dired (dir args)
171 "Find files in DIR containing a regexp ARG and start Dired on output.
172The command run (after changing into DIR) is
173
174 find . -exec grep -s ARG {} \\\; -ls
175
176Thus ARG can also contain additional grep options."
efa2bb9c 177 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
aa228418
JB
178 ;; find -exec doesn't allow shell i/o redirections in the command,
179 ;; or we could use `grep -l >/dev/null'
3443c832
RS
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.
aa228418 183 (find-dired dir
3443c832 184 (concat "-type f -exec grep " find-grep-options " "
a0f81711 185 args " {} \\\; ")))
aa228418 186
90e3797f
RM
187(defun find-dired-filter (proc string)
188 ;; Filter for \\[find-dired] processes.
c23ae044
RM
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 " ")
aa228418
JB
205 (forward-line 1))
206 ;; Convert ` ./FILE' to ` FILE'
207 ;; This would lose if the current chunk of output
347cd536 208 ;; starts or ends within the ` ./', so back up a bit:
aa228418
JB
209 (goto-char (- end 3)) ; no error if < 0
210 (while (search-forward " ./" nil t)
347cd536
RM
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 ))))
c23ae044
RM
221 ;; The buffer has been killed.
222 (delete-process proc))))
90e3797f
RM
223
224(defun find-dired-sentinel (proc state)
225 ;; Sentinel for \\[find-dired] processes.
c23ae044
RM
226 (let ((buf (process-buffer proc)))
227 (if (buffer-name buf)
228 (save-excursion
229 (set-buffer buf)
619c556e
RM
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
6657eedf 238 (concat ":"
619c556e
RM
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)
74def44f 244 (force-mode-line-update)))
c23ae044 245 (message "find-dired %s finished." (current-buffer))))))
a0f81711 246\f
49116ac0
JB
247(provide 'find-dired)
248
c0274f38 249;;; find-dired.el ends here