   ["M" "Left"] :paredit-expand-left
    })
 
+(declare sexp-dup)
+
+(defn reformat-all-forms [s] 
+  (let [rvec (map (fn [[offs p]]
+                    (let [len (v/length p)]
+                      (vector (- offs len) len (with-out-str (clojure.pprint/pprint (read-string (v/text p)))))))
+                  (filter (fn [me] (= :list (v/tag (val me)))) (v/offsets (sexp-dup s))))
+          rvec-first (nth rvec 0)
+          rlist (first (partition 2 1 rvec))
+          ;_ (println "rlist: " rlist)
+          sb (StringBuffer. s)
+        ]
+          ; replace first form
+          #_(println "first offset: " (first rvec-first))
+          (.replace sb (first rvec-first) (+ (first rvec-first) (second rvec-first)) (last rvec-first))
+
+          ; replace from 2nd to last form
+          (loop [offsum 0
+                 rl rlist]
+            (if (or (empty? rl) (not (second rl)))
+              (.toString sb)
+              (let [ra (first rl)
+                    rb (second rl)
+                    ;_ (println "ra: " ra)
+                    ;_ (println "rb: " rb)
+                    diff-a (- (.length (last ra)) (second ra))
+                    offset-b (first rb)
+                    len-b (second rb)
+                    off-next (+ diff-a offset-b offsum)]
+
+                      ; replace next form
+                      (.replace sb off-next (+ off-next len-b) (last rb))
+                      (recur off-next (rest rl)))))))
+
 (def formatting-keymap
   {["M" "f"] :fmt-pprint})
 
-(defn exec-pprint [k widget]
-  (when (formatting-keymap k)
-    (when-let [text (.getSelectedText widget)]
-      (let [fs (with-out-str (clojure.pprint/pprint (read-string text)))]
+(defn exec-pprint-old [k widget]
+  (let [cmd (formatting-keymap k)]
+    (if cmd
+      (let [fs (with-out-str (clojure.pprint/pprint (read-string (.getSelectedText widget))))]
         (if *debug* (println "replacement: " fs " at: " (.getSelectionStart widget)))
         (if fs (.replaceRange widget fs (.getSelectionStart widget) (.getSelectionEnd widget)))))))
+
+(defn exec-pprint [k widget]
+  (let [cmd (formatting-keymap k)]
+    (if cmd
+      (let [sel (.getSelectedText widget)]
+        (if sel (.replaceRange widget (reformat-all-forms sel) (.getSelectionStart widget) (.getSelectionEnd widget)))))))
 
 (defn exec-paredit [k w]
   (let [cmd (keymap k)]

                            :size [300 :by 300]))
   pack!
   show!))

  (:use [seesaw.core]
        [paredit.core]
        [paredit.parser])
  (:require
        [net.cgrand.parsley :as p]
        [net.cgrand.parsley.functional-trees :as f]
        [net.cgrand.parsley.views :as v])
  (:import java.awt.event.KeyListener
           java.awt.event.InputMethodListener))

