help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Programmatically creating functions


From: Andreas Politz
Subject: Re: Programmatically creating functions
Date: Tue, 21 Oct 2008 20:57:56 +0200
User-agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724)

Joost Diepenmaat wrote:
Ian Eure <ian@digg.com> writes:

So, I have a list of symbols:

'(foo bar baz)

I want to iterate over the list and create a function from each which
does something like this:

(defun call-foo ()
  (interactive)
  (invoke-stuff 'foo)

How can I accomplish this? I can't figure out how to create the
function. I've tried a number of approaches, but have not met with
success.

 - eval'ing the defun. Returns a function symbol, but I can't call
it. Maybe it's only created within the scope of the (eval) and not
callable from outside?

 - Creating a symbol and using fset to assign a lambda to it's
function cell. It sort of works, but I'm unclear on how to pass a
variable function name to defun, nor am I clear on how I can make sure
it calls invoke-stuff with the right symbol.

I'm not /quite/ sure where you've got problems, but in this case elisp's
lack of closures hurts. IMHO the simplest way to get what you want is to
use a macro:

(defmacro make-caller-macro (symbol) `(defun ,(intern (concat "call-" (symbol-name symbol))) () (,symbol)))

But that won't evaluate the argument, so you'd more or less have to use
eval as well:

(dolist (s '(foo bar)) (eval `(make-caller-macro ,s)))



Does this work ? (in general)

(defun make-fun (name)
  (fset (intern (format "call-%s" name))
        `(lambda nil
           ,(format "call-%s is a very good function." name)
           (interactive)
           (message "I was once %s" (quote ,name)))))

(mapcar 'make-fun '(foo bar baz))
(describe-function 'call-baz)
(call-bar)

-ap


reply via email to

[Prev in Thread] Current Thread [Next in Thread]