Initial Commit
[packages] / xemacs-packages / edit-utils / find-lisp.el
1 ;;; find-lisp.el --- emulation of find in Emacs Lisp
2
3 ;; Author: Peter Breton
4 ;; Created: Fri Mar 26 1999
5 ;; Keywords: unix
6
7 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
8
9 ;; This file is part of GNU Emacs.
10
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.
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
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Synched up with: Emacs 24.2
25
26 ;;; Commentary:
27 ;;
28 ;; This is a very generalized form of find; it basically implements a
29 ;; recursive directory descent. The conditions which bound the search
30 ;; are expressed as predicates, and I have not addressed the question
31 ;; of how to wrap up the common chores that find does in a simpler
32 ;; format than writing code for all the various predicates.
33 ;;
34 ;; Some random thoughts are to express simple queries directly with
35 ;; user-level functions, and perhaps use some kind of forms interface
36 ;; for medium-level queries. Really complicated queries can be
37 ;; expressed in Lisp.
38 ;;
39
40 ;;; Todo
41 ;;
42 ;; It would be nice if we could sort the results without running the find
43 ;; again. Maybe that could work by storing the original file attributes?
44
45 ;;; Code:
46
47 (require 'dired)
48
49 (defvar dired-buffers)
50 (defvar dired-subdir-alist)
51
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.
88 PARENT is the parent directory of DIR."
89   (and find-lisp-debug
90        (find-lisp-debug-message
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 (expand-file-name dir parent)))))
98
99 (defun find-lisp-default-file-predicate  (file dir)
100   "True if FILE matches `find-lisp-regexp'.
101 DIR is the directory containing FILE."
102   (and find-lisp-debug
103        (find-lisp-debug-message
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.
110 Argument DIR is the directory containing FILE."
111   (and find-lisp-debug
112        (find-lisp-debug-message
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)
126         (find-lisp-regexp regexp))
127     (find-lisp-find-files-internal
128      directory
129      file-predicate
130      directory-predicate)))
131
132 ;; Workhorse function
133 (defun find-lisp-find-files-internal (directory file-predicate
134                                                 directory-predicate)
135   "Find files under DIRECTORY which satisfy FILE-PREDICATE.
136 FILE-PREDICATE is a function which takes two arguments: the file and its
137 directory.
138
139 DIRECTORY-PREDICATE is used to decide whether to descend into directories.
140 It is a function which takes two arguments, the directory and its parent."
141   (setq directory (file-name-as-directory directory))
142   (let (results sub-results)
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)
149                (progn
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)))))))
163     results))
164
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;; Find-dired all in Lisp
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168
169 ;;;###autoload
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
181 ;;;###autoload
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
192 ;;
193 (defun find-lisp-find-dired-internal (dir file-predicate
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         (regexp find-lisp-regexp))
198     ;; Expand DIR ("" means default-directory), and make sure it has a
199     ;; trailing slash.
200     (setq dir (file-name-as-directory (expand-file-name dir)))
201     ;; Check that it's really a directory.
202     (or (file-directory-p dir)
203         (error "find-dired needs a directory: %s" dir))
204     (or
205      (and (buffer-name)
206           (string= buffer-name (buffer-name)))
207         (switch-to-buffer (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 (let ((map (make-sparse-keymap)))
216                      (set-keymap-parent map (current-local-map))
217                      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)
230              (find-lisp-insert-directory
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
244       ;; this does no harm)
245       (set (make-local-variable 'dired-subdir-alist)
246            (list (cons default-directory (point-min-marker)))))
247     (find-lisp-insert-directory
248      dir file-predicate directory-predicate 'ignore)
249     (goto-char (point-min))
250     
251     ;; XEmacs
252     (dired-insert-set-properties (point-min) (point-max))
253
254     (dired-goto-next-file)))
255
256 (defun find-lisp-insert-directory (dir
257                                    file-predicate
258                                    directory-predicate
259                                    _sort-function)
260   "Insert the results of `find-lisp-find-files' in the current buffer."
261   (let ((buffer-read-only nil)
262         (files (find-lisp-find-files-internal
263                 dir
264                 file-predicate
265                 directory-predicate))
266         (len (length dir)))
267     (erase-buffer)
268     ;; Subdir headlerline must come first because the first marker in
269     ;; subdir-alist points there.
270     (insert find-lisp-line-indent dir ":\n")
271     ;; Make second line a ``find'' line in analogy to the ``total'' or
272     ;; ``wildcard'' line.
273     ;;
274     ;; No analog for find-lisp?
275     (insert find-lisp-line-indent "\n")
276     ;; Run the find function
277     (mapc
278      (function
279       (lambda (file)
280         (find-lisp-find-dired-insert-file
281          (substring file len)
282          (current-buffer))))
283      (sort files 'string-lessp))
284     ;; FIXME: Sort function is ignored for now
285     ;; (funcall sort-function files))
286     (goto-char (point-min))
287     (dired-goto-next-file)))
288
289 ;;;###autoload
290 (defun find-lisp-find-dired-filter (regexp)
291   "Change the filter on a find-lisp-find-dired buffer to REGEXP."
292   (interactive "sSet filter to regexp: ")
293   (setq find-lisp-regexp regexp)
294   (revert-buffer))
295
296 (defun find-lisp-find-dired-insert-file (file buffer)
297   (set-buffer buffer)
298   (insert find-lisp-line-indent
299           (find-lisp-format file (file-attributes file 'string) (list "")
300                           (current-time))))
301
302 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 ;; Lifted from ls-lisp. We don't want to require it, because that
304 ;; would alter the insert-directory function.
305 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
306
307 (defun find-lisp-format (file-name file-attr switches now)
308   (let ((file-type (nth 0 file-attr)))
309     (concat (if (memq ?i switches)      ; inode number
310                 (format "%6d " (nth 10 file-attr)))
311             ;; nil is treated like "" in concat
312             (if (memq ?s switches)      ; size in K
313                 (format "%4d " (1+ (/ (nth 7 file-attr) 1024))))
314             (nth 8 file-attr)           ; permission bits
315             (format " %3d %-8s %-8s %8d "
316                     (nth 1 file-attr)   ; no. of links
317                     (if (numberp (nth 2 file-attr))
318                         (int-to-string (nth 2 file-attr))
319                       (nth 2 file-attr)) ; uid
320                     (if (eq system-type 'ms-dos)
321                         "root"          ; everything is root on MSDOS.
322                       (if (numberp (nth 3 file-attr))
323                           (int-to-string (nth 3 file-attr))
324                         (nth 3 file-attr))) ; gid
325                     (nth 7 file-attr)   ; size in bytes
326                     )
327             (find-lisp-format-time file-attr switches now)
328             " "
329             file-name
330             (if (stringp file-type)     ; is a symbolic link
331                 (concat " -> " file-type)
332               "")
333             "\n")))
334
335 (defun find-lisp-time-index (switches)
336   ;; Return index into file-attributes according to ls SWITCHES.
337   (cond
338    ((memq ?c switches) 6)               ; last mode change
339    ((memq ?u switches) 4)               ; last access
340    ;; default is last modtime
341    (t 5)))
342
343 (defun find-lisp-format-time (file-attr switches now)
344   ;; Format time string for file with attributes FILE-ATTR according
345   ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
346   ;; Use the same method as `ls' to decide whether to show time-of-day or year,
347   ;; depending on distance between file date and NOW.
348   (let* ((time (nth (find-lisp-time-index switches) file-attr))
349          (diff16 (- (car time) (car now)))
350          (diff (+ (ash diff16 16) (- (car (cdr time)) (car (cdr now)))))
351          (past-cutoff (- (* 6 30 24 60 60)))    ; 6 30-day months
352          (future-cutoff (* 60 60)))             ; 1 hour
353     (format-time-string
354      (if (and
355           (<= past-cutoff diff) (<= diff future-cutoff)
356           ;; Sanity check in case `diff' computation overflowed.
357           (<= (1- (ash past-cutoff -16)) diff16)
358           (<= diff16 (1+ (ash future-cutoff -16))))
359          "%b %e %H:%M"
360        "%b %e  %Y")
361      time)))
362
363 (provide 'find-lisp)
364
365 ;;; find-lisp.el ends here