find-lisp.el 12 KB

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