Convert consecutive FSF copyright years to ranges.
[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
6344985d 6
73b0cd50 7;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
6344985d
GM
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
6344985d 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
6344985d
GM
15
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.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6344985d
GM
23
24;;; Commentary:
25;;
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.
31;;
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.
82d11a0b 36;;
6344985d
GM
37
38;;; Todo
39;;
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?
42
43;;; Code:
44
9c797a34
LT
45(require 'dired)
46
3027fc3e
JB
47(defvar dired-buffers)
48(defvar dired-subdir-alist)
49
6344985d
GM
50;; Internal variables
51
52(defvar find-lisp-regexp nil
53 "Internal variable.")
54
55(defconst find-lisp-line-indent " "
56 "Indentation for dired file lines.")
57
58(defvar find-lisp-file-predicate nil
59 "Predicate for choosing to include files.")
60
61(defvar find-lisp-directory-predicate nil
62 "Predicate for choosing to descend into directories.")
63
64;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65;; Debugging Code
66;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
67
68(defvar find-lisp-debug-buffer "*Find Lisp Debug*"
69 "Buffer for debugging information.")
70
71(defvar find-lisp-debug nil
72 "Whether debugging is enabled.")
73
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"))
79
80;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81;; Directory and File predicates
82;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83
84(defun find-lisp-default-directory-predicate (dir parent)
85 "True if DIR is not a dot file, and not a symlink.
86PARENT is the parent directory of DIR."
87 (and find-lisp-debug
82d11a0b 88 (find-lisp-debug-message
6344985d
GM
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
8d60fcd5 95 (file-symlink-p (expand-file-name dir parent)))))
6344985d
GM
96
97(defun find-lisp-default-file-predicate (file dir)
98 "True if FILE matches `find-lisp-regexp'.
99DIR is the directory containing FILE."
100 (and find-lisp-debug
82d11a0b 101 (find-lisp-debug-message
6344985d
GM
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)))
105
106(defun find-lisp-file-predicate-is-directory (file dir)
107 "True if FILE is a directory.
108Argument DIR is the directory containing FILE."
109 (and find-lisp-debug
82d11a0b 110 (find-lisp-debug-message
6344985d
GM
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 "..")))))
115
116;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117;; Find functions
118;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119
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)
43adc213 124 (find-lisp-regexp regexp))
82d11a0b
PB
125 (find-lisp-find-files-internal
126 directory
6344985d
GM
127 file-predicate
128 directory-predicate)))
129
130;; Workhorse function
82d11a0b 131(defun find-lisp-find-files-internal (directory file-predicate
6344985d
GM
132 directory-predicate)
133 "Find files under DIRECTORY which satisfy FILE-PREDICATE.
82d11a0b 134FILE-PREDICATE is a function which takes two arguments: the file and its
6344985d
GM
135directory.
136
137DIRECTORY-PREDICATE is used to decide whether to descend into directories.
138It is a function which takes two arguments, the directory and its parent."
43adc213 139 (setq directory (file-name-as-directory directory))
6344985d 140 (let (results sub-results)
43adc213
SM
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)
6344985d 147 (progn
43adc213
SM
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)))))))
6344985d
GM
161 results))
162
163;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164;; Find-dired all in Lisp
165;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166
4ecdf04d 167;;;###autoload
6344985d
GM
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
173 dir
174 'find-lisp-default-file-predicate
175 'find-lisp-default-directory-predicate
176 "*Find Lisp Dired*")))
177
178;; Just the subdirectories
4ecdf04d 179;;;###autoload
6344985d
GM
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
184 dir
185 'find-lisp-file-predicate-is-directory
186 'find-lisp-default-directory-predicate
187 "*Find Lisp Dired Subdirectories*"))
188
189;; Most of this is lifted from find-dired.el
82d11a0b
PB
190;;
191(defun find-lisp-find-dired-internal (dir file-predicate
6344985d
GM
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)
195 buf
196 (regexp find-lisp-regexp))
197 ;; Expand DIR ("" means default-directory), and make sure it has a
198 ;; trailing slash.
9c797a34 199 (setq dir (file-name-as-directory (expand-file-name dir)))
6344985d
GM
200 ;; Check that it's really a directory.
201 (or (file-directory-p dir)
202 (error "find-dired needs a directory: %s" dir))
82d11a0b 203 (or
6344985d
GM
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)
213
214 (use-local-map (append (make-sparse-keymap) (current-local-map)))
215
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)
222
223 (make-local-variable 'revert-buffer-function)
224 (setq revert-buffer-function
225 (function
226 (lambda(ignore1 ignore2)
82d11a0b 227 (find-lisp-insert-directory
6344985d
GM
228 default-directory
229 find-lisp-file-predicate
230 find-lisp-directory-predicate
231 'ignore)
232 )
233 ))
234
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
82d11a0b 241 ;; this does no harm)
6344985d
GM
242 (set (make-local-variable 'dired-subdir-alist)
243 (list (cons default-directory (point-min-marker)))))
82d11a0b 244 (find-lisp-insert-directory
6344985d
GM
245 dir file-predicate directory-predicate 'ignore)
246 (goto-char (point-min))
247 (dired-goto-next-file)))
248
82d11a0b
PB
249(defun find-lisp-insert-directory (dir
250 file-predicate
251 directory-predicate
6344985d
GM
252 sort-function)
253 "Insert the results of `find-lisp-find-files' in the current buffer."
254 (let ((buffer-read-only nil)
82d11a0b
PB
255 (files (find-lisp-find-files-internal
256 dir
257 file-predicate
6344985d
GM
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
82d11a0b 265 ;; ``wildcard'' line.
6344985d
GM
266 ;;
267 ;; No analog for find-lisp?
268 (insert find-lisp-line-indent "\n")
269 ;; Run the find function
1259dfc9 270 (mapc
6344985d
GM
271 (function
272 (lambda(file)
82d11a0b 273 (find-lisp-find-dired-insert-file
6344985d
GM
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)))
281
4ecdf04d 282;;;###autoload
6344985d
GM
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))
288
289(defun find-lisp-find-dired-insert-file (file buffer)
290 (set-buffer buffer)
82d11a0b 291 (insert find-lisp-line-indent
dcd6fc38 292 (find-lisp-format file (file-attributes file 'string) (list "")
6344985d
GM
293 (current-time))))
294
295;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
296;; Lifted from ls-lisp. We don't want to require it, because that
297;; would alter the insert-directory function.
298;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299
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
6344985d
GM
308 (format " %3d %-8s %-8s %8d "
309 (nth 1 file-attr) ; no. of links
dcd6fc38
LT
310 (if (numberp (nth 2 file-attr))
311 (int-to-string (nth 2 file-attr))
312 (nth 2 file-attr)) ; uid
6344985d
GM
313 (if (eq system-type 'ms-dos)
314 "root" ; everything is root on MSDOS.
dcd6fc38
LT
315 (if (numberp (nth 3 file-attr))
316 (int-to-string (nth 3 file-attr))
317 (nth 3 file-attr))) ; gid
6344985d
GM
318 (nth 7 file-attr) ; size in bytes
319 )
320 (find-lisp-format-time file-attr switches now)
321 " "
322 file-name
323 (if (stringp file-type) ; is a symbolic link
324 (concat " -> " file-type)
325 "")
326 "\n")))
327
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)))
335
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)))
355
356(provide 'find-lisp)
357
6a05d05f 358;;; find-lisp.el ends here