*** empty log message ***
[bpt/emacs.git] / lisp / find-dired.el
1 ;;; find-dired.el -- Run a `find' command and dired the output
2 ;;; Copyright (C) 1991 Roland McGrath
3
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 $")
6
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 ;;;
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 $|
28
29 ;; INSTALLATION ======================================================
30
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 ;;;###autoload
50 (defvar find-ls-option (if (eq system-type 'berkeley-unix) "-ls"
51 "-exec ls -ldi {} \\;")
52 "*Option to `find' to produce an `ls -l'-type listing.")
53
54 ;;;###autoload
55 (defvar find-grep-options (if (eq system-type 'berkeley-unix) "-s" "-l")
56 "*Option to grep to be as silent as possible.
57 On Berkeley systems, this is `-s', for others it seems impossible to
58 suppress all output, so `-l' is used to print nothing more than the
59 file name.")
60
61 (defvar find-args nil
62 "Last arguments given to `find' by \\[find-dired].")
63
64 ;;;###autoload
65 (defun find-dired (dir args)
66 "Run `find' and go into dired-mode on a buffer of the output.
67 The command run (after changing into DIR) is
68
69 find . \\( ARGS \\) -ls"
70 (interactive (list (read-file-name "Run find in directory: " nil "" t)
71 (if (featurep 'gmhist)
72 (read-with-history-in 'find-args-history
73 "Run find (with args): ")
74 (read-string "Run find (with args): " find-args))))
75 ;; Expand DIR ("" means default-directory), and make sure it has a
76 ;; trailing slash.
77 (setq dir (file-name-as-directory (expand-file-name dir)))
78 ;; Check that it's really a directory.
79 (or (file-directory-p dir)
80 (error "find-dired needs a directory: %s" dir))
81 (switch-to-buffer (get-buffer-create "*Find*"))
82 (widen)
83 (kill-all-local-variables)
84 (setq buffer-read-only nil)
85 (erase-buffer)
86 (setq default-directory dir
87 find-args args
88 args (concat "find . " (if (string= args "") ""
89 (concat "\\( " args " \\) ")) find-ls-option))
90 (dired-mode dir "-gils");; find(1)'s -ls corresponds to `ls -gilds'
91 ;; (but we don't want -d, of course)
92 ;; Set subdir-alist so that Tree Dired will work (but STILL NOT with
93 ;; dired-nstd.el):
94 (set (make-local-variable 'dired-subdir-alist)
95 (list (cons default-directory (point-marker)))) ; we are at point-min
96 (setq buffer-read-only nil)
97 ;; Subdir headlerline must come first because the first marker in
98 ;; subdir-alist points there.
99 (insert " " dir ":\n")
100 ;; Make second line a ``find'' line in analogy to the ``total'' or
101 ;; ``wildcard'' line.
102 (insert " " args "\n")
103 ;; Start the find process
104 (set-process-filter (start-process-shell-command "find"
105 (current-buffer) args)
106 (function find-dired-filter))
107 (set-process-sentinel (get-buffer-process (current-buffer))
108 (function find-dired-sentinel))
109 (setq mode-line-process '(": %s")))
110
111 ;;;###autoload
112 (defun find-name-dired (dir pattern)
113 "Search DIR recursively for files matching the globbing pattern PATTERN,
114 and run dired on those files.
115 PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
116 The command run (after changing into DIR) is
117
118 find . -name 'PATTERN' -ls"
119 (interactive
120 "DFind-name (directory): \nsFind-name (filename wildcard): ")
121 (find-dired dir (concat "-name '" pattern "'")))
122
123 ;; This functionality suggested by
124 ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
125 ;; Subject: find-dired, lookfor-dired
126 ;; Date: 10 May 91 17:50:00 GMT
127 ;; Organization: University of Waterloo
128
129 (fset 'lookfor-dired 'find-grep-dired)
130 ;;;###autoload
131 (defun find-grep-dired (dir args)
132 "Find files in DIR containing a regexp ARG and start Dired on output.
133 The command run (after changing into DIR) is
134
135 find . -exec grep -s ARG {} \\\; -ls
136
137 Thus ARG can also contain additional grep options."
138 (interactive "DFind-grep (directory): \nsFind-grep (grep args): ")
139 ;; find -exec doesn't allow shell i/o redirections in the command,
140 ;; or we could use `grep -l >/dev/null'
141 (find-dired dir
142 (concat "-exec grep " find-grep-options " " args " {} \\\; ")))
143
144 (defun find-dired-filter (proc string)
145 ;; Filter for \\[find-dired] processes.
146 (dired-log "``%s''\n" string)
147 (let ((buf (process-buffer proc)))
148 (if (buffer-name buf) ; not killed?
149 (save-excursion
150 (set-buffer buf)
151 (save-restriction
152 (widen)
153 (save-excursion
154 (let ((buffer-read-only nil)
155 (end (point-max)))
156 (goto-char end)
157 (insert string)
158 (goto-char end)
159 (or (looking-at "^")
160 (forward-line 1))
161 (while (looking-at "^")
162 (insert " ")
163 (forward-line 1))
164 ;; Convert ` ./FILE' to ` FILE'
165 ;; This would lose if the current chunk of output
166 ;; starts or ends within the ` ./', so backup up a bit:
167 (goto-char (- end 3)) ; no error if < 0
168 (while (search-forward " ./" nil t)
169 (delete-region (point) (- (point) 2)))))))
170 ;; The buffer has been killed.
171 (delete-process proc))))
172
173 (defun find-dired-sentinel (proc state)
174 ;; Sentinel for \\[find-dired] processes.
175 (let ((buf (process-buffer proc)))
176 (if (buffer-name buf)
177 (save-excursion
178 (set-buffer buf)
179 (setq mode-line-process nil)
180 (message "find-dired %s finished." (current-buffer))))))
181
182 (or (fboundp 'start-process-shell-command)
183 ;; From version 19 subr.el.
184 (defun start-process-shell-command (name buffer &rest args)
185 "Start a program in a subprocess. Return the process object for it.
186 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
187 NAME is name for process. It is modified if necessary to make it unique.
188 BUFFER is the buffer or (buffer-name) to associate with the process.
189 Process output goes at end of that buffer, unless you specify
190 an output stream or filter function to handle the output.
191 BUFFER may be also nil, meaning that this process is not associated
192 with any buffer
193 Third arg is command name, the name of a shell command.
194 Remaining arguments are the arguments for the command.
195 Wildcards and redirection are handle as usual in the shell."
196 (if (eq system-type 'vax-vms)
197 (apply 'start-process name buffer args)
198 (start-process name buffer shell-file-name "-c"
199 (concat "exec " (mapconcat 'identity args " "))))))
200
201 (provide 'find-dired)
202