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