42

I'm just starting with Clojure and can't access to the doc function.

I'm using clojure 1.3 with emacs24 and swank-clojure.

user> *clojure-version*
{:major 1, :minor 3, :incremental 0, :qualifier nil}

But when I try:

(doc doc)

I get:

Unable to resolve symbol: doc in this context
[Thrown class java.lang.RuntimeException]

I've read Why does REPL treat clojure.core/doc as a var? and as suggested:

(clojure.repl/doc doc)

But then, I receive:

clojure.repl
[Thrown class java.lang.ClassNotFoundException]

It seems I am not "importing" the usual namespaces, but really doesn't know how to do it.

Thanks.

UPDATE

Using clojure from java (java -jar ...) it works well, so it's a problem with the emacs setup.

Community
  • 1
  • 1
zaforas
  • 423
  • 1
  • 4
  • 6
  • `(doc doc)` works fine on my Clojure run as `java -jar clojure-1.3.0.jar`. Did you try to use it this way? – Jan Nov 30 '11 at 20:44
  • Yes, using clojure directly from java works well, so it seems it's a problem with my emacs setup. Thanks for answering. – zaforas Nov 30 '11 at 20:46
  • 1
    If you're using SLIME from Emacs, the clojure.repl stuff isn't loaded into your REPL because SLIME itself already provides equivalents - for example, you can get docs with [C-c C-d d](http://common-lisp.net/project/slime/doc/html/Documentation.html#Documentation) – Paul Legato Oct 13 '12 at 03:44

3 Answers3

71

You need to grab the clojure.repl namespace one way or another:

From the REPL

user> (use 'clojure.repl)
user> (doc doc)

or in your program

(ns foobar
  (:use [clojure.repl]))
Julien Chastang
  • 17,592
  • 12
  • 63
  • 89
16

Add the following to your Leiningen user.clj file (on Mac / Linux, it's ~/.lein/user.clj):

;; ~/.lein/user.clj
(if (>= (.compareTo (clojure-version) "1.3.0") 0)
  (do (use 'clojure.repl)
      (use 'clojure.java.javadoc)))

This will cause Leiningen to automatically import those two namespaces at startup for projects using Clojure 1.3.0 and later (but not for projects using Clojure 1.2.1 or earlier - where doc and source were always available).

Credit goes to Matthew Boston for this. Note also Phil Hagelberg's reply which points out most of the REPL-specific functionality is accessible directly in Emacs / Slime without needing the functions directly in the REPL.

Sean Corfield
  • 6,297
  • 22
  • 31
  • 1
    There is a typo in Phil's reply: the correct SLIME docs command is [C-c C-d d](http://common-lisp.net/project/slime/doc/html/Documentation.html#Documentation). (Also, it may save someone some frustration to know that user.clj has been deprecated in recent versions of Leiningen.) – Paul Legato Oct 13 '12 at 03:40
  • Running [cider](https://github.com/clojure-emacs/cider) with `*clojure-version*` `{:major 1, :minor 5, :incremental 1, :qualifier nil}` and only `C-c C-d` is needed to start entering the function name to get help on. Actually, this was described at the bottom of this [tutorial](http://clojure-doc.org/articles/tutorials/emacs.html) but was prefaced with the older, no longer working, way of getting the doc. – nymo Dec 24 '13 at 14:03
4

As of Lein 2, namespaces can be automatically imported at startup using :injections, e.g:

;; ~/.lein/profiles.clj
{:user {:plugins [[lein-swank "1.4.4"]
                  [lein-noir "1.2.1"]
                  [lein-pprint "1.1.1"]]
       :injections [(use 'clojure.repl)
                    (use 'clojure.java.javadoc)
                    (use 'clojure.pprint)] }}

But see other responses for SLIME equivalents.

I'm unsure of when this became the case, but as of lein 2.2 doc is available at the repl by default.

Joffer
  • 1,921
  • 2
  • 21
  • 23