*** empty log message ***
[bpt/emacs.git] / lisp / find-lisp.el
CommitLineData
6a05d05f 1;;; find-lisp.el --- emulation of find in Emacs Lisp
6344985d 2
6a05d05f 3;; Author: Peter Breton
6344985d
GM
4;; Created: Fri Mar 26 1999
5;; Keywords: unix
6a05d05f 6;; Time-stamp: <2001-07-16 12:42:35 pavel>
6344985d 7
0d30b337
TTN
8;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
9;; 2005 Free Software Foundation, Inc.
6344985d
GM
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
6344985d
GM
27
28;;; Commentary:
29;;
30;; This is a very generalized form of find; it basically implements a
31;; recursive directory descent. The conditions which bound the search
32;; are expressed as predicates, and I have not addressed the question
33;; of how to wrap up the common chores that find does in a simpler
34;; format than writing code for all the various predicates.
35;;
36;; Some random thoughts are to express simple queries directly with
37;; user-level functions, and perhaps use some kind of forms interface
38;; for medium-level queries. Really complicated queries can be
39;; expressed in Lisp.
82d11a0b 40;;
6344985d
GM
41
42;;; Todo
43;;
44;; It would be nice if we could sort the results without running the find
45;; again. Maybe that could work by storing the original file attributes?
46
47;;; Code:
48
3027fc3e
JB
49(defvar dired-buffers)
50(defvar dired-subdir-alist)
51
6344985d
GM
52;; Internal variables
53
54(defvar find-lisp-regexp nil
55 "Internal variable.")
56
57(defconst find-lisp-line-indent " "
58 "Indentation for dired file lines.")
59
60(defvar find-lisp-file-predicate nil
61 "Predicate for choosing to include files.")
62
63(defvar find-lisp-directory-predicate nil
64 "Predicate for choosing to descend into directories.")
65
66;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
67;; Debugging Code
68;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
69
70(defvar find-lisp-debug-buffer "*Find Lisp Debug*"
71 "Buffer for debugging information.")
72
73(defvar find-lisp-debug nil
74 "Whether debugging is enabled.")
75
76(defun find-lisp-debug-message (message)
77 "Print a debug message MESSAGE in `find-lisp-debug-buffer'."
78 (set-buffer (get-buffer-create find-lisp-debug-buffer))
79 (goto-char (point-max))
80 (insert message "\n"))
81
82;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83;; Directory and File predicates
84;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85
86(defun find-lisp-default-directory-predicate (dir parent)
87 "True if DIR is not a dot file, and not a symlink.
88PARENT is the parent directory of DIR."
89 (and find-lisp-debug
82d11a0b 90 (find-lisp-debug-message
6344985d
GM
91 (format "Processing directory %s in %s" dir parent)))
92 ;; Skip current and parent directories
93 (not (or (string= dir ".")
94 (string= dir "..")
95 ;; Skip directories which are symlinks
96 ;; Easy way to circumvent recursive loops
97 (file-symlink-p dir))))
98
99(defun find-lisp-default-file-predicate (file dir)
100 "True if FILE matches `find-lisp-regexp'.
101DIR is the directory containing FILE."
102 (and find-lisp-debug
82d11a0b 103 (find-lisp-debug-message
6344985d
GM
104 (format "Processing file %s in %s" file dir)))
105 (and (not (file-directory-p (expand-file-name file dir)))
106 (string-match find-lisp-regexp file)))
107
108(defun find-lisp-file-predicate-is-directory (file dir)
109 "True if FILE is a directory.
110Argument DIR is the directory containing FILE."
111 (and find-lisp-debug
82d11a0b 112 (find-lisp-debug-message
6344985d
GM
113 (format "Processing file %s in %s" file dir)))
114 (and (file-directory-p (expand-file-name file dir))
115 (not (or (string= file ".")
116 (string= file "..")))))
117
118;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119;; Find functions
120;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121
122(defun find-lisp-find-files (directory regexp)
123 "Find files in DIRECTORY which match REGEXP."
124 (let ((file-predicate 'find-lisp-default-file-predicate)
125 (directory-predicate 'find-lisp-default-directory-predicate)
43adc213 126 (find-lisp-regexp regexp))
82d11a0b
PB
127 (find-lisp-find-files-internal
128 directory
6344985d
GM
129 file-predicate
130 directory-predicate)))
131
132;; Workhorse function
82d11a0b 133(defun find-lisp-find-files-internal (directory file-predicate
6344985d
GM
134 directory-predicate)
135 "Find files under DIRECTORY which satisfy FILE-PREDICATE.
82d11a0b 136FILE-PREDICATE is a function which takes two arguments: the file and its
6344985d
GM
137directory.
138
139DIRECTORY-PREDICATE is used to decide whether to descend into directories.
140It is a function which takes two arguments, the directory and its parent."
43adc213 141 (setq directory (file-name-as-directory directory))
6344985d 142 (let (results sub-results)
43adc213
SM
143 (dolist (file (directory-files directory nil nil t))
144 (let ((fullname (expand-file-name file directory)))
145 (when (file-readable-p (expand-file-name file directory))
146 ;; If a directory, check it we should descend into it
147 (and (file-directory-p fullname)
148 (funcall directory-predicate file directory)
6344985d 149 (progn
43adc213
SM
150 (setq sub-results
151 (find-lisp-find-files-internal
152 fullname
153 file-predicate
154 directory-predicate))
155 (if results
156 (nconc results sub-results)
157 (setq results sub-results))))
158 ;; For all files and directories, call the file predicate
159 (and (funcall file-predicate file directory)
160 (if results
161 (nconc results (list fullname))
162 (setq results (list fullname)))))))
6344985d
GM
163 results))
164
165;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166;; Find-dired all in Lisp
167;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168
4ecdf04d 169;;;###autoload
6344985d
GM
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
175 dir
176 'find-lisp-default-file-predicate
177 'find-lisp-default-directory-predicate
178 "*Find Lisp Dired*")))
179
180;; Just the subdirectories
4ecdf04d 181;;;###autoload
6344985d
GM
182(defun find-lisp-find-dired-subdirectories (dir)
183 "Find all subdirectories of DIR."
184 (interactive "DFind subdirectories of directory: ")
185 (find-lisp-find-dired-internal
186 dir
187 'find-lisp-file-predicate-is-directory
188 'find-lisp-default-directory-predicate
189 "*Find Lisp Dired Subdirectories*"))
190
191;; Most of this is lifted from find-dired.el
82d11a0b
PB
192;;
193(defun find-lisp-find-dired-internal (dir file-predicate
6344985d
GM
194 directory-predicate buffer-name)
195 "Run find (Lisp version) and go into Dired mode on a buffer of the output."
196 (let ((dired-buffers dired-buffers)
197 buf
198 (regexp find-lisp-regexp))
199 ;; Expand DIR ("" means default-directory), and make sure it has a
200 ;; trailing slash.
201 (setq dir (abbreviate-file-name
202 (file-name-as-directory (expand-file-name dir))))
203 ;; Check that it's really a directory.
204 (or (file-directory-p dir)
205 (error "find-dired needs a directory: %s" dir))
82d11a0b 206 (or
6344985d
GM
207 (and (buffer-name)
208 (string= buffer-name (buffer-name)))
209 (switch-to-buffer (setq buf (get-buffer-create buffer-name))))
210 (widen)
211 (kill-all-local-variables)
212 (setq buffer-read-only nil)
213 (erase-buffer)
214 (setq default-directory dir)
215 (dired-mode dir)
216
217 (use-local-map (append (make-sparse-keymap) (current-local-map)))
218
219 (make-local-variable 'find-lisp-file-predicate)
220 (setq find-lisp-file-predicate file-predicate)
221 (make-local-variable 'find-lisp-directory-predicate)
222 (setq find-lisp-directory-predicate directory-predicate)
223 (make-local-variable 'find-lisp-regexp)
224 (setq find-lisp-regexp regexp)
225
226 (make-local-variable 'revert-buffer-function)
227 (setq revert-buffer-function
228 (function
229 (lambda(ignore1 ignore2)
82d11a0b 230 (find-lisp-insert-directory
6344985d
GM
231 default-directory
232 find-lisp-file-predicate
233 find-lisp-directory-predicate
234 'ignore)
235 )
236 ))
237
238 ;; Set subdir-alist so that Tree Dired will work:
239 (if (fboundp 'dired-simple-subdir-alist)
240 ;; will work even with nested dired format (dired-nstd.el,v 1.15
241 ;; and later)
242 (dired-simple-subdir-alist)
243 ;; else we have an ancient tree dired (or classic dired, where
82d11a0b 244 ;; this does no harm)
6344985d
GM
245 (set (make-local-variable 'dired-subdir-alist)
246 (list (cons default-directory (point-min-marker)))))
82d11a0b 247 (find-lisp-insert-directory
6344985d
GM
248 dir file-predicate directory-predicate 'ignore)
249 (goto-char (point-min))
250 (dired-goto-next-file)))
251
82d11a0b
PB
252(defun find-lisp-insert-directory (dir
253 file-predicate
254 directory-predicate
6344985d
GM
255 sort-function)
256 "Insert the results of `find-lisp-find-files' in the current buffer."
257 (let ((buffer-read-only nil)
82d11a0b
PB
258 (files (find-lisp-find-files-internal
259 dir
260 file-predicate
6344985d
GM
261 directory-predicate))
262 (len (length dir)))
263 (erase-buffer)
264 ;; Subdir headlerline must come first because the first marker in
265 ;; subdir-alist points there.
266 (insert find-lisp-line-indent dir ":\n")
267 ;; Make second line a ``find'' line in analogy to the ``total'' or
82d11a0b 268 ;; ``wildcard'' line.
6344985d
GM
269 ;;
270 ;; No analog for find-lisp?
271 (insert find-lisp-line-indent "\n")
272 ;; Run the find function
273 (mapcar
274 (function
275 (lambda(file)
82d11a0b 276 (find-lisp-find-dired-insert-file
6344985d
GM
277 (substring file len)
278 (current-buffer))))
279 (sort files 'string-lessp))
280 ;; FIXME: Sort function is ignored for now
281 ;; (funcall sort-function files))
282 (goto-char (point-min))
283 (dired-goto-next-file)))
284
4ecdf04d 285;;;###autoload
6344985d
GM
286(defun find-lisp-find-dired-filter (regexp)
287 "Change the filter on a find-lisp-find-dired buffer to REGEXP."
288 (interactive "sSet filter to regexp: ")
289 (setq find-lisp-regexp regexp)
290 (revert-buffer))
291
292(defun find-lisp-find-dired-insert-file (file buffer)
293 (set-buffer buffer)
82d11a0b 294 (insert find-lisp-line-indent
6344985d
GM
295 (find-lisp-format file (file-attributes file) (list "")
296 (current-time))))
297
298;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299;; Lifted from ls-lisp. We don't want to require it, because that
300;; would alter the insert-directory function.
301;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302
303(defun find-lisp-format (file-name file-attr switches now)
304 (let ((file-type (nth 0 file-attr)))
305 (concat (if (memq ?i switches) ; inode number
306 (format "%6d " (nth 10 file-attr)))
307 ;; nil is treated like "" in concat
308 (if (memq ?s switches) ; size in K
309 (format "%4d " (1+ (/ (nth 7 file-attr) 1024))))
310 (nth 8 file-attr) ; permission bits
311 ;; numeric uid/gid are more confusing than helpful
312 ;; Emacs should be able to make strings of them.
313 ;; user-login-name and user-full-name could take an
314 ;; optional arg.
315 (format " %3d %-8s %-8s %8d "
316 (nth 1 file-attr) ; no. of links
317 (if (= (user-uid) (nth 2 file-attr))
318 (user-login-name)
319 (int-to-string (nth 2 file-attr))) ; uid
320 (if (eq system-type 'ms-dos)
321 "root" ; everything is root on MSDOS.
322 (int-to-string (nth 3 file-attr))) ; gid
323 (nth 7 file-attr) ; size in bytes
324 )
325 (find-lisp-format-time file-attr switches now)
326 " "
327 file-name
328 (if (stringp file-type) ; is a symbolic link
329 (concat " -> " file-type)
330 "")
331 "\n")))
332
333(defun find-lisp-time-index (switches)
334 ;; Return index into file-attributes according to ls SWITCHES.
335 (cond
336 ((memq ?c switches) 6) ; last mode change
337 ((memq ?u switches) 4) ; last access
338 ;; default is last modtime
339 (t 5)))
340
341(defun find-lisp-format-time (file-attr switches now)
342 ;; Format time string for file with attributes FILE-ATTR according
343 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
344 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
345 ;; depending on distance between file date and NOW.
346 (let* ((time (nth (find-lisp-time-index switches) file-attr))
347 (diff16 (- (car time) (car now)))
348 (diff (+ (ash diff16 16) (- (car (cdr time)) (car (cdr now)))))
349 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
350 (future-cutoff (* 60 60))) ; 1 hour
351 (format-time-string
352 (if (and
353 (<= past-cutoff diff) (<= diff future-cutoff)
354 ;; Sanity check in case `diff' computation overflowed.
355 (<= (1- (ash past-cutoff -16)) diff16)
356 (<= diff16 (1+ (ash future-cutoff -16))))
357 "%b %e %H:%M"
358 "%b %e %Y")
359 time)))
360
361(provide 'find-lisp)
362
6344985d
GM
363;; Local Variables:
364;; autocompile: t
365;; End:
6a05d05f 366
ab5796a9 367;;; arch-tag: a711374c-f12a-46f6-aa18-ba7d77b9602a
6a05d05f 368;;; find-lisp.el ends here