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