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