unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* read lisp objects from buffer
@ 2004-10-11  9:16 yuwen
  2004-10-11 11:51 ` Kai Grossjohann
  0 siblings, 1 reply; 4+ messages in thread
From: yuwen @ 2004-10-11  9:16 UTC (permalink / raw)


Dear all,

I have a file that contains lisp objects printed by function print:

((16746 13842 393000) "README.txt")

((16746 16787 969000) "/home/yuwen/.emacs")


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?

Best regards,
Dai Yuwen

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: read lisp objects from buffer
  2004-10-11  9:16 read lisp objects from buffer yuwen
@ 2004-10-11 11:51 ` Kai Grossjohann
  2004-10-12  2:00   ` yuwen
  2004-10-12  8:56   ` yuwen
  0 siblings, 2 replies; 4+ messages in thread
From: Kai Grossjohann @ 2004-10-11 11:51 UTC (permalink / raw)


yuwen <yuwen@micetek.com.cn> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: read lisp objects from buffer
  2004-10-11 11:51 ` Kai Grossjohann
@ 2004-10-12  2:00   ` yuwen
  2004-10-12  8:56   ` yuwen
  1 sibling, 0 replies; 4+ messages in thread
From: yuwen @ 2004-10-12  2:00 UTC (permalink / raw)


Kai Grossjohann wrote:

> yuwen <yuwen@micetek.com.cn> 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.
> 
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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: read lisp objects from buffer
  2004-10-11 11:51 ` Kai Grossjohann
  2004-10-12  2:00   ` yuwen
@ 2004-10-12  8:56   ` yuwen
  1 sibling, 0 replies; 4+ messages in thread
From: yuwen @ 2004-10-12  8:56 UTC (permalink / raw)


Kai Grossjohann wrote:

> yuwen <yuwen@micetek.com.cn> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-10-12  8:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-11  9:16 read lisp objects from buffer yuwen
2004-10-11 11:51 ` Kai Grossjohann
2004-10-12  2:00   ` yuwen
2004-10-12  8:56   ` yuwen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).