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.ai.mit.edu>,
6 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
30 ;; find's -ls corresponds to these switches.
31 ;; Note -b, at least GNU find quotes spaces etc. in filenames
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.")
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
))
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'.")
51 "Last arguments given to `find' by \\[find-dired].")
53 ;; History of find-args values entered in the minibuffer.
54 (defvar find-args-history nil
)
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
61 find . \\( ARGS \\) -ls
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
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*"))
76 (kill-all-local-variables)
77 (setq buffer-read-only nil
)
79 (setq default-directory dir
80 find-args args
; save for next interactive call
81 args
(concat "find . "
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
96 (dired-simple-subdir-alist)
97 ;; else we have an ancient tree dired (or classic dired, where
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")))
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
123 find . -name 'PATTERN' -ls"
125 "DFind-name (directory): \nsFind-name (filename wildcard): ")
126 (find-dired dir
(concat "-name '" pattern
"'")))
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
134 (defalias 'lookfor-dired
'find-grep-dired
)
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
140 find . -exec grep -s ARG {} \\\; -ls
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'
147 (concat "! -type d -exec grep " find-grep-options
" "
150 (defun find-dired-filter (proc string
)
151 ;; Filter for \\[find-dired] processes.
152 (let ((buf (process-buffer proc
)))
153 (if (buffer-name buf
) ; not killed?
159 (let ((buffer-read-only nil
)
166 (while (looking-at "^")
169 ;; Convert ` ./FILE' to ` FILE'
170 ;; This would lose if the current chunk of output
171 ;; starts or ends within the ` ./', so back up a bit:
172 (goto-char (- end
3)) ; no error if < 0
173 (while (search-forward " ./" nil t
)
174 (delete-region (point) (- (point) 2)))
175 ;; Find all the complete lines in the unprocessed
176 ;; output and process it to add text properties.
178 (if (search-backward "\n" (process-mark proc
) t
)
180 (dired-insert-set-properties (process-mark proc
)
182 (move-marker (process-mark proc
) (1+ (point)))))
184 ;; The buffer has been killed.
185 (delete-process proc
))))
187 (defun find-dired-sentinel (proc state
)
188 ;; Sentinel for \\[find-dired] processes.
189 (let ((buf (process-buffer proc
)))
190 (if (buffer-name buf
)
193 (let ((buffer-read-only nil
))
195 (goto-char (point-max))
196 (insert "\nfind " state
)
197 (forward-char -
1) ;Back up before \n at end of STATE.
198 (insert " at " (substring (current-time-string) 0 19))
200 (setq mode-line-process
202 (symbol-name (process-status proc
))))
203 ;; Since the buffer and mode line will show that the
204 ;; process is dead, we can delete it now. Otherwise it
205 ;; will stay around until M-x list-processes.
206 (delete-process proc
)
207 (force-mode-line-update)))
208 (message "find-dired %s finished." (current-buffer))))))
210 (provide 'find-dired
)
212 ;;; find-dired.el ends here