0

Why does it seem impossible to read an s-expression at load-time?

Given this and only this s-expression in my init file:

(save-excursion
  (end-of-defun)
  (beginning-of-defun)
  (setq beg (point))
  (setq form (funcall load-read-function (current-buffer)))
  (setq end (point))
  (message "BEG:  %s" beg)
  (message "FORM: %s" form)
  (message "END:  %s" end))

On startup, I receive the error:

End of file during parsing: /home/user/.emacs.d/init.el

Now, also curiously, if I comment out the erroneous lines:

(save-excursion
  (end-of-defun)
  (beginning-of-defun)
  (setq beg (point))
  ;; (setq form (funcall load-read-function (current-buffer)))
  (setq end (point))
  (message "BEG:  %s" beg)
  ;; (message "FORM: %s" form)
  (message "END:  %s" end))

On startup, I get this output in the Messages buffer:

BEG:  1
END:  1
For information about GNU Emacs and the GNU system, type C-h C-a.

No matter where I place that s-expression (point) will always be 1. Why's that?

3
  • 1
    This sounds like an X-Y question to me. What are you really trying to do?
    – NickD
    Aug 27, 2021 at 12:49
  • I don't understand how I'm not making this question understandable. The end goal is to get the line information of the currently-being-read/evaluating s-expression at load-time Aug 27, 2021 at 21:06
  • It's not that the question is not understandable. It is, and @phils provided an answer. The problem is that you seem to expect something else. What's not clear is what that something else is. My hope was that you could explain why you are interested in what that snippet above does: what are you trying to do? If the end goal is what you say it is, then your other question seems more to the point and has some comments that indicate a way forward.
    – NickD
    Aug 28, 2021 at 2:25

1 Answer 1

1

On startup, I get this output in the Messages buffer:

BEG:  1
END:  1
For information about GNU Emacs and the GNU system, type C-h C-a.

No matter where I place that s-expression (point) will always be 1. Why's that?

Notwithstanding that you've called (beginning-of-defun) and then set both beg and end to that same value, you're also processing an empty buffer by default, so those values could be expected to be 1.

Are you expecting that Emacs would visit your init file source code in a buffer, and select that buffer by default, while it evaluates your init code?

(message "%S (%S)" (current-buffer) (buffer-string))

gives me: #<buffer *scratch*> ("") if I evaluate it on start-up.

If you specify a file argument when you start Emacs, then you would be processing one of those buffers, so maybe this does what you want?

emacs ~/.emacs.d/init.el

9
  • My example is this snippet: github.com/emacs-mirror/emacs/blob/master/lisp/progmodes/…. beg and end are not the same value. And it can be proven at runtime. I don't understand where I'm failing to explain what I'm trying to do. I feel like it's a simple and intuitive question Aug 27, 2021 at 21:04
  • (message \"%S (%S)\" (current-buffer) (buffer-string)) doesn't work for me (version "28.0.50"). Where are you placing that form in the init file? I'm getting the error: Symbol's value as variable is void: "%S Aug 27, 2021 at 23:09
  • Are you expecting that Emacs would visit your init file source code in a buffer, and select that buffer by default, while it evaluates your init code? I'm expecting that Emacs evaluates said form in my init file; not before my init file; not after my init file; but as my init file. I don't understand how I can simplify my question anymore than what I have Aug 27, 2021 at 23:15
  • 1
    Apologies for the syntax error -- I was originally going to propose doing that on the command line with --eval and I accidentally kept my shell escaping for the quotes when I amended my answer. Thanks for clarifying that NickD.
    – phils
    Aug 28, 2021 at 2:34
  • 1
    If I put (message "%S" (buffer-list)) in an init file, the result is (#<buffer *scratch*> #<buffer *Minibuf-0*> #<buffer *Messages*> #<buffer *code-conversion-work*> #<buffer *load*>) -- these are the only buffers which exist while the init file is being evaluated.
    – phils
    Aug 28, 2021 at 2:46

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.