]> code.delx.au - gnu-emacs/blob - lisp/find-dired.el
(bookmark-bmenu-2-window): go to correct position as well as
[gnu-emacs] / lisp / find-dired.el
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 (defconst find-dired-version (substring "$Revision: 1.18 $" 11 -2)
10 "$Id: find-dired.el,v 1.18 1994/12/15 12:16:29 rms Exp roland $")
11
12 ;;; This program is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2, or (at your option)
15 ;;; any later version.
16 ;;;
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; A copy of the GNU General Public License can be obtained from this
23 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
24 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25 ;;; 02139, USA.
26 ;;;
27
28 ;;; Commentary:
29
30 ;; LISPDIR ENTRY for the Elisp Archive ===============================
31 ;; LCD Archive Entry:
32 ;; find-dired|Roland McGrath, Sebastian Kremer
33 ;; |roland@gnu.ai.mit.edu, sk@thp.uni-koeln.de
34 ;; |Run a `find' command and dired the output
35 ;; |$Date: 1994/12/15 12:16:29 $|$Revision: 1.18 $|
36
37 ;; INSTALLATION ======================================================
38
39 ;; To use this file, byte-compile it, install it somewhere in your
40 ;; load-path, and put:
41
42 ;; (autoload 'find-dired "find-dired" nil t)
43 ;; (autoload 'find-name-dired "find-dired" nil t)
44 ;; (autoload 'find-grep-dired "find-dired" nil t)
45
46 ;; in your ~/.emacs, or site-init.el, etc.
47
48 ;; To bind it to a key, put, e.g.:
49 ;;
50 ;; (global-set-key "\C-cf" 'find-dired)
51 ;; (global-set-key "\C-cn" 'find-name-dired)
52 ;; (global-set-key "\C-cl" 'find-grep-dired)
53 ;;
54 ;; in your ~/.emacs.
55
56 ;;; Code:
57
58 (require 'dired)
59
60 ;; find(1)'s -ls corresponds to these switches.
61 ;; Note -b, at least GNU find quotes spaces etc. in filenames
62 ;;;###autoload
63 (defvar find-ls-option (if (eq system-type 'berkeley-unix) '("-ls" . "-gilsb")
64 '("-exec ls -ld {} \\;" . "-ld"))
65 "*Description of the option to `find' to produce an `ls -l'-type listing.
66 This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION
67 gives the option (or options) to `find' that produce the desired output.
68 LS-SWITCHES is a list of `ls' switches to tell dired how to parse the output.")
69
70 ;;;###autoload
71 (defvar find-grep-options (if (eq system-type 'berkeley-unix) "-s" "-q")
72 "*Option to grep to be as silent as possible.
73 On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
74 On other systems, the closest you can come is to use `-l'.")
75
76 (defvar find-args nil
77 "Last arguments given to `find' by \\[find-dired].")
78
79 ;; History of find-args values entered in the minibuffer.
80 (defvar find-args-history nil)
81
82 ;;;###autoload
83 (defun find-dired (dir args)
84 "Run `find' and go into dired-mode on a buffer of the output.
85 The command run (after changing into DIR) is
86
87 find . \\( ARGS \\) -ls"
88 (interactive (list (read-file-name "Run find in directory: " nil "" t)
89 (read-string "Run find (with args): " find-args
90 '(find-args-history . 1))))
91 ;; Expand DIR ("" means default-directory), and make sure it has a
92 ;; trailing slash.
93 (setq dir (file-name-as-directory (expand-file-name dir)))
94 ;; Check that it's really a directory.
95 (or (file-directory-p dir)
96 (error "find-dired needs a directory: %s" dir))
97 (switch-to-buffer (get-buffer-create "*Find*"))
98 (widen)
99 (kill-all-local-variables)
100 (setq buffer-read-only nil)
101 (erase-buffer)
102 (setq default-directory dir
103 find-args args ; save for next interactive call
104 args (concat "find . "
105 (if (string= args "")
106 ""
107 (concat "\\( " args " \\) "))
108 (car find-ls-option)))
109 ;; The next statement will bomb in classic dired (no optional arg allowed)
110 (dired-mode dir (cdr find-ls-option))
111 ;; Set subdir-alist so that Tree Dired will work:
112 (dired-simple-subdir-alist)
113 (setq buffer-read-only nil)
114 ;; Subdir headlerline must come first because the first marker in
115 ;; subdir-alist points there.
116 (insert " " dir ":\n")
117 ;; Make second line a ``find'' line in analogy to the ``total'' or
118 ;; ``wildcard'' line.
119 (insert " " args "\n")
120 ;; Start the find process.
121 (let ((proc (start-process-shell-command "find" (current-buffer) args)))
122 (set-process-filter proc (function find-dired-filter))
123 (set-process-sentinel proc (function find-dired-sentinel))
124 ;; Initialize the process marker; it is used by the filter.
125 (move-marker (process-mark proc) 1 (current-buffer)))
126 (setq mode-line-process '(":%s")))
127
128 ;;;###autoload
129 (defun find-name-dired (dir pattern)
130 "Search DIR recursively for files matching the globbing pattern PATTERN,
131 and run dired on those files.
132 PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
133 The command run (after changing into DIR) is
134
135 find . -name 'PATTERN' -ls"
136 (interactive
137 "DFind-name (directory): \nsFind-name (filename wildcard): ")
138 (find-dired dir (concat "-name '" pattern "'")))
139
140 ;; This functionality suggested by
141 ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
142 ;; Subject: find-dired, lookfor-dired
143 ;; Date: 10 May 91 17:50:00 GMT
144 ;; Organization: University of Waterloo
145
146 (defalias 'lookfor-dired 'find-grep-dired)
147 ;;;###autoload
148 (defun find-grep-dired (dir args)
149 "Find files in DIR containing a regexp ARG and start Dired on output.
150 The command run (after changing into DIR) is
151
152 find . -exec grep -s ARG {} \\\; -ls
153
154 Thus ARG can also contain additional grep options."
155 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
156 ;; find -exec doesn't allow shell i/o redirections in the command,
157 ;; or we could use `grep -l >/dev/null'
158 (find-dired dir
159 (concat "! -type d -exec grep " find-grep-options " "
160 args " {} \\\; ")))
161
162 (defun find-dired-filter (proc string)
163 ;; Filter for \\[find-dired] processes.
164 (let ((buf (process-buffer proc)))
165 (if (buffer-name buf) ; not killed?
166 (save-excursion
167 (set-buffer buf)
168 (save-restriction
169 (widen)
170 (save-excursion
171 (let ((buffer-read-only nil)
172 (end (point-max)))
173 (goto-char end)
174 (insert string)
175 (goto-char end)
176 (or (looking-at "^")
177 (forward-line 1))
178 (while (looking-at "^")
179 (insert " ")
180 (forward-line 1))
181 ;; Convert ` ./FILE' to ` FILE'
182 ;; This would lose if the current chunk of output
183 ;; starts or ends within the ` ./', so back up a bit:
184 (goto-char (- end 3)) ; no error if < 0
185 (while (search-forward " ./" nil t)
186 (delete-region (point) (- (point) 2)))
187 ;; Find all the complete lines in the unprocessed
188 ;; output and process it to add text properties.
189 (goto-char end)
190 (if (search-backward "\n" (process-mark proc) t)
191 (progn
192 (dired-insert-set-properties (process-mark proc)
193 (1+ (point)))
194 (move-marker (process-mark proc) (1+ (point)))))
195 ))))
196 ;; The buffer has been killed.
197 (delete-process proc))))
198
199 (defun find-dired-sentinel (proc state)
200 ;; Sentinel for \\[find-dired] processes.
201 (let ((buf (process-buffer proc)))
202 (if (buffer-name buf)
203 (save-excursion
204 (set-buffer buf)
205 (let ((buffer-read-only nil))
206 (save-excursion
207 (goto-char (point-max))
208 (insert "\nfind " state)
209 (forward-char -1) ;Back up before \n at end of STATE.
210 (insert " at " (substring (current-time-string) 0 19))
211 (forward-char 1)
212 (setq mode-line-process
213 (concat ":"
214 (symbol-name (process-status proc))))
215 ;; Since the buffer and mode line will show that the
216 ;; process is dead, we can delete it now. Otherwise it
217 ;; will stay around until M-x list-processes.
218 (delete-process proc)
219 ;; Force mode line redisplay soon.
220 (set-buffer-modified-p (buffer-modified-p))))
221 (message "find-dired %s finished." (current-buffer))))))
222 \f
223 (provide 'find-dired)
224
225 ;;; find-dired.el ends here