(outline-layout): Doc fix.
[bpt/emacs.git] / lisp / find-dired.el
CommitLineData
3b1e4dd1 1;;; find-dired.el --- run a `find' command and dired the output
c0274f38 2
347cd536 3;;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
3a801d0c 4
a0f81711
RM
5;; Author: Roland McGrath <roland@gnu.ai.mit.edu>,
6;; Sebastian Kremer <sk@thp.uni-koeln.de>
fd7fa35a 7;; Keywords: unix
c23ae044 8
347cd536
RM
9(defconst find-dired-version (substring "$Revision: 1.18 $" 11 -2)
10 "$Id: find-dired.el,v 1.18 1994/12/15 12:16:29 rms Exp roland $")
c23ae044 11
90e3797f
RM
12;;; This program is free software; you can redistribute it and/or modify
13;;; it under the terms of the GNU General Public License as published by
347cd536 14;;; the Free Software Foundation; either version 2, or (at your option)
90e3797f
RM
15;;; any later version.
16;;;
17;;; This program 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.
21;;;
22;;; A copy of the GNU General Public License can be obtained from this
23;;; program's author (send electronic mail to roland@ai.mit.edu) or from
24;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25;;; 02139, USA.
a0f81711 26;;;
e5167999
ER
27
28;;; Commentary:
90e3797f 29
a0f81711
RM
30;; LISPDIR ENTRY for the Elisp Archive ===============================
31;; LCD Archive Entry:
32;; find-dired|Roland McGrath, Sebastian Kremer
33;; |roland@gnu.ai.mit.edu, sk@thp.uni-koeln.de
34;; |Run a `find' command and dired the output
347cd536 35;; |$Date: 1994/12/15 12:16:29 $|$Revision: 1.18 $|
a0f81711 36
aa228418 37;; INSTALLATION ======================================================
90e3797f 38
aa228418
JB
39;; To use this file, byte-compile it, install it somewhere in your
40;; load-path, and put:
41
42;; (autoload 'find-dired "find-dired" nil t)
43;; (autoload 'find-name-dired "find-dired" nil t)
44;; (autoload 'find-grep-dired "find-dired" nil t)
45
46;; in your ~/.emacs, or site-init.el, etc.
47
48;; To bind it to a key, put, e.g.:
49;;
50;; (global-set-key "\C-cf" 'find-dired)
51;; (global-set-key "\C-cn" 'find-name-dired)
52;; (global-set-key "\C-cl" 'find-grep-dired)
53;;
54;; in your ~/.emacs.
55
e5167999
ER
56;;; Code:
57
aa228418 58(require 'dired)
a0f81711 59
347cd536
RM
60;; find(1)'s -ls corresponds to these switches.
61;; Note -b, at least GNU find quotes spaces etc. in filenames
aa228418 62;;;###autoload
347cd536
RM
63(defvar find-ls-option (if (eq system-type 'berkeley-unix) '("-ls" . "-gilsb")
64 '("-exec ls -ld {} \\;" . "-ld"))
65 "*Description of the option to `find' to produce an `ls -l'-type listing.
66This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION
67gives the option (or options) to `find' that produce the desired output.
68LS-SWITCHES is a list of `ls' switches to tell dired how to parse the output.")
aa228418
JB
69
70;;;###autoload
efa2bb9c 71(defvar find-grep-options (if (eq system-type 'berkeley-unix) "-s" "-q")
aa228418 72 "*Option to grep to be as silent as possible.
efa2bb9c
RS
73On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
74On other systems, the closest you can come is to use `-l'.")
aa228418
JB
75
76(defvar find-args nil
77 "Last arguments given to `find' by \\[find-dired].")
90e3797f 78
347cd536
RM
79;; History of find-args values entered in the minibuffer.
80(defvar find-args-history nil)
81
90e3797f
RM
82;;;###autoload
83(defun find-dired (dir args)
84 "Run `find' and go into dired-mode on a buffer of the output.
aa228418
JB
85The command run (after changing into DIR) is
86
87 find . \\( ARGS \\) -ls"
90e3797f 88 (interactive (list (read-file-name "Run find in directory: " nil "" t)
347cd536
RM
89 (read-string "Run find (with args): " find-args
90 '(find-args-history . 1))))
aa228418
JB
91 ;; Expand DIR ("" means default-directory), and make sure it has a
92 ;; trailing slash.
90e3797f
RM
93 (setq dir (file-name-as-directory (expand-file-name dir)))
94 ;; Check that it's really a directory.
95 (or (file-directory-p dir)
aa228418 96 (error "find-dired needs a directory: %s" dir))
90e3797f
RM
97 (switch-to-buffer (get-buffer-create "*Find*"))
98 (widen)
99 (kill-all-local-variables)
100 (setq buffer-read-only nil)
101 (erase-buffer)
102 (setq default-directory dir
a0f81711
RM
103 find-args args ; save for next interactive call
104 args (concat "find . "
105 (if (string= args "")
106 ""
107 (concat "\\( " args " \\) "))
347cd536 108 (car find-ls-option)))
a0f81711 109 ;; The next statement will bomb in classic dired (no optional arg allowed)
347cd536 110 (dired-mode dir (cdr find-ls-option))
a0f81711 111 ;; Set subdir-alist so that Tree Dired will work:
347cd536 112 (dired-simple-subdir-alist)
aa228418
JB
113 (setq buffer-read-only nil)
114 ;; Subdir headlerline must come first because the first marker in
115 ;; subdir-alist points there.
116 (insert " " dir ":\n")
117 ;; Make second line a ``find'' line in analogy to the ``total'' or
118 ;; ``wildcard'' line.
119 (insert " " args "\n")
347cd536
RM
120 ;; Start the find process.
121 (let ((proc (start-process-shell-command "find" (current-buffer) args)))
122 (set-process-filter proc (function find-dired-filter))
123 (set-process-sentinel proc (function find-dired-sentinel))
124 ;; Initialize the process marker; it is used by the filter.
125 (move-marker (process-mark proc) 1 (current-buffer)))
6657eedf 126 (setq mode-line-process '(":%s")))
90e3797f
RM
127
128;;;###autoload
129(defun find-name-dired (dir pattern)
130 "Search DIR recursively for files matching the globbing pattern PATTERN,
aa228418
JB
131and run dired on those files.
132PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
133The command run (after changing into DIR) is
134
135 find . -name 'PATTERN' -ls"
136 (interactive
137 "DFind-name (directory): \nsFind-name (filename wildcard): ")
90e3797f
RM
138 (find-dired dir (concat "-name '" pattern "'")))
139
aa228418
JB
140;; This functionality suggested by
141;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
142;; Subject: find-dired, lookfor-dired
143;; Date: 10 May 91 17:50:00 GMT
144;; Organization: University of Waterloo
145
31e1d920 146(defalias 'lookfor-dired 'find-grep-dired)
aa228418
JB
147;;;###autoload
148(defun find-grep-dired (dir args)
149 "Find files in DIR containing a regexp ARG and start Dired on output.
150The command run (after changing into DIR) is
151
152 find . -exec grep -s ARG {} \\\; -ls
153
154Thus ARG can also contain additional grep options."
efa2bb9c 155 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
aa228418
JB
156 ;; find -exec doesn't allow shell i/o redirections in the command,
157 ;; or we could use `grep -l >/dev/null'
158 (find-dired dir
a0f81711
RM
159 (concat "! -type d -exec grep " find-grep-options " "
160 args " {} \\\; ")))
aa228418 161
90e3797f
RM
162(defun find-dired-filter (proc string)
163 ;; Filter for \\[find-dired] processes.
c23ae044
RM
164 (let ((buf (process-buffer proc)))
165 (if (buffer-name buf) ; not killed?
166 (save-excursion
167 (set-buffer buf)
168 (save-restriction
169 (widen)
170 (save-excursion
171 (let ((buffer-read-only nil)
172 (end (point-max)))
173 (goto-char end)
174 (insert string)
175 (goto-char end)
176 (or (looking-at "^")
177 (forward-line 1))
178 (while (looking-at "^")
179 (insert " ")
aa228418
JB
180 (forward-line 1))
181 ;; Convert ` ./FILE' to ` FILE'
182 ;; This would lose if the current chunk of output
347cd536 183 ;; starts or ends within the ` ./', so back up a bit:
aa228418
JB
184 (goto-char (- end 3)) ; no error if < 0
185 (while (search-forward " ./" nil t)
347cd536
RM
186 (delete-region (point) (- (point) 2)))
187 ;; Find all the complete lines in the unprocessed
188 ;; output and process it to add text properties.
189 (goto-char end)
190 (if (search-backward "\n" (process-mark proc) t)
191 (progn
192 (dired-insert-set-properties (process-mark proc)
193 (1+ (point)))
194 (move-marker (process-mark proc) (1+ (point)))))
195 ))))
c23ae044
RM
196 ;; The buffer has been killed.
197 (delete-process proc))))
90e3797f
RM
198
199(defun find-dired-sentinel (proc state)
200 ;; Sentinel for \\[find-dired] processes.
c23ae044
RM
201 (let ((buf (process-buffer proc)))
202 (if (buffer-name buf)
203 (save-excursion
204 (set-buffer buf)
619c556e
RM
205 (let ((buffer-read-only nil))
206 (save-excursion
207 (goto-char (point-max))
208 (insert "\nfind " state)
209 (forward-char -1) ;Back up before \n at end of STATE.
210 (insert " at " (substring (current-time-string) 0 19))
211 (forward-char 1)
212 (setq mode-line-process
6657eedf 213 (concat ":"
619c556e
RM
214 (symbol-name (process-status proc))))
215 ;; Since the buffer and mode line will show that the
216 ;; process is dead, we can delete it now. Otherwise it
217 ;; will stay around until M-x list-processes.
218 (delete-process proc)
219 ;; Force mode line redisplay soon.
220 (set-buffer-modified-p (buffer-modified-p))))
c23ae044 221 (message "find-dired %s finished." (current-buffer))))))
a0f81711 222\f
49116ac0
JB
223(provide 'find-dired)
224
c0274f38 225;;; find-dired.el ends here