pretty-print by default?

1,538 views
Skip to first unread message

Jeffrey Schwab

unread,
Jul 1, 2011, 9:32:36 AM7/1/11
to Clojure
Is there any way to make the Clojure repl pretty-print by default?

I have a bunch of little functions that return things like directory
listings and git output, mostly as seqs of lines or Files. I could
change the functions to pretty-print (rather than return) their
results, but I often want to pass the result of one function to
another. The optimal solution (I think) would be if the Clojure
repl's printer would pretty-print function results by default.

Sean Devlin

unread,
Jul 1, 2011, 10:21:37 AM7/1/11
to Clojure
Or maybe the repl could have a *printer* variable

David Powell

unread,
Jul 1, 2011, 11:04:24 AM7/1/11
to clo...@googlegroups.com
Try:

(clojure.main/repl :print pprint)

-- 
Dave

Jeffrey Schwab

unread,
Jul 1, 2011, 11:59:48 AM7/1/11
to clo...@googlegroups.com
Thank you!  That is exactly what I needed.

Kevin Downey

unread,
Jul 1, 2011, 12:14:51 PM7/1/11
to clo...@googlegroups.com
pprint by default would be excellent, except it deref's vars

On Fri, Jul 1, 2011 at 8:59 AM, Jeffrey Schwab <je...@schwabcenter.com> wrote:
> Thank you!  That is exactly what I needed.
>

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

Jeffrey Schwab

unread,
Jul 1, 2011, 12:26:09 PM7/1/11
to clo...@googlegroups.com
I tried adding (clojure.main/repl :print pprint) to my user.clj, but clojure then reports an error, because clojure.main/repl does not (yet) exist.  Is there any work-around for this?  Maybe a way to register onLoad style hooks that will be executed once the repl is running?  Here's my current (butt-ugly) work-around:

; user.clj -- to be run manually once repl prompt appears
(defn pretty [] (eval '(clojure.main/repl :print pprint)))

David Powell

unread,
Jul 1, 2011, 12:53:53 PM7/1/11
to clo...@googlegroups.com

user.clj is old, and isn't ideally suited for pre-loading handy things into a repl.

An alternative might be to put your repl init stuff in a file, such as:

/home/repl-init.clj:

(require 'clojure.pprint)
(clojure.main/repl :print clojure.pprint/pprint)

And then change your repl startup script to be:

java -cp clojure.jar clojure.main -i /home/repl-init.clj -r

Because this is loaded after the repl is inited, you can also add lines which set repl-bound variables such as:

(set! *print-length* 5)

which was raised recently as being something that user.clj can't do.

-- 
Dave

Sean Corfield

unread,
Jul 1, 2011, 2:10:54 PM7/1/11
to clo...@googlegroups.com
If you use lein repl, is there an easy equivalent?

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Phil Hagelberg

unread,
Jul 1, 2011, 2:56:11 PM7/1/11
to clo...@googlegroups.com
Sean Corfield <seanco...@gmail.com> writes:

> If you use lein repl, is there an easy equivalent?

You can set :repl-options [:print clojure.contrib/pprint], but that
depends on pprint being required before the repl launches, and I can't
think of a straightforward way to do that. Perhaps adding an :eval-init
entry in project.clj which can contain needed requires like this?

-Phil

Jeffrey Schwab

unread,
Jul 1, 2011, 3:12:22 PM7/1/11
to clo...@googlegroups.com
Thank!

Why -r? The (clojure.main/repl) call seems to start a new repl.  (In fact, it took me a minute to figure out that this really needs to be the last thing in repl-init.clj.)

Shantanu Kumar

unread,
Jul 1, 2011, 4:10:10 PM7/1/11
to Clojure
> You can set :repl-options [:print clojure.contrib/pprint], but that
> depends on pprint being required before the repl launches, and I can't
> think of a straightforward way to do that. Perhaps adding an :eval-init
> entry in project.clj which can contain needed requires like this?

+1

Sean Corfield

unread,
Jul 1, 2011, 5:18:21 PM7/1/11
to clo...@googlegroups.com
In IRC I asked Phil about this and he suggested putting (require
'clojure.pprint) in ~/.lein/user.clj with :repl-options [:print
clojure.pprint/pprint] and that does indeed work for now.

Sean

Tom Faulhaber

unread,
Jul 2, 2011, 1:56:36 AM7/2/11
to Clojure
To do this from a simple command line, just do:

java -cp <path-to-clojure-jar> clojure.main -e "(require
'clojure.pprint) (clojure.main/repl :print clojure.pprint/pprint)"

modify to taste.

I once figured how to do this in Slime, too, but when I look at my
(quite dated) copy, it looks like pr-str is hard-coded.

Tom

Phil Hagelberg

unread,
Jul 3, 2011, 6:13:11 PM7/3/11
to clo...@googlegroups.com
Phil Hagelberg <ph...@hagelb.org> writes:

For the record, I just implemented this, and it will land in Leiningen
1.6.1 in a few days:

https://github.com/technomancy/leiningen/commit/ad2e2111bf60051

-Phil

Tassilo Horn

unread,
Aug 30, 2011, 4:23:48 AM8/30/11
to clo...@googlegroups.com
Phil Hagelberg <ph...@hagelb.org> writes:

Hi Phil,

>> You can set :repl-options [:print clojure.contrib/pprint], but that
>> depends on pprint being required before the repl launches, and I
>> can't think of a straightforward way to do that. Perhaps adding an
>> :eval-init entry in project.clj which can contain needed requires
>> like this?
>
> For the record, I just implemented this, and it will land in Leiningen
> 1.6.1 in a few days:
>
> https://github.com/technomancy/leiningen/commit/ad2e2111bf60051

Great, but how do I actually use that? This is my project.clj:

--8<---------------cut here---------------start------------->8---
(defproject funtg "1.0.0-SNAPSHOT"
:description "A library for handling TGraphs. Includes the functional query
language FunQL and the transformation language FunTL."
:repositories { "sonatype"
{ :url "https://oss.sonatype.org/content/repositories/snapshots" } }
:dependencies [[org.clojure/clojure "1.3.0-master-SNAPSHOT"]
[ist/jgralab "[1.0.0,)"]
[ordered "[0.3,)"]]
:dev-dependencies [[swank-clojure "[1.4.0-SNAPSHOT,)"]]
;; AOT-compile my exception classes
:aot [de.uni-koblenz.funtg.exceptions]
;; Don't put version control dirs into the jar
:jar-exclusions [#"(?:^|/).(svn|hg|git)/"]
:warn-on-reflection true
:jvm-opts ["-Xmx2G"]
;; Pretty print results at the REPL
:project-init (require 'clojure.pprint)
:repl-options [:print clojure.pprint/pprint])
--8<---------------cut here---------------end--------------->8---

That does the trick for REPLs fired up with "lein repl", but the REPL I
get in emacs when running M-x clojure-jack-in doesn't pretty-print.

So what do I need to do to enable pretty-printing also in emacs?

Bye,
Tassilo

Reply all
Reply to author
Forward
0 new messages