| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
(defgroup cust-print nil |
|---|
| 88 |
"Handles print-level and print-circle." |
|---|
| 89 |
:prefix "print-" |
|---|
| 90 |
:group 'lisp |
|---|
| 91 |
:group 'extensions) |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
'(defpackage "cust-print" |
|---|
| 96 |
(:nicknames "CP" "custom-print") |
|---|
| 97 |
(:use "el") |
|---|
| 98 |
(:export |
|---|
| 99 |
print-level |
|---|
| 100 |
print-circle |
|---|
| 101 |
|
|---|
| 102 |
custom-print-install |
|---|
| 103 |
custom-print-uninstall |
|---|
| 104 |
custom-print-installed-p |
|---|
| 105 |
with-custom-print |
|---|
| 106 |
|
|---|
| 107 |
custom-prin1 |
|---|
| 108 |
custom-princ |
|---|
| 109 |
custom-prin1-to-string |
|---|
| 110 |
custom-print |
|---|
| 111 |
custom-format |
|---|
| 112 |
custom-message |
|---|
| 113 |
custom-error |
|---|
| 114 |
|
|---|
| 115 |
custom-printers |
|---|
| 116 |
add-custom-printer |
|---|
| 117 |
)) |
|---|
| 118 |
|
|---|
| 119 |
'(in-package cust-print) |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
(eval-and-compile |
|---|
| 124 |
(or (fboundp 'defalias) (fset 'defalias 'fset))) |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
(defcustom print-level nil |
|---|
| 135 |
"*Controls how many levels deep a nested data object will print. |
|---|
| 136 |
|
|---|
| 137 |
If nil, printing proceeds recursively and may lead to |
|---|
| 138 |
max-lisp-eval-depth being exceeded or an error may occur: |
|---|
| 139 |
`Apparently circular structure being printed.' |
|---|
| 140 |
Also see `print-length' and `print-circle'. |
|---|
| 141 |
|
|---|
| 142 |
If non-nil, components at levels equal to or greater than `print-level' |
|---|
| 143 |
are printed simply as `#'. The object to be printed is at level 0, |
|---|
| 144 |
and if the object is a list or vector, its top-level components are at |
|---|
| 145 |
level 1." |
|---|
| 146 |
:type '(choice (const nil) integer) |
|---|
| 147 |
:group 'cust-print) |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
(defcustom print-circle nil |
|---|
| 151 |
"*Controls the printing of recursive structures. |
|---|
| 152 |
|
|---|
| 153 |
If nil, printing proceeds recursively and may lead to |
|---|
| 154 |
`max-lisp-eval-depth' being exceeded or an error may occur: |
|---|
| 155 |
\"Apparently circular structure being printed.\" Also see |
|---|
| 156 |
`print-length' and `print-level'. |
|---|
| 157 |
|
|---|
| 158 |
If non-nil, shared substructures anywhere in the structure are printed |
|---|
| 159 |
with `#N=' before the first occurrence (in the order of the print |
|---|
| 160 |
representation) and `#N#' in place of each subsequent occurrence, |
|---|
| 161 |
where N is a positive decimal integer. |
|---|
| 162 |
|
|---|
| 163 |
There is no way to read this representation in standard Emacs, |
|---|
| 164 |
but if you need to do so, try the cl-read.el package." |
|---|
| 165 |
:type 'boolean |
|---|
| 166 |
:group 'cust-print) |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
(defcustom custom-print-vectors nil |
|---|
| 170 |
"*Non-nil if printing of vectors should obey print-level and print-length. |
|---|
| 171 |
|
|---|
| 172 |
For Emacs 18, setting print-level, or adding custom print list or |
|---|
| 173 |
vector handling will make this happen anyway. Emacs 19 obeys |
|---|
| 174 |
print-level, but not for vectors." |
|---|
| 175 |
:type 'boolean |
|---|
| 176 |
:group 'cust-print) |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
(defvar custom-printers nil |
|---|
| 183 |
|
|---|
| 184 |
"An alist for custom printing of any type. |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
) |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
(defun add-custom-printer (pred printer) |
|---|
| 198 |
"Add a pair of PREDICATE and PRINTER to `custom-printers'. |
|---|
| 199 |
Any pair that has the same PREDICATE is first removed." |
|---|
| 200 |
(setq custom-printers (cons (cons pred printer) |
|---|
| 201 |
(delq (assq pred custom-printers) |
|---|
| 202 |
custom-printers))) |
|---|
| 203 |
|
|---|
| 204 |
(cust-print-update-custom-printers)) |
|---|
| 205 |
|
|---|
| 206 |
(defun delete-custom-printer (pred) |
|---|
| 207 |
"Delete the custom printer associated with PREDICATE." |
|---|
| 208 |
(setq custom-printers (delq (assq pred custom-printers) |
|---|
| 209 |
custom-printers)) |
|---|
| 210 |
(cust-print-update-custom-printers)) |
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
(defun cust-print-use-custom-printer (object) |
|---|
| 214 |
|
|---|
| 215 |
nil) |
|---|
| 216 |
|
|---|
| 217 |
(defun cust-print-update-custom-printers () |
|---|
| 218 |
|
|---|
| 219 |
(defalias 'cust-print-use-custom-printer |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
`(lambda (object) |
|---|
| 223 |
(cond |
|---|
| 224 |
,@(mapcar (function |
|---|
| 225 |
(lambda (pair) |
|---|
| 226 |
`((,(car pair) object) |
|---|
| 227 |
(,(cdr pair) object)))) |
|---|
| 228 |
custom-printers) |
|---|
| 229 |
|
|---|
| 230 |
(t nil) |
|---|
| 231 |
)) |
|---|
| 232 |
|
|---|
| 233 |
)) |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
(defun cust-print-set-function-cell (symbol-pair) |
|---|
| 240 |
(defalias (car symbol-pair) |
|---|
| 241 |
(symbol-function (car (cdr symbol-pair))))) |
|---|
| 242 |
|
|---|
| 243 |
(defun cust-print-original-princ (object &optional stream)) |
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
(if (not (fboundp 'cust-print-original-prin1)) |
|---|
| 247 |
(mapcar 'cust-print-set-function-cell |
|---|
| 248 |
'((cust-print-original-prin1 prin1) |
|---|
| 249 |
(cust-print-original-princ princ) |
|---|
| 250 |
(cust-print-original-print print) |
|---|
| 251 |
(cust-print-original-prin1-to-string prin1-to-string) |
|---|
| 252 |
(cust-print-original-format format) |
|---|
| 253 |
(cust-print-original-message message) |
|---|
| 254 |
(cust-print-original-error error)))) |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
(defun custom-print-install () |
|---|
| 258 |
"Replace print functions with general, customizable, Lisp versions. |
|---|
| 259 |
The Emacs subroutines are saved away, and you can reinstall them |
|---|
| 260 |
by running `custom-print-uninstall'." |
|---|
| 261 |
(interactive) |
|---|
| 262 |
(mapcar 'cust-print-set-function-cell |
|---|
| 263 |
'((prin1 custom-prin1) |
|---|
| 264 |
(princ custom-princ) |
|---|
| 265 |
(print custom-print) |
|---|
| 266 |
(prin1-to-string custom-prin1-to-string) |
|---|
| 267 |
(format custom-format) |
|---|
| 268 |
(message custom-message) |
|---|
| 269 |
(error custom-error) |
|---|
| 270 |
)) |
|---|
| 271 |
t) |
|---|
| 272 |
|
|---|
| 273 |
(defun custom-print-uninstall () |
|---|
| 274 |
"Reset print functions to their Emacs subroutines." |
|---|
| 275 |
(interactive) |
|---|
| 276 |
(mapcar 'cust-print-set-function-cell |
|---|
| 277 |
'((prin1 cust-print-original-prin1) |
|---|
| 278 |
(princ cust-print-original-princ) |
|---|
| 279 |
(print cust-print-original-print) |
|---|
| 280 |
(prin1-to-string cust-print-original-prin1-to-string) |
|---|
| 281 |
(format cust-print-original-format) |
|---|
| 282 |
(message cust-print-original-message) |
|---|
| 283 |
(error cust-print-original-error) |
|---|
| 284 |
)) |
|---|
| 285 |
t) |
|---|
| 286 |
|
|---|
| 287 |
(defalias 'custom-print-funcs-installed-p 'custom-print-installed-p) |
|---|
| 288 |
(defun custom-print-installed-p () |
|---|
| 289 |
"Return t if custom-print is currently installed, nil otherwise." |
|---|
| 290 |
(eq (symbol-function 'custom-prin1) (symbol-function 'prin1))) |
|---|
| 291 |
|
|---|
| 292 |
(put 'with-custom-print-funcs 'edebug-form-spec '(body)) |
|---|
| 293 |
(put 'with-custom-print 'edebug-form-spec '(body)) |
|---|
| 294 |
|
|---|
| 295 |
(defalias 'with-custom-print-funcs 'with-custom-print) |
|---|
| 296 |
(defmacro with-custom-print (&rest body) |
|---|
| 297 |
"Temporarily install the custom print package while executing BODY." |
|---|
| 298 |
`(unwind-protect |
|---|
| 299 |
(progn |
|---|
| 300 |
(custom-print-install) |
|---|
| 301 |
,@body) |
|---|
| 302 |
(custom-print-uninstall))) |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
(defun custom-prin1 (object &optional stream) |
|---|
| 310 |
"Output the printed representation of OBJECT, any Lisp object. |
|---|
| 311 |
Quoting characters are printed when needed to make output that `read' |
|---|
| 312 |
can handle, whenever this is possible. |
|---|
| 313 |
Output stream is STREAM, or value of `standard-output' (which see). |
|---|
| 314 |
|
|---|
| 315 |
This is the custom-print replacement for the standard `prin1'. It |
|---|
| 316 |
uses the appropriate printer depending on the values of `print-level' |
|---|
| 317 |
and `print-circle' (which see)." |
|---|
| 318 |
(cust-print-top-level object stream 'cust-print-original-prin1)) |
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
(defun custom-princ (object &optional stream) |
|---|
| 322 |
"Output the printed representation of OBJECT, any Lisp object. |
|---|
| 323 |
No quoting characters are used; no delimiters are printed around |
|---|
| 324 |
the contents of strings. |
|---|
| 325 |
Output stream is STREAM, or value of `standard-output' (which see). |
|---|
| 326 |
|
|---|
| 327 |
This is the custom-print replacement for the standard `princ'." |
|---|
| 328 |
(cust-print-top-level object stream 'cust-print-original-princ)) |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
(defun custom-prin1-to-string (object &optional noescape) |
|---|
| 332 |
"Return a string containing the printed representation of OBJECT, |
|---|
| 333 |
any Lisp object. Quoting characters are used when needed to make output |
|---|
| 334 |
that `read' can handle, whenever this is possible, unless the optional |
|---|
| 335 |
second argument NOESCAPE is non-nil. |
|---|
| 336 |
|
|---|
| 337 |
This is the custom-print replacement for the standard `prin1-to-string'." |
|---|
| 338 |
(let ((buf (get-buffer-create " *custom-print-temp*"))) |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
(save-excursion |
|---|
| 342 |
(set-buffer buf) |
|---|
| 343 |
(erase-buffer)) |
|---|
| 344 |
|
|---|
| 345 |
(if noescape |
|---|
| 346 |
(custom-princ object buf) |
|---|
| 347 |
(custom-prin1 object buf)) |
|---|
| 348 |
(save-excursion |
|---|
| 349 |
(set-buffer buf) |
|---|
| 350 |
(buffer-string) |
|---|
| 351 |
|
|---|
| 352 |
))) |
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
(defun custom-print (object &optional stream) |
|---|
| 356 |
"Output the printed representation of OBJECT, with newlines around it. |
|---|
| 357 |
Quoting characters are printed when needed to make output that `read' |
|---|
| 358 |
can handle, whenever this is possible. |
|---|
| 359 |
Output stream is STREAM, or value of `standard-output' (which see). |
|---|
| 360 |
|
|---|
| 361 |
This is the custom-print replacement for the standard `print'." |
|---|
| 362 |
(cust-print-original-princ "\n" stream) |
|---|
| 363 |
(custom-prin1 object stream) |
|---|
| 364 |
(cust-print-original-princ "\n" stream)) |
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
(defun custom-format (fmt &rest args) |
|---|
| 368 |
"Format a string out of a control-string and arguments. |
|---|
| 369 |
The first argument is a control string. It, and subsequent arguments |
|---|
| 370 |
substituted into it, become the value, which is a string. |
|---|
| 371 |
It may contain %s or %d or %c to substitute successive following arguments. |
|---|
| 372 |
%s means print an argument as a string, %d means print as number in decimal, |
|---|
| 373 |
%c means print a number as a single character. |
|---|
| 374 |
The argument used by %s must be a string or a symbol; |
|---|
| 375 |
the argument used by %d, %b, %o, %x or %c must be a number. |
|---|
| 376 |
|
|---|
| 377 |
This is the custom-print replacement for the standard `format'. It |
|---|
| 378 |
calls the Emacs `format' after first making strings for list, |
|---|
| 379 |
vector, or symbol args. The format specification for such args should |
|---|
| 380 |
be `%s' in any case, so a string argument will also work. The string |
|---|
| 381 |
is generated with `custom-prin1-to-string', which quotes quotable |
|---|
| 382 |
characters." |
|---|
| 383 |
(apply 'cust-print-original-format fmt |
|---|
| 384 |
(mapcar (function (lambda (arg) |
|---|
| 385 |
(if (or (listp arg) (vectorp arg) (symbolp arg)) |
|---|
| 386 |
(custom-prin1-to-string arg) |
|---|
| 387 |
arg))) |
|---|
| 388 |
args))) |
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
(defun custom-message (fmt &rest args) |
|---|
| 392 |
"Print a one-line message at the bottom of the screen. |
|---|
| 393 |
The first argument is a control string. |
|---|
| 394 |
It may contain %s or %d or %c to print successive following arguments. |
|---|
| 395 |
%s means print an argument as a string, %d means print as number in decimal, |
|---|
| 396 |
%c means print a number as a single character. |
|---|
| 397 |
The argument used by %s must be a string or a symbol; |
|---|
| 398 |
the argument used by %d or %c must be a number. |
|---|
| 399 |
|
|---|
| 400 |
This is the custom-print replacement for the standard `message'. |
|---|
| 401 |
See `custom-format' for the details." |
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
(apply 'cust-print-original-message fmt |
|---|
| 408 |
(mapcar (function (lambda (arg) |
|---|
| 409 |
(if (or (listp arg) (vectorp arg) (symbolp arg)) |
|---|
| 410 |
(custom-prin1-to-string arg) |
|---|
| 411 |
arg))) |
|---|
| 412 |
args))) |
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
(defun custom-error (fmt &rest args) |
|---|
| 416 |
"Signal an error, making error message by passing all args to `format'. |
|---|
| 417 |
|
|---|
| 418 |
This is the custom-print replacement for the standard `error'. |
|---|
| 419 |
See `custom-format' for the details." |
|---|
| 420 |
(signal 'error (list (apply 'custom-format fmt args)))) |
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
(defvar circle-table) |
|---|
| 429 |
(defvar cust-print-current-level) |
|---|
| 430 |
|
|---|
| 431 |
(defun cust-print-original-printer (object)) |
|---|
| 432 |
(defun cust-print-low-level-prin (object)) |
|---|
| 433 |
(defun cust-print-prin (object)) |
|---|
| 434 |
|
|---|
| 435 |
(defun cust-print-top-level (object stream emacs-printer) |
|---|
| 436 |
|
|---|
| 437 |
(let ((standard-output (or stream standard-output)) |
|---|
| 438 |
|
|---|
| 439 |
(circle-table (and print-circle |
|---|
| 440 |
(cust-print-preprocess-circle-tree object))) |
|---|
| 441 |
(cust-print-current-level (or print-level -1))) |
|---|
| 442 |
|
|---|
| 443 |
(defalias 'cust-print-original-printer emacs-printer) |
|---|
| 444 |
(defalias 'cust-print-low-level-prin |
|---|
| 445 |
(cond |
|---|
| 446 |
((or custom-printers |
|---|
| 447 |
circle-table |
|---|
| 448 |
print-level |
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
(if custom-print-vectors |
|---|
| 452 |
(or print-level print-length))) |
|---|
| 453 |
'cust-print-print-object) |
|---|
| 454 |
(t 'cust-print-original-printer))) |
|---|
| 455 |
(defalias 'cust-print-prin |
|---|
| 456 |
(if circle-table 'cust-print-print-circular 'cust-print-low-level-prin)) |
|---|
| 457 |
|
|---|
| 458 |
(cust-print-prin object) |
|---|
| 459 |
object)) |
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
(defun cust-print-print-object (object) |
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
(cond |
|---|
| 466 |
((null object) (cust-print-original-printer object)) |
|---|
| 467 |
((cust-print-use-custom-printer object) object) |
|---|
| 468 |
((consp object) (cust-print-list object)) |
|---|
| 469 |
((vectorp object) (cust-print-vector object)) |
|---|
| 470 |
|
|---|
| 471 |
(t (cust-print-original-printer object)))) |
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
(defun cust-print-print-circular (object) |
|---|
| 475 |
|
|---|
| 476 |
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 |
(let ((tag (assq object circle-table))) |
|---|
| 480 |
(if tag |
|---|
| 481 |
(let ((id (cdr tag))) |
|---|
| 482 |
(if (> id 0) |
|---|
| 483 |
(progn |
|---|
| 484 |
|
|---|
| 485 |
(cust-print-original-princ "#") |
|---|
| 486 |
(cust-print-original-princ id) |
|---|
| 487 |
(cust-print-original-princ "#")) |
|---|
| 488 |
|
|---|
| 489 |
(setcdr tag (- id)) |
|---|
| 490 |
(cust-print-original-princ "#") |
|---|
| 491 |
(cust-print-original-princ (- id)) |
|---|
| 492 |
(cust-print-original-princ "=") |
|---|
| 493 |
(cust-print-low-level-prin object) |
|---|
| 494 |
)) |
|---|
| 495 |
|
|---|
| 496 |
(cust-print-low-level-prin object)))) |
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
(defun cust-print-list (list) |
|---|
| 503 |
|
|---|
| 504 |
(if (= cust-print-current-level 0) |
|---|
| 505 |
(cust-print-original-princ "#") |
|---|
| 506 |
(let ((cust-print-current-level (1- cust-print-current-level))) |
|---|
| 507 |
(cust-print-original-princ "(") |
|---|
| 508 |
(let ((length (or print-length 0))) |
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
(cust-print-prin (car list)) |
|---|
| 512 |
(setq list (cdr list)) |
|---|
| 513 |
(if list (cust-print-original-princ " ")) |
|---|
| 514 |
(setq length (1- length)) |
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
(while (and list (/= 0 length)) |
|---|
| 518 |
(if (and (listp list) |
|---|
| 519 |
(not (assq list circle-table))) |
|---|
| 520 |
(progn |
|---|
| 521 |
(cust-print-prin (car list)) |
|---|
| 522 |
(setq list (cdr list))) |
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 |
(cust-print-original-princ ". ") |
|---|
| 526 |
(cust-print-prin list) |
|---|
| 527 |
(setq list nil)) |
|---|
| 528 |
|
|---|
| 529 |
(setq length (1- length)) |
|---|
| 530 |
(if list (cust-print-original-princ " "))) |
|---|
| 531 |
|
|---|
| 532 |
(if (and list (= length 0)) (cust-print-original-princ "...")) |
|---|
| 533 |
(cust-print-original-princ ")")))) |
|---|
| 534 |
list) |
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
(defun cust-print-vector (vector) |
|---|
| 538 |
|
|---|
| 539 |
(if (= cust-print-current-level 0) |
|---|
| 540 |
(cust-print-original-princ "#") |
|---|
| 541 |
(let ((cust-print-current-level (1- cust-print-current-level)) |
|---|
| 542 |
(i 0) |
|---|
| 543 |
(len (length vector))) |
|---|
| 544 |
(cust-print-original-princ "[") |
|---|
| 545 |
|
|---|
| 546 |
(if print-length |
|---|
| 547 |
(setq len (min print-length len))) |
|---|
| 548 |
|
|---|
| 549 |
(while (< i len) |
|---|
| 550 |
(cust-print-prin (aref vector i)) |
|---|
| 551 |
(setq i (1+ i)) |
|---|
| 552 |
(if (< i (length vector)) (cust-print-original-princ " "))) |
|---|
| 553 |
|
|---|
| 554 |
(if (< i (length vector)) (cust-print-original-princ "...")) |
|---|
| 555 |
(cust-print-original-princ "]") |
|---|
| 556 |
)) |
|---|
| 557 |
vector) |
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
(defun cust-print-preprocess-circle-tree (object) |
|---|
| 565 |
|
|---|
| 566 |
(let ( |
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
|
|---|
| 573 |
(circle-table (list nil))) |
|---|
| 574 |
(cust-print-walk-circle-tree object) |
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
(setcdr circle-table (nreverse (cdr circle-table))) |
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 |
|
|---|
| 584 |
(let ((rest circle-table) |
|---|
| 585 |
(id -1)) |
|---|
| 586 |
(while (cdr rest) |
|---|
| 587 |
(let ((tag (car (cdr rest)))) |
|---|
| 588 |
(cond ((cdr tag) |
|---|
| 589 |
(setcdr tag id) |
|---|
| 590 |
(setq id (1- id)) |
|---|
| 591 |
(setq rest (cdr rest))) |
|---|
| 592 |
|
|---|
| 593 |
(t (setcdr rest (cdr (cdr rest)))))) |
|---|
| 594 |
)) |
|---|
| 595 |
|
|---|
| 596 |
(cdr circle-table) |
|---|
| 597 |
)) |
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 |
(defun cust-print-walk-circle-tree (object) |
|---|
| 602 |
(let (read-equivalent-p tag) |
|---|
| 603 |
(while object |
|---|
| 604 |
(setq read-equivalent-p |
|---|
| 605 |
(or (numberp object) |
|---|
| 606 |
(and (symbolp object) |
|---|
| 607 |
|
|---|
| 608 |
(eq object (intern-soft (symbol-name object))))) |
|---|
| 609 |
tag (and (not read-equivalent-p) |
|---|
| 610 |
(assq object (cdr circle-table)))) |
|---|
| 611 |
(cond (tag |
|---|
| 612 |
|
|---|
| 613 |
(setcdr tag t)) |
|---|
| 614 |
|
|---|
| 615 |
((not read-equivalent-p) |
|---|
| 616 |
|
|---|
| 617 |
(setcdr circle-table |
|---|
| 618 |
(cons (list object) |
|---|
| 619 |
(cdr circle-table))))) |
|---|
| 620 |
(setq object |
|---|
| 621 |
(cond |
|---|
| 622 |
(tag |
|---|
| 623 |
nil) |
|---|
| 624 |
|
|---|
| 625 |
((consp object) |
|---|
| 626 |
|
|---|
| 627 |
(cust-print-walk-circle-tree (car object)) |
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
(cdr object)) |
|---|
| 632 |
|
|---|
| 633 |
((vectorp object) |
|---|
| 634 |
|
|---|
| 635 |
(let ((i (length object)) |
|---|
| 636 |
(j 0)) |
|---|
| 637 |
(while (< j i) |
|---|
| 638 |
(cust-print-walk-circle-tree (aref object j)) |
|---|
| 639 |
(setq j (1+ j)))))))))) |
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
'(progn |
|---|
| 646 |
(progn |
|---|
| 647 |
|
|---|
| 648 |
(setq circ-sym (let ((x (make-symbol "FOO"))) (list x x))) |
|---|
| 649 |
(setq circ-list (list 'a 'b (vector 1 2 3 4) 'd 'e 'f)) |
|---|
| 650 |
(setcar (nthcdr 3 circ-list) circ-list) |
|---|
| 651 |
(aset (nth 2 circ-list) 2 circ-list) |
|---|
| 652 |
(setq dotted-circ-list (list 'a 'b 'c)) |
|---|
| 653 |
(setcdr (cdr (cdr dotted-circ-list)) dotted-circ-list) |
|---|
| 654 |
(setq circ-vector (vector 1 2 3 4 (list 'a 'b 'c 'd) 6 7)) |
|---|
| 655 |
(aset circ-vector 5 (make-symbol "-gensym-")) |
|---|
| 656 |
(setcar (cdr (aref circ-vector 4)) (aref circ-vector 5)) |
|---|
| 657 |
nil) |
|---|
| 658 |
|
|---|
| 659 |
(install-custom-print) |
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
(let ((print-circle t)) |
|---|
| 663 |
(or (equal (prin1-to-string circ-list) "#1=(a b [1 2 #1# 4] #1# e f)") |
|---|
| 664 |
(error "circular object with array printing"))) |
|---|
| 665 |
|
|---|
| 666 |
(let ((print-circle t)) |
|---|
| 667 |
(or (equal (prin1-to-string dotted-circ-list) "#1=(a b c . #1#)") |
|---|
| 668 |
(error "circular object with array printing"))) |
|---|
| 669 |
|
|---|
| 670 |
(let* ((print-circle t) |
|---|
| 671 |
(x (list 'p 'q)) |
|---|
| 672 |
(y (list (list 'a 'b) x 'foo x))) |
|---|
| 673 |
(setcdr (cdr (cdr (cdr y))) (cdr y)) |
|---|
| 674 |
(or (equal (prin1-to-string y) "((a b) . #1=(#2=(p q) foo #2# . #1#))" |
|---|
| 675 |
) |
|---|
| 676 |
(error "circular list example from CL manual"))) |
|---|
| 677 |
|
|---|
| 678 |
(let ((print-circle nil)) |
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
(or (equal (prin1-to-string circ-sym) "(#:FOO #:FOO)") |
|---|
| 682 |
(error "uninterned symbols in list"))) |
|---|
| 683 |
(let ((print-circle t)) |
|---|
| 684 |
(or (equal (prin1-to-string circ-sym) "(#1=FOO #1#)") |
|---|
| 685 |
(error "circular uninterned symbols in list"))) |
|---|
| 686 |
|
|---|
| 687 |
(uninstall-custom-print) |
|---|
| 688 |
) |
|---|
| 689 |
|
|---|
| 690 |
(provide 'cust-print) |
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
|
|---|