From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Daniel Pittman Newsgroups: gmane.emacs.help Subject: Re: defining many similar functions using macros Date: Sun, 03 Oct 2004 16:49:22 +1000 Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <87zn34gutp.fsf@enki.rimspace.net> References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1096786918 26931 80.91.229.6 (3 Oct 2004 07:01:58 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 3 Oct 2004 07:01:58 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Oct 03 09:01:52 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1CE0NU-0006Tu-00 for ; Sun, 03 Oct 2004 09:01:52 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1CE0U1-0000Vx-4R for geh-help-gnu-emacs@m.gmane.org; Sun, 03 Oct 2004 03:08:37 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1CE0Tt-0000Vh-3B for help-gnu-emacs@gnu.org; Sun, 03 Oct 2004 03:08:29 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1CE0Ts-0000VU-MY for help-gnu-emacs@gnu.org; Sun, 03 Oct 2004 03:08:28 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1CE0Ts-0000VR-IS for help-gnu-emacs@gnu.org; Sun, 03 Oct 2004 03:08:28 -0400 Original-Received: from [80.91.229.2] (helo=main.gmane.org) by monty-python.gnu.org with esmtp (Exim 4.34) id 1CE0N2-0001My-7d for help-gnu-emacs@gnu.org; Sun, 03 Oct 2004 03:01:24 -0400 Original-Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1CE0N0-0006iw-00 for ; Sun, 03 Oct 2004 09:01:22 +0200 Original-Received: from 203-217-29-45.perm.iinet.net.au ([203.217.29.45]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 03 Oct 2004 09:01:22 +0200 Original-Received: from daniel by 203-217-29-45.perm.iinet.net.au with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 03 Oct 2004 09:01:22 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-To: help-gnu-emacs@gnu.org Original-Lines: 54 Original-X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: 203-217-29-45.perm.iinet.net.au User-Agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:HPVw3E9geFHbAburfvrykC6BW5c= 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: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:21022 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:21022 On 3 Oct 2004, Joe Corneli wrote: > I have a lot of functions that are very similar: > > (defun tex-alpha () > (interactive) > (insert "\\alpha")) > > (defun tex-beta () > (interactive) > (insert "\\beta")) > > ... > > I would like to define them all in one go: [...] > This seems like a good chance to use a macro. My first experiment > along these lines fails however, and I could use some help > re-designing it. > > This macro works on single elements: [...] > triggers an error: [... when run from `dolist' ...] > Debugger entered--Lisp error: (wrong-type-argument sequencep elt) > concat("tex-" elt) > (intern (concat "tex-" name)) > (list (quote defun) (intern (concat "tex-" name)) nil (quote (interactive)) > (list (quote insert) "\\" name)) > ... > > There seem to be some subtleties associated with macro expansion > that I'm missing here. Help would be appreciated. Indeed. The solution suggested elsewhere in the thread makes the macro expansion work as expected, but why bother with that hoop - just write the code directly: (dolist (name '("alpha" "beta")) (fset (intern (concat "tex-" name)) `(lambda () (interactive) (insert "\\" ,name)))) Note the backquote is needed to use the value of name, rather than the symbol, but that cuts out the macro expansion middle-man nicely. Daniel -- We live in a hallucination of our own devising. -- Alan Kay