0

I have a simple elisp script as below, which I want to run using emacs.

(defun add-numbers (a b)
  (+ a b))

(add-numbers 2 3)

Alternatively, I can goto ielm mode and define the function once and then call from ELSIP console.

I need a way to simply execute the script. I feel like there's some keys to make it execute, most probably c-x c-e but not being able make it work.

enter image description here

I get following error on C-x C-e, it seems a weird debugger mode though.

Debugger entered--Lisp error: (void-variable a)
  eval(a nil)
  eval-last-sexp-1(nil)
  eval-last-sexp(nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)

Useful reference

EvaluatingExpressions

3 Answers 3

3

I would suggest you to try C-h k C-x C-e to rtfm.

As your cursor (the "point", in emacs terminology) is just after the symbol a, the last expression is a.

As the variable a is not bound, trying to evaluate it produces the message error: (void-variable a).

If you really want to use C-x C-e, you should click just after the parenthesis closing the defun call and type C-x C-e and then move the point after the second expression and type again C-x C-e.

If you want to "execute the script" (in emacs terminology, we would say "evaluate the buffer"), you should type M-x eval-buffer RET or Menu > Evaluate buffer.

In this case, all the expressions contained in the buffer are evaluated, but there is no visible output: in you case, the interpreter will evaluate (defun add-numbers (a b) (+ a b)) which will define the function add-numbers (with no output). Then the interpreter will evaluate the expression (add-numbers 2 3) which computes and returns 5, but this does not produce any output.

Try to put in your file

(message "result of %s is %d" '(add-numbers 2 3) (add-numbers 2 3))

and M-x eval-buffer RET again. You should get the message in the minibuffer result of (add-numbers 2 3) is 5.

If you put more than one message in your buffer, you can see all of them with the shortcut C-h e (or M-x view-echo-area-messages RET).

Sign up to request clarification or add additional context in comments.

2 Comments

Great. I got it working by C-x C-e twice once at function and once while invoking. But interested in M-x eval-buffer. It does nothing though.
When you do M-x eval-buffer RET, it does NOT do nothing! It evaluates the whole contents of the buffer. In your case, it defines the function add-numbers. You can verify this assertion using a fresh new emacs, where you try M-: (add-numbers 1 2) RET => you'll get an error (void-function add-numbers). Maybe you'll have to quit the debugger (with the letter q). Then you go to your buffer and do M-x eval-buffer RET and try again M-: (add-numbers 1 2) RET. This time, you'll get the result 3 in the minibuffer, which is the proof that eval-buffer has defined the function!
1

You want eval-defun, which is usually C-M-x. Despite its name, what it does is look for the current top-level form and evaluate that. 'Current' is defined in some way so it does what you expect.

Comments

0

You can use C-M-x with the pointer (cursor) somewhere within the function code, or you can use C-x C-e, but with the pointer at the end of the function.

That means, put your pointer after the last closing parenthesis of the function definition and then press C-x C-e and then move the pointer to the end of the function call and press C-x C-e again.

Comments

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.