3

I'm new to Emacs (after 10+ years of Vim) and want to use it primarily for clojure.

Coming from Lighttable, one thing I absolute liked was the way it evaluated just the right sections of code no matter where I am. This is basically: Evaluating a paragraph surrounding the cursor position. Some examples (|| represents the cursor, $FROM ... TO$ represents what I'd like to have evaluated):

(comment

$FROM
(my-func ||[:test :one])
(xy)TO$

$FROM
(defn [y]
  (let [x y]
    (println|| "xy")))
)TO$

)

So basically I want to evaluate the paragraph (Note: Not necessarily a top level form since I surround them in (comment)). I have no knowledge of Elisp. I tried this as an effort.

(defun my-cider-eval-paragraph ()
  (interactive )
  (safe-excursion
     (mark-paragraph)
     (command-execute 'cider-eval-region)))

But I'm clearly making many mistakes. Edit: Actually, my example works if I just change safe-excursion to save-excursion. Funny, I actually think both spellings makes sense.

Thanks for reading.

ClojureMostly
  • 225
  • 1
  • 5
  • Does `cider-eval-defun-at-point` not do what you want? – Dan Jan 30 '15 at 17:12
  • 1
    I dont do clojure and can't test this. but I looked at the source for `cider-eval-region` and perhaps this will work: `(cider-eval-region (save-excursion (backward-paragraph) (point)) (save-excursion (forward-paragraph) (point))` instead of using command-execute and mark-paragraph, the source of cider-eval-region is very simple, so it may be a problem with the region. What exactly goes wrong with your example? – Jordon Biondo Jan 30 '15 at 17:19
  • @Dan: No this doesn't do what I want. I want it independent of defn's. @Jordon Biondo: Yes, this seems to do exactly what I wanted. I just realized I'd like the result actually "pprint"ed but cider does not offer a `pprint-region` which I guess makes sense since a region might contain multiple forms. If you submit it as an answer (and possibly note why mine doesnt work) I'll accept the answer. – ClojureMostly Jan 30 '15 at 18:33
  • 1
    Actually, I just realized I made a very silly mistake and misspelt "save-excursion". My example then does work. Sorry for the noise. – ClojureMostly Jan 30 '15 at 18:39
  • 2
    @Vanessa: could you post your own correction as an answer and accept it? That will mark this question as answered. – Dan Jan 30 '15 at 19:50

2 Answers2

2

Although this is not pristine elisp, the following should work.

(defun my-cider-eval-paragraph ()
  (interactive) 
  (save-excursion
    (mark-paragraph) 
    (command-execute 'cider-eval-region)))

Note this is exactly what you wrote, except the macro is called saVe-excursion.

Malabarba
  • 22,878
  • 6
  • 78
  • 163
1

lispy provides a vi-like experience for LISP dialects, including Clojure. It integrates with CIDER.

You can eval the current Clojure expression with e. For instance, starting with this code, with point represented by |, and CIDER already running:

|(defn sqr [x]
  (* x x))

(filter odd? (map sqr [1 2 3 4 5]))
  • e: #'user/sqr
  • je: (1 9 25)
  • fe: (1 4 9 16 25)
  • fe: [1 2 3 4 5]
  • he: (1 4 9 16 25)
  • he: (1 9 25)
  • ke: #'user/sqr

If you want to implement something on your own instead, look at lispy's le-clojure. It's around 100 lines of CIDER integration, but the important stuff for you is something like:

(let* ((str (buffer-substring-no-properties
             (region-beginning)
             (region-end)))
       (res (nrepl-sync-request:eval str))
       (val (nrepl-dict-get res "value")))
  (message "result: %s" val))
abo-abo
  • 13,943
  • 1
  • 29
  • 43