pretty printing and expansion

EDIT I'm not just asking about 'indenting' every single line of code but about "pretty printing" and/or a wider definition of "indenting", where lines would be grouped/split depending on what they contain.

Here's a function (which I wrote to solve an Euler problem but that is not the point: I could have written it differently) which I'm trying to pretty print:

Version manually indented:

(apply max
       (flatten
        (for [x (range 100 1000)]
          (map
           #(if (= (str (* x %)) (apply str (reverse (str (* x %)))))
              (* x %)
              0)
           (range x 1000)))))

Apparently this isn't the one true Lisp indentation/printing style and, as I'd like to get good habits from the start, I tried to "pretty print" it, doing the following from Emacs' slime-repl clojure REPL:

user> (doc pprint)      ;; just to show which version of pprint I'm using
clojure.pprint/pprint
...

So I tried to do:

user> (pprint '(apply max
       (flatten
        (for [x (range 100 1000)]
          (map
           #(if (= (str (* x %)) (apply str (reverse (str (* x %)))))
              (* x %)
              0)
           (range x 1000))))))

And I got the following:

(apply
 max
 (flatten
  (for
   [x (range 100 1000)]
   (map
    (fn*
     [p1__13958#]
     (if
      (=
       (str (* x p1__13958#))
       (apply str (reverse (str (* x p1__13958#)))))
      (* x p1__13958#)
      0))
    (range x 1000)))))

As I understand it the #(...) notation for the lambda is a "reader macro". So how can I pretty print without triggering the expansion of macros? More generally: how can I pretty print "what I see in my text editor"?

Also, is it normal that nearly everything goes on a new line? For example:

(apply
 max
 (flatten
  (for
   [x (range 100 1000)]
   (map
    ...

seems to be a waste of quite some vertical screen real estate. Is this the way Lisp code should be indented?

And a last related question: can Emacs be configured to "pretty print" a selected region or an entire .clj buffer?

Answers


There's no "official" way to indent lisp code, but I like the way clojure-mode indents clojure code (see below).

I don't know why pprint prints code the way it does but according to this page, it's an early release that isn't feature-complete yet.

For the emacs indentation part, you might want to take a look at clojure-mode.


The closest I have seen to a consensus is to "indent it like Emacs does", I suppose to be pollitically correct I should include the corollary "indent it like vim does". the other editors like eclipse+CCW seem to match this fairly closely. though the default pretty printer adds more newlines.


Here's what I'm currently doing:

(defn pprint-code [code]
  (with-pprint-dispatch code-dispatch
    (binding [*print-suppress-namespaces* true]
      (pprint code))))

NOTE: Look in clojure.pprint to resolve the free-vars.

It's certainly an improvement. I still don't exactly know how to tweak *print-miser-width* and/or *print-right-margin* to get better output. YMMV. My current beef is with (auto-)gensyms - I want them demangled whilst pprinting in certain contexts.

Ideally, forms/functions could define their own formatting rules, e.g. let-bindings should always be on separate lines. I'm not aware of being able to do this with clojure.core.

For reference, see this overview.


Need Your Help

Need help in using decode or case function

sql oracle oracle11g

I need a column where, when enddate is null or greater then sysdate then display status value as "active" or else "inactive".

JSF with google app engine. not sure how to call method in managedbean

google-app-engine jsf exception

I'm using google app engine with JSF. i want to call a function when user press that button: