Initial revision
[bpt/emacs.git] / lisp / find-dired.el
CommitLineData
90e3797f 1;;; find-dired.el -- Run a `find' command and dired the result.
c23ae044
RM
2;;; Copyright (C) 1991 Roland McGrath
3
4(defconst find-dired-version "$Id: find-dired.el,v 1.7 1991/06/20 08:50:20 sk RelBeta $")
5
90e3797f
RM
6;;; This program is free software; you can redistribute it and/or modify
7;;; it under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 1, or (at your option)
9;;; any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; A copy of the GNU General Public License can be obtained from this
17;;; program's author (send electronic mail to roland@ai.mit.edu) or from
18;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
19;;; 02139, USA.
20;;;
c23ae044
RM
21;;; Send bug reports to roland@gnu.ai.mit.edu.
22
23;;; To use this file, byte-compile it, install it somewhere
24;;; in your load-path, and put:
25;;; (autoload 'find-dired "find-dired" nil t)
26;;; (autoload 'lookfor-dired "find-dired" nil t)
27;;; in your .emacs, or site-init.el, etc.
28;;; To bind it to a key, put, e.g.:
29;;; (global-set-key "\C-cf" 'find-dired)
30;;; (global-set-key "\C-cl" 'lookfor-dired)
31;;; in your .emacs.
90e3797f
RM
32
33(require 'dired)
34
35(defvar find-args nil
36 "Last arguments given to `find' by \\[find-dired].")
37
38(defvar find-ls-option (if (eq system-type 'berkeley-unix) "-ls"
39 "-exec ls -ldi {} \\;")
40 "Option to `find' to produce an `ls -l'-type listing.")
41
42;;;###autoload
43(defun find-dired (dir args)
44 "Run `find' and go into dired-mode on a buffer of the output.
45The command run is \"find . \\( ARGS \\) -ls\" (after changing into DIR)."
46 (interactive (list (read-file-name "Run find in directory: " nil "" t)
c23ae044
RM
47 (if (featurep 'gmhist)
48 (read-with-history-in 'find-args-history
49 "Run find (with args): ")
50 (read-string "Run find (with args): " find-args))))
90e3797f
RM
51 (if (equal dir "")
52 (setq dir default-directory))
53 ;; Expand DIR, and make sure it has a trailing slash.
54 (setq dir (file-name-as-directory (expand-file-name dir)))
55 ;; Check that it's really a directory.
56 (or (file-directory-p dir)
57 (error "%s is not a directory!" dir))
58 (switch-to-buffer (get-buffer-create "*Find*"))
59 (widen)
60 (kill-all-local-variables)
61 (setq buffer-read-only nil)
62 (erase-buffer)
63 (setq default-directory dir
64 find-args args
65 args (concat "find . " (if (string= args "") ""
66 (concat "\\( " args " \\) ")) find-ls-option))
67 (insert " " args "\n"
68 " " dir ":\n")
69 (set-process-filter (start-process-shell-command "find"
70 (current-buffer) args)
71 'find-dired-filter)
72 (set-process-sentinel (get-buffer-process (current-buffer))
73 'find-dired-sentinel)
74 (dired-mode)
75 (setq mode-line-process '(": %s")))
76
77;;;###autoload
78(defun find-name-dired (dir pattern)
79 "Search DIR recursively for files matching the globbing pattern PATTERN,
80and run dired on those files."
81 (interactive "DSearch directory: \nsSearch directory %s for: ")
82 (find-dired dir (concat "-name '" pattern "'")))
83
84(defun find-dired-filter (proc string)
85 ;; Filter for \\[find-dired] processes.
c23ae044
RM
86 (let ((buf (process-buffer proc)))
87 (if (buffer-name buf) ; not killed?
88 (save-excursion
89 (set-buffer buf)
90 (save-restriction
91 (widen)
92 (save-excursion
93 (let ((buffer-read-only nil)
94 (end (point-max)))
95 (goto-char end)
96 (insert string)
97 (goto-char end)
98 (or (looking-at "^")
99 (forward-line 1))
100 (while (looking-at "^")
101 (insert " ")
102 (forward-line 1))))))
103 ;; The buffer has been killed.
104 (delete-process proc))))
90e3797f
RM
105
106(defun find-dired-sentinel (proc state)
107 ;; Sentinel for \\[find-dired] processes.
c23ae044
RM
108 (let ((buf (process-buffer proc)))
109 (if (buffer-name buf)
110 (save-excursion
111 (set-buffer buf)
112 (setq mode-line-process nil)
113 (message "find-dired %s finished." (current-buffer))))))
114
115(or (fboundp 'start-process-shell-command)
116 ;; From version 19 subr.el.
117 (defun start-process-shell-command (name buffer &rest args)
118 "Start a program in a subprocess. Return the process object for it.
119Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
120NAME is name for process. It is modified if necessary to make it unique.
121BUFFER is the buffer or (buffer-name) to associate with the process.
122 Process output goes at end of that buffer, unless you specify
123 an output stream or filter function to handle the output.
124 BUFFER may be also nil, meaning that this process is not associated
125 with any buffer
126Third arg is command name, the name of a shell command.
127Remaining arguments are the arguments for the command.
128Wildcards and redirection are handle as usual in the shell."
129 (if (eq system-type 'vax-vms)
130 (apply 'start-process name buffer args)
131 (start-process name buffer shell-file-name "-c"
132 (concat "exec " (mapconcat 'identity args " ")))))
133 )
134
135;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
136;; Subject: find-dired, lookfor-dired
137;; Date: 10 May 91 17:50:00 GMT
138;; Organization: University of Waterloo
139
140;; I added a functiopn to the find-dired.el file:
141;; The function is a lookfor-dired and is used to search a string
142;; a subtree:
143
144;;;###autoload
145(defun lookfor-dired (dir args)
146 "Find files in DIR containing a regexp ARG and go into dired-mode on the output.
147The command run is
148
149 \"find . -exec grep -l ARG {} \\\; -ls\"
150
151\(after changing into DIR)."
152 (interactive (list (read-file-name "Run find in directory: " nil "" t)
153 (read-string "Run find (with args): " find-args)))
154 (if (equal dir "")
155 (setq dir default-directory))
156 ;; Expand DIR, and make sure it has a trailing slash.
157 (setq dir (file-name-as-directory (expand-file-name dir)))
158 ;; Check that it's really a directory.
159 (or (file-directory-p dir)
160 (error "%s is not a directory!" dir))
161 (switch-to-buffer (get-buffer-create "*Find*"))
162 (widen)
163 (kill-all-local-variables)
164 (setq buffer-read-only nil)
165 (erase-buffer)
166 (setq default-directory dir
167 find-args args
168 args (concat "find . -exec grep -l " args " {} \\\; -ls"))
169 (insert " " args "\n"
170 " " dir ":\n")
171 (set-process-filter (start-process-shell-command "find"
172 (current-buffer) args)
173 'find-dired-filter)
174 (set-process-sentinel (get-buffer-process (current-buffer))
175 'find-dired-sentinel)
176 (dired-mode)
177 (setq mode-line-process '(": %s")))
90e3797f
RM
178
179(provide 'find-dired)