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

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