[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: elisp: returning a local variable
From: |
Stephen Berman |
Subject: |
Re: elisp: returning a local variable |
Date: |
Tue, 12 Mar 2013 23:27:56 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) |
On Sun, 10 Mar 2013 10:39:53 +0000 (UTC) Glen Stark <mail@glenstark.net> wrote:
> Hi everyone.
>
> I'm doing some stuff in elisp, largely in an effort to become competent
> in it. I would like to obtain a list of subdirectories in the current
> working directory. I couldn't find a method that does this, so I tried
> writing my own. I got this far:
>
>
> (defun get-all-subdirectories ()
> "Returns a list of directories in the current working folder"
> (interactive)
> (let ((src-list (file-expand-wildcards "*"))
> (retval ()))
> (dolist (fname src-list)
> (when (file-directory-p fname) (push fname retval))
> )
> )
> )
>
> Which does indeed assemble a list of subdirectories into retval, but I
> can't figure out how to return it as the return value of the function
> (yes, I could setq to global variable, but I prefer not to do this).
>
> Can anyone help me out here?
> 1) how do I accomplish this?
The dolist macro returns the value of retval if you add it to the
dolist "argument" list:
(defun get-all-subdirectories ()
"Return a list of directories in the current working folder."
(interactive)
(let ((src-list (file-expand-wildcards "*"))
(retval))
(dolist (fname src-list retval)
(when (file-directory-p fname) (push fname retval)))))
> 2) Is there an existing method that does this?
I don't know of any. But note that file-expand-wildcards does not
include "." or "..", so if you want these, you should use
directory-files instead. Also, by using push the list order in retval
is the reverse of what file-expand-wildcards or directory-files returns.
To keep the same order, return the reversed list (or use append instead
of push):
(defun get-all-subdirectories ()
"Return a list of directories in the current working folder."
(interactive)
(let ((src-list (file-expand-wildcards "*"))
(retval))
(dolist (fname src-list (reverse retval))
(when (file-directory-p fname) (push fname retval)))))
Alternatively, instead of building up a new list and reversing it you
could eliminate the unwanted elements from the list by mapping them to
nil and deleting them:
(defun get-all-subdirectories ()
"Return a list of directories in the current working folder."
(interactive)
(delq nil (mapcar #'(lambda (f) (and (file-directory-p f) f))
(directory-files default-directory))))
Steve Berman