0

I have written a code:

;;; calculator.el --- Calculator
;;; Commentary:
;;; Code:

(let ((operand1 (read-number "Enter the first number: "))
      (operand2 (read-number "Enter the second number: ")))
  (princ (format "The sum is %d" (+ operand1 operand2))))
      

(provide 'calculator)
;;; calculator.el ends her

I have a problem with disappearing the last program output "The sum is NUMBER". Have you any idea on how to give the program user chance of seeing the final sum printed by princ?

When I type M-x load-file calculator.el there's printed "Loading /home/lester29/calculator.el (source)...done" in the echo area after giving for example 2 and 3 to read-number, not The sum is 5.

When I type eval-buffer there's nothing printed after feeding operand1 and operand2 with 2 and 3.

How can I use a file such as calculator.el as a script, as I would do with a file python calculator.py?

1

2 Answers 2

1

Where are you trying to print it?

This is likely what you want, if you want a user to see the message interactively:

(message "The sum is %d" (+ operand1 operand2))

princ prints to the value of standard-output by default: C-h f princ.

With Lisp you don't generally load a file just to evaluate code that performs some side effect (other than perhaps changing the program state for some reason, such as adding a function definition).

You can do so, but why would you want to in this case? My crystal ball whispers that you really want to define a function, perhaps a command, that reads two numbers and echoes their sum.

5
  • I want to print the message in the minibuffer Commented Aug 31, 2023 at 22:54
  • When I type M-x load-file calculator.el after executing read-number statements I have a message "Loading /home/lester29/calculator.el (source)...done" instead of printed message using message function. Commented Aug 31, 2023 at 23:03
  • What's the recommended way to execute calculator.el file as script like in Python python calculator.py? Commented Aug 31, 2023 at 23:54
  • I've modified my question. Commented Aug 31, 2023 at 23:58
  • You've changed it to a different question. Please don't do that. Instead, pose a separate question.
    – Drew
    Commented Sep 1, 2023 at 1:13
0

Based on your changed question, which is now How to use a file of Elisp code as a script?...

One answer is to invoke Emacs in batch mode, with command-line argument --script or -x, and read a Lisp file, as explained in the Emacs manual, node Initial Options. E.g.:

--script FILE

Run Emacs in batch mode and then read and execute the Lisp code in FILE.

The normal use of this option is in executable script files that run Emacs. They can start with this text on the first line

#! /usr/bin/emacs --script

-x

This option can only be used in executable script files, and should be invoked like this:

#! /usr/bin/emacs -x

This is like --script, but suppresses loading the init files (like --quick).


And in that or any other context, if you use a print function such as princ then pay attention to what its doc tells you about where it prints. C-h f princ tells you this about its optional arg PRINTCHARFUN:

Optional argument PRINTCHARFUN is the output stream, which can be one of these:

  • a buffer, in which case output is inserted into that buffer at point;
  • a marker, in which case output is inserted at marker's position;
  • a function, in which case that function is called once for each character of OBJECT's printed representation;
  • a symbol, in which case that symbol's function definition is called; or
  • t, in which case the output is displayed in the echo area.

If PRINTCHARFUN is omitted, the value of standard-output (which see) is used instead.

3
  • I recommend -x, which means -Q --script.
    – shynur
    Commented Sep 1, 2023 at 6:18
  • @shynur: Thanks. Please consider posting that as an answer (with reasons).
    – Drew
    Commented Sep 1, 2023 at 16:37
  • "Please consider posting that as an answer (with reasons)." -- I will just add this to your answer as a supplement
    – shynur
    Commented Sep 1, 2023 at 17:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.