2 @c This is part of the XEmacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file lispref.texi for copying conditions.
5 @setfilename ../../info/streams.info
6 @node Read and Print, Minibuffers, Debugging, Top
7 @chapter Reading and Printing Lisp Objects
9 @dfn{Printing} and @dfn{reading} are the operations of converting Lisp
10 objects to textual form and vice versa. They use the printed
11 representations and read syntax described in @ref{Lisp Data Types}.
13 This chapter describes the Lisp functions for reading and printing.
14 It also describes @dfn{streams}, which specify where to get the text (if
15 reading) or where to put it (if printing).
18 * Streams Intro:: Overview of streams, reading and printing.
19 * Input Streams:: Various data types that can be used as input streams.
20 * Input Functions:: Functions to read Lisp objects from text.
21 * Output Streams:: Various data types that can be used as output streams.
22 * Output Functions:: Functions to print Lisp objects as text.
23 * Output Variables:: Variables that control what the printing functions do.
27 @section Introduction to Reading and Printing
32 @dfn{Reading} a Lisp object means parsing a Lisp expression in textual
33 form and producing a corresponding Lisp object. This is how Lisp
34 programs get into Lisp from files of Lisp code. We call the text the
35 @dfn{read syntax} of the object. For example, the text @samp{(a .@: 5)}
36 is the read syntax for a cons cell whose @sc{car} is @code{a} and whose
37 @sc{cdr} is the number 5.
39 @dfn{Printing} a Lisp object means producing text that represents that
40 object---converting the object to its printed representation. Printing
41 the cons cell described above produces the text @samp{(a .@: 5)}.
43 Reading and printing are more or less inverse operations: printing the
44 object that results from reading a given piece of text often produces
45 the same text, and reading the text that results from printing an object
46 usually produces a similar-looking object. For example, printing the
47 symbol @code{foo} produces the text @samp{foo}, and reading that text
48 returns the symbol @code{foo}. Printing a list whose elements are
49 @code{a} and @code{b} produces the text @samp{(a b)}, and reading that
50 text produces a list (but not the same list) with elements @code{a}
53 However, these two operations are not precisely inverses. There are
54 three kinds of exceptions:
58 Printing can produce text that cannot be read. For example, buffers,
59 windows, frames, subprocesses and markers print into text that starts
60 with @samp{#}; if you try to read this text, you get an error. There is
61 no way to read those data types.
64 One object can have multiple textual representations. For example,
65 @samp{1} and @samp{01} represent the same integer, and @samp{(a b)} and
66 @samp{(a .@: (b))} represent the same list. Reading will accept any of
67 the alternatives, but printing must choose one of them.
70 Comments can appear at certain points in the middle of an object's
71 read sequence without affecting the result of reading it.
75 @section Input Streams
76 @cindex stream (for reading)
79 Most of the Lisp functions for reading text take an @dfn{input stream}
80 as an argument. The input stream specifies where or how to get the
81 characters of the text to be read. Here are the possible types of input
86 @cindex buffer input stream
87 The input characters are read from @var{buffer}, starting with the
88 character directly after point. Point advances as characters are read.
91 @cindex marker input stream
92 The input characters are read from the buffer that @var{marker} is in,
93 starting with the character directly after the marker. The marker
94 position advances as characters are read. The value of point in the
95 buffer has no effect when the stream is a marker.
98 @cindex string input stream
99 The input characters are taken from @var{string}, starting at the first
100 character in the string and using as many characters as required.
103 @cindex function input stream
104 The input characters are generated by @var{function}, one character per
105 call. Normally @var{function} is called with no arguments, and should
109 Occasionally @var{function} is called with one argument (always a
110 character). When that happens, @var{function} should save the argument
111 and arrange to return it on the next call. This is called
112 @dfn{unreading} the character; it happens when the Lisp reader reads one
113 character too many and wants to ``put it back where it came from''.
116 @cindex @code{t} input stream
117 @code{t} used as a stream means that the input is read from the
118 minibuffer. In fact, the minibuffer is invoked once and the text
119 given by the user is made into a string that is then used as the
123 @cindex @code{nil} input stream
124 @code{nil} supplied as an input stream means to use the value of
125 @code{standard-input} instead; that value is the @dfn{default input
126 stream}, and must be a non-@code{nil} input stream.
129 A symbol as input stream is equivalent to the symbol's function
133 Here is an example of reading from a stream that is a buffer, showing
134 where point is located before and after:
138 ---------- Buffer: foo ----------
139 This@point{} is the contents of foo.
140 ---------- Buffer: foo ----------
144 (read (get-buffer "foo"))
148 (read (get-buffer "foo"))
153 ---------- Buffer: foo ----------
154 This is the@point{} contents of foo.
155 ---------- Buffer: foo ----------
160 Note that the first read skips a space. Reading skips any amount of
161 whitespace preceding the significant text.
163 In Emacs 18, reading a symbol discarded the delimiter terminating the
164 symbol. Thus, point would end up at the beginning of @samp{contents}
165 rather than after @samp{the}. The Emacs 19 behavior is superior because
166 it correctly handles input such as @samp{bar(foo)}, where the
167 open-parenthesis that ends one object is needed as the beginning of
170 Here is an example of reading from a stream that is a marker,
171 initially positioned at the beginning of the buffer shown. The value
172 read is the symbol @code{This}.
177 ---------- Buffer: foo ----------
178 This is the contents of foo.
179 ---------- Buffer: foo ----------
183 (setq m (set-marker (make-marker) 1 (get-buffer "foo")))
184 @result{} #<marker at 1 in foo>
192 @result{} #<marker at 5 in foo> ;; @r{Before the first space.}
196 Here we read from the contents of a string:
200 (read "(When in) the course")
205 The following example reads from the minibuffer. The
206 prompt is: @w{@samp{Lisp expression: }}. (That is always the prompt
207 used when you read from the stream @code{t}.) The user's input is shown
208 following the prompt.
214 ---------- Buffer: Minibuffer ----------
215 Lisp expression: @kbd{23 @key{RET}}
216 ---------- Buffer: Minibuffer ----------
220 Finally, here is an example of a stream that is a function, named
221 @code{useless-stream}. Before we use the stream, we initialize the
222 variable @code{useless-list} to a list of characters. Then each call to
223 the function @code{useless-stream} obtains the next character in the list
224 or unreads a character by adding it to the front of the list.
228 (setq useless-list (append "XY()" nil))
229 @result{} (88 89 40 41)
233 (defun useless-stream (&optional unread)
235 (setq useless-list (cons unread useless-list))
236 (prog1 (car useless-list)
237 (setq useless-list (cdr useless-list)))))
238 @result{} useless-stream
243 Now we read using the stream thus constructed:
247 (read 'useless-stream)
258 Note that the open and close parentheses remains in the list. The Lisp
259 reader encountered the open parenthesis, decided that it ended the
260 input, and unread it. Another attempt to read from the stream at this
261 point would read @samp{()} and return @code{nil}.
263 @ignore @c Not in XEmacs
265 This function is used internally as an input stream to read from the
266 input file opened by the function @code{load}. Don't use this function
271 @node Input Functions
272 @section Input Functions
274 This section describes the Lisp functions and variables that pertain
277 In the functions below, @var{stream} stands for an input stream (see
278 the previous section). If @var{stream} is @code{nil} or omitted, it
279 defaults to the value of @code{standard-input}.
282 An @code{end-of-file} error is signaled if reading encounters an
283 unterminated list, vector, or string.
285 @defun read &optional stream
286 This function reads one textual Lisp expression from @var{stream},
287 returning it as a Lisp object. This is the basic Lisp input function.
290 @defun read-from-string string &optional start end
291 @cindex string to object
292 This function reads the first textual Lisp expression from the text in
293 @var{string}. It returns a cons cell whose @sc{car} is that expression,
294 and whose @sc{cdr} is an integer giving the position of the next
295 remaining character in the string (i.e., the first one not read).
297 If @var{start} is supplied, then reading begins at index @var{start} in
298 the string (where the first character is at index 0). If @var{end} is
299 also supplied, then reading stops just before that index, as if the rest
300 of the string were not there.
306 (read-from-string "(setq x 55) (setq y 5)")
307 @result{} ((setq x 55) . 11)
310 (read-from-string "\"A short string\"")
311 @result{} ("A short string" . 16)
315 ;; @r{Read starting at the first character.}
316 (read-from-string "(list 112)" 0)
317 @result{} ((list 112) . 10)
320 ;; @r{Read starting at the second character.}
321 (read-from-string "(list 112)" 1)
325 ;; @r{Read starting at the seventh character,}
326 ;; @r{and stopping at the ninth.}
327 (read-from-string "(list 112)" 6 8)
333 @defvar standard-input
334 This variable holds the default input stream---the stream that
335 @code{read} uses when the @var{stream} argument is @code{nil}.
339 @section Output Streams
340 @cindex stream (for printing)
341 @cindex output stream
343 An output stream specifies what to do with the characters produced
344 by printing. Most print functions accept an output stream as an
345 optional argument. Here are the possible types of output stream:
349 @cindex buffer output stream
350 The output characters are inserted into @var{buffer} at point.
351 Point advances as characters are inserted.
354 @cindex marker output stream
355 The output characters are inserted into the buffer that @var{marker}
356 points into, at the marker position. The marker position advances as
357 characters are inserted. The value of point in the buffer has no effect
358 on printing when the stream is a marker.
361 @cindex function output stream
362 The output characters are passed to @var{function}, which is responsible
363 for storing them away. It is called with a single character as
364 argument, as many times as there are characters to be output, and is
365 free to do anything at all with the characters it receives.
368 @cindex @code{t} output stream
369 The output characters are displayed in the echo area.
372 @cindex @code{nil} output stream
373 @code{nil} specified as an output stream means to the value of
374 @code{standard-output} instead; that value is the @dfn{default output
375 stream}, and must be a non-@code{nil} output stream.
378 A symbol as output stream is equivalent to the symbol's function
382 Many of the valid output streams are also valid as input streams. The
383 difference between input and output streams is therefore mostly one of
384 how you use a Lisp object, not a distinction of types of object.
386 Here is an example of a buffer used as an output stream. Point is
387 initially located as shown immediately before the @samp{h} in
388 @samp{the}. At the end, point is located directly before that same
391 @cindex print example
394 ---------- Buffer: foo ----------
395 This is t@point{}he contents of foo.
396 ---------- Buffer: foo ----------
399 (print "This is the output" (get-buffer "foo"))
400 @result{} "This is the output"
403 ---------- Buffer: foo ----------
406 @point{}he contents of foo.
407 ---------- Buffer: foo ----------
411 Now we show a use of a marker as an output stream. Initially, the
412 marker is in buffer @code{foo}, between the @samp{t} and the @samp{h} in
413 the word @samp{the}. At the end, the marker has advanced over the
414 inserted text so that it remains positioned before the same @samp{h}.
415 Note that the location of point, shown in the usual fashion, has no
420 ---------- Buffer: foo ----------
421 "This is the @point{}output"
422 ---------- Buffer: foo ----------
427 @result{} #<marker at 11 in foo>
431 (print "More output for foo." m)
432 @result{} "More output for foo."
436 ---------- Buffer: foo ----------
438 "More output for foo."
440 ---------- Buffer: foo ----------
445 @result{} #<marker at 35 in foo>
449 The following example shows output to the echo area:
453 (print "Echo Area output" t)
454 @result{} "Echo Area output"
455 ---------- Echo Area ----------
457 ---------- Echo Area ----------
461 Finally, we show the use of a function as an output stream. The
462 function @code{eat-output} takes each character that it is given and
463 conses it onto the front of the list @code{last-output} (@pxref{Building
464 Lists}). At the end, the list contains all the characters output, but
469 (setq last-output nil)
474 (defun eat-output (c)
475 (setq last-output (cons c last-output)))
480 (print "This is the output" 'eat-output)
481 @result{} "This is the output"
486 @result{} (?\n ?\" ?t ?u ?p ?t ?u ?o ?\ ?e ?h ?t
487 ?\ ?s ?i ?\ ?s ?i ?h ?T ?\" ?\n)
492 Now we can put the output in the proper order by reversing the list:
496 (concat (nreverse last-output))
498 \"This is the output\"
504 Calling @code{concat} converts the list to a string so you can see its
505 contents more clearly.
507 @node Output Functions
508 @section Output Functions
510 This section describes the Lisp functions for printing Lisp objects.
512 @cindex @samp{"} in printing
513 @cindex @samp{\} in printing
514 @cindex quoting characters in printing
515 @cindex escape characters in printing
516 Some of the XEmacs printing functions add quoting characters to the
517 output when necessary so that it can be read properly. The quoting
518 characters used are @samp{"} and @samp{\}; they distinguish strings from
519 symbols, and prevent punctuation characters in strings and symbols from
520 being taken as delimiters when reading. @xref{Printed Representation},
521 for full details. You specify quoting or no quoting by the choice of
524 If the text is to be read back into Lisp, then it is best to print
525 with quoting characters to avoid ambiguity. Likewise, if the purpose is
526 to describe a Lisp object clearly for a Lisp programmer. However, if
527 the purpose of the output is to look nice for humans, then it is better
528 to print without quoting.
530 Printing a self-referent Lisp object requires an infinite amount of
531 text. In certain cases, trying to produce this text leads to a stack
532 overflow. XEmacs detects such recursion and prints @samp{#@var{level}}
533 instead of recursively printing an object already being printed. For
534 example, here @samp{#0} indicates a recursive reference to the object at
535 level 0 of the current print operation:
538 (setq foo (list nil))
544 In the functions below, @var{stream} stands for an output stream.
545 (See the previous section for a description of output streams.) If
546 @var{stream} is @code{nil} or omitted, it defaults to the value of
547 @code{standard-output}.
549 @defun print object &optional stream
551 The @code{print} function is a convenient way of printing. It outputs
552 the printed representation of @var{object} to @var{stream}, printing in
553 addition one newline before @var{object} and another after it. Quoting
554 characters are used. @code{print} returns @var{object}. For example:
558 (progn (print 'The\ cat\ in)
560 (print " came back"))
562 @print{} The\ cat\ in
566 @print{} " came back"
568 @result{} " came back"
573 @defun prin1 object &optional stream
574 This function outputs the printed representation of @var{object} to
575 @var{stream}. It does not print newlines to separate output as
576 @code{print} does, but it does use quoting characters just like
577 @code{print}. It returns @var{object}.
581 (progn (prin1 'The\ cat\ in)
583 (prin1 " came back"))
584 @print{} The\ cat\ in"the hat"" came back"
585 @result{} " came back"
590 @defun princ object &optional stream
591 This function outputs the printed representation of @var{object} to
592 @var{stream}. It returns @var{object}.
594 This function is intended to produce output that is readable by people,
595 not by @code{read}, so it doesn't insert quoting characters and doesn't
596 put double-quotes around the contents of strings. It does not add any
597 spacing between calls.
603 (princ " in the \"hat\""))
604 @print{} The cat in the "hat"
605 @result{} " in the \"hat\""
610 @defun terpri &optional stream
611 @cindex newline in print
612 This function outputs a newline to @var{stream}. The name stands
613 for ``terminate print''.
616 @defun write-char character &optional stream
617 This function outputs @var{character} to @var{stream}. It returns
621 @defun prin1-to-string object &optional noescape
622 @cindex object to string
623 This function returns a string containing the text that @code{prin1}
624 would have printed for the same argument.
628 (prin1-to-string 'foo)
632 (prin1-to-string (mark-marker))
633 @result{} "#<marker at 2773 in strings.texi>"
637 If @var{noescape} is non-@code{nil}, that inhibits use of quoting
638 characters in the output. (This argument is supported in Emacs versions
643 (prin1-to-string "foo")
647 (prin1-to-string "foo" t)
652 See @code{format}, in @ref{String Conversion}, for other ways to obtain
653 the printed representation of a Lisp object as a string.
656 @node Output Variables
657 @section Variables Affecting Output
659 @defvar standard-output
660 The value of this variable is the default output stream---the stream
661 that print functions use when the @var{stream} argument is @code{nil}.
664 @defvar print-escape-newlines
665 @cindex @samp{\n} in print
666 @cindex escape characters
667 If this variable is non-@code{nil}, then newline characters in strings
668 are printed as @samp{\n} and formfeeds are printed as @samp{\f}.
669 Normally these characters are printed as actual newlines and formfeeds.
671 This variable affects the print functions @code{prin1} and @code{print},
672 as well as everything that uses them. It does not affect @code{princ}.
673 Here is an example using @code{prin1}:
685 (let ((print-escape-newlines t))
694 In the second expression, the local binding of
695 @code{print-escape-newlines} is in effect during the call to
696 @code{prin1}, but not during the printing of the result.
699 @defvar print-readably
700 @cindex printing readably
701 If non-@code{nil}, then all objects will be printed in a readable form.
702 If an object has no readable representation, then an error is signalled.
703 When @code{print-readably} is true, compiled-function objects will be
704 written in @samp{#[...]} form instead of in @samp{#<compiled-function
705 [...]>} form, and two-element lists of the form @samp{(quote object)}
706 will be written as the equivalent @samp{'object}. Do not @emph{set}
707 this variable; bind it instead.
711 @cindex printing limits
712 The value of this variable is the maximum number of elements of a list
713 that will be printed. If a list being printed has more than this many
714 elements, it is abbreviated with an ellipsis.
716 If the value is @code{nil} (the default), then there is no limit.
720 (setq print-length 2)
732 The value of this variable is the maximum depth of nesting of
733 parentheses and brackets when printed. Any list or vector at a depth
734 exceeding this limit is abbreviated with an ellipsis. A value of
735 @code{nil} (which is the default) means no limit.
737 This variable exists in version 19 and later versions.
740 @defvar print-string-length
741 @cindex string length, maximum when printing
742 The value of this variable is the maximum number of characters of a string
743 that will be printed. If a string being printed has more than this many
744 characters, it is abbreviated with an ellipsis.
748 @cindex printing uninterned symbols
749 @cindex uninterned symbols, printing
750 If non-@code{nil}, then uninterned symbols will be printed specially.
751 Uninterned symbols are those which are not present in @code{obarray},
752 that is, those which were made with @code{make-symbol} or by calling
753 @code{intern} with a second argument.
755 When @code{print-gensym} is true, such symbols will be preceded by
756 @samp{#:}, which causes the reader to create a new symbol instead of
757 interning and returning an existing one. Beware: The @samp{#:} syntax
758 creates a new symbol each time it is seen, so if you print an object
759 which contains two pointers to the same uninterned symbol, @code{read}
760 will not duplicate that structure.
762 Also, since XEmacs has no real notion of packages, there is no way for
763 the printer to distinguish between symbols interned in no obarray, and
764 symbols interned in an alternate obarray.
767 @defvar float-output-format
768 @cindex printing floating-point numbers
769 @cindex floating-point numbers, printing
770 This variable holds the format descriptor string that Lisp uses to print
771 floats. This is a @samp{%}-spec like those accepted by @code{printf} in
772 C, but with some restrictions. It must start with the two characters
773 @samp{%.}. After that comes an integer precision specification, and
774 then a letter which controls the format. The letters allowed are
775 @samp{e}, @samp{f} and @samp{g}.
779 Use @samp{e} for exponential notation
780 @samp{@var{dig}.@var{digits}e@var{expt}}.
782 Use @samp{f} for decimal point notation @samp{DIGITS.DIGITS}.
784 Use @samp{g} to choose the shorter of those two formats for the number
788 The precision in any of these cases is the number of digits following
789 the decimal point. With @samp{f}, a precision of 0 means to omit the
790 decimal point. 0 is not allowed with @samp{f} or @samp{g}.
792 A value of @code{nil} means to use @samp{%.16g}.
794 Regardless of the value of @code{float-output-format}, a floating point
795 number will never be printed in such a way that it is ambiguous with an
796 integer; that is, a floating-point number will always be printed with a
797 decimal point and/or an exponent, even if the digits following the
798 decimal point are all zero. This is to preserve read-equivalence.