unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "'Eric Abrahamsen'" <eric@ericabrahamsen.net>, <help-gnu-emacs@gnu.org>
Subject: RE: emacs lisp - unable to write a function maker
Date: Fri, 16 Sep 2011 07:06:13 -0700	[thread overview]
Message-ID: <47D4EF93ED6C47B8AD53845AECED59D4@us.oracle.com> (raw)
In-Reply-To: <87iposlshu.fsf@ericabrahamsen.net>

> if you're using eval on something you don't want eval'd
> until the function is run, you should backquote it.
> 
> (defun functionmaker (name form)
>   (eval `(defun ,name () ,form)))
> 
> I used to associate backquoting exclusively with macros, but I think
> that was wrong thinking.

Right.

But if you're evaluating the entire constructed form once at the top level like
that, then you likely do want to use a macro. That's just what a Lisp macro is:
a function that returns code that then gets evaluated.

(defmacro functionmaker (name form)
 `(defun ,name () ,form))

Or, for the original question:

(defmacro makeAbrevFun (name val)
 `(defun ,name () (insert ,val)))

(makeAbrevFun aa "aaaaaaaaaaaaaa")

Well, not quite - the OP quoted the function symbol, indicating that s?he would
pass something that needs to be eval'd. If that's rally part of the requirement,
then:

(defmacro makeAbrevFun (name val)
 `(defun ,(eval name) () (insert ,val)))

(makeAbrevFun 'aa "aaaaaaaaaaaaaa")

In both cases: (symbol-function 'aa) gives:
(lambda () (insert "aaaaaaaaaaaaaa"))




  parent reply	other threads:[~2011-09-16 14:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-16  8:46 emacs lisp - unable to write a function maker C K Kashyap
2011-09-16  8:58 ` Thien-Thi Nguyen
2011-09-16  9:26   ` C K Kashyap
2011-09-16  9:40     ` Thien-Thi Nguyen
2011-09-16  9:57     ` Eric Abrahamsen
2011-09-16 10:17       ` C K Kashyap
2011-09-16 11:19         ` Eric Abrahamsen
2011-09-16 14:06       ` Drew Adams [this message]
2011-09-23 15:31         ` Tim Landscheidt
2011-09-23 16:43           ` Drew Adams

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=47D4EF93ED6C47B8AD53845AECED59D4@us.oracle.com \
    --to=drew.adams@oracle.com \
    --cc=eric@ericabrahamsen.net \
    --cc=help-gnu-emacs@gnu.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).