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

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

Re: Redefining functions and variables


From: Elena
Subject: Re: Redefining functions and variables
Date: Wed, 08 Dec 2010 15:21:41 -0000
User-agent: G2/1.0

On Jul 28, 8:37 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Andreas Politz <poli...@fh-trier.de> writes:
> > Elena <egarr...@gmail.com> writes:
>
> >> On Jul 28, 7:51 am, Elena <egarr...@gmail.com> wrote:
> >>> > Of course this leads to an endless recursive loop, expanding your macro
> >>> > again and again ...
>
> >>> Then I should have tested the macro with `macroexpand-all' instead of
> >>> `macroexpand'. However, I've tried using the macro, and it seemed to
> >>> work, albeit it choked on the unquoted symbol passed to `fboundp'.
>
> >> I think it seemed to work because the macro was interpreted, therefore
> >> it was expanded only once before the failure.
>
> > Don't know, but defining `defun' as a macro usually deletes the original
> > subst (which is a special form, which is kind of like a macro).
>
> > (defvar defun-subst (symbol-function 'defun))
>
> > (defmacro defun (name args &rest body)
> >   `(defun ,name ,args ,@body))
>
> > (defun foo ())
> > ;; Enters the debugger because of recursion limit reached.
>
> > ;; Restore original defun
> > (fset 'defun defun-subst)
>
> Another way:
>
> (defvar old-defun 'defun) ; the symbol!
> (unintern 'defun)
>
> (defmacro defun (name args &rest body)
>   `(progn
>       (message "defining %S" name)
>       (,old-defun ,name ,args ,@body)))

This is very close to what I was looking for. Thanks, Pascal.

My modified macro is below. Why ',name is expanded as ... (as shown by
`macroexpand-all'?

(defvar old-defun 'defun)           ; the symbol!
(unintern 'defun)

(defmacro defun (name args &rest body)
    `(progn
         ;; `load-file-name' is not null only if we are loading a
file.
         (when (and load-file-name
                    (fboundp ',name))
             (message "Warning: %s is being redefined in %s."
                      (symbol-name ',name)
                      load-file-name)
             (,old-defun ,name ,args
                         ,@body))))



reply via email to

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