Commit | Line | Data |
---|---|---|
3b1e4dd1 | 1 | ;;; find-dired.el --- run a `find' command and dired the output |
c0274f38 | 2 | |
b578f267 | 3 | ;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc. |
3a801d0c | 4 | |
a0f81711 RM |
5 | ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>, |
6 | ;; Sebastian Kremer <sk@thp.uni-koeln.de> | |
fd7fa35a | 7 | ;; Keywords: unix |
c23ae044 | 8 | |
b578f267 EN |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it 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 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; 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. | |
e5167999 | 25 | |
e5167999 ER |
26 | ;;; Code: |
27 | ||
aa228418 | 28 | (require 'dired) |
a0f81711 | 29 | |
c0d79871 | 30 | ;; find's -ls corresponds to these switches. |
347cd536 | 31 | ;; Note -b, at least GNU find quotes spaces etc. in filenames |
aa228418 | 32 | ;;;###autoload |
347cd536 RM |
33 | (defvar find-ls-option (if (eq system-type 'berkeley-unix) '("-ls" . "-gilsb") |
34 | '("-exec ls -ld {} \\;" . "-ld")) | |
35 | "*Description of the option to `find' to produce an `ls -l'-type listing. | |
36 | This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION | |
37 | gives the option (or options) to `find' that produce the desired output. | |
38 | LS-SWITCHES is a list of `ls' switches to tell dired how to parse the output.") | |
aa228418 JB |
39 | |
40 | ;;;###autoload | |
9f18e619 RS |
41 | (defvar find-grep-options |
42 | (if (or (eq system-type 'berkeley-unix) | |
43 | (string-match "solaris2" system-configuration) | |
44 | (string-match "irix" system-configuration)) | |
45 | "-s" "-q") | |
aa228418 | 46 | "*Option to grep to be as silent as possible. |
efa2bb9c RS |
47 | On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it. |
48 | On other systems, the closest you can come is to use `-l'.") | |
aa228418 JB |
49 | |
50 | (defvar find-args nil | |
51 | "Last arguments given to `find' by \\[find-dired].") | |
90e3797f | 52 | |
347cd536 RM |
53 | ;; History of find-args values entered in the minibuffer. |
54 | (defvar find-args-history nil) | |
55 | ||
90e3797f RM |
56 | ;;;###autoload |
57 | (defun find-dired (dir args) | |
58 | "Run `find' and go into dired-mode on a buffer of the output. | |
aa228418 JB |
59 | The command run (after changing into DIR) is |
60 | ||
db0fb4dd RS |
61 | find . \\( ARGS \\) -ls |
62 | ||
63 | except that the variable `find-ls-option' specifies what to use | |
64 | as the final argument." | |
90e3797f | 65 | (interactive (list (read-file-name "Run find in directory: " nil "" t) |
347cd536 RM |
66 | (read-string "Run find (with args): " find-args |
67 | '(find-args-history . 1)))) | |
aa228418 JB |
68 | ;; Expand DIR ("" means default-directory), and make sure it has a |
69 | ;; trailing slash. | |
90e3797f RM |
70 | (setq dir (file-name-as-directory (expand-file-name dir))) |
71 | ;; Check that it's really a directory. | |
72 | (or (file-directory-p dir) | |
aa228418 | 73 | (error "find-dired needs a directory: %s" dir)) |
90e3797f RM |
74 | (switch-to-buffer (get-buffer-create "*Find*")) |
75 | (widen) | |
76 | (kill-all-local-variables) | |
77 | (setq buffer-read-only nil) | |
78 | (erase-buffer) | |
79 | (setq default-directory dir | |
a0f81711 RM |
80 | find-args args ; save for next interactive call |
81 | args (concat "find . " | |
82 | (if (string= args "") | |
83 | "" | |
84 | (concat "\\( " args " \\) ")) | |
347cd536 | 85 | (car find-ls-option))) |
a0f81711 | 86 | ;; The next statement will bomb in classic dired (no optional arg allowed) |
347cd536 | 87 | (dired-mode dir (cdr find-ls-option)) |
400bf317 RS |
88 | ;; This really should rerun the find command, but I don't |
89 | ;; have time for that. | |
90 | (use-local-map (append (make-sparse-keymap) (current-local-map))) | |
91 | (define-key (current-local-map) "g" 'undefined) | |
a0f81711 | 92 | ;; Set subdir-alist so that Tree Dired will work: |
82b942b7 KH |
93 | (if (fboundp 'dired-simple-subdir-alist) |
94 | ;; will work even with nested dired format (dired-nstd.el,v 1.15 | |
95 | ;; and later) | |
96 | (dired-simple-subdir-alist) | |
97 | ;; else we have an ancient tree dired (or classic dired, where | |
98 | ;; this does no harm) | |
99 | (set (make-local-variable 'dired-subdir-alist) | |
100 | (list (cons default-directory (point-min-marker))))) | |
aa228418 JB |
101 | (setq buffer-read-only nil) |
102 | ;; Subdir headlerline must come first because the first marker in | |
103 | ;; subdir-alist points there. | |
104 | (insert " " dir ":\n") | |
105 | ;; Make second line a ``find'' line in analogy to the ``total'' or | |
106 | ;; ``wildcard'' line. | |
107 | (insert " " args "\n") | |
347cd536 RM |
108 | ;; Start the find process. |
109 | (let ((proc (start-process-shell-command "find" (current-buffer) args))) | |
110 | (set-process-filter proc (function find-dired-filter)) | |
111 | (set-process-sentinel proc (function find-dired-sentinel)) | |
112 | ;; Initialize the process marker; it is used by the filter. | |
113 | (move-marker (process-mark proc) 1 (current-buffer))) | |
6657eedf | 114 | (setq mode-line-process '(":%s"))) |
90e3797f RM |
115 | |
116 | ;;;###autoload | |
117 | (defun find-name-dired (dir pattern) | |
118 | "Search DIR recursively for files matching the globbing pattern PATTERN, | |
aa228418 JB |
119 | and run dired on those files. |
120 | PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted. | |
121 | The command run (after changing into DIR) is | |
122 | ||
123 | find . -name 'PATTERN' -ls" | |
124 | (interactive | |
125 | "DFind-name (directory): \nsFind-name (filename wildcard): ") | |
90e3797f RM |
126 | (find-dired dir (concat "-name '" pattern "'"))) |
127 | ||
aa228418 JB |
128 | ;; This functionality suggested by |
129 | ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc) | |
130 | ;; Subject: find-dired, lookfor-dired | |
131 | ;; Date: 10 May 91 17:50:00 GMT | |
132 | ;; Organization: University of Waterloo | |
133 | ||
31e1d920 | 134 | (defalias 'lookfor-dired 'find-grep-dired) |
aa228418 JB |
135 | ;;;###autoload |
136 | (defun find-grep-dired (dir args) | |
137 | "Find files in DIR containing a regexp ARG and start Dired on output. | |
138 | The command run (after changing into DIR) is | |
139 | ||
140 | find . -exec grep -s ARG {} \\\; -ls | |
141 | ||
142 | Thus ARG can also contain additional grep options." | |
efa2bb9c | 143 | (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ") |
aa228418 JB |
144 | ;; find -exec doesn't allow shell i/o redirections in the command, |
145 | ;; or we could use `grep -l >/dev/null' | |
146 | (find-dired dir | |
a0f81711 RM |
147 | (concat "! -type d -exec grep " find-grep-options " " |
148 | args " {} \\\; "))) | |
aa228418 | 149 | |
90e3797f RM |
150 | (defun find-dired-filter (proc string) |
151 | ;; Filter for \\[find-dired] processes. | |
c23ae044 RM |
152 | (let ((buf (process-buffer proc))) |
153 | (if (buffer-name buf) ; not killed? | |
154 | (save-excursion | |
155 | (set-buffer buf) | |
156 | (save-restriction | |
157 | (widen) | |
158 | (save-excursion | |
159 | (let ((buffer-read-only nil) | |
160 | (end (point-max))) | |
161 | (goto-char end) | |
162 | (insert string) | |
163 | (goto-char end) | |
164 | (or (looking-at "^") | |
165 | (forward-line 1)) | |
166 | (while (looking-at "^") | |
167 | (insert " ") | |
aa228418 JB |
168 | (forward-line 1)) |
169 | ;; Convert ` ./FILE' to ` FILE' | |
170 | ;; This would lose if the current chunk of output | |
347cd536 | 171 | ;; starts or ends within the ` ./', so back up a bit: |
aa228418 JB |
172 | (goto-char (- end 3)) ; no error if < 0 |
173 | (while (search-forward " ./" nil t) | |
347cd536 RM |
174 | (delete-region (point) (- (point) 2))) |
175 | ;; Find all the complete lines in the unprocessed | |
176 | ;; output and process it to add text properties. | |
177 | (goto-char end) | |
178 | (if (search-backward "\n" (process-mark proc) t) | |
179 | (progn | |
180 | (dired-insert-set-properties (process-mark proc) | |
181 | (1+ (point))) | |
182 | (move-marker (process-mark proc) (1+ (point))))) | |
183 | )))) | |
c23ae044 RM |
184 | ;; The buffer has been killed. |
185 | (delete-process proc)))) | |
90e3797f RM |
186 | |
187 | (defun find-dired-sentinel (proc state) | |
188 | ;; Sentinel for \\[find-dired] processes. | |
c23ae044 RM |
189 | (let ((buf (process-buffer proc))) |
190 | (if (buffer-name buf) | |
191 | (save-excursion | |
192 | (set-buffer buf) | |
619c556e RM |
193 | (let ((buffer-read-only nil)) |
194 | (save-excursion | |
195 | (goto-char (point-max)) | |
196 | (insert "\nfind " state) | |
197 | (forward-char -1) ;Back up before \n at end of STATE. | |
198 | (insert " at " (substring (current-time-string) 0 19)) | |
199 | (forward-char 1) | |
200 | (setq mode-line-process | |
6657eedf | 201 | (concat ":" |
619c556e RM |
202 | (symbol-name (process-status proc)))) |
203 | ;; Since the buffer and mode line will show that the | |
204 | ;; process is dead, we can delete it now. Otherwise it | |
205 | ;; will stay around until M-x list-processes. | |
206 | (delete-process proc) | |
74def44f | 207 | (force-mode-line-update))) |
c23ae044 | 208 | (message "find-dired %s finished." (current-buffer)))))) |
a0f81711 | 209 | \f |
49116ac0 JB |
210 | (provide 'find-dired) |
211 | ||
c0274f38 | 212 | ;;; find-dired.el ends here |