Fix Rmail editing with reapplying encoding to message body
[emacs.git] / lisp / obsolete / cust-print.el
blobb9aef43e0ba06c92952ea1b01a379081d7925d89
1 ;;; cust-print.el --- handles print-level and print-circle
3 ;; Copyright (C) 1992, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Daniel LaLiberte <liberte@holonexus.org>
6 ;; Adapted-By: ESR
7 ;; Keywords: extensions
8 ;; Obsolete-since: 24.3
10 ;; LCD Archive Entry:
11 ;; cust-print|Daniel LaLiberte|liberte@holonexus.org
12 ;; |Handle print-level, print-circle and more.
14 ;; This file is part of GNU Emacs.
16 ;; 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
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;;; Commentary:
31 ;; This package provides a general print handler for prin1 and princ
32 ;; that supports print-level and print-circle, and by the way,
33 ;; print-length since the standard routines are being replaced. Also,
34 ;; to print custom types constructed from lists and vectors, use
35 ;; custom-print-list and custom-print-vector. See the documentation
36 ;; strings of these variables for more details.
38 ;; If the results of your expressions contain circular references to
39 ;; other parts of the same structure, the standard Emacs print
40 ;; subroutines may fail to print with an untrappable error,
41 ;; "Apparently circular structure being printed". If you only use cdr
42 ;; circular lists (where cdrs of lists point back; what is the right
43 ;; term here?), you can limit the length of printing with
44 ;; print-length. But car circular lists and circular vectors generate
45 ;; the above mentioned error in Emacs version 18. Version
46 ;; 19 supports print-level, but it is often useful to get a better
47 ;; print representation of circular and shared structures; the print-circle
48 ;; option may be used to print more concise representations.
50 ;; There are three main ways to use this package. First, you may
51 ;; replace prin1, princ, and some subroutines that use them by calling
52 ;; install-custom-print so that any use of these functions in
53 ;; Lisp code will be affected; you can later reset with
54 ;; uninstall-custom-print. Second, you may temporarily install
55 ;; these functions with the macro with-custom-print. Third, you
56 ;; could call the custom routines directly, thus only affecting the
57 ;; printing that requires them.
59 ;; Note that subroutines which call print subroutines directly will
60 ;; not use the custom print functions. In particular, the evaluation
61 ;; functions like eval-region call the print subroutines directly.
62 ;; Therefore, if you evaluate (aref circ-list 0), where circ-list is a
63 ;; circular list rather than an array, aref calls error directly which
64 ;; will jump to the top level instead of printing the circular list.
66 ;; Uninterned symbols are recognized when print-circle is non-nil,
67 ;; but they are not printed specially here. Use the cl-packages package
68 ;; to print according to print-gensym.
70 ;; Obviously the right way to implement this custom-print facility is
71 ;; in C or with hooks into the standard printer. Please volunteer
72 ;; since I don't have the time or need. More CL-like printing
73 ;; capabilities could be added in the future.
75 ;; Implementation design: we want to use the same list and vector
76 ;; processing algorithm for all versions of prin1 and princ, since how
77 ;; the processing is done depends on print-length, print-level, and
78 ;; print-circle. For circle printing, a preprocessing step is
79 ;; required before the final printing. Thanks to Jamie Zawinski
80 ;; for motivation and algorithms.
83 ;;; Code:
85 (defgroup cust-print nil
86 "Handles print-level and print-circle."
87 :prefix "print-"
88 :group 'lisp
89 :group 'extensions)
91 ;; If using cl-packages:
93 '(defpackage "cust-print"
94 (:nicknames "CP" "custom-print")
95 (:use "el")
96 (:export
97 print-level
98 print-circle
100 custom-print-install
101 custom-print-uninstall
102 custom-print-installed-p
103 with-custom-print
105 custom-prin1
106 custom-princ
107 custom-prin1-to-string
108 custom-print
109 custom-format
110 custom-message
111 custom-error
113 custom-printers
114 add-custom-printer
117 '(in-package cust-print)
119 ;; Emacs 18 doesn't have defalias.
120 ;; Provide def for byte compiler.
121 (eval-and-compile
122 (or (fboundp 'defalias) (fset 'defalias 'fset)))
125 ;; Variables:
126 ;;=========================================================
128 ;;(defvar print-length nil
129 ;; "*Controls how many elements of a list, at each level, are printed.
130 ;;This is defined by emacs.")
132 (defcustom print-level nil
133 "Controls how many levels deep a nested data object will print.
135 If nil, printing proceeds recursively and may lead to
136 max-lisp-eval-depth being exceeded or an error may occur:
137 `Apparently circular structure being printed.'
138 Also see `print-length' and `print-circle'.
140 If non-nil, components at levels equal to or greater than `print-level'
141 are printed simply as `#'. The object to be printed is at level 0,
142 and if the object is a list or vector, its top-level components are at
143 level 1."
144 :type '(choice (const nil) integer)
145 :group 'cust-print)
148 (defcustom print-circle nil
149 "Controls the printing of recursive structures.
151 If nil, printing proceeds recursively and may lead to
152 `max-lisp-eval-depth' being exceeded or an error may occur:
153 \"Apparently circular structure being printed.\" Also see
154 `print-length' and `print-level'.
156 If non-nil, shared substructures anywhere in the structure are printed
157 with `#N=' before the first occurrence (in the order of the print
158 representation) and `#N#' in place of each subsequent occurrence,
159 where N is a positive decimal integer.
161 There is no way to read this representation in standard Emacs,
162 but if you need to do so, try the cl-read.el package."
163 :type 'boolean
164 :group 'cust-print)
167 (defcustom custom-print-vectors nil
168 "Non-nil if printing of vectors should obey `print-level' and `print-length'."
169 :type 'boolean
170 :group 'cust-print)
173 ;; Custom printers
174 ;;==========================================================
176 (defvar custom-printers nil
177 ;; e.g. '((symbolp . pkg::print-symbol))
178 "An alist for custom printing of any type.
179 Pairs are of the form (PREDICATE . PRINTER). If PREDICATE is true
180 for an object, then PRINTER is called with the object.
181 PRINTER should print to `standard-output' using cust-print-original-princ
182 if the standard printer is sufficient, or cust-print-prin for complex things.
183 The PRINTER should return the object being printed.
185 Don't modify this variable directly. Use `add-custom-printer' and
186 `delete-custom-printer'")
187 ;; Should cust-print-original-princ and cust-print-prin be exported symbols?
188 ;; Or should the standard printers functions be replaced by
189 ;; CP ones in Emacs Lisp so that CP internal functions need not be called?
191 (defun add-custom-printer (pred printer)
192 "Add a pair of PREDICATE and PRINTER to `custom-printers'.
193 Any pair that has the same PREDICATE is first removed."
194 (setq custom-printers (cons (cons pred printer)
195 (delq (assq pred custom-printers)
196 custom-printers)))
197 ;; Rather than updating here, we could wait until cust-print-top-level is called.
198 (cust-print-update-custom-printers))
200 (defun delete-custom-printer (pred)
201 "Delete the custom printer associated with PREDICATE."
202 (setq custom-printers (delq (assq pred custom-printers)
203 custom-printers))
204 (cust-print-update-custom-printers))
207 (defun cust-print-use-custom-printer (object)
208 ;; Default function returns nil.
209 nil)
211 (defun cust-print-update-custom-printers ()
212 ;; Modify the definition of cust-print-use-custom-printer
213 (defalias 'cust-print-use-custom-printer
214 ;; We don't really want to require the byte-compiler.
215 ;; (byte-compile
216 `(lambda (object)
217 (cond
218 ,@(mapcar (function
219 (lambda (pair)
220 `((,(car pair) object)
221 (,(cdr pair) object))))
222 custom-printers)
223 ;; Otherwise return nil.
224 (t nil)
226 ;; )
230 ;; Saving and restoring emacs printing routines.
231 ;;====================================================
233 (defun cust-print-set-function-cell (symbol-pair)
234 (defalias (car symbol-pair)
235 (symbol-function (car (cdr symbol-pair)))))
237 (defun cust-print-original-princ (object &optional stream)) ; dummy def
239 ;; Save emacs routines.
240 (if (not (fboundp 'cust-print-original-prin1))
241 (mapc 'cust-print-set-function-cell
242 '((cust-print-original-prin1 prin1)
243 (cust-print-original-princ princ)
244 (cust-print-original-print print)
245 (cust-print-original-prin1-to-string prin1-to-string)
246 (cust-print-original-format format)
247 (cust-print-original-message message)
248 (cust-print-original-error error))))
251 (defun custom-print-install ()
252 "Replace print functions with general, customizable, Lisp versions.
253 The Emacs subroutines are saved away, and you can reinstall them
254 by running `custom-print-uninstall'."
255 (interactive)
256 (mapc 'cust-print-set-function-cell
257 '((prin1 custom-prin1)
258 (princ custom-princ)
259 (print custom-print)
260 (prin1-to-string custom-prin1-to-string)
261 (format custom-format)
262 (message custom-message)
263 (error custom-error)
267 (defun custom-print-uninstall ()
268 "Reset print functions to their Emacs subroutines."
269 (interactive)
270 (mapc 'cust-print-set-function-cell
271 '((prin1 cust-print-original-prin1)
272 (princ cust-print-original-princ)
273 (print cust-print-original-print)
274 (prin1-to-string cust-print-original-prin1-to-string)
275 (format cust-print-original-format)
276 (message cust-print-original-message)
277 (error cust-print-original-error)
281 (defalias 'custom-print-funcs-installed-p 'custom-print-installed-p)
282 (defun custom-print-installed-p ()
283 "Return t if custom-print is currently installed, nil otherwise."
284 (eq (symbol-function 'custom-prin1) (symbol-function 'prin1)))
286 (put 'with-custom-print-funcs 'edebug-form-spec '(body))
287 (put 'with-custom-print 'edebug-form-spec '(body))
289 (defalias 'with-custom-print-funcs 'with-custom-print)
290 (defmacro with-custom-print (&rest body)
291 "Temporarily install the custom print package while executing BODY."
292 `(unwind-protect
293 (progn
294 (custom-print-install)
295 ,@body)
296 (custom-print-uninstall)))
299 ;; Lisp replacements for prin1 and princ, and for some subrs that use them
300 ;;===============================================================
301 ;; - so far only the printing and formatting subrs.
303 (defun custom-prin1 (object &optional stream)
304 "Output the printed representation of OBJECT, any Lisp object.
305 Quoting characters are printed when needed to make output that `read'
306 can handle, whenever this is possible.
307 Output stream is STREAM, or value of `standard-output' (which see).
309 This is the custom-print replacement for the standard `prin1'. It
310 uses the appropriate printer depending on the values of `print-level'
311 and `print-circle' (which see)."
312 (cust-print-top-level object stream 'cust-print-original-prin1))
315 (defun custom-princ (object &optional stream)
316 "Output the printed representation of OBJECT, any Lisp object.
317 No quoting characters are used; no delimiters are printed around
318 the contents of strings.
319 Output stream is STREAM, or value of `standard-output' (which see).
321 This is the custom-print replacement for the standard `princ'."
322 (cust-print-top-level object stream 'cust-print-original-princ))
325 (defun custom-prin1-to-string (object &optional noescape)
326 "Return a string containing the printed representation of OBJECT,
327 any Lisp object. Quoting characters are used when needed to make output
328 that `read' can handle, whenever this is possible, unless the optional
329 second argument NOESCAPE is non-nil.
331 This is the custom-print replacement for the standard `prin1-to-string'."
332 (let ((buf (get-buffer-create " *custom-print-temp*")))
333 ;; We must erase the buffer before printing in case an error
334 ;; occurred during the last prin1-to-string and we are in debugger.
335 (with-current-buffer buf
336 (erase-buffer))
337 ;; We must be in the current-buffer when the print occurs.
338 (if noescape
339 (custom-princ object buf)
340 (custom-prin1 object buf))
341 (with-current-buffer buf
342 (buffer-string)
343 ;; We could erase the buffer again, but why bother?
347 (defun custom-print (object &optional stream)
348 "Output the printed representation of OBJECT, with newlines around it.
349 Quoting characters are printed when needed to make output that `read'
350 can handle, whenever this is possible.
351 Output stream is STREAM, or value of `standard-output' (which see).
353 This is the custom-print replacement for the standard `print'."
354 (cust-print-original-princ "\n" stream)
355 (custom-prin1 object stream)
356 (cust-print-original-princ "\n" stream))
359 (defun custom-format (fmt &rest args)
360 "Format a string out of a control-string and arguments.
361 The first argument is a control string. It, and subsequent arguments
362 substituted into it, become the value, which is a string.
363 It may contain %s or %d or %c to substitute successive following arguments.
364 %s means print an argument as a string, %d means print as number in decimal,
365 %c means print a number as a single character.
366 The argument used by %s must be a string or a symbol;
367 the argument used by %d, %b, %o, %x or %c must be a number.
369 This is the custom-print replacement for the standard `format'. It
370 calls the Emacs `format' after first making strings for list,
371 vector, or symbol args. The format specification for such args should
372 be `%s' in any case, so a string argument will also work. The string
373 is generated with `custom-prin1-to-string', which quotes quotable
374 characters."
375 (apply 'cust-print-original-format fmt
376 (mapcar (function (lambda (arg)
377 (if (or (listp arg) (vectorp arg) (symbolp arg))
378 (custom-prin1-to-string arg)
379 arg)))
380 args)))
383 (defun custom-message (fmt &rest args)
384 "Print a one-line message at the bottom of the screen.
385 The first argument is a control string.
386 It may contain %s or %d or %c to print successive following arguments.
387 %s means print an argument as a string, %d means print as number in decimal,
388 %c means print a number as a single character.
389 The argument used by %s must be a string or a symbol;
390 the argument used by %d or %c must be a number.
392 This is the custom-print replacement for the standard `message'.
393 See `custom-format' for the details."
394 ;; It doesn't work to princ the result of custom-format as in:
395 ;; (cust-print-original-princ (apply 'custom-format fmt args))
396 ;; because the echo area requires special handling
397 ;; to avoid duplicating the output.
398 ;; cust-print-original-message does it right.
399 (apply 'cust-print-original-message fmt
400 (mapcar (function (lambda (arg)
401 (if (or (listp arg) (vectorp arg) (symbolp arg))
402 (custom-prin1-to-string arg)
403 arg)))
404 args)))
407 (defun custom-error (fmt &rest args)
408 "Signal an error, making error message by passing all args to `format'.
410 This is the custom-print replacement for the standard `error'.
411 See `custom-format' for the details."
412 (signal 'error (list (apply 'custom-format fmt args))))
416 ;; Support for custom prin1 and princ
417 ;;=========================================
419 ;; Defs to quiet byte-compiler.
420 (defvar circle-table)
421 (defvar cust-print-current-level)
423 (defun cust-print-original-printer (object)) ; One of the standard printers.
424 (defun cust-print-low-level-prin (object)) ; Used internally.
425 (defun cust-print-prin (object)) ; Call this to print recursively.
427 (defun cust-print-top-level (object stream emacs-printer)
428 ;; Set up for printing.
429 (let ((standard-output (or stream standard-output))
430 ;; circle-table will be non-nil if anything is circular.
431 (circle-table (and print-circle
432 (cust-print-preprocess-circle-tree object)))
433 (cust-print-current-level (or print-level -1)))
435 (defalias 'cust-print-original-printer emacs-printer)
436 (defalias 'cust-print-low-level-prin
437 (cond
438 ((or custom-printers
439 circle-table
440 print-level ; comment out for version 19
441 ;; Emacs doesn't use print-level or print-length
442 ;; for vectors, but custom-print can.
443 (if custom-print-vectors
444 (or print-level print-length)))
445 'cust-print-print-object)
446 (t 'cust-print-original-printer)))
447 (defalias 'cust-print-prin
448 (if circle-table 'cust-print-print-circular 'cust-print-low-level-prin))
450 (cust-print-prin object)
451 object))
454 (defun cust-print-print-object (object)
455 ;; Test object type and print accordingly.
456 ;; Could be called as either cust-print-low-level-prin or cust-print-prin.
457 (cond
458 ((null object) (cust-print-original-printer object))
459 ((cust-print-use-custom-printer object) object)
460 ((consp object) (cust-print-list object))
461 ((vectorp object) (cust-print-vector object))
462 ;; All other types, just print.
463 (t (cust-print-original-printer object))))
466 (defun cust-print-print-circular (object)
467 ;; Printer for `prin1' and `princ' that handles circular structures.
468 ;; If OBJECT appears multiply, and has not yet been printed,
469 ;; prefix with label; if it has been printed, use `#N#' instead.
470 ;; Otherwise, print normally.
471 (let ((tag (assq object circle-table)))
472 (if tag
473 (let ((id (cdr tag)))
474 (if (> id 0)
475 (progn
476 ;; Already printed, so just print id.
477 (cust-print-original-princ "#")
478 (cust-print-original-princ id)
479 (cust-print-original-princ "#"))
480 ;; Not printed yet, so label with id and print object.
481 (setcdr tag (- id)) ; mark it as printed
482 (cust-print-original-princ "#")
483 (cust-print-original-princ (- id))
484 (cust-print-original-princ "=")
485 (cust-print-low-level-prin object)
487 ;; Not repeated in structure.
488 (cust-print-low-level-prin object))))
491 ;;================================================
492 ;; List and vector processing for print functions.
494 (defun cust-print-list (list)
495 ;; Print a list using print-length, print-level, and print-circle.
496 (if (= cust-print-current-level 0)
497 (cust-print-original-princ "#")
498 (let ((cust-print-current-level (1- cust-print-current-level)))
499 (cust-print-original-princ "(")
500 (let ((length (or print-length 0)))
502 ;; Print the first element always (even if length = 0).
503 (cust-print-prin (car list))
504 (setq list (cdr list))
505 (if list (cust-print-original-princ " "))
506 (setq length (1- length))
508 ;; Print the rest of the elements.
509 (while (and list (/= 0 length))
510 (if (and (listp list)
511 (not (assq list circle-table)))
512 (progn
513 (cust-print-prin (car list))
514 (setq list (cdr list)))
516 ;; cdr is not a list, or it is in circle-table.
517 (cust-print-original-princ ". ")
518 (cust-print-prin list)
519 (setq list nil))
521 (setq length (1- length))
522 (if list (cust-print-original-princ " ")))
524 (if (and list (= length 0)) (cust-print-original-princ "..."))
525 (cust-print-original-princ ")"))))
526 list)
529 (defun cust-print-vector (vector)
530 ;; Print a vector according to print-length, print-level, and print-circle.
531 (if (= cust-print-current-level 0)
532 (cust-print-original-princ "#")
533 (let ((cust-print-current-level (1- cust-print-current-level))
534 (i 0)
535 (len (length vector)))
536 (cust-print-original-princ "[")
538 (if print-length
539 (setq len (min print-length len)))
540 ;; Print the elements
541 (while (< i len)
542 (cust-print-prin (aref vector i))
543 (setq i (1+ i))
544 (if (< i (length vector)) (cust-print-original-princ " ")))
546 (if (< i (length vector)) (cust-print-original-princ "..."))
547 (cust-print-original-princ "]")
549 vector)
553 ;; Circular structure preprocessing
554 ;;==================================
556 (defun cust-print-preprocess-circle-tree (object)
557 ;; Fill up the table.
558 (let (;; Table of tags for each object in an object to be printed.
559 ;; A tag is of the form:
560 ;; ( <object> <nil-t-or-id-number> )
561 ;; The id-number is generated after the entire table has been computed.
562 ;; During walk through, the real circle-table lives in the cdr so we
563 ;; can use setcdr to add new elements instead of having to setq the
564 ;; variable sometimes (poor man's locf).
565 (circle-table (list nil)))
566 (cust-print-walk-circle-tree object)
568 ;; Reverse table so it is in the order that the objects will be printed.
569 ;; This pass could be avoided if we always added to the end of the
570 ;; table with setcdr in walk-circle-tree.
571 (setcdr circle-table (nreverse (cdr circle-table)))
573 ;; Walk through the table, assigning id-numbers to those
574 ;; objects which will be printed using #N= syntax. Delete those
575 ;; objects which will be printed only once (to speed up assq later).
576 (let ((rest circle-table)
577 (id -1))
578 (while (cdr rest)
579 (let ((tag (car (cdr rest))))
580 (cond ((cdr tag)
581 (setcdr tag id)
582 (setq id (1- id))
583 (setq rest (cdr rest)))
584 ;; Else delete this object.
585 (t (setcdr rest (cdr (cdr rest))))))
587 ;; Drop the car.
588 (cdr circle-table)
593 (defun cust-print-walk-circle-tree (object)
594 (let (read-equivalent-p tag)
595 (while object
596 (setq read-equivalent-p
597 (or (numberp object)
598 (and (symbolp object)
599 ;; Check if it is uninterned.
600 (eq object (intern-soft (symbol-name object)))))
601 tag (and (not read-equivalent-p)
602 (assq object (cdr circle-table))))
603 (cond (tag
604 ;; Seen this object already, so note that.
605 (setcdr tag t))
607 ((not read-equivalent-p)
608 ;; Add a tag for this object.
609 (setcdr circle-table
610 (cons (list object)
611 (cdr circle-table)))))
612 (setq object
613 (cond
614 (tag ;; No need to descend since we have already.
615 nil)
617 ((consp object)
618 ;; Walk the car of the list recursively.
619 (cust-print-walk-circle-tree (car object))
620 ;; But walk the cdr with the above while loop
621 ;; to avoid problems with max-lisp-eval-depth.
622 ;; And it should be faster than recursion.
623 (cdr object))
625 ((vectorp object)
626 ;; Walk the vector.
627 (let ((i (length object))
628 (j 0))
629 (while (< j i)
630 (cust-print-walk-circle-tree (aref object j))
631 (setq j (1+ j))))))))))
634 ;; Example.
635 ;;=======================================
637 '(progn
638 (progn
639 ;; Create some circular structures.
640 (setq circ-sym (let ((x (make-symbol "FOO"))) (list x x)))
641 (setq circ-list (list 'a 'b (vector 1 2 3 4) 'd 'e 'f))
642 (setcar (nthcdr 3 circ-list) circ-list)
643 (aset (nth 2 circ-list) 2 circ-list)
644 (setq dotted-circ-list (list 'a 'b 'c))
645 (setcdr (cdr (cdr dotted-circ-list)) dotted-circ-list)
646 (setq circ-vector (vector 1 2 3 4 (list 'a 'b 'c 'd) 6 7))
647 (aset circ-vector 5 (make-symbol "-gensym-"))
648 (setcar (cdr (aref circ-vector 4)) (aref circ-vector 5))
649 nil)
651 (install-custom-print)
652 ;; (setq print-circle t)
654 (let ((print-circle t))
655 (or (equal (prin1-to-string circ-list) "#1=(a b [1 2 #1# 4] #1# e f)")
656 (error "circular object with array printing")))
658 (let ((print-circle t))
659 (or (equal (prin1-to-string dotted-circ-list) "#1=(a b c . #1#)")
660 (error "circular object with array printing")))
662 (let* ((print-circle t)
663 (x (list 'p 'q))
664 (y (list (list 'a 'b) x 'foo x)))
665 (setcdr (cdr (cdr (cdr y))) (cdr y))
666 (or (equal (prin1-to-string y) "((a b) . #1=(#2=(p q) foo #2# . #1#))"
668 (error "circular list example from CL manual")))
670 (let ((print-circle nil))
671 ;; cl-packages.el is required to print uninterned symbols like #:FOO.
672 ;; (require 'cl-packages)
673 (or (equal (prin1-to-string circ-sym) "(#:FOO #:FOO)")
674 (error "uninterned symbols in list")))
675 (let ((print-circle t))
676 (or (equal (prin1-to-string circ-sym) "(#1=FOO #1#)")
677 (error "circular uninterned symbols in list")))
679 (uninstall-custom-print)
682 (provide 'cust-print)
684 ;;; cust-print.el ends here