Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Body of defmacro not being executed

Tags:

emacs

lisp

elisp

I've noticed a trend in my code of repeating the same (with-current-buffer .... over and over again so I decided to define a macro based off that macro definition of with-current-buffer - this is what I have so far:

(defmacro with-assembla-buffer(asm-buffer-name heading-str &rest body)
  "Create buffer with name of ASM-BUFFER-NAME, or uses it if exists,                                                                                                      
   preps it with readonly/erase/heading - executes `body' - then puts                                                                                                     
   readonly back on, goes to beginning of buffer, and switches to it."
  (with-current-buffer (get-buffer-create asm-buffer-name)
    (assembla-mode)
    (toggle-read-only -1)
    (erase-buffer)
    (insert (format "-- %s --------------------" heading-str))
    (newline)
    `(progn ,@body)
    (toggle-read-only 1)
    (goto-char (point-min))
    (switch-to-buffer (current-buffer))))

The body of this is never being executed, however when it's switched to defun instead of defmacro is does work perfectly. So aside from why is body never executed, my other question is - does this make more sense as a macro than a defun?

like image 331
Dan LaManna Avatar asked Oct 05 '22 22:10

Dan LaManna


1 Answers

Remember, a macro generates code. Your macro looks like it does not. Check out a macro expansion of an example call. The first step of debugging a macro is to check the macro expansion of some code.

....

(with-current-buffer (get-buffer-create asm-buffer-name)

Above: why is this as code in the macro and not as source code? This code will be executed when the macro is expanded, it won't appear in the generated code. You probably want to backquote it.

  (assembla-mode)
  (toggle-read-only -1)
  (erase-buffer)
  (insert (format "-- %s --------------------" heading-str))
  (newline)

   `(progn ,@body)

Above: this won't do what you want. You need to backquote ALL the code you want to generate - not just this form.

like image 55
Rainer Joswig Avatar answered Oct 10 '22 03:10

Rainer Joswig