           (mouseExited [this e] nil)
           (mousePressed [this e] nil)
           (mouseReleased [this e]
-            (mwm/update-cursor-position! edit-history (.getCaretPosition text-area))))))
+            (mwm/update-cursor-position! edit-history (get-cursor-position text-area))))))
     (reset! edit-history nil))
   text-area)
 

  (let [^ParinferResult res (Parinfer/indentMode text (int x) (int line) nil)]
    {:x (.-cursorX res) :text (.-text res)}))

(defn get-parinfer-state
  [^TextEditorPane text-area paren-mode?]
  (let [old-pos (.getCaretPosition text-area)
        old-x (.getCaretOffsetFromLineStart text-area)
        old-line (.getCaretLineNumber text-area)
        old-text (.getText text-area)
        result (if paren-mode?
                 (paren-mode old-text old-x old-line)
                 (indent-mode old-text old-x old-line))]
    (mwm/get-state (:text result) old-line (:x result))))

(defn get-normal-state
  [^TextEditorPane text-area]
  (let [position (.getCaretPosition text-area)
        text (.getText text-area)]
    (assoc (mwm/get-state text position) :should-indent? true)))

(defn add-indent-if-necessary
  [lines text tags state]
  (if (:should-indent? state)
    (let [[cursor-line _] (mwm/position->row-col text (:cursor-position state))
          indent-level (ts/indent-for-line tags cursor-line)]
      [(update lines
               cursor-line
               (fn [line]
                 (str (str/join (repeat indent-level " ")) (str/triml line))))
       (+ (:cursor-position state) indent-level)])
    [lines (:cursor-position state)]))

(defn refresh-content!
  [^TextEditorPane text-area state]
  (let [old-text (.getText text-area)
        lines (:lines state)
        new-text (str/join \newline lines)
        tags (ts/str->tags new-text)
        [lines cursor-position] (add-indent-if-necessary lines new-text tags state)
        new-text (str/join \newline lines)]
    (.replaceRange text-area new-text 0 (count old-text))
    (.setCaretPosition text-area cursor-position)
    state))

(defn init-parinfer!

              (contains? #{KeyEvent/VK_DOWN KeyEvent/VK_UP
                           KeyEvent/VK_RIGHT KeyEvent/VK_LEFT}
                         (.getKeyCode e))
              (mwm/update-cursor-position! edit-history (get-cursor-position text-area))
              
              (not (or (contains? #{KeyEvent/VK_SHIFT KeyEvent/VK_CONTROL
                                    KeyEvent/VK_ALT KeyEvent/VK_META}
