From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Eure Newsgroups: gmane.emacs.help Subject: Re: Programmatically creating functions Date: Sat, 25 Oct 2008 11:28:44 -0700 Message-ID: <4F2314A6-3301-4113-8D13-182038727A3F@digg.com> References: <87r669lrvk.fsf@zeekat.nl> <1224615828.488025@arno.fh-trier.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 (Apple Message framework v929.2) Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1224959407 4866 80.91.229.12 (25 Oct 2008 18:30:07 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 25 Oct 2008 18:30:07 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Andreas Politz Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Oct 25 20:31:08 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 1Ktnuj-0006AL-PE for geh-help-gnu-emacs@m.gmane.org; Sat, 25 Oct 2008 20:31:08 +0200 Original-Received: from localhost ([127.0.0.1]:58293 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ktntd-0005II-9w for geh-help-gnu-emacs@m.gmane.org; Sat, 25 Oct 2008 14:29:57 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Ktnsj-0004sb-1y for help-gnu-emacs@gnu.org; Sat, 25 Oct 2008 14:29:01 -0400 Original-Received: from [199.232.76.173] (port=34925 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ktnsi-0004ru-Fe for help-gnu-emacs@gnu.org; Sat, 25 Oct 2008 14:29:00 -0400 Original-Received: from mail.digg.com ([64.191.203.36]:51019) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Ktnsh-000106-28 for help-gnu-emacs@gnu.org; Sat, 25 Oct 2008 14:28:59 -0400 Original-Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.digg.com (Postfix) with ESMTP id 249D3A851EC; Sat, 25 Oct 2008 11:28:45 -0700 (PDT) X-Virus-Scanned: amavisd-new at X-Spam-Score: -4.399 Original-Received: from mail.digg.com ([127.0.0.1]) by localhost (mail.digg.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id oTjhQfDMuUPf; Sat, 25 Oct 2008 11:28:44 -0700 (PDT) Original-Received: from [10.2.16.254] (diggstage01.digg.com [64.191.203.34]) by mail.digg.com (Postfix) with ESMTP id 86B66A851DB; Sat, 25 Oct 2008 11:28:44 -0700 (PDT) In-Reply-To: <1224615828.488025@arno.fh-trier.de> X-Mailer: Apple Mail (2.929.2) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) 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:59161 Archived-At: On Oct 21, 2008, at 11:57 AM, Andreas Politz wrote: > 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) > Yes, with a little hacking, it works: (defvar sql-connection-alist '((poola (sql-product 'mysql) (sql-server "pool-a") (sql-user "me") (sql-password "mypass") (sql-database "default") (sql-port 3306)) (poolb (sql-product 'mysql) (sql-server "pool-b") (sql-user "me") (sql-password "mypass") (sql-database "default") (sql-port 3307))) "AList of preset connections for `sql-connect-preset'.") (defun sql-connect-preset (name) "Connect to a predefined SQL connection listed in `sql-connection- alist'" (require 'sql) (let ((conn (cdr (assoc name sql-connection-alist))) (sql-name (symbol-name name))) (eval `(let ,conn (flet ((sql-get-login (&rest what))) (sql-product-interactive sql-product)))))) (defun sql-convenience () (interactive) (mapcar (lambda (conn) (let ((name (car conn))) (fset (intern (format "sql-%s" name)) `(lambda nil ,(format "Connect to %s SQL preset." name) (interactive) (sql-connect-preset ',name))))) sql-connection-alist)) So now I can define preset connections and get sql-* methods to invoke them directly. Thank you for the help. - Ian