Commit | Line | Data |
---|---|---|
3b1e4dd1 | 1 | ;;; find-dired.el --- run a `find' command and dired the output |
c0274f38 | 2 | |
b578f267 | 3 | ;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc. |
3a801d0c | 4 | |
a0f81711 RM |
5 | ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>, |
6 | ;; Sebastian Kremer <sk@thp.uni-koeln.de> | |
fd7fa35a | 7 | ;; Keywords: unix |
c23ae044 | 8 | |
b578f267 EN |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
e5167999 | 25 | |
e5167999 ER |
26 | ;;; Code: |
27 | ||
aa228418 | 28 | (require 'dired) |
a0f81711 | 29 | |
33933d45 AS |
30 | (defgroup find-dired nil |
31 | "Run a `find' command and dired the output." | |
32 | :group 'dired | |
33 | :prefix "find-") | |
34 | ||
c0d79871 | 35 | ;; find's -ls corresponds to these switches. |
347cd536 | 36 | ;; Note -b, at least GNU find quotes spaces etc. in filenames |
aa228418 | 37 | ;;;###autoload |
33933d45 AS |
38 | (defcustom find-ls-option |
39 | (if (eq system-type 'berkeley-unix) '("-ls" . "-gilsb") | |
40 | '("-exec ls -ld {} \\;" . "-ld")) | |
347cd536 RM |
41 | "*Description of the option to `find' to produce an `ls -l'-type listing. |
42 | This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION | |
43 | gives the option (or options) to `find' that produce the desired output. | |
33933d45 AS |
44 | LS-SWITCHES is a list of `ls' switches to tell dired how to parse the output." |
45 | :type '(cons (string :tag "Find Option") | |
46 | (string :tag "Ls Switches")) | |
47 | :group 'find-dired) | |
aa228418 JB |
48 | |
49 | ;;;###autoload | |
33933d45 | 50 | (defcustom find-grep-options |
9f18e619 RS |
51 | (if (or (eq system-type 'berkeley-unix) |
52 | (string-match "solaris2" system-configuration) | |
53 | (string-match "irix" system-configuration)) | |
54 | "-s" "-q") | |
aa228418 | 55 | "*Option to grep to be as silent as possible. |
efa2bb9c | 56 | On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it. |
33933d45 AS |
57 | On other systems, the closest you can come is to use `-l'." |
58 | :type 'string | |
59 | :group 'find-dired) | |
aa228418 JB |
60 | |
61 | (defvar find-args nil | |
62 | "Last arguments given to `find' by \\[find-dired].") | |
90e3797f | 63 | |
347cd536 RM |
64 | ;; History of find-args values entered in the minibuffer. |
65 | (defvar find-args-history nil) | |
66 | ||
90e3797f RM |
67 | ;;;###autoload |
68 | (defun find-dired (dir args) | |
69 | "Run `find' and go into dired-mode on a buffer of the output. | |
aa228418 JB |
70 | The command run (after changing into DIR) is |
71 | ||
db0fb4dd RS |
72 | find . \\( ARGS \\) -ls |
73 | ||
74 | except that the variable `find-ls-option' specifies what to use | |
75 | as the final argument." | |
90e3797f | 76 | (interactive (list (read-file-name "Run find in directory: " nil "" t) |
347cd536 RM |
77 | (read-string "Run find (with args): " find-args |
78 | '(find-args-history . 1)))) | |
aa228418 JB |
79 | ;; Expand DIR ("" means default-directory), and make sure it has a |
80 | ;; trailing slash. | |
90e3797f RM |
81 | (setq dir (file-name-as-directory (expand-file-name dir))) |
82 | ;; Check that it's really a directory. | |
83 | (or (file-directory-p dir) | |
aa228418 | 84 | (error "find-dired needs a directory: %s" dir)) |
90e3797f RM |
85 | (switch-to-buffer (get-buffer-create "*Find*")) |
86 | (widen) | |
87 | (kill-all-local-variables) | |
88 | (setq buffer-read-only nil) | |
89 | (erase-buffer) | |
90 | (setq default-directory dir | |
a0f81711 RM |
91 | find-args args ; save for next interactive call |
92 | args (concat "find . " | |
93 | (if (string= args "") | |
94 | "" | |
95 | (concat "\\( " args " \\) ")) | |
347cd536 | 96 | (car find-ls-option))) |
a0f81711 | 97 | ;; The next statement will bomb in classic dired (no optional arg allowed) |
347cd536 | 98 | (dired-mode dir (cdr find-ls-option)) |
400bf317 RS |
99 | ;; This really should rerun the find command, but I don't |
100 | ;; have time for that. | |
101 | (use-local-map (append (make-sparse-keymap) (current-local-map))) | |
102 | (define-key (current-local-map) "g" 'undefined) | |
a0f81711 | 103 | ;; Set subdir-alist so that Tree Dired will work: |
82b942b7 KH |
104 | (if (fboundp 'dired-simple-subdir-alist) |
105 | ;; will work even with nested dired format (dired-nstd.el,v 1.15 | |
106 | ;; and later) | |
107 | (dired-simple-subdir-alist) | |
108 | ;; else we have an ancient tree dired (or classic dired, where | |
109 | ;; this does no harm) | |
110 | (set (make-local-variable 'dired-subdir-alist) | |
111 | (list (cons default-directory (point-min-marker))))) | |
aa228418 JB |
112 | (setq buffer-read-only nil) |
113 | ;; Subdir headlerline must come first because the first marker in | |
114 | ;; subdir-alist points there. | |
115 | (insert " " dir ":\n") | |
116 | ;; Make second line a ``find'' line in analogy to the ``total'' or | |
117 | ;; ``wildcard'' line. | |
118 | (insert " " args "\n") | |
347cd536 RM |
119 | ;; Start the find process. |
120 | (let ((proc (start-process-shell-command "find" (current-buffer) args))) | |
121 | (set-process-filter proc (function find-dired-filter)) | |
122 | (set-process-sentinel proc (function find-dired-sentinel)) | |
123 | ;; Initialize the process marker; it is used by the filter. | |
124 | (move-marker (process-mark proc) 1 (current-buffer))) | |
6657eedf | 125 | (setq mode-line-process '(":%s"))) |
90e3797f RM |
126 | |
127 | ;;;###autoload | |
128 | (defun find-name-dired (dir pattern) | |
129 | "Search DIR recursively for files matching the globbing pattern PATTERN, | |
aa228418 JB |
130 | and run dired on those files. |
131 | PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted. | |
132 | The command run (after changing into DIR) is | |
133 | ||
134 | find . -name 'PATTERN' -ls" | |
135 | (interactive | |
136 | "DFind-name (directory): \nsFind-name (filename wildcard): ") | |
90e3797f RM |
137 | (find-dired dir (concat "-name '" pattern "'"))) |
138 | ||
aa228418 JB |
139 | ;; This functionality suggested by |
140 | ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc) | |
141 | ;; Subject: find-dired, lookfor-dired | |
142 | ;; Date: 10 May 91 17:50:00 GMT | |
143 | ;; Organization: University of Waterloo | |
144 | ||
31e1d920 | 145 | (defalias 'lookfor-dired 'find-grep-dired) |
aa228418 JB |
146 | ;;;###autoload |
147 | (defun find-grep-dired (dir args) | |
148 | "Find files in DIR containing a regexp ARG and start Dired on output. | |
149 | The command run (after changing into DIR) is | |
150 | ||
151 | find . -exec grep -s ARG {} \\\; -ls | |
152 | ||
153 | Thus ARG can also contain additional grep options." | |
efa2bb9c | 154 | (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ") |
aa228418 JB |
155 | ;; find -exec doesn't allow shell i/o redirections in the command, |
156 | ;; or we could use `grep -l >/dev/null' | |
3443c832 RS |
157 | ;; We use -type f, not ! -type d, to avoid getting screwed |
158 | ;; by FIFOs and devices. I'm not sure what's best to do | |
159 | ;; about symlinks, so as far as I know this is not wrong. | |
aa228418 | 160 | (find-dired dir |
3443c832 | 161 | (concat "-type f -exec grep " find-grep-options " " |
a0f81711 | 162 | args " {} \\\; "))) |
aa228418 | 163 | |
90e3797f RM |
164 | (defun find-dired-filter (proc string) |
165 | ;; Filter for \\[find-dired] processes. | |
c23ae044 RM |
166 | (let ((buf (process-buffer proc))) |
167 | (if (buffer-name buf) ; not killed? | |
168 | (save-excursion | |
169 | (set-buffer buf) | |
170 | (save-restriction | |
171 | (widen) | |
172 | (save-excursion | |
173 | (let ((buffer-read-only nil) | |
174 | (end (point-max))) | |
175 | (goto-char end) | |
176 | (insert string) | |
177 | (goto-char end) | |
178 | (or (looking-at "^") | |
179 | (forward-line 1)) | |
180 | (while (looking-at "^") | |
181 | (insert " ") | |
aa228418 JB |
182 | (forward-line 1)) |
183 | ;; Convert ` ./FILE' to ` FILE' | |
184 | ;; This would lose if the current chunk of output | |
347cd536 | 185 | ;; starts or ends within the ` ./', so back up a bit: |
aa228418 JB |
186 | (goto-char (- end 3)) ; no error if < 0 |
187 | (while (search-forward " ./" nil t) | |
347cd536 RM |
188 | (delete-region (point) (- (point) 2))) |
189 | ;; Find all the complete lines in the unprocessed | |
190 | ;; output and process it to add text properties. | |
191 | (goto-char end) | |
192 | (if (search-backward "\n" (process-mark proc) t) | |
193 | (progn | |
194 | (dired-insert-set-properties (process-mark proc) | |
195 | (1+ (point))) | |
196 | (move-marker (process-mark proc) (1+ (point))))) | |
197 | )))) | |
c23ae044 RM |
198 | ;; The buffer has been killed. |
199 | (delete-process proc)))) | |
90e3797f RM |
200 | |
201 | (defun find-dired-sentinel (proc state) | |
202 | ;; Sentinel for \\[find-dired] processes. | |
c23ae044 RM |
203 | (let ((buf (process-buffer proc))) |
204 | (if (buffer-name buf) | |
205 | (save-excursion | |
206 | (set-buffer buf) | |
619c556e RM |
207 | (let ((buffer-read-only nil)) |
208 | (save-excursion | |
209 | (goto-char (point-max)) | |
210 | (insert "\nfind " state) | |
211 | (forward-char -1) ;Back up before \n at end of STATE. | |
212 | (insert " at " (substring (current-time-string) 0 19)) | |
213 | (forward-char 1) | |
214 | (setq mode-line-process | |
6657eedf | 215 | (concat ":" |
619c556e RM |
216 | (symbol-name (process-status proc)))) |
217 | ;; Since the buffer and mode line will show that the | |
218 | ;; process is dead, we can delete it now. Otherwise it | |
219 | ;; will stay around until M-x list-processes. | |
220 | (delete-process proc) | |
74def44f | 221 | (force-mode-line-update))) |
c23ae044 | 222 | (message "find-dired %s finished." (current-buffer)))))) |
a0f81711 | 223 | \f |
49116ac0 JB |
224 | (provide 'find-dired) |
225 | ||
c0274f38 | 226 | ;;; find-dired.el ends here |