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