Subject: Re: bug#3984

From: Ryan <[email-masked]>
Date: Wed, 18 Sep 2013 17:47:58 -0700

After reading and finally comprehending the definition of 
"advice--called-interactively-skip", I think I have a possible solution 
that doesn't require a top-down search, but it would require some minor 
rearchitecting of nadvice. Basically, once we know that a particular 
stack frame is a call to the innermost unadvised form of an advised 
function, it is relatively easy to walk up the stack looking for the 
outermost advice. This is what "advice--called-interactively-skip" does. 
(Although reading through it I don't see where the bug is that prevents 
it recognizing the before advice in my example.) The problem, then, is 
knowing when a function is advised, given only the unadvised form and a 
position in the stack. If we always unconditionally wrap an unadvised 
function with a function that we can easily identify, then we can easily 
check whether it has been advised. To that end, I propose the following:

;; Generate a private symbol
(defvar nadvice--indicator-symbol (make-symbol "nadvice--indicator-symbol"))
(defun wrap-function-in-indicator-lambda (func)
   `(lambda (&rest args)
      ,nadvice--indicator-symbol
      (apply ,func args)))
(defun indicator-lambda-p (func)
   (eq nadvice--indicator-symbol
     (nth 2 (wrap-function-in-indicator-lambda (indirect-function func)))))

If all advised functions are wrapped by a call to the above function 
"wrap-function-in-indicator-lambda", then when they are called, the call 
to the "indicator lambda" would always be 2 frames up from the call to 
the original unadvised function. This allows us to easily recognize an 
advised function on the stack by testing the function 2 frames up with 
"indicator-lambda-p". Once we know the function is advised, we can then 
initiate the search for the outermost advice's stack position.

In order to implement this, I think "advice-add" and "advice-remove" 
need to be modified to automatically wrap/unwrap the original function 
in/out of the indicator.
What do you think of this? Obviously my "indicator-lambda" could be 
replaced by e.g. a no-op before/after advice or something similar, which 
would have the same effect of making it easy to recognize the innermost 
call of an advised function based on the contents of specific stack 
frame positions above it.

What do you think of this?

-Ryan