annotate lisp/edebug/cust-print.el @ 72:b9518feda344 r20-0b31

Import from CVS: tag r20-0b31
author cvs
date Mon, 13 Aug 2007 09:03:46 +0200
parents 131b0175ea99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; cust-print.el --- handles print-level and print-circle.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Adapted-By: ESR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Keywords: extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
9 ;; This file is part of XEmacs.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
10
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
12 ;; under the terms of the GNU General Public License as published by
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
14 ;; any later version.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
15
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
19 ;; General Public License for more details.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
20
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
21 ;; You should have received a copy of the GNU General Public License
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
24 ;; 02111-1307, USA.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
25
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
26 ;;; Synched up with: Not in FSF
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
27
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
28 ;; LCD Archive Entry:
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
29 ;; cust-print|Daniel LaLiberte|liberte@cs.uiuc.edu
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
30 ;; |Handle print-level, print-circle and more.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
31 ;; |$Date: 1996/12/18 22:51:45 $|$Revision: 1.1.1.2 $|
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
33 ;; ===============================
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
34 ;; $Header: /afs/informatik.uni-tuebingen.de/local/web/xemacs/xemacs-cvs/XEmacs/xemacs/lisp/edebug/Attic/cust-print.el,v 1.1.1.2 1996/12/18 22:51:45 steve Exp $
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
35 ;; $Log: cust-print.el,v $
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
36 ;; Revision 1.1.1.2 1996/12/18 22:51:45 steve
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
37 ;; XEmacs 20.0 -- Beta 31
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
38 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
39 ;; Revision 1.4 1994/03/23 20:34:29 liberte
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
40 ;; * Change "emacs" to "original" - I just can't decide.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
41 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
42 ;; Revision 1.3 1994/02/21 21:25:36 liberte
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
43 ;; * Make custom-prin1-to-string more robust when errors occur.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
44 ;; * Change "internal" to "emacs".
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
45 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
46 ;; Revision 1.2 1993/11/22 22:36:36 liberte
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
47 ;; * Simplified and generalized printer customization.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
48 ;; custom-printers is an alist of (PREDICATE . PRINTER) pairs
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
49 ;; for any data types. The PRINTER function should print to
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
50 ;; `standard-output' add-custom-printer and delete-custom-printer
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
51 ;; change custom-printers.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
52 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
53 ;; * Installation function now called install-custom-print. The
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
54 ;; old name is still around for now.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
55 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
56 ;; * New macro with-custom-print (added earlier) - executes like
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
57 ;; progn but with custom-print activated temporarily.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
58 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
59 ;; * Cleaned up comments for replacements of standardard printers.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
60 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
61 ;; * Changed custom-prin1-to-string to use a temporary buffer.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
62 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
63 ;; * Internal symbols are prefixed with CP::.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
64 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
65 ;; * Option custom-print-vectors (added earlier) - controls whether
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
66 ;; vectors should be printed according to print-length and
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
67 ;; print-length. Emacs doesnt do this, but cust-print would
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
68 ;; otherwise do it only if custom printing is required.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
69 ;;
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
70 ;; * Uninterned symbols are treated as non-read-equivalent.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
71 ;;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;; This package provides a general print handler for prin1 and princ
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;; that supports print-level and print-circle, and by the way,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ;; print-length since the standard routines are being replaced. Also,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 ;; to print custom types constructed from lists and vectors, use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ;; custom-print-list and custom-print-vector. See the documentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;; strings of these variables for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;; If the results of your expressions contain circular references to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 ;; other parts of the same structure, the standard Emacs print
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 ;; subroutines may fail to print with an untrappable error,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 ;; "Apparently circular structure being printed". If you only use cdr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 ;; circular lists (where cdrs of lists point back; what is the right
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 ;; term here?), you can limit the length of printing with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 ;; print-length. But car circular lists and circular vectors generate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 ;; the above mentioned error in Emacs version 18. Version
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 ;; 19 supports print-level, but it is often useful to get a better
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 ;; print representation of circular and shared structures; the print-circle
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 ;; option may be used to print more concise representations.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 ;; There are three main ways to use this package. First, you may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ;; replace prin1, princ, and some subroutines that use them by calling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ;; install-custom-print so that any use of these functions in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 ;; Lisp code will be affected; you can later reset with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;; uninstall-custom-print. Second, you may temporarily install
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;; these functions with the macro with-custom-print. Third, you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;; could call the custom routines directly, thus only affecting the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 ;; printing that requires them.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ;; Note that subroutines which call print subroutines directly will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 ;; not use the custom print functions. In particular, the evaluation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ;; functions like eval-region call the print subroutines directly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ;; Therefore, if you evaluate (aref circ-list 0), where circ-list is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ;; circular list rather than an array, aref calls error directly which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 ;; will jump to the top level instead of printing the circular list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 ;; Uninterned symbols are recognized when print-circle is non-nil,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 ;; but they are not printed specially here. Use the cl-packages package
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ;; to print according to print-gensym.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 ;; Obviously the right way to implement this custom-print facility is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 ;; in C or with hooks into the standard printer. Please volunteer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;; since I don't have the time or need. More CL-like printing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 ;; capabilities could be added in the future.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;; Implementation design: we want to use the same list and vector
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;; processing algorithm for all versions of prin1 and princ, since how
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 ;; the processing is done depends on print-length, print-level, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 ;; print-circle. For circle printing, a preprocessing step is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 ;; required before the final printing. Thanks to Jamie Zawinski
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 ;; for motivation and algorithms.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;;=========================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ;; If using cl-packages:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 '(defpackage "cust-print"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (:nicknames "CP" "custom-print")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (:use "el")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (:export
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 print-level
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 print-circle
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 install-custom-print
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 uninstall-custom-print
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 custom-print-installed-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 with-custom-print
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 custom-prin1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 custom-princ
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 custom-prin1-to-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 custom-print
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 custom-format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 custom-message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 custom-error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 custom-printers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 add-custom-printer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 '(in-package cust-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (require 'backquote)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ;; Emacs 18 doesnt have defalias.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 ;; Provide def for byte compiler.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (defun defalias (symbol func) (fset symbol func))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 ;; Better def when loaded.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (or (fboundp 'defalias) (fset 'defalias 'fset))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 ;; Variables:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 ;;=========================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 ;;(defvar print-length nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 ;; "*Controls how many elements of a list, at each level, are printed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 ;;This is defined by emacs.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (defvar print-level nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 "*Controls how many levels deep a nested data object will print.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 If nil, printing proceeds recursively and may lead to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 max-lisp-eval-depth being exceeded or an error may occur:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 `Apparently circular structure being printed.'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 Also see `print-length' and `print-circle'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 If non-nil, components at levels equal to or greater than `print-level'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 are printed simply as `#'. The object to be printed is at level 0,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 and if the object is a list or vector, its top-level components are at
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 level 1.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (defvar print-circle nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 "*Controls the printing of recursive structures.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 If nil, printing proceeds recursively and may lead to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 `max-lisp-eval-depth' being exceeded or an error may occur:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 \"Apparently circular structure being printed.\" Also see
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 `print-length' and `print-level'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 If non-nil, shared substructures anywhere in the structure are printed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 with `#N=' before the first occurrence (in the order of the print
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 representation) and `#N#' in place of each subsequent occurrence,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 where N is a positive decimal integer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 There is no way to read this representation in standard Emacs,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 but if you need to do so, try the cl-read.el package.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (defvar custom-print-vectors nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 "*Non-nil if printing of vectors should obey print-level and print-length.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 For Emacs 18, setting print-level, or adding custom print list or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 vector handling will make this happen anyway. Emacs 19 obeys
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 print-level, but not for vectors.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 ;; Custom printers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 ;;==========================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (defconst custom-printers nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 ;; e.g. '((symbolp . pkg::print-symbol))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 "An alist for custom printing of any type.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 Pairs are of the form (PREDICATE . PRINTER). If PREDICATE is true
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 for an object, then PRINTER is called with the object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 PRINTER should print to `standard-output' using CP::original-princ
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 if the standard printer is sufficient, or CP::prin for complex things.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 The PRINTER should return the object being printed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 Don't modify this variable directly. Use `add-custom-printer' and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 `delete-custom-printer'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 ;; Should CP::original-princ and CP::prin be exported symbols?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 ;; Or should the standard printers functions be replaced by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 ;; CP ones in elisp so that CP internal functions need not be called?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 (defun add-custom-printer (pred printer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 "Add a pair of PREDICATE and PRINTER to `custom-printers'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 Any pair that has the same PREDICATE is first removed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (setq custom-printers (cons (cons pred printer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (delq (assq pred custom-printers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 custom-printers)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 ;; Rather than updating here, we could wait until CP::top-level is called.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (CP::update-custom-printers))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (defun delete-custom-printer (pred)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 "Delete the custom printer associated with PREDICATE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (setq custom-printers (delq (assq pred custom-printers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 custom-printers))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (CP::update-custom-printers))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (defun CP::use-custom-printer (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 ;; Default function returns nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (defun CP::update-custom-printers ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 ;; Modify the definition of CP::use-custom-printer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (defalias 'CP::use-custom-printer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 ;; We dont really want to require the byte-compiler.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 ;; (byte-compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (` (lambda (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (,@ (mapcar (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (lambda (pair)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (` (((, (car pair)) object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 ((, (cdr pair)) object)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 custom-printers))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 ;; Otherwise return nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (t nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 ;; )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 ;; Saving and restoring emacs printing routines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 ;;====================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (defun CP::set-function-cell (symbol-pair)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (defalias (car symbol-pair)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (symbol-function (car (cdr symbol-pair)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 (defun CP::original-princ (object &optional stream)) ; dummy def
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 ;; Save emacs routines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (if (not (fboundp 'CP::original-prin1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (mapcar 'CP::set-function-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 '((CP::original-prin1 prin1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (CP::original-princ princ)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (CP::original-print print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (CP::original-prin1-to-string prin1-to-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (CP::original-format format)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (CP::original-message message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (CP::original-error error))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (defalias 'install-custom-print-funcs 'install-custom-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (defun install-custom-print ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 "Replace print functions with general, customizable, Lisp versions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 The emacs subroutines are saved away, and you can reinstall them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 by running `uninstall-custom-print'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (mapcar 'CP::set-function-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 '((prin1 custom-prin1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (princ custom-princ)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (print custom-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 (prin1-to-string custom-prin1-to-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 (format custom-format)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (message custom-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (error custom-error)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 (defalias 'uninstall-custom-print-funcs 'uninstall-custom-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (defun uninstall-custom-print ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 "Reset print functions to their emacs subroutines."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (mapcar 'CP::set-function-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 '((prin1 CP::original-prin1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (princ CP::original-princ)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (print CP::original-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 (prin1-to-string CP::original-prin1-to-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 (format CP::original-format)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 (message CP::original-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (error CP::original-error)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (defalias 'custom-print-funcs-installed-p 'custom-print-installed-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (defun custom-print-installed-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 "Return t if custom-print is currently installed, nil otherwise."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (eq (symbol-function 'custom-prin1) (symbol-function 'prin1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (put 'with-custom-print-funcs 'edebug-form-spec '(body))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (put 'with-custom-print 'edebug-form-spec '(body))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (defalias 'with-custom-print-funcs 'with-custom-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (defmacro with-custom-print (&rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 "Temporarily install the custom print package while executing BODY."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (` (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (install-custom-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (,@ body))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (uninstall-custom-print))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 ;; Lisp replacements for prin1 and princ, and for some subrs that use them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 ;;===============================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 ;; - so far only the printing and formatting subrs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (defun custom-prin1 (object &optional stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 "Output the printed representation of OBJECT, any Lisp object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 Quoting characters are printed when needed to make output that `read'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 can handle, whenever this is possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 Output stream is STREAM, or value of `standard-output' (which see).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 This is the custom-print replacement for the standard `prin1'. It
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 uses the appropriate printer depending on the values of `print-level'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 and `print-circle' (which see)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (CP::top-level object stream 'CP::original-prin1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 (defun custom-princ (object &optional stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 "Output the printed representation of OBJECT, any Lisp object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 No quoting characters are used; no delimiters are printed around
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 the contents of strings.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 Output stream is STREAM, or value of `standard-output' (which see).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 This is the custom-print replacement for the standard `princ'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (CP::top-level object stream 'CP::original-princ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 (defun custom-prin1-to-string (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 "Return a string containing the printed representation of OBJECT,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 any Lisp object. Quoting characters are used when needed to make output
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 that `read' can handle, whenever this is possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 This is the custom-print replacement for the standard `prin1-to-string'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (let ((buf (get-buffer-create " *custom-print-temp*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 ;; We must erase the buffer before printing in case an error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 ;; occured during the last prin1-to-string and we are in debugger.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (set-buffer buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (erase-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 ;; We must be in the current-buffer when the print occurs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 (custom-prin1 object buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (set-buffer buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (buffer-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 ;; We could erase the buffer again, but why bother?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 (defun custom-print (object &optional stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 "Output the printed representation of OBJECT, with newlines around it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 Quoting characters are printed when needed to make output that `read'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 can handle, whenever this is possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 Output stream is STREAM, or value of `standard-output' (which see).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 This is the custom-print replacement for the standard `print'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 (CP::original-princ "\n" stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 (custom-prin1 object stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 (CP::original-princ "\n" stream))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 (defun custom-format (fmt &rest args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 "Format a string out of a control-string and arguments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 The first argument is a control string. It, and subsequent arguments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 substituted into it, become the value, which is a string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 It may contain %s or %d or %c to substitute successive following arguments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 %s means print an argument as a string, %d means print as number in decimal,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 %c means print a number as a single character.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 The argument used by %s must be a string or a symbol;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 the argument used by %d, %b, %o, %x or %c must be a number.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 This is the custom-print replacement for the standard `format'. It
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 calls the emacs `format' after first making strings for list,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 vector, or symbol args. The format specification for such args should
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 be `%s' in any case, so a string argument will also work. The string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 is generated with `custom-prin1-to-string', which quotes quotable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 characters."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 (apply 'CP::original-format fmt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 (mapcar (function (lambda (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 (if (or (listp arg) (vectorp arg) (symbolp arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 (custom-prin1-to-string arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 args)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (defun custom-message (fmt &rest args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 "Print a one-line message at the bottom of the screen.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 The first argument is a control string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 It may contain %s or %d or %c to print successive following arguments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 %s means print an argument as a string, %d means print as number in decimal,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 %c means print a number as a single character.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 The argument used by %s must be a string or a symbol;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 the argument used by %d or %c must be a number.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 This is the custom-print replacement for the standard `message'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 See `custom-format' for the details."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 ;; It doesn't work to princ the result of custom-format as in:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 ;; (CP::original-princ (apply 'custom-format fmt args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 ;; because the echo area requires special handling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 ;; to avoid duplicating the output.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 ;; CP::original-message does it right.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 (apply 'CP::original-message fmt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 (mapcar (function (lambda (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 (if (or (listp arg) (vectorp arg) (symbolp arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 (custom-prin1-to-string arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 args)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 (defun custom-error (fmt &rest args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 "Signal an error, making error message by passing all args to `format'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 This is the custom-print replacement for the standard `error'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 See `custom-format' for the details."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (signal 'error (list (apply 'custom-format fmt args))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 ;; Support for custom prin1 and princ
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 ;;=========================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 ;; Defs to quiet byte-compiler.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 (defvar circle-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 (defvar CP::current-level)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (defun CP::original-printer (object)) ; One of the standard printers.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 (defun CP::low-level-prin (object)) ; Used internally.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 (defun CP::prin (object)) ; Call this to print recursively.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 (defun CP::top-level (object stream emacs-printer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 ;; Set up for printing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 (let ((standard-output (or stream standard-output))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 ;; circle-table will be non-nil if anything is circular.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 (circle-table (and print-circle
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 (CP::preprocess-circle-tree object)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 (CP::current-level (or print-level -1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 (defalias 'CP::original-printer emacs-printer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 (defalias 'CP::low-level-prin
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 ((or custom-printers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 circle-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 print-level ; comment out for version 19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 ;; Emacs doesn't use print-level or print-length
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 ;; for vectors, but custom-print can.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 (if custom-print-vectors
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 (or print-level print-length)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 'CP::print-object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 (t 'CP::original-printer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 (defalias 'CP::prin
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 (if circle-table 'CP::print-circular 'CP::low-level-prin))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 (CP::prin object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 (defun CP::print-object (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 ;; Test object type and print accordingly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 ;; Could be called as either CP::low-level-prin or CP::prin.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 ((null object) (CP::original-printer object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 ((CP::use-custom-printer object) object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 ((consp object) (CP::list object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 ((vectorp object) (CP::vector object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 ;; All other types, just print.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 (t (CP::original-printer object))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 (defun CP::print-circular (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 ;; Printer for `prin1' and `princ' that handles circular structures.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 ;; If OBJECT appears multiply, and has not yet been printed,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 ;; prefix with label; if it has been printed, use `#N#' instead.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 ;; Otherwise, print normally.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 (let ((tag (assq object circle-table)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 (if tag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 (let ((id (cdr tag)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 (if (> id 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 ;; Already printed, so just print id.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 (CP::original-princ "#")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 (CP::original-princ id)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 (CP::original-princ "#"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 ;; Not printed yet, so label with id and print object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 (setcdr tag (- id)) ; mark it as printed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 (CP::original-princ "#")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 (CP::original-princ (- id))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 (CP::original-princ "=")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 (CP::low-level-prin object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 ;; Not repeated in structure.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 (CP::low-level-prin object))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 ;;================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 ;; List and vector processing for print functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 (defun CP::list (list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 ;; Print a list using print-length, print-level, and print-circle.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 (if (= CP::current-level 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (CP::original-princ "#")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 (let ((CP::current-level (1- CP::current-level)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 (CP::original-princ "(")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 (let ((length (or print-length 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 ;; Print the first element always (even if length = 0).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 (CP::prin (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 (setq list (cdr list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 (if list (CP::original-princ " "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 (setq length (1- length))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 ;; Print the rest of the elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 (while (and list (/= 0 length))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 (if (and (listp list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 (not (assq list circle-table)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 (CP::prin (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 (setq list (cdr list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 ;; cdr is not a list, or it is in circle-table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 (CP::original-princ ". ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 (CP::prin list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 (setq list nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 (setq length (1- length))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 (if list (CP::original-princ " ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 (if (and list (= length 0)) (CP::original-princ "..."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 (CP::original-princ ")"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 (defun CP::vector (vector)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 ;; Print a vector according to print-length, print-level, and print-circle.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 (if (= CP::current-level 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 (CP::original-princ "#")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 (let ((CP::current-level (1- CP::current-level))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 (i 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 (len (length vector)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 (CP::original-princ "[")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 (if print-length
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 (setq len (min print-length len)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 ;; Print the elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 (while (< i len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 (CP::prin (aref vector i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 (setq i (1+ i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 (if (< i (length vector)) (CP::original-princ " ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 (if (< i (length vector)) (CP::original-princ "..."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 (CP::original-princ "]")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 vector)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 ;; Circular structure preprocessing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 ;;==================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 (defun CP::preprocess-circle-tree (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 ;; Fill up the table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 (let (;; Table of tags for each object in an object to be printed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 ;; A tag is of the form:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 ;; ( <object> <nil-t-or-id-number> )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 ;; The id-number is generated after the entire table has been computed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 ;; During walk through, the real circle-table lives in the cdr so we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 ;; can use setcdr to add new elements instead of having to setq the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 ;; variable sometimes (poor man's locf).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 (circle-table (list nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 (CP::walk-circle-tree object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 ;; Reverse table so it is in the order that the objects will be printed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 ;; This pass could be avoided if we always added to the end of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 ;; table with setcdr in walk-circle-tree.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 (setcdr circle-table (nreverse (cdr circle-table)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 ;; Walk through the table, assigning id-numbers to those
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 ;; objects which will be printed using #N= syntax. Delete those
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 ;; objects which will be printed only once (to speed up assq later).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 (let ((rest circle-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 (id -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 (while (cdr rest)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 (let ((tag (car (cdr rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 (cond ((cdr tag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 (setcdr tag id)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 (setq id (1- id))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 (setq rest (cdr rest)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 ;; Else delete this object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 (t (setcdr rest (cdr (cdr rest))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 ;; Drop the car.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 (cdr circle-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 (defun CP::walk-circle-tree (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 (let (read-equivalent-p tag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 (while object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 (setq read-equivalent-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 (or (numberp object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 (and (symbolp object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 ;; Check if it is uninterned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 (eq object (intern-soft (symbol-name object)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 tag (and (not read-equivalent-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 (assq object (cdr circle-table))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 (cond (tag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 ;; Seen this object already, so note that.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 (setcdr tag t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 ((not read-equivalent-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 ;; Add a tag for this object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 (setcdr circle-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 (cons (list object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 (cdr circle-table)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 (setq object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 (tag ;; No need to descend since we have already.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 ((consp object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 ;; Walk the car of the list recursively.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 (CP::walk-circle-tree (car object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 ;; But walk the cdr with the above while loop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 ;; to avoid problems with max-lisp-eval-depth.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 ;; And it should be faster than recursion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 (cdr object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 ((vectorp object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 ;; Walk the vector.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 (let ((i (length object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 (j 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 (while (< j i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 (CP::walk-circle-tree (aref object j))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 (setq j (1+ j))))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 ;; Example.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677 ;;=======================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 '(progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 ;; Create some circular structures.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 (setq circ-sym (let ((x (make-symbol "FOO"))) (list x x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 (setq circ-list (list 'a 'b (vector 1 2 3 4) 'd 'e 'f))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 (setcar (nthcdr 3 circ-list) circ-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 (aset (nth 2 circ-list) 2 circ-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 (setq dotted-circ-list (list 'a 'b 'c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 (setcdr (cdr (cdr dotted-circ-list)) dotted-circ-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 (setq circ-vector (vector 1 2 3 4 (list 'a 'b 'c 'd) 6 7))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 (aset circ-vector 5 (make-symbol "-gensym-"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 (setcar (cdr (aref circ-vector 4)) (aref circ-vector 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 (install-custom-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 ;; (setq print-circle t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 (let ((print-circle t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 (or (equal (prin1-to-string circ-list) "#1=(a b [1 2 #1# 4] #1# e f)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 (error "circular object with array printing")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 (let ((print-circle t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 (or (equal (prin1-to-string dotted-circ-list) "#1=(a b c . #1#)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 (error "circular object with array printing")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 (let* ((print-circle t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 (x (list 'p 'q))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 (y (list (list 'a 'b) x 'foo x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 (setcdr (cdr (cdr (cdr y))) (cdr y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 (or (equal (prin1-to-string y) "((a b) . #1=(#2=(p q) foo #2# . #1#))"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 (error "circular list example from CL manual")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 (let ((print-circle nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 ;; cl-packages.el is required to print uninterned symbols like #:FOO.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 ;; (require 'cl-packages)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 (or (equal (prin1-to-string circ-sym) "(#:FOO #:FOO)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 (error "uninterned symbols in list")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 (let ((print-circle t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 (or (equal (prin1-to-string circ-sym) "(#1=FOO #1#)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 (error "circular uninterned symbols in list")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 (uninstall-custom-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 (provide 'cust-print)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 ;;; cust-print.el ends here