Initial revision
[bpt/emacs.git] / lisp / find-dired.el
CommitLineData
90e3797f
RM
1;;; find-dired.el -- Run a `find' command and dired the result.
2;;; Copyright (C) 1991 Free Software Foundation, Inc.
3;;; Written by Roland McGrath
4;;;
5;;; This program is free software; you can redistribute it and/or modify
6;;; it under the terms of the GNU General Public License as published by
7;;; the Free Software Foundation; either version 1, or (at your option)
8;;; any later version.
9;;;
10;;; This program is distributed in the hope that it will be useful,
11;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;;; GNU General Public License for more details.
14;;;
15;;; A copy of the GNU General Public License can be obtained from this
16;;; program's author (send electronic mail to roland@ai.mit.edu) or from
17;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
18;;; 02139, USA.
19;;;
20
21(require 'dired)
22
23(defvar find-args nil
24 "Last arguments given to `find' by \\[find-dired].")
25
26(defvar find-ls-option (if (eq system-type 'berkeley-unix) "-ls"
27 "-exec ls -ldi {} \\;")
28 "Option to `find' to produce an `ls -l'-type listing.")
29
30;;;###autoload
31(defun find-dired (dir args)
32 "Run `find' and go into dired-mode on a buffer of the output.
33The command run is \"find . \\( ARGS \\) -ls\" (after changing into DIR)."
34 (interactive (list (read-file-name "Run find in directory: " nil "" t)
35 (read-string "Run find (with args): " find-args)))
36 (if (equal dir "")
37 (setq dir default-directory))
38 ;; Expand DIR, and make sure it has a trailing slash.
39 (setq dir (file-name-as-directory (expand-file-name dir)))
40 ;; Check that it's really a directory.
41 (or (file-directory-p dir)
42 (error "%s is not a directory!" dir))
43 (switch-to-buffer (get-buffer-create "*Find*"))
44 (widen)
45 (kill-all-local-variables)
46 (setq buffer-read-only nil)
47 (erase-buffer)
48 (setq default-directory dir
49 find-args args
50 args (concat "find . " (if (string= args "") ""
51 (concat "\\( " args " \\) ")) find-ls-option))
52 (insert " " args "\n"
53 " " dir ":\n")
54 (set-process-filter (start-process-shell-command "find"
55 (current-buffer) args)
56 'find-dired-filter)
57 (set-process-sentinel (get-buffer-process (current-buffer))
58 'find-dired-sentinel)
59 (dired-mode)
60 (setq mode-line-process '(": %s")))
61
62;;;###autoload
63(defun find-name-dired (dir pattern)
64 "Search DIR recursively for files matching the globbing pattern PATTERN,
65and run dired on those files."
66 (interactive "DSearch directory: \nsSearch directory %s for: ")
67 (find-dired dir (concat "-name '" pattern "'")))
68
69(defun find-dired-filter (proc string)
70 ;; Filter for \\[find-dired] processes.
71 (let ((buf (process-buffer proc)))
72 (if (buffer-name buf)
73 (save-excursion
74 (set-buffer buf)
75 (save-restriction
76 (widen)
77 (save-excursion
78 (let ((buffer-read-only nil)
79 (end (point-max)))
80 (goto-char end)
81 (insert string)
82 (goto-char end)
83 (or (looking-at "^")
84 (forward-line 1))
85 (while (looking-at "^")
86 (insert " ")
87 (forward-line 1))))))
88 ;; The buffer has been killed.
89 (delete-process proc))))
90
91(defun find-dired-sentinel (proc state)
92 ;; Sentinel for \\[find-dired] processes.
93 (save-excursion
94 (set-buffer (process-buffer proc))
95 (setq mode-line-process nil)
96 (message "find-dired %s finished." (current-buffer))))
97
98(provide 'find-dired)