(lisp-mode-variables): Set it.
[bpt/emacs.git] / lisp / find-dired.el
CommitLineData
3b1e4dd1 1;;; find-dired.el --- run a `find' command and dired the output
c0274f38 2
62c804f8 3;; Copyright (C) 1992, 1994, 1995, 2000, 2002 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
55535639
PJ
27;;; Commentary:
28
e5167999
ER
29;;; Code:
30
aa228418 31(require 'dired)
a0f81711 32
33933d45
AS
33(defgroup find-dired nil
34 "Run a `find' command and dired the output."
35 :group 'dired
36 :prefix "find-")
37
d9823c3c
PB
38(defcustom find-dired-find-program "find"
39 "Program used to find files."
40 :group 'dired
8ad5033b 41 :type 'file)
d9823c3c 42
c0d79871 43;; find's -ls corresponds to these switches.
347cd536 44;; Note -b, at least GNU find quotes spaces etc. in filenames
aa228418 45;;;###autoload
33933d45
AS
46(defcustom find-ls-option
47 (if (eq system-type 'berkeley-unix) '("-ls" . "-gilsb")
48 '("-exec ls -ld {} \\;" . "-ld"))
347cd536
RM
49 "*Description of the option to `find' to produce an `ls -l'-type listing.
50This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION
51gives the option (or options) to `find' that produce the desired output.
33933d45
AS
52LS-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)
aa228418
JB
56
57;;;###autoload
33933d45 58(defcustom find-grep-options
9f18e619
RS
59 (if (or (eq system-type 'berkeley-unix)
60 (string-match "solaris2" system-configuration)
61 (string-match "irix" system-configuration))
62 "-s" "-q")
aa228418 63 "*Option to grep to be as silent as possible.
efa2bb9c 64On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
33933d45
AS
65On other systems, the closest you can come is to use `-l'."
66 :type 'string
67 :group 'find-dired)
aa228418
JB
68
69(defvar find-args nil
70 "Last arguments given to `find' by \\[find-dired].")
90e3797f 71
347cd536
RM
72;; History of find-args values entered in the minibuffer.
73(defvar find-args-history nil)
74
304d3e5d
RS
75(defvar dired-sort-inhibit)
76
90e3797f
RM
77;;;###autoload
78(defun find-dired (dir args)
0322a154 79 "Run `find' and go into Dired mode on a buffer of the output.
aa228418
JB
80The command run (after changing into DIR) is
81
db0fb4dd
RS
82 find . \\( ARGS \\) -ls
83
84except that the variable `find-ls-option' specifies what to use
85as the final argument."
90e3797f 86 (interactive (list (read-file-name "Run find in directory: " nil "" t)
347cd536
RM
87 (read-string "Run find (with args): " find-args
88 '(find-args-history . 1))))
0322a154
RS
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*"))
8ad5033b
GM
98
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)))))
71296446 112
0322a154
RS
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
d9823c3c 119 args (concat find-dired-find-program " . "
0322a154
RS
120 (if (string= args "")
121 ""
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))
1d0c2717
RS
126 (let ((map (make-sparse-keymap)))
127 (set-keymap-parent map (current-local-map))
128 (define-key map "\C-c\C-k" 'kill-find)
129 (use-local-map map))
304d3e5d
RS
130 (make-local-variable 'dired-sort-inhibit)
131 (setq dired-sort-inhibit t)
2b29f15c
MR
132 (set (make-local-variable 'revert-buffer-function)
133 `(lambda (ignore-auto noconfirm)
134 (find-dired ,dir ,find-args)))
0322a154
RS
135 ;; Set subdir-alist so that Tree Dired will work:
136 (if (fboundp 'dired-simple-subdir-alist)
137 ;; will work even with nested dired format (dired-nstd.el,v 1.15
138 ;; and later)
139 (dired-simple-subdir-alist)
140 ;; else we have an ancient tree dired (or classic dired, where
71296446 141 ;; this does no harm)
0322a154
RS
142 (set (make-local-variable 'dired-subdir-alist)
143 (list (cons default-directory (point-min-marker)))))
144 (setq buffer-read-only nil)
145 ;; Subdir headlerline must come first because the first marker in
146 ;; subdir-alist points there.
147 (insert " " dir ":\n")
148 ;; Make second line a ``find'' line in analogy to the ``total'' or
71296446 149 ;; ``wildcard'' line.
0322a154 150 (insert " " args "\n")
1d0c2717 151 (setq buffer-read-only t)
0322a154 152 ;; Start the find process.
d9823c3c 153 (let ((proc (start-process-shell-command find-dired-find-program (current-buffer) args)))
0322a154
RS
154 (set-process-filter proc (function find-dired-filter))
155 (set-process-sentinel proc (function find-dired-sentinel))
156 ;; Initialize the process marker; it is used by the filter.
157 (move-marker (process-mark proc) 1 (current-buffer)))
158 (setq mode-line-process '(":%s"))))
90e3797f 159
1d0c2717
RS
160(defun kill-find ()
161 "Kill the `find' process running in the current buffer."
162 (interactive)
163 (let ((find (get-buffer-process (current-buffer))))
164 (and find (eq (process-status find) 'run)
165 (eq (process-filter find) (function find-dired-filter))
166 (condition-case nil
167 (delete-process find)
168 (error nil)))))
169
90e3797f
RM
170;;;###autoload
171(defun find-name-dired (dir pattern)
172 "Search DIR recursively for files matching the globbing pattern PATTERN,
aa228418
JB
173and run dired on those files.
174PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
175The command run (after changing into DIR) is
176
177 find . -name 'PATTERN' -ls"
178 (interactive
179 "DFind-name (directory): \nsFind-name (filename wildcard): ")
62c804f8 180 (find-dired dir (concat "-name " (shell-quote-argument pattern))))
90e3797f 181
aa228418
JB
182;; This functionality suggested by
183;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
184;; Subject: find-dired, lookfor-dired
185;; Date: 10 May 91 17:50:00 GMT
186;; Organization: University of Waterloo
187
31e1d920 188(defalias 'lookfor-dired 'find-grep-dired)
aa228418 189;;;###autoload
4367c5a2
RS
190(defun find-grep-dired (dir regexp)
191 "Find files in DIR containing a regexp REGEXP and start Dired on output.
aa228418
JB
192The command run (after changing into DIR) is
193
4367c5a2 194 find . -exec grep -s -e REGEXP {} \\\; -ls
aa228418
JB
195
196Thus ARG can also contain additional grep options."
efa2bb9c 197 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
aa228418
JB
198 ;; find -exec doesn't allow shell i/o redirections in the command,
199 ;; or we could use `grep -l >/dev/null'
3443c832
RS
200 ;; We use -type f, not ! -type d, to avoid getting screwed
201 ;; by FIFOs and devices. I'm not sure what's best to do
202 ;; about symlinks, so as far as I know this is not wrong.
aa228418 203 (find-dired dir
a086a7fe 204 (concat "-type f -exec grep " find-grep-options " -e "
4367c5a2 205 (shell-quote-argument regexp)
bd7887c6 206 " {} \\\; ")))
aa228418 207
90e3797f
RM
208(defun find-dired-filter (proc string)
209 ;; Filter for \\[find-dired] processes.
1d0c2717
RS
210 (let ((buf (process-buffer proc))
211 (inhibit-read-only t))
c23ae044
RM
212 (if (buffer-name buf) ; not killed?
213 (save-excursion
214 (set-buffer buf)
215 (save-restriction
216 (widen)
217 (save-excursion
218 (let ((buffer-read-only nil)
219 (end (point-max)))
220 (goto-char end)
221 (insert string)
222 (goto-char end)
223 (or (looking-at "^")
224 (forward-line 1))
225 (while (looking-at "^")
226 (insert " ")
aa228418
JB
227 (forward-line 1))
228 ;; Convert ` ./FILE' to ` FILE'
229 ;; This would lose if the current chunk of output
347cd536 230 ;; starts or ends within the ` ./', so back up a bit:
aa228418
JB
231 (goto-char (- end 3)) ; no error if < 0
232 (while (search-forward " ./" nil t)
347cd536
RM
233 (delete-region (point) (- (point) 2)))
234 ;; Find all the complete lines in the unprocessed
235 ;; output and process it to add text properties.
236 (goto-char end)
237 (if (search-backward "\n" (process-mark proc) t)
238 (progn
239 (dired-insert-set-properties (process-mark proc)
240 (1+ (point)))
241 (move-marker (process-mark proc) (1+ (point)))))
242 ))))
c23ae044
RM
243 ;; The buffer has been killed.
244 (delete-process proc))))
90e3797f
RM
245
246(defun find-dired-sentinel (proc state)
247 ;; Sentinel for \\[find-dired] processes.
1d0c2717
RS
248 (let ((buf (process-buffer proc))
249 (inhibit-read-only t))
c23ae044
RM
250 (if (buffer-name buf)
251 (save-excursion
252 (set-buffer buf)
619c556e
RM
253 (let ((buffer-read-only nil))
254 (save-excursion
255 (goto-char (point-max))
0e842e15 256 (insert "\n find " state)
619c556e
RM
257 (forward-char -1) ;Back up before \n at end of STATE.
258 (insert " at " (substring (current-time-string) 0 19))
259 (forward-char 1)
260 (setq mode-line-process
6657eedf 261 (concat ":"
619c556e
RM
262 (symbol-name (process-status proc))))
263 ;; Since the buffer and mode line will show that the
264 ;; process is dead, we can delete it now. Otherwise it
265 ;; will stay around until M-x list-processes.
266 (delete-process proc)
74def44f 267 (force-mode-line-update)))
c23ae044 268 (message "find-dired %s finished." (current-buffer))))))
a0f81711 269\f
49116ac0
JB
270(provide 'find-dired)
271
ab5796a9 272;;; arch-tag: 8edece95-af00-4221-bc74-a4bd2f75f9b0
c0274f38 273;;; find-dired.el ends here