1 ;;; find-lisp.el --- Emulation of find in Emacs Lisp
3 ;; Author: Peter Breton
4 ;; Created: Fri Mar 26 1999
6 ;; Time-stamp: <1999-04-19 16:37:01 pbreton>
8 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs 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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This is a very generalized form of find; it basically implements a
30 ;; recursive directory descent. The conditions which bound the search
31 ;; are expressed as predicates, and I have not addressed the question
32 ;; of how to wrap up the common chores that find does in a simpler
33 ;; format than writing code for all the various predicates.
35 ;; Some random thoughts are to express simple queries directly with
36 ;; user-level functions, and perhaps use some kind of forms interface
37 ;; for medium-level queries. Really complicated queries can be
43 ;; It would be nice if we could sort the results without running the find
44 ;; again. Maybe that could work by storing the original file attributes?
50 (defvar find-lisp-regexp nil
53 (defconst find-lisp-line-indent
" "
54 "Indentation for dired file lines.")
56 (defvar find-lisp-file-predicate nil
57 "Predicate for choosing to include files.")
59 (defvar find-lisp-directory-predicate nil
60 "Predicate for choosing to descend into directories.")
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66 (defvar find-lisp-debug-buffer
"*Find Lisp Debug*"
67 "Buffer for debugging information.")
69 (defvar find-lisp-debug nil
70 "Whether debugging is enabled.")
72 (defun find-lisp-debug-message (message)
73 "Print a debug message MESSAGE in `find-lisp-debug-buffer'."
74 (set-buffer (get-buffer-create find-lisp-debug-buffer
))
75 (goto-char (point-max))
76 (insert message
"\n"))
78 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79 ;; Directory and File predicates
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82 (defun find-lisp-default-directory-predicate (dir parent
)
83 "True if DIR is not a dot file, and not a symlink.
84 PARENT is the parent directory of DIR."
86 (find-lisp-debug-message
87 (format "Processing directory %s in %s" dir parent
)))
88 ;; Skip current and parent directories
89 (not (or (string= dir
".")
91 ;; Skip directories which are symlinks
92 ;; Easy way to circumvent recursive loops
93 (file-symlink-p dir
))))
95 (defun find-lisp-default-file-predicate (file dir
)
96 "True if FILE matches `find-lisp-regexp'.
97 DIR is the directory containing FILE."
99 (find-lisp-debug-message
100 (format "Processing file %s in %s" file dir
)))
101 (and (not (file-directory-p (expand-file-name file dir
)))
102 (string-match find-lisp-regexp file
)))
104 (defun find-lisp-file-predicate-is-directory (file dir
)
105 "True if FILE is a directory.
106 Argument DIR is the directory containing FILE."
108 (find-lisp-debug-message
109 (format "Processing file %s in %s" file dir
)))
110 (and (file-directory-p (expand-file-name file dir
))
111 (not (or (string= file
".")
112 (string= file
"..")))))
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 (defun find-lisp-find-files (directory regexp
)
119 "Find files in DIRECTORY which match REGEXP."
120 (let ((file-predicate 'find-lisp-default-file-predicate
)
121 (directory-predicate 'find-lisp-default-directory-predicate
)
122 (find-lisp-regexp regexp
)
124 (find-lisp-find-files-internal
127 directory-predicate
)))
129 ;; Workhorse function
130 (defun find-lisp-find-files-internal (directory file-predicate
132 "Find files under DIRECTORY which satisfy FILE-PREDICATE.
133 FILE-PREDICATE is a function which takes two arguments: the file and its
136 DIRECTORY-PREDICATE is used to decide whether to descend into directories.
137 It is a function which takes two arguments, the directory and its parent."
138 (let (results sub-results
)
142 (let ((fullname (expand-file-name file directory
)))
143 (and (file-readable-p (expand-file-name file directory
))
145 ;; If a directory, check it we should descend into it
146 (and (file-directory-p fullname
)
147 (funcall directory-predicate file directory
)
150 (find-lisp-find-files-internal
153 directory-predicate
))
155 (nconc results sub-results
)
156 (setq results sub-results
))))
157 ;; For all files and directories, call the file predicate
158 (and (funcall file-predicate file directory
)
160 (nconc results
(list fullname
))
161 (setq results
(list fullname
))))
163 (directory-files directory nil nil t
))
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 ;; Find-dired all in Lisp
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 (defun find-lisp-find-dired (dir regexp
)
171 "Find files in DIR, matching REGEXP."
172 (interactive "DFind files in directory: \nsMatching regexp: ")
173 (let ((find-lisp-regexp regexp
))
174 (find-lisp-find-dired-internal
176 'find-lisp-default-file-predicate
177 'find-lisp-default-directory-predicate
178 "*Find Lisp Dired*")))
180 ;; Just the subdirectories
181 (defun find-lisp-find-dired-subdirectories (dir)
182 "Find all subdirectories of DIR."
183 (interactive "DFind subdirectories of directory: ")
184 (find-lisp-find-dired-internal
186 'find-lisp-file-predicate-is-directory
187 'find-lisp-default-directory-predicate
188 "*Find Lisp Dired Subdirectories*"))
190 ;; Most of this is lifted from find-dired.el
192 (defun find-lisp-find-dired-internal (dir file-predicate
193 directory-predicate buffer-name
)
194 "Run find (Lisp version) and go into Dired mode on a buffer of the output."
195 (let ((dired-buffers dired-buffers
)
197 (regexp find-lisp-regexp
))
198 ;; Expand DIR ("" means default-directory), and make sure it has a
200 (setq dir
(abbreviate-file-name
201 (file-name-as-directory (expand-file-name dir
))))
202 ;; Check that it's really a directory.
203 (or (file-directory-p dir
)
204 (error "find-dired needs a directory: %s" dir
))
207 (string= buffer-name
(buffer-name)))
208 (switch-to-buffer (setq buf
(get-buffer-create buffer-name
))))
210 (kill-all-local-variables)
211 (setq buffer-read-only nil
)
213 (setq default-directory dir
)
216 (use-local-map (append (make-sparse-keymap) (current-local-map)))
218 (make-local-variable 'find-lisp-file-predicate
)
219 (setq find-lisp-file-predicate file-predicate
)
220 (make-local-variable 'find-lisp-directory-predicate
)
221 (setq find-lisp-directory-predicate directory-predicate
)
222 (make-local-variable 'find-lisp-regexp
)
223 (setq find-lisp-regexp regexp
)
225 (make-local-variable 'revert-buffer-function
)
226 (setq revert-buffer-function
228 (lambda(ignore1 ignore2
)
229 (find-lisp-insert-directory
231 find-lisp-file-predicate
232 find-lisp-directory-predicate
237 ;; Set subdir-alist so that Tree Dired will work:
238 (if (fboundp 'dired-simple-subdir-alist
)
239 ;; will work even with nested dired format (dired-nstd.el,v 1.15
241 (dired-simple-subdir-alist)
242 ;; else we have an ancient tree dired (or classic dired, where
243 ;; this does no harm)
244 (set (make-local-variable 'dired-subdir-alist
)
245 (list (cons default-directory
(point-min-marker)))))
246 (find-lisp-insert-directory
247 dir file-predicate directory-predicate
'ignore
)
248 (goto-char (point-min))
249 (dired-goto-next-file)))
251 (defun find-lisp-insert-directory (dir
255 "Insert the results of `find-lisp-find-files' in the current buffer."
256 (let ((buffer-read-only nil
)
257 (files (find-lisp-find-files-internal
260 directory-predicate
))
263 ;; Subdir headlerline must come first because the first marker in
264 ;; subdir-alist points there.
265 (insert find-lisp-line-indent dir
":\n")
266 ;; Make second line a ``find'' line in analogy to the ``total'' or
267 ;; ``wildcard'' line.
269 ;; No analog for find-lisp?
270 (insert find-lisp-line-indent
"\n")
271 ;; Run the find function
275 (find-lisp-find-dired-insert-file
278 (sort files
'string-lessp
))
279 ;; FIXME: Sort function is ignored for now
280 ;; (funcall sort-function files))
281 (goto-char (point-min))
282 (dired-goto-next-file)))
284 (defun find-lisp-find-dired-filter (regexp)
285 "Change the filter on a find-lisp-find-dired buffer to REGEXP."
286 (interactive "sSet filter to regexp: ")
287 (setq find-lisp-regexp regexp
)
290 (defun find-lisp-find-dired-insert-file (file buffer
)
292 (insert find-lisp-line-indent
293 (find-lisp-format file
(file-attributes file
) (list "")
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 ;; Lifted from ls-lisp. We don't want to require it, because that
298 ;; would alter the insert-directory function.
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
301 (defun find-lisp-format (file-name file-attr switches now
)
302 (let ((file-type (nth 0 file-attr
)))
303 (concat (if (memq ?i switches
) ; inode number
304 (format "%6d " (nth 10 file-attr
)))
305 ;; nil is treated like "" in concat
306 (if (memq ?s switches
) ; size in K
307 (format "%4d " (1+ (/ (nth 7 file-attr
) 1024))))
308 (nth 8 file-attr
) ; permission bits
309 ;; numeric uid/gid are more confusing than helpful
310 ;; Emacs should be able to make strings of them.
311 ;; user-login-name and user-full-name could take an
313 (format " %3d %-8s %-8s %8d "
314 (nth 1 file-attr
) ; no. of links
315 (if (= (user-uid) (nth 2 file-attr
))
317 (int-to-string (nth 2 file-attr
))) ; uid
318 (if (eq system-type
'ms-dos
)
319 "root" ; everything is root on MSDOS.
320 (int-to-string (nth 3 file-attr
))) ; gid
321 (nth 7 file-attr
) ; size in bytes
323 (find-lisp-format-time file-attr switches now
)
326 (if (stringp file-type
) ; is a symbolic link
327 (concat " -> " file-type
)
331 (defun find-lisp-time-index (switches)
332 ;; Return index into file-attributes according to ls SWITCHES.
334 ((memq ?c switches
) 6) ; last mode change
335 ((memq ?u switches
) 4) ; last access
336 ;; default is last modtime
339 (defun find-lisp-format-time (file-attr switches now
)
340 ;; Format time string for file with attributes FILE-ATTR according
341 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
342 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
343 ;; depending on distance between file date and NOW.
344 (let* ((time (nth (find-lisp-time-index switches
) file-attr
))
345 (diff16 (- (car time
) (car now
)))
346 (diff (+ (ash diff16
16) (- (car (cdr time
)) (car (cdr now
)))))
347 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
348 (future-cutoff (* 60 60))) ; 1 hour
351 (<= past-cutoff diff
) (<= diff future-cutoff
)
352 ;; Sanity check in case `diff' computation overflowed.
353 (<= (1- (ash past-cutoff -
16)) diff16
)
354 (<= diff16
(1+ (ash future-cutoff -
16))))
361 ;;; find-lisp.el ends here