@@ -40,8 +40,9 @@ This difference is reflected in the `:style :community`:
4040```
4141This change to the defaults for zprint does several things:
4242
43- * Do not indent the second element of a pair when you have to flow
44- the pair.
43+ * Do not indent the second element of a pair when the second element
44+ of the pair does not fit on the same line as the first and must be
45+ started on the line under the first element.
4546
4647 * Do not format some functions specially to make them more understandable.
4748
@@ -52,7 +53,83 @@ This change to the defaults for zprint does several things:
5253
5354You can read lots about this [ here] ( ./pairs.md ) .
5455
55- Here is an example of the difference:
56+ Here is a simple example of the difference, where the width has been narrowed
57+ in order to force the second element onto the next line in each case:
58+ ``` clojure
59+ ; Here is what you get with the default zprint format with a normal width.
60+
61+ (czprint-fn pair-indent {:width 80 })
62+
63+ (defn pair-indent
64+ " An exmple showing how pairs are indented."
65+ [a b c d]
66+ (cond (nil? a) (list d)
67+ (nil? b) (list c d a b)
68+ :else (list a b c d)))
69+
70+ ; Here is what you get with the community formatting and a normal width.
71+ ; There is no difference between these two.
72+
73+ (czprint-fn pair-indent {:style :community :width 80 })
74+
75+ (defn pair-indent
76+ " An exmple showing how pairs are indented."
77+ [a b c d]
78+ (cond (nil? a) (list d)
79+ (nil? b) (list c d a b)
80+ :else (list a b c d)))
81+
82+ ; Here is the default zprint formatting, when the second element of a
83+ ; cond pair is indented when it formats onto the next line due to the
84+ ; narrow width.
85+
86+ (czprint-fn pair-indent {:width 22 })
87+
88+ (defn pair-indent
89+ " An exmple showing how pairs are indented."
90+ [a b c d]
91+ (cond
92+ (nil? a) (list d)
93+ (nil? b)
94+ (list c d a b)
95+ :else
96+ (list a b c d)))
97+
98+ ; Here is the community formatting, where the second element of a
99+ ; cond pair is aligned with the first element when it formats onto the
100+ ; next line due to the narrow width.
101+
102+ (czprint-fn pair-indent {:style :community :width 22 })
103+
104+ (defn pair-indent
105+ " An exmple showing how pairs are indented."
106+ [a b c d]
107+ (cond
108+ (nil? a) (list d)
109+ (nil? b)
110+ (list c d a b)
111+ :else
112+ (list a b c d)))
113+
114+ ; Some peope like to separate the pairs that end up on the next line
115+ ; with a blank line
116+
117+ (czprint-fn pair-indent {:style [:community :pair-nl ] :width 22 })
118+
119+ (defn pair-indent
120+ " An exmple showing how pairs are indented."
121+ [a b c d]
122+ (cond
123+ (nil? a) (list d)
124+ (nil? b)
125+ (list c d a b)
126+
127+ :else
128+ (list a b c d)))
129+
130+ ```
131+
132+ Here is a more realistic example of the difference:
56133
57134``` clojure
58135(czprint-fn cond-let)
@@ -102,7 +179,7 @@ At some point, the "community standards" for Clojure source formatting
102179made a distinction between "body functions" and "argument functions",
103180and wanted "argument functions" to have an indent of 1, and "body functions"
104181to have an indent of 2. The theory seemed to be that "body functions"
105- were functions which had executable forms in them, oftern (though not
182+ were functions which had executable forms in them, often (though not
106183always) of indeterminate number. "Argument functions", on the other
107184hand, had arguments (typically a fixed number) which were values and
108185not primarily executable forms.
@@ -113,8 +190,87 @@ types, and will also accept a value for `:indent-arg`, which (if non-nil)
113190will be used as the indent for argument functions (which is
114191everything that is not explicitly classified as a body function).
115192
116- Here is an example that illustrates the different indent for a body
117- function as well as the indent for the second element of a pair:
193+ Here is a simple (and contrived) example that illustrates the difference
194+ between the default zprint indent of 2 for all lists with a symbol as the
195+ first element, and the community formatting which has different indents
196+ for different types of functions:
197+
198+ ``` clojure
199+ ; Note: if you don't restrict the width, both {:style :community} and the
200+ ; default zprint formatting are identical
201+
202+ ; The default formatting with restricted width to force things onto
203+ ; subsequent lines
204+
205+ (czprint-fn body-indent {:width 16 })
206+ (defn
207+ body-indent
208+ " An example showing how indent for body fns differs from argument fns."
209+ [thing
210+ something-else
211+ ala bala
212+ portokala]
213+ ; Body functions
214+ (when thing
215+ (something-else ))
216+ (with-out-str
217+ (prn " Hi" )
218+ (prn " You" ))
219+ ; Argument functions
220+ (filter even?
221+ (list
222+ (range
223+ 1
224+ 10 )
225+ (range
226+ 100
227+ 1000 )))
228+ (or
229+ ala
230+ (list
231+ bala
232+ ala
233+ bala)
234+ portokala))
235+
236+ ; The community formatting, also with restricted width. The body functions
237+ ; don't change, but look at filter, or, list (the first one), and range -- the
238+ ; indent on all of these functions is one less than that above.
239+
240+ (czprint-fn body-indent {:style :community :width 16 })
241+
242+ (defn
243+ body-indent
244+ " An example showing how indent for body fns differs from argument fns."
245+ [thing
246+ something-else
247+ ala bala
248+ portokala]
249+ ; Body functions
250+ (when thing
251+ (something-else ))
252+ (with-out-str
253+ (prn " Hi" )
254+ (prn " You" ))
255+ ; Argument functions
256+ (filter
257+ even?
258+ (list
259+ (range 1 10 )
260+ (range
261+ 100
262+ 1000 )))
263+ (or
264+ ala
265+ (list bala
266+ ala
267+ bala)
268+ portokala))
269+ ```
270+
271+ Here is a more realistic (and more confusing) example that illustrates
272+ the different indent for a body function as well as the indent for the
273+ second element of a pair:
118274
119275``` clojure
120276(czprint-fn with-open)
0 commit comments