Help about using clojure in org mode in Emacs with CIDER

673 views
Skip to first unread message

Rui Yang

unread,
Jan 23, 2014, 2:04:22 AM1/23/14
to clo...@googlegroups.com
Trying to use org mode with clojure. I'd like to use cider as the REPL server.
Things is fine if I have only one statement in org source block. If I have more than one, then I got exception.

for example

#+name: basic-clojure
#+begin_src clojure
(defn greeting
"Returns a greeting of the form 'Hello, username.'"
[username]
(str "Hello, " username))
#+end_src

will be fine. But

#+name: basic-clojure
#+begin_src clojure
(defn greeting
"Returns a greeting of the form 'Hello, username.'"
[username]
(str "Hello, " username))
(greeting "Tom")
#+end_src

will throw exception:

java.lang.ClassCastException: java.lang.String cannot be cast to java.io.Writer
                   column_writer.clj:78 clojure.pprint/column-writer[fn]
                       (Unknown Source) clojure.pprint.proxy$java.io.Writer$IDeref$db53459f.write
                       (Unknown Source) sun.reflect.GeneratedMethodAccessor9.invoke
   DelegatingMethodAccessorImpl.java:25 sun.reflect.DelegatingMethodAccessorImpl.invoke
                        Method.java:597 java.lang.reflect.Method.invoke
                      Reflector.java:93 clojure.lang.Reflector.invokeMatchingMethod
                      Reflector.java:28 clojure.lang.Reflector.invokeInstanceMethod
                  pretty_writer.clj:443 clojure.pprint/start-block[fn]
                            AFn.java:18 clojure.lang.AFn.call
            LockingTransaction.java:263 clojure.lang.LockingTransaction.run
            LockingTransaction.java:231 clojure.lang.LockingTransaction.runInTransaction
                  pretty_writer.clj:433 clojure.pprint/start-block
                           Var.java:427 clojure.lang.Var.invoke
                       dispatch.clj:130 clojure.pprint/pprint-ideref[fn]
                       dispatch.clj:130 clojure.pprint/pprint-ideref
                       MultiFn.java:227 clojure.lang.MultiFn.invoke
                    pprint_base.clj:194 clojure.pprint/write-out
                    pprint_base.clj:250 clojure.pprint/pprint[fn]
                    pprint_base.clj:248 clojure.pprint/pprint
                       NO_SOURCE_FILE:5 user/eval611
                     Compiler.java:6619 clojure.lang.Compiler.eval
                     Compiler.java:6582 clojure.lang.Compiler.eval
                          core.clj:2852 clojure.core/eval
                           main.clj:259 clojure.main/repl[fn]
                           main.clj:259 clojure.main/repl[fn]
                           main.clj:277 clojure.main/repl[fn]
                           main.clj:277 clojure.main/repl
                       RestFn.java:1096 clojure.lang.RestFn.invoke
              interruptible_eval.clj:56 clojure.tools.nrepl.middleware.interruptible-eval/evaluate[fn]
                           AFn.java:159 clojure.lang.AFn.applyToHelper
                           AFn.java:151 clojure.lang.AFn.applyTo
                           core.clj:617 clojure.core/apply
                          core.clj:1788 clojure.core/with-bindings*
                        RestFn.java:425 clojure.lang.RestFn.invoke
              interruptible_eval.clj:41 clojure.tools.nrepl.middleware.interruptible-eval/evaluate
             interruptible_eval.clj:171 clojure.tools.nrepl.middleware.interruptible-eval/interruptible-eval[fn]
                          core.clj:2330 clojure.core/comp[fn]
             interruptible_eval.clj:138 clojure.tools.nrepl.middleware.interruptible-eval/run-next[fn]
                            AFn.java:24 clojure.lang.AFn.run
            ThreadPoolExecutor.java:886 java.util.concurrent.ThreadPoolExecutor$Worker.runTask
            ThreadPoolExecutor.java:908 java.util.concurrent.ThreadPoolExecutor$Worker.run
                        Thread.java:662 java.lang.Thread.run

Digging into ob-clojure.el and I was able to reproduce it in Emacs as:

(defvar expanded "")
(setq expanded "(clojure.pprint/pprint (defn greeting
\"Returns a greeting of the form 'Hello, username.'\"
[username]
(str \"Hello, \" username))
(greeting \"Tom\"))")

(nrepl-send-string-sync
                   expanded
                   (cider-current-ns)
                   (nrepl-current-tooling-session))

Any suggestions on fixing this or it is by design ob-clojure only evaluate one statement?

Thanks.

Bastien

unread,
Jan 23, 2014, 6:36:52 AM1/23/14
to Rui Yang, clo...@googlegroups.com
Hi Rui,

Rui Yang <ryan...@gmail.com> writes:

> Any suggestions on fixing this or it is by design ob-clojure only
> evaluate one statement?

What version of Emacs/Org are you using?

C-h v emacs-version RET
C-h v org-version RET

ob-clojure.el in latest stable release of Org works fine.
The one bundled with current Emacs stable version is not.

HTH,

--
Bastien

Rui Yang

unread,
Jan 23, 2014, 7:21:06 AM1/23/14
to clo...@googlegroups.com, Rui Yang
Thanks for the quick reply.

Emacs version: 24.3.1
Org version: Org-mode version 8.2.4

I actually copied codes of ob-clojure from master to include support for cider (sorry, my bad hack). 
But I found it strange in the org-mode git.
the latest stable tag is 8.2.5g doesn't include supporting of cider? I didn't see the custom variable org-babel-clojure-backend in ob-clojure.el.

But actually the cider support was introduced in 2013-11-04.
and the code appear in ac7fb4dd79ebfa07e3357f012206186bf3923c08 which is the one after the 8.2.5g tag

Sorry if missed sth.

I will try to install org from master and have a try.

Thanks,
Rui

Bastien

unread,
Jan 23, 2014, 7:33:08 AM1/23/14
to Rui Yang, clo...@googlegroups.com
Rui Yang <ryan...@gmail.com> writes:

> Sorry if missed sth.

No, my bad: cider is supported in the master (development)*
branch, not in the latest stable Org release.

> I will try to install org from master and have a try.

~$ git clone git://orgmode.org/org-mode.git
~$ make autoloads

and update your load-path in Emacs.

HTH,

--
Bastien

greg r

unread,
Jan 23, 2014, 10:16:33 AM1/23/14
to clo...@googlegroups.com
We had a discussion about this in the cider-emacs group:

https://groups.google.com/forum/#!topic/cider-emacs/xj-HYTAA-D0

Bastien's page on using Overtone with Clojure in org code blocks is very informative with regards to setting it all up:

http://bzg.fr/emacs-org-babel-overtone-intro.html

Note that the worg page for Clojure is out of date:

http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-clojure.html

This references swank-clojure which is depracated.
I would like to edit this page to bring it up to date with Cider, and I would like to help
as soon as I figure out the process with keys and git!

Good luck with org-mode, cider and Clojure code blocks.  It's a great way to code and experiment, as well
as create dynamic documents.  I've had very good results with the system.

Regards,
Greg

Bastien

unread,
Jan 23, 2014, 10:42:16 AM1/23/14
to greg r, clo...@googlegroups.com
Hi Greg,

greg r <soapy...@comcast.net> writes:

> I would like to edit this page to bring it up to date with Cider, and
> I would like to help
> as soon as I figure out the process with keys and git!

Great -- the steps

1. Create your public key if needed: ~$ ssh-keygen
2. Send ~/.ssh/id_rsa.pub to me
3. Wait for my confirmation that I added you to Worg
4. Clone Worg: ~$ git clone wo...@orgmode.org:worg.git
5. Edit... commit... push!

That's it. Let me know if I can help a bit more,
I'm glad some more people take care of Worg!

Best,

--
Bastien

Matching Socks

unread,
Jan 26, 2014, 4:54:27 PM1/26/14
to clo...@googlegroups.com, greg r
I tried updating org alone, but I evidently caused a mishmash of old and new elisp file versions that didn't work.  So I built Emacs from the trunk branch (emacs-version 24.3.50.1), and blended Org from the master branch to get Cider.  According to package-list-packages, I am using cider 20140125. 

(1) Is it still possible for a Clojure code block to accept an org table as an argument? 

#+tblname: columnar-stuff
| State       | Bird              |
| New York    | Eastern Bluebird  |
| New Jersey  | Eastern Goldfinch |
| Connecticut | Penguin           |

#+name: read-columnar-s(rows=columnar-stuff)
#+begin_src clojure :exports both :results pp
  (let [headings (first rows)]
    (map #(zipmap headings %) (rest rows)))
#+end_src

#+RESULTS: read-columnar-s
: CompilerException java.lang.RuntimeException: Unable to resolve symbol: rows in this context, compiling:(NO_SOURCE_PATH:1:165)

(2) There was a thread last March about emitting nice Org tables from code blocks. (https://groups.google.com/d/msg/clojure/71MEGeYiUKA/tf6rYdfvXrwJ)  Is it still possible to emit a nice Org table from a Clojure code block?  I copied from the March example, but the results section does not look right, and after M-x org-latex-export-to-latex, LaTeX gets stuck on an "Undefined control sequence":

#+begin_src clojure :exports both :results value raw
    (with-out-str (print-table [{:a 1 :b 2 :c 3} {:b 5 :a 7 :c "dog"}]))
#+end_src

#+RESULTS:
"\n| :a |  :c | :b |\n|----+-----+----|\n|  1 |   3 |  2 |\n|  7 | dog |  5 |\n"

(3) I would also like to pretty-print expressions, so the results would fit in LaTeX margins.  How can I do that with ob-clojure?  ":results pp" gives a result reminiscent of the above flattened table.

#+begin_src clojure :exports both :results pp
(def birds [["State","Bird"]
            ["New York", "Eastern Bluebird"]
            ["New Jersey", "Eastern Goldfinch"]
            ["Connecticut", "Penguin"]])
birds
#+end_src

#+RESULTS:
: "[[\"State\" \"Bird\"]\n [\"New York\" \"Eastern Bluebird\"]\n [\"New Jersey\" \"Eastern Goldfinch\"]\n [\"Connecticut\" \"Penguin\"]]\n"

Bastien

unread,
Jan 27, 2014, 5:02:13 AM1/27/14
to Matching Socks, clo...@googlegroups.com, greg r
Hi Phill,

can you repost this on the emacs-orgmode mailing list?
https://lists.gnu.org/mailman/listinfo/emacs-orgmode

I'll follow-up there.

Thanks in advance,

--
Bastien

greg r

unread,
Jan 27, 2014, 9:09:03 PM1/27/14
to clo...@googlegroups.com, greg r
I compared a computer set up with the latest of everything (org/emacs/CIDER) and compared to an older computer still using nrepl-jack-in and older versions of everything else.  The behavior is definitely different with the newer system, and can be seen with a very simple case:

Code block:
#+begin_src clojure :results value raw
[1 2 3 4]
#+end_src

New: (CIDER)
#+RESULTS:
[1 2 3 4]

Old: (nrepl)
#+RESULTS:
| 1 | 2 | 3 | 4 |

It appears the conversion to org table is not happening.

Regards,
Greg

Bastien

unread,
Jan 28, 2014, 12:55:20 AM1/28/14
to greg r, clo...@googlegroups.com
Hi Greg,

greg r <soapy...@comcast.net> writes:

> I compared a computer set up with the latest of everything (org/emacs
> /CIDER) and compared to an older computer still using nrepl-jack-in
> and older versions of everything else. The behavior is definitely
> different with the newer system, and can be seen with a very simple
> case:

Can you report this to the org-mode mailing list?
https://lists.gnu.org/mailman/listinfo/emacs-orgmode

Thanks,

--
Bastien

greg r

unread,
Jan 28, 2014, 6:41:40 AM1/28/14
to clo...@googlegroups.com, greg r
Hi Bastien, yes I will post a report at the mailing list today.

Regards,
Greg

Stuart Sierra

unread,
Jan 28, 2014, 8:35:38 AM1/28/14
to clo...@googlegroups.com
On Thursday, January 23, 2014 2:04:22 AM UTC-5, Rui Yang wrote:
> Trying to use org mode with clojure. I'd like to use cider as the
> REPL server. Things is fine if I have only one statement in org
> source block. If I have more than one, then I got exception.

Don't know if it's relevant or helpful here, but here's my Emacs org / babel / Clojure setup:

https://github.com/stuartsierra/dotfiles/blob/139083393bf4e2025b15a50737971fdf423a9a23/.emacs.d/local/init.el#L313

-S

John Mastro

unread,
Jan 28, 2014, 9:56:45 PM1/28/14
to clo...@googlegroups.com
Stuart Sierra <the.stua...@gmail.com> wrote:
> Don't know if it's relevant or helpful here, but here's my Emacs org / babel / Clojure setup:

Thanks Stuart, very useful indeed (for me anyway).

- John

greg r

unread,
Feb 2, 2014, 5:29:24 PM2/2/14
to clo...@googlegroups.com
The worg documentation for the Clojure language has been updated:
http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-clojure.html

The installation instructions should result in a current Clojure/CIDER/clojure-mode/Leiningen system.

Regards,
Greg
Reply all
Reply to author
Forward
0 new messages