(compilation-parse-errors): Skip 2 lines only for Compilation major mode.
[emacs.git] / lisp / find-dired.el
bloba46c6c651507c04db7444bd38d7785db027d013a
1 ;;; find-dired.el --- run a `find' command and dired the output
3 ;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>,
6 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
7 ;; Keywords: unix
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Code:
28 (require 'dired)
30 ;; find's -ls corresponds to these switches.
31 ;; Note -b, at least GNU find quotes spaces etc. in filenames
32 ;;;###autoload
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.")
40 ;;;###autoload
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")
46 "*Option to grep to be as silent as possible.
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'.")
50 (defvar find-args nil
51 "Last arguments given to `find' by \\[find-dired].")
53 ;; History of find-args values entered in the minibuffer.
54 (defvar find-args-history nil)
56 ;;;###autoload
57 (defun find-dired (dir args)
58 "Run `find' and go into dired-mode on a buffer of the output.
59 The command run (after changing into DIR) is
61 find . \\( ARGS \\) -ls
63 except that the variable `find-ls-option' specifies what to use
64 as the final argument."
65 (interactive (list (read-file-name "Run find in directory: " nil "" t)
66 (read-string "Run find (with args): " find-args
67 '(find-args-history . 1))))
68 ;; Expand DIR ("" means default-directory), and make sure it has a
69 ;; trailing slash.
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)
73 (error "find-dired needs a directory: %s" dir))
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
80 find-args args ; save for next interactive call
81 args (concat "find . "
82 (if (string= args "")
84 (concat "\\( " args " \\) "))
85 (car find-ls-option)))
86 ;; The next statement will bomb in classic dired (no optional arg allowed)
87 (dired-mode dir (cdr find-ls-option))
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)
92 ;; Set subdir-alist so that Tree Dired will work:
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)))))
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")
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)))
114 (setq mode-line-process '(":%s")))
116 ;;;###autoload
117 (defun find-name-dired (dir pattern)
118 "Search DIR recursively for files matching the globbing pattern PATTERN,
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
123 find . -name 'PATTERN' -ls"
124 (interactive
125 "DFind-name (directory): \nsFind-name (filename wildcard): ")
126 (find-dired dir (concat "-name '" pattern "'")))
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
134 (defalias 'lookfor-dired 'find-grep-dired)
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
140 find . -exec grep -s ARG {} \\\; -ls
142 Thus ARG can also contain additional grep options."
143 (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
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
147 (concat "! -type d -exec grep " find-grep-options " "
148 args " {} \\\; ")))
150 (defun find-dired-filter (proc string)
151 ;; Filter for \\[find-dired] processes.
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 " ")
168 (forward-line 1))
169 ;; Convert ` ./FILE' to ` FILE'
170 ;; This would lose if the current chunk of output
171 ;; starts or ends within the ` ./', so back up a bit:
172 (goto-char (- end 3)) ; no error if < 0
173 (while (search-forward " ./" nil t)
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 ))))
184 ;; The buffer has been killed.
185 (delete-process proc))))
187 (defun find-dired-sentinel (proc state)
188 ;; Sentinel for \\[find-dired] processes.
189 (let ((buf (process-buffer proc)))
190 (if (buffer-name buf)
191 (save-excursion
192 (set-buffer buf)
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
201 (concat ":"
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)
207 (force-mode-line-update)))
208 (message "find-dired %s finished." (current-buffer))))))
210 (provide 'find-dired)
212 ;;; find-dired.el ends here