(inhibit-mark-movement): New defvar.
[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 56
a994ebb8
LT
57;;;###autoload
58(defcustom find-ls-subdir-switches "-al"
59 "`ls' switches for inserting subdirectories in `*Find*' buffers.
60This should contain the \"-l\" switch.
61Use the \"-F\" or \"-b\" switches if and only if you also use
62them for `find-ls-option'."
63 :type 'string
64 :group 'find-dired
65 :version "21.4")
66
aa228418 67;;;###autoload
33933d45 68(defcustom find-grep-options
9f18e619
RS
69 (if (or (eq system-type 'berkeley-unix)
70 (string-match "solaris2" system-configuration)
71 (string-match "irix" system-configuration))
72 "-s" "-q")
aa228418 73 "*Option to grep to be as silent as possible.
efa2bb9c 74On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
33933d45
AS
75On other systems, the closest you can come is to use `-l'."
76 :type 'string
77 :group 'find-dired)
aa228418
JB
78
79(defvar find-args nil
80 "Last arguments given to `find' by \\[find-dired].")
90e3797f 81
347cd536
RM
82;; History of find-args values entered in the minibuffer.
83(defvar find-args-history nil)
84
304d3e5d
RS
85(defvar dired-sort-inhibit)
86
90e3797f
RM
87;;;###autoload
88(defun find-dired (dir args)
0322a154 89 "Run `find' and go into Dired mode on a buffer of the output.
aa228418
JB
90The command run (after changing into DIR) is
91
db0fb4dd
RS
92 find . \\( ARGS \\) -ls
93
94except that the variable `find-ls-option' specifies what to use
95as the final argument."
90e3797f 96 (interactive (list (read-file-name "Run find in directory: " nil "" t)
347cd536
RM
97 (read-string "Run find (with args): " find-args
98 '(find-args-history . 1))))
0322a154
RS
99 (let ((dired-buffers dired-buffers))
100 ;; Expand DIR ("" means default-directory), and make sure it has a
101 ;; trailing slash.
a994ebb8 102 (setq dir (file-name-as-directory (expand-file-name dir)))
0322a154
RS
103 ;; Check that it's really a directory.
104 (or (file-directory-p dir)
105 (error "find-dired needs a directory: %s" dir))
106 (switch-to-buffer (get-buffer-create "*Find*"))
8ad5033b
GM
107
108 ;; See if there's still a `find' running, and offer to kill
109 ;; it first, if it is.
110 (let ((find (get-buffer-process (current-buffer))))
111 (when find
112 (if (or (not (eq (process-status find) 'run))
113 (yes-or-no-p "A `find' process is running; kill it? "))
114 (condition-case nil
115 (progn
116 (interrupt-process find)
117 (sit-for 1)
118 (delete-process find))
119 (error nil))
120 (error "Cannot have two processes in `%s' at once" (buffer-name)))))
71296446 121
0322a154
RS
122 (widen)
123 (kill-all-local-variables)
124 (setq buffer-read-only nil)
125 (erase-buffer)
126 (setq default-directory dir
a994ebb8 127 find-args args ; save for next interactive call
d9823c3c 128 args (concat find-dired-find-program " . "
0322a154
RS
129 (if (string= args "")
130 ""
131 (concat "\\( " args " \\) "))
132 (car find-ls-option)))
1a9c959b
MA
133 ;; Start the find process.
134 (shell-command (concat args "&") (current-buffer))
0322a154
RS
135 ;; The next statement will bomb in classic dired (no optional arg allowed)
136 (dired-mode dir (cdr find-ls-option))
1d0c2717
RS
137 (let ((map (make-sparse-keymap)))
138 (set-keymap-parent map (current-local-map))
139 (define-key map "\C-c\C-k" 'kill-find)
140 (use-local-map map))
304d3e5d
RS
141 (make-local-variable 'dired-sort-inhibit)
142 (setq dired-sort-inhibit t)
2b29f15c
MR
143 (set (make-local-variable 'revert-buffer-function)
144 `(lambda (ignore-auto noconfirm)
145 (find-dired ,dir ,find-args)))
0322a154
RS
146 ;; Set subdir-alist so that Tree Dired will work:
147 (if (fboundp 'dired-simple-subdir-alist)
148 ;; will work even with nested dired format (dired-nstd.el,v 1.15
149 ;; and later)
150 (dired-simple-subdir-alist)
151 ;; else we have an ancient tree dired (or classic dired, where
71296446 152 ;; this does no harm)
0322a154
RS
153 (set (make-local-variable 'dired-subdir-alist)
154 (list (cons default-directory (point-min-marker)))))
a994ebb8 155 (set (make-local-variable 'dired-subdir-switches) find-ls-subdir-switches)
0322a154
RS
156 (setq buffer-read-only nil)
157 ;; Subdir headlerline must come first because the first marker in
158 ;; subdir-alist points there.
159 (insert " " dir ":\n")
160 ;; Make second line a ``find'' line in analogy to the ``total'' or
71296446 161 ;; ``wildcard'' line.
0322a154 162 (insert " " args "\n")
1d0c2717 163 (setq buffer-read-only t)
1a9c959b 164 (let ((proc (get-buffer-process (current-buffer))))
0322a154
RS
165 (set-process-filter proc (function find-dired-filter))
166 (set-process-sentinel proc (function find-dired-sentinel))
167 ;; Initialize the process marker; it is used by the filter.
168 (move-marker (process-mark proc) 1 (current-buffer)))
169 (setq mode-line-process '(":%s"))))
90e3797f 170
1d0c2717
RS
171(defun kill-find ()
172 "Kill the `find' process running in the current buffer."
173 (interactive)
174 (let ((find (get-buffer-process (current-buffer))))
175 (and find (eq (process-status find) 'run)
176 (eq (process-filter find) (function find-dired-filter))
177 (condition-case nil
178 (delete-process find)
179 (error nil)))))
180
90e3797f
RM
181;;;###autoload
182(defun find-name-dired (dir pattern)
183 "Search DIR recursively for files matching the globbing pattern PATTERN,
aa228418
JB
184and run dired on those files.
185PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
186The command run (after changing into DIR) is
187
188 find . -name 'PATTERN' -ls"
189 (interactive
190 "DFind-name (directory): \nsFind-name (filename wildcard): ")
62c804f8 191 (find-dired dir (concat "-name " (shell-quote-argument pattern))))
90e3797f 192
aa228418
JB
193;; This functionality suggested by
194;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
195;; Subject: find-dired, lookfor-dired
196;; Date: 10 May 91 17:50:00 GMT
197;; Organization: University of Waterloo
198
31e1d920 199(defalias 'lookfor-dired 'find-grep-dired)
aa228418 200;;;###autoload
4367c5a2
RS
201(defun find-grep-dired (dir regexp)
202 "Find files in DIR containing a regexp REGEXP and start Dired on output.
aa228418
JB
203The command run (after changing into DIR) is
204
4367c5a2 205 find . -exec grep -s -e REGEXP {} \\\; -ls
aa228418
JB
206
207Thus ARG can also contain additional grep options."
efa2bb9c 208 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
aa228418
JB
209 ;; find -exec doesn't allow shell i/o redirections in the command,
210 ;; or we could use `grep -l >/dev/null'
3443c832
RS
211 ;; We use -type f, not ! -type d, to avoid getting screwed
212 ;; by FIFOs and devices. I'm not sure what's best to do
213 ;; about symlinks, so as far as I know this is not wrong.
aa228418 214 (find-dired dir
a086a7fe 215 (concat "-type f -exec grep " find-grep-options " -e "
4367c5a2 216 (shell-quote-argument regexp)
bd7887c6 217 " {} \\\; ")))
aa228418 218
90e3797f
RM
219(defun find-dired-filter (proc string)
220 ;; Filter for \\[find-dired] processes.
1d0c2717
RS
221 (let ((buf (process-buffer proc))
222 (inhibit-read-only t))
c23ae044
RM
223 (if (buffer-name buf) ; not killed?
224 (save-excursion
225 (set-buffer buf)
226 (save-restriction
227 (widen)
228 (save-excursion
229 (let ((buffer-read-only nil)
230 (end (point-max)))
231 (goto-char end)
232 (insert string)
233 (goto-char end)
234 (or (looking-at "^")
235 (forward-line 1))
236 (while (looking-at "^")
237 (insert " ")
aa228418
JB
238 (forward-line 1))
239 ;; Convert ` ./FILE' to ` FILE'
240 ;; This would lose if the current chunk of output
347cd536 241 ;; starts or ends within the ` ./', so back up a bit:
aa228418
JB
242 (goto-char (- end 3)) ; no error if < 0
243 (while (search-forward " ./" nil t)
347cd536
RM
244 (delete-region (point) (- (point) 2)))
245 ;; Find all the complete lines in the unprocessed
246 ;; output and process it to add text properties.
247 (goto-char end)
248 (if (search-backward "\n" (process-mark proc) t)
249 (progn
250 (dired-insert-set-properties (process-mark proc)
251 (1+ (point)))
252 (move-marker (process-mark proc) (1+ (point)))))
253 ))))
c23ae044
RM
254 ;; The buffer has been killed.
255 (delete-process proc))))
90e3797f
RM
256
257(defun find-dired-sentinel (proc state)
258 ;; Sentinel for \\[find-dired] processes.
1d0c2717
RS
259 (let ((buf (process-buffer proc))
260 (inhibit-read-only t))
c23ae044
RM
261 (if (buffer-name buf)
262 (save-excursion
263 (set-buffer buf)
619c556e
RM
264 (let ((buffer-read-only nil))
265 (save-excursion
266 (goto-char (point-max))
0e842e15 267 (insert "\n find " state)
619c556e
RM
268 (forward-char -1) ;Back up before \n at end of STATE.
269 (insert " at " (substring (current-time-string) 0 19))
270 (forward-char 1)
271 (setq mode-line-process
6657eedf 272 (concat ":"
619c556e
RM
273 (symbol-name (process-status proc))))
274 ;; Since the buffer and mode line will show that the
275 ;; process is dead, we can delete it now. Otherwise it
276 ;; will stay around until M-x list-processes.
277 (delete-process proc)
74def44f 278 (force-mode-line-update)))
c23ae044 279 (message "find-dired %s finished." (current-buffer))))))
a994ebb8 280
a0f81711 281\f
49116ac0
JB
282(provide 'find-dired)
283
ab5796a9 284;;; arch-tag: 8edece95-af00-4221-bc74-a4bd2f75f9b0
c0274f38 285;;; find-dired.el ends here