Sometimes lispify is used to create lisp forms that will be read (and perhaps edited) by humans, in which case it is helpful to have reasonably short lines with good indentation. E.g., it's hard for a human to read this:
(:attribute (:end_time "00:04:04.400000" :start_time "00:03:42.030000" :title "Introduction to 'Modernity in Interwar Japan'") :cls "edx-video" :sentence-annotation "The Japanese knew about steam-driven warships but hadn't seen them before the 'Black Ships' arrived." :source "manual")
and much easier to read this:
(:attribute (:end_time "00:04:04.400000"
:start_time "00:03:42.030000"
:title "Introduction to 'Modernity in Interwar Japan'")
:cls "edx-video"
:sentence-annotation "The Japanese knew about steam-driven warships but hadn't seen them before the 'Black Ships' arrived."
:source "manual")
(Most elements of the form fit nicely and the one excessively long element doesn't affect the readability of the rest of the form.)
Add a pretty printing capability to lispify. A sophisticated implementation of line-breaking and indentation is pretty hard, but isn't necessary. It shouldn't be too difficult to implement something slightly better than a naïve implementation (see below)—something that balances adequate line-breaking with acceptable implementation cost.
Suggested operation:
Add a boolean command line argument for whether to pretty-print or not. Default to not.
Add a command line argument for the desired right margin. When not pretty-printing, ignore this argument. When pretty-printing, if this argument isn't supplied, default to something reasonable, like 70 or 75 or 80 characters.
A naïve implementation of pretty-printing is: before outputting any form, if the current column is already greater than the right margin, output a newline and enough spaces to reach the current indention level. This doesn't require any lookahead but will result in more lines that exceed the margin and often results in output getting crowded toward the right margin. E.g., with a 23-character margin, this is the naïve output:
Since the goal is human readability, it would be reasonable to err on the side of more, shorter lines, but the algorithm shouldn't be so stupidly naïve as to break after every single form.
When outputting a plist, keep a key and its value on the same line (unless this causes the margin to be overrun in a way that can reasonably be detected and prevented). Key–value pairs shouldn't increase the prevailing indentation level (which is the column for the beginning of the previous form at this level, and gets popped when the current form gets closed). E.g., (in the absence of any knowledge of whether a parenthesized form is a function call or a list of values) correct indentation is ordinarily:
(thing1 thing2
thing3 thing4)
with `thing3` indented to the same column as `thing2`. However, a plist should be indented like this:
```
(:key1 value1
:key2 value2)
```
Sometimes
lispify
is used to create lisp forms that will be read (and perhaps edited) by humans, in which case it is helpful to have reasonably short lines with good indentation. E.g., it's hard for a human to read this:and much easier to read this:
(Most elements of the form fit nicely and the one excessively long element doesn't affect the readability of the rest of the form.)
Add a pretty printing capability to
lispify
. A sophisticated implementation of line-breaking and indentation is pretty hard, but isn't necessary. It shouldn't be too difficult to implement something slightly better than a naïve implementation (see below)—something that balances adequate line-breaking with acceptable implementation cost.Suggested operation:
whereas this would be more readable:
When outputting a plist, keep a key and its value on the same line (unless this causes the margin to be overrun in a way that can reasonably be detected and prevented). Key–value pairs shouldn't increase the prevailing indentation level (which is the column for the beginning of the previous form at this level, and gets popped when the current form gets closed). E.g., (in the absence of any knowledge of whether a parenthesized form is a function call or a list of values) correct indentation is ordinarily: