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