I was trying to read and eval Lisp forms from a string using a loop, instead of just putting the string into a buffer and using eval-buffer to load everything in the string. That method was mentioned in another thread here.

The problem is that read-from-string signals an error "End of file reached during parsing" and aborts my loop. I was hoping it would return nil, or some indication of the number of characters left, or give me some other way to figure out when to end my loop.

    (while (< pos (length contents))
      (setq pair (read-from-string contents pos))
      (setq obj (car pair))
      (setq pos (cdr pair))
      .. inspect the read obj here
      .. maybe eval it, or toss it, or fix it..

Read-from-string returns starting position for the next read operation in the cdr of the pair returned. So I'm thinking that's so I can use it to start the next read in the right place. But I can't find any examples of how to read a string without getting the EOF error.

Probably it's just that I don't know how handle EOF errors while reading strings. Is there an ideomatic way of handling this kind of situation? Maybe with an unwind-protect or something?

  • 1
    Tobias' answer on the linked thread shows how to do the this.
    – npostavs
    Jul 15, 2016 at 13:09
  • 1
    @npostavs, thanks for pointing me back to the Tobias posting. I had tried to understand that code months ago, but failed because I couldn't find enough doc or examples on the (end-of-file) expression there. It's the car of a cons cell, without the cdr part, and is (about 3 or 4 links later) a standard error condition. So now I understand. And I think it is a solution to my question. Thanks again.
    – Kevin
    Jul 16, 2016 at 14:20
  • The (end-of-file) is part of the condition-case form.
    – npostavs
    Jul 16, 2016 at 16:38
  • Yes, it's one of many standard error conditions that are used throughout the system. Previously I was confused because it just sat there, and did nothing. Now I understand that it's just the front part of a condition handler required by condition-case (there could be other cases to handle other errors too), and the body of the handler was left out because there was nothing to do after reaching end of file. Another mystery of elisp figured out in my continuing education...:-)
    – Kevin
    Jul 16, 2016 at 18:04

1 Answer 1


If you want evaluation to return nil when it would normally raise an error, wrap the sexp to be evaluated in ignore-errors:

(ignore-errors (read-from-string contents pos))

See also with-demoted-errors.

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.