summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/clj/clojure/boot.clj59
-rw-r--r--src/jvm/clojure/lang/Repl.java4
2 files changed, 49 insertions, 14 deletions
diff --git a/src/clj/clojure/boot.clj b/src/clj/clojure/boot.clj
index cb6b4412..ca04ebe9 100644
--- a/src/clj/clojure/boot.clj
+++ b/src/clj/clojure/boot.clj
@@ -960,7 +960,7 @@
([x form & more] `(.. (. ~x ~form) ~@more)))
(defmacro ->
- "Macro. Threads the expr through the forms. Inserts x as the
+ "Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
@@ -3430,13 +3430,50 @@
(import '(java.io Writer))
+(def
+ #^{:doc "*print-length* controls how many items of each collection the
+ printer will print. If it is bound to logical false, there is no
+ limit. Otherwise, it must be bound to an integer indicating the maximum
+ number of items of each collection to print. If a collection contains
+ more items, the printer will print items up to the limit followed by
+ '...' to represent the remaining items. The root binding is nil
+ indicating no limit."}
+ *print-length* nil)
+
+(def
+ #^{:doc "*print-level* controls how many levels deep the printer will
+ print nested objects. If it is bound to logical false, there is no
+ limit. Otherwise, it must be bound to an integer indicating the maximum
+ level to print. Each argument to print is at level 0; if an argument is a
+ collection, its items are at level 1; and so on. If an object is a
+ collection and is at a level greater than or equal to the value bound to
+ *print-level*, the printer prints '#' to represent it. The root binding
+ is nil indicating no limit."}
+*print-level* nil)
+
(defn- print-sequential [#^String begin, print-one, #^String sep, #^String end, sequence, #^Writer w]
- (.write w begin)
- (loop [s (seq sequence)]
- (if (rest s)
- (do (print-one (first s) w) (.write w sep) (recur (rest s)))
- (when s (print-one (first s) w))))
- (.write w end))
+ (binding [*print-level* (and *print-level* (dec *print-level*))]
+ (if (and *print-level* (neg? *print-level*))
+ (.write w "#")
+ (do
+ (.write w begin)
+ (when-let xs (seq sequence)
+ (if *print-length*
+ (loop [[x & xs] xs
+ print-length *print-length*]
+ (if (zero? print-length)
+ (.write w "...")
+ (do
+ (print-one x w)
+ (when xs
+ (.write w sep)
+ (recur xs (dec print-length))))))
+ (loop [[x & xs] xs]
+ (print-one x w)
+ (when xs
+ (.write w sep)
+ (recur xs)))))
+ (.write w end)))))
(defn- print-meta [o, #^Writer w]
(when-let m (meta o)
@@ -3519,13 +3556,7 @@
(defmethod print-method clojure.lang.IPersistentVector [v, #^Writer w]
(print-meta v w)
- (.append w \[)
- (dotimes n (count v)
- (print-method (nth v n) w)
- (when (< n (dec (count v)))
- (.append w \space)))
- (.append w \])
- nil)
+ (print-sequential "[" print-method " " "]" v w))
(defmethod print-method clojure.lang.IPersistentMap [m, #^Writer w]
(print-meta m w)
diff --git a/src/jvm/clojure/lang/Repl.java b/src/jvm/clojure/lang/Repl.java
index b2549b3c..daeba5a8 100644
--- a/src/jvm/clojure/lang/Repl.java
+++ b/src/jvm/clojure/lang/Repl.java
@@ -25,6 +25,8 @@ static final Var refer = RT.var("clojure", "refer");
static final Var ns = RT.var("clojure", "*ns*");
static final Var warn_on_reflection = RT.var("clojure", "*warn-on-reflection*");
static final Var print_meta = RT.var("clojure", "*print-meta*");
+static final Var print_length = RT.var("clojure", "*print-length*");
+static final Var print_level = RT.var("clojure", "*print-level*");
static final Var star1 = RT.var("clojure", "*1");
static final Var star2 = RT.var("clojure", "*2");
static final Var star3 = RT.var("clojure", "*3");
@@ -44,6 +46,8 @@ public static void main(String[] args) throws Exception{
RT.map(ns, ns.get(),
warn_on_reflection, warn_on_reflection.get(),
print_meta, print_meta.get(),
+ print_length, print_length.get(),
+ print_level, print_level.get(),
star1, null,
star2, null,
star3, null,