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