lisp/bs.el (bs-cycle-next): Pass current buffer to `bury-buffer'.
[bpt/emacs.git] / lisp / find-dired.el
CommitLineData
3b1e4dd1 1;;; find-dired.el --- run a `find' command and dired the output
c0274f38 2
73b0cd50 3;; Copyright (C) 1992, 1994-1995, 2000-2011 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
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
b578f267
EN
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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
e5167999 24
55535639
PJ
25;;; Commentary:
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
1d2e369d
GM
36;; FIXME this option does not really belong in this file, it's more general.
37;; Eg cf some tests in grep.el.
38(defcustom find-exec-terminator
39 (if (eq 0
40 (ignore-errors
41 (process-file find-program nil nil nil
42 null-device "-exec" "echo" "{}" "+")))
43 "+"
44 (shell-quote-argument ";"))
45 "String that terminates \"find -exec COMMAND {} \".
46The value should include any needed quoting for the shell.
47Common values are \"+\" and \"\\\\;\", with the former more efficient
48than the latter."
49 :version "24.1"
50 :group 'find-dired
51 :type 'string)
52
c0d79871 53;; find's -ls corresponds to these switches.
347cd536 54;; Note -b, at least GNU find quotes spaces etc. in filenames
33933d45 55(defcustom find-ls-option
1d2e369d
GM
56 (if (eq 0
57 (ignore-errors
58 (process-file find-program nil nil nil null-device "-ls")))
59 (cons "-ls"
60 (if (eq system-type 'berkeley-unix)
61 "-gilsb"
62 "-dilsb"))
63 (cons
64 (format "-exec ls -ld {} %s" find-exec-terminator)
65 "-ld"))
9201cc28 66 "Description of the option to `find' to produce an `ls -l'-type listing.
347cd536
RM
67This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION
68gives the option (or options) to `find' that produce the desired output.
33933d45 69LS-SWITCHES is a list of `ls' switches to tell dired how to parse the output."
1d2e369d 70 :version "24.1" ; add tests for -ls and -exec + support
33933d45
AS
71 :type '(cons (string :tag "Find Option")
72 (string :tag "Ls Switches"))
73 :group 'find-dired)
aa228418 74
1d2e369d
GM
75(defcustom find-ls-subdir-switches
76 (if (string-match "-[a-z]*b" (cdr find-ls-option))
77 "-alb"
78 "-al")
a994ebb8
LT
79 "`ls' switches for inserting subdirectories in `*Find*' buffers.
80This should contain the \"-l\" switch.
81Use the \"-F\" or \"-b\" switches if and only if you also use
82them for `find-ls-option'."
1d2e369d 83 :version "24.1" ; add -b test
a994ebb8 84 :type 'string
1d2e369d 85 :group 'find-dired)
a994ebb8 86
33933d45 87(defcustom find-grep-options
e7a395b5 88 (if (or (eq system-type 'berkeley-unix)
9f18e619
RS
89 (string-match "solaris2" system-configuration)
90 (string-match "irix" system-configuration))
e7a395b5 91 "-s" "-q")
9201cc28 92 "Option to grep to be as silent as possible.
efa2bb9c 93On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
33933d45
AS
94On other systems, the closest you can come is to use `-l'."
95 :type 'string
96 :group 'find-dired)
aa228418 97
da205913 98;; This used to be autoloaded (see bug#4387).
d1dbc07c 99(defcustom find-name-arg
e7a395b5 100 (if read-file-name-completion-ignore-case
d1dbc07c 101 "-iname"
e7a395b5 102 "-name")
9201cc28 103 "Argument used to specify file name pattern.
d1dbc07c 104If `read-file-name-completion-ignore-case' is non-nil, -iname is used so that
5480359e 105find also ignores case. Otherwise, -name is used."
d1dbc07c
JR
106 :type 'string
107 :group 'find-dired
108 :version "22.2")
109
aa228418
JB
110(defvar find-args nil
111 "Last arguments given to `find' by \\[find-dired].")
90e3797f 112
347cd536
RM
113;; History of find-args values entered in the minibuffer.
114(defvar find-args-history nil)
115
304d3e5d
RS
116(defvar dired-sort-inhibit)
117
90e3797f
RM
118;;;###autoload
119(defun find-dired (dir args)
0322a154 120 "Run `find' and go into Dired mode on a buffer of the output.
1d2e369d 121The command run (after changing into DIR) is essentially
aa228418 122
db0fb4dd
RS
123 find . \\( ARGS \\) -ls
124
1d2e369d
GM
125except that the car of the variable `find-ls-option' specifies what to
126use in place of \"-ls\" as the final argument."
7ed287b5 127 (interactive (list (read-directory-name "Run find in directory: " nil "" t)
347cd536
RM
128 (read-string "Run find (with args): " find-args
129 '(find-args-history . 1))))
0322a154
RS
130 (let ((dired-buffers dired-buffers))
131 ;; Expand DIR ("" means default-directory), and make sure it has a
132 ;; trailing slash.
a994ebb8 133 (setq dir (file-name-as-directory (expand-file-name dir)))
0322a154
RS
134 ;; Check that it's really a directory.
135 (or (file-directory-p dir)
136 (error "find-dired needs a directory: %s" dir))
137 (switch-to-buffer (get-buffer-create "*Find*"))
8ad5033b
GM
138
139 ;; See if there's still a `find' running, and offer to kill
140 ;; it first, if it is.
141 (let ((find (get-buffer-process (current-buffer))))
142 (when find
143 (if (or (not (eq (process-status find) 'run))
144 (yes-or-no-p "A `find' process is running; kill it? "))
145 (condition-case nil
146 (progn
147 (interrupt-process find)
148 (sit-for 1)
149 (delete-process find))
150 (error nil))
151 (error "Cannot have two processes in `%s' at once" (buffer-name)))))
71296446 152
0322a154
RS
153 (widen)
154 (kill-all-local-variables)
155 (setq buffer-read-only nil)
156 (erase-buffer)
157 (setq default-directory dir
a994ebb8 158 find-args args ; save for next interactive call
d1dbc07c 159 args (concat find-program " . "
0322a154
RS
160 (if (string= args "")
161 ""
2fda9976
KS
162 (concat
163 (shell-quote-argument "(")
164 " " args " "
165 (shell-quote-argument ")")
166 " "))
1d2e369d
GM
167 (if (string-match "\\`\\(.*\\) {} \\(\\\\;\\|+\\)\\'"
168 (car find-ls-option))
169 (format "%s %s %s"
170 (match-string 1 (car find-ls-option))
2fda9976 171 (shell-quote-argument "{}")
1d2e369d 172 find-exec-terminator)
2fda9976 173 (car find-ls-option))))
1a9c959b
MA
174 ;; Start the find process.
175 (shell-command (concat args "&") (current-buffer))
0322a154
RS
176 ;; The next statement will bomb in classic dired (no optional arg allowed)
177 (dired-mode dir (cdr find-ls-option))
1d0c2717
RS
178 (let ((map (make-sparse-keymap)))
179 (set-keymap-parent map (current-local-map))
180 (define-key map "\C-c\C-k" 'kill-find)
181 (use-local-map map))
304d3e5d
RS
182 (make-local-variable 'dired-sort-inhibit)
183 (setq dired-sort-inhibit t)
2b29f15c
MR
184 (set (make-local-variable 'revert-buffer-function)
185 `(lambda (ignore-auto noconfirm)
186 (find-dired ,dir ,find-args)))
0322a154
RS
187 ;; Set subdir-alist so that Tree Dired will work:
188 (if (fboundp 'dired-simple-subdir-alist)
189 ;; will work even with nested dired format (dired-nstd.el,v 1.15
190 ;; and later)
191 (dired-simple-subdir-alist)
192 ;; else we have an ancient tree dired (or classic dired, where
71296446 193 ;; this does no harm)
0322a154
RS
194 (set (make-local-variable 'dired-subdir-alist)
195 (list (cons default-directory (point-min-marker)))))
a994ebb8 196 (set (make-local-variable 'dired-subdir-switches) find-ls-subdir-switches)
0322a154
RS
197 (setq buffer-read-only nil)
198 ;; Subdir headlerline must come first because the first marker in
199 ;; subdir-alist points there.
200 (insert " " dir ":\n")
201 ;; Make second line a ``find'' line in analogy to the ``total'' or
71296446 202 ;; ``wildcard'' line.
0322a154 203 (insert " " args "\n")
1d0c2717 204 (setq buffer-read-only t)
1a9c959b 205 (let ((proc (get-buffer-process (current-buffer))))
0322a154
RS
206 (set-process-filter proc (function find-dired-filter))
207 (set-process-sentinel proc (function find-dired-sentinel))
208 ;; Initialize the process marker; it is used by the filter.
209 (move-marker (process-mark proc) 1 (current-buffer)))
210 (setq mode-line-process '(":%s"))))
90e3797f 211
1d0c2717
RS
212(defun kill-find ()
213 "Kill the `find' process running in the current buffer."
214 (interactive)
215 (let ((find (get-buffer-process (current-buffer))))
216 (and find (eq (process-status find) 'run)
217 (eq (process-filter find) (function find-dired-filter))
218 (condition-case nil
219 (delete-process find)
220 (error nil)))))
221
90e3797f
RM
222;;;###autoload
223(defun find-name-dired (dir pattern)
224 "Search DIR recursively for files matching the globbing pattern PATTERN,
aa228418
JB
225and run dired on those files.
226PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
227The command run (after changing into DIR) is
228
229 find . -name 'PATTERN' -ls"
230 (interactive
231 "DFind-name (directory): \nsFind-name (filename wildcard): ")
d1dbc07c 232 (find-dired dir (concat find-name-arg " " (shell-quote-argument pattern))))
90e3797f 233
aa228418
JB
234;; This functionality suggested by
235;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
236;; Subject: find-dired, lookfor-dired
237;; Date: 10 May 91 17:50:00 GMT
238;; Organization: University of Waterloo
239
31e1d920 240(defalias 'lookfor-dired 'find-grep-dired)
aa228418 241;;;###autoload
4367c5a2
RS
242(defun find-grep-dired (dir regexp)
243 "Find files in DIR containing a regexp REGEXP and start Dired on output.
aa228418
JB
244The command run (after changing into DIR) is
245
1d2e369d
GM
246 find . \\( -type f -exec `grep-program' `find-grep-options' \\
247 -e REGEXP {} \\; \\) -ls
aa228418 248
1d2e369d
GM
249where the car of the variable `find-ls-option' specifies what to
250use in place of \"-ls\" as the final argument."
251 ;; Doc used to say "Thus ARG can also contain additional grep options."
252 ;; i) Presumably ARG == REGEXP?
253 ;; ii) No it can't have options, since it gets shell-quoted.
efa2bb9c 254 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
aa228418
JB
255 ;; find -exec doesn't allow shell i/o redirections in the command,
256 ;; or we could use `grep -l >/dev/null'
3443c832
RS
257 ;; We use -type f, not ! -type d, to avoid getting screwed
258 ;; by FIFOs and devices. I'm not sure what's best to do
259 ;; about symlinks, so as far as I know this is not wrong.
aa228418 260 (find-dired dir
d1dbc07c 261 (concat "-type f -exec " grep-program " " find-grep-options " -e "
4367c5a2 262 (shell-quote-argument regexp)
2fda9976
KS
263 " "
264 (shell-quote-argument "{}")
265 " "
1d2e369d 266 ;; Doesn't work with "+".
2fda9976 267 (shell-quote-argument ";"))))
aa228418 268
90e3797f
RM
269(defun find-dired-filter (proc string)
270 ;; Filter for \\[find-dired] processes.
1d0c2717
RS
271 (let ((buf (process-buffer proc))
272 (inhibit-read-only t))
4543e213
CY
273 (if (buffer-name buf)
274 (with-current-buffer buf
2cd119e3
AS
275 (save-excursion
276 (save-restriction
277 (widen)
278 (let ((buffer-read-only nil)
279 (beg (point-max))
280 (l-opt (and (consp find-ls-option)
281 (string-match "l" (cdr find-ls-option))))
282 (ls-regexp (concat "^ +[^ \t\r\n]+\\( +[^ \t\r\n]+\\) +"
283 "[^ \t\r\n]+ +[^ \t\r\n]+\\( +[0-9]+\\)")))
284 (goto-char beg)
285 (insert string)
4543e213 286 (goto-char beg)
2cd119e3
AS
287 (or (looking-at "^")
288 (forward-line 1))
289 (while (looking-at "^")
290 (insert " ")
291 (forward-line 1))
292 ;; Convert ` ./FILE' to ` FILE'
293 ;; This would lose if the current chunk of output
294 ;; starts or ends within the ` ./', so back up a bit:
295 (goto-char (- beg 3)) ; no error if < 0
296 (while (search-forward " ./" nil t)
297 (delete-region (point) (- (point) 2)))
298 ;; Pad the number of links and file size. This is a
299 ;; quick and dirty way of getting the columns to line up
300 ;; most of the time, but it's not foolproof.
301 (when l-opt
302 (goto-char beg)
303 (goto-char (line-beginning-position))
304 (while (re-search-forward ls-regexp nil t)
305 (replace-match (format "%4s" (match-string 1))
306 nil nil nil 1)
307 (replace-match (format "%9s" (match-string 2))
308 nil nil nil 2)
309 (forward-line 1)))
310 ;; Find all the complete lines in the unprocessed
311 ;; output and process it to add text properties.
312 (goto-char (point-max))
313 (if (search-backward "\n" (process-mark proc) t)
314 (progn
315 (dired-insert-set-properties (process-mark proc)
316 (1+ (point)))
317 (move-marker (process-mark proc) (1+ (point)))))))))
c23ae044
RM
318 ;; The buffer has been killed.
319 (delete-process proc))))
90e3797f
RM
320
321(defun find-dired-sentinel (proc state)
322 ;; Sentinel for \\[find-dired] processes.
1d0c2717
RS
323 (let ((buf (process-buffer proc))
324 (inhibit-read-only t))
c23ae044 325 (if (buffer-name buf)
7fdbcd83 326 (with-current-buffer buf
619c556e
RM
327 (let ((buffer-read-only nil))
328 (save-excursion
329 (goto-char (point-max))
0e842e15 330 (insert "\n find " state)
619c556e
RM
331 (forward-char -1) ;Back up before \n at end of STATE.
332 (insert " at " (substring (current-time-string) 0 19))
333 (forward-char 1)
334 (setq mode-line-process
6657eedf 335 (concat ":"
619c556e
RM
336 (symbol-name (process-status proc))))
337 ;; Since the buffer and mode line will show that the
338 ;; process is dead, we can delete it now. Otherwise it
339 ;; will stay around until M-x list-processes.
340 (delete-process proc)
74def44f 341 (force-mode-line-update)))
c23ae044 342 (message "find-dired %s finished." (current-buffer))))))
a994ebb8 343
a0f81711 344\f
49116ac0
JB
345(provide 'find-dired)
346
c0274f38 347;;; find-dired.el ends here