From: Mike Mattie <codermattie@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: traverse a directory
Date: Tue, 1 Jan 2008 21:43:15 -0800 [thread overview]
Message-ID: <20080101214315.442b1509@reforged> (raw)
In-Reply-To: <5f20a79c-c1fc-4853-b55f-657f6d66f0e9@l6g2000prm.googlegroups.com>
[-- Attachment #1.1: Type: text/plain, Size: 3141 bytes --]
On Tue, 1 Jan 2008 08:51:33 -0800 (PST)
Xah Lee <xah@xahlee.org> wrote:
> if i want to recurse into a directory, do i have to write my own?
>
> e.g. i want to apply a file processing function to all files in a dir
> and subdirs. Somethnig like
>
> (apply 'process-my-file "/Users/xah/emacs/" "\.html$")
>
> I noticed that directory-files only list the dir file. Someone told me
> there's a directory-files-recur but that doesn't seems to be in emacs.
>
> thx in advance.
here is something kinda related that I recently hacked up. I needed to be able to
filter the results of a directory list. The first hack started off as a plain
defun but I encountered variations so I made a macro. it may duplicate existing
emacs code.
the elisp-in-path defun shows how the form is used. It's uncommented, and
relies upon map-filter-nil which needs to be non-recursive for general use.
It is pretty easy to extend for filtering on either the path or the mode string.
Writing a general tree recursion is trivial, it shouldn't even be file specific.
this defun may help tailor such a recursion towards a fs tree.
(defun filter-ls-attributes ( filter-form )
"implement the various attribute filters for the filter-ls form"
(lexical-let
((attr-name (symbol-name (car filter-form)))
(attr-match (cadr filter-form)))
(cond
((string-equal "type" attr-name) (list 'char-equal attr-match '(aref (cdr path-pair) 0)))
((string-equal "path" attr-name) (list 'string-match-p attr-match '(car path-pair)))
;; error path
)))
(defmacro filter-ls (path path-type &rest filters)
"a form for flexibly filtering the result of listing a directory with attributes"
`(apply 'map-filter-nil
(lambda ( path-pair )
(if ,(cons 'and (mapcar 'filter-ls-attributes filters))
(car path-pair)))
;; reduce the attributes to a pair of the path, and the mode string
(mapcar (lambda ( attr-list )
(cons (car attr-list) (nth 9 attr-list)))
;; get the list of files.
(directory-files-and-attributes ,path ,path-type))
))
;; path-type is t absolute, nil relative.
(defun elisp-in-path ( path path-type )
"return a list of elisp files in the path"
(filter-ls path path-type
(type ?-)
(path "\\.el$")))
P.S
here is map-filter-nil. use at your own risk.
(defun map-filter-nil ( func &rest seq )
"map-filter-nil. apply the function to the arguements ala mapcar.
Filter any nil elements of the sequence before the function is
applied, and after the function is applied."
(if (car seq)
(let
((result (funcall func (car seq))))
(if result
(cons result (apply 'map-filter-nil func (cdr seq)))
(apply 'map-filter-nil func (cdr seq))
))
(if (cdr seq)
(apply 'map-filter-nil func (cdr seq)))
))
> Xah
> xah@xahlee.org
> ∑ http://xahlee.org/
>
> _______________________________________________
> help-gnu-emacs mailing list
> help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
[-- Attachment #2: Type: text/plain, Size: 152 bytes --]
_______________________________________________
help-gnu-emacs mailing list
help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs
next prev parent reply other threads:[~2008-01-02 5:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-01 16:51 traverse a directory Xah Lee
2008-01-01 20:22 ` Eli Zaretskii
2008-01-02 5:43 ` Mike Mattie [this message]
[not found] ` <mailman.5612.1199252679.18990.help-gnu-emacs@gnu.org>
2008-01-02 18:03 ` Joel J. Adamson
2008-01-02 19:04 ` Mike Mattie
[not found] ` <mailman.5637.1199300789.18990.help-gnu-emacs@gnu.org>
2008-01-02 19:25 ` Joel J. Adamson
2008-01-03 5:51 ` Xah Lee
2008-01-03 11:16 ` Alexey Pustyntsev
2008-01-03 16:25 ` Joel J. Adamson
[not found] ` <bf802645-6c07-46d6-b968-1067c1ff4c7f@d21g2000prf.googlegro ups.com>
2008-01-05 17:47 ` OT: " Johan Lindström
2008-01-05 0:46 ` Mike Mattie
[not found] ` <mailman.5595.1199218943.18990.help-gnu-emacs@gnu.org>
2008-01-03 4:30 ` Xah Lee
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080101214315.442b1509@reforged \
--to=codermattie@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).