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