From: ········@hotmail.com
Subject: Passing a function name as an argument to a function that calls 	defadvice?
Date: 
Message-ID: <fa6a4db9-6563-414b-bb96-46bf8b623675@b36g2000hsa.googlegroups.com>
How do I pass a function as an argument to another function that then
uses the argument as a function name to defadvice?

Here's what I'd like to do:

(defun work-around (arg-function)
  (defadvice arg-function (around work-around-fn activate)
    ... before code ...
    ad-do-it
    ... after code ...
  )
)

(work-around 'some-function)

However, I can't seem to get some-function into the defadvice call as
the function argument. 'arg-function doesn't work either.

With the current version, the advice binds to arg-function, not some-
function. When I change it to 'arg-function, I get the error: Invalid
function name: (quote arg-function)


Thanks,
Brian

From: Rainer Joswig
Subject: Re: Passing a function name as an argument to a function that calls defadvice?
Date: 
Message-ID: <joswig-4900E5.20105421112007@news-europe.giganews.com>
In article 
<····································@b36g2000hsa.googlegroups.com>,
 ········@hotmail.com wrote:

> How do I pass a function as an argument to another function that then
> uses the argument as a function name to defadvice?
> 
> Here's what I'd like to do:
> 
> (defun work-around (arg-function)
>   (defadvice arg-function (around work-around-fn activate)
>     ... before code ...
>     ad-do-it
>     ... after code ...
>   )
> )
> 
> (work-around 'some-function)
> 
> However, I can't seem to get some-function into the defadvice call as
> the function argument. 'arg-function doesn't work either.
> 
> With the current version, the advice binds to arg-function, not some-
> function. When I change it to 'arg-function, I get the error: Invalid
> function name: (quote arg-function)
> 
> 
> Thanks,
> Brian

What Lisp? What DEFADVICE?

DEFADVICE is usually a macro. You can't use macros
like you do in functions.  DEFADVICE is thought
to be used at the toplevel in code.
If you use it in a function it will get expanded
by the compiler (assuming that you use one).
At runtime, the expansion is already done.

Typical ways to get around it is to
generate the code with the macro (DEFADVICE in this case)
and evaluate it.

Another one would be to generate the necessary forms,
write them to a file, compile it and load it.

Are you sure you want to use DEFADVICE?

-- 
http://lispm.dyndns.org/
From: Tim X
Subject: Re: Passing a function name as an argument to a function that calls  defadvice?
Date: 
Message-ID: <87bq9lzjbk.fsf@lion.rapttech.com.au>
········@hotmail.com writes:

> How do I pass a function as an argument to another function that then
> uses the argument as a function name to defadvice?
>
> Here's what I'd like to do:
>
> (defun work-around (arg-function)
>   (defadvice arg-function (around work-around-fn activate)
>     ... before code ...
>     ad-do-it
>     ... after code ...
>   )
> )
>
> (work-around 'some-function)
>
> However, I can't seem to get some-function into the defadvice call as
> the function argument. 'arg-function doesn't work either.
>
> With the current version, the advice binds to arg-function, not some-
> function. When I change it to 'arg-function, I get the error: Invalid
> function name: (quote arg-function)
>
>

I'm assuming you are talking about emacs lisp.

Note that defadvice is a macro, which means it is evaluated at compile time
rather than runtime.

For a good examples of the use of defadvice, check out T.V. Raman's
emacspeak package. It uses defadvice to add spoken feedback to emacs
commands. (see http://emacspeak.googlecode/svn/trunk). 

Something like 

(loop for f in '(list-of-function-names)
  do (eval `(defadvice ,f (around my-advice pre act)
              ....
             ad-do-it
             ...
             ad-return-value)))

or similar may work. (note the ` and ,). Also, the loop function is part of
the cl package, so you may need a (require 'cl). It is possiblly bad style
to use eval, but I can't think of how to do this without using it (which
just means I don't know enough, not that it can't be done).

The defadvice macro is very useful, but be aware that it can make debugging
very difficult and you can get some weird/unexpected results, particularly
with functions that are actually implemented in C or defined to be in-line
with defsubst. Be particularly aware of possible side effects that may
behave differently depending on where your advised functions are used
(should your advice have side effects that is)

Tim

-- 
tcross (at) rapttech dot com dot au