(quoted-printable-encode-region): Remove redundant code
[emacs.git] / lisp / find-lisp.el
blobc11322439637fa12383356a01f2312d76de6ed6f
1 ;;; find-lisp.el --- Emulation of find in Emacs Lisp
3 ;; Author: Peter Breton
4 ;; Created: Fri Mar 26 1999
5 ;; Keywords: unix
6 ;; Time-stamp: <2000-10-04 00:17:29 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)
15 ;; any later version.
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.
27 ;;; Commentary:
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
38 ;; expressed in Lisp.
41 ;;; Todo
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?
46 ;;; Code:
48 ;; Internal variables
50 (defvar find-lisp-regexp nil
51 "Internal variable.")
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63 ;; Debugging Code
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."
85 (and find-lisp-debug
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 ".")
90 (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."
98 (and find-lisp-debug
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."
107 (and find-lisp-debug
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115 ;; Find functions
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))
123 (find-lisp-find-files-internal
124 directory
125 file-predicate
126 directory-predicate)))
128 ;; Workhorse function
129 (defun find-lisp-find-files-internal (directory file-predicate
130 directory-predicate)
131 "Find files under DIRECTORY which satisfy FILE-PREDICATE.
132 FILE-PREDICATE is a function which takes two arguments: the file and its
133 directory.
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."
137 (setq directory (file-name-as-directory directory))
138 (let (results sub-results)
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)
145 (progn
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)))))))
159 results))
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 ;; Find-dired all in Lisp
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
165 (defun find-lisp-find-dired (dir regexp)
166 "Find files in DIR, matching REGEXP."
167 (interactive "DFind files in directory: \nsMatching regexp: ")
168 (let ((find-lisp-regexp regexp))
169 (find-lisp-find-dired-internal
171 'find-lisp-default-file-predicate
172 'find-lisp-default-directory-predicate
173 "*Find Lisp Dired*")))
175 ;; Just the subdirectories
176 (defun find-lisp-find-dired-subdirectories (dir)
177 "Find all subdirectories of DIR."
178 (interactive "DFind subdirectories of directory: ")
179 (find-lisp-find-dired-internal
181 'find-lisp-file-predicate-is-directory
182 'find-lisp-default-directory-predicate
183 "*Find Lisp Dired Subdirectories*"))
185 ;; Most of this is lifted from find-dired.el
187 (defun find-lisp-find-dired-internal (dir file-predicate
188 directory-predicate buffer-name)
189 "Run find (Lisp version) and go into Dired mode on a buffer of the output."
190 (let ((dired-buffers dired-buffers)
192 (regexp find-lisp-regexp))
193 ;; Expand DIR ("" means default-directory), and make sure it has a
194 ;; trailing slash.
195 (setq dir (abbreviate-file-name
196 (file-name-as-directory (expand-file-name dir))))
197 ;; Check that it's really a directory.
198 (or (file-directory-p dir)
199 (error "find-dired needs a directory: %s" dir))
201 (and (buffer-name)
202 (string= buffer-name (buffer-name)))
203 (switch-to-buffer (setq buf (get-buffer-create buffer-name))))
204 (widen)
205 (kill-all-local-variables)
206 (setq buffer-read-only nil)
207 (erase-buffer)
208 (setq default-directory dir)
209 (dired-mode dir)
211 (use-local-map (append (make-sparse-keymap) (current-local-map)))
213 (make-local-variable 'find-lisp-file-predicate)
214 (setq find-lisp-file-predicate file-predicate)
215 (make-local-variable 'find-lisp-directory-predicate)
216 (setq find-lisp-directory-predicate directory-predicate)
217 (make-local-variable 'find-lisp-regexp)
218 (setq find-lisp-regexp regexp)
220 (make-local-variable 'revert-buffer-function)
221 (setq revert-buffer-function
222 (function
223 (lambda(ignore1 ignore2)
224 (find-lisp-insert-directory
225 default-directory
226 find-lisp-file-predicate
227 find-lisp-directory-predicate
228 'ignore)
232 ;; Set subdir-alist so that Tree Dired will work:
233 (if (fboundp 'dired-simple-subdir-alist)
234 ;; will work even with nested dired format (dired-nstd.el,v 1.15
235 ;; and later)
236 (dired-simple-subdir-alist)
237 ;; else we have an ancient tree dired (or classic dired, where
238 ;; this does no harm)
239 (set (make-local-variable 'dired-subdir-alist)
240 (list (cons default-directory (point-min-marker)))))
241 (find-lisp-insert-directory
242 dir file-predicate directory-predicate 'ignore)
243 (goto-char (point-min))
244 (dired-goto-next-file)))
246 (defun find-lisp-insert-directory (dir
247 file-predicate
248 directory-predicate
249 sort-function)
250 "Insert the results of `find-lisp-find-files' in the current buffer."
251 (let ((buffer-read-only nil)
252 (files (find-lisp-find-files-internal
254 file-predicate
255 directory-predicate))
256 (len (length dir)))
257 (erase-buffer)
258 ;; Subdir headlerline must come first because the first marker in
259 ;; subdir-alist points there.
260 (insert find-lisp-line-indent dir ":\n")
261 ;; Make second line a ``find'' line in analogy to the ``total'' or
262 ;; ``wildcard'' line.
264 ;; No analog for find-lisp?
265 (insert find-lisp-line-indent "\n")
266 ;; Run the find function
267 (mapcar
268 (function
269 (lambda(file)
270 (find-lisp-find-dired-insert-file
271 (substring file len)
272 (current-buffer))))
273 (sort files 'string-lessp))
274 ;; FIXME: Sort function is ignored for now
275 ;; (funcall sort-function files))
276 (goto-char (point-min))
277 (dired-goto-next-file)))
279 (defun find-lisp-find-dired-filter (regexp)
280 "Change the filter on a find-lisp-find-dired buffer to REGEXP."
281 (interactive "sSet filter to regexp: ")
282 (setq find-lisp-regexp regexp)
283 (revert-buffer))
285 (defun find-lisp-find-dired-insert-file (file buffer)
286 (set-buffer buffer)
287 (insert find-lisp-line-indent
288 (find-lisp-format file (file-attributes file) (list "")
289 (current-time))))
291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292 ;; Lifted from ls-lisp. We don't want to require it, because that
293 ;; would alter the insert-directory function.
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
296 (defun find-lisp-format (file-name file-attr switches now)
297 (let ((file-type (nth 0 file-attr)))
298 (concat (if (memq ?i switches) ; inode number
299 (format "%6d " (nth 10 file-attr)))
300 ;; nil is treated like "" in concat
301 (if (memq ?s switches) ; size in K
302 (format "%4d " (1+ (/ (nth 7 file-attr) 1024))))
303 (nth 8 file-attr) ; permission bits
304 ;; numeric uid/gid are more confusing than helpful
305 ;; Emacs should be able to make strings of them.
306 ;; user-login-name and user-full-name could take an
307 ;; optional arg.
308 (format " %3d %-8s %-8s %8d "
309 (nth 1 file-attr) ; no. of links
310 (if (= (user-uid) (nth 2 file-attr))
311 (user-login-name)
312 (int-to-string (nth 2 file-attr))) ; uid
313 (if (eq system-type 'ms-dos)
314 "root" ; everything is root on MSDOS.
315 (int-to-string (nth 3 file-attr))) ; gid
316 (nth 7 file-attr) ; size in bytes
318 (find-lisp-format-time file-attr switches now)
320 file-name
321 (if (stringp file-type) ; is a symbolic link
322 (concat " -> " file-type)
324 "\n")))
326 (defun find-lisp-time-index (switches)
327 ;; Return index into file-attributes according to ls SWITCHES.
328 (cond
329 ((memq ?c switches) 6) ; last mode change
330 ((memq ?u switches) 4) ; last access
331 ;; default is last modtime
332 (t 5)))
334 (defun find-lisp-format-time (file-attr switches now)
335 ;; Format time string for file with attributes FILE-ATTR according
336 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
337 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
338 ;; depending on distance between file date and NOW.
339 (let* ((time (nth (find-lisp-time-index switches) file-attr))
340 (diff16 (- (car time) (car now)))
341 (diff (+ (ash diff16 16) (- (car (cdr time)) (car (cdr now)))))
342 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
343 (future-cutoff (* 60 60))) ; 1 hour
344 (format-time-string
345 (if (and
346 (<= past-cutoff diff) (<= diff future-cutoff)
347 ;; Sanity check in case `diff' computation overflowed.
348 (<= (1- (ash past-cutoff -16)) diff16)
349 (<= diff16 (1+ (ash future-cutoff -16))))
350 "%b %e %H:%M"
351 "%b %e %Y")
352 time)))
354 (provide 'find-lisp)
356 ;;; find-lisp.el ends here
358 ;; Local Variables:
359 ;; autocompile: t
360 ;; End: