From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Politz Newsgroups: gmane.emacs.help Subject: Re: Programmatically creating functions Date: Tue, 21 Oct 2008 20:57:56 +0200 Organization: FH-Trier Message-ID: <1224615828.488025@arno.fh-trier.de> References: <87r669lrvk.fsf@zeekat.nl> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1224618182 17677 80.91.229.12 (21 Oct 2008 19:43:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 21 Oct 2008 19:43:02 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 21 21:44:04 2008 connect(): Connection refused Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KsN92-0007nu-QF for geh-help-gnu-emacs@m.gmane.org; Tue, 21 Oct 2008 21:43:57 +0200 Original-Received: from localhost ([127.0.0.1]:43987 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KsN7x-0005JN-GS for geh-help-gnu-emacs@m.gmane.org; Tue, 21 Oct 2008 15:42:49 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.freenet.de!news.tu-darmstadt.de!news.belwue.de!news.uni-kl.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 56 Original-NNTP-Posting-Host: 143-93-54-11.arno.fh-trier.de Original-X-Trace: news.uni-kl.de 1224615853 19773 143.93.54.11 (21 Oct 2008 19:04:13 GMT) Original-X-Complaints-To: usenet@news.uni-kl.de Original-NNTP-Posting-Date: Tue, 21 Oct 2008 19:04:13 +0000 (UTC) User-Agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724) In-Reply-To: <87r669lrvk.fsf@zeekat.nl> Cache-Post-Path: arno.fh-trier.de!unknown@dslb-084-059-210-085.pools.arcor-ip.net X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) Original-Xref: news.stanford.edu gnu.emacs.help:163661 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:59001 Archived-At: Joost Diepenmaat wrote: > Ian Eure 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