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