unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stephen Berman <stephen.berman@gmx.net>
To: help-gnu-emacs@gnu.org
Subject: Re: elisp: returning a local variable
Date: Tue, 12 Mar 2013 23:27:56 +0100	[thread overview]
Message-ID: <87boaouuc3.fsf@rosalinde.fritz.box> (raw)
In-Reply-To: khhntp$7al$3@ger.gmane.org

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




  parent reply	other threads:[~2013-03-12 22:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-10 10:39 elisp: returning a local variable Glen Stark
2013-03-12 16:38 ` W. Greenhouse
2013-03-12 22:27 ` Stephen Berman [this message]
  -- strict thread matches above, loose matches on Subject: below --
2013-03-12 20:41 Buchs, Kevin

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=87boaouuc3.fsf@rosalinde.fritz.box \
    --to=stephen.berman@gmx.net \
    --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).