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?
(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.(end-of-file)
is part of thecondition-case
form.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...:-)