annotate lisp/edebug/cust-print.el @ 0:376386a54a3c r19-14

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