Back
with-pprint-dispatch (clj)
(source)macro
(with-pprint-dispatch function & body)
Execute body with the pretty print dispatch function bound to function.
Examples
clojure
"(ns autodoc.build-html
\"This is the namespace that builds the HTML pages themselves.
It is implemented with a number of custom enlive templates.\"
{:skip-wiki true, :author \"Tom Faulhaber\"}
(:refer-clojure :exclude [empty complement])
(:import [java.util.jar JarFile]
[java.io File FileWriter BufferedWriter StringReader
BufferedInputStream BufferedOutputStream
ByteArrayOutputStream FileReader FileInputStream]
[java.util.regex Pattern])
(:require [clojure.string :as str])
(:use [net.cgrand.enlive-html :exclude (deftemplate)]
[clojure.java.io :only (as-file file writer)]
[clojure.java.shell :only (sh)]
[clojure.pprint :only (pprint cl-format pprint-ident
pprint-logical-block set-pprint-dispatch
get-pretty-writer fresh-line)]
[clojure.data.json :only (pprint-json)]
[autodoc.collect-info :only (contrib-info)]
[autodoc.params :only (params expand-classpath)])
(:use clojure.set clojure.java.io clojure.data clojure.java.browse
clojure.inspector clojure.zip clojure.stacktrace))")
(simple-tests dispatch-tests
(with-pprint-dispatch test-dispatch
(with-out-str
(pprint '("hello" "there"))))
"[\"hello\" \"there\"]\n"
)
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#)))))]
frenchy64/fully-satisfies
(ns io.github.frenchy64.fully-satisfies.non-leaky-macros.clojure.pprint
"Implementations of clojure.pprint macros that don't leak implementation details."
(:require [clojure.pprint :as pp]))
(defmacro non-leaky-with-pprint-dispatch
"Like clojure.pprint/with-pprint-dispatch, except body does not leak try/catch syntax."
[function & body]
`(pp/with-pprint-dispatch
~function
(do ~@body)))
(defmacro with-pprint-dispatch
[& args]
`(non-leaky-with-pprint-dispatch ~@args))