all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Kevin Rodgers <ihs_4664@yahoo.com>
Subject: Re: defining many similar functions using macros
Date: Mon, 04 Oct 2004 09:55:33 -0600	[thread overview]
Message-ID: <41617275.5000108@yahoo.com> (raw)
In-Reply-To: mailman.984.1096774354.2017.help-gnu-emacs@gnu.org

Joe Corneli wrote:
 > I would like to define them all in one go:
 >
 > (dolist (elt '("alpha"
 >                "beta"
 >                ...))
 >   (define-tex-symbol elt))
 >
 > 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:
 >
 > (defmacro define-tex-symbol (name)
 >   `(defun ,(intern (concat "tex-" name)) ()
 >      (interactive)
 >      (insert "\\" ,name)))
 >
 > E.g. (define-tex-symbol "alpha") ;=> tex-alpha
 >
 > But
 >
 > (dolist (elt '("alpha"
 >                "beta"))
 >   (define-tex-symbol elt))

[M-x rhetorical] Why is that better than:

(define-text-symbol "alpha")
(define-text-symbol "beta")

 > triggers an error:
 >
 > 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.

The macro argument NAME is bound to the unevaluated form the macro is
called with, e.g. "alpha".  If you want to be able to call the macro
with some other kind of form that must be evaluated to yield a string,
you have to write your macro to take that into account.

In Lisp, a macro transforms one expression into another, which is then
evaluated.  The backquote syntax makes it easy to specify the output
expression, and the comma syntax allows you to substitute forms
evaluated at compile-time for literal forms.  But then the resulting
expression still has to be evaluated.

A problem arises when you want to generate a call to another macro (e.g.
defun) that requires an unevaluated form (i.e. the function name symbol)
that you compute dynamically.  That's why you have to put the comma
before the  (intern ...) form: because this is valid

	(defun tex-alpha ...)

but this isn't

	(defun (intern (concat "tex-" "alpha")) ...)

As someone else pointed out, the solution is to use the fset function
instead of the macro:

(defmacro define-tex-symbol (name)
   `(fset (intern (concat "tex-" ,name))
          (lambda ()
            (interactive)
            (insert "\\" ,name))))

-- 
Kevin Rodgers

  parent reply	other threads:[~2004-10-04 15:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.984.1096774354.2017.help-gnu-emacs@gnu.org>
2004-10-03  5:31 ` defining many similar functions using macros Klaus Berndl
2004-10-03  8:18 ` David Kastrup
2004-10-04 15:55 ` Kevin Rodgers [this message]
2004-10-04 16:30   ` David Kastrup
2004-10-03  3:25 Joe Corneli
2004-10-03  6:49 ` Daniel Pittman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=41617275.5000108@yahoo.com \
    --to=ihs_4664@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.