1 ;; find-dired.el --- run a `find' command and dired the output
3 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
4 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
5 ;; Maintainer: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Last-Modified: 16 Mar 1992
8 (defconst find-dired-version
(substring "$Revision: 1.9 $" 11 -
2)
9 "$Id: find-dired.el,v 1.9 1991/11/11 13:24:31 sk Exp $")
11 ;;; Copyright (C) 1991 Roland McGrath
13 ;;; This program is free software; you can redistribute it and/or modify
14 ;;; it under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 2, or (at your option)
16 ;;; any later version.
18 ;;; This program is distributed in the hope that it will be useful,
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
23 ;;; A copy of the GNU General Public License can be obtained from this
24 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
25 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
30 ;; INSTALLATION ======================================================
32 ;; To use this file, byte-compile it, install it somewhere in your
33 ;; load-path, and put:
35 ;; (autoload 'find-dired "find-dired" nil t)
36 ;; (autoload 'find-name-dired "find-dired" nil t)
37 ;; (autoload 'find-grep-dired "find-dired" nil t)
39 ;; in your ~/.emacs, or site-init.el, etc.
41 ;; To bind it to a key, put, e.g.:
43 ;; (global-set-key "\C-cf" 'find-dired)
44 ;; (global-set-key "\C-cn" 'find-name-dired)
45 ;; (global-set-key "\C-cl" 'find-grep-dired)
53 (defvar find-ls-option
(if (eq system-type
'berkeley-unix
) "-ls"
54 "-exec ls -ldi {} \\;")
55 "*Option to `find' to produce an `ls -l'-type listing.")
58 (defvar find-grep-options
(if (eq system-type
'berkeley-unix
) "-s" "-l")
59 "*Option to grep to be as silent as possible.
60 On Berkeley systems, this is `-s', for others it seems impossible to
61 suppress all output, so `-l' is used to print nothing more than the
65 "Last arguments given to `find' by \\[find-dired].")
68 (defun find-dired (dir args
)
69 "Run `find' and go into dired-mode on a buffer of the output.
70 The command run (after changing into DIR) is
72 find . \\( ARGS \\) -ls"
73 (interactive (list (read-file-name "Run find in directory: " nil
"" t
)
74 (if (featurep 'gmhist
)
75 (read-with-history-in 'find-args-history
76 "Run find (with args): ")
77 (read-string "Run find (with args): " find-args
))))
78 ;; Expand DIR ("" means default-directory), and make sure it has a
80 (setq dir
(file-name-as-directory (expand-file-name dir
)))
81 ;; Check that it's really a directory.
82 (or (file-directory-p dir
)
83 (error "find-dired needs a directory: %s" dir
))
84 (switch-to-buffer (get-buffer-create "*Find*"))
86 (kill-all-local-variables)
87 (setq buffer-read-only nil
)
89 (setq default-directory dir
91 args
(concat "find . " (if (string= args
"") ""
92 (concat "\\( " args
" \\) ")) find-ls-option
))
93 (dired-mode dir
"-gils");; find(1)'s -ls corresponds to `ls -gilds'
94 ;; (but we don't want -d, of course)
95 ;; Set subdir-alist so that Tree Dired will work (but STILL NOT with
97 (set (make-local-variable 'dired-subdir-alist
)
98 (list (cons default-directory
(point-marker)))) ; we are at point-min
99 (setq buffer-read-only nil
)
100 ;; Subdir headlerline must come first because the first marker in
101 ;; subdir-alist points there.
102 (insert " " dir
":\n")
103 ;; Make second line a ``find'' line in analogy to the ``total'' or
104 ;; ``wildcard'' line.
105 (insert " " args
"\n")
106 ;; Start the find process
107 (set-process-filter (start-process-shell-command "find"
108 (current-buffer) args
)
109 (function find-dired-filter
))
110 (set-process-sentinel (get-buffer-process (current-buffer))
111 (function find-dired-sentinel
))
112 (setq mode-line-process
'(": %s")))
115 (defun find-name-dired (dir pattern
)
116 "Search DIR recursively for files matching the globbing pattern PATTERN,
117 and run dired on those files.
118 PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
119 The command run (after changing into DIR) is
121 find . -name 'PATTERN' -ls"
123 "DFind-name (directory): \nsFind-name (filename wildcard): ")
124 (find-dired dir
(concat "-name '" pattern
"'")))
126 ;; This functionality suggested by
127 ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
128 ;; Subject: find-dired, lookfor-dired
129 ;; Date: 10 May 91 17:50:00 GMT
130 ;; Organization: University of Waterloo
132 (fset 'lookfor-dired
'find-grep-dired
)
134 (defun find-grep-dired (dir args
)
135 "Find files in DIR containing a regexp ARG and start Dired on output.
136 The command run (after changing into DIR) is
138 find . -exec grep -s ARG {} \\\; -ls
140 Thus ARG can also contain additional grep options."
141 (interactive "DFind-grep (directory): \nsFind-grep (grep args): ")
142 ;; find -exec doesn't allow shell i/o redirections in the command,
143 ;; or we could use `grep -l >/dev/null'
145 (concat "-exec grep " find-grep-options
" " args
" {} \\\; ")))
147 (defun find-dired-filter (proc string
)
148 ;; Filter for \\[find-dired] processes.
149 (dired-log "``%s''\n" string
)
150 (let ((buf (process-buffer proc
)))
151 (if (buffer-name buf
) ; not killed?
157 (let ((buffer-read-only nil
)
164 (while (looking-at "^")
167 ;; Convert ` ./FILE' to ` FILE'
168 ;; This would lose if the current chunk of output
169 ;; starts or ends within the ` ./', so backup up a bit:
170 (goto-char (- end
3)) ; no error if < 0
171 (while (search-forward " ./" nil t
)
172 (delete-region (point) (- (point) 2)))))))
173 ;; The buffer has been killed.
174 (delete-process proc
))))
176 (defun find-dired-sentinel (proc state
)
177 ;; Sentinel for \\[find-dired] processes.
178 (let ((buf (process-buffer proc
)))
179 (if (buffer-name buf
)
182 (setq mode-line-process nil
)
183 (message "find-dired %s finished." (current-buffer))))))
185 (or (fboundp 'start-process-shell-command
)
186 ;; From version 19 subr.el.
187 (defun start-process-shell-command (name buffer
&rest args
)
188 "Start a program in a subprocess. Return the process object for it.
189 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
190 NAME is name for process. It is modified if necessary to make it unique.
191 BUFFER is the buffer or (buffer-name) to associate with the process.
192 Process output goes at end of that buffer, unless you specify
193 an output stream or filter function to handle the output.
194 BUFFER may be also nil, meaning that this process is not associated
196 Third arg is command name, the name of a shell command.
197 Remaining arguments are the arguments for the command.
198 Wildcards and redirection are handle as usual in the shell."
199 (if (eq system-type
'vax-vms
)
200 (apply 'start-process name buffer args
)
201 (start-process name buffer shell-file-name
"-c"
202 (concat "exec " (mapconcat 'identity args
" "))))))
204 (provide 'find-dired
)
206 ;;; find-dired.el ends here