Mercurial > emacs
comparison lisp/emacs-lisp/cust-print.el @ 26519:693b53fde264
Use new backquote syntax.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Sun, 21 Nov 1999 14:49:20 +0000 |
parents | baefeadae7a3 |
children | 3cfd3dc474b8 |
comparison
equal
deleted
inserted
replaced
26518:ed1016f53081 | 26519:693b53fde264 |
---|---|
7 ;; Keywords: extensions | 7 ;; Keywords: extensions |
8 | 8 |
9 ;; LCD Archive Entry: | 9 ;; LCD Archive Entry: |
10 ;; cust-print|Daniel LaLiberte|liberte@cs.uiuc.edu | 10 ;; cust-print|Daniel LaLiberte|liberte@cs.uiuc.edu |
11 ;; |Handle print-level, print-circle and more. | 11 ;; |Handle print-level, print-circle and more. |
12 ;; |$Date: 1994/04/05 21:05:09 $|$Revision: 1.14 $| | |
13 | 12 |
14 ;; This file is part of GNU Emacs. | 13 ;; This file is part of GNU Emacs. |
15 | 14 |
16 ;; GNU Emacs is free software; you can redistribute it and/or modify | 15 ;; GNU Emacs is free software; you can redistribute it and/or modify |
17 ;; it under the terms of the GNU General Public License as published by | 16 ;; it under the terms of the GNU General Public License as published by |
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | 21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24 ;; GNU General Public License for more details. | 23 ;; GNU General Public License for more details. |
25 | 24 |
26 ;; You should have received a copy of the GNU General Public License | 25 ;; You should have received a copy of the GNU General Public License |
27 ;; along with GNU Emacs; see the file COPYING. If not, write to | 26 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
28 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
29 | 28 ;; Boston, MA 02111-1307, USA. |
30 ;;; =============================== | 29 |
31 ;;; $Header: $ | |
32 ;;; $Log: cust-print.el,v $ | |
33 ;;; Revision 1.14 1994/04/05 21:05:09 liberte | |
34 ;;; Change install- and uninstall- to -install and -uninstall. | |
35 ;;; | |
36 ;;; Revision 1.13 1994/03/24 20:26:05 liberte | |
37 ;;; Change "internal" to "original" throughout. | |
38 ;;; (add-custom-printer, delete-custom-printer) replace customizers. | |
39 ;;; (with-custom-print) new | |
40 ;;; (custom-prin1-to-string) Made it more robust. | |
41 ;;; | |
42 ;;; Revision 1.4 1994/03/23 20:34:29 liberte | |
43 ;;; * Change "emacs" to "original" - I just can't decide. | |
44 ;;; | |
45 ;;; Revision 1.3 1994/02/21 21:25:36 liberte | |
46 ;;; * Make custom-prin1-to-string more robust when errors occur. | |
47 ;;; * Change "internal" to "emacs". | |
48 ;;; | |
49 ;;; Revision 1.2 1993/11/22 22:36:36 liberte | |
50 ;;; * Simplified and generalized printer customization. | |
51 ;;; custom-printers is an alist of (PREDICATE . PRINTER) pairs | |
52 ;;; for any data types. The PRINTER function should print to | |
53 ;;; `standard-output' add-custom-printer and delete-custom-printer | |
54 ;;; change custom-printers. | |
55 ;;; | |
56 ;;; * Installation function now called install-custom-print. The | |
57 ;;; old name is still around for now. | |
58 ;;; | |
59 ;;; * New macro with-custom-print (added earlier) - executes like | |
60 ;;; progn but with custom-print activated temporarily. | |
61 ;;; | |
62 ;;; * Cleaned up comments for replacements of standardard printers. | |
63 ;;; | |
64 ;;; * Changed custom-prin1-to-string to use a temporary buffer. | |
65 ;;; | |
66 ;;; * Option custom-print-vectors (added earlier) - controls whether | |
67 ;;; vectors should be printed according to print-length and | |
68 ;;; print-length. Emacs doesnt do this, but cust-print would | |
69 ;;; otherwise do it only if custom printing is required. | |
70 ;;; | |
71 ;;; * Uninterned symbols are treated as non-read-equivalent. | |
72 ;;; | |
73 | |
74 | |
75 ;;; Commentary: | 30 ;;; Commentary: |
76 | 31 |
77 ;; This package provides a general print handler for prin1 and princ | 32 ;; This package provides a general print handler for prin1 and princ |
78 ;; that supports print-level and print-circle, and by the way, | 33 ;; that supports print-level and print-circle, and by the way, |
79 ;; print-length since the standard routines are being replaced. Also, | 34 ;; print-length since the standard routines are being replaced. Also, |
125 ;; required before the final printing. Thanks to Jamie Zawinski | 80 ;; required before the final printing. Thanks to Jamie Zawinski |
126 ;; for motivation and algorithms. | 81 ;; for motivation and algorithms. |
127 | 82 |
128 | 83 |
129 ;;; Code: | 84 ;;; Code: |
130 ;;========================================================= | 85 |
86 (defgroup cust-print nil | |
87 "Handles print-level and print-circle." | |
88 :prefix "print-" | |
89 :group 'lisp | |
90 :group 'extensions) | |
131 | 91 |
132 ;; If using cl-packages: | 92 ;; If using cl-packages: |
133 | 93 |
134 '(defpackage "cust-print" | 94 '(defpackage "cust-print" |
135 (:nicknames "CP" "custom-print") | 95 (:nicknames "CP" "custom-print") |
155 add-custom-printer | 115 add-custom-printer |
156 )) | 116 )) |
157 | 117 |
158 '(in-package cust-print) | 118 '(in-package cust-print) |
159 | 119 |
160 (require 'backquote) | 120 ;; Emacs 18 doesn't have defalias. |
161 | |
162 ;; Emacs 18 doesnt have defalias. | |
163 ;; Provide def for byte compiler. | 121 ;; Provide def for byte compiler. |
164 (eval-and-compile | 122 (eval-and-compile |
165 (or (fboundp 'defalias) (fset 'defalias 'fset))) | 123 (or (fboundp 'defalias) (fset 'defalias 'fset))) |
166 | 124 |
167 | 125 |
170 | 128 |
171 ;;(defvar print-length nil | 129 ;;(defvar print-length nil |
172 ;; "*Controls how many elements of a list, at each level, are printed. | 130 ;; "*Controls how many elements of a list, at each level, are printed. |
173 ;;This is defined by emacs.") | 131 ;;This is defined by emacs.") |
174 | 132 |
175 (defvar print-level nil | 133 (defcustom print-level nil |
176 "*Controls how many levels deep a nested data object will print. | 134 "*Controls how many levels deep a nested data object will print. |
177 | 135 |
178 If nil, printing proceeds recursively and may lead to | 136 If nil, printing proceeds recursively and may lead to |
179 max-lisp-eval-depth being exceeded or an error may occur: | 137 max-lisp-eval-depth being exceeded or an error may occur: |
180 `Apparently circular structure being printed.' | 138 `Apparently circular structure being printed.' |
181 Also see `print-length' and `print-circle'. | 139 Also see `print-length' and `print-circle'. |
182 | 140 |
183 If non-nil, components at levels equal to or greater than `print-level' | 141 If non-nil, components at levels equal to or greater than `print-level' |
184 are printed simply as `#'. The object to be printed is at level 0, | 142 are printed simply as `#'. The object to be printed is at level 0, |
185 and if the object is a list or vector, its top-level components are at | 143 and if the object is a list or vector, its top-level components are at |
186 level 1.") | 144 level 1." |
187 | 145 :type '(choice (const nil) integer) |
188 | 146 :group 'cust-print) |
189 (defvar print-circle nil | 147 |
148 | |
149 (defcustom print-circle nil | |
190 "*Controls the printing of recursive structures. | 150 "*Controls the printing of recursive structures. |
191 | 151 |
192 If nil, printing proceeds recursively and may lead to | 152 If nil, printing proceeds recursively and may lead to |
193 `max-lisp-eval-depth' being exceeded or an error may occur: | 153 `max-lisp-eval-depth' being exceeded or an error may occur: |
194 \"Apparently circular structure being printed.\" Also see | 154 \"Apparently circular structure being printed.\" Also see |
198 with `#N=' before the first occurrence (in the order of the print | 158 with `#N=' before the first occurrence (in the order of the print |
199 representation) and `#N#' in place of each subsequent occurrence, | 159 representation) and `#N#' in place of each subsequent occurrence, |
200 where N is a positive decimal integer. | 160 where N is a positive decimal integer. |
201 | 161 |
202 There is no way to read this representation in standard Emacs, | 162 There is no way to read this representation in standard Emacs, |
203 but if you need to do so, try the cl-read.el package.") | 163 but if you need to do so, try the cl-read.el package." |
204 | 164 :type 'boolean |
205 | 165 :group 'cust-print) |
206 (defvar custom-print-vectors nil | 166 |
167 | |
168 (defcustom custom-print-vectors nil | |
207 "*Non-nil if printing of vectors should obey print-level and print-length. | 169 "*Non-nil if printing of vectors should obey print-level and print-length. |
208 | 170 |
209 For Emacs 18, setting print-level, or adding custom print list or | 171 For Emacs 18, setting print-level, or adding custom print list or |
210 vector handling will make this happen anyway. Emacs 19 obeys | 172 vector handling will make this happen anyway. Emacs 19 obeys |
211 print-level, but not for vectors.") | 173 print-level, but not for vectors." |
174 :type 'boolean | |
175 :group 'cust-print) | |
212 | 176 |
213 | 177 |
214 ;; Custom printers | 178 ;; Custom printers |
215 ;;========================================================== | 179 ;;========================================================== |
216 | 180 |
225 | 189 |
226 Don't modify this variable directly. Use `add-custom-printer' and | 190 Don't modify this variable directly. Use `add-custom-printer' and |
227 `delete-custom-printer'") | 191 `delete-custom-printer'") |
228 ;; Should cust-print-original-princ and cust-print-prin be exported symbols? | 192 ;; Should cust-print-original-princ and cust-print-prin be exported symbols? |
229 ;; Or should the standard printers functions be replaced by | 193 ;; Or should the standard printers functions be replaced by |
230 ;; CP ones in elisp so that CP internal functions need not be called? | 194 ;; CP ones in Emacs Lisp so that CP internal functions need not be called? |
231 | 195 |
232 (defun add-custom-printer (pred printer) | 196 (defun add-custom-printer (pred printer) |
233 "Add a pair of PREDICATE and PRINTER to `custom-printers'. | 197 "Add a pair of PREDICATE and PRINTER to `custom-printers'. |
234 Any pair that has the same PREDICATE is first removed." | 198 Any pair that has the same PREDICATE is first removed." |
235 (setq custom-printers (cons (cons pred printer) | 199 (setq custom-printers (cons (cons pred printer) |
250 nil) | 214 nil) |
251 | 215 |
252 (defun cust-print-update-custom-printers () | 216 (defun cust-print-update-custom-printers () |
253 ;; Modify the definition of cust-print-use-custom-printer | 217 ;; Modify the definition of cust-print-use-custom-printer |
254 (defalias 'cust-print-use-custom-printer | 218 (defalias 'cust-print-use-custom-printer |
255 ;; We dont really want to require the byte-compiler. | 219 ;; We don't really want to require the byte-compiler. |
256 ;; (byte-compile | 220 ;; (byte-compile |
257 (` (lambda (object) | 221 `(lambda (object) |
258 (cond | 222 (cond |
259 (,@ (mapcar (function | 223 ,@(mapcar (function |
260 (lambda (pair) | 224 (lambda (pair) |
261 (` (((, (car pair)) object) | 225 `((,(car pair) object) |
262 ((, (cdr pair)) object))))) | 226 (,(cdr pair) object)))) |
263 custom-printers)) | 227 custom-printers) |
264 ;; Otherwise return nil. | 228 ;; Otherwise return nil. |
265 (t nil) | 229 (t nil) |
266 ))) | 230 )) |
267 ;; ) | 231 ;; ) |
268 )) | 232 )) |
269 | 233 |
270 | 234 |
271 ;; Saving and restoring emacs printing routines. | 235 ;; Saving and restoring emacs printing routines. |
272 ;;==================================================== | 236 ;;==================================================== |
273 | 237 |
328 (put 'with-custom-print 'edebug-form-spec '(body)) | 292 (put 'with-custom-print 'edebug-form-spec '(body)) |
329 | 293 |
330 (defalias 'with-custom-print-funcs 'with-custom-print) | 294 (defalias 'with-custom-print-funcs 'with-custom-print) |
331 (defmacro with-custom-print (&rest body) | 295 (defmacro with-custom-print (&rest body) |
332 "Temporarily install the custom print package while executing BODY." | 296 "Temporarily install the custom print package while executing BODY." |
333 (` (unwind-protect | 297 `(unwind-protect |
334 (progn | 298 (progn |
335 (custom-print-install) | 299 (custom-print-install) |
336 (,@ body)) | 300 ,@body) |
337 (custom-print-uninstall)))) | 301 (custom-print-uninstall))) |
338 | 302 |
339 | 303 |
340 ;; Lisp replacements for prin1 and princ, and for some subrs that use them | 304 ;; Lisp replacements for prin1 and princ, and for some subrs that use them |
341 ;;=============================================================== | 305 ;;=============================================================== |
342 ;; - so far only the printing and formatting subrs. | 306 ;; - so far only the printing and formatting subrs. |
361 | 325 |
362 This is the custom-print replacement for the standard `princ'." | 326 This is the custom-print replacement for the standard `princ'." |
363 (cust-print-top-level object stream 'cust-print-original-princ)) | 327 (cust-print-top-level object stream 'cust-print-original-princ)) |
364 | 328 |
365 | 329 |
366 (defun custom-prin1-to-string (object) | 330 (defun custom-prin1-to-string (object &optional noescape) |
367 "Return a string containing the printed representation of OBJECT, | 331 "Return a string containing the printed representation of OBJECT, |
368 any Lisp object. Quoting characters are used when needed to make output | 332 any Lisp object. Quoting characters are used when needed to make output |
369 that `read' can handle, whenever this is possible. | 333 that `read' can handle, whenever this is possible, unless the optional |
334 second argument NOESCAPE is non-nil. | |
370 | 335 |
371 This is the custom-print replacement for the standard `prin1-to-string'." | 336 This is the custom-print replacement for the standard `prin1-to-string'." |
372 (let ((buf (get-buffer-create " *custom-print-temp*"))) | 337 (let ((buf (get-buffer-create " *custom-print-temp*"))) |
373 ;; We must erase the buffer before printing in case an error | 338 ;; We must erase the buffer before printing in case an error |
374 ;; occured during the last prin1-to-string and we are in debugger. | 339 ;; occurred during the last prin1-to-string and we are in debugger. |
375 (save-excursion | 340 (save-excursion |
376 (set-buffer buf) | 341 (set-buffer buf) |
377 (erase-buffer)) | 342 (erase-buffer)) |
378 ;; We must be in the current-buffer when the print occurs. | 343 ;; We must be in the current-buffer when the print occurs. |
379 (custom-prin1 object buf) | 344 (if noescape |
345 (custom-princ object buf) | |
346 (custom-prin1 object buf)) | |
380 (save-excursion | 347 (save-excursion |
381 (set-buffer buf) | 348 (set-buffer buf) |
382 (buffer-string) | 349 (buffer-string) |
383 ;; We could erase the buffer again, but why bother? | 350 ;; We could erase the buffer again, but why bother? |
384 ))) | 351 ))) |