comparison lisp/dired/find-dired.el @ 0:376386a54a3c r19-14

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