aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1994-03-16 19:53:19 +0000
committerRichard M. Stallman1994-03-16 19:53:19 +0000
commit5e8db0c61e9bcb0d0d34bdeda19f88cfb90b20fe (patch)
tree0eb445f545bbee2f6be9fa046c17840f87c7e610
parent0a9482c502b4b1ddd50fc801099892f48782b67f (diff)
downloademacs-5e8db0c61e9bcb0d0d34bdeda19f88cfb90b20fe.tar.gz
emacs-5e8db0c61e9bcb0d0d34bdeda19f88cfb90b20fe.zip
Initial revision
-rw-r--r--lispref/help.texi624
-rw-r--r--lispref/streams.texi713
2 files changed, 1337 insertions, 0 deletions
diff --git a/lispref/help.texi b/lispref/help.texi
new file mode 100644
index 00000000000..68fb6330f94
--- /dev/null
+++ b/lispref/help.texi
@@ -0,0 +1,624 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/help
6@node Documentation, Files, Modes, Top
7@chapter Documentation
8@cindex documentation strings
9
10 GNU Emacs Lisp has convenient on-line help facilities, most of which
11derive their information from the documentation strings associated with
12functions and variables. This chapter describes how to write good
13documentation strings for your Lisp programs, as well as how to write
14programs to access documentation.
15
16 Note that the documentation strings for Emacs are not the same thing
17as the Emacs manual. Manuals have their own source files, written in
18the Texinfo language; documentation strings are specified in the
19definitions of the functions and variables they apply to. A collection
20of documentation strings is not sufficient as a manual because a good
21manual is not organized in that fashion; it is organized in terms of
22topics of discussion.
23
24@menu
25* Documentation Basics:: Good style for doc strings.
26 Where to put them. How Emacs stores them.
27* Accessing Documentation:: How Lisp programs can access doc strings.
28* Keys in Documentation:: Substituting current key bindings.
29* Describing Characters:: Making printable descriptions of
30 non-printing characters and key sequences.
31* Help Functions:: Subroutines used by Emacs help facilities.
32@end menu
33
34@node Documentation Basics
35@comment node-name, next, previous, up
36@section Documentation Basics
37@cindex documentation conventions
38@cindex writing a documentation string
39@cindex string, writing a doc string
40
41 A documentation string is written using the Lisp syntax for strings,
42with double-quote characters surrounding the text of the string. This
43is because it really is a Lisp string object. The string serves as
44documentation when it is written in the proper place in the definition
45of a function or variable. In a function definition, the documentation
46string follows the argument list. In a variable definition, the
47documentation string follows the initial value of the variable.
48
49 When you write a documentation string, make the first line a complete
50sentence (or two complete sentences) since some commands, such as
51@code{apropos}, show only the first line of a multi-line documentation
52string. Also, you should not indent the second line of a documentation
53string, if you have one, because that looks odd when you use @kbd{C-h f}
54(@code{describe-function}) or @kbd{C-h v} (@code{describe-variable}).
55@xref{Documentation Tips}.
56
57 Documentation strings may contain several special substrings, which
58stand for key bindings to be looked up in the current keymaps when the
59documentation is displayed. This allows documentation strings to refer
60to the keys for related commands and be accurate even when a user
61rearranges the key bindings. (@xref{Accessing Documentation}.)
62
63 Within the Lisp world, a documentation string is kept with the
64function or variable that it describes:
65
66@itemize @bullet
67@item
68The documentation for a function is stored in the function definition
69itself (@pxref{Lambda Expressions}). The function
70@code{documentation} knows how to extract it.
71
72@item
73@kindex variable-documentation
74The documentation for a variable is stored in the variable's property
75list under the property name @code{variable-documentation}. The
76function @code{documentation-property} knows how to extract it.
77@end itemize
78
79@cindex @file{DOC} (documentation) file
80@cindex @file{emacs/etc/DOC-@var{version}}
81@cindex @file{etc/DOC-@var{version}}
82To save space, the documentation for preloaded functions and variables
83(including primitive functions and autoloaded functions) are stored in
84the file @file{emacs/etc/DOC-@var{version}}. The data structure inside
85Emacs has an integer offset into the file, where the documentation
86string ought to be. The functions @code{documentation} the
87@code{documentation-property} read the documentation from the file
88@file{emacs/etc/DOC-@var{version}} when they notice the integer there;
89this is transparent to the user. Keeping the documentation strings out
90of the Emacs core image saves a significant amount of space.
91@xref{Building Emacs}.
92
93 For information on the uses of documentation strings, see @ref{Help, ,
94Help, emacs, The GNU Emacs Manual}.
95
96@c Wordy to prevent overfull hbox. --rjc 15mar92
97 The @file{emacs/etc} directory contains two utilities that you can use
98to print nice-looking hardcopy for the file
99@file{emacs/etc/DOC-@var{version}}. These are @file{sorted-doc.c} and
100@file{digest-doc.c}.
101
102@node Accessing Documentation
103@section Access to Documentation Strings
104
105@defun documentation-property symbol property &optional verbatim
106This function returns the documentation string that is recorded
107@var{symbol}'s property list under property @var{property}. It
108retrieves the text from the file @file{emacs/etc/DOC-@var{version}} if
109necessary, and runs @code{substitute-command-keys} to substitute actual
110key bindings. (This substitution is not done if @var{verbatim} is
111non-@code{nil}; the @var{verbatim} argument exists only as of Emacs 19.)
112
113@smallexample
114@group
115(documentation-property 'command-line-processed
116 'variable-documentation)
117 @result{} "t once command line has been processed"
118@end group
119@group
120(symbol-plist 'command-line-processed)
121 @result{} (variable-documentation 188902)
122@end group
123@end smallexample
124@end defun
125
126@defun documentation function &optional verbatim
127This function returns the documentation string of @var{function}.
128This function will access the documentation string if it is stored in
129the @file{emacs/etc/DOC-@var{version}} file.
130
131In addition, @code{documentation} runs @code{substitute-command-keys}
132on the resulting string, so the value contains the actual (current) key
133bindings. (This is not done if @var{verbatim} is non-@code{nil}; the
134@var{verbatim} argument exists only as of Emacs 19.)
135
136The function @code{documentation} signals a @code{void-function} error
137unless @var{function} has a function definition. However, it is ok if
138the function definition has no documentation string. In that case,
139@code{documentation} returns @code{nil}.
140@end defun
141
142@c Wordy to prevent overfull hboxes. --rjc 15mar92
143 Here is an example of using the two functions, @code{documentation} and
144@code{documentation-property}, to display the documentation strings for
145several symbols in a @samp{*Help*} buffer.
146
147@smallexample
148@group
149(defun describe-symbols (pattern)
150 "Describe the Emacs Lisp symbols matching PATTERN.
151All symbols that have PATTERN in their name are described
152in the `*Help*' buffer."
153 (interactive "sDescribe symbols matching: ")
154 (let ((describe-func
155 (function
156 (lambda (s)
157@end group
158@group
159 ;; @r{Print description of symbol.}
160 (if (fboundp s) ; @r{It is a function.}
161 (princ
162 (format "%s\t%s\n%s\n\n" s
163 (if (commandp s)
164 (let ((keys (where-is-internal s)))
165 (if keys
166 (concat
167 "Keys: "
168 (mapconcat 'key-description
169 keys " "))
170 "Keys: none"))
171 "Function")
172@end group
173@group
174 (or (documentation s)
175 "not documented"))))
176
177 (if (boundp s) ; @r{It is a variable.}
178@end group
179@group
180 (princ
181 (format "%s\t%s\n%s\n\n" s
182 (if (user-variable-p s)
183 "Option " "Variable")
184@end group
185@group
186 (or (documentation-property
187 s 'variable-documentation)
188 "not documented")))))))
189 sym-list)
190@end group
191
192@group
193 ;; @r{Build a list of symbols that match pattern.}
194 (mapatoms (function
195 (lambda (sym)
196 (if (string-match pattern (symbol-name sym))
197 (setq sym-list (cons sym sym-list))))))
198@end group
199
200@group
201 ;; @r{Display the data.}
202 (with-output-to-temp-buffer "*Help*"
203 (mapcar describe-func (sort sym-list 'string<))
204 (print-help-return-message))))
205@end group
206@end smallexample
207
208 The @code{describe-symbols} function works like @code{apropos},
209but provides more information.
210
211@smallexample
212@group
213(describe-symbols "goal")
214
215---------- Buffer: *Help* ----------
216goal-column Option
217*Semipermanent goal column for vertical motion, as set by C-x C-n, or nil.
218@end group
219@c Do not blithely break or fill these lines.
220@c That makes them incorrect.
221
222@group
223set-goal-column Command: C-x C-n
224Set the current horizontal position as a goal for C-n and C-p.
225@end group
226@c DO NOT put a blank line here! That is factually inaccurate!
227@group
228Those commands will move to this position in the line moved to
229rather than trying to keep the same horizontal position.
230With a non-nil argument, clears out the goal column
231so that C-n and C-p resume vertical motion.
232The goal column is stored in the variable `goal-column'.
233@end group
234
235@group
236temporary-goal-column Variable
237Current goal column for vertical motion.
238It is the column where point was
239at the start of current run of vertical motion commands.
240When the `track-eol' feature is doing its job, the value is 9999.
241---------- Buffer: *Help* ----------
242@end group
243@end smallexample
244
245@defun Snarf-documentation filename
246 This function is used only during Emacs initialization, just before
247the runnable Emacs is dumped. It finds the file offsets of the
248documentation strings stored in the file @var{filename}, and records
249them in the in-core function definitions and variable property lists in
250place of the actual strings. @xref{Building Emacs}.
251
252 Emacs finds the file @var{filename} in the @file{emacs/etc} directory.
253When the dumped Emacs is later executed, the same file is found in the
254directory @code{doc-directory}. Usually @var{filename} is
255@code{"DOC-@var{version}"}.
256@end defun
257
258@c Emacs 19 feature
259@defvar doc-directory
260This variable holds the name of the directory which should contion the
261file @code{"DOC-@var{version}"} that contains documentation strings for
262built-in and preloaded functions and variables.
263
264In most cases, this is the same as @code{data-directory}. They may be
265different when you run Emacs from the directory where you built it,
266without actually installing it. See @code{data-directory} in @ref{Help
267Functions}.
268
269In older Emacs versions, @code{exec-directory} was used for this.
270@end defvar
271
272@node Keys in Documentation
273@section Substituting Key Bindings in Documentation
274@cindex documentation, keys in
275@cindex keys in documentation strings
276@cindex substituting keys in documentation
277
278 When documentation strings refer to key sequences, they should do so
279based on the current, actual key bindings. They can do so using certain
280special text sequences described below. Accessing documentation strings
281in the usual way substitutes current key binding information for these
282special sequences. This works by calling @code{substitute-command-keys}.
283You can also call that function yourself.
284
285 Here is a list of the special sequences and what they mean:
286
287@table @code
288@item \[@var{command}]
289stands for a key sequence that will invoke @var{command}, or @samp{M-x
290@var{command}} if @var{command} has no key bindings.
291
292@item \@{@var{mapvar}@}
293stands for a summary of the value of @var{mapvar}, which should be a
294keymap. The summary is made by @code{describe-bindings}.
295
296@item \<@var{mapvar}>
297stands for no text itself. It is used for a side effect: it specifies
298@var{mapvar} as the keymap for any following @samp{\[@var{command}]}
299sequences in this documentation string.
300@end table
301
302@strong{Please note:} each @samp{\} must be doubled when written in a
303string in Emacs Lisp.
304
305@defun substitute-command-keys string
306This function scans @var{string} for the above special sequences and
307replaces them by what they stand for, returning the result as a string.
308This permits display of documentation that refers accurately to the
309users's own customized key bindings.
310@end defun
311
312 Here are examples of the special sequences:
313
314@smallexample
315@group
316(substitute-command-keys
317 "To abort recursive edit, type: \\[abort-recursive-edit]")
318@result{} "To abort recursive edit, type: C-]"
319@end group
320
321@group
322(substitute-command-keys
323 "The keys that are defined for the minibuffer here are:
324 \\@{minibuffer-local-must-match-map@}")
325@result{} "The keys that are defined for the minibuffer here are:
326@end group
327
328? minibuffer-completion-help
329SPC minibuffer-complete-word
330TAB minibuffer-complete
331LFD minibuffer-complete-and-exit
332RET minibuffer-complete-and-exit
333C-g abort-recursive-edit
334"
335
336@group
337(substitute-command-keys
338 "To abort a recursive edit from the minibuffer, type\
339\\<minibuffer-local-must-match-map>\\[abort-recursive-edit].")
340@result{} "To abort a recursive edit from the minibuffer, type C-g."
341@end group
342@end smallexample
343
344@node Describing Characters
345@section Describing Characters for Help Messages
346
347 These functions convert events, key sequences or characters to textual
348descriptions. These descriptions are useful for including arbitrary
349text characters or key sequences in messages, because they convert
350non-printing and whitespace characters to sequences of printing
351characters. The description of a non-whitespace printing character is
352the character itself.
353
354@defun key-description sequence
355@cindex Emacs event standard notation
356This function returns a string containing the Emacs standard notation
357for the input events in @var{sequence}. The argument @var{sequence} may
358be a string, vector or list. @xref{Input Events}, for more information
359about valid events. See also the examples for
360@code{single-key-description}, below.
361@end defun
362
363@defun single-key-description event
364@cindex event printing
365@cindex character printing
366@cindex control character printing
367@cindex meta character printing
368This function returns a string describing @var{event} in the standard
369Emacs notation for keyboard input. A normal printing character appears
370as itself, but a control character turns into a string starting with
371@samp{C-}, a meta character turns into a string starting with @samp{M-},
372and space, linefeed, etc.@: appear as @samp{SPC}, @samp{LFD}, etc. A
373function key symbol appears as itself. An event which is a list appears
374as the name of the symbol in the @sc{car} of the list.
375
376@smallexample
377@group
378(single-key-description ?\C-x)
379 @result{} "C-x"
380@end group
381@group
382(key-description "\C-x \M-y \n \t \r \f123")
383 @result{} "C-x SPC M-y SPC LFD SPC TAB SPC RET SPC C-l 1 2 3"
384@end group
385@group
386(single-key-description 'C-mouse-1)
387 @result{} "C-mouse-1"
388@end group
389@end smallexample
390@end defun
391
392@defun text-char-description character
393This function returns a string describing @var{character} in the
394standard Emacs notation for characters that appear in text---like
395@code{single-key-description}, except that control characters are
396represented with a leading caret (which is how control characters in
397Emacs buffers are usually displayed).
398
399@smallexample
400@group
401(text-char-description ?\C-c)
402 @result{} "^C"
403@end group
404@group
405(text-char-description ?\M-m)
406 @result{} "M-m"
407@end group
408@group
409(text-char-description ?\C-\M-m)
410 @result{} "M-^M"
411@end group
412@end smallexample
413@end defun
414
415@node Help Functions
416@section Help Functions
417
418 Emacs provides a variety of on-line help functions, all accessible to
419the user as subcommands of the prefix @kbd{C-h}. For more information
420about them, see @ref{Help, , Help, emacs, The GNU Emacs Manual}. Here
421we describe some program-level interfaces to the same information.
422
423@deffn Command apropos regexp &optional do-all predicate
424 This function finds all symbols whose names contain a match for the
425regular expression @var{regexp}, and returns a list of them.
426It also displays the symbols in a buffer named @samp{*Help*}, each with a
427one-line description.
428
429@c Emacs 19 feature
430 If @var{do-all} is non-@code{nil}, then @code{apropos} also shows
431key bindings for the functions that are found.
432
433 If @var{predicate} is non-@code{nil}, it should be a function to be
434called on each symbol that has matched @var{regexp}. Only symbols for
435which @var{predicate} returns a non-@code{nil} value are listed or
436displayed.
437
438 In the first of the following examples, @code{apropos} finds all the
439symbols with names containing @samp{exec}. In the second example, it
440finds and returns only those symbols that are also commands.
441(We don't show the output that results in the @samp{*Help*} buffer.)
442
443@smallexample
444@group
445(apropos "exec")
446 @result{} (Buffer-menu-execute command-execute exec-directory
447 exec-path execute-extended-command execute-kbd-macro
448 executing-kbd-macro executing-macro)
449@end group
450
451@group
452(apropos "exec" nil 'commandp)
453 @result{} (Buffer-menu-execute execute-extended-command)
454@end group
455@ignore
456@group
457---------- Buffer: *Help* ----------
458Buffer-menu-execute
459 Function: Save and/or delete buffers marked with
460 M-x Buffer-menu-save or M-x Buffer-menu-delete commands.
461execute-extended-command ESC x
462 Function: Read function name, then read its
463 arguments and call it.
464---------- Buffer: *Help* ----------
465@end group
466@end ignore
467@end smallexample
468
469 The command @kbd{C-h a} (@code{command-apropos}) calls @code{apropos},
470but specifies a @var{predicate} to restrict the output to symbols that
471are commands. The call to @code{apropos} looks like this:
472
473@smallexample
474(apropos string t 'commandp)
475@end smallexample
476@end deffn
477
478@c Emacs 19 feature
479@deffn Command super-apropos regexp &optional do-all
480This function differs from @code{apropos} in that it searches
481documentation strings as well as symbol names for matches for
482@var{regexp}. By default, it searches only the documentation strings,
483and only those of functions and variables that are included in Emacs
484when it is dumped. If @var{do-all} is non-@code{nil}, it scans the
485names and documentation strings of all functions and variables.
486@end deffn
487
488@defvar help-map
489The value of this variable is a local keymap for characters following the
490Help key, @kbd{C-h}.
491@end defvar
492
493@deffn {Prefix Command} help-command
494This symbol is not a function; its function definition is actually the
495keymap known as @code{help-map}. It is defined in @file{help.el} as
496follows:
497
498@smallexample
499@group
500(define-key global-map "\C-h" 'help-command)
501(fset 'help-command help-map)
502@end group
503@end smallexample
504@end deffn
505
506@defun print-help-return-message &optional function
507This function builds a string which is a message explaining how to
508restore the previous state of the windows after a help command. After
509building the message, it applies @var{function} to it if @var{function}
510is non-@code{nil}. Otherwise it calls @code{message} to display it in
511the echo area.
512
513This function expects to be called inside a
514@code{with-output-to-temp-buffer} special form, and expects
515@code{standard-output} to have the value bound by that special form.
516For an example of its use, see the long example in @ref{Accessing
517Documentation}.
518@end defun
519
520@defvar help-char
521The value of this variable is the help character---the character that
522Emacs recognizes as meaning Help. By default, it is 8, which is
523@kbd{C-h}. When Emacs reads this character, if @code{help-form} is
524non-@code{nil} Lisp expression, it evaluates that expression, and
525displays the result in a window if it is a string.
526
527Usually the value of @code{help-form}'s value is @code{nil}. Then the
528help character has no special meaning at the level of command input, and
529it becomes part of a key sequence in the normal way. The standard key
530binding of @kbd{C-h} is a prefix key for several general-purpose help
531features.
532
533The help character is special after prefix keys, too. If it has no
534binding as a subcommand of the prefix key, it runs
535@code{describe-prefix-bindings}, which displays a list of all the
536subcommands of the prefix key.
537@end defvar
538
539@defvar help-form
540If this variable is non-@code{nil}, its value is a form to evaluate
541whenever the character @code{help-char} is read. If evaluating the form
542produces a string, that string is displayed.
543
544A command that calls @code{read-event} or @code{read-char} probably
545should bind @code{help-form} to a non-@code{nil} expression while it
546does input. (The exception is when @kbd{C-h} is meaningful input.)
547Evaluating this expression should result in a string that explains what
548the input is for and how to enter it properly.
549
550Entry to the minibuffer binds this variable to the value of
551@code{minibuffer-help-form} (@pxref{Minibuffer Misc}).
552@end defvar
553
554@defvar prefix-help-command
555This variable holds a function to print help for a prefix character.
556The function is called when the user types a prefix key followed by the
557help character, and the help character has no binding after that prefix.
558The variable's default value is @code{describe-prefix-bindings}.
559@end defvar
560
561@defun describe-prefix-bindings
562This function calls @code{describe-bindings} to display a list of all
563the subcommands of the prefix key of the most recent key sequence. The
564prefix described consists of all but the last event of that key
565sequence.
566@end defun
567
568 The following two functions are found in the library @file{helper}.
569They are for modes that want to provide help without relinquishing
570control, such as the ``electric'' modes. You must load that library
571with @code{(require 'helper)} in order to use them. Their names begin
572with @samp{Helper} to distinguish them from the ordinary help functions.
573
574@deffn Command Helper-describe-bindings
575This command pops up a window displaying a help buffer containing a
576listing of all of the key bindings from both the local and global keymaps.
577It works by calling @code{describe-bindings}.
578@end deffn
579
580@deffn Command Helper-help
581This command provides help for the current mode. It prompts the user
582in the minibuffer with the message @samp{Help (Type ? for further
583options)}, and then provides assistance in finding out what the key
584bindings are, and what the mode is intended for. It returns @code{nil}.
585
586This can be customized by changing the map @code{Helper-help-map}.
587@end deffn
588
589@c Emacs 19 feature
590@defvar data-directory
591This variable holds the name of the directory in which Emacs finds
592certain documentation and text files that come with Emacs. In older
593Emacs versions, @code{exec-directory} was used for this.
594@end defvar
595
596@c Emacs 19 feature
597@defmac make-help-screen fname help-line help-text help-map
598This macro defines a help command named @var{fname} which acts like a
599prefix key which shows a list of the subcommands it offers.
600
601When invoked, @var{fname} displays @var{help-text} in a window, then
602reads and executes a key sequence according to @var{help-map}. The
603string @var{help-text} should describe of the bindings available in
604@var{help-map}.
605
606The command @var{fname} is defined to handle a few events itself, by
607scrolling the display of @var{help-text}. When @var{fname} reads one of
608those special events, it does the scrolling and then reads another
609event. When it reads an event which is not one of those few, and which
610has a binding in @var{help-map}, it executes that key's binding and
611then returns.
612
613The argument @var{help-line} should be a single-line summary of the
614alternatives in @var{help-map}. In the current version of Emacs, this
615argument is used only if you set the option @code{three-step-help} to
616@code{t}.
617@end defmac
618
619@defopt three-step-help
620If this variable is non-@code{nil}, commands defined with
621@code{make-help-screen} display their @var{help-line} strings in the
622echo area at first, and display the longer @var{help-text} strings only
623if the user types the help character again.
624@end defopt
diff --git a/lispref/streams.texi b/lispref/streams.texi
new file mode 100644
index 00000000000..af3787f579d
--- /dev/null
+++ b/lispref/streams.texi
@@ -0,0 +1,713 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/streams
6@node Streams, Minibuffers, Debugging, Top
7@comment node-name, next, previous, up
8@chapter Reading and Printing Lisp Objects
9
10 @dfn{Printing} and @dfn{reading} are the operations of converting Lisp
11objects to textual form and vice versa. They use the printed
12representations and read syntax described in @ref{Types of Lisp Object}.
13
14 This chapter describes the Lisp functions for reading and printing.
15It also describes @dfn{streams}, which specify where to get the text (if
16reading) or where to put it (if printing).
17
18@menu
19* Streams Intro:: Overview of streams, reading and printing.
20* Input Streams:: Various data types that can be used as input streams.
21* Input Functions:: Functions to read Lisp objects from text.
22* Output Streams:: Various data types that can be used as output streams.
23* Output Functions:: Functions to print Lisp objects as text.
24* Output Variables:: Variables that control what the printing functions do.
25@end menu
26
27@node Streams Intro
28@section Introduction to Reading and Printing
29@cindex Lisp reader
30@cindex printing
31@cindex reading
32
33 @dfn{Reading} a Lisp object means parsing a Lisp expression in textual
34form and producing a corresponding Lisp object. This is how Lisp
35programs get into Lisp from files of Lisp code. We call the text the
36@dfn{read syntax} of the object. For example, the text @samp{(a .@: 5)}
37is the read syntax for a cons cell whose @sc{car} is @code{a} and whose
38@sc{cdr} is the number 5.
39
40 @dfn{Printing} a Lisp object means producing text that represents that
41object---converting the object to its printed representation. Printing
42the cons cell described above produces the text @samp{(a .@: 5)}.
43
44 Reading and printing are more or less inverse operations: printing the
45object that results from reading a given piece of text often produces
46the same text, and reading the text that results from printing an object
47usually produces a similar-looking object. For example, printing the
48symbol @code{foo} produces the text @samp{foo}, and reading that text
49returns the symbol @code{foo}. Printing a list whose elements are
50@code{a} and @code{b} produces the text @samp{(a b)}, and reading that
51text produces a list (but not the same list) with elements are @code{a}
52and @code{b}.
53
54 However, these two operations are not precisely inverses. There are
55two kinds of exceptions:
56
57@itemize @bullet
58@item
59Printing can produce text that cannot be read. For example, buffers,
60windows, frames, subprocesses and markers print into text that starts
61with @samp{#}; if you try to read this text, you get an error. There is
62no way to read those data types.
63
64@item
65One object can have multiple textual representations. For example,
66@samp{1} and @samp{01} represent the same integer, and @samp{(a b)} and
67@samp{(a .@: (b))} represent the same list. Reading will accept any of
68the alternatives, but printing must choose one of them.
69@end itemize
70
71@node Input Streams
72@section Input Streams
73@cindex stream (for reading)
74@cindex input stream
75
76 Most of the Lisp functions for reading text take an @dfn{input stream}
77as an argument. The input stream specifies where or how to get the
78characters of the text to be read. Here are the possible types of input
79stream:
80
81@table @asis
82@item @var{buffer}
83@cindex buffer input stream
84The input characters are read from @var{buffer}, starting with the
85character directly after point. Point advances as characters are read.
86
87@item @var{marker}
88@cindex marker input stream
89The input characters are read from the buffer that @var{marker} is in,
90starting with the character directly after the marker. The marker
91position advances as characters are read. The value of point in the
92buffer has no effect when the stream is a marker.
93
94@item @var{string}
95@cindex string input stream
96The input characters are taken from @var{string}, starting at the first
97character in the string and using as many characters as required.
98
99@item @var{function}
100@cindex function input stream
101The input characters are generated by @var{function}, one character per
102call. Normally @var{function} is called with no arguments, and should
103return a character.
104
105@cindex unreading
106Occasionally @var{function} is called with one argument (always a
107character). When that happens, @var{function} should save the argument
108and arrange to return it on the next call. This is called
109@dfn{unreading} the character; it happens when the Lisp reader reads one
110character too many and wants to ``put it back where it came from''.
111
112@item @code{t}
113@cindex @code{t} input stream
114@code{t} used as a stream means that the input is read from the
115minibuffer. In fact, the minibuffer is invoked once and the text
116given by the user is made into a string that is then used as the
117input stream.
118
119@item @code{nil}
120@cindex @code{nil} input stream
121@code{nil} supplied as an input stream means to use the value of
122@code{standard-input} instead; that value is the @dfn{default input
123stream}, and must be a non-@code{nil} input stream.
124
125@item @var{symbol}
126A symbol as input stream is equivalent to the symbol's function
127definition (if any).
128@end table
129
130 Here is an example of reading from a stream which is a buffer, showing
131where point is located before and after:
132
133@example
134@group
135---------- Buffer: foo ----------
136This@point{} is the contents of foo.
137---------- Buffer: foo ----------
138@end group
139
140@group
141(read (get-buffer "foo"))
142 @result{} is
143@end group
144@group
145(read (get-buffer "foo"))
146 @result{} the
147@end group
148
149@group
150---------- Buffer: foo ----------
151This is the@point{} contents of foo.
152---------- Buffer: foo ----------
153@end group
154@end example
155
156@noindent
157Note that the first read skips a space at the beginning of the buffer.
158Reading skips any amount of whitespace preceding the significant text.
159
160 In Emacs 18, reading a symbol discarded the delimiter terminating the
161symbol. Thus, point would end up at the beginning of @samp{contents}
162rather than after @samp{the}. The Emacs 19 behavior is superior because
163it correctly handles input such as @samp{bar(foo)}, where the delimiter
164that ends one object is needed as the beginning of another object.
165
166 Here is an example of reading from a stream that is a marker,
167initialized to point at the beginning of the buffer shown. The value
168read is the symbol @code{This}.
169
170@example
171@group
172
173---------- Buffer: foo ----------
174This is the contents of foo.
175---------- Buffer: foo ----------
176@end group
177
178@group
179(setq m (set-marker (make-marker) 1 (get-buffer "foo")))
180 @result{} #<marker at 1 in foo>
181@end group
182@group
183(read m)
184 @result{} This
185@end group
186@group
187m
188 @result{} #<marker at 6 in foo> ;; @r{After the first space.}
189@end group
190@end example
191
192 Here we read from the contents of a string:
193
194@example
195@group
196(read "(When in) the course")
197 @result{} (When in)
198@end group
199@end example
200
201 The following example reads from the minibuffer. The
202prompt is: @w{@samp{Lisp expression: }}. (That is always the prompt
203used when you read from the stream @code{t}.) The user's input is shown
204following the prompt.
205
206@example
207@group
208(read t)
209 @result{} 23
210---------- Buffer: Minibuffer ----------
211Lisp expression: @kbd{23 @key{RET}}
212---------- Buffer: Minibuffer ----------
213@end group
214@end example
215
216 Finally, here is an example of a stream that is a function, named
217@code{useless-stream}. Before we use the stream, we initialize the
218variable @code{useless-list} to a list of characters. Then each call to
219the function @code{useless-stream} obtains the next characters in the list
220or unreads a character by adding it to the front of the list.
221
222@example
223@group
224(setq useless-list (append "XY()" nil))
225 @result{} (88 89 40 41)
226@end group
227
228@group
229(defun useless-stream (&optional unread)
230 (if unread
231 (setq useless-list (cons unread useless-list))
232 (prog1 (car useless-list)
233 (setq useless-list (cdr useless-list)))))
234 @result{} useless-stream
235@end group
236@end example
237
238@noindent
239Now we read using the stream thus constructed:
240
241@example
242@group
243(read 'useless-stream)
244 @result{} XY
245@end group
246
247@group
248useless-list
249 @result{} (41)
250@end group
251@end example
252
253@noindent
254Note that the close parenthesis remains in the list. The reader has
255read it, discovered that it ended the input, and unread it. Another
256attempt to read from the stream at this point would get an error due to
257the unmatched close parenthesis.
258
259@defun get-file-char
260This function is used internally as an input stream to read from the
261input file opened by the function @code{load}. Don't use this function
262yourself.
263@end defun
264
265@node Input Functions
266@section Input Functions
267
268 This section describes the Lisp functions and variables that pertain
269to reading.
270
271 In the functions below, @var{stream} stands for an input stream (see
272the previous section). If @var{stream} is @code{nil} or omitted, it
273defaults to the value of @code{standard-input}.
274
275@kindex end-of-file
276 An @code{end-of-file} error is signaled if reading encounters an
277unterminated list, vector or string.
278
279@defun read &optional stream
280This function reads one textual Lisp expression from @var{stream},
281returning it as a Lisp object. This is the basic Lisp input function.
282@end defun
283
284@defun read-from-string string &optional start end
285@cindex string to object
286This function reads the first textual Lisp expression from the text in
287@var{string}. It returns a cons cell whose @sc{car} is that expression,
288and whose @sc{cdr} is an integer giving the position of the next
289remaining character in the string (i.e., the first one not read).
290
291If @var{start} is supplied, then reading begins at index @var{start} in the
292string (where the first character is at index 0). If @var{end} is also
293supplied, then reading stops at that index as if the rest of the string
294were not there.
295
296For example:
297
298@example
299@group
300(read-from-string "(setq x 55) (setq y 5)")
301 @result{} ((setq x 55) . 11)
302@end group
303@group
304(read-from-string "\"A short string\"")
305 @result{} ("A short string" . 16)
306@end group
307
308@group
309;; @r{Read starting at the first character.}
310(read-from-string "(list 112)" 0)
311 @result{} ((list 112) . 10)
312@end group
313@group
314;; @r{Read starting at the second character.}
315(read-from-string "(list 112)" 1)
316 @result{} (list . 6)
317@end group
318@group
319;; @r{Read starting at the seventh character,}
320;; @r{and stopping at the ninth.}
321(read-from-string "(list 112)" 6 8)
322 @result{} (11 . 8)
323@end group
324@end example
325@end defun
326
327@defvar standard-input
328This variable holds the default input stream---the stream that
329@code{read} uses when the @var{stream} argument is @code{nil}.
330@end defvar
331
332@node Output Streams
333@section Output Streams
334@cindex stream (for printing)
335@cindex output stream
336
337 An output stream specifies what to do with the characters produced
338by printing. Most print functions accept an output stream as an
339optional argument. Here are the possible types of output stream:
340
341@table @asis
342@item @var{buffer}
343@cindex buffer output stream
344The output characters are inserted into @var{buffer} at point.
345Point advances as characters are inserted.
346
347@item @var{marker}
348@cindex marker output stream
349The output characters are inserted into the buffer that @var{marker}
350points into, at the marker position. The position advances as
351characters are inserted. The value of point in the buffer has no effect
352on printing when the stream is a marker.
353
354@item @var{function}
355@cindex function output stream
356The output characters are passed to @var{function}, which is responsible
357for storing them away. It is called with a single character as
358argument, as many times as there are characters to be output, and is
359free to do anything at all with the characters it receives.
360
361@item @code{t}
362@cindex @code{t} output stream
363The output characters are displayed in the echo area.
364
365@item @code{nil}
366@cindex @code{nil} output stream
367@code{nil} specified as an output stream means to the value of
368@code{standard-output} instead; that value is the @dfn{default output
369stream}, and must be a non-@code{nil} output stream.
370
371@item @var{symbol}
372A symbol as output stream is equivalent to the symbol's function
373definition (if any).
374@end table
375
376 Here is an example of a buffer used as an output stream. Point is
377initially located as shown immediately before the @samp{h} in
378@samp{the}. At the end, point is located directly before that same
379@samp{h}.
380
381@cindex print example
382@example
383@group
384---------- Buffer: foo ----------
385This is t@point{}he contents of foo.
386---------- Buffer: foo ----------
387@end group
388
389(print "This is the output" (get-buffer "foo"))
390 @result{} "This is the output"
391
392@group
393---------- Buffer: foo ----------
394This is t
395"This is the output"
396@point{}he contents of foo.
397---------- Buffer: foo ----------
398@end group
399@end example
400
401 Now we show a use of a marker as an output stream. Initially, the
402marker points in buffer @code{foo}, between the @samp{t} and the
403@samp{h} in the word @samp{the}. At the end, the marker has been
404advanced over the inserted text so that it still points before the same
405@samp{h}. Note that the location of point, shown in the usual fashion,
406has no effect.
407
408@example
409@group
410---------- Buffer: foo ----------
411"This is the @point{}output"
412---------- Buffer: foo ----------
413@end group
414
415@group
416m
417 @result{} #<marker at 11 in foo>
418@end group
419
420@group
421(print "More output for foo." m)
422 @result{} "More output for foo."
423@end group
424
425@group
426---------- Buffer: foo ----------
427"This is t
428"More output for foo."
429he @point{}output"
430---------- Buffer: foo ----------
431@end group
432
433@group
434m
435 @result{} #<marker at 35 in foo>
436@end group
437@end example
438
439 The following example shows output to the echo area:
440
441@example
442@group
443(print "Echo Area output" t)
444 @result{} "Echo Area output"
445---------- Echo Area ----------
446"Echo Area output"
447---------- Echo Area ----------
448@end group
449@end example
450
451 Finally, we show the use of a function as an output stream. The
452function @code{eat-output} takes each character that it is given and
453conses it onto the front of the list @code{last-output} (@pxref{Building
454Lists}). At the end, the list contains all the characters output, but
455in reverse order.
456
457@example
458@group
459(setq last-output nil)
460 @result{} nil
461@end group
462
463@group
464(defun eat-output (c)
465 (setq last-output (cons c last-output)))
466 @result{} eat-output
467@end group
468
469@group
470(print "This is the output" 'eat-output)
471 @result{} "This is the output"
472@end group
473
474@group
475last-output
476 @result{} (10 34 116 117 112 116 117 111 32 101 104
477 116 32 115 105 32 115 105 104 84 34 10)
478@end group
479@end example
480
481@noindent
482Now we can put the output in the proper order by reversing the list:
483
484@example
485@group
486(concat (nreverse last-output))
487 @result{} "
488\"This is the output\"
489"
490@end group
491@end example
492
493@node Output Functions
494@section Output Functions
495
496 This section describes the Lisp functions for printing Lisp objects.
497
498@cindex @samp{"} in printing
499@cindex @samp{\} in printing
500@cindex quoting characters in printing
501@cindex escape characters in printing
502 Some of the Emacs printing functions add quoting characters to the
503output when necessary so that it can be read properly. The quoting
504characters used are @samp{"} and @samp{\}; they distinguish strings from
505symbols, and prevent punctuation characters in strings and symbols from
506being taken as delimiters. @xref{Printed Representation}, for full
507details. You specify quoting or no quoting by the choice of printing
508function.
509
510 If the text is to be read back into Lisp, then it is best to print
511with quoting characters to avoid ambiguity. Likewise, if the purpose is
512to describe a Lisp object clearly for a Lisp programmer. However, if
513the purpose of the output is to look nice for humans, then it is better
514to print without quoting.
515
516 Printing a self-referent Lisp object requires an infinite amount of
517text. In certain cases, trying to produce this text leads to a stack
518overflow. Emacs detects such recursion and prints @samp{#@var{level}}
519instead of recursively printing an object already being printed. For
520example, here @samp{#0} indicates a recursive reference to the object at
521level 0 of the current print operation:
522
523@example
524(setq foo (list nil))
525 @result{} (nil)
526(setcar foo foo)
527 @result{} (#0)
528@end example
529
530 In the functions below, @var{stream} stands for an output stream.
531(See the previous section for a description of output streams.) If
532@var{stream} is @code{nil} or omitted, it defaults to the value of
533@code{standard-output}.
534
535@defun print object &optional stream
536@cindex Lisp printer
537The @code{print} function is a convenient way of printing. It outputs
538the printed representation of @var{object} to @var{stream}, printing in
539addition one newline before @var{object} and another after it. Quoting
540characters are used. @code{print} returns @var{object}. For example:
541
542@example
543@group
544(progn (print 'The\ cat\ in)
545 (print "the hat")
546 (print " came back"))
547 @print{}
548 @print{} The\ cat\ in
549 @print{}
550 @print{} "the hat"
551 @print{}
552 @print{} " came back"
553 @print{}
554 @result{} " came back"
555@end group
556@end example
557@end defun
558
559@defun prin1 object &optional stream
560This function outputs the printed representation of @var{object} to
561@var{stream}. It does not print any spaces or newlines to separate
562output as @code{print} does, but it does use quoting characters just
563like @code{print}. It returns @var{object}.
564
565@example
566@group
567(progn (prin1 'The\ cat\ in)
568 (prin1 "the hat")
569 (prin1 " came back"))
570 @print{} The\ cat\ in"the hat"" came back"
571 @result{} " came back"
572@end group
573@end example
574@end defun
575
576@defun princ object &optional stream
577This function outputs the printed representation of @var{object} to
578@var{stream}. It returns @var{object}.
579
580This function is intended to produce output that is readable by people,
581not by @code{read}, so it doesn't insert quoting characters and doesn't
582put double-quotes around the contents of strings. It does not add any
583spacing between calls.
584
585@example
586@group
587(progn
588 (princ 'The\ cat)
589 (princ " in the \"hat\""))
590 @print{} The cat in the "hat"
591 @result{} " in the \"hat\""
592@end group
593@end example
594@end defun
595
596@defun terpri &optional stream
597@cindex newline in print
598This function outputs a newline to @var{stream}. The name stands
599for ``terminate print''.
600@end defun
601
602@defun write-char character &optional stream
603This function outputs @var{character} to @var{stream}. It returns
604@var{character}.
605@end defun
606
607@defun prin1-to-string object &optional noescape
608@cindex object to string
609This function returns a string containing the text that @code{prin1}
610would have printed for the same argument.
611
612@example
613@group
614(prin1-to-string 'foo)
615 @result{} "foo"
616@end group
617@group
618(prin1-to-string (mark-marker))
619 @result{} "#<marker at 2773 in strings.texi>"
620@end group
621@end example
622
623If @var{noescape} is non-@code{nil}, that inhibits use of quoting
624characters in the output. (This argument is supported in Emacs versions
62519 and later.)
626
627@example
628@group
629(prin1-to-string "foo")
630 @result{} "\"foo\""
631@end group
632@group
633(prin1-to-string "foo" t)
634 @result{} "foo"
635@end group
636@end example
637
638See @code{format}, in @ref{String Conversion}, for other ways to obtain
639the printed representation of a Lisp object as a string.
640@end defun
641
642@node Output Variables
643@section Variables Affecting Output
644
645@defvar standard-output
646The value of this variable is the default output stream---the stream
647that print functions use when the @var{stream} argument is @code{nil}.
648@end defvar
649
650@defvar print-escape-newlines
651@cindex @samp{\n} in print
652@cindex escape characters
653If this variable is non-@code{nil}, then newline characters in strings
654are printed as @samp{\n} and formfeeds are printed as @samp{\f}.
655Normally these characters are printed as actual newlines and formfeeds.
656
657This variable affects the print functions @code{prin1} and @code{print},
658as well as everything that uses them. It does not affect @code{princ}.
659Here is an example using @code{prin1}:
660
661@example
662@group
663(prin1 "a\nb")
664 @print{} "a
665 @print{} b"
666 @result{} "a
667b"
668@end group
669
670@group
671(let ((print-escape-newlines t))
672 (prin1 "a\nb"))
673 @print{} "a\nb"
674 @result{} "a
675b"
676@end group
677@end example
678
679@noindent
680In the second expression, the local binding of
681@code{print-escape-newlines} is in effect during the call to
682@code{prin1}, but not during the printing of the result.
683@end defvar
684
685@defvar print-length
686@cindex printing limits
687The value of this variable is the maximum number of elements of a list
688that will be printed. If a list being printed has more than this many
689elements, then it is abbreviated with an ellipsis.
690
691If the value is @code{nil} (the default), then there is no limit.
692
693@example
694@group
695(setq print-length 2)
696 @result{} 2
697@end group
698@group
699(print '(1 2 3 4 5))
700 @print{} (1 2 ...)
701 @result{} (1 2 ...)
702@end group
703@end example
704@end defvar
705
706@defvar print-level
707The value of this variable is the maximum depth of nesting of
708parentheses that will be printed. Any list or vector at a depth
709exceeding this limit is abbreviated with an ellipsis. A value of
710@code{nil} (which is the default) means no limit.
711
712This variable exists in version 19 and later versions.
713@end defvar