From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.help Subject: RE: emacs lisp - unable to write a function maker Date: Fri, 23 Sep 2011 09:43:09 -0700 Message-ID: <506B13F80BC848FAB0AF8AE20270CC7B@us.oracle.com> References: <87pqj0q2wo.fsf@ambire.localdomain><87iposlshu.fsf@ericabrahamsen.net><47D4EF93ED6C47B8AD53845AECED59D4@us.oracle.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1316796490 9253 80.91.229.12 (23 Sep 2011 16:48:10 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 23 Sep 2011 16:48:10 +0000 (UTC) To: "'Tim Landscheidt'" , Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Sep 23 18:48:05 2011 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1R78uy-0005hd-9J for geh-help-gnu-emacs@m.gmane.org; Fri, 23 Sep 2011 18:48:04 +0200 Original-Received: from localhost ([::1]:44167 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R78ux-0001Yv-Nu for geh-help-gnu-emacs@m.gmane.org; Fri, 23 Sep 2011 12:48:03 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:48487) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R78ut-0001Ye-I3 for help-gnu-emacs@gnu.org; Fri, 23 Sep 2011 12:48:00 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R78us-0002kX-Fp for help-gnu-emacs@gnu.org; Fri, 23 Sep 2011 12:47:59 -0400 Original-Received: from rcsinet15.oracle.com ([148.87.113.117]:43175) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R78us-0002k3-8j for help-gnu-emacs@gnu.org; Fri, 23 Sep 2011 12:47:58 -0400 Original-Received: from acsinet21.oracle.com (acsinet21.oracle.com [141.146.126.237]) by rcsinet15.oracle.com (Switch-3.4.4/Switch-3.4.4) with ESMTP id p8NGjtQT003209 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 23 Sep 2011 16:47:56 GMT Original-Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by acsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id p8NGhI3X024420 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 23 Sep 2011 16:43:18 GMT Original-Received: from abhmt108.oracle.com (abhmt108.oracle.com [141.146.116.60]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id p8NGhDN8023896; Fri, 23 Sep 2011 11:43:13 -0500 Original-Received: from dradamslap1 (/10.159.48.212) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 23 Sep 2011 09:43:12 -0700 X-Mailer: Microsoft Office Outlook 11 Thread-Index: Acx6BeycB20lvonATliG7sGv/BtI4AABYC0A X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6109 In-Reply-To: X-Source-IP: acsinet21.oracle.com [141.146.126.237] X-Auth-Type: Internal IP X-CT-RefId: str=0001.0A090207.4E7CB83D.0088:SCFMA922111,ss=1,re=-4.000,fgs=0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 1) X-Received-From: 148.87.113.117 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:82308 Archived-At: > how do I proceed if I do not want to > create a macro that creates a function, but create multiple > functions directly? For example, after some trial and error > my ~/.emacs ended up with (paraphrased): > > | (dolist (i '(("once" . 1) > | ("twice" . 2) > | ("thrice" . 3))) > | (fset (intern (concat "tl-say-" (downcase (car i)))) > | `(lambda (s) > | ,(concat "Say S only " (car i) ".") > | (interactive "sWhat to message: ") > | (dotimes (j ,(cdr i)) > | (message "I say it only %s: %s." ,(car i) s))))) > > I didn't manage to use defun as I got stuck in quoting and > non-quoting "magic". Are you asking how you would do that using `defun' instead of `fset', but without using a macro? Here's one answer: (dolist (i '(("once" . 1) ("twice" . 2) ("thrice" . 3))) (eval `(defun ,(intern (concat "tl-say-" (downcase (car i)))) (s) ,(concat "Say S only " (car i) ".") (interactive "sWhat to message: ") (dotimes (j ,(cdr i)) (message "I say it only %s: %s." ,(car i) s))))) (pp-eval-expression '(symbol-function 'tl-say-twice)) `defun' is a special form (which is similar to a macro). It does not evaluate its args. So if you want to construct the function name then you must construct a `defun' form that uses that constructed name, and then evaluate that form. There's nothing magic about quoting, but evaluation and quoting do take a little getting used to. What can be confusing is that special forms and macros do not, a priori, evaluate their arguments. A macro constructs code that then gets evaluated (normally). To debug macros you are working on, use `macroexpand': it expands a macro form without also evaluating the resulting code.