Search code examples
lispcommon-lisppretty-printsbcl

Lisp Pretty Print Object on One Line


I'm writing a list of lists to a .dot file to generate a tree. It seems that the pretty printer in lisp inserts new lines in the output so when I execute the dot file, the new lines make it hard to read the generated graph.

I've looked at the format documentation, but I haven't quite been able to make good use of it.

Here is a sample list that should be printed:

(((TOWER 'D 'C ^LEFT '0 ^RIGHT '2 ^BOTTOM '4 ^TOP '8)
  (TOWER 'C 'B ^LEFT '0 ^RIGHT '2 ^BOTTOM '2 ^TOP '6)
  (TOWER 'B 'A ^LEFT '0 ^RIGHT '2 ^BOTTOM '0 ^TOP '4) (ON 'D 'C)
  (ON 'C 'B) (ON 'B 'A))
 ((TOWER 'D 'C ^LEFT '0 ^RIGHT '2 ^BOTTOM '4 ^TOP '8)
  (TOWER 'C 'B ^LEFT '0 ^RIGHT '2 ^BOTTOM '2 ^TOP '6)
  (TOWER 'B 'A ^LEFT '0 ^RIGHT '2 ^BOTTOM '0 ^TOP '4) (ON 'D 'C)
  (ON 'C 'B) (ON 'B 'A))
 ((TOWER 'D 'C ^LEFT '0 ^RIGHT '2 ^BOTTOM '4 ^TOP '8)
  (TOWER 'C 'B ^LEFT '0 ^RIGHT '2 ^BOTTOM '2 ^TOP '6)
  (TOWER 'B 'A ^LEFT '0 ^RIGHT '2 ^BOTTOM '0 ^TOP '4) (ON 'D 'C)
  (ON 'C 'B) (ON 'B 'A))
 ((TOWER 'D 'C ^LEFT '0 ^RIGHT '2 ^BOTTOM '4 ^TOP '8)
  (TOWER 'C 'B ^LEFT '0 ^RIGHT '2 ^BOTTOM '2 ^TOP '6)
  (TOWER 'B 'A ^LEFT '0 ^RIGHT '2 ^BOTTOM '0 ^TOP '4) (ON 'D 'C)
  (ON 'C 'B) (ON 'B 'A))
 ((TOWER 'D 'C ^LEFT '0 ^RIGHT '2 ^BOTTOM '4 ^TOP '8)
  (TOWER 'C 'B ^LEFT '0 ^RIGHT '2 ^BOTTOM '2 ^TOP '6)
  (TOWER 'B 'A ^LEFT '0 ^RIGHT '2 ^BOTTOM '0 ^TOP '4) (ON 'D 'C)
  (ON 'C 'B) (ON 'B 'A))
 ((TOWER 'D 'C ^LEFT '0 ^RIGHT '2 ^BOTTOM '4 ^TOP '8)
  (TOWER 'C 'B ^LEFT '0 ^RIGHT '2 ^BOTTOM '2 ^TOP '6)
  (TOWER 'B 'A ^LEFT '0 ^RIGHT '2 ^BOTTOM '0 ^TOP '4) (ON 'D 'C)
  (ON 'C 'B) (ON 'B 'A)))

Here is how the list gets printed to the file:

(((TOWER 'D 'C
^LEFT '0
^RIGHT '2
^BOTTOM '4
^TOP '8)
(TOWER 'C 'B
^LEFT '0
^RIGHT '2
^BOTTOM '2
^TOP '6)
(TOWER 'B 'A
^LEFT '0
^RIGHT '2
^BOTTOM '0
^TOP '4)
(ON 'D 'C)
(ON 'C 'B)
(ON 'B 'A))
((TOWER 'D 'C
^LEFT '0
^RIGHT '2
^BOTTOM '4
^TOP '8)
(TOWER 'C 'B
^LEFT '0
^RIGHT '2
^BOTTOM '2
^TOP '6)
(TOWER 'B 'A
^LEFT '0
^RIGHT '2
^BOTTOM '0
^TOP '4)
(ON 'D 'C)
(ON 'C 'B)
(ON 'B 'A))
((TOWER 'D 'C
^LEFT '0
^RIGHT '2
^BOTTOM '4
^TOP '8)
(TOWER 'C 'B
^LEFT '0
^RIGHT '2
^BOTTOM '2
^TOP '6)
(TOWER 'B 'A
^LEFT '0
^RIGHT '2
^BOTTOM '0
...
...
...

What format directives should use to get the properly formatted output?

Here is the code I'm using

(format stream "~A [shape=record, label=\"{~A|~A|~A}\"];~%" a b c d)

Solution

  • If you set *print-right-margin* to a very large number, the pretty printer should not insert any newlines.

    It defaults to nil to mean the width of the device (e.g., your monitor).