1

I am learning how to program in emacs lisp. I thought I would write a simple function that prints a function definition to the messages buffer. This works fine:

(let (
(fDefAsString (symbol-function 'scroll-down)) 
)
(message "%s" fDefAsString)
(switch-to-buffer-other-frame "*Messages*"))

but I wanted this to be a function that could take an argument, the argument being the name of the function whose definition I want to see. So I tried this:

(defun message-function-definition (nameOfFunction)
(let (
(fDefAsString (symbol-function 'nameOfFunction)) 
)
(message "%s" fDefAsString)
(switch-to-buffer-other-frame "*Messages*")))

and then I wrote this:

(message-function-definition 'scroll-down)

Then I ran "eval-buffer".

I keep getting this error:

Debugger entered--Lisp error: (void-function nameOfFunction) symbol-function(nameOfFunction) (let ((fDefAsString ...)) (message "%s" fDefAsString) (switch-to-buffer-other-frame "Messages")) message-function-definition(scroll-down) eval((message-function-definition (quote scroll-down))) eval-last-sexp-1(nil) eval-last-sexp(nil) call-interactively(eval-last-sexp nil nil) recursive-edit() byte-code("\306 @\307=\203!

I've tried quoting, unquoting, and I've tried to use "nameOfFunction" but I can not get this to work. What am I doing wrong?

1 Answer 1

1

Did you try unquoting? This works for me:

(defun message-function-definition (nameOfFunction)
  (let ((fDefAsString (symbol-function nameOfFunction)))
    (message "%s" fDefAsString)
    (switch-to-buffer-other-frame "*Messages*")))

Some hints:

  1. Your code is not indented correctly. If you select the region and re-indent with tab, it'll be easier to read.

  2. You should spend a couple of hours working through the "Emacs Lisp Intro", you will have a much better idea of how things work in Emacs-lisp, rather than figuring it all by asking one question at a time on StackOverflow.

  3. symbol-function is not designed to show a human readable definition of the function. In most cases you won't really see anything useful.

    Try this:

    (find-function-other-window 'scroll-down)
    
Sign up to request clarification or add additional context in comments.

5 Comments

if I put (find-function-other-window 'scroll-down) in an empty buffer and run eval-buffer I get : "Debugger entered--Lisp error: (error "Variable binding depth exceeds max-specpdl-size")"
I don't. I'm using Emacs 24.1. Start emacs with emacs -Q. scroll-down isn't a good example because it's defined in C. But I still see the source fine.
Now I get "The C source file window.c is not available"
Strangely, in a blank new buffer, I do this: (symbol-function 'scroll-down) and then C-x C-e and all I get is: #<subr scroll-down> I am not sure what has gone wrong.
Do you actually want to see the function definition or something else? You need to have the C sources installed if you do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.