1

When I change something in a function, I need to move to the last parenthese of a function, and do C-x C-e.

I thought it would be nice if I can devise a keybinding to evaluate the function, while I'm in the middle of the function.

So I came up with the following:

(defun evaluate-sentence () 
  (interactive)
  (evil-set-marker ?8)
  (sp-end-of-sexp)
  (eval-last-sexp)
  (evil-goto-mark ?8))

The number 8 is just abritrary. You need to have Evil and Smartparens. It sets the mark on the current position of the pointer, then go to the outer parenthese, eval it, and go back to the marked position.

I need to pass the correct arguments for eval-last-sexp. When looking up the documentation for C-x C-e, it says the following:

C-x C-e runs the command eval-last-sexp (found in global-map), which
is an interactive compiled Lisp function in
‘a:/Dropbox/Emacs/share/emacs/25.0.50/lisp/progmodes/elisp-mode.el’.

It is bound to <menu-bar> <emacs-lisp> <eval-sexp>, C-x C-e.

(eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL)

Evaluate sexp before point; print value in the echo area.
Interactively, with prefix argument, print output into current buffer.

Normally, this function truncates long output according to the value
of the variables ‘eval-expression-print-length’ and
‘eval-expression-print-level’.  With a prefix argument of zero,
however, there is no such truncation.  Such a prefix argument
also causes integers to be printed in several additional formats
(octal, hexadecimal, and character).

If ‘eval-expression-debug-on-error’ is non-nil, which is the default,
this command arranges for all errors to enter the debugger.

I'm not sure what the argument EVAL-LAST-SEXP-ARG-INTERNAL actually says. When I do an aprpos search with C-h a, he found nothing about what the argument EVAL-LAST-SEXP-ARG-INTERNAL really means. I would love to solve this myself, instead calling for help. But I'm out of ideas...

Any other suggestion?

ReneFroger
  • 3,855
  • 22
  • 63
  • 2
    `C-M-x` is `eval-defun`. – abo-abo Aug 07 '15 at 12:46
  • Sometimes I'm in the middle of a `(setq [..])`, so I need to have a global function that handles other things than `eval-defun` too. Thanks for your suggestion anyway. – ReneFroger Aug 07 '15 at 13:16
  • `eval-defun` is not just for `defun`s: it evaluates the top-level form, which could just as easily be a `setq`. – Dan Aug 07 '15 at 13:24
  • Just tested it, you're right Dan. The naming of `eval-defun` is somewhat confusing. Aside to that, I really appreciate your help here on Emacs stackexchange. – ReneFroger Aug 07 '15 at 17:08
  • By the way, are you acquinated with the emacs.zeef.com? – ReneFroger Aug 07 '15 at 17:46
  • @ReneFroger: glad to help! Just a quick pointer: if you want SE to let a user know when you want him/her to see a comment you wrote, use the `@USERNAME` tag (as in the beginning of this one... it's not technically necessary when it's on that user's original question or answer, but it helps clarify the intent of the comment). – Dan Aug 09 '15 at 10:39
  • @Dan Thanks for the tip! I assumed they got automatically a notification. Very useful. – ReneFroger Aug 09 '15 at 11:52

1 Answers1

3

Since what you want is to get a similar effect to what would happen if you had called eval-last-sexp yourself, the easiest is to call it as (call-interactively 'eval-last-sexp).

On the other hand, if you really want to know what the argument should be, look at the interactive declaration in the code of eval-last-sexp (as well as the docstring of interactive). Then you can use the same interactive spec in your own function, i.e.

(defun evaluate-sentence (arg) 
  (interactive "P")
  (evil-set-marker ?8)
  (sp-end-of-sexp)
  (eval-last-sexp arg)
  (evil-goto-mark ?8))
YoungFrog
  • 3,496
  • 15
  • 27
  • Thanks for your explaination. What does the "P" in `interactive` exactly? – ReneFroger Aug 07 '15 at 13:22
  • From the docstring: `P -- Prefix arg in raw form. Does not do I/O.` See `(info "(elisp) Prefix Command Arguments")` for the full story about those prefix arguments. It might also be worth another question in its own right. Now that I think about it, surely it was already asked... here you are: http://emacs.stackexchange.com/questions/13886/what-is-a-raw-prefix-argument-capital-p-in-interactive – YoungFrog Aug 07 '15 at 14:15