cl-prevalence icon indicating copy to clipboard operation
cl-prevalence copied to clipboard

sexp.lisp: Option to insert line breaks.

Open Ambrevar opened this issue 5 years ago • 0 comments

Instead of outputting everything on one line, it's be nice to leave some customization options to the users so that they can decide when to insert line breaks.

For instance:

(export '*one-element-per-line*)
(defvar *one-element-per-line* nil
  "If non-nil, print one element per line in sequence types.")

(defun maybe-newline (stream)
  (when *one-element-per-line*
    (write-char #\newline stream)))

(defmethod serialize-sexp-internal ((object sequence) stream serialization-state)
  "Like the original `serialize-sexp-internal' but uses `*one-element-per-line* to "
  (flet ((proper-sequence (length)
           (let ((id (set-known-object serialization-state object)))
             (write-string "(:SEQUENCE " stream)
             (prin1 id stream)
             (write-string " :CLASS " stream)
             (print-symbol (etypecase object (list 'list) (vector 'vector)) stream)
             (write-string " :SIZE " stream)
             (prin1 length stream)
             (unless (zerop length)
               (write-string " :ELEMENTS (" stream)
               (maybe-newline stream)
               (map nil
                    #'(lambda (element)
                        (write-string " " stream)
                        (serialize-sexp-internal element stream serialization-state)
                        (maybe-newline stream))
                    object))
             (write-string " ) )" stream)))
         (improper-list ()
           (let ((id (set-known-object serialization-state object)))
             (write-string "(:CONS " stream)
             (prin1 id stream)
             (write-char #\Space stream)
             (serialize-sexp-internal (car object) stream serialization-state)
             (write-char #\Space stream)
             (serialize-sexp-internal (cdr object) stream serialization-state)
             (write-string " ) " stream))))
    (let ((id (known-object-id serialization-state object)))
      (if id
          (progn
            (write-string "(:REF . " stream)
            (prin1 id stream)
            (write-string ")" stream))
          (multiple-value-bind (seq-type length) (sequence-type-and-length object)
            (ecase seq-type
              ((:proper-sequence :proper-list) (proper-sequence length))
              ((:dotted-list :circular-list) (improper-list))))))))

The above function is like the original except for the maybe-newline calls.

Ambrevar avatar Sep 30 '19 13:09 Ambrevar