help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: emacs lisp - unable to write a function maker


From: Drew Adams
Subject: RE: emacs lisp - unable to write a function maker
Date: Fri, 23 Sep 2011 09:43:09 -0700

> 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.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]