emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: read lisp objects from buffer


From: yuwen
Subject: Re: read lisp objects from buffer
Date: Tue, 12 Oct 2004 16:56:56 +0800
User-agent: Mozilla Thunderbird 0.7 (Windows/20040616)

Kai Grossjohann wrote:

yuwen <address@hidden> writes:


I tried to read them back into lisp objects:

   (let ((buffer (find-file-noselect "~/src/el/tmp.txt")))
     (set-buffer buffer)
     (goto-char (point-min))
     (while (setq obj (read buffer))
        ;;; do something ))

but there's an error message :
 setq: End of file during parsing

So, what's the right way to read lisp objects from a file/buffer?


It is normal for (read buffer) to fail at the end of the buffer, so
you can catch this error using condition-case.

Or you can skip forward over whitespace and newlines after each read,
then check for eobp (end-of-buffer-p), and exit the loop if that is
true.

Or you prepend "(" and append ")" to the buffer contents, then invoke
read just once.  This will give you a list of objects.

Kai


Thank you. I use condition-case to protect the reading buffer process, and catch the `end-of-file' error:

      (condition-case nil
          (while (setq obj (read buffer))
             ;;; do something
        (end-of-file (kill-buffer buffer))))))


Best regards,
Dai Yuwen




reply via email to

[Prev in Thread] Current Thread [Next in Thread]