(auto-mode-alist): add patterns for diff-mode.
[emacs.git] / lisp / find-dired.el
bloba6f560af25ea58bcd5bdda8baa8ed4f98703afde
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>
7 ;; Maintainer: FSF
8 ;; Keywords: unix
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)
15 ;; any later version.
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.
27 ;;; Code:
29 (require 'dired)
31 (defgroup find-dired nil
32 "Run a `find' command and dired the output."
33 :group 'dired
34 :prefix "find-")
36 (defcustom find-dired-find-program "find"
37 "Program used to find files."
38 :group 'dired
39 :type 'file
42 ;; find's -ls corresponds to these switches.
43 ;; Note -b, at least GNU find quotes spaces etc. in filenames
44 ;;;###autoload
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"))
54 :group 'find-dired)
56 ;;;###autoload
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))
61 "-s" "-q")
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'."
65 :type 'string
66 :group 'find-dired)
68 (defvar find-args nil
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)
74 ;;;###autoload
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
88 ;; trailing slash.
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*"))
95 (widen)
96 (kill-all-local-variables)
97 (setq buffer-read-only nil)
98 (erase-buffer)
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
115 ;; and later)
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"))))
136 ;;;###autoload
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"
144 (interactive
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)
155 ;;;###autoload
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.
169 (find-dired dir
170 (concat "-type f -exec grep " find-grep-options " "
171 args " {} \\\; ")))
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?
177 (save-excursion
178 (set-buffer buf)
179 (save-restriction
180 (widen)
181 (save-excursion
182 (let ((buffer-read-only nil)
183 (end (point-max)))
184 (goto-char end)
185 (insert string)
186 (goto-char end)
187 (or (looking-at "^")
188 (forward-line 1))
189 (while (looking-at "^")
190 (insert " ")
191 (forward-line 1))
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.
200 (goto-char end)
201 (if (search-backward "\n" (process-mark proc) t)
202 (progn
203 (dired-insert-set-properties (process-mark proc)
204 (1+ (point)))
205 (move-marker (process-mark proc) (1+ (point)))))
206 ))))
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)
214 (save-excursion
215 (set-buffer buf)
216 (let ((buffer-read-only nil))
217 (save-excursion
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))
222 (forward-char 1)
223 (setq mode-line-process
224 (concat ":"
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