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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
;;; pprint_base.clj -- part of the pretty printer for Clojure
;; by Tom Faulhaber
;; April 3, 2009
; Copyright (c) Tom Faulhaber, Jan 2009. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
;; This module implements the generic pretty print functions and special variables
(in-ns 'clojure.contrib.pprint)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Variables that control the pretty printer
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; *print-length*, *print-level* and *print-dup* are defined in clojure.core
;;; TODO: use *print-dup* here (or is it supplanted by other variables?)
;;; TODO: make dispatch items like "(let..." get counted in *print-length*
;;; constructs
(def
#^{ :doc "Bind to true if you want write to use pretty printing"}
*print-pretty* true)
;;; TODO: implement true data-driven dispatch
(defonce ; If folks have added stuff here, don't overwrite
#^{ :doc "The pretty print dispatch table"}
*print-pprint-dispatch* (ref []))
(def
#^{ :doc "Pretty printing will try to avoid anything going beyond this column."}
*print-right-margin* 72)
(def
#^{ :doc "The column at which to enter miser style. Depending on the dispatch table,
miser style add newlines in more places to try to keep lines short allowing for further
levels of nesting."}
*print-miser-width* 40)
;;; TODO implement output limiting
(def
#^{ :doc "Maximum number of lines to print in a pretty print instance (N.B. This is not yet used)"}
*print-lines* nil)
;;; TODO: implement circle and shared
(def
#^{ :doc "Mark circular structures (N.B. This is not yet used)"}
*print-circle* nil)
;;; TODO: should we just use *print-dup* here?
(def
#^{ :doc "Mark repeated structures rather than repeat them (N.B. This is not yet used)"}
*print-shared* nil)
(def
#^{ :doc "Don't print namespaces with symbols. This is particularly useful when
pretty printing the results of macro expansions"}
*print-suppress-namespaces* nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Internal variables that keep track of where we are in the
;; structure
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def #^{ :private true } *current-level* 0)
(def #^{ :private true } *current-length* nil)
;; TODO: add variables for length, lines.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Support for the write function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def #^{:private true} write-option-table
{;:array *print-array*
;;:base *print-base*,
;;:case *print-case*,
:circle 'clojure.contrib.pprint/*print-circle*,
;;:escape *print-escape*,
;;:gensym *print-gensym*,
:length 'clojure.core/*print-length*,
:level 'clojure.core/*print-level*,
:lines 'clojure.contrib.pprint/*print-lines*,
:miser-width 'clojure.contrib.pprint/*print-miser-width*,
:dispatch 'clojure.contrib.pprint/*print-pprint-dispatch*,
:pretty 'clojure.contrib.pprint/*print-pretty*,
;;:radix *print-radix*,
:readably 'clojure.core/*print-readably*,
:right-margin 'clojure.contrib.pprint/*print-right-margin*,
:suppress-namespaces 'clojure.contrib.pprint/*print-suppress-namespaces*})
(defmacro #^{:private true} binding-map [amap & body]
(let []
`(do
(. clojure.lang.Var (pushThreadBindings ~amap))
(try
~@body
(finally
(. clojure.lang.Var (popThreadBindings)))))))
(defn- table-ize [t m]
(apply hash-map (mapcat
#(when-let [v (get t (key %))] [(find-var v) (val %)])
m)))
(defn pretty-writer? [x] (instance? PrettyWriter x))
(defn make-pretty-writer [base-writer right-margin miser-width]
(PrettyWriter. base-writer right-margin miser-width))
(defmacro #^{:private true} with-pretty-writer [base-writer & body]
`(let [new-writer# (not (pretty-writer? ~base-writer))]
(binding [*out* (if new-writer#
(make-pretty-writer ~base-writer *print-right-margin* *print-miser-width*)
~base-writer)]
~@body
(if new-writer# (.flush *out*)))))
(defn write
"Write an object subject to the current bindings of the printer control variables.
Use the options argument to override individual variables for this call (and any
recursive calls). Returns the string result if :stream is nil or nil otherwise."
[object & kw-args]
(let [options (merge {:stream true} (apply hash-map kw-args))]
(binding-map (table-ize write-option-table options)
(let [optval (if (contains? options :stream)
(:stream options)
true)
base-writer (condp = optval
nil (java.io.StringWriter.)
true *out*
optval)
length-reached (and *current-length* *print-length* (>= *current-length* *print-length*))]
(if *print-pretty*
(with-pretty-writer base-writer
(if length-reached
(print "...")
;; TODO better/faster dispatch mechanism!
(do
(if *current-length* (set! *current-length* (inc *current-length*)))
(loop [dispatch @*print-pprint-dispatch*]
(let [[test func] (first dispatch)]
(cond
(empty? dispatch) (if (and *print-suppress-namespaces* (symbol? object))
(print (name object))
(pr object))
(test object) (func *out* object)
:else (recur (next dispatch))))))))
(binding [*out* base-writer]
(pr object)))
(if (nil? optval)
(.toString #^java.io.StringWriter base-writer)
length-reached)))))
(defn pprint
"Pretty print object to the optional output writer. If the writer is not provided,
print the object to the currently bound value of *out*."
[object & more]
(let [base-stream (if (pos? (count more))
(first more)
*out*)]
(with-pretty-writer base-stream
(write object :pretty true)
(if (not (= 0 (.getColumn #^PrettyWriter *out*)))
(.write *out* (int \newline))))))
(defmacro pp
"A convenience macro that pretty prints the last thing output. This is
exactly equivalent to (pprint *1)."
[] `(pprint *1))
(defn set-pprint-dispatch
"Set the pretty print dispatch table to TABLE. Currently the supported values are
*simple-dispatch* or *code-dispatch*. In the future, this will support custom tables."
[table]
(dosync (ref-set *print-pprint-dispatch* @table))
nil)
(defmacro with-pprint-dispatch
"Execute body with the pretty print dispatch table bound to table."
[table & body]
`(binding [*print-pprint-dispatch* ~table]
~@body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Support for the functional interface to the pretty printer
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- parse-lb-options [opts body]
(loop [body body
acc []]
(if (opts (first body))
(recur (drop 2 body) (concat acc (take 2 body)))
[(apply hash-map acc) body])))
(defn- check-enumerated-arg [arg choices]
(if-not (choices arg)
(throw
(IllegalArgumentException.
;; TODO clean up choices string
(str "Bad argument: " arg ". It must be one of " choices)))))
(defmacro pprint-logical-block
"Execute the body as a pretty printing logical block with output to *out* which
is a pretty printing writer wrapping base-stream (unless base-stream is already a pretty
printing writer in which case *out* is just bound to base-stream).
After the writer, the caller can optionally specify :prefix, :per-line-prefix, and
:suffix."
[base-stream & body]
(let [[options body] (parse-lb-options #{:prefix :per-line-prefix :suffix} body)]
`(with-pretty-writer ~base-stream
(if (and *print-level* (>= *current-level* *print-level*))
(.write #^PrettyWriter *out* "#")
(binding [*current-level* (inc *current-level*)
*current-length* 0]
(.startBlock #^PrettyWriter *out*
~(:prefix options) ~(:per-line-prefix options) ~(:suffix options))
~@body
(.endBlock #^PrettyWriter *out*)))
nil)))
(defn pprint-newline
"Print a conditional newline to a pretty printing stream. kind specifies if the
newline is :linear, :miser, :fill, or :mandatory.
Optionally, a second argument which is a stream may be used. If supplied, that is
the writer to which the newline is sent, otherwise *out* is used.
If the requested stream is not a PrettyWriter, this function does nothing."
[kind & more]
(check-enumerated-arg kind #{:linear :miser :fill :mandatory})
(let [stream (if (pos? (count more))
(first more)
*out*)]
(if (instance? PrettyWriter stream)
(.newline #^PrettyWriter stream kind))))
(defn pprint-indent
"Create an indent at this point in the pretty printing stream. This defines how
following lines are indented. relative-to can be either :block or :current depending
whether the indent should be computed relative to the start of the logical block or
the current column position. n is an offset.
Optionally, a third argument which is a stream may be used. If supplied, that is
the writer indented, otherwise *out* is used.
If the requested stream is not a PrettyWriter, this function does nothing."
[relative-to n & more]
(check-enumerated-arg relative-to #{:block :current})
(let [stream (if (pos? (count more))
(first more)
*out*)]
(if (instance? PrettyWriter stream)
(.indent #^PrettyWriter stream relative-to n))))
;; TODO a real implementation for pprint-tab
(defn pprint-tab
"Tab at this point in the pretty printing stream. kind specifies whether the tab
is :line, :section, :line-relative, or :section-relative.
Colnum and colinc specify the target column and the increment to move the target
forward if the output is already past the original target.
Optionally, a fourth argument which is a stream may be used. If supplied, that is
the writer indented, otherwise *out* is used.
If the requested stream is not a PrettyWriter, this function does nothing.
THIS FUNCTION IS NOT YET IMPLEMENTED."
[kind colnum colinc & more]
(check-enumerated-arg kind #{:line :section :line-relative :section-relative})
(let [stream (if (pos? (count more))
(first more)
*out*)]
(if (instance? PrettyWriter stream)
(throw (UnsupportedOperationException. "pprint-tab is not yet implemented")))))
nil
|