Back

code-dispatch (clj)

(source)

multimethod

The pretty print dispatch function for pretty printing Clojure code.

Examples

clj-commons/useful
;; leave out of ns decl so we can load with classlojure.io/resource-forms
(require '[clojure.pprint :as p])
(require '[clojure.stacktrace :as s])

(letfn [(interrogate-form [list-head form]
          `(let [display# (fn [val#]
                            (let [form# (with-out-str
                                          (clojure.pprint/with-pprint-dispatch
                                            clojure.pprint/code-dispatch
                                            (clojure.pprint/pprint '~form)))
                                  val# (with-out-str (clojure.pprint/pprint val#))]
                              (~@list-head
                               (if (every? (partial > clojure.pprint/*print-miser-width*)
                                           [(count form#) (count val#)])
                                 (str (subs form# 0 (dec (count form#))) " is " val#)
                                 (str form# "--------- is ---------\n" val#)))))]
             (try (doto ~form display#)
                  (catch Throwable t#
                    (display# {:thrown t#
                               :trace (with-out-str
                                        (clojure.stacktrace/print-cause-trace t#))})
                    (throw t#)))))]
reborg/clojure-essential-reference
(require '[clojure.pprint :as pprint])

(binding [pprint/*print-pprint-dispatch* pprint/code-dispatch] ; <2>
  (pprint/pprint condp-statement))
;; (condp = x
;;   1 "one"
;;   2 "two"
;;   3 "three"
;;   4 "four"
;;   5 "five"
;;   6 "six"
;;   :else :none)
reborg/clojure-essential-reference
(require '[clojure.pprint :as pprint])

(binding [pprint/*print-pprint-dispatch* pprint/code-dispatch ; <3>
          pprint/*print-right-margin* 30]
     (pprint/pprint nested-statement))