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