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

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

Re: Caller of a function


From: lawrence mitchell
Subject: Re: Caller of a function
Date: Mon, 14 Jul 2003 13:32:17 +0100
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50

Klaus Berndl wrote:

> Is there an easy way to find out the caller of the function?

> example:

> (defun called-fcn ()
>   (message "My caller is %s" (function-to-get-the-caller)))

> (defun caller-fcn ()
>   (called-fcn))

> Then evaluating caller-fcn should print out "caller-fcn"....
> Is this possible with elisp, d.h. is there something like my
> "function-to-get-the-caller"?

You could look at parsing the result of BACKTRACE, or
BACKTRACE-FRAME.

e.g.:

(defun called-fn ()
  (let ((caller (find-caller)))
    (message "Caller is `%s'" caller)))

(defun find-caller ()
  (second (backtrace-frame 5)))

(defun test ()
  (called-fn))

(defun test-1 ()
  (values (test) (called-fn)))
(test-1)

(test)
    => "Caller is `test'"


(test-1)
    => ("Caller is `test'" "Caller is `values'")

It's far from foolproof, but it might work.

Alternately, you could define all your functions with a macro
s.t. their name is registered and available in an internal
variable, though that runs into problems due to namespace
collisions.

-- 
lawrence mitchell <address@hidden>


reply via email to

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