From: "Drew Adams" <drew.adams@oracle.com>
To: "'Tim Landscheidt'" <tim@tim-landscheidt.de>, <help-gnu-emacs@gnu.org>
Subject: RE: emacs lisp - unable to write a function maker
Date: Fri, 23 Sep 2011 09:43:09 -0700 [thread overview]
Message-ID: <506B13F80BC848FAB0AF8AE20270CC7B@us.oracle.com> (raw)
In-Reply-To: <m3d3ers2ba.fsf@passepartout.tim-landscheidt.de>
> 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.
prev parent reply other threads:[~2011-09-23 16:43 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
2011-09-23 15:31 ` Tim Landscheidt
2011-09-23 16:43 ` Drew Adams [this message]
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=506B13F80BC848FAB0AF8AE20270CC7B@us.oracle.com \
--to=drew.adams@oracle.com \
--cc=help-gnu-emacs@gnu.org \
--cc=tim@tim-landscheidt.de \
/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).