Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / find-lisp.el
blobe3b8391993b938202699cca7374eb673ab0ab0fa
1 ;;; find-lisp.el --- emulation of find in Emacs Lisp
3 ;; Author: Peter Breton
4 ;; Created: Fri Mar 26 1999
5 ;; Keywords: unix
7 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
8 ;; 2005, 2006, 2007, 2008 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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This is a very generalized form of find; it basically implements a
28 ;; recursive directory descent. The conditions which bound the search
29 ;; are expressed as predicates, and I have not addressed the question
30 ;; of how to wrap up the common chores that find does in a simpler
31 ;; format than writing code for all the various predicates.
33 ;; Some random thoughts are to express simple queries directly with
34 ;; user-level functions, and perhaps use some kind of forms interface
35 ;; for medium-level queries. Really complicated queries can be
36 ;; expressed in Lisp.
39 ;;; Todo
41 ;; It would be nice if we could sort the results without running the find
42 ;; again. Maybe that could work by storing the original file attributes?
44 ;;; Code:
46 (require 'dired)
48 (defvar dired-buffers)
49 (defvar dired-subdir-alist)
51 ;; Internal variables
53 (defvar find-lisp-regexp nil
54 "Internal variable.")
56 (defconst find-lisp-line-indent " "
57 "Indentation for dired file lines.")
59 (defvar find-lisp-file-predicate nil
60 "Predicate for choosing to include files.")
62 (defvar find-lisp-directory-predicate nil
63 "Predicate for choosing to descend into directories.")
65 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66 ;; Debugging Code
67 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
69 (defvar find-lisp-debug-buffer "*Find Lisp Debug*"
70 "Buffer for debugging information.")
72 (defvar find-lisp-debug nil
73 "Whether debugging is enabled.")
75 (defun find-lisp-debug-message (message)
76 "Print a debug message MESSAGE in `find-lisp-debug-buffer'."
77 (set-buffer (get-buffer-create find-lisp-debug-buffer))
78 (goto-char (point-max))
79 (insert message "\n"))
81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82 ;; Directory and File predicates
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85 (defun find-lisp-default-directory-predicate (dir parent)
86 "True if DIR is not a dot file, and not a symlink.
87 PARENT is the parent directory of DIR."
88 (and find-lisp-debug
89 (find-lisp-debug-message
90 (format "Processing directory %s in %s" dir parent)))
91 ;; Skip current and parent directories
92 (not (or (string= dir ".")
93 (string= dir "..")
94 ;; Skip directories which are symlinks
95 ;; Easy way to circumvent recursive loops
96 (file-symlink-p (expand-file-name dir parent)))))
98 (defun find-lisp-default-file-predicate (file dir)
99 "True if FILE matches `find-lisp-regexp'.
100 DIR is the directory containing FILE."
101 (and find-lisp-debug
102 (find-lisp-debug-message
103 (format "Processing file %s in %s" file dir)))
104 (and (not (file-directory-p (expand-file-name file dir)))
105 (string-match find-lisp-regexp file)))
107 (defun find-lisp-file-predicate-is-directory (file dir)
108 "True if FILE is a directory.
109 Argument DIR is the directory containing FILE."
110 (and find-lisp-debug
111 (find-lisp-debug-message
112 (format "Processing file %s in %s" file dir)))
113 (and (file-directory-p (expand-file-name file dir))
114 (not (or (string= file ".")
115 (string= file "..")))))
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;; Find functions
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121 (defun find-lisp-find-files (directory regexp)
122 "Find files in DIRECTORY which match REGEXP."
123 (let ((file-predicate 'find-lisp-default-file-predicate)
124 (directory-predicate 'find-lisp-default-directory-predicate)
125 (find-lisp-regexp regexp))
126 (find-lisp-find-files-internal
127 directory
128 file-predicate
129 directory-predicate)))
131 ;; Workhorse function
132 (defun find-lisp-find-files-internal (directory file-predicate
133 directory-predicate)
134 "Find files under DIRECTORY which satisfy FILE-PREDICATE.
135 FILE-PREDICATE is a function which takes two arguments: the file and its
136 directory.
138 DIRECTORY-PREDICATE is used to decide whether to descend into directories.
139 It is a function which takes two arguments, the directory and its parent."
140 (setq directory (file-name-as-directory directory))
141 (let (results sub-results)
142 (dolist (file (directory-files directory nil nil t))
143 (let ((fullname (expand-file-name file directory)))
144 (when (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)
148 (progn
149 (setq sub-results
150 (find-lisp-find-files-internal
151 fullname
152 file-predicate
153 directory-predicate))
154 (if results
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)
159 (if results
160 (nconc results (list fullname))
161 (setq results (list fullname)))))))
162 results))
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
165 ;; Find-dired all in Lisp
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 ;;;###autoload
169 (defun find-lisp-find-dired (dir regexp)
170 "Find files in DIR, matching REGEXP."
171 (interactive "DFind files in directory: \nsMatching regexp: ")
172 (let ((find-lisp-regexp regexp))
173 (find-lisp-find-dired-internal
175 'find-lisp-default-file-predicate
176 'find-lisp-default-directory-predicate
177 "*Find Lisp Dired*")))
179 ;; Just the subdirectories
180 ;;;###autoload
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
199 ;; trailing slash.
200 (setq dir (file-name-as-directory (expand-file-name dir)))
201 ;; Check that it's really a directory.
202 (or (file-directory-p dir)
203 (error "find-dired needs a directory: %s" dir))
205 (and (buffer-name)
206 (string= buffer-name (buffer-name)))
207 (switch-to-buffer (setq buf (get-buffer-create buffer-name))))
208 (widen)
209 (kill-all-local-variables)
210 (setq buffer-read-only nil)
211 (erase-buffer)
212 (setq default-directory dir)
213 (dired-mode dir)
215 (use-local-map (append (make-sparse-keymap) (current-local-map)))
217 (make-local-variable 'find-lisp-file-predicate)
218 (setq find-lisp-file-predicate file-predicate)
219 (make-local-variable 'find-lisp-directory-predicate)
220 (setq find-lisp-directory-predicate directory-predicate)
221 (make-local-variable 'find-lisp-regexp)
222 (setq find-lisp-regexp regexp)
224 (make-local-variable 'revert-buffer-function)
225 (setq revert-buffer-function
226 (function
227 (lambda(ignore1 ignore2)
228 (find-lisp-insert-directory
229 default-directory
230 find-lisp-file-predicate
231 find-lisp-directory-predicate
232 'ignore)
236 ;; Set subdir-alist so that Tree Dired will work:
237 (if (fboundp 'dired-simple-subdir-alist)
238 ;; will work even with nested dired format (dired-nstd.el,v 1.15
239 ;; and later)
240 (dired-simple-subdir-alist)
241 ;; else we have an ancient tree dired (or classic dired, where
242 ;; this does no harm)
243 (set (make-local-variable 'dired-subdir-alist)
244 (list (cons default-directory (point-min-marker)))))
245 (find-lisp-insert-directory
246 dir file-predicate directory-predicate 'ignore)
247 (goto-char (point-min))
248 (dired-goto-next-file)))
250 (defun find-lisp-insert-directory (dir
251 file-predicate
252 directory-predicate
253 sort-function)
254 "Insert the results of `find-lisp-find-files' in the current buffer."
255 (let ((buffer-read-only nil)
256 (files (find-lisp-find-files-internal
258 file-predicate
259 directory-predicate))
260 (len (length dir)))
261 (erase-buffer)
262 ;; Subdir headlerline must come first because the first marker in
263 ;; subdir-alist points there.
264 (insert find-lisp-line-indent dir ":\n")
265 ;; Make second line a ``find'' line in analogy to the ``total'' or
266 ;; ``wildcard'' line.
268 ;; No analog for find-lisp?
269 (insert find-lisp-line-indent "\n")
270 ;; Run the find function
271 (mapc
272 (function
273 (lambda(file)
274 (find-lisp-find-dired-insert-file
275 (substring file len)
276 (current-buffer))))
277 (sort files 'string-lessp))
278 ;; FIXME: Sort function is ignored for now
279 ;; (funcall sort-function files))
280 (goto-char (point-min))
281 (dired-goto-next-file)))
283 ;;;###autoload
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)
288 (revert-buffer))
290 (defun find-lisp-find-dired-insert-file (file buffer)
291 (set-buffer buffer)
292 (insert find-lisp-line-indent
293 (find-lisp-format file (file-attributes file 'string) (list "")
294 (current-time))))
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 (format " %3d %-8s %-8s %8d "
310 (nth 1 file-attr) ; no. of links
311 (if (numberp (nth 2 file-attr))
312 (int-to-string (nth 2 file-attr))
313 (nth 2 file-attr)) ; uid
314 (if (eq system-type 'ms-dos)
315 "root" ; everything is root on MSDOS.
316 (if (numberp (nth 3 file-attr))
317 (int-to-string (nth 3 file-attr))
318 (nth 3 file-attr))) ; gid
319 (nth 7 file-attr) ; size in bytes
321 (find-lisp-format-time file-attr switches now)
323 file-name
324 (if (stringp file-type) ; is a symbolic link
325 (concat " -> " file-type)
327 "\n")))
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)))
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)))
357 (provide 'find-lisp)
359 ;; arch-tag: a711374c-f12a-46f6-aa18-ba7d77b9602a
360 ;;; find-lisp.el ends here