Navigation Menu

Skip to content

Commit

Permalink
Remove gen-class requirement from clojure.contrib.pprint. See #81
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfaulhaber committed Apr 30, 2010
1 parent 78ee9b3 commit 661dcfd
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 257 deletions.
1 change: 0 additions & 1 deletion pom.xml
Expand Up @@ -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>
Expand Down
3 changes: 2 additions & 1 deletion src/main/clojure/clojure/contrib/pprint.clj
Expand Up @@ -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")
Expand Down
78 changes: 0 additions & 78 deletions src/main/clojure/clojure/contrib/pprint/ColumnWriter.clj

This file was deleted.

20 changes: 10 additions & 10 deletions src/main/clojure/clojure/contrib/pprint/cl_format.clj
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))))
Expand Down Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions src/main/clojure/clojure/contrib/pprint/column_writer.clj
@@ -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))))))))
31 changes: 0 additions & 31 deletions src/main/clojure/clojure/contrib/pprint/gen_class.clj

This file was deleted.

16 changes: 8 additions & 8 deletions src/main/clojure/clojure/contrib/pprint/pprint_base.clj
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 661dcfd

Please sign in to comment.