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