Subject: bug#18821: 25.0.50; Let pp-macroexpand-expression expand only once

From: Stefan Monnier <[email-masked]>
Date: Tue, 28 Oct 2014 13:00:09 -0400

>     (defun macroexpand1 (expr)
>       (apply (cdr (symbol-function (car expr))) (cdr expr)))

Daniel Colascione already submitted a patch that provides
a macroexpand1.  And if that's not sufficient I also wrote my own.
The only reason I haven't installed it is because it would make a lot of
sense to reimplement `macroexpand' (in Elisp) on top of `macroexpand1',
but when I tried that, byte-compilation slowed down by about 5%, which
seemed excessive (especially since it reflects an even larger slowdown
of macroexpansion itself, tho I haven't measured that in more detail).

        Stefan

(defun macroexp-expand-1 (form environment)
  "Perform (at most) one step of macroexpansion."
  (cond
   ((consp form)
    (let* ((head (car form))
           (env-expander (assq head environment)))
      (if env-expander
          (if (cdr env-expander)
              (apply (cdr env-expander) (cdr form))
            form)
        (if (not (and (symbolp head) (fboundp head)))
            form
          (let ((def (autoload-do-load (symbol-function head) head 'macro)))
            (cond
             ;; Follow alias, but only for macros, otherwise we may end up
             ;; skipping an important compiler-macro (e.g. cl--block-wrapper).
             ((and (symbolp def) (macrop def)) (cons def (cdr form)))
             ((not (consp def)) form)
             (t
              (if (eq 'macro (car def))
                  (apply (cdr def) (cdr form))
                form))))))))
   (t form)))