38431
|
1 ;;; find-lisp.el --- emulation of find in Emacs Lisp
|
29696
|
2
|
38431
|
3 ;; Author: Peter Breton
|
29696
|
4 ;; Created: Fri Mar 26 1999
|
|
5 ;; Keywords: unix
|
38431
|
6 ;; Time-stamp: <2001-07-16 12:42:35 pavel>
|
29696
|
7
|
|
8 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
|
|
9
|
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
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)
|
|
15 ;; any later version.
|
|
16
|
|
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.
|
|
21
|
|
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.
|
|
26
|
|
27 ;;; Commentary:
|
|
28 ;;
|
|
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.
|
|
34 ;;
|
|
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
|
|
38 ;; expressed in Lisp.
|
32116
|
39 ;;
|
29696
|
40
|
|
41 ;;; Todo
|
|
42 ;;
|
|
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?
|
|
45
|
|
46 ;;; Code:
|
|
47
|
|
48 ;; Internal variables
|
|
49
|
|
50 (defvar find-lisp-regexp nil
|
|
51 "Internal variable.")
|
|
52
|
|
53 (defconst find-lisp-line-indent " "
|
|
54 "Indentation for dired file lines.")
|
|
55
|
|
56 (defvar find-lisp-file-predicate nil
|
|
57 "Predicate for choosing to include files.")
|
|
58
|
|
59 (defvar find-lisp-directory-predicate nil
|
|
60 "Predicate for choosing to descend into directories.")
|
|
61
|
|
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
63 ;; Debugging Code
|
|
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
65
|
|
66 (defvar find-lisp-debug-buffer "*Find Lisp Debug*"
|
|
67 "Buffer for debugging information.")
|
|
68
|
|
69 (defvar find-lisp-debug nil
|
|
70 "Whether debugging is enabled.")
|
|
71
|
|
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"))
|
|
77
|
|
78 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
79 ;; Directory and File predicates
|
|
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
81
|
|
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."
|
|
85 (and find-lisp-debug
|
32116
|
86 (find-lisp-debug-message
|
29696
|
87 (format "Processing directory %s in %s" dir parent)))
|
|
88 ;; Skip current and parent directories
|
|
89 (not (or (string= dir ".")
|
|
90 (string= dir "..")
|
|
91 ;; Skip directories which are symlinks
|
|
92 ;; Easy way to circumvent recursive loops
|
|
93 (file-symlink-p dir))))
|
|
94
|
|
95 (defun find-lisp-default-file-predicate (file dir)
|
|
96 "True if FILE matches `find-lisp-regexp'.
|
|
97 DIR is the directory containing FILE."
|
|
98 (and find-lisp-debug
|
32116
|
99 (find-lisp-debug-message
|
29696
|
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)))
|
|
103
|
|
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."
|
|
107 (and find-lisp-debug
|
32116
|
108 (find-lisp-debug-message
|
29696
|
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 "..")))))
|
|
113
|
|
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
115 ;; Find functions
|
|
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
117
|
|
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)
|
33710
|
122 (find-lisp-regexp regexp))
|
32116
|
123 (find-lisp-find-files-internal
|
|
124 directory
|
29696
|
125 file-predicate
|
|
126 directory-predicate)))
|
|
127
|
|
128 ;; Workhorse function
|
32116
|
129 (defun find-lisp-find-files-internal (directory file-predicate
|
29696
|
130 directory-predicate)
|
|
131 "Find files under DIRECTORY which satisfy FILE-PREDICATE.
|
32116
|
132 FILE-PREDICATE is a function which takes two arguments: the file and its
|
29696
|
133 directory.
|
|
134
|
|
135 DIRECTORY-PREDICATE is used to decide whether to descend into directories.
|
|
136 It is a function which takes two arguments, the directory and its parent."
|
33710
|
137 (setq directory (file-name-as-directory directory))
|
29696
|
138 (let (results sub-results)
|
33710
|
139 (dolist (file (directory-files directory nil nil t))
|
|
140 (let ((fullname (expand-file-name file directory)))
|
|
141 (when (file-readable-p (expand-file-name file directory))
|
|
142 ;; If a directory, check it we should descend into it
|
|
143 (and (file-directory-p fullname)
|
|
144 (funcall directory-predicate file directory)
|
29696
|
145 (progn
|
33710
|
146 (setq sub-results
|
|
147 (find-lisp-find-files-internal
|
|
148 fullname
|
|
149 file-predicate
|
|
150 directory-predicate))
|
|
151 (if results
|
|
152 (nconc results sub-results)
|
|
153 (setq results sub-results))))
|
|
154 ;; For all files and directories, call the file predicate
|
|
155 (and (funcall file-predicate file directory)
|
|
156 (if results
|
|
157 (nconc results (list fullname))
|
|
158 (setq results (list fullname)))))))
|
29696
|
159 results))
|
|
160
|
|
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
162 ;; Find-dired all in Lisp
|
|
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
164
|
37471
|
165 ;;;###autoload
|
29696
|
166 (defun find-lisp-find-dired (dir regexp)
|
|
167 "Find files in DIR, matching REGEXP."
|
|
168 (interactive "DFind files in directory: \nsMatching regexp: ")
|
|
169 (let ((find-lisp-regexp regexp))
|
|
170 (find-lisp-find-dired-internal
|
|
171 dir
|
|
172 'find-lisp-default-file-predicate
|
|
173 'find-lisp-default-directory-predicate
|
|
174 "*Find Lisp Dired*")))
|
|
175
|
|
176 ;; Just the subdirectories
|
37471
|
177 ;;;###autoload
|
29696
|
178 (defun find-lisp-find-dired-subdirectories (dir)
|
|
179 "Find all subdirectories of DIR."
|
|
180 (interactive "DFind subdirectories of directory: ")
|
|
181 (find-lisp-find-dired-internal
|
|
182 dir
|
|
183 'find-lisp-file-predicate-is-directory
|
|
184 'find-lisp-default-directory-predicate
|
|
185 "*Find Lisp Dired Subdirectories*"))
|
|
186
|
|
187 ;; Most of this is lifted from find-dired.el
|
32116
|
188 ;;
|
|
189 (defun find-lisp-find-dired-internal (dir file-predicate
|
29696
|
190 directory-predicate buffer-name)
|
|
191 "Run find (Lisp version) and go into Dired mode on a buffer of the output."
|
|
192 (let ((dired-buffers dired-buffers)
|
|
193 buf
|
|
194 (regexp find-lisp-regexp))
|
|
195 ;; Expand DIR ("" means default-directory), and make sure it has a
|
|
196 ;; trailing slash.
|
|
197 (setq dir (abbreviate-file-name
|
|
198 (file-name-as-directory (expand-file-name dir))))
|
|
199 ;; Check that it's really a directory.
|
|
200 (or (file-directory-p dir)
|
|
201 (error "find-dired needs a directory: %s" dir))
|
32116
|
202 (or
|
29696
|
203 (and (buffer-name)
|
|
204 (string= buffer-name (buffer-name)))
|
|
205 (switch-to-buffer (setq buf (get-buffer-create buffer-name))))
|
|
206 (widen)
|
|
207 (kill-all-local-variables)
|
|
208 (setq buffer-read-only nil)
|
|
209 (erase-buffer)
|
|
210 (setq default-directory dir)
|
|
211 (dired-mode dir)
|
|
212
|
|
213 (use-local-map (append (make-sparse-keymap) (current-local-map)))
|
|
214
|
|
215 (make-local-variable 'find-lisp-file-predicate)
|
|
216 (setq find-lisp-file-predicate file-predicate)
|
|
217 (make-local-variable 'find-lisp-directory-predicate)
|
|
218 (setq find-lisp-directory-predicate directory-predicate)
|
|
219 (make-local-variable 'find-lisp-regexp)
|
|
220 (setq find-lisp-regexp regexp)
|
|
221
|
|
222 (make-local-variable 'revert-buffer-function)
|
|
223 (setq revert-buffer-function
|
|
224 (function
|
|
225 (lambda(ignore1 ignore2)
|
32116
|
226 (find-lisp-insert-directory
|
29696
|
227 default-directory
|
|
228 find-lisp-file-predicate
|
|
229 find-lisp-directory-predicate
|
|
230 'ignore)
|
|
231 )
|
|
232 ))
|
|
233
|
|
234 ;; Set subdir-alist so that Tree Dired will work:
|
|
235 (if (fboundp 'dired-simple-subdir-alist)
|
|
236 ;; will work even with nested dired format (dired-nstd.el,v 1.15
|
|
237 ;; and later)
|
|
238 (dired-simple-subdir-alist)
|
|
239 ;; else we have an ancient tree dired (or classic dired, where
|
32116
|
240 ;; this does no harm)
|
29696
|
241 (set (make-local-variable 'dired-subdir-alist)
|
|
242 (list (cons default-directory (point-min-marker)))))
|
32116
|
243 (find-lisp-insert-directory
|
29696
|
244 dir file-predicate directory-predicate 'ignore)
|
|
245 (goto-char (point-min))
|
|
246 (dired-goto-next-file)))
|
|
247
|
32116
|
248 (defun find-lisp-insert-directory (dir
|
|
249 file-predicate
|
|
250 directory-predicate
|
29696
|
251 sort-function)
|
|
252 "Insert the results of `find-lisp-find-files' in the current buffer."
|
|
253 (let ((buffer-read-only nil)
|
32116
|
254 (files (find-lisp-find-files-internal
|
|
255 dir
|
|
256 file-predicate
|
29696
|
257 directory-predicate))
|
|
258 (len (length dir)))
|
|
259 (erase-buffer)
|
|
260 ;; Subdir headlerline must come first because the first marker in
|
|
261 ;; subdir-alist points there.
|
|
262 (insert find-lisp-line-indent dir ":\n")
|
|
263 ;; Make second line a ``find'' line in analogy to the ``total'' or
|
32116
|
264 ;; ``wildcard'' line.
|
29696
|
265 ;;
|
|
266 ;; No analog for find-lisp?
|
|
267 (insert find-lisp-line-indent "\n")
|
|
268 ;; Run the find function
|
|
269 (mapcar
|
|
270 (function
|
|
271 (lambda(file)
|
32116
|
272 (find-lisp-find-dired-insert-file
|
29696
|
273 (substring file len)
|
|
274 (current-buffer))))
|
|
275 (sort files 'string-lessp))
|
|
276 ;; FIXME: Sort function is ignored for now
|
|
277 ;; (funcall sort-function files))
|
|
278 (goto-char (point-min))
|
|
279 (dired-goto-next-file)))
|
|
280
|
37471
|
281 ;;;###autoload
|
29696
|
282 (defun find-lisp-find-dired-filter (regexp)
|
|
283 "Change the filter on a find-lisp-find-dired buffer to REGEXP."
|
|
284 (interactive "sSet filter to regexp: ")
|
|
285 (setq find-lisp-regexp regexp)
|
|
286 (revert-buffer))
|
|
287
|
|
288 (defun find-lisp-find-dired-insert-file (file buffer)
|
|
289 (set-buffer buffer)
|
32116
|
290 (insert find-lisp-line-indent
|
29696
|
291 (find-lisp-format file (file-attributes file) (list "")
|
|
292 (current-time))))
|
|
293
|
|
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
295 ;; Lifted from ls-lisp. We don't want to require it, because that
|
|
296 ;; would alter the insert-directory function.
|
|
297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
298
|
|
299 (defun find-lisp-format (file-name file-attr switches now)
|
|
300 (let ((file-type (nth 0 file-attr)))
|
|
301 (concat (if (memq ?i switches) ; inode number
|
|
302 (format "%6d " (nth 10 file-attr)))
|
|
303 ;; nil is treated like "" in concat
|
|
304 (if (memq ?s switches) ; size in K
|
|
305 (format "%4d " (1+ (/ (nth 7 file-attr) 1024))))
|
|
306 (nth 8 file-attr) ; permission bits
|
|
307 ;; numeric uid/gid are more confusing than helpful
|
|
308 ;; Emacs should be able to make strings of them.
|
|
309 ;; user-login-name and user-full-name could take an
|
|
310 ;; optional arg.
|
|
311 (format " %3d %-8s %-8s %8d "
|
|
312 (nth 1 file-attr) ; no. of links
|
|
313 (if (= (user-uid) (nth 2 file-attr))
|
|
314 (user-login-name)
|
|
315 (int-to-string (nth 2 file-attr))) ; uid
|
|
316 (if (eq system-type 'ms-dos)
|
|
317 "root" ; everything is root on MSDOS.
|
|
318 (int-to-string (nth 3 file-attr))) ; gid
|
|
319 (nth 7 file-attr) ; size in bytes
|
|
320 )
|
|
321 (find-lisp-format-time file-attr switches now)
|
|
322 " "
|
|
323 file-name
|
|
324 (if (stringp file-type) ; is a symbolic link
|
|
325 (concat " -> " file-type)
|
|
326 "")
|
|
327 "\n")))
|
|
328
|
|
329 (defun find-lisp-time-index (switches)
|
|
330 ;; Return index into file-attributes according to ls SWITCHES.
|
|
331 (cond
|
|
332 ((memq ?c switches) 6) ; last mode change
|
|
333 ((memq ?u switches) 4) ; last access
|
|
334 ;; default is last modtime
|
|
335 (t 5)))
|
|
336
|
|
337 (defun find-lisp-format-time (file-attr switches now)
|
|
338 ;; Format time string for file with attributes FILE-ATTR according
|
|
339 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
|
|
340 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
|
|
341 ;; depending on distance between file date and NOW.
|
|
342 (let* ((time (nth (find-lisp-time-index switches) file-attr))
|
|
343 (diff16 (- (car time) (car now)))
|
|
344 (diff (+ (ash diff16 16) (- (car (cdr time)) (car (cdr now)))))
|
|
345 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
|
|
346 (future-cutoff (* 60 60))) ; 1 hour
|
|
347 (format-time-string
|
|
348 (if (and
|
|
349 (<= past-cutoff diff) (<= diff future-cutoff)
|
|
350 ;; Sanity check in case `diff' computation overflowed.
|
|
351 (<= (1- (ash past-cutoff -16)) diff16)
|
|
352 (<= diff16 (1+ (ash future-cutoff -16))))
|
|
353 "%b %e %H:%M"
|
|
354 "%b %e %Y")
|
|
355 time)))
|
|
356
|
|
357 (provide 'find-lisp)
|
|
358
|
|
359 ;; Local Variables:
|
|
360 ;; autocompile: t
|
|
361 ;; End:
|
38431
|
362
|
|
363 ;;; find-lisp.el ends here
|