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