Mercurial > emacs
annotate lisp/emacs-lisp/cust-print.el @ 1365:20c84bc5ad97
(hack-local-variables): Ignore attempts to bind enable-local-eval.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 08 Oct 1992 06:14:35 +0000 |
parents | 96c43cee31f1 |
children | ebf903dc2d70 |
rev | line source |
---|---|
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
1 ;; cust-print.el -- handles print-level and print-circle. |
655 | 2 |
845 | 3 ;; Copyright (C) 1992 Free Software Foundation, Inc. |
4 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
5 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu> |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
6 ;; Version: 1.0 |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
7 ;; Adapted-By: ESR |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
8 ;; Keyword: extensions |
655 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
14 ;; the Free Software Foundation; either version 2, or (at your option) |
655 | 15 ;; any later version. |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
26 ;;; Commentary: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
27 |
655 | 28 ;; This package provides a general print handler for prin1 and princ |
29 ;; that supports print-level and print-circle, and by the way, | |
30 ;; print-length since the standard routines are being replaced. Also, | |
31 ;; to print custom types constructed from lists and vectors, use | |
32 ;; custom-print-list and custom-print-vector. See the documentation | |
33 ;; strings of these variables for more details. | |
34 | |
35 ;; If the results of your expressions contain circular references to | |
36 ;; other parts of the same structure, the standard Emacs print | |
37 ;; subroutines may fail to print with an untrappable error, | |
38 ;; "Apparently circular structure being printed". If you only use cdr | |
39 ;; circular lists (where cdrs of lists point back; what is the right | |
40 ;; term here?), you can limit the length of printing with | |
41 ;; print-length. But car circular lists and circular vectors generate | |
42 ;; the above mentioned untrappable error in Emacs version 18. Version | |
43 ;; 19 will support print-level, but it is often useful to get a better | |
44 ;; print representation of circular structures; the print-circle | |
45 ;; option may be used to print more concise representations. | |
46 | |
47 ;; There are two main ways to use this package. First, you may | |
48 ;; replace prin1, princ, and some subroutines that use them by calling | |
49 ;; install-custom-print-funcs so that any use of these functions in | |
50 ;; lisp code will be affected. Second, you could call the custom | |
51 ;; routines directly, thus only affecting the printing that requires | |
52 ;; them. | |
53 | |
54 ;; Note that subroutines which call print subroutines directly will not | |
55 ;; use the custom print functions. In particular, the evaluation | |
56 ;; functions like eval-region call the print subroutines directly. | |
57 ;; Therefore, evaluating (aref circ-list 0), which calls error | |
58 ;; directly (because circ-list is not an array), will jump to the top | |
59 ;; level instead of printing the circular list. | |
60 | |
61 ;; Obviously the right way to implement this custom-print facility | |
62 ;; is in C. Please volunteer since I don't have the time or need. | |
63 | |
64 ;; Implementation design: we want to use the same list and vector | |
65 ;; processing algorithm for all versions of prin1 and princ, since how | |
66 ;; the processing is done depends on print-length, print-level, and | |
67 ;; print-circle. For circle printing, a preprocessing step is | |
68 ;; required before the final printing. Thanks to Jamie Zawinski | |
69 ;; for motivation and algorithms. | |
70 | |
71 ;;========================================================= | |
72 ;; export list: | |
73 | |
74 ;; print-level | |
75 ;; print-circle | |
76 | |
77 ;; custom-print-list | |
78 ;; custom-print-vector | |
79 ;; add-custom-print-list | |
80 ;; add-custom-print-vector | |
81 | |
82 ;; install-custom-print-funcs | |
83 ;; uninstall-custom-print-funcs | |
84 | |
85 ;; custom-prin1 | |
86 ;; custom-princ | |
87 ;; custom-prin1-to-string | |
88 ;; custom-print | |
89 ;; custom-format | |
90 ;; custom-message | |
91 ;; custom-error | |
92 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
93 ;;; Code: |
655 | 94 |
95 (provide 'custom-print) | |
96 | |
97 ;;(defvar print-length nil | |
98 ;; "*Controls how many elements of a list, at each level, are printed. | |
99 ;;This is defined by emacs.") | |
100 | |
101 (defvar print-level nil | |
102 "*Controls how many levels deep a nested data object will print. | |
103 | |
104 If nil, printing proceeds recursively and may lead to | |
105 max-lisp-eval-depth being exceeded or an untrappable error may occur: | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
106 `Apparently circular structure being printed.' |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
107 Also see `print-length' and `print-circle'. |
655 | 108 |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
109 If non-nil, components at levels equal to or greater than `print-level' |
655 | 110 are printed simply as \"#\". The object to be printed is at level 0, |
111 and if the object is a list or vector, its top-level components are at | |
112 level 1.") | |
113 | |
114 | |
115 (defvar print-circle nil | |
116 "*Controls the printing of recursive structures. | |
117 | |
118 If nil, printing proceeds recursively and may lead to | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
119 `max-lisp-eval-depth' being exceeded or an untrappable error may occur: |
655 | 120 \"Apparently circular structure being printed.\" Also see |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
121 `print-length' and `print-level'. |
655 | 122 |
123 If non-nil, shared substructures anywhere in the structure are printed | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
124 with `#N=' before the first occurance (in the order of the print |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
125 representation) and `#N#' in place of each subsequent occurance, |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
126 where N is a positive decimal integer. |
655 | 127 |
128 Currently, there is no way to read this representation in Emacs.") | |
129 | |
130 | |
131 (defconst custom-print-list | |
132 nil | |
133 ;; e.g. '((floatp . float-to-string)) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
134 "An alist for custom printing of lists. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
135 Pairs are of the form (PRED . CONVERTER). If PREDICATE is true |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
136 for an object, then CONVERTER is called with the object and should |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
137 return a string to be printed with `princ'. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
138 Also see `custom-print-vector'.") |
655 | 139 |
140 (defconst custom-print-vector | |
141 nil | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
142 "An alist for custom printing of vectors. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
143 Pairs are of the form (PRED . CONVERTER). If PREDICATE is true |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
144 for an object, then CONVERTER is called with the object and should |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
145 return a string to be printed with `princ'. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
146 Also see `custom-print-list'.") |
655 | 147 |
148 | |
149 (defun add-custom-print-list (pred converter) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
150 "Add a pair of PREDICATE and CONVERTER to `custom-print-list'. |
655 | 151 Any pair that has the same PREDICATE is first removed." |
152 (setq custom-print-list (cons (cons pred converter) | |
153 (delq (assq pred custom-print-list) | |
154 custom-print-list)))) | |
155 ;; e.g. (add-custom-print-list 'floatp 'float-to-string) | |
156 | |
157 | |
158 (defun add-custom-print-vector (pred converter) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
159 "Add a pair of PREDICATE and CONVERTER to `custom-print-vector'. |
655 | 160 Any pair that has the same PREDICATE is first removed." |
161 (setq custom-print-vector (cons (cons pred converter) | |
162 (delq (assq pred custom-print-vector) | |
163 custom-print-vector)))) | |
164 | |
165 | |
166 ;;==================================================== | |
167 ;; Saving and restoring internal printing routines. | |
168 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
169 (defun cust-print-set-function-cell (symbol-pair) |
655 | 170 (fset (car symbol-pair) |
171 (symbol-function (car (cdr symbol-pair))))) | |
172 | |
173 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
174 (if (not (fboundp 'cust-print-internal-prin1)) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
175 (mapcar 'cust-print-set-function-cell |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
176 '((cust-print-internal-prin1 prin1) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
177 (cust-print-internal-princ princ) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
178 (cust-print-internal-print print) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
179 (cust-print-internal-prin1-to-string prin1-to-string) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
180 (cust-print-internal-format format) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
181 (cust-print-internal-message message) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
182 (cust-print-internal-error error)))) |
655 | 183 |
184 | |
185 (defun install-custom-print-funcs () | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
186 "Replace print functions with general, customizable, Lisp versions. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
187 The internal subroutines are saved away, and you can reinstall them |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
188 by running `uninstall-custom-print-funcs'." |
655 | 189 (interactive) |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
190 (mapcar 'cust-print-set-function-cell |
655 | 191 '((prin1 custom-prin1) |
192 (princ custom-princ) | |
193 (print custom-print) | |
194 (prin1-to-string custom-prin1-to-string) | |
195 (format custom-format) | |
196 (message custom-message) | |
197 (error custom-error) | |
198 ))) | |
199 | |
200 (defun uninstall-custom-print-funcs () | |
201 "Reset print functions to their internal subroutines." | |
202 (interactive) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
203 (mapcar 'cust-print-set-function-cell |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
204 '((prin1 cust-print-internal-prin1) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
205 (princ cust-print-internal-princ) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
206 (print cust-print-internal-print) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
207 (prin1-to-string cust-print-internal-prin1-to-string) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
208 (format cust-print-internal-format) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
209 (message cust-print-internal-message) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
210 (error cust-print-internal-error) |
655 | 211 ))) |
212 | |
213 | |
214 ;;=============================================================== | |
215 ;; Lisp replacements for prin1 and princ and for subrs that use prin1 | |
216 ;; (or princ) -- so far only the printing and formatting subrs. | |
217 | |
218 (defun custom-prin1 (object &optional stream) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
219 "Replacement for standard `prin1'. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
220 Uses the appropriate printer depending on the values of `print-level' |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
221 and `print-circle' (which see). |
655 | 222 |
223 Output the printed representation of OBJECT, any Lisp object. | |
224 Quoting characters are printed when needed to make output that `read' | |
225 can handle, whenever this is possible. | |
226 Output stream is STREAM, or value of `standard-output' (which see)." | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
227 (cust-print-top-level object stream 'cust-print-internal-prin1)) |
655 | 228 |
229 | |
230 (defun custom-princ (object &optional stream) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
231 "Same as `custom-prin1' except no quoting." |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
232 (cust-print-top-level object stream 'cust-print-internal-princ)) |
655 | 233 |
234 (defun custom-prin1-to-string-func (c) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
235 "Stream function for `custom-prin1-to-string'." |
655 | 236 (setq prin1-chars (cons c prin1-chars))) |
237 | |
238 (defun custom-prin1-to-string (object) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
239 "Replacement for standard `prin1-to-string'." |
655 | 240 (let ((prin1-chars nil)) |
241 (custom-prin1 object 'custom-prin1-to-string-func) | |
242 (concat (nreverse prin1-chars)))) | |
243 | |
244 | |
245 (defun custom-print (object &optional stream) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
246 "Replacement for standard `print'." |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
247 (cust-print-internal-princ "\n") |
655 | 248 (custom-prin1 object stream) |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
249 (cust-print-internal-princ "\n")) |
655 | 250 |
251 | |
252 (defun custom-format (fmt &rest args) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
253 "Replacement for standard `format'. |
655 | 254 |
255 Calls format after first making strings for list or vector args. | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
256 The format specification for such args should be `%s' in any case, so a |
655 | 257 string argument will also work. The string is generated with |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
258 `custom-prin1-to-string', which quotes quotable characters." |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
259 (apply 'cust-print-internal-format fmt |
655 | 260 (mapcar (function (lambda (arg) |
261 (if (or (listp arg) (vectorp arg)) | |
262 (custom-prin1-to-string arg) | |
263 arg))) | |
264 args))) | |
265 | |
266 | |
267 | |
268 (defun custom-message (fmt &rest args) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
269 "Replacement for standard `message' that works like `custom-format'." |
655 | 270 ;; It doesnt work to princ the result of custom-format |
271 ;; because the echo area requires special handling | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
272 ;; to avoid duplicating the output. cust-print-internal-message does it right. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
273 ;; (cust-print-internal-princ (apply 'custom-format fmt args)) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
274 (apply 'cust-print-internal-message fmt |
655 | 275 (mapcar (function (lambda (arg) |
276 (if (or (listp arg) (vectorp arg)) | |
277 (custom-prin1-to-string arg) | |
278 arg))) | |
279 args))) | |
280 | |
281 | |
282 (defun custom-error (fmt &rest args) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
283 "Replacement for standard `error' that uses `custom-format'" |
655 | 284 (signal 'error (list (apply 'custom-format fmt args)))) |
285 | |
286 | |
287 ;;========================================= | |
288 ;; Support for custom prin1 and princ | |
289 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
290 (defun cust-print-top-level (object stream internal-printer) |
655 | 291 "Set up for printing." |
292 (let ((standard-output (or stream standard-output)) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
293 (circle-table (and print-circle (cust-print-preprocess-circle-tree object))) |
655 | 294 (level (or print-level -1)) |
295 ) | |
296 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
297 (fset 'cust-print-internal-printer internal-printer) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
298 (fset 'cust-print-low-level-prin |
655 | 299 (cond |
300 ((or custom-print-list | |
301 custom-print-vector | |
302 print-level ; comment out for version 19 | |
303 ) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
304 'cust-print-custom-object) |
655 | 305 (circle-table |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
306 'cust-print-object) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
307 (t 'cust-print-internal-printer))) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
308 (fset 'cust-print-prin (if circle-table 'cust-print-circular 'cust-print-low-level-prin)) |
655 | 309 |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
310 (cust-print-prin object) |
655 | 311 object)) |
312 | |
313 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
314 ;; Test object type and print accordingly. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
315 (defun cust-print-object (object) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
316 ;; Could be called as either cust-print-low-level-prin or cust-print-prin. |
655 | 317 (cond |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
318 ((null object) (cust-print-internal-printer object)) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
319 ((consp object) (cust-print-list object)) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
320 ((vectorp object) (cust-print-vector object)) |
655 | 321 ;; All other types, just print. |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
322 (t (cust-print-internal-printer object)))) |
655 | 323 |
324 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
325 ;; Test object type and print accordingly. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
326 (defun cust-print-custom-object (object) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
327 ;; Could be called as either cust-print-low-level-prin or cust-print-prin. |
655 | 328 (cond |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
329 ((null object) (cust-print-internal-printer object)) |
655 | 330 |
331 ((consp object) | |
332 (or (and custom-print-list | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
333 (cust-print-custom-object1 object custom-print-list)) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
334 (cust-print-list object))) |
655 | 335 |
336 ((vectorp object) | |
337 (or (and custom-print-vector | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
338 (cust-print-custom-object1 object custom-print-vector)) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
339 (cust-print-vector object))) |
655 | 340 |
341 ;; All other types, just print. | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
342 (t (cust-print-internal-printer object)))) |
655 | 343 |
344 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
345 ;; Helper for cust-print-custom-object. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
346 ;; Print the custom OBJECT using the custom type ALIST. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
347 ;; For the first predicate that matches the object, the corresponding |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
348 ;; converter is evaluated with the object and the string that results is |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
349 ;; printed with princ. Return nil if no predicte matches the object. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
350 (defun cust-print-custom-object1 (object alist) |
655 | 351 (while (and alist (not (funcall (car (car alist)) object))) |
352 (setq alist (cdr alist))) | |
353 ;; If alist is not null, then something matched. | |
354 (if alist | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
355 (cust-print-internal-princ |
655 | 356 (funcall (cdr (car alist)) object) ; returns string |
357 ))) | |
358 | |
359 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
360 (defun cust-print-circular (object) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
361 "Printer for `prin1' and `princ' that handles circular structures. |
655 | 362 If OBJECT appears multiply, and has not yet been printed, |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
363 prefix with label; if it has been printed, use `#N#' instead. |
655 | 364 Otherwise, print normally." |
365 (let ((tag (assq object circle-table))) | |
366 (if tag | |
367 (let ((id (cdr tag))) | |
368 (if (> id 0) | |
369 (progn | |
370 ;; Already printed, so just print id. | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
371 (cust-print-internal-princ "#") |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
372 (cust-print-internal-princ id) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
373 (cust-print-internal-princ "#")) |
655 | 374 ;; Not printed yet, so label with id and print object. |
375 (setcdr tag (- id)) ; mark it as printed | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
376 (cust-print-internal-princ "#") |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
377 (cust-print-internal-princ (- id)) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
378 (cust-print-internal-princ "=") |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
379 (cust-print-low-level-prin object) |
655 | 380 )) |
381 ;; Not repeated in structure. | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
382 (cust-print-low-level-prin object)))) |
655 | 383 |
384 | |
385 ;;================================================ | |
386 ;; List and vector processing for print functions. | |
387 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
388 ;; Print a list using print-length, print-level, and print-circle. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
389 (defun cust-print-list (list) |
655 | 390 (if (= level 0) |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
391 (cust-print-internal-princ "#") |
655 | 392 (let ((level (1- level))) |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
393 (cust-print-internal-princ "(") |
655 | 394 (let ((length (or print-length 0))) |
395 | |
396 ;; Print the first element always (even if length = 0). | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
397 (cust-print-prin (car list)) |
655 | 398 (setq list (cdr list)) |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
399 (if list (cust-print-internal-princ " ")) |
655 | 400 (setq length (1- length)) |
401 | |
402 ;; Print the rest of the elements. | |
403 (while (and list (/= 0 length)) | |
404 (if (and (listp list) | |
405 (not (assq list circle-table))) | |
406 (progn | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
407 (cust-print-prin (car list)) |
655 | 408 (setq list (cdr list))) |
409 | |
410 ;; cdr is not a list, or it is in circle-table. | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
411 (cust-print-internal-princ ". ") |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
412 (cust-print-prin list) |
655 | 413 (setq list nil)) |
414 | |
415 (setq length (1- length)) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
416 (if list (cust-print-internal-princ " "))) |
655 | 417 |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
418 (if (and list (= length 0)) (cust-print-internal-princ "...")) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
419 (cust-print-internal-princ ")")))) |
655 | 420 list) |
421 | |
422 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
423 ;; Print a vector according to print-length, print-level, and print-circle. |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
424 (defun cust-print-vector (vector) |
655 | 425 (if (= level 0) |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
426 (cust-print-internal-princ "#") |
655 | 427 (let ((level (1- level)) |
428 (i 0) | |
429 (len (length vector))) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
430 (cust-print-internal-princ "[") |
655 | 431 |
432 (if print-length | |
433 (setq len (min print-length len))) | |
434 ;; Print the elements | |
435 (while (< i len) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
436 (cust-print-prin (aref vector i)) |
655 | 437 (setq i (1+ i)) |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
438 (if (< i (length vector)) (cust-print-internal-princ " "))) |
655 | 439 |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
440 (if (< i (length vector)) (cust-print-internal-princ "...")) |
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
441 (cust-print-internal-princ "]") |
655 | 442 )) |
443 vector) | |
444 | |
445 | |
446 ;;================================== | |
447 ;; Circular structure preprocessing | |
448 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
449 (defun cust-print-preprocess-circle-tree (object) |
655 | 450 ;; Fill up the table. |
451 (let (;; Table of tags for each object in an object to be printed. | |
452 ;; A tag is of the form: | |
453 ;; ( <object> <nil-t-or-id-number> ) | |
454 ;; The id-number is generated after the entire table has been computed. | |
455 ;; During walk through, the real circle-table lives in the cdr so we | |
456 ;; can use setcdr to add new elements instead of having to setq the | |
457 ;; variable sometimes (poor man's locf). | |
458 (circle-table (list nil))) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
459 (cust-print-walk-circle-tree object) |
655 | 460 |
461 ;; Reverse table so it is in the order that the objects will be printed. | |
462 ;; This pass could be avoided if we always added to the end of the | |
463 ;; table with setcdr in walk-circle-tree. | |
464 (setcdr circle-table (nreverse (cdr circle-table))) | |
465 | |
466 ;; Walk through the table, assigning id-numbers to those | |
467 ;; objects which will be printed using #N= syntax. Delete those | |
468 ;; objects which will be printed only once (to speed up assq later). | |
469 (let ((rest circle-table) | |
470 (id -1)) | |
471 (while (cdr rest) | |
472 (let ((tag (car (cdr rest)))) | |
473 (cond ((cdr tag) | |
474 (setcdr tag id) | |
475 (setq id (1- id)) | |
476 (setq rest (cdr rest))) | |
477 ;; Else delete this object. | |
478 (t (setcdr rest (cdr (cdr rest)))))) | |
479 )) | |
480 ;; Drop the car. | |
481 (cdr circle-table) | |
482 )) | |
483 | |
484 | |
485 | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
486 (defun cust-print-walk-circle-tree (object) |
655 | 487 (let (read-equivalent-p tag) |
488 (while object | |
489 (setq read-equivalent-p (or (numberp object) (symbolp object)) | |
490 tag (and (not read-equivalent-p) | |
491 (assq object (cdr circle-table)))) | |
492 (cond (tag | |
493 ;; Seen this object already, so note that. | |
494 (setcdr tag t)) | |
495 | |
496 ((not read-equivalent-p) | |
497 ;; Add a tag for this object. | |
498 (setcdr circle-table | |
499 (cons (list object) | |
500 (cdr circle-table))))) | |
501 (setq object | |
502 (cond | |
503 (tag ;; No need to descend since we have already. | |
504 nil) | |
505 | |
506 ((consp object) | |
507 ;; Walk the car of the list recursively. | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
508 (cust-print-walk-circle-tree (car object)) |
655 | 509 ;; But walk the cdr with the above while loop |
510 ;; to avoid problems with max-lisp-eval-depth. | |
511 ;; And it should be faster than recursion. | |
512 (cdr object)) | |
513 | |
514 ((vectorp object) | |
515 ;; Walk the vector. | |
516 (let ((i (length object)) | |
517 (j 0)) | |
518 (while (< j i) | |
1359
96c43cee31f1
CP:: changed to cust-print- in all names.
Richard M. Stallman <rms@gnu.org>
parents:
1356
diff
changeset
|
519 (cust-print-walk-circle-tree (aref object j)) |
655 | 520 (setq j (1+ j)))))))))) |
521 | |
522 | |
523 | |
524 ;;======================================= | |
525 | |
526 (quote | |
527 examples | |
528 | |
529 (progn | |
530 ;; Create some circular structures. | |
531 (setq circ-sym (let ((x (make-symbol "FOO"))) (list x x))) | |
532 (setq circ-list (list 'a 'b (vector 1 2 3 4) 'd 'e 'f)) | |
533 (setcar (nthcdr 3 circ-list) circ-list) | |
534 (aset (nth 2 circ-list) 2 circ-list) | |
535 (setq dotted-circ-list (list 'a 'b 'c)) | |
536 (setcdr (cdr (cdr dotted-circ-list)) dotted-circ-list) | |
537 (setq circ-vector (vector 1 2 3 4 (list 'a 'b 'c 'd) 6 7)) | |
538 (aset circ-vector 5 (make-symbol "-gensym-")) | |
539 (setcar (cdr (aref circ-vector 4)) (aref circ-vector 5)) | |
540 nil) | |
541 | |
542 (install-custom-print-funcs) | |
543 ;; (setq print-circle t) | |
544 | |
545 (let ((print-circle t)) | |
546 (or (equal (prin1-to-string circ-list) "#1=(a b [1 2 #1# 4] #1# e f)") | |
547 (error "circular object with array printing"))) | |
548 | |
549 (let ((print-circle t)) | |
550 (or (equal (prin1-to-string dotted-circ-list) "#1=(a b c . #1#)") | |
551 (error "circular object with array printing"))) | |
552 | |
553 (let* ((print-circle t) | |
554 (x (list 'p 'q)) | |
555 (y (list (list 'a 'b) x 'foo x))) | |
556 (setcdr (cdr (cdr (cdr y))) (cdr y)) | |
557 (or (equal (prin1-to-string y) "((a b) . #1=(#2=(p q) foo #2# . #1#))" | |
558 ) | |
559 (error "circular list example from CL manual"))) | |
560 | |
561 ;; There's no special handling of uninterned symbols in custom-print. | |
562 (let ((print-circle nil)) | |
563 (or (equal (prin1-to-string circ-sym) "(#:FOO #:FOO)") | |
564 (error "uninterned symbols in list"))) | |
565 (let ((print-circle t)) | |
566 (or (equal (prin1-to-string circ-sym) "(#1=FOO #1#)") | |
567 (error "circular uninterned symbols in list"))) | |
568 | |
569 (uninstall-custom-print-funcs) | |
570 ) | |
571 | |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
572 ;;; cust-print.el ends here |