3

Consider this test function (tested in emacs -q):

  (defun foo (start end)
   "for testing purposes"
   (interactive "r")
   (when (region-active-p)
     (cons start end)))

Calling M-x foo gives no results printed in the minibuffer, even when a region is active. If I evaluate M-: (call-interactively #'foo) the cons is printed in the minibuffer.

I'd be happy to know why nothing is printed with M-x foo.

Drew
  • 75,699
  • 9
  • 109
  • 225
gigiair
  • 2,124
  • 1
  • 8
  • 14

1 Answers1

3

You're confusing the echoing of the return value by M-: with the action of M-x.

M-: expressly evaluates a sexp and prints the resulting value. M-x invokes a command.

Your command does not, itself, print or echo or otherwise display its return value.

If you want your command to echo the value, then use function message:

(when (region-active-p)
  (let ((val (cons start end)))
    (message "Result: %S" val)
    val))
Drew
  • 75,699
  • 9
  • 109
  • 225
  • This does not exactly answer my question. I thought in the evaluation loop (read (eval (print))) the evaluation stream was displayed in the mini-buffer. I had never observed it to be otherwise until now. – gigiair Sep 22 '21 at 06:59
  • 2
    I think it does answer your question. What you thought is wrong *"the evaluation stream [is] displayed in the mini-buffer"*. Display in the echo area is due to explicit calls to functions such as `message`. – Drew Sep 22 '21 at 15:25