Mercurial > emacs
comparison lisp/find-lisp.el @ 32116:3f09592bbc80
* find-lisp.el (find-lisp-find-files-internal):
Make sure directory name ends with "/".
author | Peter Breton <pbreton@attbi.com> |
---|---|
date | Wed, 04 Oct 2000 04:19:10 +0000 |
parents | a5051216d618 |
children | bec245584796 |
comparison
equal
deleted
inserted
replaced
32115:5357a26e85d1 | 32116:3f09592bbc80 |
---|---|
1 ;;; find-lisp.el --- Emulation of find in Emacs Lisp | 1 ;;; find-lisp.el --- Emulation of find in Emacs Lisp |
2 | 2 |
3 ;; Author: Peter Breton | 3 ;; Author: Peter Breton |
4 ;; Created: Fri Mar 26 1999 | 4 ;; Created: Fri Mar 26 1999 |
5 ;; Keywords: unix | 5 ;; Keywords: unix |
6 ;; Time-stamp: <1999-04-19 16:37:01 pbreton> | 6 ;; Time-stamp: <2000-10-04 00:17:29 pbreton> |
7 | 7 |
8 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc. | 8 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc. |
9 | 9 |
10 ;; This file is part of GNU Emacs. | 10 ;; This file is part of GNU Emacs. |
11 | 11 |
34 ;; | 34 ;; |
35 ;; Some random thoughts are to express simple queries directly with | 35 ;; Some random thoughts are to express simple queries directly with |
36 ;; user-level functions, and perhaps use some kind of forms interface | 36 ;; user-level functions, and perhaps use some kind of forms interface |
37 ;; for medium-level queries. Really complicated queries can be | 37 ;; for medium-level queries. Really complicated queries can be |
38 ;; expressed in Lisp. | 38 ;; expressed in Lisp. |
39 ;; | 39 ;; |
40 | 40 |
41 ;;; Todo | 41 ;;; Todo |
42 ;; | 42 ;; |
43 ;; It would be nice if we could sort the results without running the find | 43 ;; It would be nice if we could sort the results without running the find |
44 ;; again. Maybe that could work by storing the original file attributes? | 44 ;; again. Maybe that could work by storing the original file attributes? |
81 | 81 |
82 (defun find-lisp-default-directory-predicate (dir parent) | 82 (defun find-lisp-default-directory-predicate (dir parent) |
83 "True if DIR is not a dot file, and not a symlink. | 83 "True if DIR is not a dot file, and not a symlink. |
84 PARENT is the parent directory of DIR." | 84 PARENT is the parent directory of DIR." |
85 (and find-lisp-debug | 85 (and find-lisp-debug |
86 (find-lisp-debug-message | 86 (find-lisp-debug-message |
87 (format "Processing directory %s in %s" dir parent))) | 87 (format "Processing directory %s in %s" dir parent))) |
88 ;; Skip current and parent directories | 88 ;; Skip current and parent directories |
89 (not (or (string= dir ".") | 89 (not (or (string= dir ".") |
90 (string= dir "..") | 90 (string= dir "..") |
91 ;; Skip directories which are symlinks | 91 ;; Skip directories which are symlinks |
94 | 94 |
95 (defun find-lisp-default-file-predicate (file dir) | 95 (defun find-lisp-default-file-predicate (file dir) |
96 "True if FILE matches `find-lisp-regexp'. | 96 "True if FILE matches `find-lisp-regexp'. |
97 DIR is the directory containing FILE." | 97 DIR is the directory containing FILE." |
98 (and find-lisp-debug | 98 (and find-lisp-debug |
99 (find-lisp-debug-message | 99 (find-lisp-debug-message |
100 (format "Processing file %s in %s" file dir))) | 100 (format "Processing file %s in %s" file dir))) |
101 (and (not (file-directory-p (expand-file-name file dir))) | 101 (and (not (file-directory-p (expand-file-name file dir))) |
102 (string-match find-lisp-regexp file))) | 102 (string-match find-lisp-regexp file))) |
103 | 103 |
104 (defun find-lisp-file-predicate-is-directory (file dir) | 104 (defun find-lisp-file-predicate-is-directory (file dir) |
105 "True if FILE is a directory. | 105 "True if FILE is a directory. |
106 Argument DIR is the directory containing FILE." | 106 Argument DIR is the directory containing FILE." |
107 (and find-lisp-debug | 107 (and find-lisp-debug |
108 (find-lisp-debug-message | 108 (find-lisp-debug-message |
109 (format "Processing file %s in %s" file dir))) | 109 (format "Processing file %s in %s" file dir))) |
110 (and (file-directory-p (expand-file-name file dir)) | 110 (and (file-directory-p (expand-file-name file dir)) |
111 (not (or (string= file ".") | 111 (not (or (string= file ".") |
112 (string= file ".."))))) | 112 (string= file ".."))))) |
113 | 113 |
119 "Find files in DIRECTORY which match REGEXP." | 119 "Find files in DIRECTORY which match REGEXP." |
120 (let ((file-predicate 'find-lisp-default-file-predicate) | 120 (let ((file-predicate 'find-lisp-default-file-predicate) |
121 (directory-predicate 'find-lisp-default-directory-predicate) | 121 (directory-predicate 'find-lisp-default-directory-predicate) |
122 (find-lisp-regexp regexp) | 122 (find-lisp-regexp regexp) |
123 ) | 123 ) |
124 (find-lisp-find-files-internal | 124 (find-lisp-find-files-internal |
125 directory | 125 directory |
126 file-predicate | 126 file-predicate |
127 directory-predicate))) | 127 directory-predicate))) |
128 | 128 |
129 ;; Workhorse function | 129 ;; Workhorse function |
130 (defun find-lisp-find-files-internal (directory file-predicate | 130 (defun find-lisp-find-files-internal (directory file-predicate |
131 directory-predicate) | 131 directory-predicate) |
132 "Find files under DIRECTORY which satisfy FILE-PREDICATE. | 132 "Find files under DIRECTORY which satisfy FILE-PREDICATE. |
133 FILE-PREDICATE is a function which takes two arguments: the file and its | 133 FILE-PREDICATE is a function which takes two arguments: the file and its |
134 directory. | 134 directory. |
135 | 135 |
136 DIRECTORY-PREDICATE is used to decide whether to descend into directories. | 136 DIRECTORY-PREDICATE is used to decide whether to descend into directories. |
137 It is a function which takes two arguments, the directory and its parent." | 137 It is a function which takes two arguments, the directory and its parent." |
138 (or (string-match "/$" directory) | |
139 (setq directory (concat directory "/"))) | |
138 (let (results sub-results) | 140 (let (results sub-results) |
139 (mapcar | 141 (mapcar |
140 (function | 142 (function |
141 (lambda(file) | 143 (lambda(file) |
142 (let ((fullname (expand-file-name file directory))) | 144 (let ((fullname (expand-file-name file directory))) |
146 (and (file-directory-p fullname) | 148 (and (file-directory-p fullname) |
147 (funcall directory-predicate file directory) | 149 (funcall directory-predicate file directory) |
148 (progn | 150 (progn |
149 (setq sub-results | 151 (setq sub-results |
150 (find-lisp-find-files-internal | 152 (find-lisp-find-files-internal |
151 fullname | 153 fullname |
152 file-predicate | 154 file-predicate |
153 directory-predicate)) | 155 directory-predicate)) |
154 (if results | 156 (if results |
155 (nconc results sub-results) | 157 (nconc results sub-results) |
156 (setq results sub-results)))) | 158 (setq results sub-results)))) |
157 ;; For all files and directories, call the file predicate | 159 ;; For all files and directories, call the file predicate |
158 (and (funcall file-predicate file directory) | 160 (and (funcall file-predicate file directory) |
159 (if results | 161 (if results |
160 (nconc results (list fullname)) | 162 (nconc results (list fullname)) |
161 (setq results (list fullname)))) | 163 (setq results (list fullname)))) |
162 ))))) | 164 ))))) |
163 (directory-files directory nil nil t)) | 165 (directory-files directory nil nil t)) |
164 results)) | 166 results)) |
186 'find-lisp-file-predicate-is-directory | 188 'find-lisp-file-predicate-is-directory |
187 'find-lisp-default-directory-predicate | 189 'find-lisp-default-directory-predicate |
188 "*Find Lisp Dired Subdirectories*")) | 190 "*Find Lisp Dired Subdirectories*")) |
189 | 191 |
190 ;; Most of this is lifted from find-dired.el | 192 ;; Most of this is lifted from find-dired.el |
191 ;; | 193 ;; |
192 (defun find-lisp-find-dired-internal (dir file-predicate | 194 (defun find-lisp-find-dired-internal (dir file-predicate |
193 directory-predicate buffer-name) | 195 directory-predicate buffer-name) |
194 "Run find (Lisp version) and go into Dired mode on a buffer of the output." | 196 "Run find (Lisp version) and go into Dired mode on a buffer of the output." |
195 (let ((dired-buffers dired-buffers) | 197 (let ((dired-buffers dired-buffers) |
196 buf | 198 buf |
197 (regexp find-lisp-regexp)) | 199 (regexp find-lisp-regexp)) |
200 (setq dir (abbreviate-file-name | 202 (setq dir (abbreviate-file-name |
201 (file-name-as-directory (expand-file-name dir)))) | 203 (file-name-as-directory (expand-file-name dir)))) |
202 ;; Check that it's really a directory. | 204 ;; Check that it's really a directory. |
203 (or (file-directory-p dir) | 205 (or (file-directory-p dir) |
204 (error "find-dired needs a directory: %s" dir)) | 206 (error "find-dired needs a directory: %s" dir)) |
205 (or | 207 (or |
206 (and (buffer-name) | 208 (and (buffer-name) |
207 (string= buffer-name (buffer-name))) | 209 (string= buffer-name (buffer-name))) |
208 (switch-to-buffer (setq buf (get-buffer-create buffer-name)))) | 210 (switch-to-buffer (setq buf (get-buffer-create buffer-name)))) |
209 (widen) | 211 (widen) |
210 (kill-all-local-variables) | 212 (kill-all-local-variables) |
224 | 226 |
225 (make-local-variable 'revert-buffer-function) | 227 (make-local-variable 'revert-buffer-function) |
226 (setq revert-buffer-function | 228 (setq revert-buffer-function |
227 (function | 229 (function |
228 (lambda(ignore1 ignore2) | 230 (lambda(ignore1 ignore2) |
229 (find-lisp-insert-directory | 231 (find-lisp-insert-directory |
230 default-directory | 232 default-directory |
231 find-lisp-file-predicate | 233 find-lisp-file-predicate |
232 find-lisp-directory-predicate | 234 find-lisp-directory-predicate |
233 'ignore) | 235 'ignore) |
234 ) | 236 ) |
238 (if (fboundp 'dired-simple-subdir-alist) | 240 (if (fboundp 'dired-simple-subdir-alist) |
239 ;; will work even with nested dired format (dired-nstd.el,v 1.15 | 241 ;; will work even with nested dired format (dired-nstd.el,v 1.15 |
240 ;; and later) | 242 ;; and later) |
241 (dired-simple-subdir-alist) | 243 (dired-simple-subdir-alist) |
242 ;; else we have an ancient tree dired (or classic dired, where | 244 ;; else we have an ancient tree dired (or classic dired, where |
243 ;; this does no harm) | 245 ;; this does no harm) |
244 (set (make-local-variable 'dired-subdir-alist) | 246 (set (make-local-variable 'dired-subdir-alist) |
245 (list (cons default-directory (point-min-marker))))) | 247 (list (cons default-directory (point-min-marker))))) |
246 (find-lisp-insert-directory | 248 (find-lisp-insert-directory |
247 dir file-predicate directory-predicate 'ignore) | 249 dir file-predicate directory-predicate 'ignore) |
248 (goto-char (point-min)) | 250 (goto-char (point-min)) |
249 (dired-goto-next-file))) | 251 (dired-goto-next-file))) |
250 | 252 |
251 (defun find-lisp-insert-directory (dir | 253 (defun find-lisp-insert-directory (dir |
252 file-predicate | 254 file-predicate |
253 directory-predicate | 255 directory-predicate |
254 sort-function) | 256 sort-function) |
255 "Insert the results of `find-lisp-find-files' in the current buffer." | 257 "Insert the results of `find-lisp-find-files' in the current buffer." |
256 (let ((buffer-read-only nil) | 258 (let ((buffer-read-only nil) |
257 (files (find-lisp-find-files-internal | 259 (files (find-lisp-find-files-internal |
258 dir | 260 dir |
259 file-predicate | 261 file-predicate |
260 directory-predicate)) | 262 directory-predicate)) |
261 (len (length dir))) | 263 (len (length dir))) |
262 (erase-buffer) | 264 (erase-buffer) |
263 ;; Subdir headlerline must come first because the first marker in | 265 ;; Subdir headlerline must come first because the first marker in |
264 ;; subdir-alist points there. | 266 ;; subdir-alist points there. |
265 (insert find-lisp-line-indent dir ":\n") | 267 (insert find-lisp-line-indent dir ":\n") |
266 ;; Make second line a ``find'' line in analogy to the ``total'' or | 268 ;; Make second line a ``find'' line in analogy to the ``total'' or |
267 ;; ``wildcard'' line. | 269 ;; ``wildcard'' line. |
268 ;; | 270 ;; |
269 ;; No analog for find-lisp? | 271 ;; No analog for find-lisp? |
270 (insert find-lisp-line-indent "\n") | 272 (insert find-lisp-line-indent "\n") |
271 ;; Run the find function | 273 ;; Run the find function |
272 (mapcar | 274 (mapcar |
273 (function | 275 (function |
274 (lambda(file) | 276 (lambda(file) |
275 (find-lisp-find-dired-insert-file | 277 (find-lisp-find-dired-insert-file |
276 (substring file len) | 278 (substring file len) |
277 (current-buffer)))) | 279 (current-buffer)))) |
278 (sort files 'string-lessp)) | 280 (sort files 'string-lessp)) |
279 ;; FIXME: Sort function is ignored for now | 281 ;; FIXME: Sort function is ignored for now |
280 ;; (funcall sort-function files)) | 282 ;; (funcall sort-function files)) |
287 (setq find-lisp-regexp regexp) | 289 (setq find-lisp-regexp regexp) |
288 (revert-buffer)) | 290 (revert-buffer)) |
289 | 291 |
290 (defun find-lisp-find-dired-insert-file (file buffer) | 292 (defun find-lisp-find-dired-insert-file (file buffer) |
291 (set-buffer buffer) | 293 (set-buffer buffer) |
292 (insert find-lisp-line-indent | 294 (insert find-lisp-line-indent |
293 (find-lisp-format file (file-attributes file) (list "") | 295 (find-lisp-format file (file-attributes file) (list "") |
294 (current-time)))) | 296 (current-time)))) |
295 | 297 |
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
297 ;; Lifted from ls-lisp. We don't want to require it, because that | 299 ;; Lifted from ls-lisp. We don't want to require it, because that |