Permalink
Browse files

Remove gen-class requirement from clojure.contrib.pprint. See #81

  • Loading branch information...
1 parent 78ee9b3 commit 661dcfde7965c3e6650d169afca4a20ef146c940 @tomfaulhaber tomfaulhaber committed Apr 30, 2010
View
@@ -95,7 +95,6 @@
<namespace>clojure\.contrib\.fnmap\.PersistentFnMap</namespace>
<namespace>clojure\.contrib\.condition\.Condition</namespace>
<namespace>clojure\.contrib\.repl-ln</namespace>
- <namespace>clojure\.contrib\.pprint\.gen-class</namespace>
</namespaces>
</configuration>
<executions>
@@ -25,7 +25,8 @@ documentation on the the clojure-contrib web site on github.",
}
clojure.contrib.pprint
(:use clojure.contrib.pprint.utilities)
- (:import [clojure.contrib.pprint PrettyWriter]))
+ (:use clojure.contrib.pprint.pretty-writer
+ clojure.contrib.pprint.column-writer))
(load "pprint/pprint_base")
@@ -1,78 +0,0 @@
-;;; ColumnWriter.clj -- part of the pretty printer for Clojure
-
-;; by Tom Faulhaber
-;; April 3, 2009
-
-; Copyright (c) Tom Faulhaber, Dec 2008. 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 a column-aware wrapper around an instance of java.io.Writer
-
-(ns clojure.contrib.pprint.ColumnWriter)
-
-(def *default-page-width* 72)
-
-(defn- -init
- ([writer] (-init writer *default-page-width*))
- ([writer max-columns] [[] (ref {:max max-columns, :cur 0, :line 0 :base writer})]))
-
-(defn- get-field [#^clojure.contrib.pprint.ColumnWriter this sym]
- (sym @(.state this)))
-
-(defn- set-field [#^clojure.contrib.pprint.ColumnWriter this sym new-val]
- (alter (.state this) assoc sym new-val))
-
-(defn- -getColumn [this]
- (get-field this :cur))
-
-(defn- -getLine [this]
- (get-field this :line))
-
-(defn- -getMaxColumn [this]
- (get-field this :max))
-
-(defn- -setMaxColumn [this new-max]
- (dosync (set-field this :max new-max))
- nil)
-
-(defn- -getWriter [this]
- (get-field this :base))
-
-(declare write-char)
-
-(defn- -write
- ([#^clojure.contrib.pprint.ColumnWriter this #^chars cbuf #^Integer off #^Integer len]
- (let [#^java.io.Writer writer (get-field this :base)]
- (.write writer cbuf off len)))
- ([#^clojure.contrib.pprint.ColumnWriter this x]
- (condp = (class x)
- String
- (let [#^String s x
- nl (.lastIndexOf s (int \newline))]
- (dosync (if (neg? nl)
- (set-field this :cur (+ (get-field this :cur) (count s)))
- (do
- (set-field this :cur (- (count s) nl 1))
- (set-field this :line (+ (get-field this :line)
- (count (filter #(= % \newline) s)))))))
- (.write #^java.io.Writer (get-field this :base) s))
-
- Integer
- (write-char this x))))
-
-(defn- write-char [#^clojure.contrib.pprint.ColumnWriter this #^Integer c]
- (dosync (if (= c (int \newline))
- (do
- (set-field this :cur 0)
- (set-field this :line (inc (get-field this :line))))
- (set-field this :cur (inc (get-field this :cur)))))
- (.write #^java.io.Writer (get-field this :base) c))
-
-(defn- -flush [this]) ;; Currently a no-op
-
-(defn- -close [this]) ;; Currently a no-op
@@ -963,7 +963,7 @@ Note this should only be used for the last one in the sequence"
navigator (or new-navigator navigator)
min-remaining (or (first (:min-remaining else-params)) 0)
max-columns (or (first (:max-columns else-params))
- (.getMaxColumn #^PrettyWriter *out*))
+ (get-max-column *out*))
clauses (:clauses params)
[strs navigator] (render-clauses clauses navigator (:base-args params))
slots (max 1
@@ -981,7 +981,7 @@ Note this should only be used for the last one in the sequence"
pad (max minpad (quot total-pad slots))
extra-pad (- total-pad (* pad slots))
pad-str (apply str (repeat pad (:padchar params)))]
- (if (and eol-str (> (+ (.getColumn #^PrettyWriter *out*) min-remaining result-columns)
+ (if (and eol-str (> (+ (get-column (:base @@*out*)) min-remaining result-columns)
max-columns))
(print eol-str))
(loop [slots slots
@@ -1139,10 +1139,10 @@ Note this should only be used for the last one in the sequence"
;;; If necessary, wrap the writer in a PrettyWriter object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(defn pretty-writer [writer]
- (if (instance? PrettyWriter writer)
+(defn get-pretty-writer [writer]
+ (if (pretty-writer? writer)
writer
- (PrettyWriter. writer *print-right-margin* *print-miser-width*)))
+ (pretty-writer writer *print-right-margin* *print-miser-width*)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Support for column-aware operations ~&, ~T
@@ -1153,13 +1153,13 @@ Note this should only be used for the last one in the sequence"
"Make a newline if the Writer is not already at the beginning of the line.
N.B. Only works on ColumnWriters right now."
[]
- (if (not (= 0 (.getColumn #^PrettyWriter *out*)))
+ (if (not (= 0 (get-column (:base @@*out*))))
(prn)))
(defn- absolute-tabulation [params navigator offsets]
(let [colnum (:colnum params)
colinc (:colinc params)
- current (.getColumn #^PrettyWriter *out*)
+ current (get-column (:base @@*out*))
space-count (cond
(< current colnum) (- colnum current)
(= colinc 0) 0
@@ -1170,7 +1170,7 @@ N.B. Only works on ColumnWriters right now."
(defn- relative-tabulation [params navigator offsets]
(let [colrel (:colnum params)
colinc (:colinc params)
- start-col (+ colrel (.getColumn #^PrettyWriter *out*))
+ start-col (+ colrel (get-column (:base @@*out*)))
offset (if (pos? colinc) (rem start-col colinc) 0)
space-count (+ colrel (if (= 0 offset) 0 (- colinc offset)))]
(print (apply str (repeat space-count \space))))
@@ -1789,8 +1789,8 @@ because the formatter macro uses it."
(true? stream) *out*
:else stream)
#^java.io.Writer wrapped-stream (if (and (needs-pretty format)
- (not (instance? PrettyWriter real-stream)))
- (pretty-writer real-stream)
+ (not (pretty-writer? real-stream)))
+ (get-pretty-writer real-stream)
real-stream)]
(binding [*out* wrapped-stream]
(try
@@ -0,0 +1,78 @@
+;;; column_writer.clj -- part of the pretty printer for Clojure
+
+;; by Tom Faulhaber
+;; April 3, 2009
+;; Revised to use proxy instead of gen-class April 2010
+
+; Copyright (c) Tom Faulhaber, Dec 2008. 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 a column-aware wrapper around an instance of java.io.Writer
+
+(ns clojure.contrib.pprint.column-writer
+ (:import
+ [clojure.lang IDeref]
+ [java.io Writer]))
+
+(def *default-page-width* 72)
+
+(defn- get-field [#^Writer this sym]
+ (sym @@this))
+
+(defn- set-field [#^Writer this sym new-val]
+ (alter @this assoc sym new-val))
+
+(defn get-column [this]
+ (get-field this :cur))
+
+(defn get-line [this]
+ (get-field this :line))
+
+(defn get-max-column [this]
+ (get-field this :max))
+
+(defn set-max-column [this new-max]
+ (dosync (set-field this :max new-max))
+ nil)
+
+(defn get-writer [this]
+ (get-field this :base))
+
+(defn- write-char [#^Writer this #^Integer c]
+ (dosync (if (= c (int \newline))
+ (do
+ (set-field this :cur 0)
+ (set-field this :line (inc (get-field this :line))))
+ (set-field this :cur (inc (get-field this :cur)))))
+ (.write #^Writer (get-field this :base) c))
+
+(defn column-writer
+ ([writer] (column-writer writer *default-page-width*))
+ ([writer max-columns]
+ (let [fields (ref {:max max-columns, :cur 0, :line 0 :base writer})]
+ (proxy [Writer IDeref] []
+ (deref [] fields)
+ (write
+ ([#^chars cbuf #^Integer off #^Integer len]
+ (let [#^Writer writer (get-field this :base)]
+ (.write writer cbuf off len)))
+ ([x]
+ (condp = (class x)
+ String
+ (let [#^String s x
+ nl (.lastIndexOf s (int \newline))]
+ (dosync (if (neg? nl)
+ (set-field this :cur (+ (get-field this :cur) (count s)))
+ (do
+ (set-field this :cur (- (count s) nl 1))
+ (set-field this :line (+ (get-field this :line)
+ (count (filter #(= % \newline) s)))))))
+ (.write #^Writer (get-field this :base) s))
+
+ Integer
+ (write-char this x))))))))
@@ -1,31 +0,0 @@
-;;; gen_class.clj: generate statically-named classes for pprint
-
-(ns clojure.contrib.pprint.gen-class)
-
-(gen-class :name clojure.contrib.pprint.ColumnWriter
- :impl-ns clojure.contrib.pprint.ColumnWriter
- :extends java.io.Writer
- :init init
- :constructors {[java.io.Writer Integer] [],
- [java.io.Writer] []}
- :methods [[getColumn [] Integer]
- [getLine [] Integer]
- [getMaxColumn [] Integer]
- [setMaxColumn [Integer] Void]
- [getWriter [] java.io.Writer]]
- :state state)
-
-(gen-class :name clojure.contrib.pprint.PrettyWriter
- :impl-ns clojure.contrib.pprint.PrettyWriter
- :extends clojure.contrib.pprint.ColumnWriter
- :init init
- :constructors {[java.io.Writer Integer Object] [java.io.Writer Integer]}
- :methods [[startBlock [String String String] void]
- [endBlock [] void]
- [newline [clojure.lang.Keyword] void]
- [indent [clojure.lang.Keyword Integer] void]
- [getMiserWidth [] Object]
- [setMiserWidth [Object] void]
- [setLogicalBlockCallback [clojure.lang.IFn] void]]
- :exposes-methods {write col_write}
- :state pwstate)
@@ -140,12 +140,12 @@ radix specifier is in the form #XXr where XX is the decimal value of *print-base
(defn- pretty-writer?
"Return true iff x is a PrettyWriter"
- [x] (instance? PrettyWriter x))
+ [x] (and (instance? clojure.lang.IDeref x) (:pretty-writer @@x)))
(defn- make-pretty-writer
"Wrap base-writer in a PrettyWriter with the specified right-margin and miser-width"
[base-writer right-margin miser-width]
- (PrettyWriter. base-writer right-margin miser-width))
+ (pretty-writer base-writer right-margin miser-width))
(defmacro #^{:private true} with-pretty-writer [base-writer & body]
`(let [base-writer# ~base-writer
@@ -235,7 +235,7 @@ print the object to the currently bound value of *out*."
(binding [*print-pretty* true]
(binding-map (if (or (not (= *print-base* 10)) *print-radix*) {#'pr pr-with-base} {})
(write-out object)))
- (if (not (= 0 (.getColumn #^PrettyWriter *out*)))
+ (if (not (= 0 (get-column *out*)))
(.write *out* (int \newline))))))
(defmacro pp
@@ -294,13 +294,13 @@ and :suffix."
[& args]
(let [[options body] (parse-lb-options #{:prefix :per-line-prefix :suffix} args)]
`(do (if (level-exceeded)
- (.write #^PrettyWriter *out* "#")
+ (.write #^java.io.Writer *out* "#")
(binding [*current-level* (inc *current-level*)
*current-length* 0]
- (.startBlock #^PrettyWriter *out*
+ (start-block *out*
~(:prefix options) ~(:per-line-prefix options) ~(:suffix options))
~@body
- (.endBlock #^PrettyWriter *out*)))
+ (end-block *out*)))
nil)))
(defn pprint-newline
@@ -310,7 +310,7 @@ newline is :linear, :miser, :fill, or :mandatory.
Output is sent to *out* which must be a pretty printing writer."
[kind]
(check-enumerated-arg kind #{:linear :miser :fill :mandatory})
- (.newline #^PrettyWriter *out* kind))
+ (nl *out* kind))
(defn pprint-indent
"Create an indent at this point in the pretty printing stream. This defines how
@@ -321,7 +321,7 @@ the current column position. n is an offset.
Output is sent to *out* which must be a pretty printing writer."
[relative-to n]
(check-enumerated-arg relative-to #{:block :current})
- (.indent #^PrettyWriter *out* relative-to n))
+ (indent *out* relative-to n))
;; TODO a real implementation for pprint-tab
(defn pprint-tab
Oops, something went wrong.

0 comments on commit 661dcfd

Please sign in to comment.