(update_frame): Call detect_input_pending
[bpt/emacs.git] / lisp / find-dired.el
CommitLineData
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.
36This is a cons of two strings (FIND-OPTION . LS-SWITCHES). FIND-OPTION
37gives the option (or options) to `find' that produce the desired output.
38LS-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
47On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
48On 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
59The command run (after changing into DIR) is
60
61 find . \\( ARGS \\) -ls"
90e3797f 62 (interactive (list (read-file-name "Run find in directory: " nil "" t)
347cd536
RM
63 (read-string "Run find (with args): " find-args
64 '(find-args-history . 1))))
aa228418
JB
65 ;; Expand DIR ("" means default-directory), and make sure it has a
66 ;; trailing slash.
90e3797f
RM
67 (setq dir (file-name-as-directory (expand-file-name dir)))
68 ;; Check that it's really a directory.
69 (or (file-directory-p dir)
aa228418 70 (error "find-dired needs a directory: %s" dir))
90e3797f
RM
71 (switch-to-buffer (get-buffer-create "*Find*"))
72 (widen)
73 (kill-all-local-variables)
74 (setq buffer-read-only nil)
75 (erase-buffer)
76 (setq default-directory dir
a0f81711
RM
77 find-args args ; save for next interactive call
78 args (concat "find . "
79 (if (string= args "")
80 ""
81 (concat "\\( " args " \\) "))
347cd536 82 (car find-ls-option)))
a0f81711 83 ;; The next statement will bomb in classic dired (no optional arg allowed)
347cd536 84 (dired-mode dir (cdr find-ls-option))
400bf317
RS
85 ;; This really should rerun the find command, but I don't
86 ;; have time for that.
87 (use-local-map (append (make-sparse-keymap) (current-local-map)))
88 (define-key (current-local-map) "g" 'undefined)
a0f81711 89 ;; Set subdir-alist so that Tree Dired will work:
82b942b7
KH
90 (if (fboundp 'dired-simple-subdir-alist)
91 ;; will work even with nested dired format (dired-nstd.el,v 1.15
92 ;; and later)
93 (dired-simple-subdir-alist)
94 ;; else we have an ancient tree dired (or classic dired, where
95 ;; this does no harm)
96 (set (make-local-variable 'dired-subdir-alist)
97 (list (cons default-directory (point-min-marker)))))
aa228418
JB
98 (setq buffer-read-only nil)
99 ;; Subdir headlerline must come first because the first marker in
100 ;; subdir-alist points there.
101 (insert " " dir ":\n")
102 ;; Make second line a ``find'' line in analogy to the ``total'' or
103 ;; ``wildcard'' line.
104 (insert " " args "\n")
347cd536
RM
105 ;; Start the find process.
106 (let ((proc (start-process-shell-command "find" (current-buffer) args)))
107 (set-process-filter proc (function find-dired-filter))
108 (set-process-sentinel proc (function find-dired-sentinel))
109 ;; Initialize the process marker; it is used by the filter.
110 (move-marker (process-mark proc) 1 (current-buffer)))
6657eedf 111 (setq mode-line-process '(":%s")))
90e3797f
RM
112
113;;;###autoload
114(defun find-name-dired (dir pattern)
115 "Search DIR recursively for files matching the globbing pattern PATTERN,
aa228418
JB
116and run dired on those files.
117PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
118The command run (after changing into DIR) is
119
120 find . -name 'PATTERN' -ls"
121 (interactive
122 "DFind-name (directory): \nsFind-name (filename wildcard): ")
90e3797f
RM
123 (find-dired dir (concat "-name '" pattern "'")))
124
aa228418
JB
125;; This functionality suggested by
126;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
127;; Subject: find-dired, lookfor-dired
128;; Date: 10 May 91 17:50:00 GMT
129;; Organization: University of Waterloo
130
31e1d920 131(defalias 'lookfor-dired 'find-grep-dired)
aa228418
JB
132;;;###autoload
133(defun find-grep-dired (dir args)
134 "Find files in DIR containing a regexp ARG and start Dired on output.
135The command run (after changing into DIR) is
136
137 find . -exec grep -s ARG {} \\\; -ls
138
139Thus ARG can also contain additional grep options."
efa2bb9c 140 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
aa228418
JB
141 ;; find -exec doesn't allow shell i/o redirections in the command,
142 ;; or we could use `grep -l >/dev/null'
143 (find-dired dir
a0f81711
RM
144 (concat "! -type d -exec grep " find-grep-options " "
145 args " {} \\\; ")))
aa228418 146
90e3797f
RM
147(defun find-dired-filter (proc string)
148 ;; Filter for \\[find-dired] processes.
c23ae044
RM
149 (let ((buf (process-buffer proc)))
150 (if (buffer-name buf) ; not killed?
151 (save-excursion
152 (set-buffer buf)
153 (save-restriction
154 (widen)
155 (save-excursion
156 (let ((buffer-read-only nil)
157 (end (point-max)))
158 (goto-char end)
159 (insert string)
160 (goto-char end)
161 (or (looking-at "^")
162 (forward-line 1))
163 (while (looking-at "^")
164 (insert " ")
aa228418
JB
165 (forward-line 1))
166 ;; Convert ` ./FILE' to ` FILE'
167 ;; This would lose if the current chunk of output
347cd536 168 ;; starts or ends within the ` ./', so back up a bit:
aa228418
JB
169 (goto-char (- end 3)) ; no error if < 0
170 (while (search-forward " ./" nil t)
347cd536
RM
171 (delete-region (point) (- (point) 2)))
172 ;; Find all the complete lines in the unprocessed
173 ;; output and process it to add text properties.
174 (goto-char end)
175 (if (search-backward "\n" (process-mark proc) t)
176 (progn
177 (dired-insert-set-properties (process-mark proc)
178 (1+ (point)))
179 (move-marker (process-mark proc) (1+ (point)))))
180 ))))
c23ae044
RM
181 ;; The buffer has been killed.
182 (delete-process proc))))
90e3797f
RM
183
184(defun find-dired-sentinel (proc state)
185 ;; Sentinel for \\[find-dired] processes.
c23ae044
RM
186 (let ((buf (process-buffer proc)))
187 (if (buffer-name buf)
188 (save-excursion
189 (set-buffer buf)
619c556e
RM
190 (let ((buffer-read-only nil))
191 (save-excursion
192 (goto-char (point-max))
193 (insert "\nfind " state)
194 (forward-char -1) ;Back up before \n at end of STATE.
195 (insert " at " (substring (current-time-string) 0 19))
196 (forward-char 1)
197 (setq mode-line-process
6657eedf 198 (concat ":"
619c556e
RM
199 (symbol-name (process-status proc))))
200 ;; Since the buffer and mode line will show that the
201 ;; process is dead, we can delete it now. Otherwise it
202 ;; will stay around until M-x list-processes.
203 (delete-process proc)
74def44f 204 (force-mode-line-update)))
c23ae044 205 (message "find-dired %s finished." (current-buffer))))))
a0f81711 206\f
49116ac0
JB
207(provide 'find-dired)
208
c0274f38 209;;; find-dired.el ends here