comparison lisp/dired/find-dired.el @ 70:131b0175ea99 r20-0b30

Import from CVS: tag r20-0b30
author cvs
date Mon, 13 Aug 2007 09:02:59 +0200
parents
children
comparison
equal deleted inserted replaced
69:804d1389bcd6 70:131b0175ea99
1 ;;; find-dired.el --- run a `find' command and dired the output
2
3 ;;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>,
6 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
7 ;; Keywords: unix
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Synched up with: FSF 19.30.
27
28 ;;; Commentary:
29
30 ;; To bind the following functionality to a key, put, e.g.:
31 ;;
32 ;; (global-set-key "\C-cf" 'find-dired)
33 ;; (global-set-key "\C-cn" 'find-name-dired)
34 ;; (global-set-key "\C-cl" 'find-grep-dired)
35 ;;
36 ;; in your ~/.emacs.
37
38 ;;; Code:
39
40 (require 'dired)
41
42 ;; find's -ls corresponds to these switches.
43 ;; Note -b, at least GNU find quotes spaces etc. in filenames
44 ;;;###autoload
45 (defvar find-ls-option (purecopy
46 ;; XEmacs: add purecopy
47 (if (eq system-type 'berkeley-unix) '("-ls" . "-gilsb")
48 '("-exec ls -ld {} \\;" . "-ld")))
49 "*Description of the option to `find' to produce an `ls -l'-type listing.
50 This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION
51 gives the option (or options) to `find' that produce the desired output.
52 LS-SWITCHES is a list of `ls' switches to tell dired how to parse the output.")
53
54 ;;;###autoload
55 (defvar find-grep-options (purecopy
56 ;; XEmacs: add purecopy
57 (if (eq system-type 'berkeley-unix) "-s" "-q"))
58 "*Option to grep to be as silent as possible.
59 On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
60 On other systems, the closest you can come is to use `-l'.")
61
62 ;; XEmacs additions: next two variables.
63
64 ;;;###autoload
65 (defvar find-dired-multiple-buffers nil
66 "*If non-nil, generates a new buffer for each find")
67
68 (defvar find-dired-dir-history nil
69 "History of directories used by find-dired")
70
71 (defvar find-args nil
72 "Last arguments given to `find' by \\[find-dired].")
73
74 (defvar find-args-history nil
75 "Last arguments given to `find' by \\[find-dired].")
76
77 ;; XEmacs: various changes in next function.
78
79 ;;;###autoload
80 (defun find-dired (dir args)
81 "Run `find' and go into dired-mode on a buffer of the output.
82 The command run (after changing into DIR) is
83
84 find . \\( ARGS \\) -ls"
85 (interactive (list (read-file-name "Run find in directory: "
86 nil "" t nil 'find-dired-dir-history)
87 (if (featurep 'gmhist)
88 (read-with-history-in 'find-args-history
89 "Run find (with args): ")
90 (read-string "Run find (with args): "
91 (or (and (fboundp 'symbol-near-point)
92 (symbol-near-point))
93 (car find-args-history))
94 'find-args-history))))
95 ;; Expand DIR ("" means default-directory), and make sure it has a
96 ;; trailing slash.
97 (setq dir (file-name-as-directory (expand-file-name dir)))
98 ;; Check that it's really a directory.
99 (or (file-directory-p dir)
100 (error "find-dired needs a directory: %s" dir))
101 (switch-to-buffer-other-window (if find-dired-multiple-buffers
102 (generate-new-buffer (concat "*Find-in-"
103 (file-name-nondirectory (directory-file-name dir))
104 "/..*"))
105 (get-buffer-create "*Find*")))
106 (widen)
107 (kill-all-local-variables)
108 (setq buffer-read-only nil)
109 (erase-buffer)
110 (setq default-directory dir
111 find-args args ; save for next interactive call
112 args (concat "find . "
113 (if (string= args "")
114 ""
115 (concat "\\( " args " \\) "))
116 (car find-ls-option)))
117 ;; The next statement will bomb in classic dired (no optional arg allowed)
118 (dired-mode dir (cdr find-ls-option))
119 ;; This really should rerun the find command, but I don't
120 ;; have time for that.
121 (let ((keymap (make-sparse-keymap)))
122 (set-keymap-parents keymap (list (current-local-map)))
123 (define-key keymap "g" 'undefined)
124 (use-local-map keymap))
125 ;; Set subdir-alist so that Tree Dired will work:
126 (if (fboundp 'dired-simple-subdir-alist)
127 ;; will work even with nested dired format (dired-nstd.el,v 1.15
128 ;; and later)
129 (dired-simple-subdir-alist)
130 ;; else we have an ancient tree dired (or classic dired, where
131 ;; this does no harm)
132 (set (make-local-variable 'dired-subdir-alist)
133 (list (cons default-directory (point-min-marker)))))
134 (setq buffer-read-only nil)
135 ;; Subdir headlerline must come first because the first marker in
136 ;; subdir-alist points there.
137 (insert " " dir ":\n")
138 ;; Make second line a ``find'' line in analogy to the ``total'' or
139 ;; ``wildcard'' line.
140 (insert " " args "\n")
141 ;; Start the find process
142 (message "Searching .... (but you can continue other work)")
143 (sit-for 0)
144 (let ((proc (start-process-shell-command "find" (current-buffer) args)))
145 (set-process-filter proc (function find-dired-filter))
146 (set-process-sentinel proc (function find-dired-sentinel))
147 ;; Initialize the process marker; it is used by the filter.
148 (move-marker (process-mark proc) 1 (current-buffer)))
149 (setq modeline-process '(": %s")))
150
151 ;;;###autoload
152 (defun find-name-dired (dir pattern)
153 "Search DIR recursively for files matching the globbing pattern PATTERN,
154 and run dired on those files.
155 PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
156 The command run (after changing into DIR) is
157
158 find . -name 'PATTERN' -ls"
159 (interactive
160 "DFind-name (directory): \nsFind-name (filename wildcard): ")
161 (find-dired dir (concat "-name '" pattern "'")))
162
163 ;; This functionality suggested by
164 ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
165 ;; Subject: find-dired, lookfor-dired
166 ;; Date: 10 May 91 17:50:00 GMT
167 ;; Organization: University of Waterloo
168
169 (defalias 'lookfor-dired 'find-grep-dired)
170
171 ;; XEmacs addition
172 (defvar find-grep-dired-history nil
173 "history for find-grep-dired input")
174
175 ;;;###autoload
176 (defun find-grep-dired (dir args)
177 "Find files in DIR containing a regexp ARG and start Dired on output.
178 The command run (after changing into DIR) is
179
180 find . -type f -exec test -r {} \\\; -exec egrep -s ARG {} \\\; -ls
181
182 Thus ARG can also contain additional grep options."
183 (interactive
184 ;; XEmacs improvements here.
185 (list (read-string "Find-grep (directory): "
186 default-directory 'find-dired-dir-history)
187 (read-string "Find-grep (grep args): " (and (fboundp 'symbol-near-point)
188 (symbol-near-point))
189 'find-grep-dired-history)))
190 ;; find -exec doesn't allow shell i/o redirections in the command,
191 ;; or we could use `grep -l >/dev/null'
192 (find-dired dir
193 ;; XEmacs improvements here.
194 (concat "-type f -exec test -r {} \\\; -exec egrep "
195 find-grep-options " " args " {} \\\; ")))
196
197 (defun find-dired-filter (proc string)
198 ;; Filter for \\[find-dired] processes.
199 (let ((buf (process-buffer proc)))
200 (if (buffer-name buf) ; not killed?
201 (save-excursion
202 (set-buffer buf)
203 (save-restriction
204 (widen)
205 (save-excursion
206 (let ((buffer-read-only nil)
207 (end (point-max)))
208 (goto-char end)
209 (insert string)
210 (goto-char end)
211 (or (looking-at "^")
212 (forward-line 1))
213 (while (looking-at "^")
214 (insert " ")
215 (forward-line 1))
216 ;; Convert ` ./FILE' to ` FILE'
217 ;; This would lose if the current chunk of output
218 ;; starts or ends within the ` ./', so back up a bit:
219 (goto-char (- end 3)) ; no error if < 0
220 (while (search-forward " ./" nil t)
221 (delete-region (point) (- (point) 2)))
222 ;; Find all the complete lines in the unprocessed
223 ;; output and process it to add text properties.
224 (goto-char end)
225 (if (search-backward "\n" (process-mark proc) t)
226 (progn
227 (dired-insert-set-properties (process-mark proc)
228 (1+ (point)))
229 (move-marker (process-mark proc) (1+ (point)))))
230 ))))
231 ;; The buffer has been killed.
232 (delete-process proc))))
233
234 (defun find-dired-sentinel (proc state)
235 ;; Sentinel for \\[find-dired] processes.
236 (let ((buf (process-buffer proc)))
237 (if (buffer-name buf)
238 (save-excursion
239 (set-buffer buf)
240 (let ((buffer-read-only nil))
241 (save-excursion
242 (goto-char (point-max))
243 (insert "\nfind " state)
244 (forward-char -1) ;Back up before \n at end of STATE.
245 (insert " at " (substring (current-time-string) 0 19))
246 (forward-char 1)
247 (setq modeline-process ;; XEmacs: newer spelling
248 (concat ":"
249 (symbol-name (process-status proc))))
250 ;; Since the buffer and mode line will show that the
251 ;; process is dead, we can delete it now. Otherwise it
252 ;; will stay around until M-x list-processes.
253 (delete-process proc)
254 (redraw-modeline))) ;; XEmacs function
255 (message "find-dired %s finished." (current-buffer))))))
256
257 (provide 'find-dired)
258
259 ;;; find-dired.el ends here