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