unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* elisp: returning a local variable
@ 2013-03-10 10:39 Glen Stark
  2013-03-12 16:38 ` W. Greenhouse
  2013-03-12 22:27 ` Stephen Berman
  0 siblings, 2 replies; 4+ messages in thread
From: Glen Stark @ 2013-03-10 10:39 UTC (permalink / raw)
  To: help-gnu-emacs

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?  
	2)  Is there an existing method that does this?

Thanks for your help.

Glen





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: elisp: returning a local variable
  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
  1 sibling, 0 replies; 4+ messages in thread
From: W. Greenhouse @ 2013-03-12 16:38 UTC (permalink / raw)
  To: help-gnu-emacs-mXXj517/zsQ

Glen,

Glen Stark <mail-UVvFd8jbFYRbRRN4PJnoQQ@public.gmane.org> writes:

> 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?  
> 	2)  Is there an existing method that does this?
>
> Thanks for your help.
>
> Glen

To return the value of a let-bound variable as the return value of a
function, simply make that variable, without quotations or parens, the
last element of the last expression in the function.  This works because
a variable evaluates to its own value.  For example:

(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))
      )
    retval)
  ) 	


-- 
Regards,
WGG




^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: elisp: returning a local variable
@ 2013-03-12 20:41 Buchs, Kevin
  0 siblings, 0 replies; 4+ messages in thread
From: Buchs, Kevin @ 2013-03-12 20:41 UTC (permalink / raw)
  To: help-gnu-emacs

I'm not accomplished in elisp, but it seems to me that you have
recognized the inherent attribute of a local variable. The return value
of your defun is the return value of the last form evaluated - which is
let. That returns the last form evaluated in it, which is dolist, which
explicitly returns nil (C-h f dolist). So, you have defined a function
that returns nil. I would suggest you might be able to follow (dolist
...) with (retval) to return your list. I assume you are wanting to
eliminate links and other special files, or otherwise you would just use
file-expand-wildcards alone.

I note how you declared the function (interactive), which causes me to
ask exactly what you mean by returning the list. Hopefully you are
calling get-all-subdirectories from another lisp function.

Kevin Buchs | Senior Engineer | SPPDG | 507-538-5459 |
buchs.kevin@mayo.edu
Mayo Clinic | 200 First Street SW | Rochester, MN 55905 |
http://www.mayo.edu

(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).



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: elisp: returning a local variable
  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
  1 sibling, 0 replies; 4+ messages in thread
From: Stephen Berman @ 2013-03-12 22:27 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-03-12 22:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
  -- strict thread matches above, loose matches on Subject: below --
2013-03-12 20:41 Buchs, Kevin

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).